Skip to content

Commit 384b40c

Browse files
mrutland-armMarc Zyngier
authored andcommitted
KVM: arm/arm64: Context-switch ptrauth registers
When pointer authentication is supported, a guest may wish to use it. This patch adds the necessary KVM infrastructure for this to work, with a semi-lazy context switch of the pointer auth state. Pointer authentication feature is only enabled when VHE is built in the kernel and present in the CPU implementation so only VHE code paths are modified. When we schedule a vcpu, we disable guest usage of pointer authentication instructions and accesses to the keys. While these are disabled, we avoid context-switching the keys. When we trap the guest trying to use pointer authentication functionality, we change to eagerly context-switching the keys, and enable the feature. The next time the vcpu is scheduled out/in, we start again. However the host key save is optimized and implemented inside ptrauth instruction/register access trap. Pointer authentication consists of address authentication and generic authentication, and CPUs in a system might have varied support for either. Where support for either feature is not uniform, it is hidden from guests via ID register emulation, as a result of the cpufeature framework in the host. Unfortunately, address authentication and generic authentication cannot be trapped separately, as the architecture provides a single EL2 trap covering both. If we wish to expose one without the other, we cannot prevent a (badly-written) guest from intermittently using a feature which is not uniformly supported (when scheduled on a physical CPU which supports the relevant feature). Hence, this patch expects both type of authentication to be present in a cpu. This switch of key is done from guest enter/exit assembly as preparation for the upcoming in-kernel pointer authentication support. Hence, these key switching routines are not implemented in C code as they may cause pointer authentication key signing error in some situations. Signed-off-by: Mark Rutland <mark.rutland@arm.com> [Only VHE, key switch in full assembly, vcpu_has_ptrauth checks , save host key in ptrauth exception trap] Signed-off-by: Amit Daniel Kachhap <amit.kachhap@arm.com> Reviewed-by: Julien Thierry <julien.thierry@arm.com> Cc: Christoffer Dall <christoffer.dall@arm.com> Cc: kvmarm@lists.cs.columbia.edu [maz: various fixups] Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent b890d75 commit 384b40c

10 files changed

Lines changed: 240 additions & 18 deletions

File tree

‎arch/arm/include/asm/kvm_emulate.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -343,4 +343,6 @@ static inline unsigned long vcpu_data_host_to_guest(struct kvm_vcpu *vcpu,
343343
}
344344
}
345345

346+
static inline void vcpu_ptrauth_setup_lazy(struct kvm_vcpu *vcpu) {}
347+
346348
#endif /* __ARM_KVM_EMULATE_H__ */

‎arch/arm64/Kconfig‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1288,6 +1288,7 @@ menu "ARMv8.3 architectural features"
12881288
config ARM64_PTR_AUTH
12891289
bool "Enable support for pointer authentication"
12901290
default y
1291+
depends on !KVM || ARM64_VHE
12911292
help
12921293
Pointer authentication (part of the ARMv8.3 Extensions) provides
12931294
instructions for signing and authenticating pointers against secret
@@ -1301,8 +1302,9 @@ config ARM64_PTR_AUTH
13011302
context-switched along with the process.
13021303

13031304
The feature is detected at runtime. If the feature is not present in
1304-
hardware it will not be advertised to userspace nor will it be
1305-
enabled.
1305+
hardware it will not be advertised to userspace/KVM guest nor will it
1306+
be enabled. However, KVM guest also require VHE mode and hence
1307+
CONFIG_ARM64_VHE=y option to use this feature.
13061308

13071309
endmenu
13081310

‎arch/arm64/include/asm/kvm_emulate.h‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,22 @@ static inline void vcpu_set_wfe_traps(struct kvm_vcpu *vcpu)
9898
vcpu->arch.hcr_el2 |= HCR_TWE;
9999
}
100100

