Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 1 | #ifndef _LINUX_CONTEXT_TRACKING_H |
| 2 | #define _LINUX_CONTEXT_TRACKING_H |
| 3 | |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 4 | #include <linux/sched.h> |
Frederic Weisbecker | 521921b | 2013-05-16 01:21:38 +0200 | [diff] [blame] | 5 | #include <linux/vtime.h> |
Frederic Weisbecker | e7358b3 | 2013-07-12 02:15:49 +0200 | [diff] [blame] | 6 | #include <linux/context_tracking_state.h> |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 7 | #include <asm/ptrace.h> |
Frederic Weisbecker | 95a79fd | 2013-01-07 18:12:14 +0100 | [diff] [blame] | 8 | |
Frederic Weisbecker | 521921b | 2013-05-16 01:21:38 +0200 | [diff] [blame] | 9 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 10 | #ifdef CONFIG_CONTEXT_TRACKING |
Frederic Weisbecker | 2e70933 | 2013-07-10 00:55:25 +0200 | [diff] [blame] | 11 | extern void context_tracking_cpu_set(int cpu); |
| 12 | |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 13 | extern void context_tracking_user_enter(void); |
| 14 | extern void context_tracking_user_exit(void); |
Frederic Weisbecker | 73d424f | 2013-07-11 19:42:13 +0200 | [diff] [blame] | 15 | extern void __context_tracking_task_switch(struct task_struct *prev, |
| 16 | struct task_struct *next); |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 17 | |
| 18 | static inline void user_enter(void) |
| 19 | { |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 20 | if (context_tracking_is_enabled()) |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 21 | context_tracking_user_enter(); |
| 22 | |
| 23 | } |
| 24 | static inline void user_exit(void) |
| 25 | { |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 26 | if (context_tracking_is_enabled()) |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 27 | context_tracking_user_exit(); |
| 28 | } |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 29 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 30 | static inline enum ctx_state exception_enter(void) |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 31 | { |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 32 | enum ctx_state prev_ctx; |
| 33 | |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 34 | if (!context_tracking_is_enabled()) |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 35 | return 0; |
| 36 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 37 | prev_ctx = this_cpu_read(context_tracking.state); |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 38 | context_tracking_user_exit(); |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 39 | |
| 40 | return prev_ctx; |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 41 | } |
| 42 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 43 | static inline void exception_exit(enum ctx_state prev_ctx) |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 44 | { |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 45 | if (context_tracking_is_enabled()) { |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 46 | if (prev_ctx == IN_USER) |
| 47 | context_tracking_user_enter(); |
| 48 | } |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 49 | } |
| 50 | |
Frederic Weisbecker | 73d424f | 2013-07-11 19:42:13 +0200 | [diff] [blame] | 51 | static inline void context_tracking_task_switch(struct task_struct *prev, |
| 52 | struct task_struct *next) |
| 53 | { |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 54 | if (context_tracking_is_enabled()) |
Frederic Weisbecker | 73d424f | 2013-07-11 19:42:13 +0200 | [diff] [blame] | 55 | __context_tracking_task_switch(prev, next); |
| 56 | } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 57 | #else |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 58 | static inline void user_enter(void) { } |
| 59 | static inline void user_exit(void) { } |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 60 | static inline enum ctx_state exception_enter(void) { return 0; } |
| 61 | static inline void exception_exit(enum ctx_state prev_ctx) { } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 62 | static inline void context_tracking_task_switch(struct task_struct *prev, |
| 63 | struct task_struct *next) { } |
| 64 | #endif /* !CONFIG_CONTEXT_TRACKING */ |
| 65 | |
Frederic Weisbecker | 65f382f | 2013-07-11 19:12:32 +0200 | [diff] [blame] | 66 | |
| 67 | #ifdef CONFIG_CONTEXT_TRACKING_FORCE |
| 68 | extern void context_tracking_init(void); |
| 69 | #else |
| 70 | static inline void context_tracking_init(void) { } |
| 71 | #endif /* CONFIG_CONTEXT_TRACKING_FORCE */ |
| 72 | |
| 73 | |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 74 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 75 | static inline void guest_enter(void) |
| 76 | { |
Frederic Weisbecker | b049340 | 2013-07-12 03:10:15 +0200 | [diff] [blame] | 77 | if (vtime_accounting_enabled()) |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 78 | vtime_guest_enter(current); |
| 79 | else |
| 80 | current->flags |= PF_VCPU; |
| 81 | } |
| 82 | |
| 83 | static inline void guest_exit(void) |
| 84 | { |
Frederic Weisbecker | b049340 | 2013-07-12 03:10:15 +0200 | [diff] [blame] | 85 | if (vtime_accounting_enabled()) |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 86 | vtime_guest_exit(current); |
| 87 | else |
| 88 | current->flags &= ~PF_VCPU; |
| 89 | } |
Frederic Weisbecker | 73d424f | 2013-07-11 19:42:13 +0200 | [diff] [blame] | 90 | |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 91 | #else |
| 92 | static inline void guest_enter(void) |
| 93 | { |
| 94 | /* |
Frederic Weisbecker | 5b206d4 | 2013-07-12 19:05:14 +0200 | [diff] [blame] | 95 | * This is running in ioctl context so its safe |
| 96 | * to assume that it's the stime pending cputime |
| 97 | * to flush. |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 98 | */ |
| 99 | vtime_account_system(current); |
| 100 | current->flags |= PF_VCPU; |
| 101 | } |
| 102 | |
| 103 | static inline void guest_exit(void) |
| 104 | { |
Frederic Weisbecker | 5b206d4 | 2013-07-12 19:05:14 +0200 | [diff] [blame] | 105 | /* Flush the guest cputime we spent on the guest */ |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 106 | vtime_account_system(current); |
| 107 | current->flags &= ~PF_VCPU; |
| 108 | } |
| 109 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ |
| 110 | |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 111 | #endif |