示例#1
0
pid_t up_vfork(const struct vfork_s *context)
{
  _TCB *parent = (FAR _TCB *)g_readytorun.head;
  _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 & ~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.
   */

  stacksize = parent->adj_stack_size + CONFIG_STACK_ALIGNMENT - 1;

  /* Allocate the stack for the TCB */

  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?  The ARM uses
   * a push-down stack so that the current stack pointer should
   * be lower than the initial, adjusted stack pointer.  The
   * stack usage should be the difference between those two.
   */

  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 pointers and other
   * content that will not work in the child context.  However, if the
   * user follows all of the caveats of vfor() usage, even this feeble
   * effort is overkill.
   */

  newsp = (uint32_t)child->adj_stack_ptr - stackutil;
  memcpy((void *)newsp, (const void *)context->sp, stackutil);

  /* 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 volatile 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
  * child thread.
  */

  child->xcp.regs[REG_R4]  = context->r4;  /* Volatile register r4 */
  child->xcp.regs[REG_R5]  = context->r5;  /* Volatile register r5 */
  child->xcp.regs[REG_R6]  = context->r6;  /* Volatile register r6 */
  child->xcp.regs[REG_R7]  = context->r7;  /* Volatile register r7 */
  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]  = 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().
   */

  return task_vforkstart(child);
}
示例#2
0
pid_t up_vfork(const struct vfork_s *context)
{
  struct tcb_s *parent = (FAR struct tcb_s *)g_readytorun.head;
  struct task_tcb_s *child;
  size_t stacksize;
  uint32_t newsp;
  uint32_t newfp;
  uint32_t stackutil;
  int ret;

  svdbg("vfork context [%p]:\n", context);
  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 & ~1));
  if (!child)
    {
      sdbg("ERROR: task_vforksetup failed\n");
      return (pid_t)ERROR;
    }

  svdbg("TCBs: 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
   * requested.
   */

  stacksize = parent->adj_stack_size + CONFIG_STACK_ALIGNMENT - 1;

  /* Allocate the stack for the TCB */

  ret = up_create_stack((FAR struct tcb_s *)child, stacksize,
                        parent->flags & TCB_FLAG_TTYPE_MASK);
  if (ret != OK)
    {
      sdbg("ERROR: up_create_stack failed: %d\n", ret);
      task_vforkabort(child, -ret);
      return (pid_t)ERROR;
    }

  /* How much of the parent's stack was utilized?  The ARM uses
   * a push-down stack so that the current stack pointer should
   * be lower than the initial, adjusted stack pointer.  The
   * stack usage should be the difference between those two.
   */

  DEBUGASSERT((uint32_t)parent->adj_stack_ptr > context->sp);
  stackutil = (uint32_t)parent->adj_stack_ptr - context->sp;

  svdbg("Parent: stacksize:%d stackutil:%d\n", stacksize, stackutil);

  /* Make some feeble effort to preserve the stack contents.  This is
   * feeble because the stack surely contains invalid pointers and other
   * content that will not work in the child context.  However, if the
   * user follows all of the caveats of vfork() usage, even this feeble
   * effort is overkill.
   */

  newsp = (uint32_t)child->cmn.adj_stack_ptr - stackutil;
  memcpy((void *)newsp, (const void *)context->sp, stackutil);

  /* 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->cmn.adj_stack_ptr - frameutil;
    }
  else
    {
      newfp = context->fp;
    }

  svdbg("Parent: stack base:%08x SP:%08x FP:%08x\n",
        parent->adj_stack_ptr, context->sp, context->fp);
  svdbg("Child:  stack base:%08x SP:%08x FP:%08x\n",
        child->cmn.adj_stack_ptr, newsp, newfp);

 /* Update the stack pointer, frame pointer, and volatile 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
  * child thread.
  */

  child->cmn.xcp.regs[REG_R4]  = context->r4;  /* Volatile register r4 */
  child->cmn.xcp.regs[REG_R5]  = context->r5;  /* Volatile register r5 */
  child->cmn.xcp.regs[REG_R6]  = context->r6;  /* Volatile register r6 */
  child->cmn.xcp.regs[REG_R7]  = context->r7;  /* Volatile register r7 */
  child->cmn.xcp.regs[REG_R8]  = context->r8;  /* Volatile register r8 */
  child->cmn.xcp.regs[REG_R9]  = context->r9;  /* Volatile register r9 */
  child->cmn.xcp.regs[REG_R10] = context->r10; /* Volatile register r10 */
  child->cmn.xcp.regs[REG_FP]  = newfp;        /* Frame pointer */
  child->cmn.xcp.regs[REG_SP]  = newsp;        /* Stack pointer */

#ifdef CONFIG_LIB_SYSCALL
  /* If we got here via a syscall, then we are going to have to setup some
   * syscall return information as well.
   */

  if (parent->xcp.nsyscalls > 0)
    {
      int index;
      for (index = 0; index < parent->xcp.nsyscalls; index++)
        {
          child->cmn.xcp.syscall[index].sysreturn =
            parent->xcp.syscall[index].sysreturn;

          /* REVISIT:  This logic is *not* common. */

#if defined(CONFIG_ARCH_CORTEXA5) || defined(CONFIG_ARCH_CORTEXA8)
#  ifdef CONFIG_BUILD_KERNEL

          child->cmn.xcp.syscall[index].cpsr =
            parent->xcp.syscall[index].cpsr;

#  endif
#elif defined(CONFIG_ARCH_CORTEXM3) || defined(CONFIG_ARCH_CORTEXM4) || \
      defined(CONFIG_ARCH_CORTEXM0) || defined(CONFIG_ARCH_CORTEXM7)

          child->cmn.xcp.syscall[index].excreturn =
            parent->xcp.syscall[index].excreturn;
#else
#  error Missing logic
#endif
        }

      child->cmn.xcp.nsyscalls = parent->xcp.nsyscalls;
    }
#endif

  /* And, finally, start the child task.  On a failure, task_vforkstart()
   * will discard the TCB by calling task_vforkabort().
   */

  return task_vforkstart(child);
}
示例#3
0
pid_t up_vfork(const struct vfork_s *context)
{
  struct tcb_s *parent = (FAR struct tcb_s *)g_readytorun.head;
  struct task_tcb_s *child;
  size_t stacksize;
  uint32_t newsp;
#if CONFIG_MIPS32_FRAMEPOINTER
  uint32_t newfp;
#endif
  uint32_t stackutil;
  int ret;

  svdbg("s0:%08x s1:%08x s2:%08x s3:%08x s4:%08x\n",
        context->s0, context->s1, context->s2, context->s3, context->s4);
#if CONFIG_MIPS32_FRAMEPOINTER
  svdbg("s5:%08x s6:%08x s7:%08x\n",
        context->s5, context->s6, context->s7);
#ifdef MIPS32_SAVE_GP
  svdbg("fp:%08x sp:%08x ra:%08x gp:%08x\n",
        context->fp, context->sp, context->ra, context->gp);
#else
  svdbg("fp:%08x sp:%08x ra:%08x\n",
        context->fp context->sp, context->ra);
#endif
#else
  svdbg("s5:%08x s6:%08x s7:%08x s8:%08x\n",
        context->s5, context->s6, context->s7, context->s8);
#ifdef MIPS32_SAVE_GP
  svdbg("sp:%08x ra:%08x gp:%08x\n",
        context->sp, context->ra, context->gp);
#else
  svdbg("sp:%08x ra:%08x\n",
        context->sp, context->ra);
#endif
#endif

  /* Allocate and initialize a TCB for the child task. */

  child = task_vforksetup((start_t)context->ra);
  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.
   */

  stacksize = parent->adj_stack_size + CONFIG_STACK_ALIGNMENT - 1;

  /* Allocate the stack for the TCB */

  ret = up_create_stack((FAR struct tcb_s *)child, stacksize,
                        parent->flags & TCB_FLAG_TTYPE_MASK);
  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?  The MIPS uses
   * a push-down stack so that the current stack pointer should
   * be lower than the initial, adjusted stack pointer.  The
   * stack usage should be the difference between those two.
   */

  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 pointers and other
   * content that will not work in the child context.  However, if the
   * user follows all of the caveats of vfork() usage, even this feeble
   * effort is overkill.
   */

  newsp = (uint32_t)child->cmn.adj_stack_ptr - stackutil;
  memcpy((void *)newsp, (const void *)context->sp, stackutil);

  /* Was there a frame pointer in place before? */

#if CONFIG_MIPS32_FRAMEPOINTER
  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->cmn.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->cmn.adj_stack_ptr, newsp, newfp);
