s390x/pci: some translation fixes
cover s390x/pci: some translation fixes
While reviewing Konstantin's migration series, a few edge cases have been identified in the existing code. Let's tighten them up first.
Matthew Rosato (3):
s390x/pci: Fix frame stepping in rpcit_service_call() s390x/pci: Use qemu_log_mask() for guest-triggered errors in
reg_ioat()
s390x/pci: Reject apertures beyond IOAT capacity in translation mode
hw/s390x/s390-pci-inst.c | 24 +++++++++++++++++++++--- include/hw/s390x/s390-pci-bus.h | 1 + 2 files changed, 22 insertions(+), 3 deletions(-)
Signature · 1 line
2.55.0
PATCH 1/3 s390x/pci: Fix frame stepping in rpcit_service_call()
When walking the guest IO address table, an invalid segment or region entry causes table_translate() to return entry.len equal to the frame size for that table level (1 MiB for ST, 2 GiB for RT). Adding entry.len directly to start only advances to the correct next frame boundary if start is already frame-aligned at that level -- which is true for a well-behaved Linux guest but not a general requirement.
Add logic to advance start to the next frame boundary rather than simply adding entry.len. For a frame-aligned start (the typical, well-behaved case) the result is identical.
--- hw/s390x/s390-pci-inst.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)
hw/s390x/s390-pci-inst.c +2 -1
@@ -808,7 +808,8 @@ int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra) coalesce =0 ; } - start += entry.len; +/* Advance to next frame boundary if start was not frame-aligned */ + start = (start & ~(entry.len -1 )) + entry.len;while (entry.iova < start && entry.iova < end) {if (dma_avail >0 || entry.perm ==IOMMU_NONE ) { dma_avail =s390_pci_update_iotlb (iommu, &entry);
Signature · 1 line
2.55.0
Re: [PATCH 1/3] s390x/pci: Fix frame stepping in rpcit_service_call()
Am 28.09.26 um 22:23 schrieb Matthew Rosato:
Quoted 7 lines
When walking the guest IO address table, an invalid segment or region entry causes
table_translate()to return entry.len equal to the frame size for that table level (1 MiB for ST, 2 GiB for RT). Adding entry.len directly to start only advances to the correct next frame boundary if start is already frame-aligned at that level -- which is true for a well-behaved Linux guest but not a general requirement.
Quoted 7 lines
Add logic to advance start to the next frame boundary rather than simply adding entry.len. For a frame-aligned start (the typical, well-behaved case) the result is identical.
Fixes 0125861eacc3 ("s390x/pci: fixup the code walking IOMMU tables")Signed-off-by Matthew Rosato <[email protected]>
Looks good to me.
Quoted 18 lines
--- hw/s390x/s390-pci-inst.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/s390x/s390-pci-inst.c +2
-1@@ -808,7 +808,8 @@ int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra) coalesce =0 ; } - start += entry.len; +/* Advance to next frame boundary if start was not frame-aligned */ + start = (start & ~(entry.len -1 )) + entry.len;while (entry.iova < start && entry.iova < end) {if (dma_avail >0 || entry.perm ==IOMMU_NONE ) { dma_avail =s390_pci_update_iotlb (iommu, &entry);
Re: [PATCH 1/3] s390x/pci: Fix frame stepping in rpcit_service_call()
In reply to Matthew RosatoHi Matthew,
Quoted 23 lines · Matthew Rosato
When walking the guest IO address table, an invalid segment or region entry causes
table_translate()to return entry.len equal to the frame size for that table level (1 MiB for ST, 2 GiB for RT). Adding entry.len directly to start only advances to the correct next frame boundary if start is already frame-aligned at that level -- which is true for a well-behaved Linux guest but not a general requirement.Add logic to advance start to the next frame boundary rather than simply adding entry.len. For a frame-aligned start (the typical, well-behaved case) the result is identical.
Fixes 0125861eacc3 ("s390x/pci: fixup the code walking IOMMU tables")Signed-off-by Matthew Rosato <[email protected]>--- hw/s390x/s390-pci-inst.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-)hw/s390x/s390-pci-inst.c +0
-0
hw/s390x/s390-pci-inst.c int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra) · line 812
Quoted 6 lines
hw/s390x/s390-pci-inst.c +2
-1@@ -808,7 +808,8 @@ int rpcit_service_call(S390CPU *cpu, uint8_t r1, uint8_t r2, uintptr_t ra) coalesce =0 ; } - start += entry.len; +/* Advance to next frame boundary if start was not frame-aligned */ + start = (start & ~(entry.len -1 )) + entry.len;
Could that be QEMU_ALIGN_UP()?
Quoted 3 lines
hw/s390x/s390-pci-inst.c +0
-0while (entry.iova < start && entry.iova < end) {if (dma_avail >0 || entry.perm ==IOMMU_NONE ) { dma_avail =s390_pci_update_iotlb (iommu, &entry);
Re: [PATCH 1/3] s390x/pci: Fix frame stepping in rpcit_service_call()
Quoted 6 lines
Quoted 4 lines
+ /* Advance to next frame boundary if start was not frame- aligned */
+ start = (start & ~(entry.len - 1)) + entry.len;Could that be
QEMU_ALIGN_UP()?
Oh, good idea. I think
start = QEMU_ALIGN_UP(start + 1, entry.len);
would work to ensure we always move up.
I'll test it out.
Thanks,
Matt
PATCH 2/3 s390x/pci: Use qemu_log_mask() for guest-triggered errors in reg_ioat()
In reply to Matthew RosatoThe error paths in reg_ioat() that are triggered by a guest providing invalid MPCIFC arguments use error_report(), which writes unconditionally to stderr. A misbehaving guest can exploit this to flood the host log by repeatedly issuing the instruction with bad arguments.
Replace these with qemu_log_mask(LOG_GUEST_ERROR, ...) so that the messages are suppressible.
--- hw/s390x/s390-pci-inst.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)
hw/s390x/s390-pci-inst.c +4 -2
@@ -17,6 +17,7 @@#include "system/memory.h" #include "qemu/error-report.h" #include "qemu/bswap.h" +#include "qemu/log.h" #include "system/hw_accel.h" #include "hw/core/boards.h" #include "hw/pci/pci_device.h"
@@ -1044,11 +1045,12 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,/* currently we only support designation type 1 with translation */ if (t && dt !=ZPCI_IOTA_RTTO ) { -error_report ("unsupported ioat dt %d t %d" , dt, t); +qemu_log_mask (LOG_GUEST_ERROR , +"unsupported ioat dt %d t %d" , dt, t);s390_program_interrupt (env,PGM_OPERAND , ra);return -EINVAL ; }else if (!t && !pbdev->rtr_avail) { -error_report ("relaxed translation not allowed" ); +qemu_log_mask (LOG_GUEST_ERROR ,"relaxed translation not allowed" );s390_program_interrupt (env,PGM_OPERAND , ra);return -EINVAL ; }
Signature · 1 line
2.55.0
Re: [PATCH 2/3] s390x/pci: Use qemu_log_mask() for guest-triggered errors in reg_ioat()
Am 28.09.26 um 22:23 schrieb Matthew Rosato:
Quoted 21 lines
The error paths in
reg_ioat()that are triggered by a guest providing invalid MPCIFC arguments useerror_report(), which writes unconditionally to stderr. A misbehaving guest can exploit this to flood the host log by repeatedly issuing the instruction with bad arguments.Replace these with
qemu_log_mask(LOG_GUEST_ERROR, ...)so that the messages are suppressible.Fixes 863f6f52b713 ("s390: implement pci instructions")Fixes dfcee1ea4c52 ("s390x/pci: add support for guests that request direct mapping")Signed-off-by Matthew Rosato <[email protected]>--- hw/s390x/s390-pci-inst.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-)hw/s390x/s390-pci-inst.c +0
-0
hw/s390x/s390-pci-inst.c line 18
Quoted 2 lines
hw/s390x/s390-pci-inst.c +0
-0@@ -17,6 +17,7 @@#include "system/memory.h" #include "qemu/error-report.h"
can we drop that now?
Quoted 7 lines
hw/s390x/s390-pci-inst.c +1
-0#include "qemu/bswap.h" +#include "qemu/log.h" #include "system/hw_accel.h" #include "hw/core/boards.h" #include "hw/pci/pci_device.h" @@ -1044,11 +1045,12 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,
hw/s390x/s390-pci-inst.c static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, · line 1053
Quoted 10 lines
hw/s390x/s390-pci-inst.c +3
-2/* currently we only support designation type 1 with translation */ if (t && dt !=ZPCI_IOTA_RTTO ) { -error_report ("unsupported ioat dt %d t %d" , dt, t); +qemu_log_mask (LOG_GUEST_ERROR , +"unsupported ioat dt %d t %d" , dt, t);s390_program_interrupt (env,PGM_OPERAND , ra);return -EINVAL ; }else if (!t && !pbdev->rtr_avail) { -error_report ("relaxed translation not allowed" ); +qemu_log_mask (LOG_GUEST_ERROR ,"relaxed translation not allowed" );
I think we need "\n" here. error_report does add it, qemu_log_mask not.
Quoted 3 lines
hw/s390x/s390-pci-inst.c +0
-0s390_program_interrupt (env,PGM_OPERAND , ra);return -EINVAL ; }
Re: [PATCH 2/3] s390x/pci: Use qemu_log_mask() for guest-triggered errors in reg_ioat()
Quoted 20 lines · Christian Borntraeger
Am 28.09.26 um 22:23 schrieb Matthew Rosato:
Quoted 17 lines
The error paths in
reg_ioat()that are triggered by a guest providing invalid MPCIFC arguments useerror_report(), which writes unconditionally to stderr. A misbehaving guest can exploit this to flood the host log by repeatedly issuing the instruction with bad arguments.Replace these with
qemu_log_mask(LOG_GUEST_ERROR, ...)so that the messages are suppressible.Fixes 863f6f52b713 ("s390: implement pci instructions")Fixes dfcee1ea4c52 ("s390x/pci: add support for guests that requestdirect mapping")
Signed-off-by Matthew Rosato <[email protected]>---
hw/s390x/s390-pci-inst.c| 6 ++++--1 file changed, 4 insertions(+), 2 deletions(-)
Quoted 9 lines
hw/s390x/s390-pci-inst.c line 18
Quoted 7 lines
hw/s390x/s390-pci-inst.c +0
-0@@ -17,6 +17,7 @@ #include "system/memory.h" #include zcan we drop that now?
Looks like yes, will drop
Quoted 14 lines
Quoted 14 lines
hw/s390x/s390-pci-inst.c +1
-0#include "qemu/bswap.h" +#include "qemu/log.h" #include "system/hw_accel.h" #include "hw/core/boards.h" #include "hw/pci/pci_device.h"@@ -1044,11 +1045,12 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib, /* currently we only support designation type 1 withtranslation */
if (t && dt !=ZPCI_IOTA_RTTO) {
-error_report("unsupported ioat dt %d t %d", dt, t); + qemu_log_mask(LOG_GUEST_ERROR,
+ "unsupported ioat dt %d t %d", dt, t);
Quoted 9 lines
Quoted 6 lines
s390_program_interrupt(env, PGM_OPERAND, ra); return-EINVAL;
} else if (!t && !pbdev->rtr_avail) {
-error_report("relaxed translation not allowed"); +qemu_log_mask(LOG_GUEST_ERROR, "relaxed translation not allowed");I think we need "\n" here.
error_reportdoes add it,qemu_log_masknot.
ACK, just verified - will fix here and in patch 3.
Quoted 5 lines
Quoted 4 lines
s390_program_interrupt(env, PGM_OPERAND, ra); return-EINVAL;
}
PATCH 3/3 s390x/pci: Reject apertures beyond IOAT capacity in translation mode
In reply to Matthew RosatoIn order to support direct mapping (which is only bound by the aperture size of the underlying host EDMA reported via vfio) QEMU reports the host-provided EDMA to the guest. However, when using translation mode QEMU only supports up to 4 TiB currently (the size of a single RT table).
Reject attempts by a guest to register a translation aperture that would exceed what QEMU can support by giving an operand exception on the MPCIFC instruction that requested the large aperture. This restriction can be removed in the future if/when support for additional IOAT regions is added.
--- hw/s390x/s390-pci-inst.c | 15 +++++++++++++++ include/hw/s390x/s390-pci-bus.h | 1 + 2 files changed, 16 insertions(+)
hw/s390x/s390-pci-inst.c +15 -0
@@ -1055,6 +1055,21 @@ static int reg_ioat(CPUS390XState *env, S390PCIBusDevice *pbdev, ZpciFib fib,return -EINVAL ; } +/* +* We report an EDMA that may exceed what QEMU can handle in support +* of direct-mapping. If the guest attempts to register an IOAT that +* is too large, reject it with an informative message. Only direct +* mapping can be used for guests of this size until support is added +* to QEMU for additional IOAT regions. +*/ +if (t && pal >=ZPCI_TABLE_SIZE_RT ) { +qemu_log_mask (LOG_GUEST_ERROR , +"ioat pal 0x%" PRIx64" exceeds max translatable address" , + pal); +s390_program_interrupt (env,PGM_OPERAND , ra); +return -EINVAL ; + } + iommu->pba = pba; iommu->pal = pal; iommu->g_iota = g_iota;
include/hw/s390x/s390-pci-bus.h +1 -0
@@ -140,6 +140,7 @@ enum ZpciIoatDtype {#define ZPCI_PT_BITS 8#define ZPCI_ST_SHIFT (ZPCI_PT_BITS + TARGET_PAGE_BITS)#define ZPCI_RT_SHIFT (ZPCI_ST_SHIFT + ZPCI_TABLE_BITS) +#define ZPCI_TABLE_SIZE_RT (1ULL << (ZPCI_RT_SHIFT + ZPCI_TABLE_BITS))#define ZPCI_RTE_FLAG_MASK 0x3fffULL#define ZPCI_RTE_ADDR_MASK (~ZPCI_RTE_FLAG_MASK)
Signature · 1 line
2.55.0