KVM: arm64: Preamble for pKVM
Subsystems (4)
v4 cover KVM: arm64: Preamble for pKVM
Changes from V3 [0]:
- Rebased on latest kvmarm/next (dcbf421a8c08)
- Dropped atomic and locking patches for now, while we rework them
- Misc fixes (Oliver)
We are getting closer to upstreaming the remaining part of pKVM [1]. To make the process easier for us and for our dear reviewers, we are sending out this patch series as a preamble to the upcoming patches.
This series is based on Linux 6.9-rc3 -- kvmarm/next (dcbf421a8c08). Most of the patches in this series are self-standing, and can be applied directly. Patches 1 to 13 are fixes.
This series is a bit of a bombay-mix of patches we've been carrying. There's no one overarching theme, but they do improve the code by fixing existing bugs in pKVM, refactoring code to make it more readable and easier to re-use for pKVM, or adding functionality to the existing pKVM code upstream.
For a technical deep dive into pKVM, please refer to Quentin Perret's KVM Forum Presentation [2, 3]. The pKVM core series, which we plan on sending for review next, the code is here [1].
Cheers,
Fuad, Quentin, Will, and Marc
[0] https://lore.kernel.org/all/[email protected]/
[1] https://android-kvm.googlesource.com/linux/+/refs/heads/for-upstream/pkvm-core
[2] Protected KVM on arm64 (slides)
https://static.sched.com/hosted_files/kvmforum2022/88/KVM%20forum%202022%20-%20pKVM%20deep%20dive.pdf
[3] Protected KVM on arm64 (video)
https://www.youtube.com/watch?v=9npebeVFbFw
Fuad Tabba (18):
KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in
pKVM
KVM: arm64: Move guest_owns_fp_regs() to increase its scope
KVM: arm64: Refactor checks for FP state ownership
KVM: arm64: Do not re-initialize the KVM lock
KVM: arm64: Rename __tlb_switch_to_{guest,host}() in VHE
KVM: arm64: Do not map the host fpsimd state to hyp in pKVM
KVM: arm64: Fix comment for __pkvm_vcpu_init_traps()
KVM: arm64: Change kvm_handle_mmio_return() return polarity
KVM: arm64: Move setting the page as dirty out of the critical section
KVM: arm64: Introduce and use predicates that check for protected VMs
KVM: arm64: Move pstate reset value definitions to kvm_arm.h
KVM: arm64: Clarify rationale for ZCR_EL1 value restored on guest exit
KVM: arm64: Refactor calculating SVE state size to use helpers
KVM: arm64: Move some kvm_psci functions to a shared header
KVM: arm64: Refactor reset_mpidr() to extract its computation
KVM: arm64: Refactor kvm_vcpu_enable_ptrauth() for hyp use
KVM: arm64: Refactor setting the return value in
kvm_vm_ioctl_enable_cap()
KVM: arm64: Restrict supported capabilities for protected VMs
Marc Zyngier (3):
KVM: arm64: Check for PTE validity when checking for
executable/cacheable
KVM: arm64: Simplify vgic-v3 hypercalls
KVM: arm64: Force injection of a data abort on NISV MMIO exit
Quentin Perret (4):
KVM: arm64: Issue CMOs when tearing down guest s2 pages
KVM: arm64: Avoid BUG-ing from the host abort path
KVM: arm64: Prevent kmemleak from accessing .hyp.data
KVM: arm64: Add is_pkvm_initialized() helper
Will Deacon (5):
KVM: arm64: Avoid BBM when changing only s/w bits in Stage-2 PTE
KVM: arm64: Support TLB invalidation in guest context
KVM: arm64: Reformat/beautify PTP hypercall documentation
KVM: arm64: Rename firmware pseudo-register documentation file
KVM: arm64: Document the KVM/arm64-specific calls in hypercalls.rst
Documentation/virt/kvm/api.rst | 7 + .../virt/kvm/arm/fw-pseudo-registers.rst | 138 ++++++++++++++ Documentation/virt/kvm/arm/hypercalls.rst | 180 +++++------------- Documentation/virt/kvm/arm/index.rst | 1 + Documentation/virt/kvm/arm/ptp_kvm.rst | 38 ++-- arch/arm64/include/asm/kvm_arm.h | 12 ++ arch/arm64/include/asm/kvm_asm.h | 8 +- arch/arm64/include/asm/kvm_emulate.h | 11 +- arch/arm64/include/asm/kvm_host.h | 39 ++-- arch/arm64/include/asm/kvm_hyp.h | 4 +- arch/arm64/include/asm/virt.h | 12 +- arch/arm64/kvm/arm.c | 63 ++++-- arch/arm64/kvm/fpsimd.c | 60 +++--- arch/arm64/kvm/hyp/include/hyp/switch.h | 8 +- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 6 + arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 +-- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 8 +- arch/arm64/kvm/hyp/nvhe/pkvm.c | 14 +- arch/arm64/kvm/hyp/nvhe/setup.c | 1 + arch/arm64/kvm/hyp/nvhe/switch.c | 10 +- arch/arm64/kvm/hyp/nvhe/tlb.c | 115 ++++++++--- arch/arm64/kvm/hyp/pgtable.c | 21 +- arch/arm64/kvm/hyp/vgic-v3-sr.c | 27 ++- arch/arm64/kvm/hyp/vhe/switch.c | 4 +- arch/arm64/kvm/hyp/vhe/tlb.c | 26 +-- arch/arm64/kvm/mmio.c | 12 +- arch/arm64/kvm/mmu.c | 8 +- arch/arm64/kvm/pkvm.c | 2 +- arch/arm64/kvm/psci.c | 28 --- arch/arm64/kvm/reset.c | 20 +- arch/arm64/kvm/sys_regs.c | 14 +- arch/arm64/kvm/sys_regs.h | 19 ++ arch/arm64/kvm/vgic/vgic-v2.c | 9 +- arch/arm64/kvm/vgic/vgic-v3.c | 23 +-- arch/arm64/kvm/vgic/vgic.c | 11 -- arch/arm64/kvm/vgic/vgic.h | 2 - include/kvm/arm_psci.h | 29 +++ include/kvm/arm_vgic.h | 1 - 38 files changed, 597 insertions(+), 418 deletions(-) create mode 100644 Documentation/virt/kvm/arm/fw-pseudo-registers.rst
base-commit: dcbf421a8c082f52f3b8cc19cff736374df123d6
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 1/30 KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Since the host_fpsimd_state has been removed from kvm_vcpu_arch, it isn't pointing to the hyp's version of the host fp_regs in protected mode.
Initialize the host_data fpsimd_state point to the host_data's context fp_regs on pKVM initialization.
--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 1 + 3 files changed, 13 insertions(+)
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1 -0
@@ -54,6 +54,7 @@ pkvm_hyp_vcpu_to_hyp_vm(struct pkvm_hyp_vcpu *hyp_vcpu) }void pkvm_hyp_vm_table_init (void *tbl); +void pkvm_host_fpsimd_state_init (void );int __pkvm_init_vm (struct kvm *host_kvm,unsigned long vm_hva,unsigned long pgd_hva);
arch/arm64/kvm/hyp/nvhe/pkvm.c +11 -0
@@ -247,6 +247,17 @@ void pkvm_hyp_vm_table_init(void *tbl) vm_table = tbl; } +void pkvm_host_fpsimd_state_init (void ) +{ +unsigned long i; + +for (i =0 ; i < hyp_nr_cpus; i++) { +struct kvm_host_data *host_data =per_cpu_ptr (&kvm_host_data, i); + + host_data->fpsimd_state = &host_data->host_ctxt.fp_regs; + } +} +/* * Return the hyp vm structure corresponding to the handle. */
arch/arm64/kvm/hyp/nvhe/setup.c +1 -0
@@ -300,6 +300,7 @@ void __noreturn __pkvm_init_finalise(void) goto out;pkvm_hyp_vm_table_init (vm_table_base); +pkvm_host_fpsimd_state_init (); out:/* * We tail-called to here from handle___pkvm_init() and will not return,
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 2/30 KVM: arm64: Move guest_owns_fp_regs() to increase its scope
In reply to Fuad Tabbaguest_owns_fp_regs() will be used to check fpsimd state ownership across kvm/arm64. Therefore, move it to kvm_host.h to widen its scope.
Moreover, the host state is not per-vcpu anymore, the vcpu parameter isn't used, so remove it as well.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 6 ++++++ arch/arm64/kvm/hyp/include/hyp/switch.h | 6 ------ arch/arm64/kvm/hyp/nvhe/switch.c | 2 +- arch/arm64/kvm/hyp/vhe/switch.c | 2 +- 4 files changed, 8 insertions(+), 8 deletions(-)
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -1212,6 +1212,12 @@ DECLARE_KVM_HYP_PER_CPU(struct kvm_host_data, kvm_host_data); &this_cpu_ptr_hyp_sym(kvm_host_data)->f)#endif +/* Check whether the FP regs are owned by the guest */ +static inline bool guest_owns_fp_regs (void ) +{ +return *host_data_ptr (fp_owner) ==FP_STATE_GUEST_OWNED ; +} +static inline void kvm_init_host_cpu_context (struct kvm_cpu_context *cpu_ctxt) {/* The host's MPIDR is immutable, so let's set it up at boot time */
arch/arm64/kvm/hyp/include/hyp/switch.h +0 -6
@@ -40,12 +40,6 @@ struct kvm_exception_table_entry {extern struct kvm_exception_table_entry __start___kvm_ex_table;extern struct kvm_exception_table_entry __stop___kvm_ex_table; -/* Check whether the FP regs are owned by the guest */ -static inline bool guest_owns_fp_regs (struct kvm_vcpu *vcpu) -{ -return *host_data_ptr (fp_owner) ==FP_STATE_GUEST_OWNED ; -} -/* Save the 32-bit only FPSIMD system register state */ static inline void __fpsimd_save_fpexc32 (struct kvm_vcpu *vcpu) {
arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
@@ -53,7 +53,7 @@ static void __activate_traps(struct kvm_vcpu *vcpu) val |=CPTR_EL2_TSM ; } -if (!guest_owns_fp_regs (vcpu)) { +if (!guest_owns_fp_regs ()) {if (has_hvhe ()) val &= ~(CPACR_EL1_FPEN_EL0EN |CPACR_EL1_FPEN_EL1EN |CPACR_EL1_ZEN_EL0EN |CPACR_EL1_ZEN_EL1EN );
arch/arm64/kvm/hyp/vhe/switch.c +1 -1
@@ -107,7 +107,7 @@ static void __activate_traps(struct kvm_vcpu *vcpu) val |=CPTR_EL2_TAM ; -if (guest_owns_fp_regs (vcpu)) { +if (guest_owns_fp_regs ()) {if (vcpu_has_sve (vcpu)) val |=CPACR_EL1_ZEN_EL0EN |CPACR_EL1_ZEN_EL1EN ; }else {
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 3/30 KVM: arm64: Refactor checks for FP state ownership
In reply to Fuad TabbaTo avoid direct comparison against the fp_owner enum, add a new function that performs the check, host_owns_fp_regs(), to complement the existing guest_owns_fp_regs().
To check for fpsimd state ownership, use the helpers instead of directly using the enums.
No functional change intended.
--- arch/arm64/include/asm/kvm_emulate.h | 6 ++---- arch/arm64/include/asm/kvm_host.h | 6 ++++++ arch/arm64/kvm/fpsimd.c | 5 ++--- arch/arm64/kvm/hyp/include/hyp/switch.h | 2 +- arch/arm64/kvm/hyp/nvhe/switch.c | 2 +- arch/arm64/kvm/hyp/vhe/switch.c | 2 +- 6 files changed, 13 insertions(+), 10 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +2 -4
@@ -577,16 +577,14 @@ static __always_inline u64 kvm_get_reset_cptr_el2(struct kvm_vcpu *vcpu) }else if (has_hvhe ()) { val = (CPACR_EL1_FPEN_EL0EN |CPACR_EL1_FPEN_EL1EN ); -if (!vcpu_has_sve (vcpu) || - (*host_data_ptr (fp_owner) !=FP_STATE_GUEST_OWNED )) +if (!vcpu_has_sve (vcpu) || !guest_owns_fp_regs ()) val |=CPACR_EL1_ZEN_EL1EN |CPACR_EL1_ZEN_EL0EN ;if (cpus_have_final_cap (ARM64_SME )) val |=CPACR_EL1_SMEN_EL1EN |CPACR_EL1_SMEN_EL0EN ; }else { val =CPTR_NVHE_EL2_RES1 ; -if (vcpu_has_sve (vcpu) && - (*host_data_ptr (fp_owner) ==FP_STATE_GUEST_OWNED )) +if (vcpu_has_sve (vcpu) &&guest_owns_fp_regs ()) val |=CPTR_EL2_TZ ;if (cpus_have_final_cap (ARM64_SME )) val &= ~CPTR_EL2_TSM ;
arch/arm64/include/asm/kvm_host.h +6 -0
@@ -1218,6 +1218,12 @@ static inline bool guest_owns_fp_regs(void)return *host_data_ptr (fp_owner) ==FP_STATE_GUEST_OWNED ; } +/* Check whether the FP regs are owned by the host */ +static inline bool host_owns_fp_regs (void ) +{ +return *host_data_ptr (fp_owner) ==FP_STATE_HOST_OWNED ; +} +static inline void kvm_init_host_cpu_context (struct kvm_cpu_context *cpu_ctxt) {/* The host's MPIDR is immutable, so let's set it up at boot time */
arch/arm64/kvm/fpsimd.c +2 -3
@@ -141,8 +141,7 @@ void kvm_arch_vcpu_ctxsync_fp(struct kvm_vcpu *vcpu)WARN_ON_ONCE (!irqs_disabled ()); -if (*host_data_ptr (fp_owner) ==FP_STATE_GUEST_OWNED ) { - +if (guest_owns_fp_regs ()) {/* * Currently we do not support SME guests so SVCR is * always 0 and we just need a variable to point to.
@@ -195,7 +194,7 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)isb (); } -if (*host_data_ptr (fp_owner) ==FP_STATE_GUEST_OWNED ) { +if (guest_owns_fp_regs ()) {if (vcpu_has_sve (vcpu)) {__vcpu_sys_reg (vcpu,ZCR_EL1 ) =read_sysreg_el1 (SYS_ZCR );
arch/arm64/kvm/hyp/include/hyp/switch.h +1 -1
@@ -369,7 +369,7 @@ static bool kvm_hyp_handle_fpsimd(struct kvm_vcpu *vcpu, u64 *exit_code)isb ();/* Write out the host state if it's in the registers */ -if (*host_data_ptr (fp_owner) ==FP_STATE_HOST_OWNED ) +if (host_owns_fp_regs ())__fpsimd_save_state (*host_data_ptr (fpsimd_state));/* Restore the guest state */
arch/arm64/kvm/hyp/nvhe/switch.c +1 -1
@@ -335,7 +335,7 @@ int __kvm_vcpu_run(struct kvm_vcpu *vcpu)__sysreg_restore_state_nvhe (host_ctxt); -if (*host_data_ptr (fp_owner) ==FP_STATE_GUEST_OWNED ) +if (guest_owns_fp_regs ())__fpsimd_save_fpexc32 (vcpu);__debug_switch_to_host (vcpu);
arch/arm64/kvm/hyp/vhe/switch.c +1 -1
@@ -338,7 +338,7 @@ static int __kvm_vcpu_run_vhe(struct kvm_vcpu *vcpu)sysreg_restore_host_state_vhe (host_ctxt); -if (*host_data_ptr (fp_owner) ==FP_STATE_GUEST_OWNED ) +if (guest_owns_fp_regs ())__fpsimd_save_fpexc32 (vcpu);__debug_switch_to_host (vcpu);
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 4/30 KVM: arm64: Do not re-initialize the KVM lock
In reply to Fuad TabbaThe lock is already initialized in core KVM code at kvm_create_vm().
--- arch/arm64/kvm/pkvm.c | 1 - 1 file changed, 1 deletion(-)
arch/arm64/kvm/pkvm.c +0 -1
@@ -222,7 +222,6 @@ void pkvm_destroy_hyp_vm(struct kvm *host_kvm)int pkvm_init_host_vm (struct kvm *host_kvm) { -mutex_init (&host_kvm->lock);return 0 ; }
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 5/30 KVM: arm64: Issue CMOs when tearing down guest s2 pages
In reply to Fuad TabbaFrom: Quentin Perret <[email protected]>
On the guest teardown path, pKVM will zero the pages used to back the guest data structures before returning them to the host as they may contain secrets (e.g. in the vCPU registers). However, the zeroing is done using a cacheable alias, and CMOs are missing, hence giving the host a potential opportunity to read the original content of the guest structs from memory.
Fix this by issuing CMOs after zeroing the pages.
--- arch/arm64/kvm/hyp/nvhe/pkvm.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -0
@@ -441,6 +441,7 @@ static void *map_donated_memory(unsigned long host_va, size_t size)static void __unmap_donated_memory (void *va,size_t size) { +kvm_flush_dcache_to_poc (va, size);WARN_ON (__pkvm_hyp_donate_host (hyp_virt_to_pfn (va),PAGE_ALIGN (size) >>PAGE_SHIFT )); }
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 6/30 KVM: arm64: Avoid BUG-ing from the host abort path
In reply to Fuad TabbaFrom: Quentin Perret <[email protected]>
Under certain circumstances __get_fault_info() may resolve the faulting address using the AT instruction. Given that this is being done outside of the host lock critical section, it is racy and the resolution via AT may fail. We currently BUG() in this situation, which is obviously less than ideal. Moving the address resolution to the critical section may have a performance impact, so let's keep it where it is, but bail out and return to the host to try a second time.
--- arch/arm64/kvm/hyp/nvhe/mem_protect.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/mem_protect.c +7 -1
@@ -533,7 +533,13 @@ void handle_host_mem_abort(struct kvm_cpu_context *host_ctxt)int ret =0 ; esr =read_sysreg_el2 (SYS_ESR ); -BUG_ON (!__get_fault_info (esr, &fault)); +if (!__get_fault_info (esr, &fault)) { +/* +* We've presumably raced with a page-table change which caused +* AT to fail, try again. +*/ +return ; + } addr = (fault.hpfar_el2 &HPFAR_MASK ) <<8 ; ret =host_stage2_idmap (addr);
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 7/30 KVM: arm64: Check for PTE validity when checking for executable/cacheable
In reply to Fuad TabbaFrom: Marc Zyngier <[email protected]>
Don't just assume that the PTE is valid when checking whether it describes an executable or cacheable mapping.
This makes sure that we don't issue CMOs for invalid mappings.
--- arch/arm64/kvm/hyp/pgtable.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kvm/hyp/pgtable.c +3 -3
@@ -914,12 +914,12 @@ static void stage2_unmap_put_pte(const struct kvm_pgtable_visit_ctx *ctx,static bool stage2_pte_cacheable (struct kvm_pgtable *pgt,kvm_pte_t pte) {u64 memattr = pte &KVM_PTE_LEAF_ATTR_LO_S2_MEMATTR ; -return memattr ==KVM_S2_MEMATTR (pgt,NORMAL ); +return kvm_pte_valid (pte) && memattr ==KVM_S2_MEMATTR (pgt,NORMAL ); }static bool stage2_pte_executable (kvm_pte_t pte) { -return !(pte &KVM_PTE_LEAF_ATTR_HI_S2_XN ); +return kvm_pte_valid (pte) && !(pte &KVM_PTE_LEAF_ATTR_HI_S2_XN ); } static u64 stage2_map_walker_phys_addr(const struct kvm_pgtable_visit_ctx *ctx,
@@ -1370,7 +1370,7 @@ static int stage2_flush_walker(const struct kvm_pgtable_visit_ctx *ctx,struct kvm_pgtable *pgt = ctx->arg;struct kvm_pgtable_mm_ops *mm_ops = pgt->mm_ops; -if (!kvm_pte_valid (ctx->old) || !stage2_pte_cacheable (pgt, ctx->old)) +if (!stage2_pte_cacheable (pgt, ctx->old))return 0 ;if (mm_ops->dcache_clean_inval_poc)
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 8/30 KVM: arm64: Avoid BBM when changing only s/w bits in Stage-2 PTE
In reply to Fuad TabbaFrom: Will Deacon <[email protected]>
Break-before-make (BBM) can be expensive, as transitioning via an invalid mapping (i.e. the "break" step) requires the completion of TLB invalidation and can also cause other agents to fault concurrently on the invalid mapping.
Since BBM is not required when changing only the software bits of a PTE, avoid the sequence in this case and just update the PTE directly.
--- arch/arm64/kvm/hyp/pgtable.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
arch/arm64/kvm/hyp/pgtable.c +15 -0
@@ -979,6 +979,21 @@ static int stage2_map_walker_try_leaf(const struct kvm_pgtable_visit_ctx *ctx,if (!stage2_pte_needs_update (ctx->old, new))return -EAGAIN ; +/* If we're only changing software bits, then store them and go! */ +if (!kvm_pgtable_walk_shared (ctx) && + !((ctx->old ^ new) & ~KVM_PTE_LEAF_ATTR_HI_SW )) { +bool old_is_counted =stage2_pte_is_counted (ctx->old); + +if (old_is_counted !=stage2_pte_is_counted (new)) { +if (old_is_counted) + mm_ops->put_page (ctx->ptep); +else + mm_ops->get_page (ctx->ptep); + } +WARN_ON_ONCE (!stage2_try_set_pte (ctx, new)); +return 0 ; + } +if (!stage2_try_break_pte (ctx, data->mmu))return -EAGAIN ;
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 9/30 KVM: arm64: Support TLB invalidation in guest context
In reply to Fuad TabbaFrom: Will Deacon <[email protected]>
Typically, TLB invalidation of guest stage-2 mappings using nVHE is performed by a hypercall originating from the host. For the invalidation instruction to be effective, therefore, __tlb_switch_to_{guest,host}() swizzle the active stage-2 context around the TLBI instruction.
With guest-to-host memory sharing and unsharing hypercalls originating from the guest under pKVM, there is need to support both guest and host VMID invalidations issued from guest context.
Replace the __tlb_switch_to_{guest,host}() functions with a more general {enter,exit}_vmid_context() implementation which supports being invoked from guest context and acts as a no-op if the target context matches the running context.
--- arch/arm64/kvm/hyp/nvhe/tlb.c | 115 +++++++++++++++++++++++++++------- 1 file changed, 91 insertions(+), 24 deletions(-)
arch/arm64/kvm/hyp/nvhe/tlb.c +91 -24
@@ -11,13 +11,23 @@#include <nvhe/mem_protect.h> struct tlb_inv_context { -u64 tcr; +struct kvm_s2_mmu *mmu; +u64 tcr; +u64 sctlr; }; -static void __tlb_switch_to_guest (struct kvm_s2_mmu *mmu, -struct tlb_inv_context *cxt, -bool nsh) +static void enter_vmid_context (struct kvm_s2_mmu *mmu, +struct tlb_inv_context *cxt, +bool nsh) { +struct kvm_s2_mmu *host_s2_mmu = &host_mmu.arch.mmu; +struct kvm_cpu_context *host_ctxt; +struct kvm_vcpu *vcpu; + + host_ctxt = &this_cpu_ptr (&kvm_host_data)->host_ctxt; + vcpu = host_ctxt->__hyp_running_vcpu; + cxt->mmu =NULL ; +/* * We have two requirements: *
@@ -40,20 +50,55 @@ static void __tlb_switch_to_guest(struct kvm_s2_mmu *mmu,else dsb (ish); +/* +* If we're already in the desired context, then there's nothing to do. +*/ +if (vcpu) { +/* +* We're in guest context. However, for this to work, this needs +* to be called from within __kvm_vcpu_run(), which ensures that +* __hyp_running_vcpu is set to the current guest vcpu. +*/ +if (mmu == vcpu->arch.hw_mmu ||WARN_ON (mmu != host_s2_mmu)) +return ; + + cxt->mmu = vcpu->arch.hw_mmu; + }else { +/* We're in host context. */ +if (mmu == host_s2_mmu) +return ; + + cxt->mmu = host_s2_mmu; + } +if (cpus_have_final_cap (ARM64_WORKAROUND_SPECULATIVE_AT )) {u64 val;/* * For CPUs that are affected by ARM 1319367, we need to -* avoid a host Stage-1 walk while we have the guest's -* VMID set in the VTTBR in order to invalidate TLBs. -* We're guaranteed that the S1 MMU is enabled, so we can -* simply set the EPD bits to avoid any further TLB fill. +* avoid a Stage-1 walk with the old VMID while we have +* the new VMID set in the VTTBR in order to invalidate TLBs. +* We're guaranteed that the host S1 MMU is enabled, so +* we can simply set the EPD bits to avoid any further +* TLB fill. For guests, we ensure that the S1 MMU is +* temporarily enabled in the next context. */ val = cxt->tcr =read_sysreg_el1 (SYS_TCR ); val |=TCR_EPD1_MASK |TCR_EPD0_MASK ;write_sysreg_el1 (val,SYS_TCR );isb (); + +if (vcpu) { + val = cxt->sctlr =read_sysreg_el1 (SYS_SCTLR ); +if (!(val & SCTLR_ELx_M)) { + val |= SCTLR_ELx_M; +write_sysreg_el1 (val,SYS_SCTLR ); +isb (); + } + }else { +/* The host S1 MMU is always enabled. */ + cxt->sctlr = SCTLR_ELx_M; + } }/*
@@ -62,18 +107,40 @@ static void __tlb_switch_to_guest(struct kvm_s2_mmu *mmu,* ensuring that we always have an ISB, but not two ISBs back * to back. */ -__load_stage2 (mmu,kern_hyp_va (mmu->arch)); +if (vcpu) +__load_host_stage2 (); +else +__load_stage2 (mmu,kern_hyp_va (mmu->arch)); + asm(ALTERNATIVE("isb", "nop", ARM64_WORKAROUND_SPECULATIVE_AT)); } -static void __tlb_switch_to_host (struct tlb_inv_context *cxt) +static void exit_vmid_context (struct tlb_inv_context *cxt) { -__load_host_stage2 (); +struct kvm_s2_mmu *mmu = cxt->mmu; +struct kvm_cpu_context *host_ctxt; +struct kvm_vcpu *vcpu; + + host_ctxt = &this_cpu_ptr (&kvm_host_data)->host_ctxt; + vcpu = host_ctxt->__hyp_running_vcpu; + +if (!mmu) +return ; + +if (vcpu) +__load_stage2 (mmu,kern_hyp_va (mmu->arch)); +else +__load_host_stage2 ();if (cpus_have_final_cap (ARM64_WORKAROUND_SPECULATIVE_AT )) { -/* Ensure write of the host VMID */ +/* Ensure write of the old VMID */ isb (); -/* Restore the host's TCR_EL1 */ + +if (!(cxt->sctlr & SCTLR_ELx_M)) { +write_sysreg_el1 (cxt->sctlr,SYS_SCTLR ); +isb (); + } +write_sysreg_el1 (cxt->tcr,SYS_TCR ); } }
@@ -84,7 +151,7 @@ void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,struct tlb_inv_context cxt;/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt, false); +enter_vmid_context (mmu, &cxt, false);/* * We could do so much better if we had the VA as well.
@@ -105,7 +172,7 @@ void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,dsb (ish);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); } void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,
@@ -114,7 +181,7 @@ void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,struct tlb_inv_context cxt;/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt, true); +enter_vmid_context (mmu, &cxt, true);/* * We could do so much better if we had the VA as well.
@@ -135,7 +202,7 @@ void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,dsb (nsh);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); } void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
@@ -152,7 +219,7 @@ void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu, start =round_down (start, stride);/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt, false); +enter_vmid_context (mmu, &cxt, false);__flush_s2_tlb_range_op (ipas2e1is, start, pages, stride,TLBI_TTL_UNKNOWN );
@@ -162,7 +229,7 @@ void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,dsb (ish);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); }void __kvm_tlb_flush_vmid (struct kvm_s2_mmu *mmu)
@@ -170,13 +237,13 @@ void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu)struct tlb_inv_context cxt;/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt, false); +enter_vmid_context (mmu, &cxt, false);__tlbi (vmalls12e1is);dsb (ish);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); }void __kvm_flush_cpu_context (struct kvm_s2_mmu *mmu)
@@ -184,19 +251,19 @@ void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu)struct tlb_inv_context cxt;/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt, false); +enter_vmid_context (mmu, &cxt, false);__tlbi (vmalle1); asmvolatile ("ic iallu" );dsb (nsh);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); }void __kvm_flush_vm_context (void ) { -/* Same remark as in __tlb_switch_to_guest() */ +/* Same remark as in enter_vmid_context() */ dsb (ish);__tlbi (alle1is);dsb (ish);
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 10/30 KVM: arm64: Rename __tlb_switch_to_{guest,host}() in VHE
In reply to Fuad TabbaRename __tlb_switch_to_{guest,host}() to {enter,exit}_vmid_context() in VHE code to maintain symmetry between the nVHE and VHE TLB invalidations.
No functional change intended.
--- arch/arm64/kvm/hyp/vhe/tlb.c | 26 +++++++++++++------------- 1 file changed, 13 insertions(+), 13 deletions(-)
arch/arm64/kvm/hyp/vhe/tlb.c +13 -13
@@ -17,8 +17,8 @@ struct tlb_inv_context {u64 sctlr; }; -static void __tlb_switch_to_guest (struct kvm_s2_mmu *mmu, -struct tlb_inv_context *cxt) +static void enter_vmid_context (struct kvm_s2_mmu *mmu, +struct tlb_inv_context *cxt) {struct kvm_vcpu *vcpu =kvm_get_running_vcpu ();u64 val;
@@ -67,7 +67,7 @@ static void __tlb_switch_to_guest(struct kvm_s2_mmu *mmu,isb (); } -static void __tlb_switch_to_host (struct tlb_inv_context *cxt) +static void exit_vmid_context (struct tlb_inv_context *cxt) {/* * We're done with the TLB operation, let's restore the host's
@@ -97,7 +97,7 @@ void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,dsb (ishst);/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt); +enter_vmid_context (mmu, &cxt);/* * We could do so much better if we had the VA as well.
@@ -118,7 +118,7 @@ void __kvm_tlb_flush_vmid_ipa(struct kvm_s2_mmu *mmu,dsb (ish);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); } void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,
@@ -129,7 +129,7 @@ void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,dsb (nshst);/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt); +enter_vmid_context (mmu, &cxt);/* * We could do so much better if we had the VA as well.
@@ -150,7 +150,7 @@ void __kvm_tlb_flush_vmid_ipa_nsh(struct kvm_s2_mmu *mmu,dsb (nsh);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); } void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,
@@ -169,7 +169,7 @@ void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,dsb (ishst);/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt); +enter_vmid_context (mmu, &cxt);__flush_s2_tlb_range_op (ipas2e1is, start, pages, stride,TLBI_TTL_UNKNOWN );
@@ -179,7 +179,7 @@ void __kvm_tlb_flush_vmid_range(struct kvm_s2_mmu *mmu,dsb (ish);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); }void __kvm_tlb_flush_vmid (struct kvm_s2_mmu *mmu)
@@ -189,13 +189,13 @@ void __kvm_tlb_flush_vmid(struct kvm_s2_mmu *mmu)dsb (ishst);/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt); +enter_vmid_context (mmu, &cxt);__tlbi (vmalls12e1is);dsb (ish);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); }void __kvm_flush_cpu_context (struct kvm_s2_mmu *mmu)
@@ -203,14 +203,14 @@ void __kvm_flush_cpu_context(struct kvm_s2_mmu *mmu)struct tlb_inv_context cxt;/* Switch to requested VMID */ -__tlb_switch_to_guest (mmu, &cxt); +enter_vmid_context (mmu, &cxt);__tlbi (vmalle1); asmvolatile ("ic iallu" );dsb (nsh);isb (); -__tlb_switch_to_host (&cxt); +exit_vmid_context (&cxt); }void __kvm_flush_vm_context (void )
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 11/30 KVM: arm64: Do not map the host fpsimd state to hyp in pKVM
In reply to Fuad TabbapKVM maintains its own state at EL2 for tracking the host fpsimd state. Therefore, no need to map and share the host's view with it.
--- arch/arm64/include/asm/kvm_host.h | 3 --- arch/arm64/kvm/fpsimd.c | 31 ++++--------------------------- arch/arm64/kvm/reset.c | 1 - 3 files changed, 4 insertions(+), 31 deletions(-)
arch/arm64/include/asm/kvm_host.h +0 -3
@@ -663,8 +663,6 @@ struct kvm_vcpu_arch {struct kvm_guest_debug_arch vcpu_debug_state;struct kvm_guest_debug_arch external_debug_state; -struct task_struct *parent_task; -/* VGIC state */ struct vgic_cpu vgic_cpu;struct arch_timer_cpu timer_cpu;
@@ -1267,7 +1265,6 @@ void kvm_arch_vcpu_load_fp(struct kvm_vcpu *vcpu);void kvm_arch_vcpu_ctxflush_fp (struct kvm_vcpu *vcpu);void kvm_arch_vcpu_ctxsync_fp (struct kvm_vcpu *vcpu);void kvm_arch_vcpu_put_fp (struct kvm_vcpu *vcpu); -void kvm_vcpu_unshare_task_fp (struct kvm_vcpu *vcpu);static inline bool kvm_pmu_counter_deferred (struct perf_event_attr *attr) {
arch/arm64/kvm/fpsimd.c +4 -27
@@ -14,19 +14,6 @@#include <asm/kvm_mmu.h> #include <asm/sysreg.h> -void kvm_vcpu_unshare_task_fp (struct kvm_vcpu *vcpu) -{ -struct task_struct *p = vcpu->arch.parent_task; -struct user_fpsimd_state *fpsimd; - -if (!is_protected_kvm_enabled () || !p) -return ; - - fpsimd = &p->thread.uw.fpsimd_state; -kvm_unshare_hyp (fpsimd, fpsimd +1 ); -put_task_struct (p); -} -/* * Called on entry to KVM_RUN unless this vcpu previously ran at least * once and the most recent prior KVM_RUN for this vcpu was called from
@@ -38,28 +25,18 @@ void kvm_vcpu_unshare_task_fp(struct kvm_vcpu *vcpu)*/ int kvm_arch_vcpu_run_map_fp (struct kvm_vcpu *vcpu) { -int ret; -struct user_fpsimd_state *fpsimd = ¤t->thread.uw.fpsimd_state; +int ret; -kvm_vcpu_unshare_task_fp (vcpu); +/* pKVM has its own tracking of the host fpsimd state. */ +if (is_protected_kvm_enabled ()) +return 0 ;/* Make sure the host task fpsimd state is visible to hyp: */ ret =kvm_share_hyp (fpsimd, fpsimd +1 );if (ret)return ret; -/* -* We need to keep current's task_struct pinned until its data has been -* unshared with the hypervisor to make sure it is not re-used by the -* kernel and donated to someone else while already shared -- see -* kvm_vcpu_unshare_task_fp() for the matching put_task_struct(). -*/ -if (is_protected_kvm_enabled ()) { -get_task_struct (current); - vcpu->arch.parent_task = current; - } -return 0 ; }
arch/arm64/kvm/reset.c +0 -1
@@ -151,7 +151,6 @@ void kvm_arm_vcpu_destroy(struct kvm_vcpu *vcpu) {void *sve_state = vcpu->arch.sve_state; -kvm_vcpu_unshare_task_fp (vcpu);kvm_unshare_hyp (vcpu, vcpu +1 );if (sve_state)kvm_unshare_hyp (sve_state, sve_state +vcpu_sve_state_size (vcpu));
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 12/30 KVM: arm64: Prevent kmemleak from accessing .hyp.data
In reply to Fuad TabbaFrom: Quentin Perret <[email protected]>
We've added a .data section for the hypervisor, which kmemleak is eager to parse. This clearly doesn't go well, so add the section to kmemleak's block list.
--- arch/arm64/kvm/pkvm.c | 1 + 1 file changed, 1 insertion(+)
arch/arm64/kvm/pkvm.c +1 -0
@@ -258,6 +258,7 @@ static int __init finalize_pkvm(void)* at, which would end badly once inaccessible. */ kmemleak_free_part (__hyp_bss_start, __hyp_bss_end - __hyp_bss_start); +kmemleak_free_part (__hyp_rodata_start, __hyp_rodata_end - __hyp_rodata_start);kmemleak_free_part_phys (hyp_mem_base, hyp_mem_size); ret =pkvm_drop_host_privileges ();
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 13/30 KVM: arm64: Fix comment for __pkvm_vcpu_init_traps()
In reply to Fuad TabbaFix the comment to clarify that __pkvm_vcpu_init_traps() initializes traps for all VMs in protected mode, and not only for protected VMs.
--- arch/arm64/kvm/hyp/nvhe/pkvm.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/arm64/kvm/hyp/nvhe/pkvm.c +1 -1
@@ -200,7 +200,7 @@ static void pvm_init_trap_regs(struct kvm_vcpu *vcpu) }/* -* Initialize trap register values for protected VMs. +* Initialize trap register values in protected mode. */ void __pkvm_vcpu_init_traps (struct kvm_vcpu *vcpu) {
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 14/30 KVM: arm64: Change kvm_handle_mmio_return() return polarity
In reply to Fuad TabbaMost exit handlers return <= 0 to indicate that the host needs to handle the exit. Make kvm_handle_mmio_return() consistent with the exit handlers in handle_exit(). This makes the code easier to reason about, and makes it easier to add other handlers in future patches.
No functional change intended.
--- arch/arm64/kvm/arm.c | 2 +- arch/arm64/kvm/mmio.c | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-)
arch/arm64/kvm/arm.c +1 -1
@@ -1047,7 +1047,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)if (run->exit_reason ==KVM_EXIT_MMIO ) { ret =kvm_handle_mmio_return (vcpu); -if (ret) +if (ret <=0 )return ret; }
arch/arm64/kvm/mmio.c +2 -2
@@ -86,7 +86,7 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)/* Detect an already handled MMIO return */ if (unlikely (!vcpu->mmio_needed)) -return 0 ; +return 1 ; vcpu->mmio_needed =0 ;
@@ -117,7 +117,7 @@ int kvm_handle_mmio_return(struct kvm_vcpu *vcpu)*/ kvm_incr_pc (vcpu); -return 0 ; +return 1 ; }int io_mem_abort (struct kvm_vcpu *vcpu,phys_addr_t fault_ipa)
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 15/30 KVM: arm64: Move setting the page as dirty out of the critical section
In reply to Fuad TabbaMove the unlock earlier in user_mem_abort() to shorten the critical section. This also helps for future refactoring and reuse of similar code.
This moves out marking the page as dirty outside of the critical section. That code does not interact with the stage-2 page tables, which the read lock in the critical section protects.
--- arch/arm64/kvm/mmu.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-)
arch/arm64/kvm/mmu.c +5 -3
@@ -1522,8 +1522,10 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,read_lock (&kvm->mmu_lock); pgt = vcpu->arch.hw_mmu->pgt; -if (mmu_invalidate_retry (kvm, mmu_seq)) +if (mmu_invalidate_retry (kvm, mmu_seq)) { + ret = -EAGAIN ; goto out_unlock; + }/* * If we are not forced to use page mapping, check if we are
@@ -1581,6 +1583,8 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa, memcache,KVM_PGTABLE_WALK_HANDLE_FAULT | KVM_PGTABLE_WALK_SHARED); +out_unlock: +read_unlock (&kvm->mmu_lock);/* Mark the page dirty only if the fault is handled successfully */ if (writable && !ret) {
@@ -1588,8 +1592,6 @@ static int user_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa,mark_page_dirty_in_slot (kvm, memslot, gfn); } -out_unlock: -read_unlock (&kvm->mmu_lock);kvm_release_pfn_clean (pfn);return ret != -EAGAIN ? ret :0 ; }
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 16/30 KVM: arm64: Simplify vgic-v3 hypercalls
In reply to Fuad TabbaFrom: Marc Zyngier <[email protected]>
Consolidate the GICv3 VMCR accessor hypercalls into the APR save/restore hypercalls so that all of the EL2 GICv3 state is covered by a single pair of hypercalls.
--- arch/arm64/include/asm/kvm_asm.h | 8 ++------ arch/arm64/include/asm/kvm_hyp.h | 4 ++-- arch/arm64/kvm/arm.c | 5 ++--- arch/arm64/kvm/hyp/nvhe/hyp-main.c | 24 ++++++------------------ arch/arm64/kvm/hyp/vgic-v3-sr.c | 27 +++++++++++++++++++++++---- arch/arm64/kvm/vgic/vgic-v2.c | 9 +-------- arch/arm64/kvm/vgic/vgic-v3.c | 23 ++--------------------- arch/arm64/kvm/vgic/vgic.c | 11 ----------- arch/arm64/kvm/vgic/vgic.h | 2 -- include/kvm/arm_vgic.h | 1 - 10 files changed, 38 insertions(+), 76 deletions(-)
arch/arm64/include/asm/kvm_asm.h +2 -6
@@ -73,10 +73,8 @@ enum __kvm_host_smccc_func { __KVM_HOST_SMCCC_FUNC___kvm_tlb_flush_vmid_range, __KVM_HOST_SMCCC_FUNC___kvm_flush_cpu_context, __KVM_HOST_SMCCC_FUNC___kvm_timer_set_cntvoff, - __KVM_HOST_SMCCC_FUNC___vgic_v3_read_vmcr, - __KVM_HOST_SMCCC_FUNC___vgic_v3_write_vmcr, - __KVM_HOST_SMCCC_FUNC___vgic_v3_save_aprs, - __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_aprs, + __KVM_HOST_SMCCC_FUNC___vgic_v3_save_vmcr_aprs, + __KVM_HOST_SMCCC_FUNC___vgic_v3_restore_vmcr_aprs, __KVM_HOST_SMCCC_FUNC___pkvm_vcpu_init_traps, __KVM_HOST_SMCCC_FUNC___pkvm_init_vm, __KVM_HOST_SMCCC_FUNC___pkvm_init_vcpu,
@@ -241,8 +239,6 @@ extern int __kvm_vcpu_run(struct kvm_vcpu *vcpu);extern void __kvm_adjust_pc (struct kvm_vcpu *vcpu);extern u64 __vgic_v3_get_gic_config (void ); -extern u64 __vgic_v3_read_vmcr (void ); -extern void __vgic_v3_write_vmcr (u32 vmcr);extern void __vgic_v3_init_lrs (void );extern u64 __kvm_get_mdcr_el2 (void );
arch/arm64/include/asm/kvm_hyp.h +2 -2
@@ -80,8 +80,8 @@ void __vgic_v3_save_state(struct vgic_v3_cpu_if *cpu_if);void __vgic_v3_restore_state (struct vgic_v3_cpu_if *cpu_if);void __vgic_v3_activate_traps (struct vgic_v3_cpu_if *cpu_if);void __vgic_v3_deactivate_traps (struct vgic_v3_cpu_if *cpu_if); -void __vgic_v3_save_aprs (struct vgic_v3_cpu_if *cpu_if); -void __vgic_v3_restore_aprs (struct vgic_v3_cpu_if *cpu_if); +void __vgic_v3_save_vmcr_aprs (struct vgic_v3_cpu_if *cpu_if); +void __vgic_v3_restore_vmcr_aprs (struct vgic_v3_cpu_if *cpu_if);int __vgic_v3_perform_cpuif_access (struct kvm_vcpu *vcpu);#ifdef __KVM_NVHE_HYPERVISOR__
arch/arm64/kvm/arm.c +2 -3
@@ -857,9 +857,8 @@ void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)* doorbells to be signalled, should an interrupt become pending. */ preempt_disable (); -kvm_vgic_vmcr_sync (vcpu);vcpu_set_flag (vcpu,IN_WFI ); -vgic_v4_put (vcpu); +kvm_vgic_put (vcpu);preempt_enable ();kvm_vcpu_halt (vcpu);
@@ -867,7 +866,7 @@ void kvm_vcpu_wfi(struct kvm_vcpu *vcpu)preempt_disable ();vcpu_clear_flag (vcpu,IN_WFI ); -vgic_v4_load (vcpu); +kvm_vgic_load (vcpu);preempt_enable (); }
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6 -18
@@ -175,16 +175,6 @@ static void handle___vgic_v3_get_gic_config(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) =__vgic_v3_get_gic_config (); } -static void handle___vgic_v3_read_vmcr (struct kvm_cpu_context *host_ctxt) -{ -cpu_reg (host_ctxt,1 ) =__vgic_v3_read_vmcr (); -} - -static void handle___vgic_v3_write_vmcr (struct kvm_cpu_context *host_ctxt) -{ -__vgic_v3_write_vmcr (cpu_reg (host_ctxt,1 )); -} -static void handle___vgic_v3_init_lrs (struct kvm_cpu_context *host_ctxt) {__vgic_v3_init_lrs ();
@@ -195,18 +185,18 @@ static void handle___kvm_get_mdcr_el2(struct kvm_cpu_context *host_ctxt)cpu_reg (host_ctxt,1 ) =__kvm_get_mdcr_el2 (); } -static void handle___vgic_v3_save_aprs (struct kvm_cpu_context *host_ctxt) +static void handle___vgic_v3_save_vmcr_aprs (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1); -__vgic_v3_save_aprs (kern_hyp_va (cpu_if)); +__vgic_v3_save_vmcr_aprs (kern_hyp_va (cpu_if)); } -static void handle___vgic_v3_restore_aprs (struct kvm_cpu_context *host_ctxt) +static void handle___vgic_v3_restore_vmcr_aprs (struct kvm_cpu_context *host_ctxt) { DECLARE_REG(struct vgic_v3_cpu_if *, cpu_if, host_ctxt, 1); -__vgic_v3_restore_aprs (kern_hyp_va (cpu_if)); +__vgic_v3_restore_vmcr_aprs (kern_hyp_va (cpu_if)); }static void handle___pkvm_init (struct kvm_cpu_context *host_ctxt)
@@ -337,10 +327,8 @@ static const hcall_t host_hcall[] = {HANDLE_FUNC (__kvm_tlb_flush_vmid_range),HANDLE_FUNC (__kvm_flush_cpu_context),HANDLE_FUNC (__kvm_timer_set_cntvoff), -HANDLE_FUNC (__vgic_v3_read_vmcr), -HANDLE_FUNC (__vgic_v3_write_vmcr), -HANDLE_FUNC (__vgic_v3_save_aprs), -HANDLE_FUNC (__vgic_v3_restore_aprs), +HANDLE_FUNC (__vgic_v3_save_vmcr_aprs), +HANDLE_FUNC (__vgic_v3_restore_vmcr_aprs),HANDLE_FUNC (__pkvm_vcpu_init_traps),HANDLE_FUNC (__pkvm_init_vm), HANDLE_FUNC(__pkvm_init_vcpu),
arch/arm64/kvm/hyp/vgic-v3-sr.c +23 -4
@@ -330,7 +330,7 @@ void __vgic_v3_deactivate_traps(struct vgic_v3_cpu_if *cpu_if)write_gicreg (0 ,ICH_HCR_EL2 ); } -void __vgic_v3_save_aprs (struct vgic_v3_cpu_if *cpu_if) +static void __vgic_v3_save_aprs (struct vgic_v3_cpu_if *cpu_if) {u64 val;u32 nr_pre_bits;
@@ -363,7 +363,7 @@ void __vgic_v3_save_aprs(struct vgic_v3_cpu_if *cpu_if) } } -void __vgic_v3_restore_aprs (struct vgic_v3_cpu_if *cpu_if) +static void __vgic_v3_restore_aprs (struct vgic_v3_cpu_if *cpu_if) {u64 val;u32 nr_pre_bits;
@@ -455,16 +455,35 @@ u64 __vgic_v3_get_gic_config(void)return val; } -u64 __vgic_v3_read_vmcr (void ) +static u64 __vgic_v3_read_vmcr (void ) {return read_gicreg (ICH_VMCR_EL2 ); } -void __vgic_v3_write_vmcr (u32 vmcr) +static void __vgic_v3_write_vmcr (u32 vmcr) {write_gicreg (vmcr,ICH_VMCR_EL2 ); } +void __vgic_v3_save_vmcr_aprs (struct vgic_v3_cpu_if *cpu_if) +{ +__vgic_v3_save_aprs (cpu_if); +if (cpu_if->vgic_sre) + cpu_if->vgic_vmcr =__vgic_v3_read_vmcr (); +} + +void __vgic_v3_restore_vmcr_aprs (struct vgic_v3_cpu_if *cpu_if) +{ +/* +* If dealing with a GICv2 emulation on GICv3, VMCR_EL2.VFIQen +* is dependent on ICC_SRE_EL1.SRE, and we have to perform the +* VMCR_EL2 save/restore in the world switch. +*/ +if (cpu_if->vgic_sre) +__vgic_v3_write_vmcr (cpu_if->vgic_vmcr); +__vgic_v3_restore_aprs (cpu_if); +} +static int __vgic_v3_bpr_min (void ) {/* See Pseudocode for VPriorityGroup */
arch/arm64/kvm/vgic/vgic-v2.c +1 -8
@@ -464,17 +464,10 @@ void vgic_v2_load(struct kvm_vcpu *vcpu) kvm_vgic_global_state.vctrl_base + GICH_APR); } -void vgic_v2_vmcr_sync (struct kvm_vcpu *vcpu) -{ -struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; - - cpu_if->vgic_vmcr =readl_relaxed (kvm_vgic_global_state.vctrl_base +GICH_VMCR ); -} -void vgic_v2_put (struct kvm_vcpu *vcpu) {struct vgic_v2_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v2; -vgic_v2_vmcr_sync (vcpu); + cpu_if->vgic_vmcr =readl_relaxed (kvm_vgic_global_state.vctrl_base +GICH_VMCR ); cpu_if->vgic_apr =readl_relaxed (kvm_vgic_global_state.vctrl_base +GICH_APR ); }
arch/arm64/kvm/vgic/vgic-v3.c +2 -21
@@ -722,15 +722,7 @@ void vgic_v3_load(struct kvm_vcpu *vcpu) {struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; -/* -* If dealing with a GICv2 emulation on GICv3, VMCR_EL2.VFIQen -* is dependent on ICC_SRE_EL1.SRE, and we have to perform the -* VMCR_EL2 save/restore in the world switch. -*/ -if (likely (cpu_if->vgic_sre)) -kvm_call_hyp (__vgic_v3_write_vmcr, cpu_if->vgic_vmcr); - -kvm_call_hyp (__vgic_v3_restore_aprs, cpu_if); +kvm_call_hyp (__vgic_v3_restore_vmcr_aprs, cpu_if);if (has_vhe ())__vgic_v3_activate_traps (cpu_if);
@@ -738,24 +730,13 @@ void vgic_v3_load(struct kvm_vcpu *vcpu)WARN_ON (vgic_v4_load (vcpu)); } -void vgic_v3_vmcr_sync (struct kvm_vcpu *vcpu) -{ -struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; - -if (likely (cpu_if->vgic_sre)) - cpu_if->vgic_vmcr =kvm_call_hyp_ret (__vgic_v3_read_vmcr); -} -void vgic_v3_put (struct kvm_vcpu *vcpu) {struct vgic_v3_cpu_if *cpu_if = &vcpu->arch.vgic_cpu.vgic_v3; +kvm_call_hyp (__vgic_v3_save_vmcr_aprs, cpu_if);WARN_ON (vgic_v4_put (vcpu)); -vgic_v3_vmcr_sync (vcpu); - -kvm_call_hyp (__vgic_v3_save_aprs, cpu_if); -if (has_vhe ())__vgic_v3_deactivate_traps (cpu_if); }
arch/arm64/kvm/vgic/vgic.c +0 -11
@@ -939,17 +939,6 @@ void kvm_vgic_put(struct kvm_vcpu *vcpu)vgic_v3_put (vcpu); } -void kvm_vgic_vmcr_sync (struct kvm_vcpu *vcpu) -{ -if (unlikely (!irqchip_in_kernel (vcpu->kvm))) -return ; - -if (kvm_vgic_global_state.type ==VGIC_V2 ) -vgic_v2_vmcr_sync (vcpu); -else -vgic_v3_vmcr_sync (vcpu); -} -int kvm_vgic_vcpu_pending_irq (struct kvm_vcpu *vcpu) {struct vgic_cpu *vgic_cpu = &vcpu->arch.vgic_cpu;
arch/arm64/kvm/vgic/vgic.h +0 -2
@@ -214,7 +214,6 @@ int vgic_register_dist_iodev(struct kvm *kvm, gpa_t dist_base_address,void vgic_v2_init_lrs (void );void vgic_v2_load (struct kvm_vcpu *vcpu);void vgic_v2_put (struct kvm_vcpu *vcpu); -void vgic_v2_vmcr_sync (struct kvm_vcpu *vcpu);void vgic_v2_save_state (struct kvm_vcpu *vcpu);void vgic_v2_restore_state (struct kvm_vcpu *vcpu);
@@ -253,7 +252,6 @@ bool vgic_v3_check_base(struct kvm *kvm);void vgic_v3_load (struct kvm_vcpu *vcpu);void vgic_v3_put (struct kvm_vcpu *vcpu); -void vgic_v3_vmcr_sync (struct kvm_vcpu *vcpu);bool vgic_has_its (struct kvm *kvm);int kvm_vgic_register_its_device (void );
include/kvm/arm_vgic.h +0 -1
@@ -388,7 +388,6 @@ int kvm_vgic_vcpu_pending_irq(struct kvm_vcpu *vcpu);void kvm_vgic_load (struct kvm_vcpu *vcpu);void kvm_vgic_put (struct kvm_vcpu *vcpu); -void kvm_vgic_vmcr_sync (struct kvm_vcpu *vcpu);#define irqchip_in_kernel (k) (!!((k)->arch.vgic.in_kernel))#define vgic_initialized (k) ((k)->arch.vgic.initialized)
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 17/30 KVM: arm64: Add is_pkvm_initialized() helper
In reply to Fuad TabbaFrom: Quentin Perret <[email protected]>
Add a helper allowing to check when the pkvm static key is enabled to ease the introduction of pkvm hooks in other parts of the code.
--- arch/arm64/include/asm/virt.h | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-)
arch/arm64/include/asm/virt.h +8 -4
@@ -82,6 +82,12 @@ bool is_kvm_arm_initialised(void);DECLARE_STATIC_KEY_FALSE (kvm_protected_mode_initialized); +static inline bool is_pkvm_initialized (void ) +{ +return IS_ENABLED (CONFIG_KVM ) && +static_branch_likely (&kvm_protected_mode_initialized); +} +/* Reports the availability of HYP mode */ static inline bool is_hyp_mode_available (void ) {
@@ -89,8 +95,7 @@ static inline bool is_hyp_mode_available(void)* If KVM protected mode is initialized, all CPUs must have been booted * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. */ -if (IS_ENABLED (CONFIG_KVM ) && -static_branch_likely (&kvm_protected_mode_initialized)) +if (is_pkvm_initialized ())return true; return (__boot_cpu_mode[0] == BOOT_CPU_MODE_EL2 &&
@@ -104,8 +109,7 @@ static inline bool is_hyp_mode_mismatched(void)* If KVM protected mode is initialized, all CPUs must have been booted * in EL2. Avoid checking __boot_cpu_mode as CPUs now come up in EL1. */ -if (IS_ENABLED (CONFIG_KVM ) && -static_branch_likely (&kvm_protected_mode_initialized)) +if (is_pkvm_initialized ())return false;return __boot_cpu_mode[0 ] != __boot_cpu_mode[1 ];
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 18/30 KVM: arm64: Introduce and use predicates that check for protected VMs
In reply to Fuad TabbaIn order to determine whether or not a VM or vcpu are protected, introduce helpers to query this state. While at it, use the vcpu helper to check vcpus protected state instead of the kvm one.
--- arch/arm64/include/asm/kvm_host.h | 8 ++++---- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 5 +++++ arch/arm64/kvm/hyp/nvhe/switch.c | 6 ++---- 3 files changed, 11 insertions(+), 8 deletions(-)
arch/arm64/include/asm/kvm_host.h +4 -4
@@ -211,6 +211,7 @@ typedef unsigned int pkvm_handle_t;struct kvm_protected_vm {pkvm_handle_t handle;struct kvm_hyp_memcache teardown_mc; +bool enabled; };struct kvm_mpidr_data {
@@ -1300,10 +1301,9 @@ struct kvm *kvm_arch_alloc_vm(void);#define __KVM_HAVE_ARCH_FLUSH_REMOTE_TLBS_RANGE -static inline bool kvm_vm_is_protected (struct kvm *kvm) -{ -return false; -} +#define kvm_vm_is_protected (kvm) (is_protected_kvm_enabled() && (kvm)->arch.pkvm.enabled) + +#define vcpu_is_protected (vcpu) kvm_vm_is_protected((vcpu)->kvm)int kvm_arm_vcpu_finalize (struct kvm_vcpu *vcpu,int feature);bool kvm_arm_vcpu_is_finalized (struct kvm_vcpu *vcpu);
arch/arm64/kvm/hyp/include/nvhe/pkvm.h +5 -0
@@ -53,6 +53,11 @@ pkvm_hyp_vcpu_to_hyp_vm(struct pkvm_hyp_vcpu *hyp_vcpu) return container_of(hyp_vcpu->vcpu.kvm, struct pkvm_hyp_vm, kvm); } +static inline bool pkvm_hyp_vcpu_is_protected (struct pkvm_hyp_vcpu *hyp_vcpu) +{ +return vcpu_is_protected (&hyp_vcpu->vcpu); +} +void pkvm_hyp_vm_table_init (void *tbl);void pkvm_host_fpsimd_state_init (void );
arch/arm64/kvm/hyp/nvhe/switch.c +2 -4
@@ -207,7 +207,7 @@ static const exit_handler_fn pvm_exit_handlers[] = {static const exit_handler_fn *kvm_get_exit_handler_array (struct kvm_vcpu *vcpu) { -if (unlikely (kvm_vm_is_protected (kern_hyp_va (vcpu->kvm)))) +if (unlikely (vcpu_is_protected (vcpu)))return pvm_exit_handlers;return hyp_exit_handlers;
@@ -226,9 +226,7 @@ static const exit_handler_fn *kvm_get_exit_handler_array(struct kvm_vcpu *vcpu)*/ static void early_exit_filter (struct kvm_vcpu *vcpu,u64 *exit_code) { -struct kvm *kvm =kern_hyp_va (vcpu->kvm); - -if (kvm_vm_is_protected (kvm) &&vcpu_mode_is_32bit (vcpu)) { +if (unlikely (vcpu_is_protected (vcpu) &&vcpu_mode_is_32bit (vcpu))) {/* * As we have caught the guest red-handed, decide that it isn't * fit for purpose anymore by making the vcpu invalid. The VMM
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 19/30 KVM: arm64: Move pstate reset value definitions to kvm_arm.h
In reply to Fuad TabbaMove the macro defines of the pstate reset values to a shared header to be used by hyp in future patches.
No functional change intended.
--- arch/arm64/include/asm/kvm_arm.h | 12 ++++++++++++ arch/arm64/kvm/reset.c | 12 ------------ 2 files changed, 12 insertions(+), 12 deletions(-)
arch/arm64/include/asm/kvm_arm.h +12 -0
@@ -432,4 +432,16 @@ { PSR_AA32_MODE_UND, "32-bit UND" }, \ { PSR_AA32_MODE_SYS, "32-bit SYS" } +/* +* ARMv8 Reset Values +*/ +#define VCPU_RESET_PSTATE_EL1 (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | \ + PSR_F_BIT | PSR_D_BIT) + +#define VCPU_RESET_PSTATE_EL2 (PSR_MODE_EL2h | PSR_A_BIT | PSR_I_BIT | \ + PSR_F_BIT | PSR_D_BIT) + +#define VCPU_RESET_PSTATE_SVC (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \ + PSR_AA32_I_BIT | PSR_AA32_F_BIT) +#endif /* __ARM64_KVM_ARM_H__ */
arch/arm64/kvm/reset.c +0 -12
@@ -33,18 +33,6 @@/* Maximum phys_shift supported for any VM on this host */ static u32 __ro_after_init kvm_ipa_limit; -/* -* ARMv8 Reset Values -*/ -#define VCPU_RESET_PSTATE_EL1 (PSR_MODE_EL1h | PSR_A_BIT | PSR_I_BIT | \ - PSR_F_BIT | PSR_D_BIT) - -#define VCPU_RESET_PSTATE_EL2 (PSR_MODE_EL2h | PSR_A_BIT | PSR_I_BIT | \ - PSR_F_BIT | PSR_D_BIT) - -#define VCPU_RESET_PSTATE_SVC (PSR_AA32_MODE_SVC | PSR_AA32_A_BIT | \ - PSR_AA32_I_BIT | PSR_AA32_F_BIT) -unsigned int __ro_after_init kvm_sve_max_vl;int __init kvm_arm_init_sve (void )
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 20/30 KVM: arm64: Clarify rationale for ZCR_EL1 value restored on guest exit
In reply to Fuad TabbaExpand comment clarifying why the host value representing SVE vector length being restored for ZCR_EL1 on guest exit isn't the same as it was on guest entry.
--- arch/arm64/kvm/fpsimd.c | 24 +++++++++++++++++++++++- 1 file changed, 23 insertions(+), 1 deletion(-)
arch/arm64/kvm/fpsimd.c +23 -1
@@ -175,12 +175,34 @@ void kvm_arch_vcpu_put_fp(struct kvm_vcpu *vcpu)if (vcpu_has_sve (vcpu)) {__vcpu_sys_reg (vcpu,ZCR_EL1 ) =read_sysreg_el1 (SYS_ZCR ); -/* Restore the VL that was saved when bound to the CPU */ +/* +* Restore the VL that was saved when bound to the CPU, +* which is the maximum VL for the guest. Because the +* layout of the data when saving the sve state depends +* on the VL, we need to use a consistent (i.e., the +* maximum) VL. +* Note that this means that at guest exit ZCR_EL1 is +* not necessarily the same as on guest entry. +* +* Restoring the VL isn't needed in VHE mode since +* ZCR_EL2 (accessed via ZCR_EL1) would fulfill the same +* role when doing the save from EL2. +*/ if (!has_vhe ())sve_cond_update_zcr_vq (vcpu_sve_max_vq (vcpu) -1 ,SYS_ZCR_EL1 ); } +/* +* Flush (save and invalidate) the fpsimd/sve state so that if +* the host tries to use fpsimd/sve, it's not using stale data +* from the guest. +* +* Flushing the state sets the TIF_FOREIGN_FPSTATE bit for the +* context unconditionally, in both nVHE and VHE. This allows +* the kernel to restore the fpsimd/sve state, including ZCR_EL1 +* when needed. +*/ fpsimd_save_and_flush_cpu_state (); }else if (has_vhe () &&system_supports_sve ()) {/*
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 21/30 KVM: arm64: Refactor calculating SVE state size to use helpers
In reply to Fuad TabbaThe main factor for determining the SVE state size is the vector length, and future patches will need to calculate it without necessarily having a vcpu as a reference.
No functional change intended.
--- arch/arm64/include/asm/kvm_host.h | 16 +++++++++------- 1 file changed, 9 insertions(+), 7 deletions(-)
arch/arm64/include/asm/kvm_host.h +9 -7
@@ -852,22 +852,24 @@ struct kvm_vcpu_arch {#define vcpu_sve_pffr (vcpu) (kern_hyp_va((vcpu)->arch.sve_state) + \ sve_ffr_offset((vcpu)->arch.sve_max_vl)) -#define vcpu_sve_max_vq (vcpu) sve_vq_from_vl((vcpu)->arch.sve_max_vl) - -#define vcpu_sve_state_size (vcpu) ({ \ +#define sve_state_size (sve_max_vl) ({ \ size_t __size_ret; \ - unsigned int __vcpu_vq; \ + unsigned int __vq; \ \ - if (WARN_ON(!sve_vl_valid((vcpu)->arch.sve_max_vl))) { \ + if (WARN_ON(!sve_vl_valid(sve_max_vl))) { \ __size_ret = 0; \ } else { \ - __vcpu_vq = vcpu_sve_max_vq(vcpu); \ - __size_ret = SVE_SIG_REGS_SIZE(__vcpu_vq); \ + __vq = sve_vq_from_vl(sve_max_vl); \ + __size_ret = SVE_SIG_REGS_SIZE(__vq); \ } \ \ __size_ret; \ }) +#define vcpu_sve_max_vq (vcpu) sve_vq_from_vl((vcpu)->arch.sve_max_vl) + +#define vcpu_sve_state_size (vcpu) sve_state_size((vcpu)->arch.sve_max_vl) +#define KVM_GUESTDBG_VALID_MASK (KVM_GUESTDBG_ENABLE | \ KVM_GUESTDBG_USE_SW_BP | \ KVM_GUESTDBG_USE_HW | \
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 22/30 KVM: arm64: Move some kvm_psci functions to a shared header
In reply to Fuad TabbaMove some PSCI functions and macros to a shared header to be used by hyp in protected mode.
No functional change intended.
--- arch/arm64/kvm/psci.c | 28 ---------------------------- include/kvm/arm_psci.h | 29 +++++++++++++++++++++++++++++ 2 files changed, 29 insertions(+), 28 deletions(-)
arch/arm64/kvm/psci.c +0 -28
@@ -21,16 +21,6 @@* as described in ARM document number ARM DEN 0022A. */ -#define AFFINITY_MASK (level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1) - -static unsigned long psci_affinity_mask (unsigned long affinity_level) -{ -if (affinity_level <=3 ) -return MPIDR_HWID_BITMASK &AFFINITY_MASK (affinity_level); - -return 0 ; -} -static unsigned long kvm_psci_vcpu_suspend (struct kvm_vcpu *vcpu) {/*
@@ -51,12 +41,6 @@ static unsigned long kvm_psci_vcpu_suspend(struct kvm_vcpu *vcpu)return PSCI_RET_SUCCESS ; } -static inline bool kvm_psci_valid_affinity (struct kvm_vcpu *vcpu, -unsigned long affinity) -{ -return !(affinity & ~MPIDR_HWID_BITMASK ); -} -static unsigned long kvm_psci_vcpu_on (struct kvm_vcpu *source_vcpu) {struct vcpu_reset_state *reset_state;
@@ -214,18 +198,6 @@ static void kvm_psci_system_suspend(struct kvm_vcpu *vcpu) run->exit_reason =KVM_EXIT_SYSTEM_EVENT ; } -static void kvm_psci_narrow_to_32bit (struct kvm_vcpu *vcpu) -{ -int i; - -/* -* Zero the input registers' upper 32 bits. They will be fully -* zeroed on exit, so we're fine changing them in place. -*/ -for (i =1 ; i <4 ; i++) -vcpu_set_reg (vcpu, i,lower_32_bits (vcpu_get_reg (vcpu, i))); -} -static unsigned long kvm_psci_check_allowed_function (struct kvm_vcpu *vcpu,u32 fn) {/*
include/kvm/arm_psci.h +29 -0
@@ -36,6 +36,35 @@ static inline int kvm_psci_version(struct kvm_vcpu *vcpu)return KVM_ARM_PSCI_0_1 ; } +/* Narrow the PSCI register arguments (r1 to r3) to 32 bits. */ +static inline void kvm_psci_narrow_to_32bit (struct kvm_vcpu *vcpu) +{ +int i; + +/* +* Zero the input registers' upper 32 bits. They will be fully +* zeroed on exit, so we're fine changing them in place. +*/ +for (i =1 ; i <4 ; i++) +vcpu_set_reg (vcpu, i,lower_32_bits (vcpu_get_reg (vcpu, i))); +} + +static inline bool kvm_psci_valid_affinity (struct kvm_vcpu *vcpu, +unsigned long affinity) +{ +return !(affinity & ~MPIDR_HWID_BITMASK ); +} + + +#define AFFINITY_MASK (level) ~((0x1UL << ((level) * MPIDR_LEVEL_BITS)) - 1) + +static inline unsigned long psci_affinity_mask (unsigned long affinity_level) +{ +if (affinity_level <=3 ) +return MPIDR_HWID_BITMASK &AFFINITY_MASK (affinity_level); + +return 0 ; +}int kvm_psci_call (struct kvm_vcpu *vcpu);
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 23/30 KVM: arm64: Refactor reset_mpidr() to extract its computation
In reply to Fuad TabbaMove the computation of the mpidr to its own function in a shared header, as the computation will be used by hyp in protected mode.
No functional change intended.
--- arch/arm64/kvm/sys_regs.c | 14 +------------- arch/arm64/kvm/sys_regs.h | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 13 deletions(-)
arch/arm64/kvm/sys_regs.c +1 -13
@@ -816,21 +816,9 @@ static u64 reset_actlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)static u64 reset_mpidr (struct kvm_vcpu *vcpu,const struct sys_reg_desc *r) { -u64 mpidr; +u64 mpidr =calculate_mpidr (vcpu); -/* -* Map the vcpu_id into the first three affinity level fields of -* the MPIDR. We limit the number of VCPUs in level 0 due to a -* limitation to 16 CPUs in that level in the ICC_SGIxR registers -* of the GICv3 to be able to address each CPU directly when -* sending IPIs. -*/ - mpidr = (vcpu->vcpu_id &0x0f ) <<MPIDR_LEVEL_SHIFT (0 ); - mpidr |= ((vcpu->vcpu_id >>4 ) &0xff ) <<MPIDR_LEVEL_SHIFT (1 ); - mpidr |= ((vcpu->vcpu_id >>12 ) &0xff ) <<MPIDR_LEVEL_SHIFT (2 ); - mpidr |= (1ULL <<31 );vcpu_write_sys_reg (vcpu, mpidr,MPIDR_EL1 ); -return mpidr; }
arch/arm64/kvm/sys_regs.h +19 -0
@@ -222,6 +222,25 @@ find_reg(const struct sys_reg_params *params, const struct sys_reg_desc table[],return __inline_bsearch ((void *)pval, table, num,sizeof (table[0 ]), match_sys_reg); } +static inline u64 calculate_mpidr (const struct kvm_vcpu *vcpu) +{ +u64 mpidr; + +/* +* Map the vcpu_id into the first three affinity level fields of +* the MPIDR. We limit the number of VCPUs in level 0 due to a +* limitation to 16 CPUs in that level in the ICC_SGIxR registers +* of the GICv3 to be able to address each CPU directly when +* sending IPIs. +*/ + mpidr = (vcpu->vcpu_id &0x0f ) <<MPIDR_LEVEL_SHIFT (0 ); + mpidr |= ((vcpu->vcpu_id >>4 ) &0xff ) <<MPIDR_LEVEL_SHIFT (1 ); + mpidr |= ((vcpu->vcpu_id >>12 ) &0xff ) <<MPIDR_LEVEL_SHIFT (2 ); + mpidr |= (1ULL <<31 ); + +return mpidr; +} +const struct sys_reg_desc *get_reg_by_id (u64 id,const struct sys_reg_desc table[],unsigned int num);
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 24/30 KVM: arm64: Refactor kvm_vcpu_enable_ptrauth() for hyp use
In reply to Fuad TabbaMove kvm_vcpu_enable_ptrauth() to a shared header to be used by hyp in protected mode.
No functional change intended.
--- arch/arm64/include/asm/kvm_emulate.h | 5 +++++ arch/arm64/kvm/reset.c | 7 +------ 2 files changed, 6 insertions(+), 6 deletions(-)
arch/arm64/include/asm/kvm_emulate.h +5 -0
@@ -599,4 +599,9 @@ static __always_inline void kvm_reset_cptr_el2(struct kvm_vcpu *vcpu) kvm_write_cptr_el2(val); } + +static inline void kvm_vcpu_enable_ptrauth (struct kvm_vcpu *vcpu) +{ +vcpu_set_flag (vcpu,GUEST_HAS_PTRAUTH ); +}#endif /* __ARM64_KVM_EMULATE_H__ */
arch/arm64/kvm/reset.c +1 -6
@@ -105,7 +105,7 @@ static int kvm_vcpu_finalize_sve(struct kvm_vcpu *vcpu)kfree (buf);return ret; } - + vcpu->arch.sve_state = buf;vcpu_set_flag (vcpu,VCPU_SVE_FINALIZED );return 0 ;
@@ -152,11 +152,6 @@ static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)memset (vcpu->arch.sve_state,0 ,vcpu_sve_state_size (vcpu)); } -static void kvm_vcpu_enable_ptrauth (struct kvm_vcpu *vcpu) -{ -vcpu_set_flag (vcpu,GUEST_HAS_PTRAUTH ); -} -/** * kvm_reset_vcpu - sets core registers and sys_regs to reset value * @vcpu: The VCPU pointer
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 25/30 KVM: arm64: Reformat/beautify PTP hypercall documentation
In reply to Fuad TabbaFrom: Will Deacon <[email protected]>
The PTP hypercall documentation doesn't produce the best-looking table when formatting in HTML as all of the return value definitions end up on the same line.
Reformat the PTP hypercall documentation to follow the formatting used by hypercalls.rst.
--- Documentation/virt/kvm/arm/ptp_kvm.rst | 38 ++++++++++++++++---------- 1 file changed, 24 insertions(+), 14 deletions(-)
Documentation/virt/kvm/arm/ptp_kvm.rst +24 -14
@@ -7,19 +7,29 @@ PTP_KVM is used for high precision time sync between host and guests. It relies on transferring the wall clock and counter value from the host to the guest using a KVM-specific hypercall. -* ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID: 0x86000001 +``ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID`` +---------------------------------------- -This hypercall uses the SMC32/HVC32 calling convention: +Retrieve current time information for the specific counter. There are no +endianness restrictions. -ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID - ============== ======== ===================================== - Function ID: (uint32) 0x86000001 - Arguments: (uint32) KVM_PTP_VIRT_COUNTER(0) - KVM_PTP_PHYS_COUNTER(1) - Return Values: (int32) NOT_SUPPORTED(-1) on error, or - (uint32) Upper 32 bits of wall clock time (r0) - (uint32) Lower 32 bits of wall clock time (r1) - (uint32) Upper 32 bits of counter (r2) - (uint32) Lower 32 bits of counter (r3) - Endianness: No Restrictions. - ============== ======== ===================================== ++---------------------+-------------------------------------------------------+ +| Presence: | Optional | ++---------------------+-------------------------------------------------------+ +| Calling convention: | HVC32 | ++---------------------+----------+--------------------------------------------+ +| Function ID: | (uint32) | 0x86000001 | ++---------------------+----------+----+---------------------------------------+ +| Arguments: | (uint32) | R1 | ``KVM_PTP_VIRT_COUNTER (0)`` | +| | | +---------------------------------------+ +| | | | ``KVM_PTP_PHYS_COUNTER (1)`` | ++---------------------+----------+----+---------------------------------------+ +| Return Values: | (int32) | R0 | ``NOT_SUPPORTED (-1)`` on error, else | +| | | | upper 32 bits of wall clock time | +| +----------+----+---------------------------------------+ +| | (uint32) | R1 | Lower 32 bits of wall clock time | +| +----------+----+---------------------------------------+ +| | (uint32) | R2 | Upper 32 bits of counter | +| +----------+----+---------------------------------------+ +| | (uint32) | R3 | Lower 32 bits of counter | ++---------------------+----------+----+---------------------------------------+
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 26/30 KVM: arm64: Rename firmware pseudo-register documentation file
In reply to Fuad TabbaFrom: Will Deacon <[email protected]>
In preparation for describing the guest view of KVM/arm64 hypercalls in hypercalls.rst, move the existing contents of the file concerning the firmware pseudo-registers elsewhere.
---
.../kvm/arm/{hypercalls.rst => fw-pseudo-registers.rst} | 6 +++---
Documentation/virt/kvm/arm/index.rst | 2 +-
2 files changed, 4 insertions(+), 4 deletions(-)
rename Documentation/virt/kvm/arm/{hypercalls.rst => fw-pseudo-registers.rst} (97%)Documentation/virt/kvm/arm/fw-pseudo-registers.rst renamed from Documentation/virt/kvm/arm/hypercalls.rst +3 -3
@@ -1,8 +1,8 @@ .. SPDX-License-Identifier: GPL-2.0 -======================= -ARM Hypercall Interface -======================= +======================================= +ARM firmware pseudo-registers interface +======================================= KVM handles the hypercall services as requested by the guests. New hypercall services are regularly made available by the ARM specification or by KVM (as
Documentation/virt/kvm/arm/index.rst +1 -1
@@ -7,8 +7,8 @@ ARM .. toctree:: :maxdepth: 2 + fw-pseudo-registers hyp-abi - hypercalls pvtime ptp_kvm vcpu-features
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 27/30 KVM: arm64: Document the KVM/arm64-specific calls in hypercalls.rst
In reply to Fuad TabbaFrom: Will Deacon <[email protected]>
KVM/arm64 makes use of the SMCCC "Vendor Specific Hypervisor Service Call Range" to expose KVM-specific hypercalls to guests in a discoverable and extensible fashion.
Document the existence of this interface and the discovery hypercall.
--- Documentation/virt/kvm/arm/hypercalls.rst | 46 +++++++++++++++++++++++ Documentation/virt/kvm/arm/index.rst | 1 + 2 files changed, 47 insertions(+) create mode 100644 Documentation/virt/kvm/arm/hypercalls.rst
Documentation/virt/kvm/arm/hypercalls.rst new file +46 -0
@@ -0,0 +1,46 @@ +.. SPDX-License-Identifier: GPL-2.0 + +=============================================== +KVM/arm64-specific hypercalls exposed to guests +=============================================== + +This file documents the KVM/arm64-specific hypercalls which may be +exposed by KVM/arm64 to guest operating systems. These hypercalls are +issued using the HVC instruction according to version 1.1 of the Arm SMC +Calling Convention (DEN0028/C): + +https://developer.arm.com/docs/den0028/c + +All KVM/arm64-specific hypercalls are allocated within the "Vendor +Specific Hypervisor Service Call" range with a UID of +``28b46fb6-2ec5-11e9-a9ca-4b564d003a74``. This UID should be queried by the +guest using the standard "Call UID" function for the service range in +order to determine that the KVM/arm64-specific hypercalls are available. + +``ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID`` +--------------------------------------------- + +Provides a discovery mechanism for other KVM/arm64 hypercalls. + ++---------------------+-------------------------------------------------------------+ +| Presence: | Mandatory for the KVM/arm64 UID | ++---------------------+-------------------------------------------------------------+ +| Calling convention: | HVC32 | ++---------------------+----------+--------------------------------------------------+ +| Function ID: | (uint32) | 0x86000000 | ++---------------------+----------+--------------------------------------------------+ +| Arguments: | None | ++---------------------+----------+----+---------------------------------------------+ +| Return Values: | (uint32) | R0 | Bitmap of available function numbers 0-31 | +| +----------+----+---------------------------------------------+ +| | (uint32) | R1 | Bitmap of available function numbers 32-63 | +| +----------+----+---------------------------------------------+ +| | (uint32) | R2 | Bitmap of available function numbers 64-95 | +| +----------+----+---------------------------------------------+ +| | (uint32) | R3 | Bitmap of available function numbers 96-127 | ++---------------------+----------+----+---------------------------------------------+ + +``ARM_SMCCC_VENDOR_HYP_KVM_PTP_FUNC_ID`` +---------------------------------------- + +See ptp_kvm.rst
Documentation/virt/kvm/arm/index.rst +1 -0
@@ -9,6 +9,7 @@ ARM fw-pseudo-registers hyp-abi + hypercalls pvtime ptp_kvm vcpu-features
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 28/30 KVM: arm64: Refactor setting the return value in kvm_vm_ioctl_enable_cap()
In reply to Fuad TabbaInitialize r = -EINVAL to get rid of the error-path initializations in kvm_vm_ioctl_enable_cap().
No functional change intended.
--- arch/arm64/kvm/arm.c | 24 +++++++++--------------- 1 file changed, 9 insertions(+), 15 deletions(-)
arch/arm64/kvm/arm.c +9 -15
@@ -73,8 +73,7 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) { -int r; -u64 new_cap; +int r = -EINVAL ;if (cap->flags)return -EINVAL ;
@@ -87,9 +86,7 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,break ;case KVM_CAP_ARM_MTE :mutex_lock (&kvm->lock); -if (!system_supports_mte () || kvm->created_vcpus) { - r = -EINVAL ; - }else { +if (system_supports_mte () && !kvm->created_vcpus) { r =0 ;set_bit (KVM_ARCH_FLAG_MTE_ENABLED , &kvm->arch.flags); }
@@ -100,25 +97,22 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,set_bit (KVM_ARCH_FLAG_SYSTEM_SUSPEND_ENABLED , &kvm->arch.flags);break ;case KVM_CAP_ARM_EAGER_SPLIT_CHUNK_SIZE : - new_cap = cap->args[0 ]; -mutex_lock (&kvm->slots_lock);/* * To keep things simple, allow changing the chunk * size only when no memory slots have been created. */ -if (!kvm_are_all_memslots_empty (kvm)) { - r = -EINVAL ; - }else if (new_cap && !kvm_is_block_size_supported (new_cap)) { - r = -EINVAL ; - }else { - r =0 ; - kvm->arch.mmu.split_page_chunk_size = new_cap; +if (kvm_are_all_memslots_empty (kvm)) { +u64 new_cap = cap->args[0 ]; + +if (!new_cap ||kvm_is_block_size_supported (new_cap)) { + r =0 ; + kvm->arch.mmu.split_page_chunk_size = new_cap; + } }mutex_unlock (&kvm->slots_lock);break ;default : - r = -EINVAL ;break ; }
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 29/30 KVM: arm64: Restrict supported capabilities for protected VMs
In reply to Fuad TabbaFor practical reasons as well as security related ones, not all capabilities are supported for protected VMs in pKVM.
Add a function that restricts the capabilities for protected VMs. This behaves as an allow-list to ensure that future capabilities are checked for compatibility and security before being allowed for protected VMs.
--- arch/arm64/kvm/arm.c | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+)
arch/arm64/kvm/arm.c +32 -0
@@ -70,6 +70,31 @@ int kvm_arch_vcpu_should_kick(struct kvm_vcpu *vcpu)return kvm_vcpu_exiting_guest_mode (vcpu) ==IN_GUEST_MODE ; } +/* +* This functions as an allow-list of protected VM capabilities. +* Features not explicitly allowed by this function are denied. +*/ +static bool pkvm_ext_allowed (struct kvm *kvm,long ext) +{ +switch (ext) { +case KVM_CAP_IRQCHIP : +case KVM_CAP_ARM_PSCI : +case KVM_CAP_ARM_PSCI_0_2 : +case KVM_CAP_NR_VCPUS : +case KVM_CAP_MAX_VCPUS : +case KVM_CAP_MAX_VCPU_ID : +case KVM_CAP_MSI_DEVID : +case KVM_CAP_ARM_VM_IPA_SIZE : +case KVM_CAP_ARM_PMU_V3 : +case KVM_CAP_ARM_SVE : +case KVM_CAP_ARM_PTRAUTH_ADDRESS : +case KVM_CAP_ARM_PTRAUTH_GENERIC : +return true; +default : +return false; + } +} +int kvm_vm_ioctl_enable_cap (struct kvm *kvm,struct kvm_enable_cap *cap) {
@@ -78,6 +103,9 @@ int kvm_vm_ioctl_enable_cap(struct kvm *kvm,if (cap->flags)return -EINVAL ; +if (kvm_vm_is_protected (kvm) && !pkvm_ext_allowed (kvm, cap->cap)) +return -EINVAL ; +switch (cap->cap) {case KVM_CAP_ARM_NISV_TO_USER : r =0 ;
@@ -250,6 +278,10 @@ static bool kvm_has_full_ptr_auth(void)int kvm_vm_ioctl_check_extension (struct kvm *kvm,long ext) {int r; + +if (kvm &&kvm_vm_is_protected (kvm) && !pkvm_ext_allowed (kvm, ext)) +return 0 ; +switch (ext) {case KVM_CAP_IRQCHIP : r = vgic_present;
Signature · 1 line
2.44.0.769.g3c40516874-goog
PATCH v4 30/30 KVM: arm64: Force injection of a data abort on NISV MMIO exit
In reply to Fuad TabbaFrom: Marc Zyngier <[email protected]>
If a vcpu exits for a data abort with an invalid syndrome, the expectations are that userspace has a chance to save the day if it has requested to see such exits.
However, this is completely futile in the case of a protected VM, as none of the state is available. In this particular case, inject a data abort directly into the vcpu, consistent with what userspace could do.
This also helps with pKVM, which discards all syndrome information when forwarding data aborts that are not known to be MMIO.
Finally, document this tweak to the API.
--- Documentation/virt/kvm/api.rst | 7 +++++++ arch/arm64/kvm/mmio.c | 8 ++++++++ 2 files changed, 15 insertions(+)
Documentation/virt/kvm/api.rst +7 -0
@@ -6894,6 +6894,13 @@ Note that KVM does not skip the faulting instruction as it does for KVM_EXIT_MMIO, but userspace has to emulate any change to the processing state if it decides to decode and emulate the instruction. +This feature isn't available to protected VMs, as userspace does not +have access to the state that is required to perform the emulation. +Instead, a data abort exception is directly injected in the guest. +Note that although KVM_CAP_ARM_NISV_TO_USER will be reported if +queried outside of a protected VM context, the feature will not be +exposed if queried on a protected VM file descriptor. + :: /* KVM_EXIT_X86_RDMSR / KVM_EXIT_X86_WRMSR */
arch/arm64/kvm/mmio.c +8 -0
@@ -133,11 +133,19 @@ int io_mem_abort(struct kvm_vcpu *vcpu, phys_addr_t fault_ipa)/* * No valid syndrome? Ask userspace for help if it has * volunteered to do so, and bail out otherwise. +* +* In the protected VM case, there isn't much userspace can do +* though, so directly deliver an exception to the guest. */ if (!kvm_vcpu_dabt_isvalid (vcpu)) {trace_kvm_mmio_nisv (*vcpu_pc (vcpu),kvm_vcpu_get_esr (vcpu),kvm_vcpu_get_hfar (vcpu), fault_ipa); +if (vcpu_is_protected (vcpu)) { +kvm_inject_dabt (vcpu,kvm_vcpu_get_hfar (vcpu)); +return 1 ; + } +if (test_bit (KVM_ARCH_FLAG_RETURN_NISV_IO_ABORT_TO_USER , &vcpu->kvm->arch.flags)) { run->exit_reason =KVM_EXIT_ARM_NISV ;
Signature · 1 line
2.44.0.769.g3c40516874-goog
Re: [PATCH v4 21/30] KVM: arm64: Refactor calculating SVE state size to use helpers
In reply to Fuad TabbaHi Fuad,
Quoted 3 lines · Fuad Tabba
The main factor for determining the SVE state size is the vector length, and future patches will need to calculate it without necessarily having a vcpu as a reference.
Unless I'm missing something (which happens a lot), I don't see any users for sve_state_size() in this series. The refactoring looks fine, but let's only do it when there's users for it.
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v4 21/30] KVM: arm64: Refactor calculating SVE state size to use helpers
Quoted 9 lines · Oliver Upton
Quoted 3 lines · Fuad Tabba
The main factor for determining the SVE state size is the vector length, and future patches will need to calculate it without necessarily having a vcpu as a reference.
Unless I'm missing something (which happens a lot), I don't see any users for
sve_state_size()in this series. The refactoring looks fine, but let's only do it when there's users for it.
Right, there was a patch using it in earlier versions of the series but that got dropped on review.
Re: [PATCH v4 21/30] KVM: arm64: Refactor calculating SVE state size to use helpers
In reply to Oliver UptonHi,
Quoted 11 lines · Oliver Upton
Hi Fuad,
Quoted 3 lines · Fuad Tabba
The main factor for determining the SVE state size is the vector length, and future patches will need to calculate it without necessarily having a vcpu as a reference.
Unless I'm missing something (which happens a lot), I don't see any users for
sve_state_size()in this series. The refactoring looks fine, but let's only do it when there's users for it.
No, there aren't any users for this now. I thought it made sense though to decouple the calculation of the state size from the vcpu, since it's not vcpu dependent.
But sure, I can hold on to this one until the remaining sve patches are ready.
Cheers,
/fuad
Quoted 3 lines
--
Thanks,
Oliver
Hi Fuad,
Quoted 10 lines · Fuad Tabba
Changes from V3 [0]:
- Rebased on latest kvmarm/next (dcbf421a8c08)
- Dropped atomic and locking patches for now, while we rework them
- Misc fixes (Oliver)We are getting closer to upstreaming the remaining part of pKVM [1]. To make the process easier for us and for our dear reviewers, we are sending out this patch series as a preamble to the upcoming patches.
Quoted 4 lines
This series is based on Linux 6.9-rc3 -- kvmarm/next (
dcbf421a8c08). Most of the patches in this series are self-standing, and can be applied directly. Patches 1 to 13 are fixes.
Thanks for enduring my feedback. I think a lot of this looks good to go. I'm not convinced that some of the patches moving/refactoring things are a net positive _on their own_, but am happy to take them when they're paired with the feature that depends on them.
Marc, sorry to be annoying, but for patches 1-18, 20, 25-30:
Signature · 2 lines
Thanks, Oliver
Hi Oliver,
Quoted 19 lines · Oliver Upton
Hi Fuad,
Quoted 10 lines · Fuad Tabba
Changes from V3 [0]:
- Rebased on latest kvmarm/next (dcbf421a8c08)
- Dropped atomic and locking patches for now, while we rework them
- Misc fixes (Oliver)We are getting closer to upstreaming the remaining part of pKVM [1]. To make the process easier for us and for our dear reviewers, we are sending out this patch series as a preamble to the upcoming patches.
Quoted 4 lines
This series is based on Linux 6.9-rc3 -- kvmarm/next (
dcbf421a8c08). Most of the patches in this series are self-standing, and can be applied directly. Patches 1 to 13 are fixes.
Quoted 4 lines
Thanks for enduring my feedback. I think a lot of this looks good to go. I'm not convinced that some of the patches moving/refactoring things are a net positive
_ontheirown_, but am happy to take them when they're paired with the feature that depends on them.
Thank you for your reviews! I'll resent these moving patches later with the other ones in that case.
Cheers,
/fuad
Quoted 8 lines
Marc, sorry to be annoying, but for patches 1-18, 20, 25-30:
Acked-by Oliver Upton <[email protected]>--
Thanks,
Oliver
Re: [PATCH v4 02/30] KVM: arm64: Move guest_owns_fp_regs() to increase its scope
In reply to Fuad TabbaQuoted 3 lines · Fuad Tabba
guest_owns_fp_regs()will be used to check fpsimd state ownership across kvm/arm64. Therefore, move it tokvm_host.hto widen its scope.
Re: [PATCH v4 03/30] KVM: arm64: Refactor checks for FP state ownership
In reply to Fuad TabbaQuoted 3 lines · Fuad Tabba
To avoid direct comparison against the
fp_ownerenum, add a new function that performs the check,host_owns_fp_regs(), to complement the existingguest_owns_fp_regs().
Marc Zyngier
maintainerQuoted 23 lines · Fuad Tabba
Hi Oliver,
Quoted 19 lines · Oliver Upton
Hi Fuad,
Quoted 14 lines · Fuad Tabba
Changes from V3 [0]:
- Rebased on latest kvmarm/next (dcbf421a8c08)
- Dropped atomic and locking patches for now, while we rework them
- Misc fixes (Oliver)We are getting closer to upstreaming the remaining part of pKVM [1]. To make the process easier for us and for our dear reviewers, we are sending out this patch series as a preamble to the upcoming patches.
This series is based on Linux 6.9-rc3 -- kvmarm/next (
dcbf421a8c08). Most of the patches in this series are self-standing, and can be applied directly. Patches 1 to 13 are fixes.
Quoted 7 lines
Quoted 4 lines
Thanks for enduring my feedback. I think a lot of this looks good to go. I'm not convinced that some of the patches moving/refactoring things are a net positive
_ontheirown_, but am happy to take them when they're paired with the feature that depends on them.Thank you for your reviews! I'll resent these moving patches later with the other ones in that case.
No need, I'll cherry-pick them directly!
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: (subset) [PATCH v4 00/30] KVM: arm64: Preamble for pKVM
In reply to Fuad TabbaQuoted 11 lines · Fuad Tabba
Changes from V3 [0]:
- Rebased on latest kvmarm/next (dcbf421a8c08)
- Dropped atomic and locking patches for now, while we rework them
- Misc fixes (Oliver)We are getting closer to upstreaming the remaining part of pKVM [1]. To make the process easier for us and for our dear reviewers, we are sending out this patch series as a preamble to the upcoming patches.
[...]
Applied to next, thanks!
[01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
commit: 4c22a40dd9c3dcc2156f312ffc71955e56192a76
[02/30] KVM: arm64: Move guest_owns_fp_regs() to increase its scope
commit: b5b85bd713b1623c192754cd39a3351fa0c13717
[03/30] KVM: arm64: Refactor checks for FP state ownership
commit: f11290e0aa6e40e6823f80c7dcdacf033a54aaeb
[04/30] KVM: arm64: Do not re-initialize the KVM lock
commit: 40099dedb4a81fbf13ebac3a9dafcb72c7722d6a
[05/30] KVM: arm64: Issue CMOs when tearing down guest s2 pages
commit: cb16301626c339b3ccde93e5deea0569e508cb98
[06/30] KVM: arm64: Avoid BUG-ing from the host abort path
commit: 02949f36bc7b723944bf754b71cfdf75e5e36f44
[07/30] KVM: arm64: Check for PTE validity when checking for executable/cacheable
commit: 96171cfa55d0a58048ef7dada507141daa400027
[08/30] KVM: arm64: Avoid BBM when changing only s/w bits in Stage-2 PTE
commit: 7cc1d214a6cd39d7af13f931c8134c24e33dd7f6
[09/30] KVM: arm64: Support TLB invalidation in guest context
commit: 58f3b0fc3b877447592301d14e7e1c05ebbad1a6
[10/30] KVM: arm64: Rename __tlb_switch_to_{guest,host}() in VHE
commit: cfbdc546b667d16cdbec04c628dc1ce5a5d33bd2
[11/30] KVM: arm64: Do not map the host fpsimd state to hyp in pKVM
commit: d48965bc47e40b06034315260b18368d6ad152b4
[12/30] KVM: arm64: Prevent kmemleak from accessing .hyp.data
commit: 06cacc9d283c858661768fe0fc86e062ac23a5ad
[13/30] KVM: arm64: Fix comment for __pkvm_vcpu_init_traps()
commit: 40458a66afdeef42966203939c5ac6c480c99a5a
[14/30] KVM: arm64: Change kvm_handle_mmio_return() return polarity
commit: cc81b6dfc3bc82c3a2600eefbd3823bdb2190197
[15/30] KVM: arm64: Move setting the page as dirty out of the critical section
commit: 9c30fc615daa3ef177a5fd4a9b2451697c515ce9
[16/30] KVM: arm64: Simplify vgic-v3 hypercalls
commit: 948e1a53c2e95ad4c03cc6201edcb5d92e87d841
[17/30] KVM: arm64: Add is_pkvm_initialized() helper
commit: d81a91af417c8f34dc3c3f8f90240e843d1c5c08
[18/30] KVM: arm64: Introduce and use predicates that check for protected VMs
commit: b6ed4fa9411f7c17ebc69949c1df66dc12b2f827
[20/30] KVM: arm64: Clarify rationale for ZCR_EL1 value restored on guest exit
commit: eef4ce6363626cbaabceef64d0bda84c3df922ac
[25/30] KVM: arm64: Reformat/beautify PTP hypercall documentation
commit: 5a08146d9ba79838b8479739c9e494bd399074e8
[26/30] KVM: arm64: Rename firmware pseudo-register documentation file
commit: af725804f905c8fbd0a6cebc61ec3f842cca5d34
[27/30] KVM: arm64: Document the KVM/arm64-specific calls in hypercalls.rst
commit: 4dc8c9de384fb99692d35d2acdfedd5660930dfc
[28/30] KVM: arm64: Refactor setting the return value in kvm_vm_ioctl_enable_cap()
commit: 97a3dee1725dc690f806f7b899b086b67f1ef905
[29/30] KVM: arm64: Restrict supported capabilities for protected VMs
commit: 92536992cfd461207c78e46154d16050b236a6fc
[30/30] KVM: arm64: Force injection of a data abort on NISV MMIO exit
commit: 3b467b16582c077f57fab244cf0801ecea7914b6Cheers,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Zenghui Yu
reviewerRe: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
In reply to Fuad TabbaHi Fuad,
Quoted 33 lines · Fuad Tabba
Since the
host_fpsimd_statehas been removed fromkvm_vcpu_arch, it isn't pointing to the hyp's version of the hostfp_regsin protected mode.Initialize the
host_datafpsimd_statepoint to the host_data's contextfp_regson pKVM initialization.Fixes 51e09b5572d6 ("KVM: arm64: Exclude host_fpsimd_state pointer from kvm_vcpu_arch")Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 1 + 3 files changed, 13 insertions(+)arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1
-0@@ -54,6 +54,7 @@ pkvm_hyp_vcpu_to_hyp_vm(struct pkvm_hyp_vcpu *hyp_vcpu) }void pkvm_hyp_vm_table_init (void *tbl); +void pkvm_host_fpsimd_state_init (void );int __pkvm_init_vm (struct kvm *host_kvm,unsigned long vm_hva,unsigned long pgd_hva);arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0@@ -247,6 +247,17 @@ void pkvm_hyp_vm_table_init(void *tbl) vm_table = tbl;
arch/arm64/kvm/hyp/nvhe/pkvm.c void pkvm_hyp_vm_table_init(void *tbl) · line 257
Quoted 10 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +8
-0} +void pkvm_host_fpsimd_state_init (void ) +{ +unsigned long i; + +for (i =0 ; i < hyp_nr_cpus; i++) { +struct kvm_host_data *host_data =per_cpu_ptr (&kvm_host_data, i); + + host_data->fpsimd_state = &host_data->host_ctxt.fp_regs;
So we save the host FP/SIMD state in hypervisor's private host_ctxt when guest accesses to FP/SIMD. Do we then need to save it into host VCPU's process state and (if so,) how does that happen?
Could you please point me to that piece of code? Sorry I'm really not familiar with the protected mode. ;-).
Thanks,
Zenghui
Re: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Hi Zenghui,
Quoted 39 lines · Zenghui Yu
Hi Fuad,
Quoted 35 lines · Fuad Tabba
Since the
host_fpsimd_statehas been removed fromkvm_vcpu_arch, it isn't pointing to the hyp's version of the hostfp_regsin protected mode.Initialize the
host_datafpsimd_statepoint to the host_data's contextfp_regson pKVM initialization.Fixes 51e09b5572d6 ("KVM: arm64: Exclude host_fpsimd_state pointer from kvm_vcpu_arch")Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 1 + 3 files changed, 13 insertions(+)arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1
-0@@ -54,6 +54,7 @@ pkvm_hyp_vcpu_to_hyp_vm(struct pkvm_hyp_vcpu *hyp_vcpu) }void pkvm_hyp_vm_table_init (void *tbl); +void pkvm_host_fpsimd_state_init (void );int __pkvm_init_vm (struct kvm *host_kvm,unsigned long vm_hva,unsigned long pgd_hva);arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0@@ -247,6 +247,17 @@ void pkvm_hyp_vm_table_init(void *tbl) vm_table = tbl; }
Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c void pkvm_hyp_vm_table_init(void *tbl) · line 257
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +8
-0+void pkvm_host_fpsimd_state_init (void ) +{ +unsigned long i; + +for (i =0 ; i < hyp_nr_cpus; i++) { +struct kvm_host_data *host_data =per_cpu_ptr (&kvm_host_data, i); + + host_data->fpsimd_state = &host_data->host_ctxt.fp_regs;So we save the host FP/SIMD state in hypervisor's private
host_ctxtwhen guest accesses to FP/SIMD. Do we then need to save it into host VCPU's process state and (if so,) how does that happen?
Depends on which tree you're looking at. This is all new in kvmarm/next [1], and I don't think it has made it to the main tree yet.
If you look in kvmarm/next, the file arch/arm64/include/asm/kvm_host.h has "struct kvm_host_data", which has a pointer to fpsimd_state, which you've already seen.
For normal mode, i.e., without kvm: In arch/arm64/kvm/fpsimd.c, kvm_arch_vcpu_load_fp(), which is called when a vcpu is loaded, assigns that pointer to the current task's fpsimd_state:
*host_data_ptr(fpsimd_state) =kern_hyp_va(¤t->thread.uw.fpsimd_state);
You might have missed it because it uses the host_data_ptr() macro to access struct kvm_host_data
I hope this helps,
/fuad
[1] https://git.kernel.org/pub/scm/linux/kernel/git/kvmarm/kvmarm.git/log/?h=next
Quoted 6 lines
Could you please point me to that piece of code? Sorry I'm really not familiar with the protected mode. ;-).
Thanks,
Zenghui
Zenghui Yu
reviewerRe: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Quoted 55 lines · Fuad Tabba
Hi Zenghui,
Quoted 39 lines · Zenghui Yu
Hi Fuad,
Quoted 35 lines · Fuad Tabba
Since the
host_fpsimd_statehas been removed fromkvm_vcpu_arch, it isn't pointing to the hyp's version of the hostfp_regsin protected mode.Initialize the
host_datafpsimd_statepoint to the host_data's contextfp_regson pKVM initialization.Fixes 51e09b5572d6 ("KVM: arm64: Exclude host_fpsimd_state pointer from kvm_vcpu_arch")Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 1 + 3 files changed, 13 insertions(+)arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1
-0@@ -54,6 +54,7 @@ pkvm_hyp_vcpu_to_hyp_vm(struct pkvm_hyp_vcpu *hyp_vcpu) }void pkvm_hyp_vm_table_init (void *tbl); +void pkvm_host_fpsimd_state_init (void );int __pkvm_init_vm (struct kvm *host_kvm,unsigned long vm_hva,unsigned long pgd_hva);arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0@@ -247,6 +247,17 @@ void pkvm_hyp_vm_table_init(void *tbl) vm_table = tbl; }Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c void pkvm_hyp_vm_table_init(void *tbl) · line 257
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +8
-0+void pkvm_host_fpsimd_state_init (void ) +{ +unsigned long i; + +for (i =0 ; i < hyp_nr_cpus; i++) { +struct kvm_host_data *host_data =per_cpu_ptr (&kvm_host_data, i); + + host_data->fpsimd_state = &host_data->host_ctxt.fp_regs;So we save the host FP/SIMD state in hypervisor's private
host_ctxtwhen guest accesses to FP/SIMD. Do we then need to save it into host VCPU's process state and (if so,) how does that happen?
Quoted 3 lines
Depends on which tree you're looking at. This is all new in kvmarm/next [1], and I don't think it has made it to the main tree yet.
I had looked at Marc's "host-data" series before your series, so we should at the same position.
Quoted 5 lines
If you look in kvmarm/next, the file
arch/arm64/include/asm/kvm_host.hhas "structkvm_host_data", which has a pointer tofpsimd_state, which you've already seen.For normal mode, i.e., without kvm: In
arch/arm64/kvm/fpsimd.c,
"without pkvm"?
Quoted 8 lines
kvm_arch_vcpu_load_fp(), which is called when a vcpu is loaded, assigns that pointer to the current task'sfpsimd_state:
*host_data_ptr(fpsimd_state)=kern_hyp_va(¤t->thread.uw.fpsimd_state);You might have missed it because it uses the
host_data_ptr()macro to access structkvm_host_data
Actually I was not asking for the normal mode (VHE, Hyp nVHE), where we save the host's FP/SIMD state directly into the current task's fpsimd_state, which just looks fine.
I asked that in protected mode, how do we save the host state? It looks to me that we only save it into EL2's private kvm_host_data->host_ctxt.fp_regs which the host kernel isn't aware of. How can host kernel restore the state for the current task later (e.g., for host userspace accesses)? What am I missing?
Thanks,
Zenghui
Re: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Hi,
Quoted 64 lines · Zenghui Yu
Quoted 55 lines · Fuad Tabba
Hi Zenghui,
Quoted 39 lines · Zenghui Yu
Hi Fuad,
Quoted 35 lines · Fuad Tabba
Since the
host_fpsimd_statehas been removed fromkvm_vcpu_arch, it isn't pointing to the hyp's version of the hostfp_regsin protected mode.Initialize the
host_datafpsimd_statepoint to the host_data's contextfp_regson pKVM initialization.Fixes 51e09b5572d6 ("KVM: arm64: Exclude host_fpsimd_state pointer from kvm_vcpu_arch")Signed-off-by Fuad Tabba <[email protected]>--- arch/arm64/kvm/hyp/include/nvhe/pkvm.h | 1 + arch/arm64/kvm/hyp/nvhe/pkvm.c | 11 +++++++++++ arch/arm64/kvm/hyp/nvhe/setup.c | 1 + 3 files changed, 13 insertions(+)arch/arm64/kvm/hyp/include/nvhe/pkvm.h +1
-0@@ -54,6 +54,7 @@ pkvm_hyp_vcpu_to_hyp_vm(struct pkvm_hyp_vcpu *hyp_vcpu) }void pkvm_hyp_vm_table_init (void *tbl); +void pkvm_host_fpsimd_state_init (void );int __pkvm_init_vm (struct kvm *host_kvm,unsigned long vm_hva,unsigned long pgd_hva);arch/arm64/kvm/hyp/nvhe/pkvm.c +0
-0@@ -247,6 +247,17 @@ void pkvm_hyp_vm_table_init(void *tbl) vm_table = tbl; }Quoted 12 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c void pkvm_hyp_vm_table_init(void *tbl) · line 257
Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/pkvm.c +8
-0+void pkvm_host_fpsimd_state_init (void ) +{ +unsigned long i; + +for (i =0 ; i < hyp_nr_cpus; i++) { +struct kvm_host_data *host_data =per_cpu_ptr (&kvm_host_data, i); + + host_data->fpsimd_state = &host_data->host_ctxt.fp_regs;So we save the host FP/SIMD state in hypervisor's private
host_ctxtwhen guest accesses to FP/SIMD. Do we then need to save it into host VCPU's process state and (if so,) how does that happen?Quoted 3 lines
Depends on which tree you're looking at. This is all new in kvmarm/next [1], and I don't think it has made it to the main tree yet.
I had looked at Marc's "host-data" series before your series, so we should at the same position.
Quoted 7 lines
Quoted 5 lines
If you look in kvmarm/next, the file
arch/arm64/include/asm/kvm_host.hhas "structkvm_host_data", which has a pointer tofpsimd_state, which you've already seen.For normal mode, i.e., without kvm: In
arch/arm64/kvm/fpsimd.c,"without pkvm"?
Oops, typo, yes, without pKVM.
Quoted 14 lines
Quoted 9 lines
kvm_arch_vcpu_load_fp(), which is called when a vcpu is loaded, assigns that pointer to the current task'sfpsimd_state:
*host_data_ptr(fpsimd_state)=kern_hyp_va(¤t->thread.uw.fpsimd_state);You might have missed it because it uses the
host_data_ptr()macro to access structkvm_host_dataActually I was not asking for the normal mode (VHE, Hyp nVHE), where we save the host's FP/SIMD state directly into the current task's
fpsimd_state, which just looks fine.
Quoted 5 lines
I asked that in protected mode, how do we save the host state? It looks to me that we only save it into EL2's private
kvm_host_data->host_ctxt.fp_regswhich the host kernel isn't aware of. How can host kernel restore the state for the current task later (e.g., for host userspace accesses)? What am I missing?
What I'm going to say applies only to protected VMs in protected mode, i.e., non-protected VM's in protected mode use the mechanism above (kvm_arch_vcpu_load_fp()).
Also, the upstream code for pKVM isn't complete yet as we're still upstreaming it, so you not being able to find something is our fault not yours :) You can find my branch of pKVM, based on the latest kvmarm/next here [1].
For protected VMs, we don't want to lazily save/restore the host's fpsimd state, because that could leak information to the host that the protected guest is using fpsimd. Therefore, for protected VMs, the host fpsimd state is maintained (saved/restored) in hyp (at EL2), without the host needing to know or do anything about it.
I hope this helps,
/fuad
[1] https://android-kvm.googlesource.com/linux/+/refs/heads/tabba/next/pkvm-core
Quoted 3 lines
Thanks,
Zenghui
Marc Zyngier
maintainerRe: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Quoted 20 lines · Fuad Tabba
Hi,
Quoted 6 lines · Zenghui Yu
I asked that in protected mode, how do we save the host state? It looks to me that we only save it into EL2's private
kvm_host_data->host_ctxt.fp_regswhich the host kernel isn't aware of. How can host kernel restore the state for the current task later (e.g., for host userspace accesses)? What am I missing?What I'm going to say applies only to protected VMs in protected mode, i.e., non-protected VM's in protected mode use the mechanism above (
kvm_arch_vcpu_load_fp()).Also, the upstream code for pKVM isn't complete yet as we're still upstreaming it, so you not being able to find something is our fault not yours :) You can find my branch of pKVM, based on the latest kvmarm/next here [1].
Quoted 5 lines
For protected VMs, we don't want to lazily save/restore the host's fpsimd state, because that could leak information to the host that the protected guest is using fpsimd. Therefore, for protected VMs, the host fpsimd state is maintained (saved/restored) in hyp (at EL2), without the host needing to know or do anything about it.
But I think that's a regression from the current status. It looks like now that the state is really private to EL2, nothing will restore the FP context on exit, while this was the case before we reworked this.
It'd be good to plug this in 6.10, as this is a regression.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.
Re: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Hi Marc,
Quoted 29 lines · Marc Zyngier
Quoted 20 lines · Fuad Tabba
Hi,
Quoted 6 lines · Zenghui Yu
I asked that in protected mode, how do we save the host state? It looks to me that we only save it into EL2's private
kvm_host_data->host_ctxt.fp_regswhich the host kernel isn't aware of. How can host kernel restore the state for the current task later (e.g., for host userspace accesses)? What am I missing?What I'm going to say applies only to protected VMs in protected mode, i.e., non-protected VM's in protected mode use the mechanism above (
kvm_arch_vcpu_load_fp()).Also, the upstream code for pKVM isn't complete yet as we're still upstreaming it, so you not being able to find something is our fault not yours :) You can find my branch of pKVM, based on the latest kvmarm/next here [1].
Quoted 5 lines
For protected VMs, we don't want to lazily save/restore the host's fpsimd state, because that could leak information to the host that the protected guest is using fpsimd. Therefore, for protected VMs, the host fpsimd state is maintained (saved/restored) in hyp (at EL2), without the host needing to know or do anything about it.
Quoted 5 lines
But I think that's a regression from the current status. It looks like now that the state is really private to EL2, nothing will restore the FP context on exit, while this was the case before we reworked this.
It'd be good to plug this in 6.10, as this is a regression.
You're right. I'll send a patch to fix this.
Cheers,
/fuad
Quoted 6 lines
Thanks,
M.--
Without deviation from the norm, progress is not possible.
Zenghui Yu
reviewerRe: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Quoted 25 lines · Fuad Tabba
Hi Marc,
Quoted 22 lines · Marc Zyngier
Quoted 19 lines · Fuad Tabba
Hi,
Quoted 6 lines · Zenghui Yu
I asked that in protected mode, how do we save the host state? It looks to me that we only save it into EL2's private
kvm_host_data->host_ctxt.fp_regswhich the host kernel isn't aware of. How can host kernel restore the state for the current task later (e.g., for host userspace accesses)? What am I missing?What I'm going to say applies only to protected VMs in protected mode, i.e., non-protected VM's in protected mode use the mechanism above (
kvm_arch_vcpu_load_fp()).Also, the upstream code for pKVM isn't complete yet as we're still upstreaming it, so you not being able to find something is our fault not yours :) You can find my branch of pKVM, based on the latest kvmarm/next here [1].
Thanks for sharing the details!
Quoted 9 lines
Quoted 9 lines
Quoted 5 lines
For protected VMs, we don't want to lazily save/restore the host's fpsimd state, because that could leak information to the host that the protected guest is using fpsimd. Therefore, for protected VMs, the host fpsimd state is maintained (saved/restored) in hyp (at EL2), without the host needing to know or do anything about it.
But I think that's a regression from the current status. It looks like now that the state is really private to EL2, nothing will restore the FP context on exit, while this was the case before we reworked this.
My question and concern exactly. Thanks both for confirmation.
Quoted 3 lines
Quoted 1 line
It'd be good to plug this in 6.10, as this is a regression.
You're right. I'll send a patch to fix this.
Zenghui
Re: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
In reply to Fuad TabbaOn Fri, May 10, 2024 at 09:00:54AM +0100, Fuad Tabba wrote:
[...]
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
For protected VMs, we don't want to lazily save/restore the host's fpsimd state, because that could leak information to the host that the protected guest is using fpsimd. Therefore, for protected VMs, the host fpsimd state is maintained (saved/restored) in hyp (at EL2), without the host needing to know or do anything about it.
I'm a bit confused by this statement. Isn't pKVM downstream lazily managing fpsimd state on behalf of the host for protected guests? IOW, don't we return to the host with traps enabled at EL2 and load state after the trap?
Quoted 7 lines
Quoted 5 lines
But I think that's a regression from the current status. It looks like now that the state is really private to EL2, nothing will restore the FP context on exit, while this was the case before we reworked this.
It'd be good to plug this in 6.10, as this is a regression.
You're right. I'll send a patch to fix this.
I'd be fine with a hack that *eagerly* saves/restores the fpsimd state on every exit in protected mode for the time being. Not great, but obviously correct and its not like protected mode is very useful upstream to begin with.
Signature · 2 lines
Thanks, Oliver
Re: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Hi Oliver
Quoted 11 lines · Oliver Upton
On Fri, May 10, 2024 at 09:00:54AM +0100, Fuad Tabba wrote:
[...]
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
For protected VMs, we don't want to lazily save/restore the host's fpsimd state, because that could leak information to the host that the protected guest is using fpsimd. Therefore, for protected VMs, the host fpsimd state is maintained (saved/restored) in hyp (at EL2), without the host needing to know or do anything about it.
Quoted 4 lines
I'm a bit confused by this statement. Isn't pKVM downstream lazily managing fpsimd state on behalf of the host for protected guests? IOW, don't we return to the host with traps enabled at EL2 and load state after the trap?
Sorry, I got my wires crossed between fpsimd and sve. It's the sve state that is restored eagerly, rather than rely on the TIF_FOREIGN_FPSTATE as in other KVM modes.
Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
But I think that's a regression from the current status. It looks like now that the state is really private to EL2, nothing will restore the FP context on exit, while this was the case before we reworked this.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
It'd be good to plug this in 6.10, as this is a regression.
You're right. I'll send a patch to fix this.
I'd be fine with a hack that *eagerly* saves/restores the fpsimd state on every exit in protected mode for the time being. Not great, but obviously correct and its not like protected mode is very useful upstream to begin with.
I think I have a solution that would maintain the existing behavior for non-protected VMs in protected mode, i.e., lazy handling. I just want to tidy up the code a bit more, run it through a few more fpsimd and sve stress tests, and send the patches.
Thanks,
/fuad
Quoted 3 lines
--
Thanks,
Oliver
Marc Zyngier
maintainerRe: [PATCH v4 01/30] KVM: arm64: Initialize the kvm host data's fpsimd_state pointer in pKVM
Quoted 37 lines · Fuad Tabba
Hi Oliver
Quoted 11 lines · Oliver Upton
On Fri, May 10, 2024 at 09:00:54AM +0100, Fuad Tabba wrote:
[...]
Quoted 5 lines
Quoted 5 lines
Quoted 5 lines
For protected VMs, we don't want to lazily save/restore the host's fpsimd state, because that could leak information to the host that the protected guest is using fpsimd. Therefore, for protected VMs, the host fpsimd state is maintained (saved/restored) in hyp (at EL2), without the host needing to know or do anything about it.
Quoted 4 lines
I'm a bit confused by this statement. Isn't pKVM downstream lazily managing fpsimd state on behalf of the host for protected guests? IOW, don't we return to the host with traps enabled at EL2 and load state after the trap?
Sorry, I got my wires crossed between fpsimd and sve. It's the sve state that is restored eagerly, rather than rely on the
TIF_FOREIGN_FPSTATEas in other KVM modes.Quoted 4 lines
Quoted 4 lines
Quoted 4 lines
But I think that's a regression from the current status. It looks like now that the state is really private to EL2, nothing will restore the FP context on exit, while this was the case before we reworked this.
Quoted 8 lines
Quoted 3 lines
Quoted 1 line
It'd be good to plug this in 6.10, as this is a regression.
You're right. I'll send a patch to fix this.
I'd be fine with a hack that *eagerly* saves/restores the fpsimd state on every exit in protected mode for the time being. Not great, but obviously correct and its not like protected mode is very useful upstream to begin with.
Quoted 4 lines
I think I have a solution that would maintain the existing behavior for non-protected VMs in protected mode, i.e., lazy handling. I just want to tidy up the code a bit more, run it through a few more fpsimd and sve stress tests, and send the patches.
For the record, I don't think it is worth special-casing non-protected VMs under pKVM. If eager save/restore is what pKVM needs to ensure protection, then this should equally apply to non-protected VMs. Having a single behaviour for a given host mode is hugely beneficial from a maintenance PoV.
What *could* be done as an optimisation is to make the restore lazy for *everything* by trapping the host access to the FP state (similar to what we do for guests, funnily enough).
But I see this only as an optimisation, not as a functional requirement.
Thanks,
M.
Signature · 1 line
Without deviation from the norm, progress is not possible.