Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 1 | #ifndef _LINUX_CONTEXT_TRACKING_H |
| 2 | #define _LINUX_CONTEXT_TRACKING_H |
| 3 | |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 4 | #include <linux/sched.h> |
Frederic Weisbecker | 95a79fd | 2013-01-07 18:12:14 +0100 | [diff] [blame] | 5 | #include <linux/percpu.h> |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame^] | 6 | #include <asm/ptrace.h> |
Frederic Weisbecker | 95a79fd | 2013-01-07 18:12:14 +0100 | [diff] [blame] | 7 | |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame^] | 8 | #ifdef CONFIG_CONTEXT_TRACKING |
Frederic Weisbecker | 95a79fd | 2013-01-07 18:12:14 +0100 | [diff] [blame] | 9 | struct 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; |
| 17 | enum { |
| 18 | IN_KERNEL = 0, |
| 19 | IN_USER, |
| 20 | } state; |
| 21 | }; |
| 22 | |
| 23 | DECLARE_PER_CPU(struct context_tracking, context_tracking); |
| 24 | |
| 25 | static inline bool context_tracking_in_user(void) |
| 26 | { |
| 27 | return __this_cpu_read(context_tracking.state) == IN_USER; |
| 28 | } |
| 29 | |
| 30 | static inline bool context_tracking_active(void) |
| 31 | { |
| 32 | return __this_cpu_read(context_tracking.active); |
| 33 | } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 34 | |
| 35 | extern void user_enter(void); |
| 36 | extern void user_exit(void); |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame^] | 37 | |
| 38 | static inline void exception_enter(struct pt_regs *regs) |
| 39 | { |
| 40 | user_exit(); |
| 41 | } |
| 42 | |
| 43 | static inline void exception_exit(struct pt_regs *regs) |
| 44 | { |
| 45 | if (user_mode(regs)) |
| 46 | user_enter(); |
| 47 | } |
| 48 | |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 49 | extern void context_tracking_task_switch(struct task_struct *prev, |
| 50 | struct task_struct *next); |
| 51 | #else |
Frederic Weisbecker | 95a79fd | 2013-01-07 18:12:14 +0100 | [diff] [blame] | 52 | static inline bool context_tracking_in_user(void) { return false; } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 53 | static inline void user_enter(void) { } |
| 54 | static inline void user_exit(void) { } |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame^] | 55 | static inline void exception_enter(struct pt_regs *regs) { } |
| 56 | static inline void exception_exit(struct pt_regs *regs) { } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 57 | static inline void context_tracking_task_switch(struct task_struct *prev, |
| 58 | struct task_struct *next) { } |
| 59 | #endif /* !CONFIG_CONTEXT_TRACKING */ |
| 60 | |
| 61 | #endif |