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