shim_migrate.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  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_migrate.c
  17. *
  18. * Implementation of system call "checkpoint" and "restore".
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_table.h>
  22. #include <shim_thread.h>
  23. #include <shim_handle.h>
  24. #include <shim_vma.h>
  25. #include <shim_fs.h>
  26. #include <shim_ipc.h>
  27. #include <shim_checkpoint.h>
  28. #include <pal.h>
  29. #include <pal_error.h>
  30. #include <errno.h>
  31. #include <linux/fcntl.h>
  32. #include <asm/mman.h>
  33. /* cp_session objects are on the cp_sessions list, by the list field */
  34. /* cp_threads are organized onto a list, handing off of the
  35. * cp_session->registered_threads list. */
  36. DEFINE_LIST(cp_thread);
  37. struct cp_thread {
  38. struct shim_thread * thread;
  39. LIST_TYPE(cp_thread) list;
  40. };
  41. DEFINE_LIST(cp_session);
  42. DEFINE_LISTP(cp_thread);
  43. struct cp_session {
  44. IDTYPE sid;
  45. struct shim_handle * cpfile;
  46. LISTP_TYPE(cp_thread) registered_threads;
  47. LIST_TYPE(cp_session) list;
  48. PAL_HANDLE finish_event;
  49. struct shim_cp_store cpstore;
  50. };
  51. DEFINE_LISTP(cp_session);
  52. LISTP_TYPE(cp_session) cp_sessions;
  53. int create_checkpoint (const char * cpdir, IDTYPE * sid)
  54. {
  55. struct cp_session * cpsession = malloc(sizeof(struct cp_session));
  56. if (!cpsession)
  57. return -ENOMEM;
  58. int ret = 0;
  59. INIT_LISTP(&cpsession->registered_threads);
  60. INIT_LIST_HEAD(cpsession, list);
  61. cpsession->finish_event = DkNotificationEventCreate(PAL_FALSE);
  62. cpsession->cpfile = NULL;
  63. int len = strlen(cpdir);
  64. char * filename = __alloca(len + 10);
  65. memcpy(filename, cpdir, len);
  66. filename[len] = '/';
  67. snprintf(filename + len + 1, 9, "%08x", cur_process.vmid);
  68. cpsession->cpfile = get_new_handle();
  69. if (!cpsession->cpfile) {
  70. ret = -ENOMEM;
  71. goto err;
  72. }
  73. /* the directory might not be created. At least try to create it */
  74. if ((ret = open_namei(NULL, NULL, cpdir, O_CREAT|O_DIRECTORY, 0700,
  75. NULL)) < 0
  76. && ret != -EEXIST)
  77. goto err;
  78. if ((ret = open_namei(cpsession->cpfile, NULL, filename,
  79. O_CREAT|O_EXCL|O_RDWR, 0600, NULL)) < 0)
  80. goto err;
  81. open_handle(cpsession->cpfile);
  82. MASTER_LOCK();
  83. struct cp_session * s;
  84. if (*sid) {
  85. listp_for_each_entry(s, &cp_sessions, list)
  86. if (s->sid == *sid) {
  87. ret = 0;
  88. goto err_locked;
  89. }
  90. } else {
  91. retry:
  92. ret = DkRandomBitsRead(&cpsession->sid, sizeof(cpsession->sid));
  93. if (ret < 0) {
  94. ret = -convert_pal_errno(-ret);
  95. goto err_locked;
  96. }
  97. listp_for_each_entry(s, &cp_sessions, list)
  98. if (s->sid == cpsession->sid)
  99. goto retry;
  100. *sid = cpsession->sid;
  101. }
  102. listp_add_tail(cpsession, &cp_sessions, list);
  103. MASTER_UNLOCK();
  104. return 0;
  105. err_locked:
  106. MASTER_UNLOCK();
  107. err:
  108. if (cpsession->cpfile)
  109. close_handle(cpsession->cpfile);
  110. DkObjectClose(cpsession->finish_event);
  111. free(cpsession);
  112. return ret;
  113. }
  114. static int finish_checkpoint (struct cp_session * session);
  115. static int check_thread (struct shim_thread * thread, void * arg,
  116. bool * unlocked)
  117. {
  118. LISTP_TYPE(cp_thread) * registered = (LISTP_TYPE(cp_thread) *) arg;
  119. struct cp_thread * t;
  120. if (!thread->in_vm || !thread->is_alive)
  121. return 0;
  122. listp_for_each_entry(t, registered, list)
  123. if (t->thread == thread)
  124. return 0;
  125. return 1;
  126. }
  127. int join_checkpoint (struct shim_thread * thread, ucontext_t * context,
  128. IDTYPE sid)
  129. {
  130. struct cp_session * s, * cpsession = NULL;
  131. struct cp_thread cpthread;
  132. int ret = 0;
  133. bool do_checkpoint = false;
  134. MASTER_LOCK();
  135. listp_for_each_entry(s, &cp_sessions, list)
  136. if (s->sid == sid) {
  137. cpsession = s;
  138. break;
  139. }
  140. if (!cpsession) {
  141. MASTER_UNLOCK();
  142. return -EINVAL;
  143. }
  144. INIT_LIST_HEAD(&cpthread, list);
  145. cpthread.thread = thread;
  146. listp_add_tail(&cpthread, &cpsession->registered_threads, list);
  147. /* find out if there is any thread that is not registered yet */
  148. ret = walk_thread_list(&check_thread,
  149. &cpsession->registered_threads,
  150. false);
  151. if (ret == -ESRCH)
  152. do_checkpoint = true;
  153. PAL_HANDLE finish_event = cpsession->finish_event;
  154. MASTER_UNLOCK();
  155. if (!do_checkpoint) {
  156. debug("waiting for checkpointing\n");
  157. object_wait_with_retry(finish_event);
  158. return 0;
  159. }
  160. debug("ready for checkpointing\n");
  161. ret = finish_checkpoint(cpsession);
  162. if (ret < 0)
  163. debug("failed creating checkpoint\n");
  164. else
  165. debug("finish checkpointing, time to wake up all threads\n");
  166. DkEventSet(finish_event);
  167. return ret;
  168. }
  169. static void * file_alloc (struct shim_cp_store * store, void * addr, size_t size)
  170. {
  171. assert(store->cp_file);
  172. struct shim_mount * fs = store->cp_file->fs;
  173. if (!fs || !fs->fs_ops ||
  174. !fs->fs_ops->truncate || !fs->fs_ops->mmap)
  175. return NULL;
  176. if (fs->fs_ops->truncate(store->cp_file, size) < 0)
  177. return NULL;
  178. if (fs->fs_ops->mmap(store->cp_file, &addr, size,
  179. PROT_READ|PROT_WRITE,
  180. MAP_FILE|MAP_SHARED, 0) < 0)
  181. return NULL;
  182. return addr;
  183. }
  184. static int finish_checkpoint (struct cp_session * cpsession)
  185. {
  186. struct shim_cp_store * cpstore = &cpsession->cpstore;
  187. int ret;
  188. cpstore->alloc = file_alloc;
  189. BEGIN_MIGRATION_DEF(checkpoint)
  190. {
  191. DEFINE_MIGRATE(process, &cur_process, sizeof(struct shim_process));
  192. DEFINE_MIGRATE(all_mounts, NULL, 0);
  193. DEFINE_MIGRATE(all_vmas, NULL, 0);
  194. DEFINE_MIGRATE(all_running_threads, NULL, 0);
  195. DEFINE_MIGRATE(brk, NULL, 0);
  196. DEFINE_MIGRATE(loaded_libraries, NULL, 0);
  197. #ifdef DEBUG
  198. DEFINE_MIGRATE(gdb_map, NULL, 0);
  199. #endif
  200. DEFINE_MIGRATE(migratable, NULL, 0);
  201. }
  202. END_MIGRATION_DEF(checkpoint)
  203. if ((ret = START_MIGRATE(cpstore, checkpoint)) < 0)
  204. return ret;
  205. struct cp_header * hdr = (struct cp_header *) cpstore->base;
  206. hdr->addr = (void *) cpstore->base;
  207. hdr->size = cpstore->offset;
  208. DkStreamUnmap((void *) cpstore->base, cpstore->bound);
  209. close_handle(cpstore->cp_file);
  210. return 0;
  211. }
  212. int shim_do_checkpoint (const char * filename)
  213. {
  214. IDTYPE session = 0;
  215. int ret = 0;
  216. ret = shim_do_mkdir(filename, 0700);
  217. if (ret < 0)
  218. return ret;
  219. shim_tcb_t * tcb = shim_get_tls();
  220. assert(tcb && tcb->tp);
  221. struct shim_signal signal;
  222. __store_context(tcb, NULL, &signal);
  223. ret = create_checkpoint(filename, &session);
  224. if (ret < 0) {
  225. shim_do_rmdir(filename);
  226. return ret;
  227. }
  228. ipc_checkpoint_send(filename, session);
  229. kill_all_threads(tcb->tp, session, SIGCP);
  230. ret = join_checkpoint(tcb->tp, &signal.context, session);
  231. if (ret < 0) {
  232. shim_do_rmdir(filename);
  233. return ret;
  234. }
  235. return 0;
  236. }