shim_ipc_child.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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_helper.c
  17. *
  18. * This file contains functions and callbacks to handle IPC between parent
  19. * processes and their children.
  20. */
  21. #include <shim_internal.h>
  22. #include <shim_thread.h>
  23. #include <shim_handle.h>
  24. #include <shim_ipc.h>
  25. #include <shim_utils.h>
  26. #include <shim_profile.h>
  27. #include <pal.h>
  28. #include <pal_error.h>
  29. #include <errno.h>
  30. static int ipc_thread_exit (IDTYPE vmid, IDTYPE tid, unsigned int exitcode)
  31. {
  32. assert(vmid != cur_process.vmid);
  33. struct shim_thread * thread = __lookup_thread(tid);
  34. if (thread) {
  35. int ret = 0;
  36. //assert(thread->vmid == vmid && !thread->in_vm);
  37. thread->exit_code = -exitcode;
  38. ret = thread_exit(thread, false);
  39. put_thread(thread);
  40. return ret;
  41. }
  42. struct shim_simple_thread * sthread = __lookup_simple_thread(tid);
  43. if (!sthread) {
  44. sthread = get_new_simple_thread();
  45. sthread->vmid = vmid;
  46. sthread->tid = tid;
  47. add_simple_thread(sthread);
  48. }
  49. sthread->is_alive = 0;
  50. sthread->exit_code = -exitcode;
  51. DkEventSet(sthread->exit_event);
  52. put_simple_thread(sthread);
  53. return 0;
  54. }
  55. void ipc_parent_exit (struct shim_ipc_port * port, IDTYPE vmid,
  56. unsigned int exitcode)
  57. {
  58. debug("ipc port %p of process %u closed suggests parent exiting\n",
  59. port, vmid);
  60. struct shim_ipc_info * parent = NULL;
  61. lock(cur_process.lock);
  62. if (parent && vmid == cur_process.parent->vmid) {
  63. parent = cur_process.parent;
  64. cur_process.parent = NULL;
  65. }
  66. unlock(cur_process.lock);
  67. if (parent)
  68. put_ipc_info(parent);
  69. }
  70. int remove_child_thread (IDTYPE vmid, unsigned int exitcode)
  71. {
  72. assert(vmid != cur_process.vmid);
  73. struct thread_info {
  74. IDTYPE vmid;
  75. unsigned int exitcode;
  76. };
  77. int child_sthread_exit (struct shim_simple_thread * thread, void * arg,
  78. bool * unlocked)
  79. {
  80. struct thread_info * info = (struct thread_info *) arg;
  81. if (thread->vmid == info->vmid) {
  82. if (thread->is_alive) {
  83. thread->exit_code = -info->exitcode;
  84. thread->is_alive = false;
  85. DkEventSet(thread->exit_event);
  86. }
  87. return 1;
  88. }
  89. return 0;
  90. }
  91. int child_thread_exit (struct shim_thread * thread, void * arg,
  92. bool * unlocked)
  93. {
  94. struct thread_info * info = (struct thread_info *) arg;
  95. if (thread->vmid == info->vmid) {
  96. if (thread->is_alive) {
  97. thread->exit_code = -info->exitcode;
  98. thread_exit(thread, false);
  99. }
  100. return 1;
  101. }
  102. return 0;
  103. }
  104. struct thread_info info = { .vmid = vmid, .exitcode = exitcode };
  105. int nkilled = 0, ret;
  106. if ((ret = walk_thread_list(&child_thread_exit, &info, false)) > 0)
  107. nkilled += ret;
  108. if ((ret = walk_simple_thread_list(&child_sthread_exit, &info, false)) > 0)
  109. nkilled += ret;
  110. if (!nkilled)
  111. debug("child port closed, no thread exited\n");
  112. return 0;
  113. }
  114. void ipc_child_exit (struct shim_ipc_port * port, IDTYPE vmid,
  115. unsigned int exitcode)
  116. {
  117. debug("ipc port %p of process %u closed suggests child exiting\n",
  118. port, vmid);
  119. remove_child_thread(vmid, 0);
  120. }
  121. static struct shim_ipc_port * get_parent_port (IDTYPE * dest)
  122. {
  123. struct shim_ipc_port * port = NULL;
  124. lock(cur_process.lock);
  125. if (cur_process.parent && (port = cur_process.parent->port)) {
  126. get_ipc_port(port);
  127. *dest = cur_process.parent->vmid;
  128. }
  129. unlock(cur_process.lock);
  130. return port;
  131. }
  132. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_send, ipc);
  133. DEFINE_PROFILE_INTERVAL(ipc_cld_exit_callback, ipc);
  134. int ipc_cld_exit_send (IDTYPE tid, unsigned int exitcode)
  135. {
  136. BEGIN_PROFILE_INTERVAL();
  137. int ret = 0;
  138. struct shim_ipc_msg * msg =
  139. create_ipc_msg_on_stack(IPC_CLD_EXIT,
  140. sizeof(struct shim_ipc_cld_exit), 0);
  141. struct shim_ipc_cld_exit * msgin =
  142. (struct shim_ipc_cld_exit *) &msg->msg;
  143. msgin->tid = tid;
  144. msgin->exitcode = exitcode;
  145. debug("ipc broadcast: IPC_CLD_EXIT(%u, %d)\n", tid, exitcode);
  146. ret = broadcast_ipc(msg, NULL, 0, IPC_PORT_DIRPRT|IPC_PORT_DIRCLD);
  147. SAVE_PROFILE_INTERVAL(ipc_cld_exit_send);
  148. return ret;
  149. }
  150. int ipc_cld_exit_callback (IPC_CALLBACK_ARGS)
  151. {
  152. BEGIN_PROFILE_INTERVAL();
  153. struct shim_ipc_cld_exit * msgin =
  154. (struct shim_ipc_cld_exit *) &msg->msg;
  155. debug("ipc callback from %u: IPC_CLD_EXIT(%u, %d)\n",
  156. msg->src, msgin->tid, msgin->exitcode);
  157. int ret = ipc_thread_exit(msg->src, msgin->tid, msgin->exitcode);
  158. SAVE_PROFILE_INTERVAL(ipc_cld_exit_callback);
  159. return ret;
  160. }
  161. DEFINE_PROFILE_INTERVAL(ipc_cld_join_send, ipc);
  162. DEFINE_PROFILE_INTERVAL(ipc_cld_join_callback, ipc);
  163. int ipc_cld_join_send (IDTYPE dest)
  164. {
  165. BEGIN_PROFILE_INTERVAL();
  166. struct shim_ipc_port * port = dest ?
  167. lookup_ipc_port(dest, IPC_PORT_DIRPRT) :
  168. get_parent_port(&dest);
  169. if (!port)
  170. return -ESRCH;
  171. struct shim_ipc_msg * msg =
  172. create_ipc_msg_on_stack(IPC_CLD_JOIN, 0, dest);
  173. debug("ipc send to %u: IPC_CLD_JOIN\n", dest);
  174. int ret = send_ipc_message(msg, port);
  175. add_ipc_port(port, dest, IPC_PORT_DIRPRT, NULL);
  176. put_ipc_port(port);
  177. SAVE_PROFILE_INTERVAL(ipc_cld_join_send);
  178. return ret;
  179. }
  180. int ipc_cld_join_callback (IPC_CALLBACK_ARGS)
  181. {
  182. BEGIN_PROFILE_INTERVAL();
  183. debug("ipc callback from %u: IPC_CLD_JOIN\n", msg->src);
  184. add_ipc_port(port, msg->src, IPC_PORT_DIRCLD, NULL);
  185. SAVE_PROFILE_INTERVAL(ipc_cld_join_callback);
  186. return 0;
  187. }
  188. DEFINE_PROFILE_INTERVAL(ipc_send_profile, ipc);
  189. #ifdef PROFILE
  190. int ipc_cld_profile_send (void)
  191. {
  192. IDTYPE dest;
  193. struct shim_ipc_port * port = get_parent_port(&dest);
  194. if (!port)
  195. return -ESRCH;
  196. unsigned long time = GET_PROFILE_INTERVAL();
  197. int nsending = 0;
  198. for (int i = 0 ; i < N_PROFILE ; i++)
  199. switch (PROFILES[i].type) {
  200. case OCCURENCE:
  201. if (atomic_read(&PROFILES[i].val.occurence.count))
  202. nsending++;
  203. break;
  204. case INTERVAL:
  205. if (atomic_read(&PROFILES[i].val.interval.count))
  206. nsending++;
  207. break;
  208. case CATAGORY:
  209. break;
  210. }
  211. struct shim_ipc_msg * msg = create_ipc_msg_on_stack(
  212. IPC_CLD_PROFILE,
  213. sizeof(struct shim_ipc_cld_profile) +
  214. sizeof(struct profile_val) *
  215. nsending, dest);
  216. struct shim_ipc_cld_profile * msgin =
  217. (struct shim_ipc_cld_profile *) &msg->msg;
  218. int nsent = 0;
  219. for (int i = 0 ; i < N_PROFILE && nsent < nsending ; i++)
  220. switch (PROFILES[i].type) {
  221. case OCCURENCE: {
  222. unsigned long count =
  223. atomic_read(&PROFILES[i].val.occurence.count);
  224. if (count) {
  225. msgin->profile[nsent].idx = i + 1;
  226. msgin->profile[nsent].val.occurence.count = count;
  227. debug("send %s: %lu times\n", PROFILES[i].name, count);
  228. nsent++;
  229. }
  230. break;
  231. }
  232. case INTERVAL: {
  233. unsigned long count =
  234. atomic_read(&PROFILES[i].val.interval.count);
  235. if (count) {
  236. msgin->profile[nsent].idx = i + 1;
  237. msgin->profile[nsent].val.interval.count = count;
  238. msgin->profile[nsent].val.interval.time =
  239. atomic_read(&PROFILES[i].val.interval.time);
  240. debug("send %s: %lu times, %lu msec\n", PROFILES[i].name,
  241. count, msgin->profile[nsent].val.interval.time);
  242. nsent++;
  243. }
  244. break;
  245. }
  246. case CATAGORY:
  247. break;
  248. }
  249. msgin->time = time;
  250. msgin->nprofile = nsent;
  251. debug("ipc send to %u: IPC_CLD_PROFILE\n", dest);
  252. int ret = send_ipc_message(msg, port);
  253. put_ipc_port(port);
  254. return ret;
  255. }
  256. int ipc_cld_profile_callback (IPC_CALLBACK_ARGS)
  257. {
  258. struct shim_ipc_cld_profile * msgin =
  259. (struct shim_ipc_cld_profile *) &msg->msg;
  260. debug("ipc callback from %u: IPC_CLD_PROFILE\n", msg->src);
  261. for (int i = 0 ; i < msgin->nprofile ; i++) {
  262. int idx = msgin->profile[i].idx;
  263. if (idx == 0)
  264. break;
  265. idx--;
  266. switch (PROFILES[idx].type) {
  267. case OCCURENCE:
  268. debug("receive %s: %u times\n", PROFILES[idx].name,
  269. msgin->profile[i].val.occurence.count);
  270. atomic_add(msgin->profile[i].val.occurence.count,
  271. &PROFILES[idx].val.occurence.count);
  272. break;
  273. case INTERVAL:
  274. debug("receive %s: %u times, %lu msec\n", PROFILES[idx].name,
  275. msgin->profile[i].val.interval.count,
  276. msgin->profile[i].val.interval.time);
  277. atomic_add(msgin->profile[i].val.interval.count,
  278. &PROFILES[idx].val.interval.count);
  279. atomic_add(msgin->profile[i].val.interval.time,
  280. &PROFILES[idx].val.interval.time);
  281. break;
  282. case CATAGORY:
  283. break;
  284. }
  285. }
  286. SAVE_PROFILE_INTERVAL_SINCE(ipc_send_profile, msgin->time);
  287. return 0;
  288. }
  289. #endif