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 <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 <asm/idtentry.h>
19#include <linux/version.h>
20#include <linux/vmalloc.h>
21#include <linux/mm.h>
22#include <linux/hyperv.h>
23#include <linux/slab.h>
24#include <linux/kernel.h>
25#include <linux/cpuhotplug.h>
26#include <linux/syscore_ops.h>
27#include <clocksource/hyperv_timer.h>
28
29void *hv_hypercall_pg;
30EXPORT_SYMBOL_GPL(hv_hypercall_pg);
31
32
33static void *hv_hypercall_pg_saved;
34
35u32 *hv_vp_index;
36EXPORT_SYMBOL_GPL(hv_vp_index);
37
38struct hv_vp_assist_page **hv_vp_assist_page;
39EXPORT_SYMBOL_GPL(hv_vp_assist_page);
40
41void __percpu **hyperv_pcpu_input_arg;
42EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
43
44u32 hv_max_vp_index;
45EXPORT_SYMBOL_GPL(hv_max_vp_index);
46
47void *hv_alloc_hyperv_page(void)
48{
49 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
50
51 return (void *)__get_free_page(GFP_KERNEL);
52}
53EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page);
54
55void *hv_alloc_hyperv_zeroed_page(void)
56{
57 BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE);
58
59 return (void *)__get_free_page(GFP_KERNEL | __GFP_ZERO);
60}
61EXPORT_SYMBOL_GPL(hv_alloc_hyperv_zeroed_page);
62
63void hv_free_hyperv_page(unsigned long addr)
64{
65 free_page(addr);
66}
67EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
68
69static int hv_cpu_init(unsigned int cpu)
70{
71 u64 msr_vp_index;
72 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
73 void **input_arg;
74 struct page *pg;
75
76 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
77
78 pg = alloc_page(irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL);
79 if (unlikely(!pg))
80 return -ENOMEM;
81 *input_arg = page_address(pg);
82
83 hv_get_vp_index(msr_vp_index);
84
85 hv_vp_index[smp_processor_id()] = msr_vp_index;
86
87 if (msr_vp_index > hv_max_vp_index)
88 hv_max_vp_index = msr_vp_index;
89
90 if (!hv_vp_assist_page)
91 return 0;
92
93
94
95
96
97
98
99
100 if (!*hvp) {
101 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO);
102 }
103
104 if (*hvp) {
105 u64 val;
106
107 val = vmalloc_to_pfn(*hvp);
108 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
109 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
110
111 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
112 }
113
114 return 0;
115}
116
117static void (*hv_reenlightenment_cb)(void);
118
119static void hv_reenlightenment_notify(struct work_struct *dummy)
120{
121 struct hv_tsc_emulation_status emu_status;
122
123 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
124
125
126 if (hv_reenlightenment_cb && emu_status.inprogress)
127 hv_reenlightenment_cb();
128}
129static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
130
131void hyperv_stop_tsc_emulation(void)
132{
133 u64 freq;
134 struct hv_tsc_emulation_status emu_status;
135
136 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
137 emu_status.inprogress = 0;
138 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
139
140 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
141 tsc_khz = div64_u64(freq, 1000);
142}
143EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
144
145static inline bool hv_reenlightenment_available(void)
146{
147
148
149
150
151 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
152 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
153 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
154}
155
156DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment)
157{
158 ack_APIC_irq();
159 inc_irq_stat(irq_hv_reenlightenment_count);
160 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
161}
162
163void set_hv_tscchange_cb(void (*cb)(void))
164{
165 struct hv_reenlightenment_control re_ctrl = {
166 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
167 .enabled = 1,
168 .target_vp = hv_vp_index[smp_processor_id()]
169 };
170 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
171
172 if (!hv_reenlightenment_available()) {
173 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
174 return;
175 }
176
177 hv_reenlightenment_cb = cb;
178
179
180 wmb();
181
182 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
183 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
184}
185EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
186
187void clear_hv_tscchange_cb(void)
188{
189 struct hv_reenlightenment_control re_ctrl;
190
191 if (!hv_reenlightenment_available())
192 return;
193
194 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
195 re_ctrl.enabled = 0;
196 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
197
198 hv_reenlightenment_cb = NULL;
199}
200EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
201
202static int hv_cpu_die(unsigned int cpu)
203{
204 struct hv_reenlightenment_control re_ctrl;
205 unsigned int new_cpu;
206 unsigned long flags;
207 void **input_arg;
208 void *input_pg = NULL;
209
210 local_irq_save(flags);
211 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
212 input_pg = *input_arg;
213 *input_arg = NULL;
214 local_irq_restore(flags);
215 free_page((unsigned long)input_pg);
216
217 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
218 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
219
220 if (hv_reenlightenment_cb == NULL)
221 return 0;
222
223 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
224 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
225
226
227
228
229
230 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
231
232 if (new_cpu < nr_cpu_ids)
233 re_ctrl.target_vp = hv_vp_index[new_cpu];
234 else
235 re_ctrl.enabled = 0;
236
237 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
238 }
239
240 return 0;
241}
242
243static int __init hv_pci_init(void)
244{
245 int gen2vm = efi_enabled(EFI_BOOT);
246
247
248
249
250
251
252 if (gen2vm)
253 return 0;
254
255
256 return 1;
257}
258
259static int hv_suspend(void)
260{
261 union hv_x64_msr_hypercall_contents hypercall_msr;
262 int ret;
263
264
265
266
267
268
269
270
271 hv_hypercall_pg_saved = hv_hypercall_pg;
272 hv_hypercall_pg = NULL;
273
274
275 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
276 hypercall_msr.enable = 0;
277 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
278
279 ret = hv_cpu_die(0);
280 return ret;
281}
282
283static void hv_resume(void)
284{
285 union hv_x64_msr_hypercall_contents hypercall_msr;
286 int ret;
287
288 ret = hv_cpu_init(0);
289 WARN_ON(ret);
290
291
292 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
293 hypercall_msr.enable = 1;
294 hypercall_msr.guest_physical_address =
295 vmalloc_to_pfn(hv_hypercall_pg_saved);
296 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
297
298 hv_hypercall_pg = hv_hypercall_pg_saved;
299 hv_hypercall_pg_saved = NULL;
300
301
302
303
304
305 if (hv_reenlightenment_cb)
306 set_hv_tscchange_cb(hv_reenlightenment_cb);
307}
308
309
310static struct syscore_ops hv_syscore_ops = {
311 .suspend = hv_suspend,
312 .resume = hv_resume,
313};
314
315
316
317
318
319
320
321
322
323void __init hyperv_init(void)
324{
325 u64 guest_id, required_msrs;
326 union hv_x64_msr_hypercall_contents hypercall_msr;
327 int cpuhp, i;
328
329 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
330 return;
331
332
333 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
334 HV_X64_MSR_VP_INDEX_AVAILABLE;
335
336 if ((ms_hyperv.features & required_msrs) != required_msrs)
337 return;
338
339
340
341
342
343
344
345 hyperv_pcpu_input_arg = alloc_percpu(void *);
346
347 BUG_ON(hyperv_pcpu_input_arg == NULL);
348
349
350 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
351 GFP_KERNEL);
352 if (!hv_vp_index)
353 return;
354
355 for (i = 0; i < num_possible_cpus(); i++)
356 hv_vp_index[i] = VP_INVAL;
357
358 hv_vp_assist_page = kcalloc(num_possible_cpus(),
359 sizeof(*hv_vp_assist_page), GFP_KERNEL);
360 if (!hv_vp_assist_page) {
361 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
362 goto free_vp_index;
363 }
364
365 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
366 hv_cpu_init, hv_cpu_die);
367 if (cpuhp < 0)
368 goto free_vp_assist_page;
369
370
371
372
373
374
375 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
376 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
377
378 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START,
379 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX,
380 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE,
381 __builtin_return_address(0));
382 if (hv_hypercall_pg == NULL) {
383 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
384 goto remove_cpuhp_state;
385 }
386
387 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
388 hypercall_msr.enable = 1;
389 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
390 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
391
392
393
394
395
396 (void)hv_stimer_alloc();
397
398 hv_apic_init();
399
400 x86_init.pci.arch_init = hv_pci_init;
401
402 register_syscore_ops(&hv_syscore_ops);
403
404 return;
405
406remove_cpuhp_state:
407 cpuhp_remove_state(cpuhp);
408free_vp_assist_page:
409 kfree(hv_vp_assist_page);
410 hv_vp_assist_page = NULL;
411free_vp_index:
412 kfree(hv_vp_index);
413 hv_vp_index = NULL;
414}
415
416
417
418
419void hyperv_cleanup(void)
420{
421 union hv_x64_msr_hypercall_contents hypercall_msr;
422
423 unregister_syscore_ops(&hv_syscore_ops);
424
425
426 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
427
428
429
430
431
432
433 hv_hypercall_pg = NULL;
434
435
436 hypercall_msr.as_uint64 = 0;
437 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
438
439
440 hypercall_msr.as_uint64 = 0;
441 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
442}
443EXPORT_SYMBOL_GPL(hyperv_cleanup);
444
445void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
446{
447 static bool panic_reported;
448 u64 guest_id;
449
450 if (in_die && !panic_on_oops)
451 return;
452
453
454
455
456
457
458 if (panic_reported)
459 return;
460 panic_reported = true;
461
462 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
463
464 wrmsrl(HV_X64_MSR_CRASH_P0, err);
465 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
466 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
467 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
468 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
469
470
471
472
473 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
474}
475EXPORT_SYMBOL_GPL(hyperv_report_panic);
476
477
478
479
480
481
482void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
483{
484
485
486
487
488
489 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
490 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
491 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
492 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
493 wrmsrl(HV_X64_MSR_CRASH_P4, size);
494
495
496
497
498
499 wrmsrl(HV_X64_MSR_CRASH_CTL,
500 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
501}
502EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
503
504bool hv_is_hyperv_initialized(void)
505{
506 union hv_x64_msr_hypercall_contents hypercall_msr;
507
508
509
510
511
512 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
513 return false;
514
515
516
517
518
519 hypercall_msr.as_uint64 = 0;
520 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
521
522 return hypercall_msr.enable;
523}
524EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);
525
526bool hv_is_hibernation_supported(void)
527{
528 return acpi_sleep_state_supported(ACPI_STATE_S4);
529}
530EXPORT_SYMBOL_GPL(hv_is_hibernation_supported);
531