Skip to contentSkip to conversation

x86/hvm: I/O emulation cleanup and fix

PATCH Superseded Paul Durrant 5 participants 46 messages Jun 30, 2015 – Jul 7, 2015

v5 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#20 lore raw

PATCH v5 6/16 x86/hvm: add length to mmio check op

In reply to Paul Durrant

When memory mapped I/O is range checked by internal handlers, the length of the access should be taken into account.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/intercept.c |   22 +++++++++++++++++++---
 xen/include/asm-x86/hvm/io.h |   16 ++++++++++++++++
 2 files changed, 35 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/intercept.c +19 -3
index 7d36785..42050f4 100644
@@ -35,9 +35,19 @@
 static bool_t hvm_mmio_accept(const struct hvm_io_handler *handler,
                               const ioreq_t *p)
 {
+    paddr_t first = hvm_mmio_first_byte(p);
+    paddr_t last = hvm_mmio_last_byte(p);
+
     BUG_ON(handler->type != IOREQ_TYPE_COPY);
 
-    return handler->mmio.ops->check(current, p->addr);
+    if ( !handler->mmio.ops->check(current, first) )
+        return 0;
+
+    if ( p->size > 1 &&
+         !handler->mmio.ops->check(current, last) )
+        domain_crash(current->domain);
+
+    return 1;
 }
 
 static int hvm_mmio_read(const struct hvm_io_handler *handler,
@@ -112,7 +122,8 @@ static const struct hvm_io_ops portio_ops = {
 static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
                                     ioreq_t *p)
 {
-    struct hvm_vcpu_io *vio = &current->arch.hvm_vcpu.hvm_io;
+    struct vcpu *curr = current;
+    struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
     const struct hvm_io_ops *ops =
         (p->type == IOREQ_TYPE_COPY) ?
         &mmio_ops :
@@ -223,6 +234,9 @@ static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
 
     if ( i != 0 )
     {
+        if ( rc == X86EMUL_UNHANDLEABLE )
+            domain_crash(curr->domain);
+
         p->count = i;
         rc = X86EMUL_OKAY;
     }
@@ -342,7 +356,9 @@ bool_t hvm_mmio_internal(paddr_t gpa)
 {
     ioreq_t p = {
         .type = IOREQ_TYPE_COPY,
-        .addr = gpa
+        .addr = gpa,
+        .count = 1,
+        .size = 1
     };
 
     return (hvm_find_io_handler(&p) != NULL);
xen/include/asm-x86/hvm/io.h +16 -0
index 3f6a39a..78c9704 100644
@@ -43,6 +43,22 @@ struct hvm_mmio_ops {
     hvm_mmio_write_t write;
 };
 
+static inline paddr_t hvm_mmio_first_byte(const ioreq_t *p)
+{
+    return p->df ?
+           p->addr - (p->count - 1ul) * p->size :
+           p->addr;
+}
+
+static inline paddr_t hvm_mmio_last_byte(const ioreq_t *p)
+{
+    unsigned long count = p->count;
+
+    return p->df ?
+           p->addr + p->size - 1:
+           p->addr + (count * p->size) - 1;
+}
+
 typedef int (*portio_action_t)(
     int dir, uint16_t port, unsigned int bytes, uint32_t *val);
 
Signature · 1 line
1.7.10.4
#1 lore raw

PATCH v5 1/16 x86/hvm: make sure emulation is retried if domain is shutting down

In reply to Paul Durrant

The addition of commit 2df1aa01 "x86/hvm: remove hvm_io_pending() check in hvmemul_do_io()" causes a problem in migration because I/O that was caught by the test of vcpu_start_shutdown_deferral() in hvm_send_assist_req() is now considered completed rather than requiring a retry.

This patch fixes the problem by having hvm_send_assist_req() return X86EMUL_RETRY rather than X86EMUL_OKAY if the vcpu_start_shutdown_deferral() test fails and then making sure that the emulation state is reset if the domain is found to be shutting down.

Keir Fraser <[email protected]>
Jan Beulich <[email protected]>
Andrew Cooper <[email protected]>

---
 xen/arch/x86/hvm/emulate.c |    2 +-
 xen/arch/x86/hvm/hvm.c     |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -1
index fe5661d..8b60843 100644
@@ -183,7 +183,7 @@ static int hvmemul_do_io(
         else
         {
             rc = hvm_send_assist_req(s, &p);
-            if ( rc != X86EMUL_RETRY )
+            if ( rc != X86EMUL_RETRY || curr->domain->is_shutting_down )
                 vio->io_state = HVMIO_none;
             else if ( data_is_addr || dir == IOREQ_WRITE )
                 rc = X86EMUL_OKAY;
xen/arch/x86/hvm/hvm.c +1 -1
index 535d622..f0cf064 100644
@@ -2613,7 +2613,7 @@ int hvm_send_assist_req(struct hvm_ioreq_server *s, ioreq_t *proto_p)
 
     ASSERT(s);
     if ( unlikely(!vcpu_start_shutdown_deferral(curr)) )
-        return X86EMUL_OKAY;
+        return X86EMUL_RETRY;
 
     list_for_each_entry ( sv,
                           &s->ioreq_vcpu_list,
Signature · 1 line
1.7.10.4
#24 lore raw

PATCH v5 8/16 x86/hvm: unify stdvga mmio intercept with standard mmio intercept

In reply to Paul Durrant

It's clear from the following check in hvmemul_rep_movs:

    if ( sp2mt == p2m_mmio_direct || dp2mt == p2m_mmio_direct ||
         (sp2mt == p2m_mmio_dm && dp2mt == p2m_mmio_dm) )
        return X86EMUL_UNHANDLEABLE;

that mmio <-> mmio copy is not handled. This means the code in the stdvga mmio intercept that explicitly handles mmio <-> mmio copy when hvm_copy_to/from_guest_phys() fails is never going to be executed.

This patch therefore adds a check in hvmemul_do_io_addr() to make sure mmio <-> mmio is disallowed and then registers standard mmio intercept ops in stdvga_init().

With this patch all mmio and portio handled within Xen now goes through process_io_intercept().

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c   |    9 ++
 xen/arch/x86/hvm/intercept.c |   30 ++++--
 xen/arch/x86/hvm/stdvga.c    |  207 +++++++++++++++---------------------------
 xen/include/asm-x86/hvm/io.h |   10 +-
 4 files changed, 109 insertions(+), 147 deletions(-)
xen/arch/x86/hvm/emulate.c +9 -0
index 205ca5e..72603f5 100644
@@ -266,6 +266,15 @@ static int hvmemul_acquire_page(unsigned long gmfn, struct page_info **page)
         return X86EMUL_RETRY;
     }
 
+    /* This code should not be reached if the gmfn is not RAM */
+    if ( p2m_is_mmio(p2mt) )
+    {
+        domain_crash(curr_d);
+
+        put_page(*page);
+        return X86EMUL_UNHANDLEABLE;
+    }
+
     return X86EMUL_OKAY;
 }
 
xen/arch/x86/hvm/intercept.c +21 -9
index a613865..415d342 100644
@@ -268,20 +268,21 @@ static const struct hvm_io_handler *hvm_find_io_handler(ioreq_t *p)
 int hvm_io_intercept(ioreq_t *p)
 {
     const struct hvm_io_handler *handler;
-
-    if ( p->type == IOREQ_TYPE_COPY )
-    {
-        int rc = stdvga_intercept_mmio(p);
-        if ( (rc == X86EMUL_OKAY) || (rc == X86EMUL_RETRY) )
-            return rc;
-    }
+    const struct hvm_io_ops *ops;
+    int rc;
 
     handler = hvm_find_io_handler(p);
 
     if ( handler == NULL )
         return X86EMUL_UNHANDLEABLE;
 
-    return hvm_process_io_intercept(handler, p);
+    rc = hvm_process_io_intercept(handler, p);
+
+    ops = handler->ops;
+    if ( ops->complete != NULL )
+        ops->complete(handler);
+
+    return rc;
 }
 
 struct hvm_io_handler *hvm_next_io_handler(struct domain *d)
@@ -344,6 +345,8 @@ void relocate_portio_handler(struct domain *d, uint16_t old_port,
 
 bool_t hvm_mmio_internal(paddr_t gpa)
 {
+    const struct hvm_io_handler *handler;
+    const struct hvm_io_ops *ops;
     ioreq_t p = {
         .type = IOREQ_TYPE_COPY,
         .addr = gpa,
@@ -351,7 +354,16 @@ bool_t hvm_mmio_internal(paddr_t gpa)
         .size = 1
     };
 
-    return (hvm_find_io_handler(&p) != NULL);
+    handler = hvm_find_io_handler(&p);
+
+    if ( handler == NULL )
+        return 0;
+
+    ops = handler->ops;
+    if ( ops->complete != NULL )
+        ops->complete(handler);
+
+    return 1;
 }
 
 /*
xen/arch/x86/hvm/stdvga.c +73 -134

272 lines not on this page · raw message

xen/include/asm-x86/hvm/io.h +6 -4
index 740592f..1ca0d16 100644
@@ -86,10 +86,13 @@ typedef int (*hvm_io_write_t)(const struct hvm_io_handler *,
                               uint64_t data);
 typedef bool_t (*hvm_io_accept_t)(const struct hvm_io_handler *,
                                   const ioreq_t *p);
+typedef void (*hvm_io_complete_t)(const struct hvm_io_handler *);
+
 struct hvm_io_ops {
-    hvm_io_accept_t accept;
-    hvm_io_read_t   read;
-    hvm_io_write_t  write;
+    hvm_io_accept_t   accept;
+    hvm_io_read_t     read;
+    hvm_io_write_t    write;
+    hvm_io_complete_t complete;
 };
 
 int hvm_io_intercept(ioreq_t *p);
@@ -135,7 +138,6 @@ struct hvm_hw_stdvga {
 };
 
 void stdvga_init(struct domain *d);
-int stdvga_intercept_mmio(ioreq_t *p);
 void stdvga_deinit(struct domain *d);
 
 extern void hvm_dpci_msi_eoi(struct domain *d, int vector);
Signature · 1 line
1.7.10.4
#0 lore raw

v5 cover x86/hvm: I/O emulation cleanup and fix

This patch series re-works much of the code involved in emulation of port and memory mapped I/O for HVM guests.

The code has become very convoluted and, at least by inspection, certain emulations will apparently malfunction.

The series is broken down into 16 patches (which are also available in my xenbits repo: http://xenbits.xen.org/gitweb/?p=people/pauldu/xen.git
on the emulation32 branch).

Previous changelog
------------------

v2:
- Removed bogus assertion from patch #15
- Re-worked patch #17 after basic testing of back-port onto XenServer

v3:
- Addressed comments from Jan
- Re-ordered series to bring a couple of more trivial patches to the front
- Backport to XenServer (4.5) now passing automated tests
- Tested on unstable with QEMU upstream and trad, with and without HAP (to force shadow emulation)

v4:
- Removed previous patch (make sure translated MMIO reads or writes fall within a page) and rebased rest of series.
- Address Jan's comments on patch #1

Changelog (now per-patch)
-------------------------

0001-x86-hvm-make-sure-emulation-is-retried-if-domain-is-.patch

This is a fix for an issue on staging reported by Don Slutz

0002-x86-hvm-remove-multiple-open-coded-chunking-loops.patch

v5: Addressed further comments from Jan

0003-x86-hvm-change-hvm_mmio_read_t-and-hvm_mmio_write_t-.patch

v5: New patch to tidy up types

0004-x86-hvm-restrict-port-numbers-to-uint16_t-and-sizes-.patch

v5: New patch to tidy up more types

0005-x86-hvm-unify-internal-portio-and-mmio-intercepts.patch

v5: Addressed further comments from Jan and simplified implementation
by passing ioreq_t to accept() function

0006-x86-hvm-add-length-to-mmio-check-op.patch

v5: Simplified by leaving mmio_check() implementation alone and
calling to check last byte if first-byte check passes

0007-x86-hvm-unify-dpci-portio-intercept-with-standard-po.patch

v5: Addressed further comments from Jan

0008-x86-hvm-unify-stdvga-mmio-intercept-with-standard-mm.patch

v5: Fixed semantic problems pointed out by Jan

0009-x86-hvm-limit-reps-to-avoid-the-need-to-handle-retry.patch

v5: Addressed further comments from Jan

0010-x86-hvm-only-call-hvm_io_assist-from-hvm_wait_for_io.patch

v5: Added Jan's acked-by

0011-x86-hvm-split-I-O-completion-handling-from-state-mod.patch

v5: Confirmed call to msix_write_completion() is in the correct place.

0012-x86-hvm-remove-HVMIO_dispatched-I-O-state.patch

v5: Added some extra comments to the commit

0013-x86-hvm-remove-hvm_io_state-enumeration.patch

v5: Added Jan's acked-by

0014-x86-hvm-use-ioreq_t-to-track-in-flight-state.patch

v5: Added missing hunk with call to handle_pio()

0015-x86-hvm-always-re-emulate-I-O-from-a-buffer.patch

v5: Added Jan's acked-by

0016-x86-hvm-track-large-memory-mapped-accesses-by-buffer.patch

v5: Fixed to cache up three distict I/O emulations per instruction

Testing
-------

The series was been back-ported to staging-4.5 and then dropped onto the XenServer (Dundee) patch queue. All automated branch-safety tests pass.

The series as-is has been manually tested with a Windows 7 (32-bit) VM using upstream QEMU.

#9 lore raw

PATCH v5 3/16 x86/hvm: change hvm_mmio_read_t and hvm_mmio_write_t length argument...

...from unsigned long to unsigned int

A 64-bit length is not necessary, 32 bits is enough.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/hpet.c                   |    4 ++--
 xen/arch/x86/hvm/vioapic.c                |    4 ++--
 xen/arch/x86/hvm/vlapic.c                 |   18 +++++++++---------
 xen/arch/x86/hvm/vmsi.c                   |    4 ++--
 xen/drivers/passthrough/amd/iommu_guest.c |    8 ++++----
 xen/include/asm-x86/hvm/io.h              |    6 +++---
 6 files changed, 22 insertions(+), 22 deletions(-)
xen/arch/x86/hvm/hpet.c +2 -2
index 9585ca8..30ac5dd 100644
@@ -166,7 +166,7 @@ static inline int hpet_check_access_length(
 }
 
 static int hpet_read(
-    struct vcpu *v, unsigned long addr, unsigned long length,
+    struct vcpu *v, unsigned long addr, unsigned int length,
     unsigned long *pval)
 {
     HPETState *h = vcpu_vhpet(v);
@@ -301,7 +301,7 @@ static inline uint64_t hpet_fixup_reg(
 
 static int hpet_write(
     struct vcpu *v, unsigned long addr,
-    unsigned long length, unsigned long val)
+    unsigned int length, unsigned long val)
 {
     HPETState *h = vcpu_vhpet(v);
     uint64_t old_val, new_val;
xen/arch/x86/hvm/vioapic.c +2 -2
index cbbef9f..5c8d890 100644
@@ -94,7 +94,7 @@ static uint32_t vioapic_read_indirect(const struct hvm_hw_vioapic *vioapic)
 
 static int vioapic_read(
     struct vcpu *v, unsigned long addr,
-    unsigned long length, unsigned long *pval)
+    unsigned int length, unsigned long *pval)
 {
     const struct hvm_hw_vioapic *vioapic = domain_vioapic(v->domain);
     uint32_t result;
@@ -215,7 +215,7 @@ static void vioapic_write_indirect(
 
 static int vioapic_write(
     struct vcpu *v, unsigned long addr,
-    unsigned long length, unsigned long val)
+    unsigned int length, unsigned long val)
 {
     struct hvm_hw_vioapic *vioapic = domain_vioapic(v->domain);
 
xen/arch/x86/hvm/vlapic.c +9 -9
index 32e649e..0235cb3 100644
@@ -581,7 +581,7 @@ static uint32_t vlapic_read_aligned(struct vlapic *vlapic, unsigned int offset)
 
 static int vlapic_read(
     struct vcpu *v, unsigned long address,
-    unsigned long len, unsigned long *pval)
+    unsigned int len, unsigned long *pval)
 {
     struct vlapic *vlapic = vcpu_vlapic(v);
     unsigned int offset = address - vlapic_base_address(vlapic);
@@ -611,12 +611,12 @@ static int vlapic_read(
         break;
 
     default:
-        gdprintk(XENLOG_ERR, "Local APIC read with len=%#lx, "
+        gdprintk(XENLOG_ERR, "Local APIC read with len=%#x, "
                  "should be 4 instead.\n", len);
         goto exit_and_crash;
     }
 
-    HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "offset %#x with length %#lx, "
+    HVM_DBG_LOG(DBG_LEVEL_VLAPIC, "offset %#x with length %#x, "
                 "and the result is %#x", offset, len, result);
 
  out:
@@ -624,7 +624,7 @@ static int vlapic_read(
     return X86EMUL_OKAY;
 
  unaligned_exit_and_crash:
-    gdprintk(XENLOG_ERR, "Unaligned LAPIC read len=%#lx at offset=%#x.\n",
+    gdprintk(XENLOG_ERR, "Unaligned LAPIC read len=%#x at offset=%#x.\n",
              len, offset);
  exit_and_crash:
     domain_crash(v->domain);
@@ -819,7 +819,7 @@ static int vlapic_reg_write(struct vcpu *v,
 }
 
 static int vlapic_write(struct vcpu *v, unsigned long address,
-                        unsigned long len, unsigned long val)
+                        unsigned int len, unsigned long val)
 {
     struct vlapic *vlapic = vcpu_vlapic(v);
     unsigned int offset = address - vlapic_base_address(vlapic);
@@ -827,7 +827,7 @@ static int vlapic_write(struct vcpu *v, unsigned long address,
 
     if ( offset != APIC_EOI )
         HVM_DBG_LOG(DBG_LEVEL_VLAPIC,
-                    "offset %#x with length %#lx, and value is %#lx",
+                    "offset %#x with length %#x, and value is %#lx",
                     offset, len, val);
 
     /*
@@ -854,11 +854,11 @@ static int vlapic_write(struct vcpu *v, unsigned long address,
             break;
 
         default:
-            gprintk(XENLOG_ERR, "LAPIC write with len %lu\n", len);
+            gprintk(XENLOG_ERR, "LAPIC write with len %u\n", len);
             goto exit_and_crash;
         }
 
-        gdprintk(XENLOG_INFO, "Notice: LAPIC write with len %lu\n", len);
+        gdprintk(XENLOG_INFO, "Notice: LAPIC write with len %u\n", len);
         offset &= ~3;
     }
     else if ( unlikely(offset & 3) )
@@ -867,7 +867,7 @@ static int vlapic_write(struct vcpu *v, unsigned long address,
     return vlapic_reg_write(v, offset, val);
 
  unaligned_exit_and_crash:
-    gprintk(XENLOG_ERR, "Unaligned LAPIC write: len=%lu offset=%#x.\n",
+    gprintk(XENLOG_ERR, "Unaligned LAPIC write: len=%u offset=%#x.\n",
             len, offset);
  exit_and_crash:
     domain_crash(v->domain);
xen/arch/x86/hvm/vmsi.c +2 -2
index f89233d..6eeae0a 100644
@@ -202,7 +202,7 @@ static struct msi_desc *msixtbl_addr_to_desc(
 
 static int msixtbl_read(
     struct vcpu *v, unsigned long address,
-    unsigned long len, unsigned long *pval)
+    unsigned int len, unsigned long *pval)
 {
     unsigned long offset;
     struct msixtbl_entry *entry;
@@ -258,7 +258,7 @@ out:
 }
 
 static int msixtbl_write(struct vcpu *v, unsigned long address,
-                         unsigned long len, unsigned long val)
+                         unsigned int len, unsigned long val)
 {
     unsigned long offset;
     struct msixtbl_entry *entry;
xen/drivers/passthrough/amd/iommu_guest.c +4 -4
index 7b0c102..a832fdb 100644
@@ -682,7 +682,7 @@ static uint64_t iommu_mmio_read64(struct guest_iommu *iommu,
 }
 
 static int guest_iommu_mmio_read(struct vcpu *v, unsigned long addr,
-                                 unsigned long len, unsigned long *pval)
+                                 unsigned int len, unsigned long *pval)
 {
     struct guest_iommu *iommu = vcpu_iommu(v);
     unsigned long offset;
@@ -695,7 +695,7 @@ static int guest_iommu_mmio_read(struct vcpu *v, unsigned long addr,
     if ( unlikely((offset & (len - 1 )) || (len > 8)) )
     {
         AMD_IOMMU_DEBUG("iommu mmio read access is not aligned:"
-                        " offset = %lx, len = %lx\n", offset, len);
+                        " offset = %lx, len = %x\n", offset, len);
         return X86EMUL_UNHANDLEABLE;
     }
 
@@ -772,7 +772,7 @@ static void guest_iommu_mmio_write64(struct guest_iommu *iommu,
 }
 
 static int guest_iommu_mmio_write(struct vcpu *v, unsigned long addr,
-                                  unsigned long len, unsigned long val)
+                                  unsigned int len, unsigned long val)
 {
     struct guest_iommu *iommu = vcpu_iommu(v);
     unsigned long offset;
@@ -785,7 +785,7 @@ static int guest_iommu_mmio_write(struct vcpu *v, unsigned long addr,
     if ( unlikely((offset & (len - 1)) || (len > 8)) )
     {
         AMD_IOMMU_DEBUG("iommu mmio write access is not aligned:"
-                        " offset = %lx, len = %lx\n", offset, len);
+                        " offset = %lx, len = %x\n", offset, len);
         return X86EMUL_UNHANDLEABLE;
     }
 
xen/include/asm-x86/hvm/io.h +3 -3
index f2aaec5..d1d79dc 100644
@@ -32,11 +32,11 @@
 
 typedef int (*hvm_mmio_read_t)(struct vcpu *v,
                                unsigned long addr,
-                               unsigned long length,
+                               unsigned int length,
                                unsigned long *val);
 typedef int (*hvm_mmio_write_t)(struct vcpu *v,
                                 unsigned long addr,
-                                unsigned long length,
+                                unsigned int length,
                                 unsigned long val);
 typedef int (*hvm_mmio_check_t)(struct vcpu *v, unsigned long addr);
 
@@ -111,7 +111,7 @@ static inline void relocate_portio_handler(
 
 static inline void register_buffered_io_handler(
     struct domain *d, unsigned long addr,
-    unsigned long size, mmio_action_t action)
+    unsigned int size, mmio_action_t action)
 {
     register_io_handler(d, addr, size, action, HVM_BUFFERED_IO);
 }
Signature · 1 line
1.7.10.4
#11 lore raw

PATCH v5 4/16 x86/hvm: restrict port numbers to uint16_t and sizes to unsigned int

In reply to Paul Durrant

Building on the previous patch, this patch restricts portio port numbers to uint16_t in registration/relocate calls and portio_action_t. It also changes portio sizes to unsigned int which then allows the io_handler size field to reduce to an unsigned int.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/hvm.c       |    6 +++---
 xen/arch/x86/hvm/i8254.c     |    8 ++++----
 xen/arch/x86/hvm/intercept.c |    4 ++--
 xen/arch/x86/hvm/pmtimer.c   |    4 ++--
 xen/arch/x86/hvm/rtc.c       |    2 +-
 xen/arch/x86/hvm/stdvga.c    |    2 +-
 xen/arch/x86/hvm/vpic.c      |    4 ++--
 xen/include/asm-x86/hvm/io.h |   20 ++++++++++----------
 8 files changed, 25 insertions(+), 25 deletions(-)
xen/arch/x86/hvm/hvm.c +3 -3
index f0cf064..2bd7d45 100644
@@ -559,7 +559,7 @@ static int hvm_add_ioreq_gmfn(
 }
 
 static int hvm_print_line(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct domain *cd = current->domain;
     char c = *val;
@@ -585,7 +585,7 @@ static int hvm_print_line(
 }
 
 static int hvm_access_cf8(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct domain *d = current->domain;
 
@@ -597,7 +597,7 @@ static int hvm_access_cf8(
 }
 
 static int handle_pvh_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct domain *currd = current->domain;
 
xen/arch/x86/hvm/i8254.c +4 -4
index 36a0a53..5a47580 100644
@@ -50,9 +50,9 @@
 #define RW_STATE_WORD1 4
 
 static int handle_pit_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val);
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val);
 static int handle_speaker_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val);
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val);
 
 #define get_guest_time(v) \
    (is_hvm_vcpu(v) ? hvm_get_guest_time(v) : (u64)get_s_time())
@@ -479,7 +479,7 @@ void pit_deinit(struct domain *d)
 
 /* the intercept action for PIT DM retval:0--not handled; 1--handled */  
 static int handle_pit_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct PITState *vpit = vcpu_vpit(current);
 
@@ -522,7 +522,7 @@ static uint32_t speaker_ioport_read(
 }
 
 static int handle_speaker_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, uint32_t bytes, uint32_t *val)
 {
     struct PITState *vpit = vcpu_vpit(current);
 
xen/arch/x86/hvm/intercept.c +2 -2
index cc44733..52879ff 100644
@@ -369,7 +369,7 @@ int hvm_io_intercept(ioreq_t *p, int type)
 }
 
 void register_io_handler(
-    struct domain *d, unsigned long addr, unsigned long size,
+    struct domain *d, unsigned long addr, unsigned int size,
     void *action, int type)
 {
     struct hvm_io_handler *handler = d->arch.hvm_domain.io_handler;
@@ -386,7 +386,7 @@ void register_io_handler(
 
 void relocate_io_handler(
     struct domain *d, unsigned long old_addr, unsigned long new_addr,
-    unsigned long size, int type)
+    unsigned int size, int type)
 {
     struct hvm_io_handler *handler = d->arch.hvm_domain.io_handler;
     int i;
xen/arch/x86/hvm/pmtimer.c +2 -2
index 6ad2797..8b531d9 100644
@@ -142,7 +142,7 @@ static void pmt_timer_callback(void *opaque)
 
 /* Handle port I/O to the PM1a_STS and PM1a_EN registers */
 static int handle_evt_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct vcpu *v = current;
     PMTState *s = &v->domain->arch.hvm_domain.pl_time.vpmt;
@@ -205,7 +205,7 @@ static int handle_evt_io(
 
 /* Handle port I/O to the TMR_VAL register */
 static int handle_pmt_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct vcpu *v = current;
     PMTState *s = &v->domain->arch.hvm_domain.pl_time.vpmt;
xen/arch/x86/hvm/rtc.c +1 -1
index 3448971..fde120b 100644
@@ -697,7 +697,7 @@ static uint32_t rtc_ioport_read(RTCState *s, uint32_t addr)
 }
 
 static int handle_rtc_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct RTCState *vrtc = vcpu_vrtc(current);
 
xen/arch/x86/hvm/stdvga.c +1 -1
index 13d1029..98308cd 100644
@@ -173,7 +173,7 @@ static void stdvga_out(uint32_t port, uint32_t bytes, uint32_t val)
 }
 
 static int stdvga_intercept_pio(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct hvm_hw_stdvga *s = &current->domain->arch.hvm_domain.stdvga;
 
xen/arch/x86/hvm/vpic.c +2 -2
index 8eea061..909b9d0 100644
@@ -324,7 +324,7 @@ static uint32_t vpic_ioport_read(struct hvm_hw_vpic *vpic, uint32_t addr)
 }
 
 static int vpic_intercept_pic_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct hvm_hw_vpic *vpic;
 
@@ -346,7 +346,7 @@ static int vpic_intercept_pic_io(
 }
 
 static int vpic_intercept_elcr_io(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val)
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val)
 {
     struct hvm_hw_vpic *vpic;
     uint32_t data;
xen/include/asm-x86/hvm/io.h +10 -10
index d1d79dc..1ed0c13 100644
@@ -41,12 +41,12 @@ typedef int (*hvm_mmio_write_t)(struct vcpu *v,
 typedef int (*hvm_mmio_check_t)(struct vcpu *v, unsigned long addr);
 
 typedef int (*portio_action_t)(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val);
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val);
 typedef int (*mmio_action_t)(ioreq_t *);
 struct io_handler {
     int                 type;
     unsigned long       addr;
-    unsigned long       size;
+    unsigned int        size;
     union {
         portio_action_t portio;
         mmio_action_t   mmio;
@@ -75,11 +75,11 @@ extern const struct hvm_mmio_ops iommu_mmio_ops;
 
 int hvm_io_intercept(ioreq_t *p, int type);
 void register_io_handler(
-    struct domain *d, unsigned long addr, unsigned long size,
+    struct domain *d, unsigned long addr, unsigned int size,
     void *action, int type);
 void relocate_io_handler(
     struct domain *d, unsigned long old_addr, unsigned long new_addr,
-    unsigned long size, int type);
+    unsigned int size, int type);
 
 static inline int hvm_portio_intercept(ioreq_t *p)
 {
@@ -96,17 +96,17 @@ int hvm_mmio_intercept(ioreq_t *p);
 int hvm_buffered_io_send(ioreq_t *p);
 
 static inline void register_portio_handler(
-    struct domain *d, unsigned long addr,
-    unsigned long size, portio_action_t action)
+    struct domain *d, uint16_t port, unsigned int size,
+    portio_action_t action)
 {
-    register_io_handler(d, addr, size, action, HVM_PORTIO);
+    register_io_handler(d, port, size, action, HVM_PORTIO);
 }
 
 static inline void relocate_portio_handler(
-    struct domain *d, unsigned long old_addr, unsigned long new_addr,
-    unsigned long size)
+    struct domain *d, uint16_t old_port, uint16_t new_port,
+    unsigned int size)
 {
-    relocate_io_handler(d, old_addr, new_addr, size, HVM_PORTIO);
+    relocate_io_handler(d, old_port, new_port, size, HVM_PORTIO);
 }
 
 static inline void register_buffered_io_handler(
Signature · 1 line
1.7.10.4
#14 lore raw

PATCH v5 5/16 x86/hvm: unify internal portio and mmio intercepts

In reply to Paul Durrant

The implementation of mmio and portio intercepts is unnecessarily different. This leads to much code duplication. This patch unifies much of the intercept handling, leaving only distinct handlers for stdvga mmio and dpci portio. Subsequent patches will unify those handlers.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c                |   11 +-
 xen/arch/x86/hvm/hpet.c                   |    4 +-
 xen/arch/x86/hvm/hvm.c                    |    7 +-
 xen/arch/x86/hvm/intercept.c              |  486 +++++++++++++----------------
 xen/arch/x86/hvm/stdvga.c                 |   30 +-
 xen/arch/x86/hvm/vioapic.c                |    4 +-
 xen/arch/x86/hvm/vlapic.c                 |    5 +-
 xen/arch/x86/hvm/vmsi.c                   |    7 +-
 xen/drivers/passthrough/amd/iommu_guest.c |   30 +-
 xen/include/asm-x86/hvm/domain.h          |    1 +
 xen/include/asm-x86/hvm/io.h              |  115 +++----
 11 files changed, 334 insertions(+), 366 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -10
index b67f5db..205ca5e 100644
@@ -142,16 +142,7 @@ static int hvmemul_do_io(
         hvmtrace_io_assist(&p);
     }
 
-    if ( is_mmio )
-    {
-        rc = hvm_mmio_intercept(&p);
-        if ( rc == X86EMUL_UNHANDLEABLE )
-            rc = hvm_buffered_io_intercept(&p);
-    }
-    else
-    {
-        rc = hvm_portio_intercept(&p);
-    }
+    rc = hvm_io_intercept(&p);
 
     switch ( rc )
     {
xen/arch/x86/hvm/hpet.c +3 -1
index 30ac5dd..732504a 100644
@@ -504,7 +504,7 @@ static int hpet_range(struct vcpu *v, unsigned long addr)
              (addr < (HPET_BASE_ADDRESS + HPET_MMAP_SIZE)) );
 }
 
-const struct hvm_mmio_ops hpet_mmio_ops = {
+static const struct hvm_mmio_ops hpet_mmio_ops = {
     .check = hpet_range,
     .read  = hpet_read,
     .write = hpet_write
@@ -659,6 +659,8 @@ void hpet_init(struct domain *d)
         h->hpet.comparator64[i] = ~0ULL;
         h->pt[i].source = PTSRC_isa;
     }
+
+    register_mmio_handler(d, &hpet_mmio_ops);
 }
 
 void hpet_deinit(struct domain *d)
xen/arch/x86/hvm/hvm.c +5 -2
index 2bd7d45..60f8972 100644
@@ -1465,11 +1465,12 @@ int hvm_domain_initialise(struct domain *d)
         goto fail0;
 
     d->arch.hvm_domain.params = xzalloc_array(uint64_t, HVM_NR_PARAMS);
-    d->arch.hvm_domain.io_handler = xmalloc(struct hvm_io_handler);
+    d->arch.hvm_domain.io_handler = xzalloc_array(struct hvm_io_handler,
+                                                  NR_IO_HANDLERS);
     rc = -ENOMEM;
     if ( !d->arch.hvm_domain.params || !d->arch.hvm_domain.io_handler )
         goto fail1;
-    d->arch.hvm_domain.io_handler->num_slot = 0;
+    d->arch.hvm_domain.io_handler_count = 0;
 
     /* Set the default IO Bitmap. */
     if ( is_hardware_domain(d) )
@@ -1506,6 +1507,8 @@ int hvm_domain_initialise(struct domain *d)
 
     rtc_init(d);
 
+    msixtbl_init(d);
+
     register_portio_handler(d, 0xe9, 1, hvm_print_line);
     register_portio_handler(d, 0xcf8, 4, hvm_access_cf8);
 
xen/arch/x86/hvm/intercept.c +218 -268

561 lines not on this page · raw message

xen/arch/x86/hvm/stdvga.c +26 -4
index 98308cd..30760fa 100644
@@ -547,12 +547,27 @@ static int mmio_move(struct hvm_hw_stdvga *s, ioreq_t *p)
     return 1;
 }
 
-static int stdvga_intercept_mmio(ioreq_t *p)
+int stdvga_intercept_mmio(ioreq_t *p)
 {
     struct domain *d = current->domain;
     struct hvm_hw_stdvga *s = &d->arch.hvm_domain.stdvga;
+    uint64_t start, end, count = p->count, size = p->size;
     int buf = 0, rc;
 
+    if ( p->df )
+    {
+        start = (p->addr - (count - 1) * size);
+        end = p->addr + size;
+    }
+    else
+    {
+        start = p->addr;
+        end = p->addr + count * size;
+    }
+
+    if ( (start < VGA_MEM_BASE) || (end > (VGA_MEM_BASE + VGA_MEM_SIZE)) )
+        return X86EMUL_UNHANDLEABLE;
+
     if ( p->size > 8 )
     {
         gdprintk(XENLOG_WARNING, "invalid mmio size %d\n", (int)p->size);
@@ -619,9 +634,6 @@ void stdvga_init(struct domain *d)
         register_portio_handler(d, 0x3c4, 2, stdvga_intercept_pio);
         /* Graphics registers. */
         register_portio_handler(d, 0x3ce, 2, stdvga_intercept_pio);
-        /* MMIO. */
-        register_buffered_io_handler(
-            d, VGA_MEM_BASE, VGA_MEM_SIZE, stdvga_intercept_mmio);
     }
 }
 
@@ -638,3 +650,13 @@ void stdvga_deinit(struct domain *d)
         s->vram_page[i] = NULL;
     }
 }
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/arch/x86/hvm/vioapic.c +3 -1
index 5c8d890..9de2ff3 100644
@@ -250,7 +250,7 @@ static int vioapic_range(struct vcpu *v, unsigned long addr)
              (addr < vioapic->base_address + VIOAPIC_MEM_LENGTH)));
 }
 
