Skip to contentSkip to conversation

xen/x86: fix implementation of subpage r/o MMIO

PATCH Superseded Roger Pau Monne 2 participants 12 messages Apr 17, 2025 – Apr 23, 2025

v3 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

v3 cover xen/x86: fix implementation of subpage r/o MMIO

Hello,

Following series include some bugfixes and improvements to the handling of subpage r/o MMIO accesses.

Patch 1 introduces a helper to reduce code duplication when performing MMIO accesses.

Patch 2 is a bugfix for using subpage in HVM guest, and patch 3 is an optimization to only register the HVM subpage handler when needed.

Finally patch 4 moves some PV only code to a PV specific file.

Thanks, Roger.

Roger Pau Monne (4):
xen/io: provide helpers for multi size MMIO accesses x86/hvm: fix handling of accesses to partial r/o MMIO pages x86/hvm: only register the r/o subpage ops when needed x86/mm: move mmio_ro_emulated_write() to PV only file

 xen/arch/x86/hvm/Makefile              |   1 +
 xen/arch/x86/hvm/emulate.c             |  54 ++---------
 xen/arch/x86/hvm/hvm.c                 |  18 ++--
 xen/arch/x86/hvm/mmio.c                | 125 +++++++++++++++++++++++++
 xen/arch/x86/hvm/vmsi.c                |  47 +---------
 xen/arch/x86/include/asm/hvm/emulate.h |   1 -
 xen/arch/x86/include/asm/hvm/io.h      |   3 +
 xen/arch/x86/include/asm/mm.h          |  21 +++--
 xen/arch/x86/mm.c                      | 101 +++++---------------
 xen/arch/x86/pv/ro-page-fault.c        |  34 +++++++
 xen/drivers/vpci/msix.c                |  47 +---------
 xen/include/xen/io.h                   |  70 ++++++++++++++
 12 files changed, 289 insertions(+), 233 deletions(-)
 create mode 100644 xen/arch/x86/hvm/mmio.c
 create mode 100644 xen/include/xen/io.h
Signature · 1 line
2.48.1
#1 lore raw

PATCH v3 1/4 xen/io: provide helpers for multi size MMIO accesses

Several handlers have the same necessity of reading or writing from or to an MMIO region using 1, 2, 4 or 8 bytes accesses. So far this has been open-coded in the function itself. Instead provide a new set of handlers that encapsulate the accesses.

Since the added helpers are not architecture specific, introduce a new generic io.h header.

No functional change intended.

---
Changes since v2:
- Keep error message when handling non-allowed access sizes.

Changes since v1:
- Limit {read,write}q() to 64bit architectures.
- Always have a default case in switch statement.

---
 xen/arch/x86/hvm/vmsi.c | 47 ++-------------------------
 xen/arch/x86/mm.c       | 30 +++++-------------
 xen/drivers/vpci/msix.c | 47 ++-------------------------
 xen/include/xen/io.h    | 70 +++++++++++++++++++++++++++++++++++++++++
 4 files changed, 83 insertions(+), 111 deletions(-)
 create mode 100644 xen/include/xen/io.h
xen/arch/x86/hvm/vmsi.c +3 -44
index fd83abb929ec..61b89834d97d 100644
@@ -24,6 +24,7 @@
  * Will be merged it with virtual IOAPIC logic, since most is the same
 */
 
+#include <xen/io.h>
 #include <xen/types.h>
 #include <xen/mm.h>
 #include <xen/xmalloc.h>
