1
2
3
4
5
6
7
8
9
10#include <linux/acpi.h>
11#include <linux/efi.h>
12#include <linux/types.h>
13#include <linux/bitfield.h>
14#include <asm/apic.h>
15#include <asm/desc.h>
16#include <asm/hypervisor.h>
17#include <asm/hyperv-tlfs.h>
18#include <asm/mshyperv.h>
19#include <asm/idtentry.h>
20#include <linux/kexec.h>
21#include <linux/version.h>
22#include <linux/vmalloc.h>
23#include <linux/mm.h>
24#include <linux/hyperv.h>
25#include <linux/slab.h>
26#include <linux/kernel.h>
27#include <linux/cpuhotplug.h>
28#include <linux/syscore_ops.h>
29#include <clocksource/hyperv_timer.h>
30#include <linux/highmem.h>
31
32int hyperv_init_cpuhp;
33u64 hv_current_partition_id = ~0ull;
34EXPORT_SYMBOL_GPL(hv_current_partition_id);
35
36void *hv_hypercall_pg;
37EXPORT_SYMBOL_GPL(hv_hypercall_pg);
38
39
40static void *hv_hypercall_pg_saved;
41
42u32 *hv_vp_index;
43EXPORT_SYMBOL_GPL(hv_vp_index);
44
45struct hv_vp_assist_page **hv_vp_assist_page;
46EXPORT_SYMBOL_GPL(hv_vp_assist_page);
47
48void __percpu **hyperv_pcpu_input_arg;
49EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
50
51void __percpu **hyperv_pcpu_output_arg;
52EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg);
53
54u32 hv_max_vp_index;
55EXPORT_SYMBOL_GPL(hv_max_vp_index);
56
57static int hv_cpu_init(unsigned int cpu)
58{
59 u64 msr_vp_index;
60 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
61 void **input_arg;
62 struct page *pg;
63
64
65 pg = alloc_pages(irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL, hv_root_partition ? 1 : 0);
66 if (unlikely(!pg))
67 return -ENOMEM;
68
69 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
70 *input_arg = page_address(pg);
71 if (hv_root_partition) {
72 void **output_arg;
73
74 output_arg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
75 *output_arg = page_address(pg + 1);
76 }
77
78 msr_vp_index = hv_get_register(HV_REGISTER_VP_INDEX);
79
80 hv_vp_index[smp_processor_id()] = msr_vp_index;
81
82 if (msr_vp_index > hv_max_vp_index)
83 hv_max_vp_index = msr_vp_index;
84
85 if (!hv_vp_assist_page)
86 return 0;
87
88
89
90
91
92
93
94
95 if (!*hvp) {
96 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
97 }
98
99 if (*hvp) {
100 u64 val;
101
102 val = vmalloc_to_pfn(*hvp);
103 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
104 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
105
106 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
107 }
108
109 return 0;
110}
111
112static void (*hv_reenlightenment_cb)(void);
113
114static void hv_reenlightenment_notify(struct work_struct *dummy)
115{
116 struct hv_tsc_emulation_status emu_status;
117
118 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
119
120
121 if (hv_reenlightenment_cb && emu_status.inprogress)
122 hv_reenlightenment_cb();
123}
124static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
125
126void hyperv_stop_tsc_emulation(void)
127{
128 u64 freq;
129 struct hv_tsc_emulation_status emu_status;
130
131 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
132 emu_status.inprogress = 0;
133 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
134
135 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
136 tsc_khz = div64_u64(freq, 1000);
137}
138EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
139
140static inline bool hv_reenlightenment_available(void)
141{
142
143
144
145
146 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
147 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
148 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT;
149}
150
151DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
152{
153 ack_APIC_irq();
154 inc_irq_stat(irq_hv_reenlightenment_count);
155 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
156}
157
158void set_hv_tscchange_cb(void (*cb)(void))
159{
160 struct hv_reenlightenment_control re_ctrl = {
161 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
162 .enabled = 1,
163 .target_vp = hv_vp_index[smp_processor_id()]
164 };
165 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
166
167 if (!hv_reenlightenment_available()) {
168 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
169 return;
170 }
171
172 hv_reenlightenment_cb = cb;
173
174
175 wmb();
176
177 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
178 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
179}
180EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
181
182void clear_hv_tscchange_cb(void)
183{
184 struct hv_reenlightenment_control re_ctrl;
185
186 if (!hv_reenlightenment_available())
187 return;
188
189 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
190 re_ctrl.enabled = 0;
191 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
192
193 hv_reenlightenment_cb = NULL;
194}
195EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
196
197static int hv_cpu_die(unsigned int cpu)
198{
199 struct hv_reenlightenment_control re_ctrl;
200 unsigned int new_cpu;
201 unsigned long flags;
202 void **input_arg;
203 void *pg;
204
205 local_irq_save(flags);
206 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
207 pg = *input_arg;
208 *input_arg = NULL;
209
210 if (hv_root_partition) {
211 void **output_arg;
212
213 output_arg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg);
214 *output_arg = NULL;
215 }
216
217 local_irq_restore(flags);
218
219 free_pages((unsigned long)pg, hv_root_partition ? 1 : 0);
220
221 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
222 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
223
224 if (hv_reenlightenment_cb == NULL)
225 return 0;
226
227 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
228 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
229
230
231
232
233
234 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
235
236 if (new_cpu < nr_cpu_ids)
237 re_ctrl.target_vp = hv_vp_index[new_cpu];
238 else
239 re_ctrl.enabled = 0;
240
241 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
242 }
243
244 return 0;
245}
246
247static int __init hv_pci_init(void)
248{
249 int gen2vm = efi_enabled(EFI_BOOT);
250
251
252
253
254
255
256 if (gen2vm)
257 return 0;
258
259
260 return 1;
261}
262
263static int hv_suspend(void)
264{
265 union hv_x64_msr_hypercall_contents hypercall_msr;
266 int ret;
267
268 if (hv_root_partition)
269 return -EPERM;
270
271
272
273
274
275
276
277
278 hv_hypercall_pg_saved = hv_hypercall_pg;
279 hv_hypercall_pg = NULL;
280
281
282 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
283 hypercall_msr.enable = 0;
284 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
285
286 ret = hv_cpu_die(0);
287 return ret;
288}
289
290static void hv_resume(void)
291{
292 union hv_x64_msr_hypercall_contents hypercall_msr;
293 int ret;
294
295 ret = hv_cpu_init(0);
296 WARN_ON(ret);
297
298
299 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
300 hypercall_msr.enable = 1;
301 hypercall_msr.guest_physical_address =
302 vmalloc_to_pfn(hv_hypercall_pg_saved);
303 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
304
305 hv_hypercall_pg = hv_hypercall_pg_saved;
306 hv_hypercall_pg_saved = NULL;
307
308
309
310
311
312 if (hv_reenlightenment_cb)
313 set_hv_tscchange_cb(hv_reenlightenment_cb);
314}
315
316
317static struct syscore_ops hv_syscore_ops = {
318 .suspend = hv_suspend,
319 .resume = hv_resume,
320};
321
322static void (* __initdata old_setup_percpu_clockev)(void);
323
324static void __init hv_stimer_setup_percpu_clockev(void)
325{
326
327
328
329
330 (void)hv_stimer_alloc(false);
331
332
333
334
335
336
337 if (old_setup_percpu_clockev)
338 old_setup_percpu_clockev();
339}
340
341static void __init hv_get_partition_id(void)
342{
343 struct hv_get_partition_id *output_page;
344 u64 status;
345 unsigned long flags;
346
347 local_irq_save(flags);
348 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg);
349 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page);
350 if (!hv_result_success(status)) {
351
352 pr_err("Failed to get partition ID: %lld\n", status);
353 BUG();
354 }
355 hv_current_partition_id = output_page->partition_id;
356 local_irq_restore(flags);
357}
358
359
360
361
362
363
364
365
366
367void __init hyperv_init(void)
368{
369 u64 guest_id, required_msrs;
370 union hv_x64_msr_hypercall_contents hypercall_msr;
371 int cpuhp, i;
372
373 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
374 return;
375
376
377 required_msrs = HV_MSR_HYPERCALL_AVAILABLE |
378 HV_MSR_VP_INDEX_AVAILABLE;
379
380 if ((ms_hyperv.features & required_msrs) != required_msrs)
381 return;
382
383
384
385
386
387
388
389 hyperv_pcpu_input_arg = alloc_percpu(void *);
390
391 BUG_ON(hyperv_pcpu_input_arg == NULL);
392
393
394 if (hv_root_partition) {
395 hyperv_pcpu_output_arg = alloc_percpu(void *);
396 BUG_ON(hyperv_pcpu_output_arg == NULL);
397 }
398
399
400 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
401 GFP_KERNEL);
402 if (!hv_vp_index)
403 return;
404
405 for (i = 0; i < num_possible_cpus(); i++)
406 hv_vp_index[i] = VP_INVAL;
407
408 hv_vp_assist_page = kcalloc(num_possible_cpus(),
409 sizeof(*hv_vp_assist_page), GFP_KERNEL);
410 if (!hv_vp_assist_page) {
411 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
412 goto free_vp_index;
413 }
414
415 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
416 hv_cpu_init, hv_cpu_die);
417 if (cpuhp < 0)
418 goto free_vp_assist_page;
419
420
421
422
423
424
425 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
426 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
427
428 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
429 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
430 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
431 __builtin_return_address(0));
432 if (hv_hypercall_pg == NULL) {
433 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
434 goto remove_cpuhp_state;
435 }
436
437 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
438 hypercall_msr.enable = 1;
439
440 if (hv_root_partition) {
441 struct page *pg;
442 void *src, *dst;
443
444
445
446
447
448
449
450
451
452
453
454 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
455
456 pg = vmalloc_to_page(hv_hypercall_pg);
457 dst = kmap(pg);
458 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE,
459 MEMREMAP_WB);
460 BUG_ON(!(src && dst));
461 memcpy(dst, src, HV_HYP_PAGE_SIZE);
462 memunmap(src);
463 kunmap(pg);
464 } else {
465 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
466 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
467 }
468
469
470
471
472
473
474
475
476 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev;
477 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev;
478
479 hv_apic_init();
480
481 x86_init.pci.arch_init = hv_pci_init;
482
483 register_syscore_ops(&hv_syscore_ops);
484
485 hyperv_init_cpuhp = cpuhp;
486
487 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID)
488 hv_get_partition_id();
489
490 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull);
491
492#ifdef CONFIG_PCI_MSI
493
494
495
496
497 if (hv_root_partition)
498 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain;
499#endif
500
501
502 hv_query_ext_cap(0);
503 return;
504
505remove_cpuhp_state:
506 cpuhp_remove_state(cpuhp);
507free_vp_assist_page:
508 kfree(hv_vp_assist_page);
509 hv_vp_assist_page = NULL;
510free_vp_index:
511 kfree(hv_vp_index);
512 hv_vp_index = NULL;
513}
514
515
516
517
518void hyperv_cleanup(void)
519{
520 union hv_x64_msr_hypercall_contents hypercall_msr;
521
522 unregister_syscore_ops(&hv_syscore_ops);
523
524
525 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
526
527
528
529
530
531
532 hv_hypercall_pg = NULL;
533
534
535 hypercall_msr.as_uint64 = 0;
536 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
537
538
539 hypercall_msr.as_uint64 = 0;
540 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
541}
542EXPORT_SYMBOL_GPL(hyperv_cleanup);
543
544void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
545{
546 static bool panic_reported;
547 u64 guest_id;
548
549 if (in_die && !panic_on_oops)
550 return;
551
552
553
554
555
556
557 if (panic_reported)
558 return;
559 panic_reported = true;
560
561 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
562
563 wrmsrl(HV_X64_MSR_CRASH_P0, err);
564 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
565 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
566 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
567 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
568
569
570
571
572 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
573}
574EXPORT_SYMBOL_GPL(hyperv_report_panic);
575
576bool hv_is_hyperv_initialized(void)
577{
578 union hv_x64_msr_hypercall_contents hypercall_msr;
579
580
581
582
583
584 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
585 return false;
586
587
588
589
590
591 hypercall_msr.as_uint64 = 0;
592 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
593
594 return hypercall_msr.enable;
595}
596EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
597
598bool hv_is_hibernation_supported(void)
599{
600 return !hv_root_partition && acpi_sleep_state_supported(ACPI_STATE_S4);
601}
602EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
603
604enum hv_isolation_type hv_get_isolation_type(void)
605{
606 if (!(ms_hyperv.priv_high & HV_ISOLATION))
607 return HV_ISOLATION_TYPE_NONE;
608 return FIELD_GET(HV_ISOLATION_TYPE, ms_hyperv.isolation_config_b);
609}
610EXPORT_SYMBOL_GPL(hv_get_isolation_type);
611
612bool hv_is_isolation_supported(void)
613{
614 return hv_get_isolation_type() != HV_ISOLATION_TYPE_NONE;
615}
616EXPORT_SYMBOL_GPL(hv_is_isolation_supported);
617