lwp.h 14 KB

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