@@ -304,28 +305,7 @@ static void adjacent_read(
 
     hwaddr = fix_to_virt(fixmap_idx) + PAGE_OFFSET(address);
 
-    switch ( len )
-    {
-    case 1:
-        *pval = readb(hwaddr);
-        break;
-
-    case 2:
-        *pval = readw(hwaddr);
-        break;
-
-    case 4:
-        *pval = readl(hwaddr);
-        break;
-
-    case 8:
-        *pval = readq(hwaddr);
-        break;
-
-    default:
-        ASSERT_UNREACHABLE();
-        break;
-    }
+    *pval = read_mmio(hwaddr, len);
 }
 
 static void adjacent_write(
@@ -344,28 +324,7 @@ static void adjacent_write(
 
     hwaddr = fix_to_virt(fixmap_idx) + PAGE_OFFSET(address);
 
-    switch ( len )
-    {
-    case 1:
-        writeb(val, hwaddr);
-        break;
-
-    case 2:
-        writew(val, hwaddr);
-        break;
-
-    case 4:
-        writel(val, hwaddr);
-        break;
-
-    case 8:
-        writeq(val, hwaddr);
-        break;
-
-    default:
-        ASSERT_UNREACHABLE();
-        break;
-    }
+    write_mmio(hwaddr, val, len);
 }
 
 static int cf_check msixtbl_read(
xen/arch/x86/mm.c +7 -23
index 1cf236516789..8baac132b484 100644
@@ -95,6 +95,7 @@
 #include <xen/guest_access.h>
 #include <xen/hypercall.h>
 #include <xen/init.h>
+#include <xen/io.h>
 #include <xen/iocap.h>
 #include <xen/ioreq.h>
 #include <xen/irq.h>
@@ -116,7 +117,6 @@
 #include <asm/flushtlb.h>
 #include <asm/guest.h>
 #include <asm/idt.h>
-#include <asm/io.h>
 #include <asm/io_apic.h>
 #include <asm/ldt.h>
 #include <asm/mem_sharing.h>
@@ -5102,7 +5102,7 @@ static void __iomem *subpage_mmio_map_page(
 static void subpage_mmio_write_emulate(
     mfn_t mfn,
     unsigned int offset,
-    const void *data,
+    unsigned long data,
     unsigned int len)
 {
     struct subpage_ro_range *entry;
@@ -5131,26 +5131,8 @@ static void subpage_mmio_write_emulate(
         return;
     }
 
-    addr += offset;
-    switch ( len )
-    {
-    case 1:
-        writeb(*(const uint8_t*)data, addr);
-        break;
-    case 2:
-        writew(*(const uint16_t*)data, addr);
-        break;
-    case 4:
-        writel(*(const uint32_t*)data, addr);
-        break;
-    case 8:
-        writeq(*(const uint64_t*)data, addr);
-        break;
-    default:
-        /* mmio_ro_emulated_write() already validated the size */
-        ASSERT_UNREACHABLE();
+    if ( !write_mmio(addr + offset, data, len) )
         goto write_ignored;
-    }
 }
 
 #ifdef CONFIG_HVM
@@ -5185,18 +5167,20 @@ int cf_check mmio_ro_emulated_write(
     struct x86_emulate_ctxt *ctxt)
 {
     struct mmio_ro_emulate_ctxt *mmio_ro_ctxt = ctxt->data;
+    unsigned long data = 0;
 
     /* Only allow naturally-aligned stores at the original %cr2 address. */
     if ( ((bytes | offset) & (bytes - 1)) || !bytes ||
-         offset != mmio_ro_ctxt->cr2 )
+         offset != mmio_ro_ctxt->cr2 || bytes > sizeof(data) )
     {
         gdprintk(XENLOG_WARNING, "bad access (cr2=%lx, addr=%lx, bytes=%u)\n",
                 mmio_ro_ctxt->cr2, offset, bytes);
         return X86EMUL_UNHANDLEABLE;
     }
 
+    memcpy(&data, p_data, bytes);
     subpage_mmio_write_emulate(mmio_ro_ctxt->mfn, PAGE_OFFSET(offset),
-                               p_data, bytes);
+                               data, bytes);
 
     return X86EMUL_OKAY;
 }
xen/drivers/vpci/msix.c +3 -44

65 lines not on this page · raw message

xen/include/xen/io.h new file +70 -0

70 lines not on this page · raw message

Signature · 1 line
2.48.1
Reviewed-by on v3 1/4
#2 lore raw

Re: [PATCH v3 1/4] xen/io: provide helpers for multi size MMIO accesses

Quoted 11 lines · Roger Pau Monne

Several handlers have the same necessity of reading or writing from or to an MMIO region using 1, 2, 4 or 8 bytes accesses. So far this has been open-coded in the function itself. Instead provide a new set of handlers that encapsulate the accesses.

Since the added helpers are not architecture specific, introduce a new generic io.h header.

No functional change intended.

#3 lore raw

PATCH v3 2/4 x86/hvm: fix handling of accesses to partial r/o MMIO pages

In reply to Roger Pau Monne

The current logic to handle accesses to MMIO pages partially read-only is based on the (now removed) logic used to handle accesses to the r/o MMCFG region(s) for PVH v1 dom0. However that has issues when running on AMD hardware, as in that case the guest linear address that triggered the fault is not provided as part of the VM exit. This caused mmio_ro_emulated_write() to always fail before calling subpage_mmio_write_emulate() when running on AMD and called from an HVM context.

Take a different approach and convert the handling of partial read-only MMIO page accesses into an HVM MMIO ops handler, as that's the more natural way to handle this kind of emulation for HVM domains.

This allows getting rid of hvm_emulate_one_mmio() and it's single call site in hvm_hap_nested_page_fault(). As part of the fix r/o MMIO accesses are now handled by handle_mmio_with_translation(), re-using the same logic that was used for other read-only types part of p2m_is_discard_write(). The usage of emulation for faulting p2m_mmio_direct types is limited to addresses in the r/o MMIO range. The page present check is dropped as type p2m_mmio_direct must have the present bit set in the PTE.

Note a small adjustment is needed to the pf-fixup dom0 PVH logic: avoid attempting to fixup faults resulting from write accesses to read-only MMIO regions, as handling of those accesses is now done by handle_mmio().

Fixes 33c19df9a5a0 ('x86/PCI: intercept accesses to RO MMIO from dom0s in HVM containers')

---
Changes since v2:
- Attempt to fixup read access to r/o MMIO regions for dom0 PVH.
- Use PFN_DOWN() with get_gfn_query().
- Reject unaligned accesses.
- Add comment about the need to handle reads.

Changes since v1:
- Introduce hvm/mmio.c to place the r/o MMIO handlers.
- Add comment about pf-fixup and r/o MMIO range checking.
- Expand commit message about dropping the PTE present check and usage of the emulator.
- Return X86EMUL_OKAY in the read handler if the MMIO region is not found.
- Check the faulting address is in the mmio_ro_ranges before sending for emulation.

---
 xen/arch/x86/hvm/Makefile              |   1 +
 xen/arch/x86/hvm/emulate.c             |  54 ++---------
 xen/arch/x86/hvm/hvm.c                 |  17 ++--
 xen/arch/x86/hvm/mmio.c                | 125 +++++++++++++++++++++++++
 xen/arch/x86/include/asm/hvm/emulate.h |   1 -
 xen/arch/x86/include/asm/hvm/io.h      |   3 +
 xen/arch/x86/include/asm/mm.h          |  12 +++
 xen/arch/x86/mm.c                      |  37 +-------
 8 files changed, 160 insertions(+), 90 deletions(-)
 create mode 100644 xen/arch/x86/hvm/mmio.c
xen/arch/x86/hvm/Makefile +1 -0
index 4c1fa5c6c2bf..6ec2c8f2db56 100644
@@ -15,6 +15,7 @@ obj-y += intercept.o
 obj-y += io.o
 obj-y += ioreq.o
 obj-y += irq.o
+obj-y += mmio.o
 obj-y += monitor.o
 obj-y += mtrr.o
 obj-y += nestedhvm.o
xen/arch/x86/hvm/emulate.c +9 -45

66 lines not on this page · raw message

xen/arch/x86/hvm/hvm.c +7 -10
index 6f1174c5127e..6b998387e3d8 100644
@@ -8,6 +8,7 @@
  */
 
 #include <xen/init.h>
+#include <xen/io.h>
 #include <xen/ioreq.h>
 #include <xen/lib.h>
 #include <xen/trace.h>
@@ -35,7 +36,6 @@
 #include <asm/current.h>
 #include <asm/debugreg.h>
 #include <asm/e820.h>
-#include <asm/io.h>
 #include <asm/regs.h>
 #include <asm/cpufeature.h>
 #include <asm/processor.h>
@@ -692,6 +692,8 @@ int hvm_domain_initialise(struct domain *d,
 
     register_portio_handler(d, XEN_HVM_DEBUGCONS_IOPORT, 1, hvm_print_line);
 
+    register_subpage_ro_handler(d);
+
     if ( hvm_tsc_scaling_supported )
         d->arch.hvm.tsc_scaling_ratio = hvm_default_tsc_scaling_ratio;
 
@@ -1981,7 +1983,10 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
      */
     if ( (p2mt == p2m_mmio_dm) ||
          (npfec.write_access &&
-          (p2m_is_discard_write(p2mt) || (p2mt == p2m_ioreq_server))) )
+          (p2m_is_discard_write(p2mt) || (p2mt == p2m_ioreq_server) ||
+           /* MMIO entries can be r/o if the target mfn is in mmio_ro_ranges. */
+           (p2mt == p2m_mmio_direct &&
+            rangeset_contains_singleton(mmio_ro_ranges, mfn_x(mfn))))) )
     {
         if ( !handle_mmio_with_translation(gla, gfn, npfec) )
             hvm_inject_hw_exception(X86_EXC_GP, 0);
@@ -2033,14 +2038,6 @@ int hvm_hap_nested_page_fault(paddr_t gpa, unsigned long gla,
         goto out_put_gfn;
     }
 
-    if ( (p2mt == p2m_mmio_direct) && npfec.write_access && npfec.present &&
-         (is_hardware_domain(currd) || subpage_mmio_write_accept(mfn, gla)) &&
-         (hvm_emulate_one_mmio(mfn_x(mfn), gla) == X86EMUL_OKAY) )
-    {
-        rc = 1;
-        goto out_put_gfn;
-    }
-
     /* If we fell through, the vcpu will retry now that access restrictions have
      * been removed. It may fault again if the p2m entry type still requires so.
      * Otherwise, this is an error condition. */
xen/arch/x86/hvm/mmio.c new file +125 -0

125 lines not on this page · raw message

xen/arch/x86/include/asm/hvm/emulate.h +0 -1
index c7a2d2a5be4e..178ac32e151f 100644
@@ -86,7 +86,6 @@ void hvmemul_cancel(struct vcpu *v);
 struct segment_register *hvmemul_get_seg_reg(
     enum x86_segment seg,
     struct hvm_emulate_ctxt *hvmemul_ctxt);
-int hvm_emulate_one_mmio(unsigned long mfn, unsigned long gla);
 
 static inline bool handle_mmio(void)
 {
xen/arch/x86/include/asm/hvm/io.h +3 -0
index 565bad300d20..c12f099a037c 100644
@@ -135,6 +135,9 @@ void destroy_vpci_mmcfg(struct domain *d);
 /* Remove MMCFG regions from a domain ->iomem_caps. */
 int vpci_mmcfg_deny_access(struct domain *d);
 
+/* r/o MMIO subpage access handler. */
+void register_subpage_ro_handler(struct domain *d);
+
 #endif /* __ASM_X86_HVM_IO_H__ */
 
 
xen/arch/x86/include/asm/mm.h +12 -0
index a1bc8cc27451..c2e9ef6e5023 100644
@@ -554,6 +554,18 @@ int cf_check mmio_ro_emulated_write(
     enum x86_segment seg, unsigned long offset, void *p_data,
     unsigned int bytes, struct x86_emulate_ctxt *ctxt);
 
+/* r/o MMIO subpage access handlers. */
+struct subpage_ro_range {
+    struct list_head list;
+    mfn_t mfn;
+    void __iomem *mapped;
+    DECLARE_BITMAP(ro_elems, PAGE_SIZE / MMIO_RO_SUBPAGE_GRAN);
+};
+struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn);
+void __iomem *subpage_mmio_map_page(struct subpage_ro_range *entry);
+void subpage_mmio_write_emulate(
+    mfn_t mfn, unsigned int offset, unsigned long data, unsigned int len);
+
 int audit_adjust_pgtables(struct domain *d, int dir, int noisy);
 
 extern int pagefault_by_memadd(unsigned long addr, struct cpu_user_regs *regs);
xen/arch/x86/mm.c +3 -34
index 8baac132b484..927beb6f07aa 100644
@@ -157,13 +157,6 @@ struct rangeset *__read_mostly mmio_ro_ranges;
 static uint32_t __ro_after_init base_disallow_mask;
 
 /* Handling sub-page read-only MMIO regions */
-struct subpage_ro_range {
-    struct list_head list;
-    mfn_t mfn;
-    void __iomem *mapped;
-    DECLARE_BITMAP(ro_elems, PAGE_SIZE / MMIO_RO_SUBPAGE_GRAN);
-};
-
 static LIST_HEAD_RO_AFTER_INIT(subpage_ro_ranges);
 static DEFINE_SPINLOCK(subpage_ro_lock);
 
@@ -4929,7 +4922,7 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     return rc;
 }
 
