shim_migrate.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  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. getrand(&cpsession->sid, sizeof(IDTYPE));
  93. listp_for_each_entry(s, &cp_sessions, list)
  94. if (s->sid == cpsession->sid)
  95. goto retry;
  96. *sid = cpsession->sid;
  97. }
  98. listp_add_tail(cpsession, &cp_sessions, list);
  99. master_unlock();
  100. return 0;
  101. err_locked:
  102. master_unlock();
  103. err:
  104. if (cpsession->cpfile)
  105. close_handle(cpsession->cpfile);
  106. DkObjectClose(cpsession->finish_event);
  107. free(cpsession);
  108. return ret;
  109. }
  110. static int finish_checkpoint (struct cp_session * session);
  111. static int check_thread (struct shim_thread * thread, void * arg,
  112. bool * unlocked)
  113. {
  114. LISTP_TYPE(cp_thread) * registered = (LISTP_TYPE(cp_thread) *) arg;
  115. struct cp_thread * t;
  116. if (!thread->in_vm || !thread->is_alive)
  117. return 0;
  118. listp_for_each_entry(t, registered, list)
  119. if (t->thread == thread)
  120. return 0;
  121. return 1;
  122. }
  123. int join_checkpoint (struct shim_thread * thread, ucontext_t * context,
  124. IDTYPE sid)
  125. {
  126. struct cp_session * s, * cpsession = NULL;
  127. struct cp_thread cpthread;
  128. int ret = 0;
  129. bool do_checkpoint = false;
  130. master_lock();
  131. listp_for_each_entry(s, &cp_sessions, list)
  132. if (s->sid == sid) {
  133. cpsession = s;
  134. break;
  135. }
  136. if (!cpsession) {
  137. master_unlock();
  138. return -EINVAL;
  139. }
  140. INIT_LIST_HEAD(&cpthread, list);
  141. cpthread.thread = thread;
  142. listp_add_tail(&cpthread, &cpsession->registered_threads, list);
  143. /* find out if there is any thread that is not registered yet */
  144. ret = walk_thread_list(&check_thread,
  145. &cpsession->registered_threads,
  146. false);
  147. if (ret == -ESRCH)
  148. do_checkpoint = true;
  149. PAL_HANDLE finish_event = cpsession->finish_event;
  150. master_unlock();
  151. if (!do_checkpoint) {
  152. debug("waiting for checkpointing\n");
  153. DkObjectsWaitAny(1, &finish_event, NO_TIMEOUT);
  154. return 0;
  155. }
  156. debug("ready for checkpointing\n");
  157. ret = finish_checkpoint(cpsession);
  158. if (ret < 0)
  159. debug("failed creating checkpoint\n");
  160. else
  161. debug("finish checkpointing, time to wake up all threads\n");
  162. DkEventSet(finish_event);
  163. return ret;
  164. }
  165. static void * file_alloc (struct shim_cp_store * store, void * addr, int size)
  166. {
  167. assert(store->cp_file);
  168. struct shim_mount * fs = store->cp_file->fs;
  169. if (!fs || !fs->fs_ops ||
  170. !fs->fs_ops->truncate || !fs->fs_ops->mmap)
  171. return NULL;
  172. if (fs->fs_ops->truncate(store->cp_file, size) < 0)
  173. return NULL;
  174. if (fs->fs_ops->mmap(store->cp_file, &addr, size,
  175. PROT_READ|PROT_WRITE,
  176. MAP_FILE|MAP_SHARED, 0) < 0)
  177. return NULL;
  178. return addr;
  179. }
  180. static int finish_checkpoint (struct cp_session * cpsession)
  181. {
  182. struct shim_cp_store * cpstore = &cpsession->cpstore;
  183. int ret;
  184. cpstore->alloc = file_alloc;
  185. BEGIN_MIGRATION_DEF(checkpoint)
  186. {
  187. DEFINE_MIGRATE(process, &cur_process, sizeof(struct shim_process));
  188. DEFINE_MIGRATE(all_mounts, NULL, 0);
  189. DEFINE_MIGRATE(all_vmas, NULL, 0);
  190. DEFINE_MIGRATE(all_running_threads, NULL, 0);
  191. DEFINE_MIGRATE(brk, NULL, 0);
  192. DEFINE_MIGRATE(loaded_libraries, NULL, 0);
  193. #ifdef DEBUG
  194. DEFINE_MIGRATE(gdb_map, NULL, 0);
  195. #endif
  196. DEFINE_MIGRATE(migratable, NULL, 0);
  197. }
  198. END_MIGRATION_DEF(checkpoint)
  199. if ((ret = START_MIGRATE(cpstore, checkpoint)) < 0)
  200. return ret;
  201. struct cp_header * hdr = (struct cp_header *) cpstore->base;
  202. hdr->addr = (void *) cpstore->base;
  203. hdr->size = cpstore->offset;
  204. DkStreamUnmap((void *) cpstore->base, cpstore->bound);
  205. close_handle(cpstore->cp_file);
  206. return 0;
  207. }
  208. int shim_do_checkpoint (const char * filename)
  209. {
  210. IDTYPE session = 0;
  211. int ret = 0;
  212. ret = shim_do_mkdir(filename, 0700);
  213. if (ret < 0)
  214. return ret;
  215. shim_tcb_t * tcb = SHIM_GET_TLS();
  216. assert(tcb && tcb->tp);
  217. struct shim_signal signal;
  218. __store_context(tcb, NULL, &signal);
  219. ret = create_checkpoint(filename, &session);
  220. if (ret < 0) {
  221. shim_do_rmdir(filename);
  222. return ret;
  223. }
  224. ipc_checkpoint_send(filename, session);
  225. kill_all_threads(tcb->tp, session, SIGCP);
  226. ret = join_checkpoint(tcb->tp, &signal.context, session);
  227. if (ret < 0) {
  228. shim_do_rmdir(filename);
  229. return ret;
  230. }
  231. return 0;
  232. }