shim_thread.h 11 KB

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