Skip to content

Commit a22fa32

Browse files
amitdanielkachhapMarc Zyngier
authored andcommitted
KVM: arm64: Add userspace flag to enable pointer authentication
Now that the building blocks of pointer authentication are present, lets add userspace flags KVM_ARM_VCPU_PTRAUTH_ADDRESS and KVM_ARM_VCPU_PTRAUTH_GENERIC. These flags will enable pointer authentication for the KVM guest on a per-vcpu basis through the ioctl KVM_ARM_VCPU_INIT. This features will allow the KVM guest to allow the handling of pointer authentication instructions or to treat them as undefined if not set. Necessary documentations are added to reflect the changes done. Reviewed-by: Dave Martin <Dave.Martin@arm.com> Signed-off-by: Amit Daniel Kachhap <amit.kachhap@arm.com> Cc: Mark Rutland <mark.rutland@arm.com> Cc: Marc Zyngier <marc.zyngier@arm.com> Cc: Christoffer Dall <christoffer.dall@arm.com> Cc: kvmarm@lists.cs.columbia.edu Signed-off-by: Marc Zyngier <marc.zyngier@arm.com>
1 parent 384b40c commit a22fa32

5 files changed

Lines changed: 58 additions & 5 deletions

File tree

‎Documentation/arm64/pointer-authentication.txt‎

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -87,7 +87,21 @@ used to get and set the keys for a thread.
8787
Virtualization
8888
--------------
8989

90-
Pointer authentication is not currently supported in KVM guests. KVM
91-
will mask the feature bits from ID_AA64ISAR1_EL1, and attempted use of
92-
the feature will result in an UNDEFINED exception being injected into
93-
the guest.
90+
Pointer authentication is enabled in KVM guest when each virtual cpu is
91+
initialised by passing flags KVM_ARM_VCPU_PTRAUTH_[ADDRESS/GENERIC] and
92+
requesting these two separate cpu features to be enabled. The current KVM
93+
guest implementation works by enabling both features together, so both
94+
these userspace flags are checked before enabling pointer authentication.
95+
The separate userspace flag will allow to have no userspace ABI changes
96+
if support is added in the future to allow these two features to be
97+
enabled independently of one another.
98+
99+
As Arm Architecture specifies that Pointer Authentication feature is
100+
implemented along with the VHE feature so KVM arm64 ptrauth code relies
101+
on VHE mode to be present.
102+
103+
Additionally, when these vcpu feature flags are not set then KVM will
104+
filter out the Pointer Authentication system key registers from
105+
KVM_GET/SET_REG_* ioctls and mask those features from cpufeature ID
106+
register. Any attempt to use the Pointer Authentication instructions will
107+
result in an UNDEFINED exception being injected into the guest.

‎Documentation/virtual/kvm/api.txt‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2761,6 +2761,16 @@ Possible features:
27612761
- KVM_ARM_VCPU_PMU_V3: Emulate PMUv3 for the CPU.
27622762
Depends on KVM_CAP_ARM_PMU_V3.
27632763

2764+
- KVM_ARM_VCPU_PTRAUTH_ADDRESS: Enables Address Pointer authentication
2765+
for arm64 only.
2766+
Both KVM_ARM_VCPU_PTRAUTH_ADDRESS and KVM_ARM_VCPU_PTRAUTH_GENERIC
2767+
must be requested or neither must be requested.
2768+
2769+
- KVM_ARM_VCPU_PTRAUTH_GENERIC: Enables Generic Pointer authentication
2770+
for arm64 only.
2771+
Both KVM_ARM_VCPU_PTRAUTH_ADDRESS and KVM_ARM_VCPU_PTRAUTH_GENERIC
2772+
must be requested or neither must be requested.
2773+
27642774
- KVM_ARM_VCPU_SVE: Enables SVE for the CPU (arm64 only).
27652775
Depends on KVM_CAP_ARM_SVE.
27662776
Requires KVM_ARM_VCPU_FINALIZE(KVM_ARM_VCPU_SVE):

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949

5050
#define KVM_MAX_VCPUS VGIC_V3_MAX_CPUS
5151

52-
#define KVM_VCPU_MAX_FEATURES 5
52+
#define KVM_VCPU_MAX_FEATURES 7
5353

5454
#define KVM_REQ_SLEEP \
5555
KVM_ARCH_REQ_FLAGS(0, KVM_REQUEST_WAIT | KVM_REQUEST_NO_WAKEUP)

‎arch/arm64/include/uapi/asm/kvm.h‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ struct kvm_regs {
104104
#define KVM_ARM_VCPU_PSCI_0_2 2 /* CPU uses PSCI v0.2 */
105105
#define KVM_ARM_VCPU_PMU_V3 3 /* Support guest PMUv3 */
106106
#define KVM_ARM_VCPU_SVE 4 /* enable SVE for this CPU */
107+
#define KVM_ARM_VCPU_PTRAUTH_ADDRESS 5 /* VCPU uses address authentication */
108+
#define KVM_ARM_VCPU_PTRAUTH_GENERIC 6 /* VCPU uses generic authentication */
107109

108110
struct kvm_vcpu_init {
109111
__u32 target;

‎arch/arm64/kvm/reset.c‎

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -221,6 +221,27 @@ static void kvm_vcpu_reset_sve(struct kvm_vcpu *vcpu)
221221
memset(vcpu->arch.sve_state, 0, vcpu_sve_state_size(vcpu));
222222
}
223223

224+
static int kvm_vcpu_enable_ptrauth(struct kvm_vcpu *vcpu)
225+
{
226+
/* Support ptrauth only if the system supports these capabilities. */
227+
if (!has_vhe())
228+
return -EINVAL;
229+
230+
if (!system_supports_address_auth() ||
231+
!system_supports_generic_auth())
232+
return -EINVAL;
233+
/*
234+
* For now make sure that both address/generic pointer authentication
235+
* features are requested by the userspace together.
236+
*/
237+
if (!test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, vcpu->arch.features) ||
238+
!test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, vcpu->arch.features))
239+
return -EINVAL;
240+
241+
vcpu->arch.flags |= KVM_ARM64_GUEST_HAS_PTRAUTH;
242+
return 0;
243+
}
244+
224245
/**
225246
* kvm_reset_vcpu - sets core registers and sys_regs to reset value
226247
* @vcpu: The VCPU pointer
@@ -261,6 +282,12 @@ int kvm_reset_vcpu(struct kvm_vcpu *vcpu)
261282
kvm_vcpu_reset_sve(vcpu);
262283
}
263284

285+
if (test_bit(KVM_ARM_VCPU_PTRAUTH_ADDRESS, vcpu->arch.features) ||
286+
test_bit(KVM_ARM_VCPU_PTRAUTH_GENERIC, vcpu->arch.features)) {
287+
if (kvm_vcpu_enable_ptrauth(vcpu))
288+
goto out;
289+
}
290+
264291
switch (vcpu->arch.target) {
265292
default:
266293
if (test_bit(KVM_ARM_VCPU_EL1_32BIT, vcpu->arch.features)) {

0 commit comments

Comments
 (0)