Skip to contentSkip to conversation

x86/sev: Make guest termination reasons identifiable

PATCH New Kim Phillips 2 participants 5 messages Nov 17

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover x86/sev: Make guest termination reasons identifiable

When an SEV-ES/SNP guest dies early, the only thing that reliably escapes the guest is the GHCB MSR termination request: a 4-bit reason set and an 8-bit reason code written to the GHCB MSR before the final VMGEXIT. There is no console, no dmesg and no crash dump to fall back on - in a hosted environment the guest owner often has nothing but that one code to work with.

Today a large fraction of the termination sites in the guest-side SEV code collapse onto a handful of values that say almost nothing about what went wrong:

- Six sites terminate with the generic (SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ) pair. A #VC the decompressor could not handle, a #VC the running kernel could not handle, a GHCB that could not be set up, and a GHCB protocol negotiation that failed are all reported identically.

- do_vc_no_ghcb(), the MSR-protocol #VC handler used before a GHCB exists, funnels every one of its failure paths through a single label, so a CPUID table lookup failure, a failed hypervisor CPUID fallback and a hypervisor lying about SEV being active are indistinguishable.

- Both find_cc_blob() implementations report GHCB_SNP_UNSUPPORTED when the Confidential Computing blob they found has a bad header magic. That code otherwise means "the hypervisor does not support SNP", so a firmware or bootloader bug inside the guest's own boot chain looks exactly like an unsupported host - and points debugging at the wrong party.

The cost of fixing this is low: the Linux-specific reason set (SEV_TERM_SET_LINUX) exists precisely so the guest can report codes of its own choosing, and the existing GHCB_TERM_* codes already follow this pattern for more recently added features such as Secure TSC, SVSM and Secure AVIC. These patches extend that convention to the older, more generic sites.

Patch 1 replaces the six generic callsites, adding GHCB_TERM_VC_FAIL, GHCB_TERM_GHCB_SETUP and GHCB_TERM_NEGOTIATE. The four #VC handling failures deliberately share one code, as they are the same failure class observed at different stages of boot.

Patch 2 splits do_vc_no_ghcb()'s failure paths, reusing the existing GHCB_TERM_CPUID and GHCB_TERM_CPUID_HV codes - no new codes needed. The two checks that reject the #VC before any CPUID work is attempted keep reporting GHCB_TERM_VC_FAIL.

Patch 3 adds GHCB_TERM_CC_BLOB for the invalid-blob-magic checks, leaving the sites that genuinely mean "hypervisor does not support SNP", including the CC-blob/SNP-enablement discrepancy check in sme_enable(), on GHCB_SNP_UNSUPPORTED.

There is no functional change for a healthy guest: every modified site already terminated the guest unconditionally, and only the reported reason changes. Hypervisors that decode termination requests will see new codes in the Linux reason set; that set is explicitly reserved for exactly this, so no hypervisor can have been relying on the old values to mean anything specific.

Kim Phillips (3):
x86/sev: Report distinct termination reasons for generic requests x86/sev: Report distinct termination reasons in do_vc_no_ghcb() x86/sev: Distinguish CC-blob failures from generic SNP-unsupported

 arch/x86/boot/compressed/sev-handle-vc.c |  4 ++--
 arch/x86/boot/compressed/sev.c           |  2 +-
 arch/x86/boot/startup/sev-shared.c       | 14 +++++++++-----
 arch/x86/boot/startup/sev-startup.c      |  2 +-
 arch/x86/coco/sev/core.c                 |  2 +-
 arch/x86/coco/sev/vc-handle.c            |  4 ++--
 arch/x86/include/asm/sev-common.h        |  4 ++++
 7 files changed, 20 insertions(+), 12 deletions(-)
Signature · 1 line
2.43.0
#1 lore raw

PATCH 1/3 x86/sev: Report distinct termination reasons for generic requests

Six SEV failure sites terminate the guest with the generic (SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ) reason pair. Since the GHCB MSR termination protocol only conveys a 4-bit reason set and an 8-bit reason code to the hypervisor, all of these failures are indistinguishable from the VMM's point of view, which makes triaging a dead guest harder in environments without a working console.

Add distinct Linux-specific reason codes and switch each generic callsite to a specific (SEV_TERM_SET_LINUX, GHCB_TERM_*) pair so the failing component is identifiable from the termination code alone. The four #VC handling failures (decompressor, MSR-protocol, early boot and runtime) share a single GHCB_TERM_VC_FAIL code as they represent the same failure class; the remaining two sites use GHCB_TERM_GHCB_SETUP and GHCB_TERM_NEGOTIATE.

---
 arch/x86/boot/compressed/sev-handle-vc.c | 4 ++--
 arch/x86/boot/startup/sev-shared.c       | 2 +-
 arch/x86/coco/sev/core.c                 | 2 +-
 arch/x86/coco/sev/vc-handle.c            | 4 ++--
 arch/x86/include/asm/sev-common.h        | 3 +++
 5 files changed, 9 insertions(+), 6 deletions(-)
arch/x86/boot/compressed/sev-handle-vc.c +2 -2
index 030001b46554..8a4963193114 100644
@@ -101,7 +101,7 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
 	enum es_result result;
 
 	if (!boot_ghcb && !early_setup_ghcb())
-		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_GHCB_SETUP);
 
 	vc_ghcb_invalidate(boot_ghcb);
 	result = vc_init_em_ctxt(&ctxt, regs, exit_code);
@@ -132,5 +132,5 @@ void do_boot_stage2_vc(struct pt_regs *regs, unsigned long exit_code)
 	if (result == ES_OK)
 		vc_finish_insn(&ctxt);
 	else if (result != ES_RETRY)
-		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_VC_FAIL);
 }
arch/x86/boot/startup/sev-shared.c +1 -1
index d9ac3a929d33..1e4fef117171 100644
@@ -514,7 +514,7 @@ void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
 
 fail:
 	/* Terminate the guest */
-	sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
+	sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_VC_FAIL);
 }
 
 struct cc_setup_data {
arch/x86/coco/sev/core.c +1 -1
index eb2e853e950a..e94746b78e9a 100644
@@ -1108,7 +1108,7 @@ void setup_ghcb(void)
 	 * This gets called only in the BSP boot phase.
 	 */
 	if (!sev_es_negotiate_protocol())
-		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NEGOTIATE);
 
 	/*
 	 * Clear the boot_ghcb. The first exception comes in before the bss
arch/x86/coco/sev/vc-handle.c +2 -2
index f25d302efceb..fc390c0deb55 100644
@@ -975,7 +975,7 @@ noinstr void kernel_exc_vmm_communication(struct pt_regs *regs, unsigned long er
 		show_regs(regs);
 
 		/* Ask hypervisor to sev_es_terminate */
-		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_VC_FAIL);
 
 		/* If that fails and we get here - just panic */
 		panic("Returned from Terminate-Request to Hypervisor\n");
@@ -1067,6 +1067,6 @@ bool __init handle_vc_boot_ghcb(struct pt_regs *regs)
 fail:
 	show_regs(regs);
 
-	sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
+	sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_VC_FAIL);
 }
 
arch/x86/include/asm/sev-common.h +3 -0
index 01a6e4dbe423..3717796c4673 100644
@@ -209,6 +209,9 @@ struct snp_psc_desc {
 #define GHCB_TERM_SECURE_TSC		10	/* Secure TSC initialization failed */
 #define GHCB_TERM_SVSM_CA_REMAP_FAIL	11	/* SVSM is present but CA could not be remapped */
 #define GHCB_TERM_SAVIC_FAIL		12	/* Secure AVIC-specific failure */
+#define GHCB_TERM_VC_FAIL		13	/* #VC exception handling failure */
+#define GHCB_TERM_GHCB_SETUP		14	/* GHCB setup/mapping failure */
+#define GHCB_TERM_NEGOTIATE		15	/* HV GHCB protocol negotiation failure */
 
 #define GHCB_RESP_CODE(v)		((v) & GHCB_MSR_INFO_MASK)
 
Signature · 1 line
2.43.0
#2 lore raw

Re: [PATCH 1/3] x86/sev: Report distinct termination reasons for generic requests

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] x86/sev: redundant GHCB_TERM_NEGOTIATE reason code in setup_ghcb() --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] x86/sev: redundant GHCB_TERM_NEGOTIATE reason code in setup_ghcb()
--

