Skip to contentSkip to conversation

Add support for Xen access to vmport

PATCH Superseded Don Slutz 3 participants 8 messages Oct 17, 2014

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore

Don Slutz

author
#0 lore raw

v4 cover Add support for Xen access to vmport

Changes RFC-v2x to v4:
Stefano Stabellini
Please try to get rid of the #ifdefs.
Moved 2 #ifdefs into hw/xen/xen_common.h

Changes v2 to RFC-v2x:
Paul Durrant
Use a 2nd shared page.
Added HVM_PARAM_VMPORT_IOREQ_PFN usage.

Changes v1 to v2:
More info in commit message.

Stefano Stabellini
the registers being passes explicitely by Xen rather than "hiding" them into other ioreq fields.
Added vmware_ioreq_t
Paolo Bonzini & Alexander Graf
Fixup env access
Added cpu_by_ioreq_id.
Set current_cpu in regs_to_cpu(), clear in regs_from_cpu(). Drop all changes to vmport.c

Note: to use this with Xen either a version of:

[Qemu-devel] [PATCH] -machine vmport=off: Allow disabling of VMWare ioport emulation

or

Quoted 1 line

From f70663d9fb86914144ba340b6186cb1e67ac6eec Mon Sep 17 00:00:00 2001

From: Don Slutz <[email protected]>
Date: Fri, 26 Sep 2014 08:11:39 -0400
Subject: [PATCH 1/2] hack: force enable vmport

---
 hw/i386/pc_piix.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
hw/i386/pc_piix.c +1 -1
index 103d756..b76dfbc 100644
@@ -234,7 +234,7 @@ static void pc_init1(MachineState *machine,
     pc_vga_init(isa_bus, pci_enabled ? pci_bus : NULL);
 
     /* init basic PC hardware */
-    pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, xen_enabled(),
+    pc_basic_device_init(isa_bus, gsi, &rtc_state, &floppy, false,
         0x4);
 
     pc_nic_init(isa_bus, pci_bus);
Signature · 19 lines
1.8.4

needs to be done to QEMU.

And the Xen RFC patch:

[RFC][PATCH v2 1/1] Add IOREQ_TYPE_VMWARE_PORT

needs to be done to Xen.

Don Slutz (1):
  xen-hvm.c: Add support for Xen access to vmport

 include/hw/xen/xen_common.h |  15 ++++++
 xen-hvm.c                   | 121 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 131 insertions(+), 5 deletions(-)

-- 
1.8.4
author
#1 lore raw

PATCH v4 1/1 xen-hvm.c: Add support for Xen access to vmport

This adds synchronisation of the 6 vcpu registers (only 32bits of them) that vmport.c needs between Xen and QEMU.

This is to avoid a 2nd and 3rd exchange between QEMU and Xen to fetch and put these 6 vcpu registers used by the code in vmport.c and vmmouse.c

The registers are passed in the new shared page provided by HVM_PARAM_VMPORT_IOREQ_PFN.

Add new array to XenIOState that allows selection of current_cpu by ioreq_id.

Now pass XenIOState to handle_ioreq().

Add new routines regs_to_cpu(), regs_from_cpu(), and handle_vmport_ioreq().

---
 include/hw/xen/xen_common.h |  15 ++++++
 xen-hvm.c                   | 121 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 131 insertions(+), 5 deletions(-)
include/hw/xen/xen_common.h +15 -0
index 07731b9..9542756 100644
@@ -164,4 +164,19 @@ void destroy_hvm_domain(bool reboot);
 /* shutdown/destroy current domain because of an error */
 void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
+#ifdef HVM_PARAM_VMPORT_IOREQ_PFN
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return xc_get_hvm_param(xc, dom, HVM_PARAM_VMPORT_IOREQ_PFN,
+                            vmport_ioreq_pfn);
+}
+#else
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return -ENOSYS;
+}
+#endif
+
 #endif /* QEMU_HW_XEN_COMMON_H */
xen-hvm.c +116 -5

194 lines not on this page · raw message

Signature · 1 line
1.8.4
#2 lore raw

Re: [Qemu-devel] [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

Quoted 76 lines

-----Original Message-----
From: Don Slutz [mailto:[email protected]]
Sent: 17 October 2014 13:14
To: [email protected]; Paul Durrant
Cc: [email protected]; Alexander Graf; Andreas Färber; Anthony Liguori; Don Slutz; Marcel Apfelbaum; Markus Armbruster; Michael S. Tsirkin; Stefano Stabellini
Subject: [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

This adds synchronisation of the 6 vcpu registers (only 32bits of them) that vmport.c needs between Xen and QEMU.

This is to avoid a 2nd and 3rd exchange between QEMU and Xen to fetch and put these 6 vcpu registers used by the code in vmport.c and vmmouse.c

The registers are passed in the new shared page provided by HVM_PARAM_VMPORT_IOREQ_PFN.

Add new array to XenIOState that allows selection of current_cpu by ioreq_id.

Now pass XenIOState to handle_ioreq().

Add new routines regs_to_cpu(), regs_from_cpu(), and handle_vmport_ioreq().

---
 include/hw/xen/xen_common.h |  15 ++++++
 xen-hvm.c                   | 121
++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 131 insertions(+), 5 deletions(-)
include/hw/xen/xen_common.h +15 -0
index 07731b9..9542756 100644
@@ -164,4 +164,19 @@ void destroy_hvm_domain(bool reboot);
 /* shutdown/destroy current domain because of an error */
 void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
+#ifdef HVM_PARAM_VMPORT_IOREQ_PFN
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return xc_get_hvm_param(xc, dom,
 HVM_PARAM_VMPORT_IOREQ_PFN,
+                            vmport_ioreq_pfn);
+}
+#else
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return -ENOSYS;
+}
+#endif
+

#endif /* QEMU_HW_XEN_COMMON_H */

xen-hvm.c +3 -0
index 05e522c..17b3cbd 100644
@@ -41,6 +41,29 @@ static MemoryRegion *framebuffer;
 static bool xen_in_migration;
 
 /* Compatibility with older version */
+
+/* This allows QEMU to build on a system that has Xen 4.5 or earlier
+ * installed.  This here (not in hw/xen/xen_common.h) because

xen/hvm/ioreq.h
+ * needs to be included before this block and hw/xen/xen_common.h needs to
+ * be included before xen/hvm/ioreq.h
+ */

xen-hvm.c static MemoryRegion *framebuffer; · line 59

Quoted 10 lines
xen-hvm.c +10 -0
+#ifndef IOREQ_TYPE_VMWARE_PORT
+#define IOREQ_TYPE_VMWARE_PORT  3
+struct vmware_ioreq {
+    uint32_t esi;
+    uint32_t edi;
+    uint32_t ebx;
+    uint32_t ecx;
+    uint32_t edx;
+};
+typedef struct vmware_ioreq vmware_ioreq_t;

This struct is not really a request any more. Maybe vmware_regs_t?

Quoted 11 lines
xen-hvm.c +7 -0
+
+struct shared_vmport_iopage {
+    struct vmware_ioreq vcpu_vmport_ioreq[1];
+};
+typedef struct shared_vmport_iopage shared_vmport_iopage_t;
+#endif
+
 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
 {

xen-hvm.c typedef struct XenPhysmap { · line 108

Quoted 7 lines
xen-hvm.c +2 -0
@@ -79,8 +102,10 @@ typedef struct XenPhysmap {
 
 typedef struct XenIOState {
     shared_iopage_t *shared_page;
+    shared_vmport_iopage_t *shared_vmport_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
+    CPUState **cpu_by_ioreq_id;

This name implies the array is indexed by an id carries in the ioreq. That doesn't seem to be the case; it's just indexed by guest vcpu id isn't it?

Quoted 13 lines
xen-hvm.c +2 -0
     /* the evtchn port for polling the notification, */
     evtchn_port_t *ioreq_local_port;
     /* evtchn local port for buffered io */
@@ -101,6 +126,8 @@ typedef struct XenIOState {
     Notifier wakeup;
 } XenIOState;
 
+static void handle_ioreq(XenIOState *state, ioreq_t *req);
+
 /* Xen specific function for piix pci */
 
 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)

xen-hvm.c static ioreq_t · line 642

Quoted 6 lines
xen-hvm.c +2 -0
@@ -610,6 +637,20 @@ static ioreq_t
 *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
     return req;
 }
 
+/* get the vmport ioreq packets from share mem */
+static vmware_ioreq_t *cpu_get_vmport_ioreq_from_shared_memory(

Is this worth its own function? I can only see one use of it in handle_vmport_ioreq below...

xen-hvm.c static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu) · line 649

Quoted 9 lines
xen-hvm.c +8 -0
+    XenIOState *state, int vcpu)
+{
+    vmware_ioreq_t *vmport_req;
+
+    assert(state->shared_vmport_page);
+    vmport_req = &state->shared_vmport_page-
 >vcpu_vmport_ioreq[vcpu];
+
+    xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */

I don't think that comment is applicable here, and I don't think you need the barrier anyway.

Quoted 27 lines
xen-hvm.c +4 -0
+
+    return vmport_req;
+}
+
 /* use poll to get the port notification */

/* ioreq_vec--out,the */
/* retval--the number of ioreq packet */

xen-hvm.c +15 -1
@@ -773,7 +814,50 @@ static void cpu_ioreq_move(ioreq_t *req)
     }
 }
 
