lwp.h 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458
  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. * 2018-06-29 heyuanjie first version
  9. * 2019-10-12 Jesven Add MMU and userspace support
  10. * 2020-10-08 Bernard Architecture and code cleanup
  11. * 2021-08-26 linzhenxing add lwp_setcwd\lwp_getcwd
  12. * 2023-11-17 xqyjlj add process group and session support
  13. * 2023-12-02 Shell Add macro to create lwp status and
  14. * fix dead lock problem on pgrp
  15. */
  16. /*
  17. * RT-Thread light-weight process
  18. */
  19. #ifndef __LWP_H__
  20. #define __LWP_H__
  21. #include <stdint.h>
  22. #include <rthw.h>
  23. #include <rtthread.h>
  24. #include <dfs.h>
  25. #include "lwp_arch.h"
  26. #include "lwp_pid.h"
  27. #include "lwp_ipc.h"
  28. #include "lwp_signal.h"
  29. #include "lwp_syscall.h"
  30. #include "lwp_avl.h"
  31. #include "lwp_args.h"
  32. #include "mm_aspace.h"
  33. #ifdef RT_USING_MUSLLIBC
  34. #include "libc_musl.h"
  35. #endif /* RT_USING_MUSLLIBC */
  36. #ifdef ARCH_MM_MMU
  37. #include "lwp_shm.h"
  38. #include <locale.h>
  39. #include "mmu.h"
  40. #include "page.h"
  41. #else
  42. #include "lwp_mpu.h"
  43. #endif /* ARCH_MM_MMU */
  44. #ifdef RT_USING_MUSLLIBC
  45. #include <locale.h>
  46. #endif /* RT_USING_MUSLLIBC */
  47. #ifdef __cplusplus
  48. extern "C" {
  49. #endif
  50. #define LWP_MAGIC 0x5A
  51. #define LWP_TYPE_FIX_ADDR 0x01
  52. #define LWP_TYPE_DYN_ADDR 0x02
  53. #define LWP_ARG_MAX 8
  54. /**
  55. * @brief Light-weight process memory objects structure
  56. */
  57. struct rt_lwp_objs
  58. {
  59. rt_aspace_t source; /**< The address space associated with this LWP */
  60. struct rt_mem_obj mem_obj; /**< The memory object containing memory management information */
  61. };
  62. /**
  63. * @brief Light-weight process notification structure
  64. */
  65. struct rt_lwp_notify
  66. {
  67. /**
  68. * @brief Callback function pointer for signal notification
  69. * @param signalfd_queue Wait queue for signal file descriptors
  70. * @param signo Signal number
  71. */
  72. void (*notify)(rt_wqueue_t *signalfd_queue, int signo);
  73. rt_wqueue_t *signalfd_queue; /**< Wait queue for signal file descriptors */
  74. rt_slist_t list_node; /**< List node for notification */
  75. };
  76. struct lwp_tty;
  77. #ifdef RT_USING_MUSLLIBC
  78. #define LWP_COREDUMP_FLAG 0x80
  79. #define LWP_CREATE_STAT_EXIT(exit_code) (((exit_code)&0xff) << 8)
  80. #define LWP_CREATE_STAT_SIGNALED(signo, coredump) (((signo) & 0x7f) | (coredump ? LWP_COREDUMP_FLAG : 0))
  81. #define LWP_CREATE_STAT_STOPPED(signo) (LWP_CREATE_STAT_EXIT(signo) | 0x7f)
  82. #define LWP_CREATE_STAT_CONTINUED (0xffff)
  83. #else
  84. #error "No compatible lwp set status provided for this libc"
  85. #endif
  86. typedef struct rt_lwp *rt_lwp_t;
  87. typedef struct rt_session *rt_session_t;
  88. typedef struct rt_processgroup *rt_processgroup_t;
  89. /**
  90. * @brief Session control structure for process groups
  91. */
  92. struct rt_session {
  93. struct rt_object object;
  94. rt_lwp_t leader; /**< Session leader process */
  95. rt_list_t processgroup; /**< List head of process groups in this session */
  96. pid_t sid; /**< Session ID */
  97. pid_t foreground_pgid; /**< Foreground process group ID */
  98. struct rt_mutex mutex; /**< Mutex for session operations synchronization */
  99. struct lwp_tty *ctty; /**< Control terminal */
  100. };
  101. /**
  102. * @brief Process group control structure
  103. */
  104. struct rt_processgroup {
  105. struct rt_object object;
  106. rt_lwp_t leader; /**< Process group leader process */
  107. rt_list_t process; /**< List head of processes in this process group */
  108. rt_list_t pgrp_list_node; /**< List node for process group */
  109. pid_t pgid; /**< Process group ID */
  110. pid_t sid; /**< Session ID */
  111. struct rt_session *session; /**< Session pointer */
  112. struct rt_mutex mutex; /**< Mutex for process group operations synchronization */
  113. rt_atomic_t ref; /**< Reference count for process group */
  114. /* flags on process group */
  115. unsigned int is_orphaned:1; /**< Whether the process group is orphaned */
  116. };
  117. /**
  118. * @brief Light-weight process structure
  119. */
  120. struct rt_lwp
  121. {
  122. #ifdef ARCH_MM_MMU
  123. size_t end_heap; /**< End address of heap */
  124. rt_aspace_t aspace; /**< Address space associated with this LWP */
  125. #else
  126. #ifdef ARCH_MM_MPU
  127. struct rt_mpu_info mpu_info; /**< MPU information for this LWP */
  128. #endif /* ARCH_MM_MPU */
  129. #endif /* ARCH_MM_MMU */
  130. #ifdef RT_USING_SMP
  131. int bind_cpu; /**< CPU ID to which the LWP is bound */
  132. #endif
  133. uint8_t lwp_type; /**< Type of LWP */
  134. uint8_t reserv[3];
  135. /* flags */
  136. unsigned int terminated:1; /**< Process termination flag */
  137. unsigned int background:1; /**< Background process flag */
  138. unsigned int term_ctrlterm:1; /**< have control terminal? */
  139. unsigned int did_exec:1; /**< Whether exec has been performed */
  140. unsigned int jobctl_stopped:1; /**< job control: current proc is stopped */
  141. unsigned int wait_reap_stp:1; /**< job control: has wait event for parent */
  142. unsigned int sig_protected:1; /**< signal: protected proc cannot be killed or stopped */
  143. struct rt_lwp *parent; /**< parent process */
  144. struct rt_lwp *first_child; /**< first child process */
  145. struct rt_lwp *sibling; /**< sibling(child) process */
  146. struct rt_wqueue waitpid_waiters; /**< Wait queue for waitpid system call */
  147. lwp_status_t lwp_status; /**< Status of LWP */
  148. void *text_entry; /**< Entry point of text segment */
  149. uint32_t text_size; /**< Size of text segment */
  150. void *data_entry; /**< Entry point of data segment */
  151. uint32_t data_size; /**< Size of data segment */
  152. rt_atomic_t ref; /**< Reference count for LWP */
  153. void *args; /**< Arguments passed to LWP */
  154. uint32_t args_length; /**< Length of arguments */
  155. pid_t pid; /**< Process ID */
  156. pid_t sid; /**< session ID */
  157. pid_t pgid; /**< process group ID */
  158. struct rt_processgroup *pgrp; /**< process group */
  159. rt_list_t pgrp_node; /**< process group node */
  160. rt_list_t t_grp; /**< thread group */
  161. rt_list_t timer; /**< POSIX timer object binding to a process */
  162. struct dfs_fdtable fdt; /**< File descriptor table */
  163. char cmd[RT_NAME_MAX]; /**< process name */
  164. char *exe_file; /**< process file path */
  165. /* POSIX signal */
  166. struct lwp_signal signal; /**< Signal handling structure */
  167. struct lwp_avl_struct *object_root; /**< AVL tree root for objects */
  168. struct rt_mutex object_mutex; /**< Mutex for object operations synchronization */
  169. struct rt_user_context user_ctx; /**< User context for LWP */
  170. struct rt_wqueue wait_queue; /**< wait queue for console */
  171. struct tty_struct *tty; /**< Controlling terminal, NULL if no tty */
  172. struct lwp_avl_struct *address_search_head; /**< for saving private futexes with a user-space address key */
  173. char working_directory[DFS_PATH_MAX]; /**< Current working directory */
  174. int debug; /**< Debug flag */
  175. rt_uint32_t bak_first_inst; /**< backup of first instruction */
  176. struct rt_mutex lwp_lock; /**< Mutex for LWP operations synchronization */
  177. rt_slist_t signalfd_notify_head; /**< Signal file descriptor notification head */
  178. #ifdef LWP_ENABLE_ASID
  179. uint64_t generation; /**< ASID generation */
  180. unsigned int asid; /**< Address space ID */
  181. #endif
  182. struct rusage rt_rusage; /**< Resource usage information */
  183. #ifdef RT_USING_VDSO
  184. void *vdso_vbase; /**< VDSO base address */
  185. #endif
  186. };
  187. struct rt_lwp *lwp_self(void);
  188. rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child);
  189. rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child);
  190. /**
  191. * @brief LWP exit request type
  192. */
  193. enum lwp_exit_request_type
  194. {
  195. LWP_EXIT_REQUEST_NONE = 0, /**< No exit request */
  196. LWP_EXIT_REQUEST_TRIGGERED, /**< Exit request triggered */
  197. LWP_EXIT_REQUEST_IN_PROCESS, /**< Exit request in process */
  198. };
  199. struct termios *get_old_termios(void);
  200. void lwp_setcwd(char *buf);
  201. char *lwp_getcwd(void);
  202. int lwp_check_exit_request(void);
  203. void lwp_terminate(struct rt_lwp *lwp);
  204. int lwp_tid_init(void);
  205. int lwp_tid_get(void);
  206. void lwp_tid_put(int tid);
  207. /**
  208. * @brief Automatically get a thread and increase a reference count
  209. *
  210. * @param tid queried thread ID
  211. * @return rt_thread_t
  212. */
  213. rt_thread_t lwp_tid_get_thread_and_inc_ref(int tid);
  214. rt_thread_t lwp_tid_get_thread_raw(int tid);
  215. /**
  216. * @brief Decrease a reference count
  217. *
  218. * @param thread target thread
  219. */
  220. void lwp_tid_dec_ref(rt_thread_t thread);
  221. void lwp_tid_set_thread(int tid, rt_thread_t thread);
  222. int lwp_execve(char *filename, int debug, int argc, char **argv, char **envp);
  223. int lwp_load(const char *filename, struct rt_lwp *lwp, uint8_t *load_addr, size_t addr_size, struct process_aux *aux);
  224. void lwp_user_obj_free(struct rt_lwp *lwp);
  225. /*create by lwp_setsid.c*/
  226. int setsid(void);
  227. #ifdef ARCH_MM_MMU
  228. void lwp_aspace_switch(struct rt_thread *thread);
  229. #endif
  230. void lwp_user_setting_save(rt_thread_t thread);
  231. void lwp_user_setting_restore(rt_thread_t thread);
  232. void lwp_uthread_ctx_save(void *ctx);
  233. void lwp_uthread_ctx_restore(void);
  234. int lwp_setaffinity(int tid, int cpu);
  235. pid_t exec(char *filename, int debug, int argc, char **argv);
  236. /* ctime lwp API */
  237. int timer_list_free(rt_list_t *timer_list);
  238. rt_err_t lwp_futex_init(void);
  239. rt_err_t lwp_futex(struct rt_lwp *lwp, int *uaddr, int op, int val,
  240. const struct timespec *timeout, int *uaddr2, int val3);
  241. /* processgroup api */
  242. rt_inline pid_t lwp_pgid_get_bypgrp(rt_processgroup_t group)
  243. {
  244. return group ? group->pgid : 0;
  245. }
  246. rt_inline pid_t lwp_pgid_get_byprocess(rt_lwp_t process)
  247. {
  248. return process ? process->pgid : 0;
  249. }
  250. rt_processgroup_t lwp_pgrp_find(pid_t pgid);
  251. void lwp_pgrp_dec_ref(rt_processgroup_t pgrp);
  252. rt_processgroup_t lwp_pgrp_find_and_inc_ref(pid_t pgid);
  253. rt_processgroup_t lwp_pgrp_create(rt_lwp_t leader);
  254. int lwp_pgrp_delete(rt_processgroup_t group);
  255. /**
  256. * Note: all the pgrp with process operation must be called in the context where
  257. * process lock is taken. This is protect us from a possible dead lock condition
  258. *
  259. * The order is mandatory in the case:
  260. * PGRP_LOCK(pgrp);
  261. * LWP_LOCK(p);
  262. * ... bussiness logic
  263. * LWP_UNLOCK(p);
  264. * PGRP_UNLOCK(pgrp);
  265. */
  266. int lwp_pgrp_insert(rt_processgroup_t group, rt_lwp_t process);
  267. int lwp_pgrp_remove(rt_processgroup_t group, rt_lwp_t process);
  268. int lwp_pgrp_move(rt_processgroup_t group, rt_lwp_t process);
  269. int lwp_pgrp_update_children_info(rt_processgroup_t group, pid_t sid, pid_t pgid);
  270. /* session api */
  271. rt_inline pid_t lwp_sid_get_bysession(rt_session_t session)
  272. {
  273. return session ? session->sid : 0;
  274. }
  275. rt_inline pid_t lwp_sid_get_bypgrp(rt_processgroup_t group)
  276. {
  277. return group ? group->sid : 0;
  278. }
  279. rt_inline pid_t lwp_sid_get_byprocess(rt_lwp_t process)
  280. {
  281. return process ? process->sid : 0;
  282. }
  283. rt_session_t lwp_session_find(pid_t sid);
  284. rt_session_t lwp_session_create(struct rt_lwp *leader);
  285. int lwp_session_delete(rt_session_t session);
  286. /**
  287. * Note: all the session operation must be called in the context where
  288. * process lock is taken. This is protect us from a possible dead lock condition
  289. *
  290. * The order is mandatory in the case:
  291. * PGRP_LOCK(pgrp);
  292. * LWP_LOCK(p);
  293. * ... bussiness logic
  294. * LWP_UNLOCK(p);
  295. * PGRP_UNLOCK(pgrp);
  296. */
  297. int lwp_session_insert(rt_session_t session, rt_processgroup_t group);
  298. int lwp_session_remove(rt_session_t session, rt_processgroup_t group);
  299. int lwp_session_move(rt_session_t session, rt_processgroup_t group);
  300. int lwp_session_update_children_info(rt_session_t session, pid_t sid);
  301. int lwp_session_set_foreground(rt_session_t session, pid_t pgid);
  302. /* complete the job control related bussiness on process exit */
  303. void lwp_jobctrl_on_exit(struct rt_lwp *lwp);
  304. sysret_t lwp_teardown(struct rt_lwp *lwp, void (*cb)(void));
  305. #ifdef __cplusplus
  306. }
  307. #endif
  308. #ifndef AUX_ARRAY_ITEMS_NR
  309. #define AUX_ARRAY_ITEMS_NR 32
  310. #endif
  311. /* aux key */
  312. #define AT_NULL 0
  313. #define AT_IGNORE 1
  314. #define AT_EXECFD 2
  315. #define AT_PHDR 3
  316. #define AT_PHENT 4
  317. #define AT_PHNUM 5
  318. #define AT_PAGESZ 6
  319. #define AT_BASE 7
  320. #define AT_FLAGS 8
  321. #define AT_ENTRY 9
  322. #define AT_NOTELF 10
  323. #define AT_UID 11
  324. #define AT_EUID 12
  325. #define AT_GID 13
  326. #define AT_EGID 14
  327. #define AT_CLKTCK 17
  328. #define AT_PLATFORM 15
  329. #define AT_HWCAP 16
  330. #define AT_FPUCW 18
  331. #define AT_DCACHEBSIZE 19
  332. #define AT_ICACHEBSIZE 20
  333. #define AT_UCACHEBSIZE 21
  334. #define AT_IGNOREPPC 22
  335. #define AT_SECURE 23
  336. #define AT_BASE_PLATFORM 24
  337. #define AT_RANDOM 25
  338. #define AT_HWCAP2 26
  339. #define AT_EXECFN 31
  340. #define AT_SYSINFO_EHDR 33
  341. /**
  342. * @brief Process auxiliary vector item
  343. */
  344. struct process_aux_item
  345. {
  346. size_t key; /**< Auxiliary vector key */
  347. size_t value; /**< Auxiliary vector value */
  348. };
  349. /**
  350. * @brief Process auxiliary vector
  351. */
  352. struct process_aux
  353. {
  354. struct process_aux_item item[AUX_ARRAY_ITEMS_NR]; /**< Auxiliary vector items */
  355. };
  356. /**
  357. * @brief Debug operations structure
  358. */
  359. struct dbg_ops_t
  360. {
  361. int (*dbg)(int argc, char **argv); /**< Debug function */
  362. uint32_t (*arch_get_ins)(void); /**< Architecture-specific instruction getter */
  363. void (*arch_activate_step)(void); /**< Architecture-specific step activation */
  364. void (*arch_deactivate_step)(void); /**< Architecture-specific step deactivation */
  365. int (*check_debug_event)(struct rt_hw_exp_stack *regs, unsigned long esr); /**< Debug event checker */
  366. rt_channel_t (*gdb_get_server_channel)(void); /**< GDB server channel getter */
  367. int (*gdb_get_step_type)(void); /**< GDB step type getter */
  368. void (*lwp_check_debug_attach_req)(void *pc); /**< LWP debug attach request checker */
  369. int (*lwp_check_debug_suspend)(void); /**< LWP debug suspend checker */
  370. };
  371. extern struct dbg_ops_t *rt_dbg_ops;
  372. int dbg_thread_in_debug(void);
  373. void dbg_register(struct dbg_ops_t *dbg_ops);
  374. uint32_t dbg_get_ins(void);
  375. void dbg_activate_step(void);
  376. void dbg_deactivate_step(void);
  377. int dbg_check_event(struct rt_hw_exp_stack *regs, unsigned long arg);
  378. rt_channel_t gdb_server_channel(void);
  379. int dbg_step_type(void);
  380. void dbg_attach_req(void *pc);
  381. int dbg_check_suspend(void);
  382. void rt_hw_set_process_id(int pid);
  383. void lwp_futex_exit_robust_list(rt_thread_t thread);
  384. /* backtrace service */
  385. rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *frame);
  386. #endif