task_start.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. /****************************************************************************
  2. * sched/task/task_start.c
  3. *
  4. * Copyright (C) 2007-2010, 2013, 2018 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions
  9. * are met:
  10. *
  11. * 1. Redistributions of source code must retain the above copyright
  12. * notice, this list of conditions and the following disclaimer.
  13. * 2. Redistributions in binary form must reproduce the above copyright
  14. * notice, this list of conditions and the following disclaimer in
  15. * the documentation and/or other materials provided with the
  16. * distribution.
  17. * 3. Neither the name NuttX nor the names of its contributors may be
  18. * used to endorse or promote products derived from this software
  19. * without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  22. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  23. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  24. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  25. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  26. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  27. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  28. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  29. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  30. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  31. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32. * POSSIBILITY OF SUCH DAMAGE.
  33. *
  34. ****************************************************************************/
  35. /****************************************************************************
  36. * Included Files
  37. ****************************************************************************/
  38. #include <nuttx/config.h>
  39. #include <stdlib.h>
  40. #include <sched.h>
  41. #include <debug.h>
  42. #include <string.h>
  43. #include <nuttx/arch.h>
  44. #include <nuttx/sched.h>
  45. #include "group/group.h"
  46. #include "sched/sched.h"
  47. #include "signal/signal.h"
  48. #include "task/task.h"
  49. /****************************************************************************
  50. * Pre-processor Definitions
  51. ****************************************************************************/
  52. /* This is an artificial limit to detect error conditions where an argv[]
  53. * list is not properly terminated.
  54. */
  55. #define MAX_START_ARGS 256
  56. /****************************************************************************
  57. * Private Functions
  58. ****************************************************************************/
  59. /****************************************************************************
  60. * Public Functions
  61. ****************************************************************************/
  62. /****************************************************************************
  63. * Name: nxtask_start
  64. *
  65. * Description:
  66. * This function is the low level entry point into the main thread of
  67. * execution of a task. It receives initial control when the task is
  68. * started and calls main entry point of the newly started task.
  69. *
  70. * Input Parameters:
  71. * None
  72. *
  73. * Returned Value:
  74. * None
  75. *
  76. ****************************************************************************/
  77. void nxtask_start(void)
  78. {
  79. FAR struct task_tcb_s *tcb = (FAR struct task_tcb_s *)this_task();
  80. int exitcode;
  81. int argc;
  82. DEBUGASSERT((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_PTHREAD);
  83. #ifdef CONFIG_SIG_DEFAULT
  84. /* Set up default signal actions */
  85. nxsig_default_initialize(&tcb->cmn);
  86. #endif
  87. /* Execute the start hook if one has been registered */
  88. #ifdef CONFIG_SCHED_STARTHOOK
  89. if (tcb->starthook != NULL)
  90. {
  91. tcb->starthook(tcb->starthookarg);
  92. }
  93. #endif
  94. /* Count how many non-null arguments we are passing. The first non-null
  95. * argument terminates the list .
  96. */
  97. argc = 1;
  98. while (tcb->argv[argc])
  99. {
  100. /* Increment the number of args. Here is a sanity check to
  101. * prevent running away with an unterminated argv[] list.
  102. * MAX_START_ARGS should be sufficiently large that this never
  103. * happens in normal usage.
  104. */
  105. if (++argc > MAX_START_ARGS)
  106. {
  107. exit(EXIT_FAILURE);
  108. }
  109. }
  110. /* Call the 'main' entry point passing argc and argv. In the kernel build
  111. * this has to be handled differently if we are starting a user-space task;
  112. * we have to switch to user-mode before calling the task.
  113. */
  114. #if defined(CONFIG_BUILD_PROTECTED) || defined(CONFIG_BUILD_KERNEL)
  115. if ((tcb->cmn.flags & TCB_FLAG_TTYPE_MASK) != TCB_FLAG_TTYPE_KERNEL)
  116. {
  117. up_task_start(tcb->cmn.entry.main, argc, tcb->argv);
  118. exitcode = EXIT_FAILURE; /* Should not get here */
  119. }
  120. else
  121. #endif
  122. {
  123. exitcode = tcb->cmn.entry.main(argc, tcb->argv);
  124. }
  125. /* Call exit() if/when the task returns */
  126. exit(exitcode);
  127. }