shim_fork.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  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_fork.c
  17. *
  18. * Implementation of system call "fork".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <shim_ipc.h>
  24. #include <shim_profile.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. #include <sys/syscall.h>
  29. #include <sys/mman.h>
  30. #include <asm/prctl.h>
  31. #include <linux/futex.h>
  32. static void * __malloc (size_t size)
  33. {
  34. int flags = MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL;
  35. size = ALIGN_UP(size);
  36. void * addr = get_unmapped_vma(size, flags);
  37. addr = DkVirtualMemoryAlloc(addr, size, 0, PAL_PROT_READ|PAL_PROT_WRITE);
  38. if (addr)
  39. bkeep_mmap(addr, size, PROT_READ|PROT_WRITE, flags, NULL, 0, NULL);
  40. return addr;
  41. }
  42. #define malloc_method __malloc
  43. #include <shim_checkpoint.h>
  44. int migrate_fork (struct shim_cp_store * cpstore,
  45. struct shim_process * process,
  46. struct shim_thread * thread, va_list ap)
  47. {
  48. BEGIN_MIGRATION_DEF(fork, struct shim_process * proc,
  49. struct shim_thread * thread)
  50. {
  51. DEFINE_MIGRATE(process, proc, sizeof(struct shim_process), false);
  52. DEFINE_MIGRATE(all_mounts, NULL, 0, false);
  53. DEFINE_MIGRATE(all_vmas, NULL, 0, true); /* recusive for the data */
  54. DEFINE_MIGRATE(running_thread, thread, sizeof(struct shim_thread),
  55. true); /* recusive for the stack */
  56. DEFINE_MIGRATE(handle_map, thread->handle_map,
  57. sizeof (struct shim_handle_map), true);
  58. /* recursive for the handles */
  59. DEFINE_MIGRATE(brk, NULL, 0, false);
  60. DEFINE_MIGRATE(loaded_libraries, NULL, 0, false);
  61. DEFINE_MIGRATE(gdb_map, NULL, 0, false);
  62. DEFINE_MIGRATE(migratable, NULL, 0, false);
  63. }
  64. END_MIGRATION_DEF
  65. int ret = START_MIGRATE(cpstore, fork, 0, process, thread);
  66. thread->in_vm = false;
  67. if (thread->exec) {
  68. put_handle(thread->exec);
  69. thread->exec = NULL;
  70. }
  71. return ret;
  72. }
  73. int shim_do_fork (void)
  74. {
  75. int ret = 0;
  76. INC_PROFILE_OCCURENCE(syscall_use_ipc);
  77. if ((ret = prepare_ns_leaders()) < 0)
  78. return ret;
  79. struct shim_thread * cur_thread = get_cur_thread();
  80. struct shim_thread * new_thread = get_new_thread(0);
  81. if (!new_thread)
  82. return -ENOMEM;
  83. new_thread->tcb = cur_thread->tcb;
  84. new_thread->user_tcb = cur_thread->user_tcb;
  85. new_thread->tgid = new_thread->tid;
  86. new_thread->in_vm = false;
  87. new_thread->is_alive = true;
  88. add_thread(new_thread);
  89. set_as_child(cur_thread, new_thread);
  90. if ((ret = do_migrate_process(&migrate_fork, NULL, NULL, new_thread)) < 0) {
  91. put_thread(new_thread);
  92. return ret;
  93. }
  94. lock(new_thread->lock);
  95. struct shim_handle_map * handle_map = new_thread->handle_map;
  96. new_thread->handle_map = NULL;
  97. unlock(new_thread->lock);
  98. if (handle_map)
  99. put_handle_map(handle_map);
  100. IDTYPE tid = new_thread->tid;
  101. put_thread(new_thread);
  102. return tid;
  103. }