virtual interrupt controller (vAPLIC/vIMSIC) support
Re: [PATCH v1 15/17] xen/riscv: implement trap redirection to a guest
In reply to Oleksii KurochkoQuoted 5 lines · Oleksii Kurochko
Some traps taken by Xen on behalf of a guest can't or shouldn't be handled by the hypervisor and must be forwarded to the guest's own S-mode exception handler instead: e.g. when
riscv_vcpu_unpriv_read()faults while accessing guest memory, or when emulation hits a condition only the guest kernel can resolve.
Is the plan to use riscv_vcpu_unpriv_read() also for reading hypercall buffers? In that case trap redirection shouldn't come into play.
Quoted 7 lines
Introduce
riscv_vcpu_trap_redirect()for that purpose. It makes the trap appear to the guest as if it had been taken directly in VS-mode:
the trap information is transferred to the guest's virtual supervisor CSRs and the vCPU is resumed at its exception vector in supervisor mode, following the trap entry rules of the RISC-V privileged specification.
Quoted 13 lines
The implementation is based on
kvm_riscv_vcpu_trap_redirect()from Linux, with a few deviations:
- The function reads and writes physical VS-mode CSRs, so it is only meaningful for the currently running vCPU. Instead of taking a struct vcpu argument, it always operates on current.
- The MODE field of vstvec is masked off explicitly when computing the exception target PC (exceptions always vector to BASE), rather than relying on the hardwired zero bit of sepc to drop it on VM entry.
- Assertions document the preconditions: the trap must have been taken from virtualized mode (hstatus.SPV set), and only synchronous exceptions may be redirected - interrupts must be injected via hvip instead, so that the hardware performs VS-mode trap entry itself, respecting vsstatus.SIE and vectored vstvec dispatch.
For this last bullet point - how is a reviewer supposed to validate the assertions added when no caller of the new function exists?
Quoted 5 lines
Signed-off-by Oleksii Kurochko <[email protected]>--- xen/arch/riscv/guestcopy.c | 54 +++++++++++++++++++++++ xen/arch/riscv/include/asm/guest_access.h | 2 + 2 files changed, 56 insertions(+)
I don't understand this placement - trap redirection has nothing (directly) to do with accessing guest memory.
Quoted 46 lines
xen/arch/riscv/guestcopy.c +40
-0@@ -205,3 +205,57 @@ unsigned long riscv_vcpu_unpriv_read(bool read_insn,return val; } + +/* Redirect trap to Guest. */ +void riscv_vcpu_trap_redirect (const struct trap_info *trap) +{ +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +unsigned long vsstatus =csr_read (CSR_VSSTATUS ); + +/* +* Redirecting a trap makes sense only if the trap was taken from +* virtualized mode, i.e. sret is going to return to VS-mode. +*/ +ASSERT (regs->hstatus &HSTATUS_SPV ); + +/* +* Only synchronous exceptions can be redirected. Interrupts must be +* injected via hvip instead, so that the hardware itself performs +* VS-mode trap entry, respecting vsstatus.SIE and the vectored +* dispatch (BASE + 4 * cause) if vstvec is configured so. +*/ +ASSERT (!(trap->scause &CAUSE_IRQ_FLAG )); + +/* Change Guest SSTATUS.SPP bit */ + vsstatus &= ~SSTATUS_SPP ; +if ( regs->sstatus &SSTATUS_SPP ) + vsstatus |=SSTATUS_SPP ; + +/* Change Guest SSTATUS.SPIE bit */ + vsstatus &= ~SSTATUS_SPIE ; +if ( vsstatus &SSTATUS_SIE ) + vsstatus |=SSTATUS_SPIE ; + +/* Clear Guest SSTATUS.SIE bit */ + vsstatus &= ~SSTATUS_SIE ; + +/* Update Guest SSTATUS */ +csr_write (CSR_VSSTATUS , vsstatus); + +/* Update Guest SCAUSE, STVAL, and SEPC */ +csr_write (CSR_VSCAUSE , trap->scause); +csr_write (CSR_VSTVAL , trap->stval);
xen/arch/riscv/guestcopy.c unsigned long riscv_vcpu_unpriv_read(bool read_insn, · line 257
Quoted 10 lines
xen/arch/riscv/guestcopy.c +10
-0+csr_write (CSR_VSEPC , trap->sepc); + +/* +* Set Guest PC to Guest exception vector. +* +* vstvec[1:0] is the vector MODE, not part of the address. Exceptions +* always target BASE regardless of MODE, so mask it off explicitly +* instead of relying on the hardwired zero bit of sepc to drop it. +*/ + regs->sepc =csr_read (CSR_VSTVEC ) & ~0x3UL ;
Can there be a proper constant please for this mask?
Jan
Re: [PATCH v1 15/17] xen/riscv: implement trap redirection to a guest
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko
Some traps taken by Xen on behalf of a guest can't or shouldn't be handled by the hypervisor and must be forwarded to the guest's own S-mode exception handler instead: e.g. when
riscv_vcpu_unpriv_read()faults while accessing guest memory, or when emulation hits a condition only the guest kernel can resolve.Is the plan to use
riscv_vcpu_unpriv_read()also for reading hypercall buffers?
Yes, it could also be used to read hypercall buffers, but I don't think it's the best option, as hypercall buffers could be larger than 8 bytes (which is the size supported by the hlv instruction on the RV64 platform). For that case, I think it would be better to map the Xen page corresponding to the GVA of the hypercall buffer and then use the usual memcpy(). So, basically, use copy_guest() on RISC-V for that purpose.
Quoted 1 line
In that case trap redirection shouldn't come into play.
It isn't mandatory to perform a redirection in the case of riscv_vcpu_unpriv_read(), so if trap redirection shouldn't happen for hypercall buffers, then the caller of riscv_vcpu_unpriv_read() needs to handle that properly by checking utrap.cause. Something like:
```
*insn = riscv_vcpu_unpriv_read(true, regs->sepc, &utrap);
if ( utrap.scause )
{
...
utrap.sepc = regs->sepc;
utrap.stval = utrap.sepc;
riscv_vcpu_trap_redirect(&utrap);
return true;
}
```So, if this cannot happen in the case of a hypercall buffer, then we need to return -EFAULT in the if ( utrap.scause ) case.
I don't think I understand why redirection shouldn't come into play. Do you mean that the hypercall buffer will always be available, and that it is impossible for the hlv instruction to fail, so there is no point in handling redirection at all in this case?
Quoted 22 lines
Quoted 8 lines
Introduce
riscv_vcpu_trap_redirect()for that purpose. It makes the trap appear to the guest as if it had been taken directly in VS-mode:
the trap information is transferred to the guest's virtual supervisor CSRs and the vCPU is resumed at its exception vector in supervisor mode, following the trap entry rules of the RISC-V privileged specification.Quoted 13 lines
The implementation is based on
kvm_riscv_vcpu_trap_redirect()from Linux, with a few deviations:
- The function reads and writes physical VS-mode CSRs, so it is only meaningful for the currently running vCPU. Instead of taking a struct vcpu argument, it always operates on current.
- The MODE field of vstvec is masked off explicitly when computing the exception target PC (exceptions always vector to BASE), rather than relying on the hardwired zero bit of sepc to drop it on VM entry.
- Assertions document the preconditions: the trap must have been taken from virtualized mode (hstatus.SPV set), and only synchronous exceptions may be redirected - interrupts must be injected via hvip instead, so that the hardware performs VS-mode trap entry itself, respecting vsstatus.SIE and vectored vstvec dispatch.
Quoted 2 lines
For this last bullet point - how is a reviewer supposed to validate the assertions added when no caller of the new function exists?
My bad (again). The caller appears in "[PATCH v1 16/17] xen/riscv: add guest load emulation for trapped MMIO accesses" (as you already know) so I have to re-order patches and put this patch after PATCH v1 16/17.
Quoted 9 lines
Quoted 6 lines
Signed-off-by Oleksii Kurochko <[email protected]>--- xen/arch/riscv/guestcopy.c | 54 +++++++++++++++++++++++ xen/arch/riscv/include/asm/guest_access.h | 2 + 2 files changed, 56 insertions(+)I don't understand this placement - trap redirection has nothing (directly) to do with accessing guest memory.
Agree, at some point. I put trap redirection there as the idea was to catch trap from hlv{x} instructions and redirect some of them to guest so I put it to guestcopy.h.
I will put inside traps.{c,h} instead.
Quoted 51 lines
Quoted 51 lines
xen/arch/riscv/guestcopy.c +44
-0@@ -205,3 +205,57 @@ unsigned long riscv_vcpu_unpriv_read(bool read_insn,return val; } + +/* Redirect trap to Guest. */ +void riscv_vcpu_trap_redirect (const struct trap_info *trap) +{ +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +unsigned long vsstatus =csr_read (CSR_VSSTATUS ); + +/* +* Redirecting a trap makes sense only if the trap was taken from +* virtualized mode, i.e. sret is going to return to VS-mode. +*/ +ASSERT (regs->hstatus &HSTATUS_SPV ); + +/* +* Only synchronous exceptions can be redirected. Interrupts must be +* injected via hvip instead, so that the hardware itself performs +* VS-mode trap entry, respecting vsstatus.SIE and the vectored +* dispatch (BASE + 4 * cause) if vstvec is configured so. +*/ +ASSERT (!(trap->scause &CAUSE_IRQ_FLAG )); + +/* Change Guest SSTATUS.SPP bit */ + vsstatus &= ~SSTATUS_SPP ; +if ( regs->sstatus &SSTATUS_SPP ) + vsstatus |=SSTATUS_SPP ; + +/* Change Guest SSTATUS.SPIE bit */ + vsstatus &= ~SSTATUS_SPIE ; +if ( vsstatus &SSTATUS_SIE ) + vsstatus |=SSTATUS_SPIE ; + +/* Clear Guest SSTATUS.SIE bit */ + vsstatus &= ~SSTATUS_SIE ; + +/* Update Guest SSTATUS */ +csr_write (CSR_VSSTATUS , vsstatus); + +/* Update Guest SCAUSE, STVAL, and SEPC */ +csr_write (CSR_VSCAUSE , trap->scause); +csr_write (CSR_VSTVAL , trap->stval); +csr_write (CSR_VSEPC , trap->sepc); + +/* +* Set Guest PC to Guest exception vector.
Quoted 8 lines
xen/arch/riscv/guestcopy.c unsigned long riscv_vcpu_unpriv_read(bool read_insn, · line 257
Quoted 6 lines
xen/arch/riscv/guestcopy.c +6
-0+* +* vstvec[1:0] is the vector MODE, not part of the address. Exceptions +* always target BASE regardless of MODE, so mask it off explicitly +* instead of relying on the hardwired zero bit of sepc to drop it. +*/ + regs->sepc =csr_read (CSR_VSTVEC ) & ~0x3UL ;Can there be a proper constant please for this mask?
Sure, I will introduce one.
Thanks.
~ Oleksii
Re: [PATCH v1 15/17] xen/riscv: implement trap redirection to a guest
Quoted 40 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich
Quoted 5 lines · Oleksii Kurochko
Some traps taken by Xen on behalf of a guest can't or shouldn't be handled by the hypervisor and must be forwarded to the guest's own S-mode exception handler instead: e.g. when
riscv_vcpu_unpriv_read()faults while accessing guest memory, or when emulation hits a condition only the guest kernel can resolve.Is the plan to use
riscv_vcpu_unpriv_read()also for reading hypercall buffers?Yes, it could also be used to read hypercall buffers, but I don't think it's the best option, as hypercall buffers could be larger than 8 bytes (which is the size supported by the
hlvinstruction on the RV64 platform). For that case, I think it would be better to map the Xen page corresponding to the GVA of the hypercall buffer and then use the usualmemcpy(). So, basically, usecopy_guest()on RISC-V for that purpose.Quoted 1 line
In that case trap redirection shouldn't come into play.
It isn't mandatory to perform a redirection in the case of
riscv_vcpu_unpriv_read(), so if trap redirection shouldn't happen for hypercall buffers, then the caller ofriscv_vcpu_unpriv_read()needs to handle that properly by checking utrap.cause. Something like:``` *insn = riscv_vcpu_unpriv_read(true, regs->sepc, &utrap); if ( utrap.scause ) { ... utrap.sepc = regs->sepc; utrap.stval = utrap.sepc; riscv_vcpu_trap_redirect(&utrap); return true; } ```
Quoted 7 lines
So, if this cannot happen in the case of a hypercall buffer, then we need to return
-EFAULTin the if ( utrap.scause ) case.I don't think I understand why redirection shouldn't come into play. Do you mean that the hypercall buffer will always be available, and that it is impossible for the hlv instruction to fail, so there is no point in handling redirection at all in this case?
Failure to access a hypercall buffer should result in a -EFAULT return value, not in any kind of exception.
Jan
Quoted 82 lines · Oleksii Kurochko
Hi all,
This series adds the initial virtual interrupt controller (vINTC) support for RISC-V guests in Xen, based on the Advanced Interrupt Architecture (AIA): a virtual APLIC (vAPLIC) in MSI mode backed by a virtual IMSIC (vIMSIC) using hardware guest interrupt files.
Rather than emulating APLIC in direct-delivery mode (which requires trap-and-emulate for every interrupt and is costly), the series targets IMSIC from the start. AIA lets a hart implement several "guest interrupt files" (up to GEILEN), so external interrupts can be delivered to a vCPU directly by hardware via the VGEIN field of hstatus, without a hypervisor round-trip. Xen only has to emulate the APLIC MMIO programming interface and route the guest's intent onto the physical MSI topology; interrupt delivery itself stays in hardware.
The work breaks down into a few logical blocks:
Physical APLIC/AIA groundwork (patches 1-3)
- Correctly trackIRQ_DISABLEDacross runtime enable/disable and orderthe ->status update against the IMSIC CSR write.- Per-pCPU VGEIN (guest interrupt file) allocator: assign/release a hardware guest file to a vCPU and program hstatus.VGEIN.
- Add the missing APLIC register offsets/masks needed by both the physical and virtual APLIC code (no functional change).Device-agnostic MMIO dispatch + vAPLIC emulation (patches 4-5)
- A per-domain MMIO handler table modelled on Arm's framework, so emulated devices self-register their GPA ranges and the fault path stays agnostic via a singletry_handle_mmio()entry point.
- vAPLIC MMIO read/write emulation. Writes are gated by the domain's authorised-IRQ bitmap so a guest cannot touch interrupts it does not own, and TARGET writes are translated from virtual to physical hart/guest-file indices. Delegation (SOURCECFG.D) is not yet supported.vIMSIC guest interrupt files and state (patches 6-9)
- Stage-2 map a vCPU's physical guest interrupt file to the fixed per-vCPU GPA page the guest expects at offset 0.
-vcpu_aia_init(): assign a VGEIN, map its guest file and record the IMSIC state as a consistent unit.
- IMSIC state save/restore (currently tracking which pCPU owns the guest file, needed because the pCPU id is part of the MSI address).
-has_msi_support()helper to decide whether IMSIC state has to be saved/restored.vINTC state save/restore plumbing (patches 10-11)
- vintc_state_{save,restore}() wrappers over new store/restore hooks in structvintc_ops, and the vAPLIC implementation of those hooks. No callers are wired up yet; the context-switch calls arrive with vCPU context switch support.Trap and instruction emulation infrastructure (patches 12-17)
- Extend the exception-table format with type/data fields and addEX_TYPE_TRAP_INFOso fixups can capture sepc/scause/stval.
-riscv_vcpu_unpriv_read()(HLV/HLVX) to read guest memory/instructions safely, andriscv_vcpu_trap_redirect()to forward a synchronous trap back into the guest's VS-mode handler.
- A guest page-fault handler that decodes the trapped load/store instruction (HTINST, or an unprivileged fetch as a fallback) and dispatches the MMIO access throughtry_handle_mmio().Note on versioning: the series is posted as v1 as a freshly split-out series, but several patches carry v2/v3 changelogs because they were previously circulated as part of a larger another patches [1] from which the current depends.
CI tests: https://gitlab.com/xen-project/people/olkur/xen/-/pipelines/2690874319
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
Oleksii Kurochko (17):
xen/riscv: manageIRQ_DISABLEDflag in APLIC irq enable/disable
callbacks
xen/riscv: add basic VGEIN management for AIA guests xen/riscv: add missing APLIC register offsets, masks toasm/aplic.hxen/riscv: introduce device-agnostic MMIO emulation dispatch xen/riscv: implement virtual APLIC MMIO emulation xen/riscv: map IMSIC interrupt file for vCPUs
xen/riscv: introduce vCPU AIA initialization
xen/riscv: add IMSIC state save/restore
xen/riscv: add helper to check APLIC MSI mode
Quoted 8 lines
xen/riscv: introduce vintc_state_{save,restore}() xen/riscv: add vAPLIC state save/restore hooks xen/riscv: extend exception tables with type and data fields xen/riscv: add unprivileged guest memory read helper xen/riscv: add guest page fault handling stub xen/riscv: implement trap redirection to a guest xen/riscv: add guest load emulation for trapped MMIO accesses xen/riscv: add guest store emulation for trapped MMIO accesses
Where did the last two patches go? My inbox agrees with [1] that only 15 of the 17 patches arrived.
Jan
[1] https://lists.xen.org/archives/html/xen-devel/2026-07/threads.html
Quoted 92 lines · Jan Beulich
Quoted 82 lines · Oleksii Kurochko
Hi all,
This series adds the initial virtual interrupt controller (vINTC) support for RISC-V guests in Xen, based on the Advanced Interrupt Architecture (AIA): a virtual APLIC (vAPLIC) in MSI mode backed by a virtual IMSIC (vIMSIC) using hardware guest interrupt files.
Rather than emulating APLIC in direct-delivery mode (which requires trap-and-emulate for every interrupt and is costly), the series targets IMSIC from the start. AIA lets a hart implement several "guest interrupt files" (up to GEILEN), so external interrupts can be delivered to a vCPU directly by hardware via the VGEIN field of hstatus, without a hypervisor round-trip. Xen only has to emulate the APLIC MMIO programming interface and route the guest's intent onto the physical MSI topology; interrupt delivery itself stays in hardware.
The work breaks down into a few logical blocks:
Physical APLIC/AIA groundwork (patches 1-3)
- Correctly trackIRQ_DISABLEDacross runtime enable/disable and orderthe ->status update against the IMSIC CSR write.- Per-pCPU VGEIN (guest interrupt file) allocator: assign/release a hardware guest file to a vCPU and program hstatus.VGEIN.
- Add the missing APLIC register offsets/masks needed by both the physical and virtual APLIC code (no functional change).Device-agnostic MMIO dispatch + vAPLIC emulation (patches 4-5)
- A per-domain MMIO handler table modelled on Arm's framework, so emulated devices self-register their GPA ranges and the fault path stays agnostic via a singletry_handle_mmio()entry point.
- vAPLIC MMIO read/write emulation. Writes are gated by the domain's authorised-IRQ bitmap so a guest cannot touch interrupts it does not own, and TARGET writes are translated from virtual to physical hart/guest-file indices. Delegation (SOURCECFG.D) is not yet supported.vIMSIC guest interrupt files and state (patches 6-9)
- Stage-2 map a vCPU's physical guest interrupt file to the fixed per-vCPU GPA page the guest expects at offset 0.
-vcpu_aia_init(): assign a VGEIN, map its guest file and record the IMSIC state as a consistent unit.
- IMSIC state save/restore (currently tracking which pCPU owns the guest file, needed because the pCPU id is part of the MSI address).
-has_msi_support()helper to decide whether IMSIC state has to be saved/restored.vINTC state save/restore plumbing (patches 10-11)
- vintc_state_{save,restore}() wrappers over new store/restore hooks in structvintc_ops, and the vAPLIC implementation of those hooks. No callers are wired up yet; the context-switch calls arrive with vCPU context switch support.Trap and instruction emulation infrastructure (patches 12-17)
- Extend the exception-table format with type/data fields and addEX_TYPE_TRAP_INFOso fixups can capture sepc/scause/stval.
-riscv_vcpu_unpriv_read()(HLV/HLVX) to read guest memory/instructions safely, andriscv_vcpu_trap_redirect()to forward a synchronous trap back into the guest's VS-mode handler.
- A guest page-fault handler that decodes the trapped load/store instruction (HTINST, or an unprivileged fetch as a fallback) and dispatches the MMIO access throughtry_handle_mmio().Note on versioning: the series is posted as v1 as a freshly split-out series, but several patches carry v2/v3 changelogs because they were previously circulated as part of a larger another patches [1] from which the current depends.
CI tests: https://gitlab.com/xen-project/people/olkur/xen/-/pipelines/2690874319
[1] https://lore.kernel.org/xen-devel/[email protected]/T/#t
Oleksii Kurochko (17):
xen/riscv: manageIRQ_DISABLEDflag in APLIC irq enable/disable
callbacks
xen/riscv: add basic VGEIN management for AIA guests xen/riscv: add missing APLIC register offsets, masks toasm/aplic.hxen/riscv: introduce device-agnostic MMIO emulation dispatch xen/riscv: implement virtual APLIC MMIO emulation xen/riscv: map IMSIC interrupt file for vCPUs
xen/riscv: introduce vCPU AIA initialization
xen/riscv: add IMSIC state save/restore
xen/riscv: add helper to check APLIC MSI modeQuoted 8 lines
xen/riscv: introduce vintc_state_{save,restore}() xen/riscv: add vAPLIC state save/restore hooks xen/riscv: extend exception tables with type and data fields xen/riscv: add unprivileged guest memory read helper xen/riscv: add guest page fault handling stub xen/riscv: implement trap redirection to a guest xen/riscv: add guest load emulation for trapped MMIO accesses xen/riscv: add guest store emulation for trapped MMIO accesses
Quoted 3 lines
Where did the last two patches go? My inbox agrees with [1] that only 15 of the 17 patches arrived.
They lost for some reason, I sent them separately again. Now it should be okay.
Quoted 1 line
[1] https://lists.xen.org/archives/html/xen-devel/2026-07/threads.html
PATCH 16/17 xen/riscv: add guest load emulation for trapped MMIO accesses
In reply to Oleksii KurochkoIntroduce emulate_load() to decode and emulate guest load instructions that fault due to MMIO accesses. This provides the basic infrastructure required for MMIO emulation on RISC-V.
The instruction decode (decode_trapped_insn() and the mask/match chain for standard and compressed load encodings) is adapted from Linux's KVM RISC-V implementation. The completion path differs from KVM's, since Xen dispatches MMIO synchronously to an in-hypervisor handler via try_handle_mmio() and has no userspace exit/return step equivalent to KVM's kvm_io_bus_read() / KVM_EXIT_MMIO / kvm_riscv_vcpu_mmio_return() split.
A fault taken while re-reading the trapped instruction is handled depending on the faulting translation stage:
- A VS-stage fault is the guest's own fault (e.g. it modified its page tables from another vCPU) and, as in KVM, is redirected to the guest's trap vector, with the cause remapped to CAUSE_FETCH_PAGE_FAULT since HLVX reports execute-permission failures as load faults.
- A G-stage fault would mean the P2M mapping of the instruction page disappeared after the instruction was fetched. KVM must handle this by resuming the guest and retrying, as Linux MM can invalidate G-stage mappings at any time. Xen does not remove P2M mappings of a running domain at the moment, so this case is asserted unreachable with BUG_ON(); it will need to be revisited once such removal is implemented.
When a guest load triggers a page fault, the trapped instruction is decoded using HTINST or, if unavailable, fetched via unprivileged access. At the moment only virtual interrupt controller (vINTC) traps are expected to occur, since it is currently the only backend registered with the MMIO handler dispatch, so in practice the load is emulated via the vINTC backend and the guest register state is updated accordingly.
--- xen/arch/riscv/include/asm/traps.h | 6 ++ xen/arch/riscv/traps.c | 162 ++++++++++++++++++++++++++++- 2 files changed, 167 insertions(+), 1 deletion(-)
xen/arch/riscv/include/asm/traps.h +6 -0
@@ -4,6 +4,7 @@#define ASM__RISCV__TRAPS_H #include <asm/processor.h> +#include <asm/riscv_encoding.h> #ifndef __ASSEMBLER__
@@ -13,6 +14,11 @@ struct trap_info {register_t stval; }; +static inline bool is_load_guest_page_fault (unsigned long scause) +{ +return (scause ==CAUSE_LOAD_GUEST_PAGE_FAULT ); +} +void do_trap (struct cpu_user_regs *cpu_regs);void handle_trap (void );void trap_init (void );
xen/arch/riscv/traps.c +161 -1
183 lines not on this page · raw message
Signature · 1 line
2.54.0
Re: [PATCH v1 16/17] xen/riscv: add guest load emulation for trapped MMIO accesses
Quoted 12 lines · Oleksii Kurochko
Introduce
emulate_load()to decode and emulate guest load instructions that fault due to MMIO accesses. This provides the basic infrastructure required for MMIO emulation on RISC-V.The instruction decode (
decode_trapped_insn()and the mask/match chain for standard and compressed load encodings) is adapted from Linux's KVM RISC-V implementation. The completion path differs from KVM's, since Xen dispatches MMIO synchronously to an in-hypervisor handler viatry_handle_mmio()and has no userspace exit/return step equivalent to KVM'skvm_io_bus_read()/KVM_EXIT_MMIO/kvm_riscv_vcpu_mmio_return()split.
Quoted 13 lines
A fault taken while re-reading the trapped instruction is handled depending on the faulting translation stage:
- A VS-stage fault is the guest's own fault (e.g. it modified its page tables from another vCPU) and, as in KVM, is redirected to the guest's trap vector, with the cause remapped toCAUSE_FETCH_PAGE_FAULTsince HLVX reports execute-permission failures as load faults.
- A G-stage fault would mean the P2M mapping of the instruction page disappeared after the instruction was fetched. KVM must handle this by resuming the guest and retrying, as Linux MM can invalidate G-stage mappings at any time. Xen does not remove P2M mappings of a running domain at the moment, so this case is asserted unreachable withBUG_ON(); it will need to be revisited once such removal is implemented.
I don't see why this cannot be implemented correctly right away. The behavior should be that of an access to unpopulated space on bare hardware, whatever that behavior is on RISC-V.
xen/arch/riscv/include/asm/traps.h struct trap_info { · line 20
Quoted 8 lines
xen/arch/riscv/include/asm/traps.h +4
-0@@ -13,6 +14,11 @@ struct trap_info {register_t stval; }; +static inline bool is_load_guest_page_fault (unsigned long scause) +{ +return (scause ==CAUSE_LOAD_GUEST_PAGE_FAULT ); +}
Is something like this really a useful wrapper to have? It doesn't really shorten anything, nor does (imo) it aid readability.
xen/arch/riscv/traps.c static void timer_interrupt(void) · line 196
Quoted 5 lines
xen/arch/riscv/traps.c +1
-0@@ -191,6 +193,11 @@ static void timer_interrupt(void)raise_softirq (TIMER_SOFTIRQ ); } +static always_inline void advance_pc(struct cpu_user_regs *regs, int step)
See my earlier remark regarding always_inline. Also - why plain int? Are there (going to be) cases where PC is moved backwards (in which case "advance" isn't suitable naming)?
Quoted 19 lines
xen/arch/riscv/traps.c +12
-0+{ + regs->sepc += step; +} +static always_inline unsigned long get_faulting_gpa (void ) {/* @@ -210,9 +217,162 @@ static always_inline unsigned long get_faulting_gpa(void)return (csr_read (CSR_HTVAL ) <<2 ) | (csr_read (CSR_STVAL ) &0x3 ); } +/* +* Determine the trapped instruction which caused a guest MMIO trap. +* +* Returns true if the trap was redirected to the guest, in which case +* the caller must stop emulation and return success. Otherwise *insn +* and *insn_len are filled in and the caller should continue decoding. +*/ +static bool decode_trapped_insn(unsigned long htinst, unsigned long *insn,
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 237
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ unsigned int *insn_len) +{ +if ( htinst &0x1 ) + { +/* +* Bit[0] == 1 implies trapped instruction value is +* transformed instruction or custom instruction. +*/ + *insn = htinst |INSN_16BIT_MASK ; + *insn_len = (htinst &BIT (1 ,UL )) ?INSN_LEN (*insn) :2 ;
In the if() you don't use BIT(), while here you do. Please be consistent.
Why the use of INSN_LEN(), when due to the earlier assignment it'll always yield 4 here?
Finally, how would the caller know whether it looks at a transformed insn or (as fetched below) a "normal" one?
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 241
Quoted 4 lines
xen/arch/riscv/traps.c +4
-0+ } +else + { +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current);
Pointer-to-const.
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 242
Quoted 1 line
xen/arch/riscv/traps.c +1
-0+struct trap_info utrap = {0 };
Just {} please.
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 247
Quoted 4 lines
xen/arch/riscv/traps.c +4
-0+/* +* Bit[0] == 0 implies trapped instruction value is +* zero or special value. +*/
How come you get away without dealing with pseudoinsns? The insn pointed at by regs->sepc is of no interest for faults caused by implicit memory accesses originating from VS-stage address translation.
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void)
Quoted 7 lines
xen/arch/riscv/traps.c +7
-0+ *insn =riscv_vcpu_unpriv_read (true, regs->sepc, &utrap); +if ( utrap.scause ) + { +/* +* A G-stage fault here would mean the P2M mapping of the page +* containing the trapped instruction disappeared after it was +* fetched.
Does it? What about, again, faults from VS-stage address translation while hardware was trying to fetch an insn? That is ...
Review static always_inline unsigned long get_faulting_gpa(void) · line 255
Quoted 2 lines
(quoted diff) +1
-0Nothing removes P2M mappings of a running domain yet, + * so this cannot happen.
... the necessary P2M mapping may never have been there.
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 262
Quoted 6 lines
xen/arch/riscv/traps.c +6
-0+* TODO: Revisit once P2M mappings can be removed at runtime. +*/ +BUG_ON (is_load_guest_page_fault (utrap.scause)); + + utrap.sepc = regs->sepc; + utrap.stval = utrap.sepc;
How do you know the fault was at .sepc? A 32-bit insn crossing a page boundary (implying the C extension is available) may well fault only on its higher half.
Quoted 25 lines
xen/arch/riscv/traps.c +25
-0+riscv_vcpu_trap_redirect (&utrap); + +return true; + } + + *insn_len =INSN_LEN (*insn); + } + +return false; +} + +/* +* Check alignment and dispatch a decoded MMIO access to a registered +* handler. On success (0), info->data holds the read value for loads. +*/ +static int do_mmio (mmio_info_t *info,unsigned long fault_addr, +unsigned int len) +{ +/* Fault address should be aligned to length of MMIO */ +if ( fault_addr & (len -1 ) ) +return -EIO ; + + info->gpa = fault_addr; + info->len = len; +
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 298
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+switch (try_handle_mmio (info) ) + { +case IO_HANDLED : +return 0 ; +case IO_ABORT : +return -EIO ; +default : +return -EOPNOTSUPP ; + } +}
And there's no indication of "retry needed", e.g. when something changed between find_mmio_handler() and handle_{read,write}()?
Also, nit: Blank lines please between non-fall-through case blocks.
Quoted 20 lines
xen/arch/riscv/traps.c +17
-1static int emulate_load (unsigned long fault_addr,unsigned long htinst) { -return -EOPNOTSUPP ; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +mmio_info_t info = { .is_write = false }; +unsigned long insn; +unsigned int shift =0 , len, insn_len; +bool is_unsigned = false; +int rc; + +if (decode_trapped_insn (htinst, &insn, &insn_len) ) +return 0 ; + +/* Decode length of MMIO and whether it is a sign- or zero-extending load */ +if ( (insn &INSN_MASK_LB ) ==INSN_MATCH_LB ) + len =1 ; +else if ( (insn &INSN_MASK_LBU ) ==INSN_MATCH_LBU ) + { + len =1 ; + is_unsigned = true;
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 328
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ } +else if ( (insn &INSN_MASK_LH ) ==INSN_MATCH_LH ) + len =2 ; +else if ( (insn &INSN_MASK_LHU ) ==INSN_MATCH_LHU ) + { + len =2 ; + is_unsigned = true; + } +else if ( (insn &INSN_MASK_LW ) ==INSN_MATCH_LW ) + len =4 ;
Already up to here this demonstrates a weakness of the INSN_MASK_* set of #define-s (which I similarly observe in binutils, and I expect it all has the same questionable origin). All INSN_MASK_L* and INSN_MASK_FL* (also INSN_MASK_S* and INSN_MASK_FS*) are identical, allowing for a nice switch() to be used here in principle. That said, with access width nicely encoded in FUNCT3, it's not even clear whether a switch() would end up being needed / efficient.
Otoh none of these masks cover the pseudoinsns that htinst may supply.
Further, what about A-extension insns? Some (if not all) of them can plausibly be used on MMIO, I think.
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 330
Quoted 2 lines
xen/arch/riscv/traps.c +2
-0+#ifndef CONFIG_RISCV_32 + else if ( (insn & INSN_MASK_LWU) == INSN_MATCH_LWU )
First: Better use IS_ENABLED() in favor of #if{,n}def, whenever possible. And then this depends not only on CONFIG_RISCV_32, but also on guest bitness.
Quoted 17 lines
xen/arch/riscv/traps.c +17
-0+ { + len =4 ; + is_unsigned = true; + } +#endif + else if ( (insn & INSN_MASK_C_LW) == INSN_MATCH_C_LW ) + { + len =4 ; + insn =RVC_RS2S (insn) <<SH_RD ; + } + else if ( (insn & INSN_MASK_C_LWSP) == INSN_MATCH_C_LWSP && + RV_X(insn, SH_RD, 5) ) + len = 4; +#ifndef CONFIG_RISCV_32 + else if ( (insn & INSN_MASK_LD) == INSN_MATCH_LD ) + len = 8; + else if ( (insn & INSN_MASK_C_LD) == INSN_MATCH_C_LD )
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 357
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ { + len = 8; + insn = RVC_RS2S(insn) << SH_RD; + } + else if ( (insn & INSN_MASK_C_LDSP) == INSN_MATCH_C_LDSP && + RV_X(insn, SH_RD, 5) ) + len = 8; +#endif + else + return -EOPNOTSUPP;
Because you don't permit F/D/Q for guests (yet), FL* and FS* aren't covered, I expect? I wonder how easy it is going to be to spot the places needing adjustment once support is to be added. Same perhaps for Zilsd in RV32 guests.
Jan
Re: [PATCH v1 16/17] xen/riscv: add guest load emulation for trapped MMIO accesses
Quoted 40 lines · Jan Beulich
Quoted 20 lines · Oleksii Kurochko
static int
emulate_load(unsigned long fault_addr, unsigned long htinst){xen/arch/riscv/traps.c +17
-1-return -EOPNOTSUPP ; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +mmio_info_t info = { .is_write = false }; +unsigned long insn; +unsigned int shift =0 , len, insn_len; +bool is_unsigned = false; +int rc; + +if (decode_trapped_insn (htinst, &insn, &insn_len) ) +return 0 ; + +/* Decode length of MMIO and whether it is a sign- or zero-extending load */ +if ( (insn &INSN_MASK_LB ) ==INSN_MATCH_LB ) + len =1 ; +else if ( (insn &INSN_MASK_LBU ) ==INSN_MATCH_LBU ) + { + len =1 ; + is_unsigned = true;xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 328
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ } +else if ( (insn &INSN_MASK_LH ) ==INSN_MATCH_LH ) + len =2 ; +else if ( (insn &INSN_MASK_LHU ) ==INSN_MATCH_LHU ) + { + len =2 ; + is_unsigned = true; + } +else if ( (insn &INSN_MASK_LW ) ==INSN_MATCH_LW ) + len =4 ;Already up to here this demonstrates a weakness of the INSN_MASK_* set of #define-s (which I similarly observe in binutils, and I expect it all has the same questionable origin). All INSN_MASK_L* and INSN_MASK_FL* (also INSN_MASK_S* and INSN_MASK_FS*) are identical, allowing for a nice
switch()to be used here in principle. That said, with access width nicely encoded in FUNCT3, it's not even clear whether aswitch()would end up being needed / efficient.
Quoted 4 lines
Otoh none of these masks cover the pseudoinsns that htinst may supply.
Further, what about A-extension insns? Some (if not all) of them can plausibly be used on MMIO, I think.
Because of the further additions that are going to be needed, may I also suggest to consider putting emulation code in its own file (emulate.c perhaps), rather than directly in traps.c?
Jan
Re: [PATCH v1 16/17] xen/riscv: add guest load emulation for trapped MMIO accesses
Quoted 41 lines · Jan Beulich
Quoted 40 lines · Jan Beulich
Quoted 20 lines · Oleksii Kurochko
static int
emulate_load(unsigned long fault_addr, unsigned long htinst){xen/arch/riscv/traps.c +17
-1-return -EOPNOTSUPP ; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +mmio_info_t info = { .is_write = false }; +unsigned long insn; +unsigned int shift =0 , len, insn_len; +bool is_unsigned = false; +int rc; + +if (decode_trapped_insn (htinst, &insn, &insn_len) ) +return 0 ; + +/* Decode length of MMIO and whether it is a sign- or zero-extending load */ +if ( (insn &INSN_MASK_LB ) ==INSN_MATCH_LB ) + len =1 ; +else if ( (insn &INSN_MASK_LBU ) ==INSN_MATCH_LBU ) + { + len =1 ; + is_unsigned = true;xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 328
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ } +else if ( (insn &INSN_MASK_LH ) ==INSN_MATCH_LH ) + len =2 ; +else if ( (insn &INSN_MASK_LHU ) ==INSN_MATCH_LHU ) + { + len =2 ; + is_unsigned = true; + } +else if ( (insn &INSN_MASK_LW ) ==INSN_MATCH_LW ) + len =4 ;Already up to here this demonstrates a weakness of the INSN_MASK_* set of #define-s (which I similarly observe in binutils, and I expect it all has the same questionable origin). All INSN_MASK_L* and INSN_MASK_FL* (also INSN_MASK_S* and INSN_MASK_FS*) are identical, allowing for a nice
switch()to be used here in principle. That said, with access width nicely encoded in FUNCT3, it's not even clear whether aswitch()would end up being needed / efficient.
Quoted 8 lines
Quoted 4 lines
Otoh none of these masks cover the pseudoinsns that htinst may supply.
Further, what about A-extension insns? Some (if not all) of them can plausibly be used on MMIO, I think.
Because of the further additions that are going to be needed, may I also suggest to consider putting emulation code in its own file (
emulate.cperhaps), rather than directly intraps.c?
Good point. It really makes sense to move emulation now to emulate.c.
Thanks.
~ Oleksii
Re: [PATCH v1 16/17] xen/riscv: add guest load emulation for trapped MMIO accesses
In reply to Jan BeulichQuoted 27 lines · Jan Beulich
Quoted 12 lines · Oleksii Kurochko
Introduce
emulate_load()to decode and emulate guest load instructions that fault due to MMIO accesses. This provides the basic infrastructure required for MMIO emulation on RISC-V.The instruction decode (
decode_trapped_insn()and the mask/match chain for standard and compressed load encodings) is adapted from Linux's KVM RISC-V implementation. The completion path differs from KVM's, since Xen dispatches MMIO synchronously to an in-hypervisor handler viatry_handle_mmio()and has no userspace exit/return step equivalent to KVM'skvm_io_bus_read()/KVM_EXIT_MMIO/kvm_riscv_vcpu_mmio_return()split.Quoted 13 lines
A fault taken while re-reading the trapped instruction is handled depending on the faulting translation stage:
- A VS-stage fault is the guest's own fault (e.g. it modified its page tables from another vCPU) and, as in KVM, is redirected to the guest's trap vector, with the cause remapped toCAUSE_FETCH_PAGE_FAULTsince HLVX reports execute-permission failures as load faults.
- A G-stage fault would mean the P2M mapping of the instruction page disappeared after the instruction was fetched. KVM must handle this by resuming the guest and retrying, as Linux MM can invalidate G-stage mappings at any time. Xen does not remove P2M mappings of a running domain at the moment, so this case is asserted unreachable withBUG_ON(); it will need to be revisited once such removal is implemented.
Quoted 3 lines
I don't see why this cannot be implemented correctly right away. The behavior should be that of an access to unpopulated space on bare hardware, whatever that behavior is on RISC-V.
I wasn't able to find a spec what should be returned in this case but in QEMU source code I founded (unassigned_mem_ops → MEMTX_DECODE_ERROR → io_failed() → riscv_cpu_do_transaction_failed().):
void riscv_cpu_do_transaction_failed(CPUState *cs, hwaddr physaddr,
vaddr addr, unsigned size, MMUAccessType access_type, int mmu_idx, MemTxAttrs attrs, MemTxResult response, uintptr_t
retaddr)
{
RISCVCPU *cpu = RISCV_CPU(cs);
CPURISCVState *env = &cpu->env;
if (access_type == MMU_DATA_STORE) {
cs->exception_index = RISCV_EXCP_STORE_AMO_ACCESS_FAULT;
} else if (access_type == MMU_DATA_LOAD) {
cs->exception_index = RISCV_EXCP_LOAD_ACCESS_FAULT;
} else {
cs->exception_index = RISCV_EXCP_INST_ACCESS_FAULT;
}So RISCV_EXCP_INST_ACCESS_FAULT (in Xen it is CAUSE_FETCH_ACCESS) will be fine to return.
So do the following:
if ( is_load_guest_page_fault(utrap.scause) )
utrap.scause = CAUSE_FETCH_ACCESS;
will be fair enough instead of: BUG_ON(is_load_guest_page_fault(utrap.scause)).
Probably, we want to rename CAUSE_FETCH_ACCESS to be closer to RISC-V spec as for value 1 in spec it is used:
1 Instruction access fault
Interesting that all other CAUSE_* defines are aligned with the spec...
Quoted 17 lines
xen/arch/riscv/include/asm/traps.h struct trap_info { · line 20
Quoted 9 lines
xen/arch/riscv/include/asm/traps.h +4
-0@@ -13,6 +14,11 @@ struct trap_info {register_t stval; }; +static inline bool is_load_guest_page_fault (unsigned long scause) +{ +return (scause ==CAUSE_LOAD_GUEST_PAGE_FAULT ); +}Is something like this really a useful wrapper to have? It doesn't really shorten anything, nor does (imo) it aid readability.
Quoted 4 lines
xen/arch/riscv/traps.c +0
-0@@ -191,6 +193,11 @@ static void timer_interrupt(void)raise_softirq (TIMER_SOFTIRQ ); }
Quoted 5 lines
xen/arch/riscv/traps.c static void timer_interrupt(void) · line 196
Quoted 1 line
xen/arch/riscv/traps.c +1
-0+static always_inline void advance_pc(struct cpu_user_regs *regs, int step)See my earlier remark regarding
always_inline. Also - why plain int? Are there (going to be) cases where PC is moved backwards (in which case "advance" isn't suitable naming)?
No, it won't. At least, I don't see such use cases now. I'll use unsigned int instead.
Quoted 33 lines
Quoted 20 lines
xen/arch/riscv/traps.c +12
-0+{ + regs->sepc += step; +} +static always_inline unsigned long get_faulting_gpa (void ) {/* @@ -210,9 +217,162 @@ static always_inline unsigned long get_faulting_gpa(void)return (csr_read (CSR_HTVAL ) <<2 ) | (csr_read (CSR_STVAL ) &0x3 ); } +/* +* Determine the trapped instruction which caused a guest MMIO trap. +* +* Returns true if the trap was redirected to the guest, in which case +* the caller must stop emulation and return success. Otherwise *insn +* and *insn_len are filled in and the caller should continue decoding. +*/ +static bool decode_trapped_insn(unsigned long htinst, unsigned long *insn,xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 237
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ unsigned int *insn_len) +{ +if ( htinst &0x1 ) + { +/* +* Bit[0] == 1 implies trapped instruction value is +* transformed instruction or custom instruction. +*/ + *insn = htinst |INSN_16BIT_MASK ; + *insn_len = (htinst &BIT (1 ,UL )) ?INSN_LEN (*insn) :2 ;In the
if()you don't useBIT(), while here you do. Please be consistent.
Quoted 2 lines
Why the use of
INSN_LEN(), when due to the earlier assignment it'll always yield 4 here?
ld/sd instruction which we are trapping here at the moment here could be 2 bit and 4 bit depends on C extension so we need to pass correct instruction length to advance_pc() after it is emulated.
Quoted 3 lines
Finally, how would the caller know whether it looks at a transformed insn or (as fetched below) a "normal" one?
According to the spec ((part from htinst ... ):
On a synchronous exception, if a nonzero value is written, one of the following shall be true about the value:
• Bit 0 is 1, and replacing bit 1 with 1 makes the value into a valid encoding of a standard instruction.
In this case, the instruction that trapped is the same kind as indicated by the register value, and the register value is the transformation of the trapping instruction, as defined later. For example, if bits 1:0 are binary 11 and the register value is the encoding of a standard LW (load word) instruction, then the trapping instruction is LW, and the register value is the transformation of the trapping LW instruction.
• Bit 0 is 1, and replacing bit 1 with 1 makes the value into an instruction encoding that is explicitly designated for a custom instruction (not an unused reserved encoding). This is a custom value. The instruction that trapped is a non-standard instruction. The interpretation of a custom value is not otherwise specified by this standard.
• The value is one of the special pseudoinstructions defined later, all of which have bits 1:0 equal to 00.
So setting bit 0 to 1 we will guarantee that it is normal "normal" instruction.
Quoted 12 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 241
Quoted 5 lines
xen/arch/riscv/traps.c +4
-0+ } +else + { +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current);Pointer-to-const.
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 242
Quoted 1 line
xen/arch/riscv/traps.c +1
-0+struct trap_info utrap = {0 };Just {} please.
Quoted 8 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 247
Quoted 4 lines
xen/arch/riscv/traps.c +4
-0+/* +* Bit[0] == 0 implies trapped instruction value is +* zero or special value. +*/ How come you get away without dealing with pseudoinsns? The insn pointed at by
regs->sepcis of no interest for faults caused by implicit memory accesses originating from VS-stage address translation.
It is really problem but I think it should be resolved much earlier in handle_guest_page_fault(). I will add the following:
/*
* A guest page fault taken on an implicit memory access performed for
* VS-stage address translation (reading a PTE, or updating its A/D
bits)
* reports a pseudoinstruction in htinst rather than a transformed
* instruction. Such a fault can't be emulated: htval holds the guest
* physical address of a VS-stage PTE rather than of any access the
guest
* itself performed (and its two least significant bits are zero
instead
* of matching stval), while the instruction at sepc is unrelated
to the
* access which actually faulted.
*
* Report an access fault to the guest at the original virtual address,
* which is what stval already holds and what hardware would raise
for a
* page table walk hitting an inaccessible address.
*/
if ( (htinst == INSN_PSEUDO_VS_LOAD) || (htinst ==
INSN_PSEUDO_VS_STORE) )
{
struct cpu_user_regs *regs = vcpu_guest_cpu_user_regs(current);
struct trap_info utrap = {
.scause = (htinst == INSN_PSEUDO_VS_LOAD) ? CAUSE_LOAD_ACCESS
: CAUSE_STORE_ACCESS,
.sepc = regs->sepc,
.stval = csr_read(CSR_STVAL),
};
riscv_trap_redirect(&utrap);
return;
}and will update the comment:
Quoted 14 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void)
Quoted 13 lines
xen/arch/riscv/traps.c +11
-0+/* +* Bit[0] == 0 implies trapped instruction value is +* zero or special value. It can't be pseudoinstruction as it is guaranteed by check in handle_guest_page_fault(). +*/ + *insn =riscv_vcpu_unpriv_read (true, regs->sepc, &utrap); +if ( utrap.scause ) + { +/* +* A G-stage fault here would mean the P2M mapping of the page +* containing the trapped instruction disappeared after it was +* fetched.
Quoted 7 lines
Does it? What about, again, faults from VS-stage address translation while hardware was trying to fetch an insn? That is ...
Review static always_inline unsigned long get_faulting_gpa(void) · line 261
Quoted 2 lines
(quoted diff) +1
-0Nothing removes P2M mappings of a running domain yet, + * so this cannot happen.... the necessary P2M mapping may never have been there.
If VS-stage failed then CAUSE_LOAD_PAGE_FAULT will happen so BUG_ON() won't occur and it will be passed to guest to handle it.
BUG_ON() here catches CAUSE_LOAD_GUEST_PAGE_FAULT (G-stage translation failure).
Also, as I mentioned above I will change BUG_ON() too:
/*
* If during getting of trapped instruction a fault happen in
* G-stage translation then CAUSE_LOAD_GUEST_PAGE_FAULT is
* generated. Such faults during this operation is
considered as
* bus
*/
if ( is_load_guest_page_fault(utrap.scause) )
utrap.scause = CAUSE_FETCH_ACCESS;
Quoted 10 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 262
Quoted 7 lines
xen/arch/riscv/traps.c +6
-0+* TODO: Revisit once P2M mappings can be removed at runtime. +*/ +BUG_ON (is_load_guest_page_fault (utrap.scause)); + + utrap.sepc = regs->sepc; + utrap.stval = utrap.sepc;How do you know the fault was at .sepc? A 32-bit insn crossing a page boundary (implying the C extension is available) may well fault only on its higher half.
According to the spec, if stval is written with a nonzero value when an instruction access-fault or page-fault exception occurs on a system with variable-length instructions, then stval will contain the virtual address of the portion of the instruction that caused the fault, while sepc will point to the beginning of the instruction.
So here, we are trying to emulate what real hardware will do in this case. In regs->sepc, we have the start of the instruction that we didn't touch. sepc is filled according to the spec in this case.
Regarding utrap.stval, we know that utrap.sepc points to the correct part of the faulting address, as we are reading the instruction in 16-bit chunks:
HLVX_HU(%[val], %[addr]) ; low 16 bits from sepc
andi %[tmp], %[val], 3
addi %[tmp], %[tmp], -3
bne %[tmp], zero, 2f ; if not (insn & 3) == 3 -> 16-bit, end addi %[addr], %[addr], 2 ; <- addr is now sepc+2 HLVX_HU(%[tmp], %[addr]) ; high 16 bits, possibly from another page
So, if a trap happens while reading the high 16 bits (which may be located on another page), then utrap.sepc, if the read fails, will point to the high part of the instruction, which is what the spec requires.
Does that make sense?
Quoted 31 lines
Quoted 31 lines
xen/arch/riscv/traps.c +30
-0+riscv_vcpu_trap_redirect (&utrap); + +return true; + } + + *insn_len =INSN_LEN (*insn); + } + +return false; +} + +/* +* Check alignment and dispatch a decoded MMIO access to a registered +* handler. On success (0), info->data holds the read value for loads. +*/ +static int do_mmio (mmio_info_t *info,unsigned long fault_addr, +unsigned int len) +{ +/* Fault address should be aligned to length of MMIO */ +if ( fault_addr & (len -1 ) ) +return -EIO ; + + info->gpa = fault_addr; + info->len = len; + +switch (try_handle_mmio (info) ) + { +case IO_HANDLED : +return 0 ; +case IO_ABORT :
Quoted 8 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 298
Quoted 5 lines
xen/arch/riscv/traps.c +5
-0+return -EIO ; +default : +return -EOPNOTSUPP ; + } +}And there's no indication of "retry needed", e.g. when something changed between
find_mmio_handler()and handle_{read,write}()?
I don't have any specific scenario where it is needed now so I don't know what to say.
And there is no race between find_mmio_handler() and handle_{read,write}() as find_mmio_handler() returns copy of the structure under read_lock():
/*
* Return a copy of the matching handler rather than a pointer into
* vmmio->handlers: a concurrent register_mmio_handler() shifts entries
* up to keep the array sorted, so an escaped pointer could refer to a
* different (or torn) entry once the lock is dropped. The copy stays
* valid as the ops structures are never freed.
*/
static bool find_mmio_handler(struct domain *d, paddr_t gpa,
struct mmio_handler *out)
{
struct vmmio *vmmio = &d->arch.vmmio;
struct mmio_handler key = { .addr = gpa };
const struct mmio_handler *handler;
read_lock(&vmmio->lock);
handler = bsearch(&key, vmmio->handlers, vmmio->num_entries,
sizeof(*handler), cmp_mmio_handler);
if ( handler )
*out = *handler;
read_unlock(&vmmio->lock);
return handler != NULL;
}At the moment, use cases are pretty strainghforward, a device from guest trying to read/write into MMIO and so the result logically could be or it is successfully handled or some issue happened and it is just aborted. As a real hardware will do, I think.
Quoted 41 lines
Also, nit: Blank lines please between non-fall-through case blocks.
Quoted 20 lines
xen/arch/riscv/traps.c +17
-1static int emulate_load (unsigned long fault_addr,unsigned long htinst) { -return -EOPNOTSUPP ; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +mmio_info_t info = { .is_write = false }; +unsigned long insn; +unsigned int shift =0 , len, insn_len; +bool is_unsigned = false; +int rc; + +if (decode_trapped_insn (htinst, &insn, &insn_len) ) +return 0 ; + +/* Decode length of MMIO and whether it is a sign- or zero-extending load */ +if ( (insn &INSN_MASK_LB ) ==INSN_MATCH_LB ) + len =1 ; +else if ( (insn &INSN_MASK_LBU ) ==INSN_MATCH_LBU ) + { + len =1 ; + is_unsigned = true;xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 328
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ } +else if ( (insn &INSN_MASK_LH ) ==INSN_MATCH_LH ) + len =2 ; +else if ( (insn &INSN_MASK_LHU ) ==INSN_MATCH_LHU ) + { + len =2 ; + is_unsigned = true; + } +else if ( (insn &INSN_MASK_LW ) ==INSN_MATCH_LW ) + len =4 ;Already up to here this demonstrates a weakness of the INSN_MASK_* set of #define-s (which I similarly observe in binutils, and I expect it all has the same questionable origin). All INSN_MASK_L* and INSN_MASK_FL* (also INSN_MASK_S* and INSN_MASK_FS*) are identical, allowing for a nice
switch()to be used here in principle. That said, with access width nicely encoded in FUNCT3, it's not even clear whether aswitch()would end up being needed / efficient.
.......
Quoted 2 lines
Otoh none of these masks cover the pseudoinsns that htinst may supply.
As I answered above we should handle that before this function will call so here we won't deal with htinst at all. Of course, if what I wrote above is correct. I will double check before applying that.
Quoted 3 lines
Further, what about A-extension insns? Some (if not all) of them can plausibly be used on MMIO, I think.
I’m not really sure that the A-extension is actively used for MMIO. At least, Linux doesn’t do that for now, which is why we don’t handle A-extension instructions here.
I think this is related to the fact that MMIO is usually (if not always?) naturally aligned, and naturally aligned loads and stores are guaranteed by RISC-V to execute atomically.
Anyway, since we don’t have a case for this for now, I think we could go with the current emulation. If this turns out not to be true in the future, A-extension support can be added separately.
Quoted 7 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 330
Quoted 3 lines
xen/arch/riscv/traps.c +2
-0+#ifndef CONFIG_RISCV_32 + else if ( (insn & INSN_MASK_LWU) == INSN_MATCH_LWU )First: Better use
IS_ENABLED()in favor of #if{,n}def, whenever possible. And then this depends not only onCONFIG_RISCV_32, but also on guest bitness.
Both points taken. The #ifndef will become a condition in the if-chain; the INSN_MATCH_/INSN_MASK_ definitions are unconditional in riscv_encoding.h, so that builds either way.
On guest bitness you're right, and it's worse than the 32-bit-only encodings being reserved in RV32: the compressed RV64 encodings collide with the RV32 single-precision float ones — C.LD and C.FLW are both 0x6000 under mask 0xe003, likewise C.SD/C.FSW, C.LDSP/C.FLWSP and C.SDSP/C.FSWSP. A 32-bit guest doing a c.flw to an emulated MMIO region would be decoded as c.ld, i.e.an 8-byte access with the result written to an integer register.
I'll fold both into a helper returning the guest's effective XLEN
(hstatus.VSXL, or vsstatus.UXL when the trap was taken from VU-mode, and
unconditionally 32 for a RV32 build) and gate the RV64-only cases on it. As a side note, vcpu hstatus setup currently leaves VSXL alone and thus relies on the WARL behaviour of the field; I think Xen should set it explicitly.
/*
* The effective XLEN of the guest at the point of the trap:
hstatus.VSXL for a
* trap taken from VS-mode, vsstatus.UXL for one taken from VU-mode. *
* It is needed to decode a trapped instruction: the encodings which
exist only
* for XLEN=64 must not be recognized for a 32-bit guest. Besides those
simply
* being reserved there, the compressed ones are ambiguous: C.LD and C.FLW
* share the encoding 0x6000 (mask 0xe003), and likewise C.SD/C.FSW,
* C.LDSP/C.FLWSP and C.SDSP/C.FSWSP.
*
* IS_ENABLED() can't be used here as HSTATUS_VSXL is defined for
* __riscv_xlen == 64 only, the field not existing on RV32 in the first
place.
*/
static unsigned int guest_xlen(const struct cpu_user_regs *regs) {
#ifdef CONFIG_RISCV_32
return 32;
#else
unsigned long xl = (regs->sstatus & SSTATUS_SPP)
? MASK_EXTR(regs->hstatus, HSTATUS_VSXL)
: MASK_EXTR(csr_read(CSR_VSSTATUS), SSTATUS64_UXL);
/* 1 encodes XLEN=32, 2 encodes XLEN=64. */
return (xl == HSTATUS_VSXL_32) ? 32 : 64;#endif
}
and then use it in emulate_store/load():
unsigned int xlen = guest_xlen(regs);
...
else if ( (xlen == 64) && ((insn & INSN_MASK_LWU) == INSN_MATCH_LWU) )
{
len = 4;
is_unsigned = true;
}
...
else if ( (xlen == 64) && ((insn & INSN_MASK_C_LD) == INSN_MATCH_C_LD) )
Quoted 23 lines
Quoted 23 lines
xen/arch/riscv/traps.c +22
-0+ { + len =4 ; + is_unsigned = true; + } +#endif + else if ( (insn & INSN_MASK_C_LW) == INSN_MATCH_C_LW ) + { + len =4 ; + insn =RVC_RS2S (insn) <<SH_RD ; + } + else if ( (insn & INSN_MASK_C_LWSP) == INSN_MATCH_C_LWSP && + RV_X(insn, SH_RD, 5) ) + len = 4; +#ifndef CONFIG_RISCV_32 + else if ( (insn & INSN_MASK_LD) == INSN_MATCH_LD ) + len = 8; + else if ( (insn & INSN_MASK_C_LD) == INSN_MATCH_C_LD ) + { + len =8 ; + insn =RVC_RS2S (insn) <<SH_RD ; + } + else if ( (insn & INSN_MASK_C_LDSP) == INSN_MATCH_C_LDSP &&
Quoted 8 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 357
Quoted 5 lines
xen/arch/riscv/traps.c +5
-0+ RV_X(insn, SH_RD, 5) ) + len =8 ; +#endif + else + return -EOPNOTSUPP;Because you don't permit F/D/Q for guests (yet), FL* and FS* aren't covered, I expect?
At the moment, I wrote this function with handling of MMIO instruction in mind, which are at the moment ld and sd.
Even if to permit F/D/Q then do we really need to trap that instructions? Hypervisor could allow access to FPU to guest and then it will be just a question of context switch to properly save and restore FPU.
Quoted 3 lines
I wonder how easy it is going to be to spot the places needing adjustment once support is to be added. Same perhaps for Zilsd in RV32 guests.
There is a message in handle_guest_page_fault():
if ( rc )
domain_crash(current->domain,
"%s: unable to handle faulted guest %s addr %#lx\n",
__func__,
(cause == CAUSE_LOAD_GUEST_PAGE_FAULT) ? "load" :
"store",
addr);Probably, it isn't enough and we could print here instruction (in hex) before return -EOPNOTSUPP.
Thanks.
~ Oleksii
Re: [PATCH v1 16/17] xen/riscv: add guest load emulation for trapped MMIO accesses
Quoted 27 lines · Oleksii Kurochko
Quoted 26 lines · Jan Beulich
Quoted 12 lines · Oleksii Kurochko
xen/arch/riscv/traps.c +8
-0@@ -210,9 +217,162 @@ static always_inline unsigned long get_faulting_gpa(void)return (csr_read (CSR_HTVAL ) <<2 ) | (csr_read (CSR_STVAL ) &0x3 ); } +/* +* Determine the trapped instruction which caused a guest MMIO trap. +* +* Returns true if the trap was redirected to the guest, in which case +* the caller must stop emulation and return success. Otherwise *insn +* and *insn_len are filled in and the caller should continue decoding. +*/ +static bool decode_trapped_insn(unsigned long htinst, unsigned long *insn,xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 237
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ unsigned int *insn_len) +{ +if ( htinst &0x1 ) + { +/* +* Bit[0] == 1 implies trapped instruction value is +* transformed instruction or custom instruction. +*/ + *insn = htinst |INSN_16BIT_MASK ; + *insn_len = (htinst &BIT (1 ,UL )) ?INSN_LEN (*insn) :2 ;In the
if()you don't useBIT(), while here you do. Please be consistent.
Quoted 6 lines
Quoted 2 lines
Why the use of
INSN_LEN(), when due to the earlier assignment it'll always yield 4 here?ld/sd instruction which we are trapping here at the moment here could be 2 bit and 4 bit depends on C extension so we need to pass correct instruction length to
advance_pc()after it is emulated.
Well, fine, but how does that matter? I pointed you at the preceding assignment, which sets bits 0 and 1. With that INSN_LEN() is guaranteed to return (at least) 4 (and it's not presently capable of returning values larger than 4).
Quoted 23 lines
Quoted 2 lines
Finally, how would the caller know whether it looks at a transformed insn or (as fetched below) a "normal" one?
According to the spec ((part from htinst ... ):
On a synchronous exception, if a nonzero value is written, one of the following shall be true about the value:• Bit 0 is 1, and replacing bit 1 with 1 makes the value into a valid encoding of a standard instruction.
In this case, the instruction that trapped is the same kind as indicated by the register value, and the register value is the transformation of the trapping instruction, as defined later. For example, if bits 1:0 are binary 11 and the register value is the encoding of a standard LW (load word) instruction, then the trapping instruction is LW, and the register value is the transformation of the trapping LW instruction.• Bit 0 is 1, and replacing bit 1 with 1 makes the value into an instruction encoding that is explicitly designated for a custom instruction (not an unused reserved encoding). This is a custom value. The instruction that trapped is a non-standard instruction. The interpretation of a custom value is not otherwise specified by this standard.
Quoted 5 lines
• The value is one of the special pseudoinstructions defined later, all of which have bits 1:0 equal to 00.
So setting bit 0 to 1 we will guarantee that it is normal "normal" instruction.
Right. Yet my question was how to distinguish the cases. Or are you trying to tell me that distinguishing isn't going to be necessary, anywhere?
Quoted 40 lines
Quoted 11 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 241
Quoted 4 lines
xen/arch/riscv/traps.c +4
-0+ } +else + { +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current);Pointer-to-const.
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 242
Quoted 1 line
xen/arch/riscv/traps.c +1
-0+struct trap_info utrap = {0 };Just {} please.
Quoted 8 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 247
Quoted 4 lines
xen/arch/riscv/traps.c +4
-0+/* +* Bit[0] == 0 implies trapped instruction value is +* zero or special value. +*/ How come you get away without dealing with pseudoinsns? The insn pointed at by
regs->sepcis of no interest for faults caused by implicit memory accesses originating from VS-stage address translation.It is really problem but I think it should be resolved much earlier in
handle_guest_page_fault(). I will add the following:/*
* A guest page fault taken on an implicit memory access performed for
* VS-stage address translation (reading a PTE, or updating its A/D
bits)
* reports a pseudoinstruction in htinst rather than a transformed
* instruction. Such a fault can't be emulated: htval holds the guest
* physical address of a VS-stage PTE rather than of any access the
guest
* itself performed (and its two least significant bits are zero
instead
* of matching stval), while the instruction at sepc is unrelated
to the
* access which actually faulted.
*
* Report an access fault to the guest at the original virtual address,
* which is what stval already holds and what hardware would raise
for a
Quoted 16 lines
* page table walk hitting an inaccessible address.
*/
if ( (htinst == INSN_PSEUDO_VS_LOAD) || (htinst ==
INSN_PSEUDO_VS_STORE) )
{
struct cpu_user_regs *regs = vcpu_guest_cpu_user_regs(current);
struct trap_info utrap = {
.scause = (htinst == INSN_PSEUDO_VS_LOAD) ? CAUSE_LOAD_ACCESS
: CAUSE_STORE_ACCESS,
.sepc = regs->sepc,
.stval = csr_read(CSR_STVAL),
};
riscv_trap_redirect(&utrap);
return;
}That's not what would happen on bare hardware though, aiui. At least I don't think I ever found it being spelled out anywhere what the supposed behavior is when a page table resides in unpopulated space.
Quoted 11 lines
Quoted 11 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void)
Quoted 7 lines
xen/arch/riscv/traps.c +7
-0+ *insn =riscv_vcpu_unpriv_read (true, regs->sepc, &utrap); +if ( utrap.scause ) + { +/* +* A G-stage fault here would mean the P2M mapping of the page +* containing the trapped instruction disappeared after it was +* fetched. Does it? What about, again, faults from VS-stage address translation while hardware was trying to fetch an insn? That is ...
Quoted 7 lines
Quoted 4 lines
Review static always_inline unsigned long get_faulting_gpa(void) · line 255
Quoted 2 lines
(quoted diff) +1
-0Nothing removes P2M mappings of a running domain yet, + * so this cannot happen.... the necessary P2M mapping may never have been there.
If VS-stage failed then
CAUSE_LOAD_PAGE_FAULTwill happen soBUG_ON()won't occur and it will be passed to guest to handle it.
Are you sure? So far it was my understanding that CAUSE_LOAD_PAGE_FAULT would happen when VS-stage translation hits e.g. a non-present leaf entry. But got an address translation failure while doing the VS-stage page walk (i.e. failure to translate the address found in a VS-stage PTE to a host address) would raise CAUSE_LOAD_GUEST_PAGE_FAULT.
Quoted 5 lines
BUG_ON()here catchesCAUSE_LOAD_GUEST_PAGE_FAULT(G-stage translation failure).Also, as I mentioned above I will change
BUG_ON()too:
Quoted 7 lines
/*
* If during getting of trapped instruction a fault happen in
* G-stage translation thenCAUSE_LOAD_GUEST_PAGE_FAULTis
* generated. Such faults during this operation is
considered as
* bus
*/
What is "bus" here (dym "bug"?), and why is the sentence unfinished?
Quoted 16 lines
Quoted 9 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 262
Quoted 6 lines
xen/arch/riscv/traps.c +6
-0+* TODO: Revisit once P2M mappings can be removed at runtime. +*/ +BUG_ON (is_load_guest_page_fault (utrap.scause)); + + utrap.sepc = regs->sepc; + utrap.stval = utrap.sepc;How do you know the fault was at .sepc? A 32-bit insn crossing a page boundary (implying the C extension is available) may well fault only on its higher half.
According to the spec, if stval is written with a nonzero value when an instruction access-fault or page-fault exception occurs on a system with variable-length instructions, then stval will contain the virtual address of the portion of the instruction that caused the fault, while sepc will point to the beginning of the instruction.
Quoted 3 lines
So here, we are trying to emulate what real hardware will do in this case. In
regs->sepc, we have the start of the instruction that we didn't touch. sepc is filled according to the spec in this case.
Right, but utrap.stval is set to the same value, which is explicitly not in line with what you say above ("will contain the virtual address of the portion of the instruction that caused the fault").
Quoted 11 lines
Regarding utrap.stval, we know that utrap.sepc points to the correct part of the faulting address, as we are reading the instruction in 16-bit chunks:
HLVX_HU(%[val], %[addr]); low 16 bits from sepc
andi %[tmp], %[val], 3
addi %[tmp], %[tmp], -3bne %[tmp], zero, 2f ; if not (insn & 3) == 3 -> 16-bit, end addi %[addr], %[addr], 2 ; <- addr is now sepc+2 HLVX_HU(%[tmp], %[addr]) ; high 16 bits, possibly from another page
Quoted 5 lines
So, if a trap happens while reading the high 16 bits (which may be located on another page), then utrap.sepc, if the read fails, will point to the high part of the instruction, which is what the spec requires.
Does that make sense?
Not really, no. As said above - the code as written guarantees utrap.stval == utrap.sepc, and that cannot always be correct.
Quoted 28 lines
Quoted 19 lines
Quoted 19 lines
xen/arch/riscv/traps.c +19
-0+/* +* Check alignment and dispatch a decoded MMIO access to a registered +* handler. On success (0), info->data holds the read value for loads. +*/ +static int do_mmio (mmio_info_t *info,unsigned long fault_addr, +unsigned int len) +{ +/* Fault address should be aligned to length of MMIO */ +if ( fault_addr & (len -1 ) ) +return -EIO ; + + info->gpa = fault_addr; + info->len = len; + +switch (try_handle_mmio (info) ) + { +case IO_HANDLED : +return 0 ; +case IO_ABORT :Quoted 8 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 298
Quoted 5 lines
xen/arch/riscv/traps.c +5
-0+return -EIO ; +default : +return -EOPNOTSUPP ; + } +}And there's no indication of "retry needed", e.g. when something changed between
find_mmio_handler()and handle_{read,write}()?
Quoted 5 lines
I don't have any specific scenario where it is needed now so I don't know what to say.
And there is no race betweenfind_mmio_handler()and handle_{read,write}() asfind_mmio_handler()returns copy of the structure underread_lock():
Oh, right, but that's not visible here at all and requires going back to patch 04 to realize.
Quoted 49 lines
Quoted 38 lines
Quoted 20 lines
xen/arch/riscv/traps.c +17
-1static int emulate_load (unsigned long fault_addr,unsigned long htinst) { -return -EOPNOTSUPP ; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +mmio_info_t info = { .is_write = false }; +unsigned long insn; +unsigned int shift =0 , len, insn_len; +bool is_unsigned = false; +int rc; + +if (decode_trapped_insn (htinst, &insn, &insn_len) ) +return 0 ; + +/* Decode length of MMIO and whether it is a sign- or zero-extending load */ +if ( (insn &INSN_MASK_LB ) ==INSN_MATCH_LB ) + len =1 ; +else if ( (insn &INSN_MASK_LBU ) ==INSN_MATCH_LBU ) + { + len =1 ; + is_unsigned = true;xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 328
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ } +else if ( (insn &INSN_MASK_LH ) ==INSN_MATCH_LH ) + len =2 ; +else if ( (insn &INSN_MASK_LHU ) ==INSN_MATCH_LHU ) + { + len =2 ; + is_unsigned = true; + } +else if ( (insn &INSN_MASK_LW ) ==INSN_MATCH_LW ) + len =4 ;Already up to here this demonstrates a weakness of the INSN_MASK_* set of #define-s (which I similarly observe in binutils, and I expect it all has the same questionable origin). All INSN_MASK_L* and INSN_MASK_FL* (also INSN_MASK_S* and INSN_MASK_FS*) are identical, allowing for a nice
switch()to be used here in principle. That said, with access width nicely encoded in FUNCT3, it's not even clear whether aswitch()would end up being needed / efficient........
Quoted 2 lines
Otoh none of these masks cover the pseudoinsns that htinst may supply.
As I answered above we should handle that before this function will call so here we won't deal with htinst at all. Of course, if what I wrote above is correct. I will double check before applying that.
Quoted 1 line
Quoted 4 lines
Quoted 2 lines
Further, what about A-extension insns? Some (if not all) of them can plausibly be used on MMIO, I think.
I’m not really sure that the A-extension is actively used for MMIO.
Does the spec preclude their use? I'm unaware of such a restriction.
Quoted 3 lines
At
least, Linux doesn’t do that for now, which is why we don’t handle A-extension instructions here.
Focusing on what present Linux needs is okay, but then remaining gaps should (as said on various other occasions before) be clearly marked.
Quoted 3 lines
I think this is related to the fact that MMIO is usually (if not always?) naturally aligned, and naturally aligned loads and stores are guaranteed by RISC-V to execute atomically.
How does this matter, when a bit or field in MMIO may serve the purpose of e.g. a semaphore?
Quoted 31 lines
Quoted 22 lines
Quoted 22 lines
xen/arch/riscv/traps.c +22
-0+ { + len =4 ; + is_unsigned = true; + } +#endif + else if ( (insn & INSN_MASK_C_LW) == INSN_MATCH_C_LW ) + { + len =4 ; + insn =RVC_RS2S (insn) <<SH_RD ; + } + else if ( (insn & INSN_MASK_C_LWSP) == INSN_MATCH_C_LWSP && + RV_X(insn, SH_RD, 5) ) + len = 4; +#ifndef CONFIG_RISCV_32 + else if ( (insn & INSN_MASK_LD) == INSN_MATCH_LD ) + len = 8; + else if ( (insn & INSN_MASK_C_LD) == INSN_MATCH_C_LD ) + { + len =8 ; + insn =RVC_RS2S (insn) <<SH_RD ; + } + else if ( (insn & INSN_MASK_C_LDSP) == INSN_MATCH_C_LDSP &&Quoted 8 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 357
Quoted 5 lines
xen/arch/riscv/traps.c +5
-0+ RV_X(insn, SH_RD, 5) ) + len =8 ; +#endif + else + return -EOPNOTSUPP;Because you don't permit F/D/Q for guests (yet), FL* and FS* aren't covered, I expect?
Quoted 6 lines
At the moment, I wrote this function with handling of MMIO instruction in mind, which are at the moment ld and sd.
Even if to permit F/D/Q then do we really need to trap that instructions? Hypervisor could allow access to FPU to guest and then it will be just a question of context switch to properly save and restore FPU.
And how would you know FPU loads/stores aren't used against MMIO? Later on, once V support is added, even its loads/stores might be used that way. Think of video frame buffer accesses, for example.
Jan
Re: [PATCH v1 16/17] xen/riscv: add guest load emulation for trapped MMIO accesses
Quoted 31 lines · Jan Beulich
Quoted 30 lines · Oleksii Kurochko
Quoted 28 lines · Jan Beulich
Quoted 12 lines · Oleksii Kurochko
xen/arch/riscv/traps.c +8
-0@@ -210,9 +217,162 @@ static always_inline unsigned long get_faulting_gpa(void)return (csr_read (CSR_HTVAL ) <<2 ) | (csr_read (CSR_STVAL ) &0x3 ); } +/* +* Determine the trapped instruction which caused a guest MMIO trap. +* +* Returns true if the trap was redirected to the guest, in which case +* the caller must stop emulation and return success. Otherwise *insn +* and *insn_len are filled in and the caller should continue decoding. +*/ +static bool decode_trapped_insn(unsigned long htinst, unsigned long *insn,xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 237
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ unsigned int *insn_len) +{ +if ( htinst &0x1 ) + { +/* +* Bit[0] == 1 implies trapped instruction value is +* transformed instruction or custom instruction. +*/ + *insn = htinst |INSN_16BIT_MASK ; + *insn_len = (htinst &BIT (1 ,UL )) ?INSN_LEN (*insn) :2 ;In the
if()you don't useBIT(), while here you do. Please be consistent.Why the use of
INSN_LEN(), when due to the earlier assignment it'll always yield 4 here?
Quoted 8 lines
Quoted 3 lines
ld/sd instruction which we are trapping here at the moment here could be 2 bit and 4 bit depends on C extension so we need to pass correct instruction length to
advance_pc()after it is emulated.Well, fine, but how does that matter? I pointed you at the preceding assignment, which sets bits 0 and 1. With that
INSN_LEN()is guaranteed to return (at least) 4 (and it's not presently capable of returning values larger than 4).
Oh, right. But considering that htinst can handle only max 31 bits so it looks like it can't fit more then 32 bits instructions. But I think it is needed ifdef around INSN_LEN to not miss add support for longer instructions or just write now more generic macros (or static inline function).
Quoted 24 lines
Quoted 24 lines
Quoted 3 lines
Finally, how would the caller know whether it looks at a transformed insn or (as fetched below) a "normal" one?
According to the spec ((part from htinst ... ):
On a synchronous exception, if a nonzero value is written, one of the following shall be true about the value:• Bit 0 is 1, and replacing bit 1 with 1 makes the value into a valid encoding of a standard instruction.
In this case, the instruction that trapped is the same kind as indicated by the register value, and the register value is the transformation of the trapping instruction, as defined later. For example, if bits 1:0 are binary 11 and the register value is the encoding of a standard LW (load word) instruction, then the trapping instruction is LW, and the register value is the transformation of the trapping LW instruction.• Bit 0 is 1, and replacing bit 1 with 1 makes the value into an instruction encoding that is explicitly designated for a custom instruction (not an unused reserved encoding). This is a custom value. The instruction that trapped is a non-standard instruction. The interpretation of a custom value is not otherwise specified by this standard.
Quoted 8 lines
Quoted 5 lines
• The value is one of the special pseudoinstructions defined later, all of which have bits 1:0 equal to 00.
So setting bit 0 to 1 we will guarantee that it is normal "normal" instruction.
Right. Yet my question was how to distinguish the cases. Or are you trying to tell me that distinguishing isn't going to be necessary, anywhere?
Yes, I don't see for now why such distinguish is necessary. But I will re-check that point.
Quoted 54 lines
Quoted 54 lines
Quoted 12 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 241
Quoted 5 lines
xen/arch/riscv/traps.c +4
-0+ } +else + { +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current);Pointer-to-const.
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 242
Quoted 1 line
xen/arch/riscv/traps.c +1
-0+struct trap_info utrap = {0 };Just {} please.
Quoted 8 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 247
Quoted 4 lines
xen/arch/riscv/traps.c +4
-0+/* +* Bit[0] == 0 implies trapped instruction value is +* zero or special value. +*/ How come you get away without dealing with pseudoinsns? The insn pointed at by
regs->sepcis of no interest for faults caused by implicit memory accesses originating from VS-stage address translation.It is really problem but I think it should be resolved much earlier in
handle_guest_page_fault(). I will add the following:/*
* A guest page fault taken on an implicit memory access performed for
* VS-stage address translation (reading a PTE, or updating its A/D
bits)
* reports a pseudoinstruction in htinst rather than a transformed
* instruction. Such a fault can't be emulated: htval holds the guest
* physical address of a VS-stage PTE rather than of any access the
guest
* itself performed (and its two least significant bits are zero
instead
* of matching stval), while the instruction at sepc is unrelated
to the
* access which actually faulted.
*
* Report an access fault to the guest at the original virtual address,
* which is what stval already holds and what hardware would raise
for a* page table walk hitting an inaccessible address. */ if ( (htinst == INSN_PSEUDO_VS_LOAD) || (htinst == INSN_PSEUDO_VS_STORE) ) { struct cpu_user_regs *regs = vcpu_guest_cpu_user_regs(current); struct trap_info utrap = { .scause = (htinst == INSN_PSEUDO_VS_LOAD) ? CAUSE_LOAD_ACCESS : CAUSE_STORE_ACCESS, .sepc = regs->sepc, .stval = csr_read(CSR_STVAL), };
Quoted 7 lines
Quoted 3 lines
riscv_trap_redirect(&utrap); return; }That's not what would happen on bare hardware though, aiui. At least I don't think I ever found it being spelled out anywhere what the supposed behavior is when a page table resides in unpopulated space.
What do you mean here by "unpopulated space"? IIUC, it means that to have things working we should have GVA -> GPA mapping, if there is no such mapping then correspondent access bits aren't set and so a page-fault exception corresponding to the original access type. So not access fault should be here but just correspondent page fault.
If GPA itself is incorrect (it isn't mapped in G-stage) then it looks to me that access fault should be generated. But in this case I think we won't be here (in handle_guest_page_fault() at all) as just a page fault will be generated (so G-stage fault), not guest page fault (VS-fault what is the case in the code above but as I told in prev paragraph access fault is too much in that case and just page fault will be enough and it looks like it is correspond to hardware behavior).
Quoted 17 lines
Quoted 17 lines
Quoted 16 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void)
Quoted 8 lines
xen/arch/riscv/traps.c +7
-0+ *insn =riscv_vcpu_unpriv_read (true, regs->sepc, &utrap); +if ( utrap.scause ) + { +/* +* A G-stage fault here would mean the P2M mapping of the page +* containing the trapped instruction disappeared after it was +* fetched. Does it? What about, again, faults from VS-stage address translation while hardware was trying to fetch an insn? That is ...
Review static always_inline unsigned long get_faulting_gpa(void) · line 255
Quoted 2 lines
(quoted diff) +1
-0Nothing removes P2M mappings of a running domain yet, + * so this cannot happen.... the necessary P2M mapping may never have been there.
Quoted 8 lines
Quoted 2 lines
If VS-stage failed then
CAUSE_LOAD_PAGE_FAULTwill happen soBUG_ON()won't occur and it will be passed to guest to handle it.Are you sure? So far it was my understanding that
CAUSE_LOAD_PAGE_FAULTwould happen when VS-stage translation hits e.g. a non-present leaf entry. But got an address translation failure while doing the VS-stage page walk (i.e. failure to translate the address found in a VS-stage PTE to a host address) would raiseCAUSE_LOAD_GUEST_PAGE_FAULT.
Sorry, you are right. Then what I suggested before instead of BUG_ON() will be enough:
if ( is_load_guest_page_fault(utrap.scause) )
utrap.scause = CAUSE_FETCH_ACCESS;
as if we don't have mapping in G-stage then it looks like guest is trying to reach something wrong.
Quoted 50 lines
Quoted 6 lines
BUG_ON()here catchesCAUSE_LOAD_GUEST_PAGE_FAULT(G-stage translation failure).Also, as I mentioned above I will change
BUG_ON()too:Quoted 7 lines
/*
* If during getting of trapped instruction a fault happen in
* G-stage translation thenCAUSE_LOAD_GUEST_PAGE_FAULTis
* generated. Such faults during this operation is
considered as
* bus
*/What is "bus" here (dym "bug"?), and why is the sentence unfinished?
Quoted 16 lines
Quoted 9 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 262
Quoted 6 lines
xen/arch/riscv/traps.c +6
-0+* TODO: Revisit once P2M mappings can be removed at runtime. +*/ +BUG_ON (is_load_guest_page_fault (utrap.scause)); + + utrap.sepc = regs->sepc; + utrap.stval = utrap.sepc;How do you know the fault was at .sepc? A 32-bit insn crossing a page boundary (implying the C extension is available) may well fault only on its higher half.
According to the spec, if stval is written with a nonzero value when an instruction access-fault or page-fault exception occurs on a system with variable-length instructions, then stval will contain the virtual address of the portion of the instruction that caused the fault, while sepc will point to the beginning of the instruction.
Quoted 3 lines
So here, we are trying to emulate what real hardware will do in this case. In
regs->sepc, we have the start of the instruction that we didn't touch. sepc is filled according to the spec in this case.Right, but utrap.stval is set to the same value, which is explicitly not in line with what you say above ("will contain the virtual address of the portion of the instruction that caused the fault").>
Quoted 11 lines
Regarding utrap.stval, we know that utrap.sepc points to the correct part of the faulting address, as we are reading the instruction in 16-bit chunks:
HLVX_HU(%[val], %[addr]); low 16 bits from sepc
andi %[tmp], %[val], 3
addi %[tmp], %[tmp], -3bne %[tmp], zero, 2f ; if not (insn & 3) == 3 -> 16-bit, end addi %[addr], %[addr], 2 ; <- addr is now sepc+2 HLVX_HU(%[tmp], %[addr]) ; high 16 bits, possibly from another page
Quoted 8 lines
Quoted 5 lines
So, if a trap happens while reading the high 16 bits (which may be located on another page), then utrap.sepc, if the read fails, will point to the high part of the instruction, which is what the spec requires.
Does that make sense?
Not really, no. As said above - the code as written guarantees utrap.stval == utrap.sepc, and that cannot always be correct.
Oh, right, utrap.stval = utrap.sepc; should be just dropped utrap.stval already has a correct value (from ex_handler_trap_info()) which should be passed to guest.
Quoted 29 lines
Quoted 29 lines
Quoted 28 lines
Quoted 15 lines
xen/arch/riscv/traps.c +14
-0+/* +* Check alignment and dispatch a decoded MMIO access to a registered +* handler. On success (0), info->data holds the read value for loads. +*/ +static int do_mmio (mmio_info_t *info,unsigned long fault_addr, +unsigned int len) +{ +/* Fault address should be aligned to length of MMIO */ +if ( fault_addr & (len -1 ) ) +return -EIO ; + + info->gpa = fault_addr; + info->len = len; +xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 298
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+switch (try_handle_mmio (info) ) + { +case IO_HANDLED : +return 0 ; +case IO_ABORT : +return -EIO ; +default : +return -EOPNOTSUPP ; + } +}And there's no indication of "retry needed", e.g. when something changed between
find_mmio_handler()and handle_{read,write}()?
Quoted 8 lines
Quoted 5 lines
I don't have any specific scenario where it is needed now so I don't know what to say.
And there is no race betweenfind_mmio_handler()and handle_{read,write}() asfind_mmio_handler()returns copy of the structure underread_lock():Oh, right, but that's not visible here at all and requires going back to patch 04 to realize.
I will update the comment above function:
/*
* Check alignment and dispatch a decoded MMIO access to a registered
* handler. On success (0), info->data holds the read value for loads. *
* There is no "retry" outcome to handle: find_mmio_handler() returns a
* copy of the matching handler taken under vmmio->lock and the ops
* structures are never freed, so the lookup result cannot go stale
* between finding the handler and invoking it.
*/
Quoted 50 lines
Quoted 50 lines
Quoted 39 lines
Quoted 21 lines
xen/arch/riscv/traps.c +17
-1static int emulate_load (unsigned long fault_addr,unsigned long htinst) { -return -EOPNOTSUPP ; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +mmio_info_t info = { .is_write = false }; +unsigned long insn; +unsigned int shift =0 , len, insn_len; +bool is_unsigned = false; +int rc; + +if (decode_trapped_insn (htinst, &insn, &insn_len) ) +return 0 ; + +/* Decode length of MMIO and whether it is a sign- or zero-extending load */ +if ( (insn &INSN_MASK_LB ) ==INSN_MATCH_LB ) + len =1 ; +else if ( (insn &INSN_MASK_LBU ) ==INSN_MATCH_LBU ) + { + len =1 ; + is_unsigned = true;xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 328
Quoted 10 lines
xen/arch/riscv/traps.c +10
-0+ } +else if ( (insn &INSN_MASK_LH ) ==INSN_MATCH_LH ) + len =2 ; +else if ( (insn &INSN_MASK_LHU ) ==INSN_MATCH_LHU ) + { + len =2 ; + is_unsigned = true; + } +else if ( (insn &INSN_MASK_LW ) ==INSN_MATCH_LW ) + len =4 ;Already up to here this demonstrates a weakness of the INSN_MASK_* set of #define-s (which I similarly observe in binutils, and I expect it all has the same questionable origin). All INSN_MASK_L* and INSN_MASK_FL* (also INSN_MASK_S* and INSN_MASK_FS*) are identical, allowing for a nice
switch()to be used here in principle. That said, with access width nicely encoded in FUNCT3, it's not even clear whether aswitch()would end up being needed / efficient........
Quoted 2 lines
Otoh none of these masks cover the pseudoinsns that htinst may supply.
As I answered above we should handle that before this function will call so here we won't deal with htinst at all. Of course, if what I wrote above is correct. I will double check before applying that.
Quoted 1 line
Quoted 6 lines
Quoted 4 lines
Quoted 2 lines
Further, what about A-extension insns? Some (if not all) of them can plausibly be used on MMIO, I think.
I’m not really sure that the A-extension is actively used for MMIO.
Does the spec preclude their use? I'm unaware of such a restriction.
Definitely no. In this case hypervisor will tell that we can't emulate this instruction and then extra handling should be added.
Quoted 8 lines
Quoted 4 lines
At
least, Linux doesn’t do that for now, which is why we don’t handle A-extension instructions here.Focusing on what present Linux needs is okay, but then remaining gaps should (as said on various other occasions before) be clearly marked.
Quoted 6 lines
Quoted 3 lines
I think this is related to the fact that MMIO is usually (if not always?) naturally aligned, and naturally aligned loads and stores are guaranteed by RISC-V to execute atomically.
How does this matter, when a bit or field in MMIO may serve the purpose of e.g. a semaphore?
Then yes it will be an issue and such instruction should be emulated (when such use cases will come into play)
Quoted 35 lines
Quoted 35 lines
Quoted 23 lines
Quoted 23 lines
xen/arch/riscv/traps.c +22
-0+ { + len =4 ; + is_unsigned = true; + } +#endif + else if ( (insn & INSN_MASK_C_LW) == INSN_MATCH_C_LW ) + { + len =4 ; + insn =RVC_RS2S (insn) <<SH_RD ; + } + else if ( (insn & INSN_MASK_C_LWSP) == INSN_MATCH_C_LWSP && + RV_X(insn, SH_RD, 5) ) + len = 4; +#ifndef CONFIG_RISCV_32 + else if ( (insn & INSN_MASK_LD) == INSN_MATCH_LD ) + len = 8; + else if ( (insn & INSN_MASK_C_LD) == INSN_MATCH_C_LD ) + { + len =8 ; + insn =RVC_RS2S (insn) <<SH_RD ; + } + else if ( (insn & INSN_MASK_C_LDSP) == INSN_MATCH_C_LDSP &&Quoted 8 lines
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 357
Quoted 5 lines
xen/arch/riscv/traps.c +5
-0+ RV_X(insn, SH_RD, 5) ) + len =8 ; +#endif + else + return -EOPNOTSUPP;Because you don't permit F/D/Q for guests (yet), FL* and FS* aren't covered, I expect?
At the moment, I wrote this function with handling of MMIO instruction in mind, which are at the moment ld and sd.
Quoted 7 lines
Quoted 3 lines
Even if to permit F/D/Q then do we really need to trap that instructions? Hypervisor could allow access to FPU to guest and then it will be just a question of context switch to properly save and restore FPU.
And how would you know FPU loads/stores aren't used against MMIO? Later on, once V support is added, even its loads/stores might be used that way. Think of video frame buffer accesses, for example.
We will get 'return -EOPNOTSUPP' and so guest will be crashed because we don't support such work with MMIO.
And then yes it should be likely to be added in parallel with adding F/D/Q support for guest. At the moment, KVM supports, for example, F/D/Q but doesn't emulate FPU load/store but I agree that with your example it could happen.
~ Oleksii
Re: [PATCH v1 16/17] xen/riscv: add guest load emulation for trapped MMIO accesses
Quoted 50 lines · Oleksii Kurochko
Quoted 49 lines · Jan Beulich
Quoted 47 lines · Oleksii Kurochko
Quoted 9 lines · Jan Beulich
xen/arch/riscv/traps.c static always_inline unsigned long get_faulting_gpa(void) · line 247
Quoted 4 lines · Oleksii Kurochko
xen/arch/riscv/traps.c +4
-0+/* +* Bit[0] == 0 implies trapped instruction value is +* zero or special value. +*/ How come you get away without dealing with pseudoinsns? The insn pointed at by
regs->sepcis of no interest for faults caused by implicit memory accesses originating from VS-stage address translation.It is really problem but I think it should be resolved much earlier in
handle_guest_page_fault(). I will add the following:/*
* A guest page fault taken on an implicit memory access performed for
* VS-stage address translation (reading a PTE, or updating its A/D
bits)
* reports a pseudoinstruction in htinst rather than a transformed
* instruction. Such a fault can't be emulated: htval holds the guest
* physical address of a VS-stage PTE rather than of any access the
guest
* itself performed (and its two least significant bits are zero
instead
* of matching stval), while the instruction at sepc is unrelated
to the
* access which actually faulted.
*
* Report an access fault to the guest at the original virtual address,
* which is what stval already holds and what hardware would raise
for a* page table walk hitting an inaccessible address. */ if ( (htinst == INSN_PSEUDO_VS_LOAD) || (htinst == INSN_PSEUDO_VS_STORE) ) { struct cpu_user_regs *regs = vcpu_guest_cpu_user_regs(current); struct trap_info utrap = { .scause = (htinst == INSN_PSEUDO_VS_LOAD) ? CAUSE_LOAD_ACCESS : CAUSE_STORE_ACCESS, .sepc = regs->sepc, .stval = csr_read(CSR_STVAL), }; riscv_trap_redirect(&utrap); return; }
Quoted 5 lines
Quoted 3 lines
That's not what would happen on bare hardware though, aiui. At least I don't think I ever found it being spelled out anywhere what the supposed behavior is when a page table resides in unpopulated space.
What do you mean here by "unpopulated space"?
A physical address range neither populated by RAM nor used by MMIO of any device.
Jan
PATCH 17/17 xen/riscv: add guest store emulation for trapped MMIO accesses
In reply to Oleksii KurochkoExtend the guest page fault handler with store emulation to support MMIO write accesses.
The instruction decode mirrors emulate_load() and, like it, is adapted from Linux's KVM RISC-V implementation. As with the load path, the completion is synchronous through try_handle_mmio() rather than KVM's userspace exit/return split, since Xen's MMIO handlers run in the hypervisor. Faults taken while re-reading the trapped instruction are handled by decode_trapped_insn(), shared with the load path.
When a guest store instruction faults, the trapped instruction is decoded using HTINST or, if unavailable, fetched via unprivileged access. At the moment only virtual interrupt controller (vINTC) traps are expected to occur, since it is currently the only backend registered with the MMIO handler dispatch, so in practice the store is emulated via the vINTC backend.
Together with load emulation, this completes the basic MMIO handling path needed for virtual interrupt controller support on RISC-V.
--- xen/arch/riscv/traps.c | 55 +++++++++++++++++++++++++++++++++++++++++- 1 file changed, 54 insertions(+), 1 deletion(-)
xen/arch/riscv/traps.c +54 -1
@@ -377,7 +377,60 @@ static int emulate_load(unsigned long fault_addr, unsigned long htinst)static int emulate_store (unsigned long fault_addr,unsigned long htinst) { -return -EOPNOTSUPP ; +struct cpu_user_regs *regs =vcpu_guest_cpu_user_regs (current); +mmio_info_t info = { .is_write = true }; +register_t data; +unsigned long insn; +unsigned int len, insn_len; +int rc; + +if (decode_trapped_insn (htinst, &insn, &insn_len) ) +return 0 ; + + data =GET_RS2 (insn, regs); + +if ( (insn &INSN_MASK_SB ) ==INSN_MATCH_SB ) + len =1 ; +else if ( (insn &INSN_MASK_SH ) ==INSN_MATCH_SH ) + len =2 ; +else if ( (insn &INSN_MASK_SW ) ==INSN_MATCH_SW ) + len =4 ; +else if ( (insn &INSN_MASK_C_SW ) ==INSN_MATCH_C_SW ) + { + len =4 ; + data =GET_RS2S (insn, regs); + } +else if ( (insn &INSN_MASK_C_SWSP ) ==INSN_MATCH_C_SWSP ) + { + len =4 ; + data =GET_RS2C (insn, regs); + } +#ifndef CONFIG_RISCV_32 + else if ( (insn & INSN_MASK_SD) == INSN_MATCH_SD ) + len = 8; + else if ( (insn & INSN_MASK_C_SD) == INSN_MATCH_C_SD ) + { + len =8 ; + data =GET_RS2S (insn, regs); + } + else if ( (insn & INSN_MASK_C_SDSP) == INSN_MATCH_C_SDSP ) + { + len =8 ; + data =GET_RS2C (insn, regs); + } +#endif + else + return -EOPNOTSUPP; + + info.data = data; + + rc =do_mmio (&info, fault_addr, len); +if ( rc ) +return rc; + +advance_pc (regs, insn_len); + +return 0 ; } static void handle_guest_page_fault(unsigned long cause,
Signature · 1 line
2.54.0