shim_ipc_helper.c 34 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167
  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. noreturn static void shim_ipc_helper_end(struct shim_thread * self)
  654. {
  655. /* Put our handle map reference */
  656. if (self->handle_map)
  657. put_handle_map(self->handle_map);
  658. /* Another thread may be calling shim_clean(). Lower the chances of our
  659. * IPC helper thread and another thread competing on shim_clean/shim_terminate
  660. * (which is benign due to protection via ipc_helper_lock) by adding a barrier
  661. * to ensure reading the latest IPC helper state. */
  662. COMPILER_BARRIER();
  663. if (ipc_helper_state == HELPER_HANDEDOVER) {
  664. debug("ipc helper thread is the last thread, process exiting\n");
  665. shim_terminate(0); // Same as shim_clean(), but this is the official termination function
  666. }
  667. lock(&ipc_helper_lock);
  668. ipc_helper_state = HELPER_NOTALIVE;
  669. ipc_helper_thread = NULL;
  670. unlock(&ipc_helper_lock);
  671. put_thread(self);
  672. debug("ipc helper thread terminated\n");
  673. DkThreadExit();
  674. }
  675. noreturn static void __shim_ipc_helper (void * dummy)
  676. {
  677. struct shim_thread * self = get_cur_thread();
  678. void * stack = self->stack;
  679. int port_num = 0, port_size = IPC_HELPER_LIST_INIT_SIZE;
  680. struct shim_ipc_port ** local_pobjs = stack, * pobj;
  681. PAL_HANDLE * local_ports;
  682. PAL_HANDLE ipc_event_handle = event_handle(&ipc_helper_event);
  683. int nalive = 0;
  684. PAL_HANDLE polled = NULL;
  685. int count = -1;
  686. local_ports = (PAL_HANDLE *) (local_pobjs + port_size);
  687. local_ports[0] = ipc_event_handle;
  688. goto update_status;
  689. /* The compiler should be careful not to cache the ipc_helper_state or
  690. * else ths loop could fail to terminate on update. Use a compiler
  691. * barrier to force a re-read after sleeping. */
  692. while ((ipc_helper_state == HELPER_ALIVE) ||
  693. nalive) {
  694. /* do a global poll on all the ports */
  695. polled = DkObjectsWaitAny(port_num + 1, local_ports, NO_TIMEOUT);
  696. COMPILER_BARRIER();
  697. if (!polled)
  698. continue;
  699. /* before we locking pobj list, at least we can look at the returned
  700. port if it is the ipc helper event */
  701. if (polled == ipc_event_handle) {
  702. clear_event(&ipc_helper_event);
  703. update_status:
  704. COMPILER_BARRIER();
  705. if (ipc_helper_state == HELPER_NOTALIVE)
  706. goto end;
  707. else
  708. goto update_list;
  709. }
  710. pobj = NULL;
  711. count = -1;
  712. for (int i = 0 ; i < port_num ; i++)
  713. if (polled == local_pobjs[i]->pal_handle) {
  714. pobj = local_pobjs[i];
  715. count = i;
  716. break;
  717. }
  718. if (!pobj)
  719. continue;
  720. /* if the polled port is a server port, accept a client and add it
  721. to the port list */
  722. if (pobj->private.type & IPC_PORT_SERVER) {
  723. PAL_HANDLE cli = DkStreamWaitForClient(polled);
  724. if (cli) {
  725. IDTYPE type = (pobj->private.type & ~IPC_PORT_SERVER) |
  726. IPC_PORT_LISTEN;
  727. add_ipc_port_by_id(pobj->private.vmid, cli, type,
  728. NULL, NULL);
  729. } else {
  730. debug("port %p (handle %p) is removed at accepting\n",
  731. pobj, polled);
  732. del_ipc_port_fini(pobj, -ECHILD);
  733. }
  734. polled = NULL;
  735. count = -1;
  736. goto update_list;
  737. }
  738. PAL_STREAM_ATTR attr;
  739. if (!DkStreamAttributesQueryByHandle(polled, &attr)) {
  740. debug("port %p (handle %p) is removed at querying\n",
  741. pobj, polled);
  742. del_ipc_port_fini(pobj, -PAL_ERRNO);
  743. goto update_list;
  744. }
  745. if (attr.readable)
  746. receive_ipc_message(pobj, 0, NULL);
  747. if (attr.disconnected) {
  748. debug("port %p (handle %p) is disconnected\n",
  749. pobj, polled);
  750. del_ipc_port_fini(pobj, -ECONNRESET);
  751. goto update_list;
  752. }
  753. if (!ipc_helper_update)
  754. continue;
  755. update_list:
  756. ipc_helper_update = false;
  757. lock(&ipc_helper_lock);
  758. int compact = 0;
  759. /* first walk though all the polling ports and remove the one
  760. being deleted. */
  761. for (int i = 0 ; i < port_num ; i++) {
  762. struct shim_ipc_port * pobj = local_pobjs[i];
  763. // If the port is removed from the list or intended to be deleted,
  764. // remove the port from the polling array
  765. if (LIST_EMPTY(pobj, list)) {
  766. if (polled == pobj->pal_handle) {
  767. polled = NULL;
  768. count = -1;
  769. }
  770. local_pobjs[i] = NULL;
  771. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  772. nalive--;
  773. __put_ipc_port(pobj);
  774. compact++;
  775. continue;
  776. }
  777. if (pobj->update) {
  778. if (pobj->info.type & IPC_PORT_KEEPALIVE) {
  779. if (!(pobj->private.type & IPC_PORT_KEEPALIVE))
  780. nalive--;
  781. } else {
  782. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  783. nalive++;
  784. }
  785. pobj->private = pobj->info;
  786. pobj->update = false;
  787. }
  788. if (compact) {
  789. if (polled == pobj->pal_handle)
  790. count -= compact;
  791. local_pobjs[i - compact] = pobj;
  792. local_ports[i - compact + 1] = pobj->pal_handle;
  793. }
  794. }
  795. port_num -= compact;
  796. LISTP_FOR_EACH_ENTRY(pobj, &pobj_list, list) {
  797. /* we only update among recently updated ports */
  798. if (!pobj->recent)
  799. break;
  800. if (pobj->update) {
  801. pobj->private = pobj->info;
  802. pobj->update = false;
  803. }
  804. assert(pobj->private.type & IPC_PORT_IFPOLL);
  805. if (port_num == port_size) {
  806. port_size *= 2;
  807. memmove(local_pobjs + port_size,
  808. local_ports,
  809. (port_num + 1) * sizeof(PAL_HANDLE));
  810. local_ports = (PAL_HANDLE *) (local_pobjs + port_size);
  811. }
  812. pobj->recent = false;
  813. __get_ipc_port(pobj);
  814. local_pobjs[port_num] = pobj;
  815. local_ports[port_num + 1] = pobj->pal_handle;
  816. port_num++;
  817. if (pobj->private.type & IPC_PORT_KEEPALIVE)
  818. nalive++;
  819. debug("listen to process %u on port %p (handle %p, type %04x)\n",
  820. pobj->private.vmid,
  821. pobj,
  822. pobj->pal_handle,
  823. pobj->private.type);
  824. }
  825. unlock(&ipc_helper_lock);
  826. }
  827. for (int i = 0 ; i < port_num ; i++) {
  828. struct shim_ipc_port * pobj = local_pobjs[i];
  829. __put_ipc_port(pobj);
  830. }
  831. end:
  832. shim_ipc_helper_end(self);
  833. }
  834. static void shim_ipc_helper (void * arg)
  835. {
  836. /* set ipc helper thread */
  837. struct shim_thread * self = (struct shim_thread *) arg;
  838. if (!arg)
  839. return;
  840. __libc_tcb_t tcb;
  841. allocate_tls(&tcb, false, self);
  842. debug_setbuf(&tcb.shim_tcb, true);
  843. debug("set tcb to %p\n", &tcb);
  844. lock(&ipc_helper_lock);
  845. bool notme = (self != ipc_helper_thread);
  846. unlock(&ipc_helper_lock);
  847. if (notme) {
  848. put_thread(self);
  849. DkThreadExit();
  850. return;
  851. }
  852. debug("ipc helper thread started\n");
  853. void * stack = allocate_stack(IPC_HELPER_STACK_SIZE, allocsize, false);
  854. if (!stack) {
  855. shim_ipc_helper_end(self);
  856. return;
  857. }
  858. self->stack_top = stack + IPC_HELPER_STACK_SIZE;
  859. self->stack = stack;
  860. __SWITCH_STACK(self->stack_top, __shim_ipc_helper, NULL);
  861. }
  862. /* This function shoudl be called with the ipc_helper_lock held */
  863. static int create_ipc_helper (void)
  864. {
  865. int ret = 0;
  866. /* If we are holding the lock, no barrier is needed here, as
  867. * the lock (and new function) form an implicit barrier, and
  868. * any "recent" changes should have come from this thread */
  869. if (ipc_helper_state == HELPER_ALIVE)
  870. return 0;
  871. struct shim_thread * new = get_new_internal_thread();
  872. if (!new)
  873. return -ENOMEM;
  874. ipc_helper_thread = new;
  875. ipc_helper_state = HELPER_ALIVE;
  876. PAL_HANDLE handle = thread_create(shim_ipc_helper, new, 0);
  877. if (!handle) {
  878. ret = -PAL_ERRNO;
  879. ipc_helper_thread = NULL;
  880. ipc_helper_state = HELPER_NOTALIVE;
  881. put_thread(new);
  882. return ret;
  883. }
  884. new->pal_handle = handle;
  885. return 0;
  886. }
  887. /*
  888. * on success, the reference to the helper thread is returned with
  889. * reference count incremented.
  890. * The caller is responsible to wait for the IPC helper thread to exit
  891. * and release the final reference to free related resources.
  892. * It's problematic for the thread itself to release its resources which it's
  893. * using. For example stack.
  894. * So defer releasing it after its exit and make the releasing the caller
  895. * responsibility.
  896. */
  897. int exit_with_ipc_helper (bool handover, struct shim_thread ** ret)
  898. {
  899. *ret = NULL;
  900. if (IN_HELPER() || ipc_helper_state != HELPER_ALIVE)
  901. return 0;
  902. lock(&ipc_helper_lock);
  903. if (handover) {
  904. handover = false;
  905. struct shim_ipc_port * pobj;
  906. LISTP_FOR_EACH_ENTRY(pobj, &pobj_list, list)
  907. if (pobj->info.type & IPC_PORT_KEEPALIVE) {
  908. handover = true;
  909. break;
  910. }
  911. }
  912. int new_state = HELPER_NOTALIVE;
  913. if (handover) {
  914. debug("handing over to ipc helper\n");
  915. new_state = HELPER_HANDEDOVER;
  916. } else {
  917. debug("exiting ipc helper\n");
  918. }
  919. ipc_helper_state = new_state;
  920. if (ipc_helper_thread != NULL) {
  921. get_thread(ipc_helper_thread);
  922. *ret = ipc_helper_thread;
  923. }
  924. unlock(&ipc_helper_lock);
  925. set_event(&ipc_helper_event, 1);
  926. if (new_state != HELPER_NOTALIVE) {
  927. return -EAGAIN;
  928. } else {
  929. /* We could get here via a signal handler invoked during
  930. * receive_ipc_message. Let that complete so that whoever
  931. * generated the signal doesn't hang waiting for IPC_RESP. */
  932. int loops = 0;
  933. while (ipc_helper_thread != NULL && loops++ < 2000) {
  934. COMPILER_BARRIER();
  935. DkThreadDelayExecution(1000);
  936. }
  937. if (ipc_helper_thread != NULL) {
  938. debug("timed out waiting for ipc helper to exit\n");
  939. }
  940. return 0;
  941. }
  942. }
  943. int terminate_ipc_helper (void)
  944. {
  945. lock(&ipc_helper_lock);
  946. struct shim_thread * thread = ipc_helper_thread;
  947. if (!thread) {
  948. unlock(&ipc_helper_lock);
  949. return -ESRCH;
  950. }
  951. debug("terminating ipc helper\n");
  952. ipc_helper_state = HELPER_NOTALIVE;
  953. set_event(&ipc_helper_event, 1);
  954. unlock(&ipc_helper_lock);
  955. return 0;
  956. }