-static void handle_ioreq(ioreq_t *req)
+static void regs_to_cpu(XenIOState *state, vmware_ioreq_t *vmport_req,
+                        ioreq_t *req)
+{
+    X86CPU *cpu;
+    CPUX86State *env;
+
+    current_cpu = state->cpu_by_ioreq_id[state->send_vcpu];
+    cpu = X86_CPU(current_cpu);
+    env = &cpu->env;
+    env->regs[R_EAX] = req->data;
+    env->regs[R_EBX] = vmport_req->ebx;
+    env->regs[R_ECX] = vmport_req->ecx;
+    env->regs[R_EDX] = vmport_req->edx;
+    env->regs[R_ESI] = vmport_req->esi;
+    env->regs[R_EDI] = vmport_req->edi;

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 841

Quoted 10 lines
xen-hvm.c +9 -0
+}
+
+static void regs_from_cpu(XenIOState *state, vmware_ioreq_t
 *vmport_req,
+                          ioreq_t *req)
+{
+    X86CPU *cpu = X86_CPU(current_cpu);
+    CPUX86State *env = &cpu->env;
+
+    assert(sizeof(*vmport_req) <= sizeof(*req));

Why?

Quoted 8 lines
xen-hvm.c +8 -0
+
+    vmport_req->ebx = env->regs[R_EBX];
+    vmport_req->ecx = env->regs[R_ECX];
+    vmport_req->edx = env->regs[R_EDX];
+    vmport_req->esi = env->regs[R_ESI];
+    vmport_req->edi = env->regs[R_EDI];
+    current_cpu = NULL;
+}

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 857

Quoted 10 lines
xen-hvm.c +9 -0
+
+static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
+{
+    vmware_ioreq_t *vmport_req =
+        cpu_get_vmport_ioreq_from_shared_memory(state, state-
 >send_vcpu);
+
+    regs_to_cpu(state, vmport_req, req);
+    cpu_ioreq_pio(req);
+    regs_from_cpu(state, vmport_req, req);

state appears to be unused in this function, and you could avoid passing it to both of them by setting current_cpu here couldn't you?

Paul

Quoted 97 lines
xen-hvm.c +3 -0
+}
+
+static void handle_ioreq(XenIOState *state, ioreq_t *req)
 {
     if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
             (req->size < sizeof (target_ulong))) {
xen-hvm.c +4 -1
@@ -787,6 +871,9 @@ static void handle_ioreq(ioreq_t *req)
         case IOREQ_TYPE_COPY:
             cpu_ioreq_move(req);
             break;
+        case IOREQ_TYPE_VMWARE_PORT:
+            handle_vmport_ioreq(state, req);
+            break;
         case IOREQ_TYPE_TIMEOFFSET:
             break;
         case IOREQ_TYPE_INVALIDATE:
@@ -828,7 +915,7 @@ static int handle_buffered_iopage(XenIOState
 *state)
             req.data |= ((uint64_t)buf_req->data) << 32;
         }
 
-        handle_ioreq(&req);
+        handle_ioreq(state, &req);
 
         xen_mb();
         state->buffered_io_page->read_pointer += qw ? 2 : 1;
xen-hvm.c +5 -3
@@ -857,14 +944,16 @@ static void cpu_handle_ioreq(void *opaque)
 
     handle_buffered_iopage(state);
     if (req) {
-        handle_ioreq(req);
+        handle_ioreq(state, req);
 
         if (req->state != STATE_IOREQ_INPROCESS) {
             fprintf(stderr, "Badness in I/O request ... not in service?!: "
                     "%x, ptr: %x, port: %"PRIx64", "
-                    "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %"
 FMT_ioreq_size "\n",
+                    "data: %"PRIx64", count: %" FMT_ioreq_size
+                    ", size: %" FMT_ioreq_size
+                    ", type: %"FMT_ioreq_size"\n",
                     req->state, req->data_is_ptr, req->addr,
-                    req->data, req->count, req->size);
+                    req->data, req->count, req->size, req->type);
             destroy_hvm_domain(false);
             return;
         }
xen-hvm.c +8 -0
@@ -904,6 +993,14 @@ static void xen_main_loop_prepare(XenIOState
 *state)
                                                  state);
 
     if (evtchn_fd != -1) {
+        CPUState *cpu_state;
+
+        DPRINTF("%s: Init cpu_by_ioreq_id\n", __func__);
+        CPU_FOREACH(cpu_state) {
+            DPRINTF("%s: cpu_by_ioreq_id[%d]=%p\n",
+                    __func__, cpu_state->cpu_index, cpu_state);
+            state->cpu_by_ioreq_id[cpu_state->cpu_index] = cpu_state;
+        }
         qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
     }

}

xen-hvm.c +11 -0
@@ -1020,6 +1117,17 @@ int xen_hvm_init(ram_addr_t
 *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
                  errno, xen_xc);
     }
 
+    if (!xen_get_vmport_ioreq_pfn(xen_xc, xen_domid, &ioreq_pfn)) {
+        DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
+        state->shared_vmport_page =
+            xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
+                                 PROT_READ|PROT_WRITE, ioreq_pfn);
+        if (state->shared_vmport_page == NULL) {
+            hw_error("map shared vmport IO page returned error %d handle="
+                     XC_INTERFACE_FMT, errno, xen_xc);
+        }
+    }
+
     xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN,
 &ioreq_pfn);
     DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
     state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid,

XC_PAGE_SIZE,

xen-hvm.c +3 -0
@@ -1028,6 +1136,9 @@ int xen_hvm_init(ram_addr_t
 *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
         hw_error("map buffered IO page returned error %d", errno);
     }
 
+    /* Note: cpus is empty at this point in init */
+    state->cpu_by_ioreq_id = g_malloc0(max_cpus * sizeof(CPUState *));
+
     state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
 
     /* FIXME: how about if we overflow the page here? */

--
1.8.4

author
#3 lore raw

Re: [Qemu-devel] [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

Quoted 79 lines · Paul Durrant
Quoted 79 lines

-----Original Message-----
From: Don Slutz [mailto:[email protected]]
Sent: 17 October 2014 13:14
To: [email protected]; Paul Durrant
Cc: [email protected]; Alexander Graf; Andreas Färber; Anthony Liguori; Don Slutz; Marcel Apfelbaum; Markus Armbruster; Michael S. Tsirkin; Stefano Stabellini
Subject: [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

This adds synchronisation of the 6 vcpu registers (only 32bits of them) that vmport.c needs between Xen and QEMU.

This is to avoid a 2nd and 3rd exchange between QEMU and Xen to fetch and put these 6 vcpu registers used by the code in vmport.c and vmmouse.c

The registers are passed in the new shared page provided by HVM_PARAM_VMPORT_IOREQ_PFN.

Add new array to XenIOState that allows selection of current_cpu by ioreq_id.

Now pass XenIOState to handle_ioreq().

Add new routines regs_to_cpu(), regs_from_cpu(), and handle_vmport_ioreq().

---
  include/hw/xen/xen_common.h |  15 ++++++
  xen-hvm.c                   | 121
++++++++++++++++++++++++++++++++++++++++++--
  2 files changed, 131 insertions(+), 5 deletions(-)
include/hw/xen/xen_common.h +15 -0
index 07731b9..9542756 100644
@@ -164,4 +164,19 @@ void destroy_hvm_domain(bool reboot);
  /* shutdown/destroy current domain because of an error */
  void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
+#ifdef HVM_PARAM_VMPORT_IOREQ_PFN
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return xc_get_hvm_param(xc, dom,
 HVM_PARAM_VMPORT_IOREQ_PFN,
+                            vmport_ioreq_pfn);
+}
+#else
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return -ENOSYS;
+}
+#endif
+
  #endif /* QEMU_HW_XEN_COMMON_H */
xen-hvm.c +3 -0
index 05e522c..17b3cbd 100644
@@ -41,6 +41,29 @@ static MemoryRegion *framebuffer;
  static bool xen_in_migration;
 
  /* Compatibility with older version */
+
+/* This allows QEMU to build on a system that has Xen 4.5 or earlier
+ * installed.  This here (not in hw/xen/xen_common.h) because

xen/hvm/ioreq.h
+ * needs to be included before this block and hw/xen/xen_common.h needs to
+ * be included before xen/hvm/ioreq.h
+ */

xen-hvm.c +3 -0
+#ifndef IOREQ_TYPE_VMWARE_PORT
+#define IOREQ_TYPE_VMWARE_PORT  3
+struct vmware_ioreq {
Quoted 8 lines

xen-hvm.c static MemoryRegion *framebuffer; · line 59

Quoted 7 lines
xen-hvm.c +7 -0
+    uint32_t esi;
+    uint32_t edi;
+    uint32_t ebx;
+    uint32_t ecx;
+    uint32_t edx;
+};
+typedef struct vmware_ioreq vmware_ioreq_t;

This struct is not really a request any more. Maybe vmware_regs_t?

Sure.

