nx_bringup.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472
  1. /****************************************************************************
  2. * sched/init/nx_bringup.c
  3. *
  4. * Copyright (C) 2011-2012, 2019 Gregory Nutt. All rights reserved.
  5. * Author: Gregory Nutt <gnutt@nuttx.org>
  6. *
  7. * With extensions by:
  8. *
  9. * Author: Uros Platise <uros.platise@isotel.eu>
  10. *
  11. * Redistribution and use in source and binary forms, with or without
  12. * modification, are permitted provided that the following conditions
  13. * are met:
  14. *
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notice, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notice, this list of conditions and the following disclaimer in
  19. * the documentation and/or other materials provided with the
  20. * distribution.
  21. * 3. Neither the name NuttX nor the names of its contributors may be
  22. * used to endorse or promote products derived from this software
  23. * without specific prior written permission.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  26. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  27. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  28. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  29. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  30. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  31. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  32. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  33. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  34. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
  35. * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  36. * POSSIBILITY OF SUCH DAMAGE.
  37. *
  38. ****************************************************************************/
  39. /****************************************************************************
  40. * Included Files
  41. ****************************************************************************/
  42. #include <nuttx/config.h>
  43. #include <sched.h>
  44. #include <stdlib.h>
  45. #include <string.h>
  46. #include <debug.h>
  47. #include <sys/mount.h>
  48. #include <nuttx/arch.h>
  49. #include <nuttx/board.h>
  50. #include <nuttx/init.h>
  51. #include <nuttx/symtab.h>
  52. #include <nuttx/wqueue.h>
  53. #include <nuttx/kthread.h>
  54. #include <nuttx/userspace.h>
  55. #include <nuttx/binfmt/binfmt.h>
  56. #ifdef CONFIG_PAGING
  57. # include "paging/paging.h"
  58. #endif
  59. # include "wqueue/wqueue.h"
  60. # include "init/init.h"
  61. /****************************************************************************
  62. * Pre-processor Definitions
  63. ****************************************************************************/
  64. /* Configuration */
  65. #if defined(CONFIG_INIT_NONE)
  66. /* Kconfig logic will set CONFIG_INIT_NONE if dependencies are not met */
  67. # error No initialization mechanism selected (CONFIG_INIT_NONE)
  68. #else
  69. # if !defined(CONFIG_INIT_ENTRYPOINT) && !defined(CONFIG_INIT_FILEPATH)
  70. /* For backward compatibility with older defconfig files when this was
  71. * the way things were done.
  72. */
  73. # define CONFIG_INIT_ENTRYPOINT 1
  74. # endif
  75. # if defined(CONFIG_INIT_ENTRYPOINT)
  76. /* Initialize by starting a task at an entry point */
  77. # ifndef CONFIG_USER_ENTRYPOINT
  78. /* Entry point name must have been provided */
  79. # error CONFIG_USER_ENTRYPOINT must be defined
  80. # endif
  81. # elif defined(CONFIG_INIT_FILEPATH)
  82. /* Initialize by running an initialization program in the file system.
  83. * Presumably the user has configured a board initialization function
  84. * that will mount the file system containing the initialization
  85. * program.
  86. */
  87. # ifndef CONFIG_USER_INITPATH
  88. /* Path to the initialization program must have been provided */
  89. # error CONFIG_USER_INITPATH must be defined
  90. # endif
  91. # if !defined(CONFIG_INIT_SYMTAB) || !defined(CONFIG_INIT_NEXPORTS)
  92. /* No symbol information... assume no symbol table is available */
  93. # undef CONFIG_INIT_SYMTAB
  94. # undef CONFIG_INIT_NEXPORTS
  95. # define CONFIG_INIT_SYMTAB NULL
  96. # define CONFIG_INIT_NEXPORTS 0
  97. # else
  98. extern const struct symtab_s CONFIG_INIT_SYMTAB[];
  99. extern const int CONFIG_INIT_NEXPORTS;
  100. # endif
  101. # endif
  102. #endif
  103. /* In the protected build (only) we also need to start the user work queue */
  104. #if !defined(CONFIG_BUILD_PROTECTED)
  105. # undef CONFIG_LIB_USRWORK
  106. #endif
  107. #if !defined(CONFIG_USERMAIN_PRIORITY)
  108. # define CONFIG_USERMAIN_PRIORITY SCHED_PRIORITY_DEFAULT
  109. #endif
  110. /****************************************************************************
  111. * Private Functions
  112. ****************************************************************************/
  113. /****************************************************************************
  114. * Name: nx_pgworker
  115. *
  116. * Description:
  117. * Start the page fill worker kernel thread that will resolve page faults.
  118. * This should always be the first thread started because it may have to
  119. * resolve page faults in other threads
  120. *
  121. * Input Parameters:
  122. * None
  123. *
  124. * Returned Value:
  125. * None
  126. *
  127. ****************************************************************************/
  128. #ifdef CONFIG_PAGING
  129. static inline void nx_pgworker(void)
  130. {
  131. /* Start the page fill worker kernel thread that will resolve page faults.
  132. * This should always be the first thread started because it may have to
  133. * resolve page faults in other threads
  134. */
  135. sinfo("Starting paging thread\n");
  136. g_pgworker = kthread_create("pgfill", CONFIG_PAGING_DEFPRIO,
  137. CONFIG_PAGING_STACKSIZE,
  138. (main_t)pg_worker, (FAR char * const *)NULL);
  139. DEBUGASSERT(g_pgworker > 0);
  140. }
  141. #else /* CONFIG_PAGING */
  142. # define nx_pgworker()
  143. #endif /* CONFIG_PAGING */
  144. /****************************************************************************
  145. * Name: nx_workqueues
  146. *
  147. * Description:
  148. * Start the worker threads that service the work queues.
  149. *
  150. * Input Parameters:
  151. * None
  152. *
  153. * Returned Value:
  154. * None
  155. *
  156. ****************************************************************************/
  157. #ifdef CONFIG_SCHED_WORKQUEUE
  158. static inline void nx_workqueues(void)
  159. {
  160. #ifdef CONFIG_LIB_USRWORK
  161. pid_t pid;
  162. #endif
  163. #ifdef CONFIG_SCHED_HPWORK
  164. /* Start the high-priority worker thread to support device driver lower
  165. * halves.
  166. */
  167. (void)work_hpstart();
  168. #endif /* CONFIG_SCHED_HPWORK */
  169. #ifdef CONFIG_SCHED_LPWORK
  170. /* Start the low-priority worker thread for other, non-critical continuation
  171. * tasks
  172. */
  173. (void)work_lpstart();
  174. #endif /* CONFIG_SCHED_LPWORK */
  175. #ifdef CONFIG_LIB_USRWORK
  176. /* Start the user-space work queue */
  177. DEBUGASSERT(USERSPACE->work_usrstart != NULL);
  178. pid = USERSPACE->work_usrstart();
  179. DEBUGASSERT(pid > 0);
  180. UNUSED(pid);
  181. #endif
  182. }
  183. #else /* CONFIG_SCHED_WORKQUEUE */
  184. # define nx_workqueues()
  185. #endif /* CONFIG_SCHED_WORKQUEUE */
  186. /****************************************************************************
  187. * Name: nx_start_application
  188. *
  189. * Description:
  190. * Execute the board initialization function (if so configured) and start
  191. * the application initialization thread.
  192. *
  193. * Input Parameters:
  194. * None
  195. *
  196. * Returned Value:
  197. * None
  198. *
  199. ****************************************************************************/
  200. #if defined(CONFIG_INIT_ENTRYPOINT)
  201. static inline void nx_start_application(void)
  202. {
  203. int pid;
  204. #ifdef CONFIG_BOARD_LATE_INITIALIZE
  205. /* Perform any last-minute, board-specific initialization, if so
  206. * configured.
  207. */
  208. board_late_initialize();
  209. #endif
  210. /* Start the application initialization task. In a flat build, this is
  211. * entrypoint is given by the definitions, CONFIG_USER_ENTRYPOINT. In
  212. * the protected build, however, we must get the address of the
  213. * entrypoint from the header at the beginning of the user-space blob.
  214. */
  215. sinfo("Starting init thread\n");
  216. #ifdef CONFIG_BUILD_PROTECTED
  217. DEBUGASSERT(USERSPACE->us_entrypoint != NULL);
  218. pid = nxtask_create("init", CONFIG_USERMAIN_PRIORITY,
  219. CONFIG_USERMAIN_STACKSIZE, USERSPACE->us_entrypoint,
  220. (FAR char * const *)NULL);
  221. #else
  222. pid = nxtask_create("init", CONFIG_USERMAIN_PRIORITY,
  223. CONFIG_USERMAIN_STACKSIZE,
  224. (main_t)CONFIG_USER_ENTRYPOINT,
  225. (FAR char * const *)NULL);
  226. #endif
  227. DEBUGASSERT(pid > 0);
  228. UNUSED(pid);
  229. }
  230. #elif defined(CONFIG_INIT_FILEPATH)
  231. static inline void nx_start_application(void)
  232. {
  233. int ret;
  234. #ifdef CONFIG_BOARD_LATE_INITIALIZE
  235. /* Perform any last-minute, board-specific initialization, if so
  236. * configured.
  237. */
  238. board_late_initialize();
  239. #endif
  240. #ifdef CONFIG_INIT_MOUNT
  241. /* Mount the file system containing the init program. */
  242. ret = mount(CONFIG_INIT_MOUNT_SOURCE, CONFIG_INIT_MOUNT_TARGET,
  243. CONFIG_INIT_MOUNT_FSTYPE, CONFIG_INIT_MOUNT_FLAGS,
  244. CONFIG_INIT_MOUNT_DATA);
  245. DEBUGASSERT(ret >= 0);
  246. UNUSED(ret);
  247. #endif
  248. /* Start the application initialization program from a program in a
  249. * mounted file system. Presumably the file system was mounted as part
  250. * of the board_late_initialize() operation.
  251. */
  252. sinfo("Starting init task: %s\n", CONFIG_USER_INITPATH);
  253. ret = exec(CONFIG_USER_INITPATH, NULL, CONFIG_INIT_SYMTAB,
  254. CONFIG_INIT_NEXPORTS);
  255. DEBUGASSERT(ret >= 0);
  256. UNUSED(ret);
  257. }
  258. #elif defined(CONFIG_INIT_NONE)
  259. # define nx_start_application()
  260. #else
  261. # error "Cannot start initialization thread"
  262. #endif
  263. /****************************************************************************
  264. * Name: nx_start_task
  265. *
  266. * Description:
  267. * This is the framework for a short duration worker thread. It off-loads
  268. * the board initialization and application start-up from the limited
  269. * start-up, initialization thread to a more robust kernel thread.
  270. *
  271. * Input Parameters:
  272. * None
  273. *
  274. * Returned Value:
  275. * None
  276. *
  277. ****************************************************************************/
  278. #ifdef CONFIG_BOARD_LATE_INITIALIZE
  279. static int nx_start_task(int argc, FAR char **argv)
  280. {
  281. /* Do the board/application initialization and exit */
  282. nx_start_application();
  283. return OK;
  284. }
  285. #endif
  286. /****************************************************************************
  287. * Name: nx_create_initthread
  288. *
  289. * Description:
  290. * Execute the board initialization function (if so configured) and start
  291. * the application initialization thread. This will be done either on the
  292. * thread of execution of the caller or on a separate thread of execution
  293. * if so configured.
  294. *
  295. * Input Parameters:
  296. * None
  297. *
  298. * Returned Value:
  299. * None
  300. *
  301. ****************************************************************************/
  302. static inline void nx_create_initthread(void)
  303. {
  304. #ifdef CONFIG_BOARD_LATE_INITIALIZE
  305. int pid;
  306. /* Do the board/application initialization on a separate thread of
  307. * execution.
  308. */
  309. pid = kthread_create("AppBringUp", CONFIG_BOARD_INITTHREAD_PRIORITY,
  310. CONFIG_BOARD_INITTHREAD_STACKSIZE,
  311. (main_t)nx_start_task, (FAR char * const *)NULL);
  312. DEBUGASSERT(pid > 0);
  313. UNUSED(pid);
  314. #else
  315. /* Do the board/application initialization on this thread of execution. */
  316. nx_start_application();
  317. #endif
  318. }
  319. /****************************************************************************
  320. * Public Functions
  321. ****************************************************************************/
  322. /****************************************************************************
  323. * Name: nx_bringup
  324. *
  325. * Description:
  326. * Start all initial system tasks. This does the "system bring-up" after
  327. * the conclusion of basic OS initialization. These initial system tasks
  328. * may include:
  329. *
  330. * - pg_worker: The page-fault worker thread (only if CONFIG_PAGING is
  331. * defined.
  332. * - work_thread: The work thread. This general thread can be used to
  333. * perform most any kind of queued work. Its primary
  334. * function is to serve as the "bottom half" of device
  335. * drivers.
  336. *
  337. * And the main application entry point:
  338. * symbols, either:
  339. *
  340. * - CONFIG_USER_ENTRYPOINT: This is the default user application entry
  341. * point, or
  342. * - CONFIG_USER_INITPATH: The full path to the location in a mounted
  343. * file system where we can expect to find the
  344. * initialization program. Presumably, this file system
  345. * was mounted by board-specific logic when
  346. * board_late_initialize() was called.
  347. *
  348. * Input Parameters:
  349. * None
  350. *
  351. * Returned Value:
  352. * None
  353. *
  354. ****************************************************************************/
  355. int nx_bringup(void)
  356. {
  357. #ifndef CONFIG_DISABLE_ENVIRON
  358. /* Setup up the initial environment for the idle task. At present, this
  359. * may consist of only the initial PATH variable and/or and init library
  360. * path variable. These path variables are not used by the IDLE task.
  361. * However, the environment containing the PATH variable will be inherited
  362. * by all of the threads created by the IDLE task.
  363. */
  364. #ifdef CONFIG_PATH_INITIAL
  365. (void)setenv("PATH", CONFIG_PATH_INITIAL, 1);
  366. #endif
  367. #ifdef CONFIG_LDPATH_INITIAL
  368. (void)setenv("LD_LIBRARY_PATH", CONFIG_LDPATH_INITIAL, 1);
  369. #endif
  370. #endif
  371. /* Start the page fill worker kernel thread that will resolve page faults.
  372. * This should always be the first thread started because it may have to
  373. * resolve page faults in other threads
  374. */
  375. nx_pgworker();
  376. /* Start the worker thread that will serve as the device driver "bottom-
  377. * half" and will perform misc garbage clean-up.
  378. */
  379. nx_workqueues();
  380. /* Once the operating system has been initialized, the system must be
  381. * started by spawning the user initialization thread of execution. This
  382. * will be the first user-mode thread.
  383. */
  384. nx_create_initthread();
  385. #if !defined(CONFIG_DISABLE_ENVIRON) && (defined(CONFIG_PATH_INITIAL) || \
  386. defined(CONFIG_LDPATH_INITIAL))
  387. /* We an save a few bytes by discarding the IDLE thread's environment. */
  388. (void)clearenv();
  389. #endif
  390. return OK;
  391. }