Switch to user-mode before starting a new task

git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5742 42af7a65-404d-4744-a932-0658087f49c3
This commit is contained in:
patacongo
2013-03-14 22:44:06 +00:00
parent f8da801140
commit e9040a2f9d
9 changed files with 238 additions and 120 deletions
+10 -3
View File
@@ -57,9 +57,9 @@
#ifdef CONFIG_NUTTX_KERNEL
# ifndef CONFIG_SYS_RESERVED
# error "CONFIG_SYS_RESERVED must be defined to the value 4"
# elif CONFIG_SYS_RESERVED != 4
# error "CONFIG_SYS_RESERVED must have the value 4"
# error "CONFIG_SYS_RESERVED must be defined to the value 5"
# elif CONFIG_SYS_RESERVED != 5
# error "CONFIG_SYS_RESERVED must have the value 5"
# endif
#endif
@@ -93,6 +93,13 @@
*/
#define SYS_syscall_return (3)
/* SYS call 3:
*
* void up_task_start(main_t taskentry, int argc, FAR char *argv[]) noreturn_function;
*/
#define SYS_task_start (4)
#endif
/************************************************************************************
+5 -18
View File
@@ -126,26 +126,13 @@ void up_initial_state(struct tcb_s *tcb)
#endif
#endif /* CONFIG_PIC */
/* Set privileged- or unprivileged-mode, depending on how NuttX is
* configured and what kind of thread is being started.
/* All tasks start via a stub function in kernel space. So all
* tasks must start in privileged thread mode. If CONFIG_NUTTX_KERNEL
* is defined, then that stub function will switch to unprivileged
* mode before transferring control to the user task.
*/
#ifdef CONFIG_NUTTX_KERNEL
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
{
/* It is a normal task or a pthread. Set user mode */
xcp->regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
}
else
{
/* If the kernel build is not selected -OR- if this is a kernel
* thread, then start it in privileged thread mode.
*/
xcp->regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
}
#endif /* CONFIG_NUTTX_KERNEL */
xcp->regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
/* Enable or disable interrupts, based on user configuration */
+44 -11
View File
@@ -232,9 +232,9 @@ int up_svcall(int irq, FAR void *context)
}
break;
/* R0=SYS_syscall_return: This a switch context command:
/* R0=SYS_syscall_return: This a syscall return command:
*
* void up_sycall_return(void);
* void up_syscall_return(void);
*
* At this point, the following values are saved in context:
*
@@ -260,15 +260,48 @@ int up_svcall(int irq, FAR void *context)
* unprivileged mode.
*/
current_regs[REG_PC] = rtcb->xcp.sysreturn;
current_regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
rtcb->xcp.sysreturn = 0;
regs[REG_PC] = rtcb->xcp.sysreturn;
regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
rtcb->xcp.sysreturn = 0;
/* The return value must be in R0-R1. dispatch_syscall() temporarily
* moved the value to R2.
*/
current_regs[REG_R0] = current_regs[REG_R2];
regs[REG_R0] = regs[REG_R2];
}
break;
#endif
/* R0=SYS_task_start: This a user task start
*
* void up_task_start(main_t taskentry, int argc, FAR char *argv[]) noreturn_function;
*
* At this point, the following values are saved in context:
*
* R0 = SYS_task_start
* R1 = taskentry
* R2 = argc
* R3 = argv
*/
#ifdef CONFIG_NUTTX_KERNEL
case SYS_task_start:
{
/* Set up to return to the user-space task start-up function in
* unprivileged mode.
*/
regs[REG_PC] = (uint32_t)USERSPACE->task_startup;
regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
/* Change the paramter ordering to match the expection of struct
* userpace_s task_startup:
*/
regs[REG_R0] = regs[REG_R1]; /* Task entry */
regs[REG_R1] = regs[REG_R2]; /* argc */
regs[REG_R2] = regs[REG_R3]; /* argv */
}
break;
#endif
@@ -285,7 +318,7 @@ int up_svcall(int irq, FAR void *context)
/* Verify the the SYS call number is within range */
DEBUGASSERT(current_regs[REG_R0] < SYS_maxsyscall);
DEBUGASSERT(regs[REG_R0] < SYS_maxsyscall);
/* Make sure that we got here from an unprivileged thread and that
* there is a no saved syscall return address.
@@ -296,13 +329,13 @@ int up_svcall(int irq, FAR void *context)
/* Setup to return to dispatch_syscall in privileged mode. */
rtcb->xcp.sysreturn = regs[REG_PC];
regs[REG_PC] = (uint32_t)dispatch_syscall;
current_regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
rtcb->xcp.sysreturn = regs[REG_PC];
regs[REG_PC] = (uint32_t)dispatch_syscall;
regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
/* Offset R0 to account for the reserved values */
current_regs[REG_R0] -= CONFIG_SYS_RESERVED;
regs[REG_R0] -= CONFIG_SYS_RESERVED;
#else
slldbg("ERROR: Bad SYS call: %d\n", regs[REG_R0]);
#endif
+10 -3
View File
@@ -57,9 +57,9 @@
#ifdef CONFIG_NUTTX_KERNEL
# ifndef CONFIG_SYS_RESERVED
# error "CONFIG_SYS_RESERVED must be defined to the value 4"
# elif CONFIG_SYS_RESERVED != 4
# error "CONFIG_SYS_RESERVED must have the value 4"
# error "CONFIG_SYS_RESERVED must be defined to the value 5"
# elif CONFIG_SYS_RESERVED != 5
# error "CONFIG_SYS_RESERVED must have the value 5"
# endif
#endif
@@ -93,6 +93,13 @@
*/
#define SYS_syscall_return (3)
/* SYS call 3:
*
* void up_task_start(main_t taskentry, int argc, FAR char *argv[]) noreturn_function;
*/
#define SYS_task_start (4)
#endif
/************************************************************************************
+7 -50
View File
@@ -126,63 +126,20 @@ void up_initial_state(struct tcb_s *tcb)
#endif
#endif /* CONFIG_PIC */
#ifdef CONFIG_ARMV7M_CMNVECTOR
/* Set privileged- or unprivileged-mode, depending on how NuttX is
* configured and what kind of thread is being started.
*
* If the kernel build is not selected, then all threads run in
* privileged thread mode.
*
* If FPU support is not configured, set the bit that indicates that
* the context does not include the volatile FP registers.
/* All tasks start via a stub function in kernel space. So all
* tasks must start in privileged thread mode. If CONFIG_NUTTX_KERNEL
* is defined, then that stub function will switch to unprivileged
* mode before transferring control to the user task.
*/
xcp->regs[REG_EXC_RETURN] = EXC_RETURN_BASE | EXC_RETURN_THREAD_MODE;
xcp->regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
#ifndef CONFIG_ARCH_FPU
xcp->regs[REG_EXC_RETURN] |= EXC_RETURN_STD_CONTEXT;
#else
#if defined(CONFIG_ARMV7M_CMNVECTOR) && defined(CONFIG_ARCH_FPU)
xcp->regs[REG_FPSCR] = 0; // XXX initial FPSCR should be configurable
xcp->regs[REG_FPReserved] = 0;
#endif /* CONFIG_ARCH_FPU */
#ifdef CONFIG_NUTTX_KERNEL
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
{
/* It is a normal task or a pthread. Set user mode */
xcp->regs[REG_EXC_RETURN] |= EXC_RETURN_PROCESS_STACK;
}
#endif /* CONFIG_NUTTX_KERNEL */
#else /* CONFIG_ARMV7M_CMNVECTOR */
/* Set privileged- or unprivileged-mode, depending on how NuttX is
* configured and what kind of thread is being started.
*
* If the kernel build is not selected, then all threads run in
* privileged thread mode.
*/
#ifdef CONFIG_NUTTX_KERNEL
if ((tcb->flags & TCB_FLAG_TTYPE_MASK) == TCB_FLAG_TTYPE_KERNEL)
{
/* It is a kernel thread.. set privileged thread mode */
xcp->regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
}
else
{
/* It is a normal task or a pthread. Set user mode */
xcp->regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
}
#endif /* CONFIG_NUTTX_KERNEL */
#endif /* CONFIG_ARMV7M_CMNVECTOR */
#endif /* CONFIG_ARMV7M_CMNVECTOR && CONFIG_ARCH_FPU */
/* Enable or disable interrupts, based on user configuration */
+45 -14
View File
@@ -237,9 +237,9 @@ int up_svcall(int irq, FAR void *context)
}
break;
/* R0=SYS_syscall_return: This a switch context command:
/* R0=SYS_syscall_return: This a syscall return command:
*
* void up_sycall_return(void);
* void up_syscall_return(void);
*
* At this point, the following values are saved in context:
*
@@ -256,23 +256,54 @@ int up_svcall(int irq, FAR void *context)
/* Make sure that there is a saved syscall return address. */
svcdbg("sysreturn: %08x excreturn: %08x\n",
rtcb->xcp.sysreturn, rtcb->xcp.excreturn);
DEBUGASSERT(rtcb->xcp.sysreturn != 0);
/* Setup to return to the saved syscall return address in
* the original mode.
*/
current_regs[REG_PC] = rtcb->xcp.sysreturn;
current_regs[REG_EXC_RETURN] = rtcb->xcp.excreturn;
rtcb->xcp.sysreturn = 0;
regs[REG_PC] = rtcb->xcp.sysreturn;
regs[REG_EXC_RETURN] = rtcb->xcp.excreturn;
rtcb->xcp.sysreturn = 0;
/* The return value must be in R0-R1. dispatch_syscall() temporarily
* moved the value for R0 into R2.
*/
current_regs[REG_R0] = current_regs[REG_R2];
regs[REG_R0] = regs[REG_R2];
}
break;
#endif
/* R0=SYS_task_start: This a user task start
*
* void up_task_start(main_t taskentry, int argc, FAR char *argv[]) noreturn_function;
*
* At this point, the following values are saved in context:
*
* R0 = SYS_task_start
* R1 = taskentry
* R2 = argc
* R3 = argv
*/
#ifdef CONFIG_NUTTX_KERNEL
case SYS_task_start:
{
/* Set up to return to the user-space task start-up function in
* unprivileged mode.
*/
regs[REG_PC] = (uint32_t)USERSPACE->task_startup;
regs[REG_EXC_RETURN] = EXC_RETURN_UNPRIVTHR;
/* Change the paramter ordering to match the expection of struct
* userpace_s task_startup:
*/
regs[REG_R0] = regs[REG_R1]; /* Task entry */
regs[REG_R1] = regs[REG_R2]; /* argc */
regs[REG_R2] = regs[REG_R3]; /* argv */
}
break;
#endif
@@ -289,7 +320,7 @@ int up_svcall(int irq, FAR void *context)
/* Verify that the SYS call number is within range */
DEBUGASSERT(current_regs[REG_R0] < SYS_maxsyscall);
DEBUGASSERT(regs[REG_R0] < SYS_maxsyscall);
/* Make sure that we got here that there is a no saved syscall
* return address. We cannot yet handle nested system calls.
@@ -299,15 +330,15 @@ int up_svcall(int irq, FAR void *context)
/* Setup to return to dispatch_syscall in privileged mode. */
rtcb->xcp.sysreturn = regs[REG_PC];
rtcb->xcp.excreturn = current_regs[REG_EXC_RETURN];
rtcb->xcp.sysreturn = regs[REG_PC];
rtcb->xcp.excreturn = regs[REG_EXC_RETURN];
current_regs[REG_PC] = (uint32_t)dispatch_syscall;
current_regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
regs[REG_PC] = (uint32_t)dispatch_syscall;
regs[REG_EXC_RETURN] = EXC_RETURN_PRIVTHR;
/* Offset R0 to account for the reserved values */
current_regs[REG_R0] -= CONFIG_SYS_RESERVED;
regs[REG_R0] -= CONFIG_SYS_RESERVED;
#else
slldbg("ERROR: Bad SYS call: %d\n", regs[REG_R0]);
#endif
+96
View File
@@ -0,0 +1,96 @@
/****************************************************************************
* arch/arm/src/common/up_task_start.c
*
* Copyright (C) 2013 Gregory Nutt. All rights reserved.
* Author: Gregory Nutt <gnutt@nuttx.org>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
*
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in
* the documentation and/or other materials provided with the
* distribution.
* 3. Neither the name NuttX nor the names of its contributors may be
* used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
****************************************************************************/
/****************************************************************************
* Included Files
****************************************************************************/
#include <nuttx/config.h>
#include <nuttx/arch.h>
#include "svcall.h"
#include "up_internal.h"
#ifdef CONFIG_NUTTX_KERNEL
/****************************************************************************
* Pre-processor Definitions
****************************************************************************/
/****************************************************************************
* Private Data
****************************************************************************/
/****************************************************************************
* Private Functions
****************************************************************************/
/****************************************************************************
* Public Functions
****************************************************************************/
/****************************************************************************
* Name: up_task_start
*
* Description:
* In this kernel mode build, this function will be called to execute a
* task in user-space. When the task is first started, a kernel-mode
* stub will first run to perform some housekeeping functions. This
* kernel-mode stub will then be called transfer control to the user-mode
* task.
*
* Normally the a user-mode start-up stub will also execute before the
* task actually starts. See libc/sched/task_startup.c
*
* Input Parameters:
* taskentry - The user-space entry point of the task.
* argc - The number of parameters being passed.
* argv - The parameters being passed. These lie in kernel-space memory
* and will have to be reallocated in user-space memory.
*
* Returned Value:
* This function should not return. It should call the user-mode start-up
* stub and that stub should call exit if/when the user task terminates.
*
****************************************************************************/
void up_task_start(main_t taskentry, int argc, FAR char *argv[])
{
/* Let sys_call3() do all of the work */
sys_call3(SYS_task_start, (uintptr_t)taskentry, (uintptr_t)argc, (uintptr_t)argv);
}
#endif /* CONFIG_NUTTX_KERNEL */
+1 -1
View File
@@ -64,7 +64,7 @@ CMN_ASRCS += up_memcpy.S
endif
ifeq ($(CONFIG_NUTTX_KERNEL),y)
CMN_CSRCS += up_mpu.c
CMN_CSRCS += up_mpu.c up_task_start.c
endif
ifeq ($(CONFIG_NET),y)
+20 -20
View File
@@ -35,28 +35,28 @@
# The start-up, "head", file
HEAD_ASRC = sam3u_vectors.S
HEAD_ASRC = sam3u_vectors.S
# Common ARM and Cortex-M3 files
CMN_ASRCS = up_saveusercontext.S up_fullcontextrestore.S up_switchcontext.S \
vfork.S
CMN_CSRCS = up_assert.c up_blocktask.c up_copystate.c up_createstack.c \
up_mdelay.c up_udelay.c up_exit.c up_idle.c up_initialize.c \
up_initialstate.c up_interruptcontext.c up_memfault.c up_modifyreg8.c \
up_modifyreg16.c up_modifyreg32.c up_releasepending.c \
up_releasestack.c up_reprioritizertr.c up_schedulesigaction.c \
up_sigdeliver.c up_unblocktask.c up_usestack.c up_doirq.c \
up_hardfault.c up_svcall.c up_vfork.c
CMN_ASRCS = up_saveusercontext.S up_fullcontextrestore.S up_switchcontext.S
CMN_ASRCS += vfork.S
CMN_CSRCS = up_assert.c up_blocktask.c up_copystate.c up_createstack.c
CMN_CSRCS += up_mdelay.c up_udelay.c up_exit.c up_idle.c up_initialize.c
CMN_CSRCS += up_initialstate.c up_interruptcontext.c up_memfault.c up_modifyreg8.c
CMN_CSRCS += up_modifyreg16.c up_modifyreg32.c up_releasepending.c
CMN_CSRCS += up_releasestack.c up_reprioritizertr.c up_schedulesigaction.c
CMN_CSRCS += up_sigdeliver.c up_unblocktask.c up_usestack.c up_doirq.c
CMN_CSRCS += up_hardfault.c up_svcall.c up_vfork.c
# Configuration-dependent common files
ifeq ($(CONFIG_ARCH_MEMCPY),y)
CMN_ASRCS += up_memcpy.S
CMN_ASRCS += up_memcpy.S
endif
ifeq ($(CONFIG_NUTTX_KERNEL),y)
CMN_CSRCS += up_mpu.c
CMN_CSRCS += up_mpu.c up_task_start.c
endif
ifeq ($(CONFIG_ELF),y)
@@ -65,25 +65,25 @@ endif
# Required SAM3U files
CHIP_ASRCS =
CHIP_CSRCS = sam3u_allocateheap.c sam3u_clockconfig.c sam3u_gpioirq.c \
sam3u_irq.c sam3u_lowputc.c sam3u_pio.c sam3u_serial.c \
sam3u_start.c sam3u_timerisr.c
CHIP_ASRCS =
CHIP_CSRCS = sam3u_allocateheap.c sam3u_clockconfig.c sam3u_gpioirq.c
CHIP_CSRCS += sam3u_irq.c sam3u_lowputc.c sam3u_pio.c sam3u_serial.c
CHIP_CSRCS += sam3u_start.c sam3u_timerisr.c
# Configuration-dependent SAM3U files
ifeq ($(CONFIG_NUTTX_KERNEL),y)
CHIP_CSRCS += sam3u_userspace.c sam3u_mpuinit.c
CHIP_CSRCS += sam3u_userspace.c sam3u_mpuinit.c
endif
ifeq ($(CONFIG_SAM3U_DMA),y)
CHIP_CSRCS += sam3u_dmac.c
CHIP_CSRCS += sam3u_dmac.c
endif
ifeq ($(CONFIG_SAM3U_HSMCI),y)
CHIP_CSRCS += sam3u_hsmci.c
CHIP_CSRCS += sam3u_hsmci.c
endif
ifeq ($(CONFIG_SAM3U_SPI),y)
CHIP_CSRCS += sam3u_spi.c
CHIP_CSRCS += sam3u_spi.c
endif