Quoted 12 lines
Quoted 12 lines
xen-hvm.c +7 -0
+
+struct shared_vmport_iopage {
+    struct vmware_ioreq vcpu_vmport_ioreq[1];
+};
+typedef struct shared_vmport_iopage shared_vmport_iopage_t;
+#endif
+
  #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
  static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
  {
@@ -79,8 +102,10 @@ typedef struct XenPhysmap {
 
Quoted 7 lines

xen-hvm.c typedef struct XenPhysmap { · line 108

Quoted 6 lines
xen-hvm.c +2 -0
  typedef struct XenIOState {
      shared_iopage_t *shared_page;
+    shared_vmport_iopage_t *shared_vmport_page;
      buffered_iopage_t *buffered_io_page;
      QEMUTimer *buffered_io_timer;
+    CPUState **cpu_by_ioreq_id;

This name implies the array is indexed by an id carries in the ioreq. That doesn't seem to be the case; it's just indexed by guest vcpu id isn't it?

Yes. How about cpu_by_vcpu_id ?

Quoted 17 lines
Quoted 17 lines
xen-hvm.c +2 -0
      /* the evtchn port for polling the notification, */
      evtchn_port_t *ioreq_local_port;
      /* evtchn local port for buffered io */
@@ -101,6 +126,8 @@ typedef struct XenIOState {
      Notifier wakeup;
  } XenIOState;
 
+static void handle_ioreq(XenIOState *state, ioreq_t *req);
+
  /* Xen specific function for piix pci */
 
  int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
@@ -610,6 +637,20 @@ static ioreq_t
 *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
      return req;
  }
 
Quoted 3 lines

xen-hvm.c static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu) · line 641

Quoted 2 lines
xen-hvm.c +2 -0
+/* get the vmport ioreq packets from share mem */
+static vmware_ioreq_t *cpu_get_vmport_ioreq_from_shared_memory(

Is this worth its own function? I can only see one use of it in handle_vmport_ioreq below...

Will move it in-line. I was just following cpu_get_ioreq_from_shared_memory which also has 1 use.

Quoted 10 lines

xen-hvm.c static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu) · line 649

Quoted 9 lines
xen-hvm.c +8 -0
+    XenIOState *state, int vcpu)
+{
+    vmware_ioreq_t *vmport_req;
+
+    assert(state->shared_vmport_page);
+    vmport_req = &state->shared_vmport_page-
 > vcpu_vmport_ioreq[vcpu];
+
+    xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */

I don't think that comment is applicable here, and I don't think you need the barrier anyway.

I was not sure either way, so went with the safe way. Will drop.

Quoted 30 lines
Quoted 30 lines
xen-hvm.c +4 -0
+
+    return vmport_req;
+}
+
  /* use poll to get the port notification */

/* ioreq_vec--out,the */
/* retval--the number of ioreq packet */

xen-hvm.c +18 -1
@@ -773,7 +814,50 @@ static void cpu_ioreq_move(ioreq_t *req)
      }
  }
 
-static void handle_ioreq(ioreq_t *req)
+static void regs_to_cpu(XenIOState *state, vmware_ioreq_t *vmport_req,
+                        ioreq_t *req)
+{
+    X86CPU *cpu;
+    CPUX86State *env;
+
+    current_cpu = state->cpu_by_ioreq_id[state->send_vcpu];
+    cpu = X86_CPU(current_cpu);
+    env = &cpu->env;
+    env->regs[R_EAX] = req->data;
+    env->regs[R_EBX] = vmport_req->ebx;
+    env->regs[R_ECX] = vmport_req->ecx;
+    env->regs[R_EDX] = vmport_req->edx;
+    env->regs[R_ESI] = vmport_req->esi;
+    env->regs[R_EDI] = vmport_req->edi;
+}
+
+static void regs_from_cpu(XenIOState *state, vmware_ioreq_t
Quoted 8 lines

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 840

Quoted 7 lines
xen-hvm.c +6 -0
 *vmport_req,
+                          ioreq_t *req)
+{
+    X86CPU *cpu = X86_CPU(current_cpu);
+    CPUX86State *env = &cpu->env;
+
+    assert(sizeof(*vmport_req) <= sizeof(*req));

Why?

Since there is 1 page of ioreq_t indexed by vcpu id this checks that the 1 page of vmport data has at least the same number of vcpu slots. This prevents a bad memory read. Looks to not be the right place for this, will move.

Quoted 13 lines
Quoted 13 lines
xen-hvm.c +13 -0
+
+    vmport_req->ebx = env->regs[R_EBX];
+    vmport_req->ecx = env->regs[R_ECX];
+    vmport_req->edx = env->regs[R_EDX];
+    vmport_req->esi = env->regs[R_ESI];
+    vmport_req->edi = env->regs[R_EDI];
+    current_cpu = NULL;
+}
+
+static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
+{
+    vmware_ioreq_t *vmport_req =
+        cpu_get_vmport_ioreq_from_shared_memory(state, state-
Quoted 6 lines

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 857

Quoted 5 lines
xen-hvm.c +4 -0
 > send_vcpu);
+
+    regs_to_cpu(state, vmport_req, req);
+    cpu_ioreq_pio(req);
+    regs_from_cpu(state, vmport_req, req);

state appears to be unused in this function, and you could avoid passing it to both of them by setting current_cpu here couldn't you?

Yes, will move state code here.

-Don Slutz

Quoted 99 lines

Paul

Quoted 97 lines
xen-hvm.c +3 -0
+}
+
+static void handle_ioreq(XenIOState *state, ioreq_t *req)
  {
      if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
              (req->size < sizeof (target_ulong))) {
xen-hvm.c +4 -1
@@ -787,6 +871,9 @@ static void handle_ioreq(ioreq_t *req)
          case IOREQ_TYPE_COPY:
              cpu_ioreq_move(req);
              break;
+        case IOREQ_TYPE_VMWARE_PORT:
+            handle_vmport_ioreq(state, req);
+            break;
          case IOREQ_TYPE_TIMEOFFSET:
              break;
          case IOREQ_TYPE_INVALIDATE:
@@ -828,7 +915,7 @@ static int handle_buffered_iopage(XenIOState
 *state)
              req.data |= ((uint64_t)buf_req->data) << 32;
          }
 
-        handle_ioreq(&req);
+        handle_ioreq(state, &req);
 
          xen_mb();
          state->buffered_io_page->read_pointer += qw ? 2 : 1;
xen-hvm.c +5 -3
@@ -857,14 +944,16 @@ static void cpu_handle_ioreq(void *opaque)
 
      handle_buffered_iopage(state);
      if (req) {
-        handle_ioreq(req);
+        handle_ioreq(state, req);
 
          if (req->state != STATE_IOREQ_INPROCESS) {
              fprintf(stderr, "Badness in I/O request ... not in service?!: "
                      "%x, ptr: %x, port: %"PRIx64", "
-                    "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %"
 FMT_ioreq_size "\n",
+                    "data: %"PRIx64", count: %" FMT_ioreq_size
+                    ", size: %" FMT_ioreq_size
+                    ", type: %"FMT_ioreq_size"\n",
                      req->state, req->data_is_ptr, req->addr,
-                    req->data, req->count, req->size);
+                    req->data, req->count, req->size, req->type);
              destroy_hvm_domain(false);
              return;
          }
xen-hvm.c +8 -0
@@ -904,6 +993,14 @@ static void xen_main_loop_prepare(XenIOState
 *state)
                                                   state);
 
      if (evtchn_fd != -1) {
+        CPUState *cpu_state;
+
+        DPRINTF("%s: Init cpu_by_ioreq_id\n", __func__);
+        CPU_FOREACH(cpu_state) {
+            DPRINTF("%s: cpu_by_ioreq_id[%d]=%p\n",
+                    __func__, cpu_state->cpu_index, cpu_state);
+            state->cpu_by_ioreq_id[cpu_state->cpu_index] = cpu_state;
+        }
          qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
      }
  }
xen-hvm.c +11 -0
@@ -1020,6 +1117,17 @@ int xen_hvm_init(ram_addr_t
 *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
                   errno, xen_xc);
      }
 
+    if (!xen_get_vmport_ioreq_pfn(xen_xc, xen_domid, &ioreq_pfn)) {
+        DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
+        state->shared_vmport_page =
+            xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
+                                 PROT_READ|PROT_WRITE, ioreq_pfn);
+        if (state->shared_vmport_page == NULL) {
+            hw_error("map shared vmport IO page returned error %d handle="
+                     XC_INTERFACE_FMT, errno, xen_xc);
+        }
+    }
+
      xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN,
 &ioreq_pfn);
      DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
      state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid,

XC_PAGE_SIZE,

xen-hvm.c +3 -0
@@ -1028,6 +1136,9 @@ int xen_hvm_init(ram_addr_t
 *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
          hw_error("map buffered IO page returned error %d", errno);
      }
 
+    /* Note: cpus is empty at this point in init */
+    state->cpu_by_ioreq_id = g_malloc0(max_cpus * sizeof(CPUState *));
+
      state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
 
      /* FIXME: how about if we overflow the page here? */

--
1.8.4

#4 lore raw

Re: [Qemu-devel] [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

Quoted 123 lines

-----Original Message-----
From: Don Slutz [mailto:[email protected]]
Sent: 17 October 2014 14:31
To: Paul Durrant; Don Slutz; [email protected]
Cc: [email protected]; Alexander Graf; Andreas Färber; Anthony Liguori; Marcel Apfelbaum; Markus Armbruster; Michael S. Tsirkin; Stefano Stabellini
Subject: Re: [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

Quoted 6 lines · Paul Durrant
Quoted 6 lines

-----Original Message-----
From: Don Slutz [mailto:[email protected]]
Sent: 17 October 2014 13:14
To: [email protected]; Paul Durrant
Cc: [email protected]; Alexander Graf; Andreas Färber; Anthony Liguori; Don Slutz; Marcel Apfelbaum; Markus Armbruster;

Michael

Quoted 28 lines
Quoted 28 lines

S. Tsirkin; Stefano Stabellini
Subject: [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

This adds synchronisation of the 6 vcpu registers (only 32bits of them) that vmport.c needs between Xen and QEMU.

This is to avoid a 2nd and 3rd exchange between QEMU and Xen to fetch and put these 6 vcpu registers used by the code in vmport.c and vmmouse.c

The registers are passed in the new shared page provided by HVM_PARAM_VMPORT_IOREQ_PFN.

Add new array to XenIOState that allows selection of current_cpu by ioreq_id.

Now pass XenIOState to handle_ioreq().

Add new routines regs_to_cpu(), regs_from_cpu(), and handle_vmport_ioreq().

---
  include/hw/xen/xen_common.h |  15 ++++++
  xen-hvm.c                   | 121
++++++++++++++++++++++++++++++++++++++++++--
  2 files changed, 131 insertions(+), 5 deletions(-)
Quoted 8 lines

include/hw/xen/xen_common.h void destroy_hvm_domain(bool reboot); · line 165

Quoted 8 lines
include/hw/xen/xen_common.h +0 -0
index 07731b9..9542756 100644
@@ -164,4 +164,19 @@ void destroy_hvm_domain(bool reboot);
  /* shutdown/destroy current domain because of an error */
  void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1,

2);

Quoted 37 lines
Quoted 37 lines
include/hw/xen/xen_common.h +15 -0
+#ifdef HVM_PARAM_VMPORT_IOREQ_PFN
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return xc_get_hvm_param(xc, dom,
 HVM_PARAM_VMPORT_IOREQ_PFN,
+                            vmport_ioreq_pfn);
+}
+#else
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return -ENOSYS;
+}
+#endif
+
  #endif /* QEMU_HW_XEN_COMMON_H */