-const struct hvm_mmio_ops vioapic_mmio_ops = {
+static const struct hvm_mmio_ops vioapic_mmio_ops = {
     .check = vioapic_range,
     .read = vioapic_read,
     .write = vioapic_write
@@ -456,6 +456,8 @@ int vioapic_init(struct domain *d)
     d->arch.hvm_domain.vioapic->domain = d;
     vioapic_reset(d);
 
+    register_mmio_handler(d, &vioapic_mmio_ops);
+
     return 0;
 }
 
xen/arch/x86/hvm/vlapic.c +4 -1
index 0235cb3..5472f79 100644
@@ -977,7 +977,7 @@ static int vlapic_range(struct vcpu *v, unsigned long addr)
            (offset < PAGE_SIZE);
 }
 
-const struct hvm_mmio_ops vlapic_mmio_ops = {
+static const struct hvm_mmio_ops vlapic_mmio_ops = {
     .check = vlapic_range,
     .read = vlapic_read,
     .write = vlapic_write
@@ -1443,6 +1443,9 @@ int vlapic_init(struct vcpu *v)
                  vlapic_init_sipi_action,
                  (unsigned long)v);
 
+    if ( v->vcpu_id == 0 )
+        register_mmio_handler(v->domain, &vlapic_mmio_ops);
+
     return 0;
 }
 
xen/arch/x86/hvm/vmsi.c +6 -1
index 6eeae0a..bf6090e 100644
@@ -344,7 +344,7 @@ static int msixtbl_range(struct vcpu *v, unsigned long addr)
     return !!desc;
 }
 
-const struct hvm_mmio_ops msixtbl_mmio_ops = {
+static const struct hvm_mmio_ops msixtbl_mmio_ops = {
     .check = msixtbl_range,
     .read = msixtbl_read,
     .write = msixtbl_write
@@ -481,6 +481,11 @@ found:
     spin_unlock_irq(&irq_desc->lock);
 }
 
+void msixtbl_init(struct domain *d)
+{
+    register_mmio_handler(d, &msixtbl_mmio_ops);
+}
+
 void msixtbl_pt_cleanup(struct domain *d)
 {
     struct msixtbl_entry *entry, *temp;
xen/drivers/passthrough/amd/iommu_guest.c +16 -14
index a832fdb..5908bf3 100644
@@ -868,6 +868,20 @@ static void guest_iommu_reg_init(struct guest_iommu *iommu)
     iommu->reg_ext_feature.hi = upper;
 }
 
+static int guest_iommu_mmio_range(struct vcpu *v, unsigned long addr)
+{
+    struct guest_iommu *iommu = vcpu_iommu(v);
+
+    return iommu && addr >= iommu->mmio_base &&
+           addr < iommu->mmio_base + IOMMU_MMIO_SIZE;
+}
+
+static const struct hvm_mmio_ops iommu_mmio_ops = {
+    .check = guest_iommu_mmio_range,
+    .read = guest_iommu_mmio_read,
+    .write = guest_iommu_mmio_write
+};
+
 /* Domain specific initialization */
 int guest_iommu_init(struct domain* d)
 {
@@ -894,6 +908,8 @@ int guest_iommu_init(struct domain* d)
 
     spin_lock_init(&iommu->lock);
 
+    register_mmio_handler(d, &iommu_mmio_ops);
+
     return 0;
 }
 
@@ -910,17 +926,3 @@ void guest_iommu_destroy(struct domain *d)
 
     domain_hvm_iommu(d)->arch.g_iommu = NULL;
 }
