memory.c: make memory_region_dispatch_* report ACCESS_ERROR
cover memory.c: make memory_region_dispatch_* report ACCESS_ERROR
Hello,
I decided to propose this change after working in a RISC-V gitlab bug [1]. In my proposed fix [2] I had to make assumptions about address_space_ld* error return, which is always MEMTX_DECODE_ERROR, to identify that in that particular scenario this is a permission error. In RISC-V (and from what I can see other archs like x86) an ACCESS_ERROR due to mem page permission issues will fire a different fault than other errors like page not found.
The core logic is on memory_region_access_valid(), which already makes a distinction between error reasons when we look at the qemu_log_mask() errors, e.g. this is being characterized as a 'rejected':
if (mr->ops->valid.accepts
&& !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) {
qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX
", size %u, region '%s', reason: rejected\n",
is_write ? "write" : "read",
addr, size, memory_region_name(mr));but memory_region_access_valid() just returns a bool, and we remain oblivious about about the nature of the error found. Patch 1 adds an extra parameter in memory_region_access_valid() to write the reason code for a valid/invalid access.
Patch 2 changes memory_region_dispatch_(read|write) to return the reason code from memory_region_access_valid() in case an error was found. This will change error codes from DECODE_ERROR to ACCESS_ERROR when there's a permission error involved, so before proposing this change I did a search in all cases where DECODE_ERROR is handled via memory_region_dispatch_* APIs. I didn't see any code where a DECODE_ERROR is ignored (i.e treated as an OK result) and that would be impacted by this change. Gitlab CI didn't complain about it either, so I feel moderately confident that this change can be done without too much harm.
[1] https://gitlab.com/qemu-project/qemu/-/work_items/3502
[2] https://lore.kernel.org/qemu-devel/[email protected]/
Daniel Henrique Barboza (2):
system/memory.c: add MemTxResult arg in memory_region_access_valid() memory.c: read memory_region_access_valid() err reason in
memory_region_dispatch_*
hw/s390x/s390-pci-inst.c | 2 +- include/system/memory.h | 2 +- system/memory.c | 25 ++++++++++++++++++++----- system/physmem.c | 3 ++- 4 files changed, 24 insertions(+), 8 deletions(-)
Signature · 1 line
2.43.0
PATCH 1/2 system/memory.c: add MemTxResult arg in memory_region_access_valid()
memory_region_access_valid() returns true and false and nothing else, but it makes distintions between error types that we can see via each error_log message: if mr->ops->valid.accepts fails the reason is set to "rejected", reason is set to "unaligned" if addr is unaligned and reason is set to "invalid size" if size is less that min_access_size or greater than max_access_size.
We're particularly interested in the "reason: rejected" result. In RISC-V this is thrown when the mem_op tries to access a protected page, and we need to throw a special exception for this case. memory_region_dispatch_read() (and write()) will default to MEMTX_DECODE_ERROR because memory_region_access_valid() does not give additional info when it fails, and back in the RISC-V side we're having to make assumptions to determine that this MEMTX_DECODE_ERROR is in fact a "rejected" error type.
Add a new MemTxResult arg in memory_region_access_valid() that the funcion will use to set the reason why it failed. Based on the error reasons (described above) and the definition of each MemTxResult, we're setting the following results:
- result ok: MEMTX_OK
- rejected: MEMTX_ACCESS_ERROR
- all other errors: MEMTX_DECODE_ERROR
Existing callers are unchanged. No behavioral change is intended for now.
--- hw/s390x/s390-pci-inst.c | 2 +- include/system/memory.h | 2 +- system/memory.c | 20 +++++++++++++++++--- system/physmem.c | 3 ++- 4 files changed, 21 insertions(+), 6 deletions(-)
hw/s390x/s390-pci-inst.c +1 -1
@@ -904,7 +904,7 @@ int pcistb_service_call(S390CPU *cpu, uint8_t r1, uint8_t r3, uint64_t gaddr,for (i =0 ; i < len; i +=8 ) {if (!memory_region_access_valid (mr, offset + i,8 , true, -MEMTXATTRS_UNSPECIFIED )) { +MEMTXATTRS_UNSPECIFIED ,NULL )) {s390_program_interrupt (env,PGM_OPERAND , ra);return 0 ; }
include/system/memory.h +1 -1
@@ -2602,7 +2602,7 @@ void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled);bool memory_region_access_valid (MemoryRegion *mr,hwaddr addr,unsigned size,bool is_write, -MemTxAttrs attrs); +MemTxAttrs attrs,MemTxResult *result);/** * memory_region_dispatch_read: perform a read directly to the specified
system/memory.c +17 -3
@@ -1403,14 +1403,22 @@ bool memory_region_access_valid(MemoryRegion *mr,hwaddr addr, unsigned size, bool is_write, - MemTxAttrs attrs) + MemTxAttrs attrs, + MemTxResult *result) { + if (result) { + *result =MEMTX_OK ; + } +if (mr->ops->valid.accepts && !mr->ops->valid.accepts (mr->opaque, addr, size, is_write, attrs)) {qemu_log_mask (LOG_INVALID_MEM ,"Invalid %s at addr 0x%" HWADDR_PRIX ", size %u, region '%s', reason: rejected\n" , is_write ?"write" :"read" , addr, size,memory_region_name (mr)); +if (result) { + *result =MEMTX_ACCESS_ERROR ; + }return false; }
@@ -1419,6 +1427,9 @@ bool memory_region_access_valid(MemoryRegion *mr,", size %u, region '%s', reason: unaligned\n" , is_write ?"write" :"read" , addr, size, memory_region_name(mr)); +if (result) { + *result =MEMTX_DECODE_ERROR ; + }return false; }
@@ -1436,6 +1447,9 @@ bool memory_region_access_valid(MemoryRegion *mr, addr, size,memory_region_name (mr), mr->ops->valid.min_access_size, mr->ops->valid.max_access_size); +if (result) { + *result =MEMTX_DECODE_ERROR ; + }return false; }return true;
@@ -1478,7 +1492,7 @@ MemTxResult memory_region_dispatch_read(MemoryRegion *mr, mr->alias_offset + addr, pval, op, attrs); } -if (!memory_region_access_valid (mr, addr, size, false, attrs)) { +if (!memory_region_access_valid (mr, addr, size, false, attrs,NULL )) { *pval =unassigned_mem_read (mr, addr, size);return MEMTX_DECODE_ERROR ; }
@@ -1527,7 +1541,7 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr, mr->alias_offset + addr, data, op, attrs); } -if (!memory_region_access_valid (mr, addr, size, true, attrs)) { +if (!memory_region_access_valid (mr, addr, size, true, attrs,NULL )) {unassigned_mem_write (mr, addr, data, size);return MEMTX_DECODE_ERROR ; }
system/physmem.c +2 -1
@@ -3635,7 +3635,8 @@ static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len, mr =flatview_translate (fv, addr, &xlat, &l, is_write, attrs);if (!memory_access_is_direct (mr, is_write, attrs)) { l =memory_access_size (mr, l, addr); -if (!memory_region_access_valid (mr, xlat, l, is_write, attrs)) { +if (!memory_region_access_valid (mr, xlat, l, is_write, attrs, +NULL )) {return false; } }
Signature · 1 line
2.43.0
Re: [PATCH 1/2] system/memory.c: add MemTxResult arg in memory_region_access_valid()
Hi Daniel,
Quoted 16 lines · Daniel Henrique Barboza
memory_region_access_valid()returns true and false and nothing else, but it makes distintions between error types that we can see via eacherror_logmessage: ifmr->ops->valid.acceptsfails the reason is set to "rejected", reason is set to "unaligned" if addr is unaligned and reason is set to "invalid size" if size is less thatmin_access_sizeor greater thanmax_access_size.We're particularly interested in the "reason: rejected" result. In RISC-V this is thrown when the
mem_optries to access a protected page, and we need to throw a special exception for this case.memory_region_dispatch_read()(andwrite()) will default toMEMTX_DECODE_ERRORbecausememory_region_access_valid()does not give additional info when it fails, and back in the RISC-V side we're having to make assumptions to determine that thisMEMTX_DECODE_ERRORis in fact a "rejected" error type.
Quoted 8 lines
Add a new MemTxResult arg in
memory_region_access_valid()that the funcion will use to set the reason why it failed. Based on the error reasons (described above) and the definition of each MemTxResult, we're setting the following results:- result ok:
MEMTX_OK
- rejected:MEMTX_ACCESS_ERROR
- all other errors:MEMTX_DECODE_ERROR
Orthogonal, in "hw/pci/pci_device.h":
/**
* pci_dma_rw: Read from or write to an address space from PCI device. *
* Return a MemTxResult indicating whether the operation succeeded
* or failed (eg unassigned memory, device rejected the transaction,
* IOMMU fault).
This could deserve clarification as the mapping is not obvious to me.
Without looking at the code, I'd expect:
. unassigned memory -> MEMTX_DECODE_ERROR
. device rejected the transaction -> MEMTX_ACCESS_ERROR . IOMMU fault -> MEMTX_ACCESS_ERROR
What confuses me I guess is having 3 descriptions for 2 enums.
Quoted 18 lines
Existing callers are unchanged. No behavioral change is intended for now.
Signed-off-by Daniel Henrique Barboza <[email protected]>--- hw/s390x/s390-pci-inst.c | 2 +- include/system/memory.h | 2 +- system/memory.c | 20 +++++++++++++++++--- system/physmem.c | 3 ++- 4 files changed, 21 insertions(+), 6 deletions(-)include/system/memory.h +0
-0
include/system/memory.h void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled); · line 2602
Quoted 1 line
include/system/memory.h +0
-0@@ -2602,7 +2602,7 @@ void mtree_info(bool flatview, bool dispatch_tree, bool owner, bool disabled);
Please update the docstring.
Quoted 12 lines
include/system/memory.h +1
-1bool memory_region_access_valid (MemoryRegion *mr,hwaddr addr,unsigned size,bool is_write, -MemTxAttrs attrs); +MemTxAttrs attrs,MemTxResult *result);/** * memory_region_dispatch_read: perform a read directly to the specified system/memory.c +0
-0
system/memory.c bool memory_region_access_valid(MemoryRegion *mr, · line 1410
Quoted 9 lines
system/memory.c +4
-1@@ -1403,14 +1403,22 @@ bool memory_region_access_valid(MemoryRegion *mr,hwaddr addr, unsigned size, bool is_write, - MemTxAttrs attrs) + MemTxAttrs attrs, + MemTxResult *result) { + if (result) { + *result =MEMTX_OK ;
Per "exec/memattrs.h":
/* New-style MMIO accessors can indicate that the transaction failed.
* A zero (MEMTX_OK) response means success; anything else is a
* failure of some kind. The memory subsystem will bitwise-OR together
* results if it is synthesizing an operation from multiple smaller
* accesses.
*/
The caller should initialize to MEMTX_OK.
system/memory.c bool memory_region_access_valid(MemoryRegion *mr, · line 1420
Quoted 10 lines
system/memory.c +4
-0+ } +if (mr->ops->valid.accepts && !mr->ops->valid.accepts (mr->opaque, addr, size, is_write, attrs)) {qemu_log_mask (LOG_INVALID_MEM ,"Invalid %s at addr 0x%" HWADDR_PRIX ", size %u, region '%s', reason: rejected\n" , is_write ?"write" :"read" , addr, size,memory_region_name (mr)); +if (result) { + *result =MEMTX_ACCESS_ERROR ;
Here I'd OR.
system/memory.c bool memory_region_access_valid(MemoryRegion *mr, · line 1431
Quoted 10 lines
system/memory.c +3
-0+ }return false; }@@ -1419,6 +1427,9 @@ bool memory_region_access_valid(MemoryRegion *mr,", size %u, region '%s', reason: unaligned\n" , is_write ?"write" :"read" , addr, size, memory_region_name(mr)); +if (result) { + *result =MEMTX_DECODE_ERROR ;
Ditto.
system/memory.c bool memory_region_access_valid(MemoryRegion *mr, · line 1451
Quoted 10 lines
system/memory.c +3
-0+ }return false; }@@ -1436,6 +1447,9 @@ bool memory_region_access_valid(MemoryRegion *mr, addr, size,memory_region_name (mr), mr->ops->valid.min_access_size, mr->ops->valid.max_access_size); +if (result) { + *result =MEMTX_DECODE_ERROR ;
Ditto.
system/memory.c MemTxResult memory_region_dispatch_read(MemoryRegion *mr, · line 1495
Quoted 10 lines
system/memory.c +2
-1+ }return false; }return true;@@ -1478,7 +1492,7 @@ MemTxResult memory_region_dispatch_read(MemoryRegion *mr, mr->alias_offset + addr, pval, op, attrs); } -if (!memory_region_access_valid (mr, addr, size, false, attrs)) { +if (!memory_region_access_valid (mr, addr, size, false, attrs,NULL )) {
Surely here you want to propagate the dispatched MR result.
system/memory.c MemTxResult memory_region_dispatch_write(MemoryRegion *mr, · line 1546
Quoted 11 lines
system/memory.c +1
-1*pval =unassigned_mem_read (mr, addr, size);return MEMTX_DECODE_ERROR ; }@@ -1527,7 +1541,7 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr, mr->alias_offset + addr, data, op, attrs); } -if (!memory_region_access_valid (mr, addr, size, true, attrs)) { +if (!memory_region_access_valid (mr, addr, size, true, attrs,NULL )) {unassigned_mem_write (mr, addr, data, size);return MEMTX_DECODE_ERROR ;
Also here.
Quoted 15 lines
system/memory.c +0
-0}system/physmem.c +2
-1@@ -3635,7 +3635,8 @@ static bool flatview_access_valid(FlatView *fv, hwaddr addr, hwaddr len, mr =flatview_translate (fv, addr, &xlat, &l, is_write, attrs);if (!memory_access_is_direct (mr, is_write, attrs)) { l =memory_access_size (mr, l, addr); -if (!memory_region_access_valid (mr, xlat, l, is_write, attrs)) { +if (!memory_region_access_valid (mr, xlat, l, is_write, attrs, +NULL )) {return false; } }
PATCH 2/2 memory.c: read memory_region_access_valid() err reason in memory_region_dispatch_*
In reply to Daniel Henrique Barbozamemory_region_dispatch_read|write, when dealing with an invalid memory region, default to MEMTX_DECODE_ERROR error code. This happens because memory_region_access_valid() until recently would just report true/false and nothing else.
We now have access to the internal reason why memory_region_access_valid() would fail. In particular, we can now receive a MEMTX_ACCESS_ERROR when the mr->ops->valid.accepts() callback fails.
Make both functions return the actual error reason in case memory_region_access_valid fails, allowing more precise error handling throughout the code.
--- system/memory.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)
system/memory.c +5 -4
@@ -1492,9 +1492,9 @@ MemTxResult memory_region_dispatch_read(MemoryRegion *mr, mr->alias_offset + addr, pval, op, attrs); } -if (!memory_region_access_valid (mr, addr, size, false, attrs,NULL )) { +if (!memory_region_access_valid (mr, addr, size, false, attrs, &r)) { *pval =unassigned_mem_read (mr, addr, size); -return MEMTX_DECODE_ERROR ; +return r; } r =memory_region_dispatch_read1 (mr, addr, pval, size, attrs);
@@ -1535,15 +1535,16 @@ MemTxResult memory_region_dispatch_write(MemoryRegion *mr, MemTxAttrs attrs) {unsigned size =memop_size (op); +MemTxResult r;if (mr->alias) {return memory_region_dispatch_write (mr->alias, mr->alias_offset + addr, data, op, attrs); } -if (!memory_region_access_valid (mr, addr, size, true, attrs,NULL )) { +if (!memory_region_access_valid (mr, addr, size, true, attrs, &r)) {unassigned_mem_write (mr, addr, data, size); -return MEMTX_DECODE_ERROR ; +return r; }adjust_endianness (mr, &data, op);
Signature · 1 line
2.43.0
Re: [PATCH 2/2] memory.c: read memory_region_access_valid() err reason in memory_region_dispatch_*
Quoted 23 lines · Daniel Henrique Barboza
memory_region_dispatch_read|write, when dealing with an invalid memory region, default to
MEMTX_DECODE_ERRORerror code. This happens becausememory_region_access_valid()until recently would just report true/false and nothing else.We now have access to the internal reason why
memory_region_access_valid()would fail. In particular, we can now receive aMEMTX_ACCESS_ERRORwhen themr->ops->valid.accepts()callback fails.Make both functions return the actual error reason in case
memory_region_access_validfails, allowing more precise error handling throughout the code.Signed-off-by Daniel Henrique Barboza <[email protected]>--- system/memory.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-)system/memory.c +0
-0
system/memory.c MemTxResult memory_region_dispatch_read(MemoryRegion *mr, · line 1495
Quoted 5 lines
system/memory.c +1
-1@@ -1492,9 +1492,9 @@ MemTxResult memory_region_dispatch_read(MemoryRegion *mr, mr->alias_offset + addr, pval, op, attrs); } -if (!memory_region_access_valid (mr, addr, size, false, attrs,NULL )) { +if (!memory_region_access_valid (mr, addr, size, false, attrs, &r)) {
Now I see you propagate MemTxResult (my comment from previous patch).
Maybe clearer is 1st patch add the MemTxResult argument keeping current behavior and 2nd patch update to your current patch #1 change?
Quoted 4 lines
system/memory.c +1
-1*pval =unassigned_mem_read (mr, addr, size); -return MEMTX_DECODE_ERROR ; +return r; }
Quoted 15 lines · Daniel Henrique Barboza
Hello,
I decided to propose this change after working in a RISC-V gitlab bug [1]. In my proposed fix [2] I had to make assumptions about address_space_ld* error return, which is always
MEMTX_DECODE_ERROR, to identify that in that particular scenario this is a permission error. In RISC-V (and from what I can see other archs like x86) anACCESS_ERRORdue to mem page permission issues will fire a different fault than other errors like page not found.The core logic is on
memory_region_access_valid(), which already makes a distinction between error reasons when we look at theqemu_log_mask()errors, e.g. this is being characterized as a 'rejected':
Quoted 6 lines
if (
mr->ops->valid.accepts&& !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) { qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX ", size %u, region '%s', reason: rejected\n", is_write ? "write" : "read", addr, size, memory_region_name(mr));
valid.accepts returning false isn't a permissions error, though. It's for cases like "you tried to do a byte access to this device that requires 32-bit accesses only". Permissions errors are entirely in the MMU, and don't appear in the MemTx APIs, which are about transactions in the fabric outside the MMU. The CPU emulation catches permissions errors early before it even does the address_space_ld* call.
What is the equivalent of this in the underlying hardware? The current error codes are intended to roughly match the AXI bus SLVERR and DECERR error responses (which are "we were able to pass this memory transaction to a device, but it rejected it" and "we weren't able to find the device for this memory transaction at all").
The valid.accepts callback failing is always "we know the memory region but it doesn't like this", which is true for all memory_region_access_valid() failures, so maybe the problem here is that we return DECODE_ERROR for that case when we should be returning ACCESS_ERROR ?
thanks
-- PMM
Quoted 39 lines · Peter Maydell
Quoted 15 lines · Daniel Henrique Barboza
Hello,
I decided to propose this change after working in a RISC-V gitlab bug [1]. In my proposed fix [2] I had to make assumptions about address_space_ld* error return, which is always
MEMTX_DECODE_ERROR, to identify that in that particular scenario this is a permission error. In RISC-V (and from what I can see other archs like x86) anACCESS_ERRORdue to mem page permission issues will fire a different fault than other errors like page not found.The core logic is on
memory_region_access_valid(), which already makes a distinction between error reasons when we look at theqemu_log_mask()errors, e.g. this is being characterized as a 'rejected':Quoted 6 lines
if (
mr->ops->valid.accepts&& !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) { qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX ", size %u, region '%s', reason: rejected\n", is_write ? "write" : "read", addr, size, memory_region_name(mr));valid.accepts returning false isn't a permissions error, though. It's for cases like "you tried to do a byte access to this device that requires 32-bit accesses only". Permissions errors are entirely in the MMU, and don't appear in the MemTx APIs, which are about transactions in the fabric outside the MMU. The CPU emulation catches permissions errors early before it even does the address_space_ld* call.
What is the equivalent of this in the underlying hardware? The current error codes are intended to roughly match the AXI bus SLVERR and DECERR error responses (which are "we were able to pass this memory transaction to a device, but it rejected it" and "we weren't able to find the device for this memory transaction at all").
Quoted 5 lines
The valid.accepts callback failing is always "we know the memory region but it doesn't like this", which is true for all
memory_region_access_valid()failures, so maybe the problem here is that we returnDECODE_ERRORfor that case when we should be returningACCESS_ERROR?
This is the end result I'm going for. In patch 1 I changed memory_region_access_valid() to return ACCESS_ERROR when the valid.accepts fails, keeping all the
other exit code as DECODE_ERROR. The ACCESS_ERROR will then be propagated as an error in target/riscv/cpu_helper.c, get_physical_address():
if (riscv_cpu_mxl(env) == MXL_RV32) {
pte = be ? address_space_ldl_be(cs->as, pte_addr, attrs, &res)
: address_space_ldl_le(cs->as, pte_addr, attrs, &res);
} else {
pte = be ? address_space_ldq_be(cs->as, pte_addr, attrs, &res)
: address_space_ldq_le(cs->as, pte_addr, attrs, &res);
}
if (res != MEMTX_OK) {
return TRANSLATE_FAIL;
}In this particular case 'res' will fail as ACCESS_ERROR if the pte does not have the right permissions and we can throw the right exception.
Now, considering what you said:
"Permissions errors are entirely in the MMU, and don't appear in the MemTx APIs, which are about transactions in the fabric outside the MMU. The CPU emulation catches permissions errors early before it even does the address_space_ld* call."
I can say that this isn't happening in the RISC-V backend at least, so maybe the core issue here is that RISC-V is skipping steps in address translation/validation.
Thanks,
Daniel
Quoted 3 lines
thanks
-- PMM
Quoted 66 lines · Daniel Henrique Barboza
Quoted 39 lines · Peter Maydell
Quoted 15 lines · Daniel Henrique Barboza
Hello,
I decided to propose this change after working in a RISC-V gitlab bug [1]. In my proposed fix [2] I had to make assumptions about address_space_ld* error return, which is always
MEMTX_DECODE_ERROR, to identify that in that particular scenario this is a permission error. In RISC-V (and from what I can see other archs like x86) anACCESS_ERRORdue to mem page permission issues will fire a different fault than other errors like page not found.The core logic is on
memory_region_access_valid(), which already makes a distinction between error reasons when we look at theqemu_log_mask()errors, e.g. this is being characterized as a 'rejected':Quoted 6 lines
if (
mr->ops->valid.accepts&& !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) { qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX ", size %u, region '%s', reason: rejected\n", is_write ? "write" : "read", addr, size, memory_region_name(mr));valid.accepts returning false isn't a permissions error, though. It's for cases like "you tried to do a byte access to this device that requires 32-bit accesses only". Permissions errors are entirely in the MMU, and don't appear in the MemTx APIs, which are about transactions in the fabric outside the MMU. The CPU emulation catches permissions errors early before it even does the address_space_ld* call.
What is the equivalent of this in the underlying hardware? The current error codes are intended to roughly match the AXI bus SLVERR and DECERR error responses (which are "we were able to pass this memory transaction to a device, but it rejected it" and "we weren't able to find the device for this memory transaction at all").
Quoted 5 lines
The valid.accepts callback failing is always "we know the memory region but it doesn't like this", which is true for all
memory_region_access_valid()failures, so maybe the problem here is that we returnDECODE_ERRORfor that case when we should be returningACCESS_ERROR?This is the end result I'm going for. In patch 1 I changed
memory_region_access_valid()to returnACCESS_ERRORwhen the valid.accepts fails, keeping all the
other exit code asDECODE_ERROR. TheACCESS_ERRORwill then be propagated as an error intarget/riscv/cpu_helper.c,get_physical_address():if (riscv_cpu_mxl(env) == MXL_RV32) { pte = be ? address_space_ldl_be(cs->as, pte_addr, attrs, &res) : address_space_ldl_le(cs->as, pte_addr, attrs, &res); } else { pte = be ? address_space_ldq_be(cs->as, pte_addr, attrs, &res) : address_space_ldq_le(cs->as, pte_addr, attrs, &res); } if (res != MEMTX_OK) { return TRANSLATE_FAIL; }
Quoted 2 lines
In this particular case 'res' will fail as
ACCESS_ERRORif the pte does not have the right permissions and we can throw the right exception.
But what's the "right exception" here? This is, I think, the code for "the MMU is loading the page table entry from external memory". I'm not clear what this has to do with permissions. Generally "permissions" is a concept that applies to virtual addresses, i.e. "does the page table for this vaddr say we can access it?", and there's no concept of permissions for physical addresses. The exception to that is that you might have something like Arm's "granule protection table" which does apply permissions to physical accesses, so an in-guest-memory data structure defines what physical addresses can be accessed in particular ways. In QEMU we model this as effectively another level of page tables, so again the check happens before we do the address_space_ld* call. For risc-v a quick look at the architecture spec suggests that the Smepmp extension is a bit like this (but handled via registers rather than guest-memory data structures).
The other kind of check that might need to be done in the MMU before the load call is "is this physaddr out of range?" (e.g. CPU has 48 bit physaddrs and the L1 PTE specifies a physaddr for the L2 PTE that is outside the 48 bit range).
Do you have a reference in the architecture spec for the check that you're trying to implement here ?
I would be a bit surprised if the riscv architecture mandated different MMU errors for "page table entry load failed because the physaddr is not pointing at any device" vs "page table entry load failed because the physaddr points at a device and the device rejected it", because it's really up to the bus specification how clearly it distinguishes these various kinds of external abort (or even whether it distinguishes them synchronously vs asynchronously, given the existence of caches), and usually architecture specifications try not to impose too much on the implementation.
thanks
-- PMM
Quoted 86 lines · Peter Maydell
Quoted 66 lines · Daniel Henrique Barboza
Quoted 39 lines · Peter Maydell
Quoted 15 lines · Daniel Henrique Barboza
Hello,
I decided to propose this change after working in a RISC-V gitlab bug [1]. In my proposed fix [2] I had to make assumptions about address_space_ld* error return, which is always
MEMTX_DECODE_ERROR, to identify that in that particular scenario this is a permission error. In RISC-V (and from what I can see other archs like x86) anACCESS_ERRORdue to mem page permission issues will fire a different fault than other errors like page not found.The core logic is on
memory_region_access_valid(), which already makes a distinction between error reasons when we look at theqemu_log_mask()errors, e.g. this is being characterized as a 'rejected':Quoted 6 lines
if (
mr->ops->valid.accepts&& !mr->ops->valid.accepts(mr->opaque, addr, size, is_write, attrs)) { qemu_log_mask(LOG_INVALID_MEM, "Invalid %s at addr 0x%" HWADDR_PRIX ", size %u, region '%s', reason: rejected\n", is_write ? "write" : "read", addr, size, memory_region_name(mr));valid.accepts returning false isn't a permissions error, though. It's for cases like "you tried to do a byte access to this device that requires 32-bit accesses only". Permissions errors are entirely in the MMU, and don't appear in the MemTx APIs, which are about transactions in the fabric outside the MMU. The CPU emulation catches permissions errors early before it even does the address_space_ld* call.
What is the equivalent of this in the underlying hardware? The current error codes are intended to roughly match the AXI bus SLVERR and DECERR error responses (which are "we were able to pass this memory transaction to a device, but it rejected it" and "we weren't able to find the device for this memory transaction at all").
Quoted 5 lines
The valid.accepts callback failing is always "we know the memory region but it doesn't like this", which is true for all
memory_region_access_valid()failures, so maybe the problem here is that we returnDECODE_ERRORfor that case when we should be returningACCESS_ERROR?This is the end result I'm going for. In patch 1 I changed
memory_region_access_valid()to returnACCESS_ERRORwhen the valid.accepts fails, keeping all the
other exit code asDECODE_ERROR. TheACCESS_ERRORwill then be propagated as an error intarget/riscv/cpu_helper.c,get_physical_address():if (riscv_cpu_mxl(env) == MXL_RV32) { pte = be ? address_space_ldl_be(cs->as, pte_addr, attrs, &res) : address_space_ldl_le(cs->as, pte_addr, attrs, &res); } else { pte = be ? address_space_ldq_be(cs->as, pte_addr, attrs, &res) : address_space_ldq_le(cs->as, pte_addr, attrs, &res); } if (res != MEMTX_OK) { return TRANSLATE_FAIL; }Quoted 2 lines
In this particular case 'res' will fail as
ACCESS_ERRORif the pte does not have the right permissions and we can throw the right exception.But what's the "right exception" here? This is, I think, the code for "the MMU is loading the page table entry from external memory". I'm not clear what this has to do with permissions. Generally "permissions" is a concept that applies to virtual addresses, i.e. "does the page table for this vaddr say we can access it?", and there's no concept of permissions for physical addresses. The exception to that is that you might have something like Arm's "granule protection table" which does apply permissions to physical accesses, so an in-guest-memory data structure defines what physical addresses can be accessed in particular ways. In QEMU we model this as effectively another level of page tables, so again the check happens before we do the address_space_ld* call. For risc-v a quick look at the architecture spec suggests that the Smepmp extension is a bit like this (but handled via registers rather than guest-memory data structures).
Quoted 7 lines
The other kind of check that might need to be done in the MMU before the load call is "is this physaddr out of range?" (e.g. CPU has 48 bit physaddrs and the L1 PTE specifies a physaddr for the L2 PTE that is outside the 48 bit range).
Do you have a reference in the architecture spec for the check that you're trying to implement here ?
Sure. It's on the RISC-V Privileged ISA, chapter 12, section 12.3.2 "Virtual Address Translation Process":
https://docs.riscv.org/reference/isa/_attachments/riscv-privileged.pdf
9. If pte.a=0, or if the original memory access is a store and pte.d=0:
- If the Svade extension is implemented, stop and raise a page-fault exception corresponding to the original access type
- If a store to the PTE at address a+va.vpn[i]×PTESIZE would violate a PMA or PMP check, raise an access-fault exception corresponding to the original access type.
At this moment we're throwing a page-fault exception for both cases. We want the second case to throw an access-fault exception. PMA stands for "Physical Memory Access", i.e. the read/write/execute bits of the PTE. PMP is an optional physical-memory protection scheme. Both are related to a PTE permission to be read/write/executed, regardless of other failures like address misaligned/wrong operation size.
Thanks,
Daniel
Quoted 13 lines
I would be a bit surprised if the riscv architecture mandated different MMU errors for "page table entry load failed because the physaddr is not pointing at any device" vs "page table entry load failed because the physaddr points at a device and the device rejected it", because it's really up to the bus specification how clearly it distinguishes these various kinds of external abort (or even whether it distinguishes them synchronously vs asynchronously, given the existence of caches), and usually architecture specifications try not to impose too much on the implementation.
thanks
-- PMM
Quoted 18 lines · Daniel Henrique Barboza
Quoted 4 lines · Peter Maydell
On Mon, 8 Jun 2026 at 10:47, Daniel Henrique Barboza <[email protected]> wrote:
Do you have a reference in the architecture spec for the check that you're trying to implement here ?Sure. It's on the RISC-V Privileged ISA, chapter 12, section 12.3.2 "Virtual Address Translation Process":
https://docs.riscv.org/reference/isa/_attachments/riscv-privileged.pdf
9. If pte.a=0, or if the original memory access is a store and pte.d=0:
- If the Svade extension is implemented, stop and raise a page-fault exception corresponding to the original access type
- If a store to the PTE at address a+va.vpn[i]×PTESIZE would violate a PMA or PMP check, raise an access-fault exception corresponding to the original access type.
Quoted 6 lines
At this moment we're throwing a page-fault exception for both cases. We want the second case to throw an access-fault exception. PMA stands for "Physical Memory Access", i.e. the read/write/execute bits of the PTE. PMP is an optional physical-memory protection scheme. Both are related to a PTE permission to be read/write/executed, regardless of other failures like address misaligned/wrong operation size.
Are you sure that's what a PMA check is ? Section 3.6 seems to me to be describing something else, where the hardware uses some combination of hardwired info and unspecified platform-specific registers to determine whether the physical address is to somewhere that supports particular access widths, atomic insns, cacheability, and ability to read, write or exec.
I can't see where in the spec it defines what's supposed to happen if the page-table-entry read fails because it's to an invalid physical address, so presumably that's just a subtype of "failed a PMA check". (3.6.1 has a note that supports that reading, where it says that previous spec versions had a "vacant region" concept, presumably corresponding to our "decode error", but that these are now treated as I/O regions that have none of r/w/x access permissions.)
In that case the riscv page table walk code should:
- fail with page-fault exception for "pte.a field is zero" (and do not attempt a load from physaddr 0)
- otherwise, do the load, and report any kind of failure of the load as an access-fault exception
Do you implement emulation of these platform-specific PMA registers? If so, those sound to me like they should be some extra level of checks that your page-table-walk code does before it does the real physical-address load, similar to how you do the PMP checks. It doesn't sound to me quite the same as the return value from QEMU's address_space_ld* functions, which is more the case of "the guest misconfigured the PMA registers and did an access that passed the PMA checks but which the actual device rejected".
thanks
-- PMM
Hi Peter,
Quoted 34 lines · Peter Maydell
Quoted 18 lines · Daniel Henrique Barboza
Quoted 4 lines · Peter Maydell
On Mon, 8 Jun 2026 at 10:47, Daniel Henrique Barboza <[email protected]> wrote:
Do you have a reference in the architecture spec for the check that you're trying to implement here ?Sure. It's on the RISC-V Privileged ISA, chapter 12, section 12.3.2 "Virtual Address Translation Process":
https://docs.riscv.org/reference/isa/_attachments/riscv-privileged.pdf
9. If pte.a=0, or if the original memory access is a store and pte.d=0:
- If the Svade extension is implemented, stop and raise a page-fault exception corresponding to the original access type
- If a store to the PTE at address a+va.vpn[i]×PTESIZE would violate a PMA or PMP check, raise an access-fault exception corresponding to the original access type.Quoted 6 lines
At this moment we're throwing a page-fault exception for both cases. We want the second case to throw an access-fault exception. PMA stands for "Physical Memory Access", i.e. the read/write/execute bits of the PTE. PMP is an optional physical-memory protection scheme. Both are related to a PTE permission to be read/write/executed, regardless of other failures like address misaligned/wrong operation size.
Are you sure that's what a PMA check is ? Section 3.6 seems to me to be describing something else, where the hardware uses some combination of hardwired info and unspecified platform-specific registers to determine whether the physical address is to somewhere that supports particular access widths, atomic insns, cacheability, and ability to read, write or exec.
Quoted 7 lines
I can't see where in the spec it defines what's supposed to happen if the page-table-entry read fails because it's to an invalid physical address, so presumably that's just a subtype of "failed a PMA check". (3.6.1 has a note that supports that reading, where it says that previous spec versions had a "vacant region" concept, presumably corresponding to our "decode error", but that these are now treated as I/O regions that have none of r/w/x access permissions.)
Sorry taking this long to answer. Yeah, "PMA check" as far as the RISC-V seems to understand is a combination of a lot of things that includes not just read/write/exec permissions but also alignment, atomicity, caching and so on.
All memory accesses failures we're able to detect in the code, aside from the usual "address not found" error that we can interpret safely as an page fault (... I think ...), can be considered a PMA error. Which is something that we're *not* doing at this moment, and this might be an alternative for what I'm proposing here.
It might come to what you said below: an extra layer of checks that we would call "PMA". I'll have to think more about it ...
Cheers,
Daniel
Quoted 18 lines
In that case the riscv page table walk code should:
- fail with page-fault exception for "pte.a field is zero" (and do not attempt a load from physaddr 0)
- otherwise, do the load, and report any kind of failure of the load as an access-fault exceptionDo you implement emulation of these platform-specific PMA registers? If so, those sound to me like they should be some extra level of checks that your page-table-walk code does before it does the real physical-address load, similar to how you do the PMP checks. It doesn't sound to me quite the same as the return value from QEMU's address_space_ld* functions, which is more the case of "the guest misconfigured the PMA registers and did an access that passed the PMA checks but which the actual device rejected".
thanks
-- PMM