shim_ipc_helper.c 34 KB

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