diff mbox series

[V8,5/9] x86/sev-es: Expose sev_es_ghcb_hv_call() to call ghcb hv call out of sev code

Message ID 20211021154110.3734294-6-ltykernel@gmail.com (mailing list archive)
State Superseded
Headers show
Series x86/Hyper-V: Add Hyper-V Isolation VM support(First part) | expand

Checks

Context Check Description
netdev/tree_selection success Not a local patch

Commit Message

Tianyu Lan Oct. 21, 2021, 3:41 p.m. UTC
From: Tianyu Lan <Tianyu.Lan@microsoft.com>

Hyper-V needs to call ghcb hv call to write/read MSR in Isolation VM.
So expose sev_es_ghcb_hv_call() to call it in the Hyper-V code.

Hyper-V Isolation VM is unenlightened guests and run a paravisor in the
VMPL0 for communicating and GHCB pages are being allocated and set up by
that paravisor. Linux gets ghcb page pa via MSR_AMD64_SEV_ES_GHCB
from paravisor and should not change it. Add set_ghcb_msr parameter for
sev_es_ghcb_hv_call() and not set ghcb page pa when it's false.

Signed-off-by: Tianyu Lan <Tianyu.Lan@microsoft.com>
---
 arch/x86/include/asm/sev.h   | 12 ++++++++++++
 arch/x86/kernel/sev-shared.c | 26 +++++++++++++++++---------
 arch/x86/kernel/sev.c        | 13 +++++++------
 3 files changed, 36 insertions(+), 15 deletions(-)

Comments

