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 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 34 | static inline enum ctx_state exception_enter(void) |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 35 | { |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 36 | enum ctx_state prev_ctx; |
| 37 | |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 38 | if (!context_tracking_is_enabled()) |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 39 | return 0; |
| 40 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 41 | prev_ctx = this_cpu_read(context_tracking.state); |
Rik van Riel | 3aab4f5 | 2015-02-10 15:27:50 -0500 | [diff] [blame] | 42 | if (prev_ctx != CONTEXT_KERNEL) |
| 43 | context_tracking_exit(prev_ctx); |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 44 | |
| 45 | return prev_ctx; |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 46 | } |
| 47 | |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 48 | static inline void exception_exit(enum ctx_state prev_ctx) |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 49 | { |
Frederic Weisbecker | 58135f5 | 2013-11-06 14:45:57 +0100 | [diff] [blame] | 50 | if (context_tracking_is_enabled()) { |
Rik van Riel | 3aab4f5 | 2015-02-10 15:27:50 -0500 | [diff] [blame] | 51 | if (prev_ctx != CONTEXT_KERNEL) |
| 52 | context_tracking_enter(prev_ctx); |
Frederic Weisbecker | ad65782 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 53 | } |
Frederic Weisbecker | 56dd947 | 2013-02-24 00:23:25 +0100 | [diff] [blame] | 54 | } |
| 55 | |
Andy Lutomirski | f928164 | 2015-07-03 12:44:21 -0700 | [diff] [blame] | 56 | |
| 57 | /** |
| 58 | * ct_state() - return the current context tracking state if known |
| 59 | * |
| 60 | * Returns the current cpu's context tracking state if context tracking |
| 61 | * is enabled. If context tracking is disabled, returns |
| 62 | * CONTEXT_DISABLED. This should be used primarily for debugging. |
| 63 | */ |
| 64 | static inline enum ctx_state ct_state(void) |
| 65 | { |
| 66 | return context_tracking_is_enabled() ? |
| 67 | this_cpu_read(context_tracking.state) : CONTEXT_DISABLED; |
| 68 | } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 69 | #else |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 70 | static inline void user_enter(void) { } |
| 71 | static inline void user_exit(void) { } |
Frederic Weisbecker | 6c1e025 | 2013-02-24 01:19:14 +0100 | [diff] [blame] | 72 | static inline enum ctx_state exception_enter(void) { return 0; } |
| 73 | static inline void exception_exit(enum ctx_state prev_ctx) { } |
Andy Lutomirski | f928164 | 2015-07-03 12:44:21 -0700 | [diff] [blame] | 74 | static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; } |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 75 | #endif /* !CONFIG_CONTEXT_TRACKING */ |
| 76 | |
Andy Lutomirski | f928164 | 2015-07-03 12:44:21 -0700 | [diff] [blame] | 77 | #define CT_WARN_ON(cond) WARN_ON(context_tracking_is_enabled() && (cond)) |
Frederic Weisbecker | 65f382f | 2013-07-11 19:12:32 +0200 | [diff] [blame] | 78 | |
| 79 | #ifdef CONFIG_CONTEXT_TRACKING_FORCE |
| 80 | extern void context_tracking_init(void); |
| 81 | #else |
| 82 | static inline void context_tracking_init(void) { } |
| 83 | #endif /* CONFIG_CONTEXT_TRACKING_FORCE */ |
| 84 | |
| 85 | |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 86 | #ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 87 | static inline void guest_enter(void) |
| 88 | { |
Frederic Weisbecker | 55dbdcf | 2015-11-19 16:47:32 +0100 | [diff] [blame] | 89 | if (vtime_accounting_cpu_enabled()) |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 90 | vtime_guest_enter(current); |
| 91 | else |
| 92 | current->flags |= PF_VCPU; |
Rik van Riel | 126a6a5 | 2015-02-10 15:27:54 -0500 | [diff] [blame] | 93 | |
| 94 | if (context_tracking_is_enabled()) |
Paolo Bonzini | d0e536d | 2015-10-28 02:39:56 +0100 | [diff] [blame] | 95 | __context_tracking_enter(CONTEXT_GUEST); |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 96 | } |
| 97 | |
| 98 | static inline void guest_exit(void) |
| 99 | { |
Rik van Riel | 126a6a5 | 2015-02-10 15:27:54 -0500 | [diff] [blame] | 100 | if (context_tracking_is_enabled()) |
Paolo Bonzini | d0e536d | 2015-10-28 02:39:56 +0100 | [diff] [blame] | 101 | __context_tracking_exit(CONTEXT_GUEST); |
Rik van Riel | 126a6a5 | 2015-02-10 15:27:54 -0500 | [diff] [blame] | 102 | |
Frederic Weisbecker | 55dbdcf | 2015-11-19 16:47:32 +0100 | [diff] [blame] | 103 | if (vtime_accounting_cpu_enabled()) |
Frederic Weisbecker | 48d6a81 | 2013-07-10 02:44:35 +0200 | [diff] [blame] | 104 | vtime_guest_exit(current); |
| 105 | else |
| 106 | current->flags &= ~PF_VCPU; |
| 107 | } |
Frederic Weisbecker | 73d424f | 2013-07-11 19:42:13 +0200 | [diff] [blame] | 108 | |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 109 | #else |
| 110 | static inline void guest_enter(void) |
| 111 | { |
| 112 | /* |
Frederic Weisbecker | 5b206d4 | 2013-07-12 19:05:14 +0200 | [diff] [blame] | 113 | * This is running in ioctl context so its safe |
| 114 | * to assume that it's the stime pending cputime |
| 115 | * to flush. |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 116 | */ |
| 117 | vtime_account_system(current); |
| 118 | current->flags |= PF_VCPU; |
| 119 | } |
| 120 | |
| 121 | static inline void guest_exit(void) |
| 122 | { |
Frederic Weisbecker | 5b206d4 | 2013-07-12 19:05:14 +0200 | [diff] [blame] | 123 | /* Flush the guest cputime we spent on the guest */ |
Frederic Weisbecker | 2d854e5 | 2013-07-12 19:02:30 +0200 | [diff] [blame] | 124 | vtime_account_system(current); |
| 125 | current->flags &= ~PF_VCPU; |
| 126 | } |
| 127 | #endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */ |
| 128 | |
Frederic Weisbecker | 91d1aa43 | 2012-11-27 19:33:25 +0100 | [diff] [blame] | 129 | #endif |