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 | |
Paolo Bonzini | d0e536d | 2015-10-28 02:39:56 +0100 | [diff] [blame] | 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 | |
Rik van Riel | 3aab4f5 | 2015-02-10 15:27:50 -0500 | [diff] [blame] | 17 | extern void context_tracking_enter(enum ctx_state state); |
| 18 | extern void context_tracking_exit(enum ctx_state state); |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 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 | { |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 24 | if (context_tracking_is_enabled()) |
Paolo Bonzini | f70cd6b | 2015-10-28 02:39:55 +0100 | [diff] [blame] | 25 | context_tracking_enter(CONTEXT_USER); |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 26 | |
| 27 | } |
| 28 | static inline void user_exit(void) |
| 29 | { |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 30 | if (context_tracking_is_enabled()) |
Paolo Bonzini | f70cd6b | 2015-10-28 02:39:55 +0100 | [diff] [blame] | 31 | context_tracking_exit(CONTEXT_USER); |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 32 | } |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 33 | |
Paolo Bonzini | 2e9d1e1 | 2016-06-20 16:58:29 +0200 | [diff] [blame] | 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 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 47 | static inline enum ctx_state exception_enter(void) |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 48 | { |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 49 | enum ctx_state prev_ctx; |
| 50 | |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 51 | if (!context_tracking_is_enabled()) |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 52 | return 0; |
| 53 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 54 | prev_ctx = this_cpu_read(context_tracking.state); |
Rik van Riel | 3aab4f5 | 2015-02-10 15:27:50 -0500 | [diff] [blame] | 55 | if (prev_ctx != CONTEXT_KERNEL) |
| 56 | context_tracking_exit(prev_ctx); |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 57 | |
| 58 | return prev_ctx; |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 59 | } |
| 60 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 61 | static inline void exception_exit(enum ctx_state prev_ctx) |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 62 | { |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 63 | if (context_tracking_is_enabled()) { |
Rik van Riel | 3aab4f5 | 2015-02-10 15:27:50 -0500 | [diff] [blame] | 64 | if (prev_ctx != CONTEXT_KERNEL) |
| 65 | context_tracking_enter(prev_ctx); |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 66 | } |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 67 | } |
| 68 | |
Andy Lutomirski | f928164 | 2015-07-03 12:44:21 -0700 | [diff] [blame] | 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 | } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 82 | #else |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 83 | static inline void user_enter(void) { } |
| 84 | static inline void user_exit(void) { } |
Paolo Bonzini | 2e9d1e1 | 2016-06-20 16:58:29 +0200 | [diff] [blame] | 85 | static inline void user_enter_irqoff(void) { } |
| 86 | static inline void user_exit_irqoff(void) { } |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 87 | static inline enum ctx_state exception_enter(void) { return 0; } |
| 88 | static inline void exception_exit(enum ctx_state prev_ctx) { } |
Andy Lutomirski | f928164 | 2015-07-03 12:44:21 -0700 | [diff] [blame] | 89 | static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 90 | #endif /* !CONFIG_CONTEXT_TRACKING */ |
| 91 | |
Andy Lutomirski | f928164 | 2015-07-03 12:44:21 -0700 | [diff] [blame] | 92 | #define CT_WARN_ON(cond) WARN_ON(context_tracking_is_enabled() && (cond)) |
Frederic Weisbecker | 65f382f | 2013-07-11 19:12:32 +0200 | [diff] [blame] | 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 | |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 101 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 102 | static inline void guest_enter(void) |
| 103 | { |
Frederic Weisbecker | 55dbdcf | 2015-11-19 16:47:32 +0100 | [diff] [blame] | 104 | if (vtime_accounting_cpu_enabled()) |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 105 | vtime_guest_enter(current); |
| 106 | else |
| 107 | current->flags |= PF_VCPU; |
Rik van Riel | 126a6a5 | 2015-02-10 15:27:54 -0500 | [diff] [blame] | 108 | |
| 109 | if (context_tracking_is_enabled()) |
Paolo Bonzini | d0e536d | 2015-10-28 02:39:56 +0100 | [diff] [blame] | 110 | __context_tracking_enter(CONTEXT_GUEST); |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | static inline void guest_exit(void) |
| 114 | { |
Rik van Riel | 126a6a5 | 2015-02-10 15:27:54 -0500 | [diff] [blame] | 115 | if (context_tracking_is_enabled()) |
Paolo Bonzini | d0e536d | 2015-10-28 02:39:56 +0100 | [diff] [blame] | 116 | __context_tracking_exit(CONTEXT_GUEST); |
Rik van Riel | 126a6a5 | 2015-02-10 15:27:54 -0500 | [diff] [blame] | 117 | |
Frederic Weisbecker | 55dbdcf | 2015-11-19 16:47:32 +0100 | [diff] [blame] | 118 | if (vtime_accounting_cpu_enabled()) |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 119 | vtime_guest_exit(current); |
| 120 | else |
| 121 | current->flags &= ~PF_VCPU; |
| 122 | } |
Frederic Weisbecker | 73d424f | 2013-07-11 19:42:13 +0200 | [diff] [blame] | 123 | |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 124 | #else |
| 125 | static inline void guest_enter(void) |
| 126 | { |
| 127 | /* |
Frederic Weisbecker | 5b206d4 | 2013-07-12 19:05:14 +0200 | [diff] [blame] | 128 | * This is running in ioctl context so its safe |
| 129 | * to assume that it's the stime pending cputime |
| 130 | * to flush. |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 131 | */ |
| 132 | vtime_account_system(current); |
| 133 | current->flags |= PF_VCPU; |
| 134 | } |
| 135 | |
| 136 | static inline void guest_exit(void) |
| 137 | { |
Frederic Weisbecker | 5b206d4 | 2013-07-12 19:05:14 +0200 | [diff] [blame] | 138 | /* Flush the guest cputime we spent on the guest */ |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 139 | vtime_account_system(current); |
| 140 | current->flags &= ~PF_VCPU; |
| 141 | } |
| 142 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ |
| 143 | |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 144 | #endif |