shim_ipc_helper.c 32 KB

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