shim_thread.h 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354
  1. #ifndef _SHIM_THREAD_H_
  2. #define _SHIM_THREAD_H_
  3. #include <shim_defs.h>
  4. #include <shim_internal.h>
  5. #include <shim_tls.h>
  6. #include <shim_utils.h>
  7. #include <shim_signal.h>
  8. #include <shim_handle.h>
  9. #include <shim_vma.h>
  10. #include <pal.h>
  11. #include <list.h>
  12. struct shim_handle;
  13. struct shim_fd_map;
  14. struct shim_dentry;
  15. struct shim_signal_log;
  16. DEFINE_LIST(shim_thread);
  17. DEFINE_LISTP(shim_thread);
  18. struct shim_thread {
  19. /* thread identifiers */
  20. IDTYPE vmid;
  21. IDTYPE pgid, ppid, tgid, tid;
  22. bool in_vm;
  23. LEASETYPE tid_lease;
  24. /* credentials */
  25. IDTYPE uid, gid, euid, egid;
  26. /* thread pal handle */
  27. PAL_HANDLE pal_handle;
  28. /* parent handle */
  29. struct shim_thread * parent;
  30. /* thread leader */
  31. struct shim_thread * leader;
  32. #ifndef ALIAS_VFORK_AS_FORK
  33. /* dummy thread: stores blocked parent thread for vfork */
  34. struct shim_thread * dummy;
  35. #endif
  36. /* child handles; protected by thread->lock */
  37. LISTP_TYPE(shim_thread) children;
  38. /* nodes in child handles; protected by the parent's lock */
  39. LIST_TYPE(shim_thread) siblings;
  40. /* nodes in global handles; protected by thread_list_lock */
  41. LIST_TYPE(shim_thread) list;
  42. struct shim_handle_map * handle_map;
  43. /* child tid */
  44. int * set_child_tid, * clear_child_tid;
  45. /* signal handling */
  46. __sigset_t signal_mask;
  47. struct shim_signal_handle signal_handles[NUM_SIGS];
  48. struct atomic_int has_signal;
  49. struct shim_signal_log * signal_logs;
  50. bool suspend_on_signal;
  51. stack_t signal_altstack;
  52. /* futex robust list */
  53. void * robust_list;
  54. PAL_HANDLE scheduler_event;
  55. PAL_HANDLE exit_event;
  56. int exit_code;
  57. int term_signal; // Store the terminating signal, if any; needed for
  58. // wait() and friends
  59. bool is_alive;
  60. PAL_HANDLE child_exit_event;
  61. LISTP_TYPE(shim_thread) exited_children;
  62. /* file system */
  63. struct shim_dentry * root, * cwd;
  64. mode_t umask;
  65. /* executable */
  66. struct shim_handle * exec;
  67. void * stack, * stack_top, * stack_red;
  68. __libc_tcb_t * tcb;
  69. bool user_tcb; /* is tcb assigned by user? */
  70. void * frameptr;
  71. REFTYPE ref_count;
  72. struct shim_lock lock;
  73. #ifdef PROFILE
  74. unsigned long exit_time;
  75. #endif
  76. };
  77. DEFINE_LIST(shim_simple_thread);
  78. struct shim_simple_thread {
  79. /* VMID and PIDs */
  80. IDTYPE vmid;
  81. IDTYPE pgid, tgid, tid;
  82. /* exit event and status */
  83. PAL_HANDLE exit_event;
  84. int exit_code;
  85. int term_signal;
  86. bool is_alive;
  87. /* nodes in global handles */
  88. LIST_TYPE(shim_simple_thread) list;
  89. REFTYPE ref_count;
  90. struct shim_lock lock;
  91. #ifdef PROFILE
  92. unsigned long exit_time;
  93. #endif
  94. };
  95. int init_thread (void);
  96. static inline struct shim_thread * shim_thread_self(void)
  97. {
  98. struct shim_thread * __self;
  99. __asm__ ("movq %%fs:%c1,%q0" : "=r" (__self)
  100. : "i" (offsetof(__libc_tcb_t, shim_tcb.tp)));
  101. return __self;
  102. }
  103. static inline struct shim_thread * save_shim_thread_self(struct shim_thread * __self)
  104. {
  105. __asm__ ("movq %q0,%%fs:%c1" : : "r" (__self),
  106. "i" (offsetof(__libc_tcb_t, shim_tcb.tp)));
  107. return __self;
  108. }
  109. static inline bool is_internal(struct shim_thread *thread)
  110. {
  111. return thread->tid >= INTERNAL_TID_BASE;
  112. }
  113. void get_thread (struct shim_thread * thread);
  114. void put_thread (struct shim_thread * thread);
  115. void get_simple_thread (struct shim_simple_thread * thread);
  116. void put_simple_thread (struct shim_simple_thread * thread);
  117. void allocate_tls (__libc_tcb_t * tcb_location, bool user, struct shim_thread * thread);
  118. void populate_tls (__libc_tcb_t * tcb_location, bool user);
  119. void debug_setprefix (shim_tcb_t * tcb);
  120. static inline
  121. __attribute__((always_inline))
  122. void debug_setbuf (shim_tcb_t * tcb, bool on_stack)
  123. {
  124. if (!debug_handle)
  125. return;
  126. tcb->debug_buf = on_stack ? __alloca(sizeof(struct debug_buf)) :
  127. malloc(sizeof(struct debug_buf));
  128. debug_setprefix(tcb);
  129. }
  130. static inline
  131. __attribute__((always_inline))
  132. struct shim_thread * get_cur_thread (void)
  133. {
  134. return shim_thread_self();
  135. }
  136. static inline
  137. __attribute__((always_inline))
  138. bool cur_thread_is_alive (void)
  139. {
  140. struct shim_thread * thread = get_cur_thread();
  141. return thread ? thread->is_alive : false;
  142. }
  143. static inline
  144. __attribute__((always_inline))
  145. void set_cur_thread (struct shim_thread * thread)
  146. {
  147. shim_tcb_t * tcb = shim_get_tls();
  148. IDTYPE tid = 0;
  149. if (thread) {
  150. if (tcb->tp && tcb->tp != thread)
  151. put_thread(tcb->tp);
  152. if (tcb->tp != thread)
  153. get_thread(thread);
  154. tcb->tp = thread;
  155. thread->tcb = container_of(tcb, __libc_tcb_t, shim_tcb);
  156. tid = thread->tid;
  157. if (!is_internal(thread) && !thread->signal_logs)
  158. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  159. NUM_SIGS);
  160. } else if (tcb->tp) {
  161. put_thread(tcb->tp);
  162. tcb->tp = NULL;
  163. } else {
  164. BUG();
  165. }
  166. if (tcb->tid != tid) {
  167. tcb->tid = tid;
  168. debug_setprefix(tcb);
  169. }
  170. }
  171. static inline void thread_setwait (struct shim_thread ** queue,
  172. struct shim_thread * thread)
  173. {
  174. if (!thread)
  175. thread = get_cur_thread();
  176. get_thread(thread);
  177. DkEventClear(thread->scheduler_event);
  178. if (queue)
  179. *queue = thread;
  180. }
  181. static inline int thread_sleep (uint64_t timeout_us)
  182. {
  183. struct shim_thread * cur_thread = get_cur_thread();
  184. if (!cur_thread)
  185. return -EINVAL;
  186. PAL_HANDLE event = cur_thread->scheduler_event;
  187. if (!event)
  188. return -EINVAL;
  189. if ( NULL == DkObjectsWaitAny(1, &event, timeout_us))
  190. return -PAL_ERRNO;
  191. return 0;
  192. }
  193. static inline void thread_wakeup (struct shim_thread * thread)
  194. {
  195. DkEventSet(thread->scheduler_event);
  196. }
  197. extern struct shim_lock thread_list_lock;
  198. struct shim_thread * __lookup_thread (IDTYPE tid);
  199. struct shim_thread * lookup_thread (IDTYPE tid);
  200. struct shim_simple_thread * __lookup_simple_thread (IDTYPE tid);
  201. struct shim_simple_thread * lookup_simple_thread (IDTYPE tid);
  202. void set_as_child (struct shim_thread * parent, struct shim_thread * child);
  203. /* creating and revoking thread objects */
  204. struct shim_thread * get_new_thread (IDTYPE new_tid);
  205. struct shim_thread * get_new_internal_thread (void);
  206. struct shim_simple_thread * get_new_simple_thread (void);
  207. /* thread list utilities */
  208. void add_thread (struct shim_thread * thread);
  209. void del_thread (struct shim_thread * thread);
  210. void add_simple_thread (struct shim_simple_thread * thread);
  211. void del_simple_thread (struct shim_simple_thread * thread);
  212. int check_last_thread (struct shim_thread * self);
  213. #ifndef ALIAS_VFORK_AS_FORK
  214. void switch_dummy_thread (struct shim_thread * thread);
  215. #endif
  216. int walk_thread_list (int (*callback) (struct shim_thread *, void *, bool *),
  217. void * arg);
  218. int walk_simple_thread_list (int (*callback) (struct shim_simple_thread *,
  219. void *, bool *),
  220. void * arg);
  221. /* reference counting of handle maps */
  222. void get_handle_map (struct shim_handle_map * map);
  223. void put_handle_map (struct shim_handle_map * map);
  224. /* retriving handle mapping */
  225. static inline __attribute__((always_inline))
  226. struct shim_handle_map * get_cur_handle_map (struct shim_thread * thread)
  227. {
  228. if (!thread)
  229. thread = get_cur_thread();
  230. return thread ? thread->handle_map : NULL;
  231. }
  232. static inline __attribute__((always_inline))
  233. void set_handle_map (struct shim_thread * thread,
  234. struct shim_handle_map * map)
  235. {
  236. get_handle_map(map);
  237. if (!thread)
  238. thread = get_cur_thread();
  239. if (thread->handle_map)
  240. put_handle_map(thread->handle_map);
  241. thread->handle_map = map;
  242. }
  243. /* shim exit callback */
  244. int thread_exit (struct shim_thread * self, bool send_ipc);
  245. /* If the process was killed by a signal, pass it in the second
  246. * argument, else pass zero */
  247. int try_process_exit (int error_code, int term_signal);
  248. /* thread cloning helpers */
  249. struct clone_args {
  250. PAL_HANDLE create_event;
  251. PAL_HANDLE initialize_event;
  252. struct shim_thread * parent, * thread;
  253. void * stack;
  254. };
  255. int clone_implementation_wrapper(struct clone_args * arg);
  256. void * allocate_stack (size_t size, size_t protect_size, bool user);
  257. static inline __attribute__((always_inline))
  258. bool check_stack_size (struct shim_thread * cur_thread, int size)
  259. {
  260. if (!cur_thread)
  261. cur_thread = get_cur_thread();
  262. void * rsp;
  263. __asm__ volatile ("movq %%rsp, %0" : "=r"(rsp) :: "memory");
  264. if (rsp <= cur_thread->stack_top && rsp > cur_thread->stack)
  265. return size < rsp - cur_thread->stack;
  266. return false;
  267. }
  268. static inline __attribute__((always_inline))
  269. bool check_on_stack (struct shim_thread * cur_thread, void * mem)
  270. {
  271. if (!cur_thread)
  272. cur_thread = get_cur_thread();
  273. return (mem <= cur_thread->stack_top && mem > cur_thread->stack);
  274. }
  275. int init_stack (const char ** argv, const char ** envp,
  276. int ** argcpp, const char *** argpp,
  277. elf_auxv_t ** auxpp, size_t reserve);
  278. #endif /* _SHIM_THREAD_H_ */