xref: /linux/arch/x86/hyperv/hv_init.c (revision e6f2a617ac53bc0753b885ffb94379ff48b2e2df)
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/acpi.h>
11 #include <linux/efi.h>
12 #include <linux/types.h>
13 #include <asm/apic.h>
14 #include <asm/desc.h>
15 #include <asm/hypervisor.h>
16 #include <asm/hyperv-tlfs.h>
17 #include <asm/mshyperv.h>
18 #include <linux/version.h>
19 #include <linux/vmalloc.h>
20 #include <linux/mm.h>
21 #include <linux/hyperv.h>
22 #include <linux/slab.h>
23 #include <linux/cpuhotplug.h>
24 #include <clocksource/hyperv_timer.h>
25 
26 void *hv_hypercall_pg;
27 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
28 
29 u32 *hv_vp_index;
30 EXPORT_SYMBOL_GPL(hv_vp_index);
31 
32 struct hv_vp_assist_page **hv_vp_assist_page;
33 EXPORT_SYMBOL_GPL(hv_vp_assist_page);
34 
35 void  __percpu **hyperv_pcpu_input_arg;
36 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
37 
38 u32 hv_max_vp_index;
39 EXPORT_SYMBOL_GPL(hv_max_vp_index);
40 
41 void *hv_alloc_hyperv_page(void)
42 {
43 	BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
44 
45 	return (void *)__get_free_page(GFP_KERNEL);
46 }
47 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
48 
49 void *hv_alloc_hyperv_zeroed_page(void)
50 {
51         BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
52 
53         return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
54 }
55 EXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page);
56 
57 void hv_free_hyperv_page(unsigned long addr)
58 {
59 	free_page(addr);
60 }
61 EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
62 
63 static int hv_cpu_init(unsigned int cpu)
64 {
65 	u64 msr_vp_index;
66 	struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
67 	void **input_arg;
68 	struct page *pg;
69 
70 	input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
71 	pg = alloc_page(GFP_KERNEL);
72 	if (unlikely(!pg))
73 		return -ENOMEM;
74 	*input_arg = page_address(pg);
75 
76 	hv_get_vp_index(msr_vp_index);
77 
78 	hv_vp_index[smp_processor_id()] = msr_vp_index;
79 
80 	if (msr_vp_index > hv_max_vp_index)
81 		hv_max_vp_index = msr_vp_index;
82 
83 	if (!hv_vp_assist_page)
84 		return 0;
85 
86 	/*
87 	 * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section
88 	 * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure
89 	 * we always write the EOI MSR in hv_apic_eoi_write() *after* the
90 	 * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may
91 	 * not be stopped in the case of CPU offlining and the VM will hang.
92 	 */
93 	if (!*hvp) {
94 		*hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO,
95 				 PAGE_KERNEL);
96 	}
97 
98 	if (*hvp) {
99 		u64 val;
100 
101 		val = vmalloc_to_pfn(*hvp);
102 		val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
103 			HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
104 
105 		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
106 	}
107 
108 	return 0;
109 }
110 
111 static void (*hv_reenlightenment_cb)(void);
112 
113 static void hv_reenlightenment_notify(struct work_struct *dummy)
114 {
115 	struct hv_tsc_emulation_status emu_status;
116 
117 	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
118 
119 	/* Don't issue the callback if TSC accesses are not emulated */
120 	if (hv_reenlightenment_cb && emu_status.inprogress)
121 		hv_reenlightenment_cb();
122 }
123 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
124 
125 void hyperv_stop_tsc_emulation(void)
126 {
127 	u64 freq;
128 	struct hv_tsc_emulation_status emu_status;
129 
130 	rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
131 	emu_status.inprogress = 0;
132 	wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
133 
134 	rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
135 	tsc_khz = div64_u64(freq, 1000);
136 }
137 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
138 
139 static inline bool hv_reenlightenment_available(void)
140 {
141 	/*
142 	 * Check for required features and priviliges to make TSC frequency
143 	 * change notifications work.
144 	 */
145 	return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
146 		ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
147 		ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
148 }
149 
150 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
151 {
152 	entering_ack_irq();
153 
154 	inc_irq_stat(irq_hv_reenlightenment_count);
155 
156 	schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
157 
158 	exiting_irq();
159 }
160 
161 void set_hv_tscchange_cb(void (*cb)(void))
162 {
163 	struct hv_reenlightenment_control re_ctrl = {
164 		.vector = HYPERV_REENLIGHTENMENT_VECTOR,
165 		.enabled = 1,
166 		.target_vp = hv_vp_index[smp_processor_id()]
167 	};
168 	struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
169 
170 	if (!hv_reenlightenment_available()) {
171 		pr_warn("Hyper-V: reenlightenment support is unavailable\n");
172 		return;
173 	}
174 
175 	hv_reenlightenment_cb = cb;
176 
177 	/* Make sure callback is registered before we write to MSRs */
178 	wmb();
179 
180 	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
181 	wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
182 }
183 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
184 
185 void clear_hv_tscchange_cb(void)
186 {
187 	struct hv_reenlightenment_control re_ctrl;
188 
189 	if (!hv_reenlightenment_available())
190 		return;
191 
192 	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
193 	re_ctrl.enabled = 0;
194 	wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
195 
196 	hv_reenlightenment_cb = NULL;
197 }
198 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
199 
200 static int hv_cpu_die(unsigned int cpu)
201 {
202 	struct hv_reenlightenment_control re_ctrl;
203 	unsigned int new_cpu;
204 	unsigned long flags;
205 	void **input_arg;
206 	void *input_pg = NULL;
207 
208 	local_irq_save(flags);
209 	input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
210 	input_pg = *input_arg;
211 	*input_arg = NULL;
212 	local_irq_restore(flags);
213 	free_page((unsigned long)input_pg);
214 
215 	if (hv_vp_assist_page && hv_vp_assist_page[cpu])
216 		wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
217 
218 	if (hv_reenlightenment_cb == NULL)
219 		return 0;
220 
221 	rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
222 	if (re_ctrl.target_vp == hv_vp_index[cpu]) {
223 		/* Reassign to some other online CPU */
224 		new_cpu = cpumask_any_but(cpu_online_mask, cpu);
225 
226 		re_ctrl.target_vp = hv_vp_index[new_cpu];
227 		wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
228 	}
229 
230 	return 0;
231 }
232 
233 static int __init hv_pci_init(void)
234 {
235 	int gen2vm = efi_enabled(EFI_BOOT);
236 
237 	/*
238 	 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
239 	 * The purpose is to suppress the harmless warning:
240 	 * "PCI: Fatal: No config space access function found"
241 	 */
242 	if (gen2vm)
243 		return 0;
244 
245 	/* For Generation-1 VM, we'll proceed in pci_arch_init().  */
246 	return 1;
247 }
248 
249 /*
250  * This function is to be invoked early in the boot sequence after the
251  * hypervisor has been detected.
252  *
253  * 1. Setup the hypercall page.
254  * 2. Register Hyper-V specific clocksource.
255  * 3. Setup Hyper-V specific APIC entry points.
256  */
257 void __init hyperv_init(void)
258 {
259 	u64 guest_id, required_msrs;
260 	union hv_x64_msr_hypercall_contents hypercall_msr;
261 	int cpuhp, i;
262 
263 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
264 		return;
265 
266 	/* Absolutely required MSRs */
267 	required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
268 		HV_X64_MSR_VP_INDEX_AVAILABLE;
269 
270 	if ((ms_hyperv.features & required_msrs) != required_msrs)
271 		return;
272 
273 	/*
274 	 * Allocate the per-CPU state for the hypercall input arg.
275 	 * If this allocation fails, we will not be able to setup
276 	 * (per-CPU) hypercall input page and thus this failure is
277 	 * fatal on Hyper-V.
278 	 */
279 	hyperv_pcpu_input_arg = alloc_percpu(void  *);
280 
281 	BUG_ON(hyperv_pcpu_input_arg == NULL);
282 
283 	/* Allocate percpu VP index */
284 	hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
285 				    GFP_KERNEL);
286 	if (!hv_vp_index)
287 		return;
288 
289 	for (i = 0; i < num_possible_cpus(); i++)
290 		hv_vp_index[i] = VP_INVAL;
291 
292 	hv_vp_assist_page = kcalloc(num_possible_cpus(),
293 				    sizeof(*hv_vp_assist_page), GFP_KERNEL);
294 	if (!hv_vp_assist_page) {
295 		ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
296 		goto free_vp_index;
297 	}
298 
299 	cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
300 				  hv_cpu_init, hv_cpu_die);
301 	if (cpuhp < 0)
302 		goto free_vp_assist_page;
303 
304 	/*
305 	 * Setup the hypercall page and enable hypercalls.
306 	 * 1. Register the guest ID
307 	 * 2. Enable the hypercall and register the hypercall page
308 	 */
309 	guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
310 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
311 
312 	hv_hypercall_pg  = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
313 	if (hv_hypercall_pg == NULL) {
314 		wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
315 		goto remove_cpuhp_state;
316 	}
317 
318 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
319 	hypercall_msr.enable = 1;
320 	hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
321 	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
322 
323 	/*
324 	 * Ignore any errors in setting up stimer clockevents
325 	 * as we can run with the LAPIC timer as a fallback.
326 	 */
327 	(void)hv_stimer_alloc();
328 
329 	hv_apic_init();
330 
331 	x86_init.pci.arch_init = hv_pci_init;
332 
333 	return;
334 
335 remove_cpuhp_state:
336 	cpuhp_remove_state(cpuhp);
337 free_vp_assist_page:
338 	kfree(hv_vp_assist_page);
339 	hv_vp_assist_page = NULL;
340 free_vp_index:
341 	kfree(hv_vp_index);
342 	hv_vp_index = NULL;
343 }
344 
345 /*
346  * This routine is called before kexec/kdump, it does the required cleanup.
347  */
348 void hyperv_cleanup(void)
349 {
350 	union hv_x64_msr_hypercall_contents hypercall_msr;
351 
352 	/* Reset our OS id */
353 	wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
354 
355 	/*
356 	 * Reset hypercall page reference before reset the page,
357 	 * let hypercall operations fail safely rather than
358 	 * panic the kernel for using invalid hypercall page
359 	 */
360 	hv_hypercall_pg = NULL;
361 
362 	/* Reset the hypercall page */
363 	hypercall_msr.as_uint64 = 0;
364 	wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
365 
366 	/* Reset the TSC page */
367 	hypercall_msr.as_uint64 = 0;
368 	wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
369 }
370 EXPORT_SYMBOL_GPL(hyperv_cleanup);
371 
372 void hyperv_report_panic(struct pt_regs *regs, long err)
373 {
374 	static bool panic_reported;
375 	u64 guest_id;
376 
377 	/*
378 	 * We prefer to report panic on 'die' chain as we have proper
379 	 * registers to report, but if we miss it (e.g. on BUG()) we need
380 	 * to report it on 'panic'.
381 	 */
382 	if (panic_reported)
383 		return;
384 	panic_reported = true;
385 
386 	rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
387 
388 	wrmsrl(HV_X64_MSR_CRASH_P0, err);
389 	wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
390 	wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
391 	wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
392 	wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
393 
394 	/*
395 	 * Let Hyper-V know there is crash data available
396 	 */
397 	wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
398 }
399 EXPORT_SYMBOL_GPL(hyperv_report_panic);
400 
401 /**
402  * hyperv_report_panic_msg - report panic message to Hyper-V
403  * @pa: physical address of the panic page containing the message
404  * @size: size of the message in the page
405  */
406 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
407 {
408 	/*
409 	 * P3 to contain the physical address of the panic page & P4 to
410 	 * contain the size of the panic data in that page. Rest of the
411 	 * registers are no-op when the NOTIFY_MSG flag is set.
412 	 */
413 	wrmsrl(HV_X64_MSR_CRASH_P0, 0);
414 	wrmsrl(HV_X64_MSR_CRASH_P1, 0);
415 	wrmsrl(HV_X64_MSR_CRASH_P2, 0);
416 	wrmsrl(HV_X64_MSR_CRASH_P3, pa);
417 	wrmsrl(HV_X64_MSR_CRASH_P4, size);
418 
419 	/*
420 	 * Let Hyper-V know there is crash data available along with
421 	 * the panic message.
422 	 */
423 	wrmsrl(HV_X64_MSR_CRASH_CTL,
424 	       (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
425 }
426 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
427 
428 bool hv_is_hyperv_initialized(void)
429 {
430 	union hv_x64_msr_hypercall_contents hypercall_msr;
431 
432 	/*
433 	 * Ensure that we're really on Hyper-V, and not a KVM or Xen
434 	 * emulation of Hyper-V
435 	 */
436 	if (x86_hyper_type != X86_HYPER_MS_HYPERV)
437 		return false;
438 
439 	/*
440 	 * Verify that earlier initialization succeeded by checking
441 	 * that the hypercall page is setup
442 	 */
443 	hypercall_msr.as_uint64 = 0;
444 	rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
445 
446 	return hypercall_msr.enable;
447 }
448 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
449 
450 bool hv_is_hibernation_supported(void)
451 {
452 	return acpi_sleep_state_supported(ACPI_STATE_S4);
453 }
454 EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
455