Skip to contentSkip to conversation

KVM: arm64: Fixes for the host SMCs that pKVM forwards to firmware

PATCH New Fuad Tabba 1 participant 3 messages Nov 17

Subsystems (2)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

v2 cover KVM: arm64: Fixes for the host SMCs that pKVM forwards to firmware

Hi folks,

Changes since v1 [1]:
- New patch 1, which stops forwarding the host's SDEI calls.
- Don't run a protected vCPU on a CPU where the firmware Spectre-v4 mitigation is disabled, instead of re-enabling it from EL2. (Marc)
- Track the host's ARCH_WORKAROUND_2 calls with a kvm_host_data flag instead of a new per-CPU variable. (Marc)
- Handle the calls in default_host_smc_handler(). (Marc)

Two fixes for the host SMCs that EL2 forwards in protected mode, which reach firmware as calls from EL2.

The first stops forwarding the host's SDEI calls. With TF-A, the host could otherwise register an event handler that runs at EL2.

The second is the v1 fix, redone as Marc suggested. It tracks the firmware Spectre-v4 mitigation state from the host's ARCH_WORKAROUND_2 calls, and relies on the first patch because firmware restores that state when an SDEI event completes.

Tested on QEMU with TF-A v2.15 and test-only host changes. When SDEI is built into TF-A, the host's SDEI calls succeed without the first patch and get NOT_SUPPORTED with it. With TF-A patched to advertise the dynamic Spectre-v4 workaround on the Cortex-A57 that QEMU models, a protected vCPU runs with the mitigation disabled, and the second patch stops it from running.

Based on Linux 7.4-rc2 (7475ea8b58616; also applies to Linux 7.4-rc3 aebb727447640).

Cheers,
/fuad

[1] https://lore.kernel.org/all/[email protected]/

Fuad Tabba (2):
KVM: arm64: Don't forward the host's SDEI calls to firmware
KVM: arm64: Don't run a pVM with firmware Spectre-v4 mitigation
disabled

 arch/arm64/include/asm/kvm_host.h  |  1 +
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 30 ++++++++++++++++++++++++++++--
 2 files changed, 29 insertions(+), 2 deletions(-)

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.47.3
author reviewer
#1 lore raw

PATCH v2 1/2 KVM: arm64: Don't forward the host's SDEI calls to firmware

In protected mode, EL2 forwards host SMCs that it doesn't handle to EL3, where they arrive as calls from EL2. TF-A treats EL2 as the SDEI client whenever EL2 is implemented, so the host can register an event handler that runs at EL2.

A pKVM host kernel can't use SDEI, since sdei_arch_get_entry_point() fails when the kernel is booted at EL2 but runs at EL1. Return NOT_SUPPORTED for SDEI calls instead of forwarding them.

Fixes b93c17c4185bf ("KVM: arm64: Trap host SMCs in protected mode")
---
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +9 -2
index 179d7c8277b88..37fb9896ba3ad 100644
@@ -10,6 +10,7 @@
 #include <hyp/switch.h>
 
 #include <linux/irqchip/arm-gic-v3.h>
+#include <uapi/linux/arm_sdei.h>
 #include <uapi/linux/psci.h>
 
 #include <asm/pgtable-types.h>
@@ -1647,8 +1648,14 @@ static void handle_host_hcall(struct kvm_cpu_context *host_ctxt)
 	cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
 }
 
