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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU 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. struct clone_args {
  36. PAL_HANDLE create_event;
  37. PAL_HANDLE initialize_event;
  38. struct shim_thread * thread, * parent;
  39. void * stack;
  40. void * return_pc;
  41. };
  42. /* from **sysdeps/unix/sysv/linux/x86_64/clone.S:
  43. The userland implementation is:
  44. int clone (int (*fn)(void *arg), void *child_stack, int flags, void *arg),
  45. the kernel entry is:
  46. int clone (long flags, void *child_stack).
  47. The parameters are passed in register and on the stack from userland:
  48. rdi: fn
  49. rsi: child_stack
  50. rdx: flags
  51. rcx: arg
  52. r8d: TID field in parent
  53. r9d: thread pointer
  54. %esp+8: TID field in child
  55. The kernel expects:
  56. rax: system call number
  57. rdi: flags
  58. rsi: child_stack
  59. rdx: TID field in parent
  60. r10: TID field in child
  61. r8: thread pointer
  62. */
  63. /*
  64. * This Function is a wrapper around the user provided function.
  65. * Code flow for clone is as follows -
  66. * 1) User application allocates stack for child process and
  67. * calls clone. The clone code sets up the user function
  68. * address and the argument address on the child stack.
  69. * 2)we Hijack the clone call and control flows to shim_clone
  70. * 3)In Shim Clone we just call the DK Api to create a thread by providing a
  71. * wrapper function around the user provided function
  72. * 4)PAL layer allocates a stack and then invokes the clone syscall
  73. * 5)PAL runs thread_init function on PAL allocated Stack
  74. * 6)thread_init calls our wrapper and gives the user provided stack
  75. * address.
  76. * 7.In the wrapper function ,we just do the stack switch to user
  77. * Provided stack and execute the user Provided function.
  78. */
  79. int clone_implementation_wrapper(struct clone_args * arg)
  80. {
  81. //The child thread created by PAL is now running on the
  82. //PAL allocated stack. We need to switch the stack to use
  83. //the user provided stack.
  84. struct clone_args *pcargs = arg;
  85. DkObjectsWaitAny(1, &pcargs->create_event, NO_TIMEOUT);
  86. DkObjectClose(pcargs->create_event);
  87. struct shim_thread * my_thread = pcargs->thread;
  88. assert(my_thread);
  89. get_thread(my_thread);
  90. if (!my_thread->tcb)
  91. my_thread->tcb = __alloca(sizeof(__libc_tcb_t));
  92. allocate_tls(my_thread->tcb, my_thread);
  93. shim_tcb_t * tcb = &((__libc_tcb_t *) my_thread->tcb)->shim_tcb;
  94. debug_setbuf(tcb, true);
  95. debug("set tcb to %p\n", my_thread->tcb);
  96. struct shim_regs * regs = __alloca(sizeof(struct shim_regs));
  97. *regs = *((__libc_tcb_t *) arg->parent->tcb)->shim_tcb.context.regs;
  98. if (my_thread->set_child_tid)
  99. *(my_thread->set_child_tid) = my_thread->tid;
  100. void * stack = pcargs->stack;
  101. void * return_pc = pcargs->return_pc;
  102. struct shim_vma * vma = NULL;
  103. lookup_supervma(ALIGN_DOWN(stack), allocsize, &vma);
  104. assert(vma);
  105. my_thread->stack_top = vma->addr + vma->length;
  106. my_thread->stack_red = my_thread->stack = vma->addr;
  107. snprintf(vma->comment, VMA_COMMENT_LEN, "stack:%d", my_thread->tid);
  108. /* Don't signal the initialize event until we are actually init-ed */
  109. DkEventSet(pcargs->initialize_event);
  110. /***** From here down, we are switching to the user-provided stack ****/
  111. //user_stack_addr[0] ==> user provided function address
  112. //user_stack_addr[1] ==> arguments to user provided function.
  113. debug("child swapping stack to %p return %p: %d\n",
  114. stack, return_pc, my_thread->tid);
  115. tcb->context.regs = regs;
  116. tcb->context.sp = stack;
  117. tcb->context.ret_ip = return_pc;
  118. restore_context(&tcb->context);
  119. return 0;
  120. }
  121. int migrate_fork (struct shim_cp_store * cpstore,
  122. struct shim_process * process,
  123. struct shim_thread * thread, va_list ap);
  124. /* long int __arg0 - flags
  125. * long int __arg1 - 16 bytes ( 2 words ) offset into the child stack allocated
  126. * by the parent */
  127. int shim_do_clone (int flags, void * user_stack_addr, int * parent_tidptr,
  128. void * tls, int * child_tidptr)
  129. {
  130. //The Clone Implementation in glibc has setup the child's stack
  131. //with the function pointer and the argument to the funciton.
  132. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  133. struct shim_thread * self = get_cur_thread();
  134. assert(self);
  135. int * set_parent_tid = NULL;
  136. int ret = 0;
  137. assert((flags & ~(CLONE_PARENT_SETTID|CLONE_CHILD_SETTID|
  138. CLONE_CHILD_CLEARTID|CLONE_SETTLS|
  139. CLONE_VM|CLONE_FILES|
  140. CLONE_FS|CLONE_SIGHAND|CLONE_THREAD|
  141. CLONE_DETACHED| // Unused
  142. #ifdef CLONE_PTRACE
  143. CLONE_PTRACE| // Unused
  144. #endif
  145. CLONE_SYSVSEM|CSIGNAL)) == 0);
  146. if (!(flags & CLONE_FS))
  147. debug("clone without CLONE_FS is not yet implemented\n");
  148. if (!(flags & CLONE_SIGHAND))
  149. debug("clone without CLONE_SIGHAND is not yet implemented\n");
  150. if (!(flags & CLONE_SYSVSEM))
  151. debug("clone without CLONE_SYSVSEM is not yet implemented\n");
  152. if (flags & CLONE_PARENT_SETTID) {
  153. if (!parent_tidptr)
  154. return -EINVAL;
  155. set_parent_tid = parent_tidptr;
  156. }
  157. struct shim_thread * thread = get_new_thread(0);
  158. if (!thread) {
  159. ret = -ENOMEM;
  160. goto failed;
  161. }
  162. IDTYPE tid = thread->tid;
  163. if (flags & CLONE_CHILD_SETTID) {
  164. if (!child_tidptr) {
  165. ret = -EINVAL;
  166. goto failed;
  167. }
  168. thread->set_child_tid = child_tidptr;
  169. }
  170. if (flags & CLONE_CHILD_CLEARTID)
  171. /* Implemented in shim_futex.c: release_clear_child_id */
  172. thread->clear_child_tid = parent_tidptr;
  173. if (flags & CLONE_SETTLS) {
  174. if (!tls) {
  175. ret = -EINVAL;
  176. goto failed;
  177. }
  178. thread->tcb = tls;
  179. thread->user_tcb = true;
  180. } else {
  181. thread->tcb = NULL;
  182. }
  183. if (!(flags & CLONE_THREAD))
  184. thread->tgid = thread->tid;
  185. struct shim_handle_map * handle_map = get_cur_handle_map(self);
  186. if (flags & CLONE_FILES) {
  187. set_handle_map(thread, handle_map);
  188. } else {
  189. /* if CLONE_FILES is not given, the new thread should receive
  190. a copy of current descriptor table */
  191. struct shim_handle_map * new_map = NULL;
  192. get_handle_map(handle_map);
  193. dup_handle_map(&new_map, handle_map);
  194. set_handle_map(thread, new_map);
  195. put_handle_map(handle_map);
  196. }
  197. if (!(flags & CLONE_VM)) {
  198. __libc_tcb_t * tcb;
  199. shim_tcb_t * old_shim_tcb = NULL;
  200. if (thread->tcb) {
  201. tcb = (__libc_tcb_t *) thread->tcb;
  202. } else {
  203. thread->tcb = tcb = (__libc_tcb_t *) self->tcb;
  204. old_shim_tcb = __alloca(sizeof(shim_tcb_t));
  205. memcpy(old_shim_tcb, &tcb->shim_tcb, sizeof(shim_tcb_t));
  206. }
  207. if (user_stack_addr) {
  208. struct shim_vma * vma = NULL;
  209. lookup_supervma(ALIGN_DOWN(user_stack_addr), allocsize, &vma);
  210. assert(vma);
  211. thread->stack_top = vma->addr + vma->length;
  212. thread->stack_red = thread->stack = vma->addr;
  213. tcb->shim_tcb.context.sp = user_stack_addr;
  214. tcb->shim_tcb.context.ret_ip = *(void **) user_stack_addr;
  215. } else {
  216. tcb->shim_tcb.context.ret_ip = *(void **) tcb->shim_tcb.context.sp;
  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(0);
  241. if (!new_args->create_event) {
  242. ret = -PAL_ERRNO;
  243. goto clone_thread_failed;
  244. }
  245. new_args->initialize_event = DkNotificationEventCreate(0);
  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. }