xen-hvm.c +3 -0
index 05e522c..17b3cbd 100644
@@ -41,6 +41,29 @@ static MemoryRegion *framebuffer;
  static bool xen_in_migration;
 
  /* Compatibility with older version */
+
+/* This allows QEMU to build on a system that has Xen 4.5 or earlier
+ * installed.  This here (not in hw/xen/xen_common.h) because

xen/hvm/ioreq.h
+ * needs to be included before this block and hw/xen/xen_common.h needs to
+ * be included before xen/hvm/ioreq.h
+ */

xen-hvm.c +3 -0
+#ifndef IOREQ_TYPE_VMWARE_PORT
+#define IOREQ_TYPE_VMWARE_PORT  3
+struct vmware_ioreq {
Quoted 8 lines

xen-hvm.c static MemoryRegion *framebuffer; · line 59

Quoted 7 lines
xen-hvm.c +7 -0
+    uint32_t esi;
+    uint32_t edi;
+    uint32_t ebx;
+    uint32_t ecx;
+    uint32_t edx;
+};
+typedef struct vmware_ioreq vmware_ioreq_t;

This struct is not really a request any more. Maybe vmware_regs_t?

Sure.

Quoted 9 lines

xen-hvm.c static MemoryRegion *framebuffer;

Quoted 9 lines
xen-hvm.c +7 -0
+
+struct shared_vmport_iopage {
+    struct vmware_ioreq vcpu_vmport_ioreq[1];
+};
+typedef struct shared_vmport_iopage shared_vmport_iopage_t;
+#endif
+
  #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
  static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page,

int i)

Quoted 10 lines

xen-hvm.c typedef struct XenPhysmap { · line 108

Quoted 9 lines
  {
xen-hvm.c +2 -0
@@ -79,8 +102,10 @@ typedef struct XenPhysmap {
 
  typedef struct XenIOState {
      shared_iopage_t *shared_page;
+    shared_vmport_iopage_t *shared_vmport_page;
      buffered_iopage_t *buffered_io_page;
      QEMUTimer *buffered_io_timer;
+    CPUState **cpu_by_ioreq_id;

This name implies the array is indexed by an id carries in the ioreq. That

Quoted 3 lines

doesn't seem to be the case; it's just indexed by guest vcpu id isn't it?

Yes. How about cpu_by_vcpu_id ?

Ok.

Quoted 81 lines
Quoted 12 lines
Quoted 12 lines
xen-hvm.c +2 -0
      /* the evtchn port for polling the notification, */
      evtchn_port_t *ioreq_local_port;
      /* evtchn local port for buffered io */
@@ -101,6 +126,8 @@ typedef struct XenIOState {
      Notifier wakeup;
  } XenIOState;
 
+static void handle_ioreq(XenIOState *state, ioreq_t *req);
+
  /* Xen specific function for piix pci */
 
Quoted 8 lines

xen-hvm.c static ioreq_t · line 642

Quoted 8 lines
xen-hvm.c +2 -0
  int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
@@ -610,6 +637,20 @@ static ioreq_t
 *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
      return req;
  }
 
+/* get the vmport ioreq packets from share mem */
+static vmware_ioreq_t

*cpu_get_vmport_ioreq_from_shared_memory(

Quoted 1 line

Is this worth its own function? I can only see one use of it in

handle_vmport_ioreq below...

Will move it in-line. I was just following cpu_get_ioreq_from_shared_memory which also has 1 use.

Quoted 10 lines

xen-hvm.c static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu) · line 649

Quoted 9 lines
xen-hvm.c +8 -0
+    XenIOState *state, int vcpu)
+{
+    vmware_ioreq_t *vmport_req;
+
+    assert(state->shared_vmport_page);
+    vmport_req = &state->shared_vmport_page-
 > vcpu_vmport_ioreq[vcpu];
+
+    xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */

I don't think that comment is applicable here, and I don't think you need

the barrier anyway.

I was not sure either way, so went with the safe way. Will drop.

Quoted 11 lines
Quoted 11 lines
xen-hvm.c +4 -0
+
+    return vmport_req;
+}
+
  /* use poll to get the port notification */

/* ioreq_vec--out,the */
/* retval--the number of ioreq packet */

xen-hvm.c +0 -0
@@ -773,7 +814,50 @@ static void cpu_ioreq_move(ioreq_t *req)
      }
  }
 
Quoted 2 lines

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 817

Quoted 2 lines
xen-hvm.c +1 -1
-static void handle_ioreq(ioreq_t *req)
+static void regs_to_cpu(XenIOState *state, vmware_ioreq_t

*vmport_req,

Quoted 17 lines
Quoted 17 lines
xen-hvm.c +17 -0
+                        ioreq_t *req)
+{
+    X86CPU *cpu;
+    CPUX86State *env;
+
+    current_cpu = state->cpu_by_ioreq_id[state->send_vcpu];
+    cpu = X86_CPU(current_cpu);
+    env = &cpu->env;
+    env->regs[R_EAX] = req->data;
+    env->regs[R_EBX] = vmport_req->ebx;
+    env->regs[R_ECX] = vmport_req->ecx;
+    env->regs[R_EDX] = vmport_req->edx;
+    env->regs[R_ESI] = vmport_req->esi;
+    env->regs[R_EDI] = vmport_req->edi;
+}
+
+static void regs_from_cpu(XenIOState *state, vmware_ioreq_t
Quoted 8 lines

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 840

Quoted 7 lines
xen-hvm.c +6 -0
 *vmport_req,
+                          ioreq_t *req)
+{
+    X86CPU *cpu = X86_CPU(current_cpu);
+    CPUX86State *env = &cpu->env;
+
+    assert(sizeof(*vmport_req) <= sizeof(*req));

Why?

Quoted 3 lines

Since there is 1 page of ioreq_t indexed by vcpu id this checks that the 1 page of vmport data has at least the same number of vcpu slots.

Does it? It looks like it checks that the size of the vmware_ioreq_t structure is less than or equal to the size of the ioreq_t structure to me. I see nothing checking number of array elements.

Paul

Quoted 131 lines

This prevents a bad memory read. Looks to not be the right place
for this, will move.

Quoted 13 lines
Quoted 13 lines
xen-hvm.c +13 -0
+
+    vmport_req->ebx = env->regs[R_EBX];
+    vmport_req->ecx = env->regs[R_ECX];
+    vmport_req->edx = env->regs[R_EDX];
+    vmport_req->esi = env->regs[R_ESI];
+    vmport_req->edi = env->regs[R_EDI];
+    current_cpu = NULL;
+}
+
+static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
+{
+    vmware_ioreq_t *vmport_req =
+        cpu_get_vmport_ioreq_from_shared_memory(state, state-
Quoted 6 lines

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 857

Quoted 5 lines
xen-hvm.c +4 -0
 > send_vcpu);
+
+    regs_to_cpu(state, vmport_req, req);
+    cpu_ioreq_pio(req);
+    regs_from_cpu(state, vmport_req, req);

state appears to be unused in this function, and you could avoid passing it

to both of them by setting current_cpu here couldn't you?

Yes, will move state code here.

-Don Slutz

Quoted 70 lines

Paul

Quoted 68 lines
xen-hvm.c +3 -0
+}
+
+static void handle_ioreq(XenIOState *state, ioreq_t *req)
  {
      if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
              (req->size < sizeof (target_ulong))) {
xen-hvm.c +4 -1
@@ -787,6 +871,9 @@ static void handle_ioreq(ioreq_t *req)
          case IOREQ_TYPE_COPY:
              cpu_ioreq_move(req);
              break;
+        case IOREQ_TYPE_VMWARE_PORT:
+            handle_vmport_ioreq(state, req);
+            break;
          case IOREQ_TYPE_TIMEOFFSET:
              break;
          case IOREQ_TYPE_INVALIDATE:
@@ -828,7 +915,7 @@ static int handle_buffered_iopage(XenIOState
 *state)
              req.data |= ((uint64_t)buf_req->data) << 32;
          }
 
-        handle_ioreq(&req);
+        handle_ioreq(state, &req);
 
          xen_mb();
          state->buffered_io_page->read_pointer += qw ? 2 : 1;
xen-hvm.c +5 -3
@@ -857,14 +944,16 @@ static void cpu_handle_ioreq(void *opaque)
 
      handle_buffered_iopage(state);
      if (req) {
-        handle_ioreq(req);
+        handle_ioreq(state, req);
 
          if (req->state != STATE_IOREQ_INPROCESS) {
              fprintf(stderr, "Badness in I/O request ... not in service?!: "
                      "%x, ptr: %x, port: %"PRIx64", "
-                    "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %"
 FMT_ioreq_size "\n",
+                    "data: %"PRIx64", count: %" FMT_ioreq_size
+                    ", size: %" FMT_ioreq_size
+                    ", type: %"FMT_ioreq_size"\n",
                      req->state, req->data_is_ptr, req->addr,
-                    req->data, req->count, req->size);
+                    req->data, req->count, req->size, req->type);
              destroy_hvm_domain(false);
              return;
          }
