shim_vfork.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_vfork.c
  15. *
  16. * Implementation of system call "vfork".
  17. */
  18. #include <shim_internal.h>
  19. #include <shim_utils.h>
  20. #include <shim_table.h>
  21. #include <shim_thread.h>
  22. #include <shim_checkpoint.h>
  23. #include <pal.h>
  24. #include <pal_error.h>
  25. #include <sys/syscall.h>
  26. #include <sys/mman.h>
  27. #include <asm/prctl.h>
  28. #include <linux/futex.h>
  29. #include <errno.h>
  30. struct vfork_args {
  31. PAL_HANDLE create_event;
  32. struct shim_thread * thread;
  33. };
  34. int shim_do_vfork (void)
  35. {
  36. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  37. /* DEP 7/7/12 - Why r13?
  38. *
  39. * Chia-che: when libc call vfork, they store the pointer to the
  40. * caller in rdi. (reference: sysdeps/unix/sysv/linux/x86_64/vfork.S.
  41. * Because rdi might be used in SHIM, I cache rdi in r13 (reference:
  42. * syscallas.S).
  43. */
  44. struct shim_thread * cur_thread = get_cur_thread();
  45. struct shim_thread * new_thread = get_new_thread(0);
  46. /* put the new thread in a new process (thread group) */
  47. __asm__ volatile ("movq %%rbp, %0\r\n"
  48. : "=r"(new_thread->frameptr));
  49. size_t stack_size = 4096;
  50. if (new_thread->frameptr <= cur_thread->stack_top &&
  51. new_thread->frameptr > cur_thread->stack)
  52. stack_size = cur_thread->stack_top - new_thread->frameptr;
  53. void * dummy_stack = system_malloc(stack_size);
  54. if (!dummy_stack) {
  55. debug("creation of stack failed\n");
  56. put_thread(new_thread);
  57. return -PAL_ERRNO;
  58. }
  59. memcpy(dummy_stack, new_thread->frameptr, stack_size);
  60. /* assigned the stack of the thread */
  61. lock(&cur_thread->lock);
  62. new_thread->tgid = new_thread->tid;
  63. new_thread->in_vm = true;
  64. new_thread->is_alive = true;
  65. new_thread->stack = cur_thread->stack;
  66. new_thread->stack_top = cur_thread->stack_top;
  67. new_thread->tcb = cur_thread->tcb;
  68. new_thread->user_tcb = cur_thread->user_tcb;
  69. cur_thread->stack = dummy_stack;
  70. cur_thread->stack_top = dummy_stack + stack_size;
  71. cur_thread->frameptr = NULL;
  72. unlock(&cur_thread->lock);
  73. /* Now we are good, set this child as ours */
  74. set_as_child(NULL, new_thread);
  75. /* add the child to the global list */
  76. add_thread(new_thread);
  77. new_thread->dummy = cur_thread;
  78. struct shim_handle_map * handle_map = get_cur_handle_map(cur_thread);
  79. /* pop the ref count of current handle map to prevent revocation */
  80. get_handle_map(handle_map);
  81. struct shim_handle_map * new_map = NULL;
  82. /* duplicate handle map intp a new handle map */
  83. dup_handle_map(&new_map, handle_map);
  84. /* set the new handle map to new thread */
  85. set_handle_map(new_thread, new_map);
  86. /* push back the ref count of handle map */
  87. put_handle_map(handle_map);
  88. /* we have the thread handle from PAL, now set it to the child */
  89. new_thread->pal_handle = cur_thread->pal_handle;
  90. /* set the current thread running */
  91. set_cur_thread(new_thread);
  92. put_thread(new_thread);
  93. /* here we return immediately, no letting the hooks mes up our stack */
  94. return 0;
  95. }