shim_ipc_helper.c 32 KB

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