Re: [RFC PATCH 5/12] HV: Add ghcb hvcall support for SNP VM
On 3/4/2021 1:21 AM, Vitaly Kuznetsov wrote: Tianyu Lan writes: From: Tianyu Lan Hyper-V provides ghcb hvcall to handle VMBus HVCALL_SIGNAL_EVENT and HVCALL_POST_MESSAGE msg in SNP Isolation VM. Add such support. Signed-off-by: Tianyu Lan --- arch/x86/hyperv/ivm.c | 69 + arch/x86/include/asm/mshyperv.h | 1 + drivers/hv/connection.c | 6 ++- drivers/hv/hv.c | 8 +++- 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 4332bf7aaf9b..feaabcd151f5 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -14,8 +14,77 @@ union hv_ghcb { struct ghcb ghcb; + struct { + u64 hypercalldata[509]; + u64 outputgpa; + union { + union { + struct { + u32 callcode: 16; + u32 isfast : 1; + u32 reserved1 : 14; + u32 isnested: 1; + u32 countofelements : 12; + u32 reserved2 : 4; + u32 repstartindex : 12; + u32 reserved3 : 4; + }; + u64 asuint64; + } hypercallinput; + union { + struct { + u16 callstatus; + u16 reserved1; + u32 elementsprocessed : 12; + u32 reserved2 : 20; + }; + u64 asunit64; + } hypercalloutput; + }; + u64 reserved2; + } hypercall; } __packed __aligned(PAGE_SIZE); +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size) +{ + union hv_ghcb *hv_ghcb; + void **ghcb_base; + unsigned long flags; + + if (!ms_hyperv.ghcb_base) + return -EFAULT; + + local_irq_save(flags); + ghcb_base = (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + hv_ghcb = (union hv_ghcb *)*ghcb_base; + if (!hv_ghcb) { + local_irq_restore(flags); + return -EFAULT; + } + + memset(hv_ghcb, 0x00, HV_HYP_PAGE_SIZE); + hv_ghcb->ghcb.protocol_version = 1; + hv_ghcb->ghcb.ghcb_usage = 1; + + hv_ghcb->hypercall.outputgpa = (u64)output; + hv_ghcb->hypercall.hypercallinput.asuint64 = 0; + hv_ghcb->hypercall.hypercallinput.callcode = control; + + if (input_size) + memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size); + + VMGEXIT(); + + hv_ghcb->ghcb.ghcb_usage = 0x; + memset(hv_ghcb->ghcb.save.valid_bitmap, 0, + sizeof(hv_ghcb->ghcb.save.valid_bitmap)); + + local_irq_restore(flags); + + return hv_ghcb->hypercall.hypercalloutput.callstatus; +} +EXPORT_SYMBOL_GPL(hv_ghcb_hypercall); + void hv_ghcb_msr_write(u64 msr, u64 value) { union hv_ghcb *hv_ghcb; diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h index f624d72b99d3..c8f66d269e5b 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -274,6 +274,7 @@ void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value); void hv_signal_eom_ghcb(void); void hv_ghcb_msr_write(u64 msr, u64 value); void hv_ghcb_msr_read(u64 msr, u64 *value); +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size); #define hv_get_synint_state_ghcb(int_num, val) \ hv_sint_rdmsrl_ghcb(HV_X64_MSR_SINT0 + int_num, val) diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index c83612cddb99..79bca653dce9 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -442,6 +442,10 @@ void vmbus_set_event(struct vmbus_channel *channel) ++channel->sig_events; - hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); + if (hv_isolation_type_snp()) + hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, >sig_event, + NULL, sizeof(u64)); + else + hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); vmbus_set_event() is a hotpath so I'd suggest we introduce a static branch instead of checking hv_isolation_type_snp() every time. Good suggestion. Will add it in the next version. Thanks.
Re: [RFC PATCH 5/12] HV: Add ghcb hvcall support for SNP VM
Tianyu Lan writes: > From: Tianyu Lan > > Hyper-V provides ghcb hvcall to handle VMBus > HVCALL_SIGNAL_EVENT and HVCALL_POST_MESSAGE > msg in SNP Isolation VM. Add such support. > > Signed-off-by: Tianyu Lan > --- > arch/x86/hyperv/ivm.c | 69 + > arch/x86/include/asm/mshyperv.h | 1 + > drivers/hv/connection.c | 6 ++- > drivers/hv/hv.c | 8 +++- > 4 files changed, 82 insertions(+), 2 deletions(-) > > diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c > index 4332bf7aaf9b..feaabcd151f5 100644 > --- a/arch/x86/hyperv/ivm.c > +++ b/arch/x86/hyperv/ivm.c > @@ -14,8 +14,77 @@ > > union hv_ghcb { > struct ghcb ghcb; > + struct { > + u64 hypercalldata[509]; > + u64 outputgpa; > + union { > + union { > + struct { > + u32 callcode: 16; > + u32 isfast : 1; > + u32 reserved1 : 14; > + u32 isnested: 1; > + u32 countofelements : 12; > + u32 reserved2 : 4; > + u32 repstartindex : 12; > + u32 reserved3 : 4; > + }; > + u64 asuint64; > + } hypercallinput; > + union { > + struct { > + u16 callstatus; > + u16 reserved1; > + u32 elementsprocessed : 12; > + u32 reserved2 : 20; > + }; > + u64 asunit64; > + } hypercalloutput; > + }; > + u64 reserved2; > + } hypercall; > } __packed __aligned(PAGE_SIZE); > > +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size) > +{ > + union hv_ghcb *hv_ghcb; > + void **ghcb_base; > + unsigned long flags; > + > + if (!ms_hyperv.ghcb_base) > + return -EFAULT; > + > + local_irq_save(flags); > + ghcb_base = (void **)this_cpu_ptr(ms_hyperv.ghcb_base); > + hv_ghcb = (union hv_ghcb *)*ghcb_base; > + if (!hv_ghcb) { > + local_irq_restore(flags); > + return -EFAULT; > + } > + > + memset(hv_ghcb, 0x00, HV_HYP_PAGE_SIZE); > + hv_ghcb->ghcb.protocol_version = 1; > + hv_ghcb->ghcb.ghcb_usage = 1; > + > + hv_ghcb->hypercall.outputgpa = (u64)output; > + hv_ghcb->hypercall.hypercallinput.asuint64 = 0; > + hv_ghcb->hypercall.hypercallinput.callcode = control; > + > + if (input_size) > + memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size); > + > + VMGEXIT(); > + > + hv_ghcb->ghcb.ghcb_usage = 0x; > + memset(hv_ghcb->ghcb.save.valid_bitmap, 0, > +sizeof(hv_ghcb->ghcb.save.valid_bitmap)); > + > + local_irq_restore(flags); > + > + return hv_ghcb->hypercall.hypercalloutput.callstatus; > +} > +EXPORT_SYMBOL_GPL(hv_ghcb_hypercall); > + > void hv_ghcb_msr_write(u64 msr, u64 value) > { > union hv_ghcb *hv_ghcb; > diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h > index f624d72b99d3..c8f66d269e5b 100644 > --- a/arch/x86/include/asm/mshyperv.h > +++ b/arch/x86/include/asm/mshyperv.h > @@ -274,6 +274,7 @@ void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value); > void hv_signal_eom_ghcb(void); > void hv_ghcb_msr_write(u64 msr, u64 value); > void hv_ghcb_msr_read(u64 msr, u64 *value); > +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 > input_size); > > #define hv_get_synint_state_ghcb(int_num, val) \ > hv_sint_rdmsrl_ghcb(HV_X64_MSR_SINT0 + int_num, val) > diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c > index c83612cddb99..79bca653dce9 100644 > --- a/drivers/hv/connection.c > +++ b/drivers/hv/connection.c > @@ -442,6 +442,10 @@ void vmbus_set_event(struct vmbus_channel *channel) > > ++channel->sig_events; > > - hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); > + if (hv_isolation_type_snp()) > + hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, >sig_event, > + NULL, sizeof(u64)); > + else > + hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); vmbus_set_event() is a hotpath so I'd suggest we introduce a static branch instead of checking hv_isolation_type_snp() every time. > } > EXPORT_SYMBOL_GPL(vmbus_set_event); > diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c > index 28e28ccc2081..6c64a7fd1ebd 100644 > --- a/drivers/hv/hv.c > +++ b/drivers/hv/hv.c > @@ -60,7 +60,13 @@ int
[RFC PATCH 5/12] HV: Add ghcb hvcall support for SNP VM
From: Tianyu Lan Hyper-V provides ghcb hvcall to handle VMBus HVCALL_SIGNAL_EVENT and HVCALL_POST_MESSAGE msg in SNP Isolation VM. Add such support. Signed-off-by: Tianyu Lan --- arch/x86/hyperv/ivm.c | 69 + arch/x86/include/asm/mshyperv.h | 1 + drivers/hv/connection.c | 6 ++- drivers/hv/hv.c | 8 +++- 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/arch/x86/hyperv/ivm.c b/arch/x86/hyperv/ivm.c index 4332bf7aaf9b..feaabcd151f5 100644 --- a/arch/x86/hyperv/ivm.c +++ b/arch/x86/hyperv/ivm.c @@ -14,8 +14,77 @@ union hv_ghcb { struct ghcb ghcb; + struct { + u64 hypercalldata[509]; + u64 outputgpa; + union { + union { + struct { + u32 callcode: 16; + u32 isfast : 1; + u32 reserved1 : 14; + u32 isnested: 1; + u32 countofelements : 12; + u32 reserved2 : 4; + u32 repstartindex : 12; + u32 reserved3 : 4; + }; + u64 asuint64; + } hypercallinput; + union { + struct { + u16 callstatus; + u16 reserved1; + u32 elementsprocessed : 12; + u32 reserved2 : 20; + }; + u64 asunit64; + } hypercalloutput; + }; + u64 reserved2; + } hypercall; } __packed __aligned(PAGE_SIZE); +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size) +{ + union hv_ghcb *hv_ghcb; + void **ghcb_base; + unsigned long flags; + + if (!ms_hyperv.ghcb_base) + return -EFAULT; + + local_irq_save(flags); + ghcb_base = (void **)this_cpu_ptr(ms_hyperv.ghcb_base); + hv_ghcb = (union hv_ghcb *)*ghcb_base; + if (!hv_ghcb) { + local_irq_restore(flags); + return -EFAULT; + } + + memset(hv_ghcb, 0x00, HV_HYP_PAGE_SIZE); + hv_ghcb->ghcb.protocol_version = 1; + hv_ghcb->ghcb.ghcb_usage = 1; + + hv_ghcb->hypercall.outputgpa = (u64)output; + hv_ghcb->hypercall.hypercallinput.asuint64 = 0; + hv_ghcb->hypercall.hypercallinput.callcode = control; + + if (input_size) + memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size); + + VMGEXIT(); + + hv_ghcb->ghcb.ghcb_usage = 0x; + memset(hv_ghcb->ghcb.save.valid_bitmap, 0, + sizeof(hv_ghcb->ghcb.save.valid_bitmap)); + + local_irq_restore(flags); + + return hv_ghcb->hypercall.hypercalloutput.callstatus; +} +EXPORT_SYMBOL_GPL(hv_ghcb_hypercall); + void hv_ghcb_msr_write(u64 msr, u64 value) { union hv_ghcb *hv_ghcb; diff --git a/arch/x86/include/asm/mshyperv.h b/arch/x86/include/asm/mshyperv.h index f624d72b99d3..c8f66d269e5b 100644 --- a/arch/x86/include/asm/mshyperv.h +++ b/arch/x86/include/asm/mshyperv.h @@ -274,6 +274,7 @@ void hv_sint_rdmsrl_ghcb(u64 msr, u64 *value); void hv_signal_eom_ghcb(void); void hv_ghcb_msr_write(u64 msr, u64 value); void hv_ghcb_msr_read(u64 msr, u64 *value); +u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size); #define hv_get_synint_state_ghcb(int_num, val) \ hv_sint_rdmsrl_ghcb(HV_X64_MSR_SINT0 + int_num, val) diff --git a/drivers/hv/connection.c b/drivers/hv/connection.c index c83612cddb99..79bca653dce9 100644 --- a/drivers/hv/connection.c +++ b/drivers/hv/connection.c @@ -442,6 +442,10 @@ void vmbus_set_event(struct vmbus_channel *channel) ++channel->sig_events; - hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); + if (hv_isolation_type_snp()) + hv_ghcb_hypercall(HVCALL_SIGNAL_EVENT, >sig_event, + NULL, sizeof(u64)); + else + hv_do_fast_hypercall8(HVCALL_SIGNAL_EVENT, channel->sig_event); } EXPORT_SYMBOL_GPL(vmbus_set_event); diff --git a/drivers/hv/hv.c b/drivers/hv/hv.c index 28e28ccc2081..6c64a7fd1ebd 100644 --- a/drivers/hv/hv.c +++ b/drivers/hv/hv.c @@ -60,7 +60,13 @@ int hv_post_message(union hv_connection_id connection_id, aligned_msg->payload_size = payload_size; memcpy((void *)aligned_msg->payload, payload, payload_size); - status = hv_do_hypercall(HVCALL_POST_MESSAGE, aligned_msg, NULL); + if (hv_isolation_type_snp()) +