shim_checkpoint.c 39 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328
  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_process())) {
  795. ret = -ENOMEM;
  796. goto err;
  797. }
  798. if (!(new_process->self = create_ipc_info(0, NULL, 0))) {
  799. ret = -EACCES;
  800. goto err;
  801. }
  802. char pipe_uri[PIPE_URI_SIZE];
  803. if (create_pipe(NULL, pipe_uri, PIPE_URI_SIZE, &new_process->self->pal_handle,
  804. &new_process->self->uri) < 0) {
  805. ret = -EACCES;
  806. goto err;
  807. }
  808. SAVE_PROFILE_INTERVAL(migrate_connect_ipc);
  809. /* Allocate a space for dumping the checkpoint data. */
  810. struct shim_cp_store cpstore;
  811. memset(&cpstore, 0, sizeof(cpstore));
  812. cpstore.alloc = cp_alloc;
  813. cpstore.use_gipc = use_gipc;
  814. cpstore.bound = CP_INIT_VMA_SIZE;
  815. while (1) {
  816. /*
  817. * Try allocating a space of a certain size. If the allocation fails,
  818. * continue to try with smaller sizes.
  819. */
  820. cpstore.base = (ptr_t) cp_alloc(&cpstore, 0, cpstore.bound);
  821. if (cpstore.base)
  822. break;
  823. cpstore.bound >>= 1;
  824. if (cpstore.bound < allocsize)
  825. break;
  826. }
  827. if (!cpstore.base) {
  828. ret = -ENOMEM;
  829. debug("failed creating checkpoint store\n");
  830. goto err;
  831. }
  832. SAVE_PROFILE_INTERVAL(migrate_init_checkpoint);
  833. /* Calling the migration function defined by the caller. */
  834. va_list ap;
  835. va_start(ap, thread);
  836. ret = (*migrate) (&cpstore, thread, new_process, ap);
  837. va_end(ap);
  838. if (ret < 0) {
  839. debug("failed creating checkpoint (ret = %d)\n", ret);
  840. goto err;
  841. }
  842. SAVE_PROFILE_INTERVAL(migrate_save_checkpoint);
  843. unsigned long checkpoint_time = GET_PROFILE_INTERVAL();
  844. unsigned long checkpoint_size = cpstore.offset + cpstore.mem_size;
  845. /* Checkpoint data created. */
  846. debug("checkpoint of %lu bytes created, %lu microsecond is spent.\n",
  847. checkpoint_size, checkpoint_time);
  848. hdr.checkpoint.hdr.addr = (void *) cpstore.base;
  849. hdr.checkpoint.hdr.size = checkpoint_size;
  850. if (cpstore.mem_nentries) {
  851. hdr.checkpoint.mem.entoffset =
  852. (ptr_t) cpstore.last_mem_entry - cpstore.base;
  853. hdr.checkpoint.mem.nentries = cpstore.mem_nentries;
  854. }
  855. if (cpstore.use_gipc) {
  856. snprintf(hdr.checkpoint.gipc.uri, sizeof(hdr.checkpoint.gipc.uri),
  857. "gipc:%ld", gipc_key);
  858. if (cpstore.gipc_nentries) {
  859. hdr.checkpoint.gipc.entoffset =
  860. (ptr_t) cpstore.last_gipc_entry - cpstore.base;
  861. hdr.checkpoint.gipc.nentries = cpstore.gipc_nentries;
  862. }
  863. }
  864. if (cpstore.palhdl_nentries) {
  865. hdr.checkpoint.palhdl.entoffset =
  866. (ptr_t) cpstore.last_palhdl_entry - cpstore.base;
  867. hdr.checkpoint.palhdl.nentries = cpstore.palhdl_nentries;
  868. }
  869. #ifdef PROFILE
  870. hdr.begin_create_time = begin_create_time;
  871. hdr.create_time = create_time;
  872. hdr.write_proc_time = GET_PROFILE_INTERVAL();
  873. #endif
  874. /*
  875. * Sending a header to the new process through the RPC stream to
  876. * notify the process to start receiving the checkpoint.
  877. */
  878. bytes = DkStreamWrite(proc, 0, sizeof(struct newproc_header), &hdr, NULL);
  879. if (!bytes) {
  880. ret = -PAL_ERRNO;
  881. debug("failed writing to process stream (ret = %d)\n", ret);
  882. goto err;
  883. } else if (bytes < sizeof(struct newproc_header)) {
  884. ret = -EACCES;
  885. goto err;
  886. }
  887. ADD_PROFILE_OCCURENCE(migrate_send_on_stream, bytes);
  888. SAVE_PROFILE_INTERVAL(migrate_send_header);
  889. /* Sending the checkpoint either through GIPC or the RPC stream */
  890. ret = cpstore.use_gipc ? send_checkpoint_by_gipc(gipc_hdl, &cpstore) :
  891. send_checkpoint_on_stream(proc, &cpstore);
  892. if (ret < 0) {
  893. debug("failed sending checkpoint (ret = %d)\n", ret);
  894. goto err;
  895. }
  896. SAVE_PROFILE_INTERVAL(migrate_send_checkpoint);
  897. /*
  898. * For socket and RPC streams, we need to migrate the PAL handles
  899. * to the new process using PAL calls.
  900. */
  901. if ((ret = send_handles_on_stream(proc, &cpstore)) < 0)
  902. goto err;
  903. SAVE_PROFILE_INTERVAL(migrate_send_pal_handles);
  904. /* Free the checkpoint space */
  905. if ((ret = bkeep_munmap((void *) cpstore.base, cpstore.bound,
  906. CP_VMA_FLAGS)) < 0) {
  907. debug("failed unmaping checkpoint (ret = %d)\n", ret);
  908. goto err;
  909. }
  910. DkVirtualMemoryFree((PAL_PTR) cpstore.base, cpstore.bound);
  911. SAVE_PROFILE_INTERVAL(migrate_free_checkpoint);
  912. /* Wait for the response from the new process */
  913. struct newproc_response res;
  914. bytes = DkStreamRead(proc, 0, sizeof(struct newproc_response), &res,
  915. NULL, 0);
  916. if (bytes == 0) {
  917. ret = -PAL_ERRNO;
  918. goto err;
  919. }
  920. SAVE_PROFILE_INTERVAL(migrate_wait_response);
  921. if (gipc_hdl)
  922. DkObjectClose(gipc_hdl);
  923. /* Notify the namespace manager regarding the subleasing of TID */
  924. ipc_pid_sublease_send(res.child_vmid, thread->tid,
  925. qstrgetstr(&new_process->self->uri),
  926. NULL);
  927. /* Listen on the RPC stream to the new process */
  928. add_ipc_port_by_id(res.child_vmid, proc,
  929. IPC_PORT_DIRCLD|IPC_PORT_LISTEN|IPC_PORT_KEEPALIVE,
  930. &ipc_child_exit,
  931. NULL);
  932. free_process(new_process);
  933. return 0;
  934. err:
  935. if (gipc_hdl)
  936. DkObjectClose(gipc_hdl);
  937. if (proc)
  938. DkObjectClose(proc);
  939. if (new_process)
  940. free_process(new_process);
  941. SYS_PRINTF("process creation failed\n");
  942. return ret;
  943. }
  944. /*
  945. * Loading the checkpoint from the parent process or a checkpoint file
  946. *
  947. * @hdr: checkpoint header
  948. * @cpptr: returning the pointer of the loaded checkpoint
  949. */
  950. int do_migration (struct newproc_cp_header * hdr, void ** cpptr)
  951. {
  952. void * base = NULL;
  953. size_t size = hdr->hdr.size;
  954. PAL_PTR mapaddr;
  955. PAL_NUM mapsize;
  956. long rebase;
  957. bool use_gipc = !!hdr->gipc.uri[0];
  958. PAL_HANDLE gipc_store;
  959. int ret = 0;
  960. BEGIN_PROFILE_INTERVAL();
  961. /*
  962. * Allocate a large enough space to load the checkpoint data.
  963. *
  964. * If CPSTORE_DERANDOMIZATION is enabled, try to allocate the space
  965. * at the exact address where the checkpoint is created. Otherwise,
  966. * just allocate at the first space we found from the top of the virtual
  967. * memory space.
  968. */
  969. #if CPSTORE_DERANDOMIZATION == 1
  970. if (hdr->hdr.addr
  971. && lookup_overlap_vma(hdr->hdr.addr, size, NULL) == -ENOENT) {
  972. /* Try to load the checkpoint at the same address */
  973. base = hdr->hdr.addr;
  974. mapaddr = (PAL_PTR) ALIGN_DOWN(base);
  975. mapsize = (PAL_PTR) ALIGN_UP(base + size) - mapaddr;
  976. /* Need to create VMA before allocation */
  977. ret = bkeep_mmap((void *) mapaddr, mapsize,
  978. PROT_READ|PROT_WRITE, CP_VMA_FLAGS,
  979. NULL, 0, "cpstore");
  980. if (ret < 0)
  981. base = NULL;
  982. }
  983. #endif
  984. if (!base) {
  985. base = bkeep_unmapped_any(ALIGN_UP(size),
  986. PROT_READ|PROT_WRITE, CP_VMA_FLAGS,
  987. NULL, 0, "cpstore");
  988. if (!base)
  989. return -ENOMEM;
  990. mapaddr = (PAL_PTR) base;
  991. mapsize = (PAL_NUM) ALIGN_UP(size);
  992. }
  993. debug("checkpoint mapped at %p-%p\n", base, base + size);
  994. PAL_FLG pal_prot = PAL_PROT_READ|PAL_PROT_WRITE;
  995. PAL_PTR mapped = mapaddr;
  996. if (use_gipc) {
  997. debug("open gipc store: %s\n", hdr->gipc.uri);
  998. gipc_store = DkStreamOpen(hdr->gipc.uri, 0, 0, 0, 0);
  999. if (!gipc_store ||
  1000. !DkPhysicalMemoryMap(gipc_store, 1, &mapped, &mapsize, &pal_prot))
  1001. return -PAL_ERRNO;
  1002. SAVE_PROFILE_INTERVAL(child_load_checkpoint_by_gipc);
  1003. } else {
  1004. void * mapped = DkVirtualMemoryAlloc(mapaddr, mapsize, 0, pal_prot);
  1005. if (!mapped)
  1006. return -PAL_ERRNO;
  1007. }
  1008. assert(mapaddr == mapped);
  1009. /*
  1010. * If the checkpoint is loaded at a different address from where it is
  1011. * created, we need to rebase the pointers in the checkpoint.
  1012. */
  1013. rebase = (long) ((uintptr_t) base - (uintptr_t) hdr->hdr.addr);
  1014. /* Load the memory data sent separately over GIPC or the RPC stream. */
  1015. if (use_gipc) {
  1016. if ((ret = restore_gipc(gipc_store, &hdr->gipc, (ptr_t) base, rebase)) < 0)
  1017. return ret;
  1018. SAVE_PROFILE_INTERVAL(child_load_memory_by_gipc);
  1019. DkStreamDelete(gipc_store, 0);
  1020. } else {
  1021. size_t total_bytes = 0;
  1022. while (total_bytes < size) {
  1023. int bytes = DkStreamRead(PAL_CB(parent_process), 0,
  1024. size - total_bytes,
  1025. (void *) base + total_bytes, NULL, 0);
  1026. if (!bytes) {
  1027. if (PAL_ERRNO == EINTR || PAL_ERRNO == EAGAIN ||
  1028. PAL_ERRNO == EWOULDBLOCK)
  1029. continue;
  1030. return -PAL_ERRNO;
  1031. }
  1032. total_bytes += bytes;
  1033. }
  1034. SAVE_PROFILE_INTERVAL(child_load_checkpoint_on_pipe);
  1035. debug("%lu bytes read on stream\n", total_bytes);
  1036. }
  1037. /* Receive socket or RPC handles from the parent process. */
  1038. ret = receive_handles_on_stream(&hdr->palhdl, (ptr_t) base, rebase);
  1039. if (ret < 0) {
  1040. /* TODO: unload the checkpoint space */
  1041. return ret;
  1042. }
  1043. SAVE_PROFILE_INTERVAL(child_receive_handles);
  1044. migrated_memory_start = (void *) mapaddr;
  1045. migrated_memory_end = (void *) mapaddr + mapsize;
  1046. *cpptr = (void *) base;
  1047. return 0;
  1048. }
  1049. void restore_context (struct shim_context * context)
  1050. {
  1051. assert(context->regs);
  1052. struct shim_regs regs = *context->regs;
  1053. debug("restore context: SP = 0x%08lx, IP = 0x%08lx\n", regs.rsp, regs.rip);
  1054. /* don't clobber redzone. If sigaltstack is used,
  1055. * this area won't be clobbered by signal context */
  1056. *(unsigned long*) (regs.rsp - RED_ZONE_SIZE - 8) = regs.rip;
  1057. /* Ready to resume execution, re-enable preemption. */
  1058. shim_tcb_t * tcb = shim_get_tls();
  1059. __enable_preempt(tcb);
  1060. memset(context, 0, sizeof(struct shim_context));
  1061. __asm__ volatile("movq %0, %%rsp\r\n"
  1062. "addq $2 * 8, %%rsp\r\n" /* skip orig_rax and rsp */
  1063. "popq %%r15\r\n"
  1064. "popq %%r14\r\n"
  1065. "popq %%r13\r\n"
  1066. "popq %%r12\r\n"
  1067. "popq %%r11\r\n"
  1068. "popq %%r10\r\n"
  1069. "popq %%r9\r\n"
  1070. "popq %%r8\r\n"
  1071. "popq %%rcx\r\n"
  1072. "popq %%rdx\r\n"
  1073. "popq %%rsi\r\n"
  1074. "popq %%rdi\r\n"
  1075. "popq %%rbx\r\n"
  1076. "popq %%rbp\r\n"
  1077. "popfq\r\n"
  1078. "movq "XSTRINGIFY(SHIM_REGS_RSP)" - "XSTRINGIFY(SHIM_REGS_RIP)"(%%rsp), %%rsp\r\n"
  1079. "movq $0, %%rax\r\n"
  1080. "jmp *-"XSTRINGIFY(RED_ZONE_SIZE)"-8(%%rsp)\r\n"
  1081. :: "g"(&regs) : "memory");
  1082. }