shim_ipc_helper.c 29 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048
  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_helper.c
  17. *
  18. * This file contains codes to create a IPC helper thread inside library OS
  19. * and maintain bookkeeping of IPC ports.
  20. */
  21. #include <shim_internal.h>
  22. #include <shim_utils.h>
  23. #include <shim_thread.h>
  24. #include <shim_handle.h>
  25. #include <shim_ipc.h>
  26. #include <shim_checkpoint.h>
  27. #include <shim_profile.h>
  28. #include <pal.h>
  29. #include <pal_error.h>
  30. #include <linux_list.h>
  31. #define PORT_MGR_ALLOC 32
  32. #define PAGE_SIZE allocsize
  33. #define OBJ_TYPE struct shim_ipc_port
  34. #include "memmgr.h"
  35. static MEM_MGR port_mgr;
  36. static LIST_HEAD(pobj_list);
  37. #define PID_HASH_LEN 6
  38. #define PID_HASH_NUM (1 << PID_HASH_LEN)
  39. #define PID_HASH_MASK (PID_HASH_NUM - 1)
  40. #define PID_HASH(pid) ((pid) & PID_HASH_MASK)
  41. static struct hlist_head ipc_port_pool [PID_HASH_NUM];
  42. enum {
  43. HELPER_UNINITIALIZED, HELPER_DELAYED, HELPER_NOTALIVE,
  44. HELPER_ALIVE, HELPER_HANDEDOVER,
  45. };
  46. static struct shim_atomic ipc_helper_state;
  47. static struct shim_thread * ipc_helper_thread;
  48. static bool ipc_helper_update;
  49. static AEVENTTYPE ipc_helper_event;
  50. #define IN_HELPER() \
  51. (ipc_helper_thread && ipc_helper_thread == get_cur_thread())
  52. static LOCKTYPE ipc_helper_lock;
  53. static struct shim_ipc_port * broadcast_port;
  54. //#define DEBUG_REF
  55. static int init_ipc_port (struct shim_ipc_info * info, PAL_HANDLE hdl, int type)
  56. {
  57. if (!info)
  58. return 0;
  59. if (info->pal_handle == IPC_FORCE_RECONNECT) {
  60. info->pal_handle = NULL;
  61. if (!hdl && !qstrempty(&info->uri)) {
  62. debug("try reconnect port %s\n", qstrgetstr(&info->uri));
  63. hdl = DkStreamOpen(qstrgetstr(&info->uri),
  64. 0, 0, 0, 0);
  65. if (!hdl)
  66. return -PAL_ERRNO;
  67. }
  68. info->pal_handle = hdl;
  69. }
  70. if (!info->pal_handle)
  71. info->pal_handle = hdl;
  72. if (info->pal_handle)
  73. add_ipc_port_by_id(info->vmid == cur_process.vmid ? 0 : info->vmid,
  74. info->pal_handle, type, NULL, &info->port);
  75. return 0;
  76. }
  77. static void ipc_broadcast_exit (struct shim_ipc_port * port, IDTYPE vmid,
  78. unsigned exitcode)
  79. {
  80. if (port == broadcast_port) {
  81. master_lock();
  82. broadcast_port = NULL;
  83. put_ipc_port(port);
  84. master_unlock();
  85. }
  86. }
  87. int init_ipc_ports (void)
  88. {
  89. int ret = 0;
  90. if (!(port_mgr = create_mem_mgr(init_align_up(PORT_MGR_ALLOC))))
  91. return -ENOMEM;
  92. if ((ret = init_ipc_port(cur_process.self, NULL, IPC_PORT_SERVER)) < 0)
  93. return ret;
  94. if ((ret = init_ipc_port(cur_process.parent, PAL_CB(parent_process),
  95. IPC_PORT_DIRPRT|IPC_PORT_LISTEN)) < 0)
  96. return ret;
  97. if ((ret = init_ipc_port(cur_process.ns[PID_NS], NULL,
  98. IPC_PORT_PIDLDR|IPC_PORT_LISTEN)) < 0)
  99. return ret;
  100. if ((ret = init_ipc_port(cur_process.ns[SYSV_NS], NULL,
  101. IPC_PORT_SYSVLDR|IPC_PORT_LISTEN)) < 0)
  102. return ret;
  103. if (PAL_CB(broadcast_stream))
  104. add_ipc_port_by_id(0, PAL_CB(broadcast_stream), IPC_PORT_LISTEN,
  105. &ipc_broadcast_exit, &broadcast_port);
  106. return 0;
  107. }
  108. int init_ipc_helper (void)
  109. {
  110. bool need_helper = (atomic_read(&ipc_helper_state) == HELPER_DELAYED);
  111. atomic_set(&ipc_helper_state, HELPER_NOTALIVE);
  112. create_lock(ipc_helper_lock);
  113. create_event(&ipc_helper_event);
  114. if (need_helper)
  115. create_ipc_helper();
  116. return 0;
  117. }
  118. static void __get_ipc_port (struct shim_ipc_port * pobj)
  119. {
  120. #ifdef DEBUG_REF
  121. int ref_count = REF_INC(pobj->ref_count);
  122. debug("get ipc_port %p (handle %p, ref_count = %d)\n", pobj,
  123. pobj->pal_handle, ref_count);
  124. #else
  125. REF_INC(pobj->ref_count);
  126. #endif
  127. }
  128. static void __put_ipc_port (struct shim_ipc_port * pobj)
  129. {
  130. int ref_count = REF_DEC(pobj->ref_count);
  131. #ifdef DEBUG_REF
  132. debug("put ipc port %p (handle %p, ref_count = %d)\n", pobj,
  133. pobj->pal_handle, ref_count);
  134. #endif
  135. if (!ref_count) {
  136. if (pobj->pal_handle) {
  137. DkObjectClose(pobj->pal_handle);
  138. pobj->pal_handle = NULL;
  139. }
  140. free_mem_obj_to_mgr(port_mgr, pobj);
  141. }
  142. }
  143. static inline void restart_ipc_helper (bool need_create)
  144. {
  145. switch (atomic_read(&ipc_helper_state)) {
  146. case HELPER_UNINITIALIZED:
  147. atomic_set(&ipc_helper_state, HELPER_DELAYED);
  148. case HELPER_DELAYED:
  149. return;
  150. case HELPER_NOTALIVE:
  151. if (need_create)
  152. create_ipc_helper();
  153. return;
  154. case HELPER_ALIVE:
  155. if (IN_HELPER()) {
  156. ipc_helper_update = true;
  157. return;
  158. }
  159. debug("set ipc helper restart\n");
  160. set_event(&ipc_helper_event, 1);
  161. return;
  162. case HELPER_HANDEDOVER:
  163. ipc_helper_update = true;
  164. return;
  165. }
  166. }
  167. static bool __add_ipc_port (struct shim_ipc_port * port, IDTYPE vmid,
  168. int type, port_fini fini)
  169. {
  170. bool need_restart = false;
  171. assert(vmid != cur_process.vmid);
  172. if (vmid && !port->info.vmid) {
  173. port->info.vmid = vmid;
  174. port->update = true;
  175. }
  176. if (port->info.vmid && hlist_unhashed(&port->hlist)) {
  177. struct hlist_head * head = &ipc_port_pool[PID_HASH(vmid)];
  178. __get_ipc_port(port);
  179. hlist_add_head(&port->hlist, head);
  180. }
  181. if (!(port->info.type & IPC_PORT_IFPOLL) && (type & IPC_PORT_IFPOLL))
  182. need_restart = true;
  183. if ((port->info.type & type) != type) {
  184. port->info.type |= type;
  185. port->update = true;
  186. }
  187. if (fini && (type & ~IPC_PORT_IFPOLL)) {
  188. port_fini * cb = port->fini;
  189. for ( ; cb < port->fini + MAX_IPC_PORT_FINI_CB ; cb++)
  190. if (!*cb || *cb == fini)
  191. break;
  192. assert(cb < port->fini + MAX_IPC_PORT_FINI_CB);
  193. *cb = fini;
  194. }
  195. if (need_restart) {
  196. if (list_empty(&port->list)) {
  197. __get_ipc_port(port);
  198. list_add(&port->list, &pobj_list);
  199. port->recent = true;
  200. } else {
  201. if (!port->recent) {
  202. list_del_init(&port->list);
  203. list_add(&port->list, &pobj_list);
  204. port->recent = true;
  205. }
  206. }
  207. return true;
  208. } else {
  209. if (list_empty(&port->list)) {
  210. __get_ipc_port(port);
  211. list_add_tail(&port->list, &pobj_list);
  212. }
  213. return false;
  214. }
  215. }
  216. void add_ipc_port (struct shim_ipc_port * port, IDTYPE vmid, int type,
  217. port_fini fini)
  218. {
  219. debug("adding port %p (handle %p) for process %u (type=%04x)\n",
  220. port, port->pal_handle, port->info.vmid, type);
  221. lock(ipc_helper_lock);
  222. bool need_restart = __add_ipc_port(port, vmid, type, fini);
  223. unlock(ipc_helper_lock);
  224. if (need_restart)
  225. restart_ipc_helper(true);
  226. }
  227. static struct shim_ipc_port * __get_new_ipc_port (PAL_HANDLE hdl)
  228. {
  229. struct shim_ipc_port * port =
  230. get_mem_obj_from_mgr_enlarge(port_mgr,
  231. size_align_up(PORT_MGR_ALLOC));
  232. if (!port)
  233. return NULL;
  234. memset(port, 0, sizeof(struct shim_ipc_port));
  235. port->pal_handle = hdl;
  236. port->update = true;
  237. INIT_HLIST_NODE(&port->hlist);
  238. INIT_LIST_HEAD(&port->list);
  239. INIT_LIST_HEAD(&port->msgs);
  240. REF_SET(port->ref_count, 1);
  241. create_lock(port->msgs_lock);
  242. return port;
  243. }
  244. void add_ipc_port_by_id (IDTYPE vmid, PAL_HANDLE hdl, int type,
  245. port_fini fini, struct shim_ipc_port ** portptr)
  246. {
  247. debug("adding port (handle %p) for process %u (type %04x)\n",
  248. hdl, vmid, type);
  249. lock(ipc_helper_lock);
  250. struct hlist_head * head = vmid ? &ipc_port_pool[PID_HASH(vmid)] : NULL;
  251. struct shim_ipc_port * tmp, * port = NULL;
  252. struct hlist_node * pos;
  253. if (vmid)
  254. hlist_for_each_entry(tmp, pos, head, hlist)
  255. if (tmp->info.vmid == vmid && tmp->pal_handle == hdl) {
  256. port = tmp;
  257. __get_ipc_port(port);
  258. break;
  259. }
  260. if (!port)
  261. list_for_each_entry(tmp, &pobj_list, list)
  262. if (tmp->pal_handle == hdl) {
  263. port = tmp;
  264. __get_ipc_port(port);
  265. break;
  266. }
  267. if (!port && !(port = __get_new_ipc_port(hdl))) {
  268. *portptr = NULL;
  269. return;
  270. }
  271. bool need_restart = __add_ipc_port(port, vmid, type, fini);
  272. if (portptr)
  273. *portptr = port;
  274. else
  275. __put_ipc_port(port);
  276. unlock(ipc_helper_lock);
  277. if (need_restart)
  278. restart_ipc_helper(true);
  279. }
  280. static bool __del_ipc_port (struct shim_ipc_port * port, int type)
  281. {
  282. debug("deleting port %p (handle %p) for process %u\n",
  283. port, port->pal_handle, port->info.vmid);
  284. bool need_restart = false;
  285. type = type ? (type & port->info.type) : port->info.type;
  286. if ((type & IPC_PORT_KEEPALIVE) ^
  287. (port->info.type & IPC_PORT_KEEPALIVE))
  288. need_restart = true;
  289. /* if the port still have other usage, we will not remove the port */
  290. if (port->info.type & ~(type|IPC_PORT_IFPOLL|IPC_PORT_KEEPALIVE)) {
  291. debug("masking port %p (handle %p): type %x->%x\n",
  292. port, port->pal_handle, port->info.type, port->info.type & ~type);
  293. port->info.type &= ~type;
  294. goto out;
  295. }
  296. if (port->info.type & IPC_PORT_IFPOLL)
  297. need_restart = true;
  298. if (!list_empty(&port->list)) {
  299. list_del_init(&port->list);
  300. port->info.type &= IPC_PORT_IFPOLL;
  301. __put_ipc_port(port);
  302. }
  303. if (!hlist_unhashed(&port->hlist)) {
  304. hlist_del_init(&port->hlist);
  305. __put_ipc_port(port);
  306. }
  307. out:
  308. port->update = true;
  309. return need_restart;
  310. }
  311. void del_ipc_port (struct shim_ipc_port * port, int type)
  312. {
  313. lock(ipc_helper_lock);
  314. bool need_restart = __del_ipc_port(port, type);
  315. unlock(ipc_helper_lock);
  316. if (need_restart)
  317. restart_ipc_helper(false);
  318. }
  319. void del_ipc_port_by_id (IDTYPE vmid, int type)
  320. {
  321. struct hlist_head * head = &ipc_port_pool[PID_HASH(vmid)];
  322. struct shim_ipc_port * port;
  323. struct hlist_node * pos, * n;
  324. bool need_restart = false;
  325. lock(ipc_helper_lock);
  326. hlist_for_each_entry_safe(port, pos, n, head, hlist) {
  327. debug("port %p (handle %p) for process %u in list %p\n",
  328. port, port->pal_handle, port->info.vmid, head);
  329. if (port->info.vmid == vmid) {
  330. if (__del_ipc_port(port, type))
  331. need_restart = true;
  332. }
  333. }
  334. unlock(ipc_helper_lock);
  335. if (need_restart)
  336. restart_ipc_helper(false);
  337. }
  338. void del_ipc_port_fini (struct shim_ipc_port * port, unsigned int exitcode)
  339. {
  340. port_fini fini[MAX_IPC_PORT_FINI_CB];
  341. int nfini = 0;
  342. assert(REF_GET(port->ref_count) > 0);
  343. lock(ipc_helper_lock);
  344. IDTYPE vmid = port->info.vmid;
  345. for (int i = 0 ; i < MAX_IPC_PORT_FINI_CB ; i++)
  346. if (port->fini[i]) {
  347. fini[nfini++] = port->fini[i];
  348. port->fini[i] = NULL;
  349. }
  350. __get_ipc_port(port);
  351. bool need_restart = __del_ipc_port(port, 0);
  352. unlock(ipc_helper_lock);
  353. if (nfini) {
  354. for (int i = 0 ; i < nfini ; i++)
  355. (fini[i])(port, vmid, exitcode);
  356. }
  357. lock(port->msgs_lock);
  358. if (!list_empty(&port->list)) {
  359. struct shim_ipc_msg_obj * msg, * n;
  360. list_for_each_entry_safe(msg, n, &port->msgs, list) {
  361. list_del_init(&msg->list);
  362. msg->retval = -ECONNRESET;
  363. if (msg->thread)
  364. thread_wakeup(msg->thread);
  365. }
  366. }
  367. unlock(port->msgs_lock);
  368. put_ipc_port(port);
  369. assert(REF_GET(port->ref_count) > 0);
  370. if (need_restart)
  371. restart_ipc_helper(false);
  372. }
  373. static struct shim_ipc_port * __lookup_ipc_port (IDTYPE vmid, int type)
  374. {
  375. struct hlist_head * head = &ipc_port_pool[PID_HASH(vmid)];
  376. struct shim_ipc_port * tmp;
  377. struct hlist_node * pos;
  378. hlist_for_each_entry(tmp, pos, head, hlist)
  379. if (tmp->info.vmid == vmid && (!type || tmp->info.type & type)) {
  380. debug("found port %p (handle %p) for process %u (type %04x)\n",
  381. tmp, tmp->pal_handle, tmp->info.vmid, tmp->info.type);
  382. __get_ipc_port(tmp);
  383. return tmp;
  384. }
  385. return NULL;
  386. }
  387. struct shim_ipc_port * lookup_ipc_port (IDTYPE vmid, int type)
  388. {
  389. lock(ipc_helper_lock);
  390. struct shim_ipc_port * port = __lookup_ipc_port(vmid, type);
  391. unlock(ipc_helper_lock);
  392. return port;
  393. }
  394. void get_ipc_port (struct shim_ipc_port * port)
  395. {
  396. __get_ipc_port(port);
  397. }
  398. void put_ipc_port (struct shim_ipc_port * port)
  399. {
  400. __put_ipc_port(port);
  401. }
  402. void del_all_ipc_ports (int type)
  403. {
  404. struct shim_ipc_port * pobj, * n;
  405. bool need_restart = false;
  406. lock(ipc_helper_lock);
  407. list_for_each_entry_safe(pobj, n, &pobj_list, list)
  408. if (pobj->pal_handle && __del_ipc_port(pobj, type))
  409. need_restart = true;
  410. unlock(ipc_helper_lock);
  411. if (need_restart)
  412. restart_ipc_helper(false);
  413. }
  414. int broadcast_ipc (struct shim_ipc_msg * msg, struct shim_ipc_port ** exclude,
  415. int exsize, int target_type)
  416. {
  417. struct shim_ipc_port ** exend = exclude + exsize, ** ex;
  418. struct shim_ipc_port * pobj, * n;
  419. if (!target_type && broadcast_port) {
  420. for (ex = exclude ; ex < exend && *ex != broadcast_port ; ex++);
  421. if (ex != exend)
  422. return 0;
  423. debug("send to broadcast stream\n");
  424. get_ipc_port(broadcast_port);
  425. int ret = send_ipc_message(msg, broadcast_port);
  426. put_ipc_port(broadcast_port);
  427. if (!ret)
  428. return 0;
  429. }
  430. lock(ipc_helper_lock);
  431. list_for_each_entry_safe(pobj, n, &pobj_list, list) {
  432. debug("found port %p (handle %p) for process %u (type %04x)\n",
  433. pobj, pobj->pal_handle, pobj->info.vmid, pobj->info.type);
  434. if (pobj->info.type & target_type) {
  435. debug("broadcast to port %p (handle %p) for process %u "
  436. "(type %x, target %x)\n",
  437. pobj, pobj->pal_handle, pobj->info.vmid,
  438. pobj->info.type, target_type);
  439. if (exsize) {
  440. for (ex = exclude ; ex < exend && *ex != pobj ; ex++);
  441. if (ex != exend)
  442. continue;
  443. }
  444. msg->dst = pobj->info.vmid;
  445. /* has to be assigned, so shim_send_ipc_message will not try
  446. to grab ipc_helper_lock */
  447. send_ipc_message(msg, pobj);
  448. }
  449. }
  450. unlock(ipc_helper_lock);
  451. return 0;
  452. }
  453. static int ipc_resp_callback (IPC_CALLBACK_ARGS)
  454. {
  455. struct shim_ipc_resp * msgin = (struct shim_ipc_resp *) &msg->msg;
  456. debug("ipc callback from %u: IPC_RESP(%d)\n", msg->src, msgin->retval);
  457. if (!msg->seq)
  458. return msgin->retval;
  459. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  460. if (obj) {
  461. obj->retval = msgin->retval;
  462. if (obj->thread)
  463. thread_wakeup(obj->thread);
  464. return 0;
  465. }
  466. return msgin->retval;
  467. }
  468. static ipc_callback ipc_callbacks [IPC_CODE_NUM] = {
  469. /* RESP */ &ipc_resp_callback,
  470. /* FINDURI */ &ipc_finduri_callback,
  471. /* TELLURI */ &ipc_telluri_callback,
  472. /* CHECKPOINT */ &ipc_checkpoint_callback,
  473. /* parents and children */
  474. /* CLD_EXIT */ &ipc_cld_exit_callback,
  475. /* CLD_JOIN */ &ipc_cld_join_callback,
  476. #ifdef PROFILE
  477. /* CLD_PROFILE */ &ipc_cld_profile_callback,
  478. #endif
  479. /* pid namespace */
  480. IPC_NS_CALLBACKS(pid)
  481. /* PID_KILL */ &ipc_pid_kill_callback,
  482. /* PID_GETSTATUS */ &ipc_pid_getstatus_callback,
  483. /* PID_RETSTATUS */ &ipc_pid_retstatus_callback,
  484. /* PID_GETMETA */ &ipc_pid_getmeta_callback,
  485. /* PID_RETMETA */ &ipc_pid_retmeta_callback,
  486. /* PID_NOP */ &ipc_pid_nop_callback,
  487. /* PID_SENDRPC */ &ipc_pid_sendrpc_callback,
  488. /* sysv namespace */
  489. IPC_NS_CALLBACKS(sysv)
  490. IPC_NS_KEY_CALLBACKS(sysv)
  491. /* SYSV_DELRES */ &ipc_sysv_delres_callback,
  492. /* SYSV_MOVRES */ &ipc_sysv_movres_callback,
  493. /* SYSV_MSGSND */ &ipc_sysv_msgsnd_callback,
  494. /* SYSV_MSGRCV */ &ipc_sysv_msgrcv_callback,
  495. /* SYSV_MSGMOV */ &ipc_sysv_msgmov_callback,
  496. /* SYSV_SEMOP */ &ipc_sysv_semop_callback,
  497. /* SYSV_SEMCTL */ &ipc_sysv_semctl_callback,
  498. /* SYSV_SEMRET */ &ipc_sysv_semret_callback,
  499. /* SYSV_SEMMOV */ &ipc_sysv_semmov_callback,
  500. };
  501. int __response_ipc_message (struct shim_ipc_port * port, IDTYPE dest,
  502. int ret, unsigned long seq)
  503. {
  504. struct shim_ipc_msg * resp = create_ipc_resp_msg_on_stack(ret, dest, seq);
  505. ret = (ret == RESPONSE_CALLBACK) ? 0 : ret;
  506. debug("ipc send to %u: IPC_RESP(%d)\n", resp->dst, ret);
  507. struct shim_ipc_resp * msgin = (struct shim_ipc_resp *) &resp->msg;
  508. msgin->retval = ret;
  509. return send_ipc_message(resp, port);
  510. }
  511. /* not only ipc helper thread can receive messsage, anyone can
  512. receive message if they have acquired (locked) the port */
  513. int receive_ipc_message (struct shim_ipc_port * port, unsigned long seq,
  514. struct shim_ipc_msg ** msgptr)
  515. {
  516. int readahead = IPC_MSG_READAHEAD;
  517. int bufsize = IPC_MSG_MINIMAL_SIZE + readahead;
  518. struct shim_ipc_msg * msg = __alloca(bufsize);
  519. int expected_size;
  520. int bytes = 0, ret = 0;
  521. get_ipc_port(port);
  522. do {
  523. expected_size = IPC_MSG_MINIMAL_SIZE;
  524. while (bytes < expected_size) {
  525. retry_read:
  526. if (expected_size + readahead > bufsize) {
  527. while (expected_size + readahead > bufsize)
  528. bufsize *= 2;
  529. void * new_buff = __alloca(bufsize);
  530. memcpy(new_buff, msg, bytes);
  531. msg = new_buff;
  532. }
  533. if (!(ret = DkStreamRead(port->pal_handle, 0,
  534. expected_size - bytes + readahead,
  535. (void *) msg + bytes, NULL, 0)))
  536. break;
  537. bytes += ret;
  538. }
  539. if (!bytes) {
  540. if (PAL_NATIVE_ERRNO) {
  541. debug("port %p (handle %p) is removed at reading\n",
  542. port, port->pal_handle);
  543. del_ipc_port_fini(port, -ECHILD);
  544. ret = -PAL_ERRNO;
  545. }
  546. break;
  547. }
  548. debug("receive a message from port %p (handle %p): "
  549. "code=%d size=%d src=%u dst=%u seq=%lx\n",
  550. port, port->pal_handle,
  551. msg->code, msg->size, msg->src, msg->dst, msg->seq);
  552. expected_size = msg->size;
  553. if (bytes < expected_size)
  554. goto retry_read;
  555. if (msgptr && (!seq || msg->seq == seq)) {
  556. struct shim_ipc_msg * retmsg;
  557. if (*msgptr) {
  558. if (msg->size > (*msgptr)->size)
  559. msg->size = (*msgptr)->size;
  560. retmsg = *msgptr;
  561. } else {
  562. *msgptr = retmsg = malloc(msg->size);
  563. }
  564. memcpy(retmsg, msg, msg->size);
  565. return 0;
  566. }
  567. /* skip if the message comes from myself (it's possible because
  568. of the broadcast channel */
  569. if (msg->src == cur_process.vmid)
  570. goto next;
  571. ipc_callback callback = ipc_callbacks[msg->code];
  572. if (callback) {
  573. ret = (*callback) (msg, port);
  574. if ((ret < 0 || ret == RESPONSE_CALLBACK) && msg->seq)
  575. /* only helper thread sends back response */
  576. ret = __response_ipc_message(port, msg->src, ret, msg->seq);
  577. }
  578. next:
  579. if ((bytes -= expected_size) > 0)
  580. memmove(msg, (void *) msg + expected_size, bytes);
  581. } while (bytes > 0 || (seq && msg->seq != seq));
  582. if (msgptr)
  583. *msgptr = NULL;
  584. put_ipc_port(port);
  585. return ret;
  586. }
  587. #define IPC_HELPER_STACK_SIZE (allocsize * 4)
  588. #define IPC_HELPER_LIST_INIT_SIZE 32
  589. static void shim_ipc_helper (void * arg)
  590. {
  591. /* set ipc helper thread */
  592. struct shim_thread * self = (struct shim_thread *) arg;
  593. if (!arg)
  594. return;
  595. __libc_tcb_t tcb;
  596. allocate_tls(&tcb, self);
  597. debug_setbuf(&tcb.shim_tcb, true);
  598. debug("set tcb to %p\n", &tcb);
  599. lock(ipc_helper_lock);
  600. bool notme = (self != ipc_helper_thread);
  601. unlock(ipc_helper_lock);
  602. if (notme) {
  603. put_thread(self);
  604. DkThreadExit();
  605. return;
  606. }
  607. debug("ipc helper thread started\n");
  608. void * stack = allocate_stack(IPC_HELPER_STACK_SIZE, allocsize, false);
  609. if (!stack)
  610. goto end;
  611. self->stack_top = stack + IPC_HELPER_STACK_SIZE;
  612. self->stack = stack;
  613. switch_stack(stack + IPC_HELPER_STACK_SIZE);
  614. self = get_cur_thread();
  615. stack = self->stack;
  616. int port_num = 0, port_size = IPC_HELPER_LIST_INIT_SIZE;
  617. struct shim_ipc_port ** local_pobjs = stack, * pobj;
  618. PAL_HANDLE * local_ports;
  619. PAL_HANDLE ipc_event_handle = event_handle(&ipc_helper_event);
  620. int nalive = 0;
  621. PAL_HANDLE polled = NULL;
  622. int count = -1;
  623. local_ports = (PAL_HANDLE *) (local_pobjs + port_size);
  624. local_ports[0] = ipc_event_handle;
  625. goto update_status;
  626. while (atomic_read(&ipc_helper_state) == HELPER_ALIVE ||
  627. nalive) {
  628. /* do a global poll on all the ports */
  629. polled = DkObjectsWaitAny(port_num + 1, local_ports, NO_TIMEOUT);
  630. if (!polled)
  631. continue;
  632. /* before we locking pobj list, at least we can look at the returned
  633. port if it is the ipc helper event */
  634. if (polled == ipc_event_handle) {
  635. clear_event(&ipc_helper_event);
  636. update_status:
  637. if (atomic_read(&ipc_helper_state) == HELPER_NOTALIVE)
  638. goto end;
  639. else
  640. goto update_list;
  641. }
  642. pobj = NULL;
  643. count = -1;
  644. for (int i = 0 ; i < port_num ; i++)
  645. if (polled == local_pobjs[i]->pal_handle) {
  646. pobj = local_pobjs[i];
  647. count = i;
  648. break;
  649. }
  650. if (!pobj)
  651. continue;
  652. /* if the polled port is a server port, accept a client and add it
  653. to the port list */
  654. if (pobj->private.type & IPC_PORT_SERVER) {
  655. PAL_HANDLE cli = DkStreamWaitForClient(polled);
  656. if (cli) {
  657. int type = (pobj->private.type & ~IPC_PORT_SERVER) |
  658. IPC_PORT_LISTEN;
  659. add_ipc_port_by_id(pobj->private.vmid, cli, type,
  660. NULL, NULL);
  661. } else {
  662. debug("port %p (handle %p) is removed at accepting\n",
  663. pobj, polled);
  664. del_ipc_port_fini(pobj, -ECHILD);
  665. }
  666. polled = NULL;
  667. count = -1;
  668. goto update_list;
  669. }
  670. PAL_STREAM_ATTR attr;
  671. if (!DkStreamAttributesQuerybyHandle(polled, &attr)) {
  672. debug("port %p (handle %p) is removed at querying\n",
  673. pobj, polled);
  674. del_ipc_port_fini(pobj, -PAL_ERRNO);
  675. goto update_list;
  676. }
  677. if (attr.readable)
  678. receive_ipc_message(pobj, 0, NULL);
  679. if (attr.disconnected) {
  680. debug("port %p (handle %p) is disconnected\n",
  681. pobj, polled);
  682. del_ipc_port_fini(pobj, -ECONNRESET);
  683. goto update_list;
  684. }
  685. if (!ipc_helper_update)
  686. continue;
  687. update_list:
  688. ipc_helper_update = false;
  689. lock(ipc_helper_lock);
  690. int compact = 0;
  691. /* first walk though all the polling ports and remove the one
  692. being deleted. */
  693. for (int i = 0 ; i < port_num ; i++) {
  694. struct shim_ipc_port * pobj = local_pobjs[i];
  695. if (list_empty(&pobj->list)) {
  696. if (polled == pobj->pal_handle) {
  697. polled = NULL;
  698. count = -1;
  699. }
  700. local_pobjs[i] = NULL;
  701. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  702. nalive--;
  703. __put_ipc_port(pobj);
  704. compact++;
  705. continue;
  706. }
  707. if (pobj->update) {
  708. if (pobj->info.type & IPC_PORT_KEEPALIVE) {
  709. if (!(pobj->private.type & IPC_PORT_KEEPALIVE))
  710. nalive--;
  711. } else {
  712. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  713. nalive++;
  714. }
  715. pobj->private = pobj->info;
  716. pobj->update = false;
  717. }
  718. if (compact) {
  719. if (polled == pobj->pal_handle)
  720. count -= compact;
  721. local_pobjs[i - compact] = pobj;
  722. local_ports[i - compact + 1] = pobj->pal_handle;
  723. }
  724. }
  725. port_num -= compact;
  726. list_for_each_entry(pobj, &pobj_list, list) {
  727. /* we only update among recently updated ports */
  728. if (!pobj->recent)
  729. break;
  730. if (pobj->update) {
  731. pobj->private = pobj->info;
  732. pobj->update = false;
  733. }
  734. assert(pobj->private.type & IPC_PORT_IFPOLL);
  735. if (port_num == port_size) {
  736. port_size *= 2;
  737. memmove(local_pobjs + port_size,
  738. local_ports,
  739. (port_num + 1) * sizeof(PAL_HANDLE));
  740. local_ports = (PAL_HANDLE *) (local_pobjs + port_size);
  741. }
  742. pobj->recent = false;
  743. __get_ipc_port(pobj);
  744. local_pobjs[port_num] = pobj;
  745. local_ports[port_num + 1] = pobj->pal_handle;
  746. port_num++;
  747. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  748. nalive++;
  749. debug("listen to process %u on port %p (handle %p, type %04x)\n",
  750. pobj->private.vmid,
  751. pobj,
  752. pobj->pal_handle,
  753. pobj->private.type);
  754. }
  755. unlock(ipc_helper_lock);
  756. }
  757. for (int i = 0 ; i < port_num ; i++) {
  758. struct shim_ipc_port * pobj = local_pobjs[i];
  759. __put_ipc_port(pobj);
  760. }
  761. end:
  762. /* DP: Put our handle map reference */
  763. if (self->handle_map)
  764. put_handle_map(self->handle_map);
  765. if (atomic_read(&ipc_helper_state) == HELPER_HANDEDOVER) {
  766. debug("ipc helper thread is the last thread, process exiting\n");
  767. shim_clean();
  768. }
  769. atomic_xchg(&ipc_helper_state, HELPER_NOTALIVE);
  770. lock(ipc_helper_lock);
  771. ipc_helper_thread = NULL;
  772. unlock(ipc_helper_lock);
  773. put_thread(self);
  774. debug("ipc helper thread terminated\n");
  775. DkThreadExit();
  776. }
  777. int create_ipc_helper (void)
  778. {
  779. int ret = 0;
  780. if (atomic_read(&ipc_helper_state) == HELPER_ALIVE)
  781. return 0;
  782. /*
  783. * we are enabling multi-threading, must turn on threading
  784. * before grabbing any lock
  785. */
  786. enable_locking();
  787. struct shim_thread * new = get_new_internal_thread();
  788. if (!new)
  789. return -ENOMEM;
  790. lock(ipc_helper_lock);
  791. if (atomic_read(&ipc_helper_state) == HELPER_ALIVE) {
  792. unlock(ipc_helper_lock);
  793. put_thread(new);
  794. return 0;
  795. }
  796. ipc_helper_thread = new;
  797. atomic_xchg(&ipc_helper_state, HELPER_ALIVE);
  798. unlock(ipc_helper_lock);
  799. PAL_HANDLE handle = thread_create(shim_ipc_helper, new, 0);
  800. if (!handle) {
  801. ret = -PAL_ERRNO;
  802. lock(ipc_helper_lock);
  803. ipc_helper_thread = NULL;
  804. atomic_xchg(&ipc_helper_state, HELPER_NOTALIVE);
  805. unlock(ipc_helper_lock);
  806. put_thread(new);
  807. return ret;
  808. }
  809. new->pal_handle = handle;
  810. return 0;
  811. }
  812. int exit_with_ipc_helper (bool handover)
  813. {
  814. if (IN_HELPER() || atomic_read(&ipc_helper_state) != HELPER_ALIVE)
  815. return 0;
  816. lock(ipc_helper_lock);
  817. if (handover) {
  818. handover = false;
  819. struct shim_ipc_port * pobj;
  820. list_for_each_entry(pobj, &pobj_list, list)
  821. if (pobj->info.type & IPC_PORT_KEEPALIVE) {
  822. handover = true;
  823. break;
  824. }
  825. }
  826. unlock(ipc_helper_lock);
  827. int new_state = HELPER_NOTALIVE;
  828. if (handover) {
  829. debug("handing over to ipc helper\n");
  830. new_state = HELPER_HANDEDOVER;
  831. } else {
  832. debug("exiting ipc helper\n");
  833. }
  834. atomic_xchg(&ipc_helper_state, new_state);
  835. set_event(&ipc_helper_event, 1);
  836. return (new_state == HELPER_NOTALIVE) ? 0 : -EAGAIN;
  837. }
  838. int terminate_ipc_helper (void)
  839. {
  840. lock(ipc_helper_lock);
  841. struct shim_thread * thread = ipc_helper_thread;
  842. if (!thread) {
  843. unlock(ipc_helper_lock);
  844. return -ESRCH;
  845. }
  846. debug("terminating ipc helper\n");
  847. atomic_xchg(&ipc_helper_state, HELPER_NOTALIVE);
  848. set_event(&ipc_helper_event, 1);
  849. unlock(ipc_helper_lock);
  850. return 0;
  851. }