This source file includes following definitions.
- hv_alloc_hyperv_page
- hv_free_hyperv_page
- hv_cpu_init
- hv_reenlightenment_notify
- hyperv_stop_tsc_emulation
- hv_reenlightenment_available
- hyperv_reenlightenment_intr
- set_hv_tscchange_cb
- clear_hv_tscchange_cb
- hv_cpu_die
- hv_pci_init
- hyperv_init
- hyperv_cleanup
- hyperv_report_panic
- hyperv_report_panic_msg
- hv_is_hyperv_initialized
1
2
3
4
5
6
7
8
9
10 #include <linux/efi.h>
11 #include <linux/types.h>
12 #include <asm/apic.h>
13 #include <asm/desc.h>
14 #include <asm/hypervisor.h>
15 #include <asm/hyperv-tlfs.h>
16 #include <asm/mshyperv.h>
17 #include <linux/version.h>
18 #include <linux/vmalloc.h>
19 #include <linux/mm.h>
20 #include <linux/hyperv.h>
21 #include <linux/slab.h>
22 #include <linux/kernel.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_free_hyperv_page(unsigned long addr)
50 {
51 free_page(addr);
52 }
53 EXPORT_SYMBOL_GPL(hv_free_hyperv_page);
54
55 static int hv_cpu_init(unsigned int cpu)
56 {
57 u64 msr_vp_index;
58 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
59 void **input_arg;
60 struct page *pg;
61
62 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
63 pg = alloc_page(GFP_KERNEL);
64 if (unlikely(!pg))
65 return -ENOMEM;
66 *input_arg = page_address(pg);
67
68 hv_get_vp_index(msr_vp_index);
69
70 hv_vp_index[smp_processor_id()] = msr_vp_index;
71
72 if (msr_vp_index > hv_max_vp_index)
73 hv_max_vp_index = msr_vp_index;
74
75 if (!hv_vp_assist_page)
76 return 0;
77
78
79
80
81
82
83
84
85 if (!*hvp) {
86 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO,
87 PAGE_KERNEL);
88 }
89
90 if (*hvp) {
91 u64 val;
92
93 val = vmalloc_to_pfn(*hvp);
94 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
95 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
96
97 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
98 }
99
100 return 0;
101 }
102
103 static void (*hv_reenlightenment_cb)(void);
104
105 static void hv_reenlightenment_notify(struct work_struct *dummy)
106 {
107 struct hv_tsc_emulation_status emu_status;
108
109 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
110
111
112 if (hv_reenlightenment_cb && emu_status.inprogress)
113 hv_reenlightenment_cb();
114 }
115 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
116
117 void hyperv_stop_tsc_emulation(void)
118 {
119 u64 freq;
120 struct hv_tsc_emulation_status emu_status;
121
122 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
123 emu_status.inprogress = 0;
124 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
125
126 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
127 tsc_khz = div64_u64(freq, 1000);
128 }
129 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
130
131 static inline bool hv_reenlightenment_available(void)
132 {
133
134
135
136
137 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
138 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
139 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
140 }
141
142 __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
143 {
144 entering_ack_irq();
145
146 inc_irq_stat(irq_hv_reenlightenment_count);
147
148 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
149
150 exiting_irq();
151 }
152
153 void set_hv_tscchange_cb(void (*cb)(void))
154 {
155 struct hv_reenlightenment_control re_ctrl = {
156 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
157 .enabled = 1,
158 .target_vp = hv_vp_index[smp_processor_id()]
159 };
160 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
161
162 if (!hv_reenlightenment_available()) {
163 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
164 return;
165 }
166
167 hv_reenlightenment_cb = cb;
168
169
170 wmb();
171
172 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
173 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
174 }
175 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
176
177 void clear_hv_tscchange_cb(void)
178 {
179 struct hv_reenlightenment_control re_ctrl;
180
181 if (!hv_reenlightenment_available())
182 return;
183
184 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
185 re_ctrl.enabled = 0;
186 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
187
188 hv_reenlightenment_cb = NULL;
189 }
190 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
191
192 static int hv_cpu_die(unsigned int cpu)
193 {
194 struct hv_reenlightenment_control re_ctrl;
195 unsigned int new_cpu;
196 unsigned long flags;
197 void **input_arg;
198 void *input_pg = NULL;
199
200 local_irq_save(flags);
201 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
202 input_pg = *input_arg;
203 *input_arg = NULL;
204 local_irq_restore(flags);
205 free_page((unsigned long)input_pg);
206
207 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
208 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
209
210 if (hv_reenlightenment_cb == NULL)
211 return 0;
212
213 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
214 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
215
216 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
217
218 re_ctrl.target_vp = hv_vp_index[new_cpu];
219 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
220 }
221
222 return 0;
223 }
224
225 static int __init hv_pci_init(void)
226 {
227 int gen2vm = efi_enabled(EFI_BOOT);
228
229
230
231
232
233
234 if (gen2vm)
235 return 0;
236
237
238 return 1;
239 }
240
241
242
243
244
245
246
247
248
249 void __init hyperv_init(void)
250 {
251 u64 guest_id, required_msrs;
252 union hv_x64_msr_hypercall_contents hypercall_msr;
253 int cpuhp, i;
254
255 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
256 return;
257
258
259 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
260 HV_X64_MSR_VP_INDEX_AVAILABLE;
261
262 if ((ms_hyperv.features & required_msrs) != required_msrs)
263 return;
264
265
266
267
268
269
270
271 hyperv_pcpu_input_arg = alloc_percpu(void *);
272
273 BUG_ON(hyperv_pcpu_input_arg == NULL);
274
275
276 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
277 GFP_KERNEL);
278 if (!hv_vp_index)
279 return;
280
281 for (i = 0; i < num_possible_cpus(); i++)
282 hv_vp_index[i] = VP_INVAL;
283
284 hv_vp_assist_page = kcalloc(num_possible_cpus(),
285 sizeof(*hv_vp_assist_page), GFP_KERNEL);
286 if (!hv_vp_assist_page) {
287 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
288 goto free_vp_index;
289 }
290
291 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
292 hv_cpu_init, hv_cpu_die);
293 if (cpuhp < 0)
294 goto free_vp_assist_page;
295
296
297
298
299
300
301 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
302 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
303
304 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
305 if (hv_hypercall_pg == NULL) {
306 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
307 goto remove_cpuhp_state;
308 }
309
310 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
311 hypercall_msr.enable = 1;
312 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
313 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
314
315 hv_apic_init();
316
317 x86_init.pci.arch_init = hv_pci_init;
318
319 return;
320
321 remove_cpuhp_state:
322 cpuhp_remove_state(cpuhp);
323 free_vp_assist_page:
324 kfree(hv_vp_assist_page);
325 hv_vp_assist_page = NULL;
326 free_vp_index:
327 kfree(hv_vp_index);
328 hv_vp_index = NULL;
329 }
330
331
332
333
334 void hyperv_cleanup(void)
335 {
336 union hv_x64_msr_hypercall_contents hypercall_msr;
337
338
339 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
340
341
342
343
344
345
346 hv_hypercall_pg = NULL;
347
348
349 hypercall_msr.as_uint64 = 0;
350 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
351
352
353 hypercall_msr.as_uint64 = 0;
354 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
355 }
356 EXPORT_SYMBOL_GPL(hyperv_cleanup);
357
358 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
359 {
360 static bool panic_reported;
361 u64 guest_id;
362
363 if (in_die && !panic_on_oops)
364 return;
365
366
367
368
369
370
371 if (panic_reported)
372 return;
373 panic_reported = true;
374
375 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
376
377 wrmsrl(HV_X64_MSR_CRASH_P0, err);
378 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
379 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
380 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
381 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
382
383
384
385
386 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
387 }
388 EXPORT_SYMBOL_GPL(hyperv_report_panic);
389
390
391
392
393
394
395 void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
396 {
397
398
399
400
401
402 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
403 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
404 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
405 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
406 wrmsrl(HV_X64_MSR_CRASH_P4, size);
407
408
409
410
411
412 wrmsrl(HV_X64_MSR_CRASH_CTL,
413 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
414 }
415 EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
416
417 bool hv_is_hyperv_initialized(void)
418 {
419 union hv_x64_msr_hypercall_contents hypercall_msr;
420
421
422
423
424
425 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
426 return false;
427
428
429
430
431
432 hypercall_msr.as_uint64 = 0;
433 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
434
435 return hypercall_msr.enable;
436 }
437 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);