Skip to content

Commit b56680d

Browse files
Fuad Tabbaoupton
authored andcommitted
KVM: arm64: Initialize trap register values in hyp in pKVM
Handle the initialization of trap registers at the hypervisor in pKVM, even for non-protected guests. The host is not trusted with the values of the trap registers, regardless of the VM type. Therefore, when switching between the host and the guests, only flush the HCR_EL2 TWI and TWE bits. The host is allowed to configure these for opportunistic scheduling, as neither affects the protection of VMs or the hypervisor. Reported-by: Will Deacon <[email protected]> Fixes: 814ad8f ("KVM: arm64: Drop trapping of PAuth instructions/keys") Signed-off-by: Fuad Tabba <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Oliver Upton <[email protected]>
1 parent cb0c272 commit b56680d

File tree

2 files changed

+38
-1
lines changed

2 files changed

+38
-1
lines changed

arch/arm64/kvm/hyp/nvhe/hyp-main.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,8 +105,10 @@ static void flush_hyp_vcpu(struct pkvm_hyp_vcpu *hyp_vcpu)
105105

106106
hyp_vcpu->vcpu.arch.hw_mmu = host_vcpu->arch.hw_mmu;
107107

108-
hyp_vcpu->vcpu.arch.hcr_el2 = host_vcpu->arch.hcr_el2;
109108
hyp_vcpu->vcpu.arch.mdcr_el2 = host_vcpu->arch.mdcr_el2;
109+
hyp_vcpu->vcpu.arch.hcr_el2 &= ~(HCR_TWI | HCR_TWE);
110+
hyp_vcpu->vcpu.arch.hcr_el2 |= READ_ONCE(host_vcpu->arch.hcr_el2) &
111+
(HCR_TWI | HCR_TWE);
110112

111113
hyp_vcpu->vcpu.arch.iflags = host_vcpu->arch.iflags;
112114

arch/arm64/kvm/hyp/nvhe/pkvm.c

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -204,11 +204,46 @@ static void pvm_init_trap_regs(struct kvm_vcpu *vcpu)
204204
}
205205
}
206206

207+
static void pkvm_vcpu_reset_hcr(struct kvm_vcpu *vcpu)
208+
{
209+
vcpu->arch.hcr_el2 = HCR_GUEST_FLAGS;
210+
211+
if (has_hvhe())
212+
vcpu->arch.hcr_el2 |= HCR_E2H;
213+
214+
if (cpus_have_final_cap(ARM64_HAS_RAS_EXTN)) {
215+
/* route synchronous external abort exceptions to EL2 */
216+
vcpu->arch.hcr_el2 |= HCR_TEA;
217+
/* trap error record accesses */
218+
vcpu->arch.hcr_el2 |= HCR_TERR;
219+
}
220+
221+
if (cpus_have_final_cap(ARM64_HAS_STAGE2_FWB))
222+
vcpu->arch.hcr_el2 |= HCR_FWB;
223+
224+
if (cpus_have_final_cap(ARM64_HAS_EVT) &&
225+
!cpus_have_final_cap(ARM64_MISMATCHED_CACHE_TYPE))
226+
vcpu->arch.hcr_el2 |= HCR_TID4;
227+
else
228+
vcpu->arch.hcr_el2 |= HCR_TID2;
229+
230+
if (vcpu_has_ptrauth(vcpu))
231+
vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK);
232+
}
233+
207234
/*
208235
* Initialize trap register values in protected mode.
209236
*/
210237
static void pkvm_vcpu_init_traps(struct kvm_vcpu *vcpu)
211238
{
239+
vcpu->arch.cptr_el2 = kvm_get_reset_cptr_el2(vcpu);
240+
vcpu->arch.mdcr_el2 = 0;
241+
242+
pkvm_vcpu_reset_hcr(vcpu);
243+
244+
if ((!vcpu_is_protected(vcpu)))
245+
return;
246+
212247
pvm_init_trap_regs(vcpu);
213248
pvm_init_traps_aa64pfr0(vcpu);
214249
pvm_init_traps_aa64pfr1(vcpu);

0 commit comments

Comments
 (0)