shim_thread.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396
  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 = malloc(sizeof(struct shim_signal_log) *
  156. NUM_SIGS);
  157. } else if (tcb->tp) {
  158. put_thread(tcb->tp);
  159. tcb->tp = NULL;
  160. } else {
  161. BUG();
  162. }
  163. if (tcb->tid != tid) {
  164. tcb->tid = tid;
  165. if (tcb->debug_buf)
  166. debug_setprefix(tcb);
  167. }
  168. }
  169. static inline void thread_setwait (struct shim_thread ** queue,
  170. struct shim_thread * thread)
  171. {
  172. if (!thread)
  173. thread = get_cur_thread();
  174. DkEventClear(thread->scheduler_event);
  175. if (queue) {
  176. get_thread(thread);
  177. *queue = thread;
  178. }
  179. }
  180. static inline int thread_sleep (uint64_t timeout_us)
  181. {
  182. struct shim_thread * cur_thread = get_cur_thread();
  183. if (!cur_thread)
  184. return -EINVAL;
  185. PAL_HANDLE event = cur_thread->scheduler_event;
  186. if (!event)
  187. return -EINVAL;
  188. if ( NULL == DkObjectsWaitAny(1, &event, timeout_us))
  189. return -PAL_ERRNO;
  190. return 0;
  191. }
  192. static inline void thread_wakeup (struct shim_thread * thread)
  193. {
  194. DkEventSet(thread->scheduler_event);
  195. }
  196. /* Adds the thread to the wake-up queue.
  197. * If this thread is already on some queue, then it *will* be woken up soon and there is no need
  198. * to add it to another queue.
  199. * queue->first should be a valid pointer or WAKE_QUEUE_TAIL (i.e. cannot be NULL).
  200. *
  201. * Returns 0 if the thread was added to the queue, 1 otherwise. */
  202. static inline int add_thread_to_queue(struct wake_queue_head* queue, struct shim_thread* thread) {
  203. void* nptr = NULL;
  204. struct wake_queue_node* qnode = &thread->wake_queue;
  205. /* Atomic cmpxchg is enough, no need to take thread->lock */
  206. if (!__atomic_compare_exchange_n(&qnode->next, &nptr, queue->first,
  207. /*weak=*/false, __ATOMIC_RELAXED, __ATOMIC_RELAXED)) {
  208. return 1;
  209. }
  210. queue->first = qnode;
  211. return 0;
  212. }
  213. /* Wakes up all threads on the queue.
  214. * This is a destructive operation - queue cannot be used after calling this function. */
  215. static inline void wake_queue(struct wake_queue_head* queue) {
  216. struct wake_queue_node* qnode = queue->first;
  217. while (qnode != WAKE_QUEUE_TAIL) {
  218. struct shim_thread* thread = container_of(qnode, struct shim_thread, wake_queue);
  219. qnode = qnode->next;
  220. __atomic_store_n(&thread->wake_queue.next, NULL, __ATOMIC_RELAXED);
  221. thread_wakeup(thread);
  222. put_thread(thread);
  223. }
  224. }
  225. extern struct shim_lock thread_list_lock;
  226. /*!
  227. * \brief Look up the thread for a given id.
  228. *
  229. * \param tid Thread id to look for.
  230. *
  231. * Searches global threads list for a thread with id equal to \p tid.
  232. * If no thread was found returns NULL.
  233. * Increases refcount of the returned thread.
  234. */
  235. struct shim_thread* lookup_thread(IDTYPE tid);
  236. struct shim_simple_thread * __lookup_simple_thread (IDTYPE tid);
  237. struct shim_simple_thread * lookup_simple_thread (IDTYPE tid);
  238. void set_as_child (struct shim_thread * parent, struct shim_thread * child);
  239. /* creating and revoking thread objects */
  240. struct shim_thread * get_new_thread (IDTYPE new_tid);
  241. struct shim_thread * get_new_internal_thread (void);
  242. struct shim_simple_thread * get_new_simple_thread (void);
  243. /* thread list utilities */
  244. void add_thread (struct shim_thread * thread);
  245. void del_thread (struct shim_thread * thread);
  246. void add_simple_thread (struct shim_simple_thread * thread);
  247. void del_simple_thread (struct shim_simple_thread * thread);
  248. void cleanup_thread(IDTYPE caller, void* thread);
  249. int check_last_thread(struct shim_thread* self);
  250. #ifndef ALIAS_VFORK_AS_FORK
  251. void switch_dummy_thread (struct shim_thread * thread);
  252. #endif
  253. int walk_thread_list (int (*callback) (struct shim_thread *, void *, bool *),
  254. void * arg);
  255. int walk_simple_thread_list (int (*callback) (struct shim_simple_thread *,
  256. void *, bool *),
  257. void * arg);
  258. /* reference counting of handle maps */
  259. void get_handle_map (struct shim_handle_map * map);
  260. void put_handle_map (struct shim_handle_map * map);
  261. /* retriving handle mapping */
  262. static inline __attribute__((always_inline))
  263. struct shim_handle_map * get_cur_handle_map (struct shim_thread * thread)
  264. {
  265. if (!thread)
  266. thread = get_cur_thread();
  267. return thread ? thread->handle_map : NULL;
  268. }
  269. static inline __attribute__((always_inline))
  270. void set_handle_map (struct shim_thread * thread,
  271. struct shim_handle_map * map)
  272. {
  273. get_handle_map(map);
  274. if (!thread)
  275. thread = get_cur_thread();
  276. if (thread->handle_map)
  277. put_handle_map(thread->handle_map);
  278. thread->handle_map = map;
  279. }
  280. int thread_exit(struct shim_thread* self, bool send_ipc);
  281. noreturn void thread_or_process_exit(int error_code, int term_signal);
  282. void release_robust_list(struct robust_list_head* head);
  283. /* thread cloning helpers */
  284. struct shim_clone_args {
  285. PAL_HANDLE create_event;
  286. PAL_HANDLE initialize_event;
  287. struct shim_thread * parent, * thread;
  288. void * stack;
  289. unsigned long fs_base;
  290. };
  291. void * allocate_stack (size_t size, size_t protect_size, bool user);
  292. static inline __attribute__((always_inline))
  293. bool check_stack_size (struct shim_thread * cur_thread, int size)
  294. {
  295. if (!cur_thread)
  296. cur_thread = get_cur_thread();
  297. void * rsp;
  298. __asm__ volatile ("movq %%rsp, %0" : "=r"(rsp) :: "memory");
  299. if (rsp <= cur_thread->stack_top && rsp > cur_thread->stack)
  300. return size < rsp - cur_thread->stack;
  301. return false;
  302. }
  303. static inline __attribute__((always_inline))
  304. bool check_on_stack (struct shim_thread * cur_thread, void * mem)
  305. {
  306. if (!cur_thread)
  307. cur_thread = get_cur_thread();
  308. return (mem <= cur_thread->stack_top && mem > cur_thread->stack);
  309. }
  310. int init_stack (const char ** argv, const char ** envp,
  311. int ** argcpp, const char *** argpp,
  312. elf_auxv_t ** auxpp);
  313. #endif /* _SHIM_THREAD_H_ */