shim_thread.h 8.8 KB

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