David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 2 | /* |
| 3 | * X86 specific Hyper-V initialization code. |
| 4 | * |
| 5 | * Copyright (C) 2016, Microsoft, Inc. |
| 6 | * |
| 7 | * Author : K. Y. Srinivasan <kys@microsoft.com> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 8 | */ |
| 9 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 10 | #include <linux/efi.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 20 | #include <linux/hyperv.h> |
| 21 | #include <linux/slab.h> |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 22 | #include <linux/kernel.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 23 | #include <linux/cpuhotplug.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 24 | #include <clocksource/hyperv_timer.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 25 | |
| 26 | void *hv_hypercall_pg; |
| 27 | EXPORT_SYMBOL_GPL(hv_hypercall_pg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 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); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 60 | struct page *pg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 61 | |
| 62 | input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 63 | pg = alloc_page(GFP_KERNEL); |
| 64 | if (unlikely(!pg)) |
| 65 | return -ENOMEM; |
| 66 | *input_arg = page_address(pg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 78 | /* |
| 79 | * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section |
| 80 | * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure |
| 81 | * we always write the EOI MSR in hv_apic_eoi_write() *after* the |
| 82 | * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may |
| 83 | * not be stopped in the case of CPU offlining and the VM will hang. |
| 84 | */ |
| 85 | if (!*hvp) { |
| 86 | *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO, |
| 87 | PAGE_KERNEL); |
| 88 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | /* Don't issue the callback if TSC accesses are not emulated */ |
| 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 | * Check for required features and priviliges to make TSC frequency |
| 135 | * change notifications work. |
| 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 | /* Make sure callback is registered before we write to MSRs */ |
| 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 | /* Reassign to some other online CPU */ |
| 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 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 225 | static int __init hv_pci_init(void) |
| 226 | { |
| 227 | int gen2vm = efi_enabled(EFI_BOOT); |
| 228 | |
| 229 | /* |
| 230 | * For Generation-2 VM, we exit from pci_arch_init() by returning 0. |
| 231 | * The purpose is to suppress the harmless warning: |
| 232 | * "PCI: Fatal: No config space access function found" |
| 233 | */ |
| 234 | if (gen2vm) |
| 235 | return 0; |
| 236 | |
| 237 | /* For Generation-1 VM, we'll proceed in pci_arch_init(). */ |
| 238 | return 1; |
| 239 | } |
| 240 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 241 | /* |
| 242 | * This function is to be invoked early in the boot sequence after the |
| 243 | * hypervisor has been detected. |
| 244 | * |
| 245 | * 1. Setup the hypercall page. |
| 246 | * 2. Register Hyper-V specific clocksource. |
| 247 | * 3. Setup Hyper-V specific APIC entry points. |
| 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 | /* Absolutely required MSRs */ |
| 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 | * Allocate the per-CPU state for the hypercall input arg. |
| 267 | * If this allocation fails, we will not be able to setup |
| 268 | * (per-CPU) hypercall input page and thus this failure is |
| 269 | * fatal on Hyper-V. |
| 270 | */ |
| 271 | hyperv_pcpu_input_arg = alloc_percpu(void *); |
| 272 | |
| 273 | BUG_ON(hyperv_pcpu_input_arg == NULL); |
| 274 | |
| 275 | /* Allocate percpu VP index */ |
| 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 | * Setup the hypercall page and enable hypercalls. |
| 298 | * 1. Register the guest ID |
| 299 | * 2. Enable the hypercall and register the hypercall page |
| 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 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 317 | x86_init.pci.arch_init = hv_pci_init; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 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 | * This routine is called before kexec/kdump, it does the required cleanup. |
| 333 | */ |
| 334 | void hyperv_cleanup(void) |
| 335 | { |
| 336 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 337 | |
| 338 | /* Reset our OS id */ |
| 339 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
| 340 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame] | 341 | /* |
| 342 | * Reset hypercall page reference before reset the page, |
| 343 | * let hypercall operations fail safely rather than |
| 344 | * panic the kernel for using invalid hypercall page |
| 345 | */ |
| 346 | hv_hypercall_pg = NULL; |
| 347 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 348 | /* Reset the hypercall page */ |
| 349 | hypercall_msr.as_uint64 = 0; |
| 350 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 351 | |
| 352 | /* Reset the TSC page */ |
| 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 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 358 | void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die) |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 359 | { |
| 360 | static bool panic_reported; |
| 361 | u64 guest_id; |
| 362 | |
Olivier Deprez | 0e64123 | 2021-09-23 10:07:05 +0200 | [diff] [blame^] | 363 | if (in_die && !panic_on_oops) |
| 364 | return; |
| 365 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 366 | /* |
| 367 | * We prefer to report panic on 'die' chain as we have proper |
| 368 | * registers to report, but if we miss it (e.g. on BUG()) we need |
| 369 | * to report it on 'panic'. |
| 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 | * Let Hyper-V know there is crash data available |
| 385 | */ |
| 386 | wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); |
| 387 | } |
| 388 | EXPORT_SYMBOL_GPL(hyperv_report_panic); |
| 389 | |
| 390 | /** |
| 391 | * hyperv_report_panic_msg - report panic message to Hyper-V |
| 392 | * @pa: physical address of the panic page containing the message |
| 393 | * @size: size of the message in the page |
| 394 | */ |
| 395 | void hyperv_report_panic_msg(phys_addr_t pa, size_t size) |
| 396 | { |
| 397 | /* |
| 398 | * P3 to contain the physical address of the panic page & P4 to |
| 399 | * contain the size of the panic data in that page. Rest of the |
| 400 | * registers are no-op when the NOTIFY_MSG flag is set. |
| 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 | * Let Hyper-V know there is crash data available along with |
| 410 | * the panic message. |
| 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 | * Ensure that we're really on Hyper-V, and not a KVM or Xen |
| 423 | * emulation of Hyper-V |
| 424 | */ |
| 425 | if (x86_hyper_type != X86_HYPER_MS_HYPERV) |
| 426 | return false; |
| 427 | |
| 428 | /* |
| 429 | * Verify that earlier initialization succeeded by checking |
| 430 | * that the hypercall page is setup |
| 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); |