diff mbox series

[Part1,RFC,v3,03/22] x86/sev: Save the negotiated GHCB version

Message ID 20210602140416.23573-4-brijesh.singh@amd.com
State Superseded
Headers show
Series Add AMD Secure Nested Paging (SEV-SNP) Guest Support | expand

Commit Message

Brijesh Singh June 2, 2021, 2:03 p.m. UTC
The SEV-ES guest calls the sev_es_negotiate_protocol() to negotiate the
GHCB protocol version before establishing the GHCB. Cache the negotiated
GHCB version so that it can be used later.

Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>
---
 arch/x86/include/asm/sev.h   |  2 +-
 arch/x86/kernel/sev-shared.c | 15 ++++++++++++---
 2 files changed, 13 insertions(+), 4 deletions(-)

Comments

Borislav Petkov June 3, 2021, 7:57 p.m. UTC | #1
On Wed, Jun 02, 2021 at 09:03:57AM -0500, Brijesh Singh wrote:
> +/*
> + * Since feature negotiation related variables are set early in the boot
> + * process they must reside in the .data section so as not to be zeroed
> + * out when the .bss section is later cleared.
> + */
Venu Busireddy June 8, 2021, 5:35 p.m. UTC | #2
On 2021-06-02 09:03:57 -0500, Brijesh Singh wrote:
> The SEV-ES guest calls the sev_es_negotiate_protocol() to negotiate the

> GHCB protocol version before establishing the GHCB. Cache the negotiated

> GHCB version so that it can be used later.

> 

> Signed-off-by: Brijesh Singh <brijesh.singh@amd.com>


Reviewed-by: Venu Busireddy <venu.busireddy@oracle.com>


> ---

>  arch/x86/include/asm/sev.h   |  2 +-

>  arch/x86/kernel/sev-shared.c | 15 ++++++++++++---

>  2 files changed, 13 insertions(+), 4 deletions(-)

> 

> diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h

> index fa5cd05d3b5b..7ec91b1359df 100644

> --- a/arch/x86/include/asm/sev.h

> +++ b/arch/x86/include/asm/sev.h

> @@ -12,7 +12,7 @@

>  #include <asm/insn.h>

>  #include <asm/sev-common.h>

>  

> -#define GHCB_PROTO_OUR		0x0001UL

> +#define GHCB_PROTOCOL_MIN	1ULL

>  #define GHCB_PROTOCOL_MAX	1ULL

>  #define GHCB_DEFAULT_USAGE	0ULL

>  

> diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c

> index de0e7e6c52b8..70f181f20d92 100644

> --- a/arch/x86/kernel/sev-shared.c

> +++ b/arch/x86/kernel/sev-shared.c

> @@ -14,6 +14,13 @@

>  #define has_cpuflag(f)	boot_cpu_has(f)

>  #endif

>  

> +/*

> + * Since feature negotiation related variables are set early in the boot

> + * process they must reside in the .data section so as not to be zeroed

> + * out when the .bss section is later cleared.

> + */

> +static u16 ghcb_version __section(".data");

> +

>  static bool __init sev_es_check_cpu_features(void)

>  {

>  	if (!has_cpuflag(X86_FEATURE_RDRAND)) {

> @@ -54,10 +61,12 @@ static bool sev_es_negotiate_protocol(void)

>  	if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP)

>  		return false;

>  

> -	if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTO_OUR ||

> -	    GHCB_MSR_PROTO_MIN(val) > GHCB_PROTO_OUR)

> +	if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN ||

> +	    GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX)

>  		return false;

>  

> +	ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val), GHCB_PROTOCOL_MAX);

> +

>  	return true;

>  }

>  

> @@ -101,7 +110,7 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,

>  	enum es_result ret;

>  

>  	/* Fill in protocol and format specifiers */

> -	ghcb->protocol_version = GHCB_PROTOCOL_MAX;

> +	ghcb->protocol_version = ghcb_version;

>  	ghcb->ghcb_usage       = GHCB_DEFAULT_USAGE;

>  

>  	ghcb_set_sw_exit_code(ghcb, exit_code);

> -- 

> 2.17.1

>
diff mbox series

Patch

diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
index fa5cd05d3b5b..7ec91b1359df 100644
--- a/arch/x86/include/asm/sev.h
+++ b/arch/x86/include/asm/sev.h
@@ -12,7 +12,7 @@ 
 #include <asm/insn.h>
 #include <asm/sev-common.h>
 
-#define GHCB_PROTO_OUR		0x0001UL
+#define GHCB_PROTOCOL_MIN	1ULL
 #define GHCB_PROTOCOL_MAX	1ULL
 #define GHCB_DEFAULT_USAGE	0ULL
 
diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c
index de0e7e6c52b8..70f181f20d92 100644
--- a/arch/x86/kernel/sev-shared.c
+++ b/arch/x86/kernel/sev-shared.c
@@ -14,6 +14,13 @@ 
 #define has_cpuflag(f)	boot_cpu_has(f)
 #endif
 
+/*
+ * Since feature negotiation related variables are set early in the boot
+ * process they must reside in the .data section so as not to be zeroed
+ * out when the .bss section is later cleared.
+ */
+static u16 ghcb_version __section(".data");
+
 static bool __init sev_es_check_cpu_features(void)
 {
 	if (!has_cpuflag(X86_FEATURE_RDRAND)) {
@@ -54,10 +61,12 @@  static bool sev_es_negotiate_protocol(void)
 	if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP)
 		return false;
 
-	if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTO_OUR ||
-	    GHCB_MSR_PROTO_MIN(val) > GHCB_PROTO_OUR)
+	if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN ||
+	    GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX)
 		return false;
 
+	ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val), GHCB_PROTOCOL_MAX);
+
 	return true;
 }
 
@@ -101,7 +110,7 @@  static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
 	enum es_result ret;
 
 	/* Fill in protocol and format specifiers */
-	ghcb->protocol_version = GHCB_PROTOCOL_MAX;
+	ghcb->protocol_version = ghcb_version;
 	ghcb->ghcb_usage       = GHCB_DEFAULT_USAGE;
 
 	ghcb_set_sw_exit_code(ghcb, exit_code);