]>
Commit | Line | Data |
---|---|---|
91d1aa43 FW |
1 | #ifndef _LINUX_CONTEXT_TRACKING_H |
2 | #define _LINUX_CONTEXT_TRACKING_H | |
3 | ||
91d1aa43 | 4 | #include <linux/sched.h> |
95a79fd4 | 5 | #include <linux/percpu.h> |
521921ba | 6 | #include <linux/vtime.h> |
56dd9470 | 7 | #include <asm/ptrace.h> |
95a79fd4 FW |
8 | |
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; | |
6c1e0256 | 17 | enum ctx_state { |
95a79fd4 FW |
18 | IN_KERNEL = 0, |
19 | IN_USER, | |
20 | } state; | |
21 | }; | |
22 | ||
521921ba | 23 | |
6c1e0256 | 24 | #ifdef CONFIG_CONTEXT_TRACKING |
95a79fd4 FW |
25 | DECLARE_PER_CPU(struct context_tracking, context_tracking); |
26 | ||
27 | static inline bool context_tracking_in_user(void) | |
28 | { | |
29 | return __this_cpu_read(context_tracking.state) == IN_USER; | |
30 | } | |
31 | ||
32 | static inline bool context_tracking_active(void) | |
33 | { | |
34 | return __this_cpu_read(context_tracking.active); | |
35 | } | |
91d1aa43 FW |
36 | |
37 | extern void user_enter(void); | |
38 | extern void user_exit(void); | |
56dd9470 | 39 | |
6c1e0256 | 40 | static inline enum ctx_state exception_enter(void) |
56dd9470 | 41 | { |
6c1e0256 FW |
42 | enum ctx_state prev_ctx; |
43 | ||
44 | prev_ctx = this_cpu_read(context_tracking.state); | |
56dd9470 | 45 | user_exit(); |
6c1e0256 FW |
46 | |
47 | return prev_ctx; | |
56dd9470 FW |
48 | } |
49 | ||
6c1e0256 | 50 | static inline void exception_exit(enum ctx_state prev_ctx) |
56dd9470 | 51 | { |
6c1e0256 | 52 | if (prev_ctx == IN_USER) |
56dd9470 FW |
53 | user_enter(); |
54 | } | |
55 | ||
91d1aa43 FW |
56 | extern void context_tracking_task_switch(struct task_struct *prev, |
57 | struct task_struct *next); | |
58 | #else | |
95a79fd4 | 59 | static inline bool context_tracking_in_user(void) { return false; } |
91d1aa43 FW |
60 | static inline void user_enter(void) { } |
61 | static inline void user_exit(void) { } | |
2d854e57 FW |
62 | static inline enum ctx_state exception_enter(void) { return 0; } |
63 | static inline void exception_exit(enum ctx_state prev_ctx) { } | |
64 | static inline void context_tracking_task_switch(struct task_struct *prev, | |
65 | struct task_struct *next) { } | |
66 | #endif /* !CONFIG_CONTEXT_TRACKING */ | |
521921ba | 67 | |
2d854e57 FW |
68 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
69 | extern void guest_enter(void); | |
70 | extern void guest_exit(void); | |
71 | #else | |
521921ba FW |
72 | static inline void guest_enter(void) |
73 | { | |
2d854e57 FW |
74 | /* |
75 | * This is running in ioctl context so we can avoid | |
76 | * the call to vtime_account() with its unnecessary idle check. | |
77 | */ | |
78 | vtime_account_system(current); | |
79 | current->flags |= PF_VCPU; | |
521921ba FW |
80 | } |
81 | ||
82 | static inline void guest_exit(void) | |
83 | { | |
2d854e57 FW |
84 | /* |
85 | * This is running in ioctl context so we can avoid | |
86 | * the call to vtime_account() with its unnecessary idle check. | |
87 | */ | |
88 | vtime_account_system(current); | |
89 | current->flags &= ~PF_VCPU; | |
521921ba | 90 | } |
2d854e57 | 91 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ |
91d1aa43 FW |
92 | |
93 | #endif |