#else
  svdbg("Old stack base:%08x SP:%08x\n",
        parent->adj_stack_ptr, context->sp);
  svdbg("New stack base:%08x SP:%08x\n",
        child->cmn.adj_stack_ptr, newsp);
#endif

 /* Update the stack pointer, frame pointer, global pointer and saved
  * 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 v0 should be cleared to zero, providing the
  * indication to the newly started child thread.
  */

  child->cmn.xcp.regs[REG_S0]  = context->s0;  /* Saved register s0 */
  child->cmn.xcp.regs[REG_S1]  = context->s1;  /* Saved register s1 */
  child->cmn.xcp.regs[REG_S2]  = context->s2;  /* Saved register s2 */
  child->cmn.xcp.regs[REG_S3]  = context->s3;  /* Volatile register s3 */
  child->cmn.xcp.regs[REG_S4]  = context->s4;  /* Volatile register s4 */
  child->cmn.xcp.regs[REG_S5]  = context->s5;  /* Volatile register s5 */
  child->cmn.xcp.regs[REG_S6]  = context->s6;  /* Volatile register s6 */
  child->cmn.xcp.regs[REG_S7]  = context->s7;  /* Volatile register s7 */
#if CONFIG_MIPS32_FRAMEPOINTER
  child->cmn.xcp.regs[REG_FP]  = newfp;        /* Frame pointer */
#else
  child->cmn.xcp.regs[REG_S8]  = context->s8;  /* Volatile register s8 */
#endif
  child->cmn.xcp.regs[REG_SP]  = newsp;        /* Stack pointer */
#if MIPS32_SAVE_GP
  child->cmn.xcp.regs[REG_GP]  = newsp;        /* Global pointer */
#endif

  /* And, finally, start the child task.  On a failure, task_vforkstart()
   * will discard the TCB by calling task_vforkabort().
   */

  return task_vforkstart(child);
}