shim_checkpoint.h 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429
  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_checkpoints.c
  15. *
  16. * This file contains definitions and macros for checkpointing method.
  17. */
  18. #ifndef _SHIM_CHECKPOINT_H_
  19. #define _SHIM_CHECKPOINT_H_
  20. #include <stdarg.h>
  21. #include <pal.h>
  22. #include <shim_defs.h>
  23. #include <shim_ipc.h>
  24. #include <shim_profile.h>
  25. #ifdef __i386__
  26. #error "x86-32 support is heavily broken."
  27. #endif
  28. typedef uint64_t ptr_t;
  29. #define hashfunc hash64
  30. #define __attribute_migratable __attribute__((section(".migratable")))
  31. extern char __migratable;
  32. extern char __migratable_end;
  33. /* TSAI 7/11/2012:
  34. The checkpoint scheme we are expecting is to support an easy syntax to
  35. implement migration procedure. A migration procedure can be written
  36. in the following syntax:
  37. BEGIN_CP_DEFINITION(exec)
  38. {
  39. DEFINE_CP(thread, ...);
  40. DEFINE_CP(handle_map, ...);
  41. }
  42. void * checkpoint = DO_CHECKPOINT(exec);
  43. The structure of checkpoint data will be a counting-down stack-like
  44. memory segment, with enough space reserved below for 1. in case the
  45. dry run miscalculate the checkpoint size or 2. stack use for the new
  46. thread.
  47. Below is the figure for our checkpoint structure:
  48. Low Bytes -------------------------------------------------
  49. checkpoint_entry[0]
  50. data section for checkpoint 0
  51. checkpoint_entry[1]
  52. data section for checkpoint 1
  53. checkpoint_entry[2]
  54. ...
  55. checkpoint_entry[n] CP_NULL
  56. High Bytes ------------------------------------------------
  57. */
  58. struct shim_cp_entry {
  59. ptr_t cp_type; /* entry type */
  60. union {
  61. ptr_t cp_val; /* integer value */
  62. /* originally there is a pointer, now we don't need them */
  63. } cp_un;
  64. };
  65. struct shim_mem_entry {
  66. struct shim_mem_entry* prev;
  67. void* addr;
  68. size_t size;
  69. void** paddr;
  70. int prot;
  71. void* data;
  72. };
  73. struct shim_palhdl_entry {
  74. struct shim_palhdl_entry* prev;
  75. PAL_HANDLE handle;
  76. struct shim_qstr* uri;
  77. PAL_HANDLE* phandle;
  78. };
  79. struct shim_cp_store {
  80. /* checkpoint data mapping */
  81. void* cp_map;
  82. struct shim_handle* cp_file;
  83. /* allocation method for check point area */
  84. void* (*alloc)(struct shim_cp_store*, void*, size_t);
  85. /* check point area */
  86. ptr_t base, offset, bound;
  87. /* entries of out-of-band data */
  88. struct shim_mem_entry* last_mem_entry;
  89. int mem_nentries;
  90. size_t mem_size;
  91. /* entries of pal handles to send */
  92. struct shim_palhdl_entry* last_palhdl_entry;
  93. int palhdl_nentries;
  94. };
  95. #define CP_FUNC_ARGS struct shim_cp_store* store, void* obj, size_t size, void** objp
  96. #define RS_FUNC_ARGS struct shim_cp_entry* entry, ptr_t base, ptr_t* offset, long rebase
  97. #define DEFINE_CP_FUNC(name) int cp_##name(CP_FUNC_ARGS)
  98. #define DEFINE_RS_FUNC(name) int rs_##name(RS_FUNC_ARGS)
  99. typedef int (*cp_func)(CP_FUNC_ARGS);
  100. typedef int (*rs_func)(RS_FUNC_ARGS);
  101. extern const char* __cp_name;
  102. extern const cp_func __cp_func;
  103. extern const rs_func __rs_func;
  104. enum {
  105. CP_NULL = 0,
  106. CP_IGNORE,
  107. CP_OOB,
  108. CP_ADDR,
  109. CP_SIZE,
  110. CP_FUNC_BASE,
  111. };
  112. #define CP_FUNC_INDEX(name) \
  113. ({ \
  114. extern const cp_func cp_func_##name; \
  115. &cp_func_##name - &__cp_func; \
  116. })
  117. #define CP_FUNC(name) (CP_FUNC_BASE + CP_FUNC_INDEX(name))
  118. #define CP_FUNC_NAME(type) ((&__cp_name)[(type) - CP_FUNC_BASE])
  119. #define __ADD_CP_OFFSET(size) \
  120. ({ \
  121. ptr_t _off = store->offset; \
  122. if (store->offset + (size) > store->bound) { \
  123. ptr_t new_bound = store->bound * 2; \
  124. \
  125. while (store->offset + (size) > new_bound) \
  126. new_bound *= 2; \
  127. \
  128. void* buf = \
  129. store->alloc(store, (void*)store->base + store->bound, new_bound - store->bound); \
  130. if (!buf) \
  131. return -ENOMEM; \
  132. \
  133. store->bound = new_bound; \
  134. } \
  135. store->offset += (size); \
  136. _off; \
  137. })
  138. #define ADD_CP_ENTRY(type, value) \
  139. ({ \
  140. struct shim_cp_entry* tmp = (void*)base + __ADD_CP_OFFSET(sizeof(struct shim_cp_entry)); \
  141. tmp->cp_type = CP_##type; \
  142. tmp->cp_un.cp_val = (ptr_t)(value); \
  143. if (DEBUG_CHECKPOINT) \
  144. debug("ADD CP_" #type "(0x%08lx) >%ld\n", tmp->cp_un.cp_val, store->offset); \
  145. tmp; \
  146. })
  147. #define ADD_CP_OFFSET(size) \
  148. ({ \
  149. size_t _size = ALIGN_UP(size, sizeof(void*)); \
  150. struct shim_cp_entry* oob = (void*)base + __ADD_CP_OFFSET(sizeof(struct shim_cp_entry)); \
  151. oob->cp_type = CP_OOB; \
  152. oob->cp_un.cp_val = (ptr_t)_size; \
  153. ptr_t _off = (ptr_t)__ADD_CP_OFFSET(_size); \
  154. if (DEBUG_CHECKPOINT) \
  155. debug("ADD OFFSET(%lu) >%ld\n", size, store->offset); \
  156. _off; \
  157. })
  158. #define ADD_CP_FUNC_ENTRY(value) \
  159. ({ \
  160. struct shim_cp_entry* tmp = (void*)base + __ADD_CP_OFFSET(sizeof(struct shim_cp_entry)); \
  161. tmp->cp_type = CP_FUNC_TYPE; \
  162. tmp->cp_un.cp_val = (ptr_t)(value); \
  163. if (DEBUG_CHECKPOINT) \
  164. debug("ADD %s(0x%08lx) >%ld\n", CP_FUNC_NAME, value, store->offset); \
  165. tmp; \
  166. })
  167. #define NEXT_CP_ENTRY() \
  168. ({ \
  169. struct shim_cp_entry* tmp; \
  170. while (1) { \
  171. tmp = (void*)base + *offset; \
  172. if (tmp->cp_type == CP_NULL) { \
  173. tmp = NULL; \
  174. break; \
  175. } \
  176. *offset += sizeof(struct shim_cp_entry); \
  177. if (tmp->cp_type == CP_OOB) \
  178. *offset += tmp->cp_un.cp_val; \
  179. else \
  180. break; \
  181. } \
  182. tmp; \
  183. })
  184. #define GET_CP_ENTRY(type) \
  185. ({ \
  186. struct shim_cp_entry* tmp = NEXT_CP_ENTRY(); \
  187. \
  188. while (tmp->cp_type != CP_##type) \
  189. tmp = NEXT_CP_ENTRY(); \
  190. \
  191. /* debug("GET CP_" #type "(%p)\n",tmp->cp_un.cp_val); */ \
  192. tmp->cp_un.cp_val; \
  193. })
  194. #define GET_CP_FUNC_ENTRY() \
  195. ({ \
  196. /* debug("GET CP_FUNC_%s(%p) :%d\n", CP_FUNC_NAME, entry->cp_un.cp_val); */ \
  197. entry->cp_un.cp_val; \
  198. })
  199. #define BEGIN_CP_FUNC(name) \
  200. const char* cp_name_##name __attribute__((section(".cp_name." #name))) = #name; \
  201. extern DEFINE_CP_FUNC(name); \
  202. extern DEFINE_RS_FUNC(name); \
  203. const cp_func cp_func_##name __attribute__((section(".cp_func." #name))) = &cp_##name; \
  204. const rs_func rs_func_##name __attribute__((section(".rs_func." #name))) = &rs_##name; \
  205. \
  206. DEFINE_PROFILE_INTERVAL(cp_##name, checkpoint_func); \
  207. DEFINE_PROFILE_INTERVAL(rs_##name, resume_func); \
  208. \
  209. DEFINE_CP_FUNC(name) { \
  210. int CP_FUNC_TYPE __attribute__((unused)) = CP_FUNC(name); \
  211. const char* CP_FUNC_NAME __attribute__((unused)) = #name; \
  212. ptr_t base __attribute__((unused)) = store->base; \
  213. BEGIN_PROFILE_INTERVAL(); \
  214. ASSIGN_PROFILE_INTERVAL(cp_##name);
  215. #define END_CP_FUNC(name) \
  216. SAVE_PROFILE_INTERVAL_ASSIGNED(); \
  217. return 0; \
  218. }
  219. #define END_CP_FUNC_NO_RS(name) \
  220. END_CP_FUNC(name) \
  221. BEGIN_RS_FUNC(name) { \
  222. __UNUSED(entry); \
  223. __UNUSED(base); \
  224. __UNUSED(offset); \
  225. __UNUSED(rebase); \
  226. } \
  227. END_RS_FUNC(name)
  228. #define BEGIN_RS_FUNC(name) \
  229. DEFINE_RS_FUNC(name) { \
  230. int CP_FUNC_TYPE __attribute__((unused)) = CP_FUNC(name); \
  231. const char* CP_FUNC_NAME __attribute__((unused)) = #name; \
  232. BEGIN_PROFILE_INTERVAL(); \
  233. ASSIGN_PROFILE_INTERVAL(rs_##name);
  234. #define END_RS_FUNC(name) \
  235. SAVE_PROFILE_INTERVAL_ASSIGNED(); \
  236. return 0; \
  237. }
  238. #define CP_REBASE(obj) \
  239. do { \
  240. void* _ptr = &(obj); \
  241. size_t _size = sizeof(obj); \
  242. void** _p; \
  243. for (_p = _ptr; _p < (void**)(_ptr + _size); _p++) \
  244. if (*_p) \
  245. *_p += rebase; \
  246. } while (0)
  247. #define DO_CP_SIZE(name, obj, size, objp) \
  248. do { \
  249. extern DEFINE_CP_FUNC(name); \
  250. int ret = cp_##name(store, obj, size, (void**)(objp)); \
  251. if (ret < 0) \
  252. return ret; \
  253. } while (0)
  254. #define DO_CP(name, obj, objp) DO_CP_SIZE(name, obj, sizeof(*(obj)), objp)
  255. #define DO_CP_MEMBER(name, obj, newobj, member) DO_CP(name, (obj)->member, &((newobj)->member));
  256. #define DO_CP_IN_MEMBER(name, obj, member) DO_CP(name, &((obj)->member), NULL)
  257. struct shim_cp_map_entry {
  258. void* addr;
  259. ptr_t off;
  260. };
  261. void* create_cp_map(void);
  262. void destroy_cp_map(void* map);
  263. struct shim_cp_map_entry* get_cp_map_entry(void* map, void* addr, bool create);
  264. #define GET_FROM_CP_MAP(obj) \
  265. ({ \
  266. struct shim_cp_map_entry* e = get_cp_map_entry(store->cp_map, obj, false); \
  267. e ? e->off : 0; \
  268. })
  269. #define ADD_TO_CP_MAP(obj, off) \
  270. do { \
  271. struct shim_cp_map_entry* e = get_cp_map_entry(store->cp_map, obj, true); \
  272. e->off = (off); \
  273. } while (0)
  274. #define BEGIN_MIGRATION_DEF(name, ...) \
  275. int migrate_##name(struct shim_cp_store* store, ##__VA_ARGS__) { \
  276. int ret = 0; \
  277. ptr_t base = store->base;
  278. #define END_MIGRATION_DEF(name) \
  279. ADD_CP_ENTRY(NULL, 0); \
  280. return 0; \
  281. }
  282. #define DEFINE_MIGRATE(name, obj, size) \
  283. do { \
  284. extern DEFINE_CP_FUNC(name); \
  285. if ((ret = cp_##name(store, obj, size, NULL)) < 0) \
  286. return ret; \
  287. } while (0)
  288. #define DEBUG_RESUME 0
  289. #define DEBUG_CHECKPOINT 0
  290. #if DEBUG_RESUME == 1
  291. #define DEBUG_RS(fmt, ...) \
  292. debug("GET %s(0x%08lx): " fmt "\n", CP_FUNC_NAME, entry->cp_un.cp_val, ##__VA_ARGS__)
  293. #else
  294. #define DEBUG_RS(...) do {} while (0)
  295. #endif
  296. #include <shim_profile.h>
  297. #define START_MIGRATE(store, name, ...) \
  298. ({ \
  299. int ret = 0; \
  300. do { \
  301. BEGIN_PROFILE_INTERVAL(); \
  302. \
  303. if (!((store)->cp_map = create_cp_map())) { \
  304. ret = -ENOMEM; \
  305. goto out; \
  306. } \
  307. SAVE_PROFILE_INTERVAL(checkpoint_create_map); \
  308. \
  309. ret = migrate_##name(store, ##__VA_ARGS__); \
  310. if (ret < 0) \
  311. goto out; \
  312. \
  313. SAVE_PROFILE_INTERVAL(checkpoint_copy); \
  314. ADD_PROFILE_OCCURENCE(checkpoint_total_size, (store)->offset); \
  315. INC_PROFILE_OCCURENCE(checkpoint_count); \
  316. \
  317. debug("complete checkpointing data\n"); \
  318. out: \
  319. destroy_cp_map((store)->cp_map); \
  320. SAVE_PROFILE_INTERVAL(checkpoint_destroy_map); \
  321. } while (0); \
  322. ret; \
  323. })
  324. struct newproc_cp_header {
  325. struct cp_header {
  326. unsigned long size;
  327. void* addr;
  328. unsigned long offset;
  329. } hdr;
  330. struct mem_header {
  331. unsigned long entoffset;
  332. int nentries;
  333. } mem;
  334. struct palhdl_header {
  335. unsigned long entoffset;
  336. int nentries;
  337. } palhdl;
  338. };
  339. struct newproc_header {
  340. struct newproc_cp_header checkpoint;
  341. int failure;
  342. #ifdef PROFILE
  343. unsigned long begin_create_time;
  344. unsigned long create_time;
  345. unsigned long write_proc_time;
  346. #endif
  347. };
  348. struct newproc_response {
  349. IDTYPE child_vmid;
  350. int failure;
  351. };
  352. int do_migration(struct newproc_cp_header* hdr, void** cpptr);
  353. int restore_checkpoint(struct cp_header* cphdr, struct mem_header* memhdr, ptr_t base, ptr_t type);
  354. int do_migrate_process(int (*migrate)(struct shim_cp_store*, struct shim_thread*,
  355. struct shim_process*, va_list),
  356. struct shim_handle* exec, const char** argv, struct shim_thread* thread,
  357. ...);
  358. int init_from_checkpoint_file(const char* filename, struct newproc_cp_header* hdr, void** cpptr);
  359. int restore_from_file(const char* filename, struct newproc_cp_header* hdr, void** cpptr);
  360. void restore_context(struct shim_context* context);
  361. int create_checkpoint(const char* cpdir, IDTYPE* session);
  362. int join_checkpoint(struct shim_thread* cur, IDTYPE sid);
  363. #endif /* _SHIM_CHECKPOINT_H_ */