blob: d9aef2a0ec8eb48eb38a891bf263b90cfd43d502 [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 Weisbecker521921b2013-05-16 01:21:38 +02005#include <linux/vtime.h>
Frederic Weisbeckere7358b32013-07-12 02:15:49 +02006#include <linux/context_tracking_state.h>
Frederic Weisbecker56dd9472013-02-24 00:23:25 +01007#include <asm/ptrace.h>
Frederic Weisbecker95a79fd2013-01-07 18:12:14 +01008
Frederic Weisbecker521921b2013-05-16 01:21:38 +02009
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010010#ifdef CONFIG_CONTEXT_TRACKING
Frederic Weisbecker2e709332013-07-10 00:55:25 +020011extern void context_tracking_cpu_set(int cpu);
12
Paolo Bonzinid0e536d2015-10-28 02:39:56 +010013/* Called with interrupts disabled. */
14extern void __context_tracking_enter(enum ctx_state state);
15extern void __context_tracking_exit(enum ctx_state state);
16
Rik van Riel3aab4f52015-02-10 15:27:50 -050017extern void context_tracking_enter(enum ctx_state state);
18extern void context_tracking_exit(enum ctx_state state);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020019extern void context_tracking_user_enter(void);
20extern void context_tracking_user_exit(void);
21
22static inline void user_enter(void)
23{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010024 if (context_tracking_is_enabled())
Paolo Bonzinif70cd6b2015-10-28 02:39:55 +010025 context_tracking_enter(CONTEXT_USER);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020026
27}
28static inline void user_exit(void)
29{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010030 if (context_tracking_is_enabled())
Paolo Bonzinif70cd6b2015-10-28 02:39:55 +010031 context_tracking_exit(CONTEXT_USER);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020032}
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010033
Paolo Bonzini2e9d1e12016-06-20 16:58:29 +020034/* Called with interrupts disabled. */
35static inline void user_enter_irqoff(void)
36{
37 if (context_tracking_is_enabled())
38 __context_tracking_enter(CONTEXT_USER);
39
40}
41static inline void user_exit_irqoff(void)
42{
43 if (context_tracking_is_enabled())
44 __context_tracking_exit(CONTEXT_USER);
45}
46
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010047static inline enum ctx_state exception_enter(void)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010048{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010049 enum ctx_state prev_ctx;
50
Frederic Weisbecker58135f52013-11-06 14:45:57 +010051 if (!context_tracking_is_enabled())
Frederic Weisbeckerad657822013-07-10 02:44:35 +020052 return 0;
53
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010054 prev_ctx = this_cpu_read(context_tracking.state);
Rik van Riel3aab4f52015-02-10 15:27:50 -050055 if (prev_ctx != CONTEXT_KERNEL)
56 context_tracking_exit(prev_ctx);
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010057
58 return prev_ctx;
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010059}
60
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010061static inline void exception_exit(enum ctx_state prev_ctx)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010062{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010063 if (context_tracking_is_enabled()) {
Rik van Riel3aab4f52015-02-10 15:27:50 -050064 if (prev_ctx != CONTEXT_KERNEL)
65 context_tracking_enter(prev_ctx);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020066 }
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010067}
68
Andy Lutomirskif9281642015-07-03 12:44:21 -070069
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 */
77static 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 Weisbecker91d1aa432012-11-27 19:33:25 +010082#else
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010083static inline void user_enter(void) { }
84static inline void user_exit(void) { }
Paolo Bonzini2e9d1e12016-06-20 16:58:29 +020085static inline void user_enter_irqoff(void) { }
86static inline void user_exit_irqoff(void) { }
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010087static inline enum ctx_state exception_enter(void) { return 0; }
88static inline void exception_exit(enum ctx_state prev_ctx) { }
Andy Lutomirskif9281642015-07-03 12:44:21 -070089static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010090#endif /* !CONFIG_CONTEXT_TRACKING */
91
Andy Lutomirskif9281642015-07-03 12:44:21 -070092#define CT_WARN_ON(cond) WARN_ON(context_tracking_is_enabled() && (cond))
Frederic Weisbecker65f382f2013-07-11 19:12:32 +020093
94#ifdef CONFIG_CONTEXT_TRACKING_FORCE
95extern void context_tracking_init(void);
96#else
97static inline void context_tracking_init(void) { }
98#endif /* CONFIG_CONTEXT_TRACKING_FORCE */
99
100
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200101#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200102static inline void guest_enter(void)
103{
Frederic Weisbecker55dbdcf2015-11-19 16:47:32 +0100104 if (vtime_accounting_cpu_enabled())
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200105 vtime_guest_enter(current);
106 else
107 current->flags |= PF_VCPU;
Rik van Riel126a6a52015-02-10 15:27:54 -0500108
109 if (context_tracking_is_enabled())
Paolo Bonzinid0e536d2015-10-28 02:39:56 +0100110 __context_tracking_enter(CONTEXT_GUEST);
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200111}
112
113static inline void guest_exit(void)
114{
Rik van Riel126a6a52015-02-10 15:27:54 -0500115 if (context_tracking_is_enabled())
Paolo Bonzinid0e536d2015-10-28 02:39:56 +0100116 __context_tracking_exit(CONTEXT_GUEST);
Rik van Riel126a6a52015-02-10 15:27:54 -0500117
Frederic Weisbecker55dbdcf2015-11-19 16:47:32 +0100118 if (vtime_accounting_cpu_enabled())
Frederic Weisbecker48d6a812013-07-10 02:44:35 +0200119 vtime_guest_exit(current);
120 else
121 current->flags &= ~PF_VCPU;
122}
Frederic Weisbecker73d424f2013-07-11 19:42:13 +0200123
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200124#else
125static inline void guest_enter(void)
126{
127 /*
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200128 * This is running in ioctl context so its safe
129 * to assume that it's the stime pending cputime
130 * to flush.
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200131 */
132 vtime_account_system(current);
133 current->flags |= PF_VCPU;
134}
135
136static inline void guest_exit(void)
137{
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200138 /* Flush the guest cputime we spent on the guest */
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200139 vtime_account_system(current);
140 current->flags &= ~PF_VCPU;
141}
142#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
143
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100144#endif