From: "Lorenzo Stoakes (ARM)" <ljs@kernel.org> mainline inclusion from mainline-v7.4-rc1 commit 18b94675b1fb22b5ce9b607f9de818d916b3da16 category: feature bugzilla: https://atomgit.com/openeuler/kernel/issues/10374 CVE: NA Reference: https://github.com/torvalds/linux/commit/18b94675b1fb22b5ce9b607f9de818d916b... -------------------------------- commit 18b94675b1fb22b5ce9b607f9de818d916b3da16 upstream Convert kvm_vcpu_* ESR wrappers in kvm_emulate.h and the stage-2 abort handling logic in mmu.c to use the newly introduced ESR helpers. kvm_is_write_fault() is split in two, with esr_abt_is_write_fault() handling the esr parts of the operation and kvm_is_write_fault() wrapping it. Introduce/modify kvm_s2_fault_is_{perm,exec,write}(), kvm_s2_perm_fault_granule() so the abort path reads the ESR from a single place. This is to allow a later change to permit stage 2 pre-faulting via a synthetic ESR value. Remove now-unused kvm_vcpu_dabt_is_cm(), kvm_vcpu_trap_is_exec_fault() and kvm_vcpu_trap_get_perm_fault_granule(). No functional change intended. [Backport note] Adapted for OLK-6.6: - OLK-6.6 does not have gmem_abort() or the s2fd helper functions, so the s2fd-specific hunks (kvm_s2_fault_is_*, kvm_s2_perm_fault_granule) are omitted. Only the ESR-helper conversion of the existing abort path is applied. - kvm_vcpu_dabt_is_cm() is kept because OLK-6.6's tmi.c still calls it; kvm_vcpu_trap_is_exec_fault() remains until the next patch converts its last caller. - The existing OLK-6.6 cache-maintenance check in kvm_is_write_fault() is preserved inside esr_abt_is_write_fault(). Reviewed-by: Oliver Upton <oupton@kernel.org> Signed-off-by: Lorenzo Stoakes (ARM) <ljs@kernel.org> Reviewed-by: Fuad Tabba <fuad.tabba@linux.dev> Tested-by: Fuad Tabba <fuad.tabba@linux.dev> Link: https://patch.msgid.link/20260923-kvm-arm-prefault-v4-3-d4b0b4dfa8c3@kernel.... Signed-off-by: Marc Zyngier <maz@kernel.org> Signed-off-by: Yanan Wang <wangyanan55@huawei.com> --- arch/arm64/include/asm/kvm_emulate.h | 41 +++++++++++----------------- arch/arm64/kvm/mmu.c | 30 ++++++++++---------- 2 files changed, 31 insertions(+), 40 deletions(-) diff --git a/arch/arm64/include/asm/kvm_emulate.h b/arch/arm64/include/asm/kvm_emulate.h index a36796307680..373c20b998b3 100644 --- a/arch/arm64/include/asm/kvm_emulate.h +++ b/arch/arm64/include/asm/kvm_emulate.h @@ -510,18 +510,18 @@ static __always_inline int kvm_vcpu_dabt_get_rd(const struct kvm_vcpu *vcpu) static __always_inline bool kvm_vcpu_abt_iss1tw(const struct kvm_vcpu *vcpu) { - return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_S1PTW); + return esr_abt_is_s1ptw(kvm_vcpu_get_esr(vcpu)); } /* Always check for S1PTW *before* using this. */ static __always_inline bool kvm_vcpu_dabt_iswrite(const struct kvm_vcpu *vcpu) { - return kvm_vcpu_get_esr(vcpu) & ESR_ELx_WNR; + return esr_dabt_is_write(kvm_vcpu_get_esr(vcpu)); } static inline bool kvm_vcpu_dabt_is_cm(const struct kvm_vcpu *vcpu) { - return !!(kvm_vcpu_get_esr(vcpu) & ESR_ELx_CM); + return esr_dabt_is_cm(kvm_vcpu_get_esr(vcpu)); } static __always_inline unsigned int kvm_vcpu_dabt_get_as(const struct kvm_vcpu *vcpu) @@ -542,7 +542,7 @@ static __always_inline u8 kvm_vcpu_trap_get_class(const struct kvm_vcpu *vcpu) static inline bool kvm_vcpu_trap_is_iabt(const struct kvm_vcpu *vcpu) { - return kvm_vcpu_trap_get_class(vcpu) == ESR_ELx_EC_IABT_LOW; + return esr_trap_is_iabt(kvm_vcpu_get_esr(vcpu)); } static inline bool kvm_vcpu_trap_is_exec_fault(const struct kvm_vcpu *vcpu) @@ -567,21 +567,7 @@ static __always_inline u8 kvm_vcpu_trap_get_fault_level(const struct kvm_vcpu *v static __always_inline bool kvm_vcpu_abt_issea(const struct kvm_vcpu *vcpu) { - switch (kvm_vcpu_trap_get_fault(vcpu)) { - case ESR_ELx_FSC_EXTABT: - case ESR_ELx_FSC_SEA_TTW0: - case ESR_ELx_FSC_SEA_TTW1: - case ESR_ELx_FSC_SEA_TTW2: - case ESR_ELx_FSC_SEA_TTW3: - case ESR_ELx_FSC_SECC: - case ESR_ELx_FSC_SECC_TTW0: - case ESR_ELx_FSC_SECC_TTW1: - case ESR_ELx_FSC_SECC_TTW2: - case ESR_ELx_FSC_SECC_TTW3: - return true; - default: - return false; - } + return esr_abt_is_sea(kvm_vcpu_get_esr(vcpu)); } static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu) @@ -590,9 +576,9 @@ static __always_inline int kvm_vcpu_sys_get_rt(struct kvm_vcpu *vcpu) return ESR_ELx_SYS64_ISS_RT(esr); } -static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu) +static inline bool esr_abt_is_write_fault(unsigned long esr) { - if (kvm_vcpu_abt_iss1tw(vcpu)) { + if (esr_abt_is_s1ptw(esr)) { /* * Only a permission fault on a S1PTW should be * considered as a write. Otherwise, page tables baked @@ -605,7 +591,7 @@ static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu) * first), then a permission fault to allow the flags * to be set. */ - switch (kvm_vcpu_trap_get_fault_type(vcpu)) { + switch (esr & ESR_ELx_FSC_TYPE) { case ESR_ELx_FSC_PERM: return true; default: @@ -613,7 +599,7 @@ static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu) } } - if (kvm_vcpu_trap_is_iabt(vcpu)) + if (esr_trap_is_iabt(esr)) return false; /* @@ -621,10 +607,15 @@ static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu) * shadow, even though its abort reports WnR. IVAC traps as UNKNOWN * and its guest write permission is checked by the PVM backend. */ - if (is_pvm_mode() && kvm_vcpu_dabt_is_cm(vcpu)) + if (is_pvm_mode() && esr_dabt_is_cm(esr)) return false; - return kvm_vcpu_dabt_iswrite(vcpu); + return esr_dabt_is_write(esr); +} + +static inline bool kvm_is_write_fault(struct kvm_vcpu *vcpu) +{ + return esr_abt_is_write_fault(kvm_vcpu_get_esr(vcpu)); } static inline unsigned long kvm_vcpu_get_mpidr_aff(struct kvm_vcpu *vcpu) diff --git a/arch/arm64/kvm/mmu.c b/arch/arm64/kvm/mmu.c index c01338229510..3ae04d3d104c 100644 --- a/arch/arm64/kvm/mmu.c +++ b/arch/arm64/kvm/mmu.c @@ -1960,20 +1960,21 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) struct kvm_s1_trans *result = NULL; unsigned long shadow_seq = 0; unsigned long walk_seq = 0; + unsigned long esr = kvm_vcpu_get_esr(vcpu); - fault_status = kvm_vcpu_trap_get_fault_type(vcpu); + fault_status = esr & ESR_ELx_FSC_TYPE; fault_ipa = is_pvm_mode() ? kvm_vcpu_get_hfar(vcpu) : kvm_vcpu_get_fault_ipa(vcpu); ipa = fault_ipa; - is_iabt = kvm_vcpu_trap_is_iabt(vcpu); + is_iabt = esr_trap_is_iabt(esr); #ifdef CONFIG_ARM64_HDBSS /* * HDBSS buffer already flushed when enter handle_trap_exceptions(). * Nothing to do here. */ - iss2 = ESR_ELx_ISS2(kvm_vcpu_get_esr(vcpu)); + iss2 = ESR_ELx_ISS2(esr); if (fault_status == ESR_ELx_FSC_PERM && (iss2 & ESR_ELx_HDBSSF)) return 1; #endif @@ -1998,34 +1999,33 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) } /* Synchronous External Abort? */ - if (kvm_vcpu_abt_issea(vcpu)) { + if (esr_abt_is_sea(esr)) { /* * For RAS the host kernel may handle this abort. * There is no need to pass the error into the guest. */ - if (kvm_handle_guest_sea(fault_ipa, kvm_vcpu_get_esr(vcpu))) + if (kvm_handle_guest_sea(fault_ipa, esr)) kvm_inject_vabt(vcpu); return 1; } - trace_kvm_guest_fault(*vcpu_pc(vcpu), kvm_vcpu_get_esr(vcpu), + trace_kvm_guest_fault(*vcpu_pc(vcpu), esr, kvm_vcpu_get_hfar(vcpu), fault_ipa); /* PVM also receives stage-1 address-size faults (FSC type 0). */ pvm_size_fault = is_pvm_mode() && fault_status == 0; if (pvm_size_fault && !kvm_pvm_vcpu_mmu_enabled(vcpu)) - return kvm_pvm_inject_s1_fault(vcpu, - kvm_vcpu_trap_get_fault(vcpu)); + return kvm_pvm_inject_s1_fault(vcpu, esr & ESR_ELx_FSC); /* Keep the ordinary stage-2 FSC whitelist. */ if (fault_status != ESR_ELx_FSC_FAULT && fault_status != ESR_ELx_FSC_PERM && fault_status != ESR_ELx_FSC_ACCESS && !pvm_size_fault) { - kvm_err("Unsupported FSC: EC=%#x xFSC=%#lx ESR_EL2=%#lx\n", - kvm_vcpu_trap_get_class(vcpu), - (unsigned long)kvm_vcpu_trap_get_fault(vcpu), - (unsigned long)kvm_vcpu_get_esr(vcpu)); + kvm_err("Unsupported FSC: EC=%#lx xFSC=%#lx ESR_EL2=%#lx\n", + ESR_ELx_EC(esr), + (unsigned long)(esr & ESR_ELx_FSC), + (unsigned long)esr); return -EFAULT; } @@ -2068,7 +2068,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) gfn = kvm_gpa_from_fault(vcpu->kvm, ipa) >> PAGE_SHIFT; memslot = gfn_to_memslot(vcpu->kvm, gfn); hva = gfn_to_hva_memslot_prot(memslot, gfn, &writable); - write_fault = kvm_is_write_fault(vcpu); + write_fault = esr_abt_is_write_fault(esr); if (kvm_is_error_hva(hva) || (write_fault && !writable)) { /* * The guest has put either its instructions or its page-tables @@ -2081,7 +2081,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) goto out; } - if (kvm_vcpu_abt_iss1tw(vcpu)) { + if (esr_abt_is_s1ptw(esr)) { kvm_inject_dabt(vcpu, kvm_vcpu_get_hfar(vcpu)); ret = 1; goto out_unlock; @@ -2097,7 +2097,7 @@ int kvm_handle_guest_abort(struct kvm_vcpu *vcpu) * So let's assume that the guest is just being * cautious, and skip the instruction. */ - if (kvm_is_error_hva(hva) && kvm_vcpu_dabt_is_cm(vcpu)) { + if (kvm_is_error_hva(hva) && esr_dabt_is_cm(esr)) { kvm_incr_pc(vcpu); ret = 1; goto out_unlock; -- 2.43.0