xen-hvm.c +8 -0
@@ -904,6 +993,14 @@ static void xen_main_loop_prepare(XenIOState
 *state)
                                                   state);
 
      if (evtchn_fd != -1) {
+        CPUState *cpu_state;
+
+        DPRINTF("%s: Init cpu_by_ioreq_id\n", __func__);
+        CPU_FOREACH(cpu_state) {
+            DPRINTF("%s: cpu_by_ioreq_id[%d]=%p\n",
+                    __func__, cpu_state->cpu_index, cpu_state);
+            state->cpu_by_ioreq_id[cpu_state->cpu_index] = cpu_state;
+        }
          qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
      }
  }
xen-hvm.c +0 -0
@@ -1020,6 +1117,17 @@ int xen_hvm_init(ram_addr_t
 *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
                   errno, xen_xc);
      }
 
Quoted 7 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 7 lines
xen-hvm.c +7 -0
+    if (!xen_get_vmport_ioreq_pfn(xen_xc, xen_domid, &ioreq_pfn)) {
+        DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
+        state->shared_vmport_page =
+            xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
+                                 PROT_READ|PROT_WRITE, ioreq_pfn);
+        if (state->shared_vmport_page == NULL) {
+            hw_error("map shared vmport IO page returned error %d

handle="

Quoted 5 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
xen-hvm.c +4 -0
+                     XC_INTERFACE_FMT, errno, xen_xc);
+        }
+    }
+
      xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_BUFIOREQ_PFN,

Quoted 3 lines
Quoted 3 lines
xen-hvm.c +0 -0
 &ioreq_pfn);
      DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
      state->buffered_io_page = xc_map_foreign_range(xen_xc,

xen_domid,

Quoted 10 lines

xen-hvm.c int xen_hvm_init(ram_addr_t · line 1143

Quoted 10 lines

XC_PAGE_SIZE,

xen-hvm.c +3 -0
@@ -1028,6 +1136,9 @@ int xen_hvm_init(ram_addr_t
 *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
          hw_error("map buffered IO page returned error %d", errno);
      }
 
+    /* Note: cpus is empty at this point in init */
+    state->cpu_by_ioreq_id = g_malloc0(max_cpus * sizeof(CPUState *));
+
      state->ioreq_local_port = g_malloc0(max_cpus * sizeof

(evtchn_port_t));

Quoted 4 lines
Quoted 4 lines
xen-hvm.c +0 -1
      /* FIXME: how about if we overflow the page here? */
--

1.8.4

#5 lore raw

Re: [Qemu-devel] [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

Quoted 224 lines

-----Original Message-----
From: [email protected] [mailto:[email protected]] On Behalf Of Paul Durrant
Sent: 17 October 2014 14:36
To: Don Slutz; [email protected]
Cc: [email protected]; Marcel Apfelbaum; Michael S. Tsirkin; Markus Armbruster; Alexander Graf; Stefano Stabellini; Anthony Liguori; Andreas Färber
Subject: Re: [Qemu-devel] [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

Quoted 10 lines

-----Original Message-----
From: Don Slutz [mailto:[email protected]]
Sent: 17 October 2014 14:31
To: Paul Durrant; Don Slutz; [email protected]
Cc: [email protected]; Alexander Graf; Andreas Färber; Anthony Liguori; Marcel Apfelbaum; Markus Armbruster; Michael S. Tsirkin; Stefano Stabellini
Subject: Re: [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

Quoted 10 lines
Quoted 6 lines · Paul Durrant
Quoted 6 lines

-----Original Message-----
From: Don Slutz [mailto:[email protected]]
Sent: 17 October 2014 13:14
To: [email protected]; Paul Durrant
Cc: [email protected]; Alexander Graf; Andreas Färber; Anthony Liguori; Don Slutz; Marcel Apfelbaum; Markus Armbruster;

Michael

Quoted 2 lines
Quoted 2 lines

S. Tsirkin; Stefano Stabellini
Subject: [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to

vmport

Quoted 26 lines
Quoted 26 lines
Quoted 26 lines

This adds synchronisation of the 6 vcpu registers (only 32bits of them) that vmport.c needs between Xen and QEMU.

This is to avoid a 2nd and 3rd exchange between QEMU and Xen to fetch and put these 6 vcpu registers used by the code in vmport.c and vmmouse.c

The registers are passed in the new shared page provided by HVM_PARAM_VMPORT_IOREQ_PFN.

Add new array to XenIOState that allows selection of current_cpu by ioreq_id.

Now pass XenIOState to handle_ioreq().

Add new routines regs_to_cpu(), regs_from_cpu(), and handle_vmport_ioreq().

---
  include/hw/xen/xen_common.h |  15 ++++++
  xen-hvm.c                   | 121
++++++++++++++++++++++++++++++++++++++++++--
  2 files changed, 131 insertions(+), 5 deletions(-)
Quoted 8 lines
Quoted 8 lines

include/hw/xen/xen_common.h void destroy_hvm_domain(bool reboot); · line 165

Quoted 8 lines
include/hw/xen/xen_common.h +0 -0
index 07731b9..9542756 100644
@@ -164,4 +164,19 @@ void destroy_hvm_domain(bool reboot);
  /* shutdown/destroy current domain because of an error */
  void xen_shutdown_fatal_error(const char *fmt, ...)

GCC_FMT_ATTR(1,

Quoted 69 lines

2);

Quoted 37 lines
Quoted 37 lines
include/hw/xen/xen_common.h +15 -0
+#ifdef HVM_PARAM_VMPORT_IOREQ_PFN
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return xc_get_hvm_param(xc, dom,
 HVM_PARAM_VMPORT_IOREQ_PFN,
+                            vmport_ioreq_pfn);
+}
+#else
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return -ENOSYS;
+}
+#endif
+
  #endif /* QEMU_HW_XEN_COMMON_H */
xen-hvm.c +3 -0
index 05e522c..17b3cbd 100644
@@ -41,6 +41,29 @@ static MemoryRegion *framebuffer;
  static bool xen_in_migration;
 
  /* Compatibility with older version */
+
+/* This allows QEMU to build on a system that has Xen 4.5 or earlier
+ * installed.  This here (not in hw/xen/xen_common.h) because

xen/hvm/ioreq.h
+ * needs to be included before this block and hw/xen/xen_common.h needs to
+ * be included before xen/hvm/ioreq.h
+ */

xen-hvm.c +3 -0
+#ifndef IOREQ_TYPE_VMWARE_PORT
+#define IOREQ_TYPE_VMWARE_PORT  3
+struct vmware_ioreq {
Quoted 8 lines

xen-hvm.c static MemoryRegion *framebuffer; · line 59

Quoted 7 lines
xen-hvm.c +7 -0
+    uint32_t esi;
+    uint32_t edi;
+    uint32_t ebx;
+    uint32_t ecx;
+    uint32_t edx;
+};
+typedef struct vmware_ioreq vmware_ioreq_t;

This struct is not really a request any more. Maybe vmware_regs_t?

Sure.

Quoted 9 lines

xen-hvm.c static MemoryRegion *framebuffer;

Quoted 9 lines
xen-hvm.c +7 -0
+
+struct shared_vmport_iopage {
+    struct vmware_ioreq vcpu_vmport_ioreq[1];
+};
+typedef struct shared_vmport_iopage shared_vmport_iopage_t;
+#endif
+
  #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
  static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page,

int i)

Quoted 10 lines

xen-hvm.c typedef struct XenPhysmap { · line 108

Quoted 9 lines
  {
xen-hvm.c +2 -0
@@ -79,8 +102,10 @@ typedef struct XenPhysmap {
 
  typedef struct XenIOState {
      shared_iopage_t *shared_page;
+    shared_vmport_iopage_t *shared_vmport_page;
      buffered_iopage_t *buffered_io_page;
      QEMUTimer *buffered_io_timer;
+    CPUState **cpu_by_ioreq_id;

This name implies the array is indexed by an id carries in the ioreq. That

Quoted 3 lines

doesn't seem to be the case; it's just indexed by guest vcpu id isn't it?

Yes. How about cpu_by_vcpu_id ?

Ok.

Quoted 81 lines
Quoted 12 lines
Quoted 12 lines
xen-hvm.c +2 -0
      /* the evtchn port for polling the notification, */
      evtchn_port_t *ioreq_local_port;
      /* evtchn local port for buffered io */
@@ -101,6 +126,8 @@ typedef struct XenIOState {
      Notifier wakeup;
  } XenIOState;
 
+static void handle_ioreq(XenIOState *state, ioreq_t *req);
+
  /* Xen specific function for piix pci */
 
Quoted 8 lines

xen-hvm.c static ioreq_t · line 642

Quoted 8 lines
xen-hvm.c +2 -0
  int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
@@ -610,6 +637,20 @@ static ioreq_t
 *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
      return req;
  }
 
+/* get the vmport ioreq packets from share mem */
+static vmware_ioreq_t

*cpu_get_vmport_ioreq_from_shared_memory(

Quoted 1 line

Is this worth its own function? I can only see one use of it in

handle_vmport_ioreq below...

Will move it in-line. I was just following cpu_get_ioreq_from_shared_memory which also has 1 use.

Quoted 10 lines

xen-hvm.c static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu) · line 649

Quoted 9 lines
xen-hvm.c +8 -0
+    XenIOState *state, int vcpu)
+{
+    vmware_ioreq_t *vmport_req;
+
+    assert(state->shared_vmport_page);
+    vmport_req = &state->shared_vmport_page-
 > vcpu_vmport_ioreq[vcpu];
+
+    xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */

I don't think that comment is applicable here, and I don't think you need

the barrier anyway.

I was not sure either way, so went with the safe way. Will drop.

Quoted 11 lines
Quoted 11 lines
xen-hvm.c +4 -0
+
+    return vmport_req;
+}
+
  /* use poll to get the port notification */

/* ioreq_vec--out,the */
/* retval--the number of ioreq packet */

xen-hvm.c +0 -0
@@ -773,7 +814,50 @@ static void cpu_ioreq_move(ioreq_t *req)
      }
  }
 
Quoted 2 lines

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 817

Quoted 2 lines
xen-hvm.c +1 -1
-static void handle_ioreq(ioreq_t *req)
+static void regs_to_cpu(XenIOState *state, vmware_ioreq_t

*vmport_req,

Quoted 25 lines
Quoted 14 lines
xen-hvm.c +14 -0
+                        ioreq_t *req)
+{
+    X86CPU *cpu;
+    CPUX86State *env;
+
+    current_cpu = state->cpu_by_ioreq_id[state->send_vcpu];
+    cpu = X86_CPU(current_cpu);
+    env = &cpu->env;
+    env->regs[R_EAX] = req->data;
+    env->regs[R_EBX] = vmport_req->ebx;
+    env->regs[R_ECX] = vmport_req->ecx;
+    env->regs[R_EDX] = vmport_req->edx;
+    env->regs[R_ESI] = vmport_req->esi;
+    env->regs[R_EDI] = vmport_req->edi;

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 840

Quoted 10 lines
xen-hvm.c +9 -0
+}
+
+static void regs_from_cpu(XenIOState *state, vmware_ioreq_t
 *vmport_req,
+                          ioreq_t *req)
+{
+    X86CPU *cpu = X86_CPU(current_cpu);
+    CPUX86State *env = &cpu->env;
+
+    assert(sizeof(*vmport_req) <= sizeof(*req));

Why?

Quoted 8 lines
Quoted 3 lines

Since there is 1 page of ioreq_t indexed by vcpu id this checks that the 1 page of vmport data has at least the same number of vcpu slots.

Does it? It looks like it checks that the size of the vmware_ioreq_t structure is less than or equal to the size of the ioreq_t structure to me. I see nothing checking number of array elements.

Oh, I now realize you mean the same theoretical rather than actual limit, in which case this can be a build time check anyway.

Paul

Quoted 136 lines

Paul

Quoted 81 lines

This prevents a bad memory read. Looks to not be the right place
for this, will move.

Quoted 13 lines
Quoted 13 lines
xen-hvm.c +13 -0
+
+    vmport_req->ebx = env->regs[R_EBX];
+    vmport_req->ecx = env->regs[R_ECX];
+    vmport_req->edx = env->regs[R_EDX];
+    vmport_req->esi = env->regs[R_ESI];
+    vmport_req->edi = env->regs[R_EDI];
+    current_cpu = NULL;
+}
+
+static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
+{
+    vmware_ioreq_t *vmport_req =
+        cpu_get_vmport_ioreq_from_shared_memory(state, state-
Quoted 6 lines

xen-hvm.c static void cpu_ioreq_move(ioreq_t *req) · line 857

Quoted 5 lines
xen-hvm.c +4 -0
 > send_vcpu);
+
+    regs_to_cpu(state, vmport_req, req);
+    cpu_ioreq_pio(req);
+    regs_from_cpu(state, vmport_req, req);

state appears to be unused in this function, and you could avoid passing it

to both of them by setting current_cpu here couldn't you?

Yes, will move state code here.

-Don Slutz

Quoted 53 lines

Paul

Quoted 51 lines
xen-hvm.c +3 -0
+}
+
+static void handle_ioreq(XenIOState *state, ioreq_t *req)
  {
      if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
              (req->size < sizeof (target_ulong))) {
xen-hvm.c +4 -1
@@ -787,6 +871,9 @@ static void handle_ioreq(ioreq_t *req)
          case IOREQ_TYPE_COPY:
              cpu_ioreq_move(req);
              break;
+        case IOREQ_TYPE_VMWARE_PORT:
+            handle_vmport_ioreq(state, req);
+            break;
          case IOREQ_TYPE_TIMEOFFSET:
              break;
          case IOREQ_TYPE_INVALIDATE:
@@ -828,7 +915,7 @@ static int handle_buffered_iopage(XenIOState
 *state)
              req.data |= ((uint64_t)buf_req->data) << 32;
          }
 
-        handle_ioreq(&req);
+        handle_ioreq(state, &req);
 
          xen_mb();
          state->buffered_io_page->read_pointer += qw ? 2 : 1;
xen-hvm.c +5 -3
@@ -857,14 +944,16 @@ static void cpu_handle_ioreq(void *opaque)
 
      handle_buffered_iopage(state);
      if (req) {
-        handle_ioreq(req);
+        handle_ioreq(state, req);
 
          if (req->state != STATE_IOREQ_INPROCESS) {
              fprintf(stderr, "Badness in I/O request ... not in service?!: "
                      "%x, ptr: %x, port: %"PRIx64", "
-                    "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %"
 FMT_ioreq_size "\n",
+                    "data: %"PRIx64", count: %" FMT_ioreq_size
+                    ", size: %" FMT_ioreq_size
+                    ", type: %"FMT_ioreq_size"\n",
                      req->state, req->data_is_ptr, req->addr,
-                    req->data, req->count, req->size);
+                    req->data, req->count, req->size, req->type);
              destroy_hvm_domain(false);
              return;
          }
xen-hvm.c +0 -0
@@ -904,6 +993,14 @@ static void xen_main_loop_prepare(XenIOState
 *state)
                                                   state);
 
Quoted 10 lines
Quoted 10 lines

xen-hvm.c static void xen_main_loop_prepare(XenIOState *state)

Quoted 10 lines
xen-hvm.c +8 -0
      if (evtchn_fd != -1) {
+        CPUState *cpu_state;
+
+        DPRINTF("%s: Init cpu_by_ioreq_id\n", __func__);
+        CPU_FOREACH(cpu_state) {
+            DPRINTF("%s: cpu_by_ioreq_id[%d]=%p\n",
+                    __func__, cpu_state->cpu_index, cpu_state);
+            state->cpu_by_ioreq_id[cpu_state->cpu_index] = cpu_state;
+        }
          qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL,

state);

Quoted 25 lines
Quoted 7 lines
Quoted 7 lines
xen-hvm.c +0 -0
      }
  }
