Skip to contentSkip to conversation

x86/sev: Add SEV-SNP guest feature negotiation support

PATCH Superseded Nikunj A Dadhania 3 participants 6 messages Dec 1, 2022 – Dec 5, 2022

Subsystems (2)

v2 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 x86/sev: Add SEV-SNP guest feature negotiation support

The hypervisor can enable various new features (SEV_FEATURES[1:63]) and start the SNP guest. Some of these features need guest side implementation. If any of these features are enabled without guest side implementation, the behavior of the SNP guest will be undefined. The SNP guest boot may fail in a non-obvious way making it difficult to debug.

Instead of allowing the guest to continue and have it fail randomly later, detect this early and fail gracefully.

SEV_STATUS MSR indicates features which hypervisor has enabled. While booting, SNP guests should ascertain that all the enabled features have guest side implementation. In case any feature is not implemented in the guest, the guest terminates booting with SNP feature unsupported exit code.

The below table lists the expected guest behavior with various possible scenarios of guest/hypervisor SNP feature support.

+---------------+---------------+---------------+---------------+
|Feature Enabled|  Guest needs  |   Guest has   |  Guest boot   |
|     by HV     |implementation |implementation |   behavior    |
+---------------+---------------+---------------+---------------+
|      No       |      No       |      No       |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      No       |      Yes      |      No       |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      No       |      Yes      |      Yes      |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      Yes      |      No       |      No       |   Boot with   |
|               |               |               |feature enabled|
+---------------+---------------+---------------+---------------+
|      Yes      |      Yes      |      No       | Graceful Boot |
|               |               |               |    Failure    |
+---------------+---------------+---------------+---------------+
|      Yes      |      Yes      |      Yes      |   Boot with   |
|               |               |               |feature enabled|
+---------------+---------------+---------------+---------------+

More details in AMD64 APM[1] Vol 2: 15.34.10 SEV_STATUS MSR

[1] https://www.amd.com/system/files/TechDocs/40332_4.05.pdf

Fixes cbd3d4f7c4e5 ("x86/sev: Check SEV-SNP features support")
CC Borislav Petkov <[email protected]>
CC Michael Roth <[email protected]>
CC Tom Lendacky <[email protected]>

---

Changes:
v1:
* Dropped _ENABLED from the feature bits
* Use approprate macro/function names and move closer to the function where it is used.
* More details added to the commit message and comments
* Fixed compilation issue

---
 arch/x86/boot/compressed/sev.c    | 51 +++++++++++++++++++++++++++++++
 arch/x86/include/asm/msr-index.h  | 20 ++++++++++++
 arch/x86/include/asm/sev-common.h |  1 +
 3 files changed, 72 insertions(+)
arch/x86/boot/compressed/sev.c +51 -0
index c93930d5ccbd..571eb2576475 100644
@@ -270,6 +270,50 @@ static void enforce_vmpl0(void)
 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
 }
 
+/*
+ * SNP_FEATURES_NEED_GUEST_IMPLEMENTATION is the mask of SNP features that
+ * will need guest side implementation for proper functioning of the guest.
+ * If any of these features are enabled without guest side implementation,
+ * the behavior of the guest will be undefined. The guest may fail in
+ * non-obvious way making it difficult to debug.
+ *
+ * SNP reserved feature bits may or may not need guest side implementation.
+ * As the behavior of reserved feature bits are unknown, to be on the safer
+ * side add them to the NEED_GUEST_IMPLEMENTATION mask.
+ */
+#define SNP_FEATURES_NEED_GUEST_IMPLEMENTATION (MSR_AMD64_SNP_VTOM |			\
+						MSR_AMD64_SNP_REFLECT_VC |		\
+						MSR_AMD64_SNP_RESTRICTED_INJ |		\
+						MSR_AMD64_SNP_ALT_INJ |			\
+						MSR_AMD64_SNP_DEBUG_SWAP |		\
+						MSR_AMD64_SNP_VMPL_SSS |		\
+						MSR_AMD64_SNP_SECURE_TSC |		\
+						MSR_AMD64_SNP_VMGEXIT_PARAM |		\
+						MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
+						MSR_AMD64_SNP_RESERVED_BIT13 |		\
+						MSR_AMD64_SNP_RESERVED_BIT15 |		\
+						MSR_AMD64_SNP_RESERVED_MASK)
+
+/*
+ * SNP_FEATURES_HAS_GUEST_IMPLEMENTATION is the mask of SNP features that are
+ * implemented by the guest kernel. As and when a new feature is implemented
+ * in the guest kernel, a corresponding bit should be added to the mask.
+ */
+#define SNP_FEATURES_HAS_GUEST_IMPLEMENTATION (0)
+
+/*
+ * The hypervisor can enable various features flags(in SEV_FEATURES[1:63]) and
+ * start the SNP guest. Certain SNP features need guest side implementation.
+ * Check if the SNP guest has implementation for those features.
+ */
+static bool snp_guest_has_features_implemented(void)
+{
+	u64 guest_features_not_implemented = SNP_FEATURES_NEED_GUEST_IMPLEMENTATION &
+		~SNP_FEATURES_HAS_GUEST_IMPLEMENTATION;
+
+	return !(sev_status & guest_features_not_implemented);
+}
+
 void sev_enable(struct boot_params *bp)
 {
 	unsigned int eax, ebx, ecx, edx;
@@ -335,6 +379,13 @@ void sev_enable(struct boot_params *bp)
 		if (!(get_hv_features() & GHCB_HV_FT_SNP))
 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
 
+		/*
+		 * Terminate the boot if hypervisor has enabled any feature
+		 * missing guest side implementation.
+		 */
+		if (!snp_guest_has_features_implemented())
+			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_FEAT_NOT_IMPLEMENTED);
+
 		enforce_vmpl0();
 	}
 
