xen/x86: fix implementation of subpage r/o MMIO
v2 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 | 51 ++---------- xen/arch/x86/hvm/hvm.c | 18 ++--- xen/arch/x86/hvm/mmio.c | 100 ++++++++++++++++++++++++ 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 | 103 ++++++------------------- xen/arch/x86/pv/ro-page-fault.c | 34 ++++++++ xen/drivers/vpci/msix.c | 47 +---------- xen/include/xen/io.h | 68 ++++++++++++++++ 12 files changed, 259 insertions(+), 235 deletions(-) create mode 100644 xen/arch/x86/hvm/mmio.c create mode 100644 xen/include/xen/io.h
Signature · 1 line
2.48.1
PATCH v2 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 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 | 32 +++++-------------- xen/drivers/vpci/msix.c | 47 ++-------------------------- xen/include/xen/io.h | 68 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 113 deletions(-) create mode 100644 xen/include/xen/io.h
xen/arch/x86/hvm/vmsi.c +3 -44
@@ -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 -25
@@ -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;
@@ -5115,7 +5115,6 @@ static void subpage_mmio_write_emulate(if (test_bit (offset /MMIO_RO_SUBPAGE_GRAN , entry->ro_elems) ) { - write_ignored:gprintk (XENLOG_WARNING ,"ignoring write to R/O MMIO 0x%" PRI_mfn"%03x len %u\n" ,mfn_x (mfn), offset, len);
@@ -5131,26 +5130,7 @@ 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 (); - goto write_ignored; - } +write_mmio (addr + offset, data, len); }#ifdef CONFIG_HVM
@@ -5185,18 +5165,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 +68 -0
68 lines not on this page · raw message
Signature · 1 line
2.48.1
Re: [PATCH v2 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.hheader.No functional change intended.
Signed-off-by Roger Pau Monné <[email protected]>
Quoted 322 lines
---
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 | 32 +++++-------------- xen/drivers/vpci/msix.c | 47 ++-------------------------- xen/include/xen/io.h | 68 +++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 113 deletions(-) create mode 100644 xen/include/xen/io.hxen/arch/x86/hvm/vmsi.c +3
-44@@ -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
-25@@ -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;@@ -5115,7 +5115,6 @@ static void subpage_mmio_write_emulate(if (test_bit (offset /MMIO_RO_SUBPAGE_GRAN , entry->ro_elems) ) { - write_ignored:gprintk (XENLOG_WARNING ,"ignoring write to R/O MMIO 0x%" PRI_mfn"%03x len %u\n" ,mfn_x (mfn), offset, len);@@ -5131,26 +5130,7 @@ 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 (); - goto write_ignored; - } +write_mmio (addr + offset, data, len); }#ifdef CONFIG_HVM @@ -5185,18 +5165,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@@ -17,6 +17,7 @@* License along with this program; If not, see <http://www.gnu.org/licenses/>. */ +#include <xen/io.h> #include <xen/sched.h> #include <xen/vpci.h> @@ -344,28 +345,7 @@ static int adjacent_read(const struct domain *d, const struct vpci_msix *msix,return X86EMUL_OKAY ; } -switch ( len ) - { -case 1 : - *data =readb (mem +PAGE_OFFSET (addr)); -break ; - -case 2 : - *data =readw (mem +PAGE_OFFSET (addr)); -break ; - -case 4 : - *data =readl (mem +PAGE_OFFSET (addr)); -break ; - -case 8 : - *data =readq (mem +PAGE_OFFSET (addr)); -break ; - -default : -ASSERT_UNREACHABLE (); -break ; - } + *data =read_mmio (mem +PAGE_OFFSET (addr), len);spin_unlock (&vpci->lock);return X86EMUL_OKAY ;@@ -493,28 +473,7 @@ static int adjacent_write(const struct domain *d, const struct vpci_msix *msix,return X86EMUL_OKAY ; } -switch ( len ) - { -case 1 : -writeb (data, mem +PAGE_OFFSET (addr)); -break ; - -case 2 : -writew (data, mem +PAGE_OFFSET (addr)); -break ; - -case 4 : -writel (data, mem +PAGE_OFFSET (addr)); -break ; - -case 8 : -writeq (data, mem +PAGE_OFFSET (addr)); -break ; - -default : -ASSERT_UNREACHABLE (); -break ; - } +write_mmio (mem +PAGE_OFFSET (addr), data, len);spin_unlock (&vpci->lock);return X86EMUL_OKAY ;xen/include/xen/io.h new file +68
-1@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* IO related routines. +* +* Copyright (c) 2025 Cloud Software Group +*/ +#ifndef XEN_IO_H +#define XEN_IO_H + +#include <xen/bug.h> + +#include <asm/io.h> + +static inline unsigned long read_mmio (const volatile void __iomem *mem, +unsigned int size) +{ +switch ( size ) + { +case 1 : +return readb (mem); + +case 2 : +return readw (mem); + +case 4 : +return readl (mem); + +#ifdef CONFIG_64BIT +case 8 : +return readq (mem); +#endif + +default : +ASSERT_UNREACHABLE (); +return ~0UL ; + } +} + +static inline void write_mmio (volatile void __iomem *mem,unsigned long data, +unsigned int size) +{ +switch ( size ) + { +case 1 : +writeb (data, mem); +break ; + +case 2 : +writew (data, mem); +break ; + +case 4 : +writel (data, mem); +break ; + +#ifdef CONFIG_64BIT +case 8 : +writeq (data, mem); +break ; +#endif + +default : +ASSERT_UNREACHABLE (); +break ; + } +} + +#endif /* XEN_IO_H */ --2.48.1
Re: [PATCH v2 1/4] xen/io: provide helpers for multi size MMIO accesses
In reply to Roger Pau MonneQuoted 10 lines · Roger Pau Monne
xen/include/xen/io.h new file +4
-0@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* IO related routines. +*
xen/include/xen/io.h line 14
Quoted 10 lines
xen/include/xen/io.h new file +10
-0+* Copyright (c) 2025 Cloud Software Group +*/ +#ifndef XEN_IO_H +#define XEN_IO_H + +#include <xen/bug.h> + +#include <asm/io.h> + +static inline unsigned long read_mmio(const volatile void __iomem *mem,
void *__iomem. (i.e. without the __iomem in the middle of the type). It seems most examples of this in Xen are wrong.
~Andrew
Re: [PATCH v2 1/4] xen/io: provide helpers for multi size MMIO accesses
Quoted 16 lines · Andrew Cooper
Quoted 15 lines · Roger Pau Monne
xen/include/xen/io.h new file +9
-0@@ -0,0 +1,68 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* IO related routines. +* +* Copyright (c) 2025 Cloud Software Group +*/ +#ifndef XEN_IO_H +#define XEN_IO_H +
Quoted 8 lines
xen/include/xen/io.h line 14
Quoted 5 lines
xen/include/xen/io.h new file +5
-0+#include <xen/bug.h> + +#include <asm/io.h> + +static inline unsigned long read_mmio(const volatile void __iomem *mem,void *__iomem. (i.e. without the
__iomemin the middle of the type). It seems most examples of this in Xen are wrong.
Hmm, perhaps in turn because I don't see why it would need to be that placement. The attribute describes what's pointed to, not the variable (or parameter here) itself.
Jan
Re: [PATCH v2 1/4] xen/io: provide helpers for multi size MMIO accesses
In reply to Roger Pau MonneQuoted 24 lines · Roger Pau Monne
xen/arch/x86/mm.c +0
-13@@ -5115,7 +5115,6 @@ static void subpage_mmio_write_emulate(if (test_bit (offset /MMIO_RO_SUBPAGE_GRAN , entry->ro_elems) ) { - write_ignored:gprintk (XENLOG_WARNING ,"ignoring write to R/O MMIO 0x%" PRI_mfn"%03x len %u\n" ,mfn_x (mfn), offset, len);@@ -5131,26 +5130,7 @@ 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 ;
xen/arch/x86/mm.c static void subpage_mmio_write_emulate( · line 5134
Quoted 10 lines
xen/arch/x86/mm.c +1
-8-case 8 : -writeq (*(const uint64_t *)data, addr); -break ; -default : -/* mmio_ro_emulated_write() already validated the size */ -ASSERT_UNREACHABLE (); - goto write_ignored; - } +write_mmio (addr + offset, data, len); }
Should probably have noticed this on v1 already: The log message is now lost for the write-ignored case. It looks easy enough to have the function return a boolean indicating "done", to retain original behavior here.
Jan
Re: [PATCH v2 1/4] xen/io: provide helpers for multi size MMIO accesses
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Roger Pau Monne
xen/arch/x86/mm.c +0
-19@@ -5115,7 +5115,6 @@ static void subpage_mmio_write_emulate(if (test_bit (offset /MMIO_RO_SUBPAGE_GRAN , entry->ro_elems) ) { - write_ignored:gprintk (XENLOG_WARNING ,"ignoring write to R/O MMIO 0x%" PRI_mfn"%03x len %u\n" ,mfn_x (mfn), offset, len);@@ -5131,26 +5130,7 @@ 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 ();
Quoted 8 lines
xen/arch/x86/mm.c static void subpage_mmio_write_emulate( · line 5134
Quoted 4 lines
xen/arch/x86/mm.c +1
-2- goto write_ignored; - } +write_mmio (addr + offset, data, len); }Should probably have noticed this on v1 already: The log message is now lost for the write-ignored case. It looks easy enough to have the function return a boolean indicating "done", to retain original behavior here.
Hm, I didn't seem to me the message wants conserving, as it's unreachable code. I can try to add again, but we don't print such message in other cases.
Thanks, Roger.
Re: [PATCH v2 1/4] xen/io: provide helpers for multi size MMIO accesses
Quoted 41 lines · Roger Pau Monné
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Roger Pau Monne
xen/arch/x86/mm.c +0
-19@@ -5115,7 +5115,6 @@ static void subpage_mmio_write_emulate(if (test_bit (offset /MMIO_RO_SUBPAGE_GRAN , entry->ro_elems) ) { - write_ignored:gprintk (XENLOG_WARNING ,"ignoring write to R/O MMIO 0x%" PRI_mfn"%03x len %u\n" ,mfn_x (mfn), offset, len);@@ -5131,26 +5130,7 @@ 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 ();Quoted 8 lines
xen/arch/x86/mm.c static void subpage_mmio_write_emulate( · line 5134
Quoted 4 lines
xen/arch/x86/mm.c +1
-2- goto write_ignored; - } +write_mmio (addr + offset, data, len); }Should probably have noticed this on v1 already: The log message is now lost for the write-ignored case. It looks easy enough to have the function return a boolean indicating "done", to retain original behavior here.
Quoted 3 lines
Hm, I didn't seem to me the message wants conserving, as it's unreachable code. I can try to add again, but we don't print such message in other cases.
This sub-page stuff is special, but I wouldn't mind if we dropped the message altogether. The "unreachable code" argument is slightly weak, as it's in particular when the assumption is violated that we would want to know about it.
Jan
PATCH v2 2/4 x86/hvm: fix handling of accesses to partial r/o MMIO pages
In reply to Roger Pau MonneThe 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 accesses to read-only MMIO regions, as handling of those accesses is now done by handle_mmio().
---
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 | 51 ++----------- xen/arch/x86/hvm/hvm.c | 17 ++--- xen/arch/x86/hvm/mmio.c | 100 +++++++++++++++++++++++++ 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, 132 insertions(+), 90 deletions(-) create mode 100644 xen/arch/x86/hvm/mmio.c
xen/arch/x86/hvm/Makefile +1 -0
@@ -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 +6 -45
63 lines not on this page · raw message
xen/arch/x86/hvm/hvm.c +7 -10
@@ -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 +100 -0
100 lines not on this page · raw message
xen/arch/x86/include/asm/hvm/emulate.h +0 -1
@@ -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
@@ -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
@@ -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
@@ -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,
@@ -5133,30 +5126,6 @@ static void subpage_mmio_write_emulate(write_mmio (addr + offset, data, len); } -#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
Re: [PATCH v2 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,12 @@ 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 +6
-1-if ( is_mmio &&is_hardware_domain (currd) ) +if ( is_mmio &&is_hardware_domain (currd) && +/* +* Do not attempt to fixup 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)) ) {/* * PVH dom0 is likely missing MMIO mappings on the p2m, due to
Doesn't this need limiting to writes, i.e. permitting reads to still be handled right here?
Quoted 9 lines
xen/arch/x86/hvm/mmio.c new file +6
-0@@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* MMIO related routines. +* +* Copyright (c) 2025 Cloud Software Group +*/
xen/arch/x86/hvm/mmio.c line 16
Quoted 10 lines
xen/arch/x86/hvm/mmio.c new file +10
-0+ +#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, addr, &t);
Don't you need to use PFN_DOWN() on addr?
xen/arch/x86/hvm/mmio.c line 27
Quoted 10 lines
xen/arch/x86/hvm/mmio.c new file +10
-0+return !mfn_eq (mfn,INVALID_MFN ) && t == p2m_mmio_direct && +subpage_mmio_find_page (mfn); +} + +static int cf_check subpage_mmio_read( +struct vcpu *v,unsigned long addr,unsigned int len,unsigned long *data) +{ +struct domain *d = v->domain; +p2m_type_t t; +mfn_t mfn =get_gfn_query (d, addr, &t);
Same here and further down, and in the write case?
Quoted 19 lines
xen/arch/x86/hvm/mmio.c new file +19
-0+struct subpage_ro_range *entry; +volatile void __iomem *mem; + + *data = ~0UL ; + +if (mfn_eq (mfn,INVALID_MFN ) || t != p2m_mmio_direct ) + { +put_gfn (d, addr); +return X86EMUL_RETRY ; + } + + entry =subpage_mmio_find_page (mfn); +if ( !entry ) + { +put_gfn (d, addr); +return X86EMUL_OKAY ; + } + + mem =subpage_mmio_map_page (entry);
xen/arch/x86/hvm/mmio.c line 56
Quoted 10 lines
xen/arch/x86/hvm/mmio.c new file +10
-0+if ( !mem ) + { +put_gfn (d, addr); +gprintk (XENLOG_ERR , +"Failed to map page for MMIO read at %#lx -> %#lx\n" , + addr,mfn_to_maddr (mfn) +PAGE_OFFSET (addr)); +return X86EMUL_OKAY ; + } + + *data =read_mmio (mem +PAGE_OFFSET (addr), len);
What if this crosses the trailing page boundary? Imo subpage_mmio_accept() would better reject misaligned accesses (at least until we know we need to handle such for some obscure reason).
xen/arch/x86/hvm/mmio.c line 44
Quoted 3 lines
xen/arch/x86/hvm/mmio.c new file +3
-0+put_gfn (d, addr); +return X86EMUL_OKAY ; +}
Thinking of it - why do reads need handling here? The guest has read access? Hmm, yes, read-modify-write operations may take this path. Maybe worth having a comment here.
xen/arch/x86/hvm/mmio.c line 89
Quoted 9 lines
xen/arch/x86/hvm/mmio.c new file +9
-0+void register_subpage_ro_handler (struct domain *d) +{ +static const struct hvm_mmio_ops subpage_mmio_ops = { + .check = subpage_mmio_accept, + .read = subpage_mmio_read, + .write = subpage_mmio_write, + }; + +register_mmio_handler (d, &subpage_mmio_ops);
Are there any criteria by which we could limit the registration of this handler?
Jan
Re: [PATCH v2 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages
Quoted 13 lines · Jan Beulich
xen/arch/x86/hvm/mmio.c line 89
Quoted 9 lines · Roger Pau Monne
xen/arch/x86/hvm/mmio.c new file +9
-0+void register_subpage_ro_handler (struct domain *d) +{ +static const struct hvm_mmio_ops subpage_mmio_ops = { + .check = subpage_mmio_accept, + .read = subpage_mmio_read, + .write = subpage_mmio_write, + }; + +register_mmio_handler (d, &subpage_mmio_ops);Are there any criteria by which we could limit the registration of this handler?
Yes, see patch 3.
Signature · 3 lines
Best Regards, Marek Marczykowski-Górecki Invisible Things Lab
Re: [PATCH v2 2/4] x86/hvm: fix handling of accesses to partial r/o MMIO pages
In reply to Jan BeulichQuoted 12 lines · Jan Beulich
Quoted 11 lines · Roger Pau Monne
xen/arch/x86/hvm/emulate.c +4
-1@@ -370,7 +370,12 @@ 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 accesses to r/o MMIO regions, they +* are expected to be terminated by the null handler below.
Quoted 8 lines
xen/arch/x86/hvm/emulate.c static int hvmemul_do_io( · line 381
Quoted 5 lines
xen/arch/x86/hvm/emulate.c +2
-0+*/ + !rangeset_contains_singleton(mmio_ro_ranges, PFN_DOWN(addr)) ) {/* * PVH dom0 is likely missing MMIO mappings on the p2m, due to Doesn't this need limiting to writes, i.e. permitting reads to still be handled right here?
Oh, I see, yes, it would be fine to attempt to fixup read accesses to mmio_ro_ranges ranges.
Quoted 22 lines
Quoted 9 lines
xen/arch/x86/hvm/mmio.c new file +6
-0@@ -0,0 +1,100 @@ +/* SPDX-License-Identifier: GPL-2.0-only */ +/* +* MMIO related routines. +* +* Copyright (c) 2025 Cloud Software Group +*/ xen/arch/x86/hvm/mmio.c line 16
Quoted 10 lines
xen/arch/x86/hvm/mmio.c new file +10
-0+ +#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, addr, &t);Don't you need to use
PFN_DOWN()on addr?
Quoted 12 lines
xen/arch/x86/hvm/mmio.c line 27
Quoted 10 lines
xen/arch/x86/hvm/mmio.c new file +10
-0+return !mfn_eq (mfn,INVALID_MFN ) && t == p2m_mmio_direct && +subpage_mmio_find_page (mfn); +} + +static int cf_check subpage_mmio_read( +struct vcpu *v,unsigned long addr,unsigned int len,unsigned long *data) +{ +struct domain *d = v->domain; +p2m_type_t t; +mfn_t mfn =get_gfn_query (d, addr, &t);Same here and further down, and in the write case?
Hm, yes I do.
Quoted 25 lines
Quoted 25 lines
xen/arch/x86/hvm/mmio.c new file +25
-0+struct subpage_ro_range *entry; +volatile void __iomem *mem; + + *data = ~0UL ; + +if (mfn_eq (mfn,INVALID_MFN ) || t != p2m_mmio_direct ) + { +put_gfn (d, addr); +return X86EMUL_RETRY ; + } + + entry =subpage_mmio_find_page (mfn); +if ( !entry ) + { +put_gfn (d, addr); +return X86EMUL_OKAY ; + } + + mem =subpage_mmio_map_page (entry); +if ( !mem ) + { +put_gfn (d, addr); +gprintk (XENLOG_ERR , +"Failed to map page for MMIO read at %#lx -> %#lx\n" , + addr,mfn_to_maddr (mfn) +PAGE_OFFSET (addr));
Quoted 8 lines
xen/arch/x86/hvm/mmio.c line 56
Quoted 4 lines
xen/arch/x86/hvm/mmio.c new file +4
-0+return X86EMUL_OKAY ; + } + + *data =read_mmio (mem +PAGE_OFFSET (addr), len);What if this crosses the trailing page boundary? Imo
subpage_mmio_accept()would better reject misaligned accesses (at least until we know we need to handle such for some obscure reason).
Yes, the previous mmio_ro_emulated_write() did already reject such accesses.
Thanks, Roger.
PATCH v2 3/4 x86/hvm: only register the r/o subpage ops when needed
In reply to Roger Pau MonneMMIO 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
@@ -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
@@ -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
@@ -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
PATCH v2 4/4 x86/mm: move mmio_ro_emulated_write() to PV only file
In reply to Roger Pau Monnemmio_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
@@ -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
@@ -5142,32 +5142,6 @@ void subpage_mmio_write_emulate(write_mmio (addr + offset, data, len); } -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
@@ -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