Skip to contentSkip to conversation

KVM: arm64: Keep firmware Spectre-v4 mitigation on for protected guests

PATCH In review Fuad Tabba 2 participants 3 messages Nov 13 – Nov 16

Subsystems (2)
lore
Share & tools
lore

Fuad Tabba

author reviewer
#0 lore raw

PATCH 1/1 KVM: arm64: Keep firmware Spectre-v4 mitigation on for protected guests

EL2 handles a protected guest's ARM_SMCCC_ARCH_WORKAROUND_2 discovery query from the host's Spectre-v4 state at init. If the system is mitigated and the guest doesn't have SSBS, it returns NOT_REQUIRED, which means that firmware mitigation is permanently enabled. However, the host can disable the mitigation before running a protected vCPU, with an ARM_SMCCC_ARCH_WORKAROUND_2 call that EL2 forwards to the firmware.

The host makes that call for its own userspace, so keep forwarding it, without hints so that the firmware decodes it as EL2 does. Track it per CPU, and on a mitigated system enable the mitigation again before running a protected vCPU on a CPU where the host has disabled it. Firmware enables it when a CPU powers up, so the tracked state isn't reset on CPU_ON or resume.

Fixes fe40ff87ead5c ("KVM: arm64: Implement HVC handling for protected guests at EL2")

---

Notes:
Tested on QEMU with TF-A. Upstream TF-A implements the dynamic workaround only for Cortex-A76, so I patched it to advertise the workaround on the Cortex-A57 that QEMU models. With a host that disables the mitigation before running a protected vCPU, TF-A gets the call to enable it again from EL2 with this patch, and doesn't without it. TF-A's handler for the call does nothing there and QEMU doesn't model the mitigation, so this tests the calls and not their effect.

Calling the firmware before every protected vCPU run would do without the tracking, at the cost of an SMC per run.

Based on Linux 7.4-rc2 (7475ea8b58616).

 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +20 -0
index 179d7c8277b88..081a8780fa3b7 100644
@@ -21,6 +21,7 @@
 #include <asm/kvm_mmu.h>
 
 #include <nvhe/alloc.h>
+#include <nvhe/arm-smccc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>
@@ -30,6 +31,8 @@
 
 DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
 
+static DEFINE_PER_CPU(bool, host_ssbd_disabled);
+
 /*
  * Define a hypercall handler: handle_<name> unmarshals the arguments from
  * the host context and hands them, correctly typed, to the body that
@@ -940,6 +943,17 @@ DEFINE_KVM_HOST_HCALL(int, __kvm_vcpu_run, struct kvm_vcpu __kern *, vcpu)
 			return ret;
 		}
 
+		/*
+		 * Firmware Spectre-v4 mitigation can be advertised to a
+		 * protected guest as permanent. The host kernel runs with it
+		 * enabled, so nothing is restored on exit.
+		 */
+		if (pkvm_hyp_vcpu_is_protected(hyp_vcpu) && spectre_v4_state == SPECTRE_MITIGATED &&
+		    __this_cpu_read(host_ssbd_disabled)) {
+			hyp_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, true, NULL);
+			__this_cpu_write(host_ssbd_disabled, false);
+		}
+
 		flush_hyp_vcpu(hyp_vcpu);
 
 		ret = __kvm_vcpu_run(&hyp_vcpu->vcpu);
@@ -1671,6 +1685,12 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
 		goto exit_skip_instr;
 	}
 
+	if (func_id == ARM_SMCCC_ARCH_WORKAROUND_2) {
+		/* Drop the hints, so that the firmware decodes the ID as EL2 did. */
+		cpu_reg(host_ctxt, 0) = func_id;
+		__this_cpu_write(host_ssbd_disabled, !lower_32_bits(cpu_reg(host_ctxt, 1)));
+	}
+
 	handled = kvm_host_psci_handler(host_ctxt, func_id);
 	if (!handled)
 		handled = kvm_host_ffa_handler(host_ctxt, func_id);

base-commit: 7475ea8b58616b33c04afb976cf0fbaad9dd63fb

Signature · 1 line
2.47.3
maintainer
#1 lore raw
Quoted 52 lines · Fuad Tabba

