shim_thread.h 8.9 KB

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