2017-11-01 15:07:57 +01:00
|
|
|
/* SPDX-License-Identifier: GPL-2.0 */
|
2012-11-27 19:33:25 +01:00
|
|
|
#ifndef _LINUX_CONTEXT_TRACKING_H
|
|
|
|
|
#define _LINUX_CONTEXT_TRACKING_H
|
|
|
|
|
|
|
|
|
|
#include <linux/sched.h>
|
2013-05-16 01:21:38 +02:00
|
|
|
#include <linux/vtime.h>
|
2013-07-12 02:15:49 +02:00
|
|
|
#include <linux/context_tracking_state.h>
|
2020-07-24 13:50:25 +02:00
|
|
|
#include <linux/instrumentation.h>
|
|
|
|
|
|
2013-02-24 00:23:25 +01:00
|
|
|
#include <asm/ptrace.h>
|
2013-01-07 18:12:14 +01:00
|
|
|
|
2013-05-16 01:21:38 +02:00
|
|
|
|
2013-02-24 01:19:14 +01:00
|
|
|
#ifdef CONFIG_CONTEXT_TRACKING
|
2013-07-10 00:55:25 +02:00
|
|
|
extern void context_tracking_cpu_set(int cpu);
|
|
|
|
|
|
2015-10-28 02:39:56 +01:00
|
|
|
/* Called with interrupts disabled. */
|
|
|
|
|
extern void __context_tracking_enter(enum ctx_state state);
|
|
|
|
|
extern void __context_tracking_exit(enum ctx_state state);
|
|
|
|
|
|
2015-02-10 15:27:50 -05:00
|
|
|
extern void context_tracking_enter(enum ctx_state state);
|
|
|
|
|
extern void context_tracking_exit(enum ctx_state state);
|
2013-07-10 02:44:35 +02:00
|
|
|
extern void context_tracking_user_enter(void);
|
|
|
|
|
extern void context_tracking_user_exit(void);
|
|
|
|
|
|
|
|
|
|
static inline void user_enter(void)
|
|
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
if (context_tracking_enabled())
|
2015-10-28 02:39:55 +01:00
|
|
|
context_tracking_enter(CONTEXT_USER);
|
2013-07-10 02:44:35 +02:00
|
|
|
|
|
|
|
|
}
|
|
|
|
|
static inline void user_exit(void)
|
|
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
if (context_tracking_enabled())
|
2015-10-28 02:39:55 +01:00
|
|
|
context_tracking_exit(CONTEXT_USER);
|
2013-07-10 02:44:35 +02:00
|
|
|
}
|
2013-02-24 00:23:25 +01:00
|
|
|
|
2016-06-20 16:58:29 +02:00
|
|
|
/* Called with interrupts disabled. */
|
2020-03-04 11:05:22 +01:00
|
|
|
static __always_inline void user_enter_irqoff(void)
|
2016-06-20 16:58:29 +02:00
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
if (context_tracking_enabled())
|
2016-06-20 16:58:29 +02:00
|
|
|
__context_tracking_enter(CONTEXT_USER);
|
|
|
|
|
|
|
|
|
|
}
|
2020-03-04 11:05:22 +01:00
|
|
|
static __always_inline void user_exit_irqoff(void)
|
2016-06-20 16:58:29 +02:00
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
if (context_tracking_enabled())
|
2016-06-20 16:58:29 +02:00
|
|
|
__context_tracking_exit(CONTEXT_USER);
|
|
|
|
|
}
|
|
|
|
|
|
2013-02-24 01:19:14 +01:00
|
|
|
static inline enum ctx_state exception_enter(void)
|
2013-02-24 00:23:25 +01:00
|
|
|
{
|
2013-02-24 01:19:14 +01:00
|
|
|
enum ctx_state prev_ctx;
|
|
|
|
|
|
2020-11-17 16:16:34 +01:00
|
|
|
if (IS_ENABLED(CONFIG_HAVE_CONTEXT_TRACKING_OFFSTACK) ||
|
|
|
|
|
!context_tracking_enabled())
|
2013-07-10 02:44:35 +02:00
|
|
|
return 0;
|
|
|
|
|
|
2013-02-24 01:19:14 +01:00
|
|
|
prev_ctx = this_cpu_read(context_tracking.state);
|
2015-02-10 15:27:50 -05:00
|
|
|
if (prev_ctx != CONTEXT_KERNEL)
|
|
|
|
|
context_tracking_exit(prev_ctx);
|
2013-02-24 01:19:14 +01:00
|
|
|
|
|
|
|
|
return prev_ctx;
|
2013-02-24 00:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
2013-02-24 01:19:14 +01:00
|
|
|
static inline void exception_exit(enum ctx_state prev_ctx)
|
2013-02-24 00:23:25 +01:00
|
|
|
{
|
2020-11-17 16:16:34 +01:00
|
|
|
if (!IS_ENABLED(CONFIG_HAVE_CONTEXT_TRACKING_OFFSTACK) &&
|
|
|
|
|
context_tracking_enabled()) {
|
2015-02-10 15:27:50 -05:00
|
|
|
if (prev_ctx != CONTEXT_KERNEL)
|
|
|
|
|
context_tracking_enter(prev_ctx);
|
2013-07-10 02:44:35 +02:00
|
|
|
}
|
2013-02-24 00:23:25 +01:00
|
|
|
}
|
|
|
|
|
|
2021-05-04 17:27:33 -07:00
|
|
|
static __always_inline bool context_tracking_guest_enter(void)
|
|
|
|
|
{
|
|
|
|
|
if (context_tracking_enabled())
|
|
|
|
|
__context_tracking_enter(CONTEXT_GUEST);
|
|
|
|
|
|
|
|
|
|
return context_tracking_enabled_this_cpu();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static __always_inline void context_tracking_guest_exit(void)
|
|
|
|
|
{
|
|
|
|
|
if (context_tracking_enabled())
|
|
|
|
|
__context_tracking_exit(CONTEXT_GUEST);
|
|
|
|
|
}
|
2015-07-03 12:44:21 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* ct_state() - return the current context tracking state if known
|
|
|
|
|
*
|
|
|
|
|
* Returns the current cpu's context tracking state if context tracking
|
|
|
|
|
* is enabled. If context tracking is disabled, returns
|
|
|
|
|
* CONTEXT_DISABLED. This should be used primarily for debugging.
|
|
|
|
|
*/
|
2020-03-04 11:05:22 +01:00
|
|
|
static __always_inline enum ctx_state ct_state(void)
|
2015-07-03 12:44:21 -07:00
|
|
|
{
|
2019-10-16 04:56:51 +02:00
|
|
|
return context_tracking_enabled() ?
|
2015-07-03 12:44:21 -07:00
|
|
|
this_cpu_read(context_tracking.state) : CONTEXT_DISABLED;
|
|
|
|
|
}
|
2012-11-27 19:33:25 +01:00
|
|
|
#else
|
|
|
|
|
static inline void user_enter(void) { }
|
|
|
|
|
static inline void user_exit(void) { }
|
2016-06-20 16:58:29 +02:00
|
|
|
static inline void user_enter_irqoff(void) { }
|
|
|
|
|
static inline void user_exit_irqoff(void) { }
|
2013-02-24 01:19:14 +01:00
|
|
|
static inline enum ctx_state exception_enter(void) { return 0; }
|
|
|
|
|
static inline void exception_exit(enum ctx_state prev_ctx) { }
|
2015-07-03 12:44:21 -07:00
|
|
|
static inline enum ctx_state ct_state(void) { return CONTEXT_DISABLED; }
|
2021-06-24 11:41:05 +02:00
|
|
|
static __always_inline bool context_tracking_guest_enter(void) { return false; }
|
2021-05-04 17:27:33 -07:00
|
|
|
static inline void context_tracking_guest_exit(void) { }
|
|
|
|
|
|
2012-11-27 19:33:25 +01:00
|
|
|
#endif /* !CONFIG_CONTEXT_TRACKING */
|
|
|
|
|
|
2019-10-16 04:56:51 +02:00
|
|
|
#define CT_WARN_ON(cond) WARN_ON(context_tracking_enabled() && (cond))
|
2013-07-11 19:12:32 +02:00
|
|
|
|
|
|
|
|
#ifdef CONFIG_CONTEXT_TRACKING_FORCE
|
|
|
|
|
extern void context_tracking_init(void);
|
|
|
|
|
#else
|
|
|
|
|
static inline void context_tracking_init(void) { }
|
|
|
|
|
#endif /* CONFIG_CONTEXT_TRACKING_FORCE */
|
|
|
|
|
|
2012-11-27 19:33:25 +01:00
|
|
|
#endif
|