arch/x86/include/asm/msr-index.h +20 -0
index 4a2af82553e4..91447f018f6e 100644
@@ -571,6 +571,26 @@
 #define MSR_AMD64_SEV_ES_ENABLED	BIT_ULL(MSR_AMD64_SEV_ES_ENABLED_BIT)
 #define MSR_AMD64_SEV_SNP_ENABLED	BIT_ULL(MSR_AMD64_SEV_SNP_ENABLED_BIT)
 
+/* SNP feature bits enabled by the hypervisor */
+#define MSR_AMD64_SNP_VTOM			BIT_ULL(3)
+#define MSR_AMD64_SNP_REFLECT_VC		BIT_ULL(4)
+#define MSR_AMD64_SNP_RESTRICTED_INJ		BIT_ULL(5)
+#define MSR_AMD64_SNP_ALT_INJ			BIT_ULL(6)
+#define MSR_AMD64_SNP_DEBUG_SWAP		BIT_ULL(7)
+#define MSR_AMD64_SNP_PREVENT_HOST_IBS		BIT_ULL(8)
+#define MSR_AMD64_SNP_BTB_ISOLATION		BIT_ULL(9)
+#define MSR_AMD64_SNP_VMPL_SSS			BIT_ULL(10)
+#define MSR_AMD64_SNP_SECURE_TSC		BIT_ULL(11)
+#define MSR_AMD64_SNP_VMGEXIT_PARAM		BIT_ULL(12)
+#define MSR_AMD64_SNP_IBS_VIRT			BIT_ULL(14)
+#define MSR_AMD64_SNP_VMSA_REG_PROTECTION	BIT_ULL(16)
+#define MSR_AMD64_SNP_SMT_PROTECTION		BIT_ULL(17)
+
+/* SNP feature bits reserved for future use. */
+#define MSR_AMD64_SNP_RESERVED_BIT13		BIT_ULL(13)
+#define MSR_AMD64_SNP_RESERVED_BIT15		BIT_ULL(15)
+#define MSR_AMD64_SNP_RESERVED_MASK		GENMASK_ULL(63, 18)
+
 #define MSR_AMD64_VIRT_SPEC_CTRL	0xc001011f
 
 /* AMD Collaborative Processor Performance Control MSRs */
arch/x86/include/asm/sev-common.h +1 -0
index b8357d6ecd47..db60cbb01b31 100644
@@ -148,6 +148,7 @@ struct snp_psc_desc {
 #define GHCB_SEV_ES_GEN_REQ		0
 #define GHCB_SEV_ES_PROT_UNSUPPORTED	1
 #define GHCB_SNP_UNSUPPORTED		2
+#define GHCB_SNP_FEAT_NOT_IMPLEMENTED	3
 
 /* Linux-specific reason codes (used with reason set 1) */
 #define SEV_TERM_SET_LINUX		1
Signature · 1 line
2.32.0
maintainer
#1 lore raw
Quoted 33 lines · Nikunj A Dadhania

The hypervisor can enable various new features (SEV_FEATURES[1:63]) and start the SNP guest. Some of these features need guest side implementation. If any of these features are enabled without guest side implementation, the behavior of the SNP guest will be undefined. The SNP guest boot may fail in a non-obvious way making it difficult to debug.

Instead of allowing the guest to continue and have it fail randomly later, detect this early and fail gracefully.

SEV_STATUS MSR indicates features which hypervisor has enabled. While booting, SNP guests should ascertain that all the enabled features have guest side implementation. In case any feature is not implemented in the guest, the guest terminates booting with SNP feature unsupported exit code.

The below table lists the expected guest behavior with various possible scenarios of guest/hypervisor SNP feature support.

+---------------+---------------+---------------+---------------+
|Feature Enabled|  Guest needs  |   Guest has   |  Guest boot   |
|     by HV     |implementation |implementation |   behavior    |
+---------------+---------------+---------------+---------------+
|      No       |      No       |      No       |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      No       |      Yes      |      No       |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      No       |      Yes      |      Yes      |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      Yes      |      No       |      No       |   Boot with   |
Quoted 8 lines
|               |               |               |feature enabled|
+---------------+---------------+---------------+---------------+
|      Yes      |      Yes      |      No       | Graceful Boot |
|               |               |               |    Failure    |
+---------------+---------------+---------------+---------------+
|      Yes      |      Yes      |      Yes      |   Boot with   |
|               |               |               |feature enabled|
+---------------+---------------+---------------+---------------+

I like this table and I wouldn't want for it to go under in some commit message which is not that easy to retrieve so I'm thinking you should add it along with some blurb to

Documentation/x86/amd-memory-encryption.rst

instead where it belongs.

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 276

Quoted 12 lines
arch/x86/boot/compressed/sev.c +4 -0
index c93930d5ccbd..571eb2576475 100644
@@ -270,6 +270,50 @@ static void enforce_vmpl0(void)
 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
 }
 
+/*
+ * SNP_FEATURES_NEED_GUEST_IMPLEMENTATION is the mask of SNP features that
+ * will need guest side implementation for proper functioning of the guest.
+ * If any of these features are enabled without guest side implementation,

"... are enabled in the hypervisor ... "

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 280

Quoted 4 lines
arch/x86/boot/compressed/sev.c +4 -0
+ * the behavior of the guest will be undefined. The guest may fail in
+ * non-obvious way making it difficult to debug.
+ *
+ * SNP reserved feature bits may or may not need guest side implementation.

Yah, get rid of that PPR formulation. If you see the verb "may" always run away. :-)

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 282

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ * As the behavior of reserved feature bits are unknown, to be on the safer
+ * side add them to the NEED_GUEST_IMPLEMENTATION mask.

Yah, that makes sense - you want to protect those for future use. Ack.

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 284

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ */
+#define SNP_FEATURES_NEED_GUEST_IMPLEMENTATION (MSR_AMD64_SNP_VTOM |			\

SNP_FEATURES_REQUIRED

Simpler and shorter.

Quoted 6 lines
arch/x86/boot/compressed/sev.c +6 -0
+						MSR_AMD64_SNP_REFLECT_VC |		\
+						MSR_AMD64_SNP_RESTRICTED_INJ |		\
+						MSR_AMD64_SNP_ALT_INJ |			\
+						MSR_AMD64_SNP_DEBUG_SWAP |		\
+						MSR_AMD64_SNP_VMPL_SSS |		\
+						MSR_AMD64_SNP_SECURE_TSC |		\

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 300

Quoted 10 lines
arch/x86/boot/compressed/sev.c +10 -0
+						MSR_AMD64_SNP_VMGEXIT_PARAM |		\
+						MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
+						MSR_AMD64_SNP_RESERVED_BIT13 |		\
+						MSR_AMD64_SNP_RESERVED_BIT15 |		\
+						MSR_AMD64_SNP_RESERVED_MASK)
+
+/*
+ * SNP_FEATURES_HAS_GUEST_IMPLEMENTATION is the mask of SNP features that are
+ * implemented by the guest kernel. As and when a new feature is implemented
+ * in the guest kernel, a corresponding bit should be added to the mask.

And there's no way we won't notice that we've forgotten to do so because it'll terminate with the proper error code.

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 302

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ */
+#define SNP_FEATURES_HAS_GUEST_IMPLEMENTATION (0)

SNP_FEATURES_PRESENT

And so I've done a couple of changes ontop, here's a diff as it explains a lot better what I mean.

Have a look and let me know if something's wrong.

Thx.

---

arch/x86/boot/compressed/sev.c +25 -38
index 10272835dfe9..f023d37e2c41 100644
@@ -271,48 +271,35 @@ static void enforce_vmpl0(void)
 }
 
 /*
- * SNP_FEATURES_NEED_GUEST_IMPLEMENTATION is the mask of SNP features that
- * will need guest side implementation for proper functioning of the guest.
- * If any of these features are enabled without guest side implementation,
- * the behavior of the guest will be undefined. The guest may fail in
- * non-obvious way making it difficult to debug.
- *
- * SNP reserved feature bits may or may not need guest side implementation.
- * As the behavior of reserved feature bits are unknown, to be on the safer
- * side add them to the NEED_GUEST_IMPLEMENTATION mask.
- */
-#define SNP_FEATURES_NEED_GUEST_IMPLEMENTATION (MSR_AMD64_SNP_VTOM |			\
-						MSR_AMD64_SNP_REFLECT_VC |		\
-						MSR_AMD64_SNP_RESTRICTED_INJ |		\
-						MSR_AMD64_SNP_ALT_INJ |			\
-						MSR_AMD64_SNP_DEBUG_SWAP |		\
-						MSR_AMD64_SNP_VMPL_SSS |		\
-						MSR_AMD64_SNP_SECURE_TSC |		\
-						MSR_AMD64_SNP_VMGEXIT_PARAM |		\
-						MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
-						MSR_AMD64_SNP_RESERVED_BIT13 |		\
-						MSR_AMD64_SNP_RESERVED_BIT15 |		\
-						MSR_AMD64_SNP_RESERVED_MASK)
 
-/*
- * SNP_FEATURES_HAS_GUEST_IMPLEMENTATION is the mask of SNP features that are
- * implemented by the guest kernel. As and when a new feature is implemented
- * in the guest kernel, a corresponding bit should be added to the mask.
+ * SNP_FEATURES_REQUIRED is the mask of SNP features that will need
+ * guest side implementation for proper functioning of the guest. If any
+ * of these features are enabled in the hypervisor but are lacking guest
+ * side implementation, the behavior of the guest will be undefined. The
+ * guest could fail in non-obvious way making it difficult to debug.
+ *
+ * As the behavior of reserved feature bits is unknown to be on the
+ * safe side add them to the required features mask.
  */
-#define SNP_FEATURES_HAS_GUEST_IMPLEMENTATION (0)
+#define SNP_FEATURES_REQUIRED		(MSR_AMD64_SNP_VTOM |			\
+					MSR_AMD64_SNP_REFLECT_VC |		\
+					MSR_AMD64_SNP_RESTRICTED_INJ |		\
+					MSR_AMD64_SNP_ALT_INJ |			\
+					MSR_AMD64_SNP_DEBUG_SWAP |		\
+					MSR_AMD64_SNP_VMPL_SSS |		\
+					MSR_AMD64_SNP_SECURE_TSC |		\
+					MSR_AMD64_SNP_VMGEXIT_PARAM |		\
+					MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
+					MSR_AMD64_SNP_RESERVED_BIT13 |		\
+					MSR_AMD64_SNP_RESERVED_BIT15 |		\
+					MSR_AMD64_SNP_RESERVED_MASK)
 
 /*
- * The hypervisor can enable various features flags (in SEV_FEATURES[1:63]) and
- * start the SNP guest. Certain SNP features need guest side implementation.
- * Check if the SNP guest has implementation for those features.
+ * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
+ * by the guest kernel. As and when a new feature is implemented in the
+ * guest kernel, a corresponding bit should be added to the mask.
  */
-static bool snp_guest_has_features_implemented(void)
-{
-	u64 guest_features_not_implemented = SNP_FEATURES_NEED_GUEST_IMPLEMENTATION &
-		~SNP_FEATURES_HAS_GUEST_IMPLEMENTATION;
-
-	return !(sev_status & guest_features_not_implemented);
-}
+#define SNP_FEATURES_PRESENT (0)
 
 void sev_enable(struct boot_params *bp)
 {
@@ -383,7 +370,7 @@ void sev_enable(struct boot_params *bp)
 		 * Terminate the boot if hypervisor has enabled any feature
 		 * missing guest side implementation.
 		 */
-		if (!snp_guest_has_features_implemented())
+		if (sev_status & SNP_FEATURES_REQUIRED & ~SNP_FEATURES_PRESENT)
 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_FEAT_NOT_IMPLEMENTED);
 
 		enforce_vmpl0();
Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#2 lore raw
Quoted 43 lines · Borislav Petkov
Quoted 33 lines · Nikunj A Dadhania

The hypervisor can enable various new features (SEV_FEATURES[1:63]) and start the SNP guest. Some of these features need guest side implementation. If any of these features are enabled without guest side implementation, the behavior of the SNP guest will be undefined. The SNP guest boot may fail in a non-obvious way making it difficult to debug.

Instead of allowing the guest to continue and have it fail randomly later, detect this early and fail gracefully.

SEV_STATUS MSR indicates features which hypervisor has enabled. While booting, SNP guests should ascertain that all the enabled features have guest side implementation. In case any feature is not implemented in the guest, the guest terminates booting with SNP feature unsupported exit code.

The below table lists the expected guest behavior with various possible scenarios of guest/hypervisor SNP feature support.

+---------------+---------------+---------------+---------------+
|Feature Enabled|  Guest needs  |   Guest has   |  Guest boot   |
|     by HV     |implementation |implementation |   behavior    |
+---------------+---------------+---------------+---------------+
|      No       |      No       |      No       |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      No       |      Yes      |      No       |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      No       |      Yes      |      Yes      |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      Yes      |      No       |      No       |   Boot with   |
Quoted 8 lines
|               |               |               |feature enabled|
+---------------+---------------+---------------+---------------+
|      Yes      |      Yes      |      No       | Graceful Boot |
|               |               |               |    Failure    |
+---------------+---------------+---------------+---------------+
|      Yes      |      Yes      |      Yes      |   Boot with   |
|               |               |               |feature enabled|
+---------------+---------------+---------------+---------------+
Quoted 7 lines

