shim_thread.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825
  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_defs.h>
  19. #include <shim_internal.h>
  20. #include <shim_thread.h>
  21. #include <shim_handle.h>
  22. #include <shim_vma.h>
  23. #include <shim_fs.h>
  24. #include <shim_checkpoint.h>
  25. #include <shim_utils.h>
  26. #include <pal.h>
  27. #include <list.h>
  28. #include <linux/signal.h>
  29. static IDTYPE tid_alloc_idx __attribute_migratable = 0;
  30. static LISTP_TYPE(shim_thread) thread_list = LISTP_INIT;
  31. DEFINE_LISTP(shim_simple_thread);
  32. static LISTP_TYPE(shim_simple_thread) simple_thread_list = LISTP_INIT;
  33. struct shim_lock thread_list_lock;
  34. static IDTYPE internal_tid_alloc_idx = INTERNAL_TID_BASE;
  35. PAL_HANDLE thread_start_event = NULL;
  36. //#define DEBUG_REF
  37. int init_thread (void)
  38. {
  39. create_lock(&thread_list_lock);
  40. struct shim_thread * cur_thread = get_cur_thread();
  41. if (cur_thread)
  42. return 0;
  43. if (!(cur_thread = get_new_thread(0)))
  44. return -ENOMEM;
  45. cur_thread->in_vm = cur_thread->is_alive = true;
  46. set_cur_thread(cur_thread);
  47. add_thread(cur_thread);
  48. cur_thread->pal_handle = PAL_CB(first_thread);
  49. return 0;
  50. }
  51. void dump_threads (void)
  52. {
  53. struct shim_thread * tmp;
  54. lock(&thread_list_lock);
  55. LISTP_FOR_EACH_ENTRY(tmp, &thread_list, list) {
  56. debug("thread %d, vmid = %d, pgid = %d, ppid = %d, tgid = %d, in_vm = %d\n",
  57. tmp->tid, tmp->vmid, tmp->pgid, tmp->ppid, tmp->tgid, tmp->in_vm);
  58. }
  59. unlock(&thread_list_lock);
  60. }
  61. static struct shim_thread* __lookup_thread(IDTYPE tid) {
  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. lock(&thread_list_lock);
  73. struct shim_thread* thread = __lookup_thread(tid);
  74. unlock(&thread_list_lock);
  75. return thread;
  76. }
  77. struct shim_thread * __get_cur_thread (void)
  78. {
  79. return shim_thread_self();
  80. }
  81. shim_tcb_t * __get_cur_tcb (void)
  82. {
  83. return shim_get_tcb();
  84. }
  85. IDTYPE get_pid (void)
  86. {
  87. IDTYPE idx;
  88. while (1) {
  89. IDTYPE old_idx = tid_alloc_idx;
  90. IDTYPE max = 0;
  91. idx = old_idx + 1;
  92. do {
  93. if ((idx = allocate_pid(idx, max)))
  94. break;
  95. tid_alloc_idx = idx;
  96. if (!idx) {
  97. if (max == old_idx)
  98. break;
  99. max = old_idx;
  100. }
  101. } while (idx != tid_alloc_idx);
  102. if (idx != tid_alloc_idx)
  103. break;
  104. if (ipc_pid_lease_send(NULL) < 0)
  105. return 0;
  106. }
  107. tid_alloc_idx = idx;
  108. return idx;
  109. }
  110. static IDTYPE get_internal_pid (void)
  111. {
  112. lock(&thread_list_lock);
  113. internal_tid_alloc_idx++;
  114. IDTYPE idx = internal_tid_alloc_idx;
  115. unlock(&thread_list_lock);
  116. assert(is_internal_tid(idx));
  117. return idx;
  118. }
  119. struct shim_thread * alloc_new_thread (void)
  120. {
  121. struct shim_thread * thread = calloc(1, sizeof(struct shim_thread));
  122. if (!thread)
  123. return NULL;
  124. REF_SET(thread->ref_count, 1);
  125. INIT_LISTP(&thread->children);
  126. INIT_LIST_HEAD(thread, siblings);
  127. INIT_LISTP(&thread->exited_children);
  128. INIT_LIST_HEAD(thread, list);
  129. /* default value as sigalt stack isn't specified yet */
  130. thread->signal_altstack.ss_flags = SS_DISABLE;
  131. return thread;
  132. }
  133. struct shim_thread * get_new_thread (IDTYPE new_tid)
  134. {
  135. if (!new_tid) {
  136. new_tid = get_pid();
  137. assert(new_tid);
  138. }
  139. struct shim_thread * thread = alloc_new_thread();
  140. if (!thread)
  141. return NULL;
  142. struct shim_thread * cur_thread = get_cur_thread();
  143. thread->tid = new_tid;
  144. if (cur_thread) {
  145. /* The newly created thread will be in the same thread group
  146. (process group as well) with its parent */
  147. thread->pgid = cur_thread->pgid;
  148. thread->ppid = cur_thread->tgid;
  149. thread->tgid = cur_thread->tgid;
  150. thread->uid = cur_thread->uid;
  151. thread->gid = cur_thread->gid;
  152. thread->euid = cur_thread->euid;
  153. thread->egid = cur_thread->egid;
  154. thread->parent = cur_thread;
  155. thread->stack = cur_thread->stack;
  156. thread->stack_top = cur_thread->stack_top;
  157. thread->stack_red = cur_thread->stack_red;
  158. thread->cwd = cur_thread->cwd;
  159. thread->root = cur_thread->root;
  160. thread->umask = cur_thread->umask;
  161. thread->exec = cur_thread->exec;
  162. get_handle(cur_thread->exec);
  163. for (int i = 0 ; i < NUM_SIGS ; i++) {
  164. if (!cur_thread->signal_handles[i].action)
  165. continue;
  166. thread->signal_handles[i].action =
  167. malloc_copy(cur_thread->signal_handles[i].action,
  168. sizeof(struct shim_signal_handle));
  169. }
  170. memcpy(&thread->signal_mask, &cur_thread->signal_mask,
  171. sizeof(sigset_t));
  172. get_dentry(cur_thread->cwd);
  173. get_dentry(cur_thread->root);
  174. struct shim_handle_map * map = get_cur_handle_map(cur_thread);
  175. assert(map);
  176. set_handle_map(thread, map);
  177. } else {
  178. /* default pid and pgid equals to tid */
  179. thread->ppid = thread->pgid = thread->tgid = new_tid;
  180. /* This case should fall back to the global root of the file system.
  181. */
  182. path_lookupat(NULL, "/", 0, &thread->root, NULL);
  183. char dir_cfg[CONFIG_MAX];
  184. if (root_config &&
  185. get_config(root_config, "fs.start_dir", dir_cfg, sizeof(dir_cfg)) > 0) {
  186. path_lookupat(NULL, dir_cfg, 0, &thread->cwd, NULL);
  187. } else if (thread->root) {
  188. get_dentry(thread->root);
  189. thread->cwd = thread->root;
  190. }
  191. }
  192. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  193. NUM_SIGS);
  194. thread->vmid = cur_process.vmid;
  195. create_lock(&thread->lock);
  196. thread->scheduler_event = DkNotificationEventCreate(PAL_TRUE);
  197. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  198. thread->child_exit_event = DkNotificationEventCreate(PAL_FALSE);
  199. return thread;
  200. }
  201. struct shim_thread * get_new_internal_thread (void)
  202. {
  203. IDTYPE new_tid = get_internal_pid();
  204. assert(new_tid);
  205. struct shim_thread * thread = alloc_new_thread();
  206. if (!thread)
  207. return NULL;
  208. thread->vmid = cur_process.vmid;
  209. thread->tid = new_tid;
  210. thread->in_vm = thread->is_alive = true;
  211. create_lock(&thread->lock);
  212. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  213. return thread;
  214. }
  215. struct shim_simple_thread * __lookup_simple_thread (IDTYPE tid)
  216. {
  217. struct shim_simple_thread * tmp;
  218. LISTP_FOR_EACH_ENTRY(tmp, &simple_thread_list, list) {
  219. if (tmp->tid == tid) {
  220. get_simple_thread(tmp);
  221. return tmp;
  222. }
  223. }
  224. return NULL;
  225. }
  226. struct shim_simple_thread * lookup_simple_thread (IDTYPE tid)
  227. {
  228. lock(&thread_list_lock);
  229. struct shim_simple_thread * thread = __lookup_simple_thread(tid);
  230. unlock(&thread_list_lock);
  231. return thread;
  232. }
  233. struct shim_simple_thread * get_new_simple_thread (void)
  234. {
  235. struct shim_simple_thread * thread =
  236. malloc(sizeof(struct shim_simple_thread));
  237. if (!thread)
  238. return NULL;
  239. memset(thread, 0, sizeof(struct shim_simple_thread));
  240. INIT_LIST_HEAD(thread, list);
  241. create_lock(&thread->lock);
  242. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  243. return thread;
  244. }
  245. void get_thread (struct shim_thread * thread)
  246. {
  247. #ifdef DEBUG_REF
  248. int ref_count = REF_INC(thread->ref_count);
  249. debug("get_thread %p(%d) (ref_count = %d)\n", thread, thread->tid,
  250. ref_count);
  251. #else
  252. REF_INC(thread->ref_count);
  253. #endif
  254. }
  255. void put_thread (struct shim_thread * thread)
  256. {
  257. int ref_count = REF_DEC(thread->ref_count);
  258. #ifdef DEBUG_REF
  259. debug("put_thread %p(%d) (ref_count = %d)\n", thread, thread->tid,
  260. ref_count);
  261. #endif
  262. if (!ref_count) {
  263. if (thread->exec)
  264. put_handle(thread->exec);
  265. if (!is_internal(thread))
  266. release_pid(thread->tid);
  267. if (thread->pal_handle &&
  268. thread->pal_handle != PAL_CB(first_thread))
  269. DkObjectClose(thread->pal_handle);
  270. if (thread->scheduler_event)
  271. DkObjectClose(thread->scheduler_event);
  272. if (thread->exit_event)
  273. DkObjectClose(thread->exit_event);
  274. if (thread->child_exit_event)
  275. DkObjectClose(thread->child_exit_event);
  276. destroy_lock(&thread->lock);
  277. free(thread->signal_logs);
  278. free(thread);
  279. }
  280. }
  281. void get_simple_thread (struct shim_simple_thread * thread)
  282. {
  283. REF_INC(thread->ref_count);
  284. }
  285. void put_simple_thread (struct shim_simple_thread * thread)
  286. {
  287. int ref_count = REF_DEC(thread->ref_count);
  288. if (!ref_count) {
  289. /* Simple threads always live on the simple thread list */
  290. LISTP_DEL(thread, &simple_thread_list, list);
  291. if (thread->exit_event)
  292. DkObjectClose(thread->exit_event);
  293. destroy_lock(&thread->lock);
  294. free(thread);
  295. }
  296. }
  297. void set_as_child (struct shim_thread * parent,
  298. struct shim_thread * child)
  299. {
  300. if (!parent)
  301. parent = get_cur_thread();
  302. get_thread(parent);
  303. get_thread(child);
  304. lock(&child->lock);
  305. child->ppid = parent->tid;
  306. child->parent = parent;
  307. lock(&parent->lock);
  308. LISTP_ADD_TAIL(child, &parent->children, siblings);
  309. unlock(&parent->lock);
  310. unlock(&child->lock);
  311. }
  312. void add_thread (struct shim_thread * thread)
  313. {
  314. if (is_internal(thread) || !LIST_EMPTY(thread, list))
  315. return;
  316. struct shim_thread * tmp, * prev = NULL;
  317. lock(&thread_list_lock);
  318. /* keep it sorted */
  319. LISTP_FOR_EACH_ENTRY_REVERSE(tmp, &thread_list, list) {
  320. if (tmp->tid == thread->tid) {
  321. unlock(&thread_list_lock);
  322. return;
  323. }
  324. if (tmp->tid < thread->tid) {
  325. prev = tmp;
  326. break;
  327. }
  328. }
  329. get_thread(thread);
  330. LISTP_ADD_AFTER(thread, prev, &thread_list, list);
  331. unlock(&thread_list_lock);
  332. }
  333. void del_thread (struct shim_thread * thread)
  334. {
  335. debug("del_thread(%p, %d, %ld)\n", thread, thread ? (int) thread->tid : -1,
  336. atomic_read(&thread->ref_count));
  337. if (is_internal(thread) || LIST_EMPTY(thread, list)) {
  338. debug("del_thread: internal\n");
  339. return;
  340. }
  341. lock(&thread_list_lock);
  342. /* thread->list goes on the thread_list */
  343. LISTP_DEL_INIT(thread, &thread_list, list);
  344. unlock(&thread_list_lock);
  345. put_thread(thread);
  346. }
  347. void add_simple_thread (struct shim_simple_thread * thread)
  348. {
  349. if (!LIST_EMPTY(thread, list))
  350. return;
  351. struct shim_simple_thread * tmp, * prev = NULL;
  352. lock(&thread_list_lock);
  353. /* keep it sorted */
  354. LISTP_FOR_EACH_ENTRY_REVERSE(tmp, &simple_thread_list, list) {
  355. if (tmp->tid == thread->tid) {
  356. unlock(&thread_list_lock);
  357. return;
  358. }
  359. if (tmp->tid < thread->tid) {
  360. prev = tmp;
  361. break;
  362. }
  363. }
  364. get_simple_thread(thread);
  365. LISTP_ADD_AFTER(thread, prev, &simple_thread_list, list);
  366. unlock(&thread_list_lock);
  367. }
  368. void del_simple_thread (struct shim_simple_thread * thread)
  369. {
  370. if (LIST_EMPTY(thread, list))
  371. return;
  372. lock(&thread_list_lock);
  373. LISTP_DEL_INIT(thread, &simple_thread_list, list);
  374. unlock(&thread_list_lock);
  375. put_simple_thread(thread);
  376. }
  377. int check_last_thread (struct shim_thread * self)
  378. {
  379. struct shim_thread * tmp;
  380. lock(&thread_list_lock);
  381. /* find out if there is any thread that is
  382. 1) no current thread 2) in current vm
  383. 3) still alive */
  384. LISTP_FOR_EACH_ENTRY(tmp, &thread_list, list) {
  385. if (tmp->tid &&
  386. (!self || tmp->tid != self->tid) && tmp->in_vm && tmp->is_alive) {
  387. debug("check_last_thread: thread %d is alive\n", tmp->tid);
  388. unlock(&thread_list_lock);
  389. return tmp->tid;
  390. }
  391. }
  392. debug("this is the only thread %d\n", self->tid);
  393. unlock(&thread_list_lock);
  394. return 0;
  395. }
  396. int walk_thread_list (int (*callback) (struct shim_thread *, void *, bool *),
  397. void * arg)
  398. {
  399. struct shim_thread * tmp, * n;
  400. bool srched = false;
  401. int ret;
  402. IDTYPE min_tid = 0;
  403. relock:
  404. lock(&thread_list_lock);
  405. debug("walk_thread_list(callback=%p)\n", callback);
  406. LISTP_FOR_EACH_ENTRY_SAFE(tmp, n, &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. int walk_simple_thread_list (int (*callback) (struct shim_simple_thread *,
  431. void *, bool *),
  432. void * arg)
  433. {
  434. struct shim_simple_thread * tmp, * n;
  435. bool srched = false;
  436. int ret;
  437. IDTYPE min_tid = 0;
  438. relock:
  439. lock(&thread_list_lock);
  440. LISTP_FOR_EACH_ENTRY_SAFE(tmp, n, &simple_thread_list, list) {
  441. if (tmp->tid <= min_tid)
  442. continue;
  443. bool unlocked = false;
  444. ret = (*callback) (tmp, arg, &unlocked);
  445. if (ret < 0 && ret != -ESRCH) {
  446. if (unlocked)
  447. goto out;
  448. else
  449. goto out_locked;
  450. }
  451. if (ret > 0)
  452. srched = true;
  453. if (unlocked) {
  454. min_tid = tmp->tid;
  455. goto relock;
  456. }
  457. }
  458. ret = srched ? 0 : -ESRCH;
  459. out_locked:
  460. unlock(&thread_list_lock);
  461. out:
  462. return ret;
  463. }
  464. #ifndef ALIAS_VFORK_AS_FORK
  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. #endif
  492. BEGIN_CP_FUNC(thread)
  493. {
  494. __UNUSED(size);
  495. assert(size == sizeof(struct shim_thread));
  496. struct shim_thread * thread = (struct shim_thread *) obj;
  497. struct shim_thread * new_thread = NULL;
  498. ptr_t off = GET_FROM_CP_MAP(obj);
  499. if (!off) {
  500. off = ADD_CP_OFFSET(sizeof(struct shim_thread));
  501. ADD_TO_CP_MAP(obj, off);
  502. new_thread = (struct shim_thread *) (base + off);
  503. memcpy(new_thread, thread, sizeof(struct shim_thread));
  504. INIT_LISTP(&new_thread->children);
  505. INIT_LIST_HEAD(new_thread, siblings);
  506. INIT_LISTP(&new_thread->exited_children);
  507. INIT_LIST_HEAD(new_thread, list);
  508. new_thread->in_vm = false;
  509. new_thread->parent = NULL;
  510. #ifndef ALIAS_VFORK_AS_FORK
  511. new_thread->dummy = NULL;
  512. #endif
  513. new_thread->handle_map = NULL;
  514. new_thread->root = NULL;
  515. new_thread->cwd = NULL;
  516. new_thread->signal_logs = NULL;
  517. new_thread->robust_list = NULL;
  518. REF_SET(new_thread->ref_count, 0);
  519. for (int i = 0 ; i < NUM_SIGS ; i++)
  520. if (thread->signal_handles[i].action) {
  521. ptr_t soff = ADD_CP_OFFSET(sizeof(struct __kernel_sigaction));
  522. new_thread->signal_handles[i].action
  523. = (struct __kernel_sigaction *) (base + soff);
  524. memcpy(new_thread->signal_handles[i].action,
  525. thread->signal_handles[i].action,
  526. sizeof(struct __kernel_sigaction));
  527. }
  528. DO_CP_MEMBER(handle, thread, new_thread, exec);
  529. DO_CP_MEMBER(handle_map, thread, new_thread, handle_map);
  530. DO_CP_MEMBER(dentry, thread, new_thread, root);
  531. DO_CP_MEMBER(dentry, thread, new_thread, cwd);
  532. ADD_CP_FUNC_ENTRY(off);
  533. } else {
  534. new_thread = (struct shim_thread *) (base + off);
  535. }
  536. if (objp)
  537. *objp = (void *) new_thread;
  538. }
  539. END_CP_FUNC(thread)
  540. BEGIN_RS_FUNC(thread)
  541. {
  542. struct shim_thread * thread = (void *) (base + GET_CP_FUNC_ENTRY());
  543. __UNUSED(offset);
  544. CP_REBASE(thread->children);
  545. CP_REBASE(thread->siblings);
  546. CP_REBASE(thread->exited_children);
  547. CP_REBASE(thread->list);
  548. CP_REBASE(thread->exec);
  549. CP_REBASE(thread->handle_map);
  550. CP_REBASE(thread->root);
  551. CP_REBASE(thread->cwd);
  552. CP_REBASE(thread->signal_handles);
  553. create_lock(&thread->lock);
  554. thread->scheduler_event = DkNotificationEventCreate(PAL_TRUE);
  555. thread->exit_event = DkNotificationEventCreate(PAL_FALSE);
  556. thread->child_exit_event = DkNotificationEventCreate(PAL_FALSE);
  557. add_thread(thread);
  558. if (thread->exec)
  559. get_handle(thread->exec);
  560. if (thread->handle_map)
  561. get_handle_map(thread->handle_map);
  562. if (thread->root)
  563. get_dentry(thread->root);
  564. if (thread->cwd)
  565. get_dentry(thread->cwd);
  566. DEBUG_RS("tid=%d,tgid=%d,parent=%d,stack=%p,frameptr=%p,tcb=%p,shim_tcb=%p",
  567. thread->tid, thread->tgid,
  568. thread->parent ? thread->parent->tid : thread->tid,
  569. thread->stack, thread->frameptr, thread->tcb, thread->shim_tcb);
  570. }
  571. END_RS_FUNC(thread)
  572. BEGIN_CP_FUNC(running_thread)
  573. {
  574. __UNUSED(size);
  575. __UNUSED(objp);
  576. assert(size == sizeof(struct shim_thread));
  577. struct shim_thread * thread = (struct shim_thread *) obj;
  578. struct shim_thread * new_thread = NULL;
  579. DO_CP(thread, thread, &new_thread);
  580. ADD_CP_FUNC_ENTRY((ptr_t) new_thread - base);
  581. if (thread->shim_tcb) {
  582. ptr_t toff = ADD_CP_OFFSET(sizeof(shim_tcb_t));
  583. new_thread->shim_tcb = (void *)(base + toff);
  584. memcpy(new_thread->shim_tcb, thread->shim_tcb, sizeof(shim_tcb_t));
  585. }
  586. }
  587. END_CP_FUNC(running_thread)
  588. static int resume_wrapper (void * param)
  589. {
  590. struct shim_thread * thread = (struct shim_thread *) param;
  591. assert(thread);
  592. unsigned long fs_base = thread->fs_base;
  593. assert(fs_base);
  594. shim_tcb_t * tcb = thread->shim_tcb;
  595. assert(tcb->context.regs && tcb->context.regs->rsp);
  596. thread->in_vm = thread->is_alive = true;
  597. init_fs_base(fs_base, thread);
  598. debug_setbuf(tcb, false);
  599. debug("set fs_base to 0x%lx\n", fs_base);
  600. object_wait_with_retry(thread_start_event);
  601. restore_context(&tcb->context);
  602. return 0;
  603. }
  604. BEGIN_RS_FUNC(running_thread)
  605. {
  606. __UNUSED(offset);
  607. struct shim_thread * thread = (void *) (base + GET_CP_FUNC_ENTRY());
  608. struct shim_thread * cur_thread = get_cur_thread();
  609. thread->in_vm = true;
  610. thread->vmid = cur_process.vmid;
  611. if (thread->shim_tcb)
  612. CP_REBASE(thread->shim_tcb);
  613. if (thread->set_child_tid) {
  614. /* CLONE_CHILD_SETTID */
  615. *thread->set_child_tid = thread->tid;
  616. thread->set_child_tid = NULL;
  617. }
  618. thread->signal_logs = malloc(sizeof(struct shim_signal_log) *
  619. NUM_SIGS);
  620. if (cur_thread) {
  621. PAL_HANDLE handle = DkThreadCreate(resume_wrapper, thread);
  622. if (!thread)
  623. return -PAL_ERRNO;
  624. thread->pal_handle = handle;
  625. } else {
  626. if (thread->shim_tcb) {
  627. memcpy(shim_get_tcb(), thread->shim_tcb, sizeof(shim_tcb_t));
  628. thread->shim_tcb = shim_get_tcb();
  629. }
  630. debug_setbuf(thread->shim_tcb, false);
  631. unsigned long fs_base = thread->fs_base;
  632. if (fs_base) {
  633. shim_tcb_t * tcb = thread->shim_tcb;
  634. assert(tcb->context.regs && tcb->context.regs->rsp);
  635. tcb->debug_buf = shim_get_tcb()->debug_buf;
  636. init_fs_base(fs_base, thread);
  637. /* Temporarily disable preemption until the thread resumes. */
  638. __disable_preempt(tcb);
  639. debug_setprefix(tcb);
  640. debug("after resume, set tcb to 0x%lx\n", fs_base);
  641. } else {
  642. /*
  643. * In execve case, the following holds:
  644. * stack = NULL
  645. * stack_top = NULL
  646. * frameptr = NULL
  647. * tcb = NULL
  648. * shim_tcb = NULL
  649. * in_vm = false
  650. */
  651. thread->shim_tcb = shim_get_tcb();
  652. init_tcb(thread->shim_tcb);
  653. set_cur_thread(thread);
  654. }
  655. thread->in_vm = thread->is_alive = true;
  656. thread->pal_handle = PAL_CB(first_thread);
  657. }
  658. DEBUG_RS("tid=%d", thread->tid);
  659. }
  660. END_RS_FUNC(running_thread)
  661. BEGIN_CP_FUNC(all_running_threads)
  662. {
  663. __UNUSED(obj);
  664. __UNUSED(size);
  665. __UNUSED(objp);
  666. struct shim_thread * thread;
  667. lock(&thread_list_lock);
  668. LISTP_FOR_EACH_ENTRY(thread, &thread_list, list) {
  669. if (!thread->in_vm || !thread->is_alive)
  670. continue;
  671. DO_CP(running_thread, thread, NULL);
  672. DO_CP(handle_map, thread->handle_map, NULL);
  673. }
  674. unlock(&thread_list_lock);
  675. }
  676. END_CP_FUNC_NO_RS(all_running_threads)