-static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt)
+static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt, u64 func_id)
 {
+	/* Firmware would enter the host's SDEI event handlers at EL2. */
+	if ((func_id & SDEI_1_0_MASK) == SDEI_1_0_FN_BASE) {
+		cpu_reg(host_ctxt, 0) = SMCCC_RET_NOT_SUPPORTED;
+		return;
+	}
+
 	trace_hyp_exit(host_ctxt, HYP_REASON_SMC);
 	__kvm_hyp_host_forward_smc(host_ctxt);
 	trace_hyp_enter(host_ctxt, HYP_REASON_SMC);
@@ -1675,7 +1682,7 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
 	if (!handled)
 		handled = kvm_host_ffa_handler(host_ctxt, func_id);
 	if (!handled)
-		default_host_smc_handler(host_ctxt);
+		default_host_smc_handler(host_ctxt, func_id);
 
 exit_skip_instr:
 	/* SMC was trapped, move ELR past the current PC. */

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.47.3
author reviewer
#2 lore raw

PATCH v2 2/2 KVM: arm64: Don't run a pVM with firmware Spectre-v4 mitigation disabled

In reply to Fuad Tabba

EL2 handles a protected guest's ARM_SMCCC_ARCH_WORKAROUND_2 discovery query using a copy of the host's Spectre-v4 state taken at init. If the system is mitigated and the guest doesn't have SSBS, EL2 returns NOT_REQUIRED, which indicates to the guest that firmware mitigation is permanently enabled. However, EL2 forwards the host's ARM_SMCCC_ARCH_WORKAROUND_2 calls to firmware, so the host can disable the mitigation and then run a protected vCPU.

On a mitigated system, the host kernel disables the mitigation only while its own userspace runs, so a host that runs a vCPU with it disabled is misbehaving, whatever the guest was told. Track the mitigation state per CPU from the host's calls, and don't run a protected vCPU on a CPU where it's disabled. Forward the calls without hints, so that firmware decodes the same function ID as EL2.

Firmware enables the mitigation when a CPU powers up (SMCCC DEN0028, 7.6.4), so the tracked state doesn't have to be reset on CPU_ON or resume. When an SDEI event completes, firmware also restores the mitigation state of the interrupted context. EL2 can't track that, so this fix relies on the host's SDEI calls not being forwarded.

Fixes fe40ff87ead5c ("KVM: arm64: Implement HVC handling for protected guests at EL2")
---
 arch/arm64/include/asm/kvm_host.h  |  1 +
 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 19 +++++++++++++++++++
 2 files changed, 20 insertions(+)
arch/arm64/include/asm/kvm_host.h +1 -0
index 97332ffcb1300..80d0665b93f25 100644
@@ -839,6 +839,7 @@ struct kvm_host_data {
 #define KVM_HOST_DATA_FLAG_VCPU_IN_HYP_CONTEXT		4
 #define KVM_HOST_DATA_FLAG_L1_VNCR_MAPPED		5
 #define KVM_HOST_DATA_FLAG_HAS_BRBE			6
+#define KVM_HOST_DATA_FLAG_SSBD_DISABLED		7
 	unsigned long flags;
 
 	struct kvm_cpu_context host_ctxt;
arch/arm64/kvm/hyp/nvhe/hyp-main.c +19 -0
index 37fb9896ba3ad..8cc541e6e521b 100644
@@ -925,6 +925,15 @@ DEFINE_KVM_HOST_HCALL(int, __kvm_vcpu_run, struct kvm_vcpu __kern *, vcpu)
 		if (unlikely(system_supports_sme() && read_sysreg_s(SYS_SVCR)))
 			return ret;
 
+		/*
+		 * Firmware Spectre-v4 mitigation can be advertised to a
+		 * protected guest as permanent. The host kernel runs with it
+		 * enabled, so if it's disabled the host is misbehaving.
+		 */
+		if (pkvm_hyp_vcpu_is_protected(hyp_vcpu) && spectre_v4_state == SPECTRE_MITIGATED &&
+		    host_data_test_flag(SSBD_DISABLED))
+			return ret;
+
 		/*
 		 * ON has a single writer, pkvm_reset_vcpu() on this CPU, so
 		 * READ_ONCE suffices. ON_PENDING takes the reset; -ECANCELED
@@ -1656,6 +1665,16 @@ static void default_host_smc_handler(struct kvm_cpu_context *host_ctxt, u64 func
 		return;
 	}
 
+	if (func_id == ARM_SMCCC_ARCH_WORKAROUND_2) {
+		/* Drop the hints, so that firmware decodes the ID as EL2 did. */
+		cpu_reg(host_ctxt, 0) = func_id;
+
+		if (lower_32_bits(cpu_reg(host_ctxt, 1)))
+			host_data_clear_flag(SSBD_DISABLED);
+		else
+			host_data_set_flag(SSBD_DISABLED);
+	}
+
 	trace_hyp_exit(host_ctxt, HYP_REASON_SMC);
 	__kvm_hyp_host_forward_smc(host_ctxt);
 	trace_hyp_enter(host_ctxt, HYP_REASON_SMC);
Signature · 1 line
2.47.3