Support runtime configuration for per-VM's HGATP mode
Subsystems (4)
v7 cover Support runtime configuration for per-VM's HGATP mode
From: Fangyu Yu <[email protected]>
Currently, RISC-V KVM hardcodes the G-stage page table format (HGATP mode) to the maximum mode detected at boot time (e.g., SV57x4 if supported). but often such a wide GPA is unnecessary, just as a host sometimes doesn't need sv57.
This patch introduces per-VM configurability of the G-stage mode via a new KVM capability: KVM_CAP_RISCV_SET_HGATP_MODE. User-space can now explicitly request a specific HGATP mode (SV39x4, SV48x4, SV57x4 or SV32x4) during VM creation.
---
Changes in v7 (Anup's suggestions):
- Keep the original HGATP mode probing logic.
- Link to v6:
https://lore.kernel.org/linux-riscv/[email protected]/
---
Changes in v6 (Anup's suggestions):
- Reworked kvm_riscv_gstage_gpa_bits() and kvm_riscv_gstage_gpa_size() to take "unsigned long pgd_levels" instead of "struct kvm_arch *".
- Moved kvm_riscv_gstage_mode() helper from kvm_host.h to kvm_gstage.h.
- Renamed kvm->arch.kvm_riscv_gstage_pgd_levels to kvm->arch.pgd_levels.
- Added pgd_levels to struct kvm_gstage to avoid repeated gstage->kvm->arch pointer chasing.
- Link to v5:
https://lore.kernel.org/linux-riscv/[email protected]/
---
Changes in v5:
- Use architectural HGATP.MODE encodings as the bit index for the supported-mode bitmap and for the VM-mode selection UAPI; no new UAPI mode/bit defines are introduced(per Radim).
- Allow KVM_CAP_RISCV_SET_HGATP_MODE on RV32 as well(per Drew).
- Link to v4:
https://lore.kernel.org/linux-riscv/[email protected]/
---
Changes in v4:
- Extend kvm_riscv_gstage_mode_detect() to probe all HGATP.MODE values supported by the host and record them in a bitmask.
- Treat unexpected pgd_levels in kvm_riscv_gstage_mode() as an internal error (e.g. WARN_ON_ONCE())(per Radim).
- Move kvm_riscv_gstage_gpa_bits() and kvm_riscv_gstage_gpa_size() to header as static inline helpers(per Radim).
- Drop gstage_mode_user_initialized and Remove the kvm_debug() message from KVM_CAP_RISCV_SET_HGATP_MODE(per Radim).
- Link to v3:
https://lore.kernel.org/linux-riscv/[email protected]/
---
Changes in v3:
- Reworked the patch formatting (per Drew).
- Dropped kvm->arch.kvm_riscv_gstage_mode and derive HGATP.MODE from kvm_riscv_gstage_pgd_levels via a helper, avoiding redundant per-VM state(per Drew).
- Removed kvm_riscv_gstage_max_mode and keep only kvm_riscv_gstage_max_pgd_levels for host capability detection(per Drew).
- Other initialization and return value issues(per Drew).
- Enforce that KVM_CAP_RISCV_SET_HGATP_MODE can only be enabled before any vCPUs are created by rejecting the ioctl once kvm->created_vcpus is non-zero(per Radim).
- Add a memslot safety check and reject the capability unless kvm_are_all_memslots_empty(kvm) is true, ensuring the G-stage format is not changed after any memslots have been installed(per Radim).
- Link to v2:
https://lore.kernel.org/linux-riscv/[email protected]/
Fangyu Yu (4):
RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode
RISC-V: KVM: Cache gstage pgd_levels in struct kvm_gstage
RISC-V: KVM: Detect and expose supported HGATP G-stage modes
RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
Documentation/virt/kvm/api.rst | 27 ++++++++++ arch/riscv/include/asm/kvm_gstage.h | 58 ++++++++++++++++++--- arch/riscv/include/asm/kvm_host.h | 1 + arch/riscv/kvm/gstage.c | 78 ++++++++++++++++------------- arch/riscv/kvm/main.c | 12 ++--- arch/riscv/kvm/mmu.c | 70 ++++++++------------------ arch/riscv/kvm/vm.c | 20 ++++++-- arch/riscv/kvm/vmid.c | 3 +- include/uapi/linux/kvm.h | 1 + 9 files changed, 169 insertions(+), 101 deletions(-)
Signature · 1 line
2.50.1
PATCH v7 1/4 RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode
From: Fangyu Yu <[email protected]>
Introduces one per-VM architecture-specific fields to support runtime configuration of the G-stage page table format:
- kvm->arch.pgd_levels: the corresponding number of page table levels for the selected mode.
These fields replace the previous global variables kvm_riscv_gstage_mode and kvm_riscv_gstage_pgd_levels, enabling different virtual machines to independently select their G-stage page table format instead of being forced to share the maximum mode detected by the kernel at boot time.
--- arch/riscv/include/asm/kvm_gstage.h | 37 ++++++++++++---- arch/riscv/include/asm/kvm_host.h | 1 + arch/riscv/kvm/gstage.c | 65 ++++++++++++++--------------- arch/riscv/kvm/main.c | 12 +++--- arch/riscv/kvm/mmu.c | 20 +++++---- arch/riscv/kvm/vm.c | 2 +- arch/riscv/kvm/vmid.c | 3 +- 7 files changed, 83 insertions(+), 57 deletions(-)
arch/riscv/include/asm/kvm_gstage.h +30 -7
@@ -29,16 +29,22 @@ struct kvm_gstage_mapping {#define kvm_riscv_gstage_index_bits 10#endif -extern unsigned long kvm_riscv_gstage_mode; -extern unsigned long kvm_riscv_gstage_pgd_levels; +extern unsigned long kvm_riscv_gstage_max_pgd_levels;#define kvm_riscv_gstage_pgd_xbits 2#define kvm_riscv_gstage_pgd_size (1UL << (HGATP_PAGE_SHIFT + kvm_riscv_gstage_pgd_xbits)) -#define kvm_riscv_gstage_gpa_bits (HGATP_PAGE_SHIFT + \ - (kvm_riscv_gstage_pgd_levels * \ - kvm_riscv_gstage_index_bits) + \ - kvm_riscv_gstage_pgd_xbits) -#define kvm_riscv_gstage_gpa_size ((gpa_t)(1ULL << kvm_riscv_gstage_gpa_bits)) + +static inline unsigned long kvm_riscv_gstage_gpa_bits (unsigned long pgd_levels) +{ +return (HGATP_PAGE_SHIFT + + pgd_levels * kvm_riscv_gstage_index_bits + + kvm_riscv_gstage_pgd_xbits); +} + +static inline gpa_t kvm_riscv_gstage_gpa_size (unsigned long pgd_levels) +{ +return BIT_ULL (kvm_riscv_gstage_gpa_bits (pgd_levels)); +}bool kvm_riscv_gstage_get_leaf (struct kvm_gstage *gstage,gpa_t addr,pte_t **ptepp,u32 *ptep_level);
@@ -69,4 +75,21 @@ void kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t endvoid kvm_riscv_gstage_mode_detect (void ); +static inline unsigned long kvm_riscv_gstage_mode (unsigned long pgd_levels) +{ +switch (pgd_levels) { +case 2 : +return HGATP_MODE_SV32X4 ; +case 3 : +return HGATP_MODE_SV39X4 ; +case 4 : +return HGATP_MODE_SV48X4 ; +case 5 : +return HGATP_MODE_SV57X4 ; +default : +WARN_ON_ONCE (1 ); +return HGATP_MODE_OFF ; + } +} +#endif
arch/riscv/include/asm/kvm_host.h +1 -0
@@ -94,6 +94,7 @@ struct kvm_arch {/* G-stage page table */ pgd_t *pgd;phys_addr_t pgd_phys; +unsigned long pgd_levels;/* Guest Timer */ struct kvm_guest_timer timer;
arch/riscv/kvm/gstage.c +31 -34
201 lines not on this page · raw message
arch/riscv/kvm/main.c +6 -6
@@ -105,17 +105,17 @@ static int __init riscv_kvm_init(void)return rc;kvm_riscv_gstage_mode_detect (); -switch (kvm_riscv_gstage_mode) { -case HGATP_MODE_SV32X4 : +switch (kvm_riscv_gstage_max_pgd_levels) { +case 2 : str ="Sv32x4" ;break ; -case HGATP_MODE_SV39X4 : +case 3 : str ="Sv39x4" ;break ; -case HGATP_MODE_SV48X4 : +case 4 : str ="Sv48x4" ;break ; -case HGATP_MODE_SV57X4 : +case 5 : str ="Sv57x4" ;break ;default :
@@ -164,7 +164,7 @@ static int __init riscv_kvm_init(void) (rc) ? slist : "no features"); } -kvm_info ("using %s G-stage page table format\n" , str); +kvm_info ("highest G-stage page table mode is %s\n" , str);kvm_info ("VMID %ld bits available\n" ,kvm_riscv_gstage_vmid_bits ());
arch/riscv/kvm/mmu.c +12 -8
@@ -67,7 +67,7 @@ int kvm_riscv_mmu_ioremap(struct kvm *kvm, gpa_t gpa, phys_addr_t hpa,if (!writable) map.pte =pte_wrprotect (map.pte); - ret =kvm_mmu_topup_memory_cache (&pcache, kvm_riscv_gstage_pgd_levels); + ret =kvm_mmu_topup_memory_cache (&pcache, kvm->arch.pgd_levels);if (ret) goto out;
@@ -186,7 +186,7 @@ int kvm_arch_prepare_memory_region(struct kvm *kvm,* space addressable by the KVM guest GPA space. */ if ((new->base_gfn + new->npages) >= - (kvm_riscv_gstage_gpa_size >>PAGE_SHIFT )) +kvm_riscv_gstage_gpa_size (kvm->arch.pgd_levels) >>PAGE_SHIFT )return -EFAULT ; hva = new->userspace_addr;
@@ -472,7 +472,7 @@ int kvm_riscv_mmu_map(struct kvm_vcpu *vcpu, struct kvm_memory_slot *memslot,memset (out_map,0 ,sizeof (*out_map));/* We need minimum second+third level pages */ - ret =kvm_mmu_topup_memory_cache (pcache, kvm_riscv_gstage_pgd_levels); + ret =kvm_mmu_topup_memory_cache (pcache, kvm->arch.pgd_levels);if (ret) {kvm_err ("Failed to topup G-stage cache\n" );return ret;
@@ -575,6 +575,7 @@ int kvm_riscv_mmu_alloc_pgd(struct kvm *kvm)return -ENOMEM ; kvm->arch.pgd =page_to_virt (pgd_page); kvm->arch.pgd_phys =page_to_phys (pgd_page); + kvm->arch.pgd_levels = kvm_riscv_gstage_max_pgd_levels;return 0 ; }
@@ -590,10 +591,12 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm) gstage.flags =0 ; gstage.vmid =READ_ONCE (kvm->arch.vmid.vmid); gstage.pgd = kvm->arch.pgd; -kvm_riscv_gstage_unmap_range (&gstage,0UL , kvm_riscv_gstage_gpa_size, false); +kvm_riscv_gstage_unmap_range (&gstage,0UL , +kvm_riscv_gstage_gpa_size (kvm->arch.pgd_levels), false); pgd =READ_ONCE (kvm->arch.pgd); kvm->arch.pgd =NULL ; kvm->arch.pgd_phys =0 ; + kvm->arch.pgd_levels =0 ; }spin_unlock (&kvm->mmu_lock);
@@ -603,11 +606,12 @@ void kvm_riscv_mmu_free_pgd(struct kvm *kvm)void kvm_riscv_mmu_update_hgatp (struct kvm_vcpu *vcpu) { -unsigned long hgatp = kvm_riscv_gstage_mode <<HGATP_MODE_SHIFT ; -struct kvm_arch *k = &vcpu->kvm->arch; +struct kvm_arch *ka = &vcpu->kvm->arch; +unsigned long hgatp =kvm_riscv_gstage_mode (ka->pgd_levels) + <<HGATP_MODE_SHIFT ; - hgatp |= (READ_ONCE (k->vmid.vmid) <<HGATP_VMID_SHIFT ) &HGATP_VMID ; - hgatp |= (k->pgd_phys >>PAGE_SHIFT ) &HGATP_PPN ; + hgatp |= (READ_ONCE (ka->vmid.vmid) <<HGATP_VMID_SHIFT ) &HGATP_VMID ; + hgatp |= (ka->pgd_phys >>PAGE_SHIFT ) &HGATP_PPN ;ncsr_write (CSR_HGATP , hgatp);
arch/riscv/kvm/vm.c +1 -1
@@ -199,7 +199,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =KVM_USER_MEM_SLOTS ;break ;case KVM_CAP_VM_GPA_BITS : - r = kvm_riscv_gstage_gpa_bits; + r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);break ;default : r =0 ;
arch/riscv/kvm/vmid.c +2 -1
@@ -26,7 +26,8 @@ static DEFINE_SPINLOCK(vmid_lock);void __init kvm_riscv_gstage_vmid_detect (void ) {/* Figure-out number of VMID bits in HW */ -csr_write (CSR_HGATP , (kvm_riscv_gstage_mode <<HGATP_MODE_SHIFT ) |HGATP_VMID ); +csr_write (CSR_HGATP , (kvm_riscv_gstage_mode (kvm_riscv_gstage_max_pgd_levels) << +HGATP_MODE_SHIFT ) |HGATP_VMID ); vmid_bits =csr_read (CSR_HGATP ); vmid_bits = (vmid_bits &HGATP_VMID ) >>HGATP_VMID_SHIFT ; vmid_bits =fls_long (vmid_bits);
Signature · 1 line
2.50.1
Re: [PATCH v7 1/4] RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode
2026-04-02T21:23:00+08:00, <[email protected]>:
Quoted 21 lines
From: Fangyu Yu <[email protected]>
Introduces one per-VM architecture-specific fields to support runtime configuration of the G-stage page table format:
-
kvm->arch.pgd_levels: the corresponding number of page table levels for the selected mode.These fields replace the previous global variables
kvm_riscv_gstage_modeandkvm_riscv_gstage_pgd_levels, enabling different virtual machines to independently select their G-stage page table format instead of being forced to share the maximum mode detected by the kernel at boot time.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Anup Patel <[email protected]>Reviewed-by Guo Ren <[email protected]>---
arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 202
Quoted 5 lines
arch/riscv/kvm/vm.c +1
-1@@ -199,7 +199,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =KVM_USER_MEM_SLOTS ;break ;case KVM_CAP_VM_GPA_BITS : - r = kvm_riscv_gstage_gpa_bits; + r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);
kvm_vm_ioctl_check_extension() also gets called from with kvm == NULL from kvm_dev_ioctl(). I think we can continue to return ...(kvm_riscv_gstage_max_pgd_levels) in that case.
Thanks.
Re: Re: [PATCH v7 1/4] RISC-V: KVM: Support runtime configuration for per-VM's HGATP mode
Quoted 14 lines
Quoted 14 lines
From: Fangyu Yu <[email protected]>
Introduces one per-VM architecture-specific fields to support runtime configuration of the G-stage page table format:
-
kvm->arch.pgd_levels: the corresponding number of page table levels for the selected mode.These fields replace the previous global variables
kvm_riscv_gstage_modeandkvm_riscv_gstage_pgd_levels, enabling different virtual machines to independently select their G-stage page table format instead of being forced to share the maximum mode detected by the kernel at boot time.
Quoted 17 lines
arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 202
Quoted 12 lines
Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Anup Patel <[email protected]>Reviewed-by Guo Ren <[email protected]>---
arch/riscv/kvm/vm.c +1
-1@@ -199,7 +199,7 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) r =KVM_USER_MEM_SLOTS ;break ;case KVM_CAP_VM_GPA_BITS : - r = kvm_riscv_gstage_gpa_bits; + r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);
kvm_vm_ioctl_check_extension()also gets called from with kvm == NULL fromkvm_dev_ioctl(). I think we can continue to return ...(kvm_riscv_gstage_max_pgd_levels) in that case.
Thanks for catching this. I’ll handle the kvm == NULL case (from kvm_dev_ioctl) and return the host maximum based on kvm_riscv_gstage_max_pgd_levels in v8.
Also, if the intended semantics of KVM_CAP_VM_GPA_BITS is to report the maximum supported value, then we could simply always return the host maximum based on kvm_riscv_gstage_max_pgd_levels.
Thanks,
Fangyu
Quoted 1 line
Thanks.
Signature · 3 lines
kvm-riscv mailing list [email protected] http://lists.infradead.org/mailman/listinfo/kvm-riscv
PATCH v7 2/4 RISC-V: KVM: Cache gstage pgd_levels in struct kvm_gstage
In reply to [email protected]From: Fangyu Yu <[email protected]>
Gstage page-table helpers frequently chase gstage->kvm->arch to fetch pgd_levels. This adds noise and repeats the same dereference chain in hot paths.
Add pgd_levels to struct kvm_gstage and initialize it from kvm->arch when setting up a gstage instance. Introduce kvm_riscv_gstage_init() to centralize initialization and switch gstage code to use gstage->pgd_levels.
--- arch/riscv/include/asm/kvm_gstage.h | 10 ++++++ arch/riscv/kvm/gstage.c | 10 +++--- arch/riscv/kvm/mmu.c | 50 ++++++----------------------- 3 files changed, 25 insertions(+), 45 deletions(-)
arch/riscv/include/asm/kvm_gstage.h +10 -0
@@ -15,6 +15,7 @@ struct kvm_gstage {#define KVM_GSTAGE_FLAGS_LOCAL BIT(0)unsigned long vmid;pgd_t *pgd; +unsigned long pgd_levels; };struct kvm_gstage_mapping {
@@ -92,4 +93,13 @@ static inline unsigned long kvm_riscv_gstage_mode(unsigned long pgd_levels) } } +static inline void kvm_riscv_gstage_init (struct kvm_gstage *gstage,struct kvm *kvm) +{ + gstage->kvm = kvm; + gstage->flags =0 ; + gstage->vmid =READ_ONCE (kvm->arch.vmid.vmid); + gstage->pgd = kvm->arch.pgd; + gstage->pgd_levels = kvm->arch.pgd_levels; +} +#endif
arch/riscv/kvm/gstage.c +5 -5
@@ -26,7 +26,7 @@ static inline unsigned long gstage_pte_index(struct kvm_gstage *gstage,unsigned long mask;unsigned long shift =HGATP_PAGE_SHIFT + (kvm_riscv_gstage_index_bits * level); -if (level == gstage->kvm->arch.pgd_levels -1 ) +if (level == gstage->pgd_levels -1 ) mask = (PTRS_PER_PTE * (1UL << kvm_riscv_gstage_pgd_xbits)) -1 ;else mask =PTRS_PER_PTE -1 ;
@@ -45,7 +45,7 @@ static int gstage_page_size_to_level(struct kvm_gstage *gstage, unsigned long pau32 i;unsigned long psz =1UL <<12 ; -for (i =0 ; i < gstage->kvm->arch.pgd_levels; i++) { +for (i =0 ; i < gstage->pgd_levels; i++) {if (page_size == (psz << (i * kvm_riscv_gstage_index_bits))) { *out_level = i;return 0 ;
@@ -58,7 +58,7 @@ static int gstage_page_size_to_level(struct kvm_gstage *gstage, unsigned long pastatic int gstage_level_to_page_order (struct kvm_gstage *gstage,u32 level,unsigned long *out_pgorder) { -if (gstage->kvm->arch.pgd_levels < level) +if (gstage->pgd_levels < level)return -EINVAL ; *out_pgorder =12 + (level * kvm_riscv_gstage_index_bits);
@@ -83,7 +83,7 @@ bool kvm_riscv_gstage_get_leaf(struct kvm_gstage *gstage, gpa_t addr, pte_t **ptepp, u32 *ptep_level) {pte_t *ptep; -u32 current_level = gstage->kvm->arch.pgd_levels -1 ; +u32 current_level = gstage->pgd_levels -1 ; *ptep_level = current_level; ptep = (pte_t *)gstage->pgd;
@@ -127,7 +127,7 @@ int kvm_riscv_gstage_set_pte(struct kvm_gstage *gstage,struct kvm_mmu_memory_cache *pcache, const struct kvm_gstage_mapping *map) { - u32 current_level = gstage->kvm->arch.pgd_levels - 1; + u32 current_level = gstage->pgd_levels - 1;pte_t *next_ptep = (pte_t *)gstage->pgd;pte_t *ptep = &next_ptep[gstage_pte_index (gstage, map->addr, current_level)];
arch/riscv/kvm/mmu.c +10 -40
110 lines not on this page · raw message
Signature · 1 line
2.50.1
PATCH v7 3/4 RISC-V: KVM: Detect and expose supported HGATP G-stage modes
In reply to [email protected]From: Fangyu Yu <[email protected]>
Extend kvm_riscv_gstage_mode_detect() to record HGATP.MODE values in a bitmask. Keep tracking the maximum supported G-stage page table level for existing internal users.
Also provide lightweight helpers to retrieve the supported-mode bitmask and validate a requested HGATP.MODE against it.
--- arch/riscv/include/asm/kvm_gstage.h | 11 +++++++++++ arch/riscv/kvm/gstage.c | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-)
arch/riscv/include/asm/kvm_gstage.h +11 -0
@@ -31,6 +31,7 @@ struct kvm_gstage_mapping {#endif extern unsigned long kvm_riscv_gstage_max_pgd_levels; +extern u32 kvm_riscv_gstage_supported_mode_mask;#define kvm_riscv_gstage_pgd_xbits 2#define kvm_riscv_gstage_pgd_size (1UL << (HGATP_PAGE_SHIFT + kvm_riscv_gstage_pgd_xbits))
@@ -102,4 +103,14 @@ static inline void kvm_riscv_gstage_init(struct kvm_gstage *gstage, struct kvm * gstage->pgd_levels = kvm->arch.pgd_levels; } +static inline u32 kvm_riscv_get_hgatp_mode_mask (void ) +{ +return kvm_riscv_gstage_supported_mode_mask; +} + +static inline bool kvm_riscv_hgatp_mode_is_valid (unsigned long mode) +{ +return kvm_riscv_gstage_supported_mode_mask &BIT (mode); +} +#endif
arch/riscv/kvm/gstage.c +12 -3
@@ -16,6 +16,8 @@ unsigned long kvm_riscv_gstage_max_pgd_levels __ro_after_init = 3;#else unsigned long kvm_riscv_gstage_max_pgd_levels__ro_after_init =2 ;#endif +/* Bitmask of supported HGATP.MODE encodings (BIT(HGATP_MODE_*)). */ +u32 kvm_riscv_gstage_supported_mode_mask__ro_after_init ;#define gstage_pte_leaf (__ptep) \ (pte_val(*(__ptep)) & (_PAGE_READ | _PAGE_WRITE | _PAGE_EXEC))
@@ -317,11 +319,17 @@ void kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t endvoid __init kvm_riscv_gstage_mode_detect (void ) { + kvm_riscv_gstage_supported_mode_mask =0 ; + kvm_riscv_gstage_max_pgd_levels =0 ; +#ifdef CONFIG_64BIT /* Try Sv57x4 G-stage mode */ csr_write (CSR_HGATP ,HGATP_MODE_SV57X4 <<HGATP_MODE_SHIFT );if ((csr_read (CSR_HGATP ) >>HGATP_MODE_SHIFT ) ==HGATP_MODE_SV57X4 ) { kvm_riscv_gstage_max_pgd_levels =5 ; + kvm_riscv_gstage_supported_mode_mask |=BIT (HGATP_MODE_SV57X4 ) | +BIT (HGATP_MODE_SV48X4 ) | +BIT (HGATP_MODE_SV39X4 ); goto done; }
@@ -329,6 +337,8 @@ void __init kvm_riscv_gstage_mode_detect(void)csr_write (CSR_HGATP ,HGATP_MODE_SV48X4 <<HGATP_MODE_SHIFT );if ((csr_read (CSR_HGATP ) >>HGATP_MODE_SHIFT ) ==HGATP_MODE_SV48X4 ) { kvm_riscv_gstage_max_pgd_levels =4 ; + kvm_riscv_gstage_supported_mode_mask |=BIT (HGATP_MODE_SV48X4 ) | +BIT (HGATP_MODE_SV39X4 ); goto done; }
@@ -336,6 +346,7 @@ void __init kvm_riscv_gstage_mode_detect(void)csr_write (CSR_HGATP ,HGATP_MODE_SV39X4 <<HGATP_MODE_SHIFT );if ((csr_read (CSR_HGATP ) >>HGATP_MODE_SHIFT ) ==HGATP_MODE_SV39X4 ) { kvm_riscv_gstage_max_pgd_levels =3 ; + kvm_riscv_gstage_supported_mode_mask |=BIT (HGATP_MODE_SV39X4 ); goto done; }#else /* CONFIG_32BIT */
@@ -343,13 +354,11 @@ void __init kvm_riscv_gstage_mode_detect(void)csr_write (CSR_HGATP ,HGATP_MODE_SV32X4 <<HGATP_MODE_SHIFT );if ((csr_read (CSR_HGATP ) >>HGATP_MODE_SHIFT ) ==HGATP_MODE_SV32X4 ) { kvm_riscv_gstage_max_pgd_levels =2 ; + kvm_riscv_gstage_supported_mode_mask |=BIT (HGATP_MODE_SV32X4 ); goto done; }#endif -/* KVM depends on !HGATP_MODE_OFF */ - kvm_riscv_gstage_max_pgd_levels =0 ; - done:csr_write (CSR_HGATP ,0 );kvm_riscv_local_hfence_gvma_all ();
Signature · 1 line
2.50.1
Re: [PATCH v7 3/4] RISC-V: KVM: Detect and expose supported HGATP G-stage modes
Quoted 7 lines · On Thu, Apr 2, 2026 at 6:53 PM <[email protected]> wrote:
From: Fangyu Yu <[email protected]>
Extend
kvm_riscv_gstage_mode_detect()to record HGATP.MODE values in a bitmask. Keep tracking the maximum supported G-stage page table level for existing internal users.
Quoted 6 lines
Also provide lightweight helpers to retrieve the supported-mode bitmask and validate a requested HGATP.MODE against it.
Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>
LGTM.
Thanks,
Anup
Quoted 98 lines
--- arch/riscv/include/asm/kvm_gstage.h | 11 +++++++++++ arch/riscv/kvm/gstage.c | 15 ++++++++++++--- 2 files changed, 23 insertions(+), 3 deletions(-)arch/riscv/include/asm/kvm_gstage.h +11
-0@@ -31,6 +31,7 @@ struct kvm_gstage_mapping {#endif extern unsigned long kvm_riscv_gstage_max_pgd_levels; +extern u32 kvm_riscv_gstage_supported_mode_mask;#define kvm_riscv_gstage_pgd_xbits 2#define kvm_riscv_gstage_pgd_size (1UL << (HGATP_PAGE_SHIFT + kvm_riscv_gstage_pgd_xbits))@@ -102,4 +103,14 @@ static inline void kvm_riscv_gstage_init(struct kvm_gstage *gstage, struct kvm * gstage->pgd_levels = kvm->arch.pgd_levels; } +static inline u32 kvm_riscv_get_hgatp_mode_mask (void ) +{ +return kvm_riscv_gstage_supported_mode_mask; +} + +static inline bool kvm_riscv_hgatp_mode_is_valid (unsigned long mode) +{ +return kvm_riscv_gstage_supported_mode_mask &BIT (mode); +} +#endif arch/riscv/kvm/gstage.c +12
-4@@ -16,6 +16,8 @@ unsigned long kvm_riscv_gstage_max_pgd_levels __ro_after_init = 3;#else unsigned long kvm_riscv_gstage_max_pgd_levels__ro_after_init =2 ;#endif +/* Bitmask of supported HGATP.MODE encodings (BIT(HGATP_MODE_*)). */ +u32 kvm_riscv_gstage_supported_mode_mask__ro_after_init ;#define gstage_pte_leaf (__ptep) \ (pte_val(*(__ptep)) & (_PAGE_READ | _PAGE_WRITE | _PAGE_EXEC))@@ -317,11 +319,17 @@ void kvm_riscv_gstage_wp_range(struct kvm_gstage *gstage, gpa_t start, gpa_t endvoid __init kvm_riscv_gstage_mode_detect (void ) { + kvm_riscv_gstage_supported_mode_mask =0 ; + kvm_riscv_gstage_max_pgd_levels =0 ; +#ifdef CONFIG_64BIT /* Try Sv57x4 G-stage mode */ csr_write (CSR_HGATP ,HGATP_MODE_SV57X4 <<HGATP_MODE_SHIFT );if ((csr_read (CSR_HGATP ) >>HGATP_MODE_SHIFT ) ==HGATP_MODE_SV57X4 ) { kvm_riscv_gstage_max_pgd_levels =5 ; + kvm_riscv_gstage_supported_mode_mask |=BIT (HGATP_MODE_SV57X4 ) | +BIT (HGATP_MODE_SV48X4 ) | +BIT (HGATP_MODE_SV39X4 ); goto done; }@@ -329,6 +337,8 @@ void __init kvm_riscv_gstage_mode_detect(void)csr_write (CSR_HGATP ,HGATP_MODE_SV48X4 <<HGATP_MODE_SHIFT );if ((csr_read (CSR_HGATP ) >>HGATP_MODE_SHIFT ) ==HGATP_MODE_SV48X4 ) { kvm_riscv_gstage_max_pgd_levels =4 ; + kvm_riscv_gstage_supported_mode_mask |=BIT (HGATP_MODE_SV48X4 ) | +BIT (HGATP_MODE_SV39X4 ); goto done; }@@ -336,6 +346,7 @@ void __init kvm_riscv_gstage_mode_detect(void)csr_write (CSR_HGATP ,HGATP_MODE_SV39X4 <<HGATP_MODE_SHIFT );if ((csr_read (CSR_HGATP ) >>HGATP_MODE_SHIFT ) ==HGATP_MODE_SV39X4 ) { kvm_riscv_gstage_max_pgd_levels =3 ; + kvm_riscv_gstage_supported_mode_mask |=BIT (HGATP_MODE_SV39X4 ); goto done; }#else /* CONFIG_32BIT */ @@ -343,13 +354,11 @@ void __init kvm_riscv_gstage_mode_detect(void)csr_write (CSR_HGATP ,HGATP_MODE_SV32X4 <<HGATP_MODE_SHIFT );if ((csr_read (CSR_HGATP ) >>HGATP_MODE_SHIFT ) ==HGATP_MODE_SV32X4 ) { kvm_riscv_gstage_max_pgd_levels =2 ; + kvm_riscv_gstage_supported_mode_mask |=BIT (HGATP_MODE_SV32X4 ); goto done; }#endif -/* KVM depends on !HGATP_MODE_OFF */ - kvm_riscv_gstage_max_pgd_levels =0 ; - done:csr_write (CSR_HGATP ,0 );kvm_riscv_local_hfence_gvma_all (); --2.50.1
Re: [PATCH v7 3/4] RISC-V: KVM: Detect and expose supported HGATP G-stage modes
In reply to [email protected]2026-04-02T21:23:02+08:00, <[email protected]>:
Quoted 15 lines
From: Fangyu Yu <[email protected]>
Extend
kvm_riscv_gstage_mode_detect()to record HGATP.MODE values in a bitmask. Keep tracking the maximum supported G-stage page table level for existing internal users.Also provide lightweight helpers to retrieve the supported-mode bitmask and validate a requested HGATP.MODE against it.
Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>---
arch/riscv/include/asm/kvm_gstage.h static inline void kvm_riscv_gstage_init(struct kvm_gstage *gstage, struct kvm * · line 105
Quoted 3 lines
arch/riscv/include/asm/kvm_gstage.h +3
-0@@ -102,4 +103,14 @@ static inline void kvm_riscv_gstage_init(struct kvm_gstage *gstage, struct kvm * +static inline bool kvm_riscv_hgatp_mode_is_valid (unsigned long mode) +{ +return kvm_riscv_gstage_supported_mode_mask &BIT (mode);
Shifting by more than the bit width is undefined behavior in C. RV64 effectively translates BIT(mode) to 1UL << (mode & 0x3f), so this could allow values larger than the mask.
Thanks.
Re: Re: [PATCH v7 3/4] RISC-V: KVM: Detect and expose supported HGATP G-stage modes
Quoted 9 lines
Quoted 9 lines
From: Fangyu Yu <[email protected]>
Extend
kvm_riscv_gstage_mode_detect()to record HGATP.MODE values in a bitmask. Keep tracking the maximum supported G-stage page table level for existing internal users.Also provide lightweight helpers to retrieve the supported-mode bitmask and validate a requested HGATP.MODE against it.
Quoted 14 lines
arch/riscv/include/asm/kvm_gstage.h static inline void kvm_riscv_gstage_init(struct kvm_gstage *gstage, struct kvm * · line 105
Quoted 9 lines
Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>---
arch/riscv/include/asm/kvm_gstage.h +3
-0@@ -102,4 +103,14 @@ static inline void kvm_riscv_gstage_init(struct kvm_gstage *gstage, struct kvm * +static inline bool kvm_riscv_hgatp_mode_is_valid (unsigned long mode) +{ +return kvm_riscv_gstage_supported_mode_mask &BIT (mode);Shifting by more than the bit width is undefined behavior in C. RV64 effectively translates BIT(mode) to 1UL << (mode &
0x3f), so this could allow values larger than the mask.
Thanks for catching this.
You’re right: BIT(mode) is undefined for out-of-range shifts, and on RV64 it can effectively mask the shift amount, potentially making invalid MODE values appear valid. In v8 I’ll add an explicit bounds check before shifting.
Thanks,
Fangyu
Quoted 1 line
Thanks.
PATCH v7 4/4 RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
In reply to [email protected]From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with -EINVAL if the mode is not supported by the host, and with -EBUSY if the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE) returns a bitmask of the HGATP.MODE formats supported by the host.
--- Documentation/virt/kvm/api.rst | 27 +++++++++++++++++++++++++++ arch/riscv/kvm/vm.c | 18 ++++++++++++++++-- include/uapi/linux/kvm.h | 1 + 3 files changed, 44 insertions(+), 2 deletions(-)
Documentation/virt/kvm/api.rst +27 -0
@@ -8902,6 +8902,33 @@ helpful if user space wants to emulate instructions which are not This capability can be enabled dynamically even if VCPUs were already created and are running. +7.47 KVM_CAP_RISCV_SET_HGATP_MODE +--------------------------------- + +:Architectures: riscv +:Type: VM +:Parameters: args[0] contains the requested HGATP mode +:Returns: + - 0 on success. + - -EINVAL if args[0] is outside the range of HGATP modes supported by the + hardware. + - -EBUSY if vCPUs have already been created for the VM, if the VM has any + non-empty memslots. + +This capability allows userspace to explicitly select the HGATP mode for +the VM. The selected mode must be supported by both KVM and hardware. This +capability must be enabled before creating any vCPUs or memslots. + +If this capability is not enabled, KVM will select the default HGATP mode +automatically. The default is the highest HGATP.MODE value supported by +hardware. + +``KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)`` returns a bitmask of +HGATP.MODE values supported by the host. A return value of 0 indicates that +the capability is not supported. Supported-mode bitmask use HGATP.MODE +encodings as defined by the RISC-V privileged specification, such as Sv39x4 +corresponds to HGATP.MODE=8, so userspace should test bitmask & BIT(8). + 8. Other capabilities. ======================
arch/riscv/kvm/vm.c +16 -2
@@ -201,6 +201,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_VM_GPA_BITS : r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);break ; +case KVM_CAP_RISCV_SET_HGATP_MODE : + r =kvm_riscv_get_hgatp_mode_mask (); +break ;default : r =0 ;break ;
@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +if (cap->flags) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_RISCV_MP_STATE_RESET : -if (cap->flags) -return -EINVAL ; kvm->arch.mp_state_reset = true;return 0 ; +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ; +#ifdef CONFIG_64BIT + kvm->arch.pgd_levels =3 + cap->args[0 ] -HGATP_MODE_SV39X4 ; +#endif +return 0 ;default :return -EINVAL ; }
include/uapi/linux/kvm.h +1 -0
@@ -989,6 +989,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_SEA_TO_USER 245#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247 +#define KVM_CAP_RISCV_SET_HGATP_MODE 248struct kvm_irq_routing_irqchip {__u32 irqchip;
Signature · 1 line
2.50.1
Anup Patel
maintainerRe: [PATCH v7 4/4] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
Quoted 66 lines · On Thu, Apr 2, 2026 at 6:53 PM <[email protected]> wrote:
From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via
KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with-EINVALif the mode is not supported by the host, and with-EBUSYif the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)returns a bitmask of the HGATP.MODE formats supported by the host.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>--- Documentation/virt/kvm/api.rst | 27 +++++++++++++++++++++++++++ arch/riscv/kvm/vm.c | 18 ++++++++++++++++-- include/uapi/linux/kvm.h | 1 + 3 files changed, 44 insertions(+), 2 deletions(-)Documentation/virt/kvm/api.rst +27
-0@@ -8902,6 +8902,33 @@ helpful if user space wants to emulate instructions which are not This capability can be enabled dynamically even if VCPUs were already created and are running. +7.47 KVM_CAP_RISCV_SET_HGATP_MODE +--------------------------------- + +:Architectures: riscv +:Type: VM +:Parameters: args[0] contains the requested HGATP mode +:Returns: + - 0 on success. + - -EINVAL if args[0] is outside the range of HGATP modes supported by the + hardware. + - -EBUSY if vCPUs have already been created for the VM, if the VM has any + non-empty memslots. + +This capability allows userspace to explicitly select the HGATP mode for +the VM. The selected mode must be supported by both KVM and hardware. This +capability must be enabled before creating any vCPUs or memslots. + +If this capability is not enabled, KVM will select the default HGATP mode +automatically. The default is the highest HGATP.MODE value supported by +hardware. + +``KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)`` returns a bitmask of +HGATP.MODE values supported by the host. A return value of 0 indicates that +the capability is not supported. Supported-mode bitmask use HGATP.MODE +encodings as defined by the RISC-V privileged specification, such as Sv39x4 +corresponds to HGATP.MODE=8, so userspace should test bitmask & BIT(8). + 8. Other capabilities. ======================arch/riscv/kvm/vm.c +0
-0
arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 206
Quoted 6 lines
arch/riscv/kvm/vm.c +3
-0@@ -201,6 +201,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_VM_GPA_BITS : r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);break ; +case KVM_CAP_RISCV_SET_HGATP_MODE : + r =kvm_riscv_get_hgatp_mode_mask (); +break ;
Introducing a new RISC-V capability looks a bit complex. Instead of KVM_CAP_RISCV_SET_HGATP_MODE, we can simply re-use KVM_CAP_VM_GPA_BITS.
The kvm_vm_ioctl_check_extension() for KVM_CAP_VM_GPA_BITS return number of GPA bits which in-directly implies the underlying hgatp.MODE. As we know, if it return 59 bits GPA then it means Sv57x4 is the selected hgatp.MODE and Sv48x4 and Sv39x4 modes are also supported as-per RISC-V privileged specification.
The kvm_vm_ioctl_enable_cap() for KVM_CAP_VM_GPA_BITS will take the desired number of GPA bits and downsize the selected hgatp.MODE. For example, if user-space ask GPA bits <= 50 and GPA bits > 41 then we select Sv48x4. If user-space ask GPA bits <= 41 then we select Sv39x4. If user-space ask GPA bits <= 59 and GPA bits > 50 then we select Sv57x4.
Quoted 42 lines
arch/riscv/kvm/vm.c +13
-2default : r =0 ;break ;@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +if (cap->flags) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_RISCV_MP_STATE_RESET : -if (cap->flags) -return -EINVAL ; kvm->arch.mp_state_reset = true;return 0 ; +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ; +#ifdef CONFIG_64BIT + kvm->arch.pgd_levels =3 + cap->args[0 ] -HGATP_MODE_SV39X4 ; +#endif +return 0 ;default :return -EINVAL ; }include/uapi/linux/kvm.h +1
-1@@ -989,6 +989,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_SEA_TO_USER 245#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247 +#define KVM_CAP_RISCV_SET_HGATP_MODE 248struct kvm_irq_routing_irqchip {__u32 irqchip; --
Quoted 2 lines
2.50.1
Regards,
Anup
Re: Re: [PATCH v7 4/4] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
Quoted 84 lines · On Thu, Apr 2, 2026 at 6:53 PM <[email protected]> wrote:
Quoted 66 lines
From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via
KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with-EINVALif the mode is not supported by the host, and with-EBUSYif the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)returns a bitmask of the HGATP.MODE formats supported by the host.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>--- Documentation/virt/kvm/api.rst | 27 +++++++++++++++++++++++++++ arch/riscv/kvm/vm.c | 18 ++++++++++++++++-- include/uapi/linux/kvm.h | 1 + 3 files changed, 44 insertions(+), 2 deletions(-)Documentation/virt/kvm/api.rst +27
-0@@ -8902,6 +8902,33 @@ helpful if user space wants to emulate instructions which are not This capability can be enabled dynamically even if VCPUs were already created and are running. +7.47 KVM_CAP_RISCV_SET_HGATP_MODE +--------------------------------- + +:Architectures: riscv +:Type: VM +:Parameters: args[0] contains the requested HGATP mode +:Returns: + - 0 on success. + - -EINVAL if args[0] is outside the range of HGATP modes supported by the + hardware. + - -EBUSY if vCPUs have already been created for the VM, if the VM has any + non-empty memslots. + +This capability allows userspace to explicitly select the HGATP mode for +the VM. The selected mode must be supported by both KVM and hardware. This +capability must be enabled before creating any vCPUs or memslots. + +If this capability is not enabled, KVM will select the default HGATP mode +automatically. The default is the highest HGATP.MODE value supported by +hardware. + +``KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)`` returns a bitmask of +HGATP.MODE values supported by the host. A return value of 0 indicates that +the capability is not supported. Supported-mode bitmask use HGATP.MODE +encodings as defined by the RISC-V privileged specification, such as Sv39x4 +corresponds to HGATP.MODE=8, so userspace should test bitmask & BIT(8). + 8. Other capabilities. ======================arch/riscv/kvm/vm.c +0
-0arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 206
Quoted 6 lines
arch/riscv/kvm/vm.c +3
-0@@ -201,6 +201,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_VM_GPA_BITS : r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);break ; +case KVM_CAP_RISCV_SET_HGATP_MODE : + r =kvm_riscv_get_hgatp_mode_mask (); +break ;Introducing a new RISC-V capability looks a bit complex. Instead of
KVM_CAP_RISCV_SET_HGATP_MODE, we can simply re-useKVM_CAP_VM_GPA_BITS.The
kvm_vm_ioctl_check_extension()forKVM_CAP_VM_GPA_BITSreturn number of GPA bits which in-directly implies the underlying hgatp.MODE. As we know, if it return 59 bits GPA then it means Sv57x4 is the selected hgatp.MODE and Sv48x4 and Sv39x4 modes are also supported as-per RISC-V privileged specification.
Quoted 7 lines
The
kvm_vm_ioctl_enable_cap()forKVM_CAP_VM_GPA_BITSwill take the desired number of GPA bits and downsize the selected hgatp.MODE. For example, if user-space ask GPA bits <= 50 and GPA bits > 41 then we select Sv48x4. If user-space ask GPA bits <= 41 then we select Sv39x4. If user-space ask GPA bits <= 59 and GPA bits > 50 then we select Sv57x4.
Thanks, that makes sense.
In v8 I’ll drop KVM_CAP_RISCV_SET_HGATP_MODE and re-use KVM_CAP_VM_GPA_BITS for both discovery and selection.
Thanks,
Fangyu
Quoted 47 lines
Quoted 42 lines
arch/riscv/kvm/vm.c +13
-2default : r =0 ;break ;@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +if (cap->flags) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_RISCV_MP_STATE_RESET : -if (cap->flags) -return -EINVAL ; kvm->arch.mp_state_reset = true;return 0 ; +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ; +#ifdef CONFIG_64BIT + kvm->arch.pgd_levels =3 + cap->args[0 ] -HGATP_MODE_SV39X4 ; +#endif +return 0 ;default :return -EINVAL ; }include/uapi/linux/kvm.h +1
-1@@ -989,6 +989,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_SEA_TO_USER 245#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247 +#define KVM_CAP_RISCV_SET_HGATP_MODE 248struct kvm_irq_routing_irqchip {__u32 irqchip; --Quoted 2 lines
2.50.1
Regards,
Anup
Re: Re: Re: [PATCH v7 4/4] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
Quoted 92 lines
Quoted 84 lines · On Thu, Apr 2, 2026 at 6:53 PM <[email protected]> wrote:
Quoted 66 lines
From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via
KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with-EINVALif the mode is not supported by the host, and with-EBUSYif the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)returns a bitmask of the HGATP.MODE formats supported by the host.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>--- Documentation/virt/kvm/api.rst | 27 +++++++++++++++++++++++++++ arch/riscv/kvm/vm.c | 18 ++++++++++++++++-- include/uapi/linux/kvm.h | 1 + 3 files changed, 44 insertions(+), 2 deletions(-)Documentation/virt/kvm/api.rst +27
-0@@ -8902,6 +8902,33 @@ helpful if user space wants to emulate instructions which are not This capability can be enabled dynamically even if VCPUs were already created and are running. +7.47 KVM_CAP_RISCV_SET_HGATP_MODE +--------------------------------- + +:Architectures: riscv +:Type: VM +:Parameters: args[0] contains the requested HGATP mode +:Returns: + - 0 on success. + - -EINVAL if args[0] is outside the range of HGATP modes supported by the + hardware. + - -EBUSY if vCPUs have already been created for the VM, if the VM has any + non-empty memslots. + +This capability allows userspace to explicitly select the HGATP mode for +the VM. The selected mode must be supported by both KVM and hardware. This +capability must be enabled before creating any vCPUs or memslots. + +If this capability is not enabled, KVM will select the default HGATP mode +automatically. The default is the highest HGATP.MODE value supported by +hardware. + +``KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)`` returns a bitmask of +HGATP.MODE values supported by the host. A return value of 0 indicates that +the capability is not supported. Supported-mode bitmask use HGATP.MODE +encodings as defined by the RISC-V privileged specification, such as Sv39x4 +corresponds to HGATP.MODE=8, so userspace should test bitmask & BIT(8). + 8. Other capabilities. ======================arch/riscv/kvm/vm.c +0
-0arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 206
Quoted 6 lines
arch/riscv/kvm/vm.c +3
-0@@ -201,6 +201,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_VM_GPA_BITS : r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);break ; +case KVM_CAP_RISCV_SET_HGATP_MODE : + r =kvm_riscv_get_hgatp_mode_mask (); +break ;Introducing a new RISC-V capability looks a bit complex. Instead of
KVM_CAP_RISCV_SET_HGATP_MODE, we can simply re-useKVM_CAP_VM_GPA_BITS.The
kvm_vm_ioctl_check_extension()forKVM_CAP_VM_GPA_BITSreturn number of GPA bits which in-directly implies the underlying hgatp.MODE. As we know, if it return 59 bits GPA then it means Sv57x4 is the selected hgatp.MODE and Sv48x4 and Sv39x4 modes are also supported as-per RISC-V privileged specification.Quoted 7 lines
The
kvm_vm_ioctl_enable_cap()forKVM_CAP_VM_GPA_BITSwill take the desired number of GPA bits and downsize the selected hgatp.MODE. For example, if user-space ask GPA bits <= 50 and GPA bits > 41 then we select Sv48x4. If user-space ask GPA bits <= 41 then we select Sv39x4. If user-space ask GPA bits <= 59 and GPA bits > 50 then we select Sv57x4.
Quoted 5 lines
Thanks, that makes sense.
In v8 I’ll drop
KVM_CAP_RISCV_SET_HGATP_MODEand re-useKVM_CAP_VM_GPA_BITSfor both discovery and selection.
Hi Anup,
While working on the respin reusing KVM_CAP_VM_GPA_BITS, I realized a potential ambiguity in CHECK_EXTENSION semantics and wanted to confirm the intended ABI before posting v8.
One concern about the semantics: today KVM_CHECK_EXTENSION(KVM_CAP_VM_GPA_BITS) on a VM fd may be interpreted as “the GPA bits for this VM” (or at least what this VM can use). If we also use KVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS) to downsize the selected HGATP.MODE for a particular VM (e.g. to Sv48x4 => 50 bits), then a subsequent CHECK_EXTENSION(KVM_CAP_VM_GPA_BITS) on the same VM fd would return 50. Userspace might then assume 50 is the maximum supported by that VM/host and lose the information that the host actually supports 59 (Sv57x4).
Thanks,
Fangyu
Quoted 50 lines
Thanks,
FangyuQuoted 47 lines
Quoted 42 lines
arch/riscv/kvm/vm.c +13
-2default : r =0 ;break ;@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +if (cap->flags) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_RISCV_MP_STATE_RESET : -if (cap->flags) -return -EINVAL ; kvm->arch.mp_state_reset = true;return 0 ; +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ; +#ifdef CONFIG_64BIT + kvm->arch.pgd_levels =3 + cap->args[0 ] -HGATP_MODE_SV39X4 ; +#endif +return 0 ;default :return -EINVAL ; }include/uapi/linux/kvm.h +1
-1@@ -989,6 +989,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_SEA_TO_USER 245#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247 +#define KVM_CAP_RISCV_SET_HGATP_MODE 248struct kvm_irq_routing_irqchip {__u32 irqchip; --Quoted 2 lines
2.50.1
Regards,
Anup
Anup Patel
maintainerRe: Re: Re: [PATCH v7 4/4] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
Quoted 105 lines · On Fri, Apr 3, 2026 at 7:32 AM <[email protected]> wrote:
Quoted 93 lines
Quoted 85 lines · On Thu, Apr 2, 2026 at 6:53 PM <[email protected]> wrote:
Quoted 66 lines
From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via
KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with-EINVALif the mode is not supported by the host, and with-EBUSYif the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)returns a bitmask of the HGATP.MODE formats supported by the host.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>--- Documentation/virt/kvm/api.rst | 27 +++++++++++++++++++++++++++ arch/riscv/kvm/vm.c | 18 ++++++++++++++++-- include/uapi/linux/kvm.h | 1 + 3 files changed, 44 insertions(+), 2 deletions(-)Documentation/virt/kvm/api.rst +27
-0@@ -8902,6 +8902,33 @@ helpful if user space wants to emulate instructions which are not This capability can be enabled dynamically even if VCPUs were already created and are running. +7.47 KVM_CAP_RISCV_SET_HGATP_MODE +--------------------------------- + +:Architectures: riscv +:Type: VM +:Parameters: args[0] contains the requested HGATP mode +:Returns: + - 0 on success. + - -EINVAL if args[0] is outside the range of HGATP modes supported by the + hardware. + - -EBUSY if vCPUs have already been created for the VM, if the VM has any + non-empty memslots. + +This capability allows userspace to explicitly select the HGATP mode for +the VM. The selected mode must be supported by both KVM and hardware. This +capability must be enabled before creating any vCPUs or memslots. + +If this capability is not enabled, KVM will select the default HGATP mode +automatically. The default is the highest HGATP.MODE value supported by +hardware. + +``KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)`` returns a bitmask of +HGATP.MODE values supported by the host. A return value of 0 indicates that +the capability is not supported. Supported-mode bitmask use HGATP.MODE +encodings as defined by the RISC-V privileged specification, such as Sv39x4 +corresponds to HGATP.MODE=8, so userspace should test bitmask & BIT(8). + 8. Other capabilities. ======================arch/riscv/kvm/vm.c +0
-0arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 206
Quoted 6 lines
arch/riscv/kvm/vm.c +3
-0@@ -201,6 +201,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_VM_GPA_BITS : r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);break ; +case KVM_CAP_RISCV_SET_HGATP_MODE : + r =kvm_riscv_get_hgatp_mode_mask (); +break ;Introducing a new RISC-V capability looks a bit complex. Instead of
KVM_CAP_RISCV_SET_HGATP_MODE, we can simply re-useKVM_CAP_VM_GPA_BITS.The
kvm_vm_ioctl_check_extension()forKVM_CAP_VM_GPA_BITSreturn number of GPA bits which in-directly implies the underlying hgatp.MODE. As we know, if it return 59 bits GPA then it means Sv57x4 is the selected hgatp.MODE and Sv48x4 and Sv39x4 modes are also supported as-per RISC-V privileged specification.Quoted 7 lines
The
kvm_vm_ioctl_enable_cap()forKVM_CAP_VM_GPA_BITSwill take the desired number of GPA bits and downsize the selected hgatp.MODE. For example, if user-space ask GPA bits <= 50 and GPA bits > 41 then we select Sv48x4. If user-space ask GPA bits <= 41 then we select Sv39x4. If user-space ask GPA bits <= 59 and GPA bits > 50 then we select Sv57x4.Quoted 5 lines
Thanks, that makes sense.
In v8 I’ll drop
KVM_CAP_RISCV_SET_HGATP_MODEand re-useKVM_CAP_VM_GPA_BITSfor both discovery and selection.Hi Anup,
While working on the respin reusing
KVM_CAP_VM_GPA_BITS, I realized a potential ambiguity inCHECK_EXTENSIONsemantics and wanted to confirm the intended ABI before posting v8.
Quoted 7 lines
One concern about the semantics: today
KVM_CHECK_EXTENSION(KVM_CAP_VM_GPA_BITS)on a VM fd may be interpreted as “the GPA bits for this VM” (or at least what this VM can use). If we also useKVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS)to downsize the selected HGATP.MODE for a particular VM (e.g. to Sv48x4 => 50 bits), then a subsequentCHECK_EXTENSION(KVM_CAP_VM_GPA_BITS)on the same VM fd would return 50. Userspace might then assume 50 is the maximum supported by that VM/host and lose the information that the host actually supports 59 (Sv57x4).
I think there is no violation of the semantics because we are providing a way to allow KVM user space change "the GPA bits for this VM” using KVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS) so subsequent CHECK_EXTENSION(KVM_CAP_VM_GPA_BITS) must return
effective number of GPA bits visible to the VM.
The only additional constraint I would enforce is that the KVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS) must
return -EBUSY if any of the Guest VCPUs have ran_atleast_once set.
Regards,
Anup
Quoted 54 lines
Thanks,
FangyuQuoted 50 lines
Thanks,
FangyuQuoted 47 lines
Quoted 42 lines
arch/riscv/kvm/vm.c +13
-2default : r =0 ;break ;@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +if (cap->flags) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_RISCV_MP_STATE_RESET : -if (cap->flags) -return -EINVAL ; kvm->arch.mp_state_reset = true;return 0 ; +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ; +#ifdef CONFIG_64BIT + kvm->arch.pgd_levels =3 + cap->args[0 ] -HGATP_MODE_SV39X4 ; +#endif +return 0 ;default :return -EINVAL ; }include/uapi/linux/kvm.h +1
-1@@ -989,6 +989,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_SEA_TO_USER 245#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247 +#define KVM_CAP_RISCV_SET_HGATP_MODE 248struct kvm_irq_routing_irqchip {__u32 irqchip; --Quoted 2 lines
2.50.1
Regards,
Anup
Re: Re: Re: Re: [PATCH v7 4/4] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
Quoted 113 lines
Quoted 105 lines
Quoted 93 lines
Quoted 85 lines · On Thu, Apr 2, 2026 at 6:53 PM <[email protected]> wrote:
Quoted 66 lines
From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via
KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with-EINVALif the mode is not supported by the host, and with-EBUSYif the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)returns a bitmask of the HGATP.MODE formats supported by the host.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>--- Documentation/virt/kvm/api.rst | 27 +++++++++++++++++++++++++++ arch/riscv/kvm/vm.c | 18 ++++++++++++++++-- include/uapi/linux/kvm.h | 1 + 3 files changed, 44 insertions(+), 2 deletions(-)Documentation/virt/kvm/api.rst +27
-0@@ -8902,6 +8902,33 @@ helpful if user space wants to emulate instructions which are not This capability can be enabled dynamically even if VCPUs were already created and are running. +7.47 KVM_CAP_RISCV_SET_HGATP_MODE +--------------------------------- + +:Architectures: riscv +:Type: VM +:Parameters: args[0] contains the requested HGATP mode +:Returns: + - 0 on success. + - -EINVAL if args[0] is outside the range of HGATP modes supported by the + hardware. + - -EBUSY if vCPUs have already been created for the VM, if the VM has any + non-empty memslots. + +This capability allows userspace to explicitly select the HGATP mode for +the VM. The selected mode must be supported by both KVM and hardware. This +capability must be enabled before creating any vCPUs or memslots. + +If this capability is not enabled, KVM will select the default HGATP mode +automatically. The default is the highest HGATP.MODE value supported by +hardware. + +``KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)`` returns a bitmask of +HGATP.MODE values supported by the host. A return value of 0 indicates that +the capability is not supported. Supported-mode bitmask use HGATP.MODE +encodings as defined by the RISC-V privileged specification, such as Sv39x4 +corresponds to HGATP.MODE=8, so userspace should test bitmask & BIT(8). + 8. Other capabilities. ======================arch/riscv/kvm/vm.c +0
-0arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 206
Quoted 6 lines
arch/riscv/kvm/vm.c +3
-0@@ -201,6 +201,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_VM_GPA_BITS : r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);break ; +case KVM_CAP_RISCV_SET_HGATP_MODE : + r =kvm_riscv_get_hgatp_mode_mask (); +break ;Introducing a new RISC-V capability looks a bit complex. Instead of
KVM_CAP_RISCV_SET_HGATP_MODE, we can simply re-useKVM_CAP_VM_GPA_BITS.The
kvm_vm_ioctl_check_extension()forKVM_CAP_VM_GPA_BITSreturn number of GPA bits which in-directly implies the underlying hgatp.MODE. As we know, if it return 59 bits GPA then it means Sv57x4 is the selected hgatp.MODE and Sv48x4 and Sv39x4 modes are also supported as-per RISC-V privileged specification.Quoted 7 lines
The
kvm_vm_ioctl_enable_cap()forKVM_CAP_VM_GPA_BITSwill take the desired number of GPA bits and downsize the selected hgatp.MODE. For example, if user-space ask GPA bits <= 50 and GPA bits > 41 then we select Sv48x4. If user-space ask GPA bits <= 41 then we select Sv39x4. If user-space ask GPA bits <= 59 and GPA bits > 50 then we select Sv57x4.Quoted 5 lines
Thanks, that makes sense.
In v8 I’ll drop
KVM_CAP_RISCV_SET_HGATP_MODEand re-useKVM_CAP_VM_GPA_BITSfor both discovery and selection.Hi Anup,
While working on the respin reusing
KVM_CAP_VM_GPA_BITS, I realized a potential ambiguity inCHECK_EXTENSIONsemantics and wanted to confirm the intended ABI before posting v8.Quoted 7 lines
One concern about the semantics: today
KVM_CHECK_EXTENSION(KVM_CAP_VM_GPA_BITS)on a VM fd may be interpreted as “the GPA bits for this VM” (or at least what this VM can use). If we also useKVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS)to downsize the selected HGATP.MODE for a particular VM (e.g. to Sv48x4 => 50 bits), then a subsequentCHECK_EXTENSION(KVM_CAP_VM_GPA_BITS)on the same VM fd would return 50. Userspace might then assume 50 is the maximum supported by that VM/host and lose the information that the host actually supports 59 (Sv57x4).
Quoted 5 lines
I think there is no violation of the semantics because we are providing a way to allow KVM user space change "the GPA bits for this VM” using
KVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS)so subsequentCHECK_EXTENSION(KVM_CAP_VM_GPA_BITS)must return
effective number of GPA bits visible to the VM.
Thanks, agreed.
Quoted 5 lines
The only additional constraint I would enforce is that the
KVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS)must
return-EBUSYif any of the Guest VCPUs haveran_atleast_onceset.
In my current implementation I already return -EBUSY if kvm->created_vcpus is non-zero, i.e. the GPA bits can only be changed before any vCPU is created.
Thanks,
Fangyu
Quoted 58 lines
Regards,
AnupQuoted 54 lines
Thanks,
FangyuQuoted 50 lines
Thanks,
FangyuQuoted 47 lines
Quoted 42 lines
arch/riscv/kvm/vm.c +13
-2default : r =0 ;break ;@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +if (cap->flags) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_RISCV_MP_STATE_RESET : -if (cap->flags) -return -EINVAL ; kvm->arch.mp_state_reset = true;return 0 ; +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ; +#ifdef CONFIG_64BIT + kvm->arch.pgd_levels =3 + cap->args[0 ] -HGATP_MODE_SV39X4 ; +#endif +return 0 ;default :return -EINVAL ; }include/uapi/linux/kvm.h +1
-1@@ -989,6 +989,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_SEA_TO_USER 245#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247 +#define KVM_CAP_RISCV_SET_HGATP_MODE 248struct kvm_irq_routing_irqchip {__u32 irqchip; --Quoted 2 lines
2.50.1
Regards,
Anup
Anup Patel
maintainerRe: Re: Re: Re: [PATCH v7 4/4] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
Quoted 122 lines · On Fri, Apr 3, 2026 at 12:37 PM <[email protected]> wrote:
Quoted 114 lines
Quoted 106 lines
Quoted 94 lines
Quoted 86 lines · On Thu, Apr 2, 2026 at 6:53 PM <[email protected]> wrote:
Quoted 66 lines
From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via
KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with-EINVALif the mode is not supported by the host, and with-EBUSYif the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)returns a bitmask of the HGATP.MODE formats supported by the host.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>--- Documentation/virt/kvm/api.rst | 27 +++++++++++++++++++++++++++ arch/riscv/kvm/vm.c | 18 ++++++++++++++++-- include/uapi/linux/kvm.h | 1 + 3 files changed, 44 insertions(+), 2 deletions(-)Documentation/virt/kvm/api.rst +27
-0@@ -8902,6 +8902,33 @@ helpful if user space wants to emulate instructions which are not This capability can be enabled dynamically even if VCPUs were already created and are running. +7.47 KVM_CAP_RISCV_SET_HGATP_MODE +--------------------------------- + +:Architectures: riscv +:Type: VM +:Parameters: args[0] contains the requested HGATP mode +:Returns: + - 0 on success. + - -EINVAL if args[0] is outside the range of HGATP modes supported by the + hardware. + - -EBUSY if vCPUs have already been created for the VM, if the VM has any + non-empty memslots. + +This capability allows userspace to explicitly select the HGATP mode for +the VM. The selected mode must be supported by both KVM and hardware. This +capability must be enabled before creating any vCPUs or memslots. + +If this capability is not enabled, KVM will select the default HGATP mode +automatically. The default is the highest HGATP.MODE value supported by +hardware. + +``KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)`` returns a bitmask of +HGATP.MODE values supported by the host. A return value of 0 indicates that +the capability is not supported. Supported-mode bitmask use HGATP.MODE +encodings as defined by the RISC-V privileged specification, such as Sv39x4 +corresponds to HGATP.MODE=8, so userspace should test bitmask & BIT(8). + 8. Other capabilities. ======================arch/riscv/kvm/vm.c +0
-0arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 206
Quoted 6 lines
arch/riscv/kvm/vm.c +3
-0@@ -201,6 +201,9 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)case KVM_CAP_VM_GPA_BITS : r =kvm_riscv_gstage_gpa_bits (kvm->arch.pgd_levels);break ; +case KVM_CAP_RISCV_SET_HGATP_MODE : + r =kvm_riscv_get_hgatp_mode_mask (); +break ;Introducing a new RISC-V capability looks a bit complex. Instead of
KVM_CAP_RISCV_SET_HGATP_MODE, we can simply re-useKVM_CAP_VM_GPA_BITS.The
kvm_vm_ioctl_check_extension()forKVM_CAP_VM_GPA_BITSreturn number of GPA bits which in-directly implies the underlying hgatp.MODE. As we know, if it return 59 bits GPA then it means Sv57x4 is the selected hgatp.MODE and Sv48x4 and Sv39x4 modes are also supported as-per RISC-V privileged specification.Quoted 7 lines
The
kvm_vm_ioctl_enable_cap()forKVM_CAP_VM_GPA_BITSwill take the desired number of GPA bits and downsize the selected hgatp.MODE. For example, if user-space ask GPA bits <= 50 and GPA bits > 41 then we select Sv48x4. If user-space ask GPA bits <= 41 then we select Sv39x4. If user-space ask GPA bits <= 59 and GPA bits > 50 then we select Sv57x4.Quoted 5 lines
Thanks, that makes sense.
In v8 I’ll drop
KVM_CAP_RISCV_SET_HGATP_MODEand re-useKVM_CAP_VM_GPA_BITSfor both discovery and selection.Hi Anup,
While working on the respin reusing
KVM_CAP_VM_GPA_BITS, I realized a potential ambiguity inCHECK_EXTENSIONsemantics and wanted to confirm the intended ABI before posting v8.Quoted 7 lines
One concern about the semantics: today
KVM_CHECK_EXTENSION(KVM_CAP_VM_GPA_BITS)on a VM fd may be interpreted as “the GPA bits for this VM” (or at least what this VM can use). If we also useKVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS)to downsize the selected HGATP.MODE for a particular VM (e.g. to Sv48x4 => 50 bits), then a subsequentCHECK_EXTENSION(KVM_CAP_VM_GPA_BITS)on the same VM fd would return 50. Userspace might then assume 50 is the maximum supported by that VM/host and lose the information that the host actually supports 59 (Sv57x4).Quoted 5 lines
I think there is no violation of the semantics because we are providing a way to allow KVM user space change "the GPA bits for this VM” using
KVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS)so subsequentCHECK_EXTENSION(KVM_CAP_VM_GPA_BITS)must return
effective number of GPA bits visible to the VM.Thanks, agreed.
Quoted 8 lines
Quoted 5 lines
The only additional constraint I would enforce is that the
KVM_ENABLE_CAP(KVM_CAP_VM_GPA_BITS)must
return-EBUSYif any of the Guest VCPUs haveran_atleast_onceset.In my current implementation I already return
-EBUSYifkvm->created_vcpusis non-zero, i.e. the GPA bits can only be changed before any vCPU is created.
Checking kvm->created_vcpus is perfectly fine so no need to change this.
Regards,
Anup
Quoted 62 lines
Thanks,
FangyuQuoted 58 lines
Regards,
AnupQuoted 54 lines
Thanks,
FangyuQuoted 50 lines
Thanks,
FangyuQuoted 47 lines
Quoted 42 lines
arch/riscv/kvm/vm.c +13
-2default : r =0 ;break ;@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +if (cap->flags) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_RISCV_MP_STATE_RESET : -if (cap->flags) -return -EINVAL ; kvm->arch.mp_state_reset = true;return 0 ; +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ; +#ifdef CONFIG_64BIT + kvm->arch.pgd_levels =3 + cap->args[0 ] -HGATP_MODE_SV39X4 ; +#endif +return 0 ;default :return -EINVAL ; }include/uapi/linux/kvm.h +1
-1@@ -989,6 +989,7 @@ struct kvm_enable_cap {#define KVM_CAP_ARM_SEA_TO_USER 245#define KVM_CAP_S390_USER_OPEREXEC 246#define KVM_CAP_S390_KEYOP 247 +#define KVM_CAP_RISCV_SET_HGATP_MODE 248struct kvm_irq_routing_irqchip {__u32 irqchip; --Quoted 2 lines
2.50.1
Regards,
Anup
Re: [PATCH v7 4/4] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
In reply to [email protected]2026-04-02T21:23:03+08:00, <[email protected]>:
Quoted 19 lines
From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via
KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with-EINVALif the mode is not supported by the host, and with-EBUSYif the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)returns a bitmask of the HGATP.MODE formats supported by the host.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>---
arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 222
Quoted 9 lines
arch/riscv/kvm/vm.c +6
-0@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ;
Since multiple VM ioctls can execute concurrently, I would protect created_vcpus by kvm->lock and kvm_are_all_memslots_empty by kvm->slots_lock.
Thanks.
Re: Re: [PATCH v7 4/4] RISC-V: KVM: add KVM_CAP_RISCV_SET_HGATP_MODE
Quoted 20 lines
Quoted 20 lines
From: Fangyu Yu <[email protected]>
Add a VM capability that allows userspace to select the G-stage page table format by setting HGATP.MODE on a per-VM basis.
Userspace enables the capability via
KVM_ENABLE_CAP, passing the requested HGATP.MODE in args[0]. The request is rejected with-EINVALif the mode is not supported by the host, and with-EBUSYif the VM has already been committed (e.g. vCPUs have been created or any memslot is populated).
KVM_CHECK_EXTENSION(KVM_CAP_RISCV_SET_HGATP_MODE)returns a bitmask of the HGATP.MODE formats supported by the host.Signed-off-by Fangyu Yu <[email protected]>Reviewed-by Andrew Jones <[email protected]>Reviewed-by Guo Ren <[email protected]>---
arch/riscv/kvm/vm.c +0
-0@@ -211,12 +214,23 @@ int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext)
Quoted 13 lines
arch/riscv/kvm/vm.c int kvm_vm_ioctl_check_extension(struct kvm *kvm, long ext) · line 229
Quoted 8 lines
arch/riscv/kvm/vm.c +6
-0int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { +case KVM_CAP_RISCV_SET_HGATP_MODE : +if (!kvm_riscv_hgatp_mode_is_valid (cap->args[0 ])) +return -EINVAL ; + +if (kvm->created_vcpus || !kvm_are_all_memslots_empty (kvm)) +return -EBUSY ;Since multiple VM ioctls can execute concurrently, I would protect
created_vcpusbykvm->lockandkvm_are_all_memslots_emptybykvm->slots_lock.
Agreed. I’ll protect created_vcpus with kvm->lock and call kvm_are_all_memslots_empty() under kvm->slots_lock, following the kvm->lock -> kvm->slots_lock ordering in v8.
Thanks,
Fangyu
Quoted 1 line
Thanks.