shim_clone.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU Lesser General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU Lesser General Public License for more details.
  13. You should have received a copy of the GNU Lesser General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_clone.c
  17. *
  18. * Implementation of system call "clone". (using "clone" as "fork" is not
  19. * implemented yet.)
  20. */
  21. #include <shim_types.h>
  22. #include <shim_internal.h>
  23. #include <shim_table.h>
  24. #include <shim_thread.h>
  25. #include <shim_utils.h>
  26. #include <shim_checkpoint.h>
  27. #include <shim_profile.h>
  28. #include <pal.h>
  29. #include <pal_error.h>
  30. #include <errno.h>
  31. #include <sys/syscall.h>
  32. #include <sys/mman.h>
  33. #include <linux/sched.h>
  34. #include <asm/prctl.h>
  35. /* from **sysdeps/unix/sysv/linux/x86_64/clone.S:
  36. The userland implementation is:
  37. int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
  38. the kernel entry is:
  39. int clone (long flags, void *child_stack).
  40. The parameters are passed in register and on the stack from userland:
  41. rdi: fn
  42. rsi: child_stack
  43. rdx: flags
  44. rcx: arg
  45. r8d: TID field in parent
  46. r9d: thread pointer
  47. %esp+8: TID field in child
  48. The kernel expects:
  49. rax: system call number
  50. rdi: flags
  51. rsi: child_stack
  52. rdx: TID field in parent
  53. r10: TID field in child
  54. r8: thread pointer
  55. */
  56. /*
  57. * This Function is a wrapper around the user provided function.
  58. * Code flow for clone is as follows -
  59. * 1) User application allocates stack for child process and
  60. * calls clone. The clone code sets up the user function
  61. * address and the argument address on the child stack.
  62. * 2)we Hijack the clone call and control flows to shim_clone
  63. * 3)In Shim Clone we just call the DK Api to create a thread by providing a
  64. * wrapper function around the user provided function
  65. * 4)PAL layer allocates a stack and then invokes the clone syscall
  66. * 5)PAL runs thread_init function on PAL allocated Stack
  67. * 6)thread_init calls our wrapper and gives the user provided stack
  68. * address.
  69. * 7.In the wrapper function ,we just do the stack switch to user
  70. * Provided stack and execute the user Provided function.
  71. */
  72. /* glibc needs space offset by fs. In the absence of a good way to predict
  73. * how big the struct pthread will be (defined in nptl/descr.h),
  74. * let's just define a value that over-shoots it.
  75. */
  76. #define PTHREAD_PADDING 2048
  77. int clone_implementation_wrapper(struct clone_args * arg)
  78. {
  79. //The child thread created by PAL is now running on the
  80. //PAL allocated stack. We need to switch the stack to use
  81. //the user provided stack.
  82. struct clone_args *pcargs = arg;
  83. int stack_allocated = 0;
  84. DkObjectsWaitAny(1, &pcargs->create_event, NO_TIMEOUT);
  85. DkObjectClose(pcargs->create_event);
  86. struct shim_thread * my_thread = pcargs->thread;
  87. assert(my_thread);
  88. get_thread(my_thread);
  89. if (!my_thread->tcb) {
  90. stack_allocated = 1;
  91. my_thread->tcb = __alloca(sizeof(__libc_tcb_t) + PTHREAD_PADDING);
  92. }
  93. allocate_tls(my_thread->tcb, my_thread->user_tcb, my_thread);
  94. shim_tcb_t * tcb = &((__libc_tcb_t *) my_thread->tcb)->shim_tcb;
  95. debug_setbuf(tcb, true);
  96. debug("set tcb to %p (stack allocated? %d)\n", my_thread->tcb, stack_allocated);
  97. struct shim_regs * regs = __alloca(sizeof(struct shim_regs));
  98. *regs = *((__libc_tcb_t *) arg->parent->tcb)->shim_tcb.context.regs;
  99. if (my_thread->set_child_tid)
  100. *(my_thread->set_child_tid) = my_thread->tid;
  101. void * stack = pcargs->stack;
  102. void * return_pc = pcargs->return_pc;
  103. struct shim_vma_val vma;
  104. lookup_vma(ALIGN_DOWN(stack), &vma);
  105. my_thread->stack_top = vma.addr + vma.length;
  106. my_thread->stack_red = my_thread->stack = vma.addr;
  107. /* Don't signal the initialize event until we are actually init-ed */
  108. DkEventSet(pcargs->initialize_event);
  109. /***** From here down, we are switching to the user-provided stack ****/
  110. //user_stack_addr[0] ==> user provided function address
  111. //user_stack_addr[1] ==> arguments to user provided function.
  112. debug("child swapping stack to %p return %p: %d\n",
  113. stack, return_pc, my_thread->tid);
  114. tcb->context.regs = regs;
  115. tcb->context.sp = stack;
  116. tcb->context.ret_ip = return_pc;
  117. restore_context(&tcb->context);
  118. return 0;
  119. }
  120. int migrate_fork (struct shim_cp_store * cpstore,
  121. struct shim_thread * thread,
  122. struct shim_process * process, va_list ap);
  123. /* long int __arg0 - flags
  124. * long int __arg1 - 16 bytes ( 2 words ) offset into the child stack allocated
  125. * by the parent */
  126. int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
  127. int * child_tidptr, void * tls)
  128. {
  129. //The Clone Implementation in glibc has setup the child's stack
  130. //with the function pointer and the argument to the funciton.
  131. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  132. struct shim_thread * self = get_cur_thread();
  133. assert(self);
  134. int * set_parent_tid = NULL;
  135. int ret = 0;
  136. assert((flags & ~(CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
  137. CLONE_CHILD_CLEARTID|CLONE_SETTLS|
  138. CLONE_VM|CLONE_FILES|
  139. CLONE_FS|CLONE_SIGHAND|CLONE_THREAD|
  140. CLONE_DETACHED| // Unused
  141. #ifdef CLONE_PTRACE
  142. CLONE_PTRACE| // Unused
  143. #endif
  144. CLONE_SYSVSEM|CSIGNAL)) == 0);
  145. if (!(flags & CLONE_FS))
  146. debug("clone without CLONE_FS is not yet implemented\n");
  147. if (!(flags & CLONE_SIGHAND))
  148. debug("clone without CLONE_SIGHAND is not yet implemented\n");
  149. if (!(flags & CLONE_SYSVSEM))
  150. debug("clone without CLONE_SYSVSEM is not yet implemented\n");
  151. if (flags & CLONE_PARENT_SETTID) {
  152. if (!parent_tidptr)
  153. return -EINVAL;
  154. set_parent_tid = parent_tidptr;
  155. }
  156. struct shim_thread * thread = get_new_thread(0);
  157. if (!thread) {
  158. ret = -ENOMEM;
  159. goto failed;
  160. }
  161. IDTYPE tid = thread->tid;
  162. if (flags & CLONE_CHILD_SETTID) {
  163. if (!child_tidptr) {
  164. ret = -EINVAL;
  165. goto failed;
  166. }
  167. thread->set_child_tid = child_tidptr;
  168. }
  169. if (flags & CLONE_CHILD_CLEARTID)
  170. /* Implemented in shim_futex.c: release_clear_child_id */
  171. thread->clear_child_tid = parent_tidptr;
  172. if (flags & CLONE_SETTLS) {
  173. if (!tls) {
  174. ret = -EINVAL;
  175. goto failed;
  176. }
  177. thread->tcb = tls;
  178. thread->user_tcb = true;
  179. } else {
  180. thread->tcb = NULL;
  181. }
  182. if (!(flags & CLONE_THREAD))
  183. thread->tgid = thread->tid;
  184. struct shim_handle_map * handle_map = get_cur_handle_map(self);
  185. if (flags & CLONE_FILES) {
  186. set_handle_map(thread, handle_map);
  187. } else {
  188. /* if CLONE_FILES is not given, the new thread should receive
  189. a copy of current descriptor table */
  190. struct shim_handle_map * new_map = NULL;
  191. get_handle_map(handle_map);
  192. dup_handle_map(&new_map, handle_map);
  193. set_handle_map(thread, new_map);
  194. put_handle_map(handle_map);
  195. }
  196. if (!(flags & CLONE_VM)) {
  197. __libc_tcb_t * tcb;
  198. shim_tcb_t * old_shim_tcb = NULL;
  199. if (thread->tcb) {
  200. tcb = (__libc_tcb_t *) thread->tcb;
  201. } else {
  202. thread->tcb = tcb = (__libc_tcb_t *) self->tcb;
  203. old_shim_tcb = __alloca(sizeof(shim_tcb_t));
  204. memcpy(old_shim_tcb, &tcb->shim_tcb, sizeof(shim_tcb_t));
  205. }
  206. if (user_stack_addr) {
  207. struct shim_vma_val vma;
  208. lookup_vma(ALIGN_DOWN(user_stack_addr), &vma);
  209. thread->stack_top = vma.addr + vma.length;
  210. thread->stack_red = thread->stack = vma.addr;
  211. tcb->shim_tcb.context.sp = user_stack_addr;
  212. tcb->shim_tcb.context.ret_ip = *(void **) user_stack_addr;
  213. }
  214. thread->is_alive = true;
  215. thread->in_vm = false;
  216. add_thread(thread);
  217. set_as_child(self, thread);
  218. if ((ret = do_migrate_process(&migrate_fork, NULL, NULL, thread)) < 0)
  219. goto failed;
  220. if (old_shim_tcb)
  221. memcpy(&tcb->shim_tcb, old_shim_tcb, sizeof(shim_tcb_t));
  222. lock(thread->lock);
  223. handle_map = thread->handle_map;
  224. thread->handle_map = NULL;
  225. unlock(thread->lock);
  226. if (handle_map)
  227. put_handle_map(handle_map);
  228. if (set_parent_tid)
  229. *set_parent_tid = tid;
  230. put_thread(thread);
  231. return tid;
  232. }
  233. enable_locking();
  234. struct clone_args * new_args = __alloca(sizeof(struct clone_args));
  235. memset(new_args, 0, sizeof(struct clone_args));
  236. new_args->create_event = DkNotificationEventCreate(PAL_FALSE);
  237. if (!new_args->create_event) {
  238. ret = -PAL_ERRNO;
  239. goto clone_thread_failed;
  240. }
  241. new_args->initialize_event = DkNotificationEventCreate(PAL_FALSE);
  242. if (!new_args->initialize_event) {
  243. ret = -PAL_ERRNO;
  244. goto clone_thread_failed;
  245. }
  246. new_args->thread = thread;
  247. new_args->parent = self;
  248. new_args->stack = user_stack_addr;
  249. new_args->return_pc = *(void **) user_stack_addr;
  250. // Invoke DkThreadCreate to spawn off a child process using the actual
  251. // "clone" system call. DkThreadCreate allocates a stack for the child
  252. // and then runs the given function on that stack However, we want our
  253. // child to run on the Parent allocated stack , so once the DkThreadCreate
  254. // returns .The parent comes back here - however, the child is Happily
  255. // running the function we gave to DkThreadCreate.
  256. PAL_HANDLE pal_handle = thread_create(clone_implementation_wrapper,
  257. new_args, flags);
  258. if (!pal_handle) {
  259. ret = -PAL_ERRNO;
  260. goto clone_thread_failed;
  261. }
  262. thread->pal_handle = pal_handle;
  263. thread->in_vm = thread->is_alive = true;
  264. add_thread(thread);
  265. set_as_child(self, thread);
  266. if (set_parent_tid)
  267. *set_parent_tid = tid;
  268. DkEventSet(new_args->create_event);
  269. DkObjectsWaitAny(1, &new_args->initialize_event, NO_TIMEOUT);
  270. DkObjectClose(new_args->initialize_event);
  271. put_thread(thread);
  272. return tid;
  273. clone_thread_failed:
  274. if (new_args->create_event)
  275. DkObjectClose(new_args->create_event);
  276. if (new_args->initialize_event)
  277. DkObjectClose(new_args->initialize_event);
  278. failed:
  279. if (thread)
  280. put_thread(thread);
  281. return ret;
  282. }