xen-hvm.c +0 -0
@@ -1020,6 +1117,17 @@ int xen_hvm_init(ram_addr_t
 *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
                   errno, xen_xc);
      }
 
Quoted 7 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 7 lines
xen-hvm.c +7 -0
+    if (!xen_get_vmport_ioreq_pfn(xen_xc, xen_domid, &ioreq_pfn)) {
+        DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
+        state->shared_vmport_page =
+            xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
+                                 PROT_READ|PROT_WRITE, ioreq_pfn);
+        if (state->shared_vmport_page == NULL) {
+            hw_error("map shared vmport IO page returned error %d

handle="

Quoted 5 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,

Quoted 5 lines
xen-hvm.c +4 -0
+                     XC_INTERFACE_FMT, errno, xen_xc);
+        }
+    }
+
      xc_get_hvm_param(xen_xc, xen_domid,

HVM_PARAM_BUFIOREQ_PFN,

Quoted 3 lines
Quoted 3 lines
xen-hvm.c +0 -0
 &ioreq_pfn);
      DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
      state->buffered_io_page = xc_map_foreign_range(xen_xc,

xen_domid,

Quoted 8 lines
Quoted 8 lines

xen-hvm.c int xen_hvm_init(ram_addr_t · line 1141

Quoted 8 lines

XC_PAGE_SIZE,

xen-hvm.c +2 -0
@@ -1028,6 +1136,9 @@ int xen_hvm_init(ram_addr_t
 *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
          hw_error("map buffered IO page returned error %d", errno);
      }
 
+    /* Note: cpus is empty at this point in init */
+    state->cpu_by_ioreq_id = g_malloc0(max_cpus * sizeof(CPUState

*));

Quoted 7 lines
Quoted 2 lines

xen-hvm.c int xen_hvm_init(ram_addr_t · line 1143

Quoted 2 lines
xen-hvm.c +1 -0
+
      state->ioreq_local_port = g_malloc0(max_cpus * sizeof

(evtchn_port_t));

Quoted 4 lines
Quoted 4 lines
xen-hvm.c +0 -1
      /* FIXME: how about if we overflow the page here? */
--

1.8.4

#6 lore raw

Re: [Qemu-devel] [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

In reply to Don Slutz
Quoted 230 lines · Don Slutz

This adds synchronisation of the 6 vcpu registers (only 32bits of them) that vmport.c needs between Xen and QEMU.

This is to avoid a 2nd and 3rd exchange between QEMU and Xen to fetch and put these 6 vcpu registers used by the code in vmport.c and vmmouse.c

The registers are passed in the new shared page provided by HVM_PARAM_VMPORT_IOREQ_PFN.

Add new array to XenIOState that allows selection of current_cpu by ioreq_id.

Now pass XenIOState to handle_ioreq().

Add new routines regs_to_cpu(), regs_from_cpu(), and handle_vmport_ioreq().

---
 include/hw/xen/xen_common.h |  15 ++++++
 xen-hvm.c                   | 121 ++++++++++++++++++++++++++++++++++++++++++--
 2 files changed, 131 insertions(+), 5 deletions(-)
include/hw/xen/xen_common.h +15 -0
index 07731b9..9542756 100644
@@ -164,4 +164,19 @@ void destroy_hvm_domain(bool reboot);
 /* shutdown/destroy current domain because of an error */
 void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
+#ifdef HVM_PARAM_VMPORT_IOREQ_PFN
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return xc_get_hvm_param(xc, dom, HVM_PARAM_VMPORT_IOREQ_PFN,
+                            vmport_ioreq_pfn);
+}
+#else
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return -ENOSYS;
+}
+#endif
+
 #endif /* QEMU_HW_XEN_COMMON_H */
xen-hvm.c +102 -5
index 05e522c..17b3cbd 100644
@@ -41,6 +41,29 @@ static MemoryRegion *framebuffer;
 static bool xen_in_migration;
 
 /* Compatibility with older version */
+
+/* This allows QEMU to build on a system that has Xen 4.5 or earlier
+ * installed.  This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
+ * needs to be included before this block and hw/xen/xen_common.h needs to
+ * be included before xen/hvm/ioreq.h
+ */
+#ifndef IOREQ_TYPE_VMWARE_PORT
+#define IOREQ_TYPE_VMWARE_PORT  3
+struct vmware_ioreq {
+    uint32_t esi;
+    uint32_t edi;
+    uint32_t ebx;
+    uint32_t ecx;
+    uint32_t edx;
+};
+typedef struct vmware_ioreq vmware_ioreq_t;
+
+struct shared_vmport_iopage {
+    struct vmware_ioreq vcpu_vmport_ioreq[1];
+};
+typedef struct shared_vmport_iopage shared_vmport_iopage_t;
+#endif
+
 #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
 static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
 {
@@ -79,8 +102,10 @@ typedef struct XenPhysmap {
 
 typedef struct XenIOState {
     shared_iopage_t *shared_page;
+    shared_vmport_iopage_t *shared_vmport_page;
     buffered_iopage_t *buffered_io_page;
     QEMUTimer *buffered_io_timer;
+    CPUState **cpu_by_ioreq_id;
     /* the evtchn port for polling the notification, */
     evtchn_port_t *ioreq_local_port;
     /* evtchn local port for buffered io */
@@ -101,6 +126,8 @@ typedef struct XenIOState {
     Notifier wakeup;
 } XenIOState;
 
+static void handle_ioreq(XenIOState *state, ioreq_t *req);
+
 /* Xen specific function for piix pci */
 
 int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
@@ -610,6 +637,20 @@ static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
     return req;
 }
 
+/* get the vmport ioreq packets from share mem */
+static vmware_ioreq_t *cpu_get_vmport_ioreq_from_shared_memory(
+    XenIOState *state, int vcpu)
+{
+    vmware_ioreq_t *vmport_req;
+
+    assert(state->shared_vmport_page);
+    vmport_req = &state->shared_vmport_page->vcpu_vmport_ioreq[vcpu];
+
+    xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
+
+    return vmport_req;
+}
+
 /* use poll to get the port notification */
 /* ioreq_vec--out,the */
 /* retval--the number of ioreq packet */
@@ -773,7 +814,50 @@ static void cpu_ioreq_move(ioreq_t *req)
     }
 }
 
-static void handle_ioreq(ioreq_t *req)
+static void regs_to_cpu(XenIOState *state, vmware_ioreq_t *vmport_req,
+                        ioreq_t *req)
+{
+    X86CPU *cpu;
+    CPUX86State *env;
+
+    current_cpu = state->cpu_by_ioreq_id[state->send_vcpu];
+    cpu = X86_CPU(current_cpu);
+    env = &cpu->env;
+    env->regs[R_EAX] = req->data;
+    env->regs[R_EBX] = vmport_req->ebx;
+    env->regs[R_ECX] = vmport_req->ecx;
+    env->regs[R_EDX] = vmport_req->edx;
+    env->regs[R_ESI] = vmport_req->esi;
+    env->regs[R_EDI] = vmport_req->edi;
+}
+
+static void regs_from_cpu(XenIOState *state, vmware_ioreq_t *vmport_req,
+                          ioreq_t *req)
+{
+    X86CPU *cpu = X86_CPU(current_cpu);
+    CPUX86State *env = &cpu->env;
+
+    assert(sizeof(*vmport_req) <= sizeof(*req));
+
+    vmport_req->ebx = env->regs[R_EBX];
+    vmport_req->ecx = env->regs[R_ECX];
+    vmport_req->edx = env->regs[R_EDX];
+    vmport_req->esi = env->regs[R_ESI];
+    vmport_req->edi = env->regs[R_EDI];
+    current_cpu = NULL;
+}
+
+static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
+{
+    vmware_ioreq_t *vmport_req =
+        cpu_get_vmport_ioreq_from_shared_memory(state, state->send_vcpu);
+
+    regs_to_cpu(state, vmport_req, req);
+    cpu_ioreq_pio(req);
+    regs_from_cpu(state, vmport_req, req);
+}
+
+static void handle_ioreq(XenIOState *state, ioreq_t *req)
 {
     if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
             (req->size < sizeof (target_ulong))) {
@@ -787,6 +871,9 @@ static void handle_ioreq(ioreq_t *req)
         case IOREQ_TYPE_COPY:
             cpu_ioreq_move(req);
             break;
+        case IOREQ_TYPE_VMWARE_PORT:
+            handle_vmport_ioreq(state, req);
+            break;
         case IOREQ_TYPE_TIMEOFFSET:
             break;
         case IOREQ_TYPE_INVALIDATE:
@@ -828,7 +915,7 @@ static int handle_buffered_iopage(XenIOState *state)
             req.data |= ((uint64_t)buf_req->data) << 32;
         }
 
-        handle_ioreq(&req);
+        handle_ioreq(state, &req);
 
         xen_mb();
         state->buffered_io_page->read_pointer += qw ? 2 : 1;
@@ -857,14 +944,16 @@ static void cpu_handle_ioreq(void *opaque)
 
     handle_buffered_iopage(state);
     if (req) {
-        handle_ioreq(req);
+        handle_ioreq(state, req);
 
         if (req->state != STATE_IOREQ_INPROCESS) {
             fprintf(stderr, "Badness in I/O request ... not in service?!: "
                     "%x, ptr: %x, port: %"PRIx64", "
-                    "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
+                    "data: %"PRIx64", count: %" FMT_ioreq_size
+                    ", size: %" FMT_ioreq_size
+                    ", type: %"FMT_ioreq_size"\n",
                     req->state, req->data_is_ptr, req->addr,
-                    req->data, req->count, req->size);
+                    req->data, req->count, req->size, req->type);
             destroy_hvm_domain(false);
             return;
         }
@@ -904,6 +993,14 @@ static void xen_main_loop_prepare(XenIOState *state)
                                                  state);
 
     if (evtchn_fd != -1) {
+        CPUState *cpu_state;
+
+        DPRINTF("%s: Init cpu_by_ioreq_id\n", __func__);
+        CPU_FOREACH(cpu_state) {
+            DPRINTF("%s: cpu_by_ioreq_id[%d]=%p\n",
+                    __func__, cpu_state->cpu_index, cpu_state);
+            state->cpu_by_ioreq_id[cpu_state->cpu_index] = cpu_state;
+        }
         qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
     }
 }

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1120

