shim_thread.c 22 KB

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