Re: [PATCH v17 1/7] arm/arm64: Probe for the presence of KVM hypervisor

From: Marc Zyngier
Date: Fri Feb 05 2021 - 06:29:00 EST


On 2021-02-05 11:19, Will Deacon wrote:
On Fri, Feb 05, 2021 at 09:11:00AM +0000, Steven Price wrote:
On 02/02/2021 14:11, Marc Zyngier wrote:
> diff --git a/drivers/firmware/smccc/kvm_guest.c b/drivers/firmware/smccc/kvm_guest.c
> new file mode 100644
> index 000000000000..23ce1ded88b4
> --- /dev/null
> +++ b/drivers/firmware/smccc/kvm_guest.c
> @@ -0,0 +1,51 @@
> +// SPDX-License-Identifier: GPL-2.0
> +
> +#define pr_fmt(fmt) "smccc: KVM: " fmt
> +
> +#include <linux/init.h>
> +#include <linux/arm-smccc.h>
> +#include <linux/kernel.h>
> +#include <linux/string.h>
> +
> +static DECLARE_BITMAP(__kvm_arm_hyp_services, ARM_SMCCC_KVM_NUM_FUNCS) __ro_after_init = { };
> +
> +void __init kvm_init_hyp_services(void)
> +{
> + int i;
> + struct arm_smccc_res res;
> +
> + if (arm_smccc_1_1_get_conduit() != SMCCC_CONDUIT_HVC)
> + return;
> +
> + arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_CALL_UID_FUNC_ID, &res);
> + if (res.a0 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_0 ||
> + res.a1 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_1 ||
> + res.a2 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_2 ||
> + res.a3 != ARM_SMCCC_VENDOR_HYP_UID_KVM_REG_3)
> + return;
> +
> + memset(&res, 0, sizeof(res));
> + arm_smccc_1_1_invoke(ARM_SMCCC_VENDOR_HYP_KVM_FEATURES_FUNC_ID, &res);
> + for (i = 0; i < 32; ++i) {
> + if (res.a0 & (i))
> + set_bit(i + (32 * 0), __kvm_arm_hyp_services);
> + if (res.a1 & (i))
> + set_bit(i + (32 * 1), __kvm_arm_hyp_services);
> + if (res.a2 & (i))
> + set_bit(i + (32 * 2), __kvm_arm_hyp_services);
> + if (res.a3 & (i))
> + set_bit(i + (32 * 3), __kvm_arm_hyp_services);

The bit shifts are missing, the tests should be of the form:

if (res.a0 & (1 << i))

Or indeed using a BIT() macro.

Maybe even test_bit()?

yeah. I'll fix that up, thanks for pointing this out.

M.
--
Jazz is not dead. It just smells funny...