shim_thread.c 20 KB

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