shim_clone.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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_clone.c
  15. *
  16. * Implementation of system call "clone". (using "clone" as "fork" is not
  17. * implemented yet.)
  18. */
  19. #include <shim_types.h>
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <shim_utils.h>
  24. #include <shim_checkpoint.h>
  25. #include <shim_profile.h>
  26. #include <pal.h>
  27. #include <pal_error.h>
  28. #include <errno.h>
  29. #include <sys/syscall.h>
  30. #include <sys/mman.h>
  31. #include <linux/sched.h>
  32. #include <asm/prctl.h>
  33. void __attribute__((weak)) syscall_wrapper_after_syscalldb(void)
  34. {
  35. /*
  36. * workaround for linking.
  37. * syscalldb.S is excluded for libsysdb_debug.so so it fails to link
  38. * due to missing syscall_wrapper_after_syscalldb.
  39. */
  40. }
  41. /*
  42. * See syscall_wrapper @ syscalldb.S and illegal_upcall() @ shim_signal.c
  43. * for details.
  44. * child thread can _not_ use parent stack. So return right after syscall
  45. * instruction as if syscall_wrapper is executed.
  46. */
  47. static void fixup_child_context(struct shim_regs * regs)
  48. {
  49. if (regs->rip == (unsigned long)&syscall_wrapper_after_syscalldb) {
  50. /*
  51. * we don't need to emulate stack pointer change because %rsp is
  52. * initialized to new child user stack passed to clone() system call.
  53. * See the caller of fixup_child_context().
  54. */
  55. /* regs->rsp += RED_ZONE_SIZE; */
  56. regs->rflags = regs->r11;
  57. regs->rip = regs->rcx;
  58. }
  59. }
  60. /* from **sysdeps/unix/sysv/linux/x86_64/clone.S:
  61. The userland implementation is:
  62. int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
  63. the kernel entry is:
  64. int clone (long flags, void *child_stack).
  65. The parameters are passed in register and on the stack from userland:
  66. rdi: fn
  67. rsi: child_stack
  68. rdx: flags
  69. rcx: arg
  70. r8d: TID field in parent
  71. r9d: thread pointer
  72. %esp+8: TID field in child
  73. The kernel expects:
  74. rax: system call number
  75. rdi: flags
  76. rsi: child_stack
  77. rdx: TID field in parent
  78. r10: TID field in child
  79. r8: thread pointer
  80. */
  81. /*
  82. * This Function is a wrapper around the user provided function.
  83. * Code flow for clone is as follows -
  84. * 1) User application allocates stack for child process and
  85. * calls clone. The clone code sets up the user function
  86. * address and the argument address on the child stack.
  87. * 2)we Hijack the clone call and control flows to shim_clone
  88. * 3)In Shim Clone we just call the DK Api to create a thread by providing a
  89. * wrapper function around the user provided function
  90. * 4)PAL layer allocates a stack and then invokes the clone syscall
  91. * 5)PAL runs thread_init function on PAL allocated Stack
  92. * 6)thread_init calls our wrapper and gives the user provided stack
  93. * address.
  94. * 7.In the wrapper function ,we just do the stack switch to user
  95. * Provided stack and execute the user Provided function.
  96. */
  97. static int clone_implementation_wrapper(struct shim_clone_args * arg)
  98. {
  99. //The child thread created by PAL is now running on the
  100. //PAL allocated stack. We need to switch the stack to use
  101. //the user provided stack.
  102. /* We acquired ownership of arg->thread from the caller, hence there is
  103. * no need to call get_thread. */
  104. struct shim_thread* my_thread = arg->thread;
  105. assert(my_thread);
  106. shim_tcb_init();
  107. set_cur_thread(my_thread);
  108. update_fs_base(arg->fs_base);
  109. shim_tcb_t * tcb = my_thread->shim_tcb;
  110. /* only now we can call LibOS/PAL functions because they require a set-up TCB;
  111. * do not move the below functions before shim_tcb_init/set_cur_thread()! */
  112. object_wait_with_retry(arg->create_event);
  113. DkObjectClose(arg->create_event);
  114. __disable_preempt(tcb); // Temporarily disable preemption, because the preemption
  115. // will be re-enabled when the thread starts.
  116. debug_setbuf(tcb, true);
  117. debug("set fs_base to 0x%lx\n", tcb->context.fs_base);
  118. struct shim_regs regs = *arg->parent->shim_tcb->context.regs;
  119. if (my_thread->set_child_tid) {
  120. *(my_thread->set_child_tid) = my_thread->tid;
  121. my_thread->set_child_tid = NULL;
  122. }
  123. void * stack = arg->stack;
  124. struct shim_vma_val vma;
  125. lookup_vma(ALLOC_ALIGN_DOWN_PTR(stack), &vma);
  126. my_thread->stack_top = vma.addr + vma.length;
  127. my_thread->stack_red = my_thread->stack = vma.addr;
  128. /* until now we're not ready to be exposed to other thread */
  129. add_thread(my_thread);
  130. set_as_child(arg->parent, my_thread);
  131. /* Don't signal the initialize event until we are actually init-ed */
  132. DkEventSet(arg->initialize_event);
  133. /***** From here down, we are switching to the user-provided stack ****/
  134. //user_stack_addr[0] ==> user provided function address
  135. //user_stack_addr[1] ==> arguments to user provided function.
  136. debug("child swapping stack to %p return 0x%lx: %d\n",
  137. stack, regs.rip, my_thread->tid);
  138. tcb->context.regs = &regs;
  139. fixup_child_context(tcb->context.regs);
  140. tcb->context.regs->rsp = (unsigned long)stack;
  141. put_thread(my_thread);
  142. restore_context(&tcb->context);
  143. return 0;
  144. }
  145. int migrate_fork (struct shim_cp_store * cpstore,
  146. struct shim_thread * thread,
  147. struct shim_process * process, va_list ap);
  148. /* long int __arg0 - flags
  149. * long int __arg1 - 16 bytes ( 2 words ) offset into the child stack allocated
  150. * by the parent */
  151. int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
  152. int * child_tidptr, void * tls)
  153. {
  154. //The Clone Implementation in glibc has setup the child's stack
  155. //with the function pointer and the argument to the funciton.
  156. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  157. struct shim_thread * self = get_cur_thread();
  158. assert(self);
  159. int * set_parent_tid = NULL;
  160. int ret = 0;
  161. /* special case for vfork. some runtime uses clone() for vfork */
  162. if (flags == (CLONE_VFORK | CLONE_VM | SIGCHLD) &&
  163. user_stack_addr == NULL && parent_tidptr == NULL &&
  164. child_tidptr == NULL && tls == NULL) {
  165. return shim_do_vfork();
  166. }
  167. const int supported_flags =
  168. CLONE_CHILD_CLEARTID |
  169. CLONE_CHILD_SETTID |
  170. CLONE_DETACHED | // Unused
  171. CLONE_FILES |
  172. CLONE_FS |
  173. CLONE_PARENT_SETTID |
  174. #ifdef CLONE_PIDFD
  175. CLONE_PIDFD |
  176. #endif
  177. CLONE_PTRACE | // Unused
  178. CLONE_SETTLS |
  179. CLONE_SIGHAND |
  180. CLONE_SYSVSEM |
  181. CLONE_THREAD |
  182. CLONE_VM |
  183. CSIGNAL;
  184. const int unsupported_flags = ~supported_flags;
  185. if (flags & unsupported_flags) {
  186. debug("clone called with unsupported flags argument.\n");
  187. return -EINVAL;
  188. }
  189. /* Explicitly disallow CLONE_VM without either of CLONE_THREAD or CLONE_VFORK on Graphene. While
  190. * Linux allows passing CLONE_VM without either of CLONE_THREAD or CLONE_VFORK, this usage is
  191. * exotic enough to not attempt a faithful emulation in Graphene. */
  192. if (flags & CLONE_VM)
  193. if (!((flags & CLONE_THREAD) || (flags & CLONE_VFORK))) {
  194. debug("CLONE_VM without either CLONE_THREAD or CLONE_VFORK is unsupported\n");
  195. return -EINVAL;
  196. }
  197. if ((flags & CLONE_THREAD) && !(flags & CLONE_SIGHAND))
  198. return -EINVAL;
  199. if ((flags & CLONE_SIGHAND) && !(flags & CLONE_VM))
  200. return -EINVAL;
  201. /* The caller may not have set the following three flags, but Graphene treats them as set to
  202. * simplify the implementation of clone. Only print a warning since returning an explicit error
  203. * code breaks many applications. */
  204. if (!(flags & CLONE_FS))
  205. debug("clone without CLONE_FS is not yet implemented\n");
  206. if (!(flags & CLONE_SIGHAND))
  207. debug("clone without CLONE_SIGHAND is not yet implemented\n");
  208. if (!(flags & CLONE_SYSVSEM))
  209. debug("clone without CLONE_SYSVSEM is not yet implemented\n");
  210. #ifdef CLONE_PIDFD
  211. if (flags & CLONE_PIDFD) {
  212. if (flags & (CLONE_DETACHED | CLONE_PARENT_SETTID | CLONE_THREAD))
  213. return -EINVAL;
  214. if (test_user_memory(parent_tidptr, sizeof(*parent_tidptr), false))
  215. return -EFAULT;
  216. if (*parent_tidptr != 0)
  217. return -EINVAL;
  218. }
  219. #endif
  220. if (flags & CLONE_PARENT_SETTID) {
  221. if (!parent_tidptr)
  222. return -EINVAL;
  223. set_parent_tid = parent_tidptr;
  224. }
  225. struct shim_thread * thread = get_new_thread(0);
  226. if (!thread) {
  227. ret = -ENOMEM;
  228. goto failed;
  229. }
  230. IDTYPE tid = thread->tid;
  231. if (flags & CLONE_CHILD_SETTID) {
  232. if (!child_tidptr) {
  233. ret = -EINVAL;
  234. goto failed;
  235. }
  236. thread->set_child_tid = child_tidptr;
  237. }
  238. if (flags & CLONE_CHILD_CLEARTID)
  239. thread->clear_child_tid = child_tidptr;
  240. unsigned long fs_base = 0;
  241. if (flags & CLONE_SETTLS) {
  242. if (!tls) {
  243. ret = -EINVAL;
  244. goto failed;
  245. }
  246. fs_base = (unsigned long)tls;
  247. }
  248. if (!(flags & CLONE_THREAD))
  249. thread->tgid = thread->tid;
  250. struct shim_handle_map * handle_map = get_cur_handle_map(self);
  251. if (flags & CLONE_FILES) {
  252. set_handle_map(thread, handle_map);
  253. } else {
  254. /* if CLONE_FILES is not given, the new thread should receive
  255. a copy of current descriptor table */
  256. struct shim_handle_map * new_map = NULL;
  257. get_handle_map(handle_map);
  258. dup_handle_map(&new_map, handle_map);
  259. set_handle_map(thread, new_map);
  260. put_handle_map(handle_map);
  261. }
  262. if (!(flags & CLONE_VM)) {
  263. void * parent_stack = NULL;
  264. if (!fs_base) {
  265. fs_base = self->shim_tcb->context.fs_base;
  266. }
  267. /* associate cpu context to new forking thread for migration */
  268. shim_tcb_t shim_tcb;
  269. memcpy(&shim_tcb, self->shim_tcb, sizeof(shim_tcb_t));
  270. shim_tcb.context.fs_base = fs_base;
  271. thread->shim_tcb = &shim_tcb;
  272. if (user_stack_addr) {
  273. struct shim_vma_val vma;
  274. lookup_vma(ALLOC_ALIGN_DOWN_PTR(user_stack_addr), &vma);
  275. thread->stack_top = vma.addr + vma.length;
  276. thread->stack_red = thread->stack = vma.addr;
  277. parent_stack = (void *)self->shim_tcb->context.regs->rsp;
  278. thread->shim_tcb->context.regs->rsp = (unsigned long)user_stack_addr;
  279. }
  280. thread->is_alive = true;
  281. thread->in_vm = false;
  282. add_thread(thread);
  283. set_as_child(self, thread);
  284. ret = do_migrate_process(&migrate_fork, NULL, NULL, thread);
  285. thread->shim_tcb = NULL; /* cpu context of forked thread isn't
  286. * needed any more */
  287. if (parent_stack)
  288. self->shim_tcb->context.regs->rsp = (unsigned long)parent_stack;
  289. if (ret < 0)
  290. goto failed;
  291. lock(&thread->lock);
  292. handle_map = thread->handle_map;
  293. thread->handle_map = NULL;
  294. unlock(&thread->lock);
  295. if (handle_map)
  296. put_handle_map(handle_map);
  297. if (set_parent_tid)
  298. *set_parent_tid = tid;
  299. put_thread(thread);
  300. return tid;
  301. }
  302. enable_locking();
  303. struct shim_clone_args new_args;
  304. memset(&new_args, 0, sizeof(new_args));
  305. new_args.create_event = DkNotificationEventCreate(PAL_FALSE);
  306. if (!new_args.create_event) {
  307. ret = -PAL_ERRNO;
  308. goto clone_thread_failed;
  309. }
  310. new_args.initialize_event = DkNotificationEventCreate(PAL_FALSE);
  311. if (!new_args.initialize_event) {
  312. ret = -PAL_ERRNO;
  313. goto clone_thread_failed;
  314. }
  315. /* Increasing refcount due to copy below. Passing ownership of the new copy
  316. * of this pointer to the new thread (receiver of new_args). */
  317. get_thread(thread);
  318. new_args.thread = thread;
  319. new_args.parent = self;
  320. new_args.stack = user_stack_addr;
  321. new_args.fs_base = fs_base;
  322. // Invoke DkThreadCreate to spawn off a child process using the actual
  323. // "clone" system call. DkThreadCreate allocates a stack for the child
  324. // and then runs the given function on that stack However, we want our
  325. // child to run on the Parent allocated stack , so once the DkThreadCreate
  326. // returns .The parent comes back here - however, the child is Happily
  327. // running the function we gave to DkThreadCreate.
  328. PAL_HANDLE pal_handle = thread_create(clone_implementation_wrapper,
  329. &new_args);
  330. if (!pal_handle) {
  331. ret = -PAL_ERRNO;
  332. put_thread(new_args.thread);
  333. goto clone_thread_failed;
  334. }
  335. thread->pal_handle = pal_handle;
  336. thread->in_vm = thread->is_alive = true;
  337. if (set_parent_tid)
  338. *set_parent_tid = tid;
  339. DkEventSet(new_args.create_event);
  340. object_wait_with_retry(new_args.initialize_event);
  341. DkObjectClose(new_args.initialize_event);
  342. put_thread(thread);
  343. return tid;
  344. clone_thread_failed:
  345. if (new_args.create_event)
  346. DkObjectClose(new_args.create_event);
  347. if (new_args.initialize_event)
  348. DkObjectClose(new_args.initialize_event);
  349. failed:
  350. if (thread)
  351. put_thread(thread);
  352. return ret;
  353. }