xref: /linux/arch/x86/kernel/cpu/mshyperv.c (revision 808094fcbf4196be0feb17afbbdc182ec95c8cec)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3  * HyperV  Detection code.
4  *
5  * Copyright (C) 2010, Novell, Inc.
6  * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
7  */
8 
9 #include <linux/types.h>
10 #include <linux/time.h>
11 #include <linux/clocksource.h>
12 #include <linux/init.h>
13 #include <linux/export.h>
14 #include <linux/hardirq.h>
15 #include <linux/efi.h>
16 #include <linux/interrupt.h>
17 #include <linux/irq.h>
18 #include <linux/kexec.h>
19 #include <linux/i8253.h>
20 #include <linux/random.h>
21 #include <asm/processor.h>
22 #include <asm/hypervisor.h>
23 #include <asm/hyperv-tlfs.h>
24 #include <asm/mshyperv.h>
25 #include <asm/desc.h>
26 #include <asm/idtentry.h>
27 #include <asm/irq_regs.h>
28 #include <asm/i8259.h>
29 #include <asm/apic.h>
30 #include <asm/timer.h>
31 #include <asm/reboot.h>
32 #include <asm/nmi.h>
33 #include <clocksource/hyperv_timer.h>
34 #include <asm/numa.h>
35 
36 /* Is Linux running as the root partition? */
37 bool hv_root_partition;
38 EXPORT_SYMBOL_GPL(hv_root_partition);
39 
40 struct ms_hyperv_info ms_hyperv;
41 EXPORT_SYMBOL_GPL(ms_hyperv);
42 
43 #if IS_ENABLED(CONFIG_HYPERV)
44 static void (*vmbus_handler)(void);
45 static void (*hv_stimer0_handler)(void);
46 static void (*hv_kexec_handler)(void);
47 static void (*hv_crash_handler)(struct pt_regs *regs);
48 
49 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_callback)
50 {
51 	struct pt_regs *old_regs = set_irq_regs(regs);
52 
53 	inc_irq_stat(irq_hv_callback_count);
54 	if (vmbus_handler)
55 		vmbus_handler();
56 
57 	if (ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)
58 		ack_APIC_irq();
59 
60 	set_irq_regs(old_regs);
61 }
62 
63 int hv_setup_vmbus_irq(int irq, void (*handler)(void))
64 {
65 	/*
66 	 * The 'irq' argument is ignored on x86/x64 because a hard-coded
67 	 * interrupt vector is used for Hyper-V interrupts.
68 	 */
69 	vmbus_handler = handler;
70 	return 0;
71 }
72 
73 void hv_remove_vmbus_irq(void)
74 {
75 	/* We have no way to deallocate the interrupt gate */
76 	vmbus_handler = NULL;
77 }
78 EXPORT_SYMBOL_GPL(hv_setup_vmbus_irq);
79 EXPORT_SYMBOL_GPL(hv_remove_vmbus_irq);
80 
81 /*
82  * Routines to do per-architecture handling of stimer0
83  * interrupts when in Direct Mode
84  */
85 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_stimer0)
86 {
87 	struct pt_regs *old_regs = set_irq_regs(regs);
88 
89 	inc_irq_stat(hyperv_stimer0_count);
90 	if (hv_stimer0_handler)
91 		hv_stimer0_handler();
92 	add_interrupt_randomness(HYPERV_STIMER0_VECTOR, 0);
93 	ack_APIC_irq();
94 
95 	set_irq_regs(old_regs);
96 }
97 
98 int hv_setup_stimer0_irq(int *irq, int *vector, void (*handler)(void))
99 {
100 	*vector = HYPERV_STIMER0_VECTOR;
101 	*irq = -1;   /* Unused on x86/x64 */
102 	hv_stimer0_handler = handler;
103 	return 0;
104 }
105 EXPORT_SYMBOL_GPL(hv_setup_stimer0_irq);
106 
107 void hv_remove_stimer0_irq(int irq)
108 {
109 	/* We have no way to deallocate the interrupt gate */
110 	hv_stimer0_handler = NULL;
111 }
112 EXPORT_SYMBOL_GPL(hv_remove_stimer0_irq);
113 
114 void hv_setup_kexec_handler(void (*handler)(void))
115 {
116 	hv_kexec_handler = handler;
117 }
118 EXPORT_SYMBOL_GPL(hv_setup_kexec_handler);
119 
120 void hv_remove_kexec_handler(void)
121 {
122 	hv_kexec_handler = NULL;
123 }
124 EXPORT_SYMBOL_GPL(hv_remove_kexec_handler);
125 
126 void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs))
127 {
128 	hv_crash_handler = handler;
129 }
130 EXPORT_SYMBOL_GPL(hv_setup_crash_handler);
131 
132 void hv_remove_crash_handler(void)
133 {
134 	hv_crash_handler = NULL;
135 }
136 EXPORT_SYMBOL_GPL(hv_remove_crash_handler);
137 
138 #ifdef CONFIG_KEXEC_CORE
139 static void hv_machine_shutdown(void)
140 {
141 	if (kexec_in_progress && hv_kexec_handler)
142 		hv_kexec_handler();
143 
144 	/*
145 	 * Call hv_cpu_die() on all the CPUs, otherwise later the hypervisor
146 	 * corrupts the old VP Assist Pages and can crash the kexec kernel.
147 	 */
148 	if (kexec_in_progress && hyperv_init_cpuhp > 0)
149 		cpuhp_remove_state(hyperv_init_cpuhp);
150 
151 	/* The function calls stop_other_cpus(). */
152 	native_machine_shutdown();
153 
154 	/* Disable the hypercall page when there is only 1 active CPU. */
155 	if (kexec_in_progress)
156 		hyperv_cleanup();
157 }
158 
159 static void hv_machine_crash_shutdown(struct pt_regs *regs)
160 {
161 	if (hv_crash_handler)
162 		hv_crash_handler(regs);
163 
164 	/* The function calls crash_smp_send_stop(). */
165 	native_machine_crash_shutdown(regs);
166 
167 	/* Disable the hypercall page when there is only 1 active CPU. */
168 	hyperv_cleanup();
169 }
170 #endif /* CONFIG_KEXEC_CORE */
171 #endif /* CONFIG_HYPERV */
172 
173 static uint32_t  __init ms_hyperv_platform(void)
174 {
175 	u32 eax;
176 	u32 hyp_signature[3];
177 
178 	if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
179 		return 0;
180 
181 	cpuid(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS,
182 	      &eax, &hyp_signature[0], &hyp_signature[1], &hyp_signature[2]);
183 
184 	if (eax >= HYPERV_CPUID_MIN &&
185 	    eax <= HYPERV_CPUID_MAX &&
186 	    !memcmp("Microsoft Hv", hyp_signature, 12))
187 		return HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS;
188 
189 	return 0;
190 }
191 
192 static unsigned char hv_get_nmi_reason(void)
193 {
194 	return 0;
195 }
196 
197 #ifdef CONFIG_X86_LOCAL_APIC
198 /*
199  * Prior to WS2016 Debug-VM sends NMIs to all CPUs which makes
200  * it dificult to process CHANNELMSG_UNLOAD in case of crash. Handle
201  * unknown NMI on the first CPU which gets it.
202  */
203 static int hv_nmi_unknown(unsigned int val, struct pt_regs *regs)
204 {
205 	static atomic_t nmi_cpu = ATOMIC_INIT(-1);
206 
207 	if (!unknown_nmi_panic)
208 		return NMI_DONE;
209 
210 	if (atomic_cmpxchg(&nmi_cpu, -1, raw_smp_processor_id()) != -1)
211 		return NMI_HANDLED;
212 
213 	return NMI_DONE;
214 }
215 #endif
216 
217 static unsigned long hv_get_tsc_khz(void)
218 {
219 	unsigned long freq;
220 
221 	rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
222 
223 	return freq / 1000;
224 }
225 
226 #if defined(CONFIG_SMP) && IS_ENABLED(CONFIG_HYPERV)
227 static void __init hv_smp_prepare_boot_cpu(void)
228 {
229 	native_smp_prepare_boot_cpu();
230 #if defined(CONFIG_X86_64) && defined(CONFIG_PARAVIRT_SPINLOCKS)
231 	hv_init_spinlocks();
232 #endif
233 }
234 
235 static void __init hv_smp_prepare_cpus(unsigned int max_cpus)
236 {
237 #ifdef CONFIG_X86_64
238 	int i;
239 	int ret;
240 #endif
241 
242 	native_smp_prepare_cpus(max_cpus);
243 
244 #ifdef CONFIG_X86_64
245 	for_each_present_cpu(i) {
246 		if (i == 0)
247 			continue;
248 		ret = hv_call_add_logical_proc(numa_cpu_node(i), i, cpu_physical_id(i));
249 		BUG_ON(ret);
250 	}
251 
252 	for_each_present_cpu(i) {
253 		if (i == 0)
254 			continue;
255 		ret = hv_call_create_vp(numa_cpu_node(i), hv_current_partition_id, i, i);
256 		BUG_ON(ret);
257 	}
258 #endif
259 }
260 #endif
261 
262 static void __init ms_hyperv_init_platform(void)
263 {
264 	int hv_host_info_eax;
265 	int hv_host_info_ebx;
266 	int hv_host_info_ecx;
267 	int hv_host_info_edx;
268 
269 #ifdef CONFIG_PARAVIRT
270 	pv_info.name = "Hyper-V";
271 #endif
272 
273 	/*
274 	 * Extract the features and hints
275 	 */
276 	ms_hyperv.features = cpuid_eax(HYPERV_CPUID_FEATURES);
277 	ms_hyperv.features_b = cpuid_ebx(HYPERV_CPUID_FEATURES);
278 	ms_hyperv.misc_features = cpuid_edx(HYPERV_CPUID_FEATURES);
279 	ms_hyperv.hints    = cpuid_eax(HYPERV_CPUID_ENLIGHTMENT_INFO);
280 
281 	pr_info("Hyper-V: features 0x%x, hints 0x%x, misc 0x%x\n",
282 		ms_hyperv.features, ms_hyperv.hints, ms_hyperv.misc_features);
283 
284 	ms_hyperv.max_vp_index = cpuid_eax(HYPERV_CPUID_IMPLEMENT_LIMITS);
285 	ms_hyperv.max_lp_index = cpuid_ebx(HYPERV_CPUID_IMPLEMENT_LIMITS);
286 
287 	pr_debug("Hyper-V: max %u virtual processors, %u logical processors\n",
288 		 ms_hyperv.max_vp_index, ms_hyperv.max_lp_index);
289 
290 	/*
291 	 * Check CPU management privilege.
292 	 *
293 	 * To mirror what Windows does we should extract CPU management
294 	 * features and use the ReservedIdentityBit to detect if Linux is the
295 	 * root partition. But that requires negotiating CPU management
296 	 * interface (a process to be finalized).
297 	 *
298 	 * For now, use the privilege flag as the indicator for running as
299 	 * root.
300 	 */
301 	if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_CPU_MANAGEMENT) {
302 		hv_root_partition = true;
303 		pr_info("Hyper-V: running as root partition\n");
304 	}
305 
306 	/*
307 	 * Extract host information.
308 	 */
309 	if (cpuid_eax(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS) >=
310 	    HYPERV_CPUID_VERSION) {
311 		hv_host_info_eax = cpuid_eax(HYPERV_CPUID_VERSION);
312 		hv_host_info_ebx = cpuid_ebx(HYPERV_CPUID_VERSION);
313 		hv_host_info_ecx = cpuid_ecx(HYPERV_CPUID_VERSION);
314 		hv_host_info_edx = cpuid_edx(HYPERV_CPUID_VERSION);
315 
316 		pr_info("Hyper-V Host Build:%d-%d.%d-%d-%d.%d\n",
317 			hv_host_info_eax, hv_host_info_ebx >> 16,
318 			hv_host_info_ebx & 0xFFFF, hv_host_info_ecx,
319 			hv_host_info_edx >> 24, hv_host_info_edx & 0xFFFFFF);
320 	}
321 
322 	if (ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
323 	    ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE) {
324 		x86_platform.calibrate_tsc = hv_get_tsc_khz;
325 		x86_platform.calibrate_cpu = hv_get_tsc_khz;
326 	}
327 
328 	if (ms_hyperv.features_b & HV_ISOLATION) {
329 		ms_hyperv.isolation_config_a = cpuid_eax(HYPERV_CPUID_ISOLATION_CONFIG);
330 		ms_hyperv.isolation_config_b = cpuid_ebx(HYPERV_CPUID_ISOLATION_CONFIG);
331 
332 		pr_info("Hyper-V: Isolation Config: Group A 0x%x, Group B 0x%x\n",
333 			ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
334 	}
335 
336 	if (ms_hyperv.hints & HV_X64_ENLIGHTENED_VMCS_RECOMMENDED) {
337 		ms_hyperv.nested_features =
338 			cpuid_eax(HYPERV_CPUID_NESTED_FEATURES);
339 	}
340 
341 	/*
342 	 * Hyper-V expects to get crash register data or kmsg when
343 	 * crash enlightment is available and system crashes. Set
344 	 * crash_kexec_post_notifiers to be true to make sure that
345 	 * calling crash enlightment interface before running kdump
346 	 * kernel.
347 	 */
348 	if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE)
349 		crash_kexec_post_notifiers = true;
350 
351 #ifdef CONFIG_X86_LOCAL_APIC
352 	if (ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
353 	    ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE) {
354 		/*
355 		 * Get the APIC frequency.
356 		 */
357 		u64	hv_lapic_frequency;
358 
359 		rdmsrl(HV_X64_MSR_APIC_FREQUENCY, hv_lapic_frequency);
360 		hv_lapic_frequency = div_u64(hv_lapic_frequency, HZ);
361 		lapic_timer_period = hv_lapic_frequency;
362 		pr_info("Hyper-V: LAPIC Timer Frequency: %#x\n",
363 			lapic_timer_period);
364 	}
365 
366 	register_nmi_handler(NMI_UNKNOWN, hv_nmi_unknown, NMI_FLAG_FIRST,
367 			     "hv_nmi_unknown");
368 #endif
369 
370 #ifdef CONFIG_X86_IO_APIC
371 	no_timer_check = 1;
372 #endif
373 
374 #if IS_ENABLED(CONFIG_HYPERV) && defined(CONFIG_KEXEC_CORE)
375 	machine_ops.shutdown = hv_machine_shutdown;
376 	machine_ops.crash_shutdown = hv_machine_crash_shutdown;
377 #endif
378 	if (ms_hyperv.features & HV_ACCESS_TSC_INVARIANT) {
379 		wrmsrl(HV_X64_MSR_TSC_INVARIANT_CONTROL, 0x1);
380 		setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
381 	} else {
382 		mark_tsc_unstable("running on Hyper-V");
383 	}
384 
385 	/*
386 	 * Generation 2 instances don't support reading the NMI status from
387 	 * 0x61 port.
388 	 */
389 	if (efi_enabled(EFI_BOOT))
390 		x86_platform.get_nmi_reason = hv_get_nmi_reason;
391 
392 	/*
393 	 * Hyper-V VMs have a PIT emulation quirk such that zeroing the
394 	 * counter register during PIT shutdown restarts the PIT. So it
395 	 * continues to interrupt @18.2 HZ. Setting i8253_clear_counter
396 	 * to false tells pit_shutdown() not to zero the counter so that
397 	 * the PIT really is shutdown. Generation 2 VMs don't have a PIT,
398 	 * and setting this value has no effect.
399 	 */
400 	i8253_clear_counter_on_shutdown = false;
401 
402 #if IS_ENABLED(CONFIG_HYPERV)
403 	/*
404 	 * Setup the hook to get control post apic initialization.
405 	 */
406 	x86_platform.apic_post_init = hyperv_init;
407 	hyperv_setup_mmu_ops();
408 	/* Setup the IDT for hypervisor callback */
409 	alloc_intr_gate(HYPERVISOR_CALLBACK_VECTOR, asm_sysvec_hyperv_callback);
410 
411 	/* Setup the IDT for reenlightenment notifications */
412 	if (ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT) {
413 		alloc_intr_gate(HYPERV_REENLIGHTENMENT_VECTOR,
414 				asm_sysvec_hyperv_reenlightenment);
415 	}
416 
417 	/* Setup the IDT for stimer0 */
418 	if (ms_hyperv.misc_features & HV_STIMER_DIRECT_MODE_AVAILABLE) {
419 		alloc_intr_gate(HYPERV_STIMER0_VECTOR,
420 				asm_sysvec_hyperv_stimer0);
421 	}
422 
423 # ifdef CONFIG_SMP
424 	smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
425 	if (hv_root_partition)
426 		smp_ops.smp_prepare_cpus = hv_smp_prepare_cpus;
427 # endif
428 
429 	/*
430 	 * Hyper-V doesn't provide irq remapping for IO-APIC. To enable x2apic,
431 	 * set x2apic destination mode to physcial mode when x2apic is available
432 	 * and Hyper-V IOMMU driver makes sure cpus assigned with IO-APIC irqs
433 	 * have 8-bit APIC id.
434 	 */
435 # ifdef CONFIG_X86_X2APIC
436 	if (x2apic_supported())
437 		x2apic_phys = 1;
438 # endif
439 
440 	/* Register Hyper-V specific clocksource */
441 	hv_init_clocksource();
442 #endif
443 }
444 
445 static bool __init ms_hyperv_x2apic_available(void)
446 {
447 	return x2apic_supported();
448 }
449 
450 /*
451  * If ms_hyperv_msi_ext_dest_id() returns true, hyperv_prepare_irq_remapping()
452  * returns -ENODEV and the Hyper-V IOMMU driver is not used; instead, the
453  * generic support of the 15-bit APIC ID is used: see __irq_msi_compose_msg().
454  *
455  * Note: for a VM on Hyper-V, the I/O-APIC is the only device which
456  * (logically) generates MSIs directly to the system APIC irq domain.
457  * There is no HPET, and PCI MSI/MSI-X interrupts are remapped by the
458  * pci-hyperv host bridge.
459  */
460 static bool __init ms_hyperv_msi_ext_dest_id(void)
461 {
462 	u32 eax;
463 
464 	eax = cpuid_eax(HYPERV_CPUID_VIRT_STACK_INTERFACE);
465 	if (eax != HYPERV_VS_INTERFACE_EAX_SIGNATURE)
466 		return false;
467 
468 	eax = cpuid_eax(HYPERV_CPUID_VIRT_STACK_PROPERTIES);
469 	return eax & HYPERV_VS_PROPERTIES_EAX_EXTENDED_IOAPIC_RTE;
470 }
471 
472 const __initconst struct hypervisor_x86 x86_hyper_ms_hyperv = {
473 	.name			= "Microsoft Hyper-V",
474 	.detect			= ms_hyperv_platform,
475 	.type			= X86_HYPER_MS_HYPERV,
476 	.init.x2apic_available	= ms_hyperv_x2apic_available,
477 	.init.msi_ext_dest_id	= ms_hyperv_msi_ext_dest_id,
478 	.init.init_platform	= ms_hyperv_init_platform,
479 };
480