shim_thread.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397
  1. #ifndef _SHIM_THREAD_H_
  2. #define _SHIM_THREAD_H_
  3. #include <shim_defs.h>
  4. #include <shim_internal.h>
  5. #include <shim_tcb.h>
  6. #include <shim_utils.h>
  7. #include <shim_signal.h>
  8. #include <shim_handle.h>
  9. #include <shim_vma.h>
  10. #include <api.h>
  11. #include <pal.h>
  12. #include <list.h>
  13. struct shim_handle;
  14. struct shim_fd_map;
  15. struct shim_dentry;
  16. struct shim_signal_log;
  17. #define WAKE_QUEUE_TAIL ((void*)1)
  18. /* If next is NULL, then this node is not on any queue.
  19. * Otherwise it is a valid pointer to the next node or WAKE_QUEUE_TAIL. */
  20. struct wake_queue_node {
  21. struct wake_queue_node* next;
  22. };
  23. struct wake_queue_head {
  24. struct wake_queue_node* first;
  25. };
  26. DEFINE_LIST(shim_thread);
  27. DEFINE_LISTP(shim_thread);
  28. struct shim_thread {
  29. /* thread identifiers */
  30. IDTYPE vmid;
  31. IDTYPE pgid, ppid, tgid, tid;
  32. bool in_vm;
  33. LEASETYPE tid_lease;
  34. /* credentials */
  35. IDTYPE uid, gid, euid, egid;
  36. /* thread pal handle */
  37. PAL_HANDLE pal_handle;
  38. /* parent handle */
  39. struct shim_thread * parent;
  40. /* thread leader */
  41. struct shim_thread * leader;
  42. #ifndef ALIAS_VFORK_AS_FORK
  43. /* dummy thread: stores blocked parent thread for vfork */
  44. struct shim_thread * dummy;
  45. #endif
  46. /* child handles; protected by thread->lock */
  47. LISTP_TYPE(shim_thread) children;
  48. /* nodes in child handles; protected by the parent's lock */
  49. LIST_TYPE(shim_thread) siblings;
  50. /* nodes in global handles; protected by thread_list_lock */
  51. LIST_TYPE(shim_thread) list;
  52. struct shim_handle_map * handle_map;
  53. /* child tid */
  54. int* set_child_tid;
  55. int* clear_child_tid; /* LibOS zeroes it to notify parent that thread exited */
  56. int clear_child_tid_pal; /* PAL zeroes it to notify LibOS that thread exited */
  57. /* signal handling */
  58. __sigset_t signal_mask;
  59. struct shim_signal_handle signal_handles[NUM_SIGS];
  60. struct atomic_int has_signal;
  61. struct shim_signal_log * signal_logs;
  62. bool suspend_on_signal;
  63. stack_t signal_altstack;
  64. /* futex robust list */
  65. struct robust_list_head* robust_list;
  66. PAL_HANDLE scheduler_event;
  67. struct wake_queue_node wake_queue;
  68. PAL_HANDLE exit_event;
  69. int exit_code;
  70. int term_signal; // Store the terminating signal, if any; needed for
  71. // wait() and friends
  72. bool is_alive;
  73. PAL_HANDLE child_exit_event;
  74. LISTP_TYPE(shim_thread) exited_children;
  75. /* file system */
  76. struct shim_dentry * root, * cwd;
  77. mode_t umask;
  78. /* executable */
  79. struct shim_handle * exec;
  80. void * stack, * stack_top, * stack_red;
  81. shim_tcb_t * shim_tcb;
  82. void * frameptr;
  83. REFTYPE ref_count;
  84. struct shim_lock lock;
  85. #ifdef PROFILE
  86. unsigned long exit_time;
  87. #endif
  88. };
  89. DEFINE_LIST(shim_simple_thread);
  90. struct shim_simple_thread {
  91. /* VMID and PIDs */
  92. IDTYPE vmid;
  93. IDTYPE pgid, tgid, tid;
  94. /* exit event and status */
  95. PAL_HANDLE exit_event;
  96. int exit_code;
  97. int term_signal;
  98. bool is_alive;
  99. /* nodes in global handles */
  100. LIST_TYPE(shim_simple_thread) list;
  101. REFTYPE ref_count;
  102. struct shim_lock lock;
  103. #ifdef PROFILE
  104. unsigned long exit_time;
  105. #endif
  106. };
  107. int init_thread (void);
  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 update_fs_base (unsigned long fs_base);
  117. void debug_setprefix (shim_tcb_t * tcb);
  118. static inline
  119. __attribute__((always_inline))
  120. void debug_setbuf (shim_tcb_t * tcb, bool on_stack)
  121. {
  122. if (!debug_handle)
  123. return;
  124. tcb->debug_buf = on_stack ? __alloca(sizeof(struct debug_buf)) :
  125. malloc(sizeof(struct debug_buf));
  126. debug_setprefix(tcb);
  127. }
  128. static inline
  129. __attribute__((always_inline))
  130. struct shim_thread* get_cur_thread (void) {
  131. return SHIM_TCB_GET(tp);
  132. }
  133. static inline
  134. __attribute__((always_inline))
  135. bool cur_thread_is_alive (void)
  136. {
  137. struct shim_thread * thread = get_cur_thread();
  138. return thread ? thread->is_alive : false;
  139. }
  140. static inline
  141. __attribute__((always_inline))
  142. void set_cur_thread (struct shim_thread * thread)
  143. {
  144. shim_tcb_t * tcb = shim_get_tcb();
  145. IDTYPE tid = 0;
  146. if (thread) {
  147. if (tcb->tp && tcb->tp != thread)
  148. put_thread(tcb->tp);
  149. if (tcb->tp != thread)
  150. get_thread(thread);
  151. tcb->tp = thread;
  152. thread->shim_tcb = tcb;
  153. tid = thread->tid;
  154. if (!is_internal(thread) && !thread->signal_logs) {
  155. thread->signal_logs = signal_logs_alloc();
  156. assert(thread->signal_logs); /* FIXME on ENOMEM */
  157. }
  158. } else if (tcb->tp) {
  159. put_thread(tcb->tp);
  160. tcb->tp = NULL;
  161. } else {
  162. BUG();
  163. }
  164. if (tcb->tid != tid) {
  165. tcb->tid = tid;
  166. if (tcb->debug_buf)
  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. DkEventClear(thread->scheduler_event);
  176. if (queue) {
  177. get_thread(thread);
  178. *queue = thread;
  179. }
  180. }
  181. static inline int thread_sleep (uint64_t timeout_us)
  182. {
  183. struct shim_thread * cur_thread = get_cur_thread();
  184. if (!cur_thread)
  185. return -EINVAL;
  186. PAL_HANDLE event = cur_thread->scheduler_event;
  187. if (!event)
  188. return -EINVAL;
  189. if (!DkSynchronizationObjectWait(event, timeout_us))
  190. return -PAL_ERRNO;
  191. return 0;
  192. }
  193. static inline void thread_wakeup (struct shim_thread * thread)
  194. {
  195. DkEventSet(thread->scheduler_event);
  196. }
  197. /* Adds the thread to the wake-up queue.
  198. * If this thread is already on some queue, then it *will* be woken up soon and there is no need
  199. * to add it to another queue.
  200. * queue->first should be a valid pointer or WAKE_QUEUE_TAIL (i.e. cannot be NULL).
  201. *
  202. * Returns 0 if the thread was added to the queue, 1 otherwise. */
  203. static inline int add_thread_to_queue(struct wake_queue_head* queue, struct shim_thread* thread) {
  204. void* nptr = NULL;
  205. struct wake_queue_node* qnode = &thread->wake_queue;
  206. /* Atomic cmpxchg is enough, no need to take thread->lock */
  207. if (!__atomic_compare_exchange_n(&qnode->next, &nptr, queue->first,
  208. /*weak=*/false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
  209. return 1;
  210. }
  211. queue->first = qnode;
  212. return 0;
  213. }
  214. /* Wakes up all threads on the queue.
  215. * This is a destructive operation - queue cannot be used after calling this function. */
  216. static inline void wake_queue(struct wake_queue_head* queue) {
  217. struct wake_queue_node* qnode = queue->first;
  218. while (qnode != WAKE_QUEUE_TAIL) {
  219. struct shim_thread* thread = container_of(qnode, struct shim_thread, wake_queue);
  220. qnode = qnode->next;
  221. __atomic_store_n(&thread->wake_queue.next, NULL, __ATOMIC_RELAXED);
  222. thread_wakeup(thread);
  223. put_thread(thread);
  224. }
  225. }
  226. extern struct shim_lock thread_list_lock;
  227. /*!
  228. * \brief Look up the thread for a given id.
  229. *
  230. * \param tid Thread id to look for.
  231. *
  232. * Searches global threads list for a thread with id equal to \p tid.
  233. * If no thread was found returns NULL.
  234. * Increases refcount of the returned thread.
  235. */
  236. struct shim_thread* lookup_thread(IDTYPE tid);
  237. struct shim_simple_thread * __lookup_simple_thread (IDTYPE tid);
  238. struct shim_simple_thread * lookup_simple_thread (IDTYPE tid);
  239. void set_as_child (struct shim_thread * parent, struct shim_thread * child);
  240. /* creating and revoking thread objects */
  241. struct shim_thread * get_new_thread (IDTYPE new_tid);
  242. struct shim_thread * get_new_internal_thread (void);
  243. struct shim_simple_thread * get_new_simple_thread (void);
  244. /* thread list utilities */
  245. void add_thread (struct shim_thread * thread);
  246. void del_thread (struct shim_thread * thread);
  247. void add_simple_thread (struct shim_simple_thread * thread);
  248. void del_simple_thread (struct shim_simple_thread * thread);
  249. void cleanup_thread(IDTYPE caller, void* thread);
  250. int check_last_thread(struct shim_thread* self);
  251. #ifndef ALIAS_VFORK_AS_FORK
  252. void switch_dummy_thread (struct shim_thread * thread);
  253. #endif
  254. int walk_thread_list (int (*callback) (struct shim_thread *, void *, bool *),
  255. void * arg);
  256. int walk_simple_thread_list (int (*callback) (struct shim_simple_thread *,
  257. void *, bool *),
  258. void * arg);
  259. /* reference counting of handle maps */
  260. void get_handle_map (struct shim_handle_map * map);
  261. void put_handle_map (struct shim_handle_map * map);
  262. /* retriving handle mapping */
  263. static inline __attribute__((always_inline))
  264. struct shim_handle_map * get_cur_handle_map (struct shim_thread * thread)
  265. {
  266. if (!thread)
  267. thread = get_cur_thread();
  268. return thread ? thread->handle_map : NULL;
  269. }
  270. static inline __attribute__((always_inline))
  271. void set_handle_map (struct shim_thread * thread,
  272. struct shim_handle_map * map)
  273. {
  274. get_handle_map(map);
  275. if (!thread)
  276. thread = get_cur_thread();
  277. if (thread->handle_map)
  278. put_handle_map(thread->handle_map);
  279. thread->handle_map = map;
  280. }
  281. int thread_exit(struct shim_thread* self, bool send_ipc);
  282. noreturn void thread_or_process_exit(int error_code, int term_signal);
  283. void release_robust_list(struct robust_list_head* head);
  284. /* thread cloning helpers */
  285. struct shim_clone_args {
  286. PAL_HANDLE create_event;
  287. PAL_HANDLE initialize_event;
  288. struct shim_thread * parent, * thread;
  289. void * stack;
  290. unsigned long fs_base;
  291. };
  292. void * allocate_stack (size_t size, size_t protect_size, bool user);
  293. static inline __attribute__((always_inline))
  294. bool check_stack_size (struct shim_thread * cur_thread, int size)
  295. {
  296. if (!cur_thread)
  297. cur_thread = get_cur_thread();
  298. void * rsp;
  299. __asm__ volatile ("movq %%rsp, %0" : "=r"(rsp) :: "memory");
  300. if (rsp <= cur_thread->stack_top && rsp > cur_thread->stack)
  301. return size < rsp - cur_thread->stack;
  302. return false;
  303. }
  304. static inline __attribute__((always_inline))
  305. bool check_on_stack (struct shim_thread * cur_thread, void * mem)
  306. {
  307. if (!cur_thread)
  308. cur_thread = get_cur_thread();
  309. return (mem <= cur_thread->stack_top && mem > cur_thread->stack);
  310. }
  311. int init_stack (const char ** argv, const char ** envp,
  312. int ** argcpp, const char *** argpp,
  313. elf_auxv_t ** auxpp);
  314. #endif /* _SHIM_THREAD_H_ */