Borislav Petkov Oct. 21, 2021, 4:22 p.m. UTC | #1
On Thu, Oct 21, 2021 at 11:41:05AM -0400, Tianyu Lan wrote:
> diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c
> index ea9abd69237e..368ed36971e3 100644
> --- a/arch/x86/kernel/sev-shared.c
> +++ b/arch/x86/kernel/sev-shared.c
> @@ -124,10 +124,9 @@ static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt
>  	return ES_VMM_ERROR;
>  }
>  
> -static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
> -					  struct es_em_ctxt *ctxt,
> -					  u64 exit_code, u64 exit_info_1,
> -					  u64 exit_info_2)
> +enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, bool set_ghcb_msr,
> +				   struct es_em_ctxt *ctxt, u64 exit_code,
> +				   u64 exit_info_1, u64 exit_info_2)
>  {
>  	/* Fill in protocol and format specifiers */
>  	ghcb->protocol_version = GHCB_PROTOCOL_MAX;
> @@ -137,7 +136,15 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
>  	ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
>  	ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
>  
> -	sev_es_wr_ghcb_msr(__pa(ghcb));
> +	/*
> +	 * Hyper-V unenlightened guests use a paravisor for communicating and
> +	 * GHCB pages are being allocated and set up by that paravisor. Linux
> +	 * should not change ghcb page pa in such case and so add set_ghcb_msr

"... not change the GHCB page's physical address."

Remove the "so add... " rest.

Otherwise, LGTM.

Do you want me to take it through the tip tree?

Thx.
Tianyu Lan Oct. 22, 2021, 1:39 p.m. UTC | #2
On 10/22/2021 12:22 AM, Borislav Petkov wrote:
> On Thu, Oct 21, 2021 at 11:41:05AM -0400, Tianyu Lan wrote:
>> diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c
>> index ea9abd69237e..368ed36971e3 100644
>> --- a/arch/x86/kernel/sev-shared.c
>> +++ b/arch/x86/kernel/sev-shared.c
>> @@ -124,10 +124,9 @@ static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt
>>   	return ES_VMM_ERROR;
>>   }
>>   
>> -static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
>> -					  struct es_em_ctxt *ctxt,
>> -					  u64 exit_code, u64 exit_info_1,
>> -					  u64 exit_info_2)
>> +enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, bool set_ghcb_msr,
>> +				   struct es_em_ctxt *ctxt, u64 exit_code,
>> +				   u64 exit_info_1, u64 exit_info_2)
>>   {
>>   	/* Fill in protocol and format specifiers */
>>   	ghcb->protocol_version = GHCB_PROTOCOL_MAX;
>> @@ -137,7 +136,15 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
>>   	ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
>>   	ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
>>   
>> -	sev_es_wr_ghcb_msr(__pa(ghcb));
>> +	/*
>> +	 * Hyper-V unenlightened guests use a paravisor for communicating and
>> +	 * GHCB pages are being allocated and set up by that paravisor. Linux
>> +	 * should not change ghcb page pa in such case and so add set_ghcb_msr
> 
> "... not change the GHCB page's physical address."
> 
> Remove the "so add... " rest.
> 
> Otherwise, LGTM.
> 
> Do you want me to take it through the tip tree?

Yes, please and this patch is based on the your clean up patch which is 
already in the tip sev branch.
Wei Liu Oct. 25, 2021, 11:20 a.m. UTC | #3
On Fri, Oct 22, 2021 at 09:39:48PM +0800, Tianyu Lan wrote:
> On 10/22/2021 12:22 AM, Borislav Petkov wrote:
> > On Thu, Oct 21, 2021 at 11:41:05AM -0400, Tianyu Lan wrote:
> > > diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c
> > > index ea9abd69237e..368ed36971e3 100644
> > > --- a/arch/x86/kernel/sev-shared.c
> > > +++ b/arch/x86/kernel/sev-shared.c
> > > @@ -124,10 +124,9 @@ static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt
> > >   	return ES_VMM_ERROR;
> > >   }
> > > -static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
> > > -					  struct es_em_ctxt *ctxt,
> > > -					  u64 exit_code, u64 exit_info_1,
> > > -					  u64 exit_info_2)
> > > +enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, bool set_ghcb_msr,
> > > +				   struct es_em_ctxt *ctxt, u64 exit_code,
> > > +				   u64 exit_info_1, u64 exit_info_2)
> > >   {
> > >   	/* Fill in protocol and format specifiers */
> > >   	ghcb->protocol_version = GHCB_PROTOCOL_MAX;
> > > @@ -137,7 +136,15 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
> > >   	ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
> > >   	ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
> > > -	sev_es_wr_ghcb_msr(__pa(ghcb));
> > > +	/*
> > > +	 * Hyper-V unenlightened guests use a paravisor for communicating and
> > > +	 * GHCB pages are being allocated and set up by that paravisor. Linux
> > > +	 * should not change ghcb page pa in such case and so add set_ghcb_msr
> > 
> > "... not change the GHCB page's physical address."
> > 
> > Remove the "so add... " rest.
> > 
> > Otherwise, LGTM.
> > 
> > Do you want me to take it through the tip tree?
> 
> Yes, please and this patch is based on the your clean up patch which is
> already in the tip sev branch.

Borislav, please take the whole series via the tip tree if possible.
That's perhaps the easiest thing for both of us because the rest of the
series depends on this patch. Or else I will have to base hyperv-next on
the tip tree once you merge this patch.

Let me know what you think.

Thanks,
Wei.
Borislav Petkov Oct. 25, 2021, 11:24 a.m. UTC | #4
On Mon, Oct 25, 2021 at 11:20:33AM +0000, Wei Liu wrote:
> Borislav, please take the whole series via the tip tree if possible.
> That's perhaps the easiest thing for both of us because the rest of the
> series depends on this patch. Or else I will have to base hyperv-next on
> the tip tree once you merge this patch.
>
> Let me know what you think.

You'll be able to simply merge the tip/x86/sev branch which will have it
and then base everything ontop.

However, there's still a question open - see my reply to Michael.

Thx.
Tianyu Lan Oct. 25, 2021, 12:27 p.m. UTC | #5
On 10/25/2021 7:24 PM, Borislav Petkov wrote:
> On Mon, Oct 25, 2021 at 11:20:33AM +0000, Wei Liu wrote:
>> Borislav, please take the whole series via the tip tree if possible.
>> That's perhaps the easiest thing for both of us because the rest of the
>> series depends on this patch. Or else I will have to base hyperv-next on
>> the tip tree once you merge this patch.
>>
>> Let me know what you think.
> 
> You'll be able to simply merge the tip/x86/sev branch which will have it
> and then base everything ontop.
> 
> However, there's still a question open - see my reply to Michael.

Hi Boris:
       I just sent out v9 version and compile hv ghcb related functions
when CONFIG_AMD_MEM_ENCRYPT is selected. The sev_es_ghcb_hv_call() stub
is not necessary in the series and remove it. Please have a look and
give your ack if it's ok. Then Wei can merge it through Hyper-V next
branch.

Thanks.
Borislav Petkov Oct. 25, 2021, 4:37 p.m. UTC | #6
On Mon, Oct 25, 2021 at 08:27:39PM +0800, Tianyu Lan wrote:
>       I just sent out v9 version and compile hv ghcb related functions
> when CONFIG_AMD_MEM_ENCRYPT is selected. The sev_es_ghcb_hv_call()
> stub is not necessary in the series and remove it. Please have a look
> and give your ack if it's ok. Then Wei can merge it through Hyper-V
> next branch.

I have merged it after running a bunch of randbuild tests and had to fix
one or two:

https://git.kernel.org/tip/007faec014cb5d26983c1f86fd08c6539b41392e

From my POV that branch is not going to change anymore so Wei can now
merge this tip branch - tip:x86/sev - and write a proper merge commit
message explaining why he's merging a tip branch and then apply the rest
of the HyperV stuff ontop.

Thx.
Wei Liu Oct. 25, 2021, 8:54 p.m. UTC | #7
On Mon, Oct 25, 2021 at 06:37:43PM +0200, Borislav Petkov wrote:
> On Mon, Oct 25, 2021 at 08:27:39PM +0800, Tianyu Lan wrote:
> >       I just sent out v9 version and compile hv ghcb related functions
> > when CONFIG_AMD_MEM_ENCRYPT is selected. The sev_es_ghcb_hv_call()
> > stub is not necessary in the series and remove it. Please have a look
> > and give your ack if it's ok. Then Wei can merge it through Hyper-V
> > next branch.
> 
> I have merged it after running a bunch of randbuild tests and had to fix
> one or two:
> 
> https://git.kernel.org/tip/007faec014cb5d26983c1f86fd08c6539b41392e
> 
> From my POV that branch is not going to change anymore so Wei can now
> merge this tip branch - tip:x86/sev - and write a proper merge commit
> message explaining why he's merging a tip branch and then apply the rest
> of the HyperV stuff ontop.

Thanks Borislav.

> 
> Thx.
> 
> -- 
> Regards/Gruss,
>     Boris.
> 
> https://people.kernel.org/tglx/notes-about-netiquette
diff mbox series

Patch

diff --git a/arch/x86/include/asm/sev.h b/arch/x86/include/asm/sev.h
index fa5cd05d3b5b..5b7f7e2b81f7 100644
--- a/arch/x86/include/asm/sev.h
+++ b/arch/x86/include/asm/sev.h
@@ -81,12 +81,24 @@  static __always_inline void sev_es_nmi_complete(void)
 		__sev_es_nmi_complete();
 }
 extern int __init sev_es_efi_map_ghcbs(pgd_t *pgd);
+extern enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
+					  bool set_ghcb_msr,
+					  struct es_em_ctxt *ctxt,
+					  u64 exit_code, u64 exit_info_1,
+					  u64 exit_info_2);
 #else
 static inline void sev_es_ist_enter(struct pt_regs *regs) { }
 static inline void sev_es_ist_exit(void) { }
 static inline int sev_es_setup_ap_jump_table(struct real_mode_header *rmh) { return 0; }
 static inline void sev_es_nmi_complete(void) { }
 static inline int sev_es_efi_map_ghcbs(pgd_t *pgd) { return 0; }
+static inline enum
+es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
+			      bool set_ghcb_msr, u64 exit_code,
+			      u64 exit_info_1, u64 exit_info_2)
+{
+	return ES_VMM_ERROR;
+}
 #endif
 
 #endif
diff --git a/arch/x86/kernel/sev-shared.c b/arch/x86/kernel/sev-shared.c
index ea9abd69237e..368ed36971e3 100644
--- a/arch/x86/kernel/sev-shared.c
+++ b/arch/x86/kernel/sev-shared.c
@@ -124,10 +124,9 @@  static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxt
 	return ES_VMM_ERROR;
 }
 
-static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
-					  struct es_em_ctxt *ctxt,
-					  u64 exit_code, u64 exit_info_1,
-					  u64 exit_info_2)
+enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, bool set_ghcb_msr,
+				   struct es_em_ctxt *ctxt, u64 exit_code,
+				   u64 exit_info_1, u64 exit_info_2)
 {
 	/* Fill in protocol and format specifiers */
 	ghcb->protocol_version = GHCB_PROTOCOL_MAX;
@@ -137,7 +136,15 @@  static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,
 	ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
 	ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
 
-	sev_es_wr_ghcb_msr(__pa(ghcb));
+	/*
+	 * Hyper-V unenlightened guests use a paravisor for communicating and
+	 * GHCB pages are being allocated and set up by that paravisor. Linux
+	 * should not change ghcb page pa in such case and so add set_ghcb_msr
+	 * parameter.  
+	 */
+	if (set_ghcb_msr)
+		sev_es_wr_ghcb_msr(__pa(ghcb));
+
 	VMGEXIT();
 
 	return verify_exception_info(ghcb, ctxt);
@@ -417,7 +424,7 @@  static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 		 */
 		sw_scratch = __pa(ghcb) + offsetof(struct ghcb, shared_buffer);
 		ghcb_set_sw_scratch(ghcb, sw_scratch);
-		ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO,
+		ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_IOIO,
 					  exit_info_1, exit_info_2);
 		if (ret != ES_OK)
 			return ret;
