Skip to content

Commit fb8a3eb

Browse files
committed
KVM: arm64: Only read HPFAR_EL2 when value is architecturally valid
KVM's logic for deciding when HPFAR_EL2 is UNKNOWN doesn't align with the architecture. Most notably, KVM assumes HPFAR_EL2 contains the faulting IPA even in the case of an SEA. Align the logic with the architecture rather than attempting to paraphrase it. Additionally, take the opportunity to improve the language around ARM erratum #834220 such that it actually describes the bug. Reviewed-by: Marc Zyngier <maz@kernel.org> Link: https://lore.kernel.org/r/20250402201725.2963645-2-oliver.upton@linux.dev Signed-off-by: Oliver Upton <oliver.upton@linux.dev>
1 parent 1f5bdd3 commit fb8a3eb

File tree

2 files changed

+48
-20
lines changed

2 files changed

+48
-20
lines changed

arch/arm64/include/asm/esr.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,15 @@
121121
#define ESR_ELx_FSC_SEA_TTW(n) (0x14 + (n))
122122
#define ESR_ELx_FSC_SECC (0x18)
123123
#define ESR_ELx_FSC_SECC_TTW(n) (0x1c + (n))
124+
#define ESR_ELx_FSC_ADDRSZ (0x00)
125+
126+
/*
127+
* Annoyingly, the negative levels for Address size faults aren't laid out
128+
* contiguously (or in the desired order)
129+
*/
130+
#define ESR_ELx_FSC_ADDRSZ_nL(n) ((n) == -1 ? 0x25 : 0x2C)
131+
#define ESR_ELx_FSC_ADDRSZ_L(n) ((n) < 0 ? ESR_ELx_FSC_ADDRSZ_nL(n) : \
132+
(ESR_ELx_FSC_ADDRSZ + (n)))
124133

125134
/* Status codes for individual page table levels */
126135
#define ESR_ELx_FSC_ACCESS_L(n) (ESR_ELx_FSC_ACCESS + (n))
@@ -161,8 +170,6 @@
161170
#define ESR_ELx_Xs_MASK (GENMASK_ULL(4, 0))
162171

163172
/* ISS field definitions for exceptions taken in to Hyp */
164-
#define ESR_ELx_FSC_ADDRSZ (0x00)
165-
#define ESR_ELx_FSC_ADDRSZ_L(n) (ESR_ELx_FSC_ADDRSZ + (n))
166173
#define ESR_ELx_CV (UL(1) << 24)
167174
#define ESR_ELx_COND_SHIFT (20)
168175
#define ESR_ELx_COND_MASK (UL(0xF) << ESR_ELx_COND_SHIFT)
@@ -464,6 +471,17 @@ static inline bool esr_fsc_is_access_flag_fault(unsigned long esr)
464471
(esr == ESR_ELx_FSC_ACCESS_L(0));
465472
}
466473

474+
static inline bool esr_fsc_is_addr_sz_fault(unsigned long esr)
475+
{
476+
esr &= ESR_ELx_FSC;
477+
478+
return (esr == ESR_ELx_FSC_ADDRSZ_L(3)) ||
479+
(esr == ESR_ELx_FSC_ADDRSZ_L(2)) ||
480+
(esr == ESR_ELx_FSC_ADDRSZ_L(1)) ||
481+
(esr == ESR_ELx_FSC_ADDRSZ_L(0)) ||
482+
(esr == ESR_ELx_FSC_ADDRSZ_L(-1));
483+
}
484+
467485
/* Indicate whether ESR.EC==0x1A is for an ERETAx instruction */
468486
static inline bool esr_iss_is_eretax(unsigned long esr)
469487
{

arch/arm64/kvm/hyp/include/hyp/fault.h

Lines changed: 28 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -44,31 +44,41 @@ static inline bool __translate_far_to_hpfar(u64 far, u64 *hpfar)
4444
return true;
4545
}
4646

47+
/*
48+
* Checks for the conditions when HPFAR_EL2 is written, per ARM ARM R_FKLWR.
49+
*/
50+
static inline bool __hpfar_valid(u64 esr)
51+
{
52+
/*
53+
* CPUs affected by ARM erratum #834220 may incorrectly report a
54+
* stage-2 translation fault when a stage-1 permission fault occurs.
55+
*
56+
* Re-walk the page tables to determine if a stage-1 fault actually
57+
* occurred.
58+
*/
59+
if (cpus_have_final_cap(ARM64_WORKAROUND_834220) &&
60+
esr_fsc_is_translation_fault(esr))
61+
return false;
62+
63+
if (esr_fsc_is_translation_fault(esr) || esr_fsc_is_access_flag_fault(esr))
64+
return true;
65+
66+
if ((esr & ESR_ELx_S1PTW) && esr_fsc_is_permission_fault(esr))
67+
return true;
68+
69+
return esr_fsc_is_addr_sz_fault(esr);
70+
}
71+
4772
static inline bool __get_fault_info(u64 esr, struct kvm_vcpu_fault_info *fault)
4873
{
4974
u64 hpfar, far;
5075

5176
far = read_sysreg_el2(SYS_FAR);
5277

53-
/*
54-
* The HPFAR can be invalid if the stage 2 fault did not
55-
* happen during a stage 1 page table walk (the ESR_EL2.S1PTW
56-
* bit is clear) and one of the two following cases are true:
57-
* 1. The fault was due to a permission fault
58-
* 2. The processor carries errata 834220
59-
*
60-
* Therefore, for all non S1PTW faults where we either have a
61-
* permission fault or the errata workaround is enabled, we
62-
* resolve the IPA using the AT instruction.
63-
*/
64-
if (!(esr & ESR_ELx_S1PTW) &&
65-
(cpus_have_final_cap(ARM64_WORKAROUND_834220) ||
66-
esr_fsc_is_permission_fault(esr))) {
67-
if (!__translate_far_to_hpfar(far, &hpfar))
68-
return false;
69-
} else {
78+
if (__hpfar_valid(esr))
7079
hpfar = read_sysreg(hpfar_el2);
71-
}
80+
else if (!__translate_far_to_hpfar(far, &hpfar))
81+
return false;
7282

7383
fault->far_el2 = far;
7484
fault->hpfar_el2 = hpfar;

0 commit comments

Comments
 (0)