shim_checkpoint.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183
  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_checkpoint.c
  17. *
  18. * This file contains codes for checkpoint / migration scheme of library OS.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_thread.h>
  23. #include <shim_handle.h>
  24. #include <shim_vma.h>
  25. #include <shim_fs.h>
  26. #include <shim_checkpoint.h>
  27. #include <shim_ipc.h>
  28. #include <shim_profile.h>
  29. #include <pal.h>
  30. #include <pal_error.h>
  31. #include <linux_list.h>
  32. #include <stdarg.h>
  33. #include <asm/fcntl.h>
  34. #include <asm/mman.h>
  35. DEFINE_PROFILE_CATAGORY(migrate, );
  36. DEFINE_PROFILE_CATAGORY(checkpoint, migrate);
  37. DEFINE_PROFILE_INTERVAL(checkpoint_create_map, checkpoint);
  38. DEFINE_PROFILE_INTERVAL(checkpoint_copy, checkpoint);
  39. DEFINE_PROFILE_CATAGORY(checkpoint_func, checkpoint);
  40. DEFINE_PROFILE_INTERVAL(checkpoint_destroy_map, checkpoint);
  41. DEFINE_PROFILE_OCCURENCE(checkpoint_count, checkpoint);
  42. DEFINE_PROFILE_OCCURENCE(checkpoint_total_size, checkpoint);
  43. DEFINE_PROFILE_CATAGORY(resume, migrate);
  44. DEFINE_PROFILE_INTERVAL(child_created_in_new_process, resume);
  45. DEFINE_PROFILE_INTERVAL(child_wait_header, resume);
  46. DEFINE_PROFILE_INTERVAL(child_receive_header, resume);
  47. DEFINE_PROFILE_INTERVAL(do_migration, resume);
  48. DEFINE_PROFILE_INTERVAL(child_load_checkpoint_by_gipc, resume);
  49. DEFINE_PROFILE_INTERVAL(child_load_memory_by_gipc, resume);
  50. DEFINE_PROFILE_INTERVAL(child_load_checkpoint_on_pipe, resume);
  51. DEFINE_PROFILE_INTERVAL(child_receive_handles, resume);
  52. DEFINE_PROFILE_INTERVAL(restore_checkpoint, resume);
  53. DEFINE_PROFILE_CATAGORY(resume_func, resume);
  54. DEFINE_PROFILE_INTERVAL(child_total_migration_time, resume);
  55. #define CP_HASH_SIZE 256
  56. #define CP_HASH(addr) ((hashfunc((ptr_t)(addr))) & (CP_HASH_SIZE - 1))
  57. typedef uint16_t FASTHASHTYPE;
  58. #define CP_MAP_ENTRY_NUM 64
  59. struct cp_map_entry
  60. {
  61. struct hlist_node hlist;
  62. struct shim_cp_map_entry entry;
  63. };
  64. struct cp_map {
  65. struct cp_map_buffer {
  66. struct cp_map_buffer * next;
  67. int num, cnt;
  68. struct cp_map_entry entries[0];
  69. } * buffers;
  70. struct hash_map {
  71. struct hlist_head head[CP_HASH_SIZE];
  72. } map;
  73. };
  74. void * create_cp_map (void)
  75. {
  76. void * data = malloc(sizeof(struct cp_map) + sizeof(struct cp_map_buffer) +
  77. sizeof(struct cp_map_entry) * CP_MAP_ENTRY_NUM);
  78. if (!data)
  79. return NULL;
  80. struct cp_map * map = (struct cp_map *) data;
  81. struct cp_map_buffer * buffer =
  82. (struct cp_map_buffer *) (data + sizeof(struct cp_map));
  83. memset(map, 0, sizeof(*map));
  84. map->buffers = buffer;
  85. buffer->next = NULL;
  86. buffer->num = CP_MAP_ENTRY_NUM;
  87. buffer->cnt = 0;
  88. return (void *) map;
  89. }
  90. void destroy_cp_map (void * map)
  91. {
  92. struct cp_map * m = (struct cp_map *) map;
  93. struct cp_map_buffer * buffer = m->buffers, * next;
  94. for (next = buffer ? buffer->next : NULL ;
  95. buffer && next ;
  96. buffer = next, next = next ? next->next : NULL)
  97. free(buffer);
  98. free(m);
  99. }
  100. static inline
  101. struct cp_map_buffer * extend_cp_map (struct cp_map * map)
  102. {
  103. struct cp_map_buffer * buffer =
  104. malloc(sizeof(struct cp_map_buffer) +
  105. sizeof(struct cp_map_entry) * CP_MAP_ENTRY_NUM);
  106. if (!buffer)
  107. return NULL;
  108. buffer->next = map->buffers;
  109. map->buffers = buffer;
  110. buffer->num = CP_MAP_ENTRY_NUM;
  111. buffer->cnt = 0;
  112. return buffer;
  113. }
  114. struct shim_cp_map_entry *
  115. get_cp_map_entry (void * map, void * addr, bool create)
  116. {
  117. struct cp_map * m = (struct cp_map *) map;
  118. FASTHASHTYPE hash = CP_HASH(addr);
  119. struct hlist_head * head = &m->map.head[hash];
  120. struct hlist_node * pos;
  121. struct cp_map_entry * tmp;
  122. struct shim_cp_map_entry * e = NULL;
  123. hlist_for_each_entry(tmp, pos, head, hlist)
  124. if (tmp->entry.addr == addr)
  125. e = &tmp->entry;
  126. if (create && !e) {
  127. struct cp_map_buffer * buffer = m->buffers;
  128. if (buffer->cnt == buffer->num)
  129. buffer = extend_cp_map(m);
  130. struct cp_map_entry *new = &buffer->entries[buffer->cnt++];
  131. INIT_HLIST_NODE(&new->hlist);
  132. hlist_add_head(&new->hlist, head);
  133. new->entry.addr = addr;
  134. new->entry.off = 0;
  135. e = &new->entry;
  136. }
  137. return e;
  138. }
  139. BEGIN_CP_FUNC(memory)
  140. {
  141. struct shim_mem_entry * entry =
  142. (void *) (base + ADD_CP_OFFSET(sizeof(struct shim_mem_entry)));
  143. entry->addr = obj;
  144. entry->size = size;
  145. entry->paddr = NULL;
  146. entry->prot = PAL_PROT_READ|PAL_PROT_WRITE;
  147. entry->data = NULL;
  148. entry->prev = store->last_mem_entry;
  149. store->last_mem_entry = entry;
  150. store->mem_nentries++;
  151. store->mem_size += size;
  152. if (objp)
  153. *objp = entry;
  154. }
  155. END_CP_FUNC_NO_RS(memory)
  156. BEGIN_CP_FUNC(palhdl)
  157. {
  158. ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_palhdl_entry));
  159. struct shim_palhdl_entry * entry = (void *) (base + off);
  160. entry->handle = (PAL_HANDLE) obj;
  161. entry->uri = NULL;
  162. entry->phandle = NULL;
  163. entry->prev = store->last_palhdl_entry;
  164. store->last_palhdl_entry = entry;
  165. store->palhdl_nentries++;
  166. ADD_CP_FUNC_ENTRY(off);
  167. if (objp)
  168. *objp = entry;
  169. }
  170. END_CP_FUNC(palhdl)
  171. BEGIN_RS_FUNC(palhdl)
  172. {
  173. struct shim_palhdl_entry * ent = (void *) (base + GET_CP_FUNC_ENTRY());
  174. if (ent->phandle && !ent->phandle && ent->uri) {
  175. /* XXX: reopen the stream */
  176. }
  177. }
  178. END_RS_FUNC(palhdl)
  179. BEGIN_CP_FUNC(migratable)
  180. {
  181. struct shim_mem_entry * mem_entry;
  182. DO_CP_SIZE(memory, &__migratable, &__migratable_end - &__migratable,
  183. &mem_entry);
  184. struct shim_cp_entry * entry = ADD_CP_FUNC_ENTRY(0);
  185. mem_entry->paddr = (void **) &entry->cp_un.cp_val;
  186. }
  187. END_CP_FUNC(migratable)
  188. BEGIN_RS_FUNC(migratable)
  189. {
  190. void * data = (void *) GET_CP_FUNC_ENTRY();
  191. CP_REBASE(data);
  192. memcpy(&__migratable, data, &__migratable_end - &__migratable);
  193. }
  194. END_RS_FUNC(migratable)
  195. BEGIN_CP_FUNC(environ)
  196. {
  197. const char ** e, ** envp = (void *) obj;
  198. int nenvp = 0;
  199. int envp_bytes = 0;
  200. for (e = envp ; *e ; e++) {
  201. nenvp++;
  202. envp_bytes += strlen(*e) + 1;
  203. }
  204. ptr_t off = ADD_CP_OFFSET(sizeof(char *) * (nenvp + 1) + envp_bytes);
  205. const char ** new_envp = (void *) base + off;
  206. char * ptr = (void *) base + off + sizeof(char *) * (nenvp + 1);
  207. for (int i = 0 ; i < nenvp ; i++) {
  208. int len = strlen(envp[i]);
  209. new_envp[i] = ptr;
  210. memcpy(ptr, envp[i], len + 1);
  211. ptr += len + 1;
  212. }
  213. new_envp[nenvp] = NULL;
  214. ADD_CP_FUNC_ENTRY(off);
  215. }
  216. END_CP_FUNC(environ)
  217. BEGIN_RS_FUNC(environ)
  218. {
  219. const char ** envp = (void *) base + GET_CP_FUNC_ENTRY();
  220. const char ** e;
  221. for (e = envp ; *e ; e++) {
  222. CP_REBASE(*e);
  223. DEBUG_RS("%s", *e);
  224. }
  225. initial_envp = envp;
  226. }
  227. END_RS_FUNC(environ)
  228. BEGIN_CP_FUNC(qstr)
  229. {
  230. struct shim_qstr * qstr = (struct shim_qstr *) obj;
  231. if (qstr->len < QSTR_SIZE) {
  232. if (qstr->oflow) {
  233. memcpy(qstr->name, qstr->oflow, qstr->len + 1);
  234. qstr->oflow = NULL;
  235. }
  236. } else {
  237. struct shim_str * str =
  238. (void *) (base + ADD_CP_OFFSET(qstr->len + 1));
  239. memcpy(str, qstr->oflow, qstr->len + 1);
  240. qstr->oflow = str;
  241. ADD_CP_FUNC_ENTRY((ptr_t) qstr - base);
  242. }
  243. }
  244. END_CP_FUNC(qstr)
  245. BEGIN_RS_FUNC(qstr)
  246. {
  247. struct shim_qstr * qstr = (void *) (base + GET_CP_FUNC_ENTRY());
  248. CP_REBASE(qstr->oflow);
  249. }
  250. END_RS_FUNC(qstr)
  251. BEGIN_CP_FUNC(gipc)
  252. {
  253. ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_gipc_entry));
  254. void * send_addr = (void *) ALIGN_DOWN(obj);
  255. size_t send_size = (void *) ALIGN_UP(obj + size) - send_addr;
  256. struct shim_gipc_entry * entry = (void *) (base + off);
  257. entry->mem.addr = send_addr;
  258. entry->mem.size = send_size;
  259. entry->mem.prot = PAL_PROT_READ|PAL_PROT_WRITE;
  260. entry->mem.prev = (void *) store->last_gipc_entry;
  261. store->last_gipc_entry = entry;
  262. store->gipc_nentries++;
  263. #if HASH_GIPC == 1
  264. struct md5_ctx ctx;
  265. md5_init(&ctx);
  266. md5_update(&ctx, send_addr, allocsize);
  267. md5_final(&ctx);
  268. entry->first_hash = *(unsigned long *) ctx.digest;
  269. #endif /* HASH_GIPC == 1 */
  270. ADD_CP_FUNC_ENTRY(off);
  271. if (objp)
  272. *objp = entry;
  273. }
  274. END_CP_FUNC(gipc)
  275. BEGIN_RS_FUNC(gipc)
  276. {
  277. #if HASH_GIPC == 1
  278. struct shim_gipc_entry * entry = (void *) (base + GET_CP_FUNC_ENTRY());
  279. PAL_FLG pal_prot = PAL_PROT(entry->prot, 0);
  280. if (!(pal_prot & PROT_READ))
  281. DkVirtualMemoryProtect(entry->addr, entry->npages * allocsize,
  282. pal_prot|PAL_PROT_READ);
  283. struct md5_ctx ctx;
  284. md5_init(&ctx);
  285. md5_update(&ctx, entry->addr, allocsize);
  286. md5_final(&ctx);
  287. assert(*(unsigned long *) ctx.digest == entry->first_hash);
  288. if (!(pal_prot & PAL_PROT_READ))
  289. DkVirtualMemoryProtect(entry->addr, entry->npages * allocsize,
  290. pal_prot);
  291. #endif /* HASH_GIPC == 1 */
  292. }
  293. END_RS_FUNC(gipc)
  294. static int send_checkpoint_by_gipc (PAL_HANDLE gipc_store,
  295. struct shim_cp_store * store)
  296. {
  297. PAL_PTR hdr_addr = (PAL_PTR) store->base;
  298. PAL_NUM hdr_size = (PAL_NUM) store->offset + store->mem_size;
  299. assert(ALIGNED(hdr_addr));
  300. int mem_nentries = store->mem_nentries;
  301. if (mem_nentries) {
  302. struct shim_mem_entry ** mem_entries =
  303. __alloca(sizeof(struct shim_mem_entry *) * mem_nentries);
  304. int mem_cnt = mem_nentries;
  305. struct shim_mem_entry * mem_ent = store->last_mem_entry;
  306. for (; mem_ent ; mem_ent = mem_ent->prev) {
  307. if (!mem_cnt)
  308. return -EINVAL;
  309. mem_entries[--mem_cnt] = mem_ent;
  310. }
  311. mem_entries += mem_cnt;
  312. mem_nentries -= mem_cnt;
  313. for (int i = 0 ; i < mem_nentries ; i++) {
  314. void * mem_addr = (void *) store->base +
  315. __ADD_CP_OFFSET(mem_entries[i]->size);
  316. assert(store->offset <= hdr_size);
  317. memcpy(mem_addr, mem_entries[i]->addr, mem_entries[i]->size);
  318. mem_entries[i]->data = mem_addr;
  319. }
  320. }
  321. hdr_size = ALIGN_UP(hdr_size);
  322. int npages = DkPhysicalMemoryCommit(gipc_store, 1, &hdr_addr, &hdr_size, 0);
  323. if (!npages)
  324. return -EPERM;
  325. int nentries = store->gipc_nentries;
  326. PAL_PTR * gipc_addrs = __alloca(sizeof(PAL_PTR) * nentries);
  327. PAL_NUM * gipc_sizes = __alloca(sizeof(PAL_NUM) * nentries);
  328. int total_pages = 0;
  329. int cnt = nentries;
  330. struct shim_gipc_entry * ent = store->last_gipc_entry;
  331. for (; ent ; ent = (void *) ent->mem.prev) {
  332. if (!cnt)
  333. return -EINVAL;
  334. cnt--;
  335. gipc_addrs[cnt] = ent->mem.addr;
  336. gipc_sizes[cnt] = ent->mem.size;
  337. total_pages += ent->mem.size / allocsize;
  338. }
  339. gipc_addrs += cnt;
  340. gipc_sizes += cnt;
  341. nentries -= cnt;
  342. /* Chia-Che: sending an empty page can't ever be a smart idea.
  343. we might rather fail here */
  344. npages = DkPhysicalMemoryCommit(gipc_store, nentries, gipc_addrs,
  345. gipc_sizes, 0);
  346. if (npages < total_pages) {
  347. debug("gipc supposed to send %d pages, but only %d pages sent\n",
  348. total_pages, npages);
  349. return -ENOMEM;
  350. }
  351. ADD_PROFILE_OCCURENCE(migrate_send_gipc_pages, npages);
  352. return 0;
  353. }
  354. static int send_checkpoint_on_stream (PAL_HANDLE stream,
  355. struct shim_cp_store * store)
  356. {
  357. int mem_nentries = store->mem_nentries;
  358. struct shim_mem_entry ** mem_entries;
  359. if (mem_nentries) {
  360. mem_entries = __alloca(sizeof(struct shim_mem_entry *) * mem_nentries);
  361. int mem_cnt = mem_nentries;
  362. struct shim_mem_entry * mem_ent = store->last_mem_entry;
  363. for (; mem_ent ; mem_ent = mem_ent->prev) {
  364. if (!mem_cnt)
  365. return -EINVAL;
  366. mem_entries[--mem_cnt] = mem_ent;
  367. }
  368. void * mem_addr = (void *) store->base + store->offset;
  369. mem_entries += mem_cnt;
  370. mem_nentries -= mem_cnt;
  371. for (int i = 0 ; i < mem_nentries ; i++) {
  372. int mem_size = mem_entries[i]->size;
  373. mem_entries[i]->data = mem_addr;
  374. mem_addr += mem_size;
  375. }
  376. }
  377. int total_bytes = store->offset;
  378. int bytes = 0;
  379. do {
  380. int ret = DkStreamWrite(stream, 0, total_bytes - bytes,
  381. (void *) store->base + bytes, NULL);
  382. if (!ret)
  383. return -PAL_ERRNO;
  384. bytes += ret;
  385. } while (bytes < total_bytes);
  386. ADD_PROFILE_OCCURENCE(migrate_send_on_stream, total_bytes);
  387. for (int i = 0 ; i < mem_nentries ; i++) {
  388. int mem_size = mem_entries[i]->size;
  389. void * mem_addr = mem_entries[i]->addr;
  390. bytes = 0;
  391. do {
  392. int ret = DkStreamWrite(stream, 0, mem_size - bytes,
  393. mem_addr + bytes, NULL);
  394. if (!ret)
  395. return -PAL_ERRNO;
  396. bytes += ret;
  397. } while (bytes < mem_entries[i]->size);
  398. mem_entries[i]->size = mem_size;
  399. ADD_PROFILE_OCCURENCE(migrate_send_on_stream, mem_size);
  400. }
  401. return 0;
  402. }
  403. static int restore_gipc (PAL_HANDLE gipc, struct gipc_header * hdr, ptr_t base,
  404. long rebase)
  405. {
  406. struct shim_gipc_entry * gipc_entries = (void *) (base + hdr->entoffset);
  407. int nentries = hdr->nentries;
  408. if (!nentries)
  409. return 0;
  410. debug("restore memory by gipc: %d entries\n", nentries);
  411. struct shim_gipc_entry ** entries =
  412. __alloca(sizeof(struct shim_gipc_entry *) * nentries);
  413. struct shim_gipc_entry * entry = gipc_entries;
  414. int cnt = nentries;
  415. while (entry) {
  416. CP_REBASE(entry->mem.prev);
  417. CP_REBASE(entry->mem.paddr);
  418. if (!cnt)
  419. return -EINVAL;
  420. entries[--cnt] = entry;
  421. entry = (void *) entry->mem.prev;
  422. }
  423. entries += cnt;
  424. nentries -= cnt;
  425. PAL_PTR * addrs = __alloca(sizeof(PAL_PTR) * nentries);
  426. PAL_NUM * sizes = __alloca(sizeof(PAL_NUM) * nentries);
  427. PAL_FLG * prots = __alloca(sizeof(PAL_FLG) * nentries);
  428. for (int i = 0 ; i < nentries ; i++) {
  429. addrs[i] = entries[i]->mem.paddr ? NULL : (PAL_PTR) entries[i]->mem.addr;
  430. sizes[i] = entries[i]->mem.size;
  431. prots[i] = entries[i]->mem.prot;
  432. }
  433. if (!DkPhysicalMemoryMap(gipc, nentries, addrs, sizes, prots))
  434. return -PAL_ERRNO;
  435. for (int i = 0 ; i < nentries ; i++)
  436. if (entries[i]->mem.paddr)
  437. *(void **) entries[i]->mem.paddr = (void *) addrs[i];
  438. return 0;
  439. }
  440. int restore_checkpoint (struct cp_header * cphdr, struct mem_header * memhdr,
  441. ptr_t base, int type)
  442. {
  443. ptr_t cpoffset = cphdr->offset;
  444. ptr_t * offset = &cpoffset;
  445. long rebase = base - (ptr_t) cphdr->addr;
  446. int ret = 0;
  447. if (type)
  448. debug("restore checkpoint at %p rebased from %p (%s only)\n",
  449. base, cphdr->addr, CP_FUNC_NAME(type));
  450. else
  451. debug("restore checkpoint at %p rebased from %p\n",
  452. base, cphdr->addr);
  453. if (memhdr && memhdr->nentries) {
  454. struct shim_mem_entry * entry =
  455. (void *) (base + memhdr->entoffset);
  456. for (; entry ; entry = entry->prev) {
  457. CP_REBASE(entry->prev);
  458. CP_REBASE(entry->paddr);
  459. if (entry->paddr) {
  460. *entry->paddr = entry->data;
  461. } else {
  462. debug("memory entry [%p]: %p-%p\n", entry, entry->addr,
  463. entry->addr + entry->size);
  464. PAL_PTR addr = ALIGN_DOWN(entry->addr);
  465. PAL_NUM size = ALIGN_UP(entry->addr + entry->size) -
  466. (void *) addr;
  467. PAL_FLG prot = entry->prot;
  468. if (!DkVirtualMemoryAlloc(addr, size, 0, prot|PAL_PROT_WRITE)) {
  469. debug("failed allocating %p-%p\n", addr, addr + size);
  470. return -PAL_ERRNO;
  471. }
  472. CP_REBASE(entry->data);
  473. memcpy(entry->addr, entry->data, entry->size);
  474. if (!(entry->prot & PAL_PROT_WRITE) &&
  475. !DkVirtualMemoryProtect(addr, size, prot)) {
  476. debug("failed protecting %p-%p (ignored)\n", addr, addr + size);
  477. }
  478. }
  479. }
  480. }
  481. struct shim_cp_entry * cpent = NEXT_CP_ENTRY();
  482. while (cpent) {
  483. if (cpent->cp_type < CP_FUNC_BASE)
  484. goto next;
  485. if (type && cpent->cp_type != type)
  486. goto next;
  487. rs_func rs = (&__rs_func) [cpent->cp_type - CP_FUNC_BASE];
  488. ret = (*rs) (cpent, base, offset, rebase);
  489. if (ret < 0) {
  490. debug("restoring %s failed at %p (err=%d)\n", CP_FUNC_NAME(cpent->cp_type),
  491. base + offset, -ret);
  492. return ret;
  493. }
  494. next:
  495. cpent = NEXT_CP_ENTRY();
  496. }
  497. debug("successfully restore checkpoint loaded at %p - %p\n",
  498. base, base + cphdr->size);
  499. return 0;
  500. }
  501. int init_from_checkpoint_file (const char * filename,
  502. struct newproc_cp_header * hdr,
  503. void ** cpptr)
  504. {
  505. struct shim_dentry * dir = NULL;
  506. int ret;
  507. ret = path_lookupat(NULL, filename, LOOKUP_ACCESS|LOOKUP_DIRECTORY, &dir);
  508. if (ret < 0)
  509. return ret;
  510. struct shim_mount * fs = dir->fs;
  511. struct shim_dirent * dirent;
  512. if (!fs->d_ops || !fs->d_ops->readdir) {
  513. ret = -EACCES;
  514. goto out;
  515. }
  516. if ((ret = fs->d_ops->readdir(dir, &dirent)) < 0)
  517. goto out;
  518. struct shim_dentry * first = NULL;
  519. struct shim_dirent * d = dirent;
  520. for ( ; d ; d = d->next) {
  521. struct shim_dentry * file;
  522. if ((ret = lookup_dentry(dir, d->name, strlen(d->name), false,
  523. &file)) < 0)
  524. continue;
  525. if (file->state & DENTRY_NEGATIVE)
  526. continue;
  527. if (!first) {
  528. first = file;
  529. continue;
  530. }
  531. const char * argv[3];
  532. argv[0] = "-resume-file";
  533. argv[1] = dentry_get_path(file, true, NULL);
  534. argv[2] = 0;
  535. PAL_HANDLE proc = DkProcessCreate(NULL, 0, argv);
  536. if (!proc) {
  537. ret = -PAL_ERRNO;
  538. goto out;
  539. }
  540. put_dentry(file);
  541. }
  542. if (first) {
  543. ret = restore_from_file(dentry_get_path(first, true, NULL), hdr, cpptr);
  544. put_dentry(first);
  545. }
  546. free(dirent);
  547. out:
  548. put_dentry(dir);
  549. return ret;
  550. }
  551. int restore_from_file (const char * filename, struct newproc_cp_header * hdr,
  552. void ** cpptr)
  553. {
  554. struct shim_handle * file = get_new_handle();
  555. if (!file)
  556. return -ENOMEM;
  557. int ret = open_namei(file, NULL, filename, O_RDWR, 0, NULL);
  558. if (ret < 0) {
  559. put_handle(file);
  560. return ret;
  561. }
  562. struct shim_mount * fs = file->fs;
  563. open_handle(file);
  564. debug("restore %s\n", filename);
  565. struct cp_header cphdr;
  566. ret = fs->fs_ops->read(file, &cphdr, sizeof(struct cp_header));
  567. if (ret < 0)
  568. goto out;
  569. void * cpaddr = cphdr.addr;
  570. ret = fs->fs_ops->mmap(file, &cpaddr, ALIGN_UP(cphdr.size),
  571. PROT_READ|PROT_WRITE,
  572. MAP_PRIVATE|MAP_FILE, 0);
  573. if (ret < 0)
  574. goto out;
  575. hdr->hdr = cphdr;
  576. *cpptr = cpaddr;
  577. migrated_memory_start = cpaddr;
  578. migrated_memory_end = cpaddr + hdr->hdr.size;
  579. out:
  580. close_handle(file);
  581. return ret;
  582. }
  583. int send_handles_on_stream (PAL_HANDLE stream, struct shim_cp_store * store)
  584. {
  585. int nentries = store->palhdl_nentries;
  586. if (!nentries)
  587. return 0;
  588. struct shim_palhdl_entry ** entries =
  589. __alloca(sizeof(struct shim_palhdl_entry *) * nentries);
  590. struct shim_palhdl_entry * entry = store->last_palhdl_entry;
  591. int cnt = nentries;
  592. for ( ; entry ; entry = entry->prev)
  593. if (entry->handle) {
  594. if (!cnt)
  595. return -EINVAL;
  596. entries[--cnt] = entry;
  597. }
  598. entries += cnt;
  599. nentries -= cnt;
  600. for (int i = 0 ; i < nentries ; i++)
  601. if (!DkSendHandle(stream, entries[i]->handle))
  602. entries[i]->handle = NULL;
  603. return 0;
  604. }
  605. int receive_handles_on_stream (struct palhdl_header * hdr, ptr_t base,
  606. long rebase)
  607. {
  608. struct shim_palhdl_entry * palhdl_entries =
  609. (void *) (base + hdr->entoffset);
  610. int nentries = hdr->nentries;
  611. if (!nentries)
  612. return 0;
  613. debug("receive handles: %d entries\n", nentries);
  614. struct shim_palhdl_entry ** entries =
  615. __alloca(sizeof(struct shim_palhdl_entry *) * nentries);
  616. struct shim_palhdl_entry * entry = palhdl_entries;
  617. int cnt = nentries;
  618. for ( ; entry ; entry = entry->prev) {
  619. CP_REBASE(entry->prev);
  620. CP_REBASE(entry->phandle);
  621. if (!cnt)
  622. return -EINVAL;
  623. entries[--cnt] = entry;
  624. }
  625. entries += cnt;
  626. nentries -= cnt;
  627. for (int i = 0 ; i < nentries ; i++) {
  628. entry = entries[i];
  629. if (entry->handle) {
  630. PAL_HANDLE hdl = DkReceiveHandle(PAL_CB(parent_process));
  631. if (hdl) {
  632. *entry->phandle = hdl;
  633. continue;
  634. }
  635. }
  636. }
  637. return 0;
  638. }
  639. #define NTRIES 4
  640. static void * cp_alloc (struct shim_cp_store * store, void * addr, int size)
  641. {
  642. void * requested = addr;
  643. struct shim_vma * vma;
  644. int ret, n = 0;
  645. if (!requested) {
  646. again:
  647. if (n == NTRIES)
  648. return NULL;
  649. if (!(addr = get_unmapped_vma_for_cp(size)))
  650. return NULL;
  651. } else {
  652. ret = lookup_overlap_vma(addr, size, &vma);
  653. if (!ret) {
  654. if (vma->addr != addr || vma->length != size ||
  655. !(vma->flags & VMA_UNMAPPED)) {
  656. put_vma(vma);
  657. return NULL;
  658. }
  659. }
  660. }
  661. addr = (void *) DkVirtualMemoryAlloc(addr, size, 0,
  662. PAL_PROT_READ|PAL_PROT_WRITE);
  663. if (!addr) {
  664. if (!requested)
  665. goto again;
  666. return NULL;
  667. }
  668. if (requested && addr != requested) {
  669. DkVirtualMemoryFree(addr, size);
  670. return NULL;
  671. }
  672. return addr;
  673. }
  674. DEFINE_PROFILE_CATAGORY(migrate_proc, migrate);
  675. DEFINE_PROFILE_INTERVAL(migrate_create_process, migrate_proc);
  676. DEFINE_PROFILE_INTERVAL(migrate_create_gipc, migrate_proc);
  677. DEFINE_PROFILE_INTERVAL(migrate_connect_ipc, migrate_proc);
  678. DEFINE_PROFILE_INTERVAL(migrate_init_checkpoint, migrate_proc);
  679. DEFINE_PROFILE_INTERVAL(migrate_save_checkpoint, migrate_proc);
  680. DEFINE_PROFILE_INTERVAL(migrate_send_header, migrate_proc);
  681. DEFINE_PROFILE_INTERVAL(migrate_send_checkpoint, migrate_proc);
  682. DEFINE_PROFILE_OCCURENCE(migrate_send_on_stream, migrate_proc);
  683. DEFINE_PROFILE_OCCURENCE(migrate_send_gipc_pages, migrate_proc);
  684. DEFINE_PROFILE_INTERVAL(migrate_send_pal_handles, migrate_proc);
  685. DEFINE_PROFILE_INTERVAL(migrate_free_checkpoint, migrate_proc);
  686. DEFINE_PROFILE_INTERVAL(migrate_wait_response, migrate_proc);
  687. int do_migrate_process (int (*migrate) (struct shim_cp_store *,
  688. struct shim_thread *,
  689. struct shim_process *, va_list),
  690. struct shim_handle * exec,
  691. const char ** argv,
  692. struct shim_thread * thread, ...)
  693. {
  694. int ret = 0;
  695. struct shim_process * new_process = NULL;
  696. struct newproc_header hdr;
  697. struct shim_cp_store * cpstore = NULL;
  698. int bytes;
  699. memset(&hdr, 0, sizeof(hdr));
  700. #ifdef PROFILE
  701. unsigned long begin_create_time = GET_PROFILE_INTERVAL();
  702. unsigned long create_time = begin_create_time;
  703. #endif
  704. BEGIN_PROFILE_INTERVAL();
  705. PAL_HANDLE proc = DkProcessCreate(exec ? qstrgetstr(&exec->uri) :
  706. pal_control.executable,
  707. 0, argv);
  708. if (!proc) {
  709. ret = -PAL_ERRNO;
  710. goto err;
  711. }
  712. SAVE_PROFILE_INTERVAL(migrate_create_process);
  713. bool use_gipc = false;
  714. PAL_NUM gipc_key;
  715. PAL_HANDLE gipc_hdl = DkCreatePhysicalMemoryChannel(&gipc_key);
  716. if (gipc_hdl) {
  717. debug("created gipc store: gipc:%lu\n", gipc_key);
  718. use_gipc = true;
  719. SAVE_PROFILE_INTERVAL(migrate_create_gipc);
  720. } else {
  721. sys_printf("WARNING: no physical memory support, process creation "
  722. "will be slow.\n");
  723. }
  724. if (!(new_process = create_new_process(true))) {
  725. ret = -ENOMEM;
  726. goto err;
  727. }
  728. if (!(new_process->self = create_ipc_port(0, false))) {
  729. ret = -EACCES;
  730. goto err;
  731. }
  732. SAVE_PROFILE_INTERVAL(migrate_connect_ipc);
  733. cpstore = __alloca(sizeof(struct shim_cp_store));
  734. memset(cpstore, 0, sizeof(struct shim_cp_store));
  735. cpstore->alloc = cp_alloc;
  736. cpstore->use_gipc = use_gipc;
  737. cpstore->bound = CP_INIT_VMA_SIZE;
  738. while (1) {
  739. debug("try allocate checkpoint store (size = %d)\n", cpstore->bound);
  740. cpstore->base = (ptr_t) cp_alloc(cpstore, 0, cpstore->bound);
  741. if (cpstore->base)
  742. break;
  743. cpstore->bound >>= 1;
  744. if (cpstore->bound < allocsize)
  745. break;
  746. }
  747. if (!cpstore->base) {
  748. ret = -ENOMEM;
  749. debug("failed creating checkpoint store\n");
  750. goto err;
  751. }
  752. SAVE_PROFILE_INTERVAL(migrate_init_checkpoint);
  753. va_list ap;
  754. va_start(ap, thread);
  755. ret = (*migrate) (cpstore, thread, new_process, ap);
  756. va_end(ap);
  757. if (ret < 0) {
  758. debug("failed creating checkpoint (ret = %d)\n", ret);
  759. goto err;
  760. }
  761. SAVE_PROFILE_INTERVAL(migrate_save_checkpoint);
  762. unsigned long checkpoint_time = GET_PROFILE_INTERVAL();
  763. unsigned long checkpoint_size = cpstore->offset + cpstore->mem_size;
  764. debug("checkpoint of %u bytes created, %lu microsecond is spent.\n",
  765. checkpoint_size, checkpoint_time);
  766. hdr.checkpoint.hdr.addr = (void *) cpstore->base;
  767. hdr.checkpoint.hdr.size = checkpoint_size;
  768. if (cpstore->mem_nentries) {
  769. hdr.checkpoint.mem.entoffset =
  770. (ptr_t) cpstore->last_mem_entry - cpstore->base;
  771. hdr.checkpoint.mem.nentries = cpstore->mem_nentries;
  772. }
  773. if (cpstore->use_gipc) {
  774. snprintf(hdr.checkpoint.gipc.uri, sizeof(hdr.checkpoint.gipc.uri),
  775. "gipc:%lld", gipc_key);
  776. if (cpstore->gipc_nentries) {
  777. hdr.checkpoint.gipc.entoffset =
  778. (ptr_t) cpstore->last_gipc_entry - cpstore->base;
  779. hdr.checkpoint.gipc.nentries = cpstore->gipc_nentries;
  780. }
  781. }
  782. if (cpstore->palhdl_nentries) {
  783. hdr.checkpoint.palhdl.entoffset =
  784. (ptr_t) cpstore->last_palhdl_entry - cpstore->base;
  785. hdr.checkpoint.palhdl.nentries = cpstore->palhdl_nentries;
  786. }
  787. #ifdef PROFILE
  788. hdr.begin_create_time = begin_create_time;
  789. hdr.create_time = create_time;
  790. hdr.write_proc_time = GET_PROFILE_INTERVAL();
  791. #endif
  792. bytes = DkStreamWrite(proc, 0, sizeof(struct newproc_header), &hdr, NULL);
  793. if (!bytes) {
  794. ret = -PAL_ERRNO;
  795. debug("failed writing to process stream (ret = %d)\n", ret);
  796. goto err;
  797. } else if (bytes < sizeof(struct newproc_header)) {
  798. ret = -EACCES;
  799. goto err;
  800. }
  801. ADD_PROFILE_OCCURENCE(migrate_send_on_stream, bytes);
  802. SAVE_PROFILE_INTERVAL(migrate_send_header);
  803. ret = cpstore->use_gipc ? send_checkpoint_by_gipc(gipc_hdl, cpstore) :
  804. send_checkpoint_on_stream(proc, cpstore);
  805. if (ret < 0) {
  806. debug("failed sending checkpoint (ret = %d)\n", ret);
  807. goto err;
  808. }
  809. SAVE_PROFILE_INTERVAL(migrate_send_checkpoint);
  810. if ((ret = send_handles_on_stream(proc, cpstore)) < 0)
  811. goto err;
  812. SAVE_PROFILE_INTERVAL(migrate_send_pal_handles);
  813. system_free((void *) cpstore->base, cpstore->bound);
  814. SAVE_PROFILE_INTERVAL(migrate_free_checkpoint);
  815. struct newproc_response res;
  816. bytes = DkStreamRead(proc, 0, sizeof(struct newproc_response), &res,
  817. NULL, 0);
  818. if (bytes == 0) {
  819. ret = -PAL_ERRNO;
  820. goto err;
  821. }
  822. SAVE_PROFILE_INTERVAL(migrate_wait_response);
  823. if (gipc_hdl)
  824. DkObjectClose(gipc_hdl);
  825. ipc_pid_sublease_send(res.child_vmid, thread->tid,
  826. qstrgetstr(&new_process->self->uri),
  827. NULL);
  828. add_ipc_port_by_id(res.child_vmid, proc,
  829. IPC_PORT_DIRCLD|IPC_PORT_LISTEN|IPC_PORT_KEEPALIVE,
  830. &ipc_child_exit,
  831. NULL);
  832. destroy_process(new_process);
  833. return 0;
  834. err:
  835. if (gipc_hdl)
  836. DkObjectClose(gipc_hdl);
  837. if (proc)
  838. DkObjectClose(proc);
  839. if (new_process)
  840. destroy_process(new_process);
  841. sys_printf("process creation failed\n");
  842. return ret;
  843. }
  844. int do_migration (struct newproc_cp_header * hdr, void ** cpptr)
  845. {
  846. ptr_t base = (ptr_t) hdr->hdr.addr;
  847. int size = hdr->hdr.size;
  848. PAL_PTR mapaddr;
  849. PAL_NUM mapsize;
  850. unsigned long mapoff;
  851. long rebase;
  852. bool use_gipc = !!hdr->gipc.uri[0];
  853. PAL_HANDLE gipc_store;
  854. int ret = 0;
  855. debug("checkpoint detected (%d bytes, expected at %p)\n",
  856. size, base);
  857. if (base && lookup_overlap_vma((void *) base, size, NULL) == -ENOENT) {
  858. mapaddr = (PAL_PTR) ALIGN_DOWN(base);
  859. mapsize = (PAL_PTR) ALIGN_UP(base + size) - mapaddr;
  860. mapoff = base - (ptr_t) mapaddr;
  861. } else {
  862. mapaddr = (PAL_PTR) 0;
  863. mapsize = ALIGN_UP(size);
  864. mapoff = 0;
  865. }
  866. BEGIN_PROFILE_INTERVAL();
  867. if (use_gipc) {
  868. debug("open gipc store: %s\n", hdr->gipc.uri);
  869. PAL_FLG mapprot = PAL_PROT_READ|PAL_PROT_WRITE;
  870. gipc_store = DkStreamOpen(hdr->gipc.uri, 0, 0, 0, 0);
  871. if (!gipc_store ||
  872. !DkPhysicalMemoryMap(gipc_store, 1, &mapaddr, &mapsize, &mapprot))
  873. return -PAL_ERRNO;
  874. SAVE_PROFILE_INTERVAL(child_load_checkpoint_by_gipc);
  875. } else {
  876. if (!(mapaddr = DkVirtualMemoryAlloc(mapaddr, mapsize, 0,
  877. PAL_PROT_READ|PAL_PROT_WRITE)))
  878. return -PAL_ERRNO;
  879. }
  880. bkeep_mmap((void *) mapaddr, mapsize,
  881. PROT_READ|PROT_WRITE,
  882. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL,
  883. NULL, 0, NULL);
  884. base = (ptr_t) mapaddr + mapoff;
  885. rebase = (long) base - (long) hdr->hdr.addr;
  886. debug("checkpoint loaded at %p\n", base);
  887. if (use_gipc) {
  888. if ((ret = restore_gipc(gipc_store, &hdr->gipc, base, rebase)) < 0)
  889. return ret;
  890. SAVE_PROFILE_INTERVAL(child_load_memory_by_gipc);
  891. DkStreamDelete(gipc_store, 0);
  892. } else {
  893. int total_bytes = 0;
  894. while (total_bytes < size) {
  895. int bytes = DkStreamRead(PAL_CB(parent_process), 0,
  896. size - total_bytes,
  897. (void *) base + total_bytes, NULL, 0);
  898. if (!bytes)
  899. return -PAL_ERRNO;
  900. total_bytes += bytes;
  901. }
  902. SAVE_PROFILE_INTERVAL(child_load_checkpoint_on_pipe);
  903. debug("%d bytes read on stream\n", total_bytes);
  904. }
  905. struct newproc_response res;
  906. res.child_vmid = cur_process.vmid;
  907. res.failure = 0;
  908. int bytes = DkStreamWrite(PAL_CB(parent_process), 0,
  909. sizeof(struct newproc_response),
  910. &res, NULL);
  911. if (!bytes)
  912. return -PAL_ERRNO;
  913. if ((ret = receive_handles_on_stream(&hdr->palhdl, base, rebase)) < 0)
  914. return ret;
  915. SAVE_PROFILE_INTERVAL(child_receive_handles);
  916. migrated_memory_start = (void *) mapaddr;
  917. migrated_memory_end = (void *) mapaddr + mapsize;
  918. *cpptr = (void *) base;
  919. return 0;
  920. }
  921. void restore_context (struct shim_context * context)
  922. {
  923. int nregs = sizeof(struct shim_regs) / sizeof(void *);
  924. void * regs[nregs + 1];
  925. if (context->regs)
  926. memcpy(regs, context->regs, sizeof(struct shim_regs));
  927. else
  928. memset(regs, 0, sizeof(struct shim_regs));
  929. debug("restore context: SP = %p, IP = %p\n", context->sp, context->ret_ip);
  930. regs[nregs] = (void *) context->sp - 8;
  931. *(void **) (context->sp - 8) = context->ret_ip;
  932. memset(context, 0, sizeof(struct shim_context));
  933. asm volatile("movq %0, %%rsp\r\n"
  934. "popq %%r15\r\n"
  935. "popq %%r14\r\n"
  936. "popq %%r13\r\n"
  937. "popq %%r12\r\n"
  938. "popq %%r9\r\n"
  939. "popq %%r8\r\n"
  940. "popq %%rcx\r\n"
  941. "popq %%rdx\r\n"
  942. "popq %%rsi\r\n"
  943. "popq %%rdi\r\n"
  944. "popq %%rbx\r\n"
  945. "popq %%rbp\r\n"
  946. "popq %%rsp\r\n"
  947. "movq $0, %%rax\r\n"
  948. "retq\r\n"
  949. :: "g"(&regs) : "memory");
  950. }