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 | 36 | |
2e709338 FW |
37 | extern void context_tracking_cpu_set(int cpu); |
38 | ||
91d1aa43 FW |
39 | extern void user_enter(void); |
40 | extern void user_exit(void); | |
56dd9470 | 41 | |
6c1e0256 | 42 | static inline enum ctx_state exception_enter(void) |
56dd9470 | 43 | { |
6c1e0256 FW |
44 | enum ctx_state prev_ctx; |
45 | ||
46 | prev_ctx = this_cpu_read(context_tracking.state); | |
56dd9470 | 47 | user_exit(); |
6c1e0256 FW |
48 | |
49 | return prev_ctx; | |
56dd9470 FW |
50 | } |
51 | ||
6c1e0256 | 52 | static inline void exception_exit(enum ctx_state prev_ctx) |
56dd9470 | 53 | { |
6c1e0256 | 54 | if (prev_ctx == IN_USER) |
56dd9470 FW |
55 | user_enter(); |
56 | } | |
57 | ||
91d1aa43 FW |
58 | extern void context_tracking_task_switch(struct task_struct *prev, |
59 | struct task_struct *next); | |
60 | #else | |
95a79fd4 | 61 | static inline bool context_tracking_in_user(void) { return false; } |
91d1aa43 FW |
62 | static inline void user_enter(void) { } |
63 | static inline void user_exit(void) { } | |
2d854e57 FW |
64 | static inline enum ctx_state exception_enter(void) { return 0; } |
65 | static inline void exception_exit(enum ctx_state prev_ctx) { } | |
66 | static inline void context_tracking_task_switch(struct task_struct *prev, | |
67 | struct task_struct *next) { } | |
68 | #endif /* !CONFIG_CONTEXT_TRACKING */ | |
521921ba | 69 | |
2d854e57 FW |
70 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
71 | extern void guest_enter(void); | |
72 | extern void guest_exit(void); | |
73 | #else | |
521921ba FW |
74 | static inline void guest_enter(void) |
75 | { | |
2d854e57 | 76 | /* |
5b206d48 FW |
77 | * This is running in ioctl context so its safe |
78 | * to assume that it's the stime pending cputime | |
79 | * to flush. | |
2d854e57 FW |
80 | */ |
81 | vtime_account_system(current); | |
82 | current->flags |= PF_VCPU; | |
521921ba FW |
83 | } |
84 | ||
85 | static inline void guest_exit(void) | |
86 | { | |
5b206d48 | 87 | /* Flush the guest cputime we spent on the guest */ |
2d854e57 FW |
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 |