Linux Audio

Check our new training course

Loading...
v6.2
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * X86 specific Hyper-V initialization code.
  4 *
  5 * Copyright (C) 2016, Microsoft, Inc.
  6 *
  7 * Author : K. Y. Srinivasan <kys@microsoft.com>
  8 */
  9
 10#include <linux/efi.h>
 11#include <linux/types.h>
 12#include <linux/bitfield.h>
 13#include <linux/io.h>
 14#include <asm/apic.h>
 15#include <asm/desc.h>
 16#include <asm/sev.h>
 17#include <asm/hypervisor.h>
 18#include <asm/hyperv-tlfs.h>
 19#include <asm/mshyperv.h>
 20#include <asm/idtentry.h>
 21#include <linux/kexec.h>
 22#include <linux/version.h>
 23#include <linux/vmalloc.h>
 24#include <linux/mm.h>
 25#include <linux/hyperv.h>
 26#include <linux/slab.h>
 27#include <linux/kernel.h>
 28#include <linux/cpuhotplug.h>
 29#include <linux/syscore_ops.h>
 30#include <clocksource/hyperv_timer.h>
 31#include <linux/highmem.h>
 32#include <linux/swiotlb.h>
 33
 34int hyperv_init_cpuhp;
 35u64 hv_current_partition_id = ~0ull;
 36EXPORT_SYMBOL_GPL(hv_current_partition_id);
 37
 38void *hv_hypercall_pg;
 39EXPORT_SYMBOL_GPL(hv_hypercall_pg);
 40
 41union hv_ghcb * __percpu *hv_ghcb_pg;
 42
 43/* Storage to save the hypercall page temporarily for hibernation */
 44static void *hv_hypercall_pg_saved;
 45
 46struct hv_vp_assist_page **hv_vp_assist_page;
 47EXPORT_SYMBOL_GPL(hv_vp_assist_page);
 48
 49static int hyperv_init_ghcb(void)
 50{
 51	u64 ghcb_gpa;
 52	void *ghcb_va;
 53	void **ghcb_base;
 54
 55	if (!hv_isolation_type_snp())
 56		return 0;
 57
 58	if (!hv_ghcb_pg)
 59		return -EINVAL;
 60
 61	/*
 62	 * GHCB page is allocated by paravisor. The address
 63	 * returned by MSR_AMD64_SEV_ES_GHCB is above shared
 64	 * memory boundary and map it here.
 65	 */
 66	rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa);
 67	ghcb_va = memremap(ghcb_gpa, HV_HYP_PAGE_SIZE, MEMREMAP_WB);
 68	if (!ghcb_va)
 69		return -ENOMEM;
 70
 71	ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg);
 72	*ghcb_base = ghcb_va;
 
 73
 74	return 0;
 
 
 75}
 
 76
 77static int hv_cpu_init(unsigned int cpu)
 78{
 79	union hv_vp_assist_msr_contents msr = { 0 };
 80	struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu];
 81	int ret;
 82
 83	ret = hv_common_cpu_init(cpu);
 84	if (ret)
 85		return ret;
 
 
 
 
 
 
 
 
 
 
 86
 87	if (!hv_vp_assist_page)
 88		return 0;
 89
 90	if (hv_root_partition) {
 91		/*
 92		 * For root partition we get the hypervisor provided VP assist
 93		 * page, instead of allocating a new page.
 94		 */
 95		rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
 96		*hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT,
 97				PAGE_SIZE, MEMREMAP_WB);
 98	} else {
 99		/*
100		 * The VP assist page is an "overlay" page (see Hyper-V TLFS's
101		 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed
102		 * out to make sure we always write the EOI MSR in
103		 * hv_apic_eoi_write() *after* the EOI optimization is disabled
104		 * in hv_cpu_die(), otherwise a CPU may not be stopped in the
105		 * case of CPU offlining and the VM will hang.
106		 */
107		if (!*hvp)
108			*hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
109		if (*hvp)
110			msr.pfn = vmalloc_to_pfn(*hvp);
111
112	}
113	if (!WARN_ON(!(*hvp))) {
114		msr.enable = 1;
115		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
 
 
 
 
 
 
116	}
117
118	return hyperv_init_ghcb();
119}
120
121static void (*hv_reenlightenment_cb)(void);
122
123static void hv_reenlightenment_notify(struct work_struct *dummy)
124{
125	struct hv_tsc_emulation_status emu_status;
126
127	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
128
129	/* Don't issue the callback if TSC accesses are not emulated */
130	if (hv_reenlightenment_cb && emu_status.inprogress)
131		hv_reenlightenment_cb();
132}
133static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
134
135void hyperv_stop_tsc_emulation(void)
136{
137	u64 freq;
138	struct hv_tsc_emulation_status emu_status;
139
140	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
141	emu_status.inprogress = 0;
142	wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
143
144	rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
145	tsc_khz = div64_u64(freq, 1000);
146}
147EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
148
149static inline bool hv_reenlightenment_available(void)
150{
151	/*
152	 * Check for required features and privileges to make TSC frequency
153	 * change notifications work.
154	 */
155	return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
156		ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
157		ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
158}
159
160DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
161{
162	ack_APIC_irq();
 
163	inc_irq_stat(irq_hv_reenlightenment_count);
 
164	schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
 
 
165}
166
167void set_hv_tscchange_cb(void (*cb)(void))
168{
169	struct hv_reenlightenment_control re_ctrl = {
170		.vector = HYPERV_REENLIGHTENMENT_VECTOR,
171		.enabled = 1,
 
172	};
173	struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
174
175	if (!hv_reenlightenment_available()) {
176		pr_warn("Hyper-V: reenlightenment support is unavailable\n");
177		return;
178	}
179
180	if (!hv_vp_index)
181		return;
182
183	hv_reenlightenment_cb = cb;
184
185	/* Make sure callback is registered before we write to MSRs */
186	wmb();
187
188	re_ctrl.target_vp = hv_vp_index[get_cpu()];
189
190	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
191	wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
192
193	put_cpu();
194}
195EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
196
197void clear_hv_tscchange_cb(void)
198{
199	struct hv_reenlightenment_control re_ctrl;
200
201	if (!hv_reenlightenment_available())
202		return;
203
204	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
205	re_ctrl.enabled = 0;
206	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
207
208	hv_reenlightenment_cb = NULL;
209}
210EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
211
212static int hv_cpu_die(unsigned int cpu)
213{
214	struct hv_reenlightenment_control re_ctrl;
215	unsigned int new_cpu;
216	void **ghcb_va;
217
218	if (hv_ghcb_pg) {
219		ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg);
220		if (*ghcb_va)
221			memunmap(*ghcb_va);
222		*ghcb_va = NULL;
223	}
224
225	hv_common_cpu_die(cpu);
 
 
 
 
 
