shim_clone.c 13 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_context * context)
  48. {
  49. if (context->ret_ip == &syscall_wrapper_after_syscalldb) {
  50. context->sp += RED_ZONE_SIZE;
  51. context->regs->rflags = context->regs->r11;
  52. context->ret_ip = (void*)context->regs->rcx;
  53. }
  54. }
  55. /* from **sysdeps/unix/sysv/linux/x86_64/clone.S:
  56. The userland implementation is:
  57. int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
  58. the kernel entry is:
  59. int clone (long flags, void *child_stack).
  60. The parameters are passed in register and on the stack from userland:
  61. rdi: fn
  62. rsi: child_stack
  63. rdx: flags
  64. rcx: arg
  65. r8d: TID field in parent
  66. r9d: thread pointer
  67. %esp+8: TID field in child
  68. The kernel expects:
  69. rax: system call number
  70. rdi: flags
  71. rsi: child_stack
  72. rdx: TID field in parent
  73. r10: TID field in child
  74. r8: thread pointer
  75. */
  76. /*
  77. * This Function is a wrapper around the user provided function.
  78. * Code flow for clone is as follows -
  79. * 1) User application allocates stack for child process and
  80. * calls clone. The clone code sets up the user function
  81. * address and the argument address on the child stack.
  82. * 2)we Hijack the clone call and control flows to shim_clone
  83. * 3)In Shim Clone we just call the DK Api to create a thread by providing a
  84. * wrapper function around the user provided function
  85. * 4)PAL layer allocates a stack and then invokes the clone syscall
  86. * 5)PAL runs thread_init function on PAL allocated Stack
  87. * 6)thread_init calls our wrapper and gives the user provided stack
  88. * address.
  89. * 7.In the wrapper function ,we just do the stack switch to user
  90. * Provided stack and execute the user Provided function.
  91. */
  92. /* glibc needs space offset by fs. In the absence of a good way to predict
  93. * how big the struct pthread will be (defined in nptl/descr.h),
  94. * let's just define a value that over-shoots it.
  95. */
  96. #define PTHREAD_PADDING 2048
  97. int clone_implementation_wrapper(struct 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. int stack_allocated = 0;
  103. object_wait_with_retry(arg->create_event);
  104. DkObjectClose(arg->create_event);
  105. struct shim_thread * my_thread = arg->thread;
  106. assert(my_thread);
  107. get_thread(my_thread);
  108. if (!my_thread->tcb) {
  109. stack_allocated = 1;
  110. my_thread->tcb = __alloca(sizeof(__libc_tcb_t) + PTHREAD_PADDING);
  111. }
  112. allocate_tls(my_thread->tcb, my_thread->user_tcb, my_thread);
  113. shim_tcb_t * tcb = &my_thread->tcb->shim_tcb;
  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 tcb to %p (stack allocated? %d)\n", my_thread->tcb, stack_allocated);
  118. struct shim_regs regs = *arg->parent->tcb->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. void * return_pc = arg->return_pc;
  125. struct shim_vma_val vma;
  126. lookup_vma(ALIGN_DOWN(stack), &vma);
  127. my_thread->stack_top = vma.addr + vma.length;
  128. my_thread->stack_red = my_thread->stack = vma.addr;
  129. /* until now we're not ready to be exposed to other thread */
  130. add_thread(my_thread);
  131. set_as_child(arg->parent, my_thread);
  132. /* Don't signal the initialize event until we are actually init-ed */
  133. DkEventSet(arg->initialize_event);
  134. /***** From here down, we are switching to the user-provided stack ****/
  135. //user_stack_addr[0] ==> user provided function address
  136. //user_stack_addr[1] ==> arguments to user provided function.
  137. debug("child swapping stack to %p return %p: %d\n",
  138. stack, return_pc, my_thread->tid);
  139. tcb->context.regs = &regs;
  140. tcb->context.sp = stack;
  141. tcb->context.ret_ip = return_pc;
  142. fixup_child_context(&tcb->context);
  143. restore_context(&tcb->context);
  144. return 0;
  145. }
  146. int migrate_fork (struct shim_cp_store * cpstore,
  147. struct shim_thread * thread,
  148. struct shim_process * process, va_list ap);
  149. /* long int __arg0 - flags
  150. * long int __arg1 - 16 bytes ( 2 words ) offset into the child stack allocated
  151. * by the parent */
  152. int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
  153. int * child_tidptr, void * tls)
  154. {
  155. //The Clone Implementation in glibc has setup the child's stack
  156. //with the function pointer and the argument to the funciton.
  157. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  158. struct shim_thread * self = get_cur_thread();
  159. assert(self);
  160. int * set_parent_tid = NULL;
  161. int ret = 0;
  162. /* special case for vfork. some runtime uses clone() for vfork */
  163. if (flags == (CLONE_VFORK | CLONE_VM | SIGCHLD) &&
  164. user_stack_addr == NULL && parent_tidptr == NULL &&
  165. child_tidptr == NULL && tls == NULL) {
  166. return shim_do_vfork();
  167. }
  168. assert((flags & ~(CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
  169. CLONE_CHILD_CLEARTID|CLONE_SETTLS|
  170. CLONE_VM|CLONE_FILES|
  171. CLONE_FS|CLONE_SIGHAND|CLONE_THREAD|
  172. CLONE_DETACHED| // Unused
  173. #ifdef CLONE_PTRACE
  174. CLONE_PTRACE| // Unused
  175. #endif
  176. CLONE_SYSVSEM|CSIGNAL)) == 0);
  177. if (!(flags & CLONE_FS))
  178. debug("clone without CLONE_FS is not yet implemented\n");
  179. if (!(flags & CLONE_SIGHAND))
  180. debug("clone without CLONE_SIGHAND is not yet implemented\n");
  181. if (!(flags & CLONE_SYSVSEM))
  182. debug("clone without CLONE_SYSVSEM is not yet implemented\n");
  183. /* currently unsupported flags.
  184. * Please update this once you added new flags support.
  185. */
  186. const int unsupported_flags =
  187. #ifdef CLONE_PIDFD
  188. CLONE_PIDFD |
  189. #endif
  190. CLONE_VFORK | /* vfork is handled above */
  191. CLONE_PARENT |
  192. CLONE_NEWNS |
  193. CLONE_UNTRACED |
  194. CLONE_NEWCGROUP |
  195. CLONE_NEWUTS |
  196. CLONE_NEWIPC |
  197. CLONE_NEWUSER |
  198. CLONE_NEWPID |
  199. CLONE_NEWNET |
  200. CLONE_IO;
  201. if (flags & unsupported_flags)
  202. debug("clone with flags 0x%x is not yet implemented\n",
  203. flags & unsupported_flags);
  204. if ((flags & (CLONE_NEWNS|CLONE_FS)) == (CLONE_NEWNS|CLONE_FS))
  205. return -EINVAL;
  206. if ((flags & (CLONE_NEWUSER|CLONE_FS)) == (CLONE_NEWUSER|CLONE_FS))
  207. return -EINVAL;
  208. if ((flags & CLONE_THREAD) && !(flags & CLONE_SIGHAND))
  209. return -EINVAL;
  210. if ((flags & CLONE_SIGHAND) && !(flags & CLONE_VM))
  211. return -EINVAL;
  212. if (flags & CLONE_THREAD && (flags & (CLONE_NEWUSER | CLONE_NEWPID)))
  213. return -EINVAL;
  214. #ifdef CLONE_PIDFD
  215. if (flags & CLONE_PIDFD) {
  216. if (flags & (CLONE_DETACHED | CLONE_PARENT_SETTID | CLONE_THREAD))
  217. return -EINVAL;
  218. if (test_user_memory(parent_tidptr, sizeof(*parent_tidptr), false))
  219. return -EFAULT;
  220. if (*parent_tidptr != 0)
  221. return -EINVAL;
  222. }
  223. #endif
  224. if (flags & CLONE_PARENT_SETTID) {
  225. if (!parent_tidptr)
  226. return -EINVAL;
  227. set_parent_tid = parent_tidptr;
  228. }
  229. struct shim_thread * thread = get_new_thread(0);
  230. if (!thread) {
  231. ret = -ENOMEM;
  232. goto failed;
  233. }
  234. IDTYPE tid = thread->tid;
  235. if (flags & CLONE_CHILD_SETTID) {
  236. if (!child_tidptr) {
  237. ret = -EINVAL;
  238. goto failed;
  239. }
  240. thread->set_child_tid = child_tidptr;
  241. }
  242. if (flags & CLONE_CHILD_CLEARTID)
  243. /* Implemented in shim_futex.c: release_clear_child_id */
  244. thread->clear_child_tid = parent_tidptr;
  245. if (flags & CLONE_SETTLS) {
  246. if (!tls) {
  247. ret = -EINVAL;
  248. goto failed;
  249. }
  250. thread->tcb = tls;
  251. thread->user_tcb = true;
  252. } else {
  253. thread->tcb = NULL;
  254. }
  255. if (!(flags & CLONE_THREAD))
  256. thread->tgid = thread->tid;
  257. struct shim_handle_map * handle_map = get_cur_handle_map(self);
  258. if (flags & CLONE_FILES) {
  259. set_handle_map(thread, handle_map);
  260. } else {
  261. /* if CLONE_FILES is not given, the new thread should receive
  262. a copy of current descriptor table */
  263. struct shim_handle_map * new_map = NULL;
  264. get_handle_map(handle_map);
  265. dup_handle_map(&new_map, handle_map);
  266. set_handle_map(thread, new_map);
  267. put_handle_map(handle_map);
  268. }
  269. if (!(flags & CLONE_VM)) {
  270. __libc_tcb_t * tcb;
  271. shim_tcb_t * old_shim_tcb = NULL;
  272. if (thread->tcb) {
  273. tcb = thread->tcb;
  274. } else {
  275. thread->tcb = tcb = self->tcb;
  276. old_shim_tcb = __alloca(sizeof(shim_tcb_t));
  277. memcpy(old_shim_tcb, &tcb->shim_tcb, sizeof(shim_tcb_t));
  278. thread->user_tcb = self->user_tcb;
  279. }
  280. if (user_stack_addr) {
  281. struct shim_vma_val vma;
  282. lookup_vma(ALIGN_DOWN(user_stack_addr), &vma);
  283. thread->stack_top = vma.addr + vma.length;
  284. thread->stack_red = thread->stack = vma.addr;
  285. tcb->shim_tcb.context.sp = user_stack_addr;
  286. tcb->shim_tcb.context.ret_ip = shim_get_tls()->context.ret_ip;
  287. }
  288. thread->is_alive = true;
  289. thread->in_vm = false;
  290. add_thread(thread);
  291. set_as_child(self, thread);
  292. ret = do_migrate_process(&migrate_fork, NULL, NULL, thread);
  293. if (old_shim_tcb)
  294. memcpy(&tcb->shim_tcb, old_shim_tcb, sizeof(tcb->shim_tcb));
  295. if (ret < 0)
  296. goto failed;
  297. lock(&thread->lock);
  298. handle_map = thread->handle_map;
  299. thread->handle_map = NULL;
  300. unlock(&thread->lock);
  301. if (handle_map)
  302. put_handle_map(handle_map);
  303. if (set_parent_tid)
  304. *set_parent_tid = tid;
  305. put_thread(thread);
  306. return tid;
  307. }
  308. enable_locking();
  309. struct clone_args new_args;
  310. memset(&new_args, 0, sizeof(new_args));
  311. new_args.create_event = DkNotificationEventCreate(PAL_FALSE);
  312. if (!new_args.create_event) {
  313. ret = -PAL_ERRNO;
  314. goto clone_thread_failed;
  315. }
  316. new_args.initialize_event = DkNotificationEventCreate(PAL_FALSE);
  317. if (!new_args.initialize_event) {
  318. ret = -PAL_ERRNO;
  319. goto clone_thread_failed;
  320. }
  321. new_args.thread = thread;
  322. new_args.parent = self;
  323. new_args.stack = user_stack_addr;
  324. new_args.return_pc = shim_get_tls()->context.ret_ip;
  325. // Invoke DkThreadCreate to spawn off a child process using the actual
  326. // "clone" system call. DkThreadCreate allocates a stack for the child
  327. // and then runs the given function on that stack However, we want our
  328. // child to run on the Parent allocated stack , so once the DkThreadCreate
  329. // returns .The parent comes back here - however, the child is Happily
  330. // running the function we gave to DkThreadCreate.
  331. PAL_HANDLE pal_handle = thread_create(clone_implementation_wrapper,
  332. &new_args, flags);
  333. if (!pal_handle) {
  334. ret = -PAL_ERRNO;
  335. goto clone_thread_failed;
  336. }
  337. thread->pal_handle = pal_handle;
  338. thread->in_vm = thread->is_alive = true;
  339. if (set_parent_tid)
  340. *set_parent_tid = tid;
  341. DkEventSet(new_args.create_event);
  342. object_wait_with_retry(new_args.initialize_event);
  343. DkObjectClose(new_args.initialize_event);
  344. put_thread(thread);
  345. return tid;
  346. clone_thread_failed:
  347. if (new_args.create_event)
  348. DkObjectClose(new_args.create_event);
  349. if (new_args.initialize_event)
  350. DkObjectClose(new_args.initialize_event);
  351. failed:
  352. if (thread)
  353. put_thread(thread);
  354. return ret;
  355. }