message.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. /*
  2. * Copyright © 2020, 2021 Oracle and/or its affiliates.
  3. *
  4. * This work is licensed under the terms of the GNU GPL-v2, version 2 or later.
  5. *
  6. * See the COPYING file in the top-level directory.
  7. *
  8. */
  9. #include "qemu/osdep.h"
  10. #include "qemu-common.h"
  11. #include "hw/remote/machine.h"
  12. #include "io/channel.h"
  13. #include "hw/remote/mpqemu-link.h"
  14. #include "qapi/error.h"
  15. #include "sysemu/runstate.h"
  16. #include "hw/pci/pci.h"
  17. static void process_config_write(QIOChannel *ioc, PCIDevice *dev,
  18. MPQemuMsg *msg, Error **errp);
  19. static void process_config_read(QIOChannel *ioc, PCIDevice *dev,
  20. MPQemuMsg *msg, Error **errp);
  21. void coroutine_fn mpqemu_remote_msg_loop_co(void *data)
  22. {
  23. g_autofree RemoteCommDev *com = (RemoteCommDev *)data;
  24. PCIDevice *pci_dev = NULL;
  25. Error *local_err = NULL;
  26. assert(com->ioc);
  27. pci_dev = com->dev;
  28. for (; !local_err;) {
  29. MPQemuMsg msg = {0};
  30. if (!mpqemu_msg_recv(&msg, com->ioc, &local_err)) {
  31. break;
  32. }
  33. if (!mpqemu_msg_valid(&msg)) {
  34. error_setg(&local_err, "Received invalid message from proxy"
  35. "in remote process pid="FMT_pid"",
  36. getpid());
  37. break;
  38. }
  39. switch (msg.cmd) {
  40. case MPQEMU_CMD_PCI_CFGWRITE:
  41. process_config_write(com->ioc, pci_dev, &msg, &local_err);
  42. break;
  43. case MPQEMU_CMD_PCI_CFGREAD:
  44. process_config_read(com->ioc, pci_dev, &msg, &local_err);
  45. break;
  46. default:
  47. error_setg(&local_err,
  48. "Unknown command (%d) received for device %s"
  49. " (pid="FMT_pid")",
  50. msg.cmd, DEVICE(pci_dev)->id, getpid());
  51. }
  52. }
  53. if (local_err) {
  54. error_report_err(local_err);
  55. qemu_system_shutdown_request(SHUTDOWN_CAUSE_HOST_ERROR);
  56. } else {
  57. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  58. }
  59. }
  60. static void process_config_write(QIOChannel *ioc, PCIDevice *dev,
  61. MPQemuMsg *msg, Error **errp)
  62. {
  63. ERRP_GUARD();
  64. PciConfDataMsg *conf = (PciConfDataMsg *)&msg->data.pci_conf_data;
  65. MPQemuMsg ret = { 0 };
  66. if ((conf->addr + sizeof(conf->val)) > pci_config_size(dev)) {
  67. error_setg(errp, "Bad address for PCI config write, pid "FMT_pid".",
  68. getpid());
  69. ret.data.u64 = UINT64_MAX;
  70. } else {
  71. pci_default_write_config(dev, conf->addr, conf->val, conf->len);
  72. }
  73. ret.cmd = MPQEMU_CMD_RET;
  74. ret.size = sizeof(ret.data.u64);
  75. if (!mpqemu_msg_send(&ret, ioc, NULL)) {
  76. error_prepend(errp, "Error returning code to proxy, pid "FMT_pid": ",
  77. getpid());
  78. }
  79. }
  80. static void process_config_read(QIOChannel *ioc, PCIDevice *dev,
  81. MPQemuMsg *msg, Error **errp)
  82. {
  83. ERRP_GUARD();
  84. PciConfDataMsg *conf = (PciConfDataMsg *)&msg->data.pci_conf_data;
  85. MPQemuMsg ret = { 0 };
  86. if ((conf->addr + sizeof(conf->val)) > pci_config_size(dev)) {
  87. error_setg(errp, "Bad address for PCI config read, pid "FMT_pid".",
  88. getpid());
  89. ret.data.u64 = UINT64_MAX;
  90. } else {
  91. ret.data.u64 = pci_default_read_config(dev, conf->addr, conf->len);
  92. }
  93. ret.cmd = MPQEMU_CMD_RET;
  94. ret.size = sizeof(ret.data.u64);
  95. if (!mpqemu_msg_send(&ret, ioc, NULL)) {
  96. error_prepend(errp, "Error returning code to proxy, pid "FMT_pid": ",
  97. getpid());
  98. }
  99. }