summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2013-03-14 22:44:06 +0000
committerpatacongo <patacongo@42af7a65-404d-4744-a932-0658087f49c3>2013-03-14 22:44:06 +0000
commit256ff9a480238dfd1d30b7dfb3d61835991b3ee7 (patch)
tree3c62d071240bc4d2f9df1cd127113b94d7fbf332
parentc601d953ae2de87bc41d3666f6b510805bf4e67b (diff)
downloadnuttx-256ff9a480238dfd1d30b7dfb3d61835991b3ee7.tar.gz
nuttx-256ff9a480238dfd1d30b7dfb3d61835991b3ee7.tar.bz2
nuttx-256ff9a480238dfd1d30b7dfb3d61835991b3ee7.zip
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
-rw-r--r--nuttx/ChangeLog8
-rw-r--r--nuttx/arch/arm/src/armv6-m/svcall.h13
-rw-r--r--nuttx/arch/arm/src/armv6-m/up_initialstate.c23
-rw-r--r--nuttx/arch/arm/src/armv6-m/up_svcall.c55
-rw-r--r--nuttx/arch/arm/src/armv7-m/svcall.h13
-rw-r--r--nuttx/arch/arm/src/armv7-m/up_initialstate.c57
-rw-r--r--nuttx/arch/arm/src/armv7-m/up_svcall.c59
-rw-r--r--nuttx/arch/arm/src/common/up_task_start.c96
-rw-r--r--nuttx/arch/arm/src/lpc17xx/Make.defs2
-rw-r--r--nuttx/arch/arm/src/sam3u/Make.defs40
-rw-r--r--nuttx/configs/open1788/kernel/up_userspace.c5
-rwxr-xr-xnuttx/configs/open1788/knsh/defconfig2
-rwxr-xr-xnuttx/configs/sam3u-ek/knsh/defconfig2
-rw-r--r--nuttx/include/nuttx/arch.h29
-rw-r--r--nuttx/include/nuttx/sched.h68
-rw-r--r--nuttx/include/nuttx/userspace.h4
-rw-r--r--nuttx/libc/sched/Make.defs4
-rw-r--r--nuttx/libc/sched/task_startup.c104
-rw-r--r--nuttx/sched/pthread_create.c9
-rw-r--r--nuttx/sched/task_start.c27
20 files changed, 476 insertions, 144 deletions
diff --git a/nuttx/ChangeLog b/nuttx/ChangeLog
index 67c45a41a..9cb94e79e 100644
--- a/nuttx/ChangeLog
+++ b/nuttx/ChangeLog
@@ -4322,3 +4322,11 @@
configuration for the WaveShare Open1788 board. (2013-03-11)
* arch/arm/src/armv7-m/up_mpu.c: Several fixes to MPU logic.
(2013-03-12).
+ * arch/arm, configs/sam3u-ek, configs/open1788: Fix memory map for
+ kernel mode build; Some regions were overlapping. (2013-03-13).
+ * arch/: Rename g_heapbase to g_idle_topstack. This is the same value
+ however: The top of the IDLE stack is the same as the base of the
+ heap in the flat build. But not in the kernel build: The base of
+ the heap is elsewhere so the naming was wrong. (2013-03-13).
+ * libc/stdlib/lib_itoa.c: Implementation of itoa() contributed by
+ Ryan Sundberg. (2013-03-14). \ No newline at end of file
diff --git a/nuttx/arch/arm/src/armv6-m/svcall.h b/nuttx/arch/arm/src/armv6-m/svcall.h
index 46a385631..f453cad55 100644
--- a/nuttx/arch/arm/src/armv6-m/svcall.h
+++ b/nuttx/arch/arm/src/armv6-m/svcall.h
@@ -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
/************************************************************************************
diff --git a/nuttx/arch/arm/src/armv6-m/up_initialstate.c b/nuttx/arch/arm/src/armv6-m/up_initialstate.c
index 3e7e3a48f..646d148c1 100644
--- a/nuttx/arch/arm/src/armv6-m/up_initialstate.c
+++ b/nuttx/arch/arm/src/armv6-m/up_initialstate.c
@@ -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 */
diff --git a/nuttx/arch/arm/src/armv6-m/up_svcall.c b/nuttx/arch/arm/src/armv6-m/up_svcall.c
index b1bf1e4ec..f36e92297 100644
--- a/nuttx/arch/arm/src/armv6-m/up_svcall.c
+++ b/nuttx/arch/arm/src/armv6-m/up_svcall.c
@@ -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
diff --git a/nuttx/arch/arm/src/armv7-m/svcall.h b/nuttx/arch/arm/src/armv7-m/svcall.h
index 13b9935a0..f6b068f86 100644
--- a/nuttx/arch/arm/src/armv7-m/svcall.h
+++ b/nuttx/arch/arm/src/armv7-m/svcall.h
@@ -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
/************************************************************************************
diff --git a/nuttx/arch/arm/src/armv7-m/up_initialstate.c b/nuttx/arch/arm/src/armv7-m/up_initialstate.c
index 6f9abcd31..41828af5b 100644
--- a/nuttx/arch/arm/src/armv7-m/up_initialstate.c
+++ b/nuttx/arch/arm/src/armv7-m/up_initialstate.c
@@ -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 */
diff --git a/nuttx/arch/arm/src/armv7-m/up_svcall.c b/nuttx/arch/arm/src/armv7-m/up_svcall.c
index 8321cd3bb..8f7efc1da 100644
--- a/nuttx/arch/arm/src/armv7-m/up_svcall.c
+++ b/nuttx/arch/arm/src/armv7-m/up_svcall.c
@@ -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
diff --git a/nuttx/arch/arm/src/common/up_task_start.c b/nuttx/arch/arm/src/common/up_task_start.c
new file mode 100644
index 000000000..c006f9595
--- /dev/null
+++ b/nuttx/arch/arm/src/common/up_task_start.c
@@ -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 */
diff --git a/nuttx/arch/arm/src/lpc17xx/Make.defs b/nuttx/arch/arm/src/lpc17xx/Make.defs
index 679e51a5f..82ce2c4b6 100644
--- a/nuttx/arch/arm/src/lpc17xx/Make.defs
+++ b/nuttx/arch/arm/src/lpc17xx/Make.defs
@@ -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)
diff --git a/nuttx/arch/arm/src/sam3u/Make.defs b/nuttx/arch/arm/src/sam3u/Make.defs
index 1255af237..918d40148 100644
--- a/nuttx/arch/arm/src/sam3u/Make.defs
+++ b/nuttx/arch/arm/src/sam3u/Make.defs
@@ -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
diff --git a/nuttx/configs/open1788/kernel/up_userspace.c b/nuttx/configs/open1788/kernel/up_userspace.c
index 62c83e735..e2cb8c013 100644
--- a/nuttx/configs/open1788/kernel/up_userspace.c
+++ b/nuttx/configs/open1788/kernel/up_userspace.c
@@ -44,6 +44,7 @@
#include <nuttx/userspace.h>
#include <nuttx/wqueue.h>
#include <nuttx/mm.h>
+#include <nuttx/sched.h>
#if defined(CONFIG_NUTTX_KERNEL) && !defined(__KERNEL__)
@@ -101,6 +102,10 @@ const struct userspace_s userspace __attribute__ ((section (".userspace"))) =
.us_bssstart = (uintptr_t)&_sbss,
.us_bssend = (uintptr_t)&_ebss,
+ /* Task/thread startup stubs */
+
+ .task_startup = task_startup,
+
/* Memory manager entry points (declared in include/nuttx/mm.h) */
.mm_initialize = umm_initialize,
diff --git a/nuttx/configs/open1788/knsh/defconfig b/nuttx/configs/open1788/knsh/defconfig
index 22042073c..50b4e3d6d 100755
--- a/nuttx/configs/open1788/knsh/defconfig
+++ b/nuttx/configs/open1788/knsh/defconfig
@@ -344,7 +344,7 @@ CONFIG_IDLETHREAD_STACKSIZE=1024
CONFIG_USERMAIN_STACKSIZE=2048
CONFIG_PTHREAD_STACK_MIN=256
CONFIG_PTHREAD_STACK_DEFAULT=2048
-CONFIG_SYS_RESERVED=4
+CONFIG_SYS_RESERVED=5
#
# Device Drivers
diff --git a/nuttx/configs/sam3u-ek/knsh/defconfig b/nuttx/configs/sam3u-ek/knsh/defconfig
index 93d436e54..7146ef6bf 100755
--- a/nuttx/configs/sam3u-ek/knsh/defconfig
+++ b/nuttx/configs/sam3u-ek/knsh/defconfig
@@ -249,7 +249,7 @@ CONFIG_IDLETHREAD_STACKSIZE=1024
CONFIG_USERMAIN_STACKSIZE=2048
CONFIG_PTHREAD_STACK_MIN=256
CONFIG_PTHREAD_STACK_DEFAULT=2048
-CONFIG_SYS_RESERVED=4
+CONFIG_SYS_RESERVED=5
#
# Device Drivers
diff --git a/nuttx/include/nuttx/arch.h b/nuttx/include/nuttx/arch.h
index 8f3a906bb..7bdd90ade 100644
--- a/nuttx/include/nuttx/arch.h
+++ b/nuttx/include/nuttx/arch.h
@@ -366,6 +366,35 @@ void up_schedule_sigaction(FAR struct tcb_s *tcb, sig_deliver_t sigdeliver);
#endif
/****************************************************************************
+ * 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.
+ *
+ ****************************************************************************/
+
+#ifdef CONFIG_NUTTX_KERNEL
+void up_task_start(main_t taskentry, int argc, FAR char *argv[]) noreturn_function;
+#endif
+
+/****************************************************************************
* Name: up_allocate_heap
*
* Description:
diff --git a/nuttx/include/nuttx/sched.h b/nuttx/include/nuttx/sched.h
index 62ad1cc47..f707e8269 100644
--- a/nuttx/include/nuttx/sched.h
+++ b/nuttx/include/nuttx/sched.h
@@ -600,11 +600,24 @@ extern "C"
* Public Function Prototypes
********************************************************************************/
-/* TCB helpers */
+/* TCB helpers ******************************************************************/
+/* sched_self() returns the TCB of the currently running task (i.e., the
+ * caller)
+ */
FAR struct tcb_s *sched_self(void);
-/* File system helpers */
+/* sched_foreach will enumerate over each task and provide the TCB of each task
+ * or thread to a callback function. Interrupts will be disabled throughout
+ * this enumeration!
+ */
+
+void sched_foreach(sched_foreach_t handler, FAR void *arg);
+
+/* File system helpers **********************************************************/
+/* These functions all extract lists from the group structure assocated with the
+ * currently executing task.
+ */
#if CONFIG_NFILE_DESCRIPTORS > 0
FAR struct filelist *sched_getfiles(void);
@@ -617,13 +630,32 @@ FAR struct streamlist *sched_getstreams(void);
FAR struct socketlist *sched_getsockets(void);
#endif /* CONFIG_NSOCKET_DESCRIPTORS */
-/* Setup up a start hook */
+/********************************************************************************
+ * Name: task_starthook
+ *
+ * Description:
+ * Configure a start hook... a function that will be called on the thread
+ * of the new task before the new task's main entry point is called.
+ * The start hook is useful, for example, for setting up automatic
+ * configuration of C++ constructors.
+ *
+ * Inputs:
+ * tcb - The new, unstarted task task that needs the start hook
+ * starthook - The pointer to the start hook function
+ * arg - The argument to pass to the start hook function.
+ *
+ * Return:
+ * None
+ *
+ ********************************************************************************/
#ifdef CONFIG_SCHED_STARTHOOK
-void task_starthook(FAR struct task_tcb_s *tcb, starthook_t starthook, FAR void *arg);
+void task_starthook(FAR struct task_tcb_s *tcb, starthook_t starthook,
+ FAR void *arg);
#endif
-/* Internal vfork support.The overall sequence is:
+/********************************************************************************
+ * Internal vfork support. The overall sequence is:
*
* 1) User code calls vfork(). vfork() is provided in architecture-specific
* code.
@@ -643,18 +675,32 @@ void task_starthook(FAR struct task_tcb_s *tcb, starthook_t starthook, FAR void
* 6) task_vforkstart() then executes the child thread.
*
* task_vforkabort() may be called if an error occurs between steps 3 and 6.
- */
+ *
+ ********************************************************************************/
FAR struct task_tcb_s *task_vforksetup(start_t retaddr);
pid_t task_vforkstart(FAR struct task_tcb_s *child);
void task_vforkabort(FAR struct task_tcb_s *child, int errcode);
-/* sched_foreach will enumerate over each task and provide the
- * TCB of each task to a user callback functions. Interrupts
- * will be disabled throughout this enumeration!
- */
+/****************************************************************************
+ * Name: task_startup
+ *
+ * Description:
+ * This function is the user-space, task startup function. It is called
+ * from up_task_start() in user-mode.
+ *
+ * Inputs:
+ * entrypt - The user-space address of the task entry point
+ * argc and argv - Standard arguments for the task entry point
+ *
+ * Return:
+ * None. This function does not return.
+ *
+ ****************************************************************************/
-void sched_foreach(sched_foreach_t handler, FAR void *arg);
+#if defined(CONFIG_NUTTX_KERNEL) && !defined(__KERNEL__)
+void task_startup(main_t entrypt, int argc, FAR char *argv[]);
+#endif
#undef EXTERN
#if defined(__cplusplus)
diff --git a/nuttx/include/nuttx/userspace.h b/nuttx/include/nuttx/userspace.h
index d2192fe5f..40fe656b0 100644
--- a/nuttx/include/nuttx/userspace.h
+++ b/nuttx/include/nuttx/userspace.h
@@ -111,6 +111,10 @@ struct userspace_s
uintptr_t us_bssstart;
uintptr_t us_bssend;
+ /* Task/thread startup stubs */
+
+ void (*task_startup)(main_t entrypt, int argc, FAR char *argv[]) noreturn_function;
+
/* Memory manager entry points */
void (*mm_initialize)(FAR void *heap_start, size_t heap_size);
diff --git a/nuttx/libc/sched/Make.defs b/nuttx/libc/sched/Make.defs
index d2356db0f..310a6259b 100644
--- a/nuttx/libc/sched/Make.defs
+++ b/nuttx/libc/sched/Make.defs
@@ -37,6 +37,10 @@
CSRCS += sched_getprioritymax.c sched_getprioritymin.c
+ifeq ($(CONFIG_NUTTX_KERNEL),y)
+CSRCS += task_startup.c
+endif
+
# Add the sched directory to the build
DEPPATH += --dep-path sched
diff --git a/nuttx/libc/sched/task_startup.c b/nuttx/libc/sched/task_startup.c
new file mode 100644
index 000000000..8795faa7a
--- /dev/null
+++ b/nuttx/libc/sched/task_startup.c
@@ -0,0 +1,104 @@
+/****************************************************************************
+ * libc/sched/task_startup.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 <stdlib.h>
+#include <assert.h>
+
+#include <nuttx/sched.h>
+
+#if defined(CONFIG_NUTTX_KERNEL) && !defined(__KERNEL__)
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Type Declarations
+ ****************************************************************************/
+
+/****************************************************************************
+ * Global Variables
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Variables
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Function Prototypes
+ ****************************************************************************/
+
+/****************************************************************************
+ * Private Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+/****************************************************************************
+ * Name: task_startup
+ *
+ * Description:
+ * This function is the user-space, task startup function. It is called
+ * from up_task_start() in user-mode.
+ *
+ * Inputs:
+ * entrypt - The user-space address of the task entry point
+ * argc and argv - Standard arguments for the task entry point
+ *
+ * Return:
+ * None. This function does not return.
+ *
+ ****************************************************************************/
+
+void task_startup(main_t entrypt, int argc, FAR char *argv[])
+{
+ DEBUGASSERT(entrypt);
+
+ /* Call the 'main' entry point passing argc and argv, calling exit()
+ * if/when the task returns.
+ */
+
+ exit(entrypt(argc, argv));
+}
+
+#endif /* CONFIG_NUTTX_KERNEL && !__KERNEL__ */
diff --git a/nuttx/sched/pthread_create.c b/nuttx/sched/pthread_create.c
index 346f51411..3fcb7eea1 100644
--- a/nuttx/sched/pthread_create.c
+++ b/nuttx/sched/pthread_create.c
@@ -174,9 +174,7 @@ static void pthread_start(void)
DEBUGASSERT(group && pjoin);
- /* Sucessfully spawned, add the pjoin to our data set.
- * Don't re-enable pre-emption until this is done.
- */
+ /* Sucessfully spawned, add the pjoin to our data set. */
(void)pthread_takesemaphore(&group->tg_joinsem);
pthread_addjoininfo(group, pjoin);
@@ -187,10 +185,7 @@ static void pthread_start(void)
pjoin->started = true;
(void)pthread_givesemaphore(&pjoin->data_sem);
- /* Pass control to the thread entry point. The argument is
- * argv[1]. argv[0] (the thread name) and argv[2-4] are not made
- * available to the pthread.
- */
+ /* Pass control to the thread entry point. */
exit_status = (*ptcb->cmn.entry.pthread)(ptcb->arg);
diff --git a/nuttx/sched/task_start.c b/nuttx/sched/task_start.c
index 8e012b515..e9667d486 100644
--- a/nuttx/sched/task_start.c
+++ b/nuttx/sched/task_start.c
@@ -43,10 +43,13 @@
#include <sched.h>
#include <debug.h>
+#include <nuttx/arch.h>
+#include <nuttx/sched.h>
+
#include "os_internal.h"
/****************************************************************************
- * Definitions
+ * Pre-processor Definitions
****************************************************************************/
/****************************************************************************
@@ -92,6 +95,7 @@
void task_start(void)
{
FAR struct task_tcb_s *tcb = (FAR struct task_tcb_s*)g_readytorun.head;
+ int exitcode;
int argc;
DEBUGASSERT((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD);
@@ -117,9 +121,24 @@ void task_start(void)
}
}
- /* Call the 'main' entry point passing argc and argv. If/when
- * the task returns.
+ /* Call the 'main' entry point passing argc and argv. In the kernel build
+ * this has to be handled differently if we are starting a user-space task;
+ * we have to switch to user-mode before calling the task.
*/
- exit(tcb->cmn.entry.main(argc, tcb->argv));
+#ifdef CONFIG_NUTTX_KERNEL
+ if ((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
+ {
+ up_task_start(tcb->cmn.entry.main, argc, tcb->argv);
+ exitcode = EXIT_FAILURE; /* Should not get here */
+ }
+ else
+#endif
+ {
+ exitcode = tcb->cmn.entry.main(argc, tcb->argv);
+ }
+
+ /* Call exit() if/when the task returns */
+
+ exit(exitcode);
}