I like this table and I wouldn't want for it to go under in some commit message which is not that easy to retrieve so I'm thinking you should add it along with some blurb to

Documentation/x86/amd-memory-encryption.rst

instead where it belongs.

Sure will do.

Quoted 9 lines
Quoted 9 lines
arch/x86/boot/compressed/sev.c +0 -0
index c93930d5ccbd..571eb2576475 100644
@@ -270,6 +270,50 @@ static void enforce_vmpl0(void)
 		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
 }
 
Quoted 7 lines

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 276

Quoted 4 lines
arch/x86/boot/compressed/sev.c +4 -0
+/*
+ * SNP_FEATURES_NEED_GUEST_IMPLEMENTATION is the mask of SNP features that
+ * will need guest side implementation for proper functioning of the guest.
+ * If any of these features are enabled without guest side implementation,

"... are enabled in the hypervisor ... "

Sure

Quoted 8 lines

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 280

Quoted 4 lines
arch/x86/boot/compressed/sev.c +4 -0
+ * the behavior of the guest will be undefined. The guest may fail in
+ * non-obvious way making it difficult to debug.
+ *
+ * SNP reserved feature bits may or may not need guest side implementation.

Yah, get rid of that PPR formulation. If you see the verb "may" always run away. :-)

True, had to add "may" as the I don't know what the bit will be used for.

Quoted 11 lines

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 282

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ * As the behavior of reserved feature bits are unknown, to be on the safer
+ * side add them to the NEED_GUEST_IMPLEMENTATION mask.

Yah, that makes sense - you want to protect those for future use. Ack.

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 284

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ */
+#define SNP_FEATURES_NEED_GUEST_IMPLEMENTATION (MSR_AMD64_SNP_VTOM |			\

SNP_FEATURES_REQUIRED

Simpler and shorter.

Yes, much better.

Quoted 24 lines
Quoted 7 lines
arch/x86/boot/compressed/sev.c +6 -0
+						MSR_AMD64_SNP_REFLECT_VC |		\
+						MSR_AMD64_SNP_RESTRICTED_INJ |		\
+						MSR_AMD64_SNP_ALT_INJ |			\
+						MSR_AMD64_SNP_DEBUG_SWAP |		\
+						MSR_AMD64_SNP_VMPL_SSS |		\
+						MSR_AMD64_SNP_SECURE_TSC |		\

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void)

Quoted 10 lines
arch/x86/boot/compressed/sev.c +10 -0
+						MSR_AMD64_SNP_VMGEXIT_PARAM |		\
+						MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
+						MSR_AMD64_SNP_RESERVED_BIT13 |		\
+						MSR_AMD64_SNP_RESERVED_BIT15 |		\
+						MSR_AMD64_SNP_RESERVED_MASK)
+
+/*
+ * SNP_FEATURES_HAS_GUEST_IMPLEMENTATION is the mask of SNP features that are
+ * implemented by the guest kernel. As and when a new feature is implemented
+ * in the guest kernel, a corresponding bit should be added to the mask.

And there's no way we won't notice that we've forgotten to do so because it'll terminate with the proper error code.

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 302

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ */
+#define SNP_FEATURES_HAS_GUEST_IMPLEMENTATION (0)
Quoted 6 lines

SNP_FEATURES_PRESENT

And so I've done a couple of changes ontop, here's a diff as it explains a lot better what I mean.

Have a look and let me know if something's wrong.

Looks good. Do you want me to send v3 with these changes ?

Regards
Nikunj

Quoted 91 lines

Thx.

---

arch/x86/boot/compressed/sev.c +25 -38
index 10272835dfe9..f023d37e2c41 100644
@@ -271,48 +271,35 @@ static void enforce_vmpl0(void)
 }
 
 /*
- * SNP_FEATURES_NEED_GUEST_IMPLEMENTATION is the mask of SNP features that
- * will need guest side implementation for proper functioning of the guest.
- * If any of these features are enabled without guest side implementation,
- * the behavior of the guest will be undefined. The guest may fail in
- * non-obvious way making it difficult to debug.
- *
- * SNP reserved feature bits may or may not need guest side implementation.
- * As the behavior of reserved feature bits are unknown, to be on the safer
- * side add them to the NEED_GUEST_IMPLEMENTATION mask.
- */
-#define SNP_FEATURES_NEED_GUEST_IMPLEMENTATION (MSR_AMD64_SNP_VTOM |			\
-						MSR_AMD64_SNP_REFLECT_VC |		\
-						MSR_AMD64_SNP_RESTRICTED_INJ |		\
-						MSR_AMD64_SNP_ALT_INJ |			\
-						MSR_AMD64_SNP_DEBUG_SWAP |		\
-						MSR_AMD64_SNP_VMPL_SSS |		\
-						MSR_AMD64_SNP_SECURE_TSC |		\
-						MSR_AMD64_SNP_VMGEXIT_PARAM |		\
-						MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
-						MSR_AMD64_SNP_RESERVED_BIT13 |		\
-						MSR_AMD64_SNP_RESERVED_BIT15 |		\
-						MSR_AMD64_SNP_RESERVED_MASK)
 
