blob: d3f42b6bbdac96085dc6d84c1551395c1cfbcb67 [file] [log] [blame]
K. Y. Srinivasan87300462017-01-18 16:45:02 -07001/*
2 * X86 specific Hyper-V initialization code.
3 *
4 * Copyright (C) 2016, Microsoft, Inc.
5 *
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
7 *
8 * This program is free software; you can redistribute it and/or modify it
9 * under the terms of the GNU General Public License version 2 as published
10 * by the Free Software Foundation.
11 *
12 * This program is distributed in the hope that it will be useful, but
13 * WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE, GOOD TITLE or
15 * NON INFRINGEMENT. See the GNU General Public License for more
16 * details.
17 *
18 */
19
Dexuan Cui2f285f42018-09-18 22:29:50 +000020#include <linux/efi.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070021#include <linux/types.h>
Vitaly Kuznetsov93286262018-01-24 14:23:33 +010022#include <asm/apic.h>
23#include <asm/desc.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070024#include <asm/hypervisor.h>
Vitaly Kuznetsov5a485802018-03-20 15:02:05 +010025#include <asm/hyperv-tlfs.h>
K. Y. Srinivasan87300462017-01-18 16:45:02 -070026#include <asm/mshyperv.h>
27#include <linux/version.h>
28#include <linux/vmalloc.h>
29#include <linux/mm.h>
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070030#include <linux/clockchips.h>
Stephen Hemminger67071812017-03-04 18:27:11 -070031#include <linux/hyperv.h>
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020032#include <linux/slab.h>
33#include <linux/cpuhotplug.h>
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070034
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010035#ifdef CONFIG_HYPERV_TSCPAGE
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070036
37static struct ms_hyperv_tsc_page *tsc_pg;
38
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010039struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
40{
41 return tsc_pg;
42}
Vitaly Kuznetsove2768ea2018-01-24 14:23:32 +010043EXPORT_SYMBOL_GPL(hv_get_tsc_page);
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +010044
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070045static u64 read_hv_clock_tsc(struct clocksource *arg)
46{
Vitaly Kuznetsov07333792017-03-03 14:21:41 +010047 u64 current_tick = hv_read_tsc_page(tsc_pg);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070048
Vitaly Kuznetsov07333792017-03-03 14:21:41 +010049 if (current_tick == U64_MAX)
50 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070051
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -070052 return current_tick;
53}
54
55static struct clocksource hyperv_cs_tsc = {
56 .name = "hyperv_clocksource_tsc_page",
57 .rating = 400,
58 .read = read_hv_clock_tsc,
59 .mask = CLOCKSOURCE_MASK(64),
60 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
61};
62#endif
63
64static u64 read_hv_clock_msr(struct clocksource *arg)
65{
66 u64 current_tick;
67 /*
68 * Read the partition counter to get the current tick count. This count
69 * is set to 0 when the partition is created and is incremented in
70 * 100 nanosecond units.
71 */
72 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
73 return current_tick;
74}
75
76static struct clocksource hyperv_cs_msr = {
77 .name = "hyperv_clocksource_msr",
78 .rating = 400,
79 .read = read_hv_clock_msr,
80 .mask = CLOCKSOURCE_MASK(64),
81 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
82};
K. Y. Srinivasan87300462017-01-18 16:45:02 -070083
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +020084void *hv_hypercall_pg;
85EXPORT_SYMBOL_GPL(hv_hypercall_pg);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -070086struct clocksource *hyperv_cs;
87EXPORT_SYMBOL_GPL(hyperv_cs);
88
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +020089u32 *hv_vp_index;
90EXPORT_SYMBOL_GPL(hv_vp_index);
91
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +010092struct hv_vp_assist_page **hv_vp_assist_page;
93EXPORT_SYMBOL_GPL(hv_vp_assist_page);
94
K. Y. Srinivasan68bb7bf2018-05-16 14:53:31 -070095void __percpu **hyperv_pcpu_input_arg;
96EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg);
97
Vitaly Kuznetsova3b74242017-10-06 17:48:54 +020098u32 hv_max_vp_index;
99
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200100static int hv_cpu_init(unsigned int cpu)
101{
102 u64 msr_vp_index;
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100103 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()];
K. Y. Srinivasan68bb7bf2018-05-16 14:53:31 -0700104 void **input_arg;
105
106 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
107 *input_arg = page_address(alloc_page(GFP_KERNEL));
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200108
109 hv_get_vp_index(msr_vp_index);
110
111 hv_vp_index[smp_processor_id()] = msr_vp_index;
112
Vitaly Kuznetsova3b74242017-10-06 17:48:54 +0200113 if (msr_vp_index > hv_max_vp_index)
114 hv_max_vp_index = msr_vp_index;
115
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100116 if (!hv_vp_assist_page)
117 return 0;
118
119 if (!*hvp)
120 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
121
122 if (*hvp) {
123 u64 val;
124
125 val = vmalloc_to_pfn(*hvp);
126 val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) |
127 HV_X64_MSR_VP_ASSIST_PAGE_ENABLE;
128
129 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val);
130 }
131
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200132 return 0;
133}
134
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100135static void (*hv_reenlightenment_cb)(void);
136
137static void hv_reenlightenment_notify(struct work_struct *dummy)
138{
139 struct hv_tsc_emulation_status emu_status;
140
141 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
142
143 /* Don't issue the callback if TSC accesses are not emulated */
144 if (hv_reenlightenment_cb && emu_status.inprogress)
145 hv_reenlightenment_cb();
146}
147static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify);
148
149void hyperv_stop_tsc_emulation(void)
150{
151 u64 freq;
152 struct hv_tsc_emulation_status emu_status;
153
154 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
155 emu_status.inprogress = 0;
156 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status);
157
158 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
159 tsc_khz = div64_u64(freq, 1000);
160}
161EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation);
162
163static inline bool hv_reenlightenment_available(void)
164{
165 /*
166 * Check for required features and priviliges to make TSC frequency
167 * change notifications work.
168 */
169 return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS &&
170 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE &&
171 ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT;
172}
173
174__visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs)
175{
176 entering_ack_irq();
177
Vitaly Kuznetsov51d4e5d2018-01-24 14:23:35 +0100178 inc_irq_stat(irq_hv_reenlightenment_count);
179
Vitaly Kuznetsov93286262018-01-24 14:23:33 +0100180 schedule_delayed_work(&hv_reenlightenment_work, HZ/10);
181
182 exiting_irq();
183}
184
185void set_hv_tscchange_cb(void (*cb)(void))
186{
187 struct hv_reenlightenment_control re_ctrl = {
188 .vector = HYPERV_REENLIGHTENMENT_VECTOR,
189 .enabled = 1,
190 .target_vp = hv_vp_index[smp_processor_id()]
191 };
192 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1};
193
194 if (!hv_reenlightenment_available()) {
195 pr_warn("Hyper-V: reenlightenment support is unavailable\n");
196 return;
197 }
198
199 hv_reenlightenment_cb = cb;
200
201 /* Make sure callback is registered before we write to MSRs */
202 wmb();
203
204 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
205 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl));
206}
207EXPORT_SYMBOL_GPL(set_hv_tscchange_cb);
208
209void clear_hv_tscchange_cb(void)
210{
211 struct hv_reenlightenment_control re_ctrl;
212
213 if (!hv_reenlightenment_available())
214 return;
215
216 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
217 re_ctrl.enabled = 0;
218 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl);
219
220 hv_reenlightenment_cb = NULL;
221}
222EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb);
223
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100224static int hv_cpu_die(unsigned int cpu)
225{
226 struct hv_reenlightenment_control re_ctrl;
227 unsigned int new_cpu;
K. Y. Srinivasan68bb7bf2018-05-16 14:53:31 -0700228 unsigned long flags;
229 void **input_arg;
230 void *input_pg = NULL;
231
232 local_irq_save(flags);
233 input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg);
234 input_pg = *input_arg;
235 *input_arg = NULL;
236 local_irq_restore(flags);
237 free_page((unsigned long)input_pg);
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100238
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100239 if (hv_vp_assist_page && hv_vp_assist_page[cpu])
240 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0);
241
Vitaly Kuznetsove7c4e362018-01-24 14:23:34 +0100242 if (hv_reenlightenment_cb == NULL)
243 return 0;
244
245 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
246 if (re_ctrl.target_vp == hv_vp_index[cpu]) {
247 /* Reassign to some other online CPU */
248 new_cpu = cpumask_any_but(cpu_online_mask, cpu);
249
250 re_ctrl.target_vp = hv_vp_index[new_cpu];
251 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl));
252 }
253
254 return 0;
255}
256
Dexuan Cui2f285f42018-09-18 22:29:50 +0000257static int __init hv_pci_init(void)
258{
259 int gen2vm = efi_enabled(EFI_BOOT);
260
261 /*
262 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
263 * The purpose is to suppress the harmless warning:
264 * "PCI: Fatal: No config space access function found"
265 */
266 if (gen2vm)
267 return 0;
268
269 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
270 return 1;
271}
272
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700273/*
274 * This function is to be invoked early in the boot sequence after the
275 * hypervisor has been detected.
276 *
277 * 1. Setup the hypercall page.
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700278 * 2. Register Hyper-V specific clocksource.
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700279 * 3. Setup Hyper-V specific APIC entry points.
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700280 */
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700281void __init hyperv_init(void)
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700282{
Vitaly Kuznetsov89a8f6d2018-01-24 14:23:31 +0100283 u64 guest_id, required_msrs;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700284 union hv_x64_msr_hypercall_contents hypercall_msr;
K. Y. Srinivasan1268ed02018-07-03 16:01:55 -0700285 int cpuhp, i;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700286
Juergen Gross03b2a322017-11-09 14:27:36 +0100287 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700288 return;
289
Vitaly Kuznetsov89a8f6d2018-01-24 14:23:31 +0100290 /* Absolutely required MSRs */
291 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
292 HV_X64_MSR_VP_INDEX_AVAILABLE;
293
294 if ((ms_hyperv.features & required_msrs) != required_msrs)
295 return;
296
K. Y. Srinivasan68bb7bf2018-05-16 14:53:31 -0700297 /*
298 * Allocate the per-CPU state for the hypercall input arg.
299 * If this allocation fails, we will not be able to setup
300 * (per-CPU) hypercall input page and thus this failure is
301 * fatal on Hyper-V.
302 */
303 hyperv_pcpu_input_arg = alloc_percpu(void *);
304
305 BUG_ON(hyperv_pcpu_input_arg == NULL);
306
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200307 /* Allocate percpu VP index */
308 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
309 GFP_KERNEL);
310 if (!hv_vp_index)
311 return;
312
K. Y. Srinivasan1268ed02018-07-03 16:01:55 -0700313 for (i = 0; i < num_possible_cpus(); i++)
314 hv_vp_index[i] = VP_INVAL;
315
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100316 hv_vp_assist_page = kcalloc(num_possible_cpus(),
317 sizeof(*hv_vp_assist_page), GFP_KERNEL);
318 if (!hv_vp_assist_page) {
319 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED;
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200320 goto free_vp_index;
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100321 }
322
323 cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
324 hv_cpu_init, hv_cpu_die);
325 if (cpuhp < 0)
326 goto free_vp_assist_page;
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200327
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700328 /*
329 * Setup the hypercall page and enable hypercalls.
330 * 1. Register the guest ID
331 * 2. Enable the hypercall and register the hypercall page
332 */
333 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
334 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
335
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200336 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
337 if (hv_hypercall_pg == NULL) {
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700338 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100339 goto remove_cpuhp_state;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700340 }
341
342 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
343 hypercall_msr.enable = 1;
Vitaly Kuznetsovfc536622017-08-02 18:09:14 +0200344 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700345 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700346
K. Y. Srinivasan6b48cb52018-05-16 14:53:30 -0700347 hv_apic_init();
348
Dexuan Cui2f285f42018-09-18 22:29:50 +0000349 x86_init.pci.arch_init = hv_pci_init;
350
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700351 /*
352 * Register Hyper-V specific clocksource.
353 */
Vitaly Kuznetsovbd2a9ad2017-03-03 14:21:40 +0100354#ifdef CONFIG_HYPERV_TSCPAGE
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700355 if (ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE) {
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700356 union hv_x64_msr_hypercall_contents tsc_msr;
357
358 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700359 if (!tsc_pg)
360 goto register_msr_cs;
361
362 hyperv_cs = &hyperv_cs_tsc;
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700363
364 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
365
366 tsc_msr.enable = 1;
367 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
368
369 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
Vitaly Kuznetsov90b20432017-03-03 14:21:42 +0100370
371 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
372
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700373 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
374 return;
375 }
Arnd Bergmann73667e32017-02-14 22:17:17 +0100376register_msr_cs:
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700377#endif
378 /*
379 * For 32 bit guests just use the MSR based mechanism for reading
380 * the partition counter.
381 */
382
Vitaly Kuznetsovdee863b2017-02-04 09:57:13 -0700383 hyperv_cs = &hyperv_cs_msr;
Michael Kelley7dc9b6b2018-06-05 13:37:54 -0700384 if (ms_hyperv.features & HV_MSR_TIME_REF_COUNT_AVAILABLE)
K. Y. Srinivasan63ed4e02017-01-19 11:51:46 -0700385 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200386
387 return;
388
Vitaly Kuznetsova46d15c2018-03-20 15:02:08 +0100389remove_cpuhp_state:
390 cpuhp_remove_state(cpuhp);
391free_vp_assist_page:
392 kfree(hv_vp_assist_page);
393 hv_vp_assist_page = NULL;
Vitaly Kuznetsov7415aea2017-08-02 18:09:18 +0200394free_vp_index:
395 kfree(hv_vp_index);
396 hv_vp_index = NULL;
K. Y. Srinivasan87300462017-01-18 16:45:02 -0700397}
K. Y. Srinivasan6ab42a62017-01-18 16:45:03 -0700398
399/*
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700400 * This routine is called before kexec/kdump, it does the required cleanup.
401 */
402void hyperv_cleanup(void)
403{
404 union hv_x64_msr_hypercall_contents hypercall_msr;
405
406 /* Reset our OS id */
407 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
408
Kairui Song179fb362019-03-06 19:18:27 +0800409 /*
410 * Reset hypercall page reference before reset the page,
411 * let hypercall operations fail safely rather than
412 * panic the kernel for using invalid hypercall page
413 */
414 hv_hypercall_pg = NULL;
415
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700416 /* Reset the hypercall page */
417 hypercall_msr.as_uint64 = 0;
418 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
Vitaly Kuznetsov5647dbf2017-01-28 12:37:15 -0700419
420 /* Reset the TSC page */
421 hypercall_msr.as_uint64 = 0;
422 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
Vitaly Kuznetsovd6f36092017-01-28 12:37:14 -0700423}
424EXPORT_SYMBOL_GPL(hyperv_cleanup);
425
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700426void hyperv_report_panic(struct pt_regs *regs, long err)
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700427{
428 static bool panic_reported;
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700429 u64 guest_id;
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700430
431 /*
432 * We prefer to report panic on 'die' chain as we have proper
433 * registers to report, but if we miss it (e.g. on BUG()) we need
434 * to report it on 'panic'.
435 */
436 if (panic_reported)
437 return;
438 panic_reported = true;
439
K. Y. Srinivasan7ed43252017-10-29 11:33:41 -0700440 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
441
442 wrmsrl(HV_X64_MSR_CRASH_P0, err);
443 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id);
444 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip);
445 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax);
446 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp);
K. Y. Srinivasand058fa72017-01-19 11:51:48 -0700447
448 /*
449 * Let Hyper-V know there is crash data available
450 */
451 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
452}
453EXPORT_SYMBOL_GPL(hyperv_report_panic);
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700454
Sunil Muthuswamy81b18bc2018-07-08 02:56:51 +0000455/**
456 * hyperv_report_panic_msg - report panic message to Hyper-V
457 * @pa: physical address of the panic page containing the message
458 * @size: size of the message in the page
459 */
460void hyperv_report_panic_msg(phys_addr_t pa, size_t size)
461{
462 /*
463 * P3 to contain the physical address of the panic page & P4 to
464 * contain the size of the panic data in that page. Rest of the
465 * registers are no-op when the NOTIFY_MSG flag is set.
466 */
467 wrmsrl(HV_X64_MSR_CRASH_P0, 0);
468 wrmsrl(HV_X64_MSR_CRASH_P1, 0);
469 wrmsrl(HV_X64_MSR_CRASH_P2, 0);
470 wrmsrl(HV_X64_MSR_CRASH_P3, pa);
471 wrmsrl(HV_X64_MSR_CRASH_P4, size);
472
473 /*
474 * Let Hyper-V know there is crash data available along with
475 * the panic message.
476 */
477 wrmsrl(HV_X64_MSR_CRASH_CTL,
478 (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG));
479}
480EXPORT_SYMBOL_GPL(hyperv_report_panic_msg);
481
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700482bool hv_is_hyperv_initialized(void)
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700483{
484 union hv_x64_msr_hypercall_contents hypercall_msr;
485
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700486 /*
487 * Ensure that we're really on Hyper-V, and not a KVM or Xen
488 * emulation of Hyper-V
489 */
490 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
491 return false;
492
493 /*
494 * Verify that earlier initialization succeeded by checking
495 * that the hypercall page is setup
496 */
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700497 hypercall_msr.as_uint64 = 0;
498 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
499
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700500 return hypercall_msr.enable;
K. Y. Srinivasan73638cd2017-01-19 11:51:49 -0700501}
Michael Kelley4a5f3cd2017-12-22 11:19:02 -0700502EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);