226
227	if (hv_vp_assist_page && hv_vp_assist_page[cpu]) {
228		union hv_vp_assist_msr_contents msr = { 0 };
229		if (hv_root_partition) {
230			/*
231			 * For root partition the VP assist page is mapped to
232			 * hypervisor provided page, and thus we unmap the
233			 * page here and nullify it, so that in future we have
234			 * correct page address mapped in hv_cpu_init.
235			 */
236			memunmap(hv_vp_assist_page[cpu]);
237			hv_vp_assist_page[cpu] = NULL;
238			rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
239			msr.enable = 0;
240		}
241		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64);
242	}
243
244	if (hv_reenlightenment_cb == NULL)
245		return 0;
246
247	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
248	if (re_ctrl.target_vp == hv_vp_index[cpu]) {
249		/*
250		 * Reassign reenlightenment notifications to some other online
251		 * CPU or just disable the feature if there are no online CPUs
252		 * left (happens on hibernation).
253		 */
254		new_cpu = cpumask_any_but(cpu_online_mask, cpu);
255
256		if (new_cpu < nr_cpu_ids)
257			re_ctrl.target_vp = hv_vp_index[new_cpu];
258		else
259			re_ctrl.enabled = 0;
260
261		wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
262	}
263
264	return 0;
265}
266
267static int __init hv_pci_init(void)
268{
269	int gen2vm = efi_enabled(EFI_BOOT);
270
271	/*
272	 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
273	 * The purpose is to suppress the harmless warning:
274	 * "PCI: Fatal: No config space access function found"
275	 */
276	if (gen2vm)
277		return 0;
278
279	/* For Generation-1 VM, we'll proceed in pci_arch_init().  */
280	return 1;
281}
282
283static int hv_suspend(void)
284{
285	union hv_x64_msr_hypercall_contents hypercall_msr;
286	int ret;
287
288	if (hv_root_partition)
289		return -EPERM;
290
291	/*
292	 * Reset the hypercall page as it is going to be invalidated
293	 * across hibernation. Setting hv_hypercall_pg to NULL ensures
294	 * that any subsequent hypercall operation fails safely instead of
295	 * crashing due to an access of an invalid page. The hypercall page
296	 * pointer is restored on resume.
297	 */
298	hv_hypercall_pg_saved = hv_hypercall_pg;
299	hv_hypercall_pg = NULL;
300
301	/* Disable the hypercall page in the hypervisor */
302	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
303	hypercall_msr.enable = 0;
304	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
305
306	ret = hv_cpu_die(0);
307	return ret;
308}
309
310static void hv_resume(void)
311{
312	union hv_x64_msr_hypercall_contents hypercall_msr;
313	int ret;
314
315	ret = hv_cpu_init(0);
316	WARN_ON(ret);
317
318	/* Re-enable the hypercall page */
319	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
320	hypercall_msr.enable = 1;
321	hypercall_msr.guest_physical_address =
322		vmalloc_to_pfn(hv_hypercall_pg_saved);
323	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
324
325	hv_hypercall_pg = hv_hypercall_pg_saved;
326	hv_hypercall_pg_saved = NULL;
327
328	/*
329	 * Reenlightenment notifications are disabled by hv_cpu_die(0),
330	 * reenable them here if hv_reenlightenment_cb was previously set.
331	 */
332	if (hv_reenlightenment_cb)
333		set_hv_tscchange_cb(hv_reenlightenment_cb);
334}
335
336/* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */
337static struct syscore_ops hv_syscore_ops = {
338	.suspend	= hv_suspend,
339	.resume		= hv_resume,
340};
341
342static void (* __initdata old_setup_percpu_clockev)(void);
343
344static void __init hv_stimer_setup_percpu_clockev(void)
345{
346	/*
347	 * Ignore any errors in setting up stimer clockevents
348	 * as we can run with the LAPIC timer as a fallback.
349	 */
350	(void)hv_stimer_alloc(false);
351
352	/*
353	 * Still register the LAPIC timer, because the direct-mode STIMER is
354	 * not supported by old versions of Hyper-V. This also allows users
355	 * to switch to LAPIC timer via /sys, if they want to.
356	 */
357	if (old_setup_percpu_clockev)
358		old_setup_percpu_clockev();
359}
360
361static void __init hv_get_partition_id(void)
362{
363	struct hv_get_partition_id *output_page;
364	u64 status;
365	unsigned long flags;
366
367	local_irq_save(flags);
368	output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
369	status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
370	if (!hv_result_success(status)) {
371		/* No point in proceeding if this failed */
372		pr_err("Failed to get partition ID: %lld\n", status);
373		BUG();
374	}
375	hv_current_partition_id = output_page->partition_id;
376	local_irq_restore(flags);
377}
378
379/*
380 * This function is to be invoked early in the boot sequence after the
381 * hypervisor has been detected.
382 *
383 * 1. Setup the hypercall page.
384 * 2. Register Hyper-V specific clocksource.
385 * 3. Setup Hyper-V specific APIC entry points.
386 */
387void __init hyperv_init(void)
388{
389	u64 guest_id;
390	union hv_x64_msr_hypercall_contents hypercall_msr;
391	int cpuhp;
392
393	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
394		return;
395
396	if (hv_common_init())
 
 
 
 
397		return;
398
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
399	hv_vp_assist_page = kcalloc(num_possible_cpus(),
400				    sizeof(*hv_vp_assist_page), GFP_KERNEL);
401	if (!hv_vp_assist_page) {
402		ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
403		goto common_free;
404	}
405
406	if (hv_isolation_type_snp()) {
407		/* Negotiate GHCB Version. */
408		if (!hv_ghcb_negotiate_protocol())
409			hv_ghcb_terminate(SEV_TERM_SET_GEN,
410					  GHCB_SEV_ES_PROT_UNSUPPORTED);
411
412		hv_ghcb_pg = alloc_percpu(union hv_ghcb *);
413		if (!hv_ghcb_pg)
414			goto free_vp_assist_page;
415	}
416
417	cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
418				  hv_cpu_init, hv_cpu_die);
419	if (cpuhp < 0)
420		goto free_ghcb_page;
421
422	/*
423	 * Setup the hypercall page and enable hypercalls.
424	 * 1. Register the guest ID
425	 * 2. Enable the hypercall and register the hypercall page
426	 */
427	guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
428	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
429
430	/* Hyper-V requires to write guest os id via ghcb in SNP IVM. */
431	hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id);
432
433	hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
434			VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
435			VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
436			__builtin_return_address(0));
437	if (hv_hypercall_pg == NULL)
438		goto clean_guest_os_id;
439
440	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
441	hypercall_msr.enable = 1;
442
443	if (hv_root_partition) {
444		struct page *pg;
445		void *src;
446
447		/*
448		 * For the root partition, the hypervisor will set up its
449		 * hypercall page. The hypervisor guarantees it will not show
450		 * up in the root's address space. The root can't change the
451		 * location of the hypercall page.
452		 *
453		 * Order is important here. We must enable the hypercall page
454		 * so it is populated with code, then copy the code to an
455		 * executable page.
456		 */
457		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
458
459		pg = vmalloc_to_page(hv_hypercall_pg);
460		src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
461				MEMREMAP_WB);
462		BUG_ON(!src);
463		memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE);
464		memunmap(src);
465
466		hv_remap_tsc_clocksource();
467	} else {
468		hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
469		wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
470	}
471
472	/*
473	 * hyperv_init() is called before LAPIC is initialized: see
474	 * apic_intr_mode_init() -> x86_platform.apic_post_init() and
475	 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER
476	 * depends on LAPIC, so hv_stimer_alloc() should be called from
477	 * x86_init.timers.setup_percpu_clockev.
478	 */
479	old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
480	x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
481
482	hv_apic_init();
483
484	x86_init.pci.arch_init = hv_pci_init;
485
486	register_syscore_ops(&hv_syscore_ops);
487
488	hyperv_init_cpuhp = cpuhp;
489
490	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
491		hv_get_partition_id();
492
493	BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
494
495#ifdef CONFIG_PCI_MSI
496	/*
497	 * If we're running as root, we want to create our own PCI MSI domain.
498	 * We can't set this in hv_pci_init because that would be too late.
499	 */
500	if (hv_root_partition)
501		x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
502#endif
503
504	/* Query the VMs extended capability once, so that it can be cached. */
505	hv_query_ext_cap(0);
506
507#ifdef CONFIG_SWIOTLB
508	/*
509	 * Swiotlb bounce buffer needs to be mapped in extra address
510	 * space. Map function doesn't work in the early place and so
511	 * call swiotlb_update_mem_attributes() here.
512	 */
513	if (hv_is_isolation_supported())
514		swiotlb_update_mem_attributes();
515#endif
516
517	return;
518
519clean_guest_os_id:
520	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
521	hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
522	cpuhp_remove_state(cpuhp);
523free_ghcb_page:
524	free_percpu(hv_ghcb_pg);
525free_vp_assist_page:
526	kfree(hv_vp_assist_page);
527	hv_vp_assist_page = NULL;
528common_free:
529	hv_common_free();
 
