shim_ipc_helper.c 32 KB

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