shim_clone.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350
  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 * vma = NULL;
  104. lookup_supervma(ALIGN_DOWN(stack), allocsize, &vma);
  105. assert(vma);
  106. my_thread->stack_top = vma->addr + vma->length;
  107. my_thread->stack_red = my_thread->stack = vma->addr;
  108. snprintf(vma->comment, VMA_COMMENT_LEN, "stack:%d", my_thread->tid);
  109. put_vma(vma);
  110. /* Don't signal the initialize event until we are actually init-ed */
  111. DkEventSet(pcargs->initialize_event);
  112. /***** From here down, we are switching to the user-provided stack ****/
  113. //user_stack_addr[0] ==> user provided function address
  114. //user_stack_addr[1] ==> arguments to user provided function.
  115. debug("child swapping stack to %p return %p: %d\n",
  116. stack, return_pc, my_thread->tid);
  117. tcb->context.regs = regs;
  118. tcb->context.sp = stack;
  119. tcb->context.ret_ip = return_pc;
  120. restore_context(&tcb->context);
  121. return 0;
  122. }
  123. int migrate_fork (struct shim_cp_store * cpstore,
  124. struct shim_thread * thread,
  125. struct shim_process * process, va_list ap);
  126. /* long int __arg0 - flags
  127. * long int __arg1 - 16 bytes ( 2 words ) offset into the child stack allocated
  128. * by the parent */
  129. int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
  130. int * child_tidptr, void * tls)
  131. {
  132. //The Clone Implementation in glibc has setup the child's stack
  133. //with the function pointer and the argument to the funciton.
  134. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  135. struct shim_thread * self = get_cur_thread();
  136. assert(self);
  137. int * set_parent_tid = NULL;
  138. int ret = 0;
  139. assert((flags & ~(CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
  140. CLONE_CHILD_CLEARTID|CLONE_SETTLS|
  141. CLONE_VM|CLONE_FILES|
  142. CLONE_FS|CLONE_SIGHAND|CLONE_THREAD|
  143. CLONE_DETACHED| // Unused
  144. #ifdef CLONE_PTRACE
  145. CLONE_PTRACE| // Unused
  146. #endif
  147. CLONE_SYSVSEM|CSIGNAL)) == 0);
  148. if (!(flags & CLONE_FS))
  149. debug("clone without CLONE_FS is not yet implemented\n");
  150. if (!(flags & CLONE_SIGHAND))
  151. debug("clone without CLONE_SIGHAND is not yet implemented\n");
  152. if (!(flags & CLONE_SYSVSEM))
  153. debug("clone without CLONE_SYSVSEM is not yet implemented\n");
  154. if (flags & CLONE_PARENT_SETTID) {
  155. if (!parent_tidptr)
  156. return -EINVAL;
  157. set_parent_tid = parent_tidptr;
  158. }
  159. struct shim_thread * thread = get_new_thread(0);
  160. if (!thread) {
  161. ret = -ENOMEM;
  162. goto failed;
  163. }
  164. IDTYPE tid = thread->tid;
  165. if (flags & CLONE_CHILD_SETTID) {
  166. if (!child_tidptr) {
  167. ret = -EINVAL;
  168. goto failed;
  169. }
  170. thread->set_child_tid = child_tidptr;
  171. }
  172. if (flags & CLONE_CHILD_CLEARTID)
  173. /* Implemented in shim_futex.c: release_clear_child_id */
  174. thread->clear_child_tid = parent_tidptr;
  175. if (flags & CLONE_SETTLS) {
  176. if (!tls) {
  177. ret = -EINVAL;
  178. goto failed;
  179. }
  180. thread->tcb = tls;
  181. thread->user_tcb = true;
  182. } else {
  183. thread->tcb = NULL;
  184. }
  185. if (!(flags & CLONE_THREAD))
  186. thread->tgid = thread->tid;
  187. struct shim_handle_map * handle_map = get_cur_handle_map(self);
  188. if (flags & CLONE_FILES) {
  189. set_handle_map(thread, handle_map);
  190. } else {
  191. /* if CLONE_FILES is not given, the new thread should receive
  192. a copy of current descriptor table */
  193. struct shim_handle_map * new_map = NULL;
  194. get_handle_map(handle_map);
  195. dup_handle_map(&new_map, handle_map);
  196. set_handle_map(thread, new_map);
  197. put_handle_map(handle_map);
  198. }
  199. if (!(flags & CLONE_VM)) {
  200. __libc_tcb_t * tcb;
  201. shim_tcb_t * old_shim_tcb = NULL;
  202. if (thread->tcb) {
  203. tcb = (__libc_tcb_t *) thread->tcb;
  204. } else {
  205. thread->tcb = tcb = (__libc_tcb_t *) self->tcb;
  206. old_shim_tcb = __alloca(sizeof(shim_tcb_t));
  207. memcpy(old_shim_tcb, &tcb->shim_tcb, sizeof(shim_tcb_t));
  208. }
  209. if (user_stack_addr) {
  210. struct shim_vma * vma = NULL;
  211. lookup_supervma(ALIGN_DOWN(user_stack_addr), allocsize, &vma);
  212. assert(vma);
  213. thread->stack_top = vma->addr + vma->length;
  214. thread->stack_red = thread->stack = vma->addr;
  215. tcb->shim_tcb.context.sp = user_stack_addr;
  216. tcb->shim_tcb.context.ret_ip = *(void **) user_stack_addr;
  217. }
  218. thread->is_alive = true;
  219. thread->in_vm = false;
  220. add_thread(thread);
  221. set_as_child(self, thread);
  222. if ((ret = do_migrate_process(&migrate_fork, NULL, NULL, thread)) < 0)
  223. goto failed;
  224. if (old_shim_tcb)
  225. memcpy(&tcb->shim_tcb, old_shim_tcb, sizeof(shim_tcb_t));
  226. lock(thread->lock);
  227. handle_map = thread->handle_map;
  228. thread->handle_map = NULL;
  229. unlock(thread->lock);
  230. if (handle_map)
  231. put_handle_map(handle_map);
  232. if (set_parent_tid)
  233. *set_parent_tid = tid;
  234. put_thread(thread);
  235. return tid;
  236. }
  237. enable_locking();
  238. struct clone_args * new_args = __alloca(sizeof(struct clone_args));
  239. memset(new_args, 0, sizeof(struct clone_args));
  240. new_args->create_event = DkNotificationEventCreate(PAL_FALSE);
  241. if (!new_args->create_event) {
  242. ret = -PAL_ERRNO;
  243. goto clone_thread_failed;
  244. }
  245. new_args->initialize_event = DkNotificationEventCreate(PAL_FALSE);
  246. if (!new_args->initialize_event) {
  247. ret = -PAL_ERRNO;
  248. goto clone_thread_failed;
  249. }
  250. new_args->thread = thread;
  251. new_args->parent = self;
  252. new_args->stack = user_stack_addr;
  253. new_args->return_pc = *(void **) user_stack_addr;
  254. // Invoke DkThreadCreate to spawn off a child process using the actual
  255. // "clone" system call. DkThreadCreate allocates a stack for the child
  256. // and then runs the given function on that stack However, we want our
  257. // child to run on the Parent allocated stack , so once the DkThreadCreate
  258. // returns .The parent comes back here - however, the child is Happily
  259. // running the function we gave to DkThreadCreate.
  260. PAL_HANDLE pal_handle = thread_create(clone_implementation_wrapper,
  261. new_args, flags);
  262. if (!pal_handle) {
  263. ret = -PAL_ERRNO;
  264. goto clone_thread_failed;
  265. }
  266. thread->pal_handle = pal_handle;
  267. thread->in_vm = thread->is_alive = true;
  268. add_thread(thread);
  269. set_as_child(self, thread);
  270. if (set_parent_tid)
  271. *set_parent_tid = tid;
  272. DkEventSet(new_args->create_event);
  273. DkObjectsWaitAny(1, &new_args->initialize_event, NO_TIMEOUT);
  274. DkObjectClose(new_args->initialize_event);
  275. put_thread(thread);
  276. return tid;
  277. clone_thread_failed:
  278. if (new_args->create_event)
  279. DkObjectClose(new_args->create_event);
  280. if (new_args->initialize_event)
  281. DkObjectClose(new_args->initialize_event);
  282. failed:
  283. if (thread)
  284. put_thread(thread);
  285. return ret;
  286. }