RISC-V: Prepare for CONFIG_BUILD_KERNEL

- Thread context prior to system call needs to be preserved
- Allocate a kernel heap
This commit is contained in:
Ville Juven
2022-02-24 09:12:51 +02:00
committed by Xiang Xiao
parent 195705d11f
commit 75afe491ad
8 changed files with 44 additions and 28 deletions
+2 -2
View File
@@ -471,7 +471,7 @@
struct xcpt_syscall_s struct xcpt_syscall_s
{ {
uintptr_t sysreturn; /* The return PC */ uintptr_t sysreturn; /* The return PC */
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
uintptr_t int_ctx; /* Interrupt context (i.e. mstatus) */ uintptr_t int_ctx; /* Interrupt context (i.e. mstatus) */
#endif #endif
}; };
@@ -501,7 +501,7 @@ struct xcptcontext
uintptr_t saved_epc; /* Trampoline PC */ uintptr_t saved_epc; /* Trampoline PC */
uintptr_t saved_int_ctx; /* Interrupt context with interrupts disabled. */ uintptr_t saved_int_ctx; /* Interrupt context with interrupts disabled. */
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
/* This is the saved address to use when returning from a user-space /* This is the saved address to use when returning from a user-space
* signal handler. * signal handler.
*/ */
@@ -138,7 +138,7 @@ void up_schedule_sigaction(struct tcb_s *tcb, sig_deliver_t sigdeliver)
int_ctx = CURRENT_REGS[REG_INT_CTX]; int_ctx = CURRENT_REGS[REG_INT_CTX];
int_ctx &= ~MSTATUS_MPIE; int_ctx &= ~MSTATUS_MPIE;
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
int_ctx |= MSTATUS_MPPM; int_ctx |= MSTATUS_MPPM;
#endif #endif
@@ -303,7 +303,7 @@ void up_schedule_sigaction(struct tcb_s *tcb, sig_deliver_t sigdeliver)
int_ctx = CURRENT_REGS[REG_INT_CTX]; int_ctx = CURRENT_REGS[REG_INT_CTX];
int_ctx &= ~MSTATUS_MPIE; int_ctx &= ~MSTATUS_MPIE;
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
int_ctx |= MSTATUS_MPPM; int_ctx |= MSTATUS_MPPM;
#endif #endif
+20 -7
View File
@@ -276,7 +276,7 @@ int riscv_swint(int irq, void *context, void *arg)
*/ */
regs[REG_EPC] = rtcb->xcp.syscall[index].sysreturn; regs[REG_EPC] = rtcb->xcp.syscall[index].sysreturn;
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
regs[REG_INT_CTX] = rtcb->xcp.syscall[index].int_ctx; regs[REG_INT_CTX] = rtcb->xcp.syscall[index].int_ctx;
#endif #endif
@@ -313,19 +313,27 @@ int riscv_swint(int irq, void *context, void *arg)
* A3 = argv * A3 = argv
*/ */
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
case SYS_task_start: case SYS_task_start:
{ {
/* Set up to return to the user-space task start-up function in /* Set up to return to the user-space task start-up function in
* unprivileged mode. * unprivileged mode.
*/ */
regs[REG_EPC] = (uintptr_t)USERSPACE->task_startup & ~1; #if defined (CONFIG_BUILD_PROTECTED)
/* Use the nxtask_startup trampoline function */
regs[REG_EPC] = (uintptr_t)USERSPACE->task_startup & ~1;
regs[REG_A0] = regs[REG_A1]; /* Task entry */ regs[REG_A0] = regs[REG_A1]; /* Task entry */
regs[REG_A1] = regs[REG_A2]; /* argc */ regs[REG_A1] = regs[REG_A2]; /* argc */
regs[REG_A2] = regs[REG_A3]; /* argv */ regs[REG_A2] = regs[REG_A3]; /* argv */
#else
/* Start the user task directly */
regs[REG_EPC] = (uintptr_t)regs[REG_A1] & ~1;
regs[REG_A0] = regs[REG_A2]; /* argc */
regs[REG_A1] = regs[REG_A3]; /* argv */
#endif
regs[REG_INT_CTX] &= ~MSTATUS_MPPM; /* User mode */ regs[REG_INT_CTX] &= ~MSTATUS_MPPM; /* User mode */
} }
break; break;
@@ -377,7 +385,7 @@ int riscv_swint(int irq, void *context, void *arg)
* R4 = ucontext * R4 = ucontext
*/ */
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
case SYS_signal_handler: case SYS_signal_handler:
{ {
struct tcb_s *rtcb = nxsched_self(); struct tcb_s *rtcb = nxsched_self();
@@ -391,7 +399,12 @@ int riscv_swint(int irq, void *context, void *arg)
* unprivileged mode. * unprivileged mode.
*/ */
#if defined (CONFIG_BUILD_PROTECTED)
regs[REG_EPC] = (uintptr_t)USERSPACE->signal_handler & ~1; regs[REG_EPC] = (uintptr_t)USERSPACE->signal_handler & ~1;
#else
regs[REG_EPC] =
(uintptr_t)ARCH_DATA_RESERVE->ar_sigtramp & ~1;
#endif
regs[REG_INT_CTX] &= ~MSTATUS_MPPM; /* User mode */ regs[REG_INT_CTX] &= ~MSTATUS_MPPM; /* User mode */
/* Change the parameter ordering to match the expectation of struct /* Change the parameter ordering to match the expectation of struct
@@ -415,7 +428,7 @@ int riscv_swint(int irq, void *context, void *arg)
* R0 = SYS_signal_handler_return * R0 = SYS_signal_handler_return
*/ */
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
case SYS_signal_handler_return: case SYS_signal_handler_return:
{ {
struct tcb_s *rtcb = nxsched_self(); struct tcb_s *rtcb = nxsched_self();
@@ -455,7 +468,7 @@ int riscv_swint(int irq, void *context, void *arg)
/* Setup to return to dispatch_syscall in privileged mode. */ /* Setup to return to dispatch_syscall in privileged mode. */
rtcb->xcp.syscall[index].sysreturn = regs[REG_EPC]; rtcb->xcp.syscall[index].sysreturn = regs[REG_EPC];
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
rtcb->xcp.syscall[index].int_ctx = regs[REG_INT_CTX]; rtcb->xcp.syscall[index].int_ctx = regs[REG_INT_CTX];
#endif #endif
@@ -463,7 +476,7 @@ int riscv_swint(int irq, void *context, void *arg)
regs[REG_EPC] = (uintptr_t)dispatch_syscall & ~1; regs[REG_EPC] = (uintptr_t)dispatch_syscall & ~1;
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
regs[REG_INT_CTX] |= MSTATUS_MPPM; /* Machine mode */ regs[REG_INT_CTX] |= MSTATUS_MPPM; /* Machine mode */
#endif #endif
+5 -4
View File
@@ -63,13 +63,14 @@ CHIP_CSRCS += mpfs_dma.c
endif endif
ifeq ($(CONFIG_BUILD_PROTECTED),y) ifeq ($(CONFIG_BUILD_PROTECTED),y)
CHIP_CSRCS += mpfs_userspace.c
CMN_UASRCS += riscv_signal_handler.S
endif
ifneq ($(CONFIG_BUILD_FLAT),y)
CMN_CSRCS += riscv_task_start.c CMN_CSRCS += riscv_task_start.c
CMN_CSRCS += riscv_pthread_start.c CMN_CSRCS += riscv_pthread_start.c
CMN_CSRCS += riscv_signal_dispatch.c CMN_CSRCS += riscv_signal_dispatch.c
CMN_UASRCS += riscv_signal_handler.S
CHIP_CSRCS += mpfs_userspace.c
endif endif
ifeq ($(CONFIG_ARCH_USE_MPU),y) ifeq ($(CONFIG_ARCH_USE_MPU),y)
+5 -4
View File
@@ -75,7 +75,7 @@
* Kernel heap Size determined by CONFIG_MM_KERNEL_HEAPSIZE * Kernel heap Size determined by CONFIG_MM_KERNEL_HEAPSIZE
* *
****************************************************************************/ ****************************************************************************/
#ifndef CONFIG_BUILD_KERNEL
void up_allocate_heap(void **heap_start, size_t *heap_size) void up_allocate_heap(void **heap_start, size_t *heap_size)
{ {
#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP) #if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP)
@@ -100,8 +100,9 @@ void up_allocate_heap(void **heap_start, size_t *heap_size)
*heap_start = (void *)g_idle_topstack; *heap_start = (void *)g_idle_topstack;
*heap_size = KRAM_END - g_idle_topstack; *heap_size = KRAM_END - g_idle_topstack;
#endif #endif /* CONFIG_BUILD_PROTECTED && CONFIG_MM_KERNEL_HEAP */
} }
#endif /* CONFIG_BUILD_KERNEL */
/**************************************************************************** /****************************************************************************
* Name: up_allocate_kheap * Name: up_allocate_kheap
@@ -113,7 +114,7 @@ void up_allocate_heap(void **heap_start, size_t *heap_size)
* *
****************************************************************************/ ****************************************************************************/
#if defined(CONFIG_BUILD_PROTECTED) && defined(CONFIG_MM_KERNEL_HEAP) #if !defined(CONFIG_BUILD_FLAT) && defined(CONFIG_MM_KERNEL_HEAP)
void up_allocate_kheap(void **heap_start, size_t *heap_size) void up_allocate_kheap(void **heap_start, size_t *heap_size)
{ {
/* Return the kernel heap settings. */ /* Return the kernel heap settings. */
@@ -121,7 +122,7 @@ void up_allocate_kheap(void **heap_start, size_t *heap_size)
*heap_start = (void *)g_idle_topstack; *heap_start = (void *)g_idle_topstack;
*heap_size = KRAM_END - g_idle_topstack; *heap_size = KRAM_END - g_idle_topstack;
} }
#endif #endif /* !CONFIG_BUILD_FLAT && CONFIG_MM_KERNEL_HEAP */
/**************************************************************************** /****************************************************************************
* Name: up_addregion * Name: up_addregion
+1 -1
View File
@@ -107,7 +107,7 @@ void up_irqinitialize(void)
irq_attach(RISCV_IRQ_ECALLM, riscv_swint, NULL); irq_attach(RISCV_IRQ_ECALLM, riscv_swint, NULL);
#ifdef CONFIG_BUILD_PROTECTED #ifndef CONFIG_BUILD_FLAT
irq_attach(RISCV_IRQ_ECALLU, riscv_swint, NULL); irq_attach(RISCV_IRQ_ECALLU, riscv_swint, NULL);
#endif #endif
+1 -1
View File
@@ -189,7 +189,7 @@ int exec_module(FAR const struct binary_s *binp,
goto errout_with_addrenv; goto errout_with_addrenv;
} }
#if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_BUILD_KERNEL) #if defined(CONFIG_ARCH_ADDRENV) && defined(CONFIG_ARCH_KERNEL_STACK)
/* Allocate the kernel stack */ /* Allocate the kernel stack */
ret = up_addrenv_kstackalloc(&tcb->cmn); ret = up_addrenv_kstackalloc(&tcb->cmn);
+1
View File
@@ -38,6 +38,7 @@
#include <nuttx/mm/iob.h> #include <nuttx/mm/iob.h>
#include <nuttx/mm/mm.h> #include <nuttx/mm/mm.h>
#include <nuttx/kmalloc.h> #include <nuttx/kmalloc.h>
#include <nuttx/pgalloc.h>
#include <nuttx/sched_note.h> #include <nuttx/sched_note.h>
#include <nuttx/binfmt/binfmt.h> #include <nuttx/binfmt/binfmt.h>
#include <nuttx/drivers/drivers.h> #include <nuttx/drivers/drivers.h>