530}
531
532/*
533 * This routine is called before kexec/kdump, it does the required cleanup.
534 */
535void hyperv_cleanup(void)
536{
537	union hv_x64_msr_hypercall_contents hypercall_msr;
538	union hv_reference_tsc_msr tsc_msr;
539
540	/* Reset our OS id */
541	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
542	hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0);
543
544	/*
545	 * Reset hypercall page reference before reset the page,
546	 * let hypercall operations fail safely rather than
547	 * panic the kernel for using invalid hypercall page
548	 */
549	hv_hypercall_pg = NULL;
550
551	/* Reset the hypercall page */
552	hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL);
553	hypercall_msr.enable = 0;
554	hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
555
556	/* Reset the TSC page */
557	tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC);
558	tsc_msr.enable = 0;
559	hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
560}
 
561
562void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
563{
564	static bool panic_reported;
565	u64 guest_id;
566
567	if (in_die && !panic_on_oops)
568		return;
569
570	/*
571	 * We prefer to report panic on 'die' chain as we have proper
572	 * registers to report, but if we miss it (e.g. on BUG()) we need
573	 * to report it on 'panic'.
574	 */
575	if (panic_reported)
576		return;
577	panic_reported = true;
578
579	rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
580
581	wrmsrl(HV_X64_MSR_CRASH_P0, err);
582	wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
583	wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
584	wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
585	wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
586
587	/*
588	 * Let Hyper-V know there is crash data available
589	 */
590	wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
591}
592EXPORT_SYMBOL_GPL(hyperv_report_panic);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
593
594bool hv_is_hyperv_initialized(void)
595{
596	union hv_x64_msr_hypercall_contents hypercall_msr;
597
598	/*
599	 * Ensure that we're really on Hyper-V, and not a KVM or Xen
600	 * emulation of Hyper-V
601	 */
602	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
603		return false;
604
605	/*
606	 * Verify that earlier initialization succeeded by checking
607	 * that the hypercall page is setup
608	 */
609	hypercall_msr.as_uint64 = 0;
610	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
611
612	return hypercall_msr.enable;
613}
614EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
v5.4
  1// SPDX-License-Identifier: GPL-2.0-only
  2/*
  3 * X86 specific Hyper-V initialization code.
  4 *
  5 * Copyright (C) 2016, Microsoft, Inc.
  6 *
  7 * Author : K. Y. Srinivasan <kys@microsoft.com>
  8 */
  9
 10#include <linux/efi.h>
 11#include <linux/types.h>
 
 
 12#include <asm/apic.h>
 13#include <asm/desc.h>
 
 14#include <asm/hypervisor.h>
 15#include <asm/hyperv-tlfs.h>
 16#include <asm/mshyperv.h>
 
 
 17#include <linux/version.h>
 18#include <linux/vmalloc.h>
 19#include <linux/mm.h>
 20#include <linux/hyperv.h>
 21#include <linux/slab.h>
 
 22#include <linux/cpuhotplug.h>
 
 23#include <clocksource/hyperv_timer.h>
 
 
 
 
 
 
 24
 25void *hv_hypercall_pg;
 26EXPORT_SYMBOL_GPL(hv_hypercall_pg);
 27
 28u32 *hv_vp_index;
 29EXPORT_SYMBOL_GPL(hv_vp_index);
 
 
 30
 31struct hv_vp_assist_page **hv_vp_assist_page;
 32EXPORT_SYMBOL_GPL(hv_vp_assist_page);
 33
 34void  __percpu **hyperv_pcpu_input_arg;
 35EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
 
 
 
 
 
 
 36
 37u32 hv_max_vp_index;
 38EXPORT_SYMBOL_GPL(hv_max_vp_index);
 39
 40void *hv_alloc_hyperv_page(void)
 41{
 42	BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
 
 
 
 
 
 
 43
 44	return (void *)__get_free_page(GFP_KERNEL);
 45}
 46EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
 47
 48void hv_free_hyperv_page(unsigned long addr)
 49{
 50	free_page(addr);
 51}
 52EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
 53
 54static int hv_cpu_init(unsigned int cpu)
 55{
 56	u64 msr_vp_index;
 57	struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
 58	void **input_arg;
 59	struct page *pg;
 60
 61	input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
 62	pg = alloc_page(GFP_KERNEL);
 63	if (unlikely(!pg))
 64		return -ENOMEM;
 65	*input_arg = page_address(pg);
 66
 67	hv_get_vp_index(msr_vp_index);
 68
 69	hv_vp_index[smp_processor_id()] = msr_vp_index;
 70
 71	if (msr_vp_index > hv_max_vp_index)
 72		hv_max_vp_index = msr_vp_index;
 73
 74	if (!hv_vp_assist_page)
 75		return 0;
 76
 77	/*
 78	 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
 79	 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
 80	 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
 81	 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
 82	 * not be stopped in the case of CPU offlining and the VM will hang.
 83	 */
 84	if (!*hvp) {
 85		*hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO,
 86				 PAGE_KERNEL);
 
 
 
 
 
 
 
 
 
 
 
 
 87	}
 88
 89	if (*hvp) {
 90		u64 val;
 91
 92		val = vmalloc_to_pfn(*hvp);
 93		val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
 94			HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
 95
 96		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
 97	}
 98
 99	return 0;
