shim_thread.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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 <linux_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. 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 */
  36. struct list_head children;
  37. /* nodes in child handles */
  38. struct list_head siblings;
  39. /* nodes in global handles */
  40. struct list_head 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 shim_atomic 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. struct list_head 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. void * tcb;
  68. bool user_tcb; /* is tcb assigned by user? */
  69. void * frameptr;
  70. /* to save vma bookkeeping */
  71. struct { void * addr; uint64_t length; } delayed_bkeep_mmap;
  72. REFTYPE ref_count;
  73. LOCKTYPE lock;
  74. #ifdef PROFILE
  75. unsigned long exit_time;
  76. #endif
  77. };
  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. struct list_head list;
  89. REFTYPE ref_count;
  90. LOCKTYPE lock;
  91. #ifdef PROFILE
  92. unsigned long exit_time;
  93. #endif
  94. };
  95. int init_thread (void);
  96. #define SHIM_THREAD_SELF() \
  97. ({ struct shim_thread * __self; \
  98. asm ("movq %%fs:%c1,%q0" : "=r" (__self) \
  99. : "i" (offsetof(__libc_tcb_t, shim_tcb.tp))); \
  100. __self; })
  101. #define SAVE_SHIM_THREAD_SELF(__self) \
  102. ({ asm ("movq %q0,%%fs:%c1" : : "r" (__self), \
  103. "i" (offsetof(__libc_tcb_t, shim_tcb.tp))); \
  104. __self; })
  105. void get_thread (struct shim_thread * thread);
  106. void put_thread (struct shim_thread * thread);
  107. void get_simple_thread (struct shim_simple_thread * thread);
  108. void put_simple_thread (struct shim_simple_thread * thread);
  109. void allocate_tls (void * tcb_location, bool user, struct shim_thread * thread);
  110. void populate_tls (void * tcb_location, bool user);
  111. void debug_setprefix (shim_tcb_t * tcb);
  112. static inline
  113. __attribute__((always_inline))
  114. void debug_setbuf (shim_tcb_t * tcb, bool on_stack)
  115. {
  116. if (!debug_handle)
  117. return;
  118. tcb->debug_buf = on_stack ? __alloca(sizeof(struct debug_buf)) :
  119. malloc(sizeof(struct debug_buf));
  120. debug_setprefix(tcb);
  121. }
  122. static inline
  123. __attribute__((always_inline))
  124. struct shim_thread * get_cur_thread (void)
  125. {
  126. return SHIM_THREAD_SELF();
  127. }
  128. static inline
  129. __attribute__((always_inline))
  130. void set_cur_thread (struct shim_thread * thread)
  131. {
  132. shim_tcb_t * tcb = SHIM_GET_TLS();
  133. IDTYPE tid = 0;
  134. #ifndef container_of
  135. # define container_of(ptr, type, member) ({ \
  136. const typeof( ((type *)0)->member ) *__mptr = (ptr); \
  137. (type *)( (char *)__mptr - offsetof(type,member) );})
  138. #endif
  139. if (thread) {
  140. if (tcb->tp && tcb->tp != thread)
  141. put_thread(tcb->tp);
  142. if (tcb->tp != thread)
  143. get_thread(thread);
  144. tcb->tp = thread;
  145. thread->tcb = container_of(tcb, __libc_tcb_t, shim_tcb);
  146. tid = thread->tid;
  147. if (!IS_INTERNAL(thread) && !thread->signal_logs)
  148. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  149. NUM_SIGS);
  150. } else if (tcb->tp) {
  151. put_thread(tcb->tp);
  152. tcb->tp = NULL;
  153. } else {
  154. bug();
  155. }
  156. if (tcb->tid != tid) {
  157. tcb->tid = tid;
  158. debug_setprefix(tcb);
  159. }
  160. }
  161. static inline void thread_setwait (struct shim_thread ** queue,
  162. struct shim_thread * thread)
  163. {
  164. if (!thread)
  165. thread = get_cur_thread();
  166. get_thread(thread);
  167. DkEventClear(thread->scheduler_event);
  168. if (queue)
  169. *queue = thread;
  170. }
  171. static inline int thread_sleep (uint64_t timeout_us)
  172. {
  173. struct shim_thread * cur_thread = get_cur_thread();
  174. if (!cur_thread)
  175. return -EINVAL;
  176. PAL_HANDLE event = cur_thread->scheduler_event;
  177. if (!event)
  178. return -EINVAL;
  179. if ( NULL == DkObjectsWaitAny(1, &event, timeout_us))
  180. return -PAL_ERRNO;
  181. return 0;
  182. }
  183. static inline void thread_wakeup (struct shim_thread * thread)
  184. {
  185. DkEventSet(thread->scheduler_event);
  186. }
  187. extern LOCKTYPE thread_list_lock;
  188. struct shim_thread * __lookup_thread (IDTYPE tid);
  189. struct shim_thread * lookup_thread (IDTYPE tid);
  190. struct shim_simple_thread * __lookup_simple_thread (IDTYPE tid);
  191. struct shim_simple_thread * lookup_simple_thread (IDTYPE tid);
  192. void set_as_child (struct shim_thread * parent, struct shim_thread * child);
  193. /* creating and revoking thread objects */
  194. struct shim_thread * get_new_thread (IDTYPE new_tid);
  195. struct shim_thread * get_new_internal_thread (void);
  196. struct shim_simple_thread * get_new_simple_thread (void);
  197. /* thread list utilities */
  198. void add_thread (struct shim_thread * thread);
  199. void del_thread (struct shim_thread * thread);
  200. void add_simple_thread (struct shim_simple_thread * thread);
  201. void del_simple_thread (struct shim_simple_thread * thread);
  202. int check_last_thread (struct shim_thread * self);
  203. void switch_dummy_thread (struct shim_thread * thread);
  204. int walk_thread_list (int (*callback) (struct shim_thread *, void *, bool *),
  205. void * arg, bool may_write);
  206. int walk_simple_thread_list (int (*callback) (struct shim_simple_thread *,
  207. void *, bool *),
  208. void * arg, bool may_write);
  209. /* reference counting of handle maps */
  210. void get_handle_map (struct shim_handle_map * map);
  211. void put_handle_map (struct shim_handle_map * map);
  212. /* retriving handle mapping */
  213. static inline __attribute__((always_inline))
  214. struct shim_handle_map * get_cur_handle_map (struct shim_thread * thread)
  215. {
  216. if (!thread)
  217. thread = get_cur_thread();
  218. return thread ? thread->handle_map : NULL;
  219. }
  220. static inline __attribute__((always_inline))
  221. void set_handle_map (struct shim_thread * thread,
  222. struct shim_handle_map * map)
  223. {
  224. get_handle_map(map);
  225. if (!thread)
  226. thread = get_cur_thread();
  227. if (thread->handle_map)
  228. put_handle_map(thread->handle_map);
  229. thread->handle_map = map;
  230. }
  231. /* shim exit callback */
  232. int thread_exit (struct shim_thread * self, bool send_ipc);
  233. /* If the process was killed by a signal, pass it in the second
  234. * argument, else pass zero */
  235. int try_process_exit (int error_code, int term_signal);
  236. /* thread cloning helpers */
  237. struct clone_args {
  238. PAL_HANDLE create_event;
  239. PAL_HANDLE initialize_event;
  240. struct shim_thread * parent, * thread;
  241. void * stack;
  242. void * return_pc;
  243. };
  244. int clone_implementation_wrapper(struct clone_args * arg);
  245. int clean_held_locks (struct shim_thread * self);
  246. void * allocate_stack (size_t size, size_t protect_size, bool user);
  247. int populate_user_stack (void * stack, size_t stack_size,
  248. int nauxv, elf_auxv_t ** auxpp,
  249. const char *** argvp, const char *** envpp);
  250. static inline __attribute__((always_inline))
  251. bool check_stack_size (struct shim_thread * cur_thread, int size)
  252. {
  253. if (!cur_thread)
  254. cur_thread = get_cur_thread();
  255. void * rsp;
  256. asm volatile ("movq %%rsp, %0" : "=r"(rsp) :: "memory");
  257. if (rsp <= cur_thread->stack_top && rsp > cur_thread->stack)
  258. return size < rsp - cur_thread->stack;
  259. return false;
  260. }
  261. static inline __attribute__((always_inline))
  262. bool check_on_stack (struct shim_thread * cur_thread, void * mem)
  263. {
  264. if (!cur_thread)
  265. cur_thread = get_cur_thread();
  266. return (mem <= cur_thread->stack_top && mem > cur_thread->stack);
  267. }
  268. int init_stack (const char ** argv, const char ** envp, const char *** argpp,
  269. int nauxv, elf_auxv_t ** auxpp);
  270. #endif /* _SHIM_THREAD_H_ */