shim_thread.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770
  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. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_thread.c
  17. *
  18. * This file contains codes to maintain bookkeeping of threads in library OS.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_fs.h>
  25. #include <shim_checkpoint.h>
  26. #include <pal.h>
  27. #include <list.h>
  28. static IDTYPE tid_alloc_idx __attribute_migratable = 0;
  29. static LISTP_TYPE(shim_thread) thread_list = LISTP_INIT;
  30. DEFINE_LISTP(shim_simple_thread);
  31. static LISTP_TYPE(shim_simple_thread) simple_thread_list = LISTP_INIT;
  32. LOCKTYPE thread_list_lock;
  33. static IDTYPE internal_tid_alloc_idx = INTERNAL_TID_BASE;
  34. PAL_HANDLE thread_start_event = NULL;
  35. //#define DEBUG_REF
  36. int init_thread (void)
  37. {
  38. create_lock(thread_list_lock);
  39. struct shim_thread * cur_thread = get_cur_thread();
  40. if (cur_thread)
  41. return 0;
  42. if (!(cur_thread = get_new_thread(0)))
  43. return -ENOMEM;
  44. cur_thread->in_vm = cur_thread->is_alive = true;
  45. set_cur_thread(cur_thread);
  46. add_thread(cur_thread);
  47. cur_thread->pal_handle = PAL_CB(first_thread);
  48. return 0;
  49. }
  50. void dump_threads (void)
  51. {
  52. struct shim_thread * tmp;
  53. lock(thread_list_lock);
  54. listp_for_each_entry(tmp, &thread_list, list) {
  55. debug("thread %d, vmid = %d, pgid = %d, ppid = %d, tgid = %d, in_vm = %d\n",
  56. tmp->tid, tmp->vmid, tmp->pgid, tmp->ppid, tmp->tgid, tmp->in_vm);
  57. }
  58. unlock(thread_list_lock);
  59. }
  60. struct shim_thread * __lookup_thread (IDTYPE tid)
  61. {
  62. struct shim_thread * tmp;
  63. listp_for_each_entry(tmp, &thread_list, list) {
  64. if (tmp->tid == tid) {
  65. get_thread(tmp);
  66. return tmp;
  67. }
  68. }
  69. return NULL;
  70. }
  71. struct shim_thread * lookup_thread (IDTYPE tid)
  72. {
  73. lock(thread_list_lock);
  74. struct shim_thread * thread = __lookup_thread(tid);
  75. unlock(thread_list_lock);
  76. return thread;
  77. }
  78. struct shim_thread * __get_cur_thread (void)
  79. {
  80. return SHIM_THREAD_SELF();
  81. }
  82. shim_tcb_t * __get_cur_tcb (void)
  83. {
  84. return SHIM_GET_TLS();
  85. }
  86. IDTYPE get_pid (void)
  87. {
  88. IDTYPE idx;
  89. while (1) {
  90. IDTYPE old_idx = tid_alloc_idx;
  91. IDTYPE max = 0;
  92. idx = old_idx + 1;
  93. do {
  94. if ((idx = allocate_pid(idx, max)))
  95. break;
  96. tid_alloc_idx = idx;
  97. if (!idx) {
  98. if (max == old_idx)
  99. break;
  100. max = old_idx;
  101. }
  102. } while (idx != tid_alloc_idx);
  103. if (idx != tid_alloc_idx)
  104. break;
  105. if (ipc_pid_lease_send(NULL) < 0)
  106. return 0;
  107. }
  108. tid_alloc_idx = idx;
  109. return idx;
  110. }
  111. static IDTYPE get_internal_pid (void)
  112. {
  113. lock(thread_list_lock);
  114. internal_tid_alloc_idx++;
  115. IDTYPE idx = internal_tid_alloc_idx;
  116. unlock(thread_list_lock);
  117. return idx;
  118. }
  119. struct shim_thread * alloc_new_thread (void)
  120. {
  121. struct shim_thread * thread = malloc(sizeof(struct shim_thread));
  122. if (!thread)
  123. return NULL;
  124. memset(thread, 0, sizeof(struct shim_thread));
  125. REF_SET(thread->ref_count, 1);
  126. INIT_LISTP(&thread->children);
  127. INIT_LIST_HEAD(thread, siblings);
  128. INIT_LISTP(&thread->exited_children);
  129. INIT_LIST_HEAD(thread, list);
  130. return thread;
  131. }
  132. struct shim_thread * get_new_thread (IDTYPE new_tid)
  133. {
  134. if (!new_tid) {
  135. new_tid = get_pid();
  136. assert(new_tid);
  137. }
  138. struct shim_thread * thread = alloc_new_thread();
  139. if (!thread)
  140. return NULL;
  141. struct shim_thread * cur_thread = get_cur_thread();
  142. thread->tid = new_tid;
  143. if (cur_thread) {
  144. /* The newly created thread will be in the same thread group
  145. (process group as well) with its parent */
  146. thread->pgid = cur_thread->pgid;
  147. thread->ppid = cur_thread->tgid;
  148. thread->tgid = cur_thread->tgid;
  149. thread->uid = cur_thread->uid;
  150. thread->gid = cur_thread->gid;
  151. thread->euid = cur_thread->euid;
  152. thread->egid = cur_thread->egid;
  153. thread->parent = cur_thread;
  154. thread->stack = cur_thread->stack;
  155. thread->stack_top = cur_thread->stack_top;
  156. thread->stack_red = cur_thread->stack_red;
  157. thread->cwd = cur_thread->cwd;
  158. thread->root = cur_thread->root;
  159. thread->umask = cur_thread->umask;
  160. thread->exec = cur_thread->exec;
  161. get_handle(cur_thread->exec);
  162. for (int i = 0 ; i < NUM_SIGS ; i++) {
  163. if (!cur_thread->signal_handles[i].action)
  164. continue;
  165. thread->signal_handles[i].action =
  166. remalloc(cur_thread->signal_handles[i].action,
  167. sizeof(struct shim_signal_handle));
  168. }
  169. memcpy(&thread->signal_mask, &cur_thread->signal_mask,
  170. sizeof(sigset_t));
  171. get_dentry(cur_thread->cwd);
  172. get_dentry(cur_thread->root);
  173. struct shim_handle_map * map = get_cur_handle_map(cur_thread);
  174. assert(map);
  175. set_handle_map(thread, map);
  176. } else {
  177. /* default pid and pgid equals to tid */
  178. thread->ppid = thread->pgid = thread->tgid = new_tid;
  179. path_lookupat(NULL, "/", 0, &thread->root);
  180. char dir_cfg[CONFIG_MAX];
  181. if (root_config &&
  182. get_config(root_config, "fs.start_dir", dir_cfg, CONFIG_MAX) > 0) {
  183. path_lookupat(NULL, dir_cfg, 0, &thread->cwd);
  184. } else if (thread->root) {
  185. get_dentry(thread->root);
  186. thread->cwd = thread->root;
  187. }
  188. }
  189. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  190. NUM_SIGS);
  191. thread->vmid = cur_process.vmid;
  192. create_lock(thread->lock);
  193. thread->scheduler_event = DkNotificationEventCreate(PAL_TRUE);
  194. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  195. thread->child_exit_event = DkNotificationEventCreate(PAL_FALSE);
  196. return thread;
  197. }
  198. struct shim_thread * get_new_internal_thread (void)
  199. {
  200. IDTYPE new_tid = get_internal_pid();
  201. assert(new_tid);
  202. struct shim_thread * thread = alloc_new_thread();
  203. if (!thread)
  204. return NULL;
  205. thread->vmid = cur_process.vmid;
  206. thread->tid = new_tid;
  207. thread->in_vm = thread->is_alive = true;
  208. create_lock(thread->lock);
  209. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  210. return thread;
  211. }
  212. struct shim_simple_thread * __lookup_simple_thread (IDTYPE tid)
  213. {
  214. struct shim_simple_thread * tmp;
  215. listp_for_each_entry(tmp, &simple_thread_list, list) {
  216. if (tmp->tid == tid) {
  217. get_simple_thread(tmp);
  218. return tmp;
  219. }
  220. }
  221. return NULL;
  222. }
  223. struct shim_simple_thread * lookup_simple_thread (IDTYPE tid)
  224. {
  225. lock(thread_list_lock);
  226. struct shim_simple_thread * thread = __lookup_simple_thread(tid);
  227. unlock(thread_list_lock);
  228. return thread;
  229. }
  230. struct shim_simple_thread * get_new_simple_thread (void)
  231. {
  232. struct shim_simple_thread * thread =
  233. malloc(sizeof(struct shim_simple_thread));
  234. if (!thread)
  235. return NULL;
  236. memset(thread, 0, sizeof(struct shim_simple_thread));
  237. INIT_LIST_HEAD(thread, list);
  238. create_lock(thread->lock);
  239. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  240. return thread;
  241. }
  242. void get_thread (struct shim_thread * thread)
  243. {
  244. #ifdef DEBUG_REF
  245. int ref_count = REF_INC(thread->ref_count);
  246. debug("get_thread %p(%d) (ref_count = %d)\n", thread, thread->tid,
  247. ref_count);
  248. #else
  249. REF_INC(thread->ref_count);
  250. #endif
  251. }
  252. void put_thread (struct shim_thread * thread)
  253. {
  254. int ref_count = REF_DEC(thread->ref_count);
  255. #ifdef DEBUG_REF
  256. debug("put thread %p(%d) (ref_count = %d)\n", thread, thread->tid,
  257. ref_count);
  258. #endif
  259. if (!ref_count) {
  260. if (thread->exec)
  261. put_handle(thread->exec);
  262. if (!IS_INTERNAL(thread))
  263. release_pid(thread->tid);
  264. if (MEMORY_MIGRATED(thread))
  265. memset(thread, 0, sizeof(struct shim_thread));
  266. else
  267. free(thread);
  268. }
  269. }
  270. void get_simple_thread (struct shim_simple_thread * thread)
  271. {
  272. REF_INC(thread->ref_count);
  273. }
  274. void put_simple_thread (struct shim_simple_thread * thread)
  275. {
  276. int ref_count = REF_DEC(thread->ref_count);
  277. if (!ref_count) {
  278. /* Simple threads always live on the simple thread list */
  279. listp_del(thread, &simple_thread_list, list);
  280. free(thread);
  281. }
  282. }
  283. void set_as_child (struct shim_thread * parent,
  284. struct shim_thread * child)
  285. {
  286. if (!parent)
  287. parent = get_cur_thread();
  288. get_thread(parent);
  289. get_thread(child);
  290. lock(child->lock);
  291. child->ppid = parent->tid;
  292. child->parent = parent;
  293. lock(parent->lock);
  294. listp_add_tail(child, &parent->children, siblings);
  295. unlock(parent->lock);
  296. unlock(child->lock);
  297. }
  298. void add_thread (struct shim_thread * thread)
  299. {
  300. if (IS_INTERNAL(thread) || !list_empty(thread, list))
  301. return;
  302. struct shim_thread * tmp, * prev = NULL;
  303. lock(thread_list_lock);
  304. /* keep it sorted */
  305. listp_for_each_entry_reverse(tmp, &thread_list, list) {
  306. if (tmp->tid == thread->tid) {
  307. unlock(thread_list_lock);
  308. return;
  309. }
  310. if (tmp->tid < thread->tid) {
  311. prev = tmp;
  312. break;
  313. }
  314. }
  315. get_thread(thread);
  316. listp_add_after(thread, prev, &thread_list, list);
  317. unlock(thread_list_lock);
  318. }
  319. void del_thread (struct shim_thread * thread)
  320. {
  321. debug("del_thread(%p, %d, %d)\n", thread, thread ? thread->tid : -1,
  322. thread->ref_count);
  323. if (IS_INTERNAL(thread) || list_empty(thread, list)) {
  324. debug("del_thread: internal\n");
  325. return;
  326. }
  327. lock(thread_list_lock);
  328. /* thread->list goes on the thread_list */
  329. listp_del_init(thread, &thread_list, list);
  330. unlock(thread_list_lock);
  331. put_thread(thread);
  332. }
  333. void add_simple_thread (struct shim_simple_thread * thread)
  334. {
  335. if (!list_empty(thread, list))
  336. return;
  337. struct shim_simple_thread * tmp, * prev = NULL;
  338. lock(thread_list_lock);
  339. /* keep it sorted */
  340. listp_for_each_entry_reverse(tmp, &simple_thread_list, list) {
  341. if (tmp->tid == thread->tid) {
  342. unlock(thread_list_lock);
  343. return;
  344. }
  345. if (tmp->tid < thread->tid) {
  346. prev = tmp;
  347. break;
  348. }
  349. }
  350. get_simple_thread(thread);
  351. listp_add_after(thread, prev, &simple_thread_list, list);
  352. unlock(thread_list_lock);
  353. }
  354. void del_simple_thread (struct shim_simple_thread * thread)
  355. {
  356. if (list_empty(thread, list))
  357. return;
  358. lock(thread_list_lock);
  359. listp_del_init(thread, &simple_thread_list, list);
  360. unlock(thread_list_lock);
  361. put_simple_thread(thread);
  362. }
  363. int check_last_thread (struct shim_thread * self)
  364. {
  365. struct shim_thread * tmp;
  366. lock(thread_list_lock);
  367. /* find out if there is any thread that is
  368. 1) no current thread 2) in current vm
  369. 3) still alive */
  370. listp_for_each_entry(tmp, &thread_list, list) {
  371. if (tmp->tid &&
  372. (!self || tmp->tid != self->tid) && tmp->in_vm && tmp->is_alive) {
  373. debug("check_last_thread: thread %d is alive\n", tmp->tid);
  374. unlock(thread_list_lock);
  375. return tmp->tid;
  376. }
  377. }
  378. debug("this is the only thread\n", self->tid);
  379. unlock(thread_list_lock);
  380. return 0;
  381. }
  382. int walk_thread_list (int (*callback) (struct shim_thread *, void *, bool *),
  383. void * arg, bool may_write)
  384. {
  385. struct shim_thread * tmp, * n;
  386. bool srched = false;
  387. int ret;
  388. IDTYPE min_tid = 0;
  389. relock:
  390. lock(thread_list_lock);
  391. debug("walk_thread_list(callback=%p)\n", callback);
  392. listp_for_each_entry_safe(tmp, n, &thread_list, list) {
  393. if (tmp->tid <= min_tid)
  394. continue;
  395. bool unlocked = false;
  396. ret = (*callback) (tmp, arg, &unlocked);
  397. if (ret < 0 && ret != -ESRCH) {
  398. if (unlocked)
  399. goto out;
  400. else
  401. goto out_locked;
  402. }
  403. if (ret > 0)
  404. srched = true;
  405. if (unlocked) {
  406. min_tid = tmp->tid;
  407. goto relock;
  408. }
  409. }
  410. ret = srched ? 0 : -ESRCH;
  411. out_locked:
  412. unlock(thread_list_lock);
  413. out:
  414. return ret;
  415. }
  416. int walk_simple_thread_list (int (*callback) (struct shim_simple_thread *,
  417. void *, bool *),
  418. void * arg, bool may_write)
  419. {
  420. struct shim_simple_thread * tmp, * n;
  421. bool srched = false;
  422. int ret;
  423. IDTYPE min_tid = 0;
  424. relock:
  425. lock(thread_list_lock);
  426. listp_for_each_entry_safe(tmp, n, &simple_thread_list, list) {
  427. if (tmp->tid <= min_tid)
  428. continue;
  429. bool unlocked = false;
  430. ret = (*callback) (tmp, arg, &unlocked);
  431. if (ret < 0 && ret != -ESRCH) {
  432. if (unlocked)
  433. goto out;
  434. else
  435. goto out_locked;
  436. }
  437. if (ret > 0)
  438. srched = true;
  439. if (unlocked) {
  440. min_tid = tmp->tid;
  441. goto relock;
  442. }
  443. }
  444. ret = srched ? 0 : -ESRCH;
  445. out_locked:
  446. unlock(thread_list_lock);
  447. out:
  448. return ret;
  449. }
  450. void switch_dummy_thread (struct shim_thread * thread)
  451. {
  452. struct shim_thread * real_thread = thread->dummy;
  453. IDTYPE child = thread->tid;
  454. assert(thread->frameptr);
  455. assert(real_thread->stack);
  456. assert(real_thread->stack_top > real_thread->stack);
  457. memcpy(thread->frameptr, real_thread->stack,
  458. real_thread->stack_top - real_thread->stack);
  459. real_thread->stack = thread->stack;
  460. real_thread->stack_top = thread->stack_top;
  461. real_thread->frameptr = thread->frameptr;
  462. DkSegmentRegister(PAL_SEGMENT_FS, real_thread->tcb);
  463. set_cur_thread(real_thread);
  464. debug("set tcb to %p\n", real_thread->tcb);
  465. debug("jump to the stack %p\n", real_thread->frameptr);
  466. debug("shim_vfork success (returning %d)\n", child);
  467. /* jump onto old stack
  468. we actually pop rbp as rsp, and later we will call 'ret' */
  469. asm volatile("movq %0, %%rbp\r\n"
  470. "leaveq\r\n"
  471. "retq\r\n" :
  472. : "g"(real_thread->frameptr),
  473. "a"(child)
  474. : "memory");
  475. }
  476. BEGIN_CP_FUNC(thread)
  477. {
  478. assert(size == sizeof(struct shim_thread));
  479. struct shim_thread * thread = (struct shim_thread *) obj;
  480. struct shim_thread * new_thread = NULL;
  481. ptr_t off = GET_FROM_CP_MAP(obj);
  482. if (!off) {
  483. off = ADD_CP_OFFSET(sizeof(struct shim_thread));
  484. ADD_TO_CP_MAP(obj, off);
  485. new_thread = (struct shim_thread *) (base + off);
  486. memcpy(new_thread, thread, sizeof(struct shim_thread));
  487. INIT_LISTP(&new_thread->children);
  488. INIT_LIST_HEAD(new_thread, siblings);
  489. INIT_LISTP(&new_thread->exited_children);
  490. INIT_LIST_HEAD(new_thread, list);
  491. new_thread->in_vm = false;
  492. new_thread->parent = NULL;
  493. new_thread->dummy = NULL;
  494. new_thread->handle_map = NULL;
  495. new_thread->root = NULL;
  496. new_thread->cwd = NULL;
  497. new_thread->signal_logs = NULL;
  498. new_thread->robust_list = NULL;
  499. REF_SET(new_thread->ref_count, 0);
  500. for (int i = 0 ; i < NUM_SIGS ; i++)
  501. if (thread->signal_handles[i].action) {
  502. ptr_t soff = ADD_CP_OFFSET(sizeof(struct __kernel_sigaction));
  503. new_thread->signal_handles[i].action
  504. = (struct __kernel_sigaction *) (base + soff);
  505. memcpy(new_thread->signal_handles[i].action,
  506. thread->signal_handles[i].action,
  507. sizeof(struct __kernel_sigaction));
  508. }
  509. DO_CP_MEMBER(handle, thread, new_thread, exec);
  510. DO_CP_MEMBER(handle_map, thread, new_thread, handle_map);
  511. DO_CP_MEMBER(dentry, thread, new_thread, root);
  512. DO_CP_MEMBER(dentry, thread, new_thread, cwd);
  513. ADD_CP_FUNC_ENTRY(off);
  514. } else {
  515. new_thread = (struct shim_thread *) (base + off);
  516. }
  517. if (objp)
  518. *objp = (void *) new_thread;
  519. }
  520. END_CP_FUNC(thread)
  521. BEGIN_RS_FUNC(thread)
  522. {
  523. struct shim_thread * thread = (void *) (base + GET_CP_FUNC_ENTRY());
  524. CP_REBASE(thread->children);
  525. CP_REBASE(thread->siblings);
  526. CP_REBASE(thread->exited_children);
  527. CP_REBASE(thread->list);
  528. CP_REBASE(thread->exec);
  529. CP_REBASE(thread->handle_map);
  530. CP_REBASE(thread->root);
  531. CP_REBASE(thread->cwd);
  532. CP_REBASE(thread->signal_handles);
  533. create_lock(thread->lock);
  534. thread->scheduler_event = DkNotificationEventCreate(PAL_TRUE);
  535. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  536. thread->child_exit_event = DkNotificationEventCreate(PAL_FALSE);
  537. add_thread(thread);
  538. if (thread->exec)
  539. get_handle(thread->exec);
  540. if (thread->handle_map)
  541. get_handle_map(thread->handle_map);
  542. if (thread->root)
  543. get_dentry(thread->root);
  544. if (thread->cwd)
  545. get_dentry(thread->cwd);
  546. DEBUG_RS("tid=%d,tgid=%d,parent=%d,stack=%p,frameptr=%p,tcb=%p",
  547. thread->tid, thread->tgid,
  548. thread->parent ? thread->parent->tid : thread->tid,
  549. thread->stack, thread->frameptr, thread->tcb);
  550. }
  551. END_RS_FUNC(thread)
  552. BEGIN_CP_FUNC(running_thread)
  553. {
  554. assert(size == sizeof(struct shim_thread));
  555. struct shim_thread * thread = (struct shim_thread *) obj;
  556. struct shim_thread * new_thread = NULL;
  557. DO_CP(thread, thread, &new_thread);
  558. ADD_CP_FUNC_ENTRY((ptr_t) new_thread - base);
  559. if (!thread->user_tcb && thread->tcb) {
  560. ptr_t toff = ADD_CP_OFFSET(sizeof(__libc_tcb_t));
  561. new_thread->tcb = (void *) (base + toff);
  562. memcpy(new_thread->tcb, thread->tcb, sizeof(__libc_tcb_t));
  563. }
  564. }
  565. END_CP_FUNC(running_thread)
  566. int resume_wrapper (void * param)
  567. {
  568. struct shim_thread * thread = (struct shim_thread *) param;
  569. assert(thread);
  570. __libc_tcb_t * libc_tcb = (__libc_tcb_t *) thread->tcb;
  571. assert(libc_tcb);
  572. shim_tcb_t * tcb = &libc_tcb->shim_tcb;
  573. assert(tcb->context.sp);
  574. thread->in_vm = thread->is_alive = true;
  575. allocate_tls(libc_tcb, thread->user_tcb, thread);
  576. debug_setbuf(tcb, true);
  577. debug("set tcb to %p\n", libc_tcb);
  578. DkObjectsWaitAny(1, &thread_start_event, NO_TIMEOUT);
  579. restore_context(&tcb->context);
  580. return 0;
  581. }
  582. BEGIN_RS_FUNC(running_thread)
  583. {
  584. struct shim_thread * thread = (void *) (base + GET_CP_FUNC_ENTRY());
  585. struct shim_thread * cur_thread = get_cur_thread();
  586. thread->in_vm = true;
  587. if (!thread->user_tcb)
  588. CP_REBASE(thread->tcb);
  589. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  590. NUM_SIGS);
  591. if (cur_thread) {
  592. PAL_HANDLE handle = DkThreadCreate(resume_wrapper, thread, 0);
  593. if (!thread)
  594. return -PAL_ERRNO;
  595. thread->pal_handle = handle;
  596. } else {
  597. __libc_tcb_t * libc_tcb = (__libc_tcb_t *) thread->tcb;
  598. if (libc_tcb) {
  599. shim_tcb_t * tcb = &libc_tcb->shim_tcb;
  600. assert(tcb->context.sp);
  601. tcb->debug_buf = SHIM_GET_TLS()->debug_buf;
  602. allocate_tls(libc_tcb, thread->user_tcb, thread);
  603. debug_setprefix(tcb);
  604. debug("after resume, set tcb to %p\n", libc_tcb);
  605. } else {
  606. set_cur_thread(thread);
  607. }
  608. thread->in_vm = thread->is_alive = true;
  609. thread->pal_handle = PAL_CB(first_thread);
  610. }
  611. DEBUG_RS("tid=%d", thread->tid);
  612. }
  613. END_RS_FUNC(running_thread)
  614. BEGIN_CP_FUNC(all_running_threads)
  615. {
  616. struct shim_thread * thread;
  617. lock(thread_list_lock);
  618. listp_for_each_entry(thread, &thread_list, list) {
  619. if (!thread->in_vm || !thread->is_alive)
  620. continue;
  621. DO_CP(running_thread, thread, NULL);
  622. DO_CP(handle_map, thread->handle_map, NULL);
  623. }
  624. unlock(thread_list_lock);
  625. }
  626. END_CP_FUNC_NO_RS(all_running_threads)