@@ -459,7 +466,8 @@  static enum es_result vc_handle_ioio(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 
 		ghcb_set_rax(ghcb, rax);
 
-		ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_IOIO, exit_info_1, 0);
+		ret = sev_es_ghcb_hv_call(ghcb, true, ctxt,
+					  SVM_EXIT_IOIO, exit_info_1, 0);
 		if (ret != ES_OK)
 			return ret;
 
@@ -490,7 +498,7 @@  static enum es_result vc_handle_cpuid(struct ghcb *ghcb,
 		/* xgetbv will cause #GP - use reset value for xcr0 */
 		ghcb_set_xcr0(ghcb, 1);
 
-	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_CPUID, 0, 0);
+	ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_CPUID, 0, 0);
 	if (ret != ES_OK)
 		return ret;
 
@@ -515,7 +523,7 @@  static enum es_result vc_handle_rdtsc(struct ghcb *ghcb,
 	bool rdtscp = (exit_code == SVM_EXIT_RDTSCP);
 	enum es_result ret;
 
-	ret = sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, 0, 0);
+	ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, exit_code, 0, 0);
 	if (ret != ES_OK)
 		return ret;
 
diff --git a/arch/x86/kernel/sev.c b/arch/x86/kernel/sev.c
index a6895e440bc3..bb62a1d15d6c 100644
--- a/arch/x86/kernel/sev.c
+++ b/arch/x86/kernel/sev.c
@@ -648,7 +648,8 @@  static enum es_result vc_handle_msr(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
 		ghcb_set_rdx(ghcb, regs->dx);
 	}
 
