blob: 5984f2556d134d512738d57f37ebdbb594f0d09f [file] [log] [blame]
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01001#ifndef _LINUX_CONTEXT_TRACKING_H
2#define _LINUX_CONTEXT_TRACKING_H
3
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +01004#include <linux/sched.h>
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +01005#include <linux/percpu.h>
Frederic Weisbecker521921b2013-05-16 01:21:38 +02006#include <linux/vtime.h>
Frederic Weisbecker56dd9472013-02-24 00:23:25 +01007#include <asm/ptrace.h>
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +01008
9struct 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;
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010017 enum ctx_state {
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010018 IN_KERNEL = 0,
19 IN_USER,
20 } state;
21};
22
Frederic Weisbecker521921b2013-05-16 01:21:38 +020023
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010024#ifdef CONFIG_CONTEXT_TRACKING
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010025DECLARE_PER_CPU(struct context_tracking, context_tracking);
26
27static inline bool context_tracking_in_user(void)
28{
29 return __this_cpu_read(context_tracking.state) == IN_USER;
30}
31
32static inline bool context_tracking_active(void)
33{
34 return __this_cpu_read(context_tracking.active);
35}
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010036
37extern void user_enter(void);
38extern void user_exit(void);
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010039
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010040static inline enum ctx_state exception_enter(void)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010041{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010042 enum ctx_state prev_ctx;
43
44 prev_ctx = this_cpu_read(context_tracking.state);
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010045 user_exit();
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010046
47 return prev_ctx;
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010048}
49
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010050static inline void exception_exit(enum ctx_state prev_ctx)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010051{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010052 if (prev_ctx == IN_USER)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010053 user_enter();
54}
55
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010056extern void context_tracking_task_switch(struct task_struct *prev,
57 struct task_struct *next);
58#else
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +010059static inline bool context_tracking_in_user(void) { return false; }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010060static inline void user_enter(void) { }
61static inline void user_exit(void) { }
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010062static inline enum ctx_state exception_enter(void) { return 0; }
63static inline void exception_exit(enum ctx_state prev_ctx) { }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010064static inline void context_tracking_task_switch(struct task_struct *prev,
65 struct task_struct *next) { }
66#endif /* !CONFIG_CONTEXT_TRACKING */
67
Frederic Weisbecker2d854e52013-07-12 19:02:30 +020068#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
69extern void guest_enter(void);
70extern void guest_exit(void);
71#else
72static inline void guest_enter(void)
73{
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;
80}
81
82static inline void guest_exit(void)
83{
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;
90}
91#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
92
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010093#endif