Because UKL removes the barrier between kernel and user space, we need to track if we are executing application code or kernel code to ensure that we take the appropriate actions on transitions. When we transition to kernel code, we need to handle RCU and on the way to user code we need to check if scheduling needs to happen, etc. We cannot use the CS value from the stack because it will always be set to the kernel value. These functions will be used in a later change to entry_64.S to identify the execution context for the current thread. Cc: Jonathan Corbet <corbet@xxxxxxx> Cc: Masahiro Yamada <masahiroy@xxxxxxxxxx> Cc: Michal Marek <michal.lkml@xxxxxxxxxxx> Cc: Nick Desaulniers <ndesaulniers@xxxxxxxxxx> Cc: Thomas Gleixner <tglx@xxxxxxxxxxxxx> Cc: Ingo Molnar <mingo@xxxxxxxxxx> Cc: Borislav Petkov <bp@xxxxxxxxx> Cc: Dave Hansen <dave.hansen@xxxxxxxxxxxxxxx> Cc: "H. Peter Anvin" <hpa@xxxxxxxxx> Cc: Andy Lutomirski <luto@xxxxxxxxxx> Cc: Eric Biederman <ebiederm@xxxxxxxxxxxx> Cc: Kees Cook <keescook@xxxxxxxxxxxx> Cc: Peter Zijlstra <peterz@xxxxxxxxxxxxx> Cc: Alexander Viro <viro@xxxxxxxxxxxxxxxxxx> Cc: Arnd Bergmann <arnd@xxxxxxxx> Cc: Juri Lelli <juri.lelli@xxxxxxxxxx> Cc: Vincent Guittot <vincent.guittot@xxxxxxxxxx> Cc: Dietmar Eggemann <dietmar.eggemann@xxxxxxx> Cc: Steven Rostedt <rostedt@xxxxxxxxxxx> Cc: Ben Segall <bsegall@xxxxxxxxxx> Cc: Mel Gorman <mgorman@xxxxxxx> Cc: Daniel Bristot de Oliveira <bristot@xxxxxxxxxx> Cc: Valentin Schneider <vschneid@xxxxxxxxxx> Cc: Paolo Bonzini <pbonzini@xxxxxxxxxx> Cc: Josh Poimboeuf <jpoimboe@xxxxxxxxxx> Co-developed-by: Daniel Bristot de Oliveira <bristot@xxxxxxxxxx> Signed-off-by: Daniel Bristot de Oliveira <bristot@xxxxxxxxxx> Co-developed-by: Ali Raza <aliraza@xxxxxx> Signed-off-by: Ali Raza <aliraza@xxxxxx> --- arch/x86/kernel/process_64.c | 22 ++++++++++++++++++++++ include/linux/sched.h | 26 ++++++++++++++++++++++++++ 2 files changed, 48 insertions(+) diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 1962008fe743..e9e4a2946452 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -501,6 +501,28 @@ void x86_gsbase_write_task(struct task_struct *task, unsigned long gsbase) task->thread.gsbase = gsbase; } +#ifdef CONFIG_UNIKERNEL_LINUX +/* + * 0 = Non UKL thread + * 1 = UKL thread - in kernel code + * 2 = UKL thread - in application code + */ +int is_ukl_thread(void) +{ + return current->ukl_thread; +} + +void enter_ukl_user(void) +{ + current->ukl_thread = UKL_APPLICATION; +} + +void enter_ukl_kernel(void) +{ + current->ukl_thread = UKL_KERNEL; +} +#endif + static void start_thread_common(struct pt_regs *regs, unsigned long new_ip, unsigned long new_sp, diff --git a/include/linux/sched.h b/include/linux/sched.h index e7b2f8a5c711..b8bf50ae0fda 100644 --- a/include/linux/sched.h +++ b/include/linux/sched.h @@ -746,6 +746,13 @@ struct task_struct { randomized_struct_fields_start void *stack; +#ifdef CONFIG_UNIKERNEL_LINUX + /* + * Indicator used for threads in a UKL application, 0 means non-UKL thread, 1 is UKL thread + * in kernel text, 2 is UKL thread in application text + */ + int ukl_thread; +#endif refcount_t usage; /* Per task flags (PF_*), defined further below: */ unsigned int flags; @@ -1529,6 +1536,25 @@ struct task_struct { */ }; +/* + * 0 = Non UKL thread + * 1 = UKL thread - in kernel code + * 2 = UKL thread - in application code + */ +#define NON_UKL_THREAD 0 +#define UKL_KERNEL 1 +#define UKL_APPLICATION 2 + +#ifdef CONFIG_UNIKERNEL_LINUX +int is_ukl_thread(void); +void enter_ukl_user(void); +void enter_ukl_kernel(void); +#else +static inline int is_ukl_thread(void) { return NON_UKL_THREAD; } +static inline void enter_ukl_user(void) {} +static inline void enter_ukl_kernel(void) {} +#endif + static inline struct pid *task_pid(struct task_struct *task) { return task->thread_pid; -- 2.21.3