mpqemu-link.c 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263
  1. /*
  2. * Communication channel between QEMU and remote device process
  3. *
  4. * Copyright © 2018, 2021 Oracle and/or its affiliates.
  5. *
  6. * This work is licensed under the terms of the GNU GPL, version 2 or later.
  7. * See the COPYING file in the top-level directory.
  8. *
  9. */
  10. #include "qemu/osdep.h"
  11. #include "qemu/module.h"
  12. #include "hw/remote/mpqemu-link.h"
  13. #include "qapi/error.h"
  14. #include "qemu/iov.h"
  15. #include "qemu/error-report.h"
  16. #include "qemu/main-loop.h"
  17. #include "io/channel.h"
  18. #include "system/iothread.h"
  19. #include "trace.h"
  20. /*
  21. * Send message over the ioc QIOChannel.
  22. * This function is safe to call from:
  23. * - main loop in co-routine context. Will block the main loop if not in
  24. * co-routine context;
  25. * - vCPU thread with no co-routine context and if the channel is not part
  26. * of the main loop handling;
  27. * - IOThread within co-routine context, outside of co-routine context
  28. * will block IOThread;
  29. * Returns true if no errors were encountered, false otherwise.
  30. */
  31. bool mpqemu_msg_send(MPQemuMsg *msg, QIOChannel *ioc, Error **errp)
  32. {
  33. bool drop_bql = bql_locked();
  34. bool iothread = qemu_in_iothread();
  35. struct iovec send[2] = {};
  36. int *fds = NULL;
  37. size_t nfds = 0;
  38. bool ret = false;
  39. send[0].iov_base = msg;
  40. send[0].iov_len = MPQEMU_MSG_HDR_SIZE;
  41. send[1].iov_base = (void *)&msg->data;
  42. send[1].iov_len = msg->size;
  43. if (msg->num_fds) {
  44. nfds = msg->num_fds;
  45. fds = msg->fds;
  46. }
  47. /*
  48. * Dont use in IOThread out of co-routine context as
  49. * it will block IOThread.
  50. */
  51. assert(qemu_in_coroutine() || !iothread);
  52. /*
  53. * Skip unlocking/locking BQL when the IOThread is running
  54. * in co-routine context. Co-routine context is asserted above
  55. * for IOThread case.
  56. * Also skip lock handling while in a co-routine in the main context.
  57. */
  58. if (drop_bql && !iothread && !qemu_in_coroutine()) {
  59. bql_unlock();
  60. }
  61. if (!qio_channel_writev_full_all(ioc, send, G_N_ELEMENTS(send),
  62. fds, nfds, 0, errp)) {
  63. ret = true;
  64. } else {
  65. trace_mpqemu_send_io_error(msg->cmd, msg->size, nfds);
  66. }
  67. if (drop_bql && !iothread && !qemu_in_coroutine()) {
  68. /* See above comment why skip locking here. */
  69. bql_lock();
  70. }
  71. return ret;
  72. }
  73. /*
  74. * Read message from the ioc QIOChannel.
  75. * This function is safe to call from:
  76. * - From main loop in co-routine context. Will block the main loop if not in
  77. * co-routine context;
  78. * - From vCPU thread with no co-routine context and if the channel is not part
  79. * of the main loop handling;
  80. * - From IOThread within co-routine context, outside of co-routine context
  81. * will block IOThread;
  82. */
  83. static ssize_t mpqemu_read(QIOChannel *ioc, void *buf, size_t len, int **fds,
  84. size_t *nfds, Error **errp)
  85. {
  86. struct iovec iov = { .iov_base = buf, .iov_len = len };
  87. bool drop_bql = bql_locked();
  88. bool iothread = qemu_in_iothread();
  89. int ret = -1;
  90. /*
  91. * Dont use in IOThread out of co-routine context as
  92. * it will block IOThread.
  93. */
  94. assert(qemu_in_coroutine() || !iothread);
  95. if (drop_bql && !iothread && !qemu_in_coroutine()) {
  96. bql_unlock();
  97. }
  98. ret = qio_channel_readv_full_all_eof(ioc, &iov, 1, fds, nfds, errp);
  99. if (drop_bql && !iothread && !qemu_in_coroutine()) {
  100. bql_lock();
  101. }
  102. return (ret <= 0) ? ret : iov.iov_len;
  103. }
  104. bool mpqemu_msg_recv(MPQemuMsg *msg, QIOChannel *ioc, Error **errp)
  105. {
  106. ERRP_GUARD();
  107. g_autofree int *fds = NULL;
  108. size_t nfds = 0;
  109. ssize_t len;
  110. bool ret = false;
  111. len = mpqemu_read(ioc, msg, MPQEMU_MSG_HDR_SIZE, &fds, &nfds, errp);
  112. if (len <= 0) {
  113. goto fail;
  114. } else if (len != MPQEMU_MSG_HDR_SIZE) {
  115. error_setg(errp, "Message header corrupted");
  116. goto fail;
  117. }
  118. if (msg->size > sizeof(msg->data)) {
  119. error_setg(errp, "Invalid size for message");
  120. goto fail;
  121. }
  122. if (!msg->size) {
  123. goto copy_fds;
  124. }
  125. len = mpqemu_read(ioc, &msg->data, msg->size, NULL, NULL, errp);
  126. if (len <= 0) {
  127. goto fail;
  128. }
  129. if (len != msg->size) {
  130. error_setg(errp, "Unable to read full message");
  131. goto fail;
  132. }
  133. copy_fds:
  134. msg->num_fds = nfds;
  135. if (nfds > G_N_ELEMENTS(msg->fds)) {
  136. error_setg(errp,
  137. "Overflow error: received %zu fds, more than max of %d fds",
  138. nfds, REMOTE_MAX_FDS);
  139. goto fail;
  140. }
  141. if (nfds) {
  142. memcpy(msg->fds, fds, nfds * sizeof(int));
  143. }
  144. ret = true;
  145. fail:
  146. if (*errp) {
  147. trace_mpqemu_recv_io_error(msg->cmd, msg->size, nfds);
  148. }
  149. while (*errp && nfds) {
  150. close(fds[nfds - 1]);
  151. nfds--;
  152. }
  153. return ret;
  154. }
  155. /*
  156. * Send msg and wait for a reply with command code RET_MSG.
  157. * Returns the message received of size u64 or UINT64_MAX
  158. * on error.
  159. * Called from VCPU thread in non-coroutine context.
  160. * Used by the Proxy object to communicate to remote processes.
  161. */
  162. uint64_t mpqemu_msg_send_and_await_reply(MPQemuMsg *msg, PCIProxyDev *pdev,
  163. Error **errp)
  164. {
  165. MPQemuMsg msg_reply = {0};
  166. uint64_t ret = UINT64_MAX;
  167. assert(!qemu_in_coroutine());
  168. QEMU_LOCK_GUARD(&pdev->io_mutex);
  169. if (!mpqemu_msg_send(msg, pdev->ioc, errp)) {
  170. return ret;
  171. }
  172. if (!mpqemu_msg_recv(&msg_reply, pdev->ioc, errp)) {
  173. return ret;
  174. }
  175. if (!mpqemu_msg_valid(&msg_reply) || msg_reply.cmd != MPQEMU_CMD_RET) {
  176. error_setg(errp, "ERROR: Invalid reply received for command %d",
  177. msg->cmd);
  178. return ret;
  179. }
  180. return msg_reply.data.u64;
  181. }
  182. bool mpqemu_msg_valid(MPQemuMsg *msg)
  183. {
  184. if (msg->cmd >= MPQEMU_CMD_MAX || msg->cmd < 0) {
  185. return false;
  186. }
  187. /* Verify FDs. */
  188. if (msg->num_fds >= REMOTE_MAX_FDS) {
  189. return false;
  190. }
  191. if (msg->num_fds > 0) {
  192. for (int i = 0; i < msg->num_fds; i++) {
  193. if (fcntl(msg->fds[i], F_GETFL) == -1) {
  194. return false;
  195. }
  196. }
  197. }
  198. /* Verify message specific fields. */
  199. switch (msg->cmd) {
  200. case MPQEMU_CMD_SYNC_SYSMEM:
  201. if (msg->num_fds == 0 || msg->size != sizeof(SyncSysmemMsg)) {
  202. return false;
  203. }
  204. break;
  205. case MPQEMU_CMD_PCI_CFGWRITE:
  206. case MPQEMU_CMD_PCI_CFGREAD:
  207. if (msg->size != sizeof(PciConfDataMsg)) {
  208. return false;
  209. }
  210. break;
  211. case MPQEMU_CMD_BAR_WRITE:
  212. case MPQEMU_CMD_BAR_READ:
  213. if ((msg->size != sizeof(BarAccessMsg)) || (msg->num_fds != 0)) {
  214. return false;
  215. }
  216. break;
  217. case MPQEMU_CMD_SET_IRQFD:
  218. if (msg->size || (msg->num_fds != 2)) {
  219. return false;
  220. }
  221. break;
  222. default:
  223. break;
  224. }
  225. return true;
  226. }