Quoted 4 lines
xen-hvm.c +1 -0
@@ -1020,6 +1117,17 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
                  errno, xen_xc);
     }
 
+    if (!xen_get_vmport_ioreq_pfn(xen_xc, xen_domid, &ioreq_pfn)) {

I think you should error out if it fails with error != -ENOSYS.

Quoted 26 lines
xen-hvm.c +13 -0
+        DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
+        state->shared_vmport_page =
+            xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
+                                 PROT_READ|PROT_WRITE, ioreq_pfn);
+        if (state->shared_vmport_page == NULL) {
+            hw_error("map shared vmport IO page returned error %d handle="
+                     XC_INTERFACE_FMT, errno, xen_xc);
+        }
+    }
+
     xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
     DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
     state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
@@ -1028,6 +1136,9 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
         hw_error("map buffered IO page returned error %d", errno);
     }
 
+    /* Note: cpus is empty at this point in init */
+    state->cpu_by_ioreq_id = g_malloc0(max_cpus * sizeof(CPUState *));
+
     state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
 
     /* FIXME: how about if we overflow the page here? */
Signature · 1 line
1.8.4
author
#7 lore raw

Re: [Qemu-devel] [PATCH v4 1/1] xen-hvm.c: Add support for Xen access to vmport

Quoted 234 lines · Stefano Stabellini
Quoted 233 lines · Don Slutz

This adds synchronisation of the 6 vcpu registers (only 32bits of them) that vmport.c needs between Xen and QEMU.

This is to avoid a 2nd and 3rd exchange between QEMU and Xen to fetch and put these 6 vcpu registers used by the code in vmport.c and vmmouse.c

The registers are passed in the new shared page provided by HVM_PARAM_VMPORT_IOREQ_PFN.

Add new array to XenIOState that allows selection of current_cpu by ioreq_id.

Now pass XenIOState to handle_ioreq().

Add new routines regs_to_cpu(), regs_from_cpu(), and handle_vmport_ioreq().

---
  include/hw/xen/xen_common.h |  15 ++++++
  xen-hvm.c                   | 121 ++++++++++++++++++++++++++++++++++++++++++--
  2 files changed, 131 insertions(+), 5 deletions(-)
include/hw/xen/xen_common.h +15 -0
index 07731b9..9542756 100644
@@ -164,4 +164,19 @@ void destroy_hvm_domain(bool reboot);
  /* shutdown/destroy current domain because of an error */
  void xen_shutdown_fatal_error(const char *fmt, ...) GCC_FMT_ATTR(1, 2);
 