-
-static int guest_iommu_mmio_range(struct vcpu *v, unsigned long addr)
-{
-    struct guest_iommu *iommu = vcpu_iommu(v);
-
-    return iommu && addr >= iommu->mmio_base &&
-           addr < iommu->mmio_base + IOMMU_MMIO_SIZE;
-}
-
-const struct hvm_mmio_ops iommu_mmio_ops = {
-    .check = guest_iommu_mmio_range,
-    .read = guest_iommu_mmio_read,
-    .write = guest_iommu_mmio_write
-};
xen/include/asm-x86/hvm/domain.h +1 -0
index ad68fcf..b612755 100644
@@ -94,6 +94,7 @@ struct hvm_domain {
     struct pl_time         pl_time;
 
     struct hvm_io_handler *io_handler;
+    unsigned int          io_handler_count;
 
     /* Lock protects access to irq, vpic and vioapic. */
     spinlock_t             irq_lock;
xen/include/asm-x86/hvm/io.h +51 -64

157 lines not on this page · raw message

Signature · 1 line
1.7.10.4
#22 lore raw

PATCH v5 7/16 x86/hvm: unify dpci portio intercept with standard portio intercept

In reply to Paul Durrant

This patch re-works the dpci portio intercepts so that they can be unified with standard portio handling thereby removing a substantial amount of code duplication.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/hvm.c         |    2 +
 xen/arch/x86/hvm/intercept.c   |   22 ++--
 xen/arch/x86/hvm/io.c          |  220 ++++++++++++----------------------------
 xen/include/asm-x86/hvm/io.h   |    5 +
 xen/include/asm-x86/hvm/vcpu.h |    2 +
 xen/include/xen/iommu.h        |    1 -
 6 files changed, 80 insertions(+), 172 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -0
index 60f8972..6643e0c 100644
@@ -1486,6 +1486,8 @@ int hvm_domain_initialise(struct domain *d)
     else
         d->arch.hvm_domain.io_bitmap = hvm_io_bitmap;
 
+    register_dpci_portio_handler(d);
+
     if ( is_pvh_domain(d) )
     {
         register_portio_handler(d, 0, 0x10003, handle_pvh_io);
xen/arch/x86/hvm/intercept.c +6 -16
index 42050f4..a613865 100644
@@ -124,10 +124,7 @@ static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
 {
     struct vcpu *curr = current;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
-    const struct hvm_io_ops *ops =
-        (p->type == IOREQ_TYPE_COPY) ?
-        &mmio_ops :
-        &portio_ops;
+    const struct hvm_io_ops *ops = handler->ops;
     int rc = X86EMUL_OKAY, i, step = p->df ? -p->size : p->size;
     uint64_t data;
     uint64_t addr;
@@ -247,10 +244,6 @@ static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
 static const struct hvm_io_handler *hvm_find_io_handler(ioreq_t *p)
 {
     struct domain *curr_d = current->domain;
-    const struct hvm_io_ops *ops =
-        (p->type == IOREQ_TYPE_COPY) ?
-        &mmio_ops :
-        &portio_ops;
     unsigned int i;
 
     BUG_ON((p->type != IOREQ_TYPE_PIO) &&
@@ -260,6 +253,7 @@ static const struct hvm_io_handler *hvm_find_io_handler(ioreq_t *p)
     {
         const struct hvm_io_handler *handler =
             &curr_d->arch.hvm_domain.io_handler[i];
+        const struct hvm_io_ops *ops = handler->ops;
 
         if ( handler->type != p->type )
             continue;
@@ -275,13 +269,7 @@ int hvm_io_intercept(ioreq_t *p)
 {
     const struct hvm_io_handler *handler;
 
-    if ( p->type == IOREQ_TYPE_PIO )
-    {
-        int rc = dpci_ioport_intercept(p);
-        if ( (rc == X86EMUL_OKAY) || (rc == X86EMUL_RETRY) )
-            return rc;
-    }
-    else if ( p->type == IOREQ_TYPE_COPY )
+    if ( p->type == IOREQ_TYPE_COPY )
     {
         int rc = stdvga_intercept_mmio(p);
         if ( (rc == X86EMUL_OKAY) || (rc == X86EMUL_RETRY) )
@@ -296,7 +284,7 @@ int hvm_io_intercept(ioreq_t *p)
     return hvm_process_io_intercept(handler, p);
 }
 
-static struct hvm_io_handler *hvm_next_io_handler(struct domain *d)
+struct hvm_io_handler *hvm_next_io_handler(struct domain *d)
 {
     unsigned int i = d->arch.hvm_domain.io_handler_count++;
 
@@ -315,6 +303,7 @@ void register_mmio_handler(struct domain *d,
     struct hvm_io_handler *handler = hvm_next_io_handler(d);
 
     handler->type = IOREQ_TYPE_COPY;
+    handler->ops = &mmio_ops;
     handler->mmio.ops = ops;
 }
 
@@ -324,6 +313,7 @@ void register_portio_handler(struct domain *d, uint16_t port,
     struct hvm_io_handler *handler = hvm_next_io_handler(d);
 
     handler->type = IOREQ_TYPE_PIO;
+    handler->ops = &portio_ops;
     handler->portio.start = port;
     handler->portio.end = port + size;
     handler->portio.action = action;
xen/arch/x86/hvm/io.c +65 -155

250 lines not on this page · raw message

xen/include/asm-x86/hvm/io.h +5 -0
index 78c9704..740592f 100644
@@ -72,6 +72,7 @@ struct hvm_io_handler {
             portio_action_t action;
         } portio;
     };
+    const struct hvm_io_ops *ops;
     uint8_t type;
 };
 
@@ -94,6 +95,7 @@ struct hvm_io_ops {
 int hvm_io_intercept(ioreq_t *p);
 
 bool_t hvm_mmio_internal(paddr_t gpa);
+struct hvm_io_handler *hvm_next_io_handler(struct domain *d);
 
 void register_mmio_handler(struct domain *d,
                            const struct hvm_mmio_ops *ops);
@@ -137,6 +139,9 @@ int stdvga_intercept_mmio(ioreq_t *p);
 void stdvga_deinit(struct domain *d);
 
 extern void hvm_dpci_msi_eoi(struct domain *d, int vector);
+
+void register_dpci_portio_handler(struct domain *d);
+
 #endif /* __ASM_X86_HVM_IO_H__ */
 
 
xen/include/asm-x86/hvm/vcpu.h +2 -0
index 3d8f4dc..b15c1e6 100644
@@ -77,6 +77,8 @@ struct hvm_vcpu_io {
     bool_t mmio_retry, mmio_retrying;
 
     unsigned long msix_unmask_address;
+
+    const struct g2m_ioport *g2m_ioport;
 };
 
 #define VMCX_EADDR    (~0ULL)
xen/include/xen/iommu.h +0 -1
index b30bf41..1d00696 100644
@@ -93,7 +93,6 @@ void pt_pci_init(void);
 
 struct pirq;
 int hvm_do_IRQ_dpci(struct domain *, struct pirq *);
-int dpci_ioport_intercept(ioreq_t *p);
 int pt_irq_create_bind(struct domain *, xen_domctl_bind_pt_irq_t *);
 int pt_irq_destroy_bind(struct domain *, xen_domctl_bind_pt_irq_t *);
 
Signature · 1 line
1.7.10.4
#5 lore raw

PATCH v5 2/16 x86/hvm: remove multiple open coded 'chunking' loops

In reply to Paul Durrant

...in hvmemul_read/write()

Add hvmemul_phys_mmio_access() and hvmemul_linear_mmio_access() functions to reduce code duplication.

NOTE: This patch also introduces a change in 'chunking' around a page
boundary. Previously (for example) an 8 byte access at the last byte of a page would get carried out as 8 single-byte accesses. It will now be carried out as a single-byte access, followed by a 4-byte access, a 2-byte access and then another single-byte access.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c |  223 +++++++++++++++++++++++---------------------
 1 file changed, 116 insertions(+), 107 deletions(-)
xen/arch/x86/hvm/emulate.c +116 -107
index 8b60843..b67f5db 100644
@@ -539,6 +539,117 @@ static int hvmemul_virtual_to_linear(
     return X86EMUL_EXCEPTION;
 }
 
+static int hvmemul_phys_mmio_access(
+    paddr_t gpa, unsigned int size, uint8_t dir, uint8_t *buffer)
+{
+    unsigned long one_rep = 1;
+    unsigned int chunk;
+    int rc;
+
+    /* Accesses must fall within a page */
+    BUG_ON((gpa & (PAGE_SIZE - 1)) + size > PAGE_SIZE);
+
+    /*
+     * hvmemul_do_io() cannot handle non-power-of-2 accesses or
+     * accesses larger than sizeof(long), so choose the highest power
+     * of 2 not exceeding sizeof(long) as the 'chunk' size.
+     */
+    chunk = 1 << (fls(size) - 1);
+    if ( chunk > sizeof (long) )
+        chunk = sizeof (long);
+
+    for ( ;; )
+    {
+        rc = hvmemul_do_mmio_buffer(gpa, &one_rep, chunk, dir, 0,
+                                    buffer);
+        if ( rc != X86EMUL_OKAY )
+            break;
+
+        /* Advance to the next chunk */
+        gpa += chunk;
+        buffer += chunk;
+        size -= chunk;
+
+        if ( size == 0 )
+            break;
+
+        /*
+         * If the chunk now exceeds the remaining size, choose the next
+         * lowest power of 2 that will fit.
+         */
+        while ( chunk > size )
+            chunk >>= 1;
+    }
+
+    return rc;
+}
+
+static int hvmemul_linear_mmio_access(
+    unsigned long gla, unsigned int size, uint8_t dir, uint8_t *buffer,
+    uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt, bool_t known_gpfn)
+{
+    struct hvm_vcpu_io *vio = &current->arch.hvm_vcpu.hvm_io;
+    unsigned long page_off = gla & (PAGE_SIZE - 1);
+    unsigned int chunk;
+    paddr_t gpa;
+    unsigned long one_rep = 1;
+    int rc;
+
+    chunk = min_t(unsigned int, size, PAGE_SIZE - page_off);
+
+    if ( known_gpfn )
+        gpa = pfn_to_paddr(vio->mmio_gpfn) | page_off;
+    else
+    {
+        rc = hvmemul_linear_to_phys(gla, &gpa, chunk, &one_rep, pfec,
+                                    hvmemul_ctxt);
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+    }
+
+    for ( ;; )
+    {
+        rc = hvmemul_phys_mmio_access(gpa, chunk, dir, buffer);
+        if ( rc != X86EMUL_OKAY )
+            break;
+
+        gla += chunk;
+        buffer += chunk;
+        size -= chunk;
+
+        if ( size == 0 )
+            break;
+
+        ASSERT((gla & (PAGE_SIZE - 1)) == 0);
+        ASSERT(size < PAGE_SIZE);
+        chunk = size;
+        rc = hvmemul_linear_to_phys(gla, &gpa, chunk, &one_rep, pfec,
+                                    hvmemul_ctxt);
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+    }
+
+    return rc;
+}
+
+static inline int hvmemul_linear_mmio_read(
+    unsigned long gla, unsigned int size, void *buffer,
+    uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt,
+    bool_t translate)
+{
+    return hvmemul_linear_mmio_access(gla, size, IOREQ_READ, buffer,
+                                      pfec, hvmemul_ctxt, translate);
+}
+
+static inline int hvmemul_linear_mmio_write(
+    unsigned long gla, unsigned int size, void *buffer,
+    uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt,
+    bool_t translate)
+{
+    return hvmemul_linear_mmio_access(gla, size, IOREQ_WRITE, buffer,
+                                      pfec, hvmemul_ctxt, translate);
+}
+
 static int __hvmemul_read(
     enum x86_segment seg,
     unsigned long offset,
@@ -549,51 +660,19 @@ static int __hvmemul_read(
 {
     struct vcpu *curr = current;
     unsigned long addr, reps = 1;
-    unsigned int off, chunk = min(bytes, 1U << LONG_BYTEORDER);
     uint32_t pfec = PFEC_page_present;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
-    paddr_t gpa;
     int rc;
 
     rc = hvmemul_virtual_to_linear(
         seg, offset, bytes, &reps, access_type, hvmemul_ctxt, &addr);
     if ( rc != X86EMUL_OKAY )
         return rc;
-    off = addr & (PAGE_SIZE - 1);
-    /*
-     * We only need to handle sizes actual instruction operands can have. All
-     * such sizes are either powers of 2 or the sum of two powers of 2. Thus
-     * picking as initial chunk size the largest power of 2 not greater than
-     * the total size will always result in only power-of-2 size requests
-     * issued to hvmemul_do_mmio() (hvmemul_do_io() rejects non-powers-of-2).
-     */
-    while ( chunk & (chunk - 1) )
-        chunk &= chunk - 1;
-    if ( off + bytes > PAGE_SIZE )
-        while ( off & (chunk - 1) )
-            chunk >>= 1;
-
     if ( ((access_type != hvm_access_insn_fetch
            ? vio->mmio_access.read_access
            : vio->mmio_access.insn_fetch)) &&
          (vio->mmio_gva == (addr & PAGE_MASK)) )
-    {
-        gpa = (((paddr_t)vio->mmio_gpfn << PAGE_SHIFT) | off);
-        while ( (off + chunk) <= PAGE_SIZE )
-        {
-            rc = hvmemul_do_mmio_buffer(gpa, &reps, chunk, IOREQ_READ, 0,
-                                        p_data);
-            if ( rc != X86EMUL_OKAY || bytes == chunk )
-                return rc;
-            addr += chunk;
-            off += chunk;
-            gpa += chunk;
-            p_data += chunk;
-            bytes -= chunk;
-            if ( bytes < chunk )
-                chunk = bytes;
-        }
-    }
+        return hvmemul_linear_mmio_read(addr, bytes, p_data, pfec, hvmemul_ctxt, 1);
 
     if ( (seg != x86_seg_none) &&
          (hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3) )
@@ -612,30 +691,8 @@ static int __hvmemul_read(
     case HVMCOPY_bad_gfn_to_mfn:
         if ( access_type == hvm_access_insn_fetch )
             return X86EMUL_UNHANDLEABLE;
-        rc = hvmemul_linear_to_phys(addr, &gpa, chunk, &reps, pfec,
-                                    hvmemul_ctxt);
-        while ( rc == X86EMUL_OKAY )
-        {
-            rc = hvmemul_do_mmio_buffer(gpa, &reps, chunk, IOREQ_READ, 0,
-                                        p_data);
-            if ( rc != X86EMUL_OKAY || bytes == chunk )
-                break;
-            addr += chunk;
-            off += chunk;
-            p_data += chunk;
-            bytes -= chunk;
-            if ( bytes < chunk )
-                chunk = bytes;
-            if ( off < PAGE_SIZE )
-                gpa += chunk;
-            else
-            {
-                rc = hvmemul_linear_to_phys(addr, &gpa, chunk, &reps, pfec,
-                                            hvmemul_ctxt);
-                off = 0;
-            }
-        }
-        return rc;
+
+        return hvmemul_linear_mmio_read(addr, bytes, p_data, pfec, hvmemul_ctxt, 0);
     case HVMCOPY_gfn_paged_out:
     case HVMCOPY_gfn_shared:
         return X86EMUL_RETRY;
@@ -701,43 +758,18 @@ static int hvmemul_write(
         container_of(ctxt, struct hvm_emulate_ctxt, ctxt);
     struct vcpu *curr = current;
     unsigned long addr, reps = 1;
-    unsigned int off, chunk = min(bytes, 1U << LONG_BYTEORDER);
     uint32_t pfec = PFEC_page_present | PFEC_write_access;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
-    paddr_t gpa;
     int rc;
 
     rc = hvmemul_virtual_to_linear(
         seg, offset, bytes, &reps, hvm_access_write, hvmemul_ctxt, &addr);
     if ( rc != X86EMUL_OKAY )
         return rc;
-    off = addr & (PAGE_SIZE - 1);
-    /* See the respective comment in __hvmemul_read(). */
-    while ( chunk & (chunk - 1) )
-        chunk &= chunk - 1;
-    if ( off + bytes > PAGE_SIZE )
-        while ( off & (chunk - 1) )
-            chunk >>= 1;
 
     if ( vio->mmio_access.write_access &&
          (vio->mmio_gva == (addr & PAGE_MASK)) )
-    {
-        gpa = (((paddr_t)vio->mmio_gpfn << PAGE_SHIFT) | off);
-        while ( (off + chunk) <= PAGE_SIZE )
-        {
-            rc = hvmemul_do_mmio_buffer(gpa, &reps, chunk, IOREQ_WRITE, 0,
-                                        p_data);
-            if ( rc != X86EMUL_OKAY || bytes == chunk )
-                return rc;
-            addr += chunk;
-            off += chunk;
-            gpa += chunk;
-            p_data += chunk;
-            bytes -= chunk;
-            if ( bytes < chunk )
-                chunk = bytes;
-        }
-    }
+        return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 1);
 
     if ( (seg != x86_seg_none) &&
          (hvmemul_ctxt->seg_reg[x86_seg_ss].attr.fields.dpl == 3) )
@@ -752,30 +784,7 @@ static int hvmemul_write(
     case HVMCOPY_bad_gva_to_gfn:
         return X86EMUL_EXCEPTION;
     case HVMCOPY_bad_gfn_to_mfn:
-        rc = hvmemul_linear_to_phys(addr, &gpa, chunk, &reps, pfec,
-                                    hvmemul_ctxt);
-        while ( rc == X86EMUL_OKAY )
-        {
-            rc = hvmemul_do_mmio_buffer(gpa, &reps, chunk, IOREQ_WRITE, 0,
-                                        p_data);
-            if ( rc != X86EMUL_OKAY || bytes == chunk )
-                break;
-            addr += chunk;
-            off += chunk;
-            p_data += chunk;
-            bytes -= chunk;
-            if ( bytes < chunk )
-                chunk = bytes;
-            if ( off < PAGE_SIZE )
-                gpa += chunk;
-            else
-            {
-                rc = hvmemul_linear_to_phys(addr, &gpa, chunk, &reps, pfec,
-                                            hvmemul_ctxt);
-                off = 0;
-            }
-        }
-        return rc;
+        return hvmemul_linear_mmio_write(addr, bytes, p_data, pfec, hvmemul_ctxt, 0);
     case HVMCOPY_gfn_paged_out:
     case HVMCOPY_gfn_shared:
         return X86EMUL_RETRY;
Signature · 1 line
1.7.10.4
#26 lore raw

PATCH v5 9/16 x86/hvm: limit reps to avoid the need to handle retry

In reply to Paul Durrant

By limiting hvmemul_do_io_addr() to reps falling within the page on which a reference has already been taken, we can guarantee that calls to hvm_copy_to/from_guest_phys() will not hit the HVMCOPY_gfn_paged_out or HVMCOPY_gfn_shared cases. Thus we can remove the retry logic (added by c/s 82ed8716b "fix direct PCI port I/O emulation retry and error handling") from the intercept code and simplify it significantly.

Normally hvmemul_do_io_addr() will only reference single page at a time. It will, however, take an extra page reference for I/O spanning a page boundary.

It is still important to know, upon returning from x86_emulate(), whether the number of reps was reduced so the mmio_retry flag is retained for that purpose.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c     |   73 +++++++++++++++++++++++++---------------
 xen/arch/x86/hvm/hvm.c         |    4 +++
 xen/arch/x86/hvm/intercept.c   |   57 +++++++------------------------
 xen/include/asm-x86/hvm/vcpu.h |    2 +-
 4 files changed, 63 insertions(+), 73 deletions(-)
xen/arch/x86/hvm/emulate.c +46 -27
index 72603f5..83b4e5f 100644
@@ -51,7 +51,7 @@ static void hvmtrace_io_assist(const ioreq_t *p)
 }
 
 static int hvmemul_do_io(
-    bool_t is_mmio, paddr_t addr, unsigned long *reps, unsigned int size,
+    bool_t is_mmio, paddr_t addr, unsigned long reps, unsigned int size,
     uint8_t dir, bool_t df, bool_t data_is_addr, uintptr_t data)
 {
     struct vcpu *curr = current;
@@ -60,6 +60,7 @@ static int hvmemul_do_io(
         .type = is_mmio ? IOREQ_TYPE_COPY : IOREQ_TYPE_PIO,
         .addr = addr,
         .size = size,
+        .count = reps,
         .dir = dir,
         .df = df,
         .data = data,
@@ -125,15 +126,6 @@ static int hvmemul_do_io(
         HVMIO_dispatched : HVMIO_awaiting_completion;
     vio->io_size = size;
 
-    /*
-     * When retrying a repeated string instruction, force exit to guest after
-     * completion of the retried iteration to allow handling of interrupts.
-     */
-    if ( vio->mmio_retrying )
-        *reps = 1;
-
-    p.count = *reps;
-
     if ( dir == IOREQ_WRITE )
     {
         if ( !data_is_addr )
@@ -147,17 +139,9 @@ static int hvmemul_do_io(
     switch ( rc )
     {
     case X86EMUL_OKAY:
-    case X86EMUL_RETRY:
-        *reps = p.count;
         p.state = STATE_IORESP_READY;
-        if ( !vio->mmio_retry )
-        {
-            hvm_io_assist(&p);
-            vio->io_state = HVMIO_none;
-        }
-        else
-            /* Defer hvm_io_assist() invocation to hvm_do_resume(). */
-            vio->io_state = HVMIO_handle_mmio_awaiting_completion;
+        hvm_io_assist(&p);
+        vio->io_state = HVMIO_none;
         break;
     case X86EMUL_UNHANDLEABLE:
     {
@@ -235,7 +219,7 @@ static int hvmemul_do_io_buffer(
 
     BUG_ON(buffer == NULL);
 
-    rc = hvmemul_do_io(is_mmio, addr, reps, size, dir, df, 0,
+    rc = hvmemul_do_io(is_mmio, addr, *reps, size, dir, df, 0,
                        (uintptr_t)buffer);
     if ( rc == X86EMUL_UNHANDLEABLE && dir == IOREQ_READ )
         memset(buffer, 0xff, size);
@@ -287,17 +271,53 @@ static int hvmemul_do_io_addr(
     bool_t is_mmio, paddr_t addr, unsigned long *reps,
     unsigned int size, uint8_t dir, bool_t df, paddr_t ram_gpa)
 {
-    struct page_info *ram_page;
+    struct vcpu *v = current;
+    unsigned long ram_gmfn = paddr_to_pfn(ram_gpa);
+    unsigned int page_off = ram_gpa & (PAGE_SIZE - 1);
+    struct page_info *ram_page[2];
+    int nr_pages = 0;
+    unsigned long count;
     int rc;
 
-    rc = hvmemul_acquire_page(paddr_to_pfn(ram_gpa), &ram_page);
+    rc = hvmemul_acquire_page(ram_gmfn, &ram_page[nr_pages]);
     if ( rc != X86EMUL_OKAY )
-        return rc;
+        goto out;
 
-    rc = hvmemul_do_io(is_mmio, addr, reps, size, dir, df, 1,
+    nr_pages++;
+
+    /* Detemine how many reps will fit within this page */
+    count = min_t(unsigned long,
+                  *reps,
+                  df ?
+                  (page_off + size - 1) / size :
+                  (PAGE_SIZE - page_off) / size);
+
+    if ( count == 0 )
+    {
+        /*
+         * This access must span two pages, so grab a reference to
+         * the next page and do a single rep.
+         */
+        rc = hvmemul_acquire_page(df ? ram_gmfn - 1 : ram_gmfn + 1,
+                                  &ram_page[nr_pages]);
+        if ( rc != X86EMUL_OKAY )
+            goto out;
+
+        nr_pages++;
+        count = 1;
+    }
+
+    rc = hvmemul_do_io(is_mmio, addr, count, size, dir, df, 1,
                        ram_gpa);
+    if ( rc == X86EMUL_OKAY )
+    {
+        v->arch.hvm_vcpu.hvm_io.mmio_retry = (count < *reps);
+        *reps = count;
+    }
 
-    hvmemul_release_page(ram_page);
+ out:
+    while ( --nr_pages >= 0 )
+        hvmemul_release_page(ram_page[nr_pages]);
 
     return rc;
 }
@@ -1547,7 +1567,6 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
     }
 
     hvmemul_ctxt->exn_pending = 0;
-    vio->mmio_retrying = vio->mmio_retry;
     vio->mmio_retry = 0;
 
     if ( cpu_has_vmx )
xen/arch/x86/hvm/hvm.c +4 -0
index 6643e0c..a7de030 100644
@@ -440,6 +440,7 @@ static bool_t hvm_wait_for_io(struct hvm_ioreq_vcpu *sv, ioreq_t *p)
 
 void hvm_do_resume(struct vcpu *v)
 {
+    struct hvm_vcpu_io *vio = &v->arch.hvm_vcpu.hvm_io;
     struct domain *d = v->domain;
     struct hvm_ioreq_server *s;
 
@@ -468,6 +469,9 @@ void hvm_do_resume(struct vcpu *v)
         }
     }
 
+    if ( vio->mmio_retry )
+        (void)handle_mmio();
+
     /* Inject pending hw/sw trap */
     if ( v->arch.hvm_vcpu.inject_trap.vector != -1 ) 
     {
xen/arch/x86/hvm/intercept.c +12 -45

103 lines not on this page · raw message

xen/include/asm-x86/hvm/vcpu.h +1 -1
index b15c1e6..2ed0606 100644
@@ -74,7 +74,7 @@ struct hvm_vcpu_io {
      * For string instruction emulation we need to be able to signal a
      * necessary retry through other than function return codes.
      */
-    bool_t mmio_retry, mmio_retrying;
+    bool_t mmio_retry;
 
     unsigned long msix_unmask_address;
 
Signature · 1 line
1.7.10.4
#42 lore raw

PATCH v5 16/16 x86/hvm: track large memory mapped accesses by buffer offset

In reply to Paul Durrant

The code in hvmemul_do_io() that tracks large reads or writes, to avoid re-issue of component I/O, is defeated by accesses across a page boundary because it uses physical address. The code is also only relevant to memory mapped I/O to or from a buffer.

This patch re-factors the code and moves it into hvmemul_phys_mmio_access() where it is relevant and tracks using buffer offset rather than address. Separate I/O emulations (of which there may be up to three per instruction) are distinguished by linear address.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c     |  122 ++++++++++++++++++++--------------------
 xen/include/asm-x86/hvm/vcpu.h |   25 +++++---
 2 files changed, 78 insertions(+), 69 deletions(-)
xen/arch/x86/hvm/emulate.c +60 -62
index 8bb56a2..d83d21c 100644
@@ -106,29 +106,6 @@ static int hvmemul_do_io(
         return X86EMUL_UNHANDLEABLE;
     }
 
-    if ( is_mmio && !data_is_addr )
-    {
-        /* Part of a multi-cycle read or write? */
-        if ( dir == IOREQ_WRITE )
-        {
-            paddr_t pa = vio->mmio_large_write_pa;
-            unsigned int bytes = vio->mmio_large_write_bytes;
-            if ( (addr >= pa) && ((addr + size) <= (pa + bytes)) )
-                return X86EMUL_OKAY;
-        }
-        else
-        {
-            paddr_t pa = vio->mmio_large_read_pa;
-            unsigned int bytes = vio->mmio_large_read_bytes;
-            if ( (addr >= pa) && ((addr + size) <= (pa + bytes)) )
-            {
-                memcpy(p_data, &vio->mmio_large_read[addr - pa],
-                       size);
-                return X86EMUL_OKAY;
-            }
-        }
-    }
-
     switch ( vio->io_req.state )
     {
     case STATE_IOREQ_NONE:
@@ -208,33 +185,6 @@ static int hvmemul_do_io(
             memcpy(p_data, &p.data, size);
     }
 
-    if ( is_mmio && !data_is_addr )
-    {
-        /* Part of a multi-cycle read or write? */
-        if ( dir == IOREQ_WRITE )
-        {
-            paddr_t pa = vio->mmio_large_write_pa;
-            unsigned int bytes = vio->mmio_large_write_bytes;
-            if ( bytes == 0 )
-                pa = vio->mmio_large_write_pa = addr;
-            if ( addr == (pa + bytes) )
-                vio->mmio_large_write_bytes += size;
-        }
-        else
-        {
-            paddr_t pa = vio->mmio_large_read_pa;
-            unsigned int bytes = vio->mmio_large_read_bytes;
-            if ( bytes == 0 )
-                pa = vio->mmio_large_read_pa = addr;
-            if ( (addr == (pa + bytes)) &&
-                 ((bytes + size) <= sizeof(vio->mmio_large_read)) )
-            {
-                memcpy(&vio->mmio_large_read[bytes], p_data, size);
-                vio->mmio_large_read_bytes += size;
-            }
-        }
-    }
-
     return X86EMUL_OKAY;
 }
 
@@ -587,11 +537,12 @@ static int hvmemul_virtual_to_linear(
 }
 
 static int hvmemul_phys_mmio_access(
-    paddr_t gpa, unsigned int size, uint8_t dir, uint8_t *buffer)
+    struct hvm_mmio_cache *cache, paddr_t gpa, unsigned int size, uint8_t dir,
+    uint8_t *buffer, unsigned int off)
 {
     unsigned long one_rep = 1;
     unsigned int chunk;
-    int rc;
+    int rc = X86EMUL_OKAY;
 
     /* Accesses must fall within a page */
     BUG_ON((gpa & (PAGE_SIZE - 1)) + size > PAGE_SIZE);
@@ -607,14 +558,33 @@ static int hvmemul_phys_mmio_access(
 
     for ( ;; )
     {
-        rc = hvmemul_do_mmio_buffer(gpa, &one_rep, chunk, dir, 0,
-                                    buffer);
-        if ( rc != X86EMUL_OKAY )
-            break;
+        /* Have we already done this chunk? */
+        if ( off < cache->size )
+        {
+            ASSERT((off + chunk) <= cache->size);
+
+            if ( dir == IOREQ_READ )
+                memcpy(&buffer[off], &cache->buffer[off], chunk);
+            else if ( memcmp(&buffer[off], &cache->buffer[off], chunk) != 0 )
+                domain_crash(current->domain);
+        }
+        else
+        {
+            ASSERT(off == cache->size);
+
+            rc = hvmemul_do_mmio_buffer(gpa, &one_rep, chunk, dir, 0,
+                                        &buffer[off]);
+            if ( rc != X86EMUL_OKAY )
+                break;
+
+            /* Note that we have now done this chunk. */
+            memcpy(&cache->buffer[off], &buffer[off], chunk);
+            cache->size += chunk;
+        }
 
         /* Advance to the next chunk */
         gpa += chunk;
-        buffer += chunk;
+        off += chunk;
         size -= chunk;
 
         if ( size == 0 )
@@ -631,13 +601,41 @@ static int hvmemul_phys_mmio_access(
     return rc;
 }
 
+static struct hvm_mmio_cache *hvmemul_find_mmio_cache(
+    struct hvm_vcpu_io *vio, unsigned long gla, uint8_t dir)
+{
+    unsigned int i;
+    struct hvm_mmio_cache *cache;
+
+    for ( i = 0; i < vio->mmio_cache_count; i ++ )
+    {
+        cache = &vio->mmio_cache[i];
+
+        if ( gla == cache->gla &&
+             dir == cache->dir )
+            return cache;
+    }
+
+    i = vio->mmio_cache_count++;
+    BUG_ON(i == ARRAY_SIZE(vio->mmio_cache));
+
+    cache = &vio->mmio_cache[i];
+    memset(cache, 0, sizeof (*cache));
+
+    cache->gla = gla;
+    cache->dir = dir;
+
+    return cache;
+}
+
 static int hvmemul_linear_mmio_access(
-    unsigned long gla, unsigned int size, uint8_t dir, uint8_t *buffer,
+    unsigned long gla, unsigned int size, uint8_t dir, void *buffer,
     uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt, bool_t known_gpfn)
 {
     struct hvm_vcpu_io *vio = &current->arch.hvm_vcpu.hvm_io;
     unsigned long page_off = gla & (PAGE_SIZE - 1);
-    unsigned int chunk;
+    struct hvm_mmio_cache *cache = hvmemul_find_mmio_cache(vio, gla, dir);
+    unsigned int chunk, buffer_off = 0;
     paddr_t gpa;
     unsigned long one_rep = 1;
     int rc;
@@ -656,12 +654,12 @@ static int hvmemul_linear_mmio_access(
 
     for ( ;; )
     {
-        rc = hvmemul_phys_mmio_access(gpa, chunk, dir, buffer);
+        rc = hvmemul_phys_mmio_access(cache, gpa, chunk, dir, buffer, buffer_off);
         if ( rc != X86EMUL_OKAY )
             break;
 
         gla += chunk;
-        buffer += chunk;
+        buffer_off += chunk;
         size -= chunk;
 
         if ( size == 0 )
@@ -1609,7 +1607,7 @@ static int _hvm_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt,
         rc = X86EMUL_RETRY;
     if ( rc != X86EMUL_RETRY )
     {
-        vio->mmio_large_read_bytes = vio->mmio_large_write_bytes = 0;
+        vio->mmio_cache_count = 0;
         vio->mmio_insn_bytes = 0;
     }
     else
xen/include/asm-x86/hvm/vcpu.h +18 -7
index 506ce89..90dea8d 100644
@@ -42,6 +42,17 @@ struct hvm_vcpu_asid {
     uint32_t asid;
 };
 
+/*
+ * We may read or write up to m256 as a number of device-model
+ * transactions.
+ */
+struct hvm_mmio_cache {
+    unsigned long size;
+    uint8_t buffer[32];
+    unsigned long gla;
+    uint8_t dir;
+};
+
 struct hvm_vcpu_io {
     /* I/O request in flight to device model. */
     ioreq_t                io_req;
@@ -57,13 +68,13 @@ struct hvm_vcpu_io {
     unsigned long       mmio_gva;
     unsigned long       mmio_gpfn;
 
-    /* We may read up to m256 as a number of device-model transactions. */
-    paddr_t mmio_large_read_pa;
-    uint8_t mmio_large_read[32];
-    unsigned int mmio_large_read_bytes;
-    /* We may write up to m256 as a number of device-model transactions. */
-    unsigned int mmio_large_write_bytes;
-    paddr_t mmio_large_write_pa;
+    /*
+     * We may need to handle up to 3 distinct memory accesses per
+     * instruction.
+     */
+    struct hvm_mmio_cache mmio_cache[3];
+    unsigned int mmio_cache_count;
+
     /* For retries we shouldn't re-fetch the instruction. */
     unsigned int mmio_insn_bytes;
     unsigned char mmio_insn[16];
Signature · 1 line
1.7.10.4
#30 lore raw

PATCH v5 10/16 x86/hvm: only call hvm_io_assist() from hvm_wait_for_io()

In reply to Paul Durrant

By removing the calls in hvmemul_do_io() (which is replaced by a single assignment) and hvm_complete_assist_request() (which is replaced by a call to hvm_process_portio_intercept() with a suitable set of ops) then hvm_io_assist() can be moved into hvm.c and made static (and hence be a candidate for inlining).

The calls to msix_write_completion() and vcpu_end_shutdown_deferral() are also made unconditionally because the ioreq state will always be STATE_IOREQ_NONE at the end of hvm_io_assist() so the test was pointless. These calls are also only relevant when the emulation has been handled externally which is now always the case.

---
 xen/arch/x86/hvm/emulate.c    |   34 ++++++++++++++++++---
 xen/arch/x86/hvm/hvm.c        |   67 ++++++++++++++++++++++-------------------
 xen/arch/x86/hvm/intercept.c  |    4 +--
 xen/arch/x86/hvm/io.c         |   39 ------------------------
 xen/include/asm-x86/hvm/hvm.h |    1 -
 xen/include/asm-x86/hvm/io.h  |    3 +-
 6 files changed, 70 insertions(+), 78 deletions(-)
xen/arch/x86/hvm/emulate.c +30 -4
index 83b4e5f..e17e6ab 100644
@@ -50,6 +50,32 @@ static void hvmtrace_io_assist(const ioreq_t *p)
     trace_var(event, 0/*!cycles*/, size, buffer);
 }
 
+static int null_read(const struct hvm_io_handler *io_handler,
+                     uint64_t addr,
+                     uint32_t size,
+                     uint64_t *data)
+{
+    *data = ~0ul;
+    return X86EMUL_OKAY;
+}
+
+static int null_write(const struct hvm_io_handler *handler,
+                      uint64_t addr,
+                      uint32_t size,
+                      uint64_t data)
+{
+    return X86EMUL_OKAY;
+}
+
+static const struct hvm_io_ops null_ops = {
+    .read = null_read,
+    .write = null_write
+};
+
+static const struct hvm_io_handler null_handler = {
+    .ops = &null_ops
+};
+
 static int hvmemul_do_io(
     bool_t is_mmio, paddr_t addr, unsigned long reps, unsigned int size,
     uint8_t dir, bool_t df, bool_t data_is_addr, uintptr_t data)
@@ -139,8 +165,7 @@ static int hvmemul_do_io(
     switch ( rc )
     {
     case X86EMUL_OKAY:
-        p.state = STATE_IORESP_READY;
-        hvm_io_assist(&p);
+        vio->io_data = p.data;
         vio->io_state = HVMIO_none;
         break;
     case X86EMUL_UNHANDLEABLE:
@@ -151,8 +176,9 @@ static int hvmemul_do_io(
         /* If there is no suitable backing DM, just ignore accesses */
         if ( !s )
         {
-            hvm_complete_assist_req(&p);
-            rc = X86EMUL_OKAY;
+            rc = hvm_process_io_intercept(&null_handler, &p);
+            if ( rc == X86EMUL_OKAY )
+                vio->io_data = p.data;
             vio->io_state = HVMIO_none;
         }
         else
xen/arch/x86/hvm/hvm.c +36 -31

79 lines not on this page · raw message

xen/arch/x86/hvm/intercept.c +2 -2
index 11b6028..f84f7c5 100644
@@ -119,8 +119,8 @@ static const struct hvm_io_ops portio_ops = {
     .write = hvm_portio_write
 };
 
-static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
-                                    ioreq_t *p)
+int hvm_process_io_intercept(const struct hvm_io_handler *handler,
+                             ioreq_t *p)
 {
     const struct hvm_io_ops *ops = handler->ops;
     int rc = X86EMUL_OKAY, i, step = p->df ? -p->size : p->size;
xen/arch/x86/hvm/io.c +0 -39
index 2c88ddb..fe099d8 100644
@@ -169,45 +169,6 @@ int handle_pio(uint16_t port, unsigned int size, int dir)
     return 1;
 }
 
-void hvm_io_assist(ioreq_t *p)
-{
-    struct vcpu *curr = current;
-    struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
-    enum hvm_io_state io_state;
-
-    p->state = STATE_IOREQ_NONE;
-
-    io_state = vio->io_state;
-    vio->io_state = HVMIO_none;
-
-    switch ( io_state )
-    {
-    case HVMIO_awaiting_completion:
-        vio->io_state = HVMIO_completed;
-        vio->io_data = p->data;
-        break;
-    case HVMIO_handle_mmio_awaiting_completion:
-        vio->io_state = HVMIO_completed;
-        vio->io_data = p->data;
-        (void)handle_mmio();
-        break;
-    case HVMIO_handle_pio_awaiting_completion:
-        if ( vio->io_size == 4 ) /* Needs zero extension. */
-            guest_cpu_user_regs()->rax = (uint32_t)p->data;
-        else
-            memcpy(&guest_cpu_user_regs()->rax, &p->data, vio->io_size);
-        break;
-    default:
-        break;
-    }
-
-    if ( p->state == STATE_IOREQ_NONE )
-    {
-        msix_write_completion(curr);
-        vcpu_end_shutdown_deferral(curr);
-    }
-}
-
 static bool_t dpci_portio_accept(const struct hvm_io_handler *handler,
                                  const ioreq_t *p)
 {
xen/include/asm-x86/hvm/hvm.h +0 -1
index 57f9605..9792e15 100644
@@ -232,7 +232,6 @@ struct hvm_ioreq_server *hvm_select_ioreq_server(struct domain *d,
                                                  ioreq_t *p);
 int hvm_send_assist_req(struct hvm_ioreq_server *s, ioreq_t *p);
 void hvm_broadcast_assist_req(ioreq_t *p);
-void hvm_complete_assist_req(ioreq_t *p);
 
 void hvm_get_guest_pat(struct vcpu *v, u64 *guest_pat);
 int hvm_set_guest_pat(struct vcpu *v, u64 guest_pat);
xen/include/asm-x86/hvm/io.h +2 -1
index 1ca0d16..87034af 100644
@@ -95,6 +95,8 @@ struct hvm_io_ops {
     hvm_io_complete_t complete;
 };
 
+int hvm_process_io_intercept(const struct hvm_io_handler *handler,
+                             ioreq_t *p);
 int hvm_io_intercept(ioreq_t *p);
 
 bool_t hvm_mmio_internal(paddr_t gpa);
@@ -119,7 +121,6 @@ int handle_mmio_with_translation(unsigned long gva, unsigned long gpfn,
                                  struct npfec);
 int handle_pio(uint16_t port, unsigned int size, int dir);
 void hvm_interrupt_post(struct vcpu *v, int vector, int type);
-void hvm_io_assist(ioreq_t *p);
 void hvm_dpci_eoi(struct domain *d, unsigned int guest_irq,
                   const union vioapic_redir_entry *ent);
 void msix_write_completion(struct vcpu *);
Signature · 1 line
1.7.10.4
#34 lore raw

PATCH v5 12/16 x86/hvm: remove HVMIO_dispatched I/O state

In reply to Paul Durrant

By removing the HVMIO_dispatched state and making all pending emulations (i.e. all those not handled by the hypervisor) use HVMIO_awating_completion, various code-paths can be simplified.

The completion case for HVMIO_dispatched can also be trivally removed from hvmemul_do_io() as it was already unreachable. This is because that state was only ever used for writes or I/O to/from a guest page and hvmemul_do_io() is never called to complete such I/O.

NOTE: There is one sublety in handle_pio()... The only case when
handle_pio() got a return code of X86EMUL_RETRY back from hvmemul_do_pio_buffer() and found io_state was not HVMIO_awaiting_completion was in the case where the domain is shutting down. This is because all writes normally yield a return of HVMEMUL_OKAY and all reads put io_state into HVMIO_awaiting_completion. Hence the io_state check there is replaced with a check of the is_shutting_down flag on the domain.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c      |   12 +++---------
 xen/arch/x86/hvm/hvm.c          |   12 +++---------
 xen/arch/x86/hvm/io.c           |   14 +++++++-------
 xen/arch/x86/hvm/vmx/realmode.c |    2 +-
 xen/include/asm-x86/hvm/vcpu.h  |   10 +++++++++-
 5 files changed, 23 insertions(+), 27 deletions(-)
xen/arch/x86/hvm/emulate.c +3 -9
index e17e6ab..a6c7f66 100644
@@ -137,20 +137,14 @@ static int hvmemul_do_io(
         if ( data_is_addr || dir == IOREQ_WRITE )
             return X86EMUL_UNHANDLEABLE;
         goto finish_access;
-    case HVMIO_dispatched:
-        /* May have to wait for previous cycle of a multi-write to complete. */
-        if ( is_mmio && !data_is_addr && (dir == IOREQ_WRITE) &&
-             (addr == (vio->mmio_large_write_pa +
-                       vio->mmio_large_write_bytes)) )
-            return X86EMUL_RETRY;
-        /* fallthrough */
     default:
         return X86EMUL_UNHANDLEABLE;
     }
 
-    vio->io_state = (data_is_addr || dir == IOREQ_WRITE) ?
-        HVMIO_dispatched : HVMIO_awaiting_completion;
+    vio->io_state = HVMIO_awaiting_completion;
     vio->io_size = size;
+    vio->io_dir = dir;
+    vio->io_data_is_addr = data_is_addr;
 
     if ( dir == IOREQ_WRITE )
     {
xen/arch/x86/hvm/hvm.c +3 -9
index 0f0730e..b94a9b6 100644
@@ -416,22 +416,16 @@ static void hvm_io_assist(ioreq_t *p)
 {
     struct vcpu *curr = current;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
-    enum hvm_io_state io_state;
 
     p->state = STATE_IOREQ_NONE;
 
-    io_state = vio->io_state;
-    vio->io_state = HVMIO_none;
-
-    switch ( io_state )
+    if ( hvm_vcpu_io_need_completion(vio) )
     {
-    case HVMIO_awaiting_completion:
         vio->io_state = HVMIO_completed;
         vio->io_data = p->data;
-        break;
-    default:
-        break;
     }
+    else
+        vio->io_state = HVMIO_none;
 
     msix_write_completion(curr);
     vcpu_end_shutdown_deferral(curr);
xen/arch/x86/hvm/io.c +7 -7
index 221d05e..3b51d59 100644
@@ -90,9 +90,7 @@ int handle_mmio(void)
 
     rc = hvm_emulate_one(&ctxt);
 
-    if ( rc != X86EMUL_RETRY )
-        vio->io_state = HVMIO_none;
-    if ( vio->io_state == HVMIO_awaiting_completion || vio->mmio_retry )
+    if ( hvm_vcpu_io_need_completion(vio) || vio->mmio_retry )
         vio->io_completion = HVMIO_mmio_completion;
     else
         vio->mmio_access = (struct npfec){};
@@ -142,6 +140,9 @@ int handle_pio(uint16_t port, unsigned int size, int dir)
 
     rc = hvmemul_do_pio_buffer(port, size, dir, &data);
 
+    if ( hvm_vcpu_io_need_completion(vio) )
+        vio->io_completion = HVMIO_pio_completion;
+
     switch ( rc )
     {
     case X86EMUL_OKAY:
@@ -154,11 +155,10 @@ int handle_pio(uint16_t port, unsigned int size, int dir)
         }
         break;
     case X86EMUL_RETRY:
-        if ( vio->io_state != HVMIO_awaiting_completion )
+        /* We should not advance RIP/EIP if the domain is shutting down */
+        if ( curr->domain->is_shutting_down )
             return 0;
-        /* Completion in hvm_io_assist() with no re-emulation required. */
-        ASSERT(dir == IOREQ_READ);
-        vio->io_completion = HVMIO_pio_completion;
+
         break;
     default:
         gdprintk(XENLOG_ERR, "Weird HVM ioemulation status %d.\n", rc);
xen/arch/x86/hvm/vmx/realmode.c +1 -1
index 76ff9a5..deb53ae 100644
@@ -111,7 +111,7 @@ void vmx_realmode_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt)
 
     rc = hvm_emulate_one(hvmemul_ctxt);
 
-    if ( vio->io_state == HVMIO_awaiting_completion || vio->mmio_retry )
+    if ( hvm_vcpu_io_need_completion(vio) || vio->mmio_retry )
         vio->io_completion = HVMIO_realmode_completion;
 
     if ( rc == X86EMUL_UNHANDLEABLE )
xen/include/asm-x86/hvm/vcpu.h +9 -1
index efb373f..13e7eb9 100644
@@ -32,7 +32,6 @@
 
 enum hvm_io_state {
     HVMIO_none = 0,
-    HVMIO_dispatched,
     HVMIO_awaiting_completion,
     HVMIO_completed
 };
@@ -55,6 +54,8 @@ struct hvm_vcpu_io {
     unsigned long          io_data;
     unsigned int           io_size;
     enum hvm_io_completion io_completion;
+    uint8_t                io_dir;
+    uint8_t                io_data_is_addr;
 
     /*
      * HVM emulation:
@@ -87,6 +88,13 @@ struct hvm_vcpu_io {
     const struct g2m_ioport *g2m_ioport;
 };
 
+static inline bool_t hvm_vcpu_io_need_completion(const struct hvm_vcpu_io *vio)
+{
+    return (vio->io_state == HVMIO_awaiting_completion) &&
+           !vio->io_data_is_addr &&
+           (vio->io_dir == IOREQ_READ);
+}
+
 #define VMCX_EADDR    (~0ULL)
 
 struct nestedvcpu {
Signature · 1 line
1.7.10.4
#36 lore raw

PATCH v5 13/16 x86/hvm: remove hvm_io_state enumeration

In reply to Paul Durrant

Emulation request status is already covered by STATE_IOREQ_XXX values so just use those. The mapping is:

HVMIO_none -> STATE_IOREQ_NONE
HVMIO_awaiting_completion -> STATE_IOREQ_READY
HVMIO_completed -> STATE_IORESP_READY

---
 xen/arch/x86/hvm/emulate.c       |   14 +++++++-------
 xen/arch/x86/hvm/hvm.c           |    6 +++---
 xen/arch/x86/hvm/svm/nestedsvm.c |    2 +-
 xen/arch/x86/hvm/vmx/realmode.c  |    4 ++--
 xen/include/asm-x86/hvm/vcpu.h   |   10 ++--------
 5 files changed, 15 insertions(+), 21 deletions(-)
xen/arch/x86/hvm/emulate.c +7 -7
index a6c7f66..4c696d1 100644
@@ -130,10 +130,10 @@ static int hvmemul_do_io(
 
     switch ( vio->io_state )
     {
-    case HVMIO_none:
+    case STATE_IOREQ_NONE:
         break;
-    case HVMIO_completed:
-        vio->io_state = HVMIO_none;
+    case STATE_IORESP_READY:
+        vio->io_state = STATE_IOREQ_NONE;
         if ( data_is_addr || dir == IOREQ_WRITE )
             return X86EMUL_UNHANDLEABLE;
         goto finish_access;
@@ -141,7 +141,7 @@ static int hvmemul_do_io(
         return X86EMUL_UNHANDLEABLE;
     }
 
-    vio->io_state = HVMIO_awaiting_completion;
+    vio->io_state = STATE_IOREQ_READY;
     vio->io_size = size;
     vio->io_dir = dir;
     vio->io_data_is_addr = data_is_addr;
@@ -160,7 +160,7 @@ static int hvmemul_do_io(
     {
     case X86EMUL_OKAY:
         vio->io_data = p.data;
-        vio->io_state = HVMIO_none;
+        vio->io_state = STATE_IOREQ_NONE;
         break;
     case X86EMUL_UNHANDLEABLE:
     {
@@ -173,13 +173,13 @@ static int hvmemul_do_io(
             rc = hvm_process_io_intercept(&null_handler, &p);
             if ( rc == X86EMUL_OKAY )
                 vio->io_data = p.data;
-            vio->io_state = HVMIO_none;
+            vio->io_state = STATE_IOREQ_NONE;
         }
         else
         {
             rc = hvm_send_assist_req(s, &p);
             if ( rc != X86EMUL_RETRY || curr->domain->is_shutting_down )
-                vio->io_state = HVMIO_none;
+                vio->io_state = STATE_IOREQ_NONE;
             else if ( data_is_addr || dir == IOREQ_WRITE )
                 rc = X86EMUL_OKAY;
         }
xen/arch/x86/hvm/hvm.c +3 -3
index b94a9b6..32b0d32 100644
@@ -421,11 +421,11 @@ static void hvm_io_assist(ioreq_t *p)
 
     if ( hvm_vcpu_io_need_completion(vio) )
     {
-        vio->io_state = HVMIO_completed;
+        vio->io_state = STATE_IORESP_READY;
         vio->io_data = p->data;
     }
     else
-        vio->io_state = HVMIO_none;
+        vio->io_state = STATE_IOREQ_NONE;
 
     msix_write_completion(curr);
     vcpu_end_shutdown_deferral(curr);
@@ -505,7 +505,7 @@ void hvm_do_resume(struct vcpu *v)
             guest_cpu_user_regs()->rax = (uint32_t)vio->io_data;
         else
             memcpy(&guest_cpu_user_regs()->rax, &vio->io_data, vio->io_size);
-        vio->io_state = HVMIO_none;
+        vio->io_state = STATE_IOREQ_NONE;
         break;
     case HVMIO_realmode_completion:
     {
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
index be5797a..8b165c6 100644
@@ -1231,7 +1231,7 @@ enum hvm_intblk nsvm_intr_blocked(struct vcpu *v)
          * Delay the injection because this would result in delivering
          * an interrupt *within* the execution of an instruction.
          */
-        if ( v->arch.hvm_vcpu.hvm_io.io_state != HVMIO_none )
+        if ( v->arch.hvm_vcpu.hvm_io.io_state != STATE_IOREQ_NONE )
             return hvm_intblk_shadow;
 
         if ( !nv->nv_vmexit_pending && n2vmcb->exitintinfo.bytes != 0 ) {
xen/arch/x86/hvm/vmx/realmode.c +2 -2
index deb53ae..25533dc 100644
@@ -205,7 +205,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
 
         vmx_realmode_emulate_one(&hvmemul_ctxt);
 
-        if ( vio->io_state != HVMIO_none || vio->mmio_retry )
+        if ( vio->io_state != STATE_IOREQ_NONE || vio->mmio_retry )
             break;
 
         /* Stop emulating unless our segment state is not safe */
@@ -219,7 +219,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
     }
 
     /* Need to emulate next time if we've started an IO operation */
-    if ( vio->io_state != HVMIO_none )
+    if ( vio->io_state != STATE_IOREQ_NONE )
         curr->arch.hvm_vmx.vmx_emulate = 1;
 
     if ( !curr->arch.hvm_vmx.vmx_emulate && !curr->arch.hvm_vmx.vmx_realmode )
xen/include/asm-x86/hvm/vcpu.h +2 -8
index 13e7eb9..9ca96d0 100644
@@ -30,12 +30,6 @@
 #include <asm/hvm/svm/nestedsvm.h>
 #include <asm/mtrr.h>
 
-enum hvm_io_state {
-    HVMIO_none = 0,
-    HVMIO_awaiting_completion,
-    HVMIO_completed
-};
-
 enum hvm_io_completion {
     HVMIO_no_completion = 0,
     HVMIO_mmio_completion,
@@ -50,7 +44,7 @@ struct hvm_vcpu_asid {
 
 struct hvm_vcpu_io {
     /* I/O request in flight to device model. */
-    enum hvm_io_state      io_state;
+    uint8_t                io_state;
     unsigned long          io_data;
     unsigned int           io_size;
     enum hvm_io_completion io_completion;
@@ -90,7 +84,7 @@ struct hvm_vcpu_io {
 
 static inline bool_t hvm_vcpu_io_need_completion(const struct hvm_vcpu_io *vio)
 {
-    return (vio->io_state == HVMIO_awaiting_completion) &&
+    return (vio->io_state == STATE_IOREQ_READY) &&
            !vio->io_data_is_addr &&
            (vio->io_dir == IOREQ_READ);
 }
Signature · 1 line
1.7.10.4
#38 lore raw

PATCH v5 14/16 x86/hvm: use ioreq_t to track in-flight state

In reply to Paul Durrant

Use an ioreq_t rather than open coded state, size, dir and data fields in struct hvm_vcpu_io. This also allows PIO completion to be handled similarly to MMIO completion by re-issuing the handle_pio() call.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c       |   35 +++++++++++++++++++++--------------
 xen/arch/x86/hvm/hvm.c           |   13 +++++--------
 xen/arch/x86/hvm/svm/nestedsvm.c |    2 +-
 xen/arch/x86/hvm/vmx/realmode.c  |    4 ++--
 xen/include/asm-x86/hvm/vcpu.h   |   12 ++++--------
 5 files changed, 33 insertions(+), 33 deletions(-)
xen/arch/x86/hvm/emulate.c +21 -14
index 4c696d1..b5fb0da 100644
@@ -91,6 +91,7 @@ static int hvmemul_do_io(
         .df = df,
         .data = data,
         .data_is_ptr = data_is_addr, /* ioreq_t field name is misleading */
+        .state = STATE_IOREQ_READY,
     };
     void *p_data = (void *)data;
     int rc;
@@ -128,12 +129,24 @@ static int hvmemul_do_io(
         }
     }
 
-    switch ( vio->io_state )
+    switch ( vio->io_req.state )
     {
     case STATE_IOREQ_NONE:
         break;
     case STATE_IORESP_READY:
-        vio->io_state = STATE_IOREQ_NONE;
+        vio->io_req.state = STATE_IOREQ_NONE;
+        p = vio->io_req;
+
+        /* Verify the emulation request has been correctly re-issued */
+        if ( (p.type != is_mmio ? IOREQ_TYPE_COPY : IOREQ_TYPE_PIO) ||
+             (p.addr != addr) ||
+             (p.size != size) ||
+             (p.count != reps) ||
+             (p.dir != dir) ||
+             (p.df != df) ||
+             (p.data_is_ptr != data_is_addr) )
+            domain_crash(curr->domain);
+
         if ( data_is_addr || dir == IOREQ_WRITE )
             return X86EMUL_UNHANDLEABLE;
         goto finish_access;
@@ -141,11 +154,6 @@ static int hvmemul_do_io(
         return X86EMUL_UNHANDLEABLE;
     }
 
-    vio->io_state = STATE_IOREQ_READY;
-    vio->io_size = size;
-    vio->io_dir = dir;
-    vio->io_data_is_addr = data_is_addr;
-
     if ( dir == IOREQ_WRITE )
     {
         if ( !data_is_addr )
@@ -154,13 +162,14 @@ static int hvmemul_do_io(
         hvmtrace_io_assist(&p);
     }
 
+    vio->io_req = p;
+
     rc = hvm_io_intercept(&p);
 
     switch ( rc )
     {
     case X86EMUL_OKAY:
-        vio->io_data = p.data;
-        vio->io_state = STATE_IOREQ_NONE;
+        vio->io_req.state = STATE_IOREQ_NONE;
         break;
     case X86EMUL_UNHANDLEABLE:
     {
@@ -171,15 +180,13 @@ static int hvmemul_do_io(
         if ( !s )
         {
             rc = hvm_process_io_intercept(&null_handler, &p);
-            if ( rc == X86EMUL_OKAY )
-                vio->io_data = p.data;
-            vio->io_state = STATE_IOREQ_NONE;
+            vio->io_req.state = STATE_IOREQ_NONE;
         }
         else
         {
             rc = hvm_send_assist_req(s, &p);
             if ( rc != X86EMUL_RETRY || curr->domain->is_shutting_down )
-                vio->io_state = STATE_IOREQ_NONE;
+                vio->io_req.state = STATE_IOREQ_NONE;
             else if ( data_is_addr || dir == IOREQ_WRITE )
                 rc = X86EMUL_OKAY;
         }
@@ -198,7 +205,7 @@ static int hvmemul_do_io(
         hvmtrace_io_assist(&p);
 
         if ( !data_is_addr )
-            memcpy(p_data, &vio->io_data, size);
+            memcpy(p_data, &p.data, size);
     }
 
     if ( is_mmio && !data_is_addr )
xen/arch/x86/hvm/hvm.c +5 -8
index 32b0d32..901f9a8 100644
@@ -421,11 +421,11 @@ static void hvm_io_assist(ioreq_t *p)
 
     if ( hvm_vcpu_io_need_completion(vio) )
     {
-        vio->io_state = STATE_IORESP_READY;
-        vio->io_data = p->data;
+        vio->io_req.state = STATE_IORESP_READY;
+        vio->io_req.data = p->data;
     }
     else
-        vio->io_state = STATE_IOREQ_NONE;
+        vio->io_req.state = STATE_IOREQ_NONE;
 
     msix_write_completion(curr);
     vcpu_end_shutdown_deferral(curr);
@@ -501,11 +501,8 @@ void hvm_do_resume(struct vcpu *v)
         (void)handle_mmio();
         break;
     case HVMIO_pio_completion:
-        if ( vio->io_size == 4 ) /* Needs zero extension. */
-            guest_cpu_user_regs()->rax = (uint32_t)vio->io_data;
-        else
-            memcpy(&guest_cpu_user_regs()->rax, &vio->io_data, vio->io_size);
-        vio->io_state = STATE_IOREQ_NONE;
+        (void)handle_pio(vio->io_req.addr, vio->io_req.size,
+                         vio->io_req.dir);
         break;
     case HVMIO_realmode_completion:
     {
xen/arch/x86/hvm/svm/nestedsvm.c +1 -1
index 8b165c6..78667a2 100644
@@ -1231,7 +1231,7 @@ enum hvm_intblk nsvm_intr_blocked(struct vcpu *v)
          * Delay the injection because this would result in delivering
          * an interrupt *within* the execution of an instruction.
          */
-        if ( v->arch.hvm_vcpu.hvm_io.io_state != STATE_IOREQ_NONE )
+        if ( v->arch.hvm_vcpu.hvm_io.io_req.state != STATE_IOREQ_NONE )
             return hvm_intblk_shadow;
 
         if ( !nv->nv_vmexit_pending && n2vmcb->exitintinfo.bytes != 0 ) {
xen/arch/x86/hvm/vmx/realmode.c +2 -2
index 25533dc..e83a61f 100644
@@ -205,7 +205,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
 
         vmx_realmode_emulate_one(&hvmemul_ctxt);
 
-        if ( vio->io_state != STATE_IOREQ_NONE || vio->mmio_retry )
+        if ( vio->io_req.state != STATE_IOREQ_NONE || vio->mmio_retry )
             break;
 
         /* Stop emulating unless our segment state is not safe */
@@ -219,7 +219,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
     }
 
     /* Need to emulate next time if we've started an IO operation */
-    if ( vio->io_state != STATE_IOREQ_NONE )
+    if ( vio->io_req.state != STATE_IOREQ_NONE )
         curr->arch.hvm_vmx.vmx_emulate = 1;
 
     if ( !curr->arch.hvm_vmx.vmx_emulate && !curr->arch.hvm_vmx.vmx_realmode )
xen/include/asm-x86/hvm/vcpu.h +4 -8
index 9ca96d0..7ffa28a 100644
@@ -44,12 +44,8 @@ struct hvm_vcpu_asid {
 
 struct hvm_vcpu_io {
     /* I/O request in flight to device model. */
-    uint8_t                io_state;
-    unsigned long          io_data;
-    unsigned int           io_size;
+    ioreq_t                io_req;
     enum hvm_io_completion io_completion;
-    uint8_t                io_dir;
-    uint8_t                io_data_is_addr;
 
     /*
      * HVM emulation:
@@ -84,9 +80,9 @@ struct hvm_vcpu_io {
 
 static inline bool_t hvm_vcpu_io_need_completion(const struct hvm_vcpu_io *vio)
 {
-    return (vio->io_state == STATE_IOREQ_READY) &&
-           !vio->io_data_is_addr &&
-           (vio->io_dir == IOREQ_READ);
+    return (vio->io_req.state == STATE_IOREQ_READY) &&
+           !vio->io_req.data_is_ptr &&
+           (vio->io_req.dir == IOREQ_READ);
 }
 
 #define VMCX_EADDR    (~0ULL)
Signature · 1 line
1.7.10.4
#32 lore raw

PATCH v5 11/16 x86/hvm: split I/O completion handling from state model

In reply to Paul Durrant

The state of in-flight I/O and how its completion will be handled are logically separate and conflating the two makes the code unnecessarily confusing.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/hvm.c            |   44 +++++++++++++++++++++++++++----------
 xen/arch/x86/hvm/io.c             |    6 ++---
 xen/arch/x86/hvm/vmx/realmode.c   |   27 +++++++++++++++++------
 xen/include/asm-x86/hvm/vcpu.h    |   16 +++++++++-----
 xen/include/asm-x86/hvm/vmx/vmx.h |    1 +
 5 files changed, 67 insertions(+), 27 deletions(-)
xen/arch/x86/hvm/hvm.c +32 -12
index d498151..0f0730e 100644
@@ -59,6 +59,7 @@
 #include <asm/hvm/trace.h>
 #include <asm/hvm/nestedhvm.h>
 #include <asm/hvm/event.h>
+#include <asm/hvm/vmx/vmx.h>
 #include <asm/mtrr.h>
 #include <asm/apic.h>
 #include <public/sched.h>
@@ -428,17 +429,6 @@ static void hvm_io_assist(ioreq_t *p)
         vio->io_state = HVMIO_completed;
         vio->io_data = p->data;
         break;
-    case HVMIO_handle_mmio_awaiting_completion:
-        vio->io_state = HVMIO_completed;
-        vio->io_data = p->data;
-        (void)handle_mmio();
-        break;
-    case HVMIO_handle_pio_awaiting_completion:
-        if ( vio->io_size == 4 ) /* Needs zero extension. */
-            guest_cpu_user_regs()->rax = (uint32_t)p->data;
-        else
-            memcpy(&guest_cpu_user_regs()->rax, &p->data, vio->io_size);
-        break;
     default:
         break;
     }
@@ -479,6 +469,7 @@ void hvm_do_resume(struct vcpu *v)
     struct hvm_vcpu_io *vio = &v->arch.hvm_vcpu.hvm_io;
     struct domain *d = v->domain;
     struct hvm_ioreq_server *s;
+    enum hvm_io_completion io_completion;
 
     check_wakeup_from_wait();
 
@@ -505,8 +496,37 @@ void hvm_do_resume(struct vcpu *v)
         }
     }
 
-    if ( vio->mmio_retry )
+    io_completion = vio->io_completion;
+    vio->io_completion = HVMIO_no_completion;
+
+    switch ( io_completion )
+    {
+    case HVMIO_no_completion:
+        break;
+    case HVMIO_mmio_completion:
         (void)handle_mmio();
+        break;
+    case HVMIO_pio_completion:
+        if ( vio->io_size == 4 ) /* Needs zero extension. */
+            guest_cpu_user_regs()->rax = (uint32_t)vio->io_data;
+        else
+            memcpy(&guest_cpu_user_regs()->rax, &vio->io_data, vio->io_size);
+        vio->io_state = HVMIO_none;
+        break;
+    case HVMIO_realmode_completion:
+    {
+        struct hvm_emulate_ctxt ctxt;
+
+        hvm_emulate_prepare(&ctxt, guest_cpu_user_regs());
+        vmx_realmode_emulate_one(&ctxt);
+        hvm_emulate_writeback(&ctxt);
+
+        break;
+    }
+    default:
+        ASSERT_UNREACHABLE();
+        break;
+    }
 
     /* Inject pending hw/sw trap */
     if ( v->arch.hvm_vcpu.inject_trap.vector != -1 ) 
xen/arch/x86/hvm/io.c +3 -3
index fe099d8..221d05e 100644
@@ -92,8 +92,8 @@ int handle_mmio(void)
 
     if ( rc != X86EMUL_RETRY )
         vio->io_state = HVMIO_none;
-    if ( vio->io_state == HVMIO_awaiting_completion )
-        vio->io_state = HVMIO_handle_mmio_awaiting_completion;
+    if ( vio->io_state == HVMIO_awaiting_completion || vio->mmio_retry )
+        vio->io_completion = HVMIO_mmio_completion;
     else
         vio->mmio_access = (struct npfec){};
 
@@ -158,7 +158,7 @@ int handle_pio(uint16_t port, unsigned int size, int dir)
             return 0;
         /* Completion in hvm_io_assist() with no re-emulation required. */
         ASSERT(dir == IOREQ_READ);
-        vio->io_state = HVMIO_handle_pio_awaiting_completion;
+        vio->io_completion = HVMIO_pio_completion;
         break;
     default:
         gdprintk(XENLOG_ERR, "Weird HVM ioemulation status %d.\n", rc);
xen/arch/x86/hvm/vmx/realmode.c +20 -7
index fe8b4a0..76ff9a5 100644
@@ -101,15 +101,19 @@ static void realmode_deliver_exception(
     }
 }
 
-static void realmode_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt)
+void vmx_realmode_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt)
 {
     struct vcpu *curr = current;
+    struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
     int rc;
 
     perfc_incr(realmode_emulations);
 
     rc = hvm_emulate_one(hvmemul_ctxt);
 
+    if ( vio->io_state == HVMIO_awaiting_completion || vio->mmio_retry )
+        vio->io_completion = HVMIO_realmode_completion;
+
     if ( rc == X86EMUL_UNHANDLEABLE )
     {
         gdprintk(XENLOG_ERR, "Failed to emulate insn.\n");
@@ -177,9 +181,6 @@ void vmx_realmode(struct cpu_user_regs *regs)
 
     hvm_emulate_prepare(&hvmemul_ctxt, regs);
 
-    if ( vio->io_state == HVMIO_completed )
-        realmode_emulate_one(&hvmemul_ctxt);
-
     /* Only deliver interrupts into emulated real mode. */
     if ( !(curr->arch.hvm_vcpu.guest_cr[0] & X86_CR0_PE) &&
          (intr_info & INTR_INFO_VALID_MASK) )
@@ -190,8 +191,7 @@ void vmx_realmode(struct cpu_user_regs *regs)
 
     curr->arch.hvm_vmx.vmx_emulate = 1;
     while ( curr->arch.hvm_vmx.vmx_emulate &&
-            !softirq_pending(smp_processor_id()) &&
-            (vio->io_state == HVMIO_none) )
+            !softirq_pending(smp_processor_id()) )
     {
         /*
          * Check for pending interrupts only every 16 instructions, because
@@ -203,7 +203,10 @@ void vmx_realmode(struct cpu_user_regs *regs)
              hvm_local_events_need_delivery(curr) )
             break;
 
-        realmode_emulate_one(&hvmemul_ctxt);
+        vmx_realmode_emulate_one(&hvmemul_ctxt);
+
+        if ( vio->io_state != HVMIO_none || vio->mmio_retry )
+            break;
 
         /* Stop emulating unless our segment state is not safe */
         if ( curr->arch.hvm_vmx.vmx_realmode )
@@ -245,3 +248,13 @@ void vmx_realmode(struct cpu_user_regs *regs)
     if ( intr_info & INTR_INFO_VALID_MASK )
         __vmwrite(VM_ENTRY_INTR_INFO, intr_info);
 }
+
+/*
+ * Local variables:
+ * mode: C
+ * c-file-style: "BSD"
+ * c-basic-offset: 4
+ * tab-width: 4
+ * indent-tabs-mode: nil
+ * End:
+ */
xen/include/asm-x86/hvm/vcpu.h +11 -5
index 2ed0606..efb373f 100644
@@ -34,11 +34,16 @@ enum hvm_io_state {
     HVMIO_none = 0,
     HVMIO_dispatched,
     HVMIO_awaiting_completion,
-    HVMIO_handle_mmio_awaiting_completion,
-    HVMIO_handle_pio_awaiting_completion,
     HVMIO_completed
 };
 
+enum hvm_io_completion {
+    HVMIO_no_completion = 0,
+    HVMIO_mmio_completion,
+    HVMIO_pio_completion,
+    HVMIO_realmode_completion
+};
+
 struct hvm_vcpu_asid {
     uint64_t generation;
     uint32_t asid;
@@ -46,9 +51,10 @@ struct hvm_vcpu_asid {
 
 struct hvm_vcpu_io {
     /* I/O request in flight to device model. */
-    enum hvm_io_state   io_state;
-    unsigned long       io_data;
-    int                 io_size;
+    enum hvm_io_state      io_state;
+    unsigned long          io_data;
+    unsigned int           io_size;
+    enum hvm_io_completion io_completion;
 
     /*
      * HVM emulation:
xen/include/asm-x86/hvm/vmx/vmx.h +1 -0
index 35f804a..c5f3d24 100644
@@ -94,6 +94,7 @@ void vmx_asm_do_vmentry(void);
 void vmx_intr_assist(void);
 void noreturn vmx_do_resume(struct vcpu *);
 void vmx_vlapic_msr_changed(struct vcpu *v);
+void vmx_realmode_emulate_one(struct hvm_emulate_ctxt *hvmemul_ctxt);
 void vmx_realmode(struct cpu_user_regs *regs);
 void vmx_update_debug_state(struct vcpu *v);
 void vmx_update_exception_bitmap(struct vcpu *v);
Signature · 1 line
1.7.10.4
#40 lore raw

PATCH v5 15/16 x86/hvm: always re-emulate I/O from a buffer

In reply to Paul Durrant

If memory mapped I/O is 'chunked' then the I/O must be re-emulated, otherwise only the first chunk will be processed. This patch makes sure all I/O from a buffer is re-emulated regardless of whether it is a read or a write.

---
 xen/arch/x86/hvm/emulate.c     |    4 ++--
 xen/include/asm-x86/hvm/vcpu.h |    3 +--
 2 files changed, 3 insertions(+), 4 deletions(-)
xen/arch/x86/hvm/emulate.c +2 -2
index b5fb0da..8bb56a2 100644
@@ -147,7 +147,7 @@ static int hvmemul_do_io(
              (p.data_is_ptr != data_is_addr) )
             domain_crash(curr->domain);
 
-        if ( data_is_addr || dir == IOREQ_WRITE )
+        if ( data_is_addr )
             return X86EMUL_UNHANDLEABLE;
         goto finish_access;
     default:
@@ -187,7 +187,7 @@ static int hvmemul_do_io(
             rc = hvm_send_assist_req(s, &p);
             if ( rc != X86EMUL_RETRY || curr->domain->is_shutting_down )
                 vio->io_req.state = STATE_IOREQ_NONE;
-            else if ( data_is_addr || dir == IOREQ_WRITE )
+            else if ( data_is_addr )
                 rc = X86EMUL_OKAY;
         }
         break;
xen/include/asm-x86/hvm/vcpu.h +1 -2
index 7ffa28a..506ce89 100644
@@ -81,8 +81,7 @@ struct hvm_vcpu_io {
 static inline bool_t hvm_vcpu_io_need_completion(const struct hvm_vcpu_io *vio)
 {
     return (vio->io_req.state == STATE_IOREQ_READY) &&
-           !vio->io_req.data_is_ptr &&
-           (vio->io_req.dir == IOREQ_READ);
+           !vio->io_req.data_is_ptr;
 }
 
 #define VMCX_EADDR    (~0ULL)
Signature · 1 line
1.7.10.4
Reviewed-by on v5 1/16
#2 lore raw

Re: [PATCH v5 01/16] x86/hvm: make sure emulation is retried if domain is shutting down

In reply to Paul Durrant
Quoted 12 lines · Paul Durrant

The addition of commit 2df1aa01 "x86/hvm: remove hvm_io_pending() check in hvmemul_do_io()" causes a problem in migration because I/O that was caught by the test of vcpu_start_shutdown_deferral() in hvm_send_assist_req() is now considered completed rather than requiring a retry.

This patch fixes the problem by having hvm_send_assist_req() return X86EMUL_RETRY rather than X86EMUL_OKAY if the vcpu_start_shutdown_deferral() test fails and then making sure that the emulation state is reset if the domain is found to be shutting down.

Quoted 5 lines

Keir Fraser <[email protected]>
Jan Beulich <[email protected]>
Andrew Cooper <[email protected]>

Quoted 31 lines
---
 xen/arch/x86/hvm/emulate.c |    2 +-
 xen/arch/x86/hvm/hvm.c     |    2 +-
 2 files changed, 2 insertions(+), 2 deletions(-)
xen/arch/x86/hvm/emulate.c +1 -1
index fe5661d..8b60843 100644
@@ -183,7 +183,7 @@ static int hvmemul_do_io(
         else
         {
             rc = hvm_send_assist_req(s, &p);
-            if ( rc != X86EMUL_RETRY )
+            if ( rc != X86EMUL_RETRY || curr->domain->is_shutting_down )
                 vio->io_state = HVMIO_none;
             else if ( data_is_addr || dir == IOREQ_WRITE )
                 rc = X86EMUL_OKAY;
xen/arch/x86/hvm/hvm.c +1 -1
index 535d622..f0cf064 100644
@@ -2613,7 +2613,7 @@ int hvm_send_assist_req(struct hvm_ioreq_server *s, ioreq_t *proto_p)
 
     ASSERT(s);
     if ( unlikely(!vcpu_start_shutdown_deferral(curr)) )
-        return X86EMUL_OKAY;
+        return X86EMUL_RETRY;
 
     list_for_each_entry ( sv,
                           &s->ioreq_vcpu_list,
#44 lore raw
In reply to Paul Durrant

Il 30/06/2015 15:05, Paul Durrant ha scritto:

Quoted 99 lines

This patch series re-works much of the code involved in emulation of port and memory mapped I/O for HVM guests.

The code has become very convoluted and, at least by inspection, certain emulations will apparently malfunction.

The series is broken down into 16 patches (which are also available in my xenbits repo: http://xenbits.xen.org/gitweb/?p=people/pauldu/xen.git
on the emulation32 branch).

Previous changelog
------------------

v2:
- Removed bogus assertion from patch #15
- Re-worked patch #17 after basic testing of back-port onto XenServer

v3:
- Addressed comments from Jan
- Re-ordered series to bring a couple of more trivial patches to the front
- Backport to XenServer (4.5) now passing automated tests
- Tested on unstable with QEMU upstream and trad, with and without HAP (to force shadow emulation)

v4:
- Removed previous patch (make sure translated MMIO reads or writes fall within a page) and rebased rest of series.
- Address Jan's comments on patch #1

Changelog (now per-patch)
-------------------------

0001-x86-hvm-make-sure-emulation-is-retried-if-domain-is-.patch

This is a fix for an issue on staging reported by Don Slutz

0002-x86-hvm-remove-multiple-open-coded-chunking-loops.patch

v5: Addressed further comments from Jan

0003-x86-hvm-change-hvm_mmio_read_t-and-hvm_mmio_write_t-.patch

v5: New patch to tidy up types

0004-x86-hvm-restrict-port-numbers-to-uint16_t-and-sizes-.patch

v5: New patch to tidy up more types

0005-x86-hvm-unify-internal-portio-and-mmio-intercepts.patch

v5: Addressed further comments from Jan and simplified implementation
by passing ioreq_t to accept() function

0006-x86-hvm-add-length-to-mmio-check-op.patch

v5: Simplified by leaving mmio_check() implementation alone and
calling to check last byte if first-byte check passes

0007-x86-hvm-unify-dpci-portio-intercept-with-standard-po.patch

v5: Addressed further comments from Jan

0008-x86-hvm-unify-stdvga-mmio-intercept-with-standard-mm.patch

v5: Fixed semantic problems pointed out by Jan

0009-x86-hvm-limit-reps-to-avoid-the-need-to-handle-retry.patch

v5: Addressed further comments from Jan

0010-x86-hvm-only-call-hvm_io_assist-from-hvm_wait_for_io.patch

v5: Added Jan's acked-by

0011-x86-hvm-split-I-O-completion-handling-from-state-mod.patch

v5: Confirmed call to msix_write_completion() is in the correct place.

0012-x86-hvm-remove-HVMIO_dispatched-I-O-state.patch

v5: Added some extra comments to the commit

0013-x86-hvm-remove-hvm_io_state-enumeration.patch

v5: Added Jan's acked-by

0014-x86-hvm-use-ioreq_t-to-track-in-flight-state.patch

v5: Added missing hunk with call to handle_pio()

0015-x86-hvm-always-re-emulate-I-O-from-a-buffer.patch

v5: Added Jan's acked-by

0016-x86-hvm-track-large-memory-mapped-accesses-by-buffer.patch

v5: Fixed to cache up three distict I/O emulations per instruction

Quoted 10 lines

Testing
-------

The series was been back-ported to staging-4.5 and then dropped onto the XenServer (Dundee) patch queue. All automated branch-safety tests pass.

The series as-is has been manually tested with a Windows 7 (32-bit) VM using upstream QEMU.

Thanks for your work.
I did some very fast tests, no regression found, on my linux domUs qxl is still not working and I'm unable to debug it. @Jim Fehlig: can you try if qxl is still working at least on suse dom0/domU after this serie please?

Can someone tell me how to debug the qxl problem now that qemu don't crash anymore but remain at 100% cpu without nothing about in dom0 logs (if exists) please?

Thanks for any reply and sorry for my bad english.

#3 lore raw

Re: [PATCH v5 01/16] x86/hvm: make sure emulation is retried if domain is shutting down

In reply to Andrew Cooper
Quoted 25 lines · Andrew Cooper
Quoted 12 lines · Paul Durrant

The addition of commit 2df1aa01 "x86/hvm: remove hvm_io_pending() check in hvmemul_do_io()" causes a problem in migration because I/O that was caught by the test of vcpu_start_shutdown_deferral() in hvm_send_assist_req() is now considered completed rather than requiring a retry.

This patch fixes the problem by having hvm_send_assist_req() return X86EMUL_RETRY rather than X86EMUL_OKAY if the vcpu_start_shutdown_deferral() test fails and then making sure that the emulation state is reset if the domain is found to be shutting down.

Quoted 5 lines

Keir Fraser <[email protected]>
Jan Beulich <[email protected]>
Andrew Cooper <[email protected]>

Quoted 5 lines
---
  xen/arch/x86/hvm/emulate.c |    2 +-
  xen/arch/x86/hvm/hvm.c     |    2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
Quoted 10 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io( · line 186

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +1 -1
index fe5661d..8b60843 100644
@@ -183,7 +183,7 @@ static int hvmemul_do_io(
          else
          {
              rc = hvm_send_assist_req(s, &p);
-            if ( rc != X86EMUL_RETRY )
+            if ( rc != X86EMUL_RETRY || curr->domain->is_shutting_down )

Having spent more time thinking about this, a safer test is "!curr-> defer_shutdown" .

The reason are as follows:
1) This uses what vcpu_start_shutdown_deferral() returns. But it is only valid if vcpu_start_shutdown_deferral() is called which is the case now.
2) The checking of "curr->domain->is_shutting_down" without also

     checking for "curr-> defer_shutdown" may open a timing window.

3) No use of "spin_lock(&d->shutdown_lock)".

Quoted 9 lines
Quoted 9 lines
xen/arch/x86/hvm/emulate.c +0 -0
                  vio->io_state = HVMIO_none;
              else if ( data_is_addr || dir == IOREQ_WRITE )
                  rc = X86EMUL_OKAY;
xen/arch/x86/hvm/hvm.c +0 -0
index 535d622..f0cf064 100644
@@ -2613,7 +2613,7 @@ int hvm_send_assist_req(struct hvm_ioreq_server *s, ioreq_t *proto_p)
 
Quoted 4 lines

xen/arch/x86/hvm/hvm.c int hvm_send_assist_req(struct hvm_ioreq_server *s, ioreq_t *proto_p) · line 2616

Quoted 4 lines
xen/arch/x86/hvm/hvm.c +1 -1
      ASSERT(s);
      if ( unlikely(!vcpu_start_shutdown_deferral(curr)) )
-        return X86EMUL_OKAY;
+        return X86EMUL_RETRY;

I will also note that this change makes hvm_send_assist_req() have only 2 return codes:

X86EMUL_RETRY
X86EMUL_UNHANDLEABLE

and so is not clear why its return should not be a bool.

And there are now 2 reasons why X86EMUL_RETRY is returned, which require the caller to redo a test. Granted X86EMUL_OKAY is bad. But I wounder if it would be better to add an enum for hvm_send_assist_req() to return in stead of overloading the usage of X86EMUL_UNHANDLEABLE and X86EMUL_RETRY with yet more meanings.

-Don Slutz

Quoted 8 lines

xen/arch/x86/hvm/hvm.c int hvm_send_assist_req(struct hvm_ioreq_server *s, ioreq_t *proto_p) · line 2619

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
      list_for_each_entry ( sv,
                            &s->ioreq_vcpu_list,

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#4 lore raw

Re: [PATCH v5 01/16] x86/hvm: make sure emulation is retried if domain is shutting down

Quoted 49 lines

-----Original Message-----
From: Don Slutz [mailto:[email protected]]
Sent: 30 June 2015 17:14
To: Andrew Cooper; Paul Durrant; [email protected]
Subject: Re: [Xen-devel] [PATCH v5 01/16] x86/hvm: make sure emulation is retried if domain is shutting down

Quoted 2 lines · Andrew Cooper
Quoted 1 line · Paul Durrant

The addition of commit 2df1aa01 "x86/hvm: remove hvm_io_pending()

check

Quoted 3 lines
Quoted 3 lines

in hvmemul_do_io()" causes a problem in migration because I/O that was caught by the test of vcpu_start_shutdown_deferral() in hvm_send_assist_req() is now considered completed rather than

requiring

Quoted 20 lines
Quoted 8 lines

a retry.

This patch fixes the problem by having hvm_send_assist_req() return X86EMUL_RETRY rather than X86EMUL_OKAY if the vcpu_start_shutdown_deferral() test fails and then making sure that the emulation state is reset if the domain is found to be shutting down.

Quoted 5 lines

Keir Fraser <[email protected]>
Jan Beulich <[email protected]>
Andrew Cooper <[email protected]>

Quoted 5 lines
---
  xen/arch/x86/hvm/emulate.c |    2 +-
  xen/arch/x86/hvm/hvm.c     |    2 +-
  2 files changed, 2 insertions(+), 2 deletions(-)
Quoted 10 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io( · line 186

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +1 -1
index fe5661d..8b60843 100644
@@ -183,7 +183,7 @@ static int hvmemul_do_io(
          else
          {
              rc = hvm_send_assist_req(s, &p);
-            if ( rc != X86EMUL_RETRY )
+            if ( rc != X86EMUL_RETRY || curr->domain->is_shutting_down )

Having spent more time thinking about this, a safer test is "!curr-> defer_shutdown" .

Quoted 7 lines

The reason are as follows:
1) This uses what vcpu_start_shutdown_deferral() returns. But it is only valid if
vcpu_start_shutdown_deferral() is called which is the case now.
2) The checking of "curr->domain->is_shutting_down" without also

      checking for "curr-> defer_shutdown" may open a timing window.

3) No use of "spin_lock(&d->shutdown_lock)".

I don't think we need to lock. The is_shutting_down flag from this code's PoV will only ever go from 0 -> 1 so if shutdown deferral failed I think it is perfectly safe to check without taking the spinlock.

What we are essentially want to know in handle_pio() is whether the I/O has been issued or whether the whole thing needs to be retried. Another option to do this would be to change the io_state to something like IOREQ_INPROGRESS (or whatever it is called) when the I/O is actually issued and then test for that. Maybe hvm_send_assist_req() should just become a void function and the return code just set on the basis of the io_state after it has been called.

Anyway, all that is more code churn and the aim of this patch was a minimal diff to fix the problem... which I think it does.

Paul

Quoted 44 lines
Quoted 10 lines

xen/arch/x86/hvm/hvm.c int hvm_send_assist_req(struct

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +0 -0
                  vio->io_state = HVMIO_none;
              else if ( data_is_addr || dir == IOREQ_WRITE )
                  rc = X86EMUL_OKAY;
xen/arch/x86/hvm/hvm.c +0 -0
index 535d622..f0cf064 100644
@@ -2613,7 +2613,7 @@ int hvm_send_assist_req(struct

hvm_ioreq_server *s, ioreq_t *proto_p)

Quoted 5 lines

xen/arch/x86/hvm/hvm.c int hvm_send_assist_req(struct hvm_ioreq_server *s, ioreq_t *proto_p) · line 2616

Quoted 5 lines
xen/arch/x86/hvm/hvm.c +1 -1
      ASSERT(s);
      if ( unlikely(!vcpu_start_shutdown_deferral(curr)) )
-        return X86EMUL_OKAY;
+        return X86EMUL_RETRY;

I will also note that this change makes hvm_send_assist_req() have only 2 return codes:

X86EMUL_RETRY
X86EMUL_UNHANDLEABLE

and so is not clear why its return should not be a bool.

And there are now 2 reasons why X86EMUL_RETRY is returned, which require
the caller to redo a test.
Granted X86EMUL_OKAY is bad. But I wounder if it would be better to add an enum for
hvm_send_assist_req() to return in stead of overloading the usage of X86EMUL_UNHANDLEABLE
and X86EMUL_RETRY with yet more meanings.

-Don Slutz

Quoted 8 lines

xen/arch/x86/hvm/hvm.c int hvm_send_assist_req(struct hvm_ioreq_server *s, ioreq_t *proto_p) · line 2619

Quoted 3 lines
xen/arch/x86/hvm/hvm.c +0 -0
      list_for_each_entry ( sv,
                            &s->ioreq_vcpu_list,

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel

#15 lore raw

Re: [PATCH v5 05/16] x86/hvm: unify internal portio and mmio intercepts

In reply to Paul Durrant

El 30/06/15 a les 15.05, Paul Durrant ha escrit:
[...]
+void msixtbl_init(struct domain *d)

xen/arch/x86/hvm/vmsi.c found: · line 486

Quoted 2 lines
xen/arch/x86/hvm/vmsi.c +2 -0
+{
+    register_mmio_handler(d, &msixtbl_mmio_ops);

Since you are adding an initialization function to vmsi I think msixtbl_list and msixtbl_list_lock should also be initialized here instead of hvm_domain_initialise.

Roger.

#16 lore raw

Re: [PATCH v5 05/16] x86/hvm: unify internal portio and mmio intercepts

Quoted 11 lines

-----Original Message-----
From: Roger Pau Monné [mailto:[email protected]]
Sent: 02 July 2015 15:52
To: Paul Durrant; [email protected]
Cc: Andrew Cooper; Keir (Xen.org); Jan Beulich
Subject: Re: [Xen-devel] [PATCH v5 05/16] x86/hvm: unify internal portio and mmio intercepts

El 30/06/15 a les 15.05, Paul Durrant ha escrit:
[...]
+void msixtbl_init(struct domain *d)

Quoted 7 lines

xen/arch/x86/hvm/vmsi.c found: · line 486

Quoted 2 lines
xen/arch/x86/hvm/vmsi.c +2 -0
+{
+    register_mmio_handler(d, &msixtbl_mmio_ops);

Since you are adding an initialization function to vmsi I think msixtbl_list and msixtbl_list_lock should also be initialized here instead of hvm_domain_initialise.

Yes, that sounds quite reasonable. Perhaps as a follow-up patch though, unless I need to post v6 of the series for some other reason.

Paul

Quoted 1 line

Roger.

#17 lore raw

Re: [PATCH v5 05/16] x86/hvm: unify internal portio and mmio intercepts

El 02/07/15 a les 17.02, Paul Durrant ha escrit:

Quoted 14 lines
Quoted 14 lines

-----Original Message-----
From: Roger Pau Monné [mailto:[email protected]]
Sent: 02 July 2015 15:52
To: Paul Durrant; [email protected]
Cc: Andrew Cooper; Keir (Xen.org); Jan Beulich
Subject: Re: [Xen-devel] [PATCH v5 05/16] x86/hvm: unify internal portio and mmio intercepts

El 30/06/15 a les 15.05, Paul Durrant ha escrit:
[...]
+void msixtbl_init(struct domain *d)

Quoted 2 lines
xen/arch/x86/hvm/vmsi.c +2 -0
+{
+    register_mmio_handler(d, &msixtbl_mmio_ops);
Quoted 6 lines
Quoted 4 lines

Since you are adding an initialization function to vmsi I think msixtbl_list and msixtbl_list_lock should also be initialized here instead of hvm_domain_initialise.

Yes, that sounds quite reasonable. Perhaps as a follow-up patch though, unless I need to post v6 of the series for some other reason.

That's fine, if not I can also add it as a pre-patch to my HVMlite stuff, it's trivial.

Roger.

#18 lore raw

Re: [PATCH v5 05/16] x86/hvm: unify internal portio and mmio intercepts

Quoted 29 lines

-----Original Message-----
From: Roger Pau Monné [mailto:[email protected]]
Sent: 02 July 2015 16:12
To: Paul Durrant; [email protected]
Cc: Andrew Cooper; Keir (Xen.org); Jan Beulich
Subject: Re: [Xen-devel] [PATCH v5 05/16] x86/hvm: unify internal portio and mmio intercepts

El 02/07/15 a les 17.02, Paul Durrant ha escrit:

Quoted 6 lines
Quoted 6 lines

-----Original Message-----
From: Roger Pau Monné [mailto:[email protected]]
Sent: 02 July 2015 15:52
To: Paul Durrant; [email protected]
Cc: Andrew Cooper; Keir (Xen.org); Jan Beulich
Subject: Re: [Xen-devel] [PATCH v5 05/16] x86/hvm: unify internal portio

and

Quoted 13 lines
Quoted 12 lines

mmio intercepts

El 30/06/15 a les 15.05, Paul Durrant ha escrit:
[...]
+void msixtbl_init(struct domain *d)

xen/arch/x86/hvm/vmsi.c found: · line 486

Quoted 2 lines
xen/arch/x86/hvm/vmsi.c +2 -0
+{
+    register_mmio_handler(d, &msixtbl_mmio_ops);

Since you are adding an initialization function to vmsi I think msixtbl_list and msixtbl_list_lock should also be initialized here instead of hvm_domain_initialise.

Quoted 6 lines
Quoted 1 line

Yes, that sounds quite reasonable. Perhaps as a follow-up patch though,

unless I need to post v6 of the series for some other reason.

That's fine, if not I can also add it as a pre-patch to my HVMlite stuff, it's trivial.

Sure. Thanks,

Paul

Quoted 1 line

Roger.

#6 lore raw

Re: [PATCH v5 02/16] x86/hvm: remove multiple open coded 'chunking' loops

In reply to Paul Durrant
Quoted 26 lines · Paul Durrant

...in hvmemul_read/write()

Add hvmemul_phys_mmio_access() and hvmemul_linear_mmio_access() functions to reduce code duplication.

NOTE: This patch also introduces a change in 'chunking' around a page
boundary. Previously (for example) an 8 byte access at the last byte of a page would get carried out as 8 single-byte accesses. It will now be carried out as a single-byte access, followed by a 4-byte access, a 2-byte access and then another single-byte access.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c |  223 +++++++++++++++++++++++---------------------
 1 file changed, 116 insertions(+), 107 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -0
index 8b60843..b67f5db 100644
@@ -539,6 +539,117 @@ static int hvmemul_virtual_to_linear(
     return X86EMUL_EXCEPTION;

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 549

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +8 -0
 }
 
+static int hvmemul_phys_mmio_access(
+    paddr_t gpa, unsigned int size, uint8_t dir, uint8_t *buffer)
+{
+    unsigned long one_rep = 1;
+    unsigned int chunk;
+    int rc;
+
+    /* Accesses must fall within a page */

Full stop.

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 550

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    BUG_ON((gpa & (PAGE_SIZE - 1)) + size > PAGE_SIZE);

~PAGE_MASK as opposed to (PAGE_SIZE - 1)

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 557

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+
+    /*
+     * hvmemul_do_io() cannot handle non-power-of-2 accesses or
+     * accesses larger than sizeof(long), so choose the highest power
+     * of 2 not exceeding sizeof(long) as the 'chunk' size.
+     */
+    chunk = 1 << (fls(size) - 1);

Depending on size, chunk can become undefined (shifting by 31 or -1) or zero (shifting by 32).

How about

if ( size > sizeof(long) )

    chunk = sizeof(long);
else
    chunk = 1U << (fls(size) - 1);

?

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 568

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +11 -0
+    if ( chunk > sizeof (long) )
+        chunk = sizeof (long);
+
+    for ( ;; )
+    {
+        rc = hvmemul_do_mmio_buffer(gpa, &one_rep, chunk, dir, 0,
+                                    buffer);
+        if ( rc != X86EMUL_OKAY )
+            break;
+
+        /* Advance to the next chunk */

Full stop.

Quoted 14 lines
xen/arch/x86/hvm/emulate.c +14 -0
+        gpa += chunk;
+        buffer += chunk;
+        size -= chunk;
+
+        if ( size == 0 )
+            break;
+
+        /*
+         * If the chunk now exceeds the remaining size, choose the next
+         * lowest power of 2 that will fit.
+         */
+        while ( chunk > size )
+            chunk >>= 1;
+    }

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 592

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+
+    return rc;
+}
+
+static int hvmemul_linear_mmio_access(
+    unsigned long gla, unsigned int size, uint8_t dir, uint8_t *buffer,
+    uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt, bool_t known_gpfn)
+{
+    struct hvm_vcpu_io *vio = &current->arch.hvm_vcpu.hvm_io;
+    unsigned long page_off = gla & (PAGE_SIZE - 1);

Can be int as opposed to long, and "offset" appears to be the prevailing name. Also, ~PAGE_MASK.

Quoted 21 lines
xen/arch/x86/hvm/emulate.c +21 -0
+    unsigned int chunk;
+    paddr_t gpa;
+    unsigned long one_rep = 1;
+    int rc;
+
+    chunk = min_t(unsigned int, size, PAGE_SIZE - page_off);
+
+    if ( known_gpfn )
+        gpa = pfn_to_paddr(vio->mmio_gpfn) | page_off;
+    else
+    {
+        rc = hvmemul_linear_to_phys(gla, &gpa, chunk, &one_rep, pfec,
+                                    hvmemul_ctxt);
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+    }
+
+    for ( ;; )
+    {
+        rc = hvmemul_phys_mmio_access(gpa, chunk, dir, buffer);
+        if ( rc != X86EMUL_OKAY )

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 623

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+            break;
+
+        gla += chunk;
+        buffer += chunk;
+        size -= chunk;
+
+        if ( size == 0 )
+            break;
+
+        ASSERT((gla & (PAGE_SIZE - 1)) == 0);

~PAGE_MASK.

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 624

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+        ASSERT(size < PAGE_SIZE);

Nothing I can see here prevents size being greater than PAGE_SIZE. chunk strictly will be, but size -= chunk can still leave size greater than a page.

~Andrew

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+        chunk = size;
+        rc = hvmemul_linear_to_phys(gla, &gpa, chunk, &one_rep, pfec,
+                                    hvmemul_ctxt);
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+    }
+
+    return rc;
+}
+
#12 lore raw

Re: [PATCH v5 04/16] x86/hvm: restrict port numbers to uint16_t and sizes to unsigned int

In reply to Paul Durrant
Quoted 6 lines · Paul Durrant
xen/include/asm-x86/hvm/io.h +0 -0
index d1d79dc..1ed0c13 100644
@@ -41,12 +41,12 @@ typedef int (*hvm_mmio_write_t)(struct vcpu *v,
 typedef int (*hvm_mmio_check_t)(struct vcpu *v, unsigned long addr);

xen/include/asm-x86/hvm/io.h typedef int (*hvm_mmio_write_t)(struct vcpu *v, · line 49

Quoted 10 lines
xen/include/asm-x86/hvm/io.h +2 -2
 
 typedef int (*portio_action_t)(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val);
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val);
 typedef int (*mmio_action_t)(ioreq_t *);
 struct io_handler {
     int                 type;
     unsigned long       addr;
-    unsigned long       size;
+    unsigned int        size;

You want to reorder size above addr to fill a 4 byte hole and avoid introducing a second.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#7 lore raw

Re: [PATCH v5 02/16] x86/hvm: remove multiple open coded 'chunking' loops

In reply to Andrew Cooper
Quoted 65 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 02 July 2015 16:38
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [PATCH v5 02/16] x86/hvm: remove multiple open coded 'chunking' loops

Quoted 3 lines · Paul Durrant

...in hvmemul_read/write()

Add hvmemul_phys_mmio_access() and hvmemul_linear_mmio_access()

functions

Quoted 9 lines

to reduce code duplication.

NOTE: This patch also introduces a change in 'chunking' around a page
boundary. Previously (for example) an 8 byte access at the last byte of a page would get carried out as 8 single-byte accesses. It will now be carried out as a single-byte access, followed by a 4-byte access, a 2-byte access and then another single-byte access.

Quoted 6 lines
Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c |  223 +++++++++++++++++++++++----------

-----------

Quoted 8 lines
 1 file changed, 116 insertions(+), 107 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -0
index 8b60843..b67f5db 100644
@@ -539,6 +539,117 @@ static int hvmemul_virtual_to_linear(
     return X86EMUL_EXCEPTION;

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 549

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +8 -0
 }
 
+static int hvmemul_phys_mmio_access(
+    paddr_t gpa, unsigned int size, uint8_t dir, uint8_t *buffer)
+{
+    unsigned long one_rep = 1;
+    unsigned int chunk;
+    int rc;
+
+    /* Accesses must fall within a page */

Full stop.

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 550

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    BUG_ON((gpa & (PAGE_SIZE - 1)) + size > PAGE_SIZE);

~PAGE_MASK as opposed to (PAGE_SIZE - 1)

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 557

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+
+    /*
+     * hvmemul_do_io() cannot handle non-power-of-2 accesses or
+     * accesses larger than sizeof(long), so choose the highest power
+     * of 2 not exceeding sizeof(long) as the 'chunk' size.
+     */
+    chunk = 1 << (fls(size) - 1);

Depending on size, chunk can become undefined (shifting by 31 or -1) or zero (shifting by 32).

Quoted 7 lines

How about

if ( size > sizeof(long) )

    chunk = sizeof(long);
else
    chunk = 1U << (fls(size) - 1);

fls(size) - 1 can't be more than 31 (since size is an unsigned int). I can assert size != 0. So would

chunk = 1u << (fls(size) - 1);

be ok? (I.e. I just missed the 'u' suffix before).

Quoted 38 lines

?

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 568

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +11 -0
+    if ( chunk > sizeof (long) )
+        chunk = sizeof (long);
+
+    for ( ;; )
+    {
+        rc = hvmemul_do_mmio_buffer(gpa, &one_rep, chunk, dir, 0,
+                                    buffer);
+        if ( rc != X86EMUL_OKAY )
+            break;
+
+        /* Advance to the next chunk */

Full stop.

Quoted 11 lines
xen/arch/x86/hvm/emulate.c +11 -0
+        gpa += chunk;
+        buffer += chunk;
+        size -= chunk;
+
+        if ( size == 0 )
+            break;
+
+        /*
+         * If the chunk now exceeds the remaining size, choose the next
+         * lowest power of 2 that will fit.
+         */

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear(

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+        while ( chunk > size )
+            chunk >>= 1;
+    }
+
+    return rc;
+}
+
+static int hvmemul_linear_mmio_access(
+    unsigned long gla, unsigned int size, uint8_t dir, uint8_t *buffer,
+    uint32_t pfec, struct hvm_emulate_ctxt *hvmemul_ctxt, bool_t

known_gpfn)

Quoted 6 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 592

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +3 -0
+{
+    struct hvm_vcpu_io *vio = &current->arch.hvm_vcpu.hvm_io;
+    unsigned long page_off = gla & (PAGE_SIZE - 1);

Can be int as opposed to long, and "offset" appears to be the prevailing name. Also, ~PAGE_MASK.

Ok.

Quoted 35 lines
Quoted 22 lines
xen/arch/x86/hvm/emulate.c +21 -0
+    unsigned int chunk;
+    paddr_t gpa;
+    unsigned long one_rep = 1;
+    int rc;
+
+    chunk = min_t(unsigned int, size, PAGE_SIZE - page_off);
+
+    if ( known_gpfn )
+        gpa = pfn_to_paddr(vio->mmio_gpfn) | page_off;
+    else
+    {
+        rc = hvmemul_linear_to_phys(gla, &gpa, chunk, &one_rep, pfec,
+                                    hvmemul_ctxt);
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+    }
+
+    for ( ;; )
+    {
+        rc = hvmemul_phys_mmio_access(gpa, chunk, dir, buffer);
+        if ( rc != X86EMUL_OKAY )

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 623

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+            break;
+
+        gla += chunk;
+        buffer += chunk;
+        size -= chunk;
+
+        if ( size == 0 )
+            break;
+
+        ASSERT((gla & (PAGE_SIZE - 1)) == 0);

~PAGE_MASK.

Quoted 6 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 624

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+        ASSERT(size < PAGE_SIZE);

Nothing I can see here prevents size being greater than PAGE_SIZE. chunk strictly will be, but size -= chunk can still leave size greater than a page.

Ok, I'll allow for size >= PAGE_SIZE.

Paul

Quoted 12 lines

~Andrew

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+        chunk = size;
+        rc = hvmemul_linear_to_phys(gla, &gpa, chunk, &one_rep, pfec,
+                                    hvmemul_ctxt);
+        if ( rc != X86EMUL_OKAY )
+            return rc;
+    }
+
+    return rc;
+}
+
#13 lore raw

Re: [PATCH v5 04/16] x86/hvm: restrict port numbers to uint16_t and sizes to unsigned int

In reply to Andrew Cooper
Quoted 17 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 02 July 2015 16:55
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [PATCH v5 04/16] x86/hvm: restrict port numbers to uint16_t and sizes to unsigned int

xen/include/asm-

Quoted 1 line · Paul Durrant
xen/include/asm- renamed from xen/include/asm-x86/hvm/io.h +0 -0

x86/hvm/io.h

Quoted 6 lines

index d1d79dc..1ed0c13 100644

xen/include/asm-x86/hvm/io.h +0 -0
@@ -41,12 +41,12 @@ typedef int (*hvm_mmio_write_t)(struct vcpu *v,
 typedef int (*hvm_mmio_check_t)(struct vcpu *v, unsigned long addr);
 
Quoted 13 lines

xen/include/asm-x86/hvm/io.h typedef int (*hvm_mmio_write_t)(struct vcpu *v, · line 49

Quoted 9 lines
xen/include/asm-x86/hvm/io.h +2 -2
 typedef int (*portio_action_t)(
-    int dir, uint32_t port, uint32_t bytes, uint32_t *val);
+    int dir, uint16_t port, unsigned int bytes, uint32_t *val);
 typedef int (*mmio_action_t)(ioreq_t *);
 struct io_handler {
     int                 type;
     unsigned long       addr;
-    unsigned long       size;
+    unsigned int        size;

You want to reorder size above addr to fill a 4 byte hole and avoid introducing a second.

Sure.

Paul

Quoted 1 line

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#8 lore raw

Re: [PATCH v5 02/16] x86/hvm: remove multiple open coded 'chunking' loops

In reply to Paul Durrant
Quoted 62 lines · Paul Durrant
Quoted 62 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 02 July 2015 16:38
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [PATCH v5 02/16] x86/hvm: remove multiple open coded 'chunking' loops

Quoted 3 lines · Paul Durrant

...in hvmemul_read/write()

Add hvmemul_phys_mmio_access() and hvmemul_linear_mmio_access()

functions

Quoted 9 lines

to reduce code duplication.

NOTE: This patch also introduces a change in 'chunking' around a page
boundary. Previously (for example) an 8 byte access at the last byte of a page would get carried out as 8 single-byte accesses. It will now be carried out as a single-byte access, followed by a 4-byte access, a 2-byte access and then another single-byte access.

Quoted 6 lines
Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/emulate.c |  223 +++++++++++++++++++++++----------

-----------

Quoted 8 lines
 1 file changed, 116 insertions(+), 107 deletions(-)
xen/arch/x86/hvm/emulate.c +0 -0
index 8b60843..b67f5db 100644
@@ -539,6 +539,117 @@ static int hvmemul_virtual_to_linear(
     return X86EMUL_EXCEPTION;

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 549

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +8 -0
 }
 
+static int hvmemul_phys_mmio_access(
+    paddr_t gpa, unsigned int size, uint8_t dir, uint8_t *buffer)
+{
+    unsigned long one_rep = 1;
+    unsigned int chunk;
+    int rc;
+
+    /* Accesses must fall within a page */

Full stop.

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 550

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    BUG_ON((gpa & (PAGE_SIZE - 1)) + size > PAGE_SIZE);

~PAGE_MASK as opposed to (PAGE_SIZE - 1)

xen/arch/x86/hvm/emulate.c static int hvmemul_virtual_to_linear( · line 557

Quoted 7 lines
xen/arch/x86/hvm/emulate.c +7 -0
+
+    /*
+     * hvmemul_do_io() cannot handle non-power-of-2 accesses or
+     * accesses larger than sizeof(long), so choose the highest power
+     * of 2 not exceeding sizeof(long) as the 'chunk' size.
+     */
+    chunk = 1 << (fls(size) - 1);

Depending on size, chunk can become undefined (shifting by 31 or -1) or zero (shifting by 32).

Quoted 8 lines
Quoted 7 lines

How about

if ( size > sizeof(long) )

    chunk = sizeof(long);
else
    chunk = 1U << (fls(size) - 1);

fls(size) - 1 can't be more than 31 (since size is an unsigned int). I can assert size != 0. So would

Ah yes - very true (until someone goes and changes size to an unsigned long)

Size being 0 is probably the kind of thing which needs checking at the very top of the emulation calltree and assumed thereafter. Might also want an upper sanity check as well.

Quoted 4 lines

chunk = 1u << (fls(size) - 1);

be ok? (I.e. I just missed the 'u' suffix before).

Yeah.

~Andrew

#19 lore raw

Re: [PATCH v5 05/16] x86/hvm: unify internal portio and mmio intercepts

In reply to Paul Durrant

xen/arch/x86/hvm/intercept.c line 83

Quoted 9 lines · Paul Durrant
xen/arch/x86/hvm/intercept.c +5 -3
-bool_t hvm_mmio_internal(paddr_t gpa)
+static int hvm_portio_read(const struct hvm_io_handler *handler,
+                           uint64_t addr,
+                           uint32_t size,
+                           uint64_t *data)
 {
-    struct vcpu *curr = current;
-    unsigned int i;
+    uint32_t val = 0;

Default to ~0U to match real hardware.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#21 lore raw

Re: [PATCH v5 06/16] x86/hvm: add length to mmio check op

In reply to Paul Durrant
Quoted 19 lines · Paul Durrant

When memory mapped I/O is range checked by internal handlers, the length of the access should be taken into account.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/intercept.c |   22 +++++++++++++++++++---
 xen/include/asm-x86/hvm/io.h |   16 ++++++++++++++++
 2 files changed, 35 insertions(+), 3 deletions(-)
xen/arch/x86/hvm/intercept.c +0 -0
index 7d36785..42050f4 100644
@@ -35,9 +35,19 @@
 static bool_t hvm_mmio_accept(const struct hvm_io_handler *handler,
                               const ioreq_t *p)

xen/arch/x86/hvm/intercept.c line 45

Quoted 10 lines
xen/arch/x86/hvm/intercept.c +6 -1
 {
+    paddr_t first = hvm_mmio_first_byte(p);
+    paddr_t last = hvm_mmio_last_byte(p);
+
     BUG_ON(handler->type != IOREQ_TYPE_COPY);
 
-    return handler->mmio.ops->check(current, p->addr);
+    if ( !handler->mmio.ops->check(current, first) )
+        return 0;
+

I would put a comment here about an IO access straddling an MMIO handler boundary, so that someone investigating this domain crash gets some clue as to why.

Quoted 29 lines
xen/arch/x86/hvm/intercept.c +10 -1
+    if ( p->size > 1 &&
+         !handler->mmio.ops->check(current, last) )
+        domain_crash(current->domain);
+
+    return 1;
 }
 
 static int hvm_mmio_read(const struct hvm_io_handler *handler,
@@ -112,7 +122,8 @@ static const struct hvm_io_ops portio_ops = {
 static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
                                     ioreq_t *p)
 {
-    struct hvm_vcpu_io *vio = &current->arch.hvm_vcpu.hvm_io;
+    struct vcpu *curr = current;
+    struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
     const struct hvm_io_ops *ops =
         (p->type == IOREQ_TYPE_COPY) ?
         &mmio_ops :
@@ -223,6 +234,9 @@ static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
 
     if ( i != 0 )
     {
+        if ( rc == X86EMUL_UNHANDLEABLE )
+            domain_crash(curr->domain);
+
         p->count = i;
         rc = X86EMUL_OKAY;
     }

xen/arch/x86/hvm/intercept.c bool_t hvm_mmio_internal(paddr_t gpa) · line 359

Quoted 5 lines
xen/arch/x86/hvm/intercept.c +1 -1
@@ -342,7 +356,9 @@ bool_t hvm_mmio_internal(paddr_t gpa)
 {
     ioreq_t p = {
         .type = IOREQ_TYPE_COPY,
-        .addr = gpa
+        .addr = gpa,

As a general note, many compilers (gcc includes) permit having a comma as the final token before the } which avoids a diff which looks like this when adding a subsequent member.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#23 lore raw

Re: [PATCH v5 07/16] x86/hvm: unify dpci portio intercept with standard portio intercept

In reply to Paul Durrant
Quoted 81 lines · Paul Durrant

This patch re-works the dpci portio intercepts so that they can be unified with standard portio handling thereby removing a substantial amount of code duplication.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
---
 xen/arch/x86/hvm/hvm.c         |    2 +
 xen/arch/x86/hvm/intercept.c   |   22 ++--
 xen/arch/x86/hvm/io.c          |  220 ++++++++++++----------------------------
 xen/include/asm-x86/hvm/io.h   |    5 +
 xen/include/asm-x86/hvm/vcpu.h |    2 +
 xen/include/xen/iommu.h        |    1 -
 6 files changed, 80 insertions(+), 172 deletions(-)
xen/arch/x86/hvm/hvm.c +2 -0
index 60f8972..6643e0c 100644
@@ -1486,6 +1486,8 @@ int hvm_domain_initialise(struct domain *d)
     else
         d->arch.hvm_domain.io_bitmap = hvm_io_bitmap;
 
+    register_dpci_portio_handler(d);
+
     if ( is_pvh_domain(d) )
     {
         register_portio_handler(d, 0, 0x10003, handle_pvh_io);
xen/arch/x86/hvm/intercept.c +3 -15
index 42050f4..a613865 100644
@@ -124,10 +124,7 @@ static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
 {
     struct vcpu *curr = current;
     struct hvm_vcpu_io *vio = &curr->arch.hvm_vcpu.hvm_io;
-    const struct hvm_io_ops *ops =
-        (p->type == IOREQ_TYPE_COPY) ?
-        &mmio_ops :
-        &portio_ops;
+    const struct hvm_io_ops *ops = handler->ops;
     int rc = X86EMUL_OKAY, i, step = p->df ? -p->size : p->size;
     uint64_t data;
     uint64_t addr;
@@ -247,10 +244,6 @@ static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
 static const struct hvm_io_handler *hvm_find_io_handler(ioreq_t *p)
 {
     struct domain *curr_d = current->domain;
-    const struct hvm_io_ops *ops =
-        (p->type == IOREQ_TYPE_COPY) ?
-        &mmio_ops :
-        &portio_ops;
     unsigned int i;
 
     BUG_ON((p->type != IOREQ_TYPE_PIO) &&
@@ -260,6 +253,7 @@ static const struct hvm_io_handler *hvm_find_io_handler(ioreq_t *p)
     {
         const struct hvm_io_handler *handler =
             &curr_d->arch.hvm_domain.io_handler[i];
+        const struct hvm_io_ops *ops = handler->ops;
 
         if ( handler->type != p->type )
             continue;
@@ -275,13 +269,7 @@ int hvm_io_intercept(ioreq_t *p)
 {
     const struct hvm_io_handler *handler;
 
-    if ( p->type == IOREQ_TYPE_PIO )
-    {
-        int rc = dpci_ioport_intercept(p);
-        if ( (rc == X86EMUL_OKAY) || (rc == X86EMUL_RETRY) )
-            return rc;
-    }
-    else if ( p->type == IOREQ_TYPE_COPY )
+    if ( p->type == IOREQ_TYPE_COPY )
     {
         int rc = stdvga_intercept_mmio(p);
         if ( (rc == X86EMUL_OKAY) || (rc == X86EMUL_RETRY) )

xen/arch/x86/hvm/intercept.c int hvm_io_intercept(ioreq_t *p) · line 287

Quoted 5 lines
xen/arch/x86/hvm/intercept.c +1 -1
@@ -296,7 +284,7 @@ int hvm_io_intercept(ioreq_t *p)
     return hvm_process_io_intercept(handler, p);
 }
 
-static struct hvm_io_handler *hvm_next_io_handler(struct domain *d)
+struct hvm_io_handler *hvm_next_io_handler(struct domain *d)

Instead of introducing this as a static in patch 5, then modifying it here to export it, introduce it directly as an exported function in 5.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#25 lore raw

Re: [PATCH v5 08/16] x86/hvm: unify stdvga mmio intercept with standard mmio intercept

In reply to Paul Durrant

xen/arch/x86/hvm/stdvga.c static uint8_t stdvga_mem_readb(uint64_t addr) · line 281

Quoted 9 lines · Paul Durrant
xen/arch/x86/hvm/stdvga.c +2 -1
@@ -275,7 +275,8 @@ static uint8_t stdvga_mem_readb(uint64_t addr)
     return ret;
 }
 
-static uint64_t stdvga_mem_read(uint64_t addr, uint64_t size)
+static int stdvga_mem_read(const struct hvm_io_handler *handler,
+                           uint64_t addr, uint32_t size, uint64_t *p_data)
 {
     uint64_t data = 0;

This initialiser wants to default to ~0ULL.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#27 lore raw

Re: [PATCH v5 09/16] x86/hvm: limit reps to avoid the need to handle retry

In reply to Paul Durrant
Quoted 10 lines · Paul Durrant
xen/arch/x86/hvm/emulate.c +1 -1
@@ -235,7 +219,7 @@ static int hvmemul_do_io_buffer(
 
     BUG_ON(buffer == NULL);
 
-    rc = hvmemul_do_io(is_mmio, addr, reps, size, dir, df, 0,
+    rc = hvmemul_do_io(is_mmio, addr, *reps, size, dir, df, 0,
                        (uintptr_t)buffer);
     if ( rc == X86EMUL_UNHANDLEABLE && dir == IOREQ_READ )
         memset(buffer, 0xff, size);

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 274

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +1 -1
@@ -287,17 +271,53 @@ static int hvmemul_do_io_addr(
     bool_t is_mmio, paddr_t addr, unsigned long *reps,
     unsigned int size, uint8_t dir, bool_t df, paddr_t ram_gpa)
 {
-    struct page_info *ram_page;
+    struct vcpu *v = current;

curr.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 275

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    unsigned long ram_gmfn = paddr_to_pfn(ram_gpa);

ram_gfn.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 276

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    unsigned int page_off = ram_gpa & (PAGE_SIZE - 1);

offset and ~PAGE_MASK.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 278

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+    struct page_info *ram_page[2];
+    int nr_pages = 0;

unsigned int.

Quoted 17 lines
xen/arch/x86/hvm/emulate.c +10 -3
+    unsigned long count;
     int rc;
 
-    rc = hvmemul_acquire_page(paddr_to_pfn(ram_gpa), &ram_page);
+    rc = hvmemul_acquire_page(ram_gmfn, &ram_page[nr_pages]);
     if ( rc != X86EMUL_OKAY )
-        return rc;
+        goto out;
 
-    rc = hvmemul_do_io(is_mmio, addr, reps, size, dir, df, 1,
+    nr_pages++;
+
+    /* Detemine how many reps will fit within this page */
+    count = min_t(unsigned long,
+                  *reps,
+                  df ?
+                  (page_off + size - 1) / size :

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 302

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +10 -0
+                  (PAGE_SIZE - page_off) / size);
+
+    if ( count == 0 )
+    {
+        /*
+         * This access must span two pages, so grab a reference to
+         * the next page and do a single rep.
+         */
+        rc = hvmemul_acquire_page(df ? ram_gmfn - 1 : ram_gmfn + 1,
+                                  &ram_page[nr_pages]);

All guest-based ways to trigger an IO spanning a page boundary will be based on linear address. If a guest has paging enabled, this movement to an adjacent physical is not valid. A new pagetable walk will be required to determine the correct second page.

~Andrew

#28 lore raw

Re: [PATCH v5 09/16] x86/hvm: limit reps to avoid the need to handle retry

Quoted 64 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 02 July 2015 18:11
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [PATCH v5 09/16] x86/hvm: limit reps to avoid the need to handle retry

Quoted 10 lines · Paul Durrant
xen/arch/x86/hvm/emulate.c +1 -1
@@ -235,7 +219,7 @@ static int hvmemul_do_io_buffer(
 
     BUG_ON(buffer == NULL);
 
-    rc = hvmemul_do_io(is_mmio, addr, reps, size, dir, df, 0,
+    rc = hvmemul_do_io(is_mmio, addr, *reps, size, dir, df, 0,
                        (uintptr_t)buffer);
     if ( rc == X86EMUL_UNHANDLEABLE && dir == IOREQ_READ )
         memset(buffer, 0xff, size);

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 274

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +1 -1
@@ -287,17 +271,53 @@ static int hvmemul_do_io_addr(
     bool_t is_mmio, paddr_t addr, unsigned long *reps,
     unsigned int size, uint8_t dir, bool_t df, paddr_t ram_gpa)
 {
-    struct page_info *ram_page;
+    struct vcpu *v = current;

curr.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 275

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    unsigned long ram_gmfn = paddr_to_pfn(ram_gpa);

ram_gfn.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 276

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    unsigned int page_off = ram_gpa & (PAGE_SIZE - 1);

offset and ~PAGE_MASK.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 278

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+    struct page_info *ram_page[2];
+    int nr_pages = 0;

unsigned int.

Quoted 24 lines
xen/arch/x86/hvm/emulate.c +17 -3
+    unsigned long count;
     int rc;
 
-    rc = hvmemul_acquire_page(paddr_to_pfn(ram_gpa), &ram_page);
+    rc = hvmemul_acquire_page(ram_gmfn, &ram_page[nr_pages]);
     if ( rc != X86EMUL_OKAY )
-        return rc;
+        goto out;
 
-    rc = hvmemul_do_io(is_mmio, addr, reps, size, dir, df, 1,
+    nr_pages++;
+
+    /* Detemine how many reps will fit within this page */
+    count = min_t(unsigned long,
+                  *reps,
+                  df ?
+                  (page_off + size - 1) / size :
+                  (PAGE_SIZE - page_off) / size);
+
+    if ( count == 0 )
+    {
+        /*
+         * This access must span two pages, so grab a reference to
+         * the next page and do a single rep.
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 302

Quoted 3 lines
xen/arch/x86/hvm/emulate.c +3 -0
+         */
+        rc = hvmemul_acquire_page(df ? ram_gmfn - 1 : ram_gmfn + 1,
+                                  &ram_page[nr_pages]);

All guest-based ways to trigger an IO spanning a page boundary will be based on linear address. If a guest has paging enabled, this movement to an adjacent physical is not valid. A new pagetable walk will be required to determine the correct second page.

I don't think that is true. hvmemul_linear_to_phys() will break at non-contiguous boundaries.

Paul

Quoted 2 lines

~Andrew

#29 lore raw

Re: [PATCH v5 09/16] x86/hvm: limit reps to avoid the need to handle retry

Quoted 67 lines · Paul Durrant
Quoted 59 lines

-----Original Message-----
From: Andrew Cooper [mailto:[email protected]]
Sent: 02 July 2015 18:11
To: Paul Durrant; [email protected]
Cc: Keir (Xen.org); Jan Beulich
Subject: Re: [PATCH v5 09/16] x86/hvm: limit reps to avoid the need to handle retry

Quoted 10 lines · Paul Durrant
xen/arch/x86/hvm/emulate.c +1 -1
@@ -235,7 +219,7 @@ static int hvmemul_do_io_buffer(
 
     BUG_ON(buffer == NULL);
 
-    rc = hvmemul_do_io(is_mmio, addr, reps, size, dir, df, 0,
+    rc = hvmemul_do_io(is_mmio, addr, *reps, size, dir, df, 0,
                        (uintptr_t)buffer);
     if ( rc == X86EMUL_UNHANDLEABLE && dir == IOREQ_READ )
         memset(buffer, 0xff, size);

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 274

Quoted 5 lines
xen/arch/x86/hvm/emulate.c +1 -1
@@ -287,17 +271,53 @@ static int hvmemul_do_io_addr(
     bool_t is_mmio, paddr_t addr, unsigned long *reps,
     unsigned int size, uint8_t dir, bool_t df, paddr_t ram_gpa)
 {
-    struct page_info *ram_page;
+    struct vcpu *v = current;

curr.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 275

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    unsigned long ram_gmfn = paddr_to_pfn(ram_gpa);

ram_gfn.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 276

Quoted 1 line
xen/arch/x86/hvm/emulate.c +1 -0
+    unsigned int page_off = ram_gpa & (PAGE_SIZE - 1);

offset and ~PAGE_MASK.

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 278

Quoted 2 lines
xen/arch/x86/hvm/emulate.c +2 -0
+    struct page_info *ram_page[2];
+    int nr_pages = 0;

unsigned int.

Quoted 23 lines
xen/arch/x86/hvm/emulate.c +16 -3
+    unsigned long count;
     int rc;
 
-    rc = hvmemul_acquire_page(paddr_to_pfn(ram_gpa), &ram_page);
+    rc = hvmemul_acquire_page(ram_gmfn, &ram_page[nr_pages]);
     if ( rc != X86EMUL_OKAY )
-        return rc;
+        goto out;
 
-    rc = hvmemul_do_io(is_mmio, addr, reps, size, dir, df, 1,
+    nr_pages++;
+
+    /* Detemine how many reps will fit within this page */
+    count = min_t(unsigned long,
+                  *reps,
+                  df ?
+                  (page_off + size - 1) / size :
+                  (PAGE_SIZE - page_off) / size);
+
+    if ( count == 0 )
+    {
+        /*
+         * This access must span two pages, so grab a reference to
Quoted 8 lines

xen/arch/x86/hvm/emulate.c static int hvmemul_do_io_addr( · line 302

Quoted 4 lines
xen/arch/x86/hvm/emulate.c +4 -0
+         * the next page and do a single rep.
+         */
+        rc = hvmemul_acquire_page(df ? ram_gmfn - 1 : ram_gmfn + 1,
+                                  &ram_page[nr_pages]);

All guest-based ways to trigger an IO spanning a page boundary will be based on linear address. If a guest has paging enabled, this movement to an adjacent physical is not valid. A new pagetable walk will be required to determine the correct second page.

Quoted 1 line

I don't think that is true. hvmemul_linear_to_phys() will break at non-contiguous boundaries.

Hmm - it looks like it will bail with unhandleable on a straddled access across a non-contiguous boundary. In which case a comment confirming the safety of the +/- 1 will be useful to the next person who follows the same track of logic as I did.

~Andrew

#31 lore raw

Re: [PATCH v5 10/16] x86/hvm: only call hvm_io_assist() from hvm_wait_for_io()

In reply to Paul Durrant

xen/arch/x86/hvm/intercept.c static const struct hvm_io_ops portio_ops = { · line 123

Quoted 12 lines · Paul Durrant
xen/arch/x86/hvm/intercept.c +2 -2
index 11b6028..f84f7c5 100644
@@ -119,8 +119,8 @@ static const struct hvm_io_ops portio_ops = {
     .write = hvm_portio_write
 };
 
-static int hvm_process_io_intercept(const struct hvm_io_handler *handler,
-                                    ioreq_t *p)
+int hvm_process_io_intercept(const struct hvm_io_handler *handler,
+                             ioreq_t *p)

Again, I would suggest introducing this as a global function straight away, to reduce the eventual churn.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

#33 lore raw

Re: [PATCH v5 11/16] x86/hvm: split I/O completion handling from state model

In reply to Paul Durrant
Quoted 5 lines · Paul Durrant
xen/include/asm-x86/hvm/vcpu.h +0 -0
index 2ed0606..efb373f 100644

xen/include/asm-x86/hvm/vcpu.h enum hvm_io_state { · line 41

Quoted 10 lines
xen/include/asm-x86/hvm/vcpu.h +2 -2
@@ -34,11 +34,16 @@ enum hvm_io_state {
     HVMIO_none = 0,
     HVMIO_dispatched,
     HVMIO_awaiting_completion,
-    HVMIO_handle_mmio_awaiting_completion,
-    HVMIO_handle_pio_awaiting_completion,
     HVMIO_completed
 };
 
+enum hvm_io_completion {
+    HVMIO_no_completion = 0,

No need to default to 0 here.

Otherwise, Reviewed-by: Andrew Cooper <[email protected]>

Reviewed-by on v5 12/16
#35 lore raw

Re: [PATCH v5 12/16] x86/hvm: remove HVMIO_dispatched I/O state

In reply to Paul Durrant
Quoted 18 lines · Paul Durrant

By removing the HVMIO_dispatched state and making all pending emulations (i.e. all those not handled by the hypervisor) use HVMIO_awating_completion, various code-paths can be simplified.

The completion case for HVMIO_dispatched can also be trivally removed from hvmemul_do_io() as it was already unreachable. This is because that state was only ever used for writes or I/O to/from a guest page and hvmemul_do_io() is never called to complete such I/O.

NOTE: There is one sublety in handle_pio()... The only case when
handle_pio() got a return code of X86EMUL_RETRY back from hvmemul_do_pio_buffer() and found io_state was not HVMIO_awaiting_completion was in the case where the domain is shutting down. This is because all writes normally yield a return of HVMEMUL_OKAY and all reads put io_state into HVMIO_awaiting_completion. Hence the io_state check there is replaced with a check of the is_shutting_down flag on the domain.

Quoted 4 lines
Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Reviewed-by on v5 13/16
#37 lore raw

Re: [PATCH v5 13/16] x86/hvm: remove hvm_io_state enumeration

In reply to Paul Durrant
Quoted 11 lines · Paul Durrant

Emulation request status is already covered by STATE_IOREQ_XXX values so just use those. The mapping is:

HVMIO_none -> STATE_IOREQ_NONE
HVMIO_awaiting_completion -> STATE_IOREQ_READY
HVMIO_completed -> STATE_IORESP_READY

Reviewed-by on v5 14/16
#39 lore raw

Re: [PATCH v5 14/16] x86/hvm: use ioreq_t to track in-flight state

In reply to Paul Durrant
Quoted 8 lines · Paul Durrant

Use an ioreq_t rather than open coded state, size, dir and data fields in struct hvm_vcpu_io. This also allows PIO completion to be handled similarly to MMIO completion by re-issuing the handle_pio() call.

Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
Reviewed-by on v5 15/16
#41 lore raw

Re: [PATCH v5 15/16] x86/hvm: always re-emulate I/O from a buffer

In reply to Paul Durrant
Quoted 9 lines · Paul Durrant

If memory mapped I/O is 'chunked' then the I/O must be re-emulated, otherwise only the first chunk will be processed. This patch makes sure all I/O from a buffer is re-emulated regardless of whether it is a read or a write.

Reviewed-by on v5 16/16
#43 lore raw

Re: [PATCH v5 16/16] x86/hvm: track large memory mapped accesses by buffer offset

In reply to Paul Durrant
Quoted 10 lines · Paul Durrant

The code in hvmemul_do_io() that tracks large reads or writes, to avoid re-issue of component I/O, is defeated by accesses across a page boundary because it uses physical address. The code is also only relevant to memory mapped I/O to or from a buffer.

This patch re-factors the code and moves it into hvmemul_phys_mmio_access() where it is relevant and tracks using buffer offset rather than address. Separate I/O emulations (of which there may be up to three per instruction) are distinguished by linear address.

Quoted 4 lines
Cc Keir Fraser <[email protected]>
Cc Jan Beulich <[email protected]>
Cc Andrew Cooper <[email protected]>
#45 lore raw
In reply to Fabio Fantoni

Il 30/06/2015 16:48, Fabio Fantoni ha scritto:

Quoted 111 lines

Il 30/06/2015 15:05, Paul Durrant ha scritto:

Quoted 99 lines

This patch series re-works much of the code involved in emulation of port and memory mapped I/O for HVM guests.

The code has become very convoluted and, at least by inspection, certain emulations will apparently malfunction.

The series is broken down into 16 patches (which are also available in my xenbits repo: http://xenbits.xen.org/gitweb/?p=people/pauldu/xen.git
on the emulation32 branch).

Previous changelog
------------------

v2:
- Removed bogus assertion from patch #15
- Re-worked patch #17 after basic testing of back-port onto XenServer

v3:
- Addressed comments from Jan
- Re-ordered series to bring a couple of more trivial patches to the front
- Backport to XenServer (4.5) now passing automated tests
- Tested on unstable with QEMU upstream and trad, with and without HAP (to force shadow emulation)

v4:
- Removed previous patch (make sure translated MMIO reads or writes fall within a page) and rebased rest of series.
- Address Jan's comments on patch #1

Changelog (now per-patch)
-------------------------

0001-x86-hvm-make-sure-emulation-is-retried-if-domain-is-.patch

This is a fix for an issue on staging reported by Don Slutz

0002-x86-hvm-remove-multiple-open-coded-chunking-loops.patch

v5: Addressed further comments from Jan

0003-x86-hvm-change-hvm_mmio_read_t-and-hvm_mmio_write_t-.patch

v5: New patch to tidy up types

0004-x86-hvm-restrict-port-numbers-to-uint16_t-and-sizes-.patch

v5: New patch to tidy up more types

0005-x86-hvm-unify-internal-portio-and-mmio-intercepts.patch

v5: Addressed further comments from Jan and simplified implementation
by passing ioreq_t to accept() function

0006-x86-hvm-add-length-to-mmio-check-op.patch

v5: Simplified by leaving mmio_check() implementation alone and
calling to check last byte if first-byte check passes

0007-x86-hvm-unify-dpci-portio-intercept-with-standard-po.patch

v5: Addressed further comments from Jan

0008-x86-hvm-unify-stdvga-mmio-intercept-with-standard-mm.patch

v5: Fixed semantic problems pointed out by Jan

0009-x86-hvm-limit-reps-to-avoid-the-need-to-handle-retry.patch

v5: Addressed further comments from Jan

0010-x86-hvm-only-call-hvm_io_assist-from-hvm_wait_for_io.patch

v5: Added Jan's acked-by

0011-x86-hvm-split-I-O-completion-handling-from-state-mod.patch

v5: Confirmed call to msix_write_completion() is in the correct place.

0012-x86-hvm-remove-HVMIO_dispatched-I-O-state.patch

v5: Added some extra comments to the commit

0013-x86-hvm-remove-hvm_io_state-enumeration.patch

v5: Added Jan's acked-by

0014-x86-hvm-use-ioreq_t-to-track-in-flight-state.patch

v5: Added missing hunk with call to handle_pio()

0015-x86-hvm-always-re-emulate-I-O-from-a-buffer.patch

v5: Added Jan's acked-by

0016-x86-hvm-track-large-memory-mapped-accesses-by-buffer.patch

v5: Fixed to cache up three distict I/O emulations per instruction

Quoted 10 lines

Testing
-------

The series was been back-ported to staging-4.5 and then dropped onto the XenServer (Dundee) patch queue. All automated branch-safety tests pass.

The series as-is has been manually tested with a Windows 7 (32-bit) VM using upstream QEMU.

Quoted 8 lines

Thanks for your work.
I did some very fast tests, no regression found, on my linux domUs qxl is still not working and I'm unable to debug it. @Jim Fehlig: can you try if qxl is still working at least on suse
dom0/domU after this serie please?

Can someone tell me how to debug the qxl problem now that qemu don't crash anymore but remain at 100% cpu without nothing about in dom0 logs (if exists) please?

Thanks for any reply and sorry for my bad english.

I don't have knowledge about x86 emulation but I'm trying desperately to find the cause of such problems which persists despite this serie of patches. In latest xengt xen patches I saw this patch: "vgt: add support of emulating SSE2 instruction MOVD"
https://github.com/01org/XenGT-Preview-xen/commit/f2bad31f80f698a452c37cb39841da8e4f69350f
This xengt patch is still based on xen 4.5 instead, and x86_emulate.c is different but I noticed some strange things looking at it and also comparing it with upstream staging...

xengt add this:

Quoted 1 line

case 0x7e: /* movd xmm,mm/mm32 */

this seems still missing in upstream staging, should it be added? Should I try the patch with latest upstream xen or does it need other changes? There is also this:

Quoted 1 line

ea.bytes = (b == 0x7e ? 4: 16);

An sse2 istruction of 4 byte seems strange to me, is it right?

In upstream I saw this:

Quoted 6 lines

case 0xe7: /* movntq mm,m64 */

                /* {,v}movntdq xmm,m128 */
                /* vmovntdq ymm,m256 */
         fail_if(ea.type != OP_MEM);
         fail_if(vex.pfx == vex_f3);
         /* fall through */

In the last qemu crash I had with qxl on linux I got this:

Quoted 3 lines

#0 __memset_sse2 () at ../sysdeps/x86_64/multiarch/../memset.S:908 Latest istruction:
=> 0x7ffff3713f7b <__memset_sse2+2363>: movntdq %xmm0,(%rdi)

Is it possible that the "fail_if(ea.type != OP_MEM);" or the other one make a difference?

After applying this patch serie qemu doesn't crash anymore but it remains at 100% cpu and is unusable, I can do only xl destroy, I find nothing in logs and I'm unable to debug it. Is there something I can do to debug this?

I also took a fast look at suse kernel patches (https://github.com/openSUSE/kernel-source/tree/SLE12-SP1) where qxl is also working on linux domUs (other things seems already similar based on what Jim Fehlig told me) but I didn't find a possible fix/workaround for it to try. Can someone tell me about possible patches I should try please?

Any help to find the workaround or fix to apply upstream and have linux domUs working with qxl in every case is appreciated.

_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel