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>
17 #include <asm/mem_encrypt.h>
18 #include <asm/mshyperv.h>
19 #include <asm/hypervisor.h>
22 #ifdef CONFIG_AMD_MEM_ENCRYPT
24 #define GHCB_USAGE_HYPERV_CALL 1
29 u64 hypercalldata[509];
38 u32 countofelements : 12;
40 u32 repstartindex : 12;
49 u32 elementsprocessed : 12;
57 } __packed __aligned(HV_HYP_PAGE_SIZE);
59 static u16 hv_ghcb_version __ro_after_init;
61 u64 hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size)
63 union hv_ghcb *hv_ghcb;
73 local_irq_save(flags);
74 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
75 hv_ghcb = (union hv_ghcb *)*ghcb_base;
77 local_irq_restore(flags);
81 hv_ghcb->ghcb.protocol_version = GHCB_PROTOCOL_MAX;
82 hv_ghcb->ghcb.ghcb_usage = GHCB_USAGE_HYPERV_CALL;
84 hv_ghcb->hypercall.outputgpa = (u64)output;
85 hv_ghcb->hypercall.hypercallinput.asuint64 = 0;
86 hv_ghcb->hypercall.hypercallinput.callcode = control;
89 memcpy(hv_ghcb->hypercall.hypercalldata, input, input_size);
93 hv_ghcb->ghcb.ghcb_usage = 0xffffffff;
94 memset(hv_ghcb->ghcb.save.valid_bitmap, 0,
95 sizeof(hv_ghcb->ghcb.save.valid_bitmap));
97 status = hv_ghcb->hypercall.hypercalloutput.callstatus;
99 local_irq_restore(flags);
104 static inline u64 rd_ghcb_msr(void)
106 return __rdmsr(MSR_AMD64_SEV_ES_GHCB);
109 static inline void wr_ghcb_msr(u64 val)
111 native_wrmsrl(MSR_AMD64_SEV_ES_GHCB, val);
114 static enum es_result hv_ghcb_hv_call(struct ghcb *ghcb, u64 exit_code,
115 u64 exit_info_1, u64 exit_info_2)
117 /* Fill in protocol and format specifiers */
118 ghcb->protocol_version = hv_ghcb_version;
119 ghcb->ghcb_usage = GHCB_DEFAULT_USAGE;
121 ghcb_set_sw_exit_code(ghcb, exit_code);
122 ghcb_set_sw_exit_info_1(ghcb, exit_info_1);
123 ghcb_set_sw_exit_info_2(ghcb, exit_info_2);
127 if (ghcb->save.sw_exit_info_1 & GENMASK_ULL(31, 0))
133 void __noreturn hv_ghcb_terminate(unsigned int set, unsigned int reason)
135 u64 val = GHCB_MSR_TERM_REQ;
137 /* Tell the hypervisor what went wrong. */
138 val |= GHCB_SEV_TERM_REASON(set, reason);
140 /* Request Guest Termination from Hypvervisor */
145 asm volatile("hlt\n" : : : "memory");
148 bool hv_ghcb_negotiate_protocol(void)
153 /* Save ghcb page gpa. */
154 ghcb_gpa = rd_ghcb_msr();
156 /* Do the GHCB protocol version negotiation */
157 wr_ghcb_msr(GHCB_MSR_SEV_INFO_REQ);
161 if (GHCB_MSR_INFO(val) != GHCB_MSR_SEV_INFO_RESP)
164 if (GHCB_MSR_PROTO_MAX(val) < GHCB_PROTOCOL_MIN ||
165 GHCB_MSR_PROTO_MIN(val) > GHCB_PROTOCOL_MAX)
168 hv_ghcb_version = min_t(size_t, GHCB_MSR_PROTO_MAX(val),
171 /* Write ghcb page back after negotiating protocol. */
172 wr_ghcb_msr(ghcb_gpa);
178 void hv_ghcb_msr_write(u64 msr, u64 value)
180 union hv_ghcb *hv_ghcb;
189 local_irq_save(flags);
190 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
191 hv_ghcb = (union hv_ghcb *)*ghcb_base;
193 local_irq_restore(flags);
197 ghcb_set_rcx(&hv_ghcb->ghcb, msr);
198 ghcb_set_rax(&hv_ghcb->ghcb, lower_32_bits(value));
199 ghcb_set_rdx(&hv_ghcb->ghcb, upper_32_bits(value));
201 if (hv_ghcb_hv_call(&hv_ghcb->ghcb, SVM_EXIT_MSR, 1, 0))
202 pr_warn("Fail to write msr via ghcb %llx.\n", msr);
204 local_irq_restore(flags);
206 EXPORT_SYMBOL_GPL(hv_ghcb_msr_write);
208 void hv_ghcb_msr_read(u64 msr, u64 *value)
210 union hv_ghcb *hv_ghcb;
214 /* Check size of union hv_ghcb here. */
215 BUILD_BUG_ON(sizeof(union hv_ghcb) != HV_HYP_PAGE_SIZE);
222 local_irq_save(flags);
223 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
224 hv_ghcb = (union hv_ghcb *)*ghcb_base;
226 local_irq_restore(flags);
230 ghcb_set_rcx(&hv_ghcb->ghcb, msr);
231 if (hv_ghcb_hv_call(&hv_ghcb->ghcb, SVM_EXIT_MSR, 0, 0))
232 pr_warn("Fail to read msr via ghcb %llx.\n", msr);
234 *value = (u64)lower_32_bits(hv_ghcb->ghcb.save.rax)
235 | ((u64)lower_32_bits(hv_ghcb->ghcb.save.rdx) << 32);
236 local_irq_restore(flags);
238 EXPORT_SYMBOL_GPL(hv_ghcb_msr_read);
241 * hv_mark_gpa_visibility - Set pages visible to host via hvcall.
243 * In Isolation VM, all guest memory is encrypted from host and guest
244 * needs to set memory visible to host via hvcall before sharing memory
247 static int hv_mark_gpa_visibility(u16 count, const u64 pfn[],
248 enum hv_mem_host_visibility visibility)
250 struct hv_gpa_range_for_visibility **input_pcpu, *input;
255 /* no-op if partition isolation is not enabled */
256 if (!hv_is_isolation_supported())
259 if (count > HV_MAX_MODIFY_GPA_REP_COUNT) {
260 pr_err("Hyper-V: GPA count:%d exceeds supported:%lu\n", count,
261 HV_MAX_MODIFY_GPA_REP_COUNT);
265 local_irq_save(flags);
266 input_pcpu = (struct hv_gpa_range_for_visibility **)
267 this_cpu_ptr(hyperv_pcpu_input_arg);
269 if (unlikely(!input)) {
270 local_irq_restore(flags);
274 input->partition_id = HV_PARTITION_ID_SELF;
275 input->host_visibility = visibility;
276 input->reserved0 = 0;
277 input->reserved1 = 0;
278 memcpy((void *)input->gpa_page_list, pfn, count * sizeof(*pfn));
279 hv_status = hv_do_rep_hypercall(
280 HVCALL_MODIFY_SPARSE_GPA_PAGE_HOST_VISIBILITY, count,
281 0, input, &pages_processed);
282 local_irq_restore(flags);
284 if (hv_result_success(hv_status))
291 * hv_vtom_set_host_visibility - Set specified memory visible to host.
293 * In Isolation VM, all guest memory is encrypted from host and guest
294 * needs to set memory visible to host via hvcall before sharing memory
295 * with host. This function works as wrap of hv_mark_gpa_visibility()
296 * with memory base and size.
298 static bool hv_vtom_set_host_visibility(unsigned long kbuffer, int pagecount, bool enc)
300 enum hv_mem_host_visibility visibility = enc ?
301 VMBUS_PAGE_NOT_VISIBLE : VMBUS_PAGE_VISIBLE_READ_WRITE;
307 pfn_array = kmalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL);
311 for (i = 0, pfn = 0; i < pagecount; i++) {
312 pfn_array[pfn] = virt_to_hvpfn((void *)kbuffer + i * HV_HYP_PAGE_SIZE);
315 if (pfn == HV_MAX_MODIFY_GPA_REP_COUNT || i == pagecount - 1) {
316 ret = hv_mark_gpa_visibility(pfn, pfn_array,
320 goto err_free_pfn_array;
331 static bool hv_vtom_tlb_flush_required(bool private)
336 static bool hv_vtom_cache_flush_required(void)
341 static bool hv_is_private_mmio(u64 addr)
344 * Hyper-V always provides a single IO-APIC in a guest VM.
345 * When a paravisor is used, it is emulated by the paravisor
346 * in the guest context and must be mapped private.
348 if (addr >= HV_IOAPIC_BASE_ADDRESS &&
349 addr < (HV_IOAPIC_BASE_ADDRESS + PAGE_SIZE))
352 /* Same with a vTPM */
353 if (addr >= VTPM_BASE_ADDRESS &&
354 addr < (VTPM_BASE_ADDRESS + PAGE_SIZE))
360 void __init hv_vtom_init(void)
363 * By design, a VM using vTOM doesn't see the SEV setting,
364 * so SEV initialization is bypassed and sev_status isn't set.
365 * Set it here to indicate a vTOM VM.
367 sev_status = MSR_AMD64_SNP_VTOM;
368 cc_vendor = CC_VENDOR_AMD;
369 cc_set_mask(ms_hyperv.shared_gpa_boundary);
370 physical_mask &= ms_hyperv.shared_gpa_boundary - 1;
372 x86_platform.hyper.is_private_mmio = hv_is_private_mmio;
373 x86_platform.guest.enc_cache_flush_required = hv_vtom_cache_flush_required;
374 x86_platform.guest.enc_tlb_flush_required = hv_vtom_tlb_flush_required;
375 x86_platform.guest.enc_status_change_finish = hv_vtom_set_host_visibility;
377 /* Set WB as the default cache mode. */
378 mtrr_overwrite_state(NULL, 0, MTRR_TYPE_WRBACK);
381 #endif /* CONFIG_AMD_MEM_ENCRYPT */
383 enum hv_isolation_type hv_get_isolation_type(void)
385 if (!(ms_hyperv.priv_high & HV_ISOLATION))
386 return HV_ISOLATION_TYPE_NONE;
387 return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
389 EXPORT_SYMBOL_GPL(hv_get_isolation_type);
392 * hv_is_isolation_supported - Check system runs in the Hyper-V
395 bool hv_is_isolation_supported(void)
397 if (!cpu_feature_enabled(X86_FEATURE_HYPERVISOR))
400 if (!hypervisor_is_type(X86_HYPER_MS_HYPERV))
403 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
406 DEFINE_STATIC_KEY_FALSE(isolation_type_snp);
409 * hv_isolation_type_snp - Check system runs in the AMD SEV-SNP based
412 bool hv_isolation_type_snp(void)
414 return static_branch_unlikely(&isolation_type_snp);