shim_checkpoint.c 39 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321
  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_checkpoint.c
  15. *
  16. * This file contains codes for checkpoint / migration scheme of library OS.
  17. */
  18. #include "asm-offsets.h"
  19. #include <shim_internal.h>
  20. #include <shim_utils.h>
  21. #include <shim_thread.h>
  22. #include <shim_handle.h>
  23. #include <shim_vma.h>
  24. #include <shim_fs.h>
  25. #include <shim_checkpoint.h>
  26. #include <shim_ipc.h>
  27. #include <shim_profile.h>
  28. #include <pal.h>
  29. #include <pal_error.h>
  30. #include <list.h>
  31. #include <stdarg.h>
  32. #include <asm/fcntl.h>
  33. #include <asm/mman.h>
  34. DEFINE_PROFILE_CATEGORY(migrate, );
  35. DEFINE_PROFILE_CATEGORY(checkpoint, migrate);
  36. DEFINE_PROFILE_INTERVAL(checkpoint_create_map, checkpoint);
  37. DEFINE_PROFILE_INTERVAL(checkpoint_copy, checkpoint);
  38. DEFINE_PROFILE_CATEGORY(checkpoint_func, checkpoint);
  39. DEFINE_PROFILE_INTERVAL(checkpoint_destroy_map, checkpoint);
  40. DEFINE_PROFILE_OCCURENCE(checkpoint_count, checkpoint);
  41. DEFINE_PROFILE_OCCURENCE(checkpoint_total_size, checkpoint);
  42. DEFINE_PROFILE_CATEGORY(resume, migrate);
  43. DEFINE_PROFILE_INTERVAL(child_created_in_new_process, resume);
  44. DEFINE_PROFILE_INTERVAL(child_wait_header, resume);
  45. DEFINE_PROFILE_INTERVAL(child_receive_header, resume);
  46. DEFINE_PROFILE_INTERVAL(do_migration, resume);
  47. DEFINE_PROFILE_INTERVAL(child_load_checkpoint_by_gipc, resume);
  48. DEFINE_PROFILE_INTERVAL(child_load_memory_by_gipc, resume);
  49. DEFINE_PROFILE_INTERVAL(child_load_checkpoint_on_pipe, resume);
  50. DEFINE_PROFILE_INTERVAL(child_receive_handles, resume);
  51. DEFINE_PROFILE_INTERVAL(restore_checkpoint, resume);
  52. DEFINE_PROFILE_CATEGORY(resume_func, resume);
  53. DEFINE_PROFILE_INTERVAL(child_total_migration_time, resume);
  54. #define CP_HASH_SIZE 256
  55. #define CP_HASH(addr) ((hashfunc((ptr_t)(addr))) & (CP_HASH_SIZE - 1))
  56. typedef uint16_t FASTHASHTYPE;
  57. #define CP_MAP_ENTRY_NUM 64
  58. DEFINE_LIST(cp_map_entry);
  59. struct cp_map_entry
  60. {
  61. LIST_TYPE(cp_map_entry) hlist;
  62. struct shim_cp_map_entry entry;
  63. };
  64. DEFINE_LISTP(cp_map_entry);
  65. struct cp_map {
  66. struct cp_map_buffer {
  67. struct cp_map_buffer * next;
  68. int num, cnt;
  69. struct cp_map_entry entries[0];
  70. } * buffers;
  71. struct hash_map {
  72. LISTP_TYPE(cp_map_entry) head[CP_HASH_SIZE];
  73. } map;
  74. };
  75. void * create_cp_map (void)
  76. {
  77. void * data = malloc(sizeof(struct cp_map) + sizeof(struct cp_map_buffer) +
  78. sizeof(struct cp_map_entry) * CP_MAP_ENTRY_NUM);
  79. if (!data)
  80. return NULL;
  81. struct cp_map * map = (struct cp_map *) data;
  82. struct cp_map_buffer * buffer =
  83. (struct cp_map_buffer *) (data + sizeof(struct cp_map));
  84. memset(map, 0, sizeof(*map));
  85. map->buffers = buffer;
  86. buffer->next = NULL;
  87. buffer->num = CP_MAP_ENTRY_NUM;
  88. buffer->cnt = 0;
  89. return (void *) map;
  90. }
  91. void destroy_cp_map (void * map)
  92. {
  93. struct cp_map * m = (struct cp_map *) map;
  94. struct cp_map_buffer * buffer = m->buffers, * next;
  95. for (next = buffer ? buffer->next : NULL ;
  96. buffer && next ;
  97. buffer = next, next = next ? next->next : NULL)
  98. free(buffer);
  99. free(m);
  100. }
  101. static inline
  102. struct cp_map_buffer * extend_cp_map (struct cp_map * map)
  103. {
  104. struct cp_map_buffer * buffer =
  105. malloc(sizeof(struct cp_map_buffer) +
  106. sizeof(struct cp_map_entry) * CP_MAP_ENTRY_NUM);
  107. if (!buffer)
  108. return NULL;
  109. buffer->next = map->buffers;
  110. map->buffers = buffer;
  111. buffer->num = CP_MAP_ENTRY_NUM;
  112. buffer->cnt = 0;
  113. return buffer;
  114. }
  115. struct shim_cp_map_entry *
  116. get_cp_map_entry (void * map, void * addr, bool create)
  117. {
  118. struct cp_map * m = (struct cp_map *) map;
  119. FASTHASHTYPE hash = CP_HASH(addr);
  120. LISTP_TYPE(cp_map_entry) * head = &m->map.head[hash];
  121. struct cp_map_entry * tmp;
  122. struct shim_cp_map_entry * e = NULL;
  123. LISTP_FOR_EACH_ENTRY(tmp, 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_LIST_HEAD(new, hlist);
  132. LISTP_ADD(new, head, hlist);
  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. __UNUSED(size);
  159. ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_palhdl_entry));
  160. struct shim_palhdl_entry * entry = (void *) (base + off);
  161. entry->handle = (PAL_HANDLE) obj;
  162. entry->uri = NULL;
  163. entry->phandle = NULL;
  164. entry->prev = store->last_palhdl_entry;
  165. store->last_palhdl_entry = entry;
  166. store->palhdl_nentries++;
  167. ADD_CP_FUNC_ENTRY(off);
  168. if (objp)
  169. *objp = entry;
  170. }
  171. END_CP_FUNC(palhdl)
  172. BEGIN_RS_FUNC(palhdl)
  173. {
  174. __UNUSED(offset);
  175. __UNUSED(rebase);
  176. struct shim_palhdl_entry * ent = (void *) (base + GET_CP_FUNC_ENTRY());
  177. if (ent->phandle && !ent->phandle && ent->uri) {
  178. /* XXX: reopen the stream */
  179. }
  180. }
  181. END_RS_FUNC(palhdl)
  182. BEGIN_CP_FUNC(migratable)
  183. {
  184. __UNUSED(obj);
  185. __UNUSED(size);
  186. __UNUSED(objp);
  187. struct shim_mem_entry * mem_entry;
  188. DO_CP_SIZE(memory, &__migratable, &__migratable_end - &__migratable,
  189. &mem_entry);
  190. struct shim_cp_entry * entry = ADD_CP_FUNC_ENTRY(0UL);
  191. mem_entry->paddr = (void **) &entry->cp_un.cp_val;
  192. }
  193. END_CP_FUNC(migratable)
  194. BEGIN_RS_FUNC(migratable)
  195. {
  196. __UNUSED(base);
  197. __UNUSED(offset);
  198. void * data = (void *) GET_CP_FUNC_ENTRY();
  199. CP_REBASE(data);
  200. memcpy(&__migratable, data, &__migratable_end - &__migratable);
  201. }
  202. END_RS_FUNC(migratable)
  203. BEGIN_CP_FUNC(environ)
  204. {
  205. __UNUSED(size);
  206. __UNUSED(objp);
  207. const char ** e, ** envp = (void *) obj;
  208. int nenvp = 0;
  209. int envp_bytes = 0;
  210. for (e = envp ; *e ; e++) {
  211. nenvp++;
  212. envp_bytes += strlen(*e) + 1;
  213. }
  214. ptr_t off = ADD_CP_OFFSET(sizeof(char *) * (nenvp + 1) + envp_bytes);
  215. const char ** new_envp = (void *) base + off;
  216. char * ptr = (void *) base + off + sizeof(char *) * (nenvp + 1);
  217. for (int i = 0 ; i < nenvp ; i++) {
  218. int len = strlen(envp[i]);
  219. new_envp[i] = ptr;
  220. memcpy(ptr, envp[i], len + 1);
  221. ptr += len + 1;
  222. }
  223. new_envp[nenvp] = NULL;
  224. ADD_CP_FUNC_ENTRY(off);
  225. }
  226. END_CP_FUNC(environ)
  227. BEGIN_RS_FUNC(environ)
  228. {
  229. __UNUSED(offset);
  230. const char ** envp = (void *) base + GET_CP_FUNC_ENTRY();
  231. const char ** e;
  232. for (e = envp ; *e ; e++) {
  233. CP_REBASE(*e);
  234. DEBUG_RS("%s", *e);
  235. }
  236. initial_envp = envp;
  237. }
  238. END_RS_FUNC(environ)
  239. BEGIN_CP_FUNC(qstr)
  240. {
  241. __UNUSED(size);
  242. __UNUSED(objp);
  243. struct shim_qstr * qstr = (struct shim_qstr *) obj;
  244. /* qstr is always embedded as sub-object in other objects so it is
  245. * automatically checkpointed as part of other checkpoint routines.
  246. * However, its oflow string resides in some other memory region
  247. * and must be checkpointed and restored explicitly. Copy oflow
  248. * string inside checkpoint right before qstr cp entry. */
  249. if (qstr->oflow) {
  250. struct shim_str * str =
  251. (void *) (base + ADD_CP_OFFSET(qstr->len + 1));
  252. memcpy(str, qstr->oflow, qstr->len + 1);
  253. ADD_CP_FUNC_ENTRY((ptr_t) qstr - base);
  254. }
  255. }
  256. END_CP_FUNC(qstr)
  257. BEGIN_RS_FUNC(qstr)
  258. {
  259. __UNUSED(offset);
  260. __UNUSED(rebase);
  261. /* If we are here, qstr has oflow string. We know that oflow string
  262. * is right before this qstr cp entry (aligned to 8B). Calculate
  263. * oflow string's base address and update qstr to point to it. */
  264. struct shim_qstr * qstr = (void *) (base + GET_CP_FUNC_ENTRY());
  265. size_t size = qstr->len + 1;
  266. size = ((size) + sizeof(void *) - 1) & ~(sizeof(void *) - 1);
  267. qstr->oflow = (void *)entry - size;
  268. }
  269. END_RS_FUNC(qstr)
  270. BEGIN_CP_FUNC(gipc)
  271. {
  272. ptr_t off = ADD_CP_OFFSET(sizeof(struct shim_gipc_entry));
  273. void * send_addr = (void *) ALIGN_DOWN(obj);
  274. size_t send_size = (void *) ALIGN_UP(obj + size) - send_addr;
  275. struct shim_gipc_entry * entry = (void *) (base + off);
  276. entry->mem.addr = send_addr;
  277. entry->mem.size = send_size;
  278. entry->mem.prot = PAL_PROT_READ|PAL_PROT_WRITE;
  279. entry->mem.prev = (void *) store->last_gipc_entry;
  280. store->last_gipc_entry = entry;
  281. store->gipc_nentries++;
  282. #if HASH_GIPC == 1
  283. struct md5_ctx ctx;
  284. md5_init(&ctx);
  285. md5_update(&ctx, send_addr, allocsize);
  286. md5_final(&ctx);
  287. entry->first_hash = *(unsigned long *) ctx.digest;
  288. #endif /* HASH_GIPC == 1 */
  289. ADD_CP_FUNC_ENTRY(off);
  290. if (objp)
  291. *objp = entry;
  292. }
  293. END_CP_FUNC(gipc)
  294. BEGIN_RS_FUNC(gipc)
  295. {
  296. __UNUSED(rebase);
  297. __UNUSED(offset);
  298. __UNUSED(base);
  299. __UNUSED(entry);
  300. #if HASH_GIPC == 1
  301. struct shim_gipc_entry * entry = (void *) (base + GET_CP_FUNC_ENTRY());
  302. PAL_FLG pal_prot = PAL_PROT(entry->prot, 0);
  303. if (!(pal_prot & PROT_READ))
  304. DkVirtualMemoryProtect(entry->addr, entry->npages * allocsize,
  305. pal_prot|PAL_PROT_READ);
  306. struct md5_ctx ctx;
  307. md5_init(&ctx);
  308. md5_update(&ctx, entry->addr, allocsize);
  309. md5_final(&ctx);
  310. assert(*(unsigned long *) ctx.digest == entry->first_hash);
  311. if (!(pal_prot & PAL_PROT_READ))
  312. DkVirtualMemoryProtect(entry->addr, entry->npages * allocsize,
  313. pal_prot);
  314. #endif /* HASH_GIPC == 1 */
  315. }
  316. END_RS_FUNC(gipc)
  317. static int send_checkpoint_by_gipc (PAL_HANDLE gipc_store,
  318. struct shim_cp_store * store)
  319. {
  320. PAL_PTR hdr_addr = (PAL_PTR) store->base;
  321. PAL_NUM hdr_size = (PAL_NUM) store->offset + store->mem_size;
  322. assert(ALIGNED(hdr_addr));
  323. int mem_nentries = store->mem_nentries;
  324. if (mem_nentries) {
  325. struct shim_mem_entry ** mem_entries =
  326. __alloca(sizeof(struct shim_mem_entry *) * mem_nentries);
  327. int mem_cnt = mem_nentries;
  328. struct shim_mem_entry * mem_ent = store->last_mem_entry;
  329. for (; mem_ent ; mem_ent = mem_ent->prev) {
  330. if (!mem_cnt)
  331. return -EINVAL;
  332. mem_entries[--mem_cnt] = mem_ent;
  333. }
  334. mem_entries += mem_cnt;
  335. mem_nentries -= mem_cnt;
  336. for (int i = 0 ; i < mem_nentries ; i++) {
  337. void * mem_addr = (void *) store->base +
  338. __ADD_CP_OFFSET(mem_entries[i]->size);
  339. assert(store->offset <= hdr_size);
  340. memcpy(mem_addr, mem_entries[i]->addr, mem_entries[i]->size);
  341. mem_entries[i]->data = mem_addr;
  342. }
  343. }
  344. hdr_size = ALIGN_UP(hdr_size);
  345. int npages = DkPhysicalMemoryCommit(gipc_store, 1, &hdr_addr, &hdr_size);
  346. if (!npages)
  347. return -EPERM;
  348. int nentries = store->gipc_nentries;
  349. PAL_PTR * gipc_addrs = __alloca(sizeof(PAL_PTR) * nentries);
  350. PAL_NUM * gipc_sizes = __alloca(sizeof(PAL_NUM) * nentries);
  351. int total_pages = 0;
  352. int cnt = nentries;
  353. struct shim_gipc_entry * ent = store->last_gipc_entry;
  354. for (; ent ; ent = (void *) ent->mem.prev) {
  355. if (!cnt)
  356. return -EINVAL;
  357. cnt--;
  358. gipc_addrs[cnt] = ent->mem.addr;
  359. gipc_sizes[cnt] = ent->mem.size;
  360. total_pages += ent->mem.size / allocsize;
  361. }
  362. gipc_addrs += cnt;
  363. gipc_sizes += cnt;
  364. nentries -= cnt;
  365. /* Chia-Che: sending an empty page can't ever be a smart idea.
  366. we might rather fail here */
  367. npages = DkPhysicalMemoryCommit(gipc_store, nentries, gipc_addrs,
  368. gipc_sizes);
  369. if (npages < total_pages) {
  370. debug("gipc supposed to send %d pages, but only %d pages sent\n",
  371. total_pages, npages);
  372. return -ENOMEM;
  373. }
  374. ADD_PROFILE_OCCURENCE(migrate_send_gipc_pages, npages);
  375. return 0;
  376. }
  377. static int send_checkpoint_on_stream (PAL_HANDLE stream,
  378. struct shim_cp_store * store)
  379. {
  380. int mem_nentries = store->mem_nentries;
  381. struct shim_mem_entry ** mem_entries;
  382. if (mem_nentries) {
  383. mem_entries = __alloca(sizeof(struct shim_mem_entry *) * mem_nentries);
  384. int mem_cnt = mem_nentries;
  385. struct shim_mem_entry * mem_ent = store->last_mem_entry;
  386. for (; mem_ent ; mem_ent = mem_ent->prev) {
  387. if (!mem_cnt)
  388. return -EINVAL;
  389. mem_entries[--mem_cnt] = mem_ent;
  390. }
  391. void * mem_addr = (void *) store->base + store->offset;
  392. mem_entries += mem_cnt;
  393. mem_nentries -= mem_cnt;
  394. for (int i = 0 ; i < mem_nentries ; i++) {
  395. int mem_size = mem_entries[i]->size;
  396. mem_entries[i]->data = mem_addr;
  397. mem_addr += mem_size;
  398. }
  399. }
  400. size_t total_bytes = store->offset;
  401. size_t bytes = 0;
  402. do {
  403. size_t ret = DkStreamWrite(stream, 0, total_bytes - bytes,
  404. (void *) store->base + bytes, NULL);
  405. if (!ret) {
  406. if (PAL_ERRNO == EINTR || PAL_ERRNO == EAGAIN ||
  407. PAL_ERRNO == EWOULDBLOCK)
  408. continue;
  409. return -PAL_ERRNO;
  410. }
  411. bytes += ret;
  412. } while (bytes < total_bytes);
  413. ADD_PROFILE_OCCURENCE(migrate_send_on_stream, total_bytes);
  414. for (int i = 0 ; i < mem_nentries ; i++) {
  415. size_t mem_size = mem_entries[i]->size;
  416. void * mem_addr = mem_entries[i]->addr;
  417. bytes = 0;
  418. do {
  419. size_t ret = DkStreamWrite(stream, 0, mem_size - bytes,
  420. mem_addr + bytes, NULL);
  421. if (!ret) {
  422. if (PAL_ERRNO == EINTR || PAL_ERRNO == EAGAIN ||
  423. PAL_ERRNO == EWOULDBLOCK)
  424. continue;
  425. return -PAL_ERRNO;
  426. }
  427. bytes += ret;
  428. } while (bytes < mem_entries[i]->size);
  429. if (!(mem_entries[i]->prot & PAL_PROT_READ))
  430. DkVirtualMemoryProtect(mem_addr, mem_size, mem_entries[i]->prot);
  431. mem_entries[i]->size = mem_size;
  432. ADD_PROFILE_OCCURENCE(migrate_send_on_stream, mem_size);
  433. }
  434. return 0;
  435. }
  436. static int restore_gipc (PAL_HANDLE gipc, struct gipc_header * hdr, ptr_t base,
  437. long rebase)
  438. {
  439. struct shim_gipc_entry * gipc_entries = (void *) (base + hdr->entoffset);
  440. int nentries = hdr->nentries;
  441. if (!nentries)
  442. return 0;
  443. debug("restore memory by gipc: %d entries\n", nentries);
  444. struct shim_gipc_entry ** entries =
  445. __alloca(sizeof(struct shim_gipc_entry *) * nentries);
  446. struct shim_gipc_entry * entry = gipc_entries;
  447. int cnt = nentries;
  448. while (entry) {
  449. CP_REBASE(entry->mem.prev);
  450. CP_REBASE(entry->mem.paddr);
  451. if (!cnt)
  452. return -EINVAL;
  453. entries[--cnt] = entry;
  454. entry = (void *) entry->mem.prev;
  455. }
  456. entries += cnt;
  457. nentries -= cnt;
  458. PAL_PTR * addrs = __alloca(sizeof(PAL_PTR) * nentries);
  459. PAL_NUM * sizes = __alloca(sizeof(PAL_NUM) * nentries);
  460. PAL_FLG * prots = __alloca(sizeof(PAL_FLG) * nentries);
  461. for (int i = 0 ; i < nentries ; i++) {
  462. addrs[i] = entries[i]->mem.paddr ? NULL : (PAL_PTR) entries[i]->mem.addr;
  463. sizes[i] = entries[i]->mem.size;
  464. prots[i] = entries[i]->mem.prot;
  465. }
  466. if (!DkPhysicalMemoryMap(gipc, nentries, addrs, sizes, prots))
  467. return -PAL_ERRNO;
  468. for (int i = 0 ; i < nentries ; i++)
  469. if (entries[i]->mem.paddr)
  470. *(void **) entries[i]->mem.paddr = (void *) addrs[i];
  471. return 0;
  472. }
  473. int restore_checkpoint (struct cp_header * cphdr, struct mem_header * memhdr,
  474. ptr_t base, ptr_t type)
  475. {
  476. ptr_t cpoffset = cphdr->offset;
  477. ptr_t * offset = &cpoffset;
  478. long rebase = base - (ptr_t) cphdr->addr;
  479. int ret = 0;
  480. if (type)
  481. debug("restore checkpoint at 0x%08lx rebased from %p (%s only)\n",
  482. base, cphdr->addr, CP_FUNC_NAME(type));
  483. else
  484. debug("restore checkpoint at 0x%08lx rebased from %p\n",
  485. base, cphdr->addr);
  486. if (memhdr && memhdr->nentries) {
  487. struct shim_mem_entry * entry =
  488. (void *) (base + memhdr->entoffset);
  489. for (; entry ; entry = entry->prev) {
  490. CP_REBASE(entry->prev);
  491. CP_REBASE(entry->paddr);
  492. if (entry->paddr) {
  493. *entry->paddr = entry->data;
  494. } else {
  495. debug("memory entry [%p]: %p-%p\n", entry, entry->addr,
  496. entry->addr + entry->size);
  497. PAL_PTR addr = ALIGN_DOWN(entry->addr);
  498. PAL_NUM size = ALIGN_UP(entry->addr + entry->size) -
  499. (void *) addr;
  500. PAL_FLG prot = entry->prot;
  501. if (!DkVirtualMemoryAlloc(addr, size, 0, prot|PAL_PROT_WRITE)) {
  502. debug("failed allocating %p-%p\n", addr, addr + size);
  503. return -PAL_ERRNO;
  504. }
  505. CP_REBASE(entry->data);
  506. memcpy(entry->addr, entry->data, entry->size);
  507. if (!(entry->prot & PAL_PROT_WRITE) &&
  508. !DkVirtualMemoryProtect(addr, size, prot)) {
  509. debug("failed protecting %p-%p (ignored)\n", addr, addr + size);
  510. }
  511. }
  512. }
  513. }
  514. struct shim_cp_entry * cpent = NEXT_CP_ENTRY();
  515. while (cpent) {
  516. if (cpent->cp_type < CP_FUNC_BASE)
  517. goto next;
  518. if (type && cpent->cp_type != type)
  519. goto next;
  520. rs_func rs = (&__rs_func) [cpent->cp_type - CP_FUNC_BASE];
  521. ret = (*rs) (cpent, base, offset, rebase);
  522. if (ret < 0) {
  523. SYS_PRINTF("restore_checkpoint() at %s (%d)\n",
  524. CP_FUNC_NAME(cpent->cp_type), ret);
  525. return ret;
  526. }
  527. next:
  528. cpent = NEXT_CP_ENTRY();
  529. }
  530. debug("successfully restore checkpoint loaded at 0x%08lx - 0x%08lx\n",
  531. base, base + cphdr->size);
  532. return 0;
  533. }
  534. int init_from_checkpoint_file (const char * filename,
  535. struct newproc_cp_header * hdr,
  536. void ** cpptr)
  537. {
  538. struct shim_dentry * dir = NULL;
  539. int ret;
  540. /* XXX: Not sure what to do here yet */
  541. assert(0);
  542. ret = path_lookupat(NULL, filename, LOOKUP_ACCESS|LOOKUP_DIRECTORY, &dir, NULL);
  543. if (ret < 0)
  544. return ret;
  545. struct shim_mount * fs = dir->fs;
  546. struct shim_dirent * dirent;
  547. if (!fs->d_ops || !fs->d_ops->readdir) {
  548. ret = -EACCES;
  549. goto out;
  550. }
  551. if ((ret = fs->d_ops->readdir(dir, &dirent)) < 0)
  552. goto out;
  553. struct shim_dentry * first = NULL;
  554. struct shim_dirent * d = dirent;
  555. for ( ; d ; d = d->next) {
  556. struct shim_dentry * file;
  557. if ((ret = lookup_dentry(dir, d->name, strlen(d->name),
  558. &file, dir->fs)) < 0)
  559. continue;
  560. if (file->state & DENTRY_NEGATIVE)
  561. continue;
  562. if (!first) {
  563. first = file;
  564. continue;
  565. }
  566. const char * argv[3];
  567. argv[0] = "-resume-file";
  568. argv[1] = dentry_get_path(file, true, NULL);
  569. argv[2] = 0;
  570. PAL_HANDLE proc = DkProcessCreate(NULL, argv);
  571. if (!proc) {
  572. ret = -PAL_ERRNO;
  573. goto out;
  574. }
  575. put_dentry(file);
  576. }
  577. if (first) {
  578. ret = restore_from_file(dentry_get_path(first, true, NULL), hdr, cpptr);
  579. put_dentry(first);
  580. }
  581. free(dirent);
  582. out:
  583. put_dentry(dir);
  584. return ret;
  585. }
  586. int restore_from_file (const char * filename, struct newproc_cp_header * hdr,
  587. void ** cpptr)
  588. {
  589. struct shim_handle * file = get_new_handle();
  590. if (!file)
  591. return -ENOMEM;
  592. int ret = open_namei(file, NULL, filename, O_RDWR, 0, NULL);
  593. if (ret < 0) {
  594. put_handle(file);
  595. return ret;
  596. }
  597. struct shim_mount * fs = file->fs;
  598. open_handle(file);
  599. debug("restore %s\n", filename);
  600. struct cp_header cphdr;
  601. ret = fs->fs_ops->read(file, &cphdr, sizeof(struct cp_header));
  602. if (ret < 0)
  603. goto out;
  604. void * cpaddr = cphdr.addr;
  605. ret = fs->fs_ops->mmap(file, &cpaddr, ALIGN_UP(cphdr.size),
  606. PROT_READ|PROT_WRITE,
  607. MAP_PRIVATE|MAP_FILE, 0);
  608. if (ret < 0)
  609. goto out;
  610. hdr->hdr = cphdr;
  611. *cpptr = cpaddr;
  612. migrated_memory_start = cpaddr;
  613. migrated_memory_end = cpaddr + hdr->hdr.size;
  614. out:
  615. close_handle(file);
  616. return ret;
  617. }
  618. int send_handles_on_stream (PAL_HANDLE stream, struct shim_cp_store * store)
  619. {
  620. int nentries = store->palhdl_nentries;
  621. if (!nentries)
  622. return 0;
  623. struct shim_palhdl_entry ** entries =
  624. __alloca(sizeof(struct shim_palhdl_entry *) * nentries);
  625. struct shim_palhdl_entry * entry = store->last_palhdl_entry;
  626. int cnt = nentries;
  627. for ( ; entry ; entry = entry->prev)
  628. if (entry->handle) {
  629. if (!cnt)
  630. return -EINVAL;
  631. entries[--cnt] = entry;
  632. }
  633. entries += cnt;
  634. nentries -= cnt;
  635. for (int i = 0 ; i < nentries ; i++)
  636. if (!DkSendHandle(stream, entries[i]->handle))
  637. entries[i]->handle = NULL;
  638. return 0;
  639. }
  640. int receive_handles_on_stream (struct palhdl_header * hdr, ptr_t base,
  641. long rebase)
  642. {
  643. struct shim_palhdl_entry * palhdl_entries =
  644. (void *) (base + hdr->entoffset);
  645. int nentries = hdr->nentries;
  646. if (!nentries)
  647. return 0;
  648. debug("receive handles: %d entries\n", nentries);
  649. struct shim_palhdl_entry ** entries =
  650. __alloca(sizeof(struct shim_palhdl_entry *) * nentries);
  651. struct shim_palhdl_entry * entry = palhdl_entries;
  652. int cnt = nentries;
  653. for ( ; entry ; entry = entry->prev) {
  654. CP_REBASE(entry->prev);
  655. CP_REBASE(entry->phandle);
  656. if (!cnt)
  657. return -EINVAL;
  658. entries[--cnt] = entry;
  659. }
  660. entries += cnt;
  661. nentries -= cnt;
  662. for (int i = 0 ; i < nentries ; i++) {
  663. entry = entries[i];
  664. if (entry->handle) {
  665. PAL_HANDLE hdl = DkReceiveHandle(PAL_CB(parent_process));
  666. if (hdl) {
  667. *entry->phandle = hdl;
  668. continue;
  669. }
  670. }
  671. }
  672. return 0;
  673. }
  674. static void * cp_alloc (struct shim_cp_store * store, void * addr, size_t size)
  675. {
  676. // Keeping for api compatibility; not 100% sure this is needed
  677. __UNUSED(store);
  678. if (addr) {
  679. /*
  680. * If the checkpoint needs more space, try to extend the checkpoint
  681. * store at the current address.
  682. */
  683. debug("try extend checkpoint store: %p-%p (size = %ld)\n",
  684. addr, addr + size, size);
  685. if (bkeep_mmap(addr, size, PROT_READ|PROT_WRITE, CP_VMA_FLAGS,
  686. NULL, 0, "cpstore") < 0)
  687. return NULL;
  688. } else {
  689. /*
  690. * Here we use a strategy to reduce internal fragmentation of virtual
  691. * memory space. Because we need a relatively large, continuous space
  692. * for dumping the checkpoint data, internal fragmentation can cause
  693. * the process to drain the virtual address space after forking a few
  694. * times. The previous space used for checkpoint may be fragmented
  695. * at the next fork.
  696. *
  697. * A simple trick we use here is to reserve some space right after the
  698. * checkpoint space. The reserved space is half of the size of the
  699. * checkpoint space, but can be further fine-tuned.
  700. */
  701. size_t reserve_size = ALIGN_UP(size >> 1);
  702. debug("try allocate checkpoint store (size = %ld, reserve = %ld)\n",
  703. size, reserve_size);
  704. /*
  705. * Allocating the checkpoint space at the first space found from the
  706. * top of the virtual address space.
  707. */
  708. addr = bkeep_unmapped_any(size + reserve_size, PROT_READ|PROT_WRITE,
  709. CP_VMA_FLAGS, NULL, 0, "cpstore");
  710. if (!addr)
  711. return NULL;
  712. bkeep_munmap(addr + size, reserve_size, CP_VMA_FLAGS);
  713. }
  714. addr = (void *) DkVirtualMemoryAlloc(addr, size, 0,
  715. PAL_PROT_READ|PAL_PROT_WRITE);
  716. if (!addr)
  717. bkeep_munmap(addr, size, CP_VMA_FLAGS);
  718. return addr;
  719. }
  720. DEFINE_PROFILE_CATEGORY(migrate_proc, migrate);
  721. DEFINE_PROFILE_INTERVAL(migrate_create_process, migrate_proc);
  722. DEFINE_PROFILE_INTERVAL(migrate_create_gipc, migrate_proc);
  723. DEFINE_PROFILE_INTERVAL(migrate_connect_ipc, migrate_proc);
  724. DEFINE_PROFILE_INTERVAL(migrate_init_checkpoint, migrate_proc);
  725. DEFINE_PROFILE_INTERVAL(migrate_save_checkpoint, migrate_proc);
  726. DEFINE_PROFILE_INTERVAL(migrate_send_header, migrate_proc);
  727. DEFINE_PROFILE_INTERVAL(migrate_send_checkpoint, migrate_proc);
  728. DEFINE_PROFILE_OCCURENCE(migrate_send_on_stream, migrate_proc);
  729. DEFINE_PROFILE_OCCURENCE(migrate_send_gipc_pages, migrate_proc);
  730. DEFINE_PROFILE_INTERVAL(migrate_send_pal_handles, migrate_proc);
  731. DEFINE_PROFILE_INTERVAL(migrate_free_checkpoint, migrate_proc);
  732. DEFINE_PROFILE_INTERVAL(migrate_wait_response, migrate_proc);
  733. static bool warn_no_gipc __attribute_migratable = true;
  734. /*
  735. * Create a new process and migrate the process states to the new process.
  736. *
  737. * @migrate: migration function defined by the caller
  738. * @exec: the executable to load in the new process
  739. * @argv: arguments passed to the new process
  740. * @thread: thread handle to be migrated to the new process
  741. *
  742. * The remaining arguments are passed into the migration function.
  743. */
  744. int do_migrate_process (int (*migrate) (struct shim_cp_store *,
  745. struct shim_thread *,
  746. struct shim_process *, va_list),
  747. struct shim_handle * exec,
  748. const char ** argv,
  749. struct shim_thread * thread, ...)
  750. {
  751. int ret = 0;
  752. struct shim_process * new_process = NULL;
  753. struct newproc_header hdr;
  754. size_t bytes;
  755. memset(&hdr, 0, sizeof(hdr));
  756. #ifdef PROFILE
  757. unsigned long begin_create_time = GET_PROFILE_INTERVAL();
  758. unsigned long create_time = begin_create_time;
  759. #endif
  760. BEGIN_PROFILE_INTERVAL();
  761. /*
  762. * Create the process first. The new process requires some time
  763. * to initialize before starting to receive checkpoint data.
  764. * Parallizing the process creation and checkpointing can improve
  765. * the latency of forking.
  766. */
  767. PAL_HANDLE proc = DkProcessCreate(exec ? qstrgetstr(&exec->uri) :
  768. pal_control.executable, argv);
  769. if (!proc) {
  770. ret = -PAL_ERRNO;
  771. goto err;
  772. }
  773. SAVE_PROFILE_INTERVAL(migrate_create_process);
  774. /*
  775. * Detect if GIPC is supported by the host. If GIPC is not supported
  776. * forking may be slow because we have to use RPC streams for migrating
  777. * user memory.
  778. */
  779. bool use_gipc = false;
  780. PAL_NUM gipc_key;
  781. PAL_HANDLE gipc_hdl = DkCreatePhysicalMemoryChannel(&gipc_key);
  782. if (gipc_hdl) {
  783. debug("created gipc store: gipc:%lu\n", gipc_key);
  784. use_gipc = true;
  785. SAVE_PROFILE_INTERVAL(migrate_create_gipc);
  786. } else {
  787. if (warn_no_gipc) {
  788. warn_no_gipc = false;
  789. SYS_PRINTF("WARNING: no physical memory support, process creation "
  790. "may be slow.\n");
  791. }
  792. }
  793. /* Create process and IPC bookkeepings */
  794. if (!(new_process = create_new_process(true))) {
  795. ret = -ENOMEM;
  796. goto err;
  797. }
  798. if (!(new_process->self = create_ipc_port(0, false))) {
  799. ret = -EACCES;
  800. goto err;
  801. }
  802. SAVE_PROFILE_INTERVAL(migrate_connect_ipc);
  803. /* Allocate a space for dumping the checkpoint data. */
  804. struct shim_cp_store cpstore;
  805. memset(&cpstore, 0, sizeof(cpstore));
  806. cpstore.alloc = cp_alloc;
  807. cpstore.use_gipc = use_gipc;
  808. cpstore.bound = CP_INIT_VMA_SIZE;
  809. while (1) {
  810. /*
  811. * Try allocating a space of a certain size. If the allocation fails,
  812. * continue to try with smaller sizes.
  813. */
  814. cpstore.base = (ptr_t) cp_alloc(&cpstore, 0, cpstore.bound);
  815. if (cpstore.base)
  816. break;
  817. cpstore.bound >>= 1;
  818. if (cpstore.bound < allocsize)
  819. break;
  820. }
  821. if (!cpstore.base) {
  822. ret = -ENOMEM;
  823. debug("failed creating checkpoint store\n");
  824. goto err;
  825. }
  826. SAVE_PROFILE_INTERVAL(migrate_init_checkpoint);
  827. /* Calling the migration function defined by the caller. */
  828. va_list ap;
  829. va_start(ap, thread);
  830. ret = (*migrate) (&cpstore, thread, new_process, ap);
  831. va_end(ap);
  832. if (ret < 0) {
  833. debug("failed creating checkpoint (ret = %d)\n", ret);
  834. goto err;
  835. }
  836. SAVE_PROFILE_INTERVAL(migrate_save_checkpoint);
  837. unsigned long checkpoint_time = GET_PROFILE_INTERVAL();
  838. unsigned long checkpoint_size = cpstore.offset + cpstore.mem_size;
  839. /* Checkpoint data created. */
  840. debug("checkpoint of %lu bytes created, %lu microsecond is spent.\n",
  841. checkpoint_size, checkpoint_time);
  842. hdr.checkpoint.hdr.addr = (void *) cpstore.base;
  843. hdr.checkpoint.hdr.size = checkpoint_size;
  844. if (cpstore.mem_nentries) {
  845. hdr.checkpoint.mem.entoffset =
  846. (ptr_t) cpstore.last_mem_entry - cpstore.base;
  847. hdr.checkpoint.mem.nentries = cpstore.mem_nentries;
  848. }
  849. if (cpstore.use_gipc) {
  850. snprintf(hdr.checkpoint.gipc.uri, sizeof(hdr.checkpoint.gipc.uri),
  851. "gipc:%ld", gipc_key);
  852. if (cpstore.gipc_nentries) {
  853. hdr.checkpoint.gipc.entoffset =
  854. (ptr_t) cpstore.last_gipc_entry - cpstore.base;
  855. hdr.checkpoint.gipc.nentries = cpstore.gipc_nentries;
  856. }
  857. }
  858. if (cpstore.palhdl_nentries) {
  859. hdr.checkpoint.palhdl.entoffset =
  860. (ptr_t) cpstore.last_palhdl_entry - cpstore.base;
  861. hdr.checkpoint.palhdl.nentries = cpstore.palhdl_nentries;
  862. }
  863. #ifdef PROFILE
  864. hdr.begin_create_time = begin_create_time;
  865. hdr.create_time = create_time;
  866. hdr.write_proc_time = GET_PROFILE_INTERVAL();
  867. #endif
  868. /*
  869. * Sending a header to the new process through the RPC stream to
  870. * notify the process to start receiving the checkpoint.
  871. */
  872. bytes = DkStreamWrite(proc, 0, sizeof(struct newproc_header), &hdr, NULL);
  873. if (!bytes) {
  874. ret = -PAL_ERRNO;
  875. debug("failed writing to process stream (ret = %d)\n", ret);
  876. goto err;
  877. } else if (bytes < sizeof(struct newproc_header)) {
  878. ret = -EACCES;
  879. goto err;
  880. }
  881. ADD_PROFILE_OCCURENCE(migrate_send_on_stream, bytes);
  882. SAVE_PROFILE_INTERVAL(migrate_send_header);
  883. /* Sending the checkpoint either through GIPC or the RPC stream */
  884. ret = cpstore.use_gipc ? send_checkpoint_by_gipc(gipc_hdl, &cpstore) :
  885. send_checkpoint_on_stream(proc, &cpstore);
  886. if (ret < 0) {
  887. debug("failed sending checkpoint (ret = %d)\n", ret);
  888. goto err;
  889. }
  890. SAVE_PROFILE_INTERVAL(migrate_send_checkpoint);
  891. /*
  892. * For socket and RPC streams, we need to migrate the PAL handles
  893. * to the new process using PAL calls.
  894. */
  895. if ((ret = send_handles_on_stream(proc, &cpstore)) < 0)
  896. goto err;
  897. SAVE_PROFILE_INTERVAL(migrate_send_pal_handles);
  898. /* Free the checkpoint space */
  899. if ((ret = bkeep_munmap((void *) cpstore.base, cpstore.bound,
  900. CP_VMA_FLAGS)) < 0) {
  901. debug("failed unmaping checkpoint (ret = %d)\n", ret);
  902. goto err;
  903. }
  904. DkVirtualMemoryFree((PAL_PTR) cpstore.base, cpstore.bound);
  905. SAVE_PROFILE_INTERVAL(migrate_free_checkpoint);
  906. /* Wait for the response from the new process */
  907. struct newproc_response res;
  908. bytes = DkStreamRead(proc, 0, sizeof(struct newproc_response), &res,
  909. NULL, 0);
  910. if (bytes == 0) {
  911. ret = -PAL_ERRNO;
  912. goto err;
  913. }
  914. SAVE_PROFILE_INTERVAL(migrate_wait_response);
  915. if (gipc_hdl)
  916. DkObjectClose(gipc_hdl);
  917. /* Notify the namespace manager regarding the subleasing of TID */
  918. ipc_pid_sublease_send(res.child_vmid, thread->tid,
  919. qstrgetstr(&new_process->self->uri),
  920. NULL);
  921. /* Listen on the RPC stream to the new process */
  922. add_ipc_port_by_id(res.child_vmid, proc,
  923. IPC_PORT_DIRCLD|IPC_PORT_LISTEN|IPC_PORT_KEEPALIVE,
  924. &ipc_child_exit,
  925. NULL);
  926. destroy_process(new_process);
  927. return 0;
  928. err:
  929. if (gipc_hdl)
  930. DkObjectClose(gipc_hdl);
  931. if (proc)
  932. DkObjectClose(proc);
  933. if (new_process)
  934. destroy_process(new_process);
  935. SYS_PRINTF("process creation failed\n");
  936. return ret;
  937. }
  938. /*
  939. * Loading the checkpoint from the parent process or a checkpoint file
  940. *
  941. * @hdr: checkpoint header
  942. * @cpptr: returning the pointer of the loaded checkpoint
  943. */
  944. int do_migration (struct newproc_cp_header * hdr, void ** cpptr)
  945. {
  946. void * base = NULL;
  947. size_t size = hdr->hdr.size;
  948. PAL_PTR mapaddr;
  949. PAL_NUM mapsize;
  950. long rebase;
  951. bool use_gipc = !!hdr->gipc.uri[0];
  952. PAL_HANDLE gipc_store;
  953. int ret = 0;
  954. BEGIN_PROFILE_INTERVAL();
  955. /*
  956. * Allocate a large enough space to load the checkpoint data.
  957. *
  958. * If CPSTORE_DERANDOMIZATION is enabled, try to allocate the space
  959. * at the exact address where the checkpoint is created. Otherwise,
  960. * just allocate at the first space we found from the top of the virtual
  961. * memory space.
  962. */
  963. #if CPSTORE_DERANDOMIZATION == 1
  964. if (hdr->hdr.addr
  965. && lookup_overlap_vma(hdr->hdr.addr, size, NULL) == -ENOENT) {
  966. /* Try to load the checkpoint at the same address */
  967. base = hdr->hdr.addr;
  968. mapaddr = (PAL_PTR) ALIGN_DOWN(base);
  969. mapsize = (PAL_PTR) ALIGN_UP(base + size) - mapaddr;
  970. /* Need to create VMA before allocation */
  971. ret = bkeep_mmap((void *) mapaddr, mapsize,
  972. PROT_READ|PROT_WRITE, CP_VMA_FLAGS,
  973. NULL, 0, "cpstore");
  974. if (ret < 0)
  975. base = NULL;
  976. }
  977. #endif
  978. if (!base) {
  979. base = bkeep_unmapped_any(ALIGN_UP(size),
  980. PROT_READ|PROT_WRITE, CP_VMA_FLAGS,
  981. NULL, 0, "cpstore");
  982. if (!base)
  983. return -ENOMEM;
  984. mapaddr = (PAL_PTR) base;
  985. mapsize = (PAL_NUM) ALIGN_UP(size);
  986. }
  987. debug("checkpoint mapped at %p-%p\n", base, base + size);
  988. PAL_FLG pal_prot = PAL_PROT_READ|PAL_PROT_WRITE;
  989. PAL_PTR mapped = mapaddr;
  990. if (use_gipc) {
  991. debug("open gipc store: %s\n", hdr->gipc.uri);
  992. gipc_store = DkStreamOpen(hdr->gipc.uri, 0, 0, 0, 0);
  993. if (!gipc_store ||
  994. !DkPhysicalMemoryMap(gipc_store, 1, &mapped, &mapsize, &pal_prot))
  995. return -PAL_ERRNO;
  996. SAVE_PROFILE_INTERVAL(child_load_checkpoint_by_gipc);
  997. } else {
  998. void * mapped = DkVirtualMemoryAlloc(mapaddr, mapsize, 0, pal_prot);
  999. if (!mapped)
  1000. return -PAL_ERRNO;
  1001. }
  1002. assert(mapaddr == mapped);
  1003. /*
  1004. * If the checkpoint is loaded at a different address from where it is
  1005. * created, we need to rebase the pointers in the checkpoint.
  1006. */
  1007. rebase = (long) ((uintptr_t) base - (uintptr_t) hdr->hdr.addr);
  1008. /* Load the memory data sent separately over GIPC or the RPC stream. */
  1009. if (use_gipc) {
  1010. if ((ret = restore_gipc(gipc_store, &hdr->gipc, (ptr_t) base, rebase)) < 0)
  1011. return ret;
  1012. SAVE_PROFILE_INTERVAL(child_load_memory_by_gipc);
  1013. DkStreamDelete(gipc_store, 0);
  1014. } else {
  1015. size_t total_bytes = 0;
  1016. while (total_bytes < size) {
  1017. int bytes = DkStreamRead(PAL_CB(parent_process), 0,
  1018. size - total_bytes,
  1019. (void *) base + total_bytes, NULL, 0);
  1020. if (!bytes) {
  1021. if (PAL_ERRNO == EINTR || PAL_ERRNO == EAGAIN ||
  1022. PAL_ERRNO == EWOULDBLOCK)
  1023. continue;
  1024. return -PAL_ERRNO;
  1025. }
  1026. total_bytes += bytes;
  1027. }
  1028. SAVE_PROFILE_INTERVAL(child_load_checkpoint_on_pipe);
  1029. debug("%lu bytes read on stream\n", total_bytes);
  1030. }
  1031. /* Receive socket or RPC handles from the parent process. */
  1032. ret = receive_handles_on_stream(&hdr->palhdl, (ptr_t) base, rebase);
  1033. if (ret < 0) {
  1034. /* TODO: unload the checkpoint space */
  1035. return ret;
  1036. }
  1037. SAVE_PROFILE_INTERVAL(child_receive_handles);
  1038. migrated_memory_start = (void *) mapaddr;
  1039. migrated_memory_end = (void *) mapaddr + mapsize;
  1040. *cpptr = (void *) base;
  1041. return 0;
  1042. }
  1043. void restore_context (struct shim_context * context)
  1044. {
  1045. assert(context->regs);
  1046. struct shim_regs regs = *context->regs;
  1047. debug("restore context: SP = 0x%08lx, IP = 0x%08lx\n", regs.rsp, regs.rip);
  1048. /* don't clobber redzone. If sigaltstack is used,
  1049. * this area won't be clobbered by signal context */
  1050. *(unsigned long*) (regs.rsp - RED_ZONE_SIZE - 8) = regs.rip;
  1051. /* Ready to resume execution, re-enable preemption. */
  1052. shim_tcb_t * tcb = shim_get_tls();
  1053. __enable_preempt(tcb);
  1054. memset(context, 0, sizeof(struct shim_context));
  1055. __asm__ volatile("movq %0, %%rsp\r\n"
  1056. "addq $2 * 8, %%rsp\r\n" /* skip orig_rax and rsp */
  1057. "popq %%r15\r\n"
  1058. "popq %%r14\r\n"
  1059. "popq %%r13\r\n"
  1060. "popq %%r12\r\n"
  1061. "popq %%r11\r\n"
  1062. "popq %%r10\r\n"
  1063. "popq %%r9\r\n"
  1064. "popq %%r8\r\n"
  1065. "popq %%rcx\r\n"
  1066. "popq %%rdx\r\n"
  1067. "popq %%rsi\r\n"
  1068. "popq %%rdi\r\n"
  1069. "popq %%rbx\r\n"
  1070. "popq %%rbp\r\n"
  1071. "popfq\r\n"
  1072. "movq "XSTRINGIFY(SHIM_REGS_RSP)" - "XSTRINGIFY(SHIM_REGS_RIP)"(%%rsp), %%rsp\r\n"
  1073. "movq $0, %%rax\r\n"
  1074. "jmp *-"XSTRINGIFY(RED_ZONE_SIZE)"-8(%%rsp)\r\n"
  1075. :: "g"(&regs) : "memory");
  1076. }