shim_vfork.c 3.9 KB

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