blob: fc09d7b0dacfc00feef340cc03c06b207feed870 [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 Weisbecker95a79fd2013-01-07 18:12:14 +01005#include <linux/percpu.h>
Frederic Weisbecker521921b2013-05-16 01:21:38 +02006#include <linux/vtime.h>
Frederic Weisbecker56dd9472013-02-24 00:23:25 +01007#include <asm/ptrace.h>
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +01008
9struct context_tracking {
10 /*
11 * When active is false, probes are unset in order
12 * to minimize overhead: TIF flags are cleared
13 * and calls to user_enter/exit are ignored. This
14 * may be further optimized using static keys.
15 */
16 bool active;
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010017 enum ctx_state {
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010018 IN_KERNEL = 0,
19 IN_USER,
20 } state;
21};
22
Frederic Weisbecker521921b2013-05-16 01:21:38 +020023static inline void __guest_enter(void)
24{
25 /*
26 * This is running in ioctl context so we can avoid
27 * the call to vtime_account() with its unnecessary idle check.
28 */
29 vtime_account_system(current);
30 current->flags |= PF_VCPU;
31}
32
33static inline void __guest_exit(void)
34{
35 /*
36 * This is running in ioctl context so we can avoid
37 * the call to vtime_account() with its unnecessary idle check.
38 */
39 vtime_account_system(current);
40 current->flags &= ~PF_VCPU;
41}
42
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010043#ifdef CONFIG_CONTEXT_TRACKING
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010044DECLARE_PER_CPU(struct context_tracking, context_tracking);
45
46static inline bool context_tracking_in_user(void)
47{
48 return __this_cpu_read(context_tracking.state) == IN_USER;
49}
50
51static inline bool context_tracking_active(void)
52{
53 return __this_cpu_read(context_tracking.active);
54}
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010055
56extern void user_enter(void);
57extern void user_exit(void);
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010058
Frederic Weisbecker521921b2013-05-16 01:21:38 +020059extern void guest_enter(void);
60extern void guest_exit(void);
61
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010062static inline enum ctx_state exception_enter(void)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010063{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010064 enum ctx_state prev_ctx;
65
66 prev_ctx = this_cpu_read(context_tracking.state);
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010067 user_exit();
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010068
69 return prev_ctx;
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010070}
71
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010072static inline void exception_exit(enum ctx_state prev_ctx)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010073{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010074 if (prev_ctx == IN_USER)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010075 user_enter();
76}
77
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010078extern void context_tracking_task_switch(struct task_struct *prev,
79 struct task_struct *next);
80#else
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010081static inline bool context_tracking_in_user(void) { return false; }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010082static inline void user_enter(void) { }
83static inline void user_exit(void) { }
Frederic Weisbecker521921b2013-05-16 01:21:38 +020084
85static inline void guest_enter(void)
86{
87 __guest_enter();
88}
89
90static inline void guest_exit(void)
91{
92 __guest_exit();
93}
94
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010095static inline enum ctx_state exception_enter(void) { return 0; }
96static inline void exception_exit(enum ctx_state prev_ctx) { }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010097static inline void context_tracking_task_switch(struct task_struct *prev,
98 struct task_struct *next) { }
99#endif /* !CONFIG_CONTEXT_TRACKING */
100
101#endif