[RFT PATCH v3 12/21] x86/sev: Unify SEV-SNP hypervisor feature check

Ard Biesheuvel posted 21 patches 9 months ago
There is a newer version of this series
[RFT PATCH v3 12/21] x86/sev: Unify SEV-SNP hypervisor feature check
Posted by Ard Biesheuvel 9 months ago
From: Ard Biesheuvel <ardb@kernel.org>

The decompressor and the core kernel both check the hypervisor feature
mask exposed by the hypervisor, but test it in slightly different ways.

This disparity seems unintentional, and simply a result of the fact that
the decompressor and the core kernel evolve differently over time when
it comes to setting up the SEV-SNP execution context.

So move the HV feature check into a helper function and call that
instead. For the core kernel, move the check to an earlier boot stage,
right after the point where it is established that the guest is
executing in SEV-SNP mode.

Signed-off-by: Ard Biesheuvel <ardb@kernel.org>
---
 arch/x86/boot/compressed/sev.c      | 19 +----------
 arch/x86/boot/startup/sev-shared.c  | 33 +++++++++++++++-----
 arch/x86/boot/startup/sme.c         |  2 ++
 arch/x86/coco/sev/core.c            | 11 -------
 arch/x86/include/asm/sev-internal.h |  2 +-
 arch/x86/include/asm/sev.h          |  2 ++
 6 files changed, 32 insertions(+), 37 deletions(-)

diff --git a/arch/x86/boot/compressed/sev.c b/arch/x86/boot/compressed/sev.c
index 68fc3d179bbe..4b7a99b2f822 100644
--- a/arch/x86/boot/compressed/sev.c
+++ b/arch/x86/boot/compressed/sev.c
@@ -397,24 +397,7 @@ void sev_enable(struct boot_params *bp)
 			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SEV_ES_PROT_UNSUPPORTED);
 	}
 
-	/*
-	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
-	 * features.
-	 */
-	if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
-		u64 hv_features;
-
-		hv_features = get_hv_features();
-		if (!(hv_features & GHCB_HV_FT_SNP))
-			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
-
-		/*
-		 * Running at VMPL0 is required unless an SVSM is present and
-		 * the hypervisor supports the required SVSM GHCB events.
-		 */
-		if (snp_vmpl > 0 && !(hv_features & GHCB_HV_FT_SNP_MULTI_VMPL))
-			sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
-	}
+	snp_check_hv_features();
 
 	if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
 		error("SEV-SNP supported indicated by CC blob, but not SEV status MSR.");
diff --git a/arch/x86/boot/startup/sev-shared.c b/arch/x86/boot/startup/sev-shared.c
index 70ad9a0aa023..560985ef8df6 100644
--- a/arch/x86/boot/startup/sev-shared.c
+++ b/arch/x86/boot/startup/sev-shared.c
@@ -66,16 +66,10 @@ sev_es_terminate(unsigned int set, unsigned int reason)
 		asm volatile("hlt\n" : : : "memory");
 }
 
-/*
- * The hypervisor features are available from GHCB version 2 onward.
- */
-u64 get_hv_features(void)
+static u64 __head get_hv_features(void)
 {
 	u64 val;
 
-	if (ghcb_version < 2)
-		return 0;
-
 	sev_es_wr_ghcb_msr(GHCB_MSR_HV_FT_REQ);
 	VMGEXIT();
 
@@ -86,6 +80,31 @@ u64 get_hv_features(void)
 	return GHCB_MSR_HV_FT_RESP_VAL(val);
 }
 
