1 #ifndef _LINUX_CONTEXT_TRACKING_H
2 #define _LINUX_CONTEXT_TRACKING_H
4 #include <linux/sched.h>
5 #include <linux/vtime.h>
6 #include <linux/context_tracking_state.h>
7 #include <asm/ptrace.h>
10 #ifdef CONFIG_CONTEXT_TRACKING
11 extern void context_tracking_cpu_set(int cpu);
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);
17 extern void context_tracking_enter(enum ctx_state state);
18 extern void context_tracking_exit(enum ctx_state state);
19 extern void context_tracking_user_enter(void);
20 extern void context_tracking_user_exit(void);
22 static inline void user_enter(void)
24 if (context_tracking_is_enabled())
25 context_tracking_enter(CONTEXT_USER);
28 static inline void user_exit(void)
30 if (context_tracking_is_enabled())
31 context_tracking_exit(CONTEXT_USER);
34 /* Called with interrupts disabled. */
35 static inline void user_enter_irqoff(void)
37 if (context_tracking_is_enabled())
38 __context_tracking_enter(CONTEXT_USER);
41 static inline void user_exit_irqoff(void)
43 if (context_tracking_is_enabled())
44 __context_tracking_exit(CONTEXT_USER);
47 static inline enum ctx_state exception_enter(void)
49 enum ctx_state prev_ctx;
51 if (!context_tracking_is_enabled())
54 prev_ctx = this_cpu_read(context_tracking.state);
55 if (prev_ctx != CONTEXT_KERNEL)
56 context_tracking_exit(prev_ctx);
61 static inline void exception_exit(enum ctx_state prev_ctx)
63 if (context_tracking_is_enabled()) {
64 if (prev_ctx != CONTEXT_KERNEL)
65 context_tracking_enter(prev_ctx);
71 * ct_state() - return the current context tracking state if known
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.
77 static inline enum ctx_state ct_state(void)
79 return context_tracking_is_enabled() ?
80 this_cpu_read(context_tracking.state) : CONTEXT_DISABLED;
83 static inline void user_enter(void) { }
84 static inline void user_exit(void) { }
85 static inline void user_enter_irqoff(void) { }
86 static inline void user_exit_irqoff(void) { }
87 static inline enum ctx_state exception_enter(void) { return 0; }
88 static inline void exception_exit(enum ctx_state prev_ctx) { }
89 static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; }
90 #endif /* !CONFIG_CONTEXT_TRACKING */
92 #define CT_WARN_ON(cond) WARN_ON(context_tracking_is_enabled() && (cond))
94 #ifdef CONFIG_CONTEXT_TRACKING_FORCE
95 extern void context_tracking_init(void);
97 static inline void context_tracking_init(void) { }
98 #endif /* CONFIG_CONTEXT_TRACKING_FORCE */
101 #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
102 /* must be called with irqs disabled */
103 static inline void guest_enter_irqoff(void)
105 if (vtime_accounting_cpu_enabled())
106 vtime_guest_enter(current);
108 current->flags |= PF_VCPU;
110 if (context_tracking_is_enabled())
111 __context_tracking_enter(CONTEXT_GUEST);
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.
120 if (!context_tracking_cpu_is_enabled())
121 rcu_virt_note_context_switch(smp_processor_id());
124 static inline void guest_exit_irqoff(void)
126 if (context_tracking_is_enabled())
127 __context_tracking_exit(CONTEXT_GUEST);
129 if (vtime_accounting_cpu_enabled())
130 vtime_guest_exit(current);
132 current->flags &= ~PF_VCPU;
136 static inline void guest_enter_irqoff(void)
139 * This is running in ioctl context so its safe
140 * to assume that it's the stime pending cputime
143 vtime_account_system(current);
144 current->flags |= PF_VCPU;
145 rcu_virt_note_context_switch(smp_processor_id());
148 static inline void guest_exit_irqoff(void)
150 /* Flush the guest cputime we spent on the guest */
151 vtime_account_system(current);
152 current->flags &= ~PF_VCPU;
154 #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
156 static inline void guest_enter(void)
160 local_irq_save(flags);
161 guest_enter_irqoff();
162 local_irq_restore(flags);
165 static inline void guest_exit(void)
169 local_irq_save(flags);
171 local_irq_restore(flags);