shim_checkpoint.c 33 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174
  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_BUF) * 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. PAL_PTR addr = ALIGN_DOWN(entry->addr);
  463. PAL_NUM size = ALIGN_UP(entry->addr + entry->size) -
  464. (void *) addr;
  465. PAL_FLG prot = entry->prot;
  466. if (!DkVirtualMemoryAlloc(addr, size, 0, prot|PAL_PROT_WRITE)) {
  467. debug("fail protecting %p-%p\n", addr, addr + size);
  468. return -PAL_ERRNO;
  469. }
  470. CP_REBASE(entry->data);
  471. memcpy(entry->addr, entry->data, entry->size);
  472. if (!(entry->prot & PAL_PROT_WRITE) &&
  473. !DkVirtualMemoryProtect(addr, size, prot)) {
  474. debug("fail protecting %p-%p\n", addr, addr + size);
  475. return -PAL_ERRNO;
  476. }
  477. }
  478. }
  479. }
  480. struct shim_cp_entry * cpent = NEXT_CP_ENTRY();
  481. while (cpent) {
  482. if (cpent->cp_type < CP_FUNC_BASE)
  483. goto next;
  484. if (type && cpent->cp_type != type)
  485. goto next;
  486. rs_func rs = (&__rs_func) [cpent->cp_type - CP_FUNC_BASE];
  487. ret = (*rs) (cpent, base, offset, rebase);
  488. if (ret < 0) {
  489. debug("rs_%s failed at %p\n", CP_FUNC_NAME(cpent->cp_type),
  490. base + offset);
  491. return ret;
  492. }
  493. next:
  494. cpent = NEXT_CP_ENTRY();
  495. }
  496. debug("successfully restore checkpoint loaded at %p - %p\n",
  497. base, base + cphdr->size);
  498. return 0;
  499. }
  500. int init_from_checkpoint_file (const char * filename,
  501. struct newproc_cp_header * hdr,
  502. void ** cpptr)
  503. {
  504. struct shim_dentry * dir = NULL;
  505. int ret;
  506. ret = path_lookupat(NULL, filename, LOOKUP_ACCESS|LOOKUP_DIRECTORY, &dir);
  507. if (ret < 0)
  508. return ret;
  509. struct shim_mount * fs = dir->fs;
  510. struct shim_dirent * dirent;
  511. if (!fs->d_ops || !fs->d_ops->readdir) {
  512. ret = -EACCES;
  513. goto out;
  514. }
  515. if ((ret = fs->d_ops->readdir(dir, &dirent)) < 0)
  516. goto out;
  517. struct shim_dentry * first = NULL;
  518. struct shim_dirent * d = dirent;
  519. for ( ; d ; d = d->next) {
  520. struct shim_dentry * file;
  521. if ((ret = lookup_dentry(dir, d->name, strlen(d->name), false,
  522. &file)) < 0)
  523. continue;
  524. if (file->state & DENTRY_NEGATIVE)
  525. continue;
  526. if (!first) {
  527. first = file;
  528. continue;
  529. }
  530. const char * argv[3];
  531. argv[0] = "-resume-file";
  532. argv[1] = dentry_get_path(file, true, NULL);
  533. argv[2] = 0;
  534. PAL_HANDLE proc = DkProcessCreate(NULL, 0, argv);
  535. if (!proc) {
  536. ret = -PAL_ERRNO;
  537. goto out;
  538. }
  539. put_dentry(file);
  540. }
  541. if (first) {
  542. ret = restore_from_file(dentry_get_path(first, true, NULL), hdr, cpptr);
  543. put_dentry(first);
  544. }
  545. free(dirent);
  546. out:
  547. put_dentry(dir);
  548. return ret;
  549. }
  550. int restore_from_file (const char * filename, struct newproc_cp_header * hdr,
  551. void ** cpptr)
  552. {
  553. struct shim_handle * file = get_new_handle();
  554. if (!file)
  555. return -ENOMEM;
  556. int ret = open_namei(file, NULL, filename, O_RDWR, 0, NULL);
  557. if (ret < 0) {
  558. put_handle(file);
  559. return ret;
  560. }
  561. struct shim_mount * fs = file->fs;
  562. open_handle(file);
  563. debug("restore %s\n", filename);
  564. struct cp_header cphdr;
  565. ret = fs->fs_ops->read(file, &cphdr, sizeof(struct cp_header));
  566. if (ret < 0)
  567. goto out;
  568. void * cpaddr = cphdr.addr;
  569. ret = fs->fs_ops->mmap(file, &cpaddr, ALIGN_UP(cphdr.size),
  570. PROT_READ|PROT_WRITE,
  571. MAP_PRIVATE|MAP_FILE, 0);
  572. if (ret < 0)
  573. goto out;
  574. hdr->hdr = cphdr;
  575. *cpptr = cpaddr;
  576. migrated_memory_start = cpaddr;
  577. migrated_memory_end = cpaddr + hdr->hdr.size;
  578. out:
  579. close_handle(file);
  580. return ret;
  581. }
  582. int send_handles_on_stream (PAL_HANDLE stream, struct shim_cp_store * store)
  583. {
  584. int nentries = store->palhdl_nentries;
  585. if (!nentries)
  586. return 0;
  587. struct shim_palhdl_entry ** entries =
  588. __alloca(sizeof(struct shim_palhdl_entry *) * nentries);
  589. struct shim_palhdl_entry * entry = store->last_palhdl_entry;
  590. int cnt = nentries;
  591. for ( ; entry ; entry = entry->prev)
  592. if (entry->handle) {
  593. if (!cnt)
  594. return -EINVAL;
  595. entries[--cnt] = entry;
  596. }
  597. entries += cnt;
  598. nentries -= cnt;
  599. for (int i = 0 ; i < nentries ; i++)
  600. if (!DkSendHandle(stream, entries[i]->handle))
  601. entries[i]->handle = NULL;
  602. return 0;
  603. }
  604. int receive_handles_on_stream (struct palhdl_header * hdr, ptr_t base,
  605. long rebase)
  606. {
  607. struct shim_palhdl_entry * palhdl_entries =
  608. (void *) (base + hdr->entoffset);
  609. int nentries = hdr->nentries;
  610. if (!nentries)
  611. return 0;
  612. debug("receive handles: %d entries\n", nentries);
  613. struct shim_palhdl_entry ** entries =
  614. __alloca(sizeof(struct shim_palhdl_entry *) * nentries);
  615. struct shim_palhdl_entry * entry = palhdl_entries;
  616. int cnt = nentries;
  617. for ( ; entry ; entry = entry->prev) {
  618. CP_REBASE(entry->prev);
  619. CP_REBASE(entry->phandle);
  620. if (!cnt)
  621. return -EINVAL;
  622. entries[--cnt] = entry;
  623. }
  624. entries += cnt;
  625. nentries -= cnt;
  626. for (int i = 0 ; i < nentries ; i++) {
  627. entry = entries[i];
  628. if (entry->handle) {
  629. PAL_HANDLE hdl = DkReceiveHandle(PAL_CB(parent_process));
  630. if (hdl) {
  631. *entry->phandle = hdl;
  632. continue;
  633. }
  634. }
  635. }
  636. return 0;
  637. }
  638. #define NTRIES 4
  639. static void * cp_alloc (struct shim_cp_store * store, void * addr, int size)
  640. {
  641. void * requested = addr;
  642. struct shim_vma * vma;
  643. int ret, n = 0;
  644. if (!requested) {
  645. again:
  646. if (n == NTRIES)
  647. return NULL;
  648. if (!(addr = get_unmapped_vma_for_cp(size)))
  649. return NULL;
  650. } else {
  651. ret = lookup_overlap_vma(addr, size, &vma);
  652. if (!ret) {
  653. if (vma->addr != addr || vma->length != size ||
  654. !(vma->flags & VMA_UNMAPPED)) {
  655. put_vma(vma);
  656. return NULL;
  657. }
  658. }
  659. }
  660. addr = (void *) DkVirtualMemoryAlloc(addr, size, 0,
  661. PAL_PROT_READ|PAL_PROT_WRITE);
  662. if (!addr) {
  663. if (!requested)
  664. goto again;
  665. return NULL;
  666. }
  667. if (requested && addr != requested) {
  668. DkVirtualMemoryFree(addr, size);
  669. return NULL;
  670. }
  671. return addr;
  672. }
  673. DEFINE_PROFILE_CATAGORY(migrate_proc, migrate);
  674. DEFINE_PROFILE_INTERVAL(migrate_create_process, migrate_proc);
  675. DEFINE_PROFILE_INTERVAL(migrate_create_gipc, migrate_proc);
  676. DEFINE_PROFILE_INTERVAL(migrate_connect_ipc, migrate_proc);
  677. DEFINE_PROFILE_INTERVAL(migrate_init_checkpoint, migrate_proc);
  678. DEFINE_PROFILE_INTERVAL(migrate_save_checkpoint, migrate_proc);
  679. DEFINE_PROFILE_INTERVAL(migrate_send_header, migrate_proc);
  680. DEFINE_PROFILE_INTERVAL(migrate_send_checkpoint, migrate_proc);
  681. DEFINE_PROFILE_OCCURENCE(migrate_send_on_stream, migrate_proc);
  682. DEFINE_PROFILE_OCCURENCE(migrate_send_gipc_pages, migrate_proc);
  683. DEFINE_PROFILE_INTERVAL(migrate_send_pal_handles, migrate_proc);
  684. DEFINE_PROFILE_INTERVAL(migrate_free_checkpoint, migrate_proc);
  685. DEFINE_PROFILE_INTERVAL(migrate_wait_response, migrate_proc);
  686. int do_migrate_process (int (*migrate) (struct shim_cp_store *,
  687. struct shim_thread *,
  688. struct shim_process *, va_list),
  689. struct shim_handle * exec,
  690. const char ** argv,
  691. struct shim_thread * thread, ...)
  692. {
  693. int ret = 0;
  694. struct shim_process * new_process = NULL;
  695. struct newproc_header hdr;
  696. struct shim_cp_store * cpstore = NULL;
  697. int bytes;
  698. memset(&hdr, 0, sizeof(hdr));
  699. #ifdef PROFILE
  700. unsigned long begin_create_time = GET_PROFILE_INTERVAL();
  701. unsigned long create_time = begin_create_time;
  702. #endif
  703. BEGIN_PROFILE_INTERVAL();
  704. PAL_HANDLE proc = DkProcessCreate(exec ? qstrgetstr(&exec->uri) :
  705. pal_control.executable,
  706. 0, argv);
  707. if (!proc) {
  708. ret = -PAL_ERRNO;
  709. goto err;
  710. }
  711. SAVE_PROFILE_INTERVAL(migrate_create_process);
  712. bool use_gipc = false;
  713. PAL_NUM gipc_key;
  714. PAL_HANDLE gipc_hdl = DkCreatePhysicalMemoryChannel(&gipc_key);
  715. if (gipc_hdl) {
  716. debug("created gipc store: gipc:%lu\n", gipc_key);
  717. use_gipc = true;
  718. SAVE_PROFILE_INTERVAL(migrate_create_gipc);
  719. } else {
  720. sys_printf("WARNING: no physical memory support, process creation "
  721. "will be slow.\n");
  722. }
  723. if (!(new_process = create_new_process(true))) {
  724. ret = -ENOMEM;
  725. goto err;
  726. }
  727. if (!(new_process->self = create_ipc_port(0, false))) {
  728. ret = -EACCES;
  729. goto err;
  730. }
  731. SAVE_PROFILE_INTERVAL(migrate_connect_ipc);
  732. cpstore = __alloca(sizeof(struct shim_cp_store));
  733. memset(cpstore, 0, sizeof(struct shim_cp_store));
  734. cpstore->alloc = cp_alloc;
  735. cpstore->use_gipc = use_gipc;
  736. cpstore->bound = CP_INIT_VMA_SIZE;
  737. while (1) {
  738. cpstore->base = (ptr_t) cp_alloc(cpstore, 0, cpstore->bound);
  739. if (cpstore->base)
  740. break;
  741. cpstore->bound >>= 1;
  742. if (cpstore->bound < allocsize)
  743. break;
  744. }
  745. if (!cpstore->base) {
  746. ret = -ENOMEM;
  747. goto err;
  748. }
  749. SAVE_PROFILE_INTERVAL(migrate_init_checkpoint);
  750. va_list ap;
  751. va_start(ap, thread);
  752. ret = (*migrate) (cpstore, thread, new_process, ap);
  753. va_end(ap);
  754. if (ret < 0)
  755. goto err;
  756. SAVE_PROFILE_INTERVAL(migrate_save_checkpoint);
  757. unsigned long checkpoint_time = GET_PROFILE_INTERVAL();
  758. unsigned long checkpoint_size = cpstore->offset + cpstore->mem_size;
  759. debug("checkpoint of %u bytes created, %lu microsecond is spent.\n",
  760. checkpoint_size, checkpoint_time);
  761. hdr.checkpoint.hdr.addr = (void *) cpstore->base;
  762. hdr.checkpoint.hdr.size = checkpoint_size;
  763. if (cpstore->mem_nentries) {
  764. hdr.checkpoint.mem.entoffset =
  765. (ptr_t) cpstore->last_mem_entry - cpstore->base;
  766. hdr.checkpoint.mem.nentries = cpstore->mem_nentries;
  767. }
  768. if (cpstore->use_gipc) {
  769. snprintf(hdr.checkpoint.gipc.uri, sizeof(hdr.checkpoint.gipc.uri),
  770. "gipc:%lld", gipc_key);
  771. if (cpstore->gipc_nentries) {
  772. hdr.checkpoint.gipc.entoffset =
  773. (ptr_t) cpstore->last_gipc_entry - cpstore->base;
  774. hdr.checkpoint.gipc.nentries = cpstore->gipc_nentries;
  775. }
  776. }
  777. if (cpstore->palhdl_nentries) {
  778. hdr.checkpoint.palhdl.entoffset =
  779. (ptr_t) cpstore->last_palhdl_entry - cpstore->base;
  780. hdr.checkpoint.palhdl.nentries = cpstore->palhdl_nentries;
  781. }
  782. #ifdef PROFILE
  783. hdr.begin_create_time = begin_create_time;
  784. hdr.create_time = create_time;
  785. hdr.write_proc_time = GET_PROFILE_INTERVAL();
  786. #endif
  787. bytes = DkStreamWrite(proc, 0, sizeof(struct newproc_header), &hdr, NULL);
  788. if (!bytes) {
  789. ret = -PAL_ERRNO;
  790. goto err;
  791. } else if (bytes < sizeof(struct newproc_header)) {
  792. ret = -EACCES;
  793. goto err;
  794. }
  795. ADD_PROFILE_OCCURENCE(migrate_send_on_stream, bytes);
  796. SAVE_PROFILE_INTERVAL(migrate_send_header);
  797. ret = cpstore->use_gipc ? send_checkpoint_by_gipc(gipc_hdl, cpstore) :
  798. send_checkpoint_on_stream(proc, cpstore);
  799. if (ret < 0)
  800. goto err;
  801. SAVE_PROFILE_INTERVAL(migrate_send_checkpoint);
  802. if ((ret = send_handles_on_stream(proc, cpstore)) < 0)
  803. goto err;
  804. SAVE_PROFILE_INTERVAL(migrate_send_pal_handles);
  805. system_free((void *) cpstore->base, cpstore->bound);
  806. SAVE_PROFILE_INTERVAL(migrate_free_checkpoint);
  807. struct newproc_response res;
  808. bytes = DkStreamRead(proc, 0, sizeof(struct newproc_response), &res,
  809. NULL, 0);
  810. if (bytes == 0) {
  811. ret = -PAL_ERRNO;
  812. goto err;
  813. }
  814. SAVE_PROFILE_INTERVAL(migrate_wait_response);
  815. if (gipc_hdl)
  816. DkObjectClose(gipc_hdl);
  817. ipc_pid_sublease_send(res.child_vmid, thread->tid,
  818. qstrgetstr(&new_process->self->uri),
  819. NULL);
  820. add_ipc_port_by_id(res.child_vmid, proc,
  821. IPC_PORT_DIRCLD|IPC_PORT_LISTEN|IPC_PORT_KEEPALIVE,
  822. &ipc_child_exit,
  823. NULL);
  824. destroy_process(new_process);
  825. return 0;
  826. err:
  827. if (gipc_hdl)
  828. DkObjectClose(gipc_hdl);
  829. if (proc)
  830. DkObjectClose(proc);
  831. if (new_process)
  832. destroy_process(new_process);
  833. sys_printf("process creation failed\n");
  834. return ret;
  835. }
  836. int do_migration (struct newproc_cp_header * hdr, void ** cpptr)
  837. {
  838. ptr_t base = (ptr_t) hdr->hdr.addr;
  839. int size = hdr->hdr.size;
  840. PAL_PTR mapaddr;
  841. PAL_NUM mapsize;
  842. unsigned long mapoff;
  843. long rebase;
  844. bool use_gipc = !!hdr->gipc.uri[0];
  845. PAL_HANDLE gipc_store;
  846. int ret = 0;
  847. debug("checkpoint detected (%d bytes, expected at %p)\n",
  848. size, base);
  849. if (base && !lookup_overlap_vma((void *) base, size, NULL)) {
  850. mapaddr = (PAL_PTR) ALIGN_DOWN(base);
  851. mapsize = (PAL_PTR) ALIGN_UP(base + size) - mapaddr;
  852. mapoff = base - (ptr_t) mapaddr;
  853. } else {
  854. mapaddr = (PAL_PTR) 0;
  855. mapsize = ALIGN_UP(size);
  856. mapoff = 0;
  857. }
  858. BEGIN_PROFILE_INTERVAL();
  859. if (use_gipc) {
  860. debug("open gipc store: %s\n", hdr->gipc.uri);
  861. PAL_FLG mapprot = PAL_PROT_READ|PAL_PROT_WRITE;
  862. gipc_store = DkStreamOpen(hdr->gipc.uri, 0, 0, 0, 0);
  863. if (!gipc_store ||
  864. !DkPhysicalMemoryMap(gipc_store, 1, &mapaddr, &mapsize, &mapprot))
  865. return -PAL_ERRNO;
  866. SAVE_PROFILE_INTERVAL(child_load_checkpoint_by_gipc);
  867. } else {
  868. if (!(mapaddr = DkVirtualMemoryAlloc(mapaddr, mapsize, 0,
  869. PAL_PROT_READ|PAL_PROT_WRITE)))
  870. return -PAL_ERRNO;
  871. }
  872. bkeep_mmap((void *) mapaddr, mapsize,
  873. PROT_READ|PROT_WRITE,
  874. MAP_PRIVATE|MAP_ANONYMOUS|VMA_INTERNAL,
  875. NULL, 0, NULL);
  876. base = (ptr_t) mapaddr + mapoff;
  877. rebase = (long) base - (long) hdr->hdr.addr;
  878. debug("checkpoint loaded at %p\n", base);
  879. if (use_gipc) {
  880. if ((ret = restore_gipc(gipc_store, &hdr->gipc, base, rebase)) < 0)
  881. return ret;
  882. SAVE_PROFILE_INTERVAL(child_load_memory_by_gipc);
  883. DkStreamDelete(gipc_store, 0);
  884. } else {
  885. int total_bytes = 0;
  886. while (total_bytes < size) {
  887. int bytes = DkStreamRead(PAL_CB(parent_process), 0,
  888. size - total_bytes,
  889. (void *) base + total_bytes, NULL, 0);
  890. if (!bytes)
  891. return -PAL_ERRNO;
  892. total_bytes += bytes;
  893. }
  894. SAVE_PROFILE_INTERVAL(child_load_checkpoint_on_pipe);
  895. debug("%d bytes read on stream\n", total_bytes);
  896. }
  897. struct newproc_response res;
  898. res.child_vmid = cur_process.vmid;
  899. res.failure = 0;
  900. int bytes = DkStreamWrite(PAL_CB(parent_process), 0,
  901. sizeof(struct newproc_response),
  902. &res, NULL);
  903. if (!bytes)
  904. return -PAL_ERRNO;
  905. if ((ret = receive_handles_on_stream(&hdr->palhdl, base, rebase)) < 0)
  906. return ret;
  907. SAVE_PROFILE_INTERVAL(child_receive_handles);
  908. migrated_memory_start = (void *) mapaddr;
  909. migrated_memory_end = (void *) mapaddr + mapsize;
  910. *cpptr = (void *) base;
  911. return 0;
  912. }
  913. void restore_context (struct shim_context * context)
  914. {
  915. int nregs = sizeof(struct shim_regs) / sizeof(void *);
  916. void * regs[nregs + 1];
  917. if (context->regs)
  918. memcpy(regs, context->regs, sizeof(struct shim_regs));
  919. else
  920. memset(regs, 0, sizeof(struct shim_regs));
  921. debug("restore context: SP = %p, IP = %p\n", context->sp, context->ret_ip);
  922. regs[nregs] = (void *) context->sp - 8;
  923. *(void **) (context->sp - 8) = context->ret_ip;
  924. memset(context, 0, sizeof(struct shim_context));
  925. asm volatile("movq %0, %%rsp\r\n"
  926. "popq %%r15\r\n"
  927. "popq %%r14\r\n"
  928. "popq %%r13\r\n"
  929. "popq %%r12\r\n"
  930. "popq %%r9\r\n"
  931. "popq %%r8\r\n"
  932. "popq %%rcx\r\n"
  933. "popq %%rdx\r\n"
  934. "popq %%rsi\r\n"
  935. "popq %%rdi\r\n"
  936. "popq %%rbx\r\n"
  937. "popq %%rbp\r\n"
  938. "popq %%rsp\r\n"
  939. "movq $0, %%rax\r\n"
  940. "retq\r\n"
  941. :: "g"(&regs) : "memory");
  942. }