-	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_MSR, exit_info_1, 0);
+	ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_MSR,
+				  exit_info_1, 0);
 
 	if ((ret == ES_OK) && (!exit_info_1)) {
 		regs->ax = ghcb->save.rax;
@@ -867,7 +868,7 @@  static enum es_result vc_do_mmio(struct ghcb *ghcb, struct es_em_ctxt *ctxt,
 
 	ghcb_set_sw_scratch(ghcb, ghcb_pa + offsetof(struct ghcb, shared_buffer));
 
-	return sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, exit_info_2);
+	return sev_es_ghcb_hv_call(ghcb, true, ctxt, exit_code, exit_info_1, exit_info_2);
 }
 
 static enum es_result vc_handle_mmio_twobyte_ops(struct ghcb *ghcb,
@@ -1117,7 +1118,7 @@  static enum es_result vc_handle_dr7_write(struct ghcb *ghcb,
 
 	/* Using a value of 0 for ExitInfo1 means RAX holds the value */
 	ghcb_set_rax(ghcb, val);
-	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WRITE_DR7, 0, 0);
+	ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_WRITE_DR7, 0, 0);
 	if (ret != ES_OK)
 		return ret;
 
@@ -1147,7 +1148,7 @@  static enum es_result vc_handle_dr7_read(struct ghcb *ghcb,
 static enum es_result vc_handle_wbinvd(struct ghcb *ghcb,
 				       struct es_em_ctxt *ctxt)
 {
-	return sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_WBINVD, 0, 0);
+	return sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_WBINVD, 0, 0);
 }
 
 static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt)
@@ -1156,7 +1157,7 @@  static enum es_result vc_handle_rdpmc(struct ghcb *ghcb, struct es_em_ctxt *ctxt
 
 	ghcb_set_rcx(ghcb, ctxt->regs->cx);
 
-	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_RDPMC, 0, 0);
+	ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_RDPMC, 0, 0);
 	if (ret != ES_OK)
 		return ret;
 
@@ -1197,7 +1198,7 @@  static enum es_result vc_handle_vmmcall(struct ghcb *ghcb,
 	if (x86_platform.hyper.sev_es_hcall_prepare)
 		x86_platform.hyper.sev_es_hcall_prepare(ghcb, ctxt->regs);
 
-	ret = sev_es_ghcb_hv_call(ghcb, ctxt, SVM_EXIT_VMMCALL, 0, 0);
+	ret = sev_es_ghcb_hv_call(ghcb, true, ctxt, SVM_EXIT_VMMCALL, 0, 0);
 	if (ret != ES_OK)
 		return ret;