blob: 2821838256b4f728640819f9f81842954261ec03 [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
Rik van Riel3aab4f52015-02-10 15:27:50 -050013extern void context_tracking_enter(enum ctx_state state);
14extern void context_tracking_exit(enum ctx_state state);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020015extern void context_tracking_user_enter(void);
16extern void context_tracking_user_exit(void);
Frederic Weisbecker73d424f2013-07-11 19:42:13 +020017extern void __context_tracking_task_switch(struct task_struct *prev,
18 struct task_struct *next);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020019
20static inline void user_enter(void)
21{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010022 if (context_tracking_is_enabled())
Frederic Weisbeckerad657822013-07-10 02:44:35 +020023 context_tracking_user_enter();
24
25}
26static inline void user_exit(void)
27{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010028 if (context_tracking_is_enabled())
Frederic Weisbeckerad657822013-07-10 02:44:35 +020029 context_tracking_user_exit();
30}
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010031
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010032static inline enum ctx_state exception_enter(void)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010033{
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010034 enum ctx_state prev_ctx;
35
Frederic Weisbecker58135f52013-11-06 14:45:57 +010036 if (!context_tracking_is_enabled())
Frederic Weisbeckerad657822013-07-10 02:44:35 +020037 return 0;
38
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010039 prev_ctx = this_cpu_read(context_tracking.state);
Rik van Riel3aab4f52015-02-10 15:27:50 -050040 if (prev_ctx != CONTEXT_KERNEL)
41 context_tracking_exit(prev_ctx);
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010042
43 return prev_ctx;
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010044}
45
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010046static inline void exception_exit(enum ctx_state prev_ctx)
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010047{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010048 if (context_tracking_is_enabled()) {
Rik van Riel3aab4f52015-02-10 15:27:50 -050049 if (prev_ctx != CONTEXT_KERNEL)
50 context_tracking_enter(prev_ctx);
Frederic Weisbeckerad657822013-07-10 02:44:35 +020051 }
Frederic Weisbecker56dd9472013-02-24 00:23:25 +010052}
53
Frederic Weisbecker73d424f2013-07-11 19:42:13 +020054static inline void context_tracking_task_switch(struct task_struct *prev,
55 struct task_struct *next)
56{
Frederic Weisbecker58135f52013-11-06 14:45:57 +010057 if (context_tracking_is_enabled())
Frederic Weisbecker73d424f2013-07-11 19:42:13 +020058 __context_tracking_task_switch(prev, next);
59}
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010060#else
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010061static inline void user_enter(void) { }
62static inline void user_exit(void) { }
Frederic Weisbecker6c1e0252013-02-24 01:19:14 +010063static inline enum ctx_state exception_enter(void) { return 0; }
64static inline void exception_exit(enum ctx_state prev_ctx) { }
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +010065static inline void context_tracking_task_switch(struct task_struct *prev,
66 struct task_struct *next) { }
67#endif /* !CONFIG_CONTEXT_TRACKING */
68
Frederic Weisbecker65f382f2013-07-11 19:12:32 +020069
70#ifdef CONFIG_CONTEXT_TRACKING_FORCE
71extern void context_tracking_init(void);
72#else
73static inline void context_tracking_init(void) { }
74#endif /* CONFIG_CONTEXT_TRACKING_FORCE */
75
76
Frederic Weisbecker2d854e52013-07-12 19:02:30 +020077#ifdef CONFIG_VIRT_CPU_ACCOUNTING_GEN
Frederic Weisbecker48d6a812013-07-10 02:44:35 +020078static inline void guest_enter(void)
79{
Frederic Weisbeckerb0493402013-07-12 03:10:15 +020080 if (vtime_accounting_enabled())
Frederic Weisbecker48d6a812013-07-10 02:44:35 +020081 vtime_guest_enter(current);
82 else
83 current->flags |= PF_VCPU;
Rik van Riel126a6a52015-02-10 15:27:54 -050084
85 if (context_tracking_is_enabled())
86 context_tracking_enter(CONTEXT_GUEST);
Frederic Weisbecker48d6a812013-07-10 02:44:35 +020087}
88
89static inline void guest_exit(void)
90{
Rik van Riel126a6a52015-02-10 15:27:54 -050091 if (context_tracking_is_enabled())
92 context_tracking_exit(CONTEXT_GUEST);
93
Frederic Weisbeckerb0493402013-07-12 03:10:15 +020094 if (vtime_accounting_enabled())
Frederic Weisbecker48d6a812013-07-10 02:44:35 +020095 vtime_guest_exit(current);
96 else
97 current->flags &= ~PF_VCPU;
98}
Frederic Weisbecker73d424f2013-07-11 19:42:13 +020099
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200100#else
101static inline void guest_enter(void)
102{
103 /*
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200104 * This is running in ioctl context so its safe
105 * to assume that it's the stime pending cputime
106 * to flush.
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200107 */
108 vtime_account_system(current);
109 current->flags |= PF_VCPU;
110}
111
112static inline void guest_exit(void)
113{
Frederic Weisbecker5b206d42013-07-12 19:05:14 +0200114 /* Flush the guest cputime we spent on the guest */
Frederic Weisbecker2d854e52013-07-12 19:02:30 +0200115 vtime_account_system(current);
116 current->flags &= ~PF_VCPU;
117}
118#endif /* CONFIG_VIRT_CPU_ACCOUNTING_GEN */
119
Frederic Weisbecker91d1aa432012-11-27 19:33:25 +0100120#endif