x86/Hyper-V: Add Hyper-V Isolation VM support(First part)
Subsystems (8)
v7 cover x86/Hyper-V: Add Hyper-V Isolation VM support(First part)
From: Tianyu Lan <[email protected]>
Hyper-V provides two kinds of Isolation VMs. VBS(Virtualization-based security) and AMD SEV-SNP unenlightened Isolation VMs. This patchset is to add support for these Isolation VM support in Linux.
The memory of these vms are encrypted and host can't access guest memory directly. Hyper-V provides new host visibility hvcall and the guest needs to call new hvcall to mark memory visible to host before sharing memory with host. For security, all network/storage stack memory should not be shared with host and so there is bounce buffer requests.
Vmbus channel ring buffer already plays bounce buffer role because all data from/to host needs to copy from/to between the ring buffer and IO stack memory. So mark vmbus channel ring buffer visible.
For SNP isolation VM, guest needs to access the shared memory via extra address space which is specified by Hyper-V CPUID HYPERV_CPUID_ ISOLATION_CONFIG. The access physical address of the shared memory should be bounce buffer memory GPA plus with shared_gpa_boundary reported by CPUID.
This patchset is rebased on the commit 02d5e016 of Linux mainline tree.
Change since v6
- Add hv_set_mem_host_visibility() when CONFIG_HYPERV is no. Fix compile error.
- Add comment to describe __set_memory_enc_pgtable().
- Split SEV change into patch "Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code"
- Add comment about calling memunmap() in the non-snp IVM.
Change since v5
- Replace HVPFN_UP() with PFN_UP() in the __vmbus_establish_gpadl()
- Remove unused variable gpadl in the __vmbus_open() and vmbus_close_ internal()
- Clean gpadl_handle in the vmbus_teardown_gpadl().
- Adjust change layout in the asm/mshyperv.h to make hv_is_synic_reg(), hv_get_register() and hv_set_register() ahead of the #include of asm-generic/mshyperv.h
- Change vmbus_connection.monitor_pages_pa type from unsigned long to phys_addr_t
Change since v4:
- Hide hv_mark_gpa_visibility() and set memory visibility via set_memory_encrypted/decrypted()
- Change gpadl handle in netvsc and uio driver from u32 to struct vmbus_gpadl.
- Change vmbus_establish_gpadl()'s gpadl_handle parameter
to vmbus_gpadl data structure.
- Remove hv_get_simp(), hv_get_siefp() hv_get_synint_*() helper function. Move the logic into hv_get/set_register().
- Use scsi_dma_map/unmap() instead of dma_map/unmap_sg() in storvsc driver.
- Allocate rx/tx ring buffer via alloc_pages() in Isolation VM
Change since V3:
- Initalize GHCB page in the cpu init callbac.
- Change vmbus_teardown_gpadl() parameter in order to mask the memory back to non-visible to host.
- Merge hv_ringbuffer_post_init() into hv_ringbuffer_init().
- Keep Hyper-V bounce buffer size as same as AMD SEV VM
- Use dma_map_sg() instead of dm_map_page() in the storvsc driver.
Change since V2:
- Drop x86_set_memory_enc static call and use platform check in the __set_memory_enc_dec() to run platform callback of set memory encrypted or decrypted.
Change since V1:
- Introduce x86_set_memory_enc static call and so platforms can override __set_memory_enc_dec() with their implementation
- Introduce sev_es_ghcb_hv_call_simple() and share code between SEV and Hyper-V code.
- Not remap monitor pages in the non-SNP isolation VM
- Make swiotlb_init_io_tlb_mem() return error code and return error when dma_map_decrypted() fails.
Change since RFC V4:
- Introduce dma map decrypted function to remap bounce buffer and provide dma map decrypted ops for platform to hook callback.
- Split swiotlb and dma map decrypted change into two patches
- Replace vstart with vaddr in swiotlb changes.
Change since RFC v3:
- Add interface set_memory_decrypted_map() to decrypt memory and
map bounce buffer in extra address space
- Remove swiotlb remap function and store the remap address
returned by set_memory_decrypted_map() in swiotlb mem data structure.
- Introduce hv_set_mem_enc() to make code more readable in the __set_memory_enc_dec().
Change since RFC v2:
- Remove not UIO driver in Isolation VM patch
- Use vmap_pfn() to replace ioremap_page_range function in order to avoid exposing symbol ioremap_page_range() and ioremap_page_range()
- Call hv set mem host visibility hvcall in set_memory_encrypted/decrypted()
- Enable swiotlb force mode instead of adding Hyper-V dma map/unmap hook
- Fix code style
Tianyu Lan (9):
x86/hyperv: Initialize GHCB page in Isolation VM x86/hyperv: Initialize shared memory boundary in the Isolation VM. x86/hyperv: Add new hvcall guest address host visibility support
Drivers: hv: vmbus: Mark vmbus ring buffer visible to host in
Isolation VM
x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of
sev code
x86/hyperv: Add Write/Read MSR registers via ghcb page x86/hyperv: Add ghcb hvcall support for SNP VM
Drivers: hv: vmbus: Add SNP support for VMbus channel initiate
message
Drivers: hv : vmbus: Initialize VMbus ring buffer for Isolation VM
arch/x86/hyperv/Makefile | 2 +- arch/x86/hyperv/hv_init.c | 78 ++++++-- arch/x86/hyperv/ivm.c | 282 +++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 17 ++ arch/x86/include/asm/mshyperv.h | 64 +++++-- arch/x86/include/asm/sev.h | 10 + arch/x86/kernel/cpu/mshyperv.c | 5 + arch/x86/kernel/sev-shared.c | 43 +++-- arch/x86/mm/pat/set_memory.c | 23 ++- drivers/hv/Kconfig | 1 + drivers/hv/channel.c | 72 +++++--- drivers/hv/connection.c | 101 ++++++++++- drivers/hv/hv.c | 82 +++++++-- drivers/hv/hv_common.c | 12 ++ drivers/hv/hyperv_vmbus.h | 2 + drivers/hv/ring_buffer.c | 55 ++++-- drivers/net/hyperv/hyperv_net.h | 5 +- drivers/net/hyperv/netvsc.c | 15 +- drivers/uio/uio_hv_generic.c | 18 +- include/asm-generic/hyperv-tlfs.h | 1 + include/asm-generic/mshyperv.h | 20 +- include/linux/hyperv.h | 12 +- 22 files changed, 783 insertions(+), 137 deletions(-) create mode 100644 arch/x86/hyperv/ivm.c
Signature · 1 line
2.25.1
PATCH v7 1/9 x86/hyperv: Initialize GHCB page in Isolation VM
From: Tianyu Lan <[email protected]>
Hyperv exposes GHCB page via SEV ES GHCB MSR for SNP guest to communicate with hypervisor. Map GHCB page for all cpus to read/write MSR register and submit hvcall request via ghcb page.
---
Change since v4:
* Fix typo comment
Chagne since v3:
* Rename ghcb_base to hv_ghcb_pg and move it out of struct ms_hyperv_info.
* Allocate hv_ghcb_pg before cpuhp_setup_state() and leverage hv_cpu_init() to initialize ghcb page.
--- arch/x86/hyperv/hv_init.c | 68 +++++++++++++++++++++++++++++---- arch/x86/include/asm/mshyperv.h | 4 ++ arch/x86/kernel/cpu/mshyperv.c | 3 ++ include/asm-generic/mshyperv.h | 1 + 4 files changed, 69 insertions(+), 7 deletions(-)
arch/x86/hyperv/hv_init.c +61 -7
@@ -20,6 +20,7 @@#include <linux/kexec.h> #include <linux/version.h> #include <linux/vmalloc.h> +#include <linux/io.h> #include <linux/mm.h> #include <linux/hyperv.h> #include <linux/slab.h>
@@ -36,12 +37,42 @@ EXPORT_SYMBOL_GPL(hv_current_partition_id);void *hv_hypercall_pg;EXPORT_SYMBOL_GPL (hv_hypercall_pg); +void __percpu **hv_ghcb_pg; +/* Storage to save the hypercall page temporarily for hibernation */ static void *hv_hypercall_pg_saved;struct hv_vp_assist_page **hv_vp_assist_page;EXPORT_SYMBOL_GPL (hv_vp_assist_page); +static int hyperv_init_ghcb (void ) +{ +u64 ghcb_gpa; +void *ghcb_va; +void **ghcb_base; + +if (!hv_isolation_type_snp ()) +return 0 ; + +if (!hv_ghcb_pg) +return -EINVAL ; + +/* +* GHCB page is allocated by paravisor. The address +* returned by MSR_AMD64_SEV_ES_GHCB is above shared +* memory boundary and map it here. +*/ +rdmsrl (MSR_AMD64_SEV_ES_GHCB , ghcb_gpa); + ghcb_va =memremap (ghcb_gpa,HV_HYP_PAGE_SIZE ,MEMREMAP_WB ); +if (!ghcb_va) +return -ENOMEM ; + + ghcb_base = (void **)this_cpu_ptr (hv_ghcb_pg); + *ghcb_base = ghcb_va; + +return 0 ; +} +static int hv_cpu_init (unsigned int cpu) {union hv_vp_assist_msr_contents msr = {0 };
@@ -85,7 +116,7 @@ static int hv_cpu_init(unsigned int cpu) } } -return 0 ; +return hyperv_init_ghcb (); }static void (*hv_reenlightenment_cb)(void );
@@ -177,6 +208,14 @@ static int hv_cpu_die(unsigned int cpu) {struct hv_reenlightenment_control re_ctrl;unsigned int new_cpu; +void **ghcb_va; + +if (hv_ghcb_pg) { + ghcb_va = (void **)this_cpu_ptr (hv_ghcb_pg); +if (*ghcb_va) +memunmap (*ghcb_va); + *ghcb_va =NULL ; + }hv_common_cpu_die (cpu);
@@ -366,10 +405,16 @@ void __init hyperv_init(void) goto common_free; } +if (hv_isolation_type_snp ()) { + hv_ghcb_pg = alloc_percpu(void *); +if (!hv_ghcb_pg) + goto free_vp_assist_page; + } + cpuhp =cpuhp_setup_state (CPUHP_AP_ONLINE_DYN ,"x86/hyperv_init:online" , hv_cpu_init, hv_cpu_die);if (cpuhp <0 ) - goto free_vp_assist_page; + goto free_ghcb_page;/* * Setup the hypercall page and enable hypercalls.
@@ -383,10 +428,8 @@ void __init hyperv_init(void)VMALLOC_END ,GFP_KERNEL ,PAGE_KERNEL_ROX ,VM_FLUSH_RESET_PERMS ,NUMA_NO_NODE , __builtin_return_address(0)); -if (hv_hypercall_pg ==NULL ) { -wrmsrl (HV_X64_MSR_GUEST_OS_ID ,0 ); - goto remove_cpuhp_state; - } +if (hv_hypercall_pg ==NULL ) + goto clean_guest_os_id;rdmsrl (HV_X64_MSR_HYPERCALL , hypercall_msr.as_uint64); hypercall_msr.enable =1 ;
@@ -456,8 +499,11 @@ void __init hyperv_init(void)hv_query_ext_cap (0 );return ; -remove_cpuhp_state: +clean_guest_os_id: +wrmsrl (HV_X64_MSR_GUEST_OS_ID ,0 );cpuhp_remove_state (cpuhp); +free_ghcb_page: +free_percpu (hv_ghcb_pg); free_vp_assist_page:kfree (hv_vp_assist_page); hv_vp_assist_page =NULL ;
@@ -559,3 +605,11 @@ bool hv_is_isolation_supported(void) {return hv_get_isolation_type () !=HV_ISOLATION_TYPE_NONE ; } + +DEFINE_STATIC_KEY_FALSE (isolation_type_snp); + +bool hv_isolation_type_snp (void ) +{ +return static_branch_unlikely (&isolation_type_snp); +} +EXPORT_SYMBOL_GPL (hv_isolation_type_snp);
arch/x86/include/asm/mshyperv.h +4 -0
@@ -11,6 +11,8 @@#include <asm/paravirt.h> #include <asm/mshyperv.h> +DECLARE_STATIC_KEY_FALSE (isolation_type_snp); +typedef int (*hyperv_fill_flush_list_func )(struct hv_guest_mapping_flush_list *flush,void *data);
@@ -39,6 +41,8 @@ extern void *hv_hypercall_pg;extern u64 hv_current_partition_id; +extern void __percpu **hv_ghcb_pg; +int hv_call_deposit_pages (int node,u64 partition_id,u32 num_pages);int hv_call_add_logical_proc (int node,u32 lp_index,u32 acpi_id);int hv_call_create_vp (int node,u64 partition_id,u32 vp_index,u32 flags);
arch/x86/kernel/cpu/mshyperv.c +3 -0
@@ -316,6 +316,9 @@ static void __init ms_hyperv_init_platform(void)pr_info ("Hyper-V: Isolation Config: Group A 0x%x, Group B 0x%x\n" , ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b); + +if (hv_get_isolation_type () ==HV_ISOLATION_TYPE_SNP ) +static_branch_enable (&isolation_type_snp); }if (hv_max_functions_eax >=HYPERV_CPUID_NESTED_FEATURES ) {
include/asm-generic/mshyperv.h +1 -0
@@ -254,6 +254,7 @@ bool hv_is_hyperv_initialized(void);bool hv_is_hibernation_supported (void );enum hv_isolation_type hv_get_isolation_type (void );bool hv_is_isolation_supported (void ); +bool hv_isolation_type_snp (void );void hyperv_cleanup (void );bool hv_query_ext_cap (u64 cap_query);#else /* CONFIG_HYPERV */
Signature · 1 line
2.25.1
PATCH v7 2/9 x86/hyperv: Initialize shared memory boundary in the Isolation VM.
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
Hyper-V exposes shared memory boundary via cpuid HYPERV_CPUID_ISOLATION_CONFIG and store it in the shared_gpa_boundary of ms_hyperv struct. This prepares to share memory with host for SNP guest.
---
Change since v4:
* Rename reserve field.
Change since v3:
* user BIT_ULL to get shared_gpa_boundary
* Rename field Reserved* to reserved
--- arch/x86/kernel/cpu/mshyperv.c | 2 ++ include/asm-generic/mshyperv.h | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-)
arch/x86/kernel/cpu/mshyperv.c +2 -0
@@ -313,6 +313,8 @@ static void __init ms_hyperv_init_platform(void)if (ms_hyperv.priv_high &HV_ISOLATION ) { ms_hyperv.isolation_config_a =cpuid_eax (HYPERV_CPUID_ISOLATION_CONFIG ); ms_hyperv.isolation_config_b =cpuid_ebx (HYPERV_CPUID_ISOLATION_CONFIG ); + ms_hyperv.shared_gpa_boundary = +BIT_ULL (ms_hyperv.shared_gpa_boundary_bits);pr_info ("Hyper-V: Isolation Config: Group A 0x%x, Group B 0x%x\n" , ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
include/asm-generic/mshyperv.h +11 -1
@@ -35,7 +35,17 @@ struct ms_hyperv_info {u32 max_vp_index;u32 max_lp_index;u32 isolation_config_a; -u32 isolation_config_b; +union { +u32 isolation_config_b; +struct { +u32 cvm_type :4 ; +u32 reserved1 :1 ; +u32 shared_gpa_boundary_active :1 ; +u32 shared_gpa_boundary_bits :6 ; +u32 reserved2 :20 ; + }; + }; +u64 shared_gpa_boundary; };extern struct ms_hyperv_info ms_hyperv;
Signature · 1 line
2.25.1
PATCH v7 3/9 x86/hyperv: Add new hvcall guest address host visibility support
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
Add new hvcall guest address host visibility support to mark memory visible to host. Call it inside set_memory_decrypted /encrypted(). Add HYPERVISOR feature check in the hv_is_isolation_supported() to optimize in non-virtualization environment.
---
Change since v6:
* Add hv_set_mem_host_visibility() when CONFIG_HYPERV is no. Fix compile error.
* Add comment to describe __set_memory_enc_pgtable().
Change since v4:
* Fix typo in the comment
* Make hv_mark_gpa_visibility() to be a static function
* Merge __hv_set_mem_host_visibility() and hv_set_mem_host_visibility()
Change since v3:
* Fix error code handle in the __hv_set_mem_host_visibility().
* Move HvCallModifySparseGpaPageHostVisibility near to enum hv_mem_host_visibility.
Change since v2:
* Rework __set_memory_enc_dec() and call Hyper-V and AMD function according to platform check.
Change since v1:
* Use new staic call x86_set_memory_enc to avoid add Hyper-V specific check in the set_memory code.
--- arch/x86/hyperv/Makefile | 2 +- arch/x86/hyperv/hv_init.c | 6 ++ arch/x86/hyperv/ivm.c | 105 +++++++++++++++++++++++++++++ arch/x86/include/asm/hyperv-tlfs.h | 17 +++++ arch/x86/include/asm/mshyperv.h | 7 +- arch/x86/mm/pat/set_memory.c | 23 +++++-- include/asm-generic/hyperv-tlfs.h | 1 + include/asm-generic/mshyperv.h | 1 + 8 files changed, 155 insertions(+), 7 deletions(-) create mode 100644 arch/x86/hyperv/ivm.c
arch/x86/hyperv/Makefile +1 -1
@@ -1,5 +1,5 @@ # SPDX-License-Identifier: GPL-2.0-only -obj-y := hv_init.o mmu.o nested.o irqdomain.o +obj-y := hv_init.o mmu.o nested.o irqdomain.o ivm.o obj-$(CONFIG_X86_64) += hv_apic.o hv_proc.o ifdef CONFIG_X86_64
arch/x86/hyperv/hv_init.c +6 -0
@@ -603,6 +603,12 @@ EXPORT_SYMBOL_GPL(hv_get_isolation_type);bool hv_is_isolation_supported (void ) { +if (!cpu_feature_enabled (X86_FEATURE_HYPERVISOR )) +return false; + +if (!hypervisor_is_type (X86_HYPER_MS_HYPERV )) +return false; +return hv_get_isolation_type () !=HV_ISOLATION_TYPE_NONE ; }
arch/x86/hyperv/ivm.c new file +105 -0
105 lines not on this page · raw message
arch/x86/include/asm/hyperv-tlfs.h +17 -0
@@ -276,6 +276,23 @@ enum hv_isolation_type {#define HV_X64_MSR_TIME_REF_COUNT HV_REGISTER_TIME_REF_COUNT#define HV_X64_MSR_REFERENCE_TSC HV_REGISTER_REFERENCE_TSC +/* Hyper-V memory host visibility */ +enum hv_mem_host_visibility { +VMBUS_PAGE_NOT_VISIBLE =0 , +VMBUS_PAGE_VISIBLE_READ_ONLY =1 , +VMBUS_PAGE_VISIBLE_READ_WRITE =3 +}; + +/* HvCallModifySparseGpaPageHostVisibility hypercall */ +#define HV_MAX_MODIFY_GPA_REP_COUNT ((PAGE_SIZE / sizeof(u64)) - 2) +struct hv_gpa_range_for_visibility { +u64 partition_id; +u32 host_visibility:2 ; +u32 reserved0:30 ; +u32 reserved1; +u64 gpa_page_list[HV_MAX_MODIFY_GPA_REP_COUNT ]; +} __packed; +/* * Declare the MSR used to setup pages used to communicate with the hypervisor. */
arch/x86/include/asm/mshyperv.h +6 -1
@@ -192,7 +192,7 @@ struct irq_domain *hv_create_pci_msi_domain(void);int hv_map_ioapic_interrupt (int ioapic_id,bool level,int vcpu,int vector,struct hv_interrupt_entry *entry);int hv_unmap_ioapic_interrupt (int ioapic_id,struct hv_interrupt_entry *entry); - +int hv_set_mem_host_visibility (unsigned long addr,int numpages,bool visible);#else /* CONFIG_HYPERV */ static inline void hyperv_init (void ) {}static inline void hyperv_setup_mmu_ops (void ) {}
@@ -209,6 +209,11 @@ static inline int hyperv_flush_guest_mapping_range(u64 as, {return -1 ; } +static inline int hv_set_mem_host_visibility (unsigned long addr,int numpages, +bool visible) +{ +return -1 ; +}#endif /* CONFIG_HYPERV */
arch/x86/mm/pat/set_memory.c +18 -5
@@ -29,6 +29,8 @@#include <asm/proto.h> #include <asm/memtype.h> #include <asm/set_memory.h> +#include <asm/hyperv-tlfs.h> +#include <asm/mshyperv.h> #include "../mm_internal.h"
@@ -1980,15 +1982,15 @@ int set_memory_global(unsigned long addr, int numpages) __pgprot(_PAGE_GLOBAL), 0); } -static int __set_memory_enc_dec (unsigned long addr,int numpages,bool enc) +/* +* __set_memory_enc_pgtable() is used for the hypervisors that get +* informed about "encryption" status via page tables. +*/ +static int __set_memory_enc_pgtable (unsigned long addr,int numpages,bool enc) {struct cpa_data cpa;int ret; -/* Nothing to do if memory encryption is not active */ -if (!mem_encrypt_active ()) -return 0 ; -/* Should not be working on unaligned addresses */ if (WARN_ONCE (addr & ~PAGE_MASK ,"misaligned address: %#lx\n" , addr)) addr &=PAGE_MASK ;
@@ -2023,6 +2025,17 @@ static int __set_memory_enc_dec(unsigned long addr, int numpages, bool enc)return ret; } +static int __set_memory_enc_dec (unsigned long addr,int numpages,bool enc) +{ +if (hv_is_isolation_supported ()) +return hv_set_mem_host_visibility (addr, numpages, !enc); + +if (mem_encrypt_active ()) +return __set_memory_enc_pgtable (addr, numpages, enc); + +return 0 ; +} +int set_memory_encrypted (unsigned long addr,int numpages) {return __set_memory_enc_dec (addr, numpages, true);
include/asm-generic/hyperv-tlfs.h +1 -0
@@ -158,6 +158,7 @@ struct ms_hyperv_tsc_page {#define HVCALL_RETARGET_INTERRUPT 0x007e#define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_SPACE 0x00af#define HVCALL_FLUSH_GUEST_PHYSICAL_ADDRESS_LIST 0x00b0 +#define HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY 0x00db/* Extended hypercalls */ #define HV_EXT_CALL_QUERY_CAPABILITIES 0x8001
include/asm-generic/mshyperv.h +1 -0
@@ -271,6 +271,7 @@ bool hv_query_ext_cap(u64 cap_query);static inline bool hv_is_hyperv_initialized (void ) {return false; }static inline bool hv_is_hibernation_supported (void ) {return false; }static inline void hyperv_cleanup (void ) {} +static inline bool hv_is_isolation_supported (void ) {return false; }#endif /* CONFIG_HYPERV */ #endif
Signature · 1 line
2.25.1
PATCH v7 4/9 Drivers: hv: vmbus: Mark vmbus ring buffer visible to host in Isolation VM
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
Mark vmbus ring buffer visible with set_memory_decrypted() when establish gpadl handle.
---
Change since v5:
* Replace HVPFN_UP() with PFN_UP() in the __vmbus_establish_gpadl()
* Remove unused variable gpadl in the __vmbus_open() and vmbus_close_ internal()
* Clean gpadl_handle in the vmbus_teardown_gpadl().
Change since v4
* Change gpadl handle in netvsc and uio driver from u32 to struct vmbus_gpadl.
* Change vmbus_establish_gpadl()'s gpadl_handle parameter to vmbus_gpadl data structure.
Change since v3:
* Change vmbus_teardown_gpadl() parameter and put gpadl handle, buffer and buffer size in the struct vmbus_gpadl.
--- drivers/hv/channel.c | 53 +++++++++++++++++++++++---------- drivers/net/hyperv/hyperv_net.h | 5 ++-- drivers/net/hyperv/netvsc.c | 15 +++++----- drivers/uio/uio_hv_generic.c | 18 +++++------ include/linux/hyperv.h | 12 ++++++-- 5 files changed, 65 insertions(+), 38 deletions(-)
drivers/hv/channel.c +38 -15
@@ -17,6 +17,7 @@#include <linux/hyperv.h> #include <linux/uio.h> #include <linux/interrupt.h> +#include <linux/set_memory.h> #include <asm/page.h> #include <asm/mshyperv.h>
@@ -456,7 +457,7 @@ static int create_gpadl_header(enum hv_gpadl_type type, void *kbuffer,static int __vmbus_establish_gpadl (struct vmbus_channel *channel,enum hv_gpadl_type type,void *kbuffer,u32 size,u32 send_offset, -u32 *gpadl_handle) +struct vmbus_gpadl *gpadl) {struct vmbus_channel_gpadl_header *gpadlmsg;struct vmbus_channel_gpadl_body *gpadl_body;
@@ -474,6 +475,15 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,if (ret)return ret; + ret =set_memory_decrypted ((unsigned long )kbuffer, +PFN_UP (size)); +if (ret) { +dev_warn (&channel->device_obj->device, +"Failed to set host visibility for new GPADL %d.\n" , + ret); +return ret; + } +init_completion (&msginfo->waitevent); msginfo->waiting_channel = channel;
@@ -537,7 +547,10 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel, }/* At this point, we received the gpadl created msg */ - *gpadl_handle = gpadlmsg->gpadl; + gpadl->gpadl_handle = gpadlmsg->gpadl; + gpadl->buffer = kbuffer; + gpadl->size = size; + cleanup:spin_lock_irqsave (&vmbus_connection.channelmsg_lock, flags);
@@ -549,6 +562,11 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel, }kfree (msginfo); + +if (ret) +set_memory_encrypted ((unsigned long )kbuffer, +PFN_UP (size)); +return ret; }
@@ -561,10 +579,10 @@ static int __vmbus_establish_gpadl(struct vmbus_channel *channel,* @gpadl_handle: some funky thing */ int vmbus_establish_gpadl (struct vmbus_channel *channel,void *kbuffer, -u32 size,u32 *gpadl_handle) +u32 size,struct vmbus_gpadl *gpadl) {return __vmbus_establish_gpadl (channel,HV_GPADL_BUFFER , kbuffer, size, -0U , gpadl_handle); +0U , gpadl); }EXPORT_SYMBOL_GPL (vmbus_establish_gpadl);
@@ -675,7 +693,7 @@ static int __vmbus_open(struct vmbus_channel *newchannel, goto error_clean_ring;/* Establish the gpadl for the ring buffer */ - newchannel->ringbuffer_gpadlhandle =0 ; + newchannel->ringbuffer_gpadlhandle.gpadl_handle =0 ; err = __vmbus_establish_gpadl(newchannel, HV_GPADL_RING, page_address(newchannel->ringbuffer_page),
@@ -701,7 +719,8 @@ static int __vmbus_open(struct vmbus_channel *newchannel, open_msg->header.msgtype =CHANNELMSG_OPENCHANNEL ; open_msg->openid = newchannel->offermsg.child_relid; open_msg->child_relid = newchannel->offermsg.child_relid; - open_msg->ringbuffer_gpadlhandle = newchannel->ringbuffer_gpadlhandle; + open_msg->ringbuffer_gpadlhandle + = newchannel->ringbuffer_gpadlhandle.gpadl_handle;/* * The unit of ->downstream_ringbuffer_pageoffset is HV_HYP_PAGE and * the unit of ->ringbuffer_send_offset (i.e. send_pages) is PAGE, so
@@ -759,8 +778,7 @@ static int __vmbus_open(struct vmbus_channel *newchannel, error_free_info:kfree (open_info); error_free_gpadl: -vmbus_teardown_gpadl (newchannel, newchannel->ringbuffer_gpadlhandle); - newchannel->ringbuffer_gpadlhandle =0 ; +vmbus_teardown_gpadl (newchannel, &newchannel->ringbuffer_gpadlhandle); error_clean_ring:hv_ringbuffer_cleanup (&newchannel->outbound);hv_ringbuffer_cleanup (&newchannel->inbound);
@@ -806,7 +824,7 @@ EXPORT_SYMBOL_GPL(vmbus_open);/* * vmbus_teardown_gpadl -Teardown the specified GPADL handle */ -int vmbus_teardown_gpadl (struct vmbus_channel *channel,u32 gpadl_handle) +int vmbus_teardown_gpadl (struct vmbus_channel *channel,struct vmbus_gpadl *gpadl) {struct vmbus_channel_gpadl_teardown *msg;struct vmbus_channel_msginfo *info;
@@ -825,7 +843,7 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle) msg->header.msgtype =CHANNELMSG_GPADL_TEARDOWN ; msg->child_relid = channel->offermsg.child_relid; - msg->gpadl = gpadl_handle; + msg->gpadl = gpadl->gpadl_handle;spin_lock_irqsave (&vmbus_connection.channelmsg_lock, flags); list_add_tail(&info->msglistentry,
@@ -845,6 +863,8 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)wait_for_completion (&info->waitevent); + gpadl->gpadl_handle =0 ; + post_msg_err:/* * If the channel has been rescinded;
@@ -859,6 +879,12 @@ int vmbus_teardown_gpadl(struct vmbus_channel *channel, u32 gpadl_handle)spin_unlock_irqrestore (&vmbus_connection.channelmsg_lock, flags);kfree (info); + + ret =set_memory_encrypted ((unsigned long )gpadl->buffer, +PFN_UP (gpadl->size)); +if (ret) +pr_warn ("Fail to set mem host visibility in GPADL teardown %d.\n" , ret); +return ret; }EXPORT_SYMBOL_GPL (vmbus_teardown_gpadl);
@@ -933,9 +959,8 @@ static int vmbus_close_internal(struct vmbus_channel *channel) }/* Tear down the gpadl for the channel's ring buffer */ -else if (channel->ringbuffer_gpadlhandle) { - ret =vmbus_teardown_gpadl (channel, - channel->ringbuffer_gpadlhandle); +else if (channel->ringbuffer_gpadlhandle.gpadl_handle) { + ret =vmbus_teardown_gpadl (channel, &channel->ringbuffer_gpadlhandle);if (ret) {pr_err ("Close failed: teardown gpadl return %d\n" , ret);/*
@@ -943,8 +968,6 @@ static int vmbus_close_internal(struct vmbus_channel *channel)* it is perhaps better to leak memory. */ } - - channel->ringbuffer_gpadlhandle =0 ; }if (!ret)
drivers/net/hyperv/hyperv_net.h +3 -2
@@ -1075,14 +1075,15 @@ struct netvsc_device {/* Receive buffer allocated by us but manages by NetVSP */ void *recv_buf;u32 recv_buf_size;/* allocated bytes */ -u32 recv_buf_gpadl_handle; +struct vmbus_gpadl recv_buf_gpadl_handle;u32 recv_section_cnt;u32 recv_section_size;u32 recv_completion_cnt;/* Send buffer allocated by us */ void *send_buf; -u32 send_buf_gpadl_handle; +u32 send_buf_size; +struct vmbus_gpadl send_buf_gpadl_handle;u32 send_section_cnt;u32 send_section_size;unsigned long *send_section_map;
drivers/net/hyperv/netvsc.c +7 -8
@@ -278,9 +278,9 @@ static void netvsc_teardown_recv_gpadl(struct hv_device *device, {int ret; -if (net_device->recv_buf_gpadl_handle) { +if (net_device->recv_buf_gpadl_handle.gpadl_handle) { ret =vmbus_teardown_gpadl (device->channel, - net_device->recv_buf_gpadl_handle); + &net_device->recv_buf_gpadl_handle);/* If we failed here, we might as well return and have a leak * rather than continue and a bugchk
@@ -290,7 +290,6 @@ static void netvsc_teardown_recv_gpadl(struct hv_device *device, "unable to teardown receive buffer's gpadl\n");return ; } - net_device->recv_buf_gpadl_handle =0 ; } }
@@ -300,9 +299,9 @@ static void netvsc_teardown_send_gpadl(struct hv_device *device, {int ret; -if (net_device->send_buf_gpadl_handle) { +if (net_device->send_buf_gpadl_handle.gpadl_handle) { ret =vmbus_teardown_gpadl (device->channel, - net_device->send_buf_gpadl_handle); + &net_device->send_buf_gpadl_handle);/* If we failed here, we might as well return and have a leak * rather than continue and a bugchk
@@ -312,7 +311,6 @@ static void netvsc_teardown_send_gpadl(struct hv_device *device, "unable to teardown send buffer's gpadl\n");return ; } - net_device->send_buf_gpadl_handle =0 ; } }
@@ -380,7 +378,7 @@ static int netvsc_init_buf(struct hv_device *device,memset (init_packet,0 ,sizeof (struct nvsp_message )); init_packet->hdr.msg_type =NVSP_MSG1_TYPE_SEND_RECV_BUF ; init_packet->msg.v1_msg.send_recv_buf. - gpadl_handle = net_device->recv_buf_gpadl_handle; + gpadl_handle = net_device->recv_buf_gpadl_handle.gpadl_handle; init_packet->msg.v1_msg. send_recv_buf.id =NETVSC_RECEIVE_BUFFER_ID ;
@@ -463,6 +461,7 @@ static int netvsc_init_buf(struct hv_device *device, ret = -ENOMEM ; goto cleanup; } + net_device->send_buf_size = buf_size;/* Establish the gpadl handle for this buffer on this * channel. Note: This call uses the vmbus connection rather
@@ -482,7 +481,7 @@ static int netvsc_init_buf(struct hv_device *device,memset (init_packet,0 ,sizeof (struct nvsp_message )); init_packet->hdr.msg_type =NVSP_MSG1_TYPE_SEND_SEND_BUF ; init_packet->msg.v1_msg.send_send_buf.gpadl_handle = - net_device->send_buf_gpadl_handle; + net_device->send_buf_gpadl_handle.gpadl_handle; init_packet->msg.v1_msg.send_send_buf.id =NETVSC_SEND_BUFFER_ID ;trace_nvsp_send (ndev, init_packet);
drivers/uio/uio_hv_generic.c +8 -10
@@ -58,11 +58,11 @@ struct hv_uio_private_data {atomic_t refcnt;void *recv_buf; -u32 recv_gpadl; +struct vmbus_gpadl recv_gpadl;char recv_name[32 ];/* "recv_4294967295" */ void *send_buf; -u32 send_gpadl; +struct vmbus_gpadl send_gpadl;char send_name[32 ]; };
@@ -179,15 +179,13 @@ hv_uio_new_channel(struct vmbus_channel *new_sc)static void hv_uio_cleanup (struct hv_device *dev,struct hv_uio_private_data *pdata) { -if (pdata->send_gpadl) { -vmbus_teardown_gpadl (dev->channel, pdata->send_gpadl); - pdata->send_gpadl =0 ; +if (pdata->send_gpadl.gpadl_handle) { +vmbus_teardown_gpadl (dev->channel, &pdata->send_gpadl);vfree (pdata->send_buf); } -if (pdata->recv_gpadl) { -vmbus_teardown_gpadl (dev->channel, pdata->recv_gpadl); - pdata->recv_gpadl =0 ; +if (pdata->recv_gpadl.gpadl_handle) { +vmbus_teardown_gpadl (dev->channel, &pdata->recv_gpadl);vfree (pdata->recv_buf); } }
@@ -303,7 +301,7 @@ hv_uio_probe(struct hv_device *dev,/* put Global Physical Address Label in name */ snprintf (pdata->recv_name,sizeof (pdata->recv_name), -"recv:%u" , pdata->recv_gpadl); +"recv:%u" , pdata->recv_gpadl.gpadl_handle); pdata->info.mem[RECV_BUF_MAP ].name = pdata->recv_name; pdata->info.mem[RECV_BUF_MAP ].addr = (uintptr_t )pdata->recv_buf;
@@ -324,7 +322,7 @@ hv_uio_probe(struct hv_device *dev, }snprintf (pdata->send_name,sizeof (pdata->send_name), -"send:%u" , pdata->send_gpadl); +"send:%u" , pdata->send_gpadl.gpadl_handle); pdata->info.mem[SEND_BUF_MAP ].name = pdata->send_name; pdata->info.mem[SEND_BUF_MAP ].addr = (uintptr_t )pdata->send_buf;
include/linux/hyperv.h +9 -3
@@ -803,6 +803,12 @@ struct vmbus_device {#define VMBUS_DEFAULT_MAX_PKT_SIZE 4096 +struct vmbus_gpadl { +u32 gpadl_handle; +u32 size; +void *buffer; +}; +struct vmbus_channel {struct list_head listentry;
@@ -822,7 +828,7 @@ struct vmbus_channel {bool rescind_ref;/* got rescind msg, got channel reference */ struct completion rescind_event; -u32 ringbuffer_gpadlhandle; +struct vmbus_gpadl ringbuffer_gpadlhandle;/* Allocated memory for ring buffer */ struct page *ringbuffer_page;
@@ -1192,10 +1198,10 @@ extern int vmbus_sendpacket_mpb_desc(struct vmbus_channel *channel,extern int vmbus_establish_gpadl (struct vmbus_channel *channel,void *kbuffer,u32 size, -u32 *gpadl_handle); +struct vmbus_gpadl *gpadl);extern int vmbus_teardown_gpadl (struct vmbus_channel *channel, -u32 gpadl_handle); +struct vmbus_gpadl *gpadl);void vmbus_reset_channel_cb (struct vmbus_channel *channel);
Signature · 1 line
2.25.1
PATCH v7 5/9 x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
Hyper-V also 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.
--- arch/x86/include/asm/sev.h | 10 +++++++++ arch/x86/kernel/sev-shared.c | 43 +++++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 13 deletions(-)
arch/x86/include/asm/sev.h +10 -0
@@ -81,12 +81,22 @@ 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, +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, +u64 exit_code,u64 exit_info_1, +u64 exit_info_2) +{ +return ES_VMM_ERROR ; +}#endif #endif
arch/x86/kernel/sev-shared.c +30 -13
66 lines not on this page · raw message
Signature · 1 line
2.25.1
PATCH v7 6/9 x86/hyperv: Add Write/Read MSR registers via ghcb page
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
Hyperv provides GHCB protocol to write Synthetic Interrupt Controller MSR registers in Isolation VM with AMD SEV SNP and these registers are emulated by hypervisor directly. Hyperv requires to write SINTx MSR registers twice. First writes MSR via GHCB page to communicate with hypervisor and then writes wrmsr instruction to talk with paravisor which runs in VMPL0. Guest OS ID MSR also needs to be set via GHCB page.
---
Change since v6:
* Spilt sev-es code into separate patch
* Add hv_get/set_register() dummy function under CONFIG_HYPERV is not selected to fix compile error.
Change since v5:
* Adjust change layout in the asm/mshyperv.h to make hv_is_synic_reg(), hv_get_register() and hv_set_register() ahead of the #include of asm-generic/mshyperv.h
* Remove Spurious blank line
Change since v4:
* Remove hv_get_simp(), hv_get_siefp() hv_get_synint_*() helper function. Move the logic into hv_get/set_register().
Change since v3:
* Pass old_msg_type to hv_signal_eom() as parameter.
* Use HV_REGISTER_* marcro instead of HV_X64_MSR_*
* Add hv_isolation_type_snp() weak function.
* Add maros to set syinc register in ARM code.
Change since v1:
* Introduce sev_es_ghcb_hv_call_simple() and share code between SEV and Hyper-V code.
--- arch/x86/hyperv/hv_init.c | 36 +++-------- arch/x86/hyperv/ivm.c | 103 ++++++++++++++++++++++++++++++++ arch/x86/include/asm/mshyperv.h | 57 +++++++++++++----- drivers/hv/hv.c | 74 ++++++++++++++++++----- drivers/hv/hv_common.c | 6 ++ include/asm-generic/mshyperv.h | 5 ++ 6 files changed, 222 insertions(+), 59 deletions(-)
arch/x86/hyperv/hv_init.c +7 -29
@@ -37,7 +37,7 @@ EXPORT_SYMBOL_GPL(hv_current_partition_id);void *hv_hypercall_pg;EXPORT_SYMBOL_GPL (hv_hypercall_pg); -void __percpu **hv_ghcb_pg; +union hv_ghcb __percpu **hv_ghcb_pg;/* Storage to save the hypercall page temporarily for hibernation */ static void *hv_hypercall_pg_saved;
@@ -406,7 +406,7 @@ void __init hyperv_init(void) }if (hv_isolation_type_snp ()) { - hv_ghcb_pg = alloc_percpu(void *); + hv_ghcb_pg = alloc_percpu(union hv_ghcb *);if (!hv_ghcb_pg) goto free_vp_assist_page; }
@@ -424,6 +424,9 @@ void __init hyperv_init(void) guest_id =generate_guest_id (0 ,LINUX_VERSION_CODE ,0 );wrmsrl (HV_X64_MSR_GUEST_OS_ID , guest_id); +/* Hyper-V requires to write guest os id via ghcb in SNP IVM. */ +hv_ghcb_msr_write (HV_X64_MSR_GUEST_OS_ID , guest_id); + hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
@@ -501,6 +504,7 @@ void __init hyperv_init(void) clean_guest_os_id:wrmsrl (HV_X64_MSR_GUEST_OS_ID ,0 ); +hv_ghcb_msr_write (HV_X64_MSR_GUEST_OS_ID ,0 );cpuhp_remove_state (cpuhp); free_ghcb_page:free_percpu (hv_ghcb_pg);
@@ -522,6 +526,7 @@ void hyperv_cleanup(void)/* Reset our OS id */ wrmsrl (HV_X64_MSR_GUEST_OS_ID ,0 ); +hv_ghcb_msr_write (HV_X64_MSR_GUEST_OS_ID ,0 );/* * Reset hypercall page reference before reset the page,
@@ -592,30 +597,3 @@ bool hv_is_hyperv_initialized(void)return hypercall_msr.enable; }EXPORT_SYMBOL_GPL (hv_is_hyperv_initialized); - -enum hv_isolation_type hv_get_isolation_type (void ) -{ -if (!(ms_hyperv.priv_high &HV_ISOLATION )) -return HV_ISOLATION_TYPE_NONE ; -return FIELD_GET (HV_ISOLATION_TYPE , ms_hyperv.isolation_config_b); -} -EXPORT_SYMBOL_GPL (hv_get_isolation_type); - -bool hv_is_isolation_supported (void ) -{ -if (!cpu_feature_enabled (X86_FEATURE_HYPERVISOR )) -return false; - -if (!hypervisor_is_type (X86_HYPER_MS_HYPERV )) -return false; - -return hv_get_isolation_type () !=HV_ISOLATION_TYPE_NONE ; -} - -DEFINE_STATIC_KEY_FALSE (isolation_type_snp); - -bool hv_isolation_type_snp (void ) -{ -return static_branch_unlikely (&isolation_type_snp); -} -EXPORT_SYMBOL_GPL (hv_isolation_type_snp);
arch/x86/hyperv/ivm.c +103 -0
115 lines not on this page · raw message
arch/x86/include/asm/mshyperv.h +43 -14
87 lines not on this page · raw message
drivers/hv/hv.c +58 -16
117 lines not on this page · raw message
drivers/hv/hv_common.c +6 -0
@@ -249,6 +249,12 @@ bool __weak hv_is_isolation_supported(void) }EXPORT_SYMBOL_GPL (hv_is_isolation_supported); +bool __weak hv_isolation_type_snp (void ) +{ +return false; +} +EXPORT_SYMBOL_GPL (hv_isolation_type_snp); +void __weak hv_setup_vmbus_handler (void (*handler)(void )) { }
include/asm-generic/mshyperv.h +5 -0
@@ -54,6 +54,7 @@ extern void __percpu **hyperv_pcpu_output_arg;extern u64 hv_do_hypercall (u64 control,void *inputaddr,void *outputaddr);extern u64 hv_do_fast_hypercall8 (u16 control,u64 input8); +extern bool hv_isolation_type_snp (void );/* Helper functions that provide a consistent pattern for checking Hyper-V hypercall status. */ static inline int hv_result (u64 status)
@@ -272,6 +273,10 @@ static inline bool hv_is_hyperv_initialized(void) { return false; }static inline bool hv_is_hibernation_supported (void ) {return false; }static inline void hyperv_cleanup (void ) {}static inline bool hv_is_isolation_supported (void ) {return false; } +static inline enum hv_isolation_type hv_get_isolation_type (void ) +{ +return HV_ISOLATION_TYPE_NONE ; +}#endif /* CONFIG_HYPERV */ #endif
Signature · 1 line
2.25.1
PATCH v7 8/9 Drivers: hv: vmbus: Add SNP support for VMbus channel initiate message
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
The monitor pages in the CHANNELMSG_INITIATE_CONTACT msg are shared with host in Isolation VM and so it's necessary to use hvcall to set them visible to host. In Isolation VM with AMD SEV SNP, the access address should be in the extra space which is above shared gpa boundary. So remap these pages into the extra address(pa + shared_gpa_boundary).
Introduce monitor_pages_original[] in the struct vmbus_connection to store monitor page virtual address returned by hv_alloc_hyperv_ zeroed_page() and free monitor page via monitor_pages_original in the vmbus_disconnect(). The monitor_pages[] is to used to access monitor page and it is initialized to be equal with monitor_pages_ original. The monitor_pages[] will be overridden in the isolation VM with va of extra address. Introduce monitor_pages_pa[] to store monitor pages' physical address and use it to populate pa in the initiate msg.
---
Change since v6:
* Add comment about calling memunmap() in the non-snp IVM.
Change since v5:
* change vmbus_connection.monitor_pages_pa type from unsigned long to phys_addr_t
* Plus vmbus_connection.monitor_pages_pa with ms_hyperv. shared_gpa_boundary only in the IVM with AMD SEV.
Change since v4:
* Introduce monitor_pages_pa[] to store monitor pages' physical address and use it to populate pa in the initiate msg.
* Move code of mapping moniter pages in extra address into vmbus_connect().
Change since v3:
* Rename monitor_pages_va with monitor_pages_original
* free monitor page via monitor_pages_original and monitor_pages is used to access monitor page.
Change since v1:
* Not remap monitor pages in the non-SNP isolation VM.
--- drivers/hv/connection.c | 95 ++++++++++++++++++++++++++++++++++++--- drivers/hv/hyperv_vmbus.h | 2 + 2 files changed, 91 insertions(+), 6 deletions(-)
drivers/hv/connection.c +89 -6
@@ -19,6 +19,8 @@#include <linux/vmalloc.h> #include <linux/hyperv.h> #include <linux/export.h> +#include <linux/io.h> +#include <linux/set_memory.h> #include <asm/mshyperv.h> #include "hyperv_vmbus.h"
@@ -102,8 +104,9 @@ int vmbus_negotiate_version(struct vmbus_channel_msginfo *msginfo, u32 version) vmbus_connection.msg_conn_id =VMBUS_MESSAGE_CONNECTION_ID ; } - msg->monitor_page1 =virt_to_phys (vmbus_connection.monitor_pages[0 ]); - msg->monitor_page2 =virt_to_phys (vmbus_connection.monitor_pages[1 ]); + msg->monitor_page1 = vmbus_connection.monitor_pages_pa[0 ]; + msg->monitor_page2 = vmbus_connection.monitor_pages_pa[1 ]; + msg->target_vcpu =hv_cpu_number_to_vp_number (VMBUS_CONNECT_CPU );/*
@@ -216,6 +219,65 @@ int vmbus_connect(void) goto cleanup; } + vmbus_connection.monitor_pages_original[0 ] + = vmbus_connection.monitor_pages[0 ]; + vmbus_connection.monitor_pages_original[1 ] + = vmbus_connection.monitor_pages[1 ]; + vmbus_connection.monitor_pages_pa[0 ] + =virt_to_phys (vmbus_connection.monitor_pages[0 ]); + vmbus_connection.monitor_pages_pa[1 ] + =virt_to_phys (vmbus_connection.monitor_pages[1 ]); + +if (hv_is_isolation_supported ()) { + ret =set_memory_decrypted ((unsigned long ) + vmbus_connection.monitor_pages[0 ], +1 ); + ret |=set_memory_decrypted ((unsigned long ) + vmbus_connection.monitor_pages[1 ], +1 ); +if (ret) + goto cleanup; + +/* +* Isolation VM with AMD SNP needs to access monitor page via +* address space above shared gpa boundary. +*/ +if (hv_isolation_type_snp ()) { + vmbus_connection.monitor_pages_pa[0 ] += + ms_hyperv.shared_gpa_boundary; + vmbus_connection.monitor_pages_pa[1 ] += + ms_hyperv.shared_gpa_boundary; + + vmbus_connection.monitor_pages[0 ] + =memremap (vmbus_connection.monitor_pages_pa[0 ], +HV_HYP_PAGE_SIZE , +MEMREMAP_WB ); +if (!vmbus_connection.monitor_pages[0 ]) { + ret = -ENOMEM ; + goto cleanup; + } + + vmbus_connection.monitor_pages[1 ] + =memremap (vmbus_connection.monitor_pages_pa[1 ], +HV_HYP_PAGE_SIZE , +MEMREMAP_WB ); +if (!vmbus_connection.monitor_pages[1 ]) { + ret = -ENOMEM ; + goto cleanup; + } + } + +/* +* Set memory host visibility hvcall smears memory +* and so zero monitor pages here. +*/ +memset (vmbus_connection.monitor_pages[0 ],0x00 , +HV_HYP_PAGE_SIZE ); +memset (vmbus_connection.monitor_pages[1 ],0x00 , +HV_HYP_PAGE_SIZE ); + + } + msginfo =kzalloc (sizeof (*msginfo) +sizeof (struct vmbus_channel_initiate_contact ),GFP_KERNEL );
@@ -303,10 +365,31 @@ void vmbus_disconnect(void) vmbus_connection.int_page =NULL ; } -hv_free_hyperv_page ((unsigned long )vmbus_connection.monitor_pages[0 ]); -hv_free_hyperv_page ((unsigned long )vmbus_connection.monitor_pages[1 ]); - vmbus_connection.monitor_pages[0 ] =NULL ; - vmbus_connection.monitor_pages[1 ] =NULL ; +if (hv_is_isolation_supported ()) { +/* +* memunmap() checks input address is ioremap address or not +* inside. It doesn't unmap any thing in the non-SNP CVM and +* so not check CVM type here. +*/ +memunmap (vmbus_connection.monitor_pages[0 ]); +memunmap (vmbus_connection.monitor_pages[1 ]); + +set_memory_encrypted ((unsigned long ) + vmbus_connection.monitor_pages_original[0 ], +1 ); +set_memory_encrypted ((unsigned long ) + vmbus_connection.monitor_pages_original[1 ], +1 ); + } + +hv_free_hyperv_page ((unsigned long ) + vmbus_connection.monitor_pages_original[0 ]); +hv_free_hyperv_page ((unsigned long ) + vmbus_connection.monitor_pages_original[1 ]); + vmbus_connection.monitor_pages_original[0 ] = + vmbus_connection.monitor_pages[0 ] =NULL ; + vmbus_connection.monitor_pages_original[1 ] = + vmbus_connection.monitor_pages[1 ] =NULL ; }/*
drivers/hv/hyperv_vmbus.h +2 -0
@@ -240,6 +240,8 @@ struct vmbus_connection {* is child->parent notification */ struct hv_monitor_page *monitor_pages[2 ]; +void *monitor_pages_original[2 ]; +phys_addr_t monitor_pages_pa[2 ];struct list_head chn_msg_list;spinlock_t channelmsg_lock;
Signature · 1 line
2.25.1
PATCH v7 7/9 x86/hyperv: Add ghcb hvcall support for SNP VM
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
hyperv provides ghcb hvcall to handle VMBusHVCALL_SIGNAL_EVENT and HVCALL_POST_MESSAGE
msg in SNP Isolation VM. Add such support.
---
Change since v3:
* Add hv_ghcb_hypercall() stub function to avoid compile error for ARM.
--- arch/x86/hyperv/ivm.c | 74 ++++++++++++++++++++++++++++++++++ drivers/hv/connection.c | 6 ++- drivers/hv/hv.c | 8 +++- drivers/hv/hv_common.c | 6 +++ include/asm-generic/mshyperv.h | 1 + 5 files changed, 93 insertions(+), 2 deletions(-)
arch/x86/hyperv/ivm.c +74 -0
@@ -18,10 +18,84 @@#include <asm/mshyperv.h> #include <asm/hypervisor.h> +#define GHCB_USAGE_HYPERV_CALL 1 +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(HV_HYP_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; +u64 status; + +if (!hv_ghcb_pg) +return -EFAULT ; + +WARN_ON (in_nmi ()); + +local_irq_save (flags); + ghcb_base = (void **)this_cpu_ptr (hv_ghcb_pg); + hv_ghcb = (union hv_ghcb *)*ghcb_base; +if (!hv_ghcb) { +local_irq_restore (flags); +return -EFAULT ; + } + + hv_ghcb->ghcb.protocol_version =GHCB_PROTOCOL_MAX ; + hv_ghcb->ghcb.ghcb_usage =GHCB_USAGE_HYPERV_CALL ; + + 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 =0xffffffff ; +memset (hv_ghcb->ghcb.save.valid_bitmap,0 , +sizeof (hv_ghcb->ghcb.save.valid_bitmap)); + + status = hv_ghcb->hypercall.hypercalloutput.callstatus; + +local_irq_restore (flags); + +return status; +} +void hv_ghcb_msr_write (u64 msr,u64 value) {union hv_ghcb *hv_ghcb;
drivers/hv/connection.c +5 -1
@@ -447,6 +447,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 , &channel->sig_event, +NULL ,sizeof (channel->sig_event)); +else +hv_do_fast_hypercall8 (HVCALL_SIGNAL_EVENT , channel->sig_event); }EXPORT_SYMBOL_GPL (vmbus_set_event);
drivers/hv/hv.c +7 -1
@@ -98,7 +98,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 ()) + status =hv_ghcb_hypercall (HVCALL_POST_MESSAGE , + (void *)aligned_msg,NULL , +sizeof (*aligned_msg)); +else + status =hv_do_hypercall (HVCALL_POST_MESSAGE , + aligned_msg,NULL );/* Preemption must remain disabled until after the hypercall * so some other thread can't get scheduled onto this cpu and
drivers/hv/hv_common.c +6 -0
@@ -289,3 +289,9 @@ void __weak hyperv_cleanup(void) { }EXPORT_SYMBOL_GPL (hyperv_cleanup); + +u64 __weak hv_ghcb_hypercall (u64 control,void *input,void *output,u32 input_size) +{ +return HV_STATUS_INVALID_PARAMETER ; +} +EXPORT_SYMBOL_GPL (hv_ghcb_hypercall);
include/asm-generic/mshyperv.h +1 -0
@@ -266,6 +266,7 @@ bool hv_is_hibernation_supported(void);enum hv_isolation_type hv_get_isolation_type (void );bool hv_is_isolation_supported (void );bool hv_isolation_type_snp (void ); +u64 hv_ghcb_hypercall (u64 control,void *input,void *output,u32 input_size);void hyperv_cleanup (void );bool hv_query_ext_cap (u64 cap_query);#else /* CONFIG_HYPERV */
Signature · 1 line
2.25.1
PATCH v7 9/9 Drivers: hv : vmbus: Initialize VMbus ring buffer for Isolation VM
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
VMbus ring buffer are shared with host and it's need to be accessed via extra address space of Isolation VM with AMD SNP support. This patch is to map the ring buffer address in extra address space via vmap_pfn(). Hyperv set memory host visibility hvcall smears data in the ring buffer and so reset the ring buffer memory to zero after mapping.
---
Change since v4:
* Use PFN_DOWN instead of HVPFN_DOWN in the hv_ringbuffer_init()
Change since v3:
* Remove hv_ringbuffer_post_init(), merge map operation for Isolation VM into hv_ringbuffer_init()
* Call hv_ringbuffer_init() after __vmbus_establish_gpadl().
--- drivers/hv/Kconfig | 1 + drivers/hv/channel.c | 19 +++++++------- drivers/hv/ring_buffer.c | 55 ++++++++++++++++++++++++++++++---------- 3 files changed, 53 insertions(+), 22 deletions(-)
drivers/hv/Kconfig +1 -0
@@ -8,6 +8,7 @@ config HYPERV || (ARM64 && !CPU_BIG_ENDIAN)) select PARAVIRT select X86_HV_CALLBACK_VECTOR if X86 + select VMAP_PFN help Select this option to run Linux as a Hyper-V client operating system.
drivers/hv/channel.c +10 -9
@@ -683,15 +683,6 @@ static int __vmbus_open(struct vmbus_channel *newchannel,if (!newchannel->max_pkt_size) newchannel->max_pkt_size =VMBUS_DEFAULT_MAX_PKT_SIZE ; - err =hv_ringbuffer_init (&newchannel->outbound, page, send_pages,0 ); -if (err) - goto error_clean_ring; - - err =hv_ringbuffer_init (&newchannel->inbound, &page[send_pages], - recv_pages, newchannel->max_pkt_size); -if (err) - goto error_clean_ring; -/* Establish the gpadl for the ring buffer */ newchannel->ringbuffer_gpadlhandle.gpadl_handle =0 ;
@@ -703,6 +694,16 @@ static int __vmbus_open(struct vmbus_channel *newchannel,if (err) goto error_clean_ring; + err =hv_ringbuffer_init (&newchannel->outbound, + page, send_pages,0 ); +if (err) + goto error_free_gpadl; + + err =hv_ringbuffer_init (&newchannel->inbound, &page[send_pages], + recv_pages, newchannel->max_pkt_size); +if (err) + goto error_free_gpadl; +/* Create and init the channel open message */ open_info = kzalloc(sizeof(*open_info) + sizeof(struct vmbus_channel_open_channel),
drivers/hv/ring_buffer.c +42 -13
79 lines not on this page · raw message
Signature · 1 line
2.25.1
Re: [PATCH V7 5/9] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
In reply to Tianyu LanHi @Tom and Borislav:
Please have a look at this patch. If it's ok, could you give your ack.
Thanks.
Quoted 112 lines · Tianyu Lan
From: Tianyu Lan <[email protected]>
Hyper-V also 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.Signed-off-by Tianyu Lan <[email protected]>--- arch/x86/include/asm/sev.h | 10 +++++++++ arch/x86/kernel/sev-shared.c | 43 +++++++++++++++++++++++++----------- 2 files changed, 40 insertions(+), 13 deletions(-)arch/x86/include/asm/sev.h +10
-0@@ -81,12 +81,22 @@ 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, +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, +u64 exit_code,u64 exit_info_1, +u64 exit_info_2) +{ +return ES_VMM_ERROR ; +}#endif #endif arch/x86/kernel/sev-shared.c +30
-13@@ -94,10 +94,13 @@ static void vc_finish_insn(struct es_em_ctxt *ctxt) ctxt->regs->ip += ctxt->insn.length; } -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) +/* +* __sev_es_ghcb_hv_call() is also used in the other platform code(e.g +* Hyper-V). +*/ +enum es_result __sev_es_ghcb_hv_call (struct ghcb *ghcb, +u64 exit_code,u64 exit_info_1, +u64 exit_info_2) {enum es_result ret;@@ -109,15 +112,33 @@ 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));VMGEXIT (); +if (ghcb->save.sw_exit_info_1 &0xffffffff ) + ret =ES_VMM_ERROR ; +else + ret =ES_OK ; + +return ret; +} + +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 ret; + +sev_es_wr_ghcb_msr (__pa (ghcb)); + + ret =__sev_es_ghcb_hv_call (ghcb, exit_code, exit_info_1, + exit_info_2); +if (ret ==ES_OK ) +return ret; +if ((ghcb->save.sw_exit_info_1 &0xffffffff ) ==1 ) {u64 info = ghcb->save.sw_exit_info_2; -unsigned long v; - - info = ghcb->save.sw_exit_info_2; - v = info &SVM_EVTINJ_VEC_MASK ; +unsigned long v = info &SVM_EVTINJ_VEC_MASK ;/* Check if exception information from hypervisor is sane. */ if ((info & SVM_EVTINJ_VALID) &&@@ -127,11 +148,7 @@ static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb,if (info &SVM_EVTINJ_VALID_ERR ) ctxt->fi.error_code = info >>32 ; ret =ES_EXCEPTION ; - }else { - ret =ES_VMM_ERROR ; } - }else { - ret =ES_OK ; }return ret;
Borislav Petkov
maintainerRe: [PATCH V7 5/9] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 2 lines · Tianyu Lan
Hi @Tom and Borislav:
Please have a look at this patch. If it's ok, could you give your ack.
I needed to do some cleanups in that area first:
https://lore.kernel.org/r/YWRwxImd9Qcls/[email protected]
Can you redo yours ontop so that you can show what exactly you need exported for HyperV?
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH V7 5/9] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Sure. Will do that. Thanks.
Quoted 13 lines · Borislav Petkov
Quoted 2 lines · Tianyu Lan
Hi @Tom and Borislav:
Please have a look at this patch. If it's ok, could you give your ack.I needed to do some cleanups in that area first:
https://lore.kernel.org/r/YWRwxImd9Qcls/[email protected]
Can you redo yours ontop so that you can show what exactly you need exported for HyperV?
Thx.
Re: [PATCH V7 5/9] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
In reply to Borislav PetkovQuoted 4 lines · Borislav Petkov
Quoted 2 lines · Tianyu Lan
Hi @Tom and Borislav:
Please have a look at this patch. If it's ok, could you give your ack.
Quoted 8 lines
I needed to do some cleanups in that area first:
https://lore.kernel.org/r/YWRwxImd9Qcls/[email protected]
Can you redo yours ontop so that you can show what exactly you need exported for HyperV?
Thx.
Hi Borislav :
Please check whether the following change based on you patch is
ok for you.
---
x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Hyper-V also 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.
arch/x86/include/asm/sev.h +4 -0
@@ -81,12 +81,23 @@ 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, +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,
+ u64 exit_code, u64 exit_info_1,
+ u64 exit_info_2)
+{
+ return ES_VMM_ERROR;
+}
#endif
#endif
arch/x86/kernel/sev-shared.c +19 -5
@@ -124,10 +124,14 @@ static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxtreturn 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) +/* +* __sev_es_ghcb_hv_call() is also used in the other platform code(e.g +* Hyper-V). +*/ +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) {/* Fill in protocol and format specifiers */ ghcb->protocol_version =GHCB_PROTOCOL_MAX ;
@@ -137,12 +141,22 @@ 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));VMGEXIT ();return verify_exception_info (ghcb, ctxt); } +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) +{ +sev_es_wr_ghcb_msr (__pa (ghcb)); + +return __sev_es_ghcb_hv_call (ghcb, ctxt, exit_code, exit_info_1, + exit_info_2); +} +/* * Boot VC Handler - This is the first VC handler during boot, there
is no GHCB
* page yet, so it only supports the MSR based communication with the
(END)
Thanks.
Re: [PATCH V7 5/9] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Gentle Ping.
Quoted 111 lines · Tianyu Lan
Quoted 4 lines · Borislav Petkov
Quoted 2 lines · Tianyu Lan
Hi @Tom and Borislav:
Please have a look at this patch. If it's ok, could you give your ack.Quoted 8 lines
I needed to do some cleanups in that area first:
https://lore.kernel.org/r/YWRwxImd9Qcls/[email protected]
Can you redo yours ontop so that you can show what exactly you need exported for HyperV?
Thx.
Hi Borislav :
Please check whether the following change based on you patch is
ok for you.
---
x86/sev-es: Expose__sev_es_ghcb_hv_call()to call ghcb hv call out of sev codeHyper-V also 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.Signed-off-by Tianyu Lan <[email protected]>arch/x86/include/asm/sev.h +11
-0@@ -81,12 +81,23 @@ 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, + 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, + u64 exit_code, u64 exit_info_1, + u64 exit_info_2) +{ + return ES_VMM_ERROR; +} #endif#endif
arch/x86/kernel/sev-shared.c +0
-0@@ -124,10 +124,14 @@ static enum es_result verify_exception_info(structghcb *ghcb, struct
es_em_ctxt
returnES_VMM_ERROR;
}arch/x86/kernel/sev-shared.c +19
-5-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) +/* +* __sev_es_ghcb_hv_call() is also used in the other platform code(e.g +* Hyper-V). +*/ +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) { /* Fill in protocol and format specifiers */ ghcb->protocol_version = GHCB_PROTOCOL_MAX;@@ -137,12 +141,22 @@ static enum es_result sev_es_ghcb_hv_call(structghcb *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)); VMGEXIT(); return verify_exception_info(ghcb, ctxt); } +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) +{ + sev_es_wr_ghcb_msr(__pa(ghcb)); + + return __sev_es_ghcb_hv_call(ghcb, ctxt, exit_code, exit_info_1, + exit_info_2); +} + /** Boot VC Handler - This is the first VC handler during boot, there is no GHCB
* page yet, so it only supports the MSR based communication with the
(END)Thanks.
Borislav Petkov
maintainerRe: [PATCH V7 5/9] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 1 line · Tianyu Lan
Gentle Ping.
$ patch -p1 --dry-run -i /tmp/ltykernel.01 checking file arch/x86/include/asm/sev.h patch: **** malformed patch at line 128: return 0; }
Can you pls send a patch which is not mangled and I can apply?
Also, this might have some info on the matter:
Documentation/process/email-clients.rst
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquettePATCH 1/1 x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
In reply to Tianyu LanFrom: Tianyu Lan <[email protected]>
Hyper-V also 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.
--- arch/x86/include/asm/sev.h | 11 +++++++++++ arch/x86/kernel/sev-shared.c | 24 +++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-)
arch/x86/include/asm/sev.h +11 -0
@@ -81,12 +81,23 @@ 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, +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, +u64 exit_code,u64 exit_info_1, +u64 exit_info_2) +{ +return ES_VMM_ERROR ; +}#endif #endif
arch/x86/kernel/sev-shared.c +19 -5
@@ -124,10 +124,14 @@ static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxtreturn 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) +/* +* __sev_es_ghcb_hv_call() is also used in the other platform code(e.g +* Hyper-V). +*/ +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) {/* Fill in protocol and format specifiers */ ghcb->protocol_version =GHCB_PROTOCOL_MAX ;
@@ -137,12 +141,22 @@ 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));VMGEXIT ();return verify_exception_info (ghcb, ctxt); } +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) +{ +sev_es_wr_ghcb_msr (__pa (ghcb)); + +return __sev_es_ghcb_hv_call (ghcb, ctxt, exit_code, exit_info_1, + exit_info_2); +} +/* * Boot VC Handler - This is the first VC handler during boot, there is no GHCB * page yet, so it only supports the MSR based communication with the
Signature · 1 line
2.25.1
Borislav Petkov
maintainerRe: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 17 lines · Tianyu Lan
From: Tianyu Lan <[email protected]>
Hyper-V also 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.Signed-off-by Tianyu Lan <[email protected]>--- arch/x86/include/asm/sev.h | 11 +++++++++++ arch/x86/kernel/sev-shared.c | 24 +++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-)arch/x86/include/asm/sev.h +0
-0
arch/x86/include/asm/sev.h static __always_inline void sev_es_nmi_complete(void) · line 87
Quoted 7 lines
arch/x86/include/asm/sev.h +4
-0@@ -81,12 +81,23 @@ 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, +struct es_em_ctxt *ctxt, +u64 exit_code,u64 exit_info_1, +u64 exit_info_2);
You can do here:
static inline enum es_result__sev_es_ghcb_hv_call(struct ghcb *ghcb, u64 exit_code, u64 exit_info_1, u64 exit_info_2) { return ES_VMM_ERROR; }
Quoted 10 lines
arch/x86/include/asm/sev.h +0
-1@@ -137,12 +141,22 @@ 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));VMGEXIT ();return verify_exception_info (ghcb, ctxt); }
arch/x86/include/asm/sev.h static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, · line 158
Quoted 10 lines
arch/x86/include/asm/sev.h +10
-0+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) +{ +sev_es_wr_ghcb_msr (__pa (ghcb)); + +return __sev_es_ghcb_hv_call (ghcb, ctxt, exit_code, exit_info_1, + exit_info_2); +}
Well, why does Hyper-V need this thing a bit differently, without the setting of the GHCB's physical address?
What if another hypervisor does yet another SEV implementation and yet another HV call needs to be defined?
If stuff is going to be exported to other users, then stuff better be defined properly so that it is used by multiple hypervisors.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 41 lines · Borislav Petkov
Quoted 17 lines · Tianyu Lan
From: Tianyu Lan <[email protected]>
Hyper-V also 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.Signed-off-by Tianyu Lan <[email protected]>--- arch/x86/include/asm/sev.h | 11 +++++++++++ arch/x86/kernel/sev-shared.c | 24 +++++++++++++++++++----- 2 files changed, 30 insertions(+), 5 deletions(-)arch/x86/include/asm/sev.h +0
-0arch/x86/include/asm/sev.h static __always_inline void sev_es_nmi_complete(void) · line 87
Quoted 7 lines
arch/x86/include/asm/sev.h +4
-0@@ -81,12 +81,23 @@ 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, +struct es_em_ctxt *ctxt, +u64 exit_code,u64 exit_info_1, +u64 exit_info_2);You can do here:
static inline enum
es_result__sev_es_ghcb_hv_call(struct ghcb *ghcb, u64 exit_code, u64 exit_info_1, u64 exit_info_2){ returnES_VMM_ERROR; }Quoted 10 lines
arch/x86/include/asm/sev.h +0
-1@@ -137,12 +141,22 @@ 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));VMGEXIT ();return verify_exception_info (ghcb, ctxt); }
Quoted 13 lines
arch/x86/include/asm/sev.h static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, · line 158
Quoted 10 lines
arch/x86/include/asm/sev.h +10
-0+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) +{ +sev_es_wr_ghcb_msr (__pa (ghcb)); + +return __sev_es_ghcb_hv_call (ghcb, ctxt, exit_code, exit_info_1, + exit_info_2); +}Well, why does Hyper-V need this thing a bit differently, without the setting of the GHCB's physical address?
Hyper-V runs paravisor in guest VMPL0 which emulates some functions (e.g, timer, tsc, serial console and so on) via handling VC exception. GHCB pages are allocated and set up by the paravisor and report to Linux guest via MSR register.Hyper-V SEV implementation is unenlightened guest case which doesn't Linux doesn't handle VC and paravisor in the VMPL0 handle it.
Quoted 3 lines
What if another hypervisor does yet another SEV implementation and yet another HV call needs to be defined?
In the early version, these ghcb operations are implemented in Hyper-V code and get comments to use existing code in the SEV ES as much as possible. So latter versions expose the API to re-use code.
Current two cases: enlightened guest and un-enlightened guest. Tom and brjesh pushed enlightened case. Hyper-V is un-enlightened case and a paravisor runs in VMPL0 to handle VC to emulate devices inside VM. GHCB is allocated and set up by paravisor in the un-enlightened case. The new __sev_es_ghcb_hv_call() is to handle these two cases.
Borislav Petkov
maintainerRe: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 6 lines · Tianyu Lan
Hyper-V runs paravisor in guest VMPL0 which emulates some functions (e.g, timer, tsc, serial console and so on) via handling VC exception. GHCB pages are allocated and set up by the paravisor and report to Linux guest via MSR register.Hyper-V SEV implementation is unenlightened guest case which doesn't Linux doesn't handle VC and paravisor in the VMPL0 handle it.
Aha, unenlightened.
So why don't you export the original function by doing this (only partial diff to show intent only):
---
arch/x86/kernel/sev-shared.c +9 -2
@@ -125,7 +125,7 @@ static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxtreturn ES_VMM_ERROR ; } -static enum es_result sev_es_ghcb_hv_call (struct ghcb *ghcb, +static 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)
@@ -138,7 +138,14 @@ 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 by that paravisor which uses a +* different MSR and protocol. +*/ +if (set_ghcb_msr) +sev_es_wr_ghcb_msr (__pa (ghcb)); +VMGEXIT ();return verify_exception_info (ghcb, ctxt);
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 28 lines · Borislav Petkov
Quoted 6 lines · Tianyu Lan
Hyper-V runs paravisor in guest VMPL0 which emulates some functions (e.g, timer, tsc, serial console and so on) via handling VC exception. GHCB pages are allocated and set up by the paravisor and report to Linux guest via MSR register.Hyper-V SEV implementation is unenlightened guest case which doesn't Linux doesn't handle VC and paravisor in the VMPL0 handle it.
Aha, unenlightened.
So why don't you export the original function by doing this (only partial diff to show intent only):
---
arch/x86/kernel/sev-shared.c +1
-1@@ -125,7 +125,7 @@ static enum es_result verify_exception_info(struct ghcb *ghcb, struct es_em_ctxtreturn ES_VMM_ERROR ; } -static enum es_result sev_es_ghcb_hv_call (struct ghcb *ghcb, +static 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)
arch/x86/kernel/sev-shared.c static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, · line 144
Quoted 8 lines
arch/x86/kernel/sev-shared.c +4
-1@@ -138,7 +138,14 @@ 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 by that paravisor which uses a +* different MSR and protocol.
Just to clarify the comment, the paravisor uses the same GHCB MSR and GHCB protocol, it just can't use __pa() to get the address of the GHCB. So I expect that the Hyper-V support sets the address properly before calling this function.
Thanks,
Tom
Quoted 9 lines
arch/x86/kernel/sev-shared.c +4
-0+*/ +if (set_ghcb_msr) +sev_es_wr_ghcb_msr (__pa (ghcb)); +VMGEXIT ();return verify_exception_info (ghcb, ctxt);
Re: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 9 lines · Tom Lendacky
Quoted 8 lines · Borislav Petkov
Quoted 6 lines · Tianyu Lan
Hyper-V runs paravisor in guest VMPL0 which emulates some functions (e.g, timer, tsc, serial console and so on) via handling VC exception. GHCB pages are allocated and set up by the paravisor and report to Linux guest via MSR register.Hyper-V SEV implementation is unenlightened guest case which doesn't Linux doesn't handle VC and paravisor in the VMPL0 handle it.
Quoted 4 lines
Quoted 4 lines
Aha, unenlightened.
So why don't you export the original function by doing this (only partial diff to show intent only):
This follows Joreg's previous comment and I implemented similar version in the V! patchset([PATCH 05/13] HV: Add Write/Read MSR registers via ghcb page https://lkml.org/lkml/2021/7/28/668).
"Instead, factor out a helper function which contains what Hyper-V needs and use that in sev_es_ghcb_hv_call() and Hyper-V code."
https://lkml.org/lkml/2021/8/2/375
Quoted 40 lines
Quoted 23 lines
---
arch/x86/kernel/sev-shared.c +0
-0@@ -125,7 +125,7 @@ static enum es_result verify_exception_info(structghcb *ghcb, struct
es_em_ctxt
returnES_VMM_ERROR;
}arch/x86/kernel/sev-shared.c +1
-1-static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, +static enum es_result sev_es_ghcb_hv_call(struct ghcb *ghcb, bool
set_ghcb_msr,
structes_em_ctxt*ctxt, u64exit_code, u64exit_info_1, u64exit_info_2)arch/x86/kernel/sev-shared.c +2
-1@@ -138,7 +138,14 @@ static enum es_result sev_es_ghcb_hv_call(structghcb *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 Quoted 3 lines
and
+ * GHCB pages are being allocated by that paravisor which uses a + * different MSR and protocol.Just to clarify the comment, the paravisor uses the same GHCB MSR and GHCB protocol, it just can't use
__pa()to get the address of the GHCB. So I expect that the Hyper-V support sets the address properly before calling this function.Thanks,
TomQuoted 8 lines
+ */
+ if (set_ghcb_msr)
+ sev_es_wr_ghcb_msr(__pa(ghcb));
+
VMGEXIT();
returnverify_exception_info(ghcb, ctxt);
Borislav Petkov
maintainerRe: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 7 lines · Tianyu Lan
This follows Joreg's previous comment and I implemented similar version in the V! patchset([PATCH 05/13] HV: Add Write/Read MSR registers via ghcb page
https://lkml.org/lkml/2021/7/28/668).
"Instead, factor out a helper function which contains what Hyper-V needs and use that insev_es_ghcb_hv_call()and Hyper-V code."
If you wanna point to mails on a mailing list, you simply do
https://lore.kernel.org/r/<Message-id>
No need to use some random, unreliable web pages.
As to Joerg's suggestion, in the version I'm seeing, you're checking the *context* - and the one you sent today, avoids the __pa(ghcb) MSR write.
So which is it?
Because your current version will look at the context too, see
return verify_exception_info(ghcb, ctxt);
at the end of the function.
So is the issue what Tom said that "the paravisor uses the same GHCB MSR and GHCB protocol, it just can't use __pa() to get the address of the GHCB."?
If that is the case and the only thing you want is to avoid the GHCB PA write, then, in the future, we might drop that MSR write altogether on the enlightened Linux guests too and then the same function will be used by your paravisor and the Linux guest.
So please explain in detail what exactly you want to avoid from sev_es_ghcb_hv_call()'s current version and why.
As I said before, I don't want to export any random details of the SEV implementation in the kernel without any justification for it.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 7 lines · Borislav Petkov
Quoted 6 lines · Tianyu Lan
This follows Joreg's previous comment and I implemented similar version in the V! patchset([PATCH 05/13] HV: Add Write/Read MSR registers via ghcb page
https://lkml.org/lkml/2021/7/28/668).
"Instead, factor out a helper function which contains what Hyper-V needs and use that insev_es_ghcb_hv_call()and Hyper-V code."
Quoted 7 lines
Quoted 1 line
If you wanna point to mails on a mailing list, you simply do
https://lore.kernel.org/r/<Message-id>
No need to use some random, unreliable web pages.
OK. Thanks for suggestion.
Quoted 11 lines
As to Joerg's suggestion, in the version I'm seeing, you're checking the *context* - and the one you sent today, avoids the
__pa(ghcb)MSR write.So which is it?
Because your current version will look at the context too, see
return verify_exception_info(ghcb, ctxt);at the end of the function.
Both old and new patches are to avoid setting GHCB page address via MSR.
Paravisor is in charge of doing that and un-enlightened guest should not change it. The old one was in the patchset v1 "x86/Hyper-V: Add Hyper-V Isolation VM support". The patch I sent today is based on your clean up patch and for review first. It should be in patchset "x86/Hyper-V: Add Hyper-V Isolation VM support."
Quoted 4 lines
So is the issue what Tom said that "the paravisor uses the same GHCB MSR and GHCB protocol, it just can't use
__pa()to get the address of the GHCB."?
Yes, hyper-V enables vTOM in the CVM and GHCB page PA reported by paravisor contains vTOM bit. We need to use memremap() to map ghcb page before accessing GHCB page. __pa() doesn't work for PA with vTOM bit. Otherwise, guest should not set GHCB page address and avoid conflict with paravisor.
Quoted 5 lines
If that is the case and the only thing you want is to avoid the GHCB PA write, then, in the future, we might drop that MSR write altogether on the enlightened Linux guests too and then the same function will be used by your paravisor and the Linux guest.
Yes, this is the target of the patch. Can we put the change in the Hyper-V patchset? Other patch has been fully reviewed.
Thanks.
Borislav Petkov
maintainerRe: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 2 lines · Tianyu Lan
Yes, this is the target of the patch. Can we put the change in the Hyper-V patchset?
If you're asking about this version:
https://lore.kernel.org/r/[email protected]
then, no. I'd prefer if you did this:
https://lore.kernel.org/r/[email protected]
for reasons which I already explained.
Thx.
Signature · 4 lines
Regards/Gruss,
Boris.
https://people.kernel.org/tglx/notes-about-netiquetteRe: [PATCH] x86/sev-es: Expose __sev_es_ghcb_hv_call() to call ghcb hv call out of sev code
Quoted 6 lines · Borislav Petkov
Quoted 2 lines · Tianyu Lan
Yes, this is the target of the patch. Can we put the change in the Hyper-V patchset?
If you're asking about this version:
Quoted 8 lines
https://lore.kernel.org/r/[email protected]
then, no. I'd prefer if you did this:
https://lore.kernel.org/r/[email protected]
for reasons which I already explained.
Thanks for your suggestion. I just sent out v8 version according to your guide. Please have a look.
Thanks.