EL2 handles a protected guest's ARM_SMCCC_ARCH_WORKAROUND_2 discovery query from the host's Spectre-v4 state at init. If the system is mitigated and the guest doesn't have SSBS, it returns NOT_REQUIRED, which means that firmware mitigation is permanently enabled. However, the host can disable the mitigation before running a protected vCPU, with an ARM_SMCCC_ARCH_WORKAROUND_2 call that EL2 forwards to the firmware.

The host makes that call for its own userspace, so keep forwarding it, without hints so that the firmware decodes it as EL2 does. Track it per CPU, and on a mitigated system enable the mitigation again before running a protected vCPU on a CPU where the host has disabled it. Firmware enables it when a CPU powers up, so the tracked state isn't reset on CPU_ON or resume.

Fixes fe40ff87ead5c ("KVM: arm64: Implement HVC handling for protected guests at EL2")

---

Notes:
Tested on QEMU with TF-A. Upstream TF-A implements the dynamic workaround only for Cortex-A76, so I patched it to advertise the workaround on the Cortex-A57 that QEMU models. With a host that disables the mitigation before running a protected vCPU, TF-A gets the call to enable it again from EL2 with this patch, and doesn't without it. TF-A's handler for the call does nothing there and QEMU doesn't model the mitigation, so this tests the calls and not their effect.

Calling the firmware before every protected vCPU run would do without the tracking, at the cost of an SMC per run.

Based on Linux 7.4-rc2 (7475ea8b58616).

 arch/arm64/kvm/hyp/nvhe/hyp-main.c | 20 ++++++++++++++++++++
 1 file changed, 20 insertions(+)
arch/arm64/kvm/hyp/nvhe/hyp-main.c +1 -0
index 179d7c8277b88..081a8780fa3b7 100644
@@ -21,6 +21,7 @@
 #include <asm/kvm_mmu.h>
 
 #include <nvhe/alloc.h>
+#include <nvhe/arm-smccc.h>
 #include <nvhe/ffa.h>
 #include <nvhe/mem_protect.h>
 #include <nvhe/mm.h>

arch/arm64/kvm/hyp/nvhe/hyp-main.c line 35

Quoted 5 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -0
@@ -30,6 +31,8 @@
 
 DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
 
+static DEFINE_PER_CPU(bool, host_ssbd_disabled);
+

Why do we need yet another set of per-CPU booleans to track the host state that is supposed to be stored in kvm_host_data? We already have a metric bazillion of flags for any odd state tracking, and I really can't see why this should be an exception.

arch/arm64/kvm/hyp/nvhe/hyp-main.c DEFINE_KVM_HOST_HCALL(int, __kvm_vcpu_run, struct kvm_vcpu __kern *, vcpu) · line 949

Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4 -0
 /*
  * Define a hypercall handler: handle_<name> unmarshals the arguments from
  * the host context and hands them, correctly typed, to the body that
@@ -940,6 +943,17 @@ DEFINE_KVM_HOST_HCALL(int, __kvm_vcpu_run, struct kvm_vcpu __kern *, vcpu)
 			return ret;
 		}
 
+		/*
+		 * Firmware Spectre-v4 mitigation can be advertised to a
+		 * protected guest as permanent. The host kernel runs with it
+		 * enabled, so nothing is restored on exit.

I don't understand this. If the host is supposed to run with the mitigation enabled, how did we get here with the mitigation disabled? It's not like we can get here directly from userspace...

So either the reasoning in the comment is wrong (and therefore needs fixing), or this is a buggy (malicious?) host that tries to trick the hypervisor (and then I question whether we should even try to continue running the guest).

Quoted 11 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +7 -0
+		 */
+		if (pkvm_hyp_vcpu_is_protected(hyp_vcpu) && spectre_v4_state == SPECTRE_MITIGATED &&
+		    __this_cpu_read(host_ssbd_disabled)) {
+			hyp_smccc_1_1_smc(ARM_SMCCC_ARCH_WORKAROUND_2, true, NULL);
+			__this_cpu_write(host_ssbd_disabled, false);
+		}
+
 		flush_hyp_vcpu(hyp_vcpu);
 
 		ret = __kvm_vcpu_run(&hyp_vcpu->vcpu);

arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle_host_smc(struct kvm_cpu_context *host_ctxt) · line 1693

Quoted 9 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6 -0
@@ -1671,6 +1685,12 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
 		goto exit_skip_instr;
 	}
 