-static struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn)
+struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn)
 {
     struct subpage_ro_range *entry;
 
@@ -5074,7 +5067,7 @@ int __init subpage_mmio_ro_add(
     return rc;
 }
 
-static void __iomem *subpage_mmio_map_page(
+void __iomem *subpage_mmio_map_page(
     struct subpage_ro_range *entry)
 {
     void __iomem *mapped_page;
@@ -5099,7 +5092,7 @@ static void __iomem *subpage_mmio_map_page(
     return entry->mapped;
 }
 
-static void subpage_mmio_write_emulate(
+void subpage_mmio_write_emulate(
     mfn_t mfn,
     unsigned int offset,
     unsigned long data,
@@ -5135,30 +5128,6 @@ static void subpage_mmio_write_emulate(
         goto write_ignored;
 }
 
-#ifdef CONFIG_HVM
-bool subpage_mmio_write_accept(mfn_t mfn, unsigned long gla)
-{
-    unsigned int offset = PAGE_OFFSET(gla);
-    const struct subpage_ro_range *entry;
-
-    entry = subpage_mmio_find_page(mfn);
-    if ( !entry )
-        return false;
-
-    if ( !test_bit(offset / MMIO_RO_SUBPAGE_GRAN, entry->ro_elems) )
-    {
-        /*
-         * We don't know the write size at this point yet, so it could be
-         * an unaligned write, but accept it here anyway and deal with it
-         * later.
-         */
-        return true;
-    }
-
-    return false;
-}
-#endif
-
 int cf_check mmio_ro_emulated_write(
     enum x86_segment seg,
     unsigned long offset,
Signature · 1 line
2.48.1
#4 lore raw

Re: [PATCH v3 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages

Quoted 6 lines · Roger Pau Monne
xen/arch/x86/hvm/emulate.c +0 -0
@@ -370,7 +370,15 @@ static int hvmemul_do_io(
         /* If there is no suitable backing DM, just ignore accesses */
         if ( !s )
         {

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

Quoted 10 lines
xen/arch/x86/hvm/emulate.c +9 -1
-            if ( is_mmio && is_hardware_domain(currd) )
+            if ( is_mmio && is_hardware_domain(currd) &&
+                 /*
+                  * Do not attempt to fixup write accesses to r/o MMIO regions,
+                  * they are expected to be terminated by the null handler
+                  * below.
+                  */
+                 (!rangeset_contains_singleton(mmio_ro_ranges,
+                                               PFN_DOWN(addr)) ||
+                  dir == IOREQ_READ) )

These two would better be swapped, for the cheap one to be done first.

Quoted 32 lines
xen/arch/x86/hvm/mmio.c new file +29 -0
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * MMIO related routines.
+ *
+ * Copyright (c) 2025 Cloud Software Group
+ */
+
+#include <xen/io.h>
+#include <xen/mm.h>
+
+#include <asm/p2m.h>
+
+static int cf_check subpage_mmio_accept(struct vcpu *v, unsigned long addr)
+{
+    p2m_type_t t;
+    mfn_t mfn = get_gfn_query_unlocked(v->domain, PFN_DOWN(addr), &t);
+
+    return !mfn_eq(mfn, INVALID_MFN) && t == p2m_mmio_direct &&
+           subpage_mmio_find_page(mfn);
+}
+
+/*
+ * The guest has read access to those regions, and consequently read accesses
+ * shouldn't fault.  However read-modify-write operations may take this path,
+ * so handling of reads is necessary.
+ */
+static int cf_check subpage_mmio_read(
+    struct vcpu *v, unsigned long addr, unsigned int len, unsigned long *data)
+{

xen/arch/x86/hvm/mmio.c line 39

Quoted 10 lines
xen/arch/x86/hvm/mmio.c new file +10 -0
+    struct domain *d = v->domain;
+    unsigned long gfn = PFN_DOWN(addr);
+    p2m_type_t t;
+    mfn_t mfn;
+    struct subpage_ro_range *entry;
+    volatile void __iomem *mem;
+
+    *data = ~0UL;
+
+    if ( !IS_ALIGNED(len | addr, len) )

What's the point of doing the | ? len can't be misaligned with itself?

Quoted 5 lines
xen/arch/x86/include/asm/mm.h +0 -0
@@ -554,6 +554,18 @@ int cf_check mmio_ro_emulated_write(
     enum x86_segment seg, unsigned long offset, void *p_data,
     unsigned int bytes, struct x86_emulate_ctxt *ctxt);

xen/arch/x86/include/asm/mm.h int cf_check mmio_ro_emulated_write( · line 565

Quoted 10 lines
xen/arch/x86/include/asm/mm.h +9 -0
 
+/* r/o MMIO subpage access handlers. */
+struct subpage_ro_range {
+    struct list_head list;
+    mfn_t mfn;
+    void __iomem *mapped;
+    DECLARE_BITMAP(ro_elems, PAGE_SIZE / MMIO_RO_SUBPAGE_GRAN);
+};
+struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn);
+void __iomem *subpage_mmio_map_page(struct subpage_ro_range *entry);

I notice you didn't move the __iomem, which - as indicated - I agree with, but Andrew didn't. Did you sort this with him privately?

Jan

#5 lore raw

Re: [PATCH v3 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages

Quoted 11 lines · Jan Beulich
Quoted 10 lines · Roger Pau Monne
xen/arch/x86/hvm/emulate.c +3 -1
@@ -370,7 +370,15 @@ static int hvmemul_do_io(
         /* If there is no suitable backing DM, just ignore accesses */
         if ( !s )
         {
-            if ( is_mmio && is_hardware_domain(currd) )
+            if ( is_mmio && is_hardware_domain(currd) &&
+                 /*
+                  * Do not attempt to fixup write accesses to r/o MMIO regions,
Quoted 8 lines

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

Quoted 6 lines
xen/arch/x86/hvm/emulate.c +6 -0
+                  * they are expected to be terminated by the null handler
+                  * below.
+                  */
+                 (!rangeset_contains_singleton(mmio_ro_ranges,
+                                               PFN_DOWN(addr)) ||
+                  dir == IOREQ_READ) )

These two would better be swapped, for the cheap one to be done first.

Can swap, thanks for pointing out.

Quoted 36 lines
Quoted 36 lines
xen/arch/x86/hvm/mmio.c new file +33 -0
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * MMIO related routines.
+ *
+ * Copyright (c) 2025 Cloud Software Group
+ */
+
+#include <xen/io.h>
+#include <xen/mm.h>
+
+#include <asm/p2m.h>
+
+static int cf_check subpage_mmio_accept(struct vcpu *v, unsigned long addr)
+{
+    p2m_type_t t;
+    mfn_t mfn = get_gfn_query_unlocked(v->domain, PFN_DOWN(addr), &t);
+
+    return !mfn_eq(mfn, INVALID_MFN) && t == p2m_mmio_direct &&
+           subpage_mmio_find_page(mfn);
+}
+
+/*
+ * The guest has read access to those regions, and consequently read accesses
+ * shouldn't fault.  However read-modify-write operations may take this path,
+ * so handling of reads is necessary.
+ */
+static int cf_check subpage_mmio_read(
+    struct vcpu *v, unsigned long addr, unsigned int len, unsigned long *data)
+{
+    struct domain *d = v->domain;
+    unsigned long gfn = PFN_DOWN(addr);
+    p2m_type_t t;
+    mfn_t mfn;
Quoted 8 lines

xen/arch/x86/hvm/mmio.c line 39

Quoted 6 lines
xen/arch/x86/hvm/mmio.c new file +6 -0
+    struct subpage_ro_range *entry;
+    volatile void __iomem *mem;
+
+    *data = ~0UL;
+
+    if ( !IS_ALIGNED(len | addr, len) )

What's the point of doing the | ? len can't be misaligned with itself?

Hm, it's the same form that's used in mmio_ro_emulated_write(), I assumed it was to catch illegal access lengths, like 3.

Quoted 6 lines
Quoted 6 lines
xen/arch/x86/include/asm/mm.h +0 -0
@@ -554,6 +554,18 @@ int cf_check mmio_ro_emulated_write(
     enum x86_segment seg, unsigned long offset, void *p_data,
     unsigned int bytes, struct x86_emulate_ctxt *ctxt);
 
Quoted 12 lines

xen/arch/x86/include/asm/mm.h int cf_check mmio_ro_emulated_write( · line 565

Quoted 9 lines
xen/arch/x86/include/asm/mm.h +9 -0
+/* r/o MMIO subpage access handlers. */
+struct subpage_ro_range {
+    struct list_head list;
+    mfn_t mfn;
+    void __iomem *mapped;
+    DECLARE_BITMAP(ro_elems, PAGE_SIZE / MMIO_RO_SUBPAGE_GRAN);
+};
+struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn);
+void __iomem *subpage_mmio_map_page(struct subpage_ro_range *entry);

I notice you didn't move the __iomem, which - as indicated - I agree with, but Andrew didn't. Did you sort this with him privately?

I will raise with him, seeing as he didn't reply to your last email, I assumed he was OK with the reasoning?

Thanks, Roger.

#6 lore raw

Re: [PATCH v3 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages

Quoted 47 lines · Roger Pau Monné
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Roger Pau Monne
xen/arch/x86/hvm/mmio.c new file +33 -0
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * MMIO related routines.
+ *
+ * Copyright (c) 2025 Cloud Software Group
+ */
+
+#include <xen/io.h>
+#include <xen/mm.h>
+
+#include <asm/p2m.h>
+
+static int cf_check subpage_mmio_accept(struct vcpu *v, unsigned long addr)
+{
+    p2m_type_t t;
+    mfn_t mfn = get_gfn_query_unlocked(v->domain, PFN_DOWN(addr), &t);
+
+    return !mfn_eq(mfn, INVALID_MFN) && t == p2m_mmio_direct &&
+           subpage_mmio_find_page(mfn);
+}
+
+/*
+ * The guest has read access to those regions, and consequently read accesses
+ * shouldn't fault.  However read-modify-write operations may take this path,
+ * so handling of reads is necessary.
+ */
+static int cf_check subpage_mmio_read(
+    struct vcpu *v, unsigned long addr, unsigned int len, unsigned long *data)
+{
+    struct domain *d = v->domain;
+    unsigned long gfn = PFN_DOWN(addr);
+    p2m_type_t t;
+    mfn_t mfn;
Quoted 8 lines

xen/arch/x86/hvm/mmio.c line 39

Quoted 6 lines
xen/arch/x86/hvm/mmio.c new file +6 -0
+    struct subpage_ro_range *entry;
+    volatile void __iomem *mem;
+
+    *data = ~0UL;
+
+    if ( !IS_ALIGNED(len | addr, len) )

What's the point of doing the | ? len can't be misaligned with itself?

Quoted 2 lines

Hm, it's the same form that's used in mmio_ro_emulated_write(), I assumed it was to catch illegal access lengths, like 3.

Oh, I see. But that's not using IS_ALIGNED(), and imo validly so, despite the apparent open-coding. IS_ALIGNED() requires the 2nd argument to be a power of two. The combined check there is folding the power-of-2 one with the is- aligned one.

Jan

#7 lore raw

Re: [PATCH v3 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages

Quoted 48 lines · Jan Beulich
Quoted 47 lines · Roger Pau Monné
Quoted 45 lines · Jan Beulich
Quoted 32 lines · Roger Pau Monne
xen/arch/x86/hvm/mmio.c new file +29 -0
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * MMIO related routines.
+ *
+ * Copyright (c) 2025 Cloud Software Group
+ */
+
+#include <xen/io.h>
+#include <xen/mm.h>
+
+#include <asm/p2m.h>
+
+static int cf_check subpage_mmio_accept(struct vcpu *v, unsigned long addr)
+{
+    p2m_type_t t;
+    mfn_t mfn = get_gfn_query_unlocked(v->domain, PFN_DOWN(addr), &t);
+
+    return !mfn_eq(mfn, INVALID_MFN) && t == p2m_mmio_direct &&
+           subpage_mmio_find_page(mfn);
+}
+
+/*
+ * The guest has read access to those regions, and consequently read accesses
+ * shouldn't fault.  However read-modify-write operations may take this path,
+ * so handling of reads is necessary.
+ */
+static int cf_check subpage_mmio_read(
+    struct vcpu *v, unsigned long addr, unsigned int len, unsigned long *data)
+{

xen/arch/x86/hvm/mmio.c line 39

Quoted 10 lines
xen/arch/x86/hvm/mmio.c new file +10 -0
+    struct domain *d = v->domain;
+    unsigned long gfn = PFN_DOWN(addr);
+    p2m_type_t t;
+    mfn_t mfn;
+    struct subpage_ro_range *entry;
+    volatile void __iomem *mem;
+
+    *data = ~0UL;
+
+    if ( !IS_ALIGNED(len | addr, len) )

What's the point of doing the | ? len can't be misaligned with itself?

Quoted 7 lines
Quoted 2 lines

Hm, it's the same form that's used in mmio_ro_emulated_write(), I assumed it was to catch illegal access lengths, like 3.

Oh, I see. But that's not using IS_ALIGNED(), and imo validly so, despite the apparent open-coding. IS_ALIGNED() requires the 2nd argument to be a power of two. The combined check there is folding the power-of-2 one with the is- aligned one.

Do you think it's worth keeping those checks then? I could do:

if ( len & (len - 1) || len > 8 || !IS_ALIGNED(addr, len) )

As a possibly more complete and easier to parse check?

Thanks, Roger.

#8 lore raw

Re: [PATCH v3 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages

Quoted 52 lines · Roger Pau Monné
Quoted 51 lines · Jan Beulich
Quoted 49 lines · Roger Pau Monné
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Roger Pau Monne
xen/arch/x86/hvm/mmio.c new file +33 -0
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * MMIO related routines.
+ *
+ * Copyright (c) 2025 Cloud Software Group
+ */
+
+#include <xen/io.h>
+#include <xen/mm.h>
+
+#include <asm/p2m.h>
+
+static int cf_check subpage_mmio_accept(struct vcpu *v, unsigned long addr)
+{
+    p2m_type_t t;
+    mfn_t mfn = get_gfn_query_unlocked(v->domain, PFN_DOWN(addr), &t);
+
+    return !mfn_eq(mfn, INVALID_MFN) && t == p2m_mmio_direct &&
+           subpage_mmio_find_page(mfn);
+}
+
+/*
+ * The guest has read access to those regions, and consequently read accesses
+ * shouldn't fault.  However read-modify-write operations may take this path,
+ * so handling of reads is necessary.
+ */
+static int cf_check subpage_mmio_read(
+    struct vcpu *v, unsigned long addr, unsigned int len, unsigned long *data)
+{
+    struct domain *d = v->domain;
+    unsigned long gfn = PFN_DOWN(addr);
+    p2m_type_t t;
+    mfn_t mfn;
Quoted 8 lines

xen/arch/x86/hvm/mmio.c line 39

Quoted 6 lines
xen/arch/x86/hvm/mmio.c new file +6 -0
+    struct subpage_ro_range *entry;
+    volatile void __iomem *mem;
+
+    *data = ~0UL;
+
+    if ( !IS_ALIGNED(len | addr, len) )

What's the point of doing the | ? len can't be misaligned with itself?

Hm, it's the same form that's used in mmio_ro_emulated_write(), I assumed it was to catch illegal access lengths, like 3.

Quoted 6 lines
Quoted 4 lines

Oh, I see. But that's not using IS_ALIGNED(), and imo validly so, despite the apparent open-coding. IS_ALIGNED() requires the 2nd argument to be a power of two. The combined check there is folding the power-of-2 one with the is- aligned one.

Do you think it's worth keeping those checks then?

Yes, I think we should be as strict as possible in what we (try to) emulate.

Quoted 5 lines

I could do:

if ( len & (len - 1) || len > 8 || !IS_ALIGNED(addr, len) )

As a possibly more complete and easier to parse check?

If you dislike the form mmio_ro_emulated_write() uses, sure. However, you will want to check len to be non-zero, while I'm unsure you need to check len > 8 - mmio_ro_emulated_write() doesn't have such. Albeit - perhaps wrongly so; we'd end at the ASSERT_UNREACHABLE() in subpage_mmio_write_emulate() if a wider store was used. I guess I ought to make a patch there, and you want to keep the "len > 8".

Jan

#9 lore raw

Re: [PATCH v3 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages

Quoted 66 lines · Jan Beulich
Quoted 52 lines · Roger Pau Monné
Quoted 51 lines · Jan Beulich
Quoted 49 lines · Roger Pau Monné
Quoted 37 lines · Jan Beulich
Quoted 36 lines · Roger Pau Monne
xen/arch/x86/hvm/mmio.c new file +33 -0
@@ -0,0 +1,125 @@
+/* SPDX-License-Identifier: GPL-2.0-only */
+/*
+ * MMIO related routines.
+ *
+ * Copyright (c) 2025 Cloud Software Group
+ */
+
+#include <xen/io.h>
+#include <xen/mm.h>
+
+#include <asm/p2m.h>
+
+static int cf_check subpage_mmio_accept(struct vcpu *v, unsigned long addr)
+{
+    p2m_type_t t;
+    mfn_t mfn = get_gfn_query_unlocked(v->domain, PFN_DOWN(addr), &t);
+
+    return !mfn_eq(mfn, INVALID_MFN) && t == p2m_mmio_direct &&
+           subpage_mmio_find_page(mfn);
+}
+
+/*
+ * The guest has read access to those regions, and consequently read accesses
+ * shouldn't fault.  However read-modify-write operations may take this path,
+ * so handling of reads is necessary.
+ */
+static int cf_check subpage_mmio_read(
+    struct vcpu *v, unsigned long addr, unsigned int len, unsigned long *data)
+{
+    struct domain *d = v->domain;
+    unsigned long gfn = PFN_DOWN(addr);
+    p2m_type_t t;
+    mfn_t mfn;
Quoted 8 lines

xen/arch/x86/hvm/mmio.c line 39

Quoted 6 lines
xen/arch/x86/hvm/mmio.c new file +6 -0
+    struct subpage_ro_range *entry;
+    volatile void __iomem *mem;
+
+    *data = ~0UL;
+
+    if ( !IS_ALIGNED(len | addr, len) )

What's the point of doing the | ? len can't be misaligned with itself?

Hm, it's the same form that's used in mmio_ro_emulated_write(), I assumed it was to catch illegal access lengths, like 3.

Quoted 6 lines
Quoted 4 lines

Oh, I see. But that's not using IS_ALIGNED(), and imo validly so, despite the apparent open-coding. IS_ALIGNED() requires the 2nd argument to be a power of two. The combined check there is folding the power-of-2 one with the is- aligned one.

Do you think it's worth keeping those checks then?

Yes, I think we should be as strict as possible in what we (try to) emulate.

Quoted 4 lines

I could do:

if ( len & (len - 1) || len > 8 || !IS_ALIGNED(addr, len) )

Quoted 8 lines
Quoted 1 line

As a possibly more complete and easier to parse check?

If you dislike the form mmio_ro_emulated_write() uses, sure. However, you will want to check len to be non-zero, while I'm unsure you need to check len > 8 - mmio_ro_emulated_write() doesn't have such. Albeit - perhaps wrongly so; we'd end at the ASSERT_UNREACHABLE() in subpage_mmio_write_emulate() if a wider store was used. I guess I ought to make a patch there, and you want to keep the "len > 8".

OK, let me send v4 with those adjustments then.

Thanks, Roger.

#10 lore raw

PATCH v3 3/4 x86/hvm: only register the r/o subpage ops when needed

In reply to Roger Pau Monne

MMIO operation handlers can be expensive to process, hence attempt to register only those that will be needed by the domain.

Subpage r/o MMIO regions are added exclusively at boot, further limit their addition to strictly before the initial domain gets created, so by the time initial domain creation happens Xen knows whether subpage is required or not. This allows only registering the MMIO handler when there are subpage regions to handle.

---
Could possibly be part of the previous patch, but strictly speaking is an improvement, as even before the previous patch subpage r/o was always called even when no subpage regions are registered.

---
 xen/arch/x86/hvm/hvm.c        |  3 ++-
 xen/arch/x86/include/asm/mm.h |  1 +
 xen/arch/x86/mm.c             | 16 ++++++++++++++++
 3 files changed, 19 insertions(+), 1 deletion(-)
xen/arch/x86/hvm/hvm.c +2 -1
index 6b998387e3d8..4cb2e13046d1 100644
@@ -692,7 +692,8 @@ int hvm_domain_initialise(struct domain *d,
 
     register_portio_handler(d, XEN_HVM_DEBUGCONS_IOPORT, 1, hvm_print_line);
 
-    register_subpage_ro_handler(d);
+    if ( subpage_ro_active() )
+        register_subpage_ro_handler(d);
 
     if ( hvm_tsc_scaling_supported )
         d->arch.hvm.tsc_scaling_ratio = hvm_default_tsc_scaling_ratio;
xen/arch/x86/include/asm/mm.h +1 -0
index c2e9ef6e5023..aeb8ebcf2d56 100644
@@ -561,6 +561,7 @@ struct subpage_ro_range {
     void __iomem *mapped;
     DECLARE_BITMAP(ro_elems, PAGE_SIZE / MMIO_RO_SUBPAGE_GRAN);
 };
+bool subpage_ro_active(void);
 struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn);
 void __iomem *subpage_mmio_map_page(struct subpage_ro_range *entry);
 void subpage_mmio_write_emulate(
xen/arch/x86/mm.c +16 -0
index 927beb6f07aa..0e29bab03a95 100644
@@ -4922,6 +4922,11 @@ long arch_memory_op(unsigned long cmd, XEN_GUEST_HANDLE_PARAM(void) arg)
     return rc;
 }
 
+bool subpage_ro_active(void)
+{
+    return !list_empty(&subpage_ro_ranges);
+}
+
 struct subpage_ro_range *subpage_mmio_find_page(mfn_t mfn)
 {
     struct subpage_ro_range *entry;
@@ -5011,6 +5016,17 @@ int __init subpage_mmio_ro_add(
          !IS_ALIGNED(size, MMIO_RO_SUBPAGE_GRAN) )
         return -EINVAL;
 
+    /*
+     * Force all r/o subregions to be registered before initial domain
+     * creation, so that the emulation handlers can be added only when there
+     * are pages registered.
+     */
+    if ( system_state >= SYS_STATE_smp_boot )
+    {
+        ASSERT_UNREACHABLE();
+        return -EILSEQ;
+    }
+
     if ( !size )
         return 0;
 
Signature · 1 line
2.48.1
#11 lore raw

PATCH v3 4/4 x86/mm: move mmio_ro_emulated_write() to PV only file

In reply to Roger Pau Monne

mmio_ro_emulated_write() is only used in pv/ro-page-fault.c, move the function to that file and make it static.

No functional change intended.

---
 xen/arch/x86/include/asm/mm.h   | 12 ------------
 xen/arch/x86/mm.c               | 26 -------------------------
 xen/arch/x86/pv/ro-page-fault.c | 34 +++++++++++++++++++++++++++++++++
 3 files changed, 34 insertions(+), 38 deletions(-)
xen/arch/x86/include/asm/mm.h +0 -12
index aeb8ebcf2d56..2665daa6f74f 100644
@@ -542,18 +542,6 @@ void memguard_unguard_stack(void *p);
 int subpage_mmio_ro_add(paddr_t start, size_t size);
 bool subpage_mmio_write_accept(mfn_t mfn, unsigned long gla);
 
-struct mmio_ro_emulate_ctxt {
-        unsigned long cr2;
-        /* Used only for mmcfg case */
-        unsigned int seg, bdf;
-        /* Used only for non-mmcfg case */
-        mfn_t mfn;
-};
-
-int cf_check mmio_ro_emulated_write(
-    enum x86_segment seg, unsigned long offset, void *p_data,
-    unsigned int bytes, struct x86_emulate_ctxt *ctxt);
-
 /* r/o MMIO subpage access handlers. */
 struct subpage_ro_range {
     struct list_head list;
xen/arch/x86/mm.c +0 -26
index 0e29bab03a95..9ac855f998dc 100644
@@ -5144,32 +5144,6 @@ void subpage_mmio_write_emulate(
         goto write_ignored;
 }
 
-int cf_check mmio_ro_emulated_write(
-    enum x86_segment seg,
-    unsigned long offset,
-    void *p_data,
-    unsigned int bytes,
-    struct x86_emulate_ctxt *ctxt)
-{
-    struct mmio_ro_emulate_ctxt *mmio_ro_ctxt = ctxt->data;
-    unsigned long data = 0;
-
-    /* Only allow naturally-aligned stores at the original %cr2 address. */
-    if ( ((bytes | offset) & (bytes - 1)) || !bytes ||
-         offset != mmio_ro_ctxt->cr2 || bytes > sizeof(data) )
-    {
-        gdprintk(XENLOG_WARNING, "bad access (cr2=%lx, addr=%lx, bytes=%u)\n",
-                mmio_ro_ctxt->cr2, offset, bytes);
-        return X86EMUL_UNHANDLEABLE;
-    }
-
-    memcpy(&data, p_data, bytes);
-    subpage_mmio_write_emulate(mmio_ro_ctxt->mfn, PAGE_OFFSET(offset),
-                               data, bytes);
-
-    return X86EMUL_OKAY;
-}
-
 /*
  * For these PTE APIs, the caller must follow the alloc-map-unmap-free
  * lifecycle, which means explicitly mapping the PTE pages before accessing
xen/arch/x86/pv/ro-page-fault.c +34 -0
index 11b01c479e43..3dd795288379 100644
@@ -298,6 +298,14 @@ static int ptwr_do_page_fault(struct x86_emulate_ctxt *ctxt,
  * fault handling for read-only MMIO pages
  */
 
+struct mmio_ro_emulate_ctxt {
+    unsigned long cr2;
+    /* Used only for mmcfg case */
+    unsigned int seg, bdf;
+    /* Used only for non-mmcfg case */
+    mfn_t mfn;
+};
+
 static int cf_check mmcfg_intercept_write(
     enum x86_segment seg,
     unsigned long offset,
@@ -329,6 +337,32 @@ static int cf_check mmcfg_intercept_write(
     return X86EMUL_OKAY;
 }
 
+static int cf_check mmio_ro_emulated_write(
+    enum x86_segment seg,
+    unsigned long offset,
+    void *p_data,
+    unsigned int bytes,
+    struct x86_emulate_ctxt *ctxt)
+{
+    struct mmio_ro_emulate_ctxt *mmio_ro_ctxt = ctxt->data;
+    unsigned long data = 0;
+
+    /* Only allow naturally-aligned stores at the original %cr2 address. */
+    if ( ((bytes | offset) & (bytes - 1)) || !bytes ||
+         offset != mmio_ro_ctxt->cr2 || bytes > sizeof(data) )
+    {
+        gdprintk(XENLOG_WARNING, "bad access (cr2=%lx, addr=%lx, bytes=%u)\n",
+                mmio_ro_ctxt->cr2, offset, bytes);
+        return X86EMUL_UNHANDLEABLE;
+    }
+
+    memcpy(&data, p_data, bytes);
+    subpage_mmio_write_emulate(mmio_ro_ctxt->mfn, PAGE_OFFSET(offset),
+                               data, bytes);
+
+    return X86EMUL_OKAY;
+}
+
 static const struct x86_emulate_ops mmio_ro_emulate_ops = {
     .read       = x86emul_unhandleable_rw,
     .insn_fetch = ptwr_emulated_insn_fetch,
Signature · 1 line
2.48.1