riscv: make arch_is_user_context() SMP compatible

This is painful. There is no way for u-mode code to know if we're
currently executing in u-mode without generating a fault, besides
stealing a general purpose register away from the standard ABI
that is. And a global variable doesn't work on SMP as this must be
per-CPU and we could be migrated to another CPU just at the right
moment to peek at the wrong CPU variable (and u-mode can't disable
preemption either).

So, given that we'll have to pay the price of an exception entry
anyway, let's at least make it free to privileged threads by using
the mscratch register as the non-user context indicator (it must
be zero in m-mode for exception entry to work properly). In the
case of u-mode we'll simulate a proper return value in the
exception trap code. Let's settle on the return value in t0
and omit the volatile to give the compiler a chance to cache
the result.

Signed-off-by: Nicolas Pitre <npitre@baylibre.com>
This commit is contained in:
Nicolas Pitre 2022-03-10 15:50:02 -05:00 committed by Anas Nashif
commit c8bfc2afda
4 changed files with 78 additions and 9 deletions

View file

@ -11,7 +11,7 @@
#include <stdio.h>
#include <core_pmp.h>
#ifdef CONFIG_USERSPACE
#if defined(CONFIG_USERSPACE) && !defined(CONFIG_SMP)
/*
* Glogal variable used to know the current mode running.
* Is not boolean because it must match the PMP granularity of the arch.
@ -251,7 +251,9 @@ FUNC_NORETURN void arch_user_mode_enter(k_thread_entry_t user_entry,
z_riscv_init_user_accesses(_current);
z_riscv_configure_user_allowed_stack(_current);
#if !defined(CONFIG_SMP)
is_user_mode = true;
#endif
register void *a0 __asm__("a0") = user_entry;
register void *a1 __asm__("a1") = p1;