+	if (func_id == ARM_SMCCC_ARCH_WORKAROUND_2) {
+		/* Drop the hints, so that the firmware decodes the ID as EL2 did. */
+		cpu_reg(host_ctxt, 0) = func_id;
+		__this_cpu_write(host_ssbd_disabled, !lower_32_bits(cpu_reg(host_ctxt, 1)));
+	}
+

This should be pushed into default_host_smc_handler() rather than left dangling here (it is pretty obvious that it has nothing to do with PSCI or FFA).

arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle_host_smc(struct kvm_cpu_context *host_ctxt) · line 1696

Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
 	handled = kvm_host_psci_handler(host_ctxt, func_id);
 	if (!handled)
 		handled = kvm_host_ffa_handler(host_ctxt, func_id);

Thanks,

	M.
Signature · 1 line
Jazz isn't dead. It just smells funny.
author reviewer
#2 lore raw

Hi Marc,

On Sat, 14 Nov 2026 15:33:44 +0000, Marc Zyngier <[email protected]> wrote:
[...]

Quoted 1 line
Quoted 1 line
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0

[...]

Quoted 11 lines

arch/arm64/kvm/hyp/nvhe/hyp-main.c line 35

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +2 -0
@@ -30,6 +31,8 @@
 
 DEFINE_PER_CPU(struct kvm_nvhe_init_params, kvm_init_params);
 
+static DEFINE_PER_CPU(bool, host_ssbd_disabled);
+

Why do we need yet another set of per-CPU booleans to track the host state that is supposed to be stored in kvm_host_data? We already have a metric bazillion of flags for any odd state tracking, and I really can't see why this should be an exception.

I'll use a kvm_host_data flag in v2.

[...]

Quoted 13 lines

arch/arm64/kvm/hyp/nvhe/hyp-main.c DEFINE_KVM_HOST_HCALL(int, __kvm_vcpu_run, struct kvm_vcpu __kern *, vcpu) · line 949

Quoted 8 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +4 -0
@@ -940,6 +943,17 @@ DEFINE_KVM_HOST_HCALL(int, __kvm_vcpu_run, struct kvm_vcpu __kern *, vcpu)
                         return ret;
                 }
 
+                /*
+                 * Firmware Spectre-v4 mitigation can be advertised to a
+                 * protected guest as permanent. The host kernel runs with it
+                 * enabled, so nothing is restored on exit.

I don't understand this. If the host is supposed to run with the mitigation enabled, how did we get here with the mitigation disabled? It's not like we can get here directly from userspace...

Quoted 4 lines

So either the reasoning in the comment is wrong (and therefore needs fixing), or this is a buggy (malicious?) host that tries to trick the hypervisor (and then I question whether we should even try to continue running the guest).

It's the second. The host kernel enables the mitigation on every entry from userspace, so only a misbehaving host gets here with it disabled. I'll say so in the comment.

I had EL2 enable it again so that the NOT_REQUIRED it returns to the guest holds whatever the host does, but failing the run does that too. I'll do that in v2, like the SVCR check above.

[...]

Quoted 4 lines
Quoted 4 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +0 -0
@@ -1671,6 +1685,12 @@ static void handle_host_smc(struct kvm_cpu_context *host_ctxt)
                 goto exit_skip_instr;
         }
 
Quoted 10 lines

arch/arm64/kvm/hyp/nvhe/hyp-main.c static void handle_host_smc(struct kvm_cpu_context *host_ctxt) · line 1693

Quoted 6 lines
arch/arm64/kvm/hyp/nvhe/hyp-main.c +6 -0
+        if (func_id == ARM_SMCCC_ARCH_WORKAROUND_2) {
+                /* Drop the hints, so that the firmware decodes the ID as EL2 did. */
+                cpu_reg(host_ctxt, 0) = func_id;
+                __this_cpu_write(host_ssbd_disabled, !lower_32_bits(cpu_reg(host_ctxt, 1)));
+        }
+

This should be pushed into default_host_smc_handler() rather than left dangling here (it is pretty obvious that it has nothing to do with PSCI or FFA).

I put it there since that's where the function ID is decoded, but I'll move it into default_host_smc_handler() and pass the ID in.

Cheers,
/fuad