mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / arch / x86 / hyperv / hv_init.c
blob924fa9c07368112f3ec859597c474b4d3386ce97
1 /*
2 * X86 specific Hyper-V initialization code.
4 * Copyright (C) 2016, Microsoft, Inc.
6 * Author : K. Y. Srinivasan <kys@microsoft.com>
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.
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.
20 #include <linux/efi.h>
21 #include <linux/types.h>
22 #include <asm/hypervisor.h>
23 #include <asm/hyperv.h>
24 #include <asm/mshyperv.h>
25 #include <linux/version.h>
26 #include <linux/vmalloc.h>
27 #include <linux/mm.h>
28 #include <linux/clockchips.h>
29 #include <linux/hyperv.h>
30 #include <linux/slab.h>
31 #include <linux/cpuhotplug.h>
33 #ifdef CONFIG_HYPERV_TSCPAGE
35 static struct ms_hyperv_tsc_page *tsc_pg;
37 struct ms_hyperv_tsc_page *hv_get_tsc_page(void)
39 return tsc_pg;
42 static u64 read_hv_clock_tsc(struct clocksource *arg)
44 u64 current_tick = hv_read_tsc_page(tsc_pg);
46 if (current_tick == U64_MAX)
47 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
49 return current_tick;
52 static struct clocksource hyperv_cs_tsc = {
53 .name = "hyperv_clocksource_tsc_page",
54 .rating = 400,
55 .read = read_hv_clock_tsc,
56 .mask = CLOCKSOURCE_MASK(64),
57 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
59 #endif
61 static u64 read_hv_clock_msr(struct clocksource *arg)
63 u64 current_tick;
65 * Read the partition counter to get the current tick count. This count
66 * is set to 0 when the partition is created and is incremented in
67 * 100 nanosecond units.
69 rdmsrl(HV_X64_MSR_TIME_REF_COUNT, current_tick);
70 return current_tick;
73 static struct clocksource hyperv_cs_msr = {
74 .name = "hyperv_clocksource_msr",
75 .rating = 400,
76 .read = read_hv_clock_msr,
77 .mask = CLOCKSOURCE_MASK(64),
78 .flags = CLOCK_SOURCE_IS_CONTINUOUS,
81 void *hv_hypercall_pg;
82 EXPORT_SYMBOL_GPL(hv_hypercall_pg);
83 struct clocksource *hyperv_cs;
84 EXPORT_SYMBOL_GPL(hyperv_cs);
86 u32 *hv_vp_index;
87 EXPORT_SYMBOL_GPL(hv_vp_index);
89 u32 hv_max_vp_index;
91 static int hv_cpu_init(unsigned int cpu)
93 u64 msr_vp_index;
95 hv_get_vp_index(msr_vp_index);
97 hv_vp_index[smp_processor_id()] = msr_vp_index;
99 if (msr_vp_index > hv_max_vp_index)
100 hv_max_vp_index = msr_vp_index;
102 return 0;
105 static int __init hv_pci_init(void)
107 int gen2vm = efi_enabled(EFI_BOOT);
110 * For Generation-2 VM, we exit from pci_arch_init() by returning 0.
111 * The purpose is to suppress the harmless warning:
112 * "PCI: Fatal: No config space access function found"
114 if (gen2vm)
115 return 0;
117 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */
118 return 1;
122 * This function is to be invoked early in the boot sequence after the
123 * hypervisor has been detected.
125 * 1. Setup the hypercall page.
126 * 2. Register Hyper-V specific clocksource.
128 void __init hyperv_init(void)
130 u64 guest_id, required_msrs;
131 union hv_x64_msr_hypercall_contents hypercall_msr;
133 if (x86_hyper_type != X86_HYPER_MS_HYPERV)
134 return;
136 /* Absolutely required MSRs */
137 required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE |
138 HV_X64_MSR_VP_INDEX_AVAILABLE;
140 if ((ms_hyperv.features & required_msrs) != required_msrs)
141 return;
143 /* Allocate percpu VP index */
144 hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index),
145 GFP_KERNEL);
146 if (!hv_vp_index)
147 return;
149 if (cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online",
150 hv_cpu_init, NULL) < 0)
151 goto free_vp_index;
154 * Setup the hypercall page and enable hypercalls.
155 * 1. Register the guest ID
156 * 2. Enable the hypercall and register the hypercall page
158 guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0);
159 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id);
161 hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX);
162 if (hv_hypercall_pg == NULL) {
163 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
164 goto free_vp_index;
167 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
168 hypercall_msr.enable = 1;
169 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg);
170 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
172 hyper_alloc_mmu();
174 x86_init.pci.arch_init = hv_pci_init;
177 * Register Hyper-V specific clocksource.
179 #ifdef CONFIG_HYPERV_TSCPAGE
180 if (ms_hyperv.features & HV_X64_MSR_REFERENCE_TSC_AVAILABLE) {
181 union hv_x64_msr_hypercall_contents tsc_msr;
183 tsc_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL);
184 if (!tsc_pg)
185 goto register_msr_cs;
187 hyperv_cs = &hyperv_cs_tsc;
189 rdmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
191 tsc_msr.enable = 1;
192 tsc_msr.guest_physical_address = vmalloc_to_pfn(tsc_pg);
194 wrmsrl(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64);
196 hyperv_cs_tsc.archdata.vclock_mode = VCLOCK_HVCLOCK;
198 clocksource_register_hz(&hyperv_cs_tsc, NSEC_PER_SEC/100);
199 return;
201 register_msr_cs:
202 #endif
204 * For 32 bit guests just use the MSR based mechanism for reading
205 * the partition counter.
208 hyperv_cs = &hyperv_cs_msr;
209 if (ms_hyperv.features & HV_X64_MSR_TIME_REF_COUNT_AVAILABLE)
210 clocksource_register_hz(&hyperv_cs_msr, NSEC_PER_SEC/100);
212 return;
214 free_vp_index:
215 kfree(hv_vp_index);
216 hv_vp_index = NULL;
220 * This routine is called before kexec/kdump, it does the required cleanup.
222 void hyperv_cleanup(void)
224 union hv_x64_msr_hypercall_contents hypercall_msr;
226 /* Reset our OS id */
227 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0);
229 /* Reset the hypercall page */
230 hypercall_msr.as_uint64 = 0;
231 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
233 /* Reset the TSC page */
234 hypercall_msr.as_uint64 = 0;
235 wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64);
237 EXPORT_SYMBOL_GPL(hyperv_cleanup);
239 void hyperv_report_panic(struct pt_regs *regs)
241 static bool panic_reported;
244 * We prefer to report panic on 'die' chain as we have proper
245 * registers to report, but if we miss it (e.g. on BUG()) we need
246 * to report it on 'panic'.
248 if (panic_reported)
249 return;
250 panic_reported = true;
252 wrmsrl(HV_X64_MSR_CRASH_P0, regs->ip);
253 wrmsrl(HV_X64_MSR_CRASH_P1, regs->ax);
254 wrmsrl(HV_X64_MSR_CRASH_P2, regs->bx);
255 wrmsrl(HV_X64_MSR_CRASH_P3, regs->cx);
256 wrmsrl(HV_X64_MSR_CRASH_P4, regs->dx);
259 * Let Hyper-V know there is crash data available
261 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
263 EXPORT_SYMBOL_GPL(hyperv_report_panic);
265 bool hv_is_hypercall_page_setup(void)
267 union hv_x64_msr_hypercall_contents hypercall_msr;
269 /* Check if the hypercall page is setup */
270 hypercall_msr.as_uint64 = 0;
271 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64);
273 if (!hypercall_msr.enable)
274 return false;
276 return true;
278 EXPORT_SYMBOL_GPL(hv_is_hypercall_page_setup);