From 89f3c966e75b483eaeb4af2a85721f9b1d8ed4e2 Mon Sep 17 00:00:00 2001 From: patacongo Date: Mon, 7 Jan 2013 21:41:20 +0000 Subject: Add ostest vfork test (does not work yet) git-svn-id: svn://svn.code.sf.net/p/nuttx/code/trunk@5488 42af7a65-404d-4744-a932-0658087f49c3 --- apps/examples/ostest/Makefile | 4 ++ apps/examples/ostest/ostest.h | 6 +++ apps/examples/ostest/ostest_main.c | 6 +++ apps/examples/ostest/vfork.c | 99 ++++++++++++++++++++++++++++++++++++ nuttx/arch/Kconfig | 5 ++ nuttx/arch/arm/src/arm/vfork.S | 10 ++-- nuttx/arch/arm/src/armv7-m/vfork.S | 10 ++-- nuttx/arch/arm/src/common/up_vfork.c | 45 +++++++++++++--- nuttx/sched/sched_addprioritized.c | 2 +- nuttx/sched/sched_addreadytorun.c | 8 +-- nuttx/sched/task_vfork.c | 9 +++- 11 files changed, 182 insertions(+), 22 deletions(-) create mode 100644 apps/examples/ostest/vfork.c diff --git a/apps/examples/ostest/Makefile b/apps/examples/ostest/Makefile index 3d19f6a78..b7ba9a9a8 100644 --- a/apps/examples/ostest/Makefile +++ b/apps/examples/ostest/Makefile @@ -84,6 +84,10 @@ ifneq ($(CONFIG_DISABLE_POSIX_TIMERS),y) CSRCS += posixtimer.c endif +ifeq ($(CONFIG_ARCH_HAVE_VFORK),y) +CSRCS += vfork.c +endif + ifneq ($(CONFIG_DISABLE_SIGNALS),y) ifneq ($(CONFIG_DISABLE_PTHREAD),y) ifeq ($(CONFIG_PRIORITY_INHERITANCE),y) diff --git a/apps/examples/ostest/ostest.h b/apps/examples/ostest/ostest.h index a4af37f05..bc46a3860 100644 --- a/apps/examples/ostest/ostest.h +++ b/apps/examples/ostest/ostest.h @@ -163,6 +163,12 @@ extern void barrier_test(void); extern void priority_inheritance(void); +/* vfork.c ******************************************************************/ + +#ifdef CONFIG_ARCH_HAVE_VFORK +extern int vfork_test(void); +#endif + /* APIs exported (conditionally) by the OS specifically for testing of * priority inheritance */ diff --git a/apps/examples/ostest/ostest_main.c b/apps/examples/ostest/ostest_main.c index 46726d515..ca44353c3 100644 --- a/apps/examples/ostest/ostest_main.c +++ b/apps/examples/ostest/ostest_main.c @@ -409,6 +409,11 @@ static int user_main(int argc, char *argv[]) check_test_memory_usage(); #endif /* CONFIG_PRIORITY_INHERITANCE && !CONFIG_DISABLE_SIGNALS && !CONFIG_DISABLE_PTHREAD */ +#ifdef CONFIG_ARCH_HAVE_VFORK + printf("\nuser_main: vfork() test\n"); + vfork_test(); +#endif + /* Compare memory usage at time ostest_main started until * user_main exits. These should not be identical, but should * be similar enough that we can detect any serious OS memory @@ -428,6 +433,7 @@ static int user_main(int argc, char *argv[]) show_memory_usage(&g_mmbefore, &g_mmafter); #endif } + printf("user_main: Exitting\n"); return 0; } diff --git a/apps/examples/ostest/vfork.c b/apps/examples/ostest/vfork.c new file mode 100644 index 000000000..6c83047e3 --- /dev/null +++ b/apps/examples/ostest/vfork.c @@ -0,0 +1,99 @@ +/**************************************************************************** + * examples/ostest/vfork.c + * + * Copyright (C) 2013 Gregory Nutt. All rights reserved. + * Author: Gregory Nutt + * + * 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 + +#include +#include +#include +#include +#include + +#include "ostest.h" + +/**************************************************************************** + * Private Data + ****************************************************************************/ + +#if defined(CONFIG_ARCH_HAVE_VFORK) && !defined(CONFIG_DISABLE_SIGNALS) +static volatile bool g_vforkchild; +#endif + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +int vfork_test(void) +{ +#if defined(CONFIG_ARCH_HAVE_VFORK) && !defined(CONFIG_DISABLE_SIGNALS) + pid_t pid; + + g_vforkchild = false; + pid = vfork(); + if (pid == 0) + { + /* There is not very much that the child is permitted to do. Perhaps + * it can just set g_vforkchild. + */ + + g_vforkchild = true; + exit(0); + } + else if (pid < 0) + { + printf("vfork_test: vfork() failed: %d\n", errno); + return -1; + } + else + { + sleep(1); + if (g_vforkchild) + { + printf("vfork_test: Child %d ran successfully\n", pid); + } + else + { + printf("vfork_test: ERROR Child %d did not run\n", pid); + return -1; + } + } +#endif + + return 0; +} diff --git a/nuttx/arch/Kconfig b/nuttx/arch/Kconfig index f19228143..7d34b56f4 100644 --- a/nuttx/arch/Kconfig +++ b/nuttx/arch/Kconfig @@ -16,6 +16,7 @@ config ARCH_8051 config ARCH_ARM bool "ARM" select ARCH_HAVE_INTERRUPTSTACK + select ARCH_HAVE_VFORK ---help--- The ARM architectures @@ -124,6 +125,10 @@ config ADDRENV bool default n +config ARCH_HAVE_VFORK + bool + default n + config ARCH_STACKDUMP bool "Dump stack on assertions" default n diff --git a/nuttx/arch/arm/src/arm/vfork.S b/nuttx/arch/arm/src/arm/vfork.S index f0fe17f73..b498fd7f7 100644 --- a/nuttx/arch/arm/src/arm/vfork.S +++ b/nuttx/arch/arm/src/arm/vfork.S @@ -50,7 +50,7 @@ ************************************************************************************/ .file "vfork.S" - .globl task_vfork + .globl up_vfork /************************************************************************************ * Public Functions @@ -66,7 +66,7 @@ * from the function in which vfork() was called, or calls any other function before * successfully calling _exit() or one of the exec family of functions. * - * This thin layer implements vfork by simply calling task_vfork() with the vfork() + * This thin layer implements vfork by simply calling up_vfork() with the vfork() * context as an argument. The overall sequence is: * * 1) User code calls vfork(). vfork() collects context information and @@ -121,12 +121,12 @@ vfork: str r0, [sp, #VFORK_SP_OFFSET] str lr, [sp, #VFORK_LR_OFFSET] - /* Then, call task_vfork(), passing it a pointer to the stack structure */ + /* Then, call up_vfork(), passing it a pointer to the stack structure */ mov r0, sp - bl task_vfork + bl up_vfork - /* Release the stack data and return the value returned by task_vfork */ + /* Release the stack data and return the value returned by up_vfork */ add sp, sp, #VFORK_SIZEOF mov pc, lr diff --git a/nuttx/arch/arm/src/armv7-m/vfork.S b/nuttx/arch/arm/src/armv7-m/vfork.S index aceded400..0d9e144cd 100644 --- a/nuttx/arch/arm/src/armv7-m/vfork.S +++ b/nuttx/arch/arm/src/armv7-m/vfork.S @@ -52,7 +52,7 @@ .syntax unified .thumb .file "vfork.S" - .globl task_vfork + .globl up_vfork /************************************************************************************ * Public Functions @@ -68,7 +68,7 @@ * from the function in which vfork() was called, or calls any other function before * successfully calling _exit() or one of the exec family of functions. * - * This thin layer implements vfork by simply calling task_vfork() with the vfork() + * This thin layer implements vfork by simply calling up_vfork() with the vfork() * context as an argument. The overall sequence is: * * 1) User code calls vfork(). vfork() collects context information and @@ -124,12 +124,12 @@ vfork: str r0, [sp, #VFORK_SP_OFFSET] str lr, [sp, #VFORK_LR_OFFSET] - /* Then, call task_vfork(), passing it a pointer to the stack structure */ + /* Then, call up_vfork(), passing it a pointer to the stack structure */ mov r0, sp - bl task_vfork + bl up_vfork - /* Release the stack data and return the value returned by task_vfork */ + /* Release the stack data and return the value returned by up_vfork */ add sp, sp, #VFORK_SIZEOF bx lr diff --git a/nuttx/arch/arm/src/common/up_vfork.c b/nuttx/arch/arm/src/common/up_vfork.c index 404abd1f8..2e3c2d4a1 100644 --- a/nuttx/arch/arm/src/common/up_vfork.c +++ b/nuttx/arch/arm/src/common/up_vfork.c @@ -43,6 +43,7 @@ #include #include #include +#include #include #include @@ -129,17 +130,28 @@ pid_t up_vfork(struct vfork_s *context) _TCB *child; size_t stacksize; uint32_t newsp; + uint32_t newfp; uint32_t stackutil; int ret; + svdbg("r4:%08x r5:%08x r6:%08x r7:%08x\n", + context->r4, context->r5, context->r6, context->r7); + svdbg("r8:%08x r9:%08x r10:%08x\n", + context->r8, context->r9, context->r10); + svdbg("fp:%08x sp:%08x lr:%08x\n", + context->fp, context->sp, context->lr); + /* Allocate and initialize a TCB for the child task. */ - child = task_vforksetup((start_t)context->lr); + child = task_vforksetup((start_t)(context->lr & ~1)); if (!child) { + sdbg("task_vforksetup failed\n"); return (pid_t)ERROR; } + svdbg("Parent=%p Child=%p\n", parent, child); + /* Get the size of the parent task's stack. Due to alignment operations, * the adjusted stack size may be smaller than the stack size originally * requrested. @@ -152,15 +164,18 @@ pid_t up_vfork(struct vfork_s *context) ret = up_create_stack(child, stacksize); if (ret != OK) { + sdbg("up_create_stack failed: %d\n", ret); task_vforkabort(child, -ret); return (pid_t)ERROR; } /* How much of the parent's stack was utilized? */ - DEBUGASSERT(parent->adj_stack_ptr > context->sp); + DEBUGASSERT((uint32_t)parent->adj_stack_ptr > context->sp); stackutil = (uint32_t)parent->adj_stack_ptr - context->sp; + svdbg("stacksize:%d stackutil:%d\n", stacksize, stackutil); + /* Make some feeble effort to perserve the stack contents. This is * feeble because the stack surely contains invalid pointer and other * content that will not work in the child context. However, if the @@ -170,8 +185,26 @@ pid_t up_vfork(struct vfork_s *context) newsp = (uint32_t)child->adj_stack_ptr - stackutil; memcpy((void *)newsp, (const void *)context->sp, stackutil); - - /* Update the stack pointer, frame pointer, and voltile registers. When + + /* Was there a frame pointer in place before? */ + + if (context->fp <= (uint32_t)parent->adj_stack_ptr && + context->fp >= (uint32_t)parent->adj_stack_ptr - stacksize) + { + uint32_t frameutil = (uint32_t)parent->adj_stack_ptr - context->fp; + newfp = (uint32_t)child->adj_stack_ptr - frameutil; + } + else + { + newfp = context->fp; + } + + svdbg("Old stack base:%08x SP:%08x FP:%08x\n", + parent->adj_stack_ptr, context->sp, context->fp); + svdbg("New stack base:%08x SP:%08x FP:%08x\n", + child->adj_stack_ptr, newsp, newfp); + + /* Update the stack pointer, frame pointer, and voltile registers. When * the child TCB was initialized, all of the values were set to zero. * up_initial_state() altered a few values, but the return value in R0 * should be cleared to zero, providing the indication to the newly started @@ -185,8 +218,8 @@ pid_t up_vfork(struct vfork_s *context) child->xcp.regs[REG_R8] = context->r8; /* Volatile register r8 */ child->xcp.regs[REG_R9] = context->r9; /* Volatile register r9 */ child->xcp.regs[REG_R10] = context->r10; /* Volatile register r10 */ - child->xcp.regs[REG_FP] = context->fp; /* Frame pointer */ - child->xcp.regs[REG_SP] = context->sp; /* Stack pointer */ + child->xcp.regs[REG_FP] = newfp; /* Frame pointer */ + child->xcp.regs[REG_SP] = newsp; /* Stack pointer */ /* And, finally, start the child task. On a failure, task_vforkstart() * will discard the TCB by calling task_vforkabort(). diff --git a/nuttx/sched/sched_addprioritized.c b/nuttx/sched/sched_addprioritized.c index 8f19a4731..20178fb9c 100644 --- a/nuttx/sched/sched_addprioritized.c +++ b/nuttx/sched/sched_addprioritized.c @@ -114,7 +114,7 @@ bool sched_addprioritized(FAR _TCB *tcb, DSEG dq_queue_t *list) (next && sched_priority <= next->sched_priority); next = next->flink); - /* Add the tcb to the spot found in the list. Check if the tcb + /* Add the tcb to the spot found in the list. Check if the tcb * goes at the end of the list. NOTE: This could only happen if list * is the g_pendingtasks list! */ diff --git a/nuttx/sched/sched_addreadytorun.c b/nuttx/sched/sched_addreadytorun.c index f6117b6ff..1e1829343 100644 --- a/nuttx/sched/sched_addreadytorun.c +++ b/nuttx/sched/sched_addreadytorun.c @@ -84,8 +84,8 @@ * btcb - Points to the blocked TCB that is ready-to-run * * Return Value: - * true if the currently active task (the head of the - * g_readytorun list) has changed. + * true if the currently active task (the head of the g_readytorun list) + * has changed. * * Assumptions: * - The caller has established a critical section before @@ -104,7 +104,7 @@ bool sched_addreadytorun(FAR _TCB *btcb) bool ret; /* Check if pre-emption is disabled for the current running task and if - * the new ready-to-run task would cause the current running task to be + * the new ready-to-run task would cause the current running task to be * preempted. */ @@ -123,7 +123,7 @@ bool sched_addreadytorun(FAR _TCB *btcb) else if (sched_addprioritized(btcb, (FAR dq_queue_t*)&g_readytorun)) { - /* Information the instrumentation logic that we are switching tasks */ + /* Inform the instrumentation logic that we are switching tasks */ sched_note_switch(rtcb, btcb); diff --git a/nuttx/sched/task_vfork.c b/nuttx/sched/task_vfork.c index 93fcb46da..64f6f0636 100644 --- a/nuttx/sched/task_vfork.c +++ b/nuttx/sched/task_vfork.c @@ -43,6 +43,7 @@ #include #include #include +#include #include @@ -151,12 +152,14 @@ FAR _TCB *task_vforksetup(start_t retaddr) /* Initialize the task control block. This calls up_initial_state() */ + svdbg("Child priority=%d start=%p\n", priority, retaddr); ret = task_schedsetup(child, priority, retaddr, parent->entry.main); if (ret != OK) { goto errout_with_tcb; } + svdbg("parent=%p, returning child=%p\n", parent, child); return child; errout_with_tcb: @@ -210,10 +213,14 @@ errout_with_tcb: pid_t task_vforkstart(FAR _TCB *child) { +#if CONFIG_TASK_NAME_SIZE > 0 + _TCB *parent = (FAR _TCB *)g_readytorun.head; +#endif FAR const char *name; pid_t pid; int ret; + svdbg("Starting Child TCB=%p, parent=%p\n", child, g_readytorun.head); DEBUGASSERT(child); /* Setup to pass parameters to the new task */ @@ -221,7 +228,7 @@ pid_t task_vforkstart(FAR _TCB *child) #if CONFIG_TASK_NAME_SIZE > 0 name = parent->name; #else - name = ""; + name = NULL; #endif (void)task_argsetup(child, name, (const char **)NULL); -- cgit v1.2.3