shim_thread.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772
  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 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 Lesser 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 Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser 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. /* This case should fall back to the global root of the file system.
  180. */
  181. path_lookupat(NULL, "/", 0, &thread->root, NULL);
  182. char dir_cfg[CONFIG_MAX];
  183. if (root_config &&
  184. get_config(root_config, "fs.start_dir", dir_cfg, CONFIG_MAX) > 0) {
  185. path_lookupat(NULL, dir_cfg, 0, &thread->cwd, NULL);
  186. } else if (thread->root) {
  187. get_dentry(thread->root);
  188. thread->cwd = thread->root;
  189. }
  190. }
  191. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  192. NUM_SIGS);
  193. thread->vmid = cur_process.vmid;
  194. create_lock(thread->lock);
  195. thread->scheduler_event = DkNotificationEventCreate(PAL_TRUE);
  196. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  197. thread->child_exit_event = DkNotificationEventCreate(PAL_FALSE);
  198. return thread;
  199. }
  200. struct shim_thread * get_new_internal_thread (void)
  201. {
  202. IDTYPE new_tid = get_internal_pid();
  203. assert(new_tid);
  204. struct shim_thread * thread = alloc_new_thread();
  205. if (!thread)
  206. return NULL;
  207. thread->vmid = cur_process.vmid;
  208. thread->tid = new_tid;
  209. thread->in_vm = thread->is_alive = true;
  210. create_lock(thread->lock);
  211. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  212. return thread;
  213. }
  214. struct shim_simple_thread * __lookup_simple_thread (IDTYPE tid)
  215. {
  216. struct shim_simple_thread * tmp;
  217. listp_for_each_entry(tmp, &simple_thread_list, list) {
  218. if (tmp->tid == tid) {
  219. get_simple_thread(tmp);
  220. return tmp;
  221. }
  222. }
  223. return NULL;
  224. }
  225. struct shim_simple_thread * lookup_simple_thread (IDTYPE tid)
  226. {
  227. lock(thread_list_lock);
  228. struct shim_simple_thread * thread = __lookup_simple_thread(tid);
  229. unlock(thread_list_lock);
  230. return thread;
  231. }
  232. struct shim_simple_thread * get_new_simple_thread (void)
  233. {
  234. struct shim_simple_thread * thread =
  235. malloc(sizeof(struct shim_simple_thread));
  236. if (!thread)
  237. return NULL;
  238. memset(thread, 0, sizeof(struct shim_simple_thread));
  239. INIT_LIST_HEAD(thread, list);
  240. create_lock(thread->lock);
  241. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  242. return thread;
  243. }
  244. void get_thread (struct shim_thread * thread)
  245. {
  246. #ifdef DEBUG_REF
  247. int ref_count = REF_INC(thread->ref_count);
  248. debug("get_thread %p(%d) (ref_count = %d)\n", thread, thread->tid,
  249. ref_count);
  250. #else
  251. REF_INC(thread->ref_count);
  252. #endif
  253. }
  254. void put_thread (struct shim_thread * thread)
  255. {
  256. int ref_count = REF_DEC(thread->ref_count);
  257. #ifdef DEBUG_REF
  258. debug("put thread %p(%d) (ref_count = %d)\n", thread, thread->tid,
  259. ref_count);
  260. #endif
  261. if (!ref_count) {
  262. if (thread->exec)
  263. put_handle(thread->exec);
  264. if (!IS_INTERNAL(thread))
  265. release_pid(thread->tid);
  266. if (MEMORY_MIGRATED(thread))
  267. memset(thread, 0, sizeof(struct shim_thread));
  268. else
  269. free(thread);
  270. }
  271. }
  272. void get_simple_thread (struct shim_simple_thread * thread)
  273. {
  274. REF_INC(thread->ref_count);
  275. }
  276. void put_simple_thread (struct shim_simple_thread * thread)
  277. {
  278. int ref_count = REF_DEC(thread->ref_count);
  279. if (!ref_count) {
  280. /* Simple threads always live on the simple thread list */
  281. listp_del(thread, &simple_thread_list, list);
  282. free(thread);
  283. }
  284. }
  285. void set_as_child (struct shim_thread * parent,
  286. struct shim_thread * child)
  287. {
  288. if (!parent)
  289. parent = get_cur_thread();
  290. get_thread(parent);
  291. get_thread(child);
  292. lock(child->lock);
  293. child->ppid = parent->tid;
  294. child->parent = parent;
  295. lock(parent->lock);
  296. listp_add_tail(child, &parent->children, siblings);
  297. unlock(parent->lock);
  298. unlock(child->lock);
  299. }
  300. void add_thread (struct shim_thread * thread)
  301. {
  302. if (IS_INTERNAL(thread) || !list_empty(thread, list))
  303. return;
  304. struct shim_thread * tmp, * prev = NULL;
  305. lock(thread_list_lock);
  306. /* keep it sorted */
  307. listp_for_each_entry_reverse(tmp, &thread_list, list) {
  308. if (tmp->tid == thread->tid) {
  309. unlock(thread_list_lock);
  310. return;
  311. }
  312. if (tmp->tid < thread->tid) {
  313. prev = tmp;
  314. break;
  315. }
  316. }
  317. get_thread(thread);
  318. listp_add_after(thread, prev, &thread_list, list);
  319. unlock(thread_list_lock);
  320. }
  321. void del_thread (struct shim_thread * thread)
  322. {
  323. debug("del_thread(%p, %d, %d)\n", thread, thread ? thread->tid : -1,
  324. thread->ref_count);
  325. if (IS_INTERNAL(thread) || list_empty(thread, list)) {
  326. debug("del_thread: internal\n");
  327. return;
  328. }
  329. lock(thread_list_lock);
  330. /* thread->list goes on the thread_list */
  331. listp_del_init(thread, &thread_list, list);
  332. unlock(thread_list_lock);
  333. put_thread(thread);
  334. }
  335. void add_simple_thread (struct shim_simple_thread * thread)
  336. {
  337. if (!list_empty(thread, list))
  338. return;
  339. struct shim_simple_thread * tmp, * prev = NULL;
  340. lock(thread_list_lock);
  341. /* keep it sorted */
  342. listp_for_each_entry_reverse(tmp, &simple_thread_list, list) {
  343. if (tmp->tid == thread->tid) {
  344. unlock(thread_list_lock);
  345. return;
  346. }
  347. if (tmp->tid < thread->tid) {
  348. prev = tmp;
  349. break;
  350. }
  351. }
  352. get_simple_thread(thread);
  353. listp_add_after(thread, prev, &simple_thread_list, list);
  354. unlock(thread_list_lock);
  355. }
  356. void del_simple_thread (struct shim_simple_thread * thread)
  357. {
  358. if (list_empty(thread, list))
  359. return;
  360. lock(thread_list_lock);
  361. listp_del_init(thread, &simple_thread_list, list);
  362. unlock(thread_list_lock);
  363. put_simple_thread(thread);
  364. }
  365. int check_last_thread (struct shim_thread * self)
  366. {
  367. struct shim_thread * tmp;
  368. lock(thread_list_lock);
  369. /* find out if there is any thread that is
  370. 1) no current thread 2) in current vm
  371. 3) still alive */
  372. listp_for_each_entry(tmp, &thread_list, list) {
  373. if (tmp->tid &&
  374. (!self || tmp->tid != self->tid) && tmp->in_vm && tmp->is_alive) {
  375. debug("check_last_thread: thread %d is alive\n", tmp->tid);
  376. unlock(thread_list_lock);
  377. return tmp->tid;
  378. }
  379. }
  380. debug("this is the only thread\n", self->tid);
  381. unlock(thread_list_lock);
  382. return 0;
  383. }
  384. int walk_thread_list (int (*callback) (struct shim_thread *, void *, bool *),
  385. void * arg, bool may_write)
  386. {
  387. struct shim_thread * tmp, * n;
  388. bool srched = false;
  389. int ret;
  390. IDTYPE min_tid = 0;
  391. relock:
  392. lock(thread_list_lock);
  393. debug("walk_thread_list(callback=%p)\n", callback);
  394. listp_for_each_entry_safe(tmp, n, &thread_list, list) {
  395. if (tmp->tid <= min_tid)
  396. continue;
  397. bool unlocked = false;
  398. ret = (*callback) (tmp, arg, &unlocked);
  399. if (ret < 0 && ret != -ESRCH) {
  400. if (unlocked)
  401. goto out;
  402. else
  403. goto out_locked;
  404. }
  405. if (ret > 0)
  406. srched = true;
  407. if (unlocked) {
  408. min_tid = tmp->tid;
  409. goto relock;
  410. }
  411. }
  412. ret = srched ? 0 : -ESRCH;
  413. out_locked:
  414. unlock(thread_list_lock);
  415. out:
  416. return ret;
  417. }
  418. int walk_simple_thread_list (int (*callback) (struct shim_simple_thread *,
  419. void *, bool *),
  420. void * arg, bool may_write)
  421. {
  422. struct shim_simple_thread * tmp, * n;
  423. bool srched = false;
  424. int ret;
  425. IDTYPE min_tid = 0;
  426. relock:
  427. lock(thread_list_lock);
  428. listp_for_each_entry_safe(tmp, n, &simple_thread_list, list) {
  429. if (tmp->tid <= min_tid)
  430. continue;
  431. bool unlocked = false;
  432. ret = (*callback) (tmp, arg, &unlocked);
  433. if (ret < 0 && ret != -ESRCH) {
  434. if (unlocked)
  435. goto out;
  436. else
  437. goto out_locked;
  438. }
  439. if (ret > 0)
  440. srched = true;
  441. if (unlocked) {
  442. min_tid = tmp->tid;
  443. goto relock;
  444. }
  445. }
  446. ret = srched ? 0 : -ESRCH;
  447. out_locked:
  448. unlock(thread_list_lock);
  449. out:
  450. return ret;
  451. }
  452. void switch_dummy_thread (struct shim_thread * thread)
  453. {
  454. struct shim_thread * real_thread = thread->dummy;
  455. IDTYPE child = thread->tid;
  456. assert(thread->frameptr);
  457. assert(real_thread->stack);
  458. assert(real_thread->stack_top > real_thread->stack);
  459. memcpy(thread->frameptr, real_thread->stack,
  460. real_thread->stack_top - real_thread->stack);
  461. real_thread->stack = thread->stack;
  462. real_thread->stack_top = thread->stack_top;
  463. real_thread->frameptr = thread->frameptr;
  464. DkSegmentRegister(PAL_SEGMENT_FS, real_thread->tcb);
  465. set_cur_thread(real_thread);
  466. debug("set tcb to %p\n", real_thread->tcb);
  467. debug("jump to the stack %p\n", real_thread->frameptr);
  468. debug("shim_vfork success (returning %d)\n", child);
  469. /* jump onto old stack
  470. we actually pop rbp as rsp, and later we will call 'ret' */
  471. asm volatile("movq %0, %%rbp\r\n"
  472. "leaveq\r\n"
  473. "retq\r\n" :
  474. : "g"(real_thread->frameptr),
  475. "a"(child)
  476. : "memory");
  477. }
  478. BEGIN_CP_FUNC(thread)
  479. {
  480. assert(size == sizeof(struct shim_thread));
  481. struct shim_thread * thread = (struct shim_thread *) obj;
  482. struct shim_thread * new_thread = NULL;
  483. ptr_t off = GET_FROM_CP_MAP(obj);
  484. if (!off) {
  485. off = ADD_CP_OFFSET(sizeof(struct shim_thread));
  486. ADD_TO_CP_MAP(obj, off);
  487. new_thread = (struct shim_thread *) (base + off);
  488. memcpy(new_thread, thread, sizeof(struct shim_thread));
  489. INIT_LISTP(&new_thread->children);
  490. INIT_LIST_HEAD(new_thread, siblings);
  491. INIT_LISTP(&new_thread->exited_children);
  492. INIT_LIST_HEAD(new_thread, list);
  493. new_thread->in_vm = false;
  494. new_thread->parent = NULL;
  495. new_thread->dummy = NULL;
  496. new_thread->handle_map = NULL;
  497. new_thread->root = NULL;
  498. new_thread->cwd = NULL;
  499. new_thread->signal_logs = NULL;
  500. new_thread->robust_list = NULL;
  501. REF_SET(new_thread->ref_count, 0);
  502. for (int i = 0 ; i < NUM_SIGS ; i++)
  503. if (thread->signal_handles[i].action) {
  504. ptr_t soff = ADD_CP_OFFSET(sizeof(struct __kernel_sigaction));
  505. new_thread->signal_handles[i].action
  506. = (struct __kernel_sigaction *) (base + soff);
  507. memcpy(new_thread->signal_handles[i].action,
  508. thread->signal_handles[i].action,
  509. sizeof(struct __kernel_sigaction));
  510. }
  511. DO_CP_MEMBER(handle, thread, new_thread, exec);
  512. DO_CP_MEMBER(handle_map, thread, new_thread, handle_map);
  513. DO_CP_MEMBER(dentry, thread, new_thread, root);
  514. DO_CP_MEMBER(dentry, thread, new_thread, cwd);
  515. ADD_CP_FUNC_ENTRY(off);
  516. } else {
  517. new_thread = (struct shim_thread *) (base + off);
  518. }
  519. if (objp)
  520. *objp = (void *) new_thread;
  521. }
  522. END_CP_FUNC(thread)
  523. BEGIN_RS_FUNC(thread)
  524. {
  525. struct shim_thread * thread = (void *) (base + GET_CP_FUNC_ENTRY());
  526. CP_REBASE(thread->children);
  527. CP_REBASE(thread->siblings);
  528. CP_REBASE(thread->exited_children);
  529. CP_REBASE(thread->list);
  530. CP_REBASE(thread->exec);
  531. CP_REBASE(thread->handle_map);
  532. CP_REBASE(thread->root);
  533. CP_REBASE(thread->cwd);
  534. CP_REBASE(thread->signal_handles);
  535. create_lock(thread->lock);
  536. thread->scheduler_event = DkNotificationEventCreate(PAL_TRUE);
  537. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  538. thread->child_exit_event = DkNotificationEventCreate(PAL_FALSE);
  539. add_thread(thread);
  540. if (thread->exec)
  541. get_handle(thread->exec);
  542. if (thread->handle_map)
  543. get_handle_map(thread->handle_map);
  544. if (thread->root)
  545. get_dentry(thread->root);
  546. if (thread->cwd)
  547. get_dentry(thread->cwd);
  548. DEBUG_RS("tid=%d,tgid=%d,parent=%d,stack=%p,frameptr=%p,tcb=%p",
  549. thread->tid, thread->tgid,
  550. thread->parent ? thread->parent->tid : thread->tid,
  551. thread->stack, thread->frameptr, thread->tcb);
  552. }
  553. END_RS_FUNC(thread)
  554. BEGIN_CP_FUNC(running_thread)
  555. {
  556. assert(size == sizeof(struct shim_thread));
  557. struct shim_thread * thread = (struct shim_thread *) obj;
  558. struct shim_thread * new_thread = NULL;
  559. DO_CP(thread, thread, &new_thread);
  560. ADD_CP_FUNC_ENTRY((ptr_t) new_thread - base);
  561. if (!thread->user_tcb && thread->tcb) {
  562. ptr_t toff = ADD_CP_OFFSET(sizeof(__libc_tcb_t));
  563. new_thread->tcb = (void *) (base + toff);
  564. memcpy(new_thread->tcb, thread->tcb, sizeof(__libc_tcb_t));
  565. }
  566. }
  567. END_CP_FUNC(running_thread)
  568. int resume_wrapper (void * param)
  569. {
  570. struct shim_thread * thread = (struct shim_thread *) param;
  571. assert(thread);
  572. __libc_tcb_t * libc_tcb = (__libc_tcb_t *) thread->tcb;
  573. assert(libc_tcb);
  574. shim_tcb_t * tcb = &libc_tcb->shim_tcb;
  575. assert(tcb->context.sp);
  576. thread->in_vm = thread->is_alive = true;
  577. allocate_tls(libc_tcb, thread->user_tcb, thread);
  578. debug_setbuf(tcb, true);
  579. debug("set tcb to %p\n", libc_tcb);
  580. DkObjectsWaitAny(1, &thread_start_event, NO_TIMEOUT);
  581. restore_context(&tcb->context);
  582. return 0;
  583. }
  584. BEGIN_RS_FUNC(running_thread)
  585. {
  586. struct shim_thread * thread = (void *) (base + GET_CP_FUNC_ENTRY());
  587. struct shim_thread * cur_thread = get_cur_thread();
  588. thread->in_vm = true;
  589. if (!thread->user_tcb)
  590. CP_REBASE(thread->tcb);
  591. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  592. NUM_SIGS);
  593. if (cur_thread) {
  594. PAL_HANDLE handle = DkThreadCreate(resume_wrapper, thread, 0);
  595. if (!thread)
  596. return -PAL_ERRNO;
  597. thread->pal_handle = handle;
  598. } else {
  599. __libc_tcb_t * libc_tcb = (__libc_tcb_t *) thread->tcb;
  600. if (libc_tcb) {
  601. shim_tcb_t * tcb = &libc_tcb->shim_tcb;
  602. assert(tcb->context.sp);
  603. tcb->debug_buf = SHIM_GET_TLS()->debug_buf;
  604. allocate_tls(libc_tcb, thread->user_tcb, thread);
  605. debug_setprefix(tcb);
  606. debug("after resume, set tcb to %p\n", libc_tcb);
  607. } else {
  608. set_cur_thread(thread);
  609. }
  610. thread->in_vm = thread->is_alive = true;
  611. thread->pal_handle = PAL_CB(first_thread);
  612. }
  613. DEBUG_RS("tid=%d", thread->tid);
  614. }
  615. END_RS_FUNC(running_thread)
  616. BEGIN_CP_FUNC(all_running_threads)
  617. {
  618. struct shim_thread * thread;
  619. lock(thread_list_lock);
  620. listp_for_each_entry(thread, &thread_list, list) {
  621. if (!thread->in_vm || !thread->is_alive)
  622. continue;
  623. DO_CP(running_thread, thread, NULL);
  624. DO_CP(handle_map, thread->handle_map, NULL);
  625. }
  626. unlock(thread_list_lock);
  627. }
  628. END_CP_FUNC_NO_RS(all_running_threads)