-/*
- * SNP_FEATURES_HAS_GUEST_IMPLEMENTATION is the mask of SNP features that are
- * implemented by the guest kernel. As and when a new feature is implemented
- * in the guest kernel, a corresponding bit should be added to the mask.
+ * SNP_FEATURES_REQUIRED is the mask of SNP features that will need
+ * guest side implementation for proper functioning of the guest. If any
+ * of these features are enabled in the hypervisor but are lacking guest
+ * side implementation, the behavior of the guest will be undefined. The
+ * guest could fail in non-obvious way making it difficult to debug.
+ *
+ * As the behavior of reserved feature bits is unknown to be on the
+ * safe side add them to the required features mask.
  */
-#define SNP_FEATURES_HAS_GUEST_IMPLEMENTATION (0)
+#define SNP_FEATURES_REQUIRED		(MSR_AMD64_SNP_VTOM |			\
+					MSR_AMD64_SNP_REFLECT_VC |		\
+					MSR_AMD64_SNP_RESTRICTED_INJ |		\
+					MSR_AMD64_SNP_ALT_INJ |			\
+					MSR_AMD64_SNP_DEBUG_SWAP |		\
+					MSR_AMD64_SNP_VMPL_SSS |		\
+					MSR_AMD64_SNP_SECURE_TSC |		\
+					MSR_AMD64_SNP_VMGEXIT_PARAM |		\
+					MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
+					MSR_AMD64_SNP_RESERVED_BIT13 |		\
+					MSR_AMD64_SNP_RESERVED_BIT15 |		\
+					MSR_AMD64_SNP_RESERVED_MASK)
 
 /*
- * The hypervisor can enable various features flags (in SEV_FEATURES[1:63]) and
- * start the SNP guest. Certain SNP features need guest side implementation.
- * Check if the SNP guest has implementation for those features.
+ * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
+ * by the guest kernel. As and when a new feature is implemented in the
+ * guest kernel, a corresponding bit should be added to the mask.
  */
-static bool snp_guest_has_features_implemented(void)
-{
-	u64 guest_features_not_implemented = SNP_FEATURES_NEED_GUEST_IMPLEMENTATION &
-		~SNP_FEATURES_HAS_GUEST_IMPLEMENTATION;
-
-	return !(sev_status & guest_features_not_implemented);
-}
+#define SNP_FEATURES_PRESENT (0)
 
 void sev_enable(struct boot_params *bp)
 {
@@ -383,7 +370,7 @@ void sev_enable(struct boot_params *bp)
 		 * Terminate the boot if hypervisor has enabled any feature
 		 * missing guest side implementation.
 		 */
-		if (!snp_guest_has_features_implemented())
+		if (sev_status & SNP_FEATURES_REQUIRED & ~SNP_FEATURES_PRESENT)
 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_FEAT_NOT_IMPLEMENTED);
 
 		enforce_vmpl0();
maintainer
#3 lore raw
Quoted 1 line · Nikunj A. Dadhania

Looks good. Do you want me to send v3 with these changes ?

Sure, pls do, after testing it again.

But no hurry because the merge window opens next week and that one will have to wait for after it.

Thx.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
#4 lore raw
In reply to Borislav Petkov
Quoted 77 lines · Borislav Petkov
Quoted 33 lines · Nikunj A Dadhania

The hypervisor can enable various new features (SEV_FEATURES[1:63]) and start the SNP guest. Some of these features need guest side implementation. If any of these features are enabled without guest side implementation, the behavior of the SNP guest will be undefined. The SNP guest boot may fail in a non-obvious way making it difficult to debug.

Instead of allowing the guest to continue and have it fail randomly later, detect this early and fail gracefully.

SEV_STATUS MSR indicates features which hypervisor has enabled. While booting, SNP guests should ascertain that all the enabled features have guest side implementation. In case any feature is not implemented in the guest, the guest terminates booting with SNP feature unsupported exit code.

The below table lists the expected guest behavior with various possible scenarios of guest/hypervisor SNP feature support.

