Implements RISC-V WorldGuard extension v0.4
Subsystems (2)
cover Implements RISC-V WorldGuard extension v0.4
This patchset implements Smwg/Smwgd/Sswg CPU extension and wgChecker device defined in WorldGuard spec v0.4.
The WG v0.4 spec could be found here:
https://lists.riscv.org/g/security/attachment/711/0/worldguard_rvia_spec-v0.4.pdf
To enable WG in QEMU, pass "wg=on" as machine parameter to virt machine. It enables both WG CPU CSRs to apply WID of CPU and wgCheckers on the DRAM, FLASH, and UART to protect these resources.
This patchset contains 5 parts:
1. Commit 1: Bugfix of IOMMUMemoryRegion 2. Commit 2 ~ 3: Extend IOMMUMemoryRegion and MemTxAttr for WG support 3. Commit 4 ~ 11: Add WG global device and CPU extensions
4. Commit 12 ~ 15: Add WG checker device
5. Commit 16: Add WG support to the virt machine
Jim Shu (16):
accel/tcg: Store section pointer in CPUTLBEntryFull accel/tcg: memory access from CPU will pass access_type to IOMMU
exec: Add RISC-V WorldGuard WID to MemTxAttrs
hw/misc: riscv_worldguard: Add RISC-V WorldGuard global config target/riscv: Add CPU options of WorldGuard CPU extension target/riscv: Add hard-coded CPU state of WG extension target/riscv: Add defines for WorldGuard CSRs target/riscv: Allow global WG config to set WG CPU callbacks target/riscv: Implement WorldGuard CSRs
target/riscv: Add WID to MemTxAttrs of CPU memory transactions hw/misc: riscv_worldguard: Add API to enable WG extension of CPU hw/misc: riscv_wgchecker: Implement RISC-V WorldGuard Checker hw/misc: riscv_wgchecker: Implement wgchecker slot registers hw/misc: riscv_wgchecker: Implement correct block-access behavior hw/misc: riscv_wgchecker: Check the slot settings in translate hw/riscv: virt: Add WorldGuard support
accel/tcg/cputlb.c | 34 +- docs/system/riscv/virt.rst | 10 + hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/riscv_wgchecker.c | 1161 ++++++++++++++++++++++++++ hw/misc/riscv_worldguard.c | 273 ++++++ hw/misc/trace-events | 9 + hw/riscv/Kconfig | 1 + hw/riscv/virt.c | 163 +++- include/exec/exec-all.h | 11 +- include/exec/memattrs.h | 5 + include/hw/core/cpu.h | 3 + include/hw/misc/riscv_worldguard.h | 123 +++ include/hw/riscv/virt.h | 17 +- system/physmem.c | 16 +- target/alpha/helper.c | 2 +- target/arm/tcg/tlb_helper.c | 2 +- target/avr/helper.c | 2 +- target/cris/helper.c | 2 +- target/hppa/mem_helper.c | 2 +- target/i386/tcg/sysemu/excp_helper.c | 3 +- target/loongarch/tcg/tlb_helper.c | 2 +- target/m68k/helper.c | 10 +- target/microblaze/helper.c | 8 +- target/mips/tcg/sysemu/tlb_helper.c | 4 +- target/openrisc/mmu.c | 2 +- target/ppc/mmu_helper.c | 2 +- target/riscv/cpu.c | 16 +- target/riscv/cpu.h | 12 + target/riscv/cpu_bits.h | 5 + target/riscv/cpu_cfg.h | 5 + target/riscv/cpu_helper.c | 69 +- target/riscv/csr.c | 107 +++ target/riscv/tcg/tcg-cpu.c | 11 + target/rx/cpu.c | 3 +- target/s390x/tcg/excp_helper.c | 2 +- target/sh4/helper.c | 2 +- target/sparc/mmu_helper.c | 6 +- target/tricore/helper.c | 2 +- target/xtensa/helper.c | 3 +- 40 files changed, 2054 insertions(+), 60 deletions(-) create mode 100644 hw/misc/riscv_wgchecker.c create mode 100644 hw/misc/riscv_worldguard.c create mode 100644 include/hw/misc/riscv_worldguard.h
Signature · 1 line
2.17.1
RFC PATCH 1/16 accel/tcg: Store section pointer in CPUTLBEntryFull
'CPUTLBEntryFull.xlat_section' stores section_index in last 12 bits to find the correct section when CPU access the IO region over the IOTLB (iotlb_to_section()).
However, section_index is only unique inside single AddressSpace. If address space translation is over IOMMUMemoryRegion, it could return section from other AddressSpace. 'iotlb_to_section()' API only finds the sections from CPU's AddressSpace so that it couldn't find section in other AddressSpace. Thus, using 'iotlb_to_section()' API will find the wrong section and QEMU will have wrong load/store access.
To fix this bug, store complete MemoryRegionSection pointer in CPUTLBEntryFull instead of section_index.
This bug occurs only when
(1) IOMMUMemoryRegion is in the path of CPU access.
(2) IOMMUMemoryRegion returns different target_as and the section is in the IO region.
Common IOMMU devices don't have this issue since they are only in the path of DMA access. Currently, the bug only occurs when ARM MPC device (hw/misc/tz-mpc.c) returns 'blocked_io_as' to emulate blocked access handling. Upcoming RISC-V wgChecker device is also affected by this bug.
--- accel/tcg/cputlb.c | 19 +++++++++---------- include/hw/core/cpu.h | 3 +++ 2 files changed, 12 insertions(+), 10 deletions(-)
accel/tcg/cputlb.c +9 -10
@@ -1169,6 +1169,7 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, desc->fulltlb[index] = *full; full = &desc->fulltlb[index]; full->xlat_section = iotlb - addr_page; + full->section = section; full->phys_addr = paddr_page;/* Now calculate the new entry */
@@ -1248,14 +1249,14 @@ static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr, }static MemoryRegionSection * -io_prepare (hwaddr *out_offset,CPUState *cpu,hwaddr xlat, +io_prepare (hwaddr *out_offset,CPUState *cpu,CPUTLBEntryFull *full,MemTxAttrs attrs,vaddr addr,uintptr_t retaddr) {MemoryRegionSection *section;hwaddr mr_offset; - section =iotlb_to_section (cpu, xlat, attrs); - mr_offset = (xlat &TARGET_PAGE_MASK ) + addr; + section = full->section; + mr_offset = (full->xlat_section &TARGET_PAGE_MASK ) + addr; cpu->mem_io_pc = retaddr;if (!cpu->neg.can_do_io) {cpu_io_recompile (cpu, retaddr);
@@ -1571,9 +1572,7 @@ bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,/* We must have an iotlb entry for MMIO */ if (tlb_addr &TLB_MMIO ) { -MemoryRegionSection *section = -iotlb_to_section (cpu, full->xlat_section & ~TARGET_PAGE_MASK , - full->attrs); +MemoryRegionSection *section = full->section; data->is_io = true; data->mr = section->mr; }else {
@@ -1972,7 +1971,7 @@ static uint64_t do_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >0 && size <=8 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();
@@ -1993,7 +1992,7 @@ static Int128 do_ld16_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >8 && size <=16 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();
@@ -2513,7 +2512,7 @@ static uint64_t do_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >0 && size <=8 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();
@@ -2533,7 +2532,7 @@ static uint64_t do_st16_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >8 && size <=16 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();
include/hw/core/cpu.h +3 -0
@@ -217,6 +217,9 @@ typedef struct CPUTLBEntryFull {*/ hwaddr xlat_section; +/* @section contains physical section. */ +MemoryRegionSection *section; +/* * @phys_addr contains the physical address in the address space * given by cpu_asidx_from_attrs(cpu, @attrs).
Signature · 1 line
2.17.1
Re: [RFC PATCH 01/16] accel/tcg: Store section pointer in CPUTLBEntryFull
Quoted 11 lines · Jim Shu
'CPUTLBEntryFull.xlat_section' stores
section_indexin last 12 bits to find the correct section when CPU access the IO region over the IOTLB (iotlb_to_section()).However,
section_indexis only unique inside single AddressSpace. If address space translation is over IOMMUMemoryRegion, it could return section from other AddressSpace. 'iotlb_to_section()' API only finds the sections from CPU's AddressSpace so that it couldn't find section in other AddressSpace. Thus, using 'iotlb_to_section()' API will find the wrong section and QEMU will have wrong load/store access.
Quoted 5 lines
To fix this bug, store complete MemoryRegionSection pointer in CPUTLBEntryFull instead of
section_index.This bug occurs only when
(1) IOMMUMemoryRegion is in the path of CPU access.
Hi Jim,
Can you explain a little more on when IOMMUMemoryRegion is in the path of CPU access?
Thanks,
Zhiwei
Quoted 105 lines
(2) IOMMUMemoryRegion returns different
target_asand the section is in the IO region.Common IOMMU devices don't have this issue since they are only in the path of DMA access. Currently, the bug only occurs when ARM MPC device (
hw/misc/tz-mpc.c) returns 'blocked_io_as' to emulate blocked access handling. Upcoming RISC-V wgChecker device is also affected by this bug.Signed-off-by Jim Shu <[email protected]>--- accel/tcg/cputlb.c | 19 +++++++++---------- include/hw/core/cpu.h | 3 +++ 2 files changed, 12 insertions(+), 10 deletions(-)accel/tcg/cputlb.c +9
-10@@ -1169,6 +1169,7 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, desc->fulltlb[index] = *full; full = &desc->fulltlb[index]; full->xlat_section = iotlb - addr_page; + full->section = section; full->phys_addr = paddr_page;/* Now calculate the new entry */ @@ -1248,14 +1249,14 @@ static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr, }static MemoryRegionSection * -io_prepare (hwaddr *out_offset,CPUState *cpu,hwaddr xlat, +io_prepare (hwaddr *out_offset,CPUState *cpu,CPUTLBEntryFull *full,MemTxAttrs attrs,vaddr addr,uintptr_t retaddr) {MemoryRegionSection *section;hwaddr mr_offset; - section =iotlb_to_section (cpu, xlat, attrs); - mr_offset = (xlat &TARGET_PAGE_MASK ) + addr; + section = full->section; + mr_offset = (full->xlat_section &TARGET_PAGE_MASK ) + addr; cpu->mem_io_pc = retaddr;if (!cpu->neg.can_do_io) {cpu_io_recompile (cpu, retaddr);@@ -1571,9 +1572,7 @@ bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,/* We must have an iotlb entry for MMIO */ if (tlb_addr &TLB_MMIO ) { -MemoryRegionSection *section = -iotlb_to_section (cpu, full->xlat_section & ~TARGET_PAGE_MASK , - full->attrs); +MemoryRegionSection *section = full->section; data->is_io = true; data->mr = section->mr; }else {@@ -1972,7 +1971,7 @@ static uint64_t do_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >0 && size <=8 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -1993,7 +1992,7 @@ static Int128 do_ld16_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >8 && size <=16 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -2513,7 +2512,7 @@ static uint64_t do_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >0 && size <=8 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -2533,7 +2532,7 @@ static uint64_t do_st16_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >8 && size <=16 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();include/hw/core/cpu.h +3
-0@@ -217,6 +217,9 @@ typedef struct CPUTLBEntryFull {*/ hwaddr xlat_section; +/* @section contains physical section. */ +MemoryRegionSection *section; +/* * @phys_addr contains the physical address in the address space * given by cpu_asidx_from_attrs(cpu, @attrs).
Re: [RFC PATCH 01/16] accel/tcg: Store section pointer in CPUTLBEntryFull
Hi Zhiwei,
Common IOMMU devices will not have IOMMUMemoryRegion in the path of CPU access since It only affects DMA access.
In QEMU, it usually places this IOMMU MR as the parent of "system_memory", and changes the target_mr of DMA from "system_memory" to IOMMU MR.
For the wgChecker, it is in front of memory or device MMIO and protects both CPU/DMA access to memory or device MMIO.
In QEMU, wgChecker re-use IOMMUMemoryRegion to implement the memory protection inside the translate() function of IOMMU MR.
In the machine code, wgChecker replaces the MemoryRegion of protected resources with the checker's IOMMU MR in the MemoryRegion tree of "system_memory".
Both CPU/DMA access will go through the "system_memory". They will go through the checker's IOMMU MR when accessing the protected resources.
This mechanism is used by Cortex-M MPC devices (hw/misc/tz-mpc.c) originally. I have leveraged it and extended it little (in patch 2) as MPC doesn't support RO/WO permission.
If we'd like to have a device to do the memory protection of both CPU & DMA access, we could implement it in this mechanism. (p.s. Cortex-A TZASC is not supported in QEMU, which is similar to MPC or wgChecker device.)
Thanks,
Jim Shu
Quoted 132 lines · LIU Zhiwei
Quoted 11 lines · Jim Shu
'CPUTLBEntryFull.xlat_section' stores
section_indexin last 12 bits to find the correct section when CPU access the IO region over the IOTLB (iotlb_to_section()).However,
section_indexis only unique inside single AddressSpace. If address space translation is over IOMMUMemoryRegion, it could return section from other AddressSpace. 'iotlb_to_section()' API only finds the sections from CPU's AddressSpace so that it couldn't find section in other AddressSpace. Thus, using 'iotlb_to_section()' API will find the wrong section and QEMU will have wrong load/store access.Quoted 5 lines
To fix this bug, store complete MemoryRegionSection pointer in CPUTLBEntryFull instead of
section_index.This bug occurs only when
(1) IOMMUMemoryRegion is in the path of CPU access.Hi Jim,
Can you explain a little more on when IOMMUMemoryRegion is in the path of CPU access?
Thanks,
ZhiweiQuoted 105 lines
(2) IOMMUMemoryRegion returns different
target_asand the section is in the IO region.Common IOMMU devices don't have this issue since they are only in the path of DMA access. Currently, the bug only occurs when ARM MPC device (
hw/misc/tz-mpc.c) returns 'blocked_io_as' to emulate blocked access handling. Upcoming RISC-V wgChecker device is also affected by this bug.Signed-off-by Jim Shu <[email protected]>--- accel/tcg/cputlb.c | 19 +++++++++---------- include/hw/core/cpu.h | 3 +++ 2 files changed, 12 insertions(+), 10 deletions(-)accel/tcg/cputlb.c +9
-10@@ -1169,6 +1169,7 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, desc->fulltlb[index] = *full; full = &desc->fulltlb[index]; full->xlat_section = iotlb - addr_page; + full->section = section; full->phys_addr = paddr_page;/* Now calculate the new entry */ @@ -1248,14 +1249,14 @@ static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr, }static MemoryRegionSection * -io_prepare (hwaddr *out_offset,CPUState *cpu,hwaddr xlat, +io_prepare (hwaddr *out_offset,CPUState *cpu,CPUTLBEntryFull *full,MemTxAttrs attrs,vaddr addr,uintptr_t retaddr) {MemoryRegionSection *section;hwaddr mr_offset; - section =iotlb_to_section (cpu, xlat, attrs); - mr_offset = (xlat &TARGET_PAGE_MASK ) + addr; + section = full->section; + mr_offset = (full->xlat_section &TARGET_PAGE_MASK ) + addr; cpu->mem_io_pc = retaddr;if (!cpu->neg.can_do_io) {cpu_io_recompile (cpu, retaddr);@@ -1571,9 +1572,7 @@ bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,/* We must have an iotlb entry for MMIO */ if (tlb_addr &TLB_MMIO ) { -MemoryRegionSection *section = -iotlb_to_section (cpu, full->xlat_section & ~TARGET_PAGE_MASK , - full->attrs); +MemoryRegionSection *section = full->section; data->is_io = true; data->mr = section->mr; }else {@@ -1972,7 +1971,7 @@ static uint64_t do_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >0 && size <=8 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -1993,7 +1992,7 @@ static Int128 do_ld16_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >8 && size <=16 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -2513,7 +2512,7 @@ static uint64_t do_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >0 && size <=8 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -2533,7 +2532,7 @@ static uint64_t do_st16_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >8 && size <=16 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();include/hw/core/cpu.h +3
-0@@ -217,6 +217,9 @@ typedef struct CPUTLBEntryFull {*/ hwaddr xlat_section; +/* @section contains physical section. */ +MemoryRegionSection *section; +/* * @phys_addr contains the physical address in the address space * given by cpu_asidx_from_attrs(cpu, @attrs).
Re: [RFC PATCH 01/16] accel/tcg: Store section pointer in CPUTLBEntryFull
Quoted 8 lines · Jim Shu
Hi Zhiwei,
Common IOMMU devices will not have IOMMUMemoryRegion in the path of CPU access since It only affects DMA access.
In QEMU, it usually places this IOMMU MR as the parent of "system_memory", and changes thetarget_mrof DMA from "system_memory" to IOMMU MR.
Quoted 9 lines
For the wgChecker, it is in front of memory or device MMIO and protects both CPU/DMA access to memory or device MMIO.
In QEMU, wgChecker re-use IOMMUMemoryRegion to implement the memory protection inside thetranslate()function of IOMMU MR.
In the machine code, wgChecker replaces the MemoryRegion of protected resources with the checker's IOMMU MR in the MemoryRegion tree of "system_memory".
Both CPU/DMA access will go through the "system_memory". They will go through the checker's IOMMU MR when accessing the protected resources.
Thanks. It is clear and very helpful.
Zhiwei
Quoted 147 lines
This mechanism is used by Cortex-M MPC devices (
hw/misc/tz-mpc.c) originally. I have leveraged it and extended it little (in patch 2) as MPC doesn't support RO/WO permission.
If we'd like to have a device to do the memory protection of both CPU & DMA access, we could implement it in this mechanism. (p.s. Cortex-A TZASC is not supported in QEMU, which is similar to MPC or wgChecker device.)Thanks,
Jim ShuQuoted 130 lines · LIU Zhiwei
Quoted 11 lines · Jim Shu
'CPUTLBEntryFull.xlat_section' stores
section_indexin last 12 bits to find the correct section when CPU access the IO region over the IOTLB (iotlb_to_section()).However,
section_indexis only unique inside single AddressSpace. If address space translation is over IOMMUMemoryRegion, it could return section from other AddressSpace. 'iotlb_to_section()' API only finds the sections from CPU's AddressSpace so that it couldn't find section in other AddressSpace. Thus, using 'iotlb_to_section()' API will find the wrong section and QEMU will have wrong load/store access.Quoted 5 lines
To fix this bug, store complete MemoryRegionSection pointer in CPUTLBEntryFull instead of
section_index.This bug occurs only when
(1) IOMMUMemoryRegion is in the path of CPU access.Hi Jim,
Can you explain a little more on when IOMMUMemoryRegion is in the path of CPU access?
Thanks,
ZhiweiQuoted 105 lines
(2) IOMMUMemoryRegion returns different
target_asand the section is in the IO region.Common IOMMU devices don't have this issue since they are only in the path of DMA access. Currently, the bug only occurs when ARM MPC device (
hw/misc/tz-mpc.c) returns 'blocked_io_as' to emulate blocked access handling. Upcoming RISC-V wgChecker device is also affected by this bug.Signed-off-by Jim Shu <[email protected]>--- accel/tcg/cputlb.c | 19 +++++++++---------- include/hw/core/cpu.h | 3 +++ 2 files changed, 12 insertions(+), 10 deletions(-)accel/tcg/cputlb.c +9
-10@@ -1169,6 +1169,7 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, desc->fulltlb[index] = *full; full = &desc->fulltlb[index]; full->xlat_section = iotlb - addr_page; + full->section = section; full->phys_addr = paddr_page;/* Now calculate the new entry */ @@ -1248,14 +1249,14 @@ static inline void cpu_unaligned_access(CPUState *cpu, vaddr addr, }static MemoryRegionSection * -io_prepare (hwaddr *out_offset,CPUState *cpu,hwaddr xlat, +io_prepare (hwaddr *out_offset,CPUState *cpu,CPUTLBEntryFull *full,MemTxAttrs attrs,vaddr addr,uintptr_t retaddr) {MemoryRegionSection *section;hwaddr mr_offset; - section =iotlb_to_section (cpu, xlat, attrs); - mr_offset = (xlat &TARGET_PAGE_MASK ) + addr; + section = full->section; + mr_offset = (full->xlat_section &TARGET_PAGE_MASK ) + addr; cpu->mem_io_pc = retaddr;if (!cpu->neg.can_do_io) {cpu_io_recompile (cpu, retaddr);@@ -1571,9 +1572,7 @@ bool tlb_plugin_lookup(CPUState *cpu, vaddr addr, int mmu_idx,/* We must have an iotlb entry for MMIO */ if (tlb_addr &TLB_MMIO ) { -MemoryRegionSection *section = -iotlb_to_section (cpu, full->xlat_section & ~TARGET_PAGE_MASK , - full->attrs); +MemoryRegionSection *section = full->section; data->is_io = true; data->mr = section->mr; }else {@@ -1972,7 +1971,7 @@ static uint64_t do_ld_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >0 && size <=8 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -1993,7 +1992,7 @@ static Int128 do_ld16_mmio_beN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >8 && size <=16 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -2513,7 +2512,7 @@ static uint64_t do_st_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >0 && size <=8 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();@@ -2533,7 +2532,7 @@ static uint64_t do_st16_mmio_leN(CPUState *cpu, CPUTLBEntryFull *full,tcg_debug_assert (size >8 && size <=16 ); attrs = full->attrs; - section =io_prepare (&mr_offset, cpu, full->xlat_section, attrs, addr, ra); + section =io_prepare (&mr_offset, cpu, full, attrs, addr, ra); mr = section->mr;BQL_LOCK_GUARD ();include/hw/core/cpu.h +3
-0@@ -217,6 +217,9 @@ typedef struct CPUTLBEntryFull {*/ hwaddr xlat_section; +/* @section contains physical section. */ +MemoryRegionSection *section; +/* * @phys_addr contains the physical address in the address space * given by cpu_asidx_from_attrs(cpu, @attrs).
RFC PATCH 2/16 accel/tcg: memory access from CPU will pass access_type to IOMMU
In reply to Jim ShuIt is the preparation patch for upcoming RISC-V wgChecker device.
Since RISC-V wgChecker could permit access in RO/WO permission, the IOMMUMemoryRegion could return different section for read & write access. The memory access from CPU should also pass the access_type to IOMMU translate function so that IOMMU could return the correct section of specified access_type.
--- accel/tcg/cputlb.c | 15 +++++++++------ include/exec/exec-all.h | 11 +++++++---- system/physmem.c | 16 +++++++++++----- target/alpha/helper.c | 2 +- target/arm/tcg/tlb_helper.c | 2 +- target/avr/helper.c | 2 +- target/cris/helper.c | 2 +- target/hppa/mem_helper.c | 2 +- target/i386/tcg/sysemu/excp_helper.c | 3 ++- target/loongarch/tcg/tlb_helper.c | 2 +- target/m68k/helper.c | 10 +++++++--- target/microblaze/helper.c | 8 ++++---- target/mips/tcg/sysemu/tlb_helper.c | 4 ++-- target/openrisc/mmu.c | 2 +- target/ppc/mmu_helper.c | 2 +- target/riscv/cpu_helper.c | 2 +- target/rx/cpu.c | 3 ++- target/s390x/tcg/excp_helper.c | 2 +- target/sh4/helper.c | 2 +- target/sparc/mmu_helper.c | 6 +++--- target/tricore/helper.c | 2 +- target/xtensa/helper.c | 3 ++- 22 files changed, 61 insertions(+), 42 deletions(-)
accel/tcg/cputlb.c +9 -6
@@ -1036,7 +1036,8 @@ static inline void tlb_set_compare(CPUTLBEntryFull *full, CPUTLBEntry *ent,* critical section. */ void tlb_set_page_full (CPUState *cpu,int mmu_idx, -vaddr addr,CPUTLBEntryFull *full) +vaddr addr,MMUAccessType access_type, +CPUTLBEntryFull *full) {CPUTLB *tlb = &cpu->neg.tlb;CPUTLBDesc *desc = &tlb->d[mmu_idx];
@@ -1063,7 +1064,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, prot = full->prot; asidx =cpu_asidx_from_attrs (cpu, full->attrs); section =address_space_translate_for_iotlb (cpu, asidx, paddr_page, - &xlat, &sz, full->attrs, &prot); + &xlat, &sz, full->attrs, &prot, + access_type);assert (sz >=TARGET_PAGE_SIZE ); tlb_debug("vaddr=%016" VADDR_PRIx " paddr=0x" HWADDR_FMT_plx
@@ -1200,7 +1202,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx,void tlb_set_page_with_attrs (CPUState *cpu,vaddr addr,hwaddr paddr,MemTxAttrs attrs,int prot, -int mmu_idx,uint64_t size) +MMUAccessType access_type,int mmu_idx, +uint64_t size) {CPUTLBEntryFull full = { .phys_addr = paddr,
@@ -1210,15 +1213,15 @@ void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr, };assert (is_power_of_2 (size)); -tlb_set_page_full (cpu, mmu_idx, addr, &full); +tlb_set_page_full (cpu, mmu_idx, addr, access_type, &full); }void tlb_set_page (CPUState *cpu,vaddr addr, -hwaddr paddr,int prot, +hwaddr paddr,int prot,MMUAccessType access_type,int mmu_idx,uint64_t size) {tlb_set_page_with_attrs (cpu, addr, paddr,MEMTXATTRS_UNSPECIFIED , - prot, mmu_idx, size); + prot, access_type, mmu_idx, size); }/*
include/exec/exec-all.h +7 -4
@@ -205,7 +205,7 @@ void tlb_flush_range_by_mmuidx_all_cpus_synced(CPUState *cpu,* used by tlb_flush_page. */ void tlb_set_page_full (CPUState *cpu,int mmu_idx,vaddr addr, -CPUTLBEntryFull *full); +MMUAccessType access_type,CPUTLBEntryFull *full);/** * tlb_set_page_with_attrs:
@@ -231,7 +231,8 @@ void tlb_set_page_full(CPUState *cpu, int mmu_idx, vaddr addr,*/ void tlb_set_page_with_attrs (CPUState *cpu,vaddr addr,hwaddr paddr,MemTxAttrs attrs, -int prot,int mmu_idx,vaddr size); +int prot,MMUAccessType access_type,int mmu_idx, +vaddr size);/* tlb_set_page: * * This function is equivalent to calling tlb_set_page_with_attrs()
@@ -240,7 +241,8 @@ void tlb_set_page_with_attrs(CPUState *cpu, vaddr addr,*/ void tlb_set_page (CPUState *cpu,vaddr addr,hwaddr paddr,int prot, -int mmu_idx,vaddr size); +MMUAccessType access_type,int mmu_idx, +vaddr size);#else static inline void tlb_init (CPUState *cpu) {
@@ -591,7 +593,8 @@ void tlb_reset_dirty_range_all(ram_addr_t start, ram_addr_t length);MemoryRegionSection *address_space_translate_for_iotlb (CPUState *cpu,int asidx,hwaddr addr,hwaddr *xlat,hwaddr *plen, -MemTxAttrs attrs,int *prot); +MemTxAttrs attrs,int *prot, +MMUAccessType access_type);hwaddr memory_region_section_get_iotlb (CPUState *cpu,MemoryRegionSection *section);#endif
system/physmem.c +11 -5
@@ -674,12 +674,14 @@ void tcg_iommu_init_notifier_list(CPUState *cpu)MemoryRegionSection *address_space_translate_for_iotlb (CPUState *cpu,int asidx,hwaddr orig_addr,hwaddr *xlat,hwaddr *plen, -MemTxAttrs attrs,int *prot) +MemTxAttrs attrs,int *prot, +MMUAccessType access_type) {MemoryRegionSection *section;IOMMUMemoryRegion *iommu_mr;IOMMUMemoryRegionClass *imrc;IOMMUTLBEntry iotlb; +IOMMUAccessFlags iommu_flags;int iommu_idx;hwaddr addr = orig_addr;AddressSpaceDispatch *d = cpu->cpu_ases[asidx].memory_dispatch;
@@ -696,10 +698,14 @@ address_space_translate_for_iotlb(CPUState *cpu, int asidx, hwaddr orig_addr, iommu_idx = imrc->attrs_to_index (iommu_mr, attrs);tcg_register_iommu_notifier (cpu, iommu_mr, iommu_idx); -/* We need all the permissions, so pass IOMMU_NONE so the IOMMU -* doesn't short-cut its translation table walk. -*/ - iotlb = imrc->translate (iommu_mr, addr,IOMMU_NONE , iommu_idx); + +if (access_type ==MMU_DATA_STORE ) { + iommu_flags =IOMMU_WO ; + }else { + iommu_flags =IOMMU_RO ; + } + + iotlb = imrc->translate (iommu_mr, addr, iommu_flags, iommu_idx); addr = ((iotlb.translated_addr & ~iotlb.addr_mask) | (addr & iotlb.addr_mask));/* Update the caller's prot bits to remove permissions the IOMMU
target/alpha/helper.c +1 -1
@@ -318,7 +318,7 @@ bool alpha_cpu_tlb_fill(CPUState *cs, vaddr addr, int size, }tlb_set_page (cs, addr &TARGET_PAGE_MASK , phys &TARGET_PAGE_MASK , - prot, mmu_idx,TARGET_PAGE_SIZE ); + prot, access_type, mmu_idx,TARGET_PAGE_SIZE );return true; }
target/arm/tcg/tlb_helper.c +1 -1
@@ -361,7 +361,7 @@ bool arm_cpu_tlb_fill(CPUState *cs, vaddr address, int size, res.f.extra.arm.pte_attrs = res.cacheattrs.attrs; res.f.extra.arm.shareability = res.cacheattrs.shareability; -tlb_set_page_full (cs, mmu_idx, address, &res.f); +tlb_set_page_full (cs, mmu_idx, address, access_type, &res.f);return true; }else if (probe) {return false;
target/avr/helper.c +1 -1
@@ -149,7 +149,7 @@ bool avr_cpu_tlb_fill(CPUState *cs, vaddr address, int size, } } -tlb_set_page (cs, address, paddr, prot, mmu_idx, page_size); +tlb_set_page (cs, address, paddr, prot, access_type, mmu_idx, page_size);return true; }
target/cris/helper.c +1 -1
@@ -68,7 +68,7 @@ bool cris_cpu_tlb_fill(CPUState *cs, vaddr address, int size, phy = res.phy & ~0x80000000 ; prot = res.prot;tlb_set_page (cs, address &TARGET_PAGE_MASK , phy, - prot, mmu_idx,TARGET_PAGE_SIZE ); + prot, access_type, mmu_idx,TARGET_PAGE_SIZE );return true; }
target/hppa/mem_helper.c +1 -1
@@ -461,7 +461,7 @@ bool hppa_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,* because we record the large page here in the hppa tlb. */ tlb_set_page (cs, addr &TARGET_PAGE_MASK , phys &TARGET_PAGE_MASK , - prot, mmu_idx,TARGET_PAGE_SIZE ); + prot, type, mmu_idx,TARGET_PAGE_SIZE );return true; }
target/i386/tcg/sysemu/excp_helper.c +2 -1
@@ -610,7 +610,8 @@ bool x86_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,tlb_set_page_with_attrs (cs, addr &TARGET_PAGE_MASK , out.paddr &TARGET_PAGE_MASK ,cpu_get_mem_attrs (env), - out.prot, mmu_idx, out.page_size); + out.prot, access_type, mmu_idx, + out.page_size);return true; }
target/loongarch/tcg/tlb_helper.c +1 -1
@@ -490,7 +490,7 @@ bool loongarch_cpu_tlb_fill(CPUState *cs, vaddr address, int size,if (ret ==TLBRET_MATCH ) {tlb_set_page (cs, address &TARGET_PAGE_MASK , physical &TARGET_PAGE_MASK , prot, - mmu_idx,TARGET_PAGE_SIZE ); + access_type, mmu_idx,TARGET_PAGE_SIZE );qemu_log_mask (CPU_LOG_MMU ,"%s address=%" VADDR_PRIx" physical " HWADDR_FMT_plx" prot %d\n" , __func__, address, physical, prot);
target/m68k/helper.c +7 -3
@@ -967,7 +967,7 @@ bool m68k_cpu_tlb_fill(CPUState *cs, vaddr address, int size,tlb_set_page (cs, address &TARGET_PAGE_MASK , address &TARGET_PAGE_MASK ,PAGE_READ |PAGE_WRITE |PAGE_EXEC , - mmu_idx,TARGET_PAGE_SIZE ); + qemu_access_type, mmu_idx,TARGET_PAGE_SIZE );return true; }
@@ -987,7 +987,8 @@ bool m68k_cpu_tlb_fill(CPUState *cs, vaddr address, int size, address, access_type, &page_size);if (likely (ret ==0 )) {tlb_set_page (cs, address &TARGET_PAGE_MASK , - physical &TARGET_PAGE_MASK , prot, mmu_idx, page_size); + physical &TARGET_PAGE_MASK , prot, qemu_access_type, + mmu_idx, page_size);return true; }
@@ -1459,6 +1460,7 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr, uint32_t is_read)int prot;int ret;target_ulong page_size; +MMUAccessType qemu_access_type; access_type =ACCESS_PTEST ;if (env->dfc &4 ) {
@@ -1466,9 +1468,11 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr, uint32_t is_read) }if ((env->dfc &3 ) ==2 ) { access_type |=ACCESS_CODE ; + qemu_access_type =MMU_INST_FETCH ; }if (!is_read) { access_type |=ACCESS_STORE ; + qemu_access_type =MMU_DATA_STORE ; } env->mmu.mmusr =0 ;
@@ -1478,7 +1482,7 @@ void HELPER(ptest)(CPUM68KState *env, uint32_t addr, uint32_t is_read)if (ret ==0 ) {tlb_set_page (env_cpu (env), addr &TARGET_PAGE_MASK , physical &TARGET_PAGE_MASK , - prot, access_type &ACCESS_SUPER ? + prot, qemu_access_type, access_type &ACCESS_SUPER ?MMU_KERNEL_IDX :MMU_USER_IDX , page_size); } }
target/microblaze/helper.c +4 -4
@@ -53,8 +53,8 @@ bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int size,/* MMU disabled or not available. */ address &=TARGET_PAGE_MASK ; prot =PAGE_RWX ; -tlb_set_page_with_attrs (cs, address, address, attrs, prot, mmu_idx, -TARGET_PAGE_SIZE ); +tlb_set_page_with_attrs (cs, address, address, attrs, prot, access_type, + mmu_idx,TARGET_PAGE_SIZE );return true; }
@@ -65,8 +65,8 @@ bool mb_cpu_tlb_fill(CPUState *cs, vaddr address, int size,qemu_log_mask (CPU_LOG_MMU ,"MMU map mmu=%d v=%x p=%x prot=%x\n" , mmu_idx, vaddr, paddr, lu.prot); -tlb_set_page_with_attrs (cs, vaddr, paddr, attrs, lu.prot, mmu_idx, -TARGET_PAGE_SIZE ); +tlb_set_page_with_attrs (cs, vaddr, paddr, attrs, lu.prot, access_type, + mmu_idx,TARGET_PAGE_SIZE );return true; }
target/mips/tcg/sysemu/tlb_helper.c +2 -2
@@ -931,7 +931,7 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,if (ret ==TLBRET_MATCH ) {tlb_set_page (cs, address &TARGET_PAGE_MASK , physical &TARGET_PAGE_MASK , prot, - mmu_idx,TARGET_PAGE_SIZE ); + access_type, mmu_idx,TARGET_PAGE_SIZE );return true; }#if !defined(TARGET_MIPS64 )
@@ -949,7 +949,7 @@ bool mips_cpu_tlb_fill(CPUState *cs, vaddr address, int size,if (ret ==TLBRET_MATCH ) {tlb_set_page (cs, address &TARGET_PAGE_MASK , physical &TARGET_PAGE_MASK , prot, - mmu_idx,TARGET_PAGE_SIZE ); + access_type, mmu_idx,TARGET_PAGE_SIZE );return true; } }
target/openrisc/mmu.c +1 -1
@@ -128,7 +128,7 @@ bool openrisc_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,if (likely (excp ==0 )) {tlb_set_page (cs, addr &TARGET_PAGE_MASK , phys_addr &TARGET_PAGE_MASK , prot, - mmu_idx,TARGET_PAGE_SIZE ); + access_type, mmu_idx,TARGET_PAGE_SIZE );return true; }if (probe) {
target/ppc/mmu_helper.c +1 -1
@@ -1368,7 +1368,7 @@ bool ppc_cpu_tlb_fill(CPUState *cs, vaddr eaddr, int size,if (ppc_xlate (cpu, eaddr, access_type, &raddr, &page_size, &prot, mmu_idx, !probe)) {tlb_set_page (cs, eaddr &TARGET_PAGE_MASK , raddr &TARGET_PAGE_MASK , - prot, mmu_idx,1UL << page_size); + prot, access_type, mmu_idx,1UL << page_size);return true; }if (probe) {
target/riscv/cpu_helper.c +1 -1
@@ -1419,7 +1419,7 @@ bool riscv_cpu_tlb_fill(CPUState *cs, vaddr address, int size,if (ret ==TRANSLATE_SUCCESS ) {tlb_set_page (cs, address & ~(tlb_size -1 ), pa & ~(tlb_size -1 ), - prot, mmu_idx, tlb_size); + prot, access_type, mmu_idx, tlb_size);return true; }else if (probe) {return false;
target/rx/cpu.c +2 -1
@@ -163,7 +163,8 @@ static bool rx_cpu_tlb_fill(CPUState *cs, vaddr addr, int size,/* Linear mapping */ address = physical = addr &TARGET_PAGE_MASK ; prot =PAGE_READ |PAGE_WRITE |PAGE_EXEC ; -tlb_set_page (cs, address, physical, prot, mmu_idx,TARGET_PAGE_SIZE ); +tlb_set_page (cs, address, physical, prot, access_type, + mmu_idx,TARGET_PAGE_SIZE );return true; }
target/s390x/tcg/excp_helper.c +1 -1
@@ -178,7 +178,7 @@ bool s390_cpu_tlb_fill(CPUState *cs, vaddr address, int size,"%s: set tlb %" PRIx64" -> %" PRIx64" (%x)\n" , __func__, (uint64_t)vaddr, (uint64_t)raddr, prot);tlb_set_page (cs, address &TARGET_PAGE_MASK , raddr, prot, - mmu_idx,TARGET_PAGE_SIZE ); + access_type, mmu_idx,TARGET_PAGE_SIZE );return true; }if (probe) {
target/sh4/helper.c +1 -1
@@ -807,7 +807,7 @@ bool superh_cpu_tlb_fill(CPUState *cs, vaddr address, int size,if (ret ==MMU_OK ) { address &=TARGET_PAGE_MASK ; physical &=TARGET_PAGE_MASK ; -tlb_set_page (cs, address, physical, prot, mmu_idx,TARGET_PAGE_SIZE ); +tlb_set_page (cs, address, physical, prot, access_type, mmu_idx,TARGET_PAGE_SIZE );return true; }if (probe) {
target/sparc/mmu_helper.c +3 -3
@@ -229,7 +229,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,"Translate at %" VADDR_PRIx" -> " HWADDR_FMT_plx", vaddr " TARGET_FMT_lx"\n" , address, full.phys_addr, vaddr); -tlb_set_page_full (cs, mmu_idx, vaddr, &full); +tlb_set_page_full (cs, mmu_idx, vaddr, access_type, &full);return true; }
@@ -245,7 +245,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,neverland. Fake/overridden mappings will be flushed when switching to normal mode. */ full.prot =PAGE_READ |PAGE_WRITE |PAGE_EXEC ; -tlb_set_page_full (cs, mmu_idx, vaddr, &full); +tlb_set_page_full (cs, mmu_idx, vaddr, access_type, &full);return true; }else {if (access_type ==MMU_INST_FETCH ) {
@@ -769,7 +769,7 @@ bool sparc_cpu_tlb_fill(CPUState *cs, vaddr address, int size,trace_mmu_helper_mmu_fault (address, full.phys_addr, mmu_idx, env->tl, env->dmmu.mmu_primary_context, env->dmmu.mmu_secondary_context); -tlb_set_page_full (cs, mmu_idx, address, &full); +tlb_set_page_full (cs, mmu_idx, address, access_type, &full);return true; }if (probe) {
target/tricore/helper.c +1 -1
@@ -84,7 +84,7 @@ bool tricore_cpu_tlb_fill(CPUState *cs, vaddr address, int size,if (ret ==TLBRET_MATCH ) {tlb_set_page (cs, address &TARGET_PAGE_MASK , physical &TARGET_PAGE_MASK , prot |PAGE_EXEC , - mmu_idx,TARGET_PAGE_SIZE ); + rw, mmu_idx,TARGET_PAGE_SIZE );return true; }else {assert (ret <0 );
target/xtensa/helper.c +2 -1
@@ -280,7 +280,8 @@ bool xtensa_cpu_tlb_fill(CPUState *cs, vaddr address, int size,tlb_set_page (cs, address &TARGET_PAGE_MASK , paddr &TARGET_PAGE_MASK , - access, mmu_idx, page_size); + access, access_type, mmu_idx, + page_size);return true; }else if (probe) {return false;
Signature · 1 line
2.17.1
Re: [RFC PATCH 02/16] accel/tcg: memory access from CPU will pass access_type to IOMMU
Quoted 10 lines · Jim Shu
[EXTERNAL MAIL]
It is the preparation patch for upcoming RISC-V wgChecker device.
Since RISC-V wgChecker could permit access in RO/WO permission, the IOMMUMemoryRegion could return different section for read & write access. The memory access from CPU should also pass the
access_typeto IOMMU translate function so that IOMMU could return the correct section of specifiedaccess_type.
Hi Jim,
Does this method take into account the situation where the CPU access type is different from the access type when creating iotlb? I think the section might be wrong in this situation.
Thanks,
Ethan
Quoted 2 lines
Re: [RFC PATCH 02/16] accel/tcg: memory access from CPU will pass access_type to IOMMU
Hi Ethan,
Yes, this mechanism could handle such situations.
The important part is that the translate() function of IOMMUMemoryRegion only returns the correct permission of the section related to CPU access type.
For example, if wgChecker only permits RO permission, it will return "downstream_as" with RO perm or "blocked_io_as" with WO perm. (depending on CPU access type).
When the CPU access type is different from the previous one, it will get TLB miss due to mismatched permissions.
Then, it will try to translate again, find the section of another access type, and fill into iotlb. (also kick out the previous iotlb entry).
This mechanism has poor performance if alternatively doing read/write access from CPU to IOMMUMemoryRegion with RO/WO perm due to TLB miss. I think it is the limitation that CPUTLBEntry doesn't support having different sections of each permission. At least this mechanism has the correct behavior.
Thanks,
Jim
Quoted 23 lines · Ethan Chen
Quoted 10 lines · Jim Shu
[EXTERNAL MAIL]
It is the preparation patch for upcoming RISC-V wgChecker device.
Since RISC-V wgChecker could permit access in RO/WO permission, the IOMMUMemoryRegion could return different section for read & write access. The memory access from CPU should also pass the
access_typeto IOMMU translate function so that IOMMU could return the correct section of specifiedaccess_type.Hi Jim,
Does this method take into account the situation where the CPU access type is different from the access type when creating iotlb? I think the section might be wrong in this situation.
Thanks,
EthanQuoted 2 lines
RFC PATCH 3/16 exec: Add RISC-V WorldGuard WID to MemTxAttrs
In reply to Jim ShuRISC-V WorldGuard will add 5-bit world_id (WID) to the each memory transaction on the bus. The wgChecker in front of RAM or peripherals MMIO could do the access control based on the WID. It is similar to ARM TrustZone NS bit, but the WID is 5-bit.
The common implementation of WID is AXI4 AxUSER signal.
--- include/exec/memattrs.h | 5 +++++ 1 file changed, 5 insertions(+)
include/exec/memattrs.h +5 -0
@@ -52,6 +52,11 @@ typedef struct MemTxAttrs { unsigned int memory:1;/* Requester ID (for MSI for example) */ unsigned int requester_id:16; + +/* +* RISC-V WorldGuard: the 5-bit WID of memory access. +*/ +uint8_t world_id; } MemTxAttrs;/* Bus masters which don't specify any attributes will get this,
Signature · 1 line
2.17.1
Re: [RFC PATCH 03/16] exec: Add RISC-V WorldGuard WID to MemTxAttrs
Quoted 18 lines · Jim Shu
RISC-V WorldGuard will add 5-bit
world_id(WID) to the each memory transaction on the bus. The wgChecker in front of RAM or peripherals MMIO could do the access control based on the WID. It is similar to ARM TrustZone NS bit, but the WID is 5-bit.The common implementation of WID is AXI4 AxUSER signal.
Signed-off-by Jim Shu <[email protected]>--- include/exec/memattrs.h | 5 +++++ 1 file changed, 5 insertions(+)include/exec/memattrs.h +0
-0
include/exec/memattrs.h typedef struct MemTxAttrs { · line 59
Quoted 8 lines
include/exec/memattrs.h +5
-0@@ -52,6 +52,11 @@ typedef struct MemTxAttrs { unsigned int memory:1;/* Requester ID (for MSI for example) */ unsigned int requester_id:16; + +/* +* RISC-V WorldGuard: the 5-bit WID of memory access. +*/ +uint8_t world_id;
Everything else is using unsigned int so I think we should as well
Alistair
Quoted 7 lines
include/exec/memattrs.h +0
-1} MemTxAttrs;/* Bus masters which don't specify any attributes will get this, -- 2.17.1
RFC PATCH 4/16 hw/misc: riscv_worldguard: Add RISC-V WorldGuard global config
In reply to Jim ShuAdd a device for RISCV WG global config, which contains the number of worlds, reset value, and trusted WID ... etc.
This global config is used by both CPU WG extension and wgChecker devices.
--- hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/riscv_worldguard.c | 183 +++++++++++++++++++++++++++++ include/hw/misc/riscv_worldguard.h | 55 +++++++++ 4 files changed, 242 insertions(+) create mode 100644 hw/misc/riscv_worldguard.c create mode 100644 include/hw/misc/riscv_worldguard.h
hw/misc/Kconfig +3 -0
@@ -213,4 +213,7 @@ config IOSB config XLNX_VERSAL_TRNG bool +config RISCV_WORLDGUARD + bool + source macio/Kconfig
hw/misc/meson.build +1 -0
@@ -34,6 +34,7 @@ system_ss.add(when: 'CONFIG_SIFIVE_E_PRCI', if_true: files('sifive_e_prci.c')) system_ss.add(when: 'CONFIG_SIFIVE_E_AON', if_true: files('sifive_e_aon.c')) system_ss.add(when: 'CONFIG_SIFIVE_U_OTP', if_true: files('sifive_u_otp.c')) system_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.c')) +specific_ss.add(when: 'CONFIG_RISCV_WORLDGUARD', if_true: files('riscv_worldguard.c')) subdir('macio')
hw/misc/riscv_worldguard.c new file +183 -0
183 lines not on this page · raw message
include/hw/misc/riscv_worldguard.h new file +55 -0
55 lines not on this page · raw message
Signature · 1 line
2.17.1
Re: [RFC PATCH 04/16] hw/misc: riscv_worldguard: Add RISC-V WorldGuard global config
Quoted 21 lines · Jim Shu
Add a device for RISCV WG global config, which contains the number of worlds, reset value, and trusted WID ... etc.
This global config is used by both CPU WG extension and wgChecker devices.
Signed-off-by Jim Shu <[email protected]>--- hw/misc/Kconfig | 3 + hw/misc/meson.build | 1 + hw/misc/riscv_worldguard.c | 183 +++++++++++++++++++++++++++++ include/hw/misc/riscv_worldguard.h | 55 +++++++++ 4 files changed, 242 insertions(+) create mode 100644 hw/misc/riscv_worldguard.c create mode 100644 include/hw/misc/riscv_worldguard.hhw/misc/Kconfig +0
-0
hw/misc/Kconfig config IOSB · line 217
Quoted 5 lines
hw/misc/Kconfig +2
-0@@ -213,4 +213,7 @@ config IOSB config XLNX_VERSAL_TRNG bool +config RISCV_WORLDGUARD + bool
We should ensure this is enabled as well so that it is built by default
Alistair
Quoted 268 lines
hw/misc/Kconfig +1
-0+ source macio/Kconfighw/misc/meson.build +1
-0@@ -34,6 +34,7 @@ system_ss.add(when: 'CONFIG_SIFIVE_E_PRCI', if_true: files('sifive_e_prci.c')) system_ss.add(when: 'CONFIG_SIFIVE_E_AON', if_true: files('sifive_e_aon.c')) system_ss.add(when: 'CONFIG_SIFIVE_U_OTP', if_true: files('sifive_u_otp.c')) system_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.c')) +specific_ss.add(when: 'CONFIG_RISCV_WORLDGUARD', if_true: files('riscv_worldguard.c')) subdir('macio')hw/misc/riscv_worldguard.c new file +183
-0@@ -0,0 +1,183 @@ +/* +* RISC-V WorldGuard Device +* +* Copyright (c) 2022 SiFive, Inc. +* +* This provides WorldGuard global config. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#include "qemu/osdep.h" +#include "qapi/error.h" +#include "qemu/log.h" +#include "exec/hwaddr.h" +#include "hw/registerfields.h" +#include "hw/sysbus.h" +#include "hw/hw.h" +#include "hw/qdev-properties.h" +#include "hw/misc/riscv_worldguard.h" +#include "hw/core/cpu.h" +#include "target/riscv/cpu.h" +#include "trace.h" + +/* +* WorldGuard global config: +* List the global setting of WG, like num-of-worlds. It is unique in the machine. +* All CPUs with WG extension and wgChecker devices will use it. +*/ +struct RISCVWorldGuardState *worldguard_config =NULL ; + +static Property riscv_worldguard_properties[] = { +DEFINE_PROP_UINT32 ("nworlds" , RISCVWorldGuardState, nworlds,0 ), + +/* Only Trusted WID could access wgCheckers if it is enabled. */ +DEFINE_PROP_UINT32 ("trustedwid" , RISCVWorldGuardState, trustedwid,NO_TRUSTEDWID ), + +/* +* WG reset value is bypass mode in HW. All WG permission checkings are +* pass by default, so SW could correctly run on the machine w/o any WG +* programming. +*/ +DEFINE_PROP_BOOL ("hw-bypass" , RISCVWorldGuardState, hw_bypass, false), + +/* +* TrustZone compatible mode: +* This mode is only supported in 2 worlds system. It converts WorldGuard +* WID to TZ NS signal on the bus so WG could be cooperated with +* TZ components. In QEMU, it converts WID to 'MemTxAttrs.secure' bit used +* by TZ. +*/ +DEFINE_PROP_BOOL ("tz-compat" , RISCVWorldGuardState, tz_compat, false), +DEFINE_PROP_END_OF_LIST (), +}; + +/* WID to MemTxAttrs converter */ +static void wid_to_mem_attrs (MemTxAttrs *attrs,uint32_t wid) +{ +g_assert (wid < worldguard_config->nworlds); + + attrs->unspecified =0 ; +if (worldguard_config->tz_compat) { + attrs->secure = wid; + }else { + attrs->world_id = wid; + } +} + +/* MemTxAttrs to WID converter */ +uint32_t mem_attrs_to_wid (MemTxAttrs attrs) +{ +if (attrs.unspecified) { +if (worldguard_config->trustedwid !=NO_TRUSTEDWID ) { +return worldguard_config->trustedwid; + }else { +return worldguard_config->nworlds -1 ; + } + } + +if (worldguard_config->tz_compat) { +return attrs.secure; + }else { +return attrs.world_id; + } +} + +bool could_access_wgblocks (MemTxAttrs attrs,const char *wgblock) +{ +uint32_t wid =mem_attrs_to_wid (attrs); +uint32_t trustedwid = worldguard_config->trustedwid; + +if ((trustedwid ==NO_TRUSTEDWID ) || (wid == trustedwid)) { +return true; + }else { +/* +* Only Trusted WID could access WG blocks if having it. +* Access them from other WIDs will get failed. +*/ +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Invalid access to %s from non-trusted WID %d\n" , + __func__, wgblock, wid); + +return false; + } +} + +static void riscv_worldguard_realize (DeviceState *dev,Error **errp) +{ +RISCVWorldGuardState *s =RISCV_WORLDGUARD (dev); + +if (worldguard_config !=NULL ) { +error_setg (errp,"Couldn't realize multiple global WorldGuard configs." ); +return ; + } + +if ((s->nworlds) & (s->nworlds -1 )) { +error_setg (errp,"Current implementation only support power-of-2 NWorld." ); +return ; + } + +if ((s->trustedwid !=NO_TRUSTEDWID ) && (s->trustedwid >= s->nworlds)) { +error_setg (errp,"Trusted WID must be less than the number of world." ); +return ; + } + +if ((s->nworlds !=2 ) && (s->tz_compat)) { +error_setg (errp,"Only 2 worlds system could use TrustZone compatible mode." ); +return ; + } + +/* Register WG global config */ + worldguard_config = s; + +/* Initialize global data for wgChecker */ + wgc_slot_perm_mask =MAKE_64BIT_MASK (0 ,2 * worldguard_config->nworlds); +} + +static void riscv_worldguard_class_init (ObjectClass *klass,void *data) +{ +DeviceClass *dc =DEVICE_CLASS (klass); + +device_class_set_props (dc, riscv_worldguard_properties); + dc->user_creatable = true; + dc->realize = riscv_worldguard_realize; +} + +static const TypeInfo riscv_worldguard_info = { + .name =TYPE_RISCV_WORLDGUARD , + .parent =TYPE_DEVICE , + .instance_size =sizeof (RISCVWorldGuardState), + .class_init = riscv_worldguard_class_init, +}; + +/* +* Create WorldGuard global config +*/ +DeviceState *riscv_worldguard_create (uint32_t nworlds,uint32_t trustedwid, +bool hw_bypass,bool tz_compat) +{ +DeviceState *dev =qdev_new (TYPE_RISCV_WORLDGUARD ); +qdev_prop_set_uint32 (dev,"nworlds" , nworlds); +qdev_prop_set_uint32 (dev,"trustedwid" , trustedwid); +qdev_prop_set_bit (dev,"hw-bypass" , hw_bypass); +qdev_prop_set_bit (dev,"tz-compat" , tz_compat); +qdev_realize (DEVICE (dev),NULL , &error_fatal); +return dev; +} + +static void riscv_worldguard_register_types (void ) +{ +type_register_static (&riscv_worldguard_info); +} + +type_init (riscv_worldguard_register_types)include/hw/misc/riscv_worldguard.h new file +55
-1@@ -0,0 +1,55 @@ +/* +* RISC-V WorldGuard Devices +* +* Copyright (c) 2022 RISCV, Inc. +* +* This program is free software; you can redistribute it and/or modify it +* under the terms and conditions of the GNU General Public License, +* version 2 or later, as published by the Free Software Foundation. +* +* This program is distributed in the hope it will be useful, but WITHOUT +* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +* more details. +* +* You should have received a copy of the GNU General Public License along with +* this program. If not, see <http://www.gnu.org/licenses/>. +*/ + +#ifndef HW_RISCV_WORLDGUARD_H +#define HW_RISCV_WORLDGUARD_H + +#include "qom/object.h" +#include "hw/sysbus.h" +#include "exec/hwaddr.h" + +#define TYPE_RISCV_WORLDGUARD "riscv.worldguard" + +#define NO_TRUSTEDWID UINT32_MAX + +typedef struct RISCVWorldGuardState RISCVWorldGuardState ; +DECLARE_INSTANCE_CHECKER (RISCVWorldGuardState,RISCV_WORLDGUARD , + TYPE_RISCV_WORLDGUARD) + +struct RISCVWorldGuardState { +/*< private >*/ +DeviceState parent_obj; + +/*< public >*/ + +/* Property */ +uint32_t nworlds; +uint32_t trustedwid; +bool hw_bypass; +bool tz_compat; +}; + +extern struct RISCVWorldGuardState *worldguard_config; + +DeviceState *riscv_worldguard_create (uint32_t nworlds,uint32_t trustedwid, +bool hw_bypass,bool tz_compat); + +uint32_t mem_attrs_to_wid (MemTxAttrs attrs); +bool could_access_wgblocks (MemTxAttrs attrs,const char *wgblock); + +#endif --2.17.1
RFC PATCH 5/16 target/riscv: Add CPU options of WorldGuard CPU extension
In reply to Jim ShuWe define CPU options for WG CSR support in RISC-V CPUs which can be set by machine/device emulation. The RISC-V CSR emulation will also check this feature for emulating WG CSRs.
--- target/riscv/cpu.c | 8 ++++++++ target/riscv/cpu_cfg.h | 3 +++ target/riscv/tcg/tcg-cpu.c | 11 +++++++++++ 3 files changed, 22 insertions(+)
target/riscv/cpu.c +8 -0
@@ -204,6 +204,9 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (xtheadmempair,PRIV_VERSION_1_11_0 , ext_xtheadmempair),ISA_EXT_DATA_ENTRY (xtheadsync,PRIV_VERSION_1_11_0 , ext_xtheadsync),ISA_EXT_DATA_ENTRY (xventanacondops,PRIV_VERSION_1_12_0 , ext_XVentanaCondOps), +ISA_EXT_DATA_ENTRY (smwg,PRIV_VERSION_1_12_0 , ext_smwg), +ISA_EXT_DATA_ENTRY (smwgd,PRIV_VERSION_1_12_0 , ext_smwgd), +ISA_EXT_DATA_ENTRY (sswg,PRIV_VERSION_1_12_0 , ext_sswg), DEFINE_PROP_END_OF_LIST(), };
@@ -1595,6 +1598,11 @@ const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {const RISCVCPUMultiExtConfig riscv_cpu_named_features[] = {MULTI_EXT_CFG_BOOL ("zic64b" , ext_zic64b, true), +/* RISC-V WorldGuard v0.4 */ +MULTI_EXT_CFG_BOOL ("x-smwg" , ext_smwg, false), +MULTI_EXT_CFG_BOOL ("x-smwgd" , ext_smwgd, false), +MULTI_EXT_CFG_BOOL ("x-sswg" , ext_sswg, false), +DEFINE_PROP_END_OF_LIST (), };
target/riscv/cpu_cfg.h +3 -0
@@ -120,6 +120,9 @@ struct RISCVCPUConfig {bool ext_ssaia;bool ext_sscofpmf;bool ext_smepmp; +bool ext_smwg; +bool ext_smwgd; +bool ext_sswg;bool rvv_ta_all_1s;bool rvv_ma_all_1s;
target/riscv/tcg/tcg-cpu.c +11 -0
@@ -726,6 +726,17 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) cpu->pmu_avail_ctrs =0 ; } +/* RISC-V WorldGuard */ +if (cpu->cfg.ext_sswg && !cpu->cfg.ext_smwg) { +error_setg (errp,"Sswg extension requires Smwg extension" ); +return ; + } + +if (cpu->cfg.ext_smwgd != cpu->cfg.ext_sswg) { +error_setg (errp,"Smwgd/Sswg extensions should be enabled together" ); +return ; + } +/* * Disable isa extensions based on priv spec after we * validated and set everything we need.
Signature · 1 line
2.17.1
Re: [RFC PATCH 05/16] target/riscv: Add CPU options of WorldGuard CPU extension
Quoted 27 lines · Jim Shu
We define CPU options for WG CSR support in RISC-V CPUs which can be set by machine/device emulation. The RISC-V CSR emulation will also check this feature for emulating WG CSRs.
Signed-off-by Jim Shu <[email protected]>--- target/riscv/cpu.c | 8 ++++++++ target/riscv/cpu_cfg.h | 3 +++ target/riscv/tcg/tcg-cpu.c | 11 +++++++++++ 3 files changed, 22 insertions(+)target/riscv/cpu.c +3
-0@@ -204,6 +204,9 @@ const RISCVIsaExtData isa_edata_arr[] = {ISA_EXT_DATA_ENTRY (xtheadmempair,PRIV_VERSION_1_11_0 , ext_xtheadmempair),ISA_EXT_DATA_ENTRY (xtheadsync,PRIV_VERSION_1_11_0 , ext_xtheadsync),ISA_EXT_DATA_ENTRY (xventanacondops,PRIV_VERSION_1_12_0 , ext_XVentanaCondOps), +ISA_EXT_DATA_ENTRY (smwg,PRIV_VERSION_1_12_0 , ext_smwg), +ISA_EXT_DATA_ENTRY (smwgd,PRIV_VERSION_1_12_0 , ext_smwgd), +ISA_EXT_DATA_ENTRY (sswg,PRIV_VERSION_1_12_0 , ext_sswg), DEFINE_PROP_END_OF_LIST(), };
target/riscv/cpu.c const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = { · line 1604
Quoted 7 lines
target/riscv/cpu.c +4
-0@@ -1595,6 +1598,11 @@ const RISCVCPUMultiExtConfig riscv_cpu_experimental_exts[] = {const RISCVCPUMultiExtConfig riscv_cpu_named_features[] = {MULTI_EXT_CFG_BOOL ("zic64b" , ext_zic64b, true), +/* RISC-V WorldGuard v0.4 */ +MULTI_EXT_CFG_BOOL ("x-smwg" , ext_smwg, false), +MULTI_EXT_CFG_BOOL ("x-smwgd" , ext_smwgd, false), +MULTI_EXT_CFG_BOOL ("x-sswg" , ext_sswg, false),
We want to add the functionality then expose the properties, as this is user accessible and we want to maintain bistability.
I understand this doesn't break the build, but it's a bit strange to allow users to enable something when at this point it doesn't yet work
Alistair
Quoted 44 lines
target/riscv/cpu.c +1
-0+ DEFINE_PROP_END_OF_LIST(), };target/riscv/cpu_cfg.h +3
-0@@ -120,6 +120,9 @@ struct RISCVCPUConfig {bool ext_ssaia;bool ext_sscofpmf;bool ext_smepmp; +bool ext_smwg; +bool ext_smwgd; +bool ext_sswg;bool rvv_ta_all_1s;bool rvv_ma_all_1s;target/riscv/tcg/tcg-cpu.c +11
-1@@ -726,6 +726,17 @@ void riscv_cpu_validate_set_extensions(RISCVCPU *cpu, Error **errp) cpu->pmu_avail_ctrs =0 ; } +/* RISC-V WorldGuard */ +if (cpu->cfg.ext_sswg && !cpu->cfg.ext_smwg) { +error_setg (errp,"Sswg extension requires Smwg extension" ); +return ; + } + +if (cpu->cfg.ext_smwgd != cpu->cfg.ext_sswg) { +error_setg (errp,"Smwgd/Sswg extensions should be enabled together" ); +return ; + } +/* * Disable isa extensions based on priv spec after we * validated and set everything we need. -- 2.17.1
RFC PATCH 6/16 target/riscv: Add hard-coded CPU state of WG extension
In reply to Jim ShuAdd hard-coded state of WG extension. 'mwid' is the M-mode WID of CPU. 'mwidlist' is the list of allowed WID value of 'mlwid' CSR.
These CPU states can be set by CPU option, or can be set by machine code via newly added APIs. If we want different WG configs of CPUs, we should set it by machine code.
--- target/riscv/cpu.c | 2 ++ target/riscv/cpu.h | 2 ++ target/riscv/cpu_cfg.h | 2 ++ target/riscv/cpu_helper.c | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+)
target/riscv/cpu.c +2 -0
@@ -2291,6 +2291,8 @@ static Property riscv_cpu_properties[] = {* it with -x and default to 'false'. */ DEFINE_PROP_BOOL ("x-misa-w" ,RISCVCPU , cfg.misa_w, false), +DEFINE_PROP_UINT32 ("mwid" ,RISCVCPU , cfg.mwid,UINT32_MAX ), +DEFINE_PROP_UINT32 ("mwidlist" ,RISCVCPU , cfg.mwidlist,UINT32_MAX ), DEFINE_PROP_END_OF_LIST(), };
target/riscv/cpu.h +2 -0
@@ -540,6 +540,8 @@ void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, uint32_t priv, void *rmw_fn_arg);RISCVException smstateen_acc_ok (CPURISCVState *env,int index,uint64_t bit); +void riscv_cpu_set_wg_mwid (CPURISCVState *env,uint32_t mwid); +void riscv_cpu_set_wg_mwidlist (CPURISCVState *env,uint32_t mwidlist);#endif /* !CONFIG_USER_ONLY */ void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv);
target/riscv/cpu_cfg.h +2 -0
@@ -166,6 +166,8 @@ struct RISCVCPUConfig {bool pmp;bool debug;bool misa_w; +uint32_t mwid; +uint32_t mwidlist;bool short_isa_string;
target/riscv/cpu_helper.c +18 -0
@@ -149,6 +149,24 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, *pflags = flags; } +#ifndef CONFIG_USER_ONLY +void riscv_cpu_set_wg_mwid (CPURISCVState *env,uint32_t mwid) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); + + cpu->cfg.mwid = mwid; +} + +void riscv_cpu_set_wg_mwidlist (CPURISCVState *env,uint32_t mwidlist) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); + + cpu->cfg.mwidlist = mwidlist; +} +#endif /* CONFIG_USER_ONLY */ +void riscv_cpu_update_mask (CPURISCVState *env) {target_ulong mask =0 , base =0 ;
Signature · 1 line
2.17.1
Re: [RFC PATCH 06/16] target/riscv: Add hard-coded CPU state of WG extension
Quoted 21 lines · Jim Shu
Add hard-coded state of WG extension. 'mwid' is the M-mode WID of CPU. 'mwidlist' is the list of allowed WID value of 'mlwid' CSR.
These CPU states can be set by CPU option, or can be set by machine code via newly added APIs. If we want different WG configs of CPUs, we should set it by machine code.
Signed-off-by Jim Shu <[email protected]>--- target/riscv/cpu.c | 2 ++ target/riscv/cpu.h | 2 ++ target/riscv/cpu_cfg.h | 2 ++ target/riscv/cpu_helper.c | 18 ++++++++++++++++++ 4 files changed, 24 insertions(+)target/riscv/cpu.c +0
-0
target/riscv/cpu.c static Property riscv_cpu_properties[] = { · line 2295
Quoted 5 lines
target/riscv/cpu.c +2
-0@@ -2291,6 +2291,8 @@ static Property riscv_cpu_properties[] = {* it with -x and default to 'false'. */ DEFINE_PROP_BOOL ("x-misa-w" ,RISCVCPU , cfg.misa_w, false), +DEFINE_PROP_UINT32 ("mwid" ,RISCVCPU , cfg.mwid,UINT32_MAX ), + DEFINE_PROP_UINT32("mwidlist", RISCVCPU, cfg.mwidlist, UINT32_MAX),
These should be "x-" as well.
Also same comment about functionality and properties as previous patch
Alistair
Quoted 62 lines
target/riscv/cpu.c +0
-0DEFINE_PROP_END_OF_LIST(), };target/riscv/cpu.h +2
-0@@ -540,6 +540,8 @@ void riscv_cpu_set_aia_ireg_rmw_fn(CPURISCVState *env, uint32_t priv, void *rmw_fn_arg);RISCVException smstateen_acc_ok (CPURISCVState *env,int index,uint64_t bit); +void riscv_cpu_set_wg_mwid (CPURISCVState *env,uint32_t mwid); +void riscv_cpu_set_wg_mwidlist (CPURISCVState *env,uint32_t mwidlist);#endif /* !CONFIG_USER_ONLY */ void riscv_cpu_set_mode (CPURISCVState *env,target_ulong newpriv);target/riscv/cpu_cfg.h +2
-0@@ -166,6 +166,8 @@ struct RISCVCPUConfig {bool pmp;bool debug;bool misa_w; +uint32_t mwid; +uint32_t mwidlist;bool short_isa_string;target/riscv/cpu_helper.c +18
-1@@ -149,6 +149,24 @@ void cpu_get_tb_cpu_state(CPURISCVState *env, vaddr *pc, *pflags = flags; } +#ifndef CONFIG_USER_ONLY +void riscv_cpu_set_wg_mwid (CPURISCVState *env,uint32_t mwid) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); + + cpu->cfg.mwid = mwid; +} + +void riscv_cpu_set_wg_mwidlist (CPURISCVState *env,uint32_t mwidlist) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); + + cpu->cfg.mwidlist = mwidlist; +} +#endif /* CONFIG_USER_ONLY */ +void riscv_cpu_update_mask (CPURISCVState *env) {target_ulong mask =0 , base =0 ; --2.17.1
RFC PATCH 7/16 target/riscv: Add defines for WorldGuard CSRs
In reply to Jim ShuAdd CSRs for 3 WG extensions: Smwg, Smwgd, and Sswg.
--- target/riscv/cpu_bits.h | 5 +++++ 1 file changed, 5 insertions(+)
target/riscv/cpu_bits.h +5 -0
@@ -360,6 +360,11 @@#define CSR_DPC 0x7b1#define CSR_DSCRATCH 0x7b2 +/* RISC-V WorldGuard */ +#define CSR_MLWID 0x390 +#define CSR_SLWID 0x190 +#define CSR_MWIDDELEG 0x748 +/* Performance Counters */ #define CSR_MHPMCOUNTER3 0xb03#define CSR_MHPMCOUNTER4 0xb04
Signature · 1 line
2.17.1
RFC PATCH 8/16 target/riscv: Allow global WG config to set WG CPU callbacks
In reply to Jim ShuSome WG CPU functions depend on global WG config (like num-of-world), so we let the global WG config device to set callbacks of a RISC-V HART.
--- target/riscv/cpu.h | 4 ++++ 1 file changed, 4 insertions(+)
target/riscv/cpu.h +4 -0
@@ -431,6 +431,10 @@ struct CPUArchState {uint64_t kvm_timer_state;uint64_t kvm_timer_frequency;#endif /* CONFIG_KVM */ + +/* machine specific WorldGuard callback */ +void (*wg_reset)(CPURISCVState *env); +void (*wid_to_mem_attrs)(MemTxAttrs *attrs,uint32_t wid); };/*
Signature · 1 line
2.17.1
RFC PATCH 9/16 target/riscv: Implement WorldGuard CSRs
In reply to Jim ShuThe WG v0.4 specification adds 3 CSRs to configure S/U/HS/VS-mode WIDs of CPUs in the higher privileged modes.
The Smwg extension at least requires a RISC-V HART to have M/U-mode, and the Sswg/Smwgd extension at least requires a RISC-V HART to have M/S/U-mode.
--- target/riscv/cpu.c | 4 ++ target/riscv/cpu.h | 5 +++ target/riscv/csr.c | 107 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+)
target/riscv/cpu.c +4 -0
@@ -1022,6 +1022,10 @@ static void riscv_cpu_reset_hold(Object *obj, ResetType type)riscv_trigger_reset_hold (env); } +if (riscv_cpu_cfg (env)->ext_smwg && env->wg_reset) { + env->wg_reset (env); + } +if (kvm_enabled ()) {kvm_riscv_reset_vcpu (cpu); }
target/riscv/cpu.h +5 -0
@@ -432,6 +432,11 @@ struct CPUArchState {uint64_t kvm_timer_frequency;#endif /* CONFIG_KVM */ +/* RISC-V WorldGuard */ +target_ulong mlwid; +target_ulong slwid; +target_ulong mwiddeleg; +/* machine specific WorldGuard callback */ void (*wg_reset)(CPURISCVState *env);void (*wid_to_mem_attrs)(MemTxAttrs *attrs,uint32_t wid);
target/riscv/csr.c +107 -0
118 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH 10/16 target/riscv: Add WID to MemTxAttrs of CPU memory transactions
In reply to Jim ShuWhen a RISC-V HART has WG extension, their memory transactions will contain WID. Support MemTxAttrs in RISC-V target and add WID inside if a HART has WG extension.
--- target/riscv/cpu.c | 2 +- target/riscv/cpu.h | 1 + target/riscv/cpu_helper.c | 51 ++++++++++++++++++++++++++++++++++++--- 3 files changed, 50 insertions(+), 4 deletions(-)
target/riscv/cpu.c +1 -1
@@ -2343,7 +2343,7 @@ static int64_t riscv_get_arch_id(CPUState *cs)#include "hw/core/sysemu-cpu-ops.h" static const struct SysemuCPUOps riscv_sysemu_ops = { - .get_phys_page_debug = riscv_cpu_get_phys_page_debug, + .get_phys_page_attrs_debug = riscv_cpu_get_phys_page_attrs_debug, .write_elf64_note = riscv_cpu_write_elf64_note, .write_elf32_note = riscv_cpu_write_elf32_note, .legacy_vmsd = &vmstate_riscv_cpu,
target/riscv/cpu.h +1 -0
@@ -512,6 +512,7 @@ void riscv_cpu_set_geilen(CPURISCVState *env, target_ulong geilen);bool riscv_cpu_vector_enabled (CPURISCVState *env);void riscv_cpu_set_virt_enabled (CPURISCVState *env,bool enable);int riscv_env_mmu_index (CPURISCVState *env,bool ifetch); +hwaddr riscv_cpu_get_phys_page_attrs_debug (CPUState *cpu,vaddr addr,MemTxAttrs *attrs); G_NORETURN void riscv_cpu_do_unaligned_access(CPUState *cs, vaddr addr,MMUAccessType access_type,int mmu_idx,uintptr_t retaddr);
target/riscv/cpu_helper.c +48 -3
87 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH 11/16 hw/misc: riscv_worldguard: Add API to enable WG extension of CPU
In reply to Jim Shuriscv_worldguard_apply_cpu() could enable WG CPU extension and set WG callback to CPUs. It is used by machine code after realizing global WG device.
--- hw/misc/riscv_worldguard.c | 87 ++++++++++++++++++++++++++++++ include/hw/misc/riscv_worldguard.h | 1 + 2 files changed, 88 insertions(+)
hw/misc/riscv_worldguard.c +87 -0
@@ -93,6 +93,93 @@ uint32_t mem_attrs_to_wid(MemTxAttrs attrs) } } +static void riscv_cpu_wg_reset (CPURISCVState *env) +{ +CPUState *cs =env_cpu (env); +RISCVCPU *cpu =RISCV_CPU (cs); +uint32_t mlwid, slwid, mwiddeleg; +uint32_t trustedwid; + +if (!riscv_cpu_cfg (env)->ext_smwg) { +return ; + } + +if (worldguard_config ==NULL ) { +/* +* Note: This reset is dummy now and WG CSRs will be reset again +* after worldguard_config is realized. +*/ +return ; + } + + trustedwid = worldguard_config->trustedwid; +if (trustedwid ==NO_TRUSTEDWID ) { + trustedwid = worldguard_config->nworlds -1 ; + } + +/* Reset mlwid, slwid, mwiddeleg CSRs */ +if (worldguard_config->hw_bypass) { +/* HW bypass mode */ + mlwid = trustedwid; + }else { + mlwid =0 ; + } + slwid =0 ; + mwiddeleg =0 ; + + env->mlwid = mlwid; +if (riscv_cpu_cfg (env)->ext_sswg) { + env->slwid = slwid; + env->mwiddeleg = mwiddeleg; + } + +/* Check mwid, mwidlist config */ +if (worldguard_config !=NULL ) { +uint32_t valid_widlist =MAKE_64BIT_MASK (0 , worldguard_config->nworlds); + +/* CPU use default mwid / mwidlist config if not set */ +if (cpu->cfg.mwidlist ==UINT32_MAX ) { +/* mwidlist contains all WIDs */ + cpu->cfg.mwidlist = valid_widlist; + } +if (cpu->cfg.mwid ==UINT32_MAX ) { + cpu->cfg.mwid = trustedwid; + } + +/* Check if mwid/mwidlist HW config is valid in NWorld. */ +g_assert ((cpu->cfg.mwidlist & ~valid_widlist) ==0 ); +g_assert (cpu->cfg.mwid < worldguard_config->nworlds); + } +} + +/* +* riscv_worldguard_apply_cpu - Enable WG extension of CPU +* +* Note: This API should be used after global WG device is created +* (riscv_worldguard_realize()). +*/ +void riscv_worldguard_apply_cpu (uint32_t hartid) +{ +/* WG global config should exist */ +g_assert (worldguard_config); + +CPUState *cpu =qemu_get_cpu (hartid); +RISCVCPU *rcpu =RISCV_CPU (cpu); +CPURISCVState *env = cpu ?cpu_env (cpu) :NULL ; + + rcpu->cfg.ext_smwg = true; +if (riscv_has_ext (env,RVS ) &&riscv_has_ext (env,RVU )) { + rcpu->cfg.ext_sswg = true; + } + +/* Set machine specific WorldGuard callback */ + env->wg_reset = riscv_cpu_wg_reset; + env->wid_to_mem_attrs = wid_to_mem_attrs; + +/* Reset WG CSRs in CPU */ + env->wg_reset (env); +} +bool could_access_wgblocks (MemTxAttrs attrs,const char *wgblock) {uint32_t wid =mem_attrs_to_wid (attrs);
include/hw/misc/riscv_worldguard.h +1 -0
@@ -48,6 +48,7 @@ extern struct RISCVWorldGuardState *worldguard_config;DeviceState *riscv_worldguard_create (uint32_t nworlds,uint32_t trustedwid,bool hw_bypass,bool tz_compat); +void riscv_worldguard_apply_cpu (uint32_t hartid);uint32_t mem_attrs_to_wid (MemTxAttrs attrs);bool could_access_wgblocks (MemTxAttrs attrs,const char *wgblock);
Signature · 1 line
2.17.1
RFC PATCH 12/16 hw/misc: riscv_wgchecker: Implement RISC-V WorldGuard Checker
In reply to Jim ShuImplement the RISC-V WorldGuard Checker, which sits in front of RAM or device MMIO and allow software to configure it to either pass through or reject transactions.
We implement the wgChecker as a QEMU IOMMU, which will direct transactions either through to the devices and memory behind it or to a special "never works" AddressSpace if they are blocked.
This initial commit implements the skeleton of the device:
* it always permits accesses
* it doesn't implement wgChecker's slot registers
* it doesn't implement the interrupt or other behaviour for blocked transactions
--- hw/misc/meson.build | 2 +- hw/misc/riscv_wgchecker.c | 604 +++++++++++++++++++++++++++++ hw/misc/trace-events | 8 + include/hw/misc/riscv_worldguard.h | 63 +++ 4 files changed, 676 insertions(+), 1 deletion(-) create mode 100644 hw/misc/riscv_wgchecker.c
hw/misc/meson.build +1 -1
@@ -34,7 +34,7 @@ system_ss.add(when: 'CONFIG_SIFIVE_E_PRCI', if_true: files('sifive_e_prci.c')) system_ss.add(when: 'CONFIG_SIFIVE_E_AON', if_true: files('sifive_e_aon.c')) system_ss.add(when: 'CONFIG_SIFIVE_U_OTP', if_true: files('sifive_u_otp.c')) system_ss.add(when: 'CONFIG_SIFIVE_U_PRCI', if_true: files('sifive_u_prci.c')) -specific_ss.add(when: 'CONFIG_RISCV_WORLDGUARD', if_true: files('riscv_worldguard.c')) +specific_ss.add(when: 'CONFIG_RISCV_WORLDGUARD', if_true: files('riscv_worldguard.c', 'riscv_wgchecker.c')) subdir('macio')
hw/misc/riscv_wgchecker.c new file +604 -0
604 lines not on this page · raw message
hw/misc/trace-events +8 -0
@@ -351,3 +351,11 @@ djmemc_write(int reg, uint64_t value, unsigned int size) "reg=0x%x value=0x%"PRI # iosb.c iosb_read(int reg, uint64_t value, unsigned int size) "reg=0x%x value=0x%"PRIx64" size=%u" iosb_write(int reg, uint64_t value, unsigned int size) "reg=0x%x value=0x%"PRIx64" size=%u" + +# riscv_worldguard.c +riscv_wgchecker_mmio_read(uint64_t base, uint64_t offset, unsigned int size) "base = 0x%"PRIx64", offset = 0x%"PRIx64", size = 0x%x" +riscv_wgchecker_mmio_write(uint64_t base, uint64_t offset, unsigned int size, uint64_t val) "base = 0x%"PRIx64", offset = 0x%"PRIx64", size = 0x%x, val = 0x%"PRIx64 + +riscv_wgc_mem_blocked_read(uint64_t addr, unsigned size, uint32_t wid) "wgChecker blocked read: offset 0x%" PRIx64 " size %u wid %" PRIu32 +riscv_wgc_mem_blocked_write(uint64_t addr, uint64_t data, unsigned size, uint32_t wid) "wgChecker blocked write: offset 0x%" PRIx64 " data 0x%" PRIx64 " size %u wid %" PRIu32 +riscv_wgc_translate(uint64_t addr, int flags, int wid, const char *res) "wgChecker translate: addr 0x%016" PRIx64 " flags 0x%x wid %d: %s"
include/hw/misc/riscv_worldguard.h +63 -0
67 lines not on this page · raw message
Signature · 1 line
2.17.1
RFC PATCH 13/16 hw/misc: riscv_wgchecker: Implement wgchecker slot registers
In reply to Jim ShuwgChecker slot is similar to PMP region. SW could program each slot to configure the permission of address range.
--- hw/misc/riscv_wgchecker.c | 330 +++++++++++++++++++++++++++++ hw/misc/riscv_worldguard.c | 3 + include/hw/misc/riscv_worldguard.h | 4 + 3 files changed, 337 insertions(+)
hw/misc/riscv_wgchecker.c +330 -0
@@ -53,6 +53,52 @@ REG64(ERRCAUSE, 0x010) R_ERRCAUSE_IP_MASK) REG64(ERRADDR, 0x018) +REG64 (WGC_SLOT ,0x020 ) + +/* wgChecker slots */ +REG64 (SLOT_ADDR ,0x000 ) +REG64 (SLOT_PERM ,0x008 ) +REG32 (SLOT_CFG ,0x010 ) +FIELD (SLOT_CFG ,A ,0 ,2 ) +FIELD (SLOT_CFG ,ER ,8 ,1 ) +FIELD (SLOT_CFG ,EW ,9 ,1 ) +FIELD (SLOT_CFG ,IR ,10 ,1 ) +FIELD (SLOT_CFG ,IW ,11 ,1 ) +FIELD (SLOT_CFG ,LOCK ,31 ,1 ) + +#define SLOT_SIZE 0x020 + +#define SLOT0_CFG_MASK \ + (R_SLOT_CFG_ER_MASK | \ +R_SLOT_CFG_EW_MASK | \ +R_SLOT_CFG_IR_MASK | \ +R_SLOT_CFG_IW_MASK | \ +R_SLOT_CFG_LOCK_MASK ) + +#define SLOT_CFG_MASK \ + (R_SLOT_CFG_A_MASK | (SLOT0_CFG_MASK )) + +#define WGC_SLOT_END(nslots) \ + (A_WGC_SLOT + SLOT_SIZE * (nslots + 1)) + +/* wgChecker slot is 4K alignment */ +#define WG_ALIGNED_SIZE (1 << 12) +#define WG_ALIGNED_MASK MAKE_64BIT_MASK(0, 12) + +/* wgChecker slot address is (addr / 4). */ +#define TO_SLOT_ADDR (addr) ((addr) >> 2) +#define FROM_SLOT_ADDR (addr) ((addr) << 2) + +/* wgChecker slot cfg.A[1:0] */ +#define A_OFF 0 +#define A_TOR 1 +#define A_NA4 2 +#define A_NAPOT 3 + +/* wgChecker slot perm */ +#define WGC_PERM (wid, perm) ((uint64_t)(perm) << (2 * (wid))) +#define P_READ (1 << 0) +#define P_WRITE (1 << 1)/* * Accesses only reach these read and write functions if the wgChecker
@@ -146,6 +192,28 @@ static uint64_t riscv_wgchecker_readq(void *opaque, hwaddr addr)RISCVWgCheckerState *s =RISCV_WGCHECKER (opaque);uint64_t val =0 ; +if ((addr >=A_WGC_SLOT ) && (addr <WGC_SLOT_END (s->slot_count))) { +/* Read from WGC slot */ +int slot_id = (addr -A_WGC_SLOT ) /SLOT_SIZE ; +int slot_offset = (addr -A_WGC_SLOT ) %SLOT_SIZE ; + +switch (slot_offset) { +case A_SLOT_ADDR : + val = s->slots[slot_id].addr; +break ; +case A_SLOT_PERM : + val = s->slots[slot_id].perm; +break ; +default : +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Unexpected memory access to (0x%" HWADDR_PRIX ", %u) \n" , + __func__, addr,8 ); +break ; + } + +return val; + } +switch (addr) {case A_ERRCAUSE : val = s->errcause &ERRCAUSE_MASK ;
@@ -171,6 +239,37 @@ static uint64_t riscv_wgchecker_readl(void *opaque, hwaddr addr)RISCVWgCheckerState *s =RISCV_WGCHECKER (opaque);uint64_t val =0 ; +if ((addr >=A_WGC_SLOT ) && (addr <WGC_SLOT_END (s->slot_count))) { +/* Read from WGC slot */ +int slot_id = (addr -A_WGC_SLOT ) /SLOT_SIZE ; +int slot_offset = (addr -A_WGC_SLOT ) %SLOT_SIZE ; + +switch (slot_offset) { +case A_SLOT_ADDR : + val =extract64 (s->slots[slot_id].addr,0 ,32 ); +break ; +case A_SLOT_ADDR +4 : + val =extract64 (s->slots[slot_id].addr,32 ,32 ); +break ; +case A_SLOT_PERM : + val =extract64 (s->slots[slot_id].perm,0 ,32 ); +break ; +case A_SLOT_PERM +4 : + val =extract64 (s->slots[slot_id].perm,32 ,32 ); +break ; +case A_SLOT_CFG : + val = s->slots[slot_id].cfg &SLOT_CFG_MASK ; +break ; +default : +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Unexpected memory access to (0x%" HWADDR_PRIX ", %u) \n" , + __func__, addr,4 ); +break ; + } + +return val; + } +switch (addr) {case A_VENDOR : val =0 ;
@@ -228,11 +327,121 @@ static uint64_t riscv_wgchecker_read(void *opaque, hwaddr addr, unsigned size)return val; } +/* +* Validate the WGC slot address is between address range. +* +* Fix the slot address to the start address if it's not within the address range. +* We need validation when changing "slot address" or "TOR/NAPOT mode (cfg.A)" +*/ +static void validate_slot_address (void *opaque,int slot_id) +{ +RISCVWgCheckerState *s =RISCV_WGCHECKER (opaque); +uint64_t start =TO_SLOT_ADDR (s->addr_range_start); +uint64_t end =TO_SLOT_ADDR (s->addr_range_start + s->addr_range_size); +uint32_t cfg_a =FIELD_EX32 (s->slots[slot_id].cfg,SLOT_CFG ,A ); + +/* First and last slot address are hard-coded. */ +if ((slot_id ==0 ) || (slot_id == s->slot_count)) { +return ; + } + +/* Check WGC slot address is between address range. */ +if ((s->slots[slot_id].addr < start) || (s->slots[slot_id].addr >= end)) { + s->slots[slot_id].addr = start; + } + +/* Check WGC slot is 4k-aligned. */ +if (cfg_a ==A_TOR ) { + s->slots[slot_id].addr &= ~TO_SLOT_ADDR (WG_ALIGNED_MASK ); + }else if (cfg_a ==A_NAPOT ) { + s->slots[slot_id].addr |=TO_SLOT_ADDR (WG_ALIGNED_MASK >>1 ); + }else if (cfg_a ==A_NA4 ) { +/* Forcely replace NA4 slot with 4K-aligned NAPOT slot. */ +FIELD_DP32 (s->slots[slot_id].cfg,SLOT_CFG ,A ,A_NAPOT ); + s->slots[slot_id].addr |=TO_SLOT_ADDR (WG_ALIGNED_MASK >>1 ); + } +} + +static bool slots_reg_is_ro (int slot_id,int slot_offset,uint32_t nslots) +{ +/* +* Special slots: +* - slot[0]: +* - addr is RO +* - perm is RO +* - cfg.A is OFF +* +* - slot[nslots]: +* - addr is RO +* - cfg.A is OFF or TOR +*/ +if (slot_id ==0 ) { +switch (slot_offset) { +case A_SLOT_ADDR : +case A_SLOT_ADDR +4 : +case A_SLOT_PERM : +case A_SLOT_PERM +4 : +return true; +default : +break ; + } + }else if (slot_id == nslots) { +switch (slot_offset) { +case A_SLOT_ADDR : +case A_SLOT_ADDR +4 : +return true; +default : +break ; + } + } + +return false; +} +static void riscv_wgchecker_writeq (void *opaque,hwaddr addr,uint64_t value) {RISCVWgCheckerState *s =RISCV_WGCHECKER (opaque); +if ((addr >=A_WGC_SLOT ) && (addr <WGC_SLOT_END (s->slot_count))) { +/* Read from WGC slot */ +int slot_id = (addr -A_WGC_SLOT ) /SLOT_SIZE ; +int slot_offset = (addr -A_WGC_SLOT ) %SLOT_SIZE ; +bool locked =FIELD_EX32 (s->slots[slot_id].cfg,SLOT_CFG ,LOCK ); + +if (locked) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Couldn't write access to locked wgChecker Slot: " +"slot = %d, offset = %d\n" , __func__, slot_id, + slot_offset); +return ; + } + +if (slots_reg_is_ro (slot_id, slot_offset, s->slot_count)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Unexpected memory access to (0x%" HWADDR_PRIX ", %u) \n" , + __func__, addr,8 ); + } + +switch (slot_offset) { +case A_SLOT_ADDR : + s->slots[slot_id].addr = value; +validate_slot_address (s, slot_id); +break ; +case A_SLOT_PERM : + value &= wgc_slot_perm_mask; + s->slots[slot_id].perm = value; +break ; +default : +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Unexpected memory access to (0x%" HWADDR_PRIX ", %u) \n" , + __func__, addr,8 ); +break ; + } + +return ; + } +switch (addr) {case A_ERRCAUSE : s->errcause = value &ERRCAUSE_MASK ;
@@ -253,6 +462,81 @@ static void riscv_wgchecker_writel(void *opaque, hwaddr addr, {RISCVWgCheckerState *s =RISCV_WGCHECKER (opaque); +if ((addr >=A_WGC_SLOT ) && (addr <WGC_SLOT_END (s->slot_count))) { +/* Write to WGC slot */ +int slot_id = (addr -A_WGC_SLOT ) /SLOT_SIZE ; +int slot_offset = (addr -A_WGC_SLOT ) %SLOT_SIZE ; +bool locked =FIELD_EX32 (s->slots[slot_id].cfg,SLOT_CFG ,LOCK ); +int cfg_a, old_cfg_a; + +if (locked) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Couldn't write access to locked wgChecker Slot: " +"slot = %d, offset = %d\n" , __func__, slot_id, + slot_offset); +return ; + } + +if (slots_reg_is_ro (slot_id, slot_offset, s->slot_count)) { +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Unexpected memory access to (0x%" HWADDR_PRIX ", %u) \n" , + __func__, addr,4 ); + } + +switch (slot_offset) { +case A_SLOT_ADDR : + s->slots[slot_id].addr =deposit64 ( + s->slots[slot_id].addr,0 ,32 , value); +validate_slot_address (s, slot_id); +break ; +case A_SLOT_ADDR +4 : + s->slots[slot_id].addr =deposit64 ( + s->slots[slot_id].addr,32 ,32 , value); +validate_slot_address (s, slot_id); +break ; +case A_SLOT_PERM : + value &= wgc_slot_perm_mask; + s->slots[slot_id].perm =deposit64 ( + s->slots[slot_id].perm,0 ,32 , value); +break ; +case A_SLOT_PERM +4 : + value &=extract64 (wgc_slot_perm_mask,32 ,32 ); + s->slots[slot_id].perm =deposit64 ( + s->slots[slot_id].perm,32 ,32 , value); +break ; +case A_SLOT_CFG : +if (slot_id ==0 ) { + value &=SLOT0_CFG_MASK ; + s->slots[0 ].cfg = value; + }else if (slot_id == s->slot_count) { + old_cfg_a =FIELD_EX32 (s->slots[s->slot_count].cfg,SLOT_CFG ,A ); + cfg_a =FIELD_EX32 (value,SLOT_CFG ,A ); + + value &=SLOT0_CFG_MASK ; +if ((cfg_a ==A_OFF ) || (cfg_a ==A_TOR )) { + value |= cfg_a; + }else { +/* slot[nslots] could only use OFF or TOR config. */ + value |= old_cfg_a; + } + s->slots[s->slot_count].cfg = value; + +validate_slot_address (s, slot_id); + }else { + value &=SLOT_CFG_MASK ; + s->slots[slot_id].cfg = value; + } +break ; +default : +qemu_log_mask (LOG_GUEST_ERROR , +"%s: Unexpected memory access to (0x%" HWADDR_PRIX ", %u) \n" , + __func__, addr,4 ); +break ; + } + +return ; + } +switch (addr) {case A_ERRCAUSE : value &=extract64 (ERRCAUSE_MASK ,0 ,32 );
@@ -461,6 +745,8 @@ static void riscv_wgchecker_realize(DeviceState *dev, Error **errp)return ; } + s->slots =g_new0 (WgCheckerSlot, s->slot_count +1 ); +memory_region_init_io (&s->mmio,OBJECT (dev), &riscv_wgchecker_ops, s,TYPE_RISCV_WGCHECKER , s->mmio_size);sysbus_init_mmio (SYS_BUS_DEVICE (dev), &s->mmio);
@@ -524,6 +810,37 @@ static void riscv_wgchecker_reset_enter(Object *obj, ResetType type) s->errcause =0 ; s->erraddr =0 ; + +for (int i =0 ; i < nslots; i++) { + s->slots[i].addr =TO_SLOT_ADDR (start); + s->slots[i].perm =0 ; + s->slots[i].cfg =0 ; + } + s->slots[nslots].addr =TO_SLOT_ADDR (end); + s->slots[nslots].perm =0 ; + s->slots[nslots].cfg =0 ; + +if (s->num_default_slots !=0 ) { +/* +* Use default slots: +* slot[0] is hard-coded to start address, so the default slots +* start from slot[1]. +*/ +memcpy (&s->slots[1 ], s->default_slots, +sizeof (WgCheckerSlot) * s->num_default_slots); + }else if ((s->hw_bypass) || + ((worldguard_config !=NULL ) && worldguard_config->hw_bypass)) { +/* HW bypass mode */ +uint32_t trustedwid = worldguard_config->trustedwid; + +if (trustedwid ==NO_TRUSTEDWID ) { + trustedwid = worldguard_config->nworlds -1 ; + } + + s->slots[nslots].perm =WGC_PERM (trustedwid,P_READ |P_WRITE ); + s->slots[nslots].perm &= wgc_slot_perm_mask; + s->slots[nslots].cfg =A_TOR ; + } }static void riscv_wgchecker_class_init (ObjectClass *klass,void *data)
@@ -597,6 +914,19 @@ DeviceState *riscv_wgchecker_create(hwaddr addr, uint32_t size,qdev_prop_set_uint64 (dev, name_offset, region_offset[i]); } +if (num_default_slots > slot_count) { + num_default_slots = slot_count; + } + + s->num_default_slots = num_default_slots; +if (s->num_default_slots) { + s->default_slots =g_new0 (WgCheckerSlot, s->num_default_slots); +memcpy (s->default_slots, default_slots, +sizeof (WgCheckerSlot) * s->num_default_slots); + }else { + s->default_slots =NULL ; + } +sysbus_realize_and_unref (SYS_BUS_DEVICE (dev), &error_fatal);sysbus_mmio_map (SYS_BUS_DEVICE (dev),0 , addr);sysbus_connect_irq (SYS_BUS_DEVICE (dev),0 , irq);
hw/misc/riscv_worldguard.c +3 -0
@@ -38,6 +38,9 @@*/ struct RISCVWorldGuardState *worldguard_config =NULL ; +/* perm field bitmask of wgChecker slot, it's depends on NWorld. */ +uint64_t wgc_slot_perm_mask =0 ; +static Property riscv_worldguard_properties[] = {DEFINE_PROP_UINT32 ("nworlds" , RISCVWorldGuardState, nworlds,0 ),
include/hw/misc/riscv_worldguard.h +4 -0
@@ -45,6 +45,7 @@ struct RISCVWorldGuardState { };extern struct RISCVWorldGuardState *worldguard_config; +extern uint64_t wgc_slot_perm_mask;DeviceState *riscv_worldguard_create (uint32_t nworlds,uint32_t trustedwid,bool hw_bypass,bool tz_compat);
@@ -86,9 +87,12 @@ struct WgCheckerRegion {struct RISCVWgCheckerState {/*< private >*/ SysBusDevice parent_obj; +uint32_t num_default_slots; +WgCheckerSlot *default_slots;/*< public >*/ MemoryRegion mmio; +WgCheckerSlot *slots;qemu_irq irq;/* error reg */
Signature · 1 line
2.17.1
RFC PATCH 14/16 hw/misc: riscv_wgchecker: Implement correct block-access behavior
In reply to Jim ShuThe wgChecker is configurable for whether blocked accesses:
* should cause a bus error or just read return zero and write ignore
* should generate the interrupt or not
--- hw/misc/riscv_wgchecker.c | 169 +++++++++++++++++++++++++++++++++++++- 1 file changed, 167 insertions(+), 2 deletions(-)
hw/misc/riscv_wgchecker.c +167 -2
@@ -100,6 +100,169 @@ REG32(SLOT_CFG, 0x010)#define P_READ (1 << 0)#define P_WRITE (1 << 1) +static void decode_napot (hwaddr a,hwaddr *sa,hwaddr *ea) +{ +/* +* aaaa...aaa0 8-byte NAPOT range +* aaaa...aa01 16-byte NAPOT range +* aaaa...a011 32-byte NAPOT range +* ... +* aa01...1111 2^XLEN-byte NAPOT range +* a011...1111 2^(XLEN+1)-byte NAPOT range +* 0111...1111 2^(XLEN+2)-byte NAPOT range +* 1111...1111 Reserved +*/ + + a =FROM_SLOT_ADDR (a) |0x3 ; + +if (sa) { + *sa = a & (a +1 ); + } +if (ea) { + *ea = a | (a +1 ); + } +} + +typedef struct WgAccessResult WgAccessResult ; +struct WgAccessResult { +bool is_success; +bool has_bus_error; +bool has_interrupt; +uint8_t perm:2 ; +}; + +static WgAccessResult wgc_check_access ( +RISCVWgCheckerState *s,hwaddr phys_addr,uint32_t wid,bool is_write) +{ +WgCheckerSlot *slot, *prev_slot; +uint32_t cfg_a, prev_cfg_a; +uint64_t start, end; +int slot_id, wgc_perm =0 ; +WgAccessResult result = {0 }; + +bool is_matching = false; +bool slot0_be, slot0_ip; +bool matched_slot_be = false, matched_slot_ip = false; + +for (slot_id =0 ; slot_id < s->slot_count; slot_id++) { + slot = &s->slots[slot_id +1 ]; + cfg_a =FIELD_EX32 (slot->cfg,SLOT_CFG ,A ); + +if (cfg_a ==A_TOR ) { + prev_slot = &s->slots[slot_id]; + + prev_cfg_a =FIELD_EX32 (prev_slot->cfg,SLOT_CFG ,A ); +if (prev_cfg_a ==A_NA4 ) { + start =FROM_SLOT_ADDR (prev_slot->addr) +4 ; + }else if (prev_cfg_a ==A_NAPOT ) { +decode_napot (prev_slot->addr,NULL , &start); + start +=1 ; + }else {/* A_TOR or A_OFF */ + start =FROM_SLOT_ADDR (prev_slot->addr); + } + end =FROM_SLOT_ADDR (slot->addr); + }else if (cfg_a ==A_NA4 ) { + start =FROM_SLOT_ADDR (slot->addr); + end = start +4 ; + }else if (cfg_a ==A_NAPOT ) { +decode_napot (slot->addr, &start, &end); + end +=1 ; + }else { +/* A_OFF: not in slot range. */ +continue ; + } + +/* wgChecker slot range is between start to (end - 1). */ +if ((start <= phys_addr) && (phys_addr < end)) { +/* Match the wgC slot */ +int perm = ((slot->perm >> (wid *2 )) &0x3 ); + +/* If any matching rule permits access, the access is permitted. */ + wgc_perm |= perm; + +/* +* If any matching rule wants to report error (IRQ or Bus Error), +* the denied access should report error. +*/ + is_matching = true; +if (is_write) { + matched_slot_be |=FIELD_EX64 (slot->cfg,SLOT_CFG ,EW ); + matched_slot_ip |=FIELD_EX64 (slot->cfg,SLOT_CFG ,IW ); + }else { + matched_slot_be |=FIELD_EX64 (slot->cfg,SLOT_CFG ,ER ); + matched_slot_ip |=FIELD_EX64 (slot->cfg,SLOT_CFG ,IR ); + } + } + } + +/* If no matching rule, error reporting depends on the slot0's config. */ +if (is_write) { + slot0_be =FIELD_EX64 (s->slots[0 ].cfg,SLOT_CFG ,EW ); + slot0_ip =FIELD_EX64 (s->slots[0 ].cfg,SLOT_CFG ,IW ); + }else { + slot0_be =FIELD_EX64 (s->slots[0 ].cfg,SLOT_CFG ,ER ); + slot0_ip =FIELD_EX64 (s->slots[0 ].cfg,SLOT_CFG ,IR ); + } + + result.is_success = is_write ? (wgc_perm &P_WRITE ) : (wgc_perm &P_READ ); + result.perm = wgc_perm; +if (!result.is_success) { +if (is_matching) { + result.has_bus_error = matched_slot_be; + result.has_interrupt = matched_slot_ip; + }else { + result.has_bus_error = slot0_be; + result.has_interrupt = slot0_ip; + } + } + +return result; +} + +static MemTxResult riscv_wgc_handle_blocked_access ( +WgCheckerRegion *region,hwaddr addr,uint32_t wid,bool is_write) +{ +RISCVWgCheckerState *s =RISCV_WGCHECKER (region->wgchecker); +bool be, ip; +WgAccessResult result; +hwaddr phys_addr; + + be =FIELD_EX64 (s->errcause,ERRCAUSE ,BE ); + ip =FIELD_EX64 (s->errcause,ERRCAUSE ,IP ); + phys_addr = addr + region->region_offset; + +/* +* Check if this blocked access trigger IRQ (Bus Error) or not. +* It depends on wgChecker slot config (cfg.IR/IW/ER/EW bits). +*/ + result =wgc_check_access (s, phys_addr, wid, is_write); + +if (!be && !ip) { +/* +* With either of the be or ip bits is set, further violations do not +* update the errcause or erraddr registers. Also, new interrupts +* cannot be generated until the be and ip fields are cleared. +*/ +if (result.has_interrupt || result.has_bus_error) { + s->errcause =FIELD_DP64 (s->errcause,ERRCAUSE ,WID , wid); + s->errcause =FIELD_DP64 (s->errcause,ERRCAUSE ,R , !is_write); + s->errcause =FIELD_DP64 (s->errcause,ERRCAUSE ,W , is_write); + s->erraddr =TO_SLOT_ADDR (phys_addr); + } + +if (result.has_interrupt) { + s->errcause =FIELD_DP64 (s->errcause,ERRCAUSE ,IP ,1 ); +qemu_irq_raise (s->irq); + } + +if (result.has_bus_error) { + s->errcause =FIELD_DP64 (s->errcause,ERRCAUSE ,BE ,1 ); + } + } + +return result.has_bus_error ?MEMTX_ERROR :MEMTX_OK ; +} +/* * Accesses only reach these read and write functions if the wgChecker * is blocking them; non-blocked accesses go directly to the downstream
@@ -109,23 +272,25 @@ static MemTxResult riscv_wgc_mem_blocked_read(void *opaque, hwaddr addr,uint64_t *pdata, unsigned size, MemTxAttrs attrs) { + WgCheckerRegion *region = opaque;uint32_t wid =mem_attrs_to_wid (attrs);trace_riscv_wgc_mem_blocked_read (addr, size, wid); *pdata =0 ; -return MEMTX_OK ; +return riscv_wgc_handle_blocked_access (region, addr, wid, false); }static MemTxResult riscv_wgc_mem_blocked_write (void *opaque,hwaddr addr,uint64_t value,unsigned size,MemTxAttrs attrs) { +WgCheckerRegion *region = opaque;uint32_t wid =mem_attrs_to_wid (attrs);trace_riscv_wgc_mem_blocked_write (addr, value, size, wid); -return MEMTX_OK ; +return riscv_wgc_handle_blocked_access (region, addr, wid, true); } static const MemoryRegionOps riscv_wgc_mem_blocked_ops = {
Signature · 1 line
2.17.1
RFC PATCH 15/16 hw/misc: riscv_wgchecker: Check the slot settings in translate
In reply to Jim ShuThe final part of wgChecker we need to implement is actually using the wgChecker slots programmed by guest to determine whether to block the transaction or not.
Since this means we now change transaction mappings when the guest writes to wgChecker slots, we must also call the IOMMU notifiers at that point.
One tricky part here is that the perm of 'blocked_io_as' is the condition of deny access. For example, if wgChecker only permits RO access, the perm of 'downstream_as' will be IOMMU_RO and the perm of 'blocked_io_as' will be IOMMU_WO.
--- hw/misc/riscv_wgchecker.c | 70 ++++++++++++++++++++++++++++++++++++--- hw/misc/trace-events | 1 + 2 files changed, 67 insertions(+), 4 deletions(-)
hw/misc/riscv_wgchecker.c +66 -4
@@ -100,6 +100,52 @@ REG32(SLOT_CFG, 0x010)#define P_READ (1 << 0)#define P_WRITE (1 << 1) +static IOMMUAccessFlags wgc_perm_to_iommu_flags (int wgc_perm) +{ +if (wgc_perm == (P_READ |P_WRITE )) { +return IOMMU_RW ; + }else if (wgc_perm &P_WRITE ) { +return IOMMU_WO ; + }else if (wgc_perm &P_READ ) { +return IOMMU_RO ; + }else { +return IOMMU_NONE ; + } +} + +static void wgchecker_iommu_notify_all (RISCVWgCheckerState *s) +{ +/* +* Do tlb_flush() to whole address space via memory_region_notify_iommu() +* when wgChecker changes it's config. +*/ + +IOMMUTLBEvent event = { + .entry = { + .addr_mask =-1ULL , + } + }; + +trace_riscv_wgc_iommu_notify_all (); + +for (int i=0 ; i<WGC_NUM_REGIONS ; i++) { +WgCheckerRegion *region = &s->mem_regions[i]; +uint32_t nworlds = worldguard_config->nworlds; + +if (!region->downstream) { +continue ; + } + event.entry.iova =0 ; + event.entry.translated_addr =0 ; + event.type =IOMMU_NOTIFIER_UNMAP ; + event.entry.perm =IOMMU_NONE ; + +for (int wid=0 ; wid<nworlds; wid++) { +memory_region_notify_iommu (®ion->upstream, wid, event); + } + } +} +static void decode_napot (hwaddr a,hwaddr *sa,hwaddr *ea) {/*
@@ -309,6 +355,9 @@ static IOMMUTLBEntry riscv_wgc_translate(IOMMUMemoryRegion *iommu, {WgCheckerRegion *region =container_of (iommu, WgCheckerRegion, upstream);RISCVWgCheckerState *s =RISCV_WGCHECKER (region->wgchecker); +bool is_write; +WgAccessResult result; +int wgc_perm;hwaddr phys_addr;uint64_t region_size;
@@ -327,18 +376,25 @@ static IOMMUTLBEntry riscv_wgc_translate(IOMMUMemoryRegion *iommu,* Look at the wgChecker configuration for this address, and * return a TLB entry directing the transaction at either * downstream_as or blocked_io_as, as appropriate. -* For the moment, always permit accesses. */ /* Use physical address instead of offset */ phys_addr = addr + region->region_offset; + is_write = (flags ==IOMMU_WO ); - is_success = true; + result =wgc_check_access (s, phys_addr, iommu_idx, is_write);trace_riscv_wgc_translate (phys_addr, flags, - iommu_idx, is_success ?"pass" :"block" ); + iommu_idx, result.is_success ?"pass" :"block" ); - ret.target_as = is_success ? ®ion->downstream_as : ®ion->blocked_io_as; + wgc_perm = result.perm; +if (!result.is_success) { +/* if target_as is blocked_io_as, the perm is the condition of deny access. */ + wgc_perm ^= (P_READ |P_WRITE ); + } + ret.perm =wgc_perm_to_iommu_flags (wgc_perm); + + ret.target_as = result.is_success ? ®ion->downstream_as : ®ion->blocked_io_as;return ret; }
@@ -604,6 +660,9 @@ static void riscv_wgchecker_writeq(void *opaque, hwaddr addr,break ; } +/* Flush softmmu TLB when wgChecker changes config. */ +wgchecker_iommu_notify_all (s); +return ; }
@@ -699,6 +758,9 @@ static void riscv_wgchecker_writel(void *opaque, hwaddr addr,break ; } +/* Flush softmmu TLB when wgChecker changes config. */ +wgchecker_iommu_notify_all (s); +return ; }
hw/misc/trace-events +1 -0
@@ -359,3 +359,4 @@ riscv_wgchecker_mmio_write(uint64_t base, uint64_t offset, unsigned int size, ui riscv_wgc_mem_blocked_read(uint64_t addr, unsigned size, uint32_t wid) "wgChecker blocked read: offset 0x%" PRIx64 " size %u wid %" PRIu32 riscv_wgc_mem_blocked_write(uint64_t addr, uint64_t data, unsigned size, uint32_t wid) "wgChecker blocked write: offset 0x%" PRIx64 " data 0x%" PRIx64 " size %u wid %" PRIu32 riscv_wgc_translate(uint64_t addr, int flags, int wid, const char *res) "wgChecker translate: addr 0x%016" PRIx64 " flags 0x%x wid %d: %s" +riscv_wgc_iommu_notify_all(void) "wgChecker iommu: notifying UNMAP for all"
Signature · 1 line
2.17.1
RFC PATCH 16/16 hw/riscv: virt: Add WorldGuard support
In reply to Jim Shu* Add 'wg=on' option to enable RISC-V WorldGuard
* Add wgChecker to protect several resources:
DRAM, FLASH, UART.
--- docs/system/riscv/virt.rst | 10 +++ hw/riscv/Kconfig | 1 + hw/riscv/virt.c | 163 ++++++++++++++++++++++++++++++++++++- include/hw/riscv/virt.h | 17 +++- 4 files changed, 186 insertions(+), 5 deletions(-)
docs/system/riscv/virt.rst +10 -0
@@ -116,6 +116,16 @@ The following machine-specific options are supported: having AIA IMSIC (i.e. "aia=aplic-imsic" selected). When not specified, the default number of per-HART VS-level AIA IMSIC pages is 0. +- wg=[on|off] + + When this option is "on", RISC-V WorldGuard will be enabled in the system + to provide the isolation of multiple worlds. RISC-V HARTS will enable WG + extensions to have WID in memory transaction. wgCheckers in front of RAMs + and device MMIO will be enabled to provide the access control of resources + if the transaction contains WID. When not specified, this option is assumed + to be "off". + This option is restricted to the TCG accelerator. + Running Linux kernel --------------------
hw/riscv/Kconfig +1 -0
@@ -56,6 +56,7 @@ config RISCV_VIRT select PLATFORM_BUS select ACPI select ACPI_PCI + select RISCV_WORLDGUARD config SHAKTI_C bool
hw/riscv/virt.c +160 -3
248 lines not on this page · raw message
include/hw/riscv/virt.h +15 -2
@@ -57,6 +57,7 @@ struct RISCVVirtState {bool have_aclint;RISCVVirtAIAType aia_type;int aia_guests; +bool have_wg;char *oem_id;char *oem_table_id;OnOffAuto acpi;
@@ -84,12 +85,18 @@ enum {VIRT_PCIE_MMIO ,VIRT_PCIE_PIO ,VIRT_PLATFORM_BUS , -VIRT_PCIE_ECAM +VIRT_PCIE_ECAM , +VIRT_WGC_DRAM , +VIRT_WGC_FLASH , +VIRT_WGC_UART };enum {UART0_IRQ =10 ,RTC_IRQ =11 , +WGC_DRAM_IRQ =15 , +WGC_FLASH_IRQ =16 , +WGC_UART_IRQ =17 ,VIRTIO_IRQ =1 ,/* 1 to 8 */ VIRTIO_COUNT =8 , PCIE_IRQ = 0x20, /* 32 to 35 */
@@ -99,7 +106,7 @@ enum {#define VIRT_PLATFORM_BUS_NUM_IRQS 32#define VIRT_IRQCHIP_NUM_MSIS 255 -#define VIRT_IRQCHIP_NUM_SOURCES 96 +#define VIRT_IRQCHIP_NUM_SOURCES 128#define VIRT_IRQCHIP_NUM_PRIO_BITS 3#define VIRT_IRQCHIP_MAX_GUESTS_BITS 3#define VIRT_IRQCHIP_MAX_GUESTS ((1U << VIRT_IRQCHIP_MAX_GUESTS_BITS) - 1U)
@@ -153,4 +160,10 @@ uint32_t imsic_num_bits(uint32_t count);#error "Can't accommodate all IMSIC groups in address space"#endif +/* WorldGuard */ +#define VIRT_WG_NWORLDS 4 +#define VIRT_WG_TRUSTEDWID 3 +#define VIRT_WG_HWBYPASS true +#define VIRT_WG_TZCOMPAT false +#endif
Signature · 1 line
2.17.1
Re: [RFC PATCH 16/16] hw/riscv: virt: Add WorldGuard support
Quoted 20 lines · Jim Shu
* Add 'wg=on' option to enable RISC-V WorldGuard
* Add wgChecker to protect several resources:
DRAM, FLASH, UART.Signed-off-by Jim Shu <[email protected]>--- docs/system/riscv/virt.rst | 10 +++ hw/riscv/Kconfig | 1 + hw/riscv/virt.c | 163 ++++++++++++++++++++++++++++++++++++- include/hw/riscv/virt.h | 17 +++- 4 files changed, 186 insertions(+), 5 deletions(-)docs/system/riscv/virt.rst +0
-0@@ -116,6 +116,16 @@ The following machine-specific options are supported: having AIA IMSIC (i.e. "aia=aplic-imsic" selected). When not specified, the default number of per-HART VS-level AIA IMSIC pages is 0.
docs/system/riscv/virt.rst The following machine-specific options are supported: · line 127
Quoted 10 lines
docs/system/riscv/virt.rst +9
-0+- wg=[on|off] + + When this option is "on", RISC-V WorldGuard will be enabled in the system + to provide the isolation of multiple worlds. RISC-V HARTS will enable WG + extensions to have WID in memory transaction. wgCheckers in front of RAMs + and device MMIO will be enabled to provide the access control of resources + if the transaction contains WID. When not specified, this option is assumed + to be "off". + This option is restricted to the TCG accelerator.
We need a lot more documentation here.
The WID of M-mode for example is set by an external environment. How are users going to set that in QEMU?
Are the Smwg, Smwgd, and Sswg extensions enabled? Or is it hard coded one world per hart?
We should make it clear to users how this is setup as the spec leaves a lot of this up to implementations. We also don't expect users to read the code or commit messages to figure it out.
Alistair
Quoted 337 lines
docs/system/riscv/virt.rst +1
-0+ Running Linux kernel --------------------hw/riscv/Kconfig +1
-0@@ -56,6 +56,7 @@ config RISCV_VIRT select PLATFORM_BUS select ACPI select ACPI_PCI + select RISCV_WORLDGUARD config SHAKTI_C boolhw/riscv/virt.c +160
-3@@ -55,6 +55,7 @@#include "hw/acpi/aml-build.h" #include "qapi/qapi-visit-common.h" #include "hw/virtio/virtio-iommu.h" +#include "hw/misc/riscv_worldguard.h" /* KVM AIA only supports APLIC MSI. APLIC Wired is always emulated by QEMU. */ static bool virt_use_kvm_aia (RISCVVirtState *s)@@ -76,6 +77,9 @@ static const MemMapEntry virt_memmap[] = { [VIRT_ACLINT_SSWI] = { 0x2F00000, 0x4000 }, [VIRT_PCIE_PIO] = { 0x3000000, 0x10000 }, [VIRT_PLATFORM_BUS] = { 0x4000000, 0x2000000 }, + [VIRT_WGC_DRAM] = { 0x6000000, 0x1000 }, + [VIRT_WGC_FLASH] = { 0x6001000, 0x1000 }, + [VIRT_WGC_UART] = { 0x6002000, 0x1000 }, [VIRT_PLIC] = { 0xc000000, VIRT_PLIC_SIZE(VIRT_CPUS_MAX * 2) }, [VIRT_APLIC_M] = { 0xc000000, APLIC_SIZE(VIRT_CPUS_MAX) }, [VIRT_APLIC_S] = { 0xd000000, APLIC_SIZE(VIRT_CPUS_MAX) },@@ -101,6 +105,38 @@ static MemMapEntry virt_high_pcie_memmap;#define VIRT_FLASH_SECTOR_SIZE (256 * KiB) +/* wgChecker helpers */ +typedef struct WGCInfo { +int memmap_idx; +uint32_t irq_num; +uint32_t slot_count; + +int num_of_child; +MemoryRegion *c_region[WGC_NUM_REGIONS ]; +uint64_t c_offset[WGC_NUM_REGIONS ]; +}WGCInfo ; + +enum { +WGC_DRAM , +WGC_FLASH , +WGC_UART , +WGC_NUM , +}; + +static WGCInfo virt_wgcinfo[] = { + [WGC_DRAM ] = {VIRT_WGC_DRAM ,WGC_DRAM_IRQ ,16 }, + [WGC_FLASH ] = {VIRT_WGC_FLASH ,WGC_FLASH_IRQ ,16 }, + [WGC_UART ] = {VIRT_WGC_UART ,WGC_UART_IRQ ,1 }, +}; + +static void wgc_append_child (WGCInfo *info,MemoryRegion *region, +uint64_t offset) +{ + info->c_region[info->num_of_child] = region; + info->c_offset[info->num_of_child] = offset; + info->num_of_child +=1 ; +} +static PFlashCFI01 *virt_flash_create1 (RISCVVirtState *s,const char *name,const char *alias_prop_name)@@ -151,7 +187,8 @@ static void virt_flash_map1(PFlashCFI01 *flash, }static void virt_flash_map (RISCVVirtState *s, -MemoryRegion *sysmem) +MemoryRegion *sysmem, +WGCInfo *info) {hwaddr flashsize = virt_memmap[VIRT_FLASH ].size /2 ;hwaddr flashbase = virt_memmap[VIRT_FLASH ].base;@@ -160,6 +197,15 @@ static void virt_flash_map(RISCVVirtState *s, sysmem);virt_flash_map1 (s->flash[1 ], flashbase + flashsize, flashsize, sysmem); + +if (info) { +wgc_append_child (info, +sysbus_mmio_get_region (SYS_BUS_DEVICE (s->flash[0 ]),0 ), + flashbase); +wgc_append_child (info, +sysbus_mmio_get_region (SYS_BUS_DEVICE (s->flash[1 ]),0 ), + flashbase + flashsize); + } } static void create_pcie_irq_map(RISCVVirtState *s, void *fdt, char *nodename,@@ -1303,6 +1349,71 @@ static void virt_build_smbios(RISCVVirtState *s) } } +static DeviceState *create_wgc (WGCInfo *info,DeviceState *irqchip) +{ +MemoryRegion *system_memory =get_system_memory (); +DeviceState *wgc; +MemoryRegion *upstream_mr, *downstream_mr; +qemu_irq irq =qdev_get_gpio_in (irqchip, info->irq_num); +hwaddr base, size; + +/* Unmap downstream_mr from system_memory if it is already mapped. */ +for (int i=0 ; i<info->num_of_child; i++) { + downstream_mr = info->c_region[i]; + +g_assert (downstream_mr); +if (downstream_mr->container == system_memory) { +memory_region_del_subregion (system_memory, downstream_mr); + } + +/* +* Clear the offset of downstream_mr, so we could correctly do +* address_space_init() to it in wgchecker. +*/ +memory_region_set_address (downstream_mr,0 ); + } + + base = virt_memmap[info->memmap_idx].base; + size = virt_memmap[info->memmap_idx].size; + + wgc =riscv_wgchecker_create ( + base, size, irq, info->slot_count,0 ,0 , + info->num_of_child, info->c_region, info->c_offset,0 ,NULL ); + +/* Map upstream_mr to system_memory */ +for (int i=0 ; i<info->num_of_child; i++) { + upstream_mr =sysbus_mmio_get_region (SYS_BUS_DEVICE (wgc), i+1 ); +g_assert (upstream_mr); +memory_region_add_subregion (system_memory, info->c_offset[i], upstream_mr); + } + +return wgc; +} + +static void virt_create_worldguard (WGCInfo *wgcinfo,int wgc_num, +DeviceState *irqchip) +{ +CPUState *cpu; + +/* Global WG config */ +riscv_worldguard_create (VIRT_WG_NWORLDS , +VIRT_WG_TRUSTEDWID , +VIRT_WG_HWBYPASS , +VIRT_WG_TZCOMPAT ); + +/* Enable WG extension of each CPU */ +CPU_FOREACH (cpu) { +CPURISCVState *env = cpu ?cpu_env (cpu) :NULL ; + +riscv_worldguard_apply_cpu (env->mhartid); + } + +/* Create all wgChecker devices */ +for (int i=0 ; i<wgc_num; i++) { +create_wgc (&wgcinfo[i],DEVICE (irqchip)); + } +} +static void virt_machine_done (Notifier *notifier,void *data) { RISCVVirtState *s = container_of(notifier, RISCVVirtState,@@ -1401,10 +1512,12 @@ static void virt_machine_done(Notifier *notifier, void *data)static void virt_machine_init (MachineState *machine) {const MemMapEntry *memmap = virt_memmap; +WGCInfo *wgcinfo = virt_wgcinfo;RISCVVirtState *s =RISCV_VIRT_MACHINE (machine);MemoryRegion *system_memory =get_system_memory ();MemoryRegion *mask_rom =g_new (MemoryRegion,1 );DeviceState *mmio_irqchip, *virtio_irqchip, *pcie_irqchip; +SerialMM *uart;int i, base_hartid, hart_count;int socket_count =riscv_socket_count (machine);@@ -1420,6 +1533,11 @@ static void virt_machine_init(MachineState *machine)exit (1 ); } +if (!tcg_enabled () && s->have_wg) { +error_report ("'wg' is only available with TCG acceleration" ); +exit (1 ); + } +/* Initialize sockets */ mmio_irqchip = virtio_irqchip = pcie_irqchip =NULL ;for (i =0 ; i < socket_count; i++) {@@ -1547,6 +1665,10 @@ static void virt_machine_init(MachineState *machine)memory_region_add_subregion (system_memory, memmap[VIRT_DRAM ].base, machine->ram); +if (tcg_enabled () && s->have_wg) { +wgc_append_child (&wgcinfo[WGC_DRAM ], machine->ram, memmap[VIRT_DRAM ].base); + } +/* boot rom */ memory_region_init_rom (mask_rom,NULL ,"riscv_virt_board.mrom" , memmap[VIRT_MROM ].size, &error_fatal);@@ -1574,10 +1696,16 @@ static void virt_machine_init(MachineState *machine)create_platform_bus (s, mmio_irqchip); -serial_mm_init (system_memory, memmap[VIRT_UART0 ].base, + uart =serial_mm_init (system_memory, memmap[VIRT_UART0 ].base,0 ,qdev_get_gpio_in (mmio_irqchip,UART0_IRQ ),399193 ,serial_hd (0 ),DEVICE_LITTLE_ENDIAN ); +if (tcg_enabled () && s->have_wg) { +wgc_append_child (&wgcinfo[WGC_UART ], +sysbus_mmio_get_region (SYS_BUS_DEVICE (uart),0 ), + memmap[VIRT_UART0 ].base); + } +sysbus_create_simple ("goldfish_rtc" , memmap[VIRT_RTC ].base,qdev_get_gpio_in (mmio_irqchip,RTC_IRQ ));@@ -1586,7 +1714,16 @@ static void virt_machine_init(MachineState *machine)pflash_cfi01_legacy_drive (s->flash[i],drive_get (IF_PFLASH ,0 , i)); } -virt_flash_map (s, system_memory); + +if (tcg_enabled () && s->have_wg) { +virt_flash_map (s, system_memory, &wgcinfo[WGC_FLASH ]); + }else { +virt_flash_map (s, system_memory,NULL ); + } + +if (tcg_enabled () && s->have_wg) { +virt_create_worldguard (wgcinfo,WGC_NUM , mmio_irqchip); + }/* load/create device tree */ if (machine->dtb) {@@ -1614,6 +1751,20 @@ static void virt_machine_instance_init(Object *obj) s->acpi =ON_OFF_AUTO_AUTO ; } +static bool virt_get_wg (Object *obj,Error **errp) +{ +RISCVVirtState *s =RISCV_VIRT_MACHINE (obj); + +return s->have_wg; +} + +static void virt_set_wg (Object *obj,bool value,Error **errp) +{ +RISCVVirtState *s =RISCV_VIRT_MACHINE (obj); + + s->have_wg = value; +} +static char *virt_get_aia_guests (Object *obj,Error **errp) {RISCVVirtState *s =RISCV_VIRT_MACHINE (obj);@@ -1794,6 +1945,12 @@ static void virt_machine_class_init(ObjectClass *oc, void *data) NULL, NULL);object_class_property_set_description (oc,"acpi" ,"Enable ACPI" ); + +object_class_property_add_bool (oc,"wg" , virt_get_wg, + virt_set_wg); +object_class_property_set_description (oc,"wg" , +"Set on/off to enable/disable the " +"RISC-V WorldGuard." ); }static const TypeInfo virt_machine_typeinfo = {include/hw/riscv/virt.h +15
-3@@ -57,6 +57,7 @@ struct RISCVVirtState {bool have_aclint;RISCVVirtAIAType aia_type;int aia_guests; +bool have_wg;char *oem_id;char *oem_table_id;OnOffAuto acpi;@@ -84,12 +85,18 @@ enum {VIRT_PCIE_MMIO ,VIRT_PCIE_PIO ,VIRT_PLATFORM_BUS , -VIRT_PCIE_ECAM +VIRT_PCIE_ECAM , +VIRT_WGC_DRAM , +VIRT_WGC_FLASH , +VIRT_WGC_UART };enum {UART0_IRQ =10 ,RTC_IRQ =11 , +WGC_DRAM_IRQ =15 , +WGC_FLASH_IRQ =16 , +WGC_UART_IRQ =17 ,VIRTIO_IRQ =1 ,/* 1 to 8 */ VIRTIO_COUNT =8 , PCIE_IRQ = 0x20, /* 32 to 35 */@@ -99,7 +106,7 @@ enum {#define VIRT_PLATFORM_BUS_NUM_IRQS 32#define VIRT_IRQCHIP_NUM_MSIS 255 -#define VIRT_IRQCHIP_NUM_SOURCES 96 +#define VIRT_IRQCHIP_NUM_SOURCES 128#define VIRT_IRQCHIP_NUM_PRIO_BITS 3#define VIRT_IRQCHIP_MAX_GUESTS_BITS 3#define VIRT_IRQCHIP_MAX_GUESTS ((1U << VIRT_IRQCHIP_MAX_GUESTS_BITS) - 1U)@@ -153,4 +160,10 @@ uint32_t imsic_num_bits(uint32_t count);#error "Can't accommodate all IMSIC groups in address space"#endif +/* WorldGuard */ +#define VIRT_WG_NWORLDS 4 +#define VIRT_WG_TRUSTEDWID 3 +#define VIRT_WG_HWBYPASS true +#define VIRT_WG_TZCOMPAT false +#endif --2.17.1
Dear, Jim Shu
Thank you for your work!
Do you plan to work futher on this series? Seems like current version is no longer applicable to current master.
Also, I really wonder how did you test these changes? Like is there a way I could build some binary and run it in trusted mode? Should there be an SBI support for that?
With regards,
Pavel Skripkin