shim_ipc.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769
  1. /* -*- mode:c; c-file-style:"k&r"; c-basic-offset: 4; tab-width:4; indent-tabs-mode:nil; mode:auto-fill; fill-column:78; -*- */
  2. /* vim: set ts=4 sw=4 et tw=78 fo=cqt wm=0: */
  3. /* Copyright (C) 2014 OSCAR lab, Stony Brook University
  4. This file is part of Graphene Library OS.
  5. Graphene Library OS is free software: you can redistribute it and/or
  6. modify it under the terms of the GNU General Public License
  7. as published by the Free Software Foundation, either version 3 of the
  8. License, or (at your option) any later version.
  9. Graphene Library OS is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_ipc.c
  17. *
  18. * This file contains codes to maintain generic bookkeeping of IPC.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_utils.h>
  22. #include <shim_thread.h>
  23. #include <shim_handle.h>
  24. #include <shim_ipc.h>
  25. #include <shim_checkpoint.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <linux_list.h>
  30. #define ipc_info_mgr_ALLOC 32
  31. #define PAGE_SIZE allocsize
  32. #define OBJ_TYPE struct shim_ipc_info
  33. #include "memmgr.h"
  34. static MEM_MGR ipc_info_mgr;
  35. LOCKTYPE ipc_info_lock;
  36. struct shim_process cur_process;
  37. DEFINE_PROFILE_CATAGORY(ipc, );
  38. DEFINE_PROFILE_OCCURENCE(syscall_use_ipc, ipc);
  39. //#define DEBUG_REF
  40. int init_ipc_ports (void);
  41. int init_ns_pid (void);
  42. int init_ns_sysv (void);
  43. int init_ipc (void)
  44. {
  45. int ret = 0;
  46. if (!cur_process.vmid) {
  47. if (getrand(&cur_process.vmid, sizeof(IDTYPE)) < sizeof(IDTYPE))
  48. return -EINVAL;
  49. debug("process started: %u\n", cur_process.vmid);
  50. }
  51. create_lock(ipc_info_lock);
  52. if (!(ipc_info_mgr = create_mem_mgr(init_align_up(ipc_info_mgr_ALLOC))))
  53. return -ENOMEM;
  54. if ((ret = init_ipc_ports()) < 0)
  55. return ret;
  56. if ((ret = init_ns_pid()) < 0)
  57. return ret;
  58. if ((ret = init_ns_sysv()) < 0)
  59. return ret;
  60. return 0;
  61. }
  62. int prepare_ns_leaders (void)
  63. {
  64. int ret = 0;
  65. if ((ret = prepare_pid_leader()) < 0)
  66. return ret;
  67. if ((ret = prepare_sysv_leader()) < 0)
  68. return ret;
  69. return 0;
  70. }
  71. static struct shim_ipc_info * __get_new_ipc_info (IDTYPE vmid, const char * uri,
  72. size_t len)
  73. {
  74. struct shim_ipc_info * info =
  75. get_mem_obj_from_mgr_enlarge(ipc_info_mgr,
  76. size_align_up(ipc_info_mgr_ALLOC));
  77. if (!info)
  78. return NULL;
  79. memset(info, 0, sizeof(struct shim_ipc_info));
  80. if (vmid)
  81. info->vmid = vmid;
  82. if (uri)
  83. qstrsetstr(&info->uri, uri, len);
  84. REF_SET(info->ref_count, 1);
  85. INIT_HLIST_NODE(&info->hlist);
  86. return info;
  87. }
  88. struct shim_ipc_info * get_new_ipc_info (IDTYPE vmid, const char * uri,
  89. size_t len)
  90. {
  91. lock(ipc_info_lock);
  92. struct shim_ipc_info * info = __get_new_ipc_info(vmid, uri, len);
  93. unlock(ipc_info_lock);
  94. return info;
  95. }
  96. static void __get_ipc_info (struct shim_ipc_info * info)
  97. {
  98. #ifdef DEBUG_REF
  99. int ref_count = REF_INC(info->ref_count);
  100. debug("get port %p (vmid %u uri %s, ref_count = %d)\n", info,
  101. info->vmid, qstrgetstr(&info->uri), ref_count);
  102. #else
  103. REF_INC(info->ref_count);
  104. #endif
  105. }
  106. void get_ipc_info (struct shim_ipc_info * info)
  107. {
  108. __get_ipc_info(info);
  109. }
  110. static void unset_ipc_info (struct shim_ipc_info * info)
  111. {
  112. qstrfree(&info->uri);
  113. if (info->port)
  114. put_ipc_port(info->port);
  115. if (info->pal_handle)
  116. DkObjectClose(info->pal_handle);
  117. }
  118. static void __put_ipc_info (struct shim_ipc_info * info)
  119. {
  120. int ref_count = REF_DEC(info->ref_count);
  121. #ifdef DEBUG_REF
  122. debug("put port %p (vmid %u uri %s, ref_count = %d)\n", info,
  123. info->vmid, qstrgetstr(&info->uri), ref_count);
  124. #endif
  125. if (ref_count)
  126. return;
  127. unset_ipc_info(info);
  128. free_mem_obj_to_mgr(ipc_info_mgr, info);
  129. }
  130. void put_ipc_info (struct shim_ipc_info * info)
  131. {
  132. int ref_count = REF_DEC(info->ref_count);
  133. #ifdef DEBUG_REF
  134. debug("put port %p (vmid %u uri %s, ref_count = %d)\n", info,
  135. info->vmid, qstrgetstr(&info->uri), ref_count);
  136. #endif
  137. if (ref_count)
  138. return;
  139. unset_ipc_info(info);
  140. lock(ipc_info_lock);
  141. free_mem_obj_to_mgr(ipc_info_mgr, info);
  142. unlock(ipc_info_lock);
  143. }
  144. #define CLIENT_HASH_LEN 6
  145. #define CLIENT_HASH_NUM (1 << CLIENT_HASH_LEN)
  146. #define CLIENT_HASH_MASK (CLIENT_HASH_NUM - 1)
  147. #define CLIENT_HASH(vmid) ((vmid) & CLIENT_HASH_MASK)
  148. static struct hlist_head client_table [CLIENT_HASH_NUM];
  149. struct shim_ipc_info *
  150. lookup_and_alloc_client (IDTYPE vmid, const char * uri)
  151. {
  152. struct shim_ipc_info * p;
  153. struct hlist_head * head = client_table + CLIENT_HASH(vmid);
  154. struct hlist_node * pos;
  155. size_t len = strlen(uri);
  156. assert(vmid);
  157. lock(ipc_info_lock);
  158. hlist_for_each_entry(p, pos, head, hlist)
  159. if (p->vmid == vmid && !qstrcmpstr(&p->uri, uri, len)) {
  160. get_ipc_info(p);
  161. unlock(ipc_info_lock);
  162. return p;
  163. }
  164. unlock(ipc_info_lock);
  165. lock(ipc_info_lock);
  166. p = __get_new_ipc_info(vmid, uri, len);
  167. if (p) {
  168. hlist_add_head(&p->hlist, head);
  169. get_ipc_info(p);
  170. }
  171. unlock(ipc_info_lock);
  172. return p;
  173. }
  174. void put_client (struct shim_ipc_info * info)
  175. {
  176. lock(ipc_info_lock);
  177. __put_ipc_info(info);
  178. if (REF_GET(info->ref_count) == 1) {
  179. hlist_del_init(&info->hlist);
  180. __put_ipc_info(info);
  181. }
  182. unlock(ipc_info_lock);
  183. }
  184. struct shim_ipc_info * discover_client (struct shim_ipc_port * port,
  185. IDTYPE vmid)
  186. {
  187. struct shim_ipc_info * p;
  188. struct hlist_head * head = client_table + CLIENT_HASH(vmid);
  189. struct hlist_node * pos;
  190. assert(vmid);
  191. lock(ipc_info_lock);
  192. hlist_for_each_entry(p, pos, head, hlist)
  193. if (p->vmid == vmid && !qstrempty(&p->uri)) {
  194. __get_ipc_info(p);
  195. unlock(ipc_info_lock);
  196. return p;
  197. }
  198. unlock(ipc_info_lock);
  199. return NULL;
  200. if (!ipc_finduri_send(port, vmid, &p))
  201. return p;
  202. return NULL;
  203. }
  204. struct shim_process * create_new_process (bool inherit_parent)
  205. {
  206. IDTYPE vmid;
  207. if (getrand(&vmid, sizeof(IDTYPE)) < sizeof(IDTYPE))
  208. return NULL;
  209. struct shim_process * new_process = malloc(sizeof(struct shim_process));
  210. if (!new_process)
  211. return NULL;
  212. assert(vmid != cur_process.vmid);
  213. memset(new_process, 0, sizeof(struct shim_process));
  214. new_process->vmid = vmid;
  215. new_process->parent = get_new_ipc_info(cur_process.vmid, NULL, 0);
  216. if (!inherit_parent)
  217. return new_process;
  218. lock(cur_process.lock);
  219. if (cur_process.self)
  220. qstrcopy(&new_process->parent->uri, &cur_process.self->uri);
  221. for (int i = 0 ; i < TOTAL_NS ; i++)
  222. if (cur_process.ns[i])
  223. new_process->ns[i] =
  224. get_new_ipc_info(cur_process.ns[i]->vmid,
  225. qstrgetstr(&cur_process.ns[i]->uri),
  226. cur_process.ns[i]->uri.len);
  227. unlock(cur_process.lock);
  228. return new_process;
  229. }
  230. void destroy_process (struct shim_process * proc)
  231. {
  232. if (proc->self)
  233. put_ipc_info(proc->self);
  234. if (proc->parent)
  235. put_ipc_info(proc->parent);
  236. for (int i = 0 ; i < TOTAL_NS ; i++)
  237. if (proc->ns[i])
  238. put_ipc_info(proc->ns[i]);
  239. free(proc);
  240. }
  241. int __init_ipc_msg (struct shim_ipc_msg * msg, int code, int size, IDTYPE dest)
  242. {
  243. msg->code = code;
  244. msg->size = IPC_MSG_SIZE(size);
  245. msg->src = cur_process.vmid;
  246. msg->dst = dest;
  247. msg->seq = 0;
  248. return 0;
  249. }
  250. struct shim_ipc_msg * create_ipc_msg (int code, int size, IDTYPE dest)
  251. {
  252. struct shim_ipc_msg * msg = malloc(IPC_MSG_SIZE(size));
  253. if (msg && __init_ipc_msg(msg, code, size, dest)) {
  254. free(msg);
  255. msg = NULL;
  256. }
  257. return msg;
  258. }
  259. int __init_ipc_msg_duplex (struct shim_ipc_msg_obj * msg, int code, int size,
  260. IDTYPE dest)
  261. {
  262. __init_ipc_msg(&msg->msg, code, size, dest);
  263. msg->thread = NULL;
  264. INIT_LIST_HEAD(&msg->list);
  265. msg->retval = 0;
  266. msg->private = NULL;
  267. return 0;
  268. }
  269. struct shim_ipc_msg_obj *
  270. create_ipc_msg_duplex (int code, int size, IDTYPE dest)
  271. {
  272. struct shim_ipc_msg_obj * msg = malloc(IPC_MSGOBJ_SIZE(size));
  273. if (msg && __init_ipc_msg_duplex(msg, code, size, dest)) {
  274. free(msg);
  275. msg = NULL;
  276. }
  277. return msg;
  278. }
  279. int __init_ipc_resp_msg (struct shim_ipc_msg * resp, int ret,
  280. unsigned long seq)
  281. {
  282. struct shim_ipc_resp * resp_in = (struct shim_ipc_resp *) resp->msg;
  283. resp->seq = seq;
  284. resp_in->retval = ret;
  285. return 0;
  286. }
  287. struct shim_ipc_msg *
  288. create_ipc_resp_msg (int ret, IDTYPE dest, unsigned long seq)
  289. {
  290. struct shim_ipc_msg * resp =
  291. create_ipc_msg(IPC_RESP, sizeof(struct shim_ipc_resp), dest);
  292. if (resp && __init_ipc_resp_msg(resp, ret, seq)) {
  293. free(resp);
  294. resp = NULL;
  295. }
  296. return resp;
  297. }
  298. int send_ipc_message (struct shim_ipc_msg * msg, struct shim_ipc_port * port)
  299. {
  300. assert(msg->size >= IPC_MSG_MINIMAL_SIZE);
  301. msg->src = cur_process.vmid;
  302. int ret = DkStreamWrite(port->pal_handle, 0, msg->size, msg, NULL);
  303. if (ret == 0 && PAL_NATIVE_ERRNO) {
  304. debug("port %p (handle %p) is removed at sending\n", port,
  305. port->pal_handle);
  306. del_ipc_port_fini(port, -ECHILD);
  307. return -PAL_ERRNO;
  308. }
  309. return 0;
  310. }
  311. int close_ipc_message_duplex (struct shim_ipc_msg_obj * msg,
  312. struct shim_ipc_port * port)
  313. {
  314. if (port && !list_empty(&msg->list)) {
  315. lock(port->msgs_lock);
  316. list_del_init(&msg->list);
  317. unlock(port->msgs_lock);
  318. }
  319. if (msg->thread)
  320. put_thread(msg->thread);
  321. return 0;
  322. }
  323. static struct shim_atomic ipc_seq_counter;
  324. int send_ipc_message_duplex (struct shim_ipc_msg_obj * msg,
  325. struct shim_ipc_port * port, bool save,
  326. void * private_data)
  327. {
  328. atomic_inc(&ipc_seq_counter);
  329. msg->msg.seq = atomic_read(&ipc_seq_counter);
  330. if (save) {
  331. lock(port->msgs_lock);
  332. msg->private = private_data;
  333. list_add_tail(&msg->list, &port->msgs);
  334. unlock(port->msgs_lock);
  335. }
  336. int ret = send_ipc_message(&msg->msg, port);
  337. if (ret < 0) {
  338. if (save)
  339. close_ipc_message_duplex(msg, port);
  340. return ret;
  341. }
  342. return 0;
  343. }
  344. struct shim_ipc_msg_obj * find_ipc_msg_duplex (struct shim_ipc_port * port,
  345. unsigned long seq)
  346. {
  347. struct shim_ipc_msg_obj * tmp, * found = NULL;
  348. lock(port->msgs_lock);
  349. list_for_each_entry(tmp, &port->msgs, list)
  350. if (tmp->msg.seq == seq) {
  351. found = tmp;
  352. list_del_init(&tmp->list);
  353. break;
  354. }
  355. unlock(port->msgs_lock);
  356. return found;
  357. }
  358. /* for convenience */
  359. int do_ipc_duplex (struct shim_ipc_msg_obj * msg,
  360. struct shim_ipc_port * port, unsigned long * seq,
  361. void * private_data)
  362. {
  363. int ret = 0;
  364. struct shim_thread * thread = get_cur_thread();
  365. assert(thread);
  366. if (!msg->thread)
  367. thread_setwait(&msg->thread, thread);
  368. ret = send_ipc_message_duplex(msg, port, true, private_data);
  369. if (seq)
  370. *seq = (ret < 0) ? 0 : msg->msg.seq;
  371. if (ret < 0)
  372. goto out;
  373. debug("wait for response (seq = %lu)\n", msg->msg.seq);
  374. thread_sleep();
  375. ret = msg->retval;
  376. out:
  377. close_ipc_message_duplex(msg, port);
  378. return ret;
  379. }
  380. struct shim_ipc_info * create_ipc_port (IDTYPE vmid, bool listen)
  381. {
  382. struct shim_ipc_info * proc = get_new_ipc_info(vmid, NULL, 0);
  383. if (!proc)
  384. return NULL;
  385. char uri[PIPE_URI_SIZE];
  386. if (create_pipe(NULL, uri, PIPE_URI_SIZE, &proc->pal_handle,
  387. &proc->uri) < 0) {
  388. put_ipc_info(proc);
  389. return NULL;
  390. }
  391. if (listen)
  392. add_ipc_port_by_id(0, proc->pal_handle, IPC_PORT_SERVER,
  393. NULL, &proc->port);
  394. return proc;
  395. }
  396. int create_ipc_location (struct shim_ipc_info ** info)
  397. {
  398. lock(cur_process.lock);
  399. int ret = -EACCES;
  400. if (cur_process.self)
  401. goto success;
  402. cur_process.self = create_ipc_port(cur_process.vmid, true);
  403. if (!cur_process.self)
  404. goto out;
  405. success:
  406. get_ipc_info(cur_process.self);
  407. *info = cur_process.self;
  408. ret = 0;
  409. out:
  410. unlock(cur_process.lock);
  411. return ret;
  412. }
  413. DEFINE_PROFILE_INTERVAL(ipc_finduri_send, ipc);
  414. DEFINE_PROFILE_INTERVAL(ipc_finduri_callback, ipc);
  415. int ipc_finduri_send (struct shim_ipc_port * port, IDTYPE dest,
  416. struct shim_ipc_info ** info)
  417. {
  418. BEGIN_PROFILE_INTERVAL();
  419. int ret;
  420. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  421. IPC_FINDURI, 0, dest);
  422. debug("ipc send to %u: IPC_FINDURI\n", dest);
  423. ret = do_ipc_duplex(msg, port, NULL, info);
  424. SAVE_PROFILE_INTERVAL(ipc_finduri_send);
  425. return ret;
  426. }
  427. int ipc_finduri_callback (IPC_CALLBACK_ARGS)
  428. {
  429. BEGIN_PROFILE_INTERVAL();
  430. int ret = 0;
  431. debug("ipc callback from %u: IPC_FINDURI\n", msg->src);
  432. struct shim_ipc_info * info;
  433. if ((ret = create_ipc_location(&info)) < 0)
  434. goto out;
  435. ret = ipc_telluri_send(port, msg->src, info);
  436. out:
  437. SAVE_PROFILE_INTERVAL(ipc_finduri_callback);
  438. return ret;
  439. }
  440. DEFINE_PROFILE_INTERVAL(ipc_telluri_send, ipc);
  441. DEFINE_PROFILE_INTERVAL(ipc_telluri_callback, ipc);
  442. int ipc_telluri_send (struct shim_ipc_port * port, IDTYPE dest,
  443. struct shim_ipc_info * info)
  444. {
  445. BEGIN_PROFILE_INTERVAL();
  446. int ret;
  447. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  448. IPC_TELLURI,
  449. info->uri.len, dest);
  450. struct shim_ipc_telluri * msgin =
  451. (struct shim_ipc_telluri *) &msg->msg;
  452. if (qstrempty(&info->uri)) {
  453. ret = -ENOENT;
  454. return ret;
  455. }
  456. memcpy(msgin->uri, qstrgetstr(&info->uri), info->uri.len + 1);
  457. debug("ipc send to %u: IPC_TELLURI(%s)\n", dest,
  458. qstrgetstr(&info->uri));
  459. ret = send_ipc_message(msg, port);
  460. SAVE_PROFILE_INTERVAL(ipc_telluri_send);
  461. return ret;
  462. }
  463. int ipc_telluri_callback (IPC_CALLBACK_ARGS)
  464. {
  465. BEGIN_PROFILE_INTERVAL();
  466. int ret = 0;
  467. struct shim_ipc_telluri * msgin =
  468. (struct shim_ipc_telluri *) &msg->msg;
  469. debug("ipc callback from %u: IPC_TELLURI(%s)\n", msg->src, msgin->uri);
  470. struct shim_ipc_info * info =
  471. lookup_and_alloc_client(msg->src, msgin->uri);
  472. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  473. if (obj) {
  474. if (info) {
  475. if (obj->private)
  476. *(struct shim_ipc_info **) obj->private = info;
  477. obj->retval = 0;
  478. } else {
  479. obj->retval = -ENOMEM;
  480. }
  481. if (obj->thread)
  482. thread_wakeup(obj->thread);
  483. }
  484. SAVE_PROFILE_INTERVAL(ipc_telluri_callback);
  485. return ret;
  486. }
  487. DEFINE_PROFILE_INTERVAL(ipc_checkpoint_send, ipc);
  488. DEFINE_PROFILE_INTERVAL(ipc_checkpoint_callback, ipc);
  489. int ipc_checkpoint_send (const char * cpdir, IDTYPE cpsession)
  490. {
  491. BEGIN_PROFILE_INTERVAL();
  492. int ret;
  493. int len = strlen(cpdir);
  494. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  495. IPC_CHECKPOINT,
  496. sizeof(struct shim_ipc_checkpoint)
  497. + len, 0);
  498. struct shim_ipc_checkpoint * msgin =
  499. (struct shim_ipc_checkpoint *) &msg->msg;
  500. msgin->cpsession = cpsession;
  501. memcpy(&msgin->cpdir, cpdir, len + 1);
  502. debug("ipc broadcast to all: IPC_CHECKPOINT(%u, %s)\n",
  503. cpsession, cpdir);
  504. ret = broadcast_ipc(msg, NULL, 0, IPC_PORT_DIRCLD|IPC_PORT_DIRPRT);
  505. SAVE_PROFILE_INTERVAL(ipc_checkpoint_send);
  506. return ret;
  507. }
  508. int ipc_checkpoint_callback (IPC_CALLBACK_ARGS)
  509. {
  510. BEGIN_PROFILE_INTERVAL();
  511. int ret = 0;
  512. struct shim_ipc_checkpoint * msgin =
  513. (struct shim_ipc_checkpoint *) msg->msg;
  514. debug("ipc callback form %u: IPC_CHECKPOINT(%u, %s)\n", msg->src,
  515. msgin->cpsession, msgin->cpdir);
  516. ret = create_checkpoint(msgin->cpdir, &msgin->cpsession);
  517. if (ret < 0)
  518. goto out;
  519. kill_all_threads(NULL, CHECKPOINT_REQUESTED, SIGINT);
  520. broadcast_ipc(msg, &port, 1, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD);
  521. out:
  522. SAVE_PROFILE_INTERVAL(ipc_checkpoint_callback);
  523. return ret;
  524. }
  525. DEFINE_MIGRATE_FUNC(ipc_info)
  526. MIGRATE_FUNC_BODY(ipc_info)
  527. {
  528. assert(size == sizeof(struct shim_ipc_info));
  529. unsigned long off = ADD_TO_MIGRATE_MAP(obj, *offset,
  530. sizeof(struct shim_ipc_info));
  531. struct shim_ipc_info * port = (struct shim_ipc_info *) obj;
  532. struct shim_ipc_info * new_port = NULL;
  533. if (ENTRY_JUST_CREATED(off)) {
  534. ADD_OFFSET(sizeof(struct shim_ipc_info));
  535. if (!dry) {
  536. new_port = (struct shim_ipc_info *) (base + *offset);
  537. *new_port = *port;
  538. REF_SET(new_port->ref_count, 0);
  539. }
  540. ADD_ENTRY(PALHDL, port->pal_handle && port->pal_handle !=
  541. IPC_FORCE_RECONNECT ? *offset +
  542. offsetof(struct shim_ipc_info, pal_handle) : 0);
  543. } else if (!dry)
  544. new_port = (struct shim_ipc_info *) (base + off);
  545. if (new_port && objp)
  546. *objp = (void *) new_port;
  547. DO_MIGRATE_IN_MEMBER(qstr, port, new_port, uri, false);
  548. }
  549. END_MIGRATE_FUNC
  550. RESUME_FUNC_BODY(ipc_info)
  551. {
  552. /* do nothing */
  553. }
  554. END_RESUME_FUNC
  555. DEFINE_MIGRATE_FUNC(process)
  556. MIGRATE_FUNC_BODY(process)
  557. {
  558. assert(size == sizeof(struct shim_process));
  559. unsigned long off = ADD_TO_MIGRATE_MAP(obj, *offset,
  560. sizeof(struct shim_process));
  561. struct shim_process * proc = (struct shim_process *) obj;
  562. struct shim_process * new_proc = NULL;
  563. if (ENTRY_JUST_CREATED(off)) {
  564. ADD_OFFSET(sizeof(struct shim_process));
  565. ADD_FUNC_ENTRY(*offset);
  566. ADD_ENTRY(SIZE, sizeof(struct shim_process));
  567. if (!dry) {
  568. new_proc = (struct shim_process *) (base + *offset);
  569. *new_proc = *proc;
  570. }
  571. } else if (!dry)
  572. new_proc = (struct shim_process *) (base + off);
  573. if (new_proc && objp)
  574. *objp = (void *) new_proc;
  575. if (proc->self)
  576. __DO_MIGRATE(ipc_info, proc->self, &new_proc->self, true);
  577. if (proc->parent)
  578. __DO_MIGRATE(ipc_info, proc->parent, &new_proc->parent, true);
  579. for (int i = 0 ; i < TOTAL_NS ; i++)
  580. if (proc->ns[i])
  581. __DO_MIGRATE(ipc_info, proc->ns[i], &new_proc->ns[i], true);
  582. }
  583. END_MIGRATE_FUNC
  584. RESUME_FUNC_BODY(process)
  585. {
  586. unsigned long off = GET_FUNC_ENTRY();
  587. assert((size_t) GET_ENTRY(SIZE) == sizeof(struct shim_process));
  588. struct shim_process * proc = (struct shim_process *) (base + off);
  589. if (proc->self)
  590. get_ipc_info(proc->self);
  591. if (proc->parent)
  592. get_ipc_info(proc->parent);
  593. for (int i = 0 ; i < TOTAL_NS ; i++)
  594. if (proc->ns[i])
  595. get_ipc_info(proc->ns[i]);
  596. memcpy(&cur_process, proc, sizeof(struct shim_process));
  597. create_lock(cur_process.lock);
  598. #ifdef DEBUG_RESUME
  599. debug("process: vmid=%u, uri=%s, parent=%u(%s)\n", proc->vmid,
  600. proc->self ? qstrgetstr(&proc->self->uri) : NULL,
  601. proc->parent ? proc->parent->vmid : 0,
  602. proc->parent ? qstrgetstr(&proc->parent->uri) : NULL);
  603. #endif
  604. }
  605. END_RESUME_FUNC