shim_migrate.c 7.6 KB

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