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> |
| 22 | #include <linux/cpuhotplug.h> |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 23 | #include <clocksource/hyperv_timer.h> |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 24 | |
| 25 | void *hv_hypercall_pg; |
| 26 | EXPORT_SYMBOL_GPL(hv_hypercall_pg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 27 | |
| 28 | u32 *hv_vp_index; |
| 29 | EXPORT_SYMBOL_GPL(hv_vp_index); |
| 30 | |
| 31 | struct hv_vp_assist_page **hv_vp_assist_page; |
| 32 | EXPORT_SYMBOL_GPL(hv_vp_assist_page); |
| 33 | |
| 34 | void __percpu **hyperv_pcpu_input_arg; |
| 35 | EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg); |
| 36 | |
| 37 | u32 hv_max_vp_index; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 38 | EXPORT_SYMBOL_GPL(hv_max_vp_index); |
| 39 | |
| 40 | void *hv_alloc_hyperv_page(void) |
| 41 | { |
| 42 | BUILD_BUG_ON(PAGE_SIZE != HV_HYP_PAGE_SIZE); |
| 43 | |
| 44 | return (void *)__get_free_page(GFP_KERNEL); |
| 45 | } |
| 46 | EXPORT_SYMBOL_GPL(hv_alloc_hyperv_page); |
| 47 | |
| 48 | void hv_free_hyperv_page(unsigned long addr) |
| 49 | { |
| 50 | free_page(addr); |
| 51 | } |
| 52 | EXPORT_SYMBOL_GPL(hv_free_hyperv_page); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 53 | |
| 54 | static int hv_cpu_init(unsigned int cpu) |
| 55 | { |
| 56 | u64 msr_vp_index; |
| 57 | struct hv_vp_assist_page **hvp = &hv_vp_assist_page[smp_processor_id()]; |
| 58 | void **input_arg; |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 59 | struct page *pg; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 60 | |
| 61 | input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 62 | pg = alloc_page(GFP_KERNEL); |
| 63 | if (unlikely(!pg)) |
| 64 | return -ENOMEM; |
| 65 | *input_arg = page_address(pg); |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 66 | |
| 67 | hv_get_vp_index(msr_vp_index); |
| 68 | |
| 69 | hv_vp_index[smp_processor_id()] = msr_vp_index; |
| 70 | |
| 71 | if (msr_vp_index > hv_max_vp_index) |
| 72 | hv_max_vp_index = msr_vp_index; |
| 73 | |
| 74 | if (!hv_vp_assist_page) |
| 75 | return 0; |
| 76 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 77 | /* |
| 78 | * The VP ASSIST PAGE is an "overlay" page (see Hyper-V TLFS's Section |
| 79 | * 5.2.1 "GPA Overlay Pages"). Here it must be zeroed out to make sure |
| 80 | * we always write the EOI MSR in hv_apic_eoi_write() *after* the |
| 81 | * EOI optimization is disabled in hv_cpu_die(), otherwise a CPU may |
| 82 | * not be stopped in the case of CPU offlining and the VM will hang. |
| 83 | */ |
| 84 | if (!*hvp) { |
| 85 | *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO, |
| 86 | PAGE_KERNEL); |
| 87 | } |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 88 | |
| 89 | if (*hvp) { |
| 90 | u64 val; |
| 91 | |
| 92 | val = vmalloc_to_pfn(*hvp); |
| 93 | val = (val << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT) | |
| 94 | HV_X64_MSR_VP_ASSIST_PAGE_ENABLE; |
| 95 | |
| 96 | wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, val); |
| 97 | } |
| 98 | |
| 99 | return 0; |
| 100 | } |
| 101 | |
| 102 | static void (*hv_reenlightenment_cb)(void); |
| 103 | |
| 104 | static void hv_reenlightenment_notify(struct work_struct *dummy) |
| 105 | { |
| 106 | struct hv_tsc_emulation_status emu_status; |
| 107 | |
| 108 | rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 109 | |
| 110 | /* Don't issue the callback if TSC accesses are not emulated */ |
| 111 | if (hv_reenlightenment_cb && emu_status.inprogress) |
| 112 | hv_reenlightenment_cb(); |
| 113 | } |
| 114 | static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify); |
| 115 | |
| 116 | void hyperv_stop_tsc_emulation(void) |
| 117 | { |
| 118 | u64 freq; |
| 119 | struct hv_tsc_emulation_status emu_status; |
| 120 | |
| 121 | rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 122 | emu_status.inprogress = 0; |
| 123 | wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); |
| 124 | |
| 125 | rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq); |
| 126 | tsc_khz = div64_u64(freq, 1000); |
| 127 | } |
| 128 | EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation); |
| 129 | |
| 130 | static inline bool hv_reenlightenment_available(void) |
| 131 | { |
| 132 | /* |
| 133 | * Check for required features and priviliges to make TSC frequency |
| 134 | * change notifications work. |
| 135 | */ |
| 136 | return ms_hyperv.features & HV_X64_ACCESS_FREQUENCY_MSRS && |
| 137 | ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE && |
| 138 | ms_hyperv.features & HV_X64_ACCESS_REENLIGHTENMENT; |
| 139 | } |
| 140 | |
| 141 | __visible void __irq_entry hyperv_reenlightenment_intr(struct pt_regs *regs) |
| 142 | { |
| 143 | entering_ack_irq(); |
| 144 | |
| 145 | inc_irq_stat(irq_hv_reenlightenment_count); |
| 146 | |
| 147 | schedule_delayed_work(&hv_reenlightenment_work, HZ/10); |
| 148 | |
| 149 | exiting_irq(); |
| 150 | } |
| 151 | |
| 152 | void set_hv_tscchange_cb(void (*cb)(void)) |
| 153 | { |
| 154 | struct hv_reenlightenment_control re_ctrl = { |
| 155 | .vector = HYPERV_REENLIGHTENMENT_VECTOR, |
| 156 | .enabled = 1, |
| 157 | .target_vp = hv_vp_index[smp_processor_id()] |
| 158 | }; |
| 159 | struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1}; |
| 160 | |
| 161 | if (!hv_reenlightenment_available()) { |
| 162 | pr_warn("Hyper-V: reenlightenment support is unavailable\n"); |
| 163 | return; |
| 164 | } |
| 165 | |
| 166 | hv_reenlightenment_cb = cb; |
| 167 | |
| 168 | /* Make sure callback is registered before we write to MSRs */ |
| 169 | wmb(); |
| 170 | |
| 171 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 172 | wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl)); |
| 173 | } |
| 174 | EXPORT_SYMBOL_GPL(set_hv_tscchange_cb); |
| 175 | |
| 176 | void clear_hv_tscchange_cb(void) |
| 177 | { |
| 178 | struct hv_reenlightenment_control re_ctrl; |
| 179 | |
| 180 | if (!hv_reenlightenment_available()) |
| 181 | return; |
| 182 | |
| 183 | rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); |
| 184 | re_ctrl.enabled = 0; |
| 185 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); |
| 186 | |
| 187 | hv_reenlightenment_cb = NULL; |
| 188 | } |
| 189 | EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb); |
| 190 | |
| 191 | static int hv_cpu_die(unsigned int cpu) |
| 192 | { |
| 193 | struct hv_reenlightenment_control re_ctrl; |
| 194 | unsigned int new_cpu; |
| 195 | unsigned long flags; |
| 196 | void **input_arg; |
| 197 | void *input_pg = NULL; |
| 198 | |
| 199 | local_irq_save(flags); |
| 200 | input_arg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); |
| 201 | input_pg = *input_arg; |
| 202 | *input_arg = NULL; |
| 203 | local_irq_restore(flags); |
| 204 | free_page((unsigned long)input_pg); |
| 205 | |
| 206 | if (hv_vp_assist_page && hv_vp_assist_page[cpu]) |
| 207 | wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, 0); |
| 208 | |
| 209 | if (hv_reenlightenment_cb == NULL) |
| 210 | return 0; |
| 211 | |
| 212 | rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 213 | if (re_ctrl.target_vp == hv_vp_index[cpu]) { |
| 214 | /* Reassign to some other online CPU */ |
| 215 | new_cpu = cpumask_any_but(cpu_online_mask, cpu); |
| 216 | |
| 217 | re_ctrl.target_vp = hv_vp_index[new_cpu]; |
| 218 | wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 224 | static int __init hv_pci_init(void) |
| 225 | { |
| 226 | int gen2vm = efi_enabled(EFI_BOOT); |
| 227 | |
| 228 | /* |
| 229 | * For Generation-2 VM, we exit from pci_arch_init() by returning 0. |
| 230 | * The purpose is to suppress the harmless warning: |
| 231 | * "PCI: Fatal: No config space access function found" |
| 232 | */ |
| 233 | if (gen2vm) |
| 234 | return 0; |
| 235 | |
| 236 | /* For Generation-1 VM, we'll proceed in pci_arch_init(). */ |
| 237 | return 1; |
| 238 | } |
| 239 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 240 | /* |
| 241 | * This function is to be invoked early in the boot sequence after the |
| 242 | * hypervisor has been detected. |
| 243 | * |
| 244 | * 1. Setup the hypercall page. |
| 245 | * 2. Register Hyper-V specific clocksource. |
| 246 | * 3. Setup Hyper-V specific APIC entry points. |
| 247 | */ |
| 248 | void __init hyperv_init(void) |
| 249 | { |
| 250 | u64 guest_id, required_msrs; |
| 251 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 252 | int cpuhp, i; |
| 253 | |
| 254 | if (x86_hyper_type != X86_HYPER_MS_HYPERV) |
| 255 | return; |
| 256 | |
| 257 | /* Absolutely required MSRs */ |
| 258 | required_msrs = HV_X64_MSR_HYPERCALL_AVAILABLE | |
| 259 | HV_X64_MSR_VP_INDEX_AVAILABLE; |
| 260 | |
| 261 | if ((ms_hyperv.features & required_msrs) != required_msrs) |
| 262 | return; |
| 263 | |
| 264 | /* |
| 265 | * Allocate the per-CPU state for the hypercall input arg. |
| 266 | * If this allocation fails, we will not be able to setup |
| 267 | * (per-CPU) hypercall input page and thus this failure is |
| 268 | * fatal on Hyper-V. |
| 269 | */ |
| 270 | hyperv_pcpu_input_arg = alloc_percpu(void *); |
| 271 | |
| 272 | BUG_ON(hyperv_pcpu_input_arg == NULL); |
| 273 | |
| 274 | /* Allocate percpu VP index */ |
| 275 | hv_vp_index = kmalloc_array(num_possible_cpus(), sizeof(*hv_vp_index), |
| 276 | GFP_KERNEL); |
| 277 | if (!hv_vp_index) |
| 278 | return; |
| 279 | |
| 280 | for (i = 0; i < num_possible_cpus(); i++) |
| 281 | hv_vp_index[i] = VP_INVAL; |
| 282 | |
| 283 | hv_vp_assist_page = kcalloc(num_possible_cpus(), |
| 284 | sizeof(*hv_vp_assist_page), GFP_KERNEL); |
| 285 | if (!hv_vp_assist_page) { |
| 286 | ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; |
| 287 | goto free_vp_index; |
| 288 | } |
| 289 | |
| 290 | cpuhp = cpuhp_setup_state(CPUHP_AP_ONLINE_DYN, "x86/hyperv_init:online", |
| 291 | hv_cpu_init, hv_cpu_die); |
| 292 | if (cpuhp < 0) |
| 293 | goto free_vp_assist_page; |
| 294 | |
| 295 | /* |
| 296 | * Setup the hypercall page and enable hypercalls. |
| 297 | * 1. Register the guest ID |
| 298 | * 2. Enable the hypercall and register the hypercall page |
| 299 | */ |
| 300 | guest_id = generate_guest_id(0, LINUX_VERSION_CODE, 0); |
| 301 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 302 | |
| 303 | hv_hypercall_pg = __vmalloc(PAGE_SIZE, GFP_KERNEL, PAGE_KERNEL_RX); |
| 304 | if (hv_hypercall_pg == NULL) { |
| 305 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
| 306 | goto remove_cpuhp_state; |
| 307 | } |
| 308 | |
| 309 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 310 | hypercall_msr.enable = 1; |
| 311 | hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); |
| 312 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 313 | |
| 314 | hv_apic_init(); |
| 315 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 316 | x86_init.pci.arch_init = hv_pci_init; |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 317 | |
| 318 | return; |
| 319 | |
| 320 | remove_cpuhp_state: |
| 321 | cpuhp_remove_state(cpuhp); |
| 322 | free_vp_assist_page: |
| 323 | kfree(hv_vp_assist_page); |
| 324 | hv_vp_assist_page = NULL; |
| 325 | free_vp_index: |
| 326 | kfree(hv_vp_index); |
| 327 | hv_vp_index = NULL; |
| 328 | } |
| 329 | |
| 330 | /* |
| 331 | * This routine is called before kexec/kdump, it does the required cleanup. |
| 332 | */ |
| 333 | void hyperv_cleanup(void) |
| 334 | { |
| 335 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 336 | |
| 337 | /* Reset our OS id */ |
| 338 | wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); |
| 339 | |
David Brazdil | 0f672f6 | 2019-12-10 10:32:29 +0000 | [diff] [blame^] | 340 | /* |
| 341 | * Reset hypercall page reference before reset the page, |
| 342 | * let hypercall operations fail safely rather than |
| 343 | * panic the kernel for using invalid hypercall page |
| 344 | */ |
| 345 | hv_hypercall_pg = NULL; |
| 346 | |
Andrew Scull | b4b6d4a | 2019-01-02 15:54:55 +0000 | [diff] [blame] | 347 | /* Reset the hypercall page */ |
| 348 | hypercall_msr.as_uint64 = 0; |
| 349 | wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 350 | |
| 351 | /* Reset the TSC page */ |
| 352 | hypercall_msr.as_uint64 = 0; |
| 353 | wrmsrl(HV_X64_MSR_REFERENCE_TSC, hypercall_msr.as_uint64); |
| 354 | } |
| 355 | EXPORT_SYMBOL_GPL(hyperv_cleanup); |
| 356 | |
| 357 | void hyperv_report_panic(struct pt_regs *regs, long err) |
| 358 | { |
| 359 | static bool panic_reported; |
| 360 | u64 guest_id; |
| 361 | |
| 362 | /* |
| 363 | * We prefer to report panic on 'die' chain as we have proper |
| 364 | * registers to report, but if we miss it (e.g. on BUG()) we need |
| 365 | * to report it on 'panic'. |
| 366 | */ |
| 367 | if (panic_reported) |
| 368 | return; |
| 369 | panic_reported = true; |
| 370 | |
| 371 | rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); |
| 372 | |
| 373 | wrmsrl(HV_X64_MSR_CRASH_P0, err); |
| 374 | wrmsrl(HV_X64_MSR_CRASH_P1, guest_id); |
| 375 | wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip); |
| 376 | wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax); |
| 377 | wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp); |
| 378 | |
| 379 | /* |
| 380 | * Let Hyper-V know there is crash data available |
| 381 | */ |
| 382 | wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); |
| 383 | } |
| 384 | EXPORT_SYMBOL_GPL(hyperv_report_panic); |
| 385 | |
| 386 | /** |
| 387 | * hyperv_report_panic_msg - report panic message to Hyper-V |
| 388 | * @pa: physical address of the panic page containing the message |
| 389 | * @size: size of the message in the page |
| 390 | */ |
| 391 | void hyperv_report_panic_msg(phys_addr_t pa, size_t size) |
| 392 | { |
| 393 | /* |
| 394 | * P3 to contain the physical address of the panic page & P4 to |
| 395 | * contain the size of the panic data in that page. Rest of the |
| 396 | * registers are no-op when the NOTIFY_MSG flag is set. |
| 397 | */ |
| 398 | wrmsrl(HV_X64_MSR_CRASH_P0, 0); |
| 399 | wrmsrl(HV_X64_MSR_CRASH_P1, 0); |
| 400 | wrmsrl(HV_X64_MSR_CRASH_P2, 0); |
| 401 | wrmsrl(HV_X64_MSR_CRASH_P3, pa); |
| 402 | wrmsrl(HV_X64_MSR_CRASH_P4, size); |
| 403 | |
| 404 | /* |
| 405 | * Let Hyper-V know there is crash data available along with |
| 406 | * the panic message. |
| 407 | */ |
| 408 | wrmsrl(HV_X64_MSR_CRASH_CTL, |
| 409 | (HV_CRASH_CTL_CRASH_NOTIFY | HV_CRASH_CTL_CRASH_NOTIFY_MSG)); |
| 410 | } |
| 411 | EXPORT_SYMBOL_GPL(hyperv_report_panic_msg); |
| 412 | |
| 413 | bool hv_is_hyperv_initialized(void) |
| 414 | { |
| 415 | union hv_x64_msr_hypercall_contents hypercall_msr; |
| 416 | |
| 417 | /* |
| 418 | * Ensure that we're really on Hyper-V, and not a KVM or Xen |
| 419 | * emulation of Hyper-V |
| 420 | */ |
| 421 | if (x86_hyper_type != X86_HYPER_MS_HYPERV) |
| 422 | return false; |
| 423 | |
| 424 | /* |
| 425 | * Verify that earlier initialization succeeded by checking |
| 426 | * that the hypercall page is setup |
| 427 | */ |
| 428 | hypercall_msr.as_uint64 = 0; |
| 429 | rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); |
| 430 | |
| 431 | return hypercall_msr.enable; |
| 432 | } |
| 433 | EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); |