shim_thread.h 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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 <pal.h>
  11. #include <list.h>
  12. struct shim_handle;
  13. struct shim_fd_map;
  14. struct shim_dentry;
  15. struct shim_signal_log;
  16. DEFINE_LIST(shim_thread);
  17. DEFINE_LISTP(shim_thread);
  18. struct shim_thread {
  19. /* thread identifiers */
  20. IDTYPE vmid;
  21. IDTYPE pgid, ppid, tgid, tid;
  22. bool in_vm;
  23. LEASETYPE tid_lease;
  24. /* credentials */
  25. IDTYPE uid, gid, euid, egid;
  26. /* thread pal handle */
  27. PAL_HANDLE pal_handle;
  28. /* parent handle */
  29. struct shim_thread * parent;
  30. /* thread leader */
  31. struct shim_thread * leader;
  32. #ifndef ALIAS_VFORK_AS_FORK
  33. /* dummy thread: stores blocked parent thread for vfork */
  34. struct shim_thread * dummy;
  35. #endif
  36. /* child handles; protected by thread->lock */
  37. LISTP_TYPE(shim_thread) children;
  38. /* nodes in child handles; protected by the parent's lock */
  39. LIST_TYPE(shim_thread) siblings;
  40. /* nodes in global handles; protected by thread_list_lock */
  41. LIST_TYPE(shim_thread) list;
  42. struct shim_handle_map * handle_map;
  43. /* child tid */
  44. int* set_child_tid;
  45. int* clear_child_tid; /* LibOS zeroes it to notify parent that thread exited */
  46. int clear_child_tid_pal; /* PAL zeroes it to notify LibOS that thread exited */
  47. /* signal handling */
  48. __sigset_t signal_mask;
  49. struct shim_signal_handle signal_handles[NUM_SIGS];
  50. struct atomic_int has_signal;
  51. struct shim_signal_log * signal_logs;
  52. bool suspend_on_signal;
  53. stack_t signal_altstack;
  54. /* futex robust list */
  55. void * robust_list;
  56. PAL_HANDLE scheduler_event;
  57. PAL_HANDLE exit_event;
  58. int exit_code;
  59. int term_signal; // Store the terminating signal, if any; needed for
  60. // wait() and friends
  61. bool is_alive;
  62. PAL_HANDLE child_exit_event;
  63. LISTP_TYPE(shim_thread) exited_children;
  64. /* file system */
  65. struct shim_dentry * root, * cwd;
  66. mode_t umask;
  67. /* executable */
  68. struct shim_handle * exec;
  69. void * stack, * stack_top, * stack_red;
  70. shim_tcb_t * shim_tcb;
  71. void * frameptr;
  72. REFTYPE ref_count;
  73. struct shim_lock lock;
  74. #ifdef PROFILE
  75. unsigned long exit_time;
  76. #endif
  77. };
  78. DEFINE_LIST(shim_simple_thread);
  79. struct shim_simple_thread {
  80. /* VMID and PIDs */
  81. IDTYPE vmid;
  82. IDTYPE pgid, tgid, tid;
  83. /* exit event and status */
  84. PAL_HANDLE exit_event;
  85. int exit_code;
  86. int term_signal;
  87. bool is_alive;
  88. /* nodes in global handles */
  89. LIST_TYPE(shim_simple_thread) list;
  90. REFTYPE ref_count;
  91. struct shim_lock lock;
  92. #ifdef PROFILE
  93. unsigned long exit_time;
  94. #endif
  95. };
  96. int init_thread (void);
  97. static inline struct shim_thread * shim_thread_self(void)
  98. {
  99. /* TODO: optimize to use single movq %gs:<offset> */
  100. shim_tcb_t * shim_tcb = shim_get_tcb();
  101. return shim_tcb->tp;
  102. }
  103. static inline struct shim_thread * save_shim_thread_self(struct shim_thread * __self)
  104. {
  105. /* TODO: optimize to use single movq %gs:<offset> */
  106. shim_tcb_t * shim_tcb = shim_get_tcb();
  107. shim_tcb->tp = __self;
  108. return __self;
  109. }
  110. static inline bool is_internal(struct shim_thread *thread)
  111. {
  112. return thread->tid >= INTERNAL_TID_BASE;
  113. }
  114. void get_thread (struct shim_thread * thread);
  115. void put_thread (struct shim_thread * thread);
  116. void get_simple_thread (struct shim_simple_thread * thread);
  117. void put_simple_thread (struct shim_simple_thread * thread);
  118. void init_fs_base (unsigned long fs_base, struct shim_thread * thread);
  119. void update_fs_base (unsigned long fs_base);
  120. void debug_setprefix (shim_tcb_t * tcb);
  121. static inline
  122. __attribute__((always_inline))
  123. void debug_setbuf (shim_tcb_t * tcb, bool on_stack)
  124. {
  125. if (!debug_handle)
  126. return;
  127. tcb->debug_buf = on_stack ? __alloca(sizeof(struct debug_buf)) :
  128. malloc(sizeof(struct debug_buf));
  129. debug_setprefix(tcb);
  130. }
  131. static inline
  132. __attribute__((always_inline))
  133. struct shim_thread * get_cur_thread (void)
  134. {
  135. return shim_thread_self();
  136. }
  137. static inline
  138. __attribute__((always_inline))
  139. bool cur_thread_is_alive (void)
  140. {
  141. struct shim_thread * thread = get_cur_thread();
  142. return thread ? thread->is_alive : false;
  143. }
  144. static inline
  145. __attribute__((always_inline))
  146. void set_cur_thread (struct shim_thread * thread)
  147. {
  148. shim_tcb_t * tcb = shim_get_tcb();
  149. IDTYPE tid = 0;
  150. if (thread) {
  151. if (tcb->tp && tcb->tp != thread)
  152. put_thread(tcb->tp);
  153. if (tcb->tp != thread)
  154. get_thread(thread);
  155. tcb->tp = thread;
  156. thread->shim_tcb = tcb;
  157. tid = thread->tid;
  158. if (!is_internal(thread) && !thread->signal_logs)
  159. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  160. NUM_SIGS);
  161. } else if (tcb->tp) {
  162. put_thread(tcb->tp);
  163. tcb->tp = NULL;
  164. } else {
  165. BUG();
  166. }
  167. if (tcb->tid != tid) {
  168. tcb->tid = tid;
  169. debug_setprefix(tcb);
  170. }
  171. }
  172. static inline void thread_setwait (struct shim_thread ** queue,
  173. struct shim_thread * thread)
  174. {
  175. if (!thread)
  176. thread = get_cur_thread();
  177. DkEventClear(thread->scheduler_event);
  178. if (queue) {
  179. get_thread(thread);
  180. *queue = thread;
  181. }
  182. }
  183. static inline int thread_sleep (uint64_t timeout_us)
  184. {
  185. struct shim_thread * cur_thread = get_cur_thread();
  186. if (!cur_thread)
  187. return -EINVAL;
  188. PAL_HANDLE event = cur_thread->scheduler_event;
  189. if (!event)
  190. return -EINVAL;
  191. if ( NULL == DkObjectsWaitAny(1, &event, timeout_us))
  192. return -PAL_ERRNO;
  193. return 0;
  194. }
  195. static inline void thread_wakeup (struct shim_thread * thread)
  196. {
  197. DkEventSet(thread->scheduler_event);
  198. }
  199. extern struct shim_lock thread_list_lock;
  200. /*!
  201. * \brief Look up the thread for a given id.
  202. *
  203. * \param tid Thread id to look for.
  204. *
  205. * Searches global threads list for a thread with id equal to \p tid.
  206. * If no thread was found returns NULL.
  207. * Increases refcount of the returned thread.
  208. */
  209. struct shim_thread* lookup_thread(IDTYPE tid);
  210. struct shim_simple_thread * __lookup_simple_thread (IDTYPE tid);
  211. struct shim_simple_thread * lookup_simple_thread (IDTYPE tid);
  212. void set_as_child (struct shim_thread * parent, struct shim_thread * child);
  213. /* creating and revoking thread objects */
  214. struct shim_thread * get_new_thread (IDTYPE new_tid);
  215. struct shim_thread * get_new_internal_thread (void);
  216. struct shim_simple_thread * get_new_simple_thread (void);
  217. /* thread list utilities */
  218. void add_thread (struct shim_thread * thread);
  219. void del_thread (struct shim_thread * thread);
  220. void add_simple_thread (struct shim_simple_thread * thread);
  221. void del_simple_thread (struct shim_simple_thread * thread);
  222. void cleanup_thread(IDTYPE caller, void* thread);
  223. int check_last_thread(struct shim_thread* self);
  224. #ifndef ALIAS_VFORK_AS_FORK
  225. void switch_dummy_thread (struct shim_thread * thread);
  226. #endif
  227. int walk_thread_list (int (*callback) (struct shim_thread *, void *, bool *),
  228. void * arg);
  229. int walk_simple_thread_list (int (*callback) (struct shim_simple_thread *,
  230. void *, bool *),
  231. void * arg);
  232. /* reference counting of handle maps */
  233. void get_handle_map (struct shim_handle_map * map);
  234. void put_handle_map (struct shim_handle_map * map);
  235. /* retriving handle mapping */
  236. static inline __attribute__((always_inline))
  237. struct shim_handle_map * get_cur_handle_map (struct shim_thread * thread)
  238. {
  239. if (!thread)
  240. thread = get_cur_thread();
  241. return thread ? thread->handle_map : NULL;
  242. }
  243. static inline __attribute__((always_inline))
  244. void set_handle_map (struct shim_thread * thread,
  245. struct shim_handle_map * map)
  246. {
  247. get_handle_map(map);
  248. if (!thread)
  249. thread = get_cur_thread();
  250. if (thread->handle_map)
  251. put_handle_map(thread->handle_map);
  252. thread->handle_map = map;
  253. }
  254. int thread_exit(struct shim_thread* self, bool send_ipc);
  255. noreturn void thread_or_process_exit(int error_code, int term_signal);
  256. /* thread cloning helpers */
  257. struct shim_clone_args {
  258. PAL_HANDLE create_event;
  259. PAL_HANDLE initialize_event;
  260. struct shim_thread * parent, * thread;
  261. void * stack;
  262. unsigned long fs_base;
  263. };
  264. void * allocate_stack (size_t size, size_t protect_size, bool user);
  265. static inline __attribute__((always_inline))
  266. bool check_stack_size (struct shim_thread * cur_thread, int size)
  267. {
  268. if (!cur_thread)
  269. cur_thread = get_cur_thread();
  270. void * rsp;
  271. __asm__ volatile ("movq %%rsp, %0" : "=r"(rsp) :: "memory");
  272. if (rsp <= cur_thread->stack_top && rsp > cur_thread->stack)
  273. return size < rsp - cur_thread->stack;
  274. return false;
  275. }
  276. static inline __attribute__((always_inline))
  277. bool check_on_stack (struct shim_thread * cur_thread, void * mem)
  278. {
  279. if (!cur_thread)
  280. cur_thread = get_cur_thread();
  281. return (mem <= cur_thread->stack_top && mem > cur_thread->stack);
  282. }
  283. int init_stack (const char ** argv, const char ** envp,
  284. int ** argcpp, const char *** argpp,
  285. elf_auxv_t ** auxpp);
  286. #endif /* _SHIM_THREAD_H_ */