commit 91d9fc8e8c2597bdeaa888f419992440ce36ac9a
Author: Kim Phillips <[email protected]>

x86/sev: Report distinct termination reasons for generic requests

This commit replaces generic (SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ) guest termination calls with specific Linux termination reason codes.

[ ... ]

Quoted 13 lines
arch/x86/coco/sev/core.c +1 -1
index cc292d7c6fd1e..12152f2fe8c9e 100644
@@ -1108,7 +1108,7 @@ void setup_ghcb(void)
 	 * This gets called only in the BSP boot phase.
 	 */
 	if (!sev_es_negotiate_protocol())
-		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_GEN_REQ);
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NEGOTIATE);
 
 	/*
 	 * Clear the boot_ghcb. The first exception comes in before the bss

[ ... ]

arch/x86/include/asm/sev-common.h struct snp_psc_desc { · line 214

Quoted 11 lines
arch/x86/include/asm/sev-common.h +3 -0
index 01a6e4dbe4235..3717796c46737 100644
@@ -209,6 +209,9 @@ struct snp_psc_desc {
 #define GHCB_TERM_SECURE_TSC		10	/* Secure TSC initialization failed */
 #define GHCB_TERM_SVSM_CA_REMAP_FAIL	11	/* SVSM is present but CA could not be remapped */
 #define GHCB_TERM_SAVIC_FAIL		12	/* Secure AVIC-specific failure */
+#define GHCB_TERM_VC_FAIL		13	/* #VC exception handling failure */
+#define GHCB_TERM_GHCB_SETUP		14	/* GHCB setup/mapping failure */
+#define GHCB_TERM_NEGOTIATE		15	/* HV GHCB protocol negotiation failure */

[Severity: Low]
Should setup_ghcb() use (SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED) instead of introducing GHCB_TERM_NEGOTIATE?

Reason set 0 already defines GHCB_SEV_ES_PROT_UNSUPPORTED for GHCB protocol negotiation failures, and sev_enable() in arch/x86/boot/compressed/sev.c uses that pair when the same sev_es_negotiate_protocol() helper fails. Using GHCB_TERM_NEGOTIATE here reports two different termination codes for the same protocol negotiation failure depending on whether it occurs in the decompressor or the core kernel.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH 2/3 x86/sev: Report distinct termination reasons in do_vc_no_ghcb()

In reply to Kim Phillips

do_vc_no_ghcb() funnels every failure through a single label, so a guest that dies in the MSR-protocol #VC handler reports GHCB_TERM_VC_FAIL whether the CPUID table lookup failed, the hypervisor fallback failed, or the hypervisor lied about SEV being active. CPUID is by far the most common early-boot #VC source, so it is worth separating out from the generic #VC failure class.

Use the existing GHCB_TERM_CPUID for the CPUID-handling failures (the snp_cpuid() error and the two SEV sanity checks on the returned values) and GHCB_TERM_CPUID_HV for a failed hypervisor fallback. The two checks that reject the #VC before any CPUID work is attempted - a non-CPUID exit-code and a non-CPUID faulting instruction - keep reporting GHCB_TERM_VC_FAIL. No new reason codes are needed.

---
 arch/x86/boot/startup/sev-shared.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
