lwp.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2006-03-12 Bernard first version
  9. * 2018-11-02 heyuanjie fix complie error in iar
  10. * 2021-02-03 lizhirui add 64-bit arch support and riscv64 arch support
  11. * 2021-08-26 linzhenxing add lwp_setcwd\lwp_getcwd
  12. * 2023-02-20 wangxiaoyao inv icache before new app startup
  13. * 2023-02-20 wangxiaoyao fix bug on foreground app switch
  14. * 2023-10-16 Shell Support a new backtrace framework
  15. * 2023-11-17 xqyjlj add process group and session support
  16. * 2023-11-30 Shell add lwp_startup()
  17. */
  18. #define DBG_TAG "lwp"
  19. #define DBG_LVL DBG_INFO
  20. #include <rtdbg.h>
  21. #include <rthw.h>
  22. #include <rtthread.h>
  23. #include <dfs_file.h>
  24. #include <unistd.h>
  25. #include <stdio.h> /* rename() */
  26. #include <fcntl.h>
  27. #include <sys/stat.h>
  28. #include <sys/statfs.h> /* statfs() */
  29. #include <lwp_elf.h>
  30. #ifndef RT_USING_DFS
  31. #error "lwp need file system(RT_USING_DFS)"
  32. #endif
  33. #include "lwp_internal.h"
  34. #include "lwp_arch.h"
  35. #include "lwp_arch_comm.h"
  36. #include "lwp_signal.h"
  37. #include "lwp_dbg.h"
  38. #include <terminal/terminal.h>
  39. #ifdef ARCH_MM_MMU
  40. #include <lwp_user_mm.h>
  41. #endif /* end of ARCH_MM_MMU */
  42. #ifndef O_DIRECTORY
  43. #define O_DIRECTORY 0x200000
  44. #endif
  45. #ifndef O_BINARY
  46. #define O_BINARY 0x10000
  47. #endif
  48. #ifdef DFS_USING_WORKDIR
  49. extern char working_directory[];
  50. #endif
  51. static int lwp_component_init(void)
  52. {
  53. int rc;
  54. if ((rc = lwp_tid_init()) != RT_EOK)
  55. {
  56. LOG_E("%s: lwp_component_init() failed", __func__);
  57. }
  58. else if ((rc = lwp_pid_init()) != RT_EOK)
  59. {
  60. LOG_E("%s: lwp_pid_init() failed", __func__);
  61. }
  62. else if ((rc = rt_channel_component_init()) != RT_EOK)
  63. {
  64. LOG_E("%s: rt_channel_component_init failed", __func__);
  65. }
  66. else if ((rc = lwp_futex_init()) != RT_EOK)
  67. {
  68. LOG_E("%s: lwp_futex_init() failed", __func__);
  69. }
  70. return rc;
  71. }
  72. INIT_COMPONENT_EXPORT(lwp_component_init);
  73. rt_weak int lwp_startup_debug_request(void)
  74. {
  75. return 0;
  76. }
  77. #define LATENCY_TIMES (3)
  78. #define LATENCY_IN_MSEC (128)
  79. #define LWP_CONSOLE_PATH "CONSOLE=/dev/console"
  80. const char *init_search_path[] = {
  81. "/sbin/init",
  82. "/bin/init",
  83. };
  84. /**
  85. * Startup process 0 and do the essential works
  86. * This is the "Hello World" point of RT-Smart
  87. */
  88. static int lwp_startup(void)
  89. {
  90. int error;
  91. const char *init_path;
  92. char *argv[] = {0, "&"};
  93. char *envp[] = {LWP_CONSOLE_PATH, 0};
  94. #ifdef LWP_DEBUG_INIT
  95. int command;
  96. int countdown = LATENCY_TIMES;
  97. while (countdown)
  98. {
  99. command = lwp_startup_debug_request();
  100. if (command)
  101. {
  102. return 0;
  103. }
  104. rt_kprintf("Press any key to stop init process startup ... %d\n", countdown);
  105. countdown -= 1;
  106. rt_thread_mdelay(LATENCY_IN_MSEC);
  107. }
  108. rt_kprintf("Starting init ...\n");
  109. #endif /* LWP_DEBUG_INIT */
  110. for (size_t i = 0; i < sizeof(init_search_path)/sizeof(init_search_path[0]); i++)
  111. {
  112. struct stat s;
  113. init_path = init_search_path[i];
  114. error = stat(init_path, &s);
  115. if (error == 0)
  116. {
  117. argv[0] = (void *)init_path;
  118. error = lwp_execve((void *)init_path, 0, sizeof(argv)/sizeof(argv[0]), argv, envp);
  119. if (error < 0)
  120. {
  121. LOG_E("%s: failed to startup process 0 (init)\n"
  122. "Switching to legacy mode...", __func__);
  123. }
  124. else if (error != 1)
  125. {
  126. LOG_E("%s: pid 1 is already allocated", __func__);
  127. error = -EBUSY;
  128. }
  129. else
  130. {
  131. rt_lwp_t p = lwp_from_pid_locked(1);
  132. p->sig_protected = 1;
  133. error = 0;
  134. }
  135. break;
  136. }
  137. }
  138. if (error)
  139. {
  140. LOG_D("%s: init program not found\n"
  141. "Switching to legacy mode...", __func__);
  142. }
  143. return error;
  144. }
  145. INIT_APP_EXPORT(lwp_startup);
  146. void lwp_setcwd(char *buf)
  147. {
  148. struct rt_lwp *lwp = RT_NULL;
  149. if(strlen(buf) >= DFS_PATH_MAX)
  150. {
  151. rt_kprintf("buf too long!\n");
  152. return ;
  153. }
  154. lwp = (struct rt_lwp *)rt_thread_self()->lwp;
  155. if (lwp)
  156. {
  157. rt_strncpy(lwp->working_directory, buf, DFS_PATH_MAX - 1);
  158. }
  159. else
  160. {
  161. rt_strncpy(working_directory, buf, DFS_PATH_MAX - 1);
  162. }
  163. return ;
  164. }
  165. char *lwp_getcwd(void)
  166. {
  167. char *dir_buf = RT_NULL;
  168. struct rt_lwp *lwp = RT_NULL;
  169. rt_thread_t thread = rt_thread_self();
  170. if (thread)
  171. {
  172. lwp = (struct rt_lwp *)thread->lwp;
  173. }
  174. if (lwp)
  175. {
  176. if(lwp->working_directory[0] != '/')
  177. {
  178. dir_buf = &working_directory[0];
  179. }
  180. else
  181. {
  182. dir_buf = &lwp->working_directory[0];
  183. }
  184. }
  185. else
  186. dir_buf = &working_directory[0];
  187. return dir_buf;
  188. }
  189. /**
  190. * RT-Thread light-weight process
  191. */
  192. void lwp_set_kernel_sp(uint32_t *sp)
  193. {
  194. rt_thread_self()->kernel_sp = (rt_uint32_t *)sp;
  195. }
  196. uint32_t *lwp_get_kernel_sp(void)
  197. {
  198. #ifdef ARCH_MM_MMU
  199. return (uint32_t *)rt_thread_self()->sp;
  200. #else
  201. uint32_t* kernel_sp;
  202. extern rt_uint32_t rt_interrupt_from_thread;
  203. extern rt_uint32_t rt_thread_switch_interrupt_flag;
  204. if (rt_thread_switch_interrupt_flag)
  205. {
  206. kernel_sp = (uint32_t *)((rt_thread_t)rt_container_of(rt_interrupt_from_thread, struct rt_thread, sp))->kernel_sp;
  207. }
  208. else
  209. {
  210. kernel_sp = (uint32_t *)rt_thread_self()->kernel_sp;
  211. }
  212. return kernel_sp;
  213. #endif
  214. }
  215. /* lwp-thread clean up routine */
  216. void lwp_cleanup(struct rt_thread *tid)
  217. {
  218. struct rt_lwp *lwp;
  219. if (tid == NULL)
  220. {
  221. LOG_I("%s: invalid parameter tid == NULL", __func__);
  222. return;
  223. }
  224. else
  225. LOG_D("cleanup thread: %s, stack_addr: 0x%x", tid->parent.name, tid->stack_addr);
  226. /**
  227. * Brief: lwp thread cleanup
  228. *
  229. * Note: Critical Section
  230. * - thread control block (RW. It's ensured that no one else can access tcb
  231. * other than itself)
  232. */
  233. lwp = (struct rt_lwp *)tid->lwp;
  234. lwp_thread_signal_detach(&tid->signal);
  235. /* tty will be release in lwp_ref_dec() if ref is cleared */
  236. lwp_ref_dec(lwp);
  237. return;
  238. }
  239. static void lwp_execve_setup_stdio(struct rt_lwp *lwp)
  240. {
  241. struct dfs_fdtable *lwp_fdt;
  242. struct dfs_file *cons_file;
  243. int cons_fd;
  244. lwp_fdt = &lwp->fdt;
  245. /* open console */
  246. cons_fd = open("/dev/console", O_RDWR);
  247. if (cons_fd < 0)
  248. {
  249. LOG_E("%s: Cannot open console tty", __func__);
  250. return ;
  251. }
  252. LOG_D("%s: open console as fd %d", __func__, cons_fd);
  253. /* init 4 fds */
  254. lwp_fdt->fds = rt_calloc(4, sizeof(void *));
  255. if (lwp_fdt->fds)
  256. {
  257. cons_file = fd_get(cons_fd);
  258. lwp_fdt->maxfd = 4;
  259. fdt_fd_associate_file(lwp_fdt, 0, cons_file);
  260. fdt_fd_associate_file(lwp_fdt, 1, cons_file);
  261. fdt_fd_associate_file(lwp_fdt, 2, cons_file);
  262. }
  263. close(cons_fd);
  264. return;
  265. }
  266. static void _lwp_thread_entry(void *parameter)
  267. {
  268. rt_thread_t tid;
  269. struct rt_lwp *lwp;
  270. tid = rt_thread_self();
  271. lwp = (struct rt_lwp *)tid->lwp;
  272. tid->cleanup = lwp_cleanup;
  273. tid->user_stack = RT_NULL;
  274. if (lwp->debug)
  275. {
  276. lwp->bak_first_inst = *(uint32_t *)lwp->text_entry;
  277. *(uint32_t *)lwp->text_entry = dbg_get_ins();
  278. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, lwp->text_entry, sizeof(uint32_t));
  279. icache_invalid_all();
  280. }
  281. /**
  282. * without ASID support, it will be a special case when trying to run application
  283. * and exit multiple times and a same page frame allocated to it bound to
  284. * different text segment. Then we are in a situation where icache contains
  285. * out-of-dated data and must be handle by the running core itself.
  286. * with ASID support, this should be a rare case that ASID & page frame both
  287. * identical to previous running application.
  288. *
  289. * For a new application loaded into memory, icache are seen as empty. And there
  290. * should be nothing in the icache entry to match. So this icache invalidation
  291. * operation should have barely influence.
  292. */
  293. rt_hw_icache_invalidate_all();
  294. #ifdef ARCH_MM_MMU
  295. arch_start_umode(lwp->args, lwp->text_entry, (void *)USER_STACK_VEND, (char *)tid->stack_addr + tid->stack_size);
  296. #else
  297. arch_start_umode(lwp->args, lwp->text_entry, lwp->data_entry, (void *)((uint32_t)lwp->data_entry + lwp->data_size));
  298. #endif /* ARCH_MM_MMU */
  299. }
  300. struct rt_lwp *lwp_self(void)
  301. {
  302. rt_thread_t tid;
  303. tid = rt_thread_self();
  304. if (tid)
  305. {
  306. return (struct rt_lwp *)tid->lwp;
  307. }
  308. return RT_NULL;
  309. }
  310. rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child)
  311. {
  312. /* lwp add to children link */
  313. LWP_LOCK(parent);
  314. child->sibling = parent->first_child;
  315. parent->first_child = child;
  316. child->parent = parent;
  317. LWP_UNLOCK(parent);
  318. LOG_D("%s(parent=%p, child=%p)", __func__, parent, child);
  319. /* parent holds reference to child */
  320. lwp_ref_inc(parent);
  321. /* child holds reference to parent */
  322. lwp_ref_inc(child);
  323. return 0;
  324. }
  325. rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child)
  326. {
  327. struct rt_lwp **lwp_node;
  328. LWP_LOCK(parent);
  329. /* detach from children link */
  330. lwp_node = &parent->first_child;
  331. while (*lwp_node != child)
  332. {
  333. RT_ASSERT(*lwp_node != RT_NULL);
  334. lwp_node = &(*lwp_node)->sibling;
  335. }
  336. (*lwp_node) = child->sibling;
  337. child->parent = RT_NULL;
  338. LWP_UNLOCK(parent);
  339. LOG_D("%s(parent=%p, child=%p)", __func__, parent, child);
  340. lwp_ref_dec(child);
  341. lwp_ref_dec(parent);
  342. return 0;
  343. }
  344. struct process_aux *argscopy(struct rt_lwp *lwp, int argc, char **argv, char **envp)
  345. {
  346. struct lwp_args_info ai;
  347. rt_err_t error;
  348. struct process_aux *ua;
  349. const char **tail_argv[2] = {0};
  350. error = lwp_args_init(&ai);
  351. if (error)
  352. {
  353. return RT_NULL;
  354. }
  355. if (argc > 0)
  356. {
  357. tail_argv[0] = (void *)argv[argc - 1];
  358. argv[argc - 1] = NULL;
  359. lwp_args_put(&ai, (void *)argv, LWP_ARGS_TYPE_KARG);
  360. lwp_args_put(&ai, (void *)tail_argv, LWP_ARGS_TYPE_KARG);
  361. }
  362. lwp_args_put(&ai, (void *)envp, LWP_ARGS_TYPE_KENVP);
  363. ua = lwp_argscopy(lwp, &ai);
  364. lwp_args_detach(&ai);
  365. return ua;
  366. }
  367. pid_t lwp_execve(char *filename, int debug, int argc, char **argv, char **envp)
  368. {
  369. int result;
  370. struct rt_lwp *lwp;
  371. char *thread_name;
  372. struct process_aux *aux;
  373. int tid = 0;
  374. if (filename == RT_NULL)
  375. {
  376. return -EINVAL;
  377. }
  378. if (access(filename, X_OK) != 0)
  379. {
  380. return -EACCES;
  381. }
  382. lwp = lwp_create(LWP_CREATE_FLAG_ALLOC_PID | LWP_CREATE_FLAG_NOTRACE_EXEC);
  383. if (lwp == RT_NULL)
  384. {
  385. dbg_log(DBG_ERROR, "lwp struct out of memory!\n");
  386. return -ENOMEM;
  387. }
  388. LOG_D("lwp malloc : %p, size: %d!", lwp, sizeof(struct rt_lwp));
  389. if ((tid = lwp_tid_get()) == 0)
  390. {
  391. lwp_ref_dec(lwp);
  392. return -ENOMEM;
  393. }
  394. #ifdef ARCH_MM_MMU
  395. if (lwp_user_space_init(lwp, 0) != 0)
  396. {
  397. lwp_tid_put(tid);
  398. lwp_ref_dec(lwp);
  399. return -ENOMEM;
  400. }
  401. #endif
  402. if ((aux = argscopy(lwp, argc, argv, envp)) == RT_NULL)
  403. {
  404. lwp_tid_put(tid);
  405. lwp_ref_dec(lwp);
  406. return -ENOMEM;
  407. }
  408. result = lwp_load(filename, lwp, RT_NULL, 0, aux);
  409. if (result == RT_EOK)
  410. {
  411. rt_thread_t thread = RT_NULL;
  412. rt_uint32_t priority = 25, tick = 200;
  413. lwp_execve_setup_stdio(lwp);
  414. /* obtain the base name */
  415. thread_name = strrchr(filename, '/');
  416. thread_name = thread_name ? thread_name + 1 : filename;
  417. #ifndef ARCH_MM_MMU
  418. struct lwp_app_head *app_head = lwp->text_entry;
  419. if (app_head->priority)
  420. {
  421. priority = app_head->priority;
  422. }
  423. if (app_head->tick)
  424. {
  425. tick = app_head->tick;
  426. }
  427. #endif /* not defined ARCH_MM_MMU */
  428. thread = rt_thread_create(thread_name, _lwp_thread_entry, RT_NULL,
  429. LWP_TASK_STACK_SIZE, priority, tick);
  430. if (thread != RT_NULL)
  431. {
  432. struct rt_lwp *self_lwp;
  433. rt_session_t session;
  434. rt_processgroup_t group;
  435. thread->tid = tid;
  436. lwp_tid_set_thread(tid, thread);
  437. LOG_D("lwp kernel => (0x%08x, 0x%08x)\n", (rt_size_t)thread->stack_addr,
  438. (rt_size_t)thread->stack_addr + thread->stack_size);
  439. self_lwp = lwp_self();
  440. /* when create init, self_lwp == null */
  441. if (self_lwp == RT_NULL && lwp_to_pid(lwp) != 1)
  442. {
  443. self_lwp = lwp_from_pid_and_lock(1);
  444. }
  445. if (self_lwp)
  446. {
  447. /* lwp add to children link */
  448. lwp_children_register(self_lwp, lwp);
  449. }
  450. session = RT_NULL;
  451. group = RT_NULL;
  452. group = lwp_pgrp_create(lwp);
  453. if (group)
  454. {
  455. lwp_pgrp_insert(group, lwp);
  456. if (self_lwp == RT_NULL)
  457. {
  458. session = lwp_session_create(lwp);
  459. lwp_session_insert(session, group);
  460. }
  461. else
  462. {
  463. session = lwp_session_find(lwp_sid_get_byprocess(self_lwp));
  464. lwp_session_insert(session, group);
  465. }
  466. }
  467. thread->lwp = lwp;
  468. #ifndef ARCH_MM_MMU
  469. struct lwp_app_head *app_head = (struct lwp_app_head*)lwp->text_entry;
  470. thread->user_stack = app_head->stack_offset ?
  471. (void *)(app_head->stack_offset -
  472. app_head->data_offset +
  473. (uint32_t)lwp->data_entry) : RT_NULL;
  474. thread->user_stack_size = app_head->stack_size;
  475. /* init data area */
  476. rt_memset(lwp->data_entry, 0, lwp->data_size);
  477. /* init user stack */
  478. rt_memset(thread->user_stack, '#', thread->user_stack_size);
  479. #endif /* not defined ARCH_MM_MMU */
  480. rt_list_insert_after(&lwp->t_grp, &thread->sibling);
  481. lwp->did_exec = RT_TRUE;
  482. if (debug && rt_dbg_ops)
  483. {
  484. lwp->debug = debug;
  485. rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  486. }
  487. rt_thread_startup(thread);
  488. return lwp_to_pid(lwp);
  489. }
  490. }
  491. lwp_tid_put(tid);
  492. lwp_ref_dec(lwp);
  493. return -RT_ERROR;
  494. }
  495. #ifdef RT_USING_MUSLLIBC
  496. extern char **__environ;
  497. #else
  498. char **__environ = 0;
  499. #endif
  500. pid_t exec(char *filename, int debug, int argc, char **argv)
  501. {
  502. setenv("OS", "RT-Thread", 1);
  503. return lwp_execve(filename, debug, argc, argv, __environ);
  504. }
  505. #ifdef ARCH_MM_MMU
  506. void lwp_user_setting_save(rt_thread_t thread)
  507. {
  508. if (thread)
  509. {
  510. thread->thread_idr = arch_get_tidr();
  511. }
  512. }
  513. void lwp_user_setting_restore(rt_thread_t thread)
  514. {
  515. if (!thread)
  516. {
  517. return;
  518. }
  519. #if !defined(ARCH_RISCV64)
  520. /* tidr will be set in RESTORE_ALL in risc-v */
  521. arch_set_tidr(thread->thread_idr);
  522. #endif
  523. if (rt_dbg_ops)
  524. {
  525. struct rt_lwp *l = (struct rt_lwp *)thread->lwp;
  526. if (l != 0)
  527. {
  528. rt_hw_set_process_id((size_t)l->pid);
  529. }
  530. else
  531. {
  532. rt_hw_set_process_id(0);
  533. }
  534. if (l && l->debug)
  535. {
  536. uint32_t step_type = 0;
  537. step_type = dbg_step_type();
  538. if ((step_type == 2) || (thread->step_exec && (step_type == 1)))
  539. {
  540. dbg_activate_step();
  541. }
  542. else
  543. {
  544. dbg_deactivate_step();
  545. }
  546. }
  547. }
  548. }
  549. #endif /* ARCH_MM_MMU */
  550. void lwp_uthread_ctx_save(void *ctx)
  551. {
  552. rt_thread_t thread;
  553. thread = rt_thread_self();
  554. thread->user_ctx.ctx = ctx;
  555. }
  556. void lwp_uthread_ctx_restore(void)
  557. {
  558. rt_thread_t thread;
  559. thread = rt_thread_self();
  560. thread->user_ctx.ctx = RT_NULL;
  561. }
  562. rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *frame)
  563. {
  564. rt_err_t rc = -RT_ERROR;
  565. long nesting = 0;
  566. char **argv;
  567. rt_lwp_t lwp;
  568. if (uthread && uthread->lwp && rt_scheduler_is_available())
  569. {
  570. lwp = uthread->lwp;
  571. argv = lwp_get_command_line_args(lwp);
  572. if (argv)
  573. {
  574. rt_kprintf("please use: addr2line -e %s -a -f", argv[0]);
  575. lwp_free_command_line_args(argv);
  576. }
  577. else
  578. {
  579. rt_kprintf("please use: addr2line -e %s -a -f", lwp->cmd);
  580. }
  581. while (nesting < RT_BACKTRACE_LEVEL_MAX_NR)
  582. {
  583. rt_kprintf(" 0x%lx", frame->pc);
  584. if (rt_hw_backtrace_frame_unwind(uthread, frame))
  585. {
  586. break;
  587. }
  588. nesting++;
  589. }
  590. rt_kprintf("\n");
  591. rc = RT_EOK;
  592. }
  593. return rc;
  594. }