101+
static inline void vcpu_ptrauth_enable(struct kvm_vcpu *vcpu)
102+
{
103+
vcpu->arch.hcr_el2 |= (HCR_API | HCR_APK);
104+
}
105+
106+
static inline void vcpu_ptrauth_disable(struct kvm_vcpu *vcpu)
107+
{
108+
vcpu->arch.hcr_el2 &= ~(HCR_API | HCR_APK);
109+
}
110+
111+
static inline void vcpu_ptrauth_setup_lazy(struct kvm_vcpu *vcpu)
112+
{
113+
if (vcpu_has_ptrauth(vcpu))
114+
vcpu_ptrauth_disable(vcpu);
115+
}
116+
101117
static inline unsigned long vcpu_get_vsesr(struct kvm_vcpu *vcpu)
102118
{
103119
return vcpu->arch.vsesr_el2;

‎arch/arm64/include/asm/kvm_host.h‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,18 @@ enum vcpu_sysreg {
161161
PMSWINC_EL0, /* Software Increment Register */
162162
PMUSERENR_EL0, /* User Enable Register */
163163

164+
/* Pointer Authentication Registers in a strict increasing order. */
165+
APIAKEYLO_EL1,
166+
APIAKEYHI_EL1,
167+
APIBKEYLO_EL1,
168+
APIBKEYHI_EL1,
169+
APDAKEYLO_EL1,
170+
APDAKEYHI_EL1,
171+
APDBKEYLO_EL1,
172+
APDBKEYHI_EL1,
173+
APGAKEYLO_EL1,
174+
APGAKEYHI_EL1,
175+
164176
/* 32bit specific registers. Keep them at the end of the range */
165177
DACR32_EL2, /* Domain Access Control Register */
166178
IFSR32_EL2, /* Instruction Fault Status Register */
@@ -530,6 +542,8 @@ static inline bool kvm_arch_requires_vhe(void)
530542
return false;
531543
}
532544

545+
void kvm_arm_vcpu_ptrauth_trap(struct kvm_vcpu *vcpu);
546+
533547
static inline void kvm_arch_hardware_unsetup(void) {}
534548
static inline void kvm_arch_sync_events(struct kvm *kvm) {}
535549
static inline void kvm_arch_sched_in(struct kvm_vcpu *vcpu, int cpu) {}
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
/* SPDX-License-Identifier: GPL-2.0 */
2+
/* arch/arm64/include/asm/kvm_ptrauth.h: Guest/host ptrauth save/restore
3+
* Copyright 2019 Arm Limited
4+
* Authors: Mark Rutland <mark.rutland@arm.com>
5+
* Amit Daniel Kachhap <amit.kachhap@arm.com>
6+
*/
7+
8+
#ifndef __ASM_KVM_PTRAUTH_H
9+
#define __ASM_KVM_PTRAUTH_H
10+
11+
#ifdef __ASSEMBLY__
12+
13+
#include <asm/sysreg.h>
14+
15+
#ifdef CONFIG_ARM64_PTR_AUTH
16+
17+
#define PTRAUTH_REG_OFFSET(x) (x - CPU_APIAKEYLO_EL1)
18+
19+
/*
20+
* CPU_AP*_EL1 values exceed immediate offset range (512) for stp
21+
* instruction so below macros takes CPU_APIAKEYLO_EL1 as base and
22+
* calculates the offset of the keys from this base to avoid an extra add
23+
* instruction. These macros assumes the keys offsets follow the order of
24+
* the sysreg enum in kvm_host.h.
25+
*/
26+
.macro ptrauth_save_state base, reg1, reg2
27+
mrs_s \reg1, SYS_APIAKEYLO_EL1
28+
mrs_s \reg2, SYS_APIAKEYHI_EL1
29+
stp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APIAKEYLO_EL1)]
30+
mrs_s \reg1, SYS_APIBKEYLO_EL1
31+
mrs_s \reg2, SYS_APIBKEYHI_EL1
32+
stp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APIBKEYLO_EL1)]
33+
mrs_s \reg1, SYS_APDAKEYLO_EL1
34+
mrs_s \reg2, SYS_APDAKEYHI_EL1
35+
stp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APDAKEYLO_EL1)]
36+
mrs_s \reg1, SYS_APDBKEYLO_EL1
37+
mrs_s \reg2, SYS_APDBKEYHI_EL1
38+
stp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APDBKEYLO_EL1)]
39+
mrs_s \reg1, SYS_APGAKEYLO_EL1
40+
mrs_s \reg2, SYS_APGAKEYHI_EL1
41+
stp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APGAKEYLO_EL1)]
42+
.endm
43+
44+
.macro ptrauth_restore_state base, reg1, reg2
45+
ldp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APIAKEYLO_EL1)]
46+
msr_s SYS_APIAKEYLO_EL1, \reg1
47+
msr_s SYS_APIAKEYHI_EL1, \reg2
48+
ldp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APIBKEYLO_EL1)]
49+
msr_s SYS_APIBKEYLO_EL1, \reg1
50+
msr_s SYS_APIBKEYHI_EL1, \reg2
51+
ldp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APDAKEYLO_EL1)]
52+
msr_s SYS_APDAKEYLO_EL1, \reg1
53+
msr_s SYS_APDAKEYHI_EL1, \reg2
54+
ldp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APDBKEYLO_EL1)]
55+
msr_s SYS_APDBKEYLO_EL1, \reg1
56+
msr_s SYS_APDBKEYHI_EL1, \reg2
57+
ldp \reg1, \reg2, [\base, #PTRAUTH_REG_OFFSET(CPU_APGAKEYLO_EL1)]
58+
msr_s SYS_APGAKEYLO_EL1, \reg1
59+
msr_s SYS_APGAKEYHI_EL1, \reg2
60+
.endm
61+
62+
/*
63+
* Both ptrauth_switch_to_guest and ptrauth_switch_to_host macros will
64+
* check for the presence of one of the cpufeature flag
65+
* ARM64_HAS_ADDRESS_AUTH_ARCH or ARM64_HAS_ADDRESS_AUTH_IMP_DEF and
66+
* then proceed ahead with the save/restore of Pointer Authentication
67+
* key registers.
68+
*/
69+
.macro ptrauth_switch_to_guest g_ctxt, reg1, reg2, reg3
70+
alternative_if ARM64_HAS_ADDRESS_AUTH_ARCH
71+
b 1000f
72+
alternative_else_nop_endif
73+
alternative_if_not ARM64_HAS_ADDRESS_AUTH_IMP_DEF
74+
b 1001f
75+
alternative_else_nop_endif
76+
1000:
77+
ldr \reg1, [\g_ctxt, #(VCPU_HCR_EL2 - VCPU_CONTEXT)]
78+
and \reg1, \reg1, #(HCR_API | HCR_APK)
79+
cbz \reg1, 1001f
80+
add \reg1, \g_ctxt, #CPU_APIAKEYLO_EL1
81+
ptrauth_restore_state \reg1, \reg2, \reg3
82+
1001:
83+
.endm
84+
85+
.macro ptrauth_switch_to_host g_ctxt, h_ctxt, reg1, reg2, reg3
86+
alternative_if ARM64_HAS_ADDRESS_AUTH_ARCH
87+
b 2000f
88+
alternative_else_nop_endif
89+
alternative_if_not ARM64_HAS_ADDRESS_AUTH_IMP_DEF
90+
b 2001f
91+
alternative_else_nop_endif
92+
2000:
93+
ldr \reg1, [\g_ctxt, #(VCPU_HCR_EL2 - VCPU_CONTEXT)]
94+
and \reg1, \reg1, #(HCR_API | HCR_APK)
95+
cbz \reg1, 2001f
96+
add \reg1, \g_ctxt, #CPU_APIAKEYLO_EL1
97+
ptrauth_save_state \reg1, \reg2, \reg3
98+
add \reg1, \h_ctxt, #CPU_APIAKEYLO_EL1
99+
ptrauth_restore_state \reg1, \reg2, \reg3
100+
isb
101+
2001:
102+
.endm
103+
104+
#else /* !CONFIG_ARM64_PTR_AUTH */
105+
.macro ptrauth_switch_to_guest g_ctxt, reg1, reg2, reg3
106+
.endm
107+
.macro ptrauth_switch_to_host g_ctxt, h_ctxt, reg1, reg2, reg3
108+
.endm
109+
#endif /* CONFIG_ARM64_PTR_AUTH */
110+
#endif /* __ASSEMBLY__ */
111+
#endif /* __ASM_KVM_PTRAUTH_H */

‎arch/arm64/kernel/asm-offsets.c‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,13 @@ int main(void)
125125
DEFINE(VCPU_CONTEXT, offsetof(struct kvm_vcpu, arch.ctxt));
126126
DEFINE(VCPU_FAULT_DISR, offsetof(struct kvm_vcpu, arch.fault.disr_el1));
127127
DEFINE(VCPU_WORKAROUND_FLAGS, offsetof(struct kvm_vcpu, arch.workaround_flags));
128+
DEFINE(VCPU_HCR_EL2, offsetof(struct kvm_vcpu, arch.hcr_el2));
128129
DEFINE(CPU_GP_REGS, offsetof(struct kvm_cpu_context, gp_regs));
130+
DEFINE(CPU_APIAKEYLO_EL1, offsetof(struct kvm_cpu_context, sys_regs[APIAKEYLO_EL1]));
131+
DEFINE(CPU_APIBKEYLO_EL1, offsetof(struct kvm_cpu_context, sys_regs[APIBKEYLO_EL1]));
132+
DEFINE(CPU_APDAKEYLO_EL1, offsetof(struct kvm_cpu_context, sys_regs[APDAKEYLO_EL1]));
133+
DEFINE(CPU_APDBKEYLO_EL1, offsetof(struct kvm_cpu_context, sys_regs[APDBKEYLO_EL1]));
134+
DEFINE(CPU_APGAKEYLO_EL1, offsetof(struct kvm_cpu_context, sys_regs[APGAKEYLO_EL1]));
129135
DEFINE(CPU_USER_PT_REGS, offsetof(struct kvm_regs, regs));
130136
DEFINE(HOST_CONTEXT_VCPU, offsetof(struct kvm_cpu_context, __hyp_running_vcpu));
131137
#endif

‎arch/arm64/kvm/handle_exit.c‎

Lines changed: 28 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -173,20 +173,40 @@ static int handle_sve(struct kvm_vcpu *vcpu, struct kvm_run *run)
173173
return 1;
174174
}
175175

176+
#define __ptrauth_save_key(regs, key) \
177+
({ \
178+
regs[key ## KEYLO_EL1] = read_sysreg_s(SYS_ ## key ## KEYLO_EL1); \
179+
regs[key ## KEYHI_EL1] = read_sysreg_s(SYS_ ## key ## KEYHI_EL1); \
180+
})
181+
182+
/*
183+
* Handle the guest trying to use a ptrauth instruction, or trying to access a
184+
* ptrauth register.
185+
*/
186+
void kvm_arm_vcpu_ptrauth_trap(struct kvm_vcpu *vcpu)
187+
{
188+
struct kvm_cpu_context *ctxt;
189+
190+
if (vcpu_has_ptrauth(vcpu)) {
191+
vcpu_ptrauth_enable(vcpu);
192+
ctxt = vcpu->arch.host_cpu_context;
193+
__ptrauth_save_key(ctxt->sys_regs, APIA);
194+
__ptrauth_save_key(ctxt->sys_regs, APIB);
195+
__ptrauth_save_key(ctxt->sys_regs, APDA);
196+
__ptrauth_save_key(ctxt->sys_regs, APDB);
197+
__ptrauth_save_key(ctxt->sys_regs, APGA);
198+
} else {
199+
kvm_inject_undefined(vcpu);
200+
}
201+
}
202+
176203
/*
177204
* Guest usage of a ptrauth instruction (which the guest EL1 did not turn into
178205
* a NOP).
179206
*/
180207
static int kvm_handle_ptrauth(struct kvm_vcpu *vcpu, struct kvm_run *run)
181208
{
182-
/*
183-
* We don't currently support ptrauth in a guest, and we mask the ID
184-
* registers to prevent well-behaved guests from trying to make use of
185-
* it.
186-
*
187-
* Inject an UNDEF, as if the feature really isn't present.
188-
*/
189-
kvm_inject_undefined(vcpu);
209+
kvm_arm_vcpu_ptrauth_trap(vcpu);
190210
return 1;
191211
}
192212

‎arch/arm64/kvm/hyp/entry.S‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include <asm/kvm_arm.h>
2525
#include <asm/kvm_asm.h>
2626
#include <asm/kvm_mmu.h>
27+
#include <asm/kvm_ptrauth.h>
2728

2829
#define CPU_GP_REG_OFFSET(x) (CPU_GP_REGS + x)
2930
#define CPU_XREG_OFFSET(x) CPU_GP_REG_OFFSET(CPU_USER_PT_REGS + 8*x)
@@ -64,6 +65,13 @@ ENTRY(__guest_enter)
6465

6566
add x18, x0, #VCPU_CONTEXT
6667

68+
// Macro ptrauth_switch_to_guest format:
69+
// ptrauth_switch_to_guest(guest cxt, tmp1, tmp2, tmp3)
70+
// The below macro to restore guest keys is not implemented in C code
71+
// as it may cause Pointer Authentication key signing mismatch errors
72+
// when this feature is enabled for kernel code.
73+
ptrauth_switch_to_guest x18, x0, x1, x2
74+
6775
// Restore guest regs x0-x17
6876
ldp x0, x1, [x18, #CPU_XREG_OFFSET(0)]
6977
ldp x2, x3, [x18, #CPU_XREG_OFFSET(2)]
@@ -118,6 +126,13 @@ ENTRY(__guest_exit)
118126

119127
get_host_ctxt x2, x3
120128

129+
// Macro ptrauth_switch_to_guest format:
130+
// ptrauth_switch_to_host(guest cxt, host cxt, tmp1, tmp2, tmp3)
131+
// The below macro to save/restore keys is not implemented in C code
132+
// as it may cause Pointer Authentication key signing mismatch errors
133+
// when this feature is enabled for kernel code.
134+
ptrauth_switch_to_host x1, x2, x3, x4, x5
135+
121136
// Now restore the host regs
122137
restore_callee_saved_regs x2
123138

‎arch/arm64/kvm/sys_regs.c‎

Lines changed: 42 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1007,6 +1007,37 @@ static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
10071007
{ SYS_DESC(SYS_PMEVTYPERn_EL0(n)), \
10081008
access_pmu_evtyper, reset_unknown, (PMEVTYPER0_EL0 + n), }
10091009

1010+
static bool trap_ptrauth(struct kvm_vcpu *vcpu,
1011+
struct sys_reg_params *p,
1012+
const struct sys_reg_desc *rd)
1013+
{
1014+
kvm_arm_vcpu_ptrauth_trap(vcpu);
1015+
1016+
/*
1017+
* Return false for both cases as we never skip the trapped
1018+
* instruction:
1019+
*
1020+
* - Either we re-execute the same key register access instruction
1021+
* after enabling ptrauth.
1022+
* - Or an UNDEF is injected as ptrauth is not supported/enabled.
1023+
*/
1024+
return false;
1025+
}
1026+
1027+
static unsigned int ptrauth_visibility(const struct kvm_vcpu *vcpu,
1028+
const struct sys_reg_desc *rd)
1029+
{
1030+
return vcpu_has_ptrauth(vcpu) ? 0 : REG_HIDDEN_USER | REG_HIDDEN_GUEST;
1031+
}
1032+
1033+
#define __PTRAUTH_KEY(k) \
1034+
{ SYS_DESC(SYS_## k), trap_ptrauth, reset_unknown, k, \
1035+
.visibility = ptrauth_visibility}
1036+
1037+
#define PTRAUTH_KEY(k) \
1038+
__PTRAUTH_KEY(k ## KEYLO_EL1), \
1039+
__PTRAUTH_KEY(k ## KEYHI_EL1)
1040+
10101041
static bool access_arch_timer(struct kvm_vcpu *vcpu,
10111042
struct sys_reg_params *p,
10121043
const struct sys_reg_desc *r)
@@ -1053,14 +1084,11 @@ static u64 read_id_reg(const struct kvm_vcpu *vcpu,
10531084

10541085
if (id == SYS_ID_AA64PFR0_EL1 && !vcpu_has_sve(vcpu)) {
10551086
val &= ~(0xfUL << ID_AA64PFR0_SVE_SHIFT);
1056-
} else if (id == SYS_ID_AA64ISAR1_EL1) {
1057-
const u64 ptrauth_mask = (0xfUL << ID_AA64ISAR1_APA_SHIFT) |
1058-
(0xfUL << ID_AA64ISAR1_API_SHIFT) |
1059-
(0xfUL << ID_AA64ISAR1_GPA_SHIFT) |
1060-
(0xfUL << ID_AA64ISAR1_GPI_SHIFT);
1061-
if (val & ptrauth_mask)
1062-
kvm_debug("ptrauth unsupported for guests, suppressing\n");
1063-
val &= ~ptrauth_mask;
1087+
} else if (id == SYS_ID_AA64ISAR1_EL1 && !vcpu_has_ptrauth(vcpu)) {
1088+
val &= ~(0xfUL << ID_AA64ISAR1_APA_SHIFT) |
1089+
(0xfUL << ID_AA64ISAR1_API_SHIFT) |
1090+
(0xfUL << ID_AA64ISAR1_GPA_SHIFT) |
1091+
(0xfUL << ID_AA64ISAR1_GPI_SHIFT);
10641092
}
10651093

10661094
return val;
@@ -1460,6 +1488,12 @@ static const struct sys_reg_desc sys_reg_descs[] = {
14601488
{ SYS_DESC(SYS_TTBR1_EL1), access_vm_reg, reset_unknown, TTBR1_EL1 },
14611489
{ SYS_DESC(SYS_TCR_EL1), access_vm_reg, reset_val, TCR_EL1, 0 },
14621490

1491+
PTRAUTH_KEY(APIA),
1492+
PTRAUTH_KEY(APIB),
1493+
PTRAUTH_KEY(APDA),
1494+
PTRAUTH_KEY(APDB),
1495+
PTRAUTH_KEY(APGA),
1496+
14631497
{ SYS_DESC(SYS_AFSR0_EL1), access_vm_reg, reset_unknown, AFSR0_EL1 },
14641498
{ SYS_DESC(SYS_AFSR1_EL1), access_vm_reg, reset_unknown, AFSR1_EL1 },
14651499
{ SYS_DESC(SYS_ESR_EL1), access_vm_reg, reset_unknown, ESR_EL1 },

‎virt/kvm/arm/arm.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -385,6 +385,8 @@ void kvm_arch_vcpu_load(struct kvm_vcpu *vcpu, int cpu)
385385
vcpu_clear_wfe_traps(vcpu);
386386
else
387387
vcpu_set_wfe_traps(vcpu);
388+
389+
vcpu_ptrauth_setup_lazy(vcpu);
388390
}
389391

390392
void kvm_arch_vcpu_put(struct kvm_vcpu *vcpu)

0 commit comments

Comments
 (0)