+---------------+---------------+---------------+---------------+
|Feature Enabled|  Guest needs  |   Guest has   |  Guest boot   |
|     by HV     |implementation |implementation |   behavior    |
+---------------+---------------+---------------+---------------+
|      No       |      No       |      No       |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      No       |      Yes      |      No       |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      No       |      Yes      |      Yes      |     Boot      |
|               |               |               |               |
+---------------+---------------+---------------+---------------+
|      Yes      |      No       |      No       |   Boot with   |
Quoted 8 lines
|               |               |               |feature enabled|
+---------------+---------------+---------------+---------------+
|      Yes      |      Yes      |      No       | Graceful Boot |
|               |               |               |    Failure    |
+---------------+---------------+---------------+---------------+
|      Yes      |      Yes      |      Yes      |   Boot with   |
|               |               |               |feature enabled|
+---------------+---------------+---------------+---------------+

I like this table and I wouldn't want for it to go under in some commit message which is not that easy to retrieve so I'm thinking you should add it along with some blurb to

Documentation/x86/amd-memory-encryption.rst

instead where it belongs.

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 276

Quoted 12 lines
arch/x86/boot/compressed/sev.c +4 -0
index c93930d5ccbd..571eb2576475 100644
@@ -270,6 +270,50 @@ static void enforce_vmpl0(void)
  		sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
  }
 
+/*
+ * SNP_FEATURES_NEED_GUEST_IMPLEMENTATION is the mask of SNP features that
+ * will need guest side implementation for proper functioning of the guest.
+ * If any of these features are enabled without guest side implementation,

"... are enabled in the hypervisor ..."

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 280

Quoted 4 lines
arch/x86/boot/compressed/sev.c +4 -0
+ * the behavior of the guest will be undefined. The guest may fail in
+ * non-obvious way making it difficult to debug.
+ *
+ * SNP reserved feature bits may or may not need guest side implementation.

Yah, get rid of that PPR formulation. If you see the verb "may" always run away. :-)

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 282

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ * As the behavior of reserved feature bits are unknown, to be on the safer
+ * side add them to the NEED_GUEST_IMPLEMENTATION mask.
Quoted 6 lines

Yah, that makes sense - you want to protect those for future use. Ack.

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 284

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ */
+#define SNP_FEATURES_NEED_GUEST_IMPLEMENTATION (MSR_AMD64_SNP_VTOM |			\

SNP_FEATURES_REQUIRED

This makes it sound like these features are required to run SNP, which they're not. It's that we can't run SNP with these features without support in the guest. So while the patch name is long, it is accurate.

Maybe SNP_FEATURES_NEED_IMPLEMENTATION if you want to shorten it a little?

Thanks,
Tom

Quoted 123 lines

Simpler and shorter.

Quoted 6 lines
arch/x86/boot/compressed/sev.c +6 -0
+						MSR_AMD64_SNP_REFLECT_VC |		\
+						MSR_AMD64_SNP_RESTRICTED_INJ |		\
+						MSR_AMD64_SNP_ALT_INJ |			\
+						MSR_AMD64_SNP_DEBUG_SWAP |		\
+						MSR_AMD64_SNP_VMPL_SSS |		\
+						MSR_AMD64_SNP_SECURE_TSC |		\

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void)

Quoted 10 lines
arch/x86/boot/compressed/sev.c +10 -0
+						MSR_AMD64_SNP_VMGEXIT_PARAM |		\
+						MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
+						MSR_AMD64_SNP_RESERVED_BIT13 |		\
+						MSR_AMD64_SNP_RESERVED_BIT15 |		\
+						MSR_AMD64_SNP_RESERVED_MASK)
+
+/*
+ * SNP_FEATURES_HAS_GUEST_IMPLEMENTATION is the mask of SNP features that are
+ * implemented by the guest kernel. As and when a new feature is implemented
+ * in the guest kernel, a corresponding bit should be added to the mask.

And there's no way we won't notice that we've forgotten to do so because it'll terminate with the proper error code.

arch/x86/boot/compressed/sev.c static void enforce_vmpl0(void) · line 302

Quoted 2 lines
arch/x86/boot/compressed/sev.c +2 -0
+ */
+#define SNP_FEATURES_HAS_GUEST_IMPLEMENTATION (0)

SNP_FEATURES_PRESENT

And so I've done a couple of changes ontop, here's a diff as it explains a lot better what I mean.

Have a look and let me know if something's wrong.

Thx.

---

