blob: 37b81bd51ec0e48ce7ec9c24608aeca3ca92ad36 [file] [log] [blame]
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01001#ifndef _LINUX_CONTEXT_TRACKING_H
2#define _LINUX_CONTEXT_TRACKING_H
3
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01004#include <linux/sched.h>
Frederic Weisbecker521921b2013-05-16 01:21:38 +02005#include <linux/vtime.h>
Frederic Weisbeckere7358b32013-07-12 02:15:49 +02006#include <linux/context_tracking_state.h>
Frederic Weisbecker56dd9472013-02-24 00:23:25 +01007#include <asm/ptrace.h>
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +01008
Frederic Weisbecker521921b2013-05-16 01:21:38 +02009
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010010#ifdef CONFIG_CONTEXT_TRACKING
Frederic Weisbecker2e709332013-07-10 00:55:25 +020011extern void context_tracking_cpu_set(int cpu);
12
Frederic Weisbeckerad657822013-07-10 02:44:35 +020013extern void context_tracking_user_enter(void);
14extern void context_tracking_user_exit(void);
Frederic Weisbecker73d424f2013-07-11 19:42:13 +020015extern void __context_tracking_task_switch(struct task_struct *prev,
16 struct task_struct *next);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020017
18static inline void user_enter(void)
19{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010020 if (context_tracking_is_enabled())
Frederic Weisbeckerad657822013-07-10 02:44:35 +020021 context_tracking_user_enter();
22
23}
24static inline void user_exit(void)
25{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010026 if (context_tracking_is_enabled())
Frederic Weisbeckerad657822013-07-10 02:44:35 +020027 context_tracking_user_exit();
28}
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010029
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010030static inline enum ctx_state exception_enter(void)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010031{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010032 enum ctx_state prev_ctx;
33
Frederic Weisbecker58135f52013-11-06 14:45:57 +010034 if (!context_tracking_is_enabled())
Frederic Weisbeckerad657822013-07-10 02:44:35 +020035 return 0;
36
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010037 prev_ctx = this_cpu_read(context_tracking.state);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020038 context_tracking_user_exit();
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010039
40 return prev_ctx;
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010041}
42
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010043static inline void exception_exit(enum ctx_state prev_ctx)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010044{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010045 if (context_tracking_is_enabled()) {
Frederic Weisbeckerad657822013-07-10 02:44:35 +020046 if (prev_ctx == IN_USER)
47 context_tracking_user_enter();
48 }
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010049}
50
Frederic Weisbecker73d424f2013-07-11 19:42:13 +020051static inline void context_tracking_task_switch(struct task_struct *prev,
52 struct task_struct *next)
53{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010054 if (context_tracking_is_enabled())
Frederic Weisbecker73d424f2013-07-11 19:42:13 +020055 __context_tracking_task_switch(prev, next);
56}
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010057#else
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010058static inline void user_enter(void) { }
59static inline void user_exit(void) { }
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010060static inline enum ctx_state exception_enter(void) { return 0; }
61static inline void exception_exit(enum ctx_state prev_ctx) { }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010062static inline void context_tracking_task_switch(struct task_struct *prev,
63 struct task_struct *next) { }
64#endif /* !CONFIG_CONTEXT_TRACKING */
65
Frederic Weisbecker65f382f2013-07-11 19:12:32 +020066
67#ifdef CONFIG_CONTEXT_TRACKING_FORCE
68extern void context_tracking_init(void);
69#else
70static inline void context_tracking_init(void) { }
71#endif /* CONFIG_CONTEXT_TRACKING_FORCE */
72
73
Frederic Weisbecker2d854e52013-07-12 19:02:30 +020074#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Frederic Weisbecker48d6a812013-07-10 02:44:35 +020075static inline void guest_enter(void)
76{
Frederic Weisbeckerb0493402013-07-12 03:10:15 +020077 if (vtime_accounting_enabled())
Frederic Weisbecker48d6a812013-07-10 02:44:35 +020078 vtime_guest_enter(current);
79 else
80 current->flags |= PF_VCPU;
81}
82
83static inline void guest_exit(void)
84{
Frederic Weisbeckerb0493402013-07-12 03:10:15 +020085 if (vtime_accounting_enabled())
Frederic Weisbecker48d6a812013-07-10 02:44:35 +020086 vtime_guest_exit(current);
87 else
88 current->flags &= ~PF_VCPU;
89}
Frederic Weisbecker73d424f2013-07-11 19:42:13 +020090
Frederic Weisbecker2d854e52013-07-12 19:02:30 +020091#else
92static inline void guest_enter(void)
93{
94 /*
Frederic Weisbecker5b206d42013-07-12 19:05:14 +020095 * This is running in ioctl context so its safe
96 * to assume that it's the stime pending cputime
97 * to flush.
Frederic Weisbecker2d854e52013-07-12 19:02:30 +020098 */
99 vtime_account_system(current);
100 current->flags |= PF_VCPU;
101}
102
103static inline void guest_exit(void)
104{
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200105 /* Flush the guest cputime we spent on the guest */
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200106 vtime_account_system(current);
107 current->flags &= ~PF_VCPU;
108}
109#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
110
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100111#endif