shim_clone.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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. /* glibc needs space offset by fs. In the absence of a good way to predict
  98. * how big the struct pthread will be (defined in nptl/descr.h),
  99. * let's just define a value that over-shoots it.
  100. */
  101. #define PTHREAD_PADDING 2048
  102. static int clone_implementation_wrapper(struct clone_args * arg)
  103. {
  104. //The child thread created by PAL is now running on the
  105. //PAL allocated stack. We need to switch the stack to use
  106. //the user provided stack.
  107. /* We acquired ownership of arg->thread from the caller, hence there is
  108. * no need to call get_thread. */
  109. struct shim_thread* my_thread = arg->thread;
  110. assert(my_thread);
  111. init_fs_base(arg->fs_base, my_thread);
  112. shim_tcb_t * tcb = my_thread->shim_tcb;
  113. /* only now we can call LibOS/PAL functions because they require a set-up TCB;
  114. * do not move the below functions before init_fs_base()! */
  115. object_wait_with_retry(arg->create_event);
  116. DkObjectClose(arg->create_event);
  117. __disable_preempt(tcb); // Temporarily disable preemption, because the preemption
  118. // will be re-enabled when the thread starts.
  119. debug_setbuf(tcb, true);
  120. debug("set fs_base to 0x%lx\n", tcb->context.fs_base);
  121. struct shim_regs regs = *arg->parent->shim_tcb->context.regs;
  122. if (my_thread->set_child_tid) {
  123. *(my_thread->set_child_tid) = my_thread->tid;
  124. my_thread->set_child_tid = NULL;
  125. }
  126. void * stack = arg->stack;
  127. struct shim_vma_val vma;
  128. lookup_vma(ALLOC_ALIGN_DOWN_PTR(stack), &vma);
  129. my_thread->stack_top = vma.addr + vma.length;
  130. my_thread->stack_red = my_thread->stack = vma.addr;
  131. /* until now we're not ready to be exposed to other thread */
  132. add_thread(my_thread);
  133. set_as_child(arg->parent, my_thread);
  134. /* Don't signal the initialize event until we are actually init-ed */
  135. DkEventSet(arg->initialize_event);
  136. /***** From here down, we are switching to the user-provided stack ****/
  137. //user_stack_addr[0] ==> user provided function address
  138. //user_stack_addr[1] ==> arguments to user provided function.
  139. debug("child swapping stack to %p return 0x%lx: %d\n",
  140. stack, regs.rip, my_thread->tid);
  141. tcb->context.regs = &regs;
  142. fixup_child_context(tcb->context.regs);
  143. tcb->context.regs->rsp = (unsigned long)stack;
  144. put_thread(my_thread);
  145. restore_context(&tcb->context);
  146. return 0;
  147. }
  148. int migrate_fork (struct shim_cp_store * cpstore,
  149. struct shim_thread * thread,
  150. struct shim_process * process, va_list ap);
  151. /* long int __arg0 - flags
  152. * long int __arg1 - 16 bytes ( 2 words ) offset into the child stack allocated
  153. * by the parent */
  154. int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
  155. int * child_tidptr, void * tls)
  156. {
  157. //The Clone Implementation in glibc has setup the child's stack
  158. //with the function pointer and the argument to the funciton.
  159. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  160. struct shim_thread * self = get_cur_thread();
  161. assert(self);
  162. int * set_parent_tid = NULL;
  163. int ret = 0;
  164. /* special case for vfork. some runtime uses clone() for vfork */
  165. if (flags == (CLONE_VFORK | CLONE_VM | SIGCHLD) &&
  166. user_stack_addr == NULL && parent_tidptr == NULL &&
  167. child_tidptr == NULL && tls == NULL) {
  168. return shim_do_vfork();
  169. }
  170. const int supported_flags =
  171. CLONE_CHILD_CLEARTID |
  172. CLONE_CHILD_SETTID |
  173. CLONE_DETACHED | // Unused
  174. CLONE_FILES |
  175. CLONE_FS |
  176. CLONE_PARENT_SETTID |
  177. #ifdef CLONE_PIDFD
  178. CLONE_PIDFD |
  179. #endif
  180. CLONE_PTRACE | // Unused
  181. CLONE_SETTLS |
  182. CLONE_SIGHAND |
  183. CLONE_SYSVSEM |
  184. CLONE_THREAD |
  185. CLONE_VM |
  186. CSIGNAL;
  187. const int unsupported_flags = ~supported_flags;
  188. if (flags & unsupported_flags) {
  189. debug("clone called with unsupported flags argument.\n");
  190. return -EINVAL;
  191. }
  192. /* Explicitly disallow CLONE_VM without either of CLONE_THREAD or CLONE_VFORK on Graphene. While
  193. * Linux allows passing CLONE_VM without either of CLONE_THREAD or CLONE_VFORK, this usage is
  194. * exotic enough to not attempt a faithful emulation in Graphene. */
  195. if (flags & CLONE_VM)
  196. if (!((flags & CLONE_THREAD) || (flags & CLONE_VFORK))) {
  197. debug("CLONE_VM without either CLONE_THREAD or CLONE_VFORK is unsupported\n");
  198. return -EINVAL;
  199. }
  200. if ((flags & CLONE_THREAD) && !(flags & CLONE_SIGHAND))
  201. return -EINVAL;
  202. if ((flags & CLONE_SIGHAND) && !(flags & CLONE_VM))
  203. return -EINVAL;
  204. /* The caller may not have set the following three flags, but Graphene treats them as set to
  205. * simplify the implementation of clone. Only print a warning since returning an explicit error
  206. * code breaks many applications. */
  207. if (!(flags & CLONE_FS))
  208. debug("clone without CLONE_FS is not yet implemented\n");
  209. if (!(flags & CLONE_SIGHAND))
  210. debug("clone without CLONE_SIGHAND is not yet implemented\n");
  211. if (!(flags & CLONE_SYSVSEM))
  212. debug("clone without CLONE_SYSVSEM is not yet implemented\n");
  213. #ifdef CLONE_PIDFD
  214. if (flags & CLONE_PIDFD) {
  215. if (flags & (CLONE_DETACHED | CLONE_PARENT_SETTID | CLONE_THREAD))
  216. return -EINVAL;
  217. if (test_user_memory(parent_tidptr, sizeof(*parent_tidptr), false))
  218. return -EFAULT;
  219. if (*parent_tidptr != 0)
  220. return -EINVAL;
  221. }
  222. #endif
  223. if (flags & CLONE_PARENT_SETTID) {
  224. if (!parent_tidptr)
  225. return -EINVAL;
  226. set_parent_tid = parent_tidptr;
  227. }
  228. struct shim_thread * thread = get_new_thread(0);
  229. if (!thread) {
  230. ret = -ENOMEM;
  231. goto failed;
  232. }
  233. IDTYPE tid = thread->tid;
  234. if (flags & CLONE_CHILD_SETTID) {
  235. if (!child_tidptr) {
  236. ret = -EINVAL;
  237. goto failed;
  238. }
  239. thread->set_child_tid = child_tidptr;
  240. }
  241. if (flags & CLONE_CHILD_CLEARTID)
  242. /* Implemented in shim_futex.c: release_clear_child_id */
  243. thread->clear_child_tid = parent_tidptr;
  244. unsigned long fs_base = 0;
  245. if (flags & CLONE_SETTLS) {
  246. if (!tls) {
  247. ret = -EINVAL;
  248. goto failed;
  249. }
  250. fs_base = (unsigned long)tls;
  251. }
  252. if (!(flags & CLONE_THREAD))
  253. thread->tgid = thread->tid;
  254. struct shim_handle_map * handle_map = get_cur_handle_map(self);
  255. if (flags & CLONE_FILES) {
  256. set_handle_map(thread, handle_map);
  257. } else {
  258. /* if CLONE_FILES is not given, the new thread should receive
  259. a copy of current descriptor table */
  260. struct shim_handle_map * new_map = NULL;
  261. get_handle_map(handle_map);
  262. dup_handle_map(&new_map, handle_map);
  263. set_handle_map(thread, new_map);
  264. put_handle_map(handle_map);
  265. }
  266. if (!(flags & CLONE_VM)) {
  267. void * parent_stack = NULL;
  268. if (!fs_base) {
  269. fs_base = self->shim_tcb->context.fs_base;
  270. }
  271. /* associate cpu context to new forking thread for migration */
  272. shim_tcb_t shim_tcb;
  273. memcpy(&shim_tcb, self->shim_tcb, sizeof(shim_tcb_t));
  274. shim_tcb.context.fs_base = fs_base;
  275. thread->shim_tcb = &shim_tcb;
  276. if (user_stack_addr) {
  277. struct shim_vma_val vma;
  278. lookup_vma(ALLOC_ALIGN_DOWN_PTR(user_stack_addr), &vma);
  279. thread->stack_top = vma.addr + vma.length;
  280. thread->stack_red = thread->stack = vma.addr;
  281. parent_stack = (void *)self->shim_tcb->context.regs->rsp;
  282. thread->shim_tcb->context.regs->rsp = (unsigned long)user_stack_addr;
  283. }
  284. thread->is_alive = true;
  285. thread->in_vm = false;
  286. add_thread(thread);
  287. set_as_child(self, thread);
  288. ret = do_migrate_process(&migrate_fork, NULL, NULL, thread);
  289. thread->shim_tcb = NULL; /* cpu context of forked thread isn't
  290. * needed any more */
  291. if (parent_stack)
  292. self->shim_tcb->context.regs->rsp = (unsigned long)parent_stack;
  293. if (ret < 0)
  294. goto failed;
  295. lock(&thread->lock);
  296. handle_map = thread->handle_map;
  297. thread->handle_map = NULL;
  298. unlock(&thread->lock);
  299. if (handle_map)
  300. put_handle_map(handle_map);
  301. if (set_parent_tid)
  302. *set_parent_tid = tid;
  303. put_thread(thread);
  304. return tid;
  305. }
  306. enable_locking();
  307. struct clone_args new_args;
  308. memset(&new_args, 0, sizeof(new_args));
  309. new_args.create_event = DkNotificationEventCreate(PAL_FALSE);
  310. if (!new_args.create_event) {
  311. ret = -PAL_ERRNO;
  312. goto clone_thread_failed;
  313. }
  314. new_args.initialize_event = DkNotificationEventCreate(PAL_FALSE);
  315. if (!new_args.initialize_event) {
  316. ret = -PAL_ERRNO;
  317. goto clone_thread_failed;
  318. }
  319. /* Increasing refcount due to copy below. Passing ownership of the new copy
  320. * of this pointer to the new thread (receiver of new_args). */
  321. get_thread(thread);
  322. new_args.thread = thread;
  323. new_args.parent = self;
  324. new_args.stack = user_stack_addr;
  325. new_args.fs_base = fs_base;
  326. // Invoke DkThreadCreate to spawn off a child process using the actual
  327. // "clone" system call. DkThreadCreate allocates a stack for the child
  328. // and then runs the given function on that stack However, we want our
  329. // child to run on the Parent allocated stack , so once the DkThreadCreate
  330. // returns .The parent comes back here - however, the child is Happily
  331. // running the function we gave to DkThreadCreate.
  332. PAL_HANDLE pal_handle = thread_create(clone_implementation_wrapper,
  333. &new_args);
  334. if (!pal_handle) {
  335. ret = -PAL_ERRNO;
  336. put_thread(new_args.thread);
  337. goto clone_thread_failed;
  338. }
  339. thread->pal_handle = pal_handle;
  340. thread->in_vm = thread->is_alive = true;
  341. if (set_parent_tid)
  342. *set_parent_tid = tid;
  343. DkEventSet(new_args.create_event);
  344. object_wait_with_retry(new_args.initialize_event);
  345. DkObjectClose(new_args.initialize_event);
  346. put_thread(thread);
  347. return tid;
  348. clone_thread_failed:
  349. if (new_args.create_event)
  350. DkObjectClose(new_args.create_event);
  351. if (new_args.initialize_event)
  352. DkObjectClose(new_args.initialize_event);
  353. failed:
  354. if (thread)
  355. put_thread(thread);
  356. return ret;
  357. }