shim_vfork.c 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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 <asm/prctl.h>
  19. #include <errno.h>
  20. #include <linux/futex.h>
  21. #include <pal.h>
  22. #include <pal_error.h>
  23. #include <shim_checkpoint.h>
  24. #include <shim_internal.h>
  25. #include <shim_table.h>
  26. #include <shim_thread.h>
  27. #include <shim_utils.h>
  28. #include <sys/mman.h>
  29. #include <sys/syscall.h>
  30. int shim_do_vfork(void) {
  31. #ifdef ALIAS_VFORK_AS_FORK
  32. debug("vfork() is an alias to fork() in Graphene, calling fork() now\n");
  33. return shim_do_fork();
  34. #else
  35. /* NOTE: leaving this old implementation for historical reference */
  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" : "=r"(new_thread->frameptr));
  48. size_t stack_size = 4096;
  49. if (new_thread->frameptr <= cur_thread->stack_top && new_thread->frameptr > cur_thread->stack)
  50. stack_size = cur_thread->stack_top - new_thread->frameptr;
  51. void* dummy_stack = system_malloc(stack_size);
  52. if (!dummy_stack) {
  53. debug("creation of stack failed\n");
  54. put_thread(new_thread);
  55. return -PAL_ERRNO;
  56. }
  57. memcpy(dummy_stack, new_thread->frameptr, stack_size);
  58. /* assigned the stack of the thread */
  59. lock(&cur_thread->lock);
  60. new_thread->tgid = new_thread->tid;
  61. new_thread->in_vm = true;
  62. new_thread->is_alive = true;
  63. new_thread->stack = cur_thread->stack;
  64. new_thread->stack_top = cur_thread->stack_top;
  65. new_thread->tcb = cur_thread->tcb;
  66. new_thread->user_tcb = cur_thread->user_tcb;
  67. cur_thread->stack = dummy_stack;
  68. cur_thread->stack_top = dummy_stack + stack_size;
  69. cur_thread->frameptr = NULL;
  70. unlock(&cur_thread->lock);
  71. /* Now we are good, set this child as ours */
  72. set_as_child(NULL, new_thread);
  73. /* add the child to the global list */
  74. add_thread(new_thread);
  75. new_thread->dummy = cur_thread;
  76. struct shim_handle_map* handle_map = get_cur_handle_map(cur_thread);
  77. /* pop the ref count of current handle map to prevent revocation */
  78. get_handle_map(handle_map);
  79. struct shim_handle_map* new_map = NULL;
  80. /* duplicate handle map intp a new handle map */
  81. dup_handle_map(&new_map, handle_map);
  82. /* set the new handle map to new thread */
  83. set_handle_map(new_thread, new_map);
  84. /* push back the ref count of handle map */
  85. put_handle_map(handle_map);
  86. /* we have the thread handle from PAL, now set it to the child */
  87. new_thread->pal_handle = cur_thread->pal_handle;
  88. /* set the current thread running */
  89. set_cur_thread(new_thread);
  90. put_thread(new_thread);
  91. /* here we return immediately, no letting the hooks mes up our stack */
  92. return 0;
  93. #endif
  94. }