shim_checkpoint.c 37 KB

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