Merge branch 'kvm-arm64/rng-5.12' into kvmarm-master/next

Signed-off-by: Marc Zyngier <maz@kernel.org>
This commit is contained in:
Marc Zyngier 2021-02-12 14:08:25 +00:00
commit 8cb68a9d14
5 changed files with 125 additions and 1 deletions

View File

@ -771,4 +771,6 @@ bool kvm_arm_vcpu_is_finalized(struct kvm_vcpu *vcpu);
#define kvm_vcpu_has_pmu(vcpu) \
(test_bit(KVM_ARM_VCPU_PMU_V3, (vcpu)->arch.features))
int kvm_trng_call(struct kvm_vcpu *vcpu);
#endif /* __ARM64_KVM_HOST_H__ */

View File

@ -16,7 +16,7 @@ kvm-y := $(KVM)/kvm_main.o $(KVM)/coalesced_mmio.o $(KVM)/eventfd.o \
inject_fault.o va_layout.o handle_exit.o \
guest.o debug.o reset.o sys_regs.o \
vgic-sys-reg-v3.o fpsimd.o pmu.o \
arch_timer.o \
arch_timer.o trng.o\
vgic/vgic.o vgic/vgic-init.o \
vgic/vgic-irqfd.o vgic/vgic-v2.o \
vgic/vgic-v3.o vgic/vgic-v4.o \

View File

@ -71,6 +71,12 @@ int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
if (gpa != GPA_INVALID)
val = gpa;
break;
case ARM_SMCCC_TRNG_VERSION:
case ARM_SMCCC_TRNG_FEATURES:
case ARM_SMCCC_TRNG_GET_UUID:
case ARM_SMCCC_TRNG_RND32:
case ARM_SMCCC_TRNG_RND64:
return kvm_trng_call(vcpu);
default:
return kvm_psci_call(vcpu);
}

85
arch/arm64/kvm/trng.c Normal file
View File

@ -0,0 +1,85 @@
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2020 Arm Ltd.
#include <linux/arm-smccc.h>
#include <linux/kvm_host.h>
#include <asm/kvm_emulate.h>
#include <kvm/arm_hypercalls.h>
#define ARM_SMCCC_TRNG_VERSION_1_0 0x10000UL
/* Those values are deliberately separate from the generic SMCCC definitions. */
#define TRNG_SUCCESS 0UL
#define TRNG_NOT_SUPPORTED ((unsigned long)-1)
#define TRNG_INVALID_PARAMETER ((unsigned long)-2)
#define TRNG_NO_ENTROPY ((unsigned long)-3)
#define TRNG_MAX_BITS64 192
static const uuid_t arm_smc_trng_uuid __aligned(4) = UUID_INIT(
0x0d21e000, 0x4384, 0x11eb, 0x80, 0x70, 0x52, 0x44, 0x55, 0x4e, 0x5a, 0x4c);
static int kvm_trng_do_rnd(struct kvm_vcpu *vcpu, int size)
{
DECLARE_BITMAP(bits, TRNG_MAX_BITS64);
u32 num_bits = smccc_get_arg1(vcpu);
int i;
if (num_bits > 3 * size) {
smccc_set_retval(vcpu, TRNG_INVALID_PARAMETER, 0, 0, 0);
return 1;
}
/* get as many bits as we need to fulfil the request */
for (i = 0; i < DIV_ROUND_UP(num_bits, BITS_PER_LONG); i++)
bits[i] = get_random_long();
bitmap_clear(bits, num_bits, TRNG_MAX_BITS64 - num_bits);
if (size == 32)
smccc_set_retval(vcpu, TRNG_SUCCESS, lower_32_bits(bits[1]),
upper_32_bits(bits[0]), lower_32_bits(bits[0]));
else
smccc_set_retval(vcpu, TRNG_SUCCESS, bits[2], bits[1], bits[0]);
memzero_explicit(bits, sizeof(bits));
return 1;
}
int kvm_trng_call(struct kvm_vcpu *vcpu)
{
const __le32 *u = (__le32 *)arm_smc_trng_uuid.b;
u32 func_id = smccc_get_function(vcpu);
unsigned long val = TRNG_NOT_SUPPORTED;
int size = 64;
switch (func_id) {
case ARM_SMCCC_TRNG_VERSION:
val = ARM_SMCCC_TRNG_VERSION_1_0;
break;
case ARM_SMCCC_TRNG_FEATURES:
switch (smccc_get_arg1(vcpu)) {
case ARM_SMCCC_TRNG_VERSION:
case ARM_SMCCC_TRNG_FEATURES:
case ARM_SMCCC_TRNG_GET_UUID:
case ARM_SMCCC_TRNG_RND32:
case ARM_SMCCC_TRNG_RND64:
val = TRNG_SUCCESS;
}
break;
case ARM_SMCCC_TRNG_GET_UUID:
smccc_set_retval(vcpu, le32_to_cpu(u[0]), le32_to_cpu(u[1]),
le32_to_cpu(u[2]), le32_to_cpu(u[3]));
return 1;
case ARM_SMCCC_TRNG_RND32:
size = 32;
fallthrough;
case ARM_SMCCC_TRNG_RND64:
return kvm_trng_do_rnd(vcpu, size);
}
smccc_set_retval(vcpu, val, 0, 0, 0);
return 1;
}

View File

@ -102,6 +102,37 @@
ARM_SMCCC_OWNER_STANDARD_HYP, \
0x21)
/* TRNG entropy source calls (defined by ARM DEN0098) */
#define ARM_SMCCC_TRNG_VERSION \
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
ARM_SMCCC_SMC_32, \
ARM_SMCCC_OWNER_STANDARD, \
0x50)
#define ARM_SMCCC_TRNG_FEATURES \
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
ARM_SMCCC_SMC_32, \
ARM_SMCCC_OWNER_STANDARD, \
0x51)
#define ARM_SMCCC_TRNG_GET_UUID \
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
ARM_SMCCC_SMC_32, \
ARM_SMCCC_OWNER_STANDARD, \
0x52)
#define ARM_SMCCC_TRNG_RND32 \
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
ARM_SMCCC_SMC_32, \
ARM_SMCCC_OWNER_STANDARD, \
0x53)
#define ARM_SMCCC_TRNG_RND64 \
ARM_SMCCC_CALL_VAL(ARM_SMCCC_FAST_CALL, \
ARM_SMCCC_SMC_64, \
ARM_SMCCC_OWNER_STANDARD, \
0x53)
/*
* Return codes defined in ARM DEN 0070A
* ARM DEN 0070A is now merged/consolidated into ARM DEN 0028 C