+#ifdef HVM_PARAM_VMPORT_IOREQ_PFN
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return xc_get_hvm_param(xc, dom, HVM_PARAM_VMPORT_IOREQ_PFN,
+                            vmport_ioreq_pfn);
+}
+#else
+static inline int xen_get_vmport_ioreq_pfn(XenXC xc, domid_t dom,
+                                           unsigned long *vmport_ioreq_pfn)
+{
+    return -ENOSYS;
+}
+#endif
+
  #endif /* QEMU_HW_XEN_COMMON_H */
xen-hvm.c +102 -5
index 05e522c..17b3cbd 100644
@@ -41,6 +41,29 @@ static MemoryRegion *framebuffer;
  static bool xen_in_migration;
 
  /* Compatibility with older version */
+
+/* This allows QEMU to build on a system that has Xen 4.5 or earlier
+ * installed.  This here (not in hw/xen/xen_common.h) because xen/hvm/ioreq.h
+ * needs to be included before this block and hw/xen/xen_common.h needs to
+ * be included before xen/hvm/ioreq.h
+ */
+#ifndef IOREQ_TYPE_VMWARE_PORT
+#define IOREQ_TYPE_VMWARE_PORT  3
+struct vmware_ioreq {
+    uint32_t esi;
+    uint32_t edi;
+    uint32_t ebx;
+    uint32_t ecx;
+    uint32_t edx;
+};
+typedef struct vmware_ioreq vmware_ioreq_t;
+
+struct shared_vmport_iopage {
+    struct vmware_ioreq vcpu_vmport_ioreq[1];
+};
+typedef struct shared_vmport_iopage shared_vmport_iopage_t;
+#endif
+
  #if __XEN_LATEST_INTERFACE_VERSION__ < 0x0003020a
  static inline uint32_t xen_vcpu_eport(shared_iopage_t *shared_page, int i)
  {
@@ -79,8 +102,10 @@ typedef struct XenPhysmap {
 
  typedef struct XenIOState {
      shared_iopage_t *shared_page;
+    shared_vmport_iopage_t *shared_vmport_page;
      buffered_iopage_t *buffered_io_page;
      QEMUTimer *buffered_io_timer;
+    CPUState **cpu_by_ioreq_id;
      /* the evtchn port for polling the notification, */
      evtchn_port_t *ioreq_local_port;
      /* evtchn local port for buffered io */
@@ -101,6 +126,8 @@ typedef struct XenIOState {
      Notifier wakeup;
  } XenIOState;
 
+static void handle_ioreq(XenIOState *state, ioreq_t *req);
+
  /* Xen specific function for piix pci */
 
  int xen_pci_slot_get_pirq(PCIDevice *pci_dev, int irq_num)
@@ -610,6 +637,20 @@ static ioreq_t *cpu_get_ioreq_from_shared_memory(XenIOState *state, int vcpu)
      return req;
  }
 
+/* get the vmport ioreq packets from share mem */
+static vmware_ioreq_t *cpu_get_vmport_ioreq_from_shared_memory(
+    XenIOState *state, int vcpu)
+{
+    vmware_ioreq_t *vmport_req;
+
+    assert(state->shared_vmport_page);
+    vmport_req = &state->shared_vmport_page->vcpu_vmport_ioreq[vcpu];
+
+    xen_rmb(); /* see IOREQ_READY /then/ read contents of ioreq */
+
+    return vmport_req;
+}
+
  /* use poll to get the port notification */
  /* ioreq_vec--out,the */
  /* retval--the number of ioreq packet */
@@ -773,7 +814,50 @@ static void cpu_ioreq_move(ioreq_t *req)
      }
  }
 
-static void handle_ioreq(ioreq_t *req)
+static void regs_to_cpu(XenIOState *state, vmware_ioreq_t *vmport_req,
+                        ioreq_t *req)
+{
+    X86CPU *cpu;
+    CPUX86State *env;
+
+    current_cpu = state->cpu_by_ioreq_id[state->send_vcpu];
+    cpu = X86_CPU(current_cpu);
+    env = &cpu->env;
+    env->regs[R_EAX] = req->data;
+    env->regs[R_EBX] = vmport_req->ebx;
+    env->regs[R_ECX] = vmport_req->ecx;
+    env->regs[R_EDX] = vmport_req->edx;
+    env->regs[R_ESI] = vmport_req->esi;
+    env->regs[R_EDI] = vmport_req->edi;
+}
+
+static void regs_from_cpu(XenIOState *state, vmware_ioreq_t *vmport_req,
+                          ioreq_t *req)
+{
+    X86CPU *cpu = X86_CPU(current_cpu);
+    CPUX86State *env = &cpu->env;
+
+    assert(sizeof(*vmport_req) <= sizeof(*req));
+
+    vmport_req->ebx = env->regs[R_EBX];
+    vmport_req->ecx = env->regs[R_ECX];
+    vmport_req->edx = env->regs[R_EDX];
+    vmport_req->esi = env->regs[R_ESI];
+    vmport_req->edi = env->regs[R_EDI];
+    current_cpu = NULL;
+}
+
+static void handle_vmport_ioreq(XenIOState *state, ioreq_t *req)
+{
+    vmware_ioreq_t *vmport_req =
+        cpu_get_vmport_ioreq_from_shared_memory(state, state->send_vcpu);
+
+    regs_to_cpu(state, vmport_req, req);
+    cpu_ioreq_pio(req);
+    regs_from_cpu(state, vmport_req, req);
+}
+
+static void handle_ioreq(XenIOState *state, ioreq_t *req)
  {
      if (!req->data_is_ptr && (req->dir == IOREQ_WRITE) &&
              (req->size < sizeof (target_ulong))) {
@@ -787,6 +871,9 @@ static void handle_ioreq(ioreq_t *req)
          case IOREQ_TYPE_COPY:
              cpu_ioreq_move(req);
              break;
+        case IOREQ_TYPE_VMWARE_PORT:
+            handle_vmport_ioreq(state, req);
+            break;
          case IOREQ_TYPE_TIMEOFFSET:
              break;
          case IOREQ_TYPE_INVALIDATE:
@@ -828,7 +915,7 @@ static int handle_buffered_iopage(XenIOState *state)
              req.data |= ((uint64_t)buf_req->data) << 32;
          }
 
-        handle_ioreq(&req);
+        handle_ioreq(state, &req);
 
          xen_mb();
          state->buffered_io_page->read_pointer += qw ? 2 : 1;
@@ -857,14 +944,16 @@ static void cpu_handle_ioreq(void *opaque)
 
      handle_buffered_iopage(state);
      if (req) {
-        handle_ioreq(req);
+        handle_ioreq(state, req);
 
          if (req->state != STATE_IOREQ_INPROCESS) {
              fprintf(stderr, "Badness in I/O request ... not in service?!: "
                      "%x, ptr: %x, port: %"PRIx64", "
-                    "data: %"PRIx64", count: %" FMT_ioreq_size ", size: %" FMT_ioreq_size "\n",
+                    "data: %"PRIx64", count: %" FMT_ioreq_size
+                    ", size: %" FMT_ioreq_size
+                    ", type: %"FMT_ioreq_size"\n",
                      req->state, req->data_is_ptr, req->addr,
-                    req->data, req->count, req->size);
+                    req->data, req->count, req->size, req->type);
              destroy_hvm_domain(false);
              return;
          }
@@ -904,6 +993,14 @@ static void xen_main_loop_prepare(XenIOState *state)
                                                   state);
 
      if (evtchn_fd != -1) {
+        CPUState *cpu_state;
+
+        DPRINTF("%s: Init cpu_by_ioreq_id\n", __func__);
+        CPU_FOREACH(cpu_state) {
+            DPRINTF("%s: cpu_by_ioreq_id[%d]=%p\n",
+                    __func__, cpu_state->cpu_index, cpu_state);
+            state->cpu_by_ioreq_id[cpu_state->cpu_index] = cpu_state;
+        }
          qemu_set_fd_handler(evtchn_fd, cpu_handle_ioreq, NULL, state);
      }
  }
@@ -1020,6 +1117,17 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
                   errno, xen_xc);
      }
 
Quoted 3 lines

xen-hvm.c int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size, · line 1120

Quoted 1 line
xen-hvm.c +1 -0
+    if (!xen_get_vmport_ioreq_pfn(xen_xc, xen_domid, &ioreq_pfn)) {

I think you should error out if it fails with error != -ENOSYS.

Will do.

-Don Slutz

Quoted 26 lines
Quoted 26 lines
xen-hvm.c +13 -0
+        DPRINTF("shared vmport page at pfn %lx\n", ioreq_pfn);
+        state->shared_vmport_page =
+            xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
+                                 PROT_READ|PROT_WRITE, ioreq_pfn);
+        if (state->shared_vmport_page == NULL) {
+            hw_error("map shared vmport IO page returned error %d handle="
+                     XC_INTERFACE_FMT, errno, xen_xc);
+        }
+    }
+
      xc_get_hvm_param(xen_xc, xen_domid, HVM_PARAM_BUFIOREQ_PFN, &ioreq_pfn);
      DPRINTF("buffered io page at pfn %lx\n", ioreq_pfn);
      state->buffered_io_page = xc_map_foreign_range(xen_xc, xen_domid, XC_PAGE_SIZE,
@@ -1028,6 +1136,9 @@ int xen_hvm_init(ram_addr_t *below_4g_mem_size, ram_addr_t *above_4g_mem_size,
          hw_error("map buffered IO page returned error %d", errno);
      }
 
+    /* Note: cpus is empty at this point in init */
+    state->cpu_by_ioreq_id = g_malloc0(max_cpus * sizeof(CPUState *));
+
      state->ioreq_local_port = g_malloc0(max_cpus * sizeof (evtchn_port_t));
 
      /* FIXME: how about if we overflow the page here? */
Signature · 1 line
1.8.4