100}
101
102static void (*hv_reenlightenment_cb)(void);
103
104static void hv_reenlightenment_notify(struct work_struct *dummy)
105{
106	struct hv_tsc_emulation_status emu_status;
107
108	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
109
110	/* Don't issue the callback if TSC accesses are not emulated */
111	if (hv_reenlightenment_cb && emu_status.inprogress)
112		hv_reenlightenment_cb();
113}
114static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
115
116void hyperv_stop_tsc_emulation(void)
117{
118	u64 freq;
119	struct hv_tsc_emulation_status emu_status;
120
121	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
122	emu_status.inprogress = 0;
123	wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
124
125	rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
126	tsc_khz = div64_u64(freq, 1000);
127}
128EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
129
130static inline bool hv_reenlightenment_available(void)
131{
132	/*
133	 * Check for required features and priviliges to make TSC frequency
134	 * change notifications work.
135	 */
136	return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
137		ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
138		ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
139}
140
141__visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
142{
143	entering_ack_irq();
144
145	inc_irq_stat(irq_hv_reenlightenment_count);
146
147	schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
148
149	exiting_irq();
150}
151
152void set_hv_tscchange_cb(void (*cb)(void))
153{
154	struct hv_reenlightenment_control re_ctrl = {
155		.vector = HYPERV_REENLIGHTENMENT_VECTOR,
156		.enabled = 1,
157		.target_vp = hv_vp_index[smp_processor_id()]
158	};
159	struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
160
161	if (!hv_reenlightenment_available()) {
162		pr_warn("Hyper-V: reenlightenment support is unavailable\n");
163		return;
164	}
165
 
 
 
166	hv_reenlightenment_cb = cb;
167
168	/* Make sure callback is registered before we write to MSRs */
169	wmb();
170
 
 
171	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
172	wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
 
 
173}
174EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
175
176void clear_hv_tscchange_cb(void)
177{
178	struct hv_reenlightenment_control re_ctrl;
179
180	if (!hv_reenlightenment_available())
181		return;
182
183	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
184	re_ctrl.enabled = 0;
185	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
186
187	hv_reenlightenment_cb = NULL;
188}
189EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
190
191static int hv_cpu_die(unsigned int cpu)
192{
193	struct hv_reenlightenment_control re_ctrl;
194	unsigned int new_cpu;
195	unsigned long flags;
196	void **input_arg;
197	void *input_pg = NULL;
 
 
 
 
 
198
199	local_irq_save(flags);
200	input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
201	input_pg = *input_arg;
202	*input_arg = NULL;
203	local_irq_restore(flags);
204	free_page((unsigned long)input_pg);
205
206	if (hv_vp_assist_page && hv_vp_assist_page[cpu])
207		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
208
209	if (hv_reenlightenment_cb == NULL)
210		return 0;
211
212	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
213	if (re_ctrl.target_vp == hv_vp_index[cpu]) {
214		/* Reassign to some other online CPU */
 
 
 
 
215		new_cpu = cpumask_any_but(cpu_online_mask, cpu);
216
217		re_ctrl.target_vp = hv_vp_index[new_cpu];
 
 
 
 
218		wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
219	}
220
221	return 0;
222}
223
224static int __init hv_pci_init(void)
225{
226	int gen2vm = efi_enabled(EFI_BOOT);
227
228	/*
229	 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
230	 * The purpose is to suppress the harmless warning:
231	 * "PCI: Fatal: No config space access function found"
232	 */
233	if (gen2vm)
234		return 0;
235
236	/* For Generation-1 VM, we'll proceed in pci_arch_init().  */
237	return 1;
238}
239
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
240/*
241 * This function is to be invoked early in the boot sequence after the
242 * hypervisor has been detected.
243 *
244 * 1. Setup the hypercall page.
245 * 2. Register Hyper-V specific clocksource.
246 * 3. Setup Hyper-V specific APIC entry points.
247 */
248void __init hyperv_init(void)
249{
250	u64 guest_id, required_msrs;
251	union hv_x64_msr_hypercall_contents hypercall_msr;
252	int cpuhp, i;
253
254	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
255		return;
256
257	/* Absolutely required MSRs */
258	required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
259		HV_X64_MSR_VP_INDEX_AVAILABLE;
260
261	if ((ms_hyperv.features & required_msrs) != required_msrs)
262		return;
263
264	/*
265	 * Allocate the per-CPU state for the hypercall input arg.
266	 * If this allocation fails, we will not be able to setup
267	 * (per-CPU) hypercall input page and thus this failure is
268	 * fatal on Hyper-V.
269	 */
270	hyperv_pcpu_input_arg = alloc_percpu(void  *);
271
272	BUG_ON(hyperv_pcpu_input_arg == NULL);
273
274	/* Allocate percpu VP index */
275	hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
276				    GFP_KERNEL);
277	if (!hv_vp_index)
278		return;
279
280	for (i = 0; i < num_possible_cpus(); i++)
281		hv_vp_index[i] = VP_INVAL;
282
283	hv_vp_assist_page = kcalloc(num_possible_cpus(),
284				    sizeof(*hv_vp_assist_page), GFP_KERNEL);
285	if (!hv_vp_assist_page) {
286		ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
287		goto free_vp_index;
 
 
 
 
 
 
 
 
 
 
 
288	}
289
290	cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
291				  hv_cpu_init, hv_cpu_die);
292	if (cpuhp < 0)
293		goto free_vp_assist_page;
294
295	/*
296	 * Setup the hypercall page and enable hypercalls.
297	 * 1. Register the guest ID
298	 * 2. Enable the hypercall and register the hypercall page
299	 */
300	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
301	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
302
303	hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
304	if (hv_hypercall_pg == NULL) {
305		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
306		goto remove_cpuhp_state;
307	}
 
 
 
 
308
309	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
310	hypercall_msr.enable = 1;
311	hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
312	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
313
314	hv_apic_init();
315
316	x86_init.pci.arch_init = hv_pci_init;
317
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
318	return;
319
320remove_cpuhp_state:
 
 
321	cpuhp_remove_state(cpuhp);
 
 
322free_vp_assist_page:
323	kfree(hv_vp_assist_page);
324	hv_vp_assist_page = NULL;
325free_vp_index:
326	kfree(hv_vp_index);
327	hv_vp_index = NULL;
328}
329
330/*
331 * This routine is called before kexec/kdump, it does the required cleanup.
332 */
333void hyperv_cleanup(void)
334{
335	union hv_x64_msr_hypercall_contents hypercall_msr;
 
336
337	/* Reset our OS id */
338	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
 
339
340	/*
341	 * Reset hypercall page reference before reset the page,
342	 * let hypercall operations fail safely rather than
343	 * panic the kernel for using invalid hypercall page
344	 */
345	hv_hypercall_pg = NULL;
346
347	/* Reset the hypercall page */
348	hypercall_msr.as_uint64 = 0;
349	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
 
350
351	/* Reset the TSC page */
352	hypercall_msr.as_uint64 = 0;
353	wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
 
354}
355EXPORT_SYMBOL_GPL(hyperv_cleanup);
356
357void hyperv_report_panic(struct pt_regs *regs, long err)
358{
359	static bool panic_reported;
360	u64 guest_id;
361
 
 
 
362	/*
363	 * We prefer to report panic on 'die' chain as we have proper
364	 * registers to report, but if we miss it (e.g. on BUG()) we need
365	 * to report it on 'panic'.
366	 */
367	if (panic_reported)
368		return;
369	panic_reported = true;
370
371	rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
372
373	wrmsrl(HV_X64_MSR_CRASH_P0, err);
374	wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
375	wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
376	wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
377	wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
378
379	/*
380	 * Let Hyper-V know there is crash data available
381	 */
382	wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
383}
384EXPORT_SYMBOL_GPL(hyperv_report_panic);
385
386/**
387 * hyperv_report_panic_msg - report panic message to Hyper-V
388 * @pa: physical address of the panic page containing the message
389 * @size: size of the message in the page
390 */
391void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
392{
393	/*
394	 * P3 to contain the physical address of the panic page & P4 to
395	 * contain the size of the panic data in that page. Rest of the
396	 * registers are no-op when the NOTIFY_MSG flag is set.
397	 */
398	wrmsrl(HV_X64_MSR_CRASH_P0, 0);
399	wrmsrl(HV_X64_MSR_CRASH_P1, 0);
400	wrmsrl(HV_X64_MSR_CRASH_P2, 0);
401	wrmsrl(HV_X64_MSR_CRASH_P3, pa);
402	wrmsrl(HV_X64_MSR_CRASH_P4, size);
403
404	/*
405	 * Let Hyper-V know there is crash data available along with
406	 * the panic message.
407	 */
408	wrmsrl(HV_X64_MSR_CRASH_CTL,
409	       (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
410}
411EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
412
413bool hv_is_hyperv_initialized(void)
414{
415	union hv_x64_msr_hypercall_contents hypercall_msr;
416
417	/*
418	 * Ensure that we're really on Hyper-V, and not a KVM or Xen
419	 * emulation of Hyper-V
420	 */
421	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
422		return false;
423
424	/*
425	 * Verify that earlier initialization succeeded by checking
426	 * that the hypercall page is setup
427	 */
428	hypercall_msr.as_uint64 = 0;
429	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
430
431	return hypercall_msr.enable;
432}
433EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);