arch/x86/boot/startup/sev-shared.c +8 -4
index 1e4fef117171..9ffe04dfb28c 100644
@@ -470,14 +470,14 @@ void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
 		goto cpuid_done;
 
 	if (ret != -EOPNOTSUPP)
-		goto fail;
+		goto fail_cpuid;
 
 	/*
 	 * This is reached by a SEV-ES guest and needs to invoke the HV for
 	 * the CPUID data.
 	 */
 	if (__sev_cpuid_hv_msr(&leaf))
-		goto fail;
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID_HV);
 
 cpuid_done:
 	regs->ax = leaf.eax;
@@ -502,16 +502,20 @@ void do_vc_no_ghcb(struct pt_regs *regs, unsigned long exit_code)
 
 	if (fn == 0x80000000 && (regs->ax < 0x8000001f))
 		/* SEV leaf check */
-		goto fail;
+		goto fail_cpuid;
 	else if ((fn == 0x8000001f && !(regs->ax & BIT(1))))
 		/* SEV bit */
-		goto fail;
+		goto fail_cpuid;
 
 	/* Skip over the CPUID two-byte opcode */
 	regs->ip += 2;
 
 	return;
 
+fail_cpuid:
+	/* Terminate the guest, blaming the CPUID handling */
+	sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CPUID);
+
 fail:
 	/* Terminate the guest */
 	sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_VC_FAIL);
Signature · 1 line
2.43.0
#4 lore raw

PATCH 3/3 x86/sev: Distinguish CC-blob failures from generic SNP-unsupported

In reply to Kim Phillips

Both find_cc_blob() implementations terminate with GHCB_SNP_UNSUPPORTED when the Confidential Computing blob they located has a bad header magic. That reason code is otherwise used for a hypervisor that genuinely does not support SNP, so a corrupt or misplaced blob - a firmware or bootloader bug in the guest's own boot chain - is indistinguishable from an unsupported host.

Add a GHCB_TERM_CC_BLOB Linux-specific reason code and use it for the two invalid-blob-magic checks. The sites that truly represent an unsupported-SNP hypervisor, including the CC-blob/SNP-enablement discrepancy check in sme_enable(), are left on GHCB_SNP_UNSUPPORTED.

---
 arch/x86/boot/compressed/sev.c      | 2 +-
 arch/x86/boot/startup/sev-startup.c | 2 +-
 arch/x86/include/asm/sev-common.h   | 1 +
 3 files changed, 3 insertions(+), 2 deletions(-)
arch/x86/boot/compressed/sev.c +1 -1
index c6512f2ea31e..2deaf8bfaaa7 100644
@@ -274,7 +274,7 @@ static struct cc_blob_sev_info *find_cc_blob(struct boot_params *bp)
 
 found_cc_info:
 	if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
-		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CC_BLOB);
 
 	return cc_info;
 }
arch/x86/boot/startup/sev-startup.c +1 -1
index 789e99d38d17..75b40aefb856 100644
@@ -145,7 +145,7 @@ static struct cc_blob_sev_info *__init find_cc_blob(struct boot_params *bp)
 
 found_cc_info:
 	if (cc_info->magic != CC_BLOB_SEV_HDR_MAGIC)
-		sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
+		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_CC_BLOB);
 
 	return cc_info;
 }
arch/x86/include/asm/sev-common.h +1 -0
index 3717796c4673..ee251fa5e266 100644
@@ -212,6 +212,7 @@ struct snp_psc_desc {
 #define GHCB_TERM_VC_FAIL		13	/* #VC exception handling failure */
 #define GHCB_TERM_GHCB_SETUP		14	/* GHCB setup/mapping failure */
 #define GHCB_TERM_NEGOTIATE		15	/* HV GHCB protocol negotiation failure */
+#define GHCB_TERM_CC_BLOB		16	/* CC blob header magic is invalid */
 
 #define GHCB_RESP_CODE(v)		((v) & GHCB_MSR_INFO_MASK)
 
Signature · 1 line
2.43.0