arch/x86/boot/compressed/sev.c +25 -38
index 10272835dfe9..f023d37e2c41 100644
@@ -271,48 +271,35 @@ static void enforce_vmpl0(void)
  }
 
  /*
- * SNP_FEATURES_NEED_GUEST_IMPLEMENTATION is the mask of SNP features that
- * will need guest side implementation for proper functioning of the guest.
- * If any of these features are enabled without guest side implementation,
- * the behavior of the guest will be undefined. The guest may fail in
- * non-obvious way making it difficult to debug.
- *
- * SNP reserved feature bits may or may not need guest side implementation.
- * As the behavior of reserved feature bits are unknown, to be on the safer
- * side add them to the NEED_GUEST_IMPLEMENTATION mask.
- */
-#define SNP_FEATURES_NEED_GUEST_IMPLEMENTATION (MSR_AMD64_SNP_VTOM |			\
-						MSR_AMD64_SNP_REFLECT_VC |		\
-						MSR_AMD64_SNP_RESTRICTED_INJ |		\
-						MSR_AMD64_SNP_ALT_INJ |			\
-						MSR_AMD64_SNP_DEBUG_SWAP |		\
-						MSR_AMD64_SNP_VMPL_SSS |		\
-						MSR_AMD64_SNP_SECURE_TSC |		\
-						MSR_AMD64_SNP_VMGEXIT_PARAM |		\
-						MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
-						MSR_AMD64_SNP_RESERVED_BIT13 |		\
-						MSR_AMD64_SNP_RESERVED_BIT15 |		\
-						MSR_AMD64_SNP_RESERVED_MASK)
 
-/*
- * SNP_FEATURES_HAS_GUEST_IMPLEMENTATION is the mask of SNP features that are
- * implemented by the guest kernel. As and when a new feature is implemented
- * in the guest kernel, a corresponding bit should be added to the mask.
+ * SNP_FEATURES_REQUIRED is the mask of SNP features that will need
+ * guest side implementation for proper functioning of the guest. If any
+ * of these features are enabled in the hypervisor but are lacking guest
+ * side implementation, the behavior of the guest will be undefined. The
+ * guest could fail in non-obvious way making it difficult to debug.
+ *
+ * As the behavior of reserved feature bits is unknown to be on the
+ * safe side add them to the required features mask.
   */
-#define SNP_FEATURES_HAS_GUEST_IMPLEMENTATION (0)
+#define SNP_FEATURES_REQUIRED		(MSR_AMD64_SNP_VTOM |			\
+					MSR_AMD64_SNP_REFLECT_VC |		\
+					MSR_AMD64_SNP_RESTRICTED_INJ |		\
+					MSR_AMD64_SNP_ALT_INJ |			\
+					MSR_AMD64_SNP_DEBUG_SWAP |		\
+					MSR_AMD64_SNP_VMPL_SSS |		\
+					MSR_AMD64_SNP_SECURE_TSC |		\
+					MSR_AMD64_SNP_VMGEXIT_PARAM |		\
+					MSR_AMD64_SNP_VMSA_REG_PROTECTION |	\
+					MSR_AMD64_SNP_RESERVED_BIT13 |		\
+					MSR_AMD64_SNP_RESERVED_BIT15 |		\
+					MSR_AMD64_SNP_RESERVED_MASK)
 
  /*
- * The hypervisor can enable various features flags (in SEV_FEATURES[1:63]) and
- * start the SNP guest. Certain SNP features need guest side implementation.
- * Check if the SNP guest has implementation for those features.
+ * SNP_FEATURES_PRESENT is the mask of SNP features that are implemented
+ * by the guest kernel. As and when a new feature is implemented in the
+ * guest kernel, a corresponding bit should be added to the mask.
   */
-static bool snp_guest_has_features_implemented(void)
-{
-	u64 guest_features_not_implemented = SNP_FEATURES_NEED_GUEST_IMPLEMENTATION &
-		~SNP_FEATURES_HAS_GUEST_IMPLEMENTATION;
-
-	return !(sev_status & guest_features_not_implemented);
-}
+#define SNP_FEATURES_PRESENT (0)
 
  void sev_enable(struct boot_params *bp)
  {
@@ -383,7 +370,7 @@ void sev_enable(struct boot_params *bp)
  		 * Terminate the boot if hypervisor has enabled any feature
  		 * missing guest side implementation.
  		 */
-		if (!snp_guest_has_features_implemented())
+		if (sev_status & SNP_FEATURES_REQUIRED & ~SNP_FEATURES_PRESENT)
  			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_FEAT_NOT_IMPLEMENTED);
 
  		enforce_vmpl0();
maintainer
#5 lore raw
Quoted 3 lines · Tom Lendacky

This makes it sound like these features are required to run SNP, which they're not. It's that we can't run SNP with these features without support in the guest. So while the patch name is long, it is accurate.

Maybe, but it is a mouthful this way. And unreadable. And there's plenty of comments explaining what it is. And it is used in one place only.

Quoted 1 line

Maybe SNP_FEATURES_NEED_IMPLEMENTATION if you want to shorten it a little?

"need implementation" where? Host? Guest?

I.e., we're relying on comments to explain what the name is, one way or the other.

How about a short and sweet:

if (sev_status & SNP_FEATURES_IMPL_REQ & ~SNP_FEATURES_PRESENT)

along with the explanation in a comment above it. Comment which is a must regardless.

Hmm.

Signature · 4 lines
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette