Commit | Line | Data |
---|---|---|
91d1aa43 FW |
1 | #ifndef _LINUX_CONTEXT_TRACKING_H |
2 | #define _LINUX_CONTEXT_TRACKING_H | |
3 | ||
91d1aa43 | 4 | #include <linux/sched.h> |
521921ba | 5 | #include <linux/vtime.h> |
e7358b3b | 6 | #include <linux/context_tracking_state.h> |
56dd9470 | 7 | #include <asm/ptrace.h> |
95a79fd4 | 8 | |
521921ba | 9 | |
6c1e0256 | 10 | #ifdef CONFIG_CONTEXT_TRACKING |
2e709338 FW |
11 | extern void context_tracking_cpu_set(int cpu); |
12 | ||
d0e536d8 PB |
13 | /* Called with interrupts disabled. */ |
14 | extern void __context_tracking_enter(enum ctx_state state); | |
15 | extern void __context_tracking_exit(enum ctx_state state); | |
16 | ||
3aab4f50 RR |
17 | extern void context_tracking_enter(enum ctx_state state); |
18 | extern void context_tracking_exit(enum ctx_state state); | |
ad65782f FW |
19 | extern void context_tracking_user_enter(void); |
20 | extern void context_tracking_user_exit(void); | |
21 | ||
22 | static inline void user_enter(void) | |
23 | { | |
58135f57 | 24 | if (context_tracking_is_enabled()) |
f70cd6b0 | 25 | context_tracking_enter(CONTEXT_USER); |
ad65782f FW |
26 | |
27 | } | |
28 | static inline void user_exit(void) | |
29 | { | |
58135f57 | 30 | if (context_tracking_is_enabled()) |
f70cd6b0 | 31 | context_tracking_exit(CONTEXT_USER); |
ad65782f | 32 | } |
56dd9470 | 33 | |
2e9d1e15 PB |
34 | /* Called with interrupts disabled. */ |
35 | static inline void user_enter_irqoff(void) | |
36 | { | |
37 | if (context_tracking_is_enabled()) | |
38 | __context_tracking_enter(CONTEXT_USER); | |
39 | ||
40 | } | |
41 | static inline void user_exit_irqoff(void) | |
42 | { | |
43 | if (context_tracking_is_enabled()) | |
44 | __context_tracking_exit(CONTEXT_USER); | |
45 | } | |
46 | ||
6c1e0256 | 47 | static inline enum ctx_state exception_enter(void) |
56dd9470 | 48 | { |
6c1e0256 FW |
49 | enum ctx_state prev_ctx; |
50 | ||
58135f57 | 51 | if (!context_tracking_is_enabled()) |
ad65782f FW |
52 | return 0; |
53 | ||
6c1e0256 | 54 | prev_ctx = this_cpu_read(context_tracking.state); |
3aab4f50 RR |
55 | if (prev_ctx != CONTEXT_KERNEL) |
56 | context_tracking_exit(prev_ctx); | |
6c1e0256 FW |
57 | |
58 | return prev_ctx; | |
56dd9470 FW |
59 | } |
60 | ||
6c1e0256 | 61 | static inline void exception_exit(enum ctx_state prev_ctx) |
56dd9470 | 62 | { |
58135f57 | 63 | if (context_tracking_is_enabled()) { |
3aab4f50 RR |
64 | if (prev_ctx != CONTEXT_KERNEL) |
65 | context_tracking_enter(prev_ctx); | |
ad65782f | 66 | } |
56dd9470 FW |
67 | } |
68 | ||
f9281648 AL |
69 | |
70 | /** | |
71 | * ct_state() - return the current context tracking state if known | |
72 | * | |
73 | * Returns the current cpu's context tracking state if context tracking | |
74 | * is enabled. If context tracking is disabled, returns | |
75 | * CONTEXT_DISABLED. This should be used primarily for debugging. | |
76 | */ | |
77 | static inline enum ctx_state ct_state(void) | |
78 | { | |
79 | return context_tracking_is_enabled() ? | |
80 | this_cpu_read(context_tracking.state) : CONTEXT_DISABLED; | |
81 | } | |
91d1aa43 FW |
82 | #else |
83 | static inline void user_enter(void) { } | |
84 | static inline void user_exit(void) { } | |
2e9d1e15 PB |
85 | static inline void user_enter_irqoff(void) { } |
86 | static inline void user_exit_irqoff(void) { } | |
2d854e57 FW |
87 | static inline enum ctx_state exception_enter(void) { return 0; } |
88 | static inline void exception_exit(enum ctx_state prev_ctx) { } | |
f9281648 | 89 | static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; } |
2d854e57 | 90 | #endif /* !CONFIG_CONTEXT_TRACKING */ |
521921ba | 91 | |
f9281648 | 92 | #define CT_WARN_ON(cond) WARN_ON(context_tracking_is_enabled() && (cond)) |
65f382fd FW |
93 | |
94 | #ifdef CONFIG_CONTEXT_TRACKING_FORCE | |
95 | extern void context_tracking_init(void); | |
96 | #else | |
97 | static inline void context_tracking_init(void) { } | |
98 | #endif /* CONFIG_CONTEXT_TRACKING_FORCE */ | |
99 | ||
100 | ||
2d854e57 | 101 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
ebaac173 PB |
102 | /* must be called with irqs disabled */ |
103 | static inline void guest_enter_irqoff(void) | |
48d6a816 | 104 | { |
55dbdcfa | 105 | if (vtime_accounting_cpu_enabled()) |
48d6a816 FW |
106 | vtime_guest_enter(current); |
107 | else | |
108 | current->flags |= PF_VCPU; | |
126a6a54 RR |
109 | |
110 | if (context_tracking_is_enabled()) | |
d0e536d8 | 111 | __context_tracking_enter(CONTEXT_GUEST); |
ebaac173 PB |
112 | |
113 | /* KVM does not hold any references to rcu protected data when it | |
114 | * switches CPU into a guest mode. In fact switching to a guest mode | |
115 | * is very similar to exiting to userspace from rcu point of view. In | |
116 | * addition CPU may stay in a guest mode for quite a long time (up to | |
117 | * one time slice). Lets treat guest mode as quiescent state, just like | |
118 | * we do with user-mode execution. | |
119 | */ | |
120 | if (!context_tracking_cpu_is_enabled()) | |
121 | rcu_virt_note_context_switch(smp_processor_id()); | |
48d6a816 FW |
122 | } |
123 | ||
ebaac173 | 124 | static inline void guest_exit_irqoff(void) |
48d6a816 | 125 | { |
126a6a54 | 126 | if (context_tracking_is_enabled()) |
d0e536d8 | 127 | __context_tracking_exit(CONTEXT_GUEST); |
126a6a54 | 128 | |
55dbdcfa | 129 | if (vtime_accounting_cpu_enabled()) |
48d6a816 FW |
130 | vtime_guest_exit(current); |
131 | else | |
132 | current->flags &= ~PF_VCPU; | |
133 | } | |
73d424f9 | 134 | |
2d854e57 | 135 | #else |
ebaac173 | 136 | static inline void guest_enter_irqoff(void) |
521921ba | 137 | { |
2d854e57 | 138 | /* |
5b206d48 FW |
139 | * This is running in ioctl context so its safe |
140 | * to assume that it's the stime pending cputime | |
141 | * to flush. | |
2d854e57 FW |
142 | */ |
143 | vtime_account_system(current); | |
144 | current->flags |= PF_VCPU; | |
ebaac173 | 145 | rcu_virt_note_context_switch(smp_processor_id()); |
521921ba FW |
146 | } |
147 | ||
ebaac173 | 148 | static inline void guest_exit_irqoff(void) |
521921ba | 149 | { |
5b206d48 | 150 | /* Flush the guest cputime we spent on the guest */ |
2d854e57 FW |
151 | vtime_account_system(current); |
152 | current->flags &= ~PF_VCPU; | |
521921ba | 153 | } |
2d854e57 | 154 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ |
91d1aa43 | 155 | |
ebaac173 PB |
156 | static inline void guest_enter(void) |
157 | { | |
158 | unsigned long flags; | |
159 | ||
160 | local_irq_save(flags); | |
161 | guest_enter_irqoff(); | |
162 | local_irq_restore(flags); | |
163 | } | |
164 | ||
165 | static inline void guest_exit(void) | |
166 | { | |
167 | unsigned long flags; | |
168 | ||
169 | local_irq_save(flags); | |
170 | guest_exit_irqoff(); | |
171 | local_irq_restore(flags); | |
172 | } | |
173 | ||
91d1aa43 | 174 | #endif |