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