shim_checkpoint.c 38 KB

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