hypercalls.c 2.0 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// SPDX-License-Identifier: GPL-2.0
// Copyright (C) 2019 Arm Ltd.

#include <linux/arm-smccc.h>
#include <linux/kvm_host.h>

#include <asm/kvm_emulate.h>

#include <kvm/arm_hypercalls.h>
#include <kvm/arm_psci.h>

int kvm_hvc_call_handler(struct kvm_vcpu *vcpu)
{
	u32 func_id = smccc_get_function(vcpu);
15
	long val = SMCCC_RET_NOT_SUPPORTED;
16
	u32 feature;
17
	gpa_t gpa;
18 19 20 21 22 23 24 25 26

	switch (func_id) {
	case ARM_SMCCC_VERSION_FUNC_ID:
		val = ARM_SMCCC_VERSION_1_1;
		break;
	case ARM_SMCCC_ARCH_FEATURES_FUNC_ID:
		feature = smccc_get_arg1(vcpu);
		switch (feature) {
		case ARM_SMCCC_ARCH_WORKAROUND_1:
27 28
			switch (arm64_get_spectre_v2_state()) {
			case SPECTRE_VULNERABLE:
29
				break;
30
			case SPECTRE_MITIGATED:
31 32
				val = SMCCC_RET_SUCCESS;
				break;
33
			case SPECTRE_UNAFFECTED:
34
				val = SMCCC_ARCH_WORKAROUND_RET_UNAFFECTED;
35 36 37 38
				break;
			}
			break;
		case ARM_SMCCC_ARCH_WORKAROUND_2:
39 40
			switch (arm64_get_spectre_v4_state()) {
			case SPECTRE_VULNERABLE:
41
				break;
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56
			case SPECTRE_MITIGATED:
				/*
				 * SSBS everywhere: Indicate no firmware
				 * support, as the SSBS support will be
				 * indicated to the guest and the default is
				 * safe.
				 *
				 * Otherwise, expose a permanent mitigation
				 * to the guest, and hide SSBS so that the
				 * guest stays protected.
				 */
				if (cpus_have_final_cap(ARM64_SSBS))
					break;
				fallthrough;
			case SPECTRE_UNAFFECTED:
57 58 59 60
				val = SMCCC_RET_NOT_REQUIRED;
				break;
			}
			break;
61 62 63
		case ARM_SMCCC_HV_PV_TIME_FEATURES:
			val = SMCCC_RET_SUCCESS;
			break;
64 65
		}
		break;
66 67 68
	case ARM_SMCCC_HV_PV_TIME_FEATURES:
		val = kvm_hypercall_pv_features(vcpu);
		break;
69 70 71 72 73
	case ARM_SMCCC_HV_PV_TIME_ST:
		gpa = kvm_init_stolen_time(vcpu);
		if (gpa != GPA_INVALID)
			val = gpa;
		break;
74 75 76 77 78 79
	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);
80 81 82 83 84 85 86
	default:
		return kvm_psci_call(vcpu);
	}

	smccc_set_retval(vcpu, val, 0, 0, 0);
	return 1;
}