shim_ipc_sysv.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041
  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 OSCAR lab, 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 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 General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  15. /*
  16. * shim_ipc_pid.c
  17. *
  18. * This file contains functions and callbacks to handle IPC of SYSV namespace.
  19. */
  20. #include <shim_internal.h>
  21. #include <shim_thread.h>
  22. #include <shim_ipc.h>
  23. #include <shim_checkpoint.h>
  24. #include <shim_sysv.h>
  25. #include <pal.h>
  26. #include <pal_error.h>
  27. #include <errno.h>
  28. #define SYSV_RANGE_SIZE 128
  29. #define SYSV_LEASE_TIME 1000
  30. #define KEY_HASH(k) ((k)->key)
  31. #define KEY_COMP(k1, k2) \
  32. ((k1)->key != (k2)->key || (k1)->type != (k2)->type)
  33. #define KEY_COPY(k1, k2) \
  34. do { (k1)->key = (k2)->key; (k1)->type = (k2)->type; } while (0)
  35. #define NS sysv
  36. #define NS_CAP SYSV
  37. #define NS_KEY struct sysv_key
  38. #include "shim_ipc_nsimpl.h"
  39. int init_ns_sysv (void)
  40. {
  41. init_namespace();
  42. return 0;
  43. }
  44. DEFINE_PROFILE_INTERVAL(ipc_sysv_delres_send, ipc);
  45. DEFINE_PROFILE_INTERVAL(ipc_sysv_delres_callback, ipc);
  46. int ipc_sysv_delres_send (struct shim_ipc_port * port, IDTYPE dest,
  47. IDTYPE resid, enum sysv_type type)
  48. {
  49. BEGIN_PROFILE_INTERVAL();
  50. int ret = 0;
  51. bool owned = false;
  52. if (!port) {
  53. if ((ret = connect_owner(resid, &port, &dest)) < 0)
  54. goto out;
  55. owned = true;
  56. }
  57. if (!owned) {
  58. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  59. IPC_SYSV_DELRES,
  60. sizeof(struct shim_ipc_sysv_delres),
  61. dest);
  62. struct shim_ipc_sysv_delres * msgin = (struct shim_ipc_sysv_delres *)
  63. &msg->msg;
  64. msgin->resid = resid;
  65. msgin->type = type;
  66. debug("ipc send to %u: IPC_SYSV_DELRES(%u, %s)\n", dest, resid,
  67. SYSV_TYPE_STR(type));
  68. ret = send_ipc_message(msg, port);
  69. goto out;
  70. }
  71. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  72. IPC_SYSV_DELRES,
  73. sizeof(struct shim_ipc_sysv_delres),
  74. dest);
  75. struct shim_ipc_sysv_delres * msgin = (struct shim_ipc_sysv_delres *)
  76. &msg->msg.msg;
  77. msgin->resid = resid;
  78. msgin->type = type;
  79. debug("ipc send to %u: IPC_SYSV_DELRES(%u, %s)\n", dest, resid,
  80. SYSV_TYPE_STR(type));
  81. ret = do_ipc_duplex(msg, port, NULL, NULL);
  82. put_ipc_port(port);
  83. out:
  84. SAVE_PROFILE_INTERVAL(ipc_sysv_delres_send);
  85. return ret;
  86. }
  87. int ipc_sysv_delres_callback (IPC_CALLBACK_ARGS)
  88. {
  89. BEGIN_PROFILE_INTERVAL();
  90. int ret = 0;
  91. struct shim_ipc_sysv_delres * msgin =
  92. (struct shim_ipc_sysv_delres *) &msg->msg;
  93. debug("ipc callback from %u: IPC_SYSV_DELRES(%u, %s)\n", msg->src,
  94. msgin->resid, SYSV_TYPE_STR(msgin->type));
  95. bool owned = false;
  96. ret = -ENOENT;
  97. switch(msgin->type) {
  98. case SYSV_MSGQ: {
  99. struct shim_msg_handle * msgq = get_msg_handle_by_id(msgin->resid);
  100. if (!msgq)
  101. goto out;
  102. owned = msgq->owned;
  103. ret = del_msg_handle(msgq);
  104. break;
  105. }
  106. case SYSV_SEM: {
  107. struct shim_sem_handle * sem = get_sem_handle_by_id(msgin->resid);
  108. if (!sem)
  109. goto out;
  110. owned = sem->owned;
  111. ret = del_sem_handle(sem);
  112. break;
  113. }
  114. default:
  115. ret = -ENOSYS;
  116. break;
  117. }
  118. if (!ret)
  119. ret = owned ? RESPONSE_CALLBACK : 0;
  120. out:
  121. SAVE_PROFILE_INTERVAL(ipc_sysv_delres_callback);
  122. return ret;
  123. }
  124. DEFINE_PROFILE_INTERVAL(ipc_sysv_movres_send, ipc);
  125. DEFINE_PROFILE_INTERVAL(ipc_sysv_movres_callback, ipc);
  126. int ipc_sysv_movres_send (struct sysv_client * client, IDTYPE owner,
  127. const char * uri, LEASETYPE lease, IDTYPE resid,
  128. enum sysv_type type)
  129. {
  130. BEGIN_PROFILE_INTERVAL();
  131. int ret = 0;
  132. int len = strlen(uri);
  133. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  134. IPC_SYSV_MOVRES,
  135. sizeof(struct shim_ipc_sysv_movres) +
  136. len, client->vmid);
  137. struct shim_ipc_sysv_movres * msgin = (struct shim_ipc_sysv_movres *)
  138. &msg->msg;
  139. msgin->resid = resid;
  140. msgin->type = type;
  141. msgin->owner = owner;
  142. msgin->lease = lease;
  143. memcpy(msgin->uri, uri, len + 1);
  144. msg->seq = client->seq;
  145. debug("ipc send to %u: IPC_SYSV_MOVRES(%u, %s, %u, %s)\n", client->vmid,
  146. resid, SYSV_TYPE_STR(type), owner, uri);
  147. ret = send_ipc_message(msg, client->port);
  148. SAVE_PROFILE_INTERVAL(ipc_sysv_movres_send);
  149. return ret;
  150. }
  151. int ipc_sysv_movres_callback (IPC_CALLBACK_ARGS)
  152. {
  153. BEGIN_PROFILE_INTERVAL();
  154. int ret = 0;
  155. struct shim_ipc_sysv_movres * msgin =
  156. (struct shim_ipc_sysv_movres *) &msg->msg;
  157. debug("ipc callback from %u: IPC_SYSV_MOVRES(%u, %s, %u, %s)\n", msg->src,
  158. msgin->resid, SYSV_TYPE_STR(msgin->type), msgin->owner, msgin->uri);
  159. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  160. if (!obj)
  161. goto out;
  162. switch(msgin->type) {
  163. case SYSV_MSGQ:
  164. case SYSV_SEM:
  165. obj->retval = -EAGAIN;
  166. break;
  167. default:
  168. ret = -ENOSYS;
  169. goto out;
  170. }
  171. add_sysv_subrange(msgin->resid, msgin->owner, msgin->uri, &msgin->lease);
  172. if (obj->thread)
  173. thread_wakeup(obj->thread);
  174. out:
  175. SAVE_PROFILE_INTERVAL(ipc_sysv_movres_callback);
  176. return ret;
  177. }
  178. DEFINE_PROFILE_INTERVAL(ipc_sysv_msgsnd_send, ipc);
  179. DEFINE_PROFILE_INTERVAL(ipc_sysv_msgsnd_callback, ipc);
  180. int ipc_sysv_msgsnd_send (struct shim_ipc_port * port, IDTYPE dest,
  181. IDTYPE msgid, long msgtype,
  182. const void * buf, size_t size, unsigned long seq)
  183. {
  184. BEGIN_PROFILE_INTERVAL();
  185. int ret = 0;
  186. bool owned = true;
  187. if (!dest) {
  188. if ((ret = connect_owner(msgid, &port, &dest)) < 0)
  189. goto out;
  190. owned = false;
  191. }
  192. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  193. IPC_SYSV_MSGSND,
  194. sizeof(struct shim_ipc_sysv_msgsnd) +
  195. size, dest);
  196. struct shim_ipc_sysv_msgsnd * msgin =
  197. (struct shim_ipc_sysv_msgsnd *) &msg->msg;
  198. msgin->msgid = msgid;
  199. msgin->msgtype = msgtype;
  200. memcpy(msgin->msg, buf, size);
  201. msg->seq = seq;
  202. debug("ipc send to %u: IPC_SYSV_MSGSND(%u, %ld)\n", dest,
  203. msgid, msgtype);
  204. ret = send_ipc_message(msg, port);
  205. if (!owned)
  206. put_ipc_port(port);
  207. out:
  208. SAVE_PROFILE_INTERVAL(ipc_sysv_msgsnd_send);
  209. return ret;
  210. }
  211. int ipc_sysv_msgsnd_callback (IPC_CALLBACK_ARGS)
  212. {
  213. BEGIN_PROFILE_INTERVAL();
  214. int ret = 0;
  215. struct shim_ipc_sysv_msgsnd * msgin =
  216. (struct shim_ipc_sysv_msgsnd *) &msg->msg;
  217. debug("ipc callback from %u: IPC_SYSV_MSGSND(%u, %ld)\n", msg->src,
  218. msgin->msgid, msgin->msgtype);
  219. size_t size = msg->size - sizeof(*msg) - sizeof(*msgin);
  220. if (msg->seq) {
  221. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  222. void * priv = obj ? obj->private : NULL;
  223. if (priv) {
  224. struct shim_ipc_sysv_msgrcv * rcv =
  225. (struct shim_ipc_sysv_msgrcv *) obj->msg.msg;
  226. if (size > rcv->size)
  227. size = rcv->size;
  228. memcpy(priv, msgin->msg, size);
  229. obj->retval = size;
  230. if (obj->thread)
  231. thread_wakeup(obj->thread);
  232. goto out;
  233. }
  234. }
  235. struct shim_msg_handle * msgq = get_msg_handle_by_id(msgin->msgid);
  236. if (!msgq) {
  237. ret = -ENOENT;
  238. goto out;
  239. }
  240. if (msg->seq) {
  241. ret = add_sysv_msg(msgq, msgin->msgtype, size, msgin->msg, NULL);
  242. } else {
  243. struct sysv_client src;
  244. src.port = port;
  245. src.vmid = msg->src;
  246. src.seq = msg->seq;
  247. ret = add_sysv_msg(msgq, msgin->msgtype, size, msgin->msg, &src);
  248. }
  249. out:
  250. SAVE_PROFILE_INTERVAL(ipc_sysv_msgsnd_callback);
  251. return ret;
  252. }
  253. DEFINE_PROFILE_INTERVAL(ipc_sysv_msgrcv_send, ipc);
  254. DEFINE_PROFILE_INTERVAL(ipc_sysv_msgrcv_callback, ipc);
  255. int ipc_sysv_msgrcv_send (IDTYPE msgid, long msgtype, int flags, void * buf,
  256. size_t size)
  257. {
  258. BEGIN_PROFILE_INTERVAL();
  259. IDTYPE owner;
  260. struct shim_ipc_port * port = NULL;
  261. int ret = 0;
  262. if ((ret = connect_owner(msgid, &port, &owner)) < 0)
  263. goto out;
  264. if (owner == cur_process.vmid) {
  265. ret = -EAGAIN;
  266. goto out;
  267. }
  268. assert(port);
  269. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  270. IPC_SYSV_MSGRCV,
  271. sizeof(struct shim_ipc_sysv_msgrcv),
  272. true);
  273. struct shim_ipc_sysv_msgrcv * msgin =
  274. (struct shim_ipc_sysv_msgrcv *) &msg->msg.msg;
  275. msgin->msgid = msgid;
  276. msgin->msgtype = msgtype;
  277. msgin->size = size;
  278. msgin->flags = flags;
  279. debug("ipc send to %u: IPC_SYSV_MSGRCV(%u, %ld)\n", owner,
  280. msgid, msgtype);
  281. ret = do_ipc_duplex(msg, port, NULL, buf);
  282. put_ipc_port(port);
  283. out:
  284. SAVE_PROFILE_INTERVAL(ipc_sysv_msgrcv_send);
  285. return ret;
  286. }
  287. int ipc_sysv_msgrcv_callback (IPC_CALLBACK_ARGS)
  288. {
  289. BEGIN_PROFILE_INTERVAL();
  290. int ret = 0;
  291. struct shim_ipc_sysv_msgrcv * msgin =
  292. (struct shim_ipc_sysv_msgrcv *) &msg->msg;
  293. debug("ipc callback from %u: IPC_SYSV_MSGRCV(%u, %ld)\n", msg->src,
  294. msgin->msgid, msgin->msgtype);
  295. struct shim_msg_handle * msgq = get_msg_handle_by_id(msgin->msgid);
  296. if (!msgq) {
  297. ret = -ENOENT;
  298. goto out;
  299. }
  300. void * buf = __alloca(msgin->size);
  301. struct sysv_client src;
  302. src.port = port;
  303. src.vmid = msg->src;
  304. src.seq = msg->seq;
  305. ret = get_sysv_msg(msgq, msgin->msgtype, msgin->size, buf, msgin->flags,
  306. &src);
  307. if (ret > 0) {
  308. size_t size = ret;
  309. ret = ipc_sysv_msgsnd_send(port, msg->src, msgin->msgid, msgin->msgtype,
  310. buf, size, msg->seq);
  311. }
  312. put_msg_handle(msgq);
  313. out:
  314. SAVE_PROFILE_INTERVAL(ipc_sysv_msgrcv_callback);
  315. return ret;
  316. }
  317. DEFINE_PROFILE_INTERVAL(ipc_sysv_msgmov_send, ipc);
  318. DEFINE_PROFILE_INTERVAL(ipc_sysv_msgmov_callback, ipc);
  319. int ipc_sysv_msgmov_send (struct shim_ipc_port * port, IDTYPE dest,
  320. IDTYPE msgid, LEASETYPE lease,
  321. struct sysv_score * scores, int nscores)
  322. {
  323. BEGIN_PROFILE_INTERVAL();
  324. struct shim_ipc_msg * msg =
  325. create_ipc_msg_on_stack(IPC_SYSV_MSGMOV,
  326. sizeof(struct shim_ipc_sysv_msgmov) +
  327. sizeof(struct sysv_score) * nscores,
  328. dest);
  329. struct shim_ipc_sysv_msgmov * msgin =
  330. (struct shim_ipc_sysv_msgmov *) &msg->msg;
  331. msgin->msgid = msgid;
  332. msgin->lease = lease;
  333. msgin->nscores = nscores;
  334. if (nscores)
  335. memcpy(msgin->scores, scores, sizeof(struct sysv_score) * nscores);
  336. debug("ipc send to %u: IPC_SYSV_MSGMOV(%ld)\n", dest, msgid);
  337. int ret = send_ipc_message(msg, port);
  338. SAVE_PROFILE_INTERVAL(ipc_sysv_msgmov_send);
  339. return ret;
  340. }
  341. int ipc_sysv_msgmov_callback (IPC_CALLBACK_ARGS)
  342. {
  343. BEGIN_PROFILE_INTERVAL();
  344. int ret = 0;
  345. struct shim_ipc_sysv_msgmov * msgin =
  346. (struct shim_ipc_sysv_msgmov *) &msg->msg;
  347. debug("ipc callback from %u: IPC_SYSV_MSGMOV(%ld)\n", msg->src,
  348. msgin->msgid);
  349. struct shim_msg_handle * msgq = get_msg_handle_by_id(msgin->msgid);
  350. if (!msgq) {
  351. ret = -ENOENT;
  352. goto out;
  353. }
  354. struct shim_handle * hdl = container_of(msgq, struct shim_handle,
  355. info.msg);
  356. lock(hdl->lock);
  357. int nscores = (msgin->nscores > MAX_SYSV_CLIENTS) ?
  358. MAX_SYSV_CLIENTS : msgin->nscores;
  359. if (nscores)
  360. memcpy(msgq->scores, msgin->scores, nscores);
  361. if (nscores < MAX_SYSV_CLIENTS)
  362. memset(msgq->scores + nscores, 0,
  363. sizeof(struct sysv_score) * (MAX_SYSV_CLIENTS - nscores));
  364. unlock(hdl->lock);
  365. ret = recover_msg_ownership(msgq);
  366. struct shim_ipc_info * info;
  367. if (!create_ipc_location(&info)) {
  368. add_sysv_subrange(msgin->msgid, info->vmid, qstrgetstr(&info->uri),
  369. &msgin->lease);
  370. put_ipc_info(info);
  371. }
  372. put_msg_handle(msgq);
  373. out:
  374. SAVE_PROFILE_INTERVAL(ipc_sysv_msgmov_callback);
  375. return ret;
  376. }
  377. DEFINE_PROFILE_INTERVAL(ipc_sysv_semop_send, ipc);
  378. DEFINE_PROFILE_INTERVAL(ipc_sysv_semop_callback, ipc);
  379. int ipc_sysv_semop_send (IDTYPE semid, struct sembuf * sops, int nsops,
  380. unsigned long timeout, unsigned long * seq)
  381. {
  382. BEGIN_PROFILE_INTERVAL();
  383. IDTYPE owner;
  384. struct shim_ipc_port * port = NULL;
  385. int ret = 0;
  386. bool waitforreply = false;
  387. for (int i = 0 ; i < nsops ; i++)
  388. if (sops[i].sem_op <= 0) {
  389. waitforreply = true;
  390. break;
  391. }
  392. if ((ret = connect_owner(semid, &port, &owner)) < 0)
  393. goto out;
  394. if (owner == cur_process.vmid) {
  395. ret = -EAGAIN;
  396. goto out;
  397. }
  398. assert(port);
  399. if (!waitforreply) {
  400. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  401. IPC_SYSV_SEMOP,
  402. sizeof(struct shim_ipc_sysv_semop) +
  403. sizeof(struct sembuf) * nsops,
  404. owner);
  405. struct shim_ipc_sysv_semop * msgin =
  406. (struct shim_ipc_sysv_semop *) &msg->msg;
  407. msgin->semid = semid;
  408. msgin->timeout = timeout;
  409. msgin->nsops = nsops;
  410. memcpy(msgin->sops, sops, sizeof(struct sembuf) * nsops);
  411. msg->seq = *seq;
  412. debug("ipc send to %u: IPC_SYSV_SEMOP(%u, %ld, %u)\n", owner, semid,
  413. timeout, nsops);
  414. ret = send_ipc_message(msg, port);
  415. put_ipc_port(port);
  416. goto out;
  417. }
  418. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  419. IPC_SYSV_SEMOP,
  420. sizeof(struct shim_ipc_sysv_semop) +
  421. sizeof(struct sembuf) * nsops,
  422. owner);
  423. struct shim_ipc_sysv_semop * msgin =
  424. (struct shim_ipc_sysv_semop *) &msg->msg.msg;
  425. msgin->semid = semid;
  426. msgin->timeout = timeout;
  427. msgin->nsops = nsops;
  428. memcpy(msgin->sops, sops, sizeof(struct sembuf) * nsops);
  429. msg->msg.seq = *seq;
  430. debug("ipc send to %u: IPC_SYSV_SEMOP(%u, %ld, %u)\n", owner, semid,
  431. timeout, nsops);
  432. ret = do_ipc_duplex(msg, port, seq, NULL);
  433. put_ipc_port(port);
  434. out:
  435. SAVE_PROFILE_INTERVAL(ipc_sysv_semop_send);
  436. return ret;
  437. }
  438. int ipc_sysv_semop_callback (IPC_CALLBACK_ARGS)
  439. {
  440. BEGIN_PROFILE_INTERVAL();
  441. int ret = 0;
  442. struct shim_ipc_sysv_semop * msgin =
  443. (struct shim_ipc_sysv_semop *) &msg->msg;
  444. debug("ipc callback from %u: IPC_SYSV_SEMOP(%u, %ld, %u)\n", msg->src,
  445. msgin->semid, msgin->timeout, msgin->nsops);
  446. struct shim_sem_handle * sem = get_sem_handle_by_id(msgin->semid);
  447. if (!sem) {
  448. ret = -ENOENT;
  449. goto out;
  450. }
  451. struct sysv_client client;
  452. client.port = port;
  453. client.vmid = msg->src;
  454. client.seq = msg->seq;
  455. ret = submit_sysv_sem(sem, msgin->sops, msgin->nsops, msgin->timeout,
  456. &client);
  457. put_sem_handle(sem);
  458. out:
  459. SAVE_PROFILE_INTERVAL(ipc_sysv_semop_callback);
  460. return ret;
  461. }
  462. DEFINE_PROFILE_INTERVAL(ipc_sysv_semctl_send, ipc);
  463. DEFINE_PROFILE_INTERVAL(ipc_sysv_semctl_callback, ipc);
  464. int ipc_sysv_semctl_send (IDTYPE semid, int semnum, int cmd, void * vals,
  465. int valsize)
  466. {
  467. BEGIN_PROFILE_INTERVAL();
  468. IDTYPE owner;
  469. struct shim_ipc_port * port = NULL;
  470. int ret = 0;
  471. if ((ret = connect_owner(semid, &port, &owner)) < 0)
  472. goto out;
  473. int ctlvalsize = (cmd == SETALL || cmd == SETVAL) ? valsize : 0;
  474. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  475. IPC_SYSV_SEMCTL,
  476. sizeof(struct shim_ipc_sysv_semctl) +
  477. ctlvalsize,
  478. owner);
  479. struct shim_ipc_sysv_semctl * msgin =
  480. (struct shim_ipc_sysv_semctl *) &msg->msg.msg;
  481. msgin->semid = semid;
  482. msgin->semnum = semnum;
  483. msgin->cmd = cmd;
  484. msgin->valsize = ctlvalsize;
  485. if (ctlvalsize)
  486. memcpy(msgin->vals, vals, ctlvalsize);
  487. debug("ipc send to %u: IPC_SYSV_SEMCTL(%u, %d, %d)\n", owner, semid,
  488. semnum, cmd);
  489. ret = do_ipc_duplex(msg, port, NULL, vals);
  490. put_ipc_port(port);
  491. out:
  492. SAVE_PROFILE_INTERVAL(ipc_sysv_semctl_send);
  493. return ret;
  494. }
  495. int ipc_sysv_semctl_callback (IPC_CALLBACK_ARGS)
  496. {
  497. BEGIN_PROFILE_INTERVAL();
  498. int ret = 0;
  499. struct shim_ipc_sysv_semctl * msgin =
  500. (struct shim_ipc_sysv_semctl *) &msg->msg;
  501. debug("ipc callback from %u: IPC_SYSV_SEMCTL(%u, %d, %d)\n", msg->src,
  502. msgin->semid, msgin->semnum, msgin->cmd);
  503. struct shim_sem_handle * sem = get_sem_handle_by_id(msgin->semid);
  504. if (!sem) {
  505. ret = -ENOENT;
  506. goto out;
  507. }
  508. void * vals = NULL;
  509. int valsize;
  510. switch(msgin->cmd) {
  511. case GETALL: {
  512. unsigned short * allsems = __alloca(sizeof(unsigned short) *
  513. sem->nsems);
  514. for (int i = 0 ; i < sem->nsems ; i++)
  515. allsems[i] = sem->sems[i].val;
  516. vals = allsems;
  517. valsize = sizeof(unsigned short) * sem->nsems;
  518. goto semret;
  519. }
  520. case GETNCNT:
  521. vals = &sem->sems[msgin->semnum].ncnt;
  522. valsize = sizeof(unsigned short);
  523. goto semret;
  524. case GETPID:
  525. vals = &sem->sems[msgin->semnum].pid;
  526. valsize = sizeof(IDTYPE);
  527. goto semret;
  528. case GETVAL:
  529. vals = &sem->sems[msgin->semnum].val;
  530. valsize = sizeof(unsigned short);
  531. goto semret;
  532. case GETZCNT:
  533. vals = &sem->sems[msgin->semnum].zcnt;
  534. valsize = sizeof(unsigned short);
  535. break;
  536. case SETALL: {
  537. if (msgin->valsize != sizeof(unsigned short) * sem->nsems) {
  538. ret = -EINVAL;
  539. break;
  540. }
  541. unsigned short * vals = (void *) msgin->vals;
  542. for (int i = 0 ; i < sem->nsems ; i++)
  543. sem->sems[i].val = vals[i];
  544. ret = RESPONSE_CALLBACK;
  545. break;
  546. }
  547. case SETVAL: {
  548. ret = -EINVAL;
  549. if (msgin->valsize != sizeof(unsigned short))
  550. break;
  551. if (msgin->semnum >= sem->nsems)
  552. break;
  553. sem->sems[msgin->semnum].val = *((int *) msgin->vals);
  554. ret = RESPONSE_CALLBACK;
  555. break;
  556. }
  557. default:
  558. ret = -ENOSYS;
  559. break;
  560. }
  561. put_sem_handle(sem);
  562. goto out;
  563. semret:
  564. ret = ipc_sysv_semret_send(port, msg->src, vals, valsize,
  565. msg->seq);
  566. out:
  567. SAVE_PROFILE_INTERVAL(ipc_sysv_semctl_callback);
  568. return ret;
  569. }
  570. DEFINE_PROFILE_INTERVAL(ipc_sysv_semret_send, ipc);
  571. DEFINE_PROFILE_INTERVAL(ipc_sysv_semret_callback, ipc);
  572. int ipc_sysv_semret_send (struct shim_ipc_port * port, IDTYPE dest, void * vals,
  573. int valsize, unsigned long seq)
  574. {
  575. BEGIN_PROFILE_INTERVAL();
  576. int ret = 0;
  577. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  578. IPC_SYSV_SEMRET,
  579. sizeof(struct shim_ipc_sysv_semret) +
  580. valsize,
  581. dest);
  582. struct shim_ipc_sysv_semret * msgin =
  583. (struct shim_ipc_sysv_semret *) &msg->msg;
  584. msgin->valsize = valsize;
  585. memcpy(msgin->vals, vals, valsize);
  586. msg->seq = seq;
  587. debug("ipc send to %u: IPC_SYSV_SEMRET\n", dest);
  588. ret = send_ipc_message(msg, port);
  589. SAVE_PROFILE_INTERVAL(ipc_sysv_semret_send);
  590. return ret;
  591. }
  592. int ipc_sysv_semret_callback (IPC_CALLBACK_ARGS)
  593. {
  594. BEGIN_PROFILE_INTERVAL();
  595. struct shim_ipc_sysv_semret * semret =
  596. (struct shim_ipc_sysv_semret *) &msg->msg;
  597. debug("ipc callback from %u: IPC_SYSV_SEMRET\n", msg->src);
  598. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  599. if (obj) {
  600. struct shim_ipc_sysv_semctl * semctl =
  601. (struct shim_ipc_sysv_semctl *) &obj->msg.msg;
  602. void * vals = obj->private;
  603. if (vals) {
  604. switch(semctl->cmd) {
  605. case GETALL:
  606. case GETNCNT:
  607. case GETPID:
  608. case GETVAL:
  609. case GETZCNT: {
  610. int retvalsize = semret->valsize;
  611. if (retvalsize > semctl->valsize)
  612. retvalsize = semctl->valsize;
  613. memcpy(vals, semret->vals, retvalsize);
  614. break;
  615. }
  616. }
  617. }
  618. if (obj->thread)
  619. thread_wakeup(obj->thread);
  620. }
  621. SAVE_PROFILE_INTERVAL(ipc_sysv_semret_callback);
  622. return 0;
  623. }
  624. DEFINE_PROFILE_INTERVAL(ipc_sysv_semmov_send, ipc);
  625. DEFINE_PROFILE_INTERVAL(ipc_sysv_semmov_callback, ipc);
  626. int ipc_sysv_semmov_send (struct shim_ipc_port * port, IDTYPE dest,
  627. IDTYPE semid, LEASETYPE lease,
  628. struct sem_backup * sems, int nsems,
  629. struct sem_client_backup * srcs, int nsrcs,
  630. struct sysv_score * scores, int nscores)
  631. {
  632. BEGIN_PROFILE_INTERVAL();
  633. struct shim_ipc_msg * msg =
  634. create_ipc_msg_on_stack(IPC_SYSV_SEMMOV,
  635. sizeof(struct shim_ipc_sysv_semmov) +
  636. sizeof(struct sem_backup) * nsems +
  637. sizeof(struct sem_client_backup) * nsrcs +
  638. sizeof(struct sysv_score) * nscores,
  639. dest);
  640. struct shim_ipc_sysv_semmov * msgin =
  641. (struct shim_ipc_sysv_semmov *) &msg->msg;
  642. msgin->semid = semid;
  643. msgin->lease = lease;
  644. msgin->nsems = nsems;
  645. msgin->nsrcs = nsrcs;
  646. msgin->nscores = nscores;
  647. memcpy(&msgin->sems, sems, sizeof(struct sem_backup) * nsems);
  648. memcpy((void *) msgin->sems + sizeof(struct sem_backup) * nsems,
  649. srcs, sizeof(struct sem_client_backup) * nsrcs);
  650. memcpy((void *) msgin->sems + sizeof(struct sem_backup) * nsems +
  651. sizeof(struct sem_client_backup) * nsrcs,
  652. scores, sizeof(struct sysv_score) * nscores);
  653. debug("ipc send to %u: IPC_SYSV_SEMMOV(%ld)\n", semid);
  654. int ret = send_ipc_message(msg, port);
  655. SAVE_PROFILE_INTERVAL(ipc_sysv_semmov_send);
  656. return ret;
  657. }
  658. int ipc_sysv_semmov_callback (IPC_CALLBACK_ARGS)
  659. {
  660. BEGIN_PROFILE_INTERVAL();
  661. int ret = 0;
  662. struct shim_ipc_sysv_semmov * msgin =
  663. (struct shim_ipc_sysv_semmov *) &msg->msg;
  664. debug("ipc callback from %u: IPC_SYSV_SEMMOV(%ld)\n", msg->src,
  665. msgin->semid);
  666. struct sem_backup * sems = msgin->sems;
  667. struct sem_client_backup * clients =
  668. (struct sem_client_backup *) (sems + msgin->nsems);
  669. struct sysv_score * scores =
  670. (struct sysv_score *) (clients + msgin->nsrcs);
  671. struct shim_sem_handle * sem = get_sem_handle_by_id(msgin->semid);
  672. if (!sem) {
  673. ret = -ENOENT;
  674. goto out;
  675. }
  676. struct shim_handle * hdl = container_of(sem, struct shim_handle,
  677. info.sem);
  678. lock(hdl->lock);
  679. int nscores = (msgin->nscores > MAX_SYSV_CLIENTS) ?
  680. MAX_SYSV_CLIENTS : msgin->nscores;
  681. if (nscores)
  682. memcpy(sem->scores, scores, nscores);
  683. if (nscores < MAX_SYSV_CLIENTS)
  684. memset(sem->scores + nscores, 0,
  685. sizeof(struct sysv_score) * (MAX_SYSV_CLIENTS - nscores));
  686. unlock(hdl->lock);
  687. ret = recover_sem_ownership(sem, sems, msgin->nsems, clients,
  688. msgin->nsrcs);
  689. struct shim_ipc_info * info;
  690. if (!create_ipc_location(&info)) {
  691. add_sysv_subrange(msgin->semid, info->vmid, qstrgetstr(&info->uri),
  692. &msgin->lease);
  693. put_ipc_info(info);
  694. }
  695. put_sem_handle(sem);
  696. out:
  697. SAVE_PROFILE_INTERVAL(ipc_sysv_semmov_callback);
  698. return ret;
  699. }
  700. #ifdef USE_SHARED_SEMAPHORE
  701. DEFINE_PROFILE_INTERVAL(ipc_sysv_semquery_send, ipc);
  702. DEFINE_PROFILE_INTERVAL(ipc_sysv_semquery_callback, ipc);
  703. int ipc_sysv_semquery_send (IDTYPE semid, int * nsems,
  704. PAL_NUM ** host_sem_ids)
  705. {
  706. BEGIN_PROFILE_INTERVAL();
  707. int ret = 0;
  708. IDTYPE dest;
  709. struct shim_ipc_port * port = NULL;
  710. if ((ret = connect_owner(semid, &port, &dest)) < 0)
  711. goto out;
  712. if (dest == cur_process.vmid) {
  713. ret = -EAGAIN;
  714. goto out;
  715. }
  716. assert(port);
  717. struct shim_ipc_msg_obj * msg = create_ipc_msg_duplex_on_stack(
  718. IPC_SYSV_SEMQUERY,
  719. sizeof(struct shim_ipc_sysv_semquery),
  720. dest);
  721. struct shim_ipc_sysv_semquery * msgin =
  722. (struct shim_ipc_sysv_semquery *) &msg->msg.msg;
  723. msgin->semid = semid;
  724. debug("ipc send to %u: IPC_SYSV_SEMQUERY(%u)\n", dest, semid);
  725. ret = do_ipc_duplex(msg, port, NULL, host_sem_ids);
  726. put_ipc_port(port);
  727. if (ret >= 0) {
  728. *nsems = ret;
  729. ret = 0;
  730. }
  731. out:
  732. SAVE_PROFILE_INTERVAL(ipc_sysv_semquery_send);
  733. return ret;
  734. }
  735. int ipc_sysv_semquery_callback (IPC_CALLBACK_ARGS)
  736. {
  737. BEGIN_PROFILE_INTERVAL();
  738. int ret = 0;
  739. struct shim_ipc_sysv_semquery * msgin =
  740. (struct shim_ipc_sysv_semquery *) &msg->msg;
  741. debug("ipc callback from %u: IPC_SYSV_SEMQUERY(%u)\n", msg->src,
  742. msgin->semid);
  743. struct shim_sem_handle * sem = get_sem_handle_by_id(msgin->semid);
  744. if (!sem) {
  745. ret = -ENOENT;
  746. goto out;
  747. }
  748. ret = send_sem_host_ids(sem, port, msg->src, msg->seq);
  749. put_sem_handle(sem);
  750. out:
  751. SAVE_PROFILE_INTERVAL(ipc_sysv_semreply_callback);
  752. return ret;
  753. }
  754. DEFINE_PROFILE_INTERVAL(ipc_sysv_semreply_send, ipc);
  755. DEFINE_PROFILE_INTERVAL(ipc_sysv_semreply_callback, ipc);
  756. int ipc_sysv_semreply_send (struct shim_ipc_port * port, IDTYPE dest,
  757. IDTYPE semid, int nsems, PAL_NUM * host_sem_ids,
  758. unsigned long seq)
  759. {
  760. BEGIN_PROFILE_INTERVAL();
  761. int ret = 0;
  762. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  763. IPC_SYSV_SEMREPLY,
  764. sizeof(struct shim_ipc_sysv_semreply)
  765. + sizeof(PAL_NUM) * nsems,
  766. dest);
  767. struct shim_ipc_sysv_semreply * msgin =
  768. (struct shim_ipc_sysv_semreply *) &msg->msg;
  769. msgin->semid = semid;
  770. msgin->nsems = nsems;
  771. if (nsems)
  772. memcpy(msgin->host_sem_ids, host_sem_ids, sizeof(PAL_NUM) * nsems);
  773. msg->seq = seq;
  774. debug("ipc send to %u: IPC_SYSV_SEMREPLY(%u, %d)\n", dest, semid, nsems);
  775. ret = send_ipc_message(msg, port);
  776. SAVE_PROFILE_INTERVAL(ipc_sysv_semreply_send);
  777. return ret;
  778. }
  779. int ipc_sysv_semreply_callback (IPC_CALLBACK_ARGS)
  780. {
  781. BEGIN_PROFILE_INTERVAL();
  782. int ret = 0;
  783. struct shim_ipc_sysv_semreply * msgin =
  784. (struct shim_ipc_sysv_semreply *) &msg->msg;
  785. debug("ipc callback from %u: IPC_SYSV_SEMREPLY(%u, %d)\n", msg->src,
  786. msgin->semid, msgin->nsems);
  787. struct shim_ipc_msg_obj * obj = find_ipc_msg_duplex(port, msg->seq);
  788. if (!obj)
  789. goto out;
  790. PAL_NUM ** semids = obj->private;
  791. if (semids)
  792. *semids = remalloc(msgin->host_sem_ids, sizeof(PAL_NUM) * msgin->nsems);
  793. obj->retval = msgin->nsems;
  794. if (obj->thread)
  795. thread_wakeup(obj->thread);
  796. out:
  797. SAVE_PROFILE_INTERVAL(ipc_sysv_semreply_callback);
  798. return ret;
  799. }
  800. #endif /* USE_SHARED_SEMAPHORE */
  801. int __balance_sysv_score (struct sysv_balance_policy * policy,
  802. struct shim_handle * hdl,
  803. struct sysv_score * scores, int nscores,
  804. struct sysv_client * src, long score)
  805. {
  806. struct sysv_score * s = scores;
  807. struct sysv_score * last = scores + nscores;
  808. for ( ; s < last && !s->vmid ; s++);
  809. struct sysv_score * free = s > scores ? scores : NULL;
  810. struct sysv_score * highest = s < last ? s : NULL;
  811. struct sysv_score * lowest = highest;
  812. struct sysv_score * owner = NULL;
  813. struct sysv_score * chosen = NULL;
  814. for ( ; s < last ; s++) {
  815. if (!s->vmid) {
  816. if (!free)
  817. free = s;
  818. continue;
  819. }
  820. if (s->score >= highest->score)
  821. highest = s;
  822. if (s->score < lowest->score)
  823. lowest = s;
  824. if (src) {
  825. if (s->vmid == cur_process.vmid)
  826. owner = s;
  827. if (s->vmid == src->vmid) {
  828. chosen = s;
  829. continue;
  830. }
  831. } else {
  832. if (s->vmid == cur_process.vmid) {
  833. owner = chosen = s;
  834. continue;
  835. }
  836. }
  837. s->score = (s->score >= policy->score_decay) ?
  838. s->score - policy->score_decay : 0;
  839. debug("balance: %u => %d\n", s->vmid, s->score);
  840. }
  841. if (!chosen) {
  842. chosen = free ? : lowest;
  843. chosen->vmid = src ? src->vmid : cur_process.vmid;
  844. chosen->score = 0;
  845. }
  846. chosen->score += score;
  847. if (chosen->score > policy->score_max)
  848. chosen->score = policy->score_max;
  849. debug("balance: %u => %d\n", chosen->vmid, chosen->score);
  850. if (!src || chosen != highest ||
  851. chosen->score < (owner ? owner->score : 0) + policy->balance_threshold)
  852. return 0;
  853. return policy->migrate(hdl, src);
  854. }