shim_thread.h 9.3 KB

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