+u64 __head snp_check_hv_features(void)
+{
+	/*
+	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
+	 * features.
+	 */
+	if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
+		u64 hv_features;
+
+		hv_features = get_hv_features();
+		if (!(hv_features & GHCB_HV_FT_SNP))
+			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
+
+		/*
+		 * Running at VMPL0 is required unless an SVSM is present and
+		 * the hypervisor supports the required SVSM GHCB events.
+		 */
+		if (snp_vmpl > 0 && !(hv_features & GHCB_HV_FT_SNP_MULTI_VMPL))
+			sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
+
+		return hv_features;
+	}
+	return 0;
+}
+
 int svsm_perform_msr_protocol(struct svsm_call *call)
 {
 	u8 pending = 0;
diff --git a/arch/x86/boot/startup/sme.c b/arch/x86/boot/startup/sme.c
index 753cd2094080..0ae04e333f51 100644
--- a/arch/x86/boot/startup/sme.c
+++ b/arch/x86/boot/startup/sme.c
@@ -533,6 +533,8 @@ void __head sme_enable(struct boot_params *bp)
 	if (snp_en ^ !!(msr & MSR_AMD64_SEV_SNP_ENABLED))
 		snp_abort();
 
+	sev_hv_features = snp_check_hv_features();
+
 	/* Check if memory encryption is enabled */
 	if (feature_mask == AMD_SME_BIT) {
 		if (!(bp->hdr.xloadflags & XLF_MEM_ENCRYPTION))
diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
index fa7fdd11a45b..fc4f6f188d42 100644
--- a/arch/x86/coco/sev/core.c
+++ b/arch/x86/coco/sev/core.c
@@ -1264,17 +1264,6 @@ void __init sev_es_init_vc_handling(void)
 	if (!sev_es_check_cpu_features())
 		panic("SEV-ES CPU Features missing");
 
-	/*
-	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
-	 * features.
-	 */
-	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
-		sev_hv_features = get_hv_features();
-
-		if (!(sev_hv_features & GHCB_HV_FT_SNP))
-			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
-	}
-
 	/* Initialize per-cpu GHCB pages */
 	for_each_possible_cpu(cpu) {
 		alloc_runtime_data(cpu);
diff --git a/arch/x86/include/asm/sev-internal.h b/arch/x86/include/asm/sev-internal.h
index 3690994275dd..7ad8faf5e88b 100644
--- a/arch/x86/include/asm/sev-internal.h
+++ b/arch/x86/include/asm/sev-internal.h
@@ -81,6 +81,6 @@ static __always_inline void sev_es_wr_ghcb_msr(u64 val)
 	native_wrmsr(MSR_AMD64_SEV_ES_GHCB, low, high);
 }
 
-u64 get_hv_features(void);
+void check_hv_features(void);
 
 const struct snp_cpuid_table *snp_cpuid_get_table(void);
diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
index ae2502253bd3..17b03a1f5694 100644
--- a/arch/x86/include/asm/sev.h
+++ b/arch/x86/include/asm/sev.h
@@ -418,6 +418,7 @@ struct svsm_call {
 #ifdef CONFIG_AMD_MEM_ENCRYPT
 
 extern u8 snp_vmpl;
+extern u64 sev_hv_features;
 
 extern void __sev_es_ist_enter(struct pt_regs *regs);
 extern void __sev_es_ist_exit(void);
@@ -495,6 +496,7 @@ void snp_set_memory_private(unsigned long vaddr, unsigned long npages);
 void snp_set_wakeup_secondary_cpu(void);
 bool snp_init(struct boot_params *bp);
 void __noreturn snp_abort(void);
+u64 snp_check_hv_features(void);
 void snp_dmi_setup(void);
 int snp_issue_svsm_attest_req(u64 call_id, struct svsm_call *call, struct svsm_attest_call *input);
 void snp_accept_memory(phys_addr_t start, phys_addr_t end);
-- 
2.49.0.1045.g170613ef41-goog
Re: [RFT PATCH v3 12/21] x86/sev: Unify SEV-SNP hypervisor feature check
Posted by Borislav Petkov 8 months, 1 week ago
On Mon, May 12, 2025 at 09:08:47PM +0200, Ard Biesheuvel wrote:
> diff --git a/arch/x86/boot/startup/sev-shared.c b/arch/x86/boot/startup/sev-shared.c
> index 70ad9a0aa023..560985ef8df6 100644
> --- a/arch/x86/boot/startup/sev-shared.c
> +++ b/arch/x86/boot/startup/sev-shared.c
> @@ -66,16 +66,10 @@ sev_es_terminate(unsigned int set, unsigned int reason)
>  		asm volatile("hlt\n" : : : "memory");
>  }
>  
> -/*
> - * The hypervisor features are available from GHCB version 2 onward.
> - */
> -u64 get_hv_features(void)
> +static u64 __head get_hv_features(void)
>  {
>  	u64 val;
>  
> -	if (ghcb_version < 2)
> -		return 0;

Why?

> -
>  	sev_es_wr_ghcb_msr(GHCB_MSR_HV_FT_REQ);
>  	VMGEXIT();
>  
> @@ -86,6 +80,31 @@ u64 get_hv_features(void)
>  	return GHCB_MSR_HV_FT_RESP_VAL(val);
>  }
>  
> +u64 __head snp_check_hv_features(void)
> +{
> +	/*
> +	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
> +	 * features.
> +	 */
> +	if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
> +		u64 hv_features;
> +
> +		hv_features = get_hv_features();
> +		if (!(hv_features & GHCB_HV_FT_SNP))
> +			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
> +
> +		/*
> +		 * Running at VMPL0 is required unless an SVSM is present and
> +		 * the hypervisor supports the required SVSM GHCB events.
> +		 */
> +		if (snp_vmpl > 0 && !(hv_features & GHCB_HV_FT_SNP_MULTI_VMPL))
> +			sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
> +
> +		return hv_features;
> +	}
> +	return 0;
> +}
> +
>  int svsm_perform_msr_protocol(struct svsm_call *call)
>  {
>  	u8 pending = 0;
> diff --git a/arch/x86/boot/startup/sme.c b/arch/x86/boot/startup/sme.c
> index 753cd2094080..0ae04e333f51 100644
> --- a/arch/x86/boot/startup/sme.c
> +++ b/arch/x86/boot/startup/sme.c
> @@ -533,6 +533,8 @@ void __head sme_enable(struct boot_params *bp)
>  	if (snp_en ^ !!(msr & MSR_AMD64_SEV_SNP_ENABLED))
>  		snp_abort();
>  
> +	sev_hv_features = snp_check_hv_features();

Is this writing the sev_hv_features declared in

arch/x86/boot/startup/sev-startup.c

?

arch/x86/boot/startup/sev-startup.c:45:u64 sev_hv_features __ro_after_init;
arch/x86/boot/startup/sme.c:536:        sev_hv_features = snp_check_hv_features();
arch/x86/coco/sev/core.c:980:   if (!(sev_hv_features & GHCB_HV_FT_SNP_AP_CREATION))
arch/x86/include/asm/sev-internal.h:5:extern u64 sev_hv_features;
arch/x86/include/asm/sev.h:428:extern u64 sev_hv_features;

I'm confused.

If sev_hv_features is startup code, why isn't it accessed this way...?

/me goes and looks forward in the set...

oh my, that is coming.

> +
>  	/* Check if memory encryption is enabled */
>  	if (feature_mask == AMD_SME_BIT) {
>  		if (!(bp->hdr.xloadflags & XLF_MEM_ENCRYPTION))
> diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
> index fa7fdd11a45b..fc4f6f188d42 100644
> --- a/arch/x86/coco/sev/core.c
> +++ b/arch/x86/coco/sev/core.c
> @@ -1264,17 +1264,6 @@ void __init sev_es_init_vc_handling(void)
>  	if (!sev_es_check_cpu_features())
>  		panic("SEV-ES CPU Features missing");
>  
> -	/*
> -	 * SNP is supported in v2 of the GHCB spec which mandates support for HV
> -	 * features.
> -	 */
> -	if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
> -		sev_hv_features = get_hv_features();
> -
> -		if (!(sev_hv_features & GHCB_HV_FT_SNP))
> -			sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
> -	}

I guess we would've terminated earlier anyway so that's probably ok...

> -
>  	/* Initialize per-cpu GHCB pages */
>  	for_each_possible_cpu(cpu) {
>  		alloc_runtime_data(cpu);

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [RFT PATCH v3 12/21] x86/sev: Unify SEV-SNP hypervisor feature check
Posted by Ard Biesheuvel 8 months, 1 week ago
On Fri, 30 May 2025 at 13:17, Borislav Petkov <bp@alien8.de> wrote:
>
> On Mon, May 12, 2025 at 09:08:47PM +0200, Ard Biesheuvel wrote:
> > diff --git a/arch/x86/boot/startup/sev-shared.c b/arch/x86/boot/startup/sev-shared.c
> > index 70ad9a0aa023..560985ef8df6 100644
> > --- a/arch/x86/boot/startup/sev-shared.c
> > +++ b/arch/x86/boot/startup/sev-shared.c
> > @@ -66,16 +66,10 @@ sev_es_terminate(unsigned int set, unsigned int reason)
> >               asm volatile("hlt\n" : : : "memory");
> >  }
> >
> > -/*
> > - * The hypervisor features are available from GHCB version 2 onward.
> > - */
> > -u64 get_hv_features(void)
> > +static u64 __head get_hv_features(void)
> >  {
> >       u64 val;
> >
> > -     if (ghcb_version < 2)
> > -             return 0;
>
> Why?
>

It is explained below ...
> > -
> >       sev_es_wr_ghcb_msr(GHCB_MSR_HV_FT_REQ);
> >       VMGEXIT();
> >
> > @@ -86,6 +80,31 @@ u64 get_hv_features(void)
> >       return GHCB_MSR_HV_FT_RESP_VAL(val);
> >  }
> >
> > +u64 __head snp_check_hv_features(void)
> > +{
> > +     /*
> > +      * SNP is supported in v2 of the GHCB spec which mandates support for HV
> > +      * features.
> > +      */

... get_hv_features() is only when SEV-SNP has already been detected.

> > +     if (sev_status & MSR_AMD64_SEV_SNP_ENABLED) {
> > +             u64 hv_features;
> > +
> > +             hv_features = get_hv_features();
> > +             if (!(hv_features & GHCB_HV_FT_SNP))
> > +                     sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
> > +
> > +             /*
> > +              * Running at VMPL0 is required unless an SVSM is present and
> > +              * the hypervisor supports the required SVSM GHCB events.
> > +              */
> > +             if (snp_vmpl > 0 && !(hv_features & GHCB_HV_FT_SNP_MULTI_VMPL))
> > +                     sev_es_terminate(SEV_TERM_SET_LINUX, GHCB_TERM_NOT_VMPL0);
> > +
> > +             return hv_features;
> > +     }
> > +     return 0;
> > +}
> > +
> >  int svsm_perform_msr_protocol(struct svsm_call *call)
> >  {
> >       u8 pending = 0;
> > diff --git a/arch/x86/boot/startup/sme.c b/arch/x86/boot/startup/sme.c
> > index 753cd2094080..0ae04e333f51 100644
> > --- a/arch/x86/boot/startup/sme.c
> > +++ b/arch/x86/boot/startup/sme.c
> > @@ -533,6 +533,8 @@ void __head sme_enable(struct boot_params *bp)
> >       if (snp_en ^ !!(msr & MSR_AMD64_SEV_SNP_ENABLED))
> >               snp_abort();
> >
> > +     sev_hv_features = snp_check_hv_features();
>
> Is this writing the sev_hv_features declared in
>
> arch/x86/boot/startup/sev-startup.c
>
> ?
>
> arch/x86/boot/startup/sev-startup.c:45:u64 sev_hv_features __ro_after_init;
> arch/x86/boot/startup/sme.c:536:        sev_hv_features = snp_check_hv_features();
> arch/x86/coco/sev/core.c:980:   if (!(sev_hv_features & GHCB_HV_FT_SNP_AP_CREATION))
> arch/x86/include/asm/sev-internal.h:5:extern u64 sev_hv_features;
> arch/x86/include/asm/sev.h:428:extern u64 sev_hv_features;
>
> I'm confused.
>
> If sev_hv_features is startup code, why isn't it accessed this way...?
>
> /me goes and looks forward in the set...
>
> oh my, that is coming.
>
> > +
> >       /* Check if memory encryption is enabled */
> >       if (feature_mask == AMD_SME_BIT) {
> >               if (!(bp->hdr.xloadflags & XLF_MEM_ENCRYPTION))
> > diff --git a/arch/x86/coco/sev/core.c b/arch/x86/coco/sev/core.c
> > index fa7fdd11a45b..fc4f6f188d42 100644
> > --- a/arch/x86/coco/sev/core.c
> > +++ b/arch/x86/coco/sev/core.c
> > @@ -1264,17 +1264,6 @@ void __init sev_es_init_vc_handling(void)
> >       if (!sev_es_check_cpu_features())
> >               panic("SEV-ES CPU Features missing");
> >
> > -     /*
> > -      * SNP is supported in v2 of the GHCB spec which mandates support for HV
> > -      * features.
> > -      */
> > -     if (cc_platform_has(CC_ATTR_GUEST_SEV_SNP)) {
> > -             sev_hv_features = get_hv_features();
> > -
> > -             if (!(sev_hv_features & GHCB_HV_FT_SNP))
> > -                     sev_es_terminate(SEV_TERM_SET_GEN, GHCB_SNP_UNSUPPORTED);
> > -     }
>
> I guess we would've terminated earlier anyway so that's probably ok...
>
> > -
> >       /* Initialize per-cpu GHCB pages */
> >       for_each_possible_cpu(cpu) {
> >               alloc_runtime_data(cpu);
>
> --
> Regards/Gruss,
>     Boris.
>
> https://people.kernel.org/tglx/notes-about-netiquette
Re: [RFT PATCH v3 12/21] x86/sev: Unify SEV-SNP hypervisor feature check
Posted by Borislav Petkov 8 months, 1 week ago
On Fri, May 30, 2025 at 04:28:52PM +0200, Ard Biesheuvel wrote:
> > > +u64 __head snp_check_hv_features(void)
> > > +{
> > > +     /*
> > > +      * SNP is supported in v2 of the GHCB spec which mandates support for HV
> > > +      * features.
> > > +      */
> 
> ... get_hv_features() is only when SEV-SNP has already been detected.

Hmm, I see

void sev_enable(struct boot_params *bp)
{
	...

        /*
         * Setup/preliminary detection of SNP. This will be sanity-checked
         * against CPUID/MSR values later.
         */
        snp = early_snp_init(bp);

	...

        snp_check_hv_features();

        if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))

This is called here without checking the snp boolean.

And without checking the version it is fragile anyway. Why do you even need to
remove the version check?

Just leave it in.

-- 
Regards/Gruss,
    Boris.

https://people.kernel.org/tglx/notes-about-netiquette
Re: [RFT PATCH v3 12/21] x86/sev: Unify SEV-SNP hypervisor feature check
Posted by Ard Biesheuvel 8 months, 1 week ago
On Fri, 30 May 2025 at 18:08, Borislav Petkov <bp@alien8.de> wrote:
>
> On Fri, May 30, 2025 at 04:28:52PM +0200, Ard Biesheuvel wrote:
> > > > +u64 __head snp_check_hv_features(void)
> > > > +{
> > > > +     /*
> > > > +      * SNP is supported in v2 of the GHCB spec which mandates support for HV
> > > > +      * features.
> > > > +      */
> >
> > ... get_hv_features() is only when SEV-SNP has already been detected.
>
> Hmm, I see
>
> void sev_enable(struct boot_params *bp)
> {
>         ...
>
>         /*
>          * Setup/preliminary detection of SNP. This will be sanity-checked
>          * against CPUID/MSR values later.
>          */
>         snp = early_snp_init(bp);
>
>         ...
>
>         snp_check_hv_features();
>
>         if (snp && !(sev_status & MSR_AMD64_SEV_SNP_ENABLED))
>
> This is called here without checking the snp boolean.
>
> And without checking the version it is fragile anyway. Why do you even need to
> remove the version check?
>

Because the assignment is moved out of the startup code later.
Re: [RFT PATCH v3 12/21] x86/sev: Unify SEV-SNP hypervisor feature check
Posted by Borislav Petkov 8 months, 1 week ago
On Fri, May 30, 2025 at 06:12:43PM +0200, Ard Biesheuvel wrote:
> Because the assignment is moved out of the startup code later.

Yeah, we will have to check ghcb version though.

-- 
Regards/Gruss,
    Boris.

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