lwp.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. * 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. */
  13. /*
  14. * RT-Thread light-weight process
  15. */
  16. #ifndef __LWP_H__
  17. #define __LWP_H__
  18. #include <stdint.h>
  19. #include <rthw.h>
  20. #include <rtthread.h>
  21. #include <dfs.h>
  22. #include "lwp_arch.h"
  23. #include "lwp_pid.h"
  24. #include "lwp_ipc.h"
  25. #include "lwp_signal.h"
  26. #include "lwp_syscall.h"
  27. #include "lwp_avl.h"
  28. #include "mm_aspace.h"
  29. #ifdef RT_USING_MUSLLIBC
  30. #include "libc_musl.h"
  31. #endif /* RT_USING_MUSLLIBC */
  32. #ifdef ARCH_MM_MMU
  33. #include "lwp_shm.h"
  34. #include <locale.h>
  35. #include "mmu.h"
  36. #include "page.h"
  37. #else
  38. #include "lwp_mpu.h"
  39. #endif /* ARCH_MM_MMU */
  40. #ifdef RT_USING_MUSLLIBC
  41. #include <locale.h>
  42. #endif /* RT_USING_MUSLLIBC */
  43. #ifdef RT_USING_TTY
  44. struct tty_struct;
  45. #endif /* RT_USING_TTY */
  46. #ifdef __cplusplus
  47. extern "C" {
  48. #endif
  49. #define LWP_MAGIC 0x5A
  50. #define LWP_TYPE_FIX_ADDR 0x01
  51. #define LWP_TYPE_DYN_ADDR 0x02
  52. #define LWP_ARG_MAX 8
  53. struct rt_lwp_objs
  54. {
  55. rt_aspace_t source;
  56. struct rt_mem_obj mem_obj;
  57. };
  58. struct rt_lwp_notify
  59. {
  60. void (*notify)(rt_wqueue_t *signalfd_queue, int signo);
  61. rt_wqueue_t *signalfd_queue;
  62. rt_slist_t list_node;
  63. };
  64. #ifdef RT_USING_MUSLLIBC
  65. #define LWP_CREATE_STAT(exit_code) (((exit_code) & 0xff) << 8)
  66. #else
  67. #error "No compatible lwp set status provided for this libc"
  68. #endif
  69. struct rt_lwp
  70. {
  71. #ifdef ARCH_MM_MMU
  72. size_t end_heap;
  73. rt_aspace_t aspace;
  74. #else
  75. #ifdef ARCH_MM_MPU
  76. struct rt_mpu_info mpu_info;
  77. #endif /* ARCH_MM_MPU */
  78. #endif /* ARCH_MM_MMU */
  79. #ifdef RT_USING_SMP
  80. int bind_cpu;
  81. #endif
  82. uint8_t lwp_type;
  83. uint8_t reserv[3];
  84. struct rt_lwp *parent;
  85. struct rt_lwp *first_child;
  86. struct rt_lwp *sibling;
  87. rt_list_t wait_list;
  88. rt_bool_t terminated;
  89. rt_bool_t background;
  90. int lwp_ret;
  91. void *text_entry;
  92. uint32_t text_size;
  93. void *data_entry;
  94. uint32_t data_size;
  95. rt_atomic_t ref;
  96. void *args;
  97. uint32_t args_length;
  98. pid_t pid;
  99. pid_t __pgrp; /*Accessed via process_group()*/
  100. pid_t tty_old_pgrp;
  101. pid_t session;
  102. rt_list_t t_grp;
  103. rt_list_t timer; /* POSIX timer object binding to a process */
  104. int leader; /* boolean value for session group_leader*/
  105. struct dfs_fdtable fdt;
  106. char cmd[RT_NAME_MAX];
  107. /* POSIX signal */
  108. struct lwp_signal signal;
  109. struct lwp_avl_struct *object_root;
  110. struct rt_mutex object_mutex;
  111. struct rt_user_context user_ctx;
  112. struct rt_wqueue wait_queue; /*for console */
  113. struct tty_struct *tty; /* NULL if no tty */
  114. struct lwp_avl_struct *address_search_head; /* for addressed object fast search */
  115. char working_directory[DFS_PATH_MAX];
  116. int debug;
  117. rt_uint32_t bak_first_inst; /* backup of first instruction */
  118. struct rt_mutex lwp_lock;
  119. rt_slist_t signalfd_notify_head;
  120. #ifdef LWP_ENABLE_ASID
  121. uint64_t generation;
  122. unsigned int asid;
  123. #endif
  124. };
  125. typedef struct rt_lwp *rt_lwp_t;
  126. struct rt_lwp *lwp_self(void);
  127. rt_err_t lwp_children_register(struct rt_lwp *parent, struct rt_lwp *child);
  128. rt_err_t lwp_children_unregister(struct rt_lwp *parent, struct rt_lwp *child);
  129. enum lwp_exit_request_type
  130. {
  131. LWP_EXIT_REQUEST_NONE = 0,
  132. LWP_EXIT_REQUEST_TRIGGERED,
  133. LWP_EXIT_REQUEST_IN_PROCESS,
  134. };
  135. struct termios *get_old_termios(void);
  136. void lwp_setcwd(char *buf);
  137. char *lwp_getcwd(void);
  138. void lwp_request_thread_exit(rt_thread_t thread_to_exit);
  139. int lwp_check_exit_request(void);
  140. void lwp_terminate(struct rt_lwp *lwp);
  141. int lwp_tid_init(void);
  142. int lwp_tid_get(void);
  143. void lwp_tid_put(int tid);
  144. /**
  145. * @brief Automatically get a thread and increase a reference count
  146. *
  147. * @param tid queried thread ID
  148. * @return rt_thread_t
  149. */
  150. rt_thread_t lwp_tid_get_thread_and_inc_ref(int tid);
  151. /**
  152. * @brief Decrease a reference count
  153. *
  154. * @param thread target thread
  155. */
  156. void lwp_tid_dec_ref(rt_thread_t thread);
  157. void lwp_tid_set_thread(int tid, rt_thread_t thread);
  158. int lwp_execve(char *filename, int debug, int argc, char **argv, char **envp);
  159. /*create by lwp_setsid.c*/
  160. int setsid(void);
  161. #ifdef ARCH_MM_MMU
  162. void lwp_aspace_switch(struct rt_thread *thread);
  163. #endif
  164. void lwp_user_setting_save(rt_thread_t thread);
  165. void lwp_user_setting_restore(rt_thread_t thread);
  166. void lwp_uthread_ctx_save(void *ctx);
  167. void lwp_uthread_ctx_restore(void);
  168. int lwp_setaffinity(pid_t pid, int cpu);
  169. pid_t exec(char *filename, int debug, int argc, char **argv);
  170. /* ctime lwp API */
  171. int timer_list_free(rt_list_t *timer_list);
  172. struct rt_futex;
  173. rt_err_t lwp_futex(struct rt_lwp *lwp, struct rt_futex *futex, int *uaddr, int op, int val, const struct timespec *timeout);
  174. #ifdef ARCH_MM_MMU
  175. struct __pthread {
  176. /* Part 1 -- these fields may be external or
  177. * * internal (accessed via asm) ABI. Do not change. */
  178. struct pthread *self;
  179. uintptr_t *dtv;
  180. struct pthread *prev, *next; /* non-ABI */
  181. uintptr_t sysinfo;
  182. uintptr_t canary, canary2;
  183. /* Part 2 -- implementation details, non-ABI. */
  184. int tid;
  185. int errno_val;
  186. volatile int detach_state;
  187. volatile int cancel;
  188. volatile unsigned char canceldisable, cancelasync;
  189. unsigned char tsd_used:1;
  190. unsigned char dlerror_flag:1;
  191. unsigned char *map_base;
  192. size_t map_size;
  193. void *stack;
  194. size_t stack_size;
  195. size_t guard_size;
  196. void *result;
  197. struct __ptcb *cancelbuf;
  198. void **tsd;
  199. struct {
  200. volatile void *volatile head;
  201. long off;
  202. volatile void *volatile pending;
  203. } robust_list;
  204. volatile int timer_id;
  205. locale_t locale;
  206. volatile int killlock[1];
  207. char *dlerror_buf;
  208. void *stdio_locks;
  209. /* Part 3 -- the positions of these fields relative to
  210. * * the end of the structure is external and internal ABI. */
  211. uintptr_t canary_at_end;
  212. uintptr_t *dtv_copy;
  213. };
  214. #endif
  215. #ifdef __cplusplus
  216. }
  217. #endif
  218. #ifndef AUX_ARRAY_ITEMS_NR
  219. #define AUX_ARRAY_ITEMS_NR 32
  220. #endif
  221. /* aux key */
  222. #define AT_NULL 0
  223. #define AT_IGNORE 1
  224. #define AT_EXECFD 2
  225. #define AT_PHDR 3
  226. #define AT_PHENT 4
  227. #define AT_PHNUM 5
  228. #define AT_PAGESZ 6
  229. #define AT_BASE 7
  230. #define AT_FLAGS 8
  231. #define AT_ENTRY 9
  232. #define AT_NOTELF 10
  233. #define AT_UID 11
  234. #define AT_EUID 12
  235. #define AT_GID 13
  236. #define AT_EGID 14
  237. #define AT_CLKTCK 17
  238. #define AT_PLATFORM 15
  239. #define AT_HWCAP 16
  240. #define AT_FPUCW 18
  241. #define AT_DCACHEBSIZE 19
  242. #define AT_ICACHEBSIZE 20
  243. #define AT_UCACHEBSIZE 21
  244. #define AT_IGNOREPPC 22
  245. #define AT_SECURE 23
  246. #define AT_BASE_PLATFORM 24
  247. #define AT_RANDOM 25
  248. #define AT_HWCAP2 26
  249. #define AT_EXECFN 31
  250. struct process_aux_item
  251. {
  252. size_t key;
  253. size_t value;
  254. };
  255. struct process_aux
  256. {
  257. struct process_aux_item item[AUX_ARRAY_ITEMS_NR];
  258. };
  259. struct lwp_args_info
  260. {
  261. char **argv;
  262. char **envp;
  263. int argc;
  264. int envc;
  265. int size;
  266. };
  267. struct dbg_ops_t
  268. {
  269. int (*dbg)(int argc, char **argv);
  270. uint32_t (*arch_get_ins)(void);
  271. void (*arch_activate_step)(void);
  272. void (*arch_deactivate_step)(void);
  273. int (*check_debug_event)(struct rt_hw_exp_stack *regs, unsigned long esr);
  274. rt_channel_t (*gdb_get_server_channel)(void);
  275. int (*gdb_get_step_type)(void);
  276. void (*lwp_check_debug_attach_req)(void *pc);
  277. int (*lwp_check_debug_suspend)(void);
  278. };
  279. extern struct dbg_ops_t *rt_dbg_ops;
  280. int dbg_thread_in_debug(void);
  281. void dbg_register(struct dbg_ops_t *dbg_ops);
  282. uint32_t dbg_get_ins(void);
  283. void dbg_activate_step(void);
  284. void dbg_deactivate_step(void);
  285. int dbg_check_event(struct rt_hw_exp_stack *regs, unsigned long arg);
  286. rt_channel_t gdb_server_channel(void);
  287. int dbg_step_type(void);
  288. void dbg_attach_req(void *pc);
  289. int dbg_check_suspend(void);
  290. void rt_hw_set_process_id(int pid);
  291. /* backtrace service */
  292. rt_err_t lwp_backtrace_frame(rt_thread_t uthread, struct rt_hw_backtrace_frame *frame);
  293. #endif