shim_thread.h 8.9 KB

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