1
0

lwp.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607
  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. void lwp_setcwd(char *buf)
  74. {
  75. struct rt_lwp *lwp = RT_NULL;
  76. if(strlen(buf) >= DFS_PATH_MAX)
  77. {
  78. rt_kprintf("buf too long!\n");
  79. return ;
  80. }
  81. lwp = (struct rt_lwp *)rt_thread_self()->lwp;
  82. if (lwp)
  83. {
  84. rt_strncpy(lwp->working_directory, buf, DFS_PATH_MAX - 1);
  85. }
  86. else
  87. {
  88. rt_strncpy(working_directory, buf, DFS_PATH_MAX - 1);
  89. }
  90. return ;
  91. }
  92. char *lwp_getcwd(void)
  93. {
  94. char *dir_buf = RT_NULL;
  95. struct rt_lwp *lwp = RT_NULL;
  96. rt_thread_t thread = rt_thread_self();
  97. if (thread)
  98. {
  99. lwp = (struct rt_lwp *)thread->lwp;
  100. }
  101. if (lwp)
  102. {
  103. if(lwp->working_directory[0] != '/')
  104. {
  105. dir_buf = &working_directory[0];
  106. }
  107. else
  108. {
  109. dir_buf = &lwp->working_directory[0];
  110. }
  111. }
  112. else
  113. dir_buf = &working_directory[0];
  114. return dir_buf;
  115. }
  116. /**
  117. * RT-Thread light-weight process
  118. */
  119. void lwp_set_kernel_sp(uint32_t *sp)
  120. {
  121. rt_thread_self()->kernel_sp = (rt_uint32_t *)sp;
  122. }
  123. uint32_t *lwp_get_kernel_sp(void)
  124. {
  125. #ifdef ARCH_MM_MMU
  126. return (uint32_t *)rt_thread_self()->sp;
  127. #else
  128. uint32_t* kernel_sp;
  129. extern rt_uint32_t rt_interrupt_from_thread;
  130. extern rt_uint32_t rt_thread_switch_interrupt_flag;
  131. if (rt_thread_switch_interrupt_flag)
  132. {
  133. kernel_sp = (uint32_t *)((rt_thread_t)rt_container_of(rt_interrupt_from_thread, struct rt_thread, sp))->kernel_sp;
  134. }
  135. else
  136. {
  137. kernel_sp = (uint32_t *)rt_thread_self()->kernel_sp;
  138. }
  139. return kernel_sp;
  140. #endif
  141. }
  142. /* lwp-thread clean up routine */
  143. void lwp_cleanup(struct rt_thread *tid)
  144. {
  145. struct rt_lwp *lwp;
  146. if (tid == NULL)
  147. {
  148. LOG_I("%s: invalid parameter tid == NULL", __func__);
  149. return;
  150. }
  151. else
  152. LOG_D("cleanup thread: %s, stack_addr: 0x%x", tid->parent.name, tid->stack_addr);
  153. /**
  154. * Brief: lwp thread cleanup
  155. *
  156. * Note: Critical Section
  157. * - thread control block (RW. It's ensured that no one else can access tcb
  158. * other than itself)
  159. */
  160. lwp = (struct rt_lwp *)tid->lwp;
  161. lwp_thread_signal_detach(&tid->signal);
  162. /* tty will be release in lwp_ref_dec() if ref is cleared */
  163. lwp_ref_dec(lwp);
  164. return;
  165. }
  166. static void lwp_execve_setup_stdio(struct rt_lwp *lwp)
  167. {
  168. struct dfs_fdtable *lwp_fdt;
  169. struct dfs_file *cons_file;
  170. int cons_fd;
  171. lwp_fdt = &lwp->fdt;
  172. /* open console */
  173. cons_fd = open("/dev/console", O_RDWR);
  174. if (cons_fd < 0)
  175. {
  176. LOG_E("%s: Cannot open console tty", __func__);
  177. return ;
  178. }
  179. LOG_D("%s: open console as fd %d", __func__, cons_fd);
  180. /* init 4 fds */
  181. lwp_fdt->fds = rt_calloc(4, sizeof(void *));
  182. if (lwp_fdt->fds)
  183. {
  184. cons_file = fd_get(cons_fd);
  185. lwp_fdt->maxfd = 4;
  186. fdt_fd_associate_file(lwp_fdt, 0, cons_file);
  187. fdt_fd_associate_file(lwp_fdt, 1, cons_file);
  188. fdt_fd_associate_file(lwp_fdt, 2, cons_file);
  189. }
  190. close(cons_fd);
  191. return;
  192. }
  193. static void _lwp_thread_entry(void *parameter)
  194. {
  195. rt_thread_t tid;
  196. struct rt_lwp *lwp;
  197. tid = rt_thread_self();
  198. lwp = (struct rt_lwp *)tid->lwp;
  199. tid->cleanup = lwp_cleanup;
  200. tid->user_stack = RT_NULL;
  201. if (lwp->debug)
  202. {
  203. lwp->bak_first_inst = *(uint32_t *)lwp->text_entry;
  204. *(uint32_t *)lwp->text_entry = dbg_get_ins();
  205. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, lwp->text_entry, sizeof(uint32_t));
  206. icache_invalid_all();
  207. }
  208. /**
  209. * without ASID support, it will be a special case when trying to run application
  210. * and exit multiple times and a same page frame allocated to it bound to
  211. * different text segment. Then we are in a situation where icache contains
  212. * out-of-dated data and must be handle by the running core itself.
  213. * with ASID support, this should be a rare case that ASID & page frame both
  214. * identical to previous running application.
  215. *
  216. * For a new application loaded into memory, icache are seen as empty. And there
  217. * should be nothing in the icache entry to match. So this icache invalidation
  218. * operation should have barely influence.
  219. */
  220. rt_hw_icache_invalidate_all();
  221. #ifdef ARCH_MM_MMU
  222. arch_start_umode(lwp->args, lwp->text_entry, (void *)USER_STACK_VEND, (char *)tid->stack_addr + tid->stack_size);
  223. #else
  224. arch_start_umode(lwp->args, lwp->text_entry, lwp->data_entry, (void *)((uint32_t)lwp->data_entry + lwp->data_size));
  225. #endif /* ARCH_MM_MMU */
  226. }
  227. struct rt_lwp *lwp_self(void)
  228. {
  229. rt_thread_t tid;
  230. tid = rt_thread_self();
  231. if (tid)
  232. {
  233. return (struct rt_lwp *)tid->lwp;
  234. }
  235. return RT_NULL;
  236. }
  237. rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child)
  238. {
  239. /* lwp add to children link */
  240. LWP_LOCK(parent);
  241. child->sibling = parent->first_child;
  242. parent->first_child = child;
  243. child->parent = parent;
  244. LWP_UNLOCK(parent);
  245. LOG_D("%s(parent=%p, child=%p)", __func__, parent, child);
  246. /* parent holds reference to child */
  247. lwp_ref_inc(parent);
  248. /* child holds reference to parent */
  249. lwp_ref_inc(child);
  250. return 0;
  251. }
  252. rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child)
  253. {
  254. struct rt_lwp **lwp_node;
  255. LWP_LOCK(parent);
  256. /* detach from children link */
  257. lwp_node = &parent->first_child;
  258. while (*lwp_node != child)
  259. {
  260. RT_ASSERT(*lwp_node != RT_NULL);
  261. lwp_node = &(*lwp_node)->sibling;
  262. }
  263. (*lwp_node) = child->sibling;
  264. child->parent = RT_NULL;
  265. LWP_UNLOCK(parent);
  266. LOG_D("%s(parent=%p, child=%p)", __func__, parent, child);
  267. lwp_ref_dec(child);
  268. lwp_ref_dec(parent);
  269. return 0;
  270. }
  271. struct process_aux *argscopy(struct rt_lwp *lwp, int argc, char **argv, char **envp)
  272. {
  273. struct lwp_args_info ai;
  274. rt_err_t error;
  275. struct process_aux *ua;
  276. const char **tail_argv[2] = {0};
  277. error = lwp_args_init(&ai);
  278. if (error)
  279. {
  280. return RT_NULL;
  281. }
  282. if (argc > 0)
  283. {
  284. tail_argv[0] = (void *)argv[argc - 1];
  285. argv[argc - 1] = NULL;
  286. lwp_args_put(&ai, (void *)argv, LWP_ARGS_TYPE_KARG);
  287. lwp_args_put(&ai, (void *)tail_argv, LWP_ARGS_TYPE_KARG);
  288. }
  289. lwp_args_put(&ai, (void *)envp, LWP_ARGS_TYPE_KENVP);
  290. ua = lwp_argscopy(lwp, &ai);
  291. lwp_args_detach(&ai);
  292. return ua;
  293. }
  294. pid_t lwp_execve(char *filename, int debug, int argc, char **argv, char **envp)
  295. {
  296. int result;
  297. struct rt_lwp *lwp;
  298. char *thread_name;
  299. struct process_aux *aux;
  300. int tid = 0;
  301. if (filename == RT_NULL)
  302. {
  303. return -EINVAL;
  304. }
  305. if (access(filename, X_OK) != 0)
  306. {
  307. return -EACCES;
  308. }
  309. lwp = lwp_create(LWP_CREATE_FLAG_ALLOC_PID | LWP_CREATE_FLAG_NOTRACE_EXEC);
  310. if (lwp == RT_NULL)
  311. {
  312. LOG_E("lwp struct out of memory!\n");
  313. return -ENOMEM;
  314. }
  315. LOG_D("lwp malloc : %p, size: %d!", lwp, sizeof(struct rt_lwp));
  316. if ((tid = lwp_tid_get()) == 0)
  317. {
  318. lwp_ref_dec(lwp);
  319. return -ENOMEM;
  320. }
  321. #ifdef ARCH_MM_MMU
  322. if (lwp_user_space_init(lwp, 0) != 0)
  323. {
  324. lwp_tid_put(tid);
  325. lwp_ref_dec(lwp);
  326. return -ENOMEM;
  327. }
  328. #endif
  329. if ((aux = argscopy(lwp, argc, argv, envp)) == RT_NULL)
  330. {
  331. lwp_tid_put(tid);
  332. lwp_ref_dec(lwp);
  333. return -ENOMEM;
  334. }
  335. result = lwp_load(filename, lwp, RT_NULL, 0, aux);
  336. if (result == RT_EOK)
  337. {
  338. rt_thread_t thread = RT_NULL;
  339. rt_uint32_t priority = 25, tick = 200;
  340. lwp_execve_setup_stdio(lwp);
  341. /* obtain the base name */
  342. thread_name = strrchr(filename, '/');
  343. thread_name = thread_name ? thread_name + 1 : filename;
  344. #ifndef ARCH_MM_MMU
  345. struct lwp_app_head *app_head = lwp->text_entry;
  346. if (app_head->priority)
  347. {
  348. priority = app_head->priority;
  349. }
  350. if (app_head->tick)
  351. {
  352. tick = app_head->tick;
  353. }
  354. #endif /* not defined ARCH_MM_MMU */
  355. thread = rt_thread_create(thread_name, _lwp_thread_entry, RT_NULL,
  356. LWP_TASK_STACK_SIZE, priority, tick);
  357. if (thread != RT_NULL)
  358. {
  359. struct rt_lwp *self_lwp;
  360. rt_session_t session;
  361. rt_processgroup_t group;
  362. thread->tid = tid;
  363. lwp_tid_set_thread(tid, thread);
  364. LOG_D("lwp kernel => (0x%08x, 0x%08x)\n", (rt_size_t)thread->stack_addr,
  365. (rt_size_t)thread->stack_addr + thread->stack_size);
  366. self_lwp = lwp_self();
  367. /* when create init, self_lwp == null */
  368. if (self_lwp == RT_NULL && lwp_to_pid(lwp) != 1)
  369. {
  370. self_lwp = lwp_from_pid_and_lock(1);
  371. }
  372. if (self_lwp)
  373. {
  374. /* lwp add to children link */
  375. lwp_children_register(self_lwp, lwp);
  376. }
  377. session = RT_NULL;
  378. group = RT_NULL;
  379. group = lwp_pgrp_create(lwp);
  380. if (group)
  381. {
  382. lwp_pgrp_insert(group, lwp);
  383. if (self_lwp == RT_NULL)
  384. {
  385. session = lwp_session_create(lwp);
  386. lwp_session_insert(session, group);
  387. }
  388. else
  389. {
  390. session = lwp_session_find(lwp_sid_get_byprocess(self_lwp));
  391. lwp_session_insert(session, group);
  392. }
  393. }
  394. thread->lwp = lwp;
  395. #ifndef ARCH_MM_MMU
  396. struct lwp_app_head *app_head = (struct lwp_app_head*)lwp->text_entry;
  397. thread->user_stack = app_head->stack_offset ?
  398. (void *)(app_head->stack_offset -
  399. app_head->data_offset +
  400. (uint32_t)lwp->data_entry) : RT_NULL;
  401. thread->user_stack_size = app_head->stack_size;
  402. /* init data area */
  403. rt_memset(lwp->data_entry, 0, lwp->data_size);
  404. /* init user stack */
  405. rt_memset(thread->user_stack, '#', thread->user_stack_size);
  406. #endif /* not defined ARCH_MM_MMU */
  407. rt_list_insert_after(&lwp->t_grp, &thread->sibling);
  408. lwp->did_exec = RT_TRUE;
  409. if (debug && rt_dbg_ops)
  410. {
  411. lwp->debug = debug;
  412. rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  413. }
  414. rt_thread_startup(thread);
  415. return lwp_to_pid(lwp);
  416. }
  417. }
  418. lwp_tid_put(tid);
  419. lwp_ref_dec(lwp);
  420. return -RT_ERROR;
  421. }
  422. #ifdef RT_USING_MUSLLIBC
  423. extern char **__environ;
  424. #else
  425. char **__environ = 0;
  426. #endif
  427. pid_t exec(char *filename, int debug, int argc, char **argv)
  428. {
  429. setenv("OS", "RT-Thread", 1);
  430. return lwp_execve(filename, debug, argc, argv, __environ);
  431. }
  432. #ifdef ARCH_MM_MMU
  433. void lwp_user_setting_save(rt_thread_t thread)
  434. {
  435. if (thread)
  436. {
  437. thread->thread_idr = arch_get_tidr();
  438. }
  439. }
  440. void lwp_user_setting_restore(rt_thread_t thread)
  441. {
  442. if (!thread)
  443. {
  444. return;
  445. }
  446. #if !defined(ARCH_RISCV64)
  447. /* tidr will be set in RESTORE_ALL in risc-v */
  448. arch_set_tidr(thread->thread_idr);
  449. #endif
  450. if (rt_dbg_ops)
  451. {
  452. struct rt_lwp *l = (struct rt_lwp *)thread->lwp;
  453. if (l != 0)
  454. {
  455. rt_hw_set_process_id((size_t)l->pid);
  456. }
  457. else
  458. {
  459. rt_hw_set_process_id(0);
  460. }
  461. if (l && l->debug)
  462. {
  463. uint32_t step_type = 0;
  464. step_type = dbg_step_type();
  465. if ((step_type == 2) || (thread->step_exec && (step_type == 1)))
  466. {
  467. dbg_activate_step();
  468. }
  469. else
  470. {
  471. dbg_deactivate_step();
  472. }
  473. }
  474. }
  475. }
  476. #endif /* ARCH_MM_MMU */
  477. void lwp_uthread_ctx_save(void *ctx)
  478. {
  479. rt_thread_t thread;
  480. thread = rt_thread_self();
  481. thread->user_ctx.ctx = ctx;
  482. }
  483. void lwp_uthread_ctx_restore(void)
  484. {
  485. rt_thread_t thread;
  486. thread = rt_thread_self();
  487. thread->user_ctx.ctx = RT_NULL;
  488. }
  489. rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *frame)
  490. {
  491. rt_err_t rc = -RT_ERROR;
  492. long nesting = 0;
  493. char **argv;
  494. rt_lwp_t lwp;
  495. if (uthread && uthread->lwp && rt_scheduler_is_available())
  496. {
  497. lwp = uthread->lwp;
  498. argv = lwp_get_command_line_args(lwp);
  499. if (argv)
  500. {
  501. rt_kprintf("please use: addr2line -e %s -a -f\n", argv[0]);
  502. lwp_free_command_line_args(argv);
  503. }
  504. else
  505. {
  506. rt_kprintf("please use: addr2line -e %s -a -f\n", lwp->cmd);
  507. }
  508. while (nesting < RT_BACKTRACE_LEVEL_MAX_NR)
  509. {
  510. rt_kprintf(" 0x%lx", frame->pc);
  511. if (rt_hw_backtrace_frame_unwind(uthread, frame))
  512. {
  513. break;
  514. }
  515. nesting++;
  516. }
  517. rt_kprintf("\n");
  518. rc = RT_EOK;
  519. }
  520. return rc;
  521. }