lwp.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807
  1. /*
  2. * Copyright (c) 2006-2025 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. /**
  52. * @brief Initializes the LWP (Light-Weight Process) component
  53. *
  54. * @return int Returns RT_EOK if all initializations succeed, otherwise returns
  55. * the error code from the first failed initialization
  56. *
  57. * @note This function performs initialization of various LWP subsystems in sequence:
  58. * 1. Thread ID (TID) initialization
  59. * 2. Process ID (PID) initialization
  60. * 3. Channel component initialization
  61. * 4. Futex (Fast Userspace Mutex) initialization
  62. */
  63. static int lwp_component_init(void)
  64. {
  65. int rc;
  66. if ((rc = lwp_tid_init()) != RT_EOK)
  67. {
  68. LOG_E("%s: lwp_component_init() failed", __func__);
  69. }
  70. else if ((rc = lwp_pid_init()) != RT_EOK)
  71. {
  72. LOG_E("%s: lwp_pid_init() failed", __func__);
  73. }
  74. else if ((rc = rt_channel_component_init()) != RT_EOK)
  75. {
  76. LOG_E("%s: rt_channel_component_init failed", __func__);
  77. }
  78. else if ((rc = lwp_futex_init()) != RT_EOK)
  79. {
  80. LOG_E("%s: lwp_futex_init() failed", __func__);
  81. }
  82. return rc;
  83. }
  84. INIT_COMPONENT_EXPORT(lwp_component_init);
  85. /**
  86. * @brief Sets the current working directory for the calling LWP or system
  87. *
  88. * @param[in] buf Pointer to the path string to set as working directory
  89. *
  90. * @note This function handles both LWP-specific and system-wide working directories:
  91. * - For LWPs, sets the working_directory in the LWP structure
  92. * - For non-LWP threads, sets the global working_directory variable
  93. */
  94. void lwp_setcwd(char *buf)
  95. {
  96. struct rt_lwp *lwp = RT_NULL;
  97. if(strlen(buf) >= DFS_PATH_MAX)
  98. {
  99. rt_kprintf("buf too long!\n");
  100. return ;
  101. }
  102. lwp = (struct rt_lwp *)rt_thread_self()->lwp;
  103. if (lwp)
  104. {
  105. rt_strncpy(lwp->working_directory, buf, DFS_PATH_MAX - 1);
  106. }
  107. else
  108. {
  109. rt_strncpy(working_directory, buf, DFS_PATH_MAX - 1);
  110. }
  111. return ;
  112. }
  113. /**
  114. * @brief Get the current working directory for the light-weight process
  115. *
  116. * @return char* Pointer to the current working directory string
  117. *
  118. * @note The function returns either:
  119. * - LWP's working directory (if valid and absolute path)
  120. * - System default working directory (if no LWP or invalid path)
  121. */
  122. char *lwp_getcwd(void)
  123. {
  124. char *dir_buf = RT_NULL;
  125. struct rt_lwp *lwp = RT_NULL;
  126. rt_thread_t thread = rt_thread_self();
  127. if (thread)
  128. {
  129. lwp = (struct rt_lwp *)thread->lwp;
  130. }
  131. if (lwp)
  132. {
  133. if(lwp->working_directory[0] != '/')
  134. {
  135. dir_buf = &working_directory[0];
  136. }
  137. else
  138. {
  139. dir_buf = &lwp->working_directory[0];
  140. }
  141. }
  142. else
  143. dir_buf = &working_directory[0];
  144. return dir_buf;
  145. }
  146. /**
  147. * @brief Set the kernel stack pointer for the current thread
  148. *
  149. * @param[in] sp Pointer to the new kernel stack location
  150. *
  151. * @note It's typically used during context switching or thread initialization.
  152. */
  153. void lwp_set_kernel_sp(uint32_t *sp)
  154. {
  155. rt_thread_self()->kernel_sp = (rt_uint32_t *)sp;
  156. }
  157. /**
  158. * @brief Get the kernel stack pointer for the current thread
  159. *
  160. * @return uint32_t* Pointer to the kernel stack
  161. *
  162. * @note Architecture-specific behavior:
  163. * 1. With MMU: Simply returns the current thread's stack pointer
  164. * 2. Without MMU: Checks interrupt context and returns either:
  165. * - Interrupted thread's kernel_sp (if in interrupt)
  166. * - Current thread's kernel_sp (if not in interrupt)
  167. */
  168. uint32_t *lwp_get_kernel_sp(void)
  169. {
  170. #ifdef ARCH_MM_MMU
  171. return (uint32_t *)rt_thread_self()->sp;
  172. #else
  173. uint32_t* kernel_sp;
  174. extern rt_uint32_t rt_interrupt_from_thread;
  175. extern rt_uint32_t rt_thread_switch_interrupt_flag;
  176. if (rt_thread_switch_interrupt_flag)
  177. {
  178. kernel_sp = (uint32_t *)((rt_thread_t)rt_container_of(rt_interrupt_from_thread, struct rt_thread, sp))->kernel_sp;
  179. }
  180. else
  181. {
  182. kernel_sp = (uint32_t *)rt_thread_self()->kernel_sp;
  183. }
  184. return kernel_sp;
  185. #endif
  186. }
  187. /**
  188. * @brief Clean up resources associated with a light-weight process thread
  189. *
  190. * @param[in] tid Pointer to the thread control block to be cleaned up
  191. *
  192. * @note This function performs cleanup operations for a thread associated with a light-weight process (LWP).
  193. * It handles signal detachment and reference count decrement for the LWP structure.
  194. *
  195. */
  196. void lwp_cleanup(struct rt_thread *tid)
  197. {
  198. struct rt_lwp *lwp;
  199. if (tid == NULL)
  200. {
  201. LOG_I("%s: invalid parameter tid == NULL", __func__);
  202. return;
  203. }
  204. else
  205. LOG_D("cleanup thread: %s, stack_addr: 0x%x", tid->parent.name, tid->stack_addr);
  206. /**
  207. * Brief: lwp thread cleanup
  208. *
  209. * Note: Critical Section
  210. * - thread control block (RW. It's ensured that no one else can access tcb
  211. * other than itself)
  212. */
  213. lwp = (struct rt_lwp *)tid->lwp;
  214. lwp_thread_signal_detach(&tid->signal);
  215. /* tty will be release in lwp_ref_dec() if ref is cleared */
  216. lwp_ref_dec(lwp);
  217. return;
  218. }
  219. /**
  220. * @brief Set up standard I/O for a light-weight process
  221. *
  222. * @param[in] lwp Pointer to the light-weight process structure
  223. *
  224. * @note This function initializes the standard input, output, and error streams
  225. * for a light-weight process by opening the console device and associating
  226. * it with file descriptors 0, 1, and 2.
  227. */
  228. static void lwp_execve_setup_stdio(struct rt_lwp *lwp)
  229. {
  230. struct dfs_fdtable *lwp_fdt;
  231. struct dfs_file *cons_file;
  232. int cons_fd;
  233. lwp_fdt = &lwp->fdt;
  234. /* open console */
  235. cons_fd = open("/dev/console", O_RDWR);
  236. if (cons_fd < 0)
  237. {
  238. LOG_E("%s: Cannot open console tty", __func__);
  239. return ;
  240. }
  241. LOG_D("%s: open console as fd %d", __func__, cons_fd);
  242. /* init 4 fds */
  243. lwp_fdt->fds = rt_calloc(4, sizeof(void *));
  244. if (lwp_fdt->fds)
  245. {
  246. cons_file = fd_get(cons_fd);
  247. lwp_fdt->maxfd = 4;
  248. fdt_fd_associate_file(lwp_fdt, 0, cons_file);
  249. fdt_fd_associate_file(lwp_fdt, 1, cons_file);
  250. fdt_fd_associate_file(lwp_fdt, 2, cons_file);
  251. }
  252. close(cons_fd);
  253. return;
  254. }
  255. /**
  256. * @brief Entry point for light-weight process threads
  257. *
  258. * @param[in] parameter Thread parameter (unused)
  259. *
  260. * @note This function is the main entry point for threads created within a light-weight process.
  261. * It handles thread initialization, debug mode setup, and transitions to user mode.
  262. */
  263. static void _lwp_thread_entry(void *parameter)
  264. {
  265. rt_thread_t tid;
  266. struct rt_lwp *lwp;
  267. tid = rt_thread_self();
  268. lwp = (struct rt_lwp *)tid->lwp;
  269. tid->cleanup = lwp_cleanup;
  270. tid->user_stack = RT_NULL;
  271. if (lwp->debug)
  272. {
  273. lwp->bak_first_inst = *(uint32_t *)lwp->text_entry;
  274. *(uint32_t *)lwp->text_entry = dbg_get_ins();
  275. rt_hw_cpu_dcache_ops(RT_HW_CACHE_FLUSH, lwp->text_entry, sizeof(uint32_t));
  276. icache_invalid_all();
  277. }
  278. /**
  279. * without ASID support, it will be a special case when trying to run application
  280. * and exit multiple times and a same page frame allocated to it bound to
  281. * different text segment. Then we are in a situation where icache contains
  282. * out-of-dated data and must be handle by the running core itself.
  283. * with ASID support, this should be a rare case that ASID & page frame both
  284. * identical to previous running application.
  285. *
  286. * For a new application loaded into memory, icache are seen as empty. And there
  287. * should be nothing in the icache entry to match. So this icache invalidation
  288. * operation should have barely influence.
  289. */
  290. rt_hw_icache_invalidate_all();
  291. #ifdef ARCH_MM_MMU
  292. arch_start_umode(lwp->args, lwp->text_entry, (void *)USER_STACK_VEND, (char *)tid->stack_addr + tid->stack_size);
  293. #else
  294. arch_start_umode(lwp->args, lwp->text_entry, lwp->data_entry, (void *)((uint32_t)lwp->data_entry + lwp->data_size));
  295. #endif /* ARCH_MM_MMU */
  296. }
  297. /**
  298. * @brief Get the current light-weight process
  299. *
  300. * @return Pointer to the current light-weight process structure
  301. * RT_NULL if no process is associated with current thread
  302. *
  303. * @note This function retrieves the light-weight process associated with the
  304. * currently running thread.
  305. */
  306. struct rt_lwp *lwp_self(void)
  307. {
  308. rt_thread_t tid;
  309. tid = rt_thread_self();
  310. if (tid)
  311. {
  312. return (struct rt_lwp *)tid->lwp;
  313. }
  314. return RT_NULL;
  315. }
  316. /**
  317. * @brief Register a child process with its parent
  318. *
  319. * @param[in] parent Pointer to the parent process structure
  320. * @param[in] child Pointer to the child process structure to register
  321. *
  322. * @return RT_EOK on success
  323. *
  324. * @note This function adds a child process to its parent's children list and
  325. * increases reference counts for both processes.
  326. */
  327. rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child)
  328. {
  329. /* lwp add to children link */
  330. LWP_LOCK(parent);
  331. child->sibling = parent->first_child;
  332. parent->first_child = child;
  333. child->parent = parent;
  334. LWP_UNLOCK(parent);
  335. LOG_D("%s(parent=%p, child=%p)", __func__, parent, child);
  336. /* parent holds reference to child */
  337. lwp_ref_inc(parent);
  338. /* child holds reference to parent */
  339. lwp_ref_inc(child);
  340. return 0;
  341. }
  342. /**
  343. * @brief Unregister a child process from its parent
  344. *
  345. * @param[in] parent Pointer to the parent process structure
  346. * @param[in] child Pointer to the child process structure to unregister
  347. *
  348. * @return RT_EOK on success
  349. *
  350. * @note This function removes a child process from its parent's children list and
  351. * decreases reference counts for both processes.
  352. */
  353. rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child)
  354. {
  355. struct rt_lwp **lwp_node;
  356. LWP_LOCK(parent);
  357. /* detach from children link */
  358. lwp_node = &parent->first_child;
  359. while (*lwp_node != child)
  360. {
  361. RT_ASSERT(*lwp_node != RT_NULL);
  362. lwp_node = &(*lwp_node)->sibling;
  363. }
  364. (*lwp_node) = child->sibling;
  365. child->parent = RT_NULL;
  366. LWP_UNLOCK(parent);
  367. LOG_D("%s(parent=%p, child=%p)", __func__, parent, child);
  368. lwp_ref_dec(child);
  369. lwp_ref_dec(parent);
  370. return 0;
  371. }
  372. /**
  373. * @brief Copy process arguments and environment variables from kernel space to user space.
  374. *
  375. * @param[in] lwp Pointer to the light-weight process structure
  376. * @param[in] argc Argument count
  377. * @param[in] argv Argument vector
  378. * @param[in] envp Environment variables
  379. *
  380. * @return Pointer to the process auxiliary structure on success
  381. * RT_NULL if memory allocation fails or arguments initialization fails
  382. *
  383. * @note This function performs the following operations:
  384. * 1. Initializes argument information structure
  385. * 2. Copies command line arguments to user space
  386. * 3. Copies environment variables to user space
  387. * 4. Returns the auxiliary structure containing copied data
  388. */
  389. struct process_aux *argscopy(struct rt_lwp *lwp, int argc, char **argv, char **envp)
  390. {
  391. struct lwp_args_info ai;
  392. rt_err_t error;
  393. struct process_aux *ua;
  394. const char **tail_argv[2] = {0};
  395. error = lwp_args_init(&ai);
  396. if (error)
  397. {
  398. return RT_NULL;
  399. }
  400. if (argc > 0)
  401. {
  402. tail_argv[0] = (void *)argv[argc - 1];
  403. argv[argc - 1] = NULL;
  404. lwp_args_put(&ai, (void *)argv, LWP_ARGS_TYPE_KARG);
  405. lwp_args_put(&ai, (void *)tail_argv, LWP_ARGS_TYPE_KARG);
  406. }
  407. lwp_args_put(&ai, (void *)envp, LWP_ARGS_TYPE_KENVP);
  408. ua = lwp_argscopy(lwp, &ai);
  409. lwp_args_detach(&ai);
  410. return ua;
  411. }
  412. /**
  413. * @brief Creates and starts a new LWP by loading and executing the specified executable file.
  414. *
  415. * @param[in] filename Path to the executable file
  416. * @param[in] debug Debug flag (non-zero to enable debugging)
  417. * @param[in] argc Argument count
  418. * @param[in] argv Argument vector
  419. * @param[in] envp Environment variables
  420. *
  421. * @return Process ID (PID) of the new LWP on success
  422. * -EINVAL if filename is NULL
  423. * -EACCES if file is not executable
  424. * -ENOMEM if memory allocation fails
  425. * -RT_ERROR on other failures
  426. *
  427. * @note This function performs the following operations:
  428. * 1. Validates input parameters
  429. * 2. Creates new LWP structure
  430. * 3. Initializes user space (for MMU systems)
  431. * 4. Copies arguments and environment
  432. * 5. Loads the executable
  433. * 6. Sets up standard I/O
  434. * 7. Creates and starts the main thread
  435. */
  436. pid_t lwp_execve(char *filename, int debug, int argc, char **argv, char **envp)
  437. {
  438. int result;
  439. struct rt_lwp *lwp;
  440. char *thread_name;
  441. struct process_aux *aux;
  442. int tid = 0;
  443. if (filename == RT_NULL)
  444. {
  445. return -EINVAL;
  446. }
  447. if (access(filename, X_OK) != 0)
  448. {
  449. return -EACCES;
  450. }
  451. lwp = lwp_create(LWP_CREATE_FLAG_ALLOC_PID | LWP_CREATE_FLAG_NOTRACE_EXEC);
  452. if (lwp == RT_NULL)
  453. {
  454. LOG_E("lwp struct out of memory!\n");
  455. return -ENOMEM;
  456. }
  457. LOG_D("lwp malloc : %p, size: %d!", lwp, sizeof(struct rt_lwp));
  458. if ((tid = lwp_tid_get()) == 0)
  459. {
  460. lwp_ref_dec(lwp);
  461. return -ENOMEM;
  462. }
  463. #ifdef ARCH_MM_MMU
  464. if (lwp_user_space_init(lwp, 0) != 0)
  465. {
  466. lwp_tid_put(tid);
  467. lwp_ref_dec(lwp);
  468. return -ENOMEM;
  469. }
  470. #endif
  471. if ((aux = argscopy(lwp, argc, argv, envp)) == RT_NULL)
  472. {
  473. lwp_tid_put(tid);
  474. lwp_ref_dec(lwp);
  475. return -ENOMEM;
  476. }
  477. result = lwp_load(filename, lwp, RT_NULL, 0, aux);
  478. if (result == RT_EOK)
  479. {
  480. rt_thread_t thread = RT_NULL;
  481. rt_uint32_t priority = 25, tick = 200;
  482. lwp_execve_setup_stdio(lwp);
  483. /* obtain the base name */
  484. thread_name = strrchr(filename, '/');
  485. thread_name = thread_name ? thread_name + 1 : filename;
  486. #ifndef ARCH_MM_MMU
  487. struct lwp_app_head *app_head = lwp->text_entry;
  488. if (app_head->priority)
  489. {
  490. priority = app_head->priority;
  491. }
  492. if (app_head->tick)
  493. {
  494. tick = app_head->tick;
  495. }
  496. #endif /* not defined ARCH_MM_MMU */
  497. thread = rt_thread_create(thread_name, _lwp_thread_entry, RT_NULL,
  498. LWP_TASK_STACK_SIZE, priority, tick);
  499. if (thread != RT_NULL)
  500. {
  501. struct rt_lwp *self_lwp;
  502. rt_session_t session;
  503. rt_processgroup_t group;
  504. thread->tid = tid;
  505. lwp_tid_set_thread(tid, thread);
  506. LOG_D("lwp kernel => (0x%08x, 0x%08x)\n", (rt_size_t)thread->stack_addr,
  507. (rt_size_t)thread->stack_addr + thread->stack_size);
  508. self_lwp = lwp_self();
  509. /* when create init, self_lwp == null */
  510. if (self_lwp == RT_NULL && lwp_to_pid(lwp) != 1)
  511. {
  512. self_lwp = lwp_from_pid_and_lock(1);
  513. }
  514. if (self_lwp)
  515. {
  516. /* lwp add to children link */
  517. lwp_children_register(self_lwp, lwp);
  518. }
  519. session = RT_NULL;
  520. group = RT_NULL;
  521. group = lwp_pgrp_create(lwp);
  522. if (group)
  523. {
  524. lwp_pgrp_insert(group, lwp);
  525. if (self_lwp == RT_NULL)
  526. {
  527. session = lwp_session_create(lwp);
  528. lwp_session_insert(session, group);
  529. }
  530. else
  531. {
  532. session = lwp_session_find(lwp_sid_get_byprocess(self_lwp));
  533. lwp_session_insert(session, group);
  534. }
  535. }
  536. thread->lwp = lwp;
  537. #ifndef ARCH_MM_MMU
  538. struct lwp_app_head *app_head = (struct lwp_app_head*)lwp->text_entry;
  539. thread->user_stack = app_head->stack_offset ?
  540. (void *)(app_head->stack_offset -
  541. app_head->data_offset +
  542. (uint32_t)lwp->data_entry) : RT_NULL;
  543. thread->user_stack_size = app_head->stack_size;
  544. /* init data area */
  545. rt_memset(lwp->data_entry, 0, lwp->data_size);
  546. /* init user stack */
  547. rt_memset(thread->user_stack, '#', thread->user_stack_size);
  548. #endif /* not defined ARCH_MM_MMU */
  549. rt_list_insert_after(&lwp->t_grp, &thread->sibling);
  550. lwp->did_exec = RT_TRUE;
  551. if (debug && rt_dbg_ops)
  552. {
  553. lwp->debug = debug;
  554. rt_thread_control(thread, RT_THREAD_CTRL_BIND_CPU, (void*)0);
  555. }
  556. rt_thread_startup(thread);
  557. return lwp_to_pid(lwp);
  558. }
  559. }
  560. lwp_tid_put(tid);
  561. lwp_ref_dec(lwp);
  562. return -RT_ERROR;
  563. }
  564. #ifdef RT_USING_MUSLLIBC
  565. extern char **__environ;
  566. #else
  567. char **__environ = 0;
  568. #endif
  569. /**
  570. * @brief Execute a new program in the current process context
  571. *
  572. * @param[in] filename Path to the executable file
  573. * @param[in] debug Debug flag (non-zero enables debug mode)
  574. * @param[in] argc Number of command line arguments
  575. * @param[in] argv Array of command line argument strings
  576. *
  577. * @return Process ID (PID) of the new process on success
  578. * Negative error code on failure
  579. *
  580. * @note This is a wrapper function for lwp_execve.
  581. *
  582. * @see lwp_execve()
  583. */
  584. pid_t exec(char *filename, int debug, int argc, char **argv)
  585. {
  586. setenv("OS", "RT-Thread", 1);
  587. return lwp_execve(filename, debug, argc, argv, __environ);
  588. }
  589. #ifdef ARCH_MM_MMU
  590. /**
  591. * @brief Saves thread-specific user settings (TID register)
  592. *
  593. * @param[in,out] thread Pointer to the thread control block
  594. *
  595. * @note This function stores the architecture-specific TID register
  596. * into the specified thread's control block.This is typically used
  597. * when switching between threads to preserve thread-specific settings
  598. */
  599. void lwp_user_setting_save(rt_thread_t thread)
  600. {
  601. if (thread)
  602. {
  603. thread->thread_idr = arch_get_tidr();
  604. }
  605. }
  606. /**
  607. * @brief Restores thread-specific user settings (TID register and debug state)
  608. *
  609. * @param[in] thread Pointer to the thread control block
  610. *
  611. * @note This function restores architecture-specific Thread ID Register (TIDR) value
  612. * and debug-related settings for the specified thread.
  613. */
  614. void lwp_user_setting_restore(rt_thread_t thread)
  615. {
  616. if (!thread)
  617. {
  618. return;
  619. }
  620. #if !defined(ARCH_RISCV64)
  621. /* tidr will be set in RESTORE_ALL in risc-v */
  622. arch_set_tidr(thread->thread_idr);
  623. #endif
  624. if (rt_dbg_ops)
  625. {
  626. struct rt_lwp *l = (struct rt_lwp *)thread->lwp;
  627. if (l != 0)
  628. {
  629. rt_hw_set_process_id((size_t)l->pid);
  630. }
  631. else
  632. {
  633. rt_hw_set_process_id(0);
  634. }
  635. if (l && l->debug)
  636. {
  637. uint32_t step_type = 0;
  638. step_type = dbg_step_type();
  639. if ((step_type == 2) || (thread->step_exec && (step_type == 1)))
  640. {
  641. dbg_activate_step();
  642. }
  643. else
  644. {
  645. dbg_deactivate_step();
  646. }
  647. }
  648. }
  649. }
  650. #endif /* ARCH_MM_MMU */
  651. /**
  652. * @brief Saves user thread context pointer
  653. *
  654. * @param[in] ctx Pointer to user thread context structure to be saved
  655. *
  656. * @note This function stores a pointer to user thread context in the current thread's
  657. * control block for later restoration. The context pointer is typically used
  658. * during thread context switching.
  659. */
  660. void lwp_uthread_ctx_save(void *ctx)
  661. {
  662. rt_thread_t thread;
  663. thread = rt_thread_self();
  664. thread->user_ctx.ctx = ctx;
  665. }
  666. /**
  667. * @brief Restores the user thread context by clearing the context pointer
  668. *
  669. * @note Typically called during thread context switching to clean up any
  670. * previously saved user context.
  671. */
  672. void lwp_uthread_ctx_restore(void)
  673. {
  674. rt_thread_t thread;
  675. thread = rt_thread_self();
  676. thread->user_ctx.ctx = RT_NULL;
  677. }
  678. /**
  679. * @brief Prints a backtrace of the current thread's call stack
  680. *
  681. * @param[in] uthread The thread to backtrace (must be associated with an LWP)
  682. * @param[in] frame Pointer to the initial stack frame
  683. *
  684. * @return RT_EOK on success, -RT_ERROR on failure
  685. *
  686. * @note This function prints a backtrace of the call stack for the specified user thread,
  687. * providing addresses that can be used with addr2line to get file and line information.
  688. */
  689. rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *frame)
  690. {
  691. rt_err_t rc = -RT_ERROR;
  692. long nesting = 0;
  693. char **argv;
  694. rt_lwp_t lwp;
  695. if (uthread && uthread->lwp && rt_scheduler_is_available())
  696. {
  697. lwp = uthread->lwp;
  698. argv = lwp_get_command_line_args(lwp);
  699. if (argv)
  700. {
  701. rt_kprintf("please use: addr2line -e %s -a -f\n", argv[0]);
  702. lwp_free_command_line_args(argv);
  703. }
  704. else
  705. {
  706. rt_kprintf("please use: addr2line -e %s -a -f\n", lwp->cmd);
  707. }
  708. while (nesting < RT_BACKTRACE_LEVEL_MAX_NR)
  709. {
  710. rt_kprintf(" 0x%lx", frame->pc);
  711. if (rt_hw_backtrace_frame_unwind(uthread, frame))
  712. {
  713. break;
  714. }
  715. nesting++;
  716. }
  717. rt_kprintf("\n");
  718. rc = RT_EOK;
  719. }
  720. return rc;
  721. }