1 // SPDX-License-Identifier: GPL-2.0
3 * Hyper-V Isolation VM interface with paravisor and hypervisor
9 #include <linux/bitfield.h>
10 #include <linux/hyperv.h>
11 #include <linux/types.h>
12 #include <linux/slab.h>
16 #include <asm/mshyperv.h>
17 #include <asm/hypervisor.h>
19 #ifdef CONFIG_AMD_MEM_ENCRYPT
21 #define GHCB_USAGE_HYPERV_CALL 1
26 u64 hypercalldata[509];
35 u32 countofelements : 12;
37 u32 repstartindex : 12;
46 u32 elementsprocessed : 12;
54 } __packed __aligned(HV_HYP_PAGE_SIZE);
56 u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
58 union hv_ghcb *hv_ghcb;
68 local_irq_save(flags);
69 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
70 hv_ghcb = (union hv_ghcb *)*ghcb_base;
72 local_irq_restore(flags);
76 hv_ghcb->ghcb.protocol_version = GHCB_PROTOCOL_MAX;
77 hv_ghcb->ghcb.ghcb_usage = GHCB_USAGE_HYPERV_CALL;
79 hv_ghcb->hypercall.outputgpa = (u64)output;
80 hv_ghcb->hypercall.hypercallinput.asuint64 = 0;
81 hv_ghcb->hypercall.hypercallinput.callcode = control;
84 memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size);
88 hv_ghcb->ghcb.ghcb_usage = 0xffffffff;
89 memset(hv_ghcb->ghcb.save.valid_bitmap, 0,
90 sizeof(hv_ghcb->ghcb.save.valid_bitmap));
92 status = hv_ghcb->hypercall.hypercalloutput.callstatus;
94 local_irq_restore(flags);
99 void hv_ghcb_msr_write(u64 msr, u64 value)
101 union hv_ghcb *hv_ghcb;
104 struct es_em_ctxt ctxt;
111 local_irq_save(flags);
112 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
113 hv_ghcb = (union hv_ghcb *)*ghcb_base;
115 local_irq_restore(flags);
119 ghcb_set_rcx(&hv_ghcb->ghcb, msr);
120 ghcb_set_rax(&hv_ghcb->ghcb, lower_32_bits(value));
121 ghcb_set_rdx(&hv_ghcb->ghcb, upper_32_bits(value));
123 if (sev_es_ghcb_hv_call(&hv_ghcb->ghcb, false, &ctxt,
125 pr_warn("Fail to write msr via ghcb %llx.\n", msr);
127 local_irq_restore(flags);
129 EXPORT_SYMBOL_GPL(hv_ghcb_msr_write);
131 void hv_ghcb_msr_read(u64 msr, u64 *value)
133 union hv_ghcb *hv_ghcb;
136 struct es_em_ctxt ctxt;
138 /* Check size of union hv_ghcb here. */
139 BUILD_BUG_ON(sizeof(union hv_ghcb) != HV_HYP_PAGE_SIZE);
146 local_irq_save(flags);
147 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
148 hv_ghcb = (union hv_ghcb *)*ghcb_base;
150 local_irq_restore(flags);
154 ghcb_set_rcx(&hv_ghcb->ghcb, msr);
155 if (sev_es_ghcb_hv_call(&hv_ghcb->ghcb, false, &ctxt,
157 pr_warn("Fail to read msr via ghcb %llx.\n", msr);
159 *value = (u64)lower_32_bits(hv_ghcb->ghcb.save.rax)
160 | ((u64)lower_32_bits(hv_ghcb->ghcb.save.rdx) << 32);
161 local_irq_restore(flags);
163 EXPORT_SYMBOL_GPL(hv_ghcb_msr_read);
166 enum hv_isolation_type hv_get_isolation_type(void)
168 if (!(ms_hyperv.priv_high & HV_ISOLATION))
169 return HV_ISOLATION_TYPE_NONE;
170 return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
172 EXPORT_SYMBOL_GPL(hv_get_isolation_type);
175 * hv_is_isolation_supported - Check system runs in the Hyper-V
178 bool hv_is_isolation_supported(void)
180 if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
183 if (!hypervisor_is_type(X86_HYPER_MS_HYPERV))
186 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
189 DEFINE_STATIC_KEY_FALSE(isolation_type_snp);
192 * hv_isolation_type_snp - Check system runs in the AMD SEV-SNP based
195 bool hv_isolation_type_snp(void)
197 return static_branch_unlikely(&isolation_type_snp);
201 * hv_mark_gpa_visibility - Set pages visible to host via hvcall.
203 * In Isolation VM, all guest memory is encrypted from host and guest
204 * needs to set memory visible to host via hvcall before sharing memory
207 static int hv_mark_gpa_visibility(u16 count, const u64 pfn[],
208 enum hv_mem_host_visibility visibility)
210 struct hv_gpa_range_for_visibility **input_pcpu, *input;
215 /* no-op if partition isolation is not enabled */
216 if (!hv_is_isolation_supported())
219 if (count > HV_MAX_MODIFY_GPA_REP_COUNT) {
220 pr_err("Hyper-V: GPA count:%d exceeds supported:%lu\n", count,
221 HV_MAX_MODIFY_GPA_REP_COUNT);
225 local_irq_save(flags);
226 input_pcpu = (struct hv_gpa_range_for_visibility **)
227 this_cpu_ptr(hyperv_pcpu_input_arg);
229 if (unlikely(!input)) {
230 local_irq_restore(flags);
234 input->partition_id = HV_PARTITION_ID_SELF;
235 input->host_visibility = visibility;
236 input->reserved0 = 0;
237 input->reserved1 = 0;
238 memcpy((void *)input->gpa_page_list, pfn, count * sizeof(*pfn));
239 hv_status = hv_do_rep_hypercall(
240 HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY, count,
241 0, input, &pages_processed);
242 local_irq_restore(flags);
244 if (hv_result_success(hv_status))
251 * hv_set_mem_host_visibility - Set specified memory visible to host.
253 * In Isolation VM, all guest memory is encrypted from host and guest
254 * needs to set memory visible to host via hvcall before sharing memory
255 * with host. This function works as wrap of hv_mark_gpa_visibility()
256 * with memory base and size.
258 int hv_set_mem_host_visibility(unsigned long kbuffer, int pagecount, bool visible)
260 enum hv_mem_host_visibility visibility = visible ?
261 VMBUS_PAGE_VISIBLE_READ_WRITE : VMBUS_PAGE_NOT_VISIBLE;
266 if (!hv_is_isolation_supported() || !hv_hypercall_pg)
269 pfn_array = kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
273 for (i = 0, pfn = 0; i < pagecount; i++) {
274 pfn_array[pfn] = virt_to_hvpfn((void *)kbuffer + i * HV_HYP_PAGE_SIZE);
277 if (pfn == HV_MAX_MODIFY_GPA_REP_COUNT || i == pagecount - 1) {
278 ret = hv_mark_gpa_visibility(pfn, pfn_array,
281 goto err_free_pfn_array;
292 * hv_map_memory - map memory to extra space in the AMD SEV-SNP Isolation VM.
294 void *hv_map_memory(void *addr, unsigned long size)
296 unsigned long *pfns = kcalloc(size / PAGE_SIZE,
297 sizeof(unsigned long), GFP_KERNEL);
304 for (i = 0; i < size / PAGE_SIZE; i++)
305 pfns[i] = vmalloc_to_pfn(addr + i * PAGE_SIZE) +
306 (ms_hyperv.shared_gpa_boundary >> PAGE_SHIFT);
308 vaddr = vmap_pfn(pfns, size / PAGE_SIZE, PAGE_KERNEL_IO);
314 void hv_unmap_memory(void *addr)