shim_ipc_helper.c 34 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159
  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 struct shim_lock 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 __free_ipc_port (struct shim_ipc_port * pobj)
  150. {
  151. if (pobj->pal_handle) {
  152. DkObjectClose(pobj->pal_handle);
  153. pobj->pal_handle = NULL;
  154. }
  155. destroy_lock(pobj->msgs_lock);
  156. free_mem_obj_to_mgr(port_mgr, pobj);
  157. }
  158. static void __put_ipc_port (struct shim_ipc_port * pobj)
  159. {
  160. int ref_count = REF_DEC(pobj->ref_count);
  161. #ifdef DEBUG_REF
  162. debug("put ipc port %p (handle %p, ref_count = %d)\n", pobj,
  163. pobj->pal_handle, ref_count);
  164. #endif
  165. if (!ref_count)
  166. __free_ipc_port(pobj);
  167. }
  168. /* This should be called with the ipc_helper_lock held */
  169. static inline void restart_ipc_helper (bool need_create)
  170. {
  171. switch (ipc_helper_state) {
  172. case HELPER_UNINITIALIZED:
  173. ipc_helper_state = HELPER_DELAYED;
  174. case HELPER_DELAYED:
  175. return;
  176. case HELPER_NOTALIVE:
  177. if (need_create)
  178. create_ipc_helper();
  179. return;
  180. case HELPER_ALIVE:
  181. if (IN_HELPER()) {
  182. ipc_helper_update = true;
  183. return;
  184. }
  185. debug("set ipc helper restart\n");
  186. set_event(&ipc_helper_event, 1);
  187. return;
  188. case HELPER_HANDEDOVER:
  189. ipc_helper_update = true;
  190. return;
  191. }
  192. }
  193. static bool __add_ipc_port (struct shim_ipc_port * port, IDTYPE vmid,
  194. int type, port_fini fini)
  195. {
  196. bool need_restart = false;
  197. assert(vmid != cur_process.vmid);
  198. if (vmid && !port->info.vmid) {
  199. port->info.vmid = vmid;
  200. port->update = true;
  201. }
  202. if (port->info.vmid && list_empty(port, hlist)) {
  203. LISTP_TYPE(shim_ipc_port) * head = &ipc_port_pool[PID_HASH(vmid)];
  204. __get_ipc_port(port);
  205. listp_add(port, head, hlist);
  206. }
  207. if (!(port->info.type & IPC_PORT_IFPOLL) && (type & IPC_PORT_IFPOLL))
  208. need_restart = true;
  209. if ((port->info.type & type) != type) {
  210. port->info.type |= type;
  211. port->update = true;
  212. }
  213. if (fini && (type & ~IPC_PORT_IFPOLL)) {
  214. port_fini * cb = port->fini;
  215. for ( ; cb < port->fini + MAX_IPC_PORT_FINI_CB ; cb++)
  216. if (!*cb || *cb == fini)
  217. break;
  218. assert(cb < port->fini + MAX_IPC_PORT_FINI_CB);
  219. *cb = fini;
  220. }
  221. if (need_restart) {
  222. if (list_empty(port, list)) {
  223. __get_ipc_port(port);
  224. listp_add(port, &pobj_list, list);
  225. port->recent = true;
  226. } else {
  227. if (!port->recent) {
  228. listp_del_init(port, &pobj_list, list);
  229. listp_add(port, &pobj_list, list);
  230. port->recent = true;
  231. }
  232. }
  233. return true;
  234. } else {
  235. if (list_empty(port, list)) {
  236. __get_ipc_port(port);
  237. listp_add_tail(port, &pobj_list, list);
  238. }
  239. return false;
  240. }
  241. }
  242. void add_ipc_port (struct shim_ipc_port * port, IDTYPE vmid, int type,
  243. port_fini fini)
  244. {
  245. debug("adding port %p (handle %p) for process %u (type=%04x)\n",
  246. port, port->pal_handle, port->info.vmid, type);
  247. lock(ipc_helper_lock);
  248. bool need_restart = __add_ipc_port(port, vmid, type, fini);
  249. if (need_restart)
  250. restart_ipc_helper(true);
  251. unlock(ipc_helper_lock);
  252. }
  253. static struct shim_ipc_port * __get_new_ipc_port (PAL_HANDLE hdl)
  254. {
  255. struct shim_ipc_port * port =
  256. get_mem_obj_from_mgr_enlarge(port_mgr,
  257. size_align_up(PORT_MGR_ALLOC));
  258. if (!port) {
  259. debug("failed to allocate shim_ipc_port\n");
  260. return NULL;
  261. }
  262. memset(port, 0, sizeof(struct shim_ipc_port));
  263. port->pal_handle = hdl;
  264. port->update = true;
  265. INIT_LIST_HEAD(port, hlist);
  266. INIT_LIST_HEAD(port, list);
  267. INIT_LISTP(&port->msgs);
  268. REF_SET(port->ref_count, 1);
  269. create_lock(port->msgs_lock);
  270. return port;
  271. }
  272. void add_ipc_port_by_id (IDTYPE vmid, PAL_HANDLE hdl, int type,
  273. port_fini fini, struct shim_ipc_port ** portptr)
  274. {
  275. debug("adding port (handle %p) for process %u (type %04x)\n",
  276. hdl, vmid, type);
  277. assert(!!hdl && PAL_GET_TYPE(hdl));
  278. lock(ipc_helper_lock);
  279. LISTP_TYPE(shim_ipc_port) * head = vmid ? &ipc_port_pool[PID_HASH(vmid)] : NULL;
  280. struct shim_ipc_port * tmp, * port = NULL;
  281. if (vmid)
  282. listp_for_each_entry(tmp, head, hlist)
  283. if (tmp->info.vmid == vmid && tmp->pal_handle == hdl) {
  284. port = tmp;
  285. __get_ipc_port(port);
  286. break;
  287. }
  288. if (!port)
  289. listp_for_each_entry(tmp, &pobj_list, list)
  290. if (tmp->pal_handle == hdl) {
  291. port = tmp;
  292. __get_ipc_port(port);
  293. break;
  294. }
  295. if (!port && !(port = __get_new_ipc_port(hdl))) {
  296. *portptr = NULL;
  297. goto out;
  298. }
  299. bool need_restart = __add_ipc_port(port, vmid, type, fini);
  300. assert(!list_empty(port, list));
  301. assert(!vmid || !list_empty(port, hlist));
  302. if (portptr)
  303. *portptr = port;
  304. else
  305. __put_ipc_port(port);
  306. if (need_restart)
  307. restart_ipc_helper(true);
  308. out:
  309. unlock(ipc_helper_lock);
  310. }
  311. static bool __del_ipc_port (struct shim_ipc_port * port, int type)
  312. {
  313. debug("deleting port %p (handle %p) for process %u\n",
  314. port, port->pal_handle, port->info.vmid);
  315. __get_ipc_port(port); // Prevent the object from being freed during deletion
  316. assert(!list_empty(port, list)); // Never delete a port twice
  317. bool need_restart = false;
  318. type = type ? (type & port->info.type) : port->info.type;
  319. if ((type & IPC_PORT_KEEPALIVE) ^
  320. (port->info.type & IPC_PORT_KEEPALIVE))
  321. need_restart = true;
  322. /* if the port still have other usage, we will not remove the port */
  323. if (port->info.type & ~(type|IPC_PORT_IFPOLL|IPC_PORT_KEEPALIVE)) {
  324. debug("masking port %p (handle %p): type %x->%x\n",
  325. port, port->pal_handle, port->info.type, port->info.type & ~type);
  326. port->info.type &= ~type;
  327. goto out;
  328. }
  329. // Prevent further usage of the PAL handle
  330. DkStreamDelete(port->pal_handle, 0);
  331. if (port->info.type & IPC_PORT_IFPOLL)
  332. need_restart = true;
  333. // Officially delete the port
  334. listp_del_init(port, &pobj_list, list);
  335. port->info.type &= IPC_PORT_IFPOLL;
  336. __put_ipc_port(port);
  337. if (!list_empty(port, hlist)) {
  338. // Re-fetch head pointer
  339. LISTP_TYPE(shim_ipc_port) * head = &ipc_port_pool[PID_HASH(port->info.vmid)];
  340. listp_del_init(port, head, hlist);
  341. __put_ipc_port(port);
  342. }
  343. // Need to check if there are any pending messages on the port, which means
  344. // some threads might be blocking for responses.
  345. lock(port->msgs_lock);
  346. struct shim_ipc_msg_obj * msg, * n;
  347. listp_for_each_entry_safe(msg, n, &port->msgs, list) {
  348. listp_del_init(msg, &port->msgs, list);
  349. msg->retval = -ECONNRESET;
  350. if (msg->thread) {
  351. debug("wake up thread %d\n", msg->thread->tid);
  352. thread_wakeup(msg->thread);
  353. }
  354. }
  355. unlock(port->msgs_lock);
  356. out:
  357. port->update = true;
  358. __put_ipc_port(port); // Free the object if ref_count is 0
  359. return need_restart;
  360. }
  361. void del_ipc_port (struct shim_ipc_port * port, int type)
  362. {
  363. lock(ipc_helper_lock);
  364. // If the port is already deleted, don't delete it again.
  365. if (list_empty(port, list)) {
  366. unlock(ipc_helper_lock);
  367. return;
  368. }
  369. bool need_restart = __del_ipc_port(port, type);
  370. if (need_restart)
  371. restart_ipc_helper(false);
  372. unlock(ipc_helper_lock);
  373. }
  374. void del_ipc_port_by_id (IDTYPE vmid, int type)
  375. {
  376. LISTP_TYPE(shim_ipc_port) * head = &ipc_port_pool[PID_HASH(vmid)];
  377. struct shim_ipc_port * port, *n;
  378. bool need_restart = false;
  379. lock(ipc_helper_lock);
  380. listp_for_each_entry_safe(port, n, head, hlist) {
  381. if (list_empty(port, list))
  382. continue;
  383. debug("port %p (handle %p) for process %u in list %p\n",
  384. port, port->pal_handle, port->info.vmid, head);
  385. if (port->info.vmid == vmid && __del_ipc_port(port, type))
  386. need_restart = true;
  387. }
  388. if (need_restart)
  389. restart_ipc_helper(false);
  390. unlock(ipc_helper_lock);
  391. }
  392. void del_ipc_port_fini (struct shim_ipc_port * port, unsigned int exitcode)
  393. {
  394. port_fini fini[MAX_IPC_PORT_FINI_CB];
  395. int nfini = 0;
  396. assert(REF_GET(port->ref_count) > 0);
  397. lock(ipc_helper_lock);
  398. // If the port is already deleted, don't delete it again.
  399. if (list_empty(port, list)) {
  400. unlock(ipc_helper_lock);
  401. return;
  402. }
  403. IDTYPE vmid = port->info.vmid;
  404. for (int i = 0 ; i < MAX_IPC_PORT_FINI_CB ; i++)
  405. if (port->fini[i]) {
  406. fini[nfini++] = port->fini[i];
  407. port->fini[i] = NULL;
  408. }
  409. bool need_restart = __del_ipc_port(port, 0);
  410. if (need_restart)
  411. restart_ipc_helper(false);
  412. unlock(ipc_helper_lock);
  413. for (int i = 0 ; i < nfini ; i++)
  414. (fini[i])(port, vmid, exitcode);
  415. }
  416. static struct shim_ipc_port * __lookup_ipc_port (IDTYPE vmid, int type)
  417. {
  418. LISTP_TYPE(shim_ipc_port) * head = &ipc_port_pool[PID_HASH(vmid)];
  419. struct shim_ipc_port * tmp;
  420. listp_for_each_entry(tmp, head, hlist)
  421. if (tmp->info.vmid == vmid && (!type || tmp->info.type & type)) {
  422. debug("found port %p (handle %p) for process %u (type %04x)\n",
  423. tmp, tmp->pal_handle, tmp->info.vmid, tmp->info.type);
  424. __get_ipc_port(tmp);
  425. return tmp;
  426. }
  427. return NULL;
  428. }
  429. struct shim_ipc_port * lookup_ipc_port (IDTYPE vmid, int type)
  430. {
  431. lock(ipc_helper_lock);
  432. struct shim_ipc_port * port = __lookup_ipc_port(vmid, type);
  433. if (port) {
  434. assert(!list_empty(port, list));
  435. assert(!vmid || !list_empty(port, hlist));
  436. }
  437. unlock(ipc_helper_lock);
  438. return port;
  439. }
  440. void get_ipc_port (struct shim_ipc_port * port)
  441. {
  442. // No need to grab ipc_helper_lock because __get_ipc_port() is atomic.
  443. __get_ipc_port(port);
  444. }
  445. void put_ipc_port (struct shim_ipc_port * port)
  446. {
  447. int ref_count = REF_DEC(port->ref_count);
  448. #ifdef DEBUG_REF
  449. debug("put ipc port %p (handle %p, ref_count = %d)\n", port,
  450. port->pal_handle, ref_count);
  451. #endif
  452. if (!ref_count) {
  453. lock(ipc_helper_lock); // Need to grab the lock
  454. __free_ipc_port(port);
  455. unlock(ipc_helper_lock);
  456. }
  457. }
  458. void del_all_ipc_ports (int type)
  459. {
  460. struct shim_ipc_port * pobj, * n;
  461. bool need_restart = false;
  462. lock(ipc_helper_lock);
  463. listp_for_each_entry_safe(pobj, n, &pobj_list, list) {
  464. if (__del_ipc_port(pobj, type))
  465. need_restart = true;
  466. }
  467. if (need_restart)
  468. restart_ipc_helper(false);
  469. unlock(ipc_helper_lock);
  470. }
  471. int broadcast_ipc (struct shim_ipc_msg * msg, struct shim_ipc_port ** exclude,
  472. int exsize, int target_type)
  473. {
  474. struct shim_ipc_port ** exend = exclude + exsize, ** ex;
  475. struct shim_ipc_port * pobj;
  476. if (!target_type && broadcast_port) {
  477. for (ex = exclude ; ex < exend && *ex != broadcast_port ; ex++);
  478. if (ex != exend)
  479. return 0;
  480. debug("send to broadcast stream\n");
  481. int ret = send_ipc_message(msg, broadcast_port);
  482. if (!ret)
  483. return 0;
  484. }
  485. lock(ipc_helper_lock);
  486. int ntargets = 0;
  487. listp_for_each_entry(pobj, &pobj_list, list) {
  488. debug("found port %p (handle %p) for process %u (type %04x)\n", pobj,
  489. pobj->pal_handle, pobj->info.vmid, pobj->info.type);
  490. if (pobj->info.type & target_type)
  491. ntargets++;
  492. }
  493. struct shim_ipc_port ** targets = __alloca(sizeof(struct shim_ipc_port *)
  494. * ntargets);
  495. int i = 0;
  496. listp_for_each_entry(pobj, &pobj_list, list)
  497. if (pobj->info.type & target_type) {
  498. get_ipc_port(pobj);
  499. targets[i++] = pobj;
  500. }
  501. unlock(ipc_helper_lock);
  502. for (i = 0 ; i < ntargets ; i++) {
  503. pobj = targets[i];
  504. debug("broadcast to port %p (handle %p) for process %u "
  505. "(type %x, target %x)\n",
  506. pobj, pobj->pal_handle, pobj->info.vmid,
  507. pobj->info.type, target_type);
  508. if (exsize) {
  509. for (ex = exclude ; ex < exend && *ex != pobj ; ex++);
  510. if (ex != exend)
  511. continue;
  512. }
  513. msg->dst = pobj->info.vmid;
  514. /* has to be assigned, so shim_send_ipc_message will not try
  515. to grab ipc_helper_lock */
  516. send_ipc_message(msg, pobj);
  517. put_ipc_port(pobj);
  518. }
  519. return 0;
  520. }
  521. static int ipc_resp_callback (IPC_CALLBACK_ARGS)
  522. {
  523. struct shim_ipc_resp * msgin = (struct shim_ipc_resp *) &msg->msg;
  524. debug("ipc callback from %u: IPC_RESP(%d)\n", msg->src, msgin->retval);
  525. if (!msg->seq)
  526. return msgin->retval;
  527. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  528. if (obj) {
  529. obj->retval = msgin->retval;
  530. if (obj->thread)
  531. thread_wakeup(obj->thread);
  532. return 0;
  533. }
  534. return msgin->retval;
  535. }
  536. static ipc_callback ipc_callbacks [IPC_CODE_NUM] = {
  537. /* RESP */ &ipc_resp_callback,
  538. /* FINDURI */ &ipc_finduri_callback,
  539. /* TELLURI */ &ipc_telluri_callback,
  540. /* CHECKPOINT */ &ipc_checkpoint_callback,
  541. /* parents and children */
  542. /* CLD_EXIT */ &ipc_cld_exit_callback,
  543. /* CLD_JOIN */ &ipc_cld_join_callback,
  544. #ifdef PROFILE
  545. /* CLD_PROFILE */ &ipc_cld_profile_callback,
  546. #endif
  547. /* pid namespace */
  548. IPC_NS_CALLBACKS(pid)
  549. /* PID_KILL */ &ipc_pid_kill_callback,
  550. /* PID_GETSTATUS */ &ipc_pid_getstatus_callback,
  551. /* PID_RETSTATUS */ &ipc_pid_retstatus_callback,
  552. /* PID_GETMETA */ &ipc_pid_getmeta_callback,
  553. /* PID_RETMETA */ &ipc_pid_retmeta_callback,
  554. /* PID_NOP */ &ipc_pid_nop_callback,
  555. /* PID_SENDRPC */ &ipc_pid_sendrpc_callback,
  556. /* sysv namespace */
  557. IPC_NS_CALLBACKS(sysv)
  558. IPC_NS_KEY_CALLBACKS(sysv)
  559. /* SYSV_DELRES */ &ipc_sysv_delres_callback,
  560. /* SYSV_MOVRES */ &ipc_sysv_movres_callback,
  561. /* SYSV_MSGSND */ &ipc_sysv_msgsnd_callback,
  562. /* SYSV_MSGRCV */ &ipc_sysv_msgrcv_callback,
  563. /* SYSV_MSGMOV */ &ipc_sysv_msgmov_callback,
  564. /* SYSV_SEMOP */ &ipc_sysv_semop_callback,
  565. /* SYSV_SEMCTL */ &ipc_sysv_semctl_callback,
  566. /* SYSV_SEMRET */ &ipc_sysv_semret_callback,
  567. /* SYSV_SEMMOV */ &ipc_sysv_semmov_callback,
  568. };
  569. int __response_ipc_message (struct shim_ipc_port * port, IDTYPE dest,
  570. int ret, unsigned long seq)
  571. {
  572. struct shim_ipc_msg * resp = create_ipc_resp_msg_on_stack(ret, dest, seq);
  573. ret = (ret == RESPONSE_CALLBACK) ? 0 : ret;
  574. debug("ipc send to %u: IPC_RESP(%d)\n", resp->dst, ret);
  575. struct shim_ipc_resp * msgin = (struct shim_ipc_resp *) &resp->msg;
  576. msgin->retval = ret;
  577. return send_ipc_message(resp, port);
  578. }
  579. /* not only ipc helper thread can receive messsage, anyone can
  580. receive message if they have acquired (locked) the port */
  581. int receive_ipc_message (struct shim_ipc_port * port, unsigned long seq,
  582. struct shim_ipc_msg ** msgptr)
  583. {
  584. int readahead = IPC_MSG_READAHEAD;
  585. int bufsize = IPC_MSG_MINIMAL_SIZE + readahead;
  586. struct shim_ipc_msg * msg = __alloca(bufsize);
  587. int expected_size;
  588. int bytes = 0, ret = 0;
  589. do {
  590. expected_size = IPC_MSG_MINIMAL_SIZE;
  591. while (bytes < expected_size) {
  592. retry_read:
  593. if (expected_size + readahead > bufsize) {
  594. while (expected_size + readahead > bufsize)
  595. bufsize *= 2;
  596. void * new_buff = __alloca(bufsize);
  597. memcpy(new_buff, msg, bytes);
  598. msg = new_buff;
  599. }
  600. if (!(ret = DkStreamRead(port->pal_handle, 0,
  601. expected_size - bytes + readahead,
  602. (void *) msg + bytes, NULL, 0)))
  603. break;
  604. bytes += ret;
  605. }
  606. if (!bytes) {
  607. if (PAL_NATIVE_ERRNO) {
  608. debug("port %p (handle %p) is removed at reading\n",
  609. port, port->pal_handle);
  610. del_ipc_port_fini(port, -ECHILD);
  611. ret = -PAL_ERRNO;
  612. }
  613. break;
  614. }
  615. debug("receive a message from port %p (handle %p): "
  616. "code=%d size=%d src=%u dst=%u seq=%lx\n",
  617. port, port->pal_handle,
  618. msg->code, msg->size, msg->src, msg->dst, msg->seq);
  619. expected_size = msg->size;
  620. if (bytes < expected_size)
  621. goto retry_read;
  622. if (msgptr && (!seq || msg->seq == seq)) {
  623. struct shim_ipc_msg * retmsg;
  624. if (*msgptr) {
  625. if (msg->size > (*msgptr)->size)
  626. msg->size = (*msgptr)->size;
  627. retmsg = *msgptr;
  628. } else {
  629. *msgptr = retmsg = malloc(msg->size);
  630. }
  631. memcpy(retmsg, msg, msg->size);
  632. return 0;
  633. }
  634. /* skip if the message comes from myself (it's possible because
  635. of the broadcast channel */
  636. if (msg->src == cur_process.vmid)
  637. goto next;
  638. ipc_callback callback = ipc_callbacks[msg->code];
  639. if (callback) {
  640. ret = (*callback) (msg, port);
  641. if ((ret < 0 || ret == RESPONSE_CALLBACK) && msg->seq)
  642. /* only helper thread sends back response */
  643. ret = __response_ipc_message(port, msg->src, ret, msg->seq);
  644. }
  645. next:
  646. if ((bytes -= expected_size) > 0)
  647. memmove(msg, (void *) msg + expected_size, bytes);
  648. } while (bytes > 0 || (seq && msg->seq != seq));
  649. if (msgptr)
  650. *msgptr = NULL;
  651. return ret;
  652. }
  653. #define IPC_HELPER_STACK_SIZE (allocsize * 4)
  654. #define IPC_HELPER_LIST_INIT_SIZE 32
  655. static void shim_ipc_helper (void * arg)
  656. {
  657. /* set ipc helper thread */
  658. struct shim_thread * self = (struct shim_thread *) arg;
  659. if (!arg)
  660. return;
  661. __libc_tcb_t tcb;
  662. allocate_tls(&tcb, false, self);
  663. debug_setbuf(&tcb.shim_tcb, true);
  664. debug("set tcb to %p\n", &tcb);
  665. lock(ipc_helper_lock);
  666. bool notme = (self != ipc_helper_thread);
  667. unlock(ipc_helper_lock);
  668. if (notme) {
  669. put_thread(self);
  670. DkThreadExit();
  671. return;
  672. }
  673. debug("ipc helper thread started\n");
  674. void * stack = allocate_stack(IPC_HELPER_STACK_SIZE, allocsize, false);
  675. if (!stack)
  676. goto end;
  677. self->stack_top = stack + IPC_HELPER_STACK_SIZE;
  678. self->stack = stack;
  679. switch_stack(stack + IPC_HELPER_STACK_SIZE);
  680. self = get_cur_thread();
  681. stack = self->stack;
  682. int port_num = 0, port_size = IPC_HELPER_LIST_INIT_SIZE;
  683. struct shim_ipc_port ** local_pobjs = stack, * pobj;
  684. PAL_HANDLE * local_ports;
  685. PAL_HANDLE ipc_event_handle = event_handle(&ipc_helper_event);
  686. int nalive = 0;
  687. PAL_HANDLE polled = NULL;
  688. int count = -1;
  689. local_ports = (PAL_HANDLE *) (local_pobjs + port_size);
  690. local_ports[0] = ipc_event_handle;
  691. goto update_status;
  692. /* The compiler should be careful not to cache the ipc_helper_state or
  693. * else ths loop could fail to terminate on update. Use a compiler
  694. * barrier to force a re-read after sleeping. */
  695. while ((ipc_helper_state == HELPER_ALIVE) ||
  696. nalive) {
  697. /* do a global poll on all the ports */
  698. polled = DkObjectsWaitAny(port_num + 1, local_ports, NO_TIMEOUT);
  699. barrier();
  700. if (!polled)
  701. continue;
  702. /* before we locking pobj list, at least we can look at the returned
  703. port if it is the ipc helper event */
  704. if (polled == ipc_event_handle) {
  705. clear_event(&ipc_helper_event);
  706. update_status:
  707. barrier();
  708. if (ipc_helper_state == HELPER_NOTALIVE)
  709. goto end;
  710. else
  711. goto update_list;
  712. }
  713. pobj = NULL;
  714. count = -1;
  715. for (int i = 0 ; i < port_num ; i++)
  716. if (polled == local_pobjs[i]->pal_handle) {
  717. pobj = local_pobjs[i];
  718. count = i;
  719. break;
  720. }
  721. if (!pobj)
  722. continue;
  723. /* if the polled port is a server port, accept a client and add it
  724. to the port list */
  725. if (pobj->private.type & IPC_PORT_SERVER) {
  726. PAL_HANDLE cli = DkStreamWaitForClient(polled);
  727. if (cli) {
  728. int type = (pobj->private.type & ~IPC_PORT_SERVER) |
  729. IPC_PORT_LISTEN;
  730. add_ipc_port_by_id(pobj->private.vmid, cli, type,
  731. NULL, NULL);
  732. } else {
  733. debug("port %p (handle %p) is removed at accepting\n",
  734. pobj, polled);
  735. del_ipc_port_fini(pobj, -ECHILD);
  736. }
  737. polled = NULL;
  738. count = -1;
  739. goto update_list;
  740. }
  741. PAL_STREAM_ATTR attr;
  742. if (!DkStreamAttributesQueryByHandle(polled, &attr)) {
  743. debug("port %p (handle %p) is removed at querying\n",
  744. pobj, polled);
  745. del_ipc_port_fini(pobj, -PAL_ERRNO);
  746. goto update_list;
  747. }
  748. if (attr.readable)
  749. receive_ipc_message(pobj, 0, NULL);
  750. if (attr.disconnected) {
  751. debug("port %p (handle %p) is disconnected\n",
  752. pobj, polled);
  753. del_ipc_port_fini(pobj, -ECONNRESET);
  754. goto update_list;
  755. }
  756. if (!ipc_helper_update)
  757. continue;
  758. update_list:
  759. ipc_helper_update = false;
  760. lock(ipc_helper_lock);
  761. int compact = 0;
  762. /* first walk though all the polling ports and remove the one
  763. being deleted. */
  764. for (int i = 0 ; i < port_num ; i++) {
  765. struct shim_ipc_port * pobj = local_pobjs[i];
  766. // If the port is removed from the list or intended to be deleted,
  767. // remove the port from the polling array
  768. if (list_empty(pobj, list)) {
  769. if (polled == pobj->pal_handle) {
  770. polled = NULL;
  771. count = -1;
  772. }
  773. local_pobjs[i] = NULL;
  774. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  775. nalive--;
  776. __put_ipc_port(pobj);
  777. compact++;
  778. continue;
  779. }
  780. if (pobj->update) {
  781. if (pobj->info.type & IPC_PORT_KEEPALIVE) {
  782. if (!(pobj->private.type & IPC_PORT_KEEPALIVE))
  783. nalive--;
  784. } else {
  785. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  786. nalive++;
  787. }
  788. pobj->private = pobj->info;
  789. pobj->update = false;
  790. }
  791. if (compact) {
  792. if (polled == pobj->pal_handle)
  793. count -= compact;
  794. local_pobjs[i - compact] = pobj;
  795. local_ports[i - compact + 1] = pobj->pal_handle;
  796. }
  797. }
  798. port_num -= compact;
  799. listp_for_each_entry(pobj, &pobj_list, list) {
  800. /* we only update among recently updated ports */
  801. if (!pobj->recent)
  802. break;
  803. if (pobj->update) {
  804. pobj->private = pobj->info;
  805. pobj->update = false;
  806. }
  807. assert(pobj->private.type & IPC_PORT_IFPOLL);
  808. if (port_num == port_size) {
  809. port_size *= 2;
  810. memmove(local_pobjs + port_size,
  811. local_ports,
  812. (port_num + 1) * sizeof(PAL_HANDLE));
  813. local_ports = (PAL_HANDLE *) (local_pobjs + port_size);
  814. }
  815. pobj->recent = false;
  816. __get_ipc_port(pobj);
  817. local_pobjs[port_num] = pobj;
  818. local_ports[port_num + 1] = pobj->pal_handle;
  819. port_num++;
  820. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  821. nalive++;
  822. debug("listen to process %u on port %p (handle %p, type %04x)\n",
  823. pobj->private.vmid,
  824. pobj,
  825. pobj->pal_handle,
  826. pobj->private.type);
  827. }
  828. unlock(ipc_helper_lock);
  829. }
  830. for (int i = 0 ; i < port_num ; i++) {
  831. struct shim_ipc_port * pobj = local_pobjs[i];
  832. __put_ipc_port(pobj);
  833. }
  834. end:
  835. /* DP: Put our handle map reference */
  836. if (self->handle_map)
  837. put_handle_map(self->handle_map);
  838. /* shim_clean ultimately calls del_all_ipc_ports(), which reacquires the
  839. * helper lock. Err on the side of caution by adding a barrier to ensure
  840. * reading the latest ipc helper state.
  841. */
  842. barrier();
  843. if (ipc_helper_state == HELPER_HANDEDOVER) {
  844. debug("ipc helper thread is the last thread, process exiting\n");
  845. shim_terminate(0); // Same as shim_clean(), but this is the official termination function
  846. }
  847. lock(ipc_helper_lock);
  848. ipc_helper_state = HELPER_NOTALIVE;
  849. ipc_helper_thread = NULL;
  850. unlock(ipc_helper_lock);
  851. put_thread(self);
  852. debug("ipc helper thread terminated\n");
  853. DkThreadExit();
  854. }
  855. /* This function shoudl be called with the ipc_helper_lock held */
  856. static int create_ipc_helper (void)
  857. {
  858. int ret = 0;
  859. /* If we are holding the lock, no barrier is needed here, as
  860. * the lock (and new function) form an implicit barrier, and
  861. * any "recent" changes should have come from this thread */
  862. if (ipc_helper_state == HELPER_ALIVE)
  863. return 0;
  864. struct shim_thread * new = get_new_internal_thread();
  865. if (!new)
  866. return -ENOMEM;
  867. ipc_helper_thread = new;
  868. ipc_helper_state = HELPER_ALIVE;
  869. PAL_HANDLE handle = thread_create(shim_ipc_helper, new, 0);
  870. if (!handle) {
  871. ret = -PAL_ERRNO;
  872. ipc_helper_thread = NULL;
  873. ipc_helper_state = HELPER_NOTALIVE;
  874. put_thread(new);
  875. return ret;
  876. }
  877. new->pal_handle = handle;
  878. return 0;
  879. }
  880. /*
  881. * on success, the reference to the helper thread is returned with
  882. * reference count incremented.
  883. * The caller is responsible to wait for the IPC helper thread to exit
  884. * and release the final reference to free related resources.
  885. * It's problematic for the thread itself to release its resources which it's
  886. * using. For example stack.
  887. * So defer releasing it after its exit and make the releasing the caller
  888. * responsibility.
  889. */
  890. int exit_with_ipc_helper (bool handover, struct shim_thread ** ret)
  891. {
  892. *ret = NULL;
  893. if (IN_HELPER() || ipc_helper_state != HELPER_ALIVE)
  894. return 0;
  895. lock(ipc_helper_lock);
  896. if (handover) {
  897. handover = false;
  898. struct shim_ipc_port * pobj;
  899. listp_for_each_entry(pobj, &pobj_list, list)
  900. if (pobj->info.type & IPC_PORT_KEEPALIVE) {
  901. handover = true;
  902. break;
  903. }
  904. }
  905. int new_state = HELPER_NOTALIVE;
  906. if (handover) {
  907. debug("handing over to ipc helper\n");
  908. new_state = HELPER_HANDEDOVER;
  909. } else {
  910. debug("exiting ipc helper\n");
  911. }
  912. ipc_helper_state = new_state;
  913. if (ipc_helper_thread != NULL) {
  914. get_thread(ipc_helper_thread);
  915. *ret = ipc_helper_thread;
  916. }
  917. unlock(ipc_helper_lock);
  918. set_event(&ipc_helper_event, 1);
  919. if (new_state != HELPER_NOTALIVE) {
  920. return -EAGAIN;
  921. } else {
  922. /* We could get here via a signal handler invoked during
  923. * receive_ipc_message. Let that complete so that whoever
  924. * generated the signal doesn't hang waiting for IPC_RESP. */
  925. int loops = 0;
  926. while (ipc_helper_thread != NULL && loops++ < 2000) {
  927. barrier();
  928. DkThreadDelayExecution(1000);
  929. }
  930. if (ipc_helper_thread != NULL) {
  931. debug("timed out waiting for ipc helper to exit\n");
  932. }
  933. return 0;
  934. }
  935. }
  936. int terminate_ipc_helper (void)
  937. {
  938. lock(ipc_helper_lock);
  939. struct shim_thread * thread = ipc_helper_thread;
  940. if (!thread) {
  941. unlock(ipc_helper_lock);
  942. return -ESRCH;
  943. }
  944. debug("terminating ipc helper\n");
  945. ipc_helper_state = HELPER_NOTALIVE;
  946. set_event(&ipc_helper_event, 1);
  947. unlock(ipc_helper_lock);
  948. return 0;
  949. }