1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * X86 specific Hyper-V initialization code. 4 * 5 * Copyright (C) 2016, Microsoft, Inc. 6 * 7 * Author : K. Y. Srinivasan <kys@microsoft.com> 8 */ 9 10 #include <linux/efi.h> 11 #include <linux/types.h> 12 #include <linux/bitfield.h> 13 #include <linux/io.h> 14 #include <asm/apic.h> 15 #include <asm/desc.h> 16 #include <asm/sev.h> 17 #include <asm/ibt.h> 18 #include <asm/hypervisor.h> 19 #include <asm/hyperv-tlfs.h> 20 #include <asm/mshyperv.h> 21 #include <asm/idtentry.h> 22 #include <linux/kexec.h> 23 #include <linux/version.h> 24 #include <linux/vmalloc.h> 25 #include <linux/mm.h> 26 #include <linux/hyperv.h> 27 #include <linux/slab.h> 28 #include <linux/kernel.h> 29 #include <linux/cpuhotplug.h> 30 #include <linux/syscore_ops.h> 31 #include <clocksource/hyperv_timer.h> 32 #include <linux/highmem.h> 33 34 int hyperv_init_cpuhp; 35 u64 hv_current_partition_id = ~0ull; 36 EXPORT_SYMBOL_GPL(hv_current_partition_id); 37 38 void *hv_hypercall_pg; 39 EXPORT_SYMBOL_GPL(hv_hypercall_pg); 40 41 union hv_ghcb * __percpu *hv_ghcb_pg; 42 43 /* Storage to save the hypercall page temporarily for hibernation */ 44 static void *hv_hypercall_pg_saved; 45 46 struct hv_vp_assist_page **hv_vp_assist_page; 47 EXPORT_SYMBOL_GPL(hv_vp_assist_page); 48 49 static int hyperv_init_ghcb(void) 50 { 51 u64 ghcb_gpa; 52 void *ghcb_va; 53 void **ghcb_base; 54 55 if (!hv_isolation_type_snp()) 56 return 0; 57 58 if (!hv_ghcb_pg) 59 return -EINVAL; 60 61 /* 62 * GHCB page is allocated by paravisor. The address 63 * returned by MSR_AMD64_SEV_ES_GHCB is above shared 64 * memory boundary and map it here. 65 */ 66 rdmsrl(MSR_AMD64_SEV_ES_GHCB, ghcb_gpa); 67 68 /* Mask out vTOM bit. ioremap_cache() maps decrypted */ 69 ghcb_gpa &= ~ms_hyperv.shared_gpa_boundary; 70 ghcb_va = (void *)ioremap_cache(ghcb_gpa, HV_HYP_PAGE_SIZE); 71 if (!ghcb_va) 72 return -ENOMEM; 73 74 ghcb_base = (void **)this_cpu_ptr(hv_ghcb_pg); 75 *ghcb_base = ghcb_va; 76 77 return 0; 78 } 79 80 static int hv_cpu_init(unsigned int cpu) 81 { 82 union hv_vp_assist_msr_contents msr = { 0 }; 83 struct hv_vp_assist_page **hvp = &hv_vp_assist_page[cpu]; 84 int ret; 85 86 ret = hv_common_cpu_init(cpu); 87 if (ret) 88 return ret; 89 90 if (!hv_vp_assist_page) 91 return 0; 92 93 if (hv_root_partition) { 94 /* 95 * For root partition we get the hypervisor provided VP assist 96 * page, instead of allocating a new page. 97 */ 98 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 99 *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT, 100 PAGE_SIZE, MEMREMAP_WB); 101 } else { 102 /* 103 * The VP assist page is an "overlay" page (see Hyper-V TLFS's 104 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed 105 * out to make sure we always write the EOI MSR in 106 * hv_apic_eoi_write() *after* the EOI optimization is disabled 107 * in hv_cpu_die(), otherwise a CPU may not be stopped in the 108 * case of CPU offlining and the VM will hang. 109 */ 110 if (!*hvp) 111 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO); 112 if (*hvp) 113 msr.pfn = vmalloc_to_pfn(*hvp); 114 115 } 116 if (!WARN_ON(!(*hvp))) { 117 msr.enable = 1; 118 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 119 } 120 121 return hyperv_init_ghcb(); 122 } 123 124 static void (*hv_reenlightenment_cb)(void); 125 126 static void hv_reenlightenment_notify(struct work_struct *dummy) 127 { 128 struct hv_tsc_emulation_status emu_status; 129 130 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 131 132 /* Don't issue the callback if TSC accesses are not emulated */ 133 if (hv_reenlightenment_cb && emu_status.inprogress) 134 hv_reenlightenment_cb(); 135 } 136 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify); 137 138 void hyperv_stop_tsc_emulation(void) 139 { 140 u64 freq; 141 struct hv_tsc_emulation_status emu_status; 142 143 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 144 emu_status.inprogress = 0; 145 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 146 147 rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq); 148 tsc_khz = div64_u64(freq, 1000); 149 } 150 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation); 151 152 static inline bool hv_reenlightenment_available(void) 153 { 154 /* 155 * Check for required features and privileges to make TSC frequency 156 * change notifications work. 157 */ 158 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS && 159 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE && 160 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT; 161 } 162 163 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment) 164 { 165 ack_APIC_irq(); 166 inc_irq_stat(irq_hv_reenlightenment_count); 167 schedule_delayed_work(&hv_reenlightenment_work, HZ/10); 168 } 169 170 void set_hv_tscchange_cb(void (*cb)(void)) 171 { 172 struct hv_reenlightenment_control re_ctrl = { 173 .vector = HYPERV_REENLIGHTENMENT_VECTOR, 174 .enabled = 1, 175 }; 176 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1}; 177 178 if (!hv_reenlightenment_available()) { 179 pr_warn("Hyper-V: reenlightenment support is unavailable\n"); 180 return; 181 } 182 183 if (!hv_vp_index) 184 return; 185 186 hv_reenlightenment_cb = cb; 187 188 /* Make sure callback is registered before we write to MSRs */ 189 wmb(); 190 191 re_ctrl.target_vp = hv_vp_index[get_cpu()]; 192 193 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 194 wrmsrl(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl)); 195 196 put_cpu(); 197 } 198 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb); 199 200 void clear_hv_tscchange_cb(void) 201 { 202 struct hv_reenlightenment_control re_ctrl; 203 204 if (!hv_reenlightenment_available()) 205 return; 206 207 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); 208 re_ctrl.enabled = 0; 209 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); 210 211 hv_reenlightenment_cb = NULL; 212 } 213 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb); 214 215 static int hv_cpu_die(unsigned int cpu) 216 { 217 struct hv_reenlightenment_control re_ctrl; 218 unsigned int new_cpu; 219 void **ghcb_va; 220 221 if (hv_ghcb_pg) { 222 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg); 223 if (*ghcb_va) 224 iounmap(*ghcb_va); 225 *ghcb_va = NULL; 226 } 227 228 hv_common_cpu_die(cpu); 229 230 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) { 231 union hv_vp_assist_msr_contents msr = { 0 }; 232 if (hv_root_partition) { 233 /* 234 * For root partition the VP assist page is mapped to 235 * hypervisor provided page, and thus we unmap the 236 * page here and nullify it, so that in future we have 237 * correct page address mapped in hv_cpu_init. 238 */ 239 memunmap(hv_vp_assist_page[cpu]); 240 hv_vp_assist_page[cpu] = NULL; 241 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 242 msr.enable = 0; 243 } 244 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 245 } 246 247 if (hv_reenlightenment_cb == NULL) 248 return 0; 249 250 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 251 if (re_ctrl.target_vp == hv_vp_index[cpu]) { 252 /* 253 * Reassign reenlightenment notifications to some other online 254 * CPU or just disable the feature if there are no online CPUs 255 * left (happens on hibernation). 256 */ 257 new_cpu = cpumask_any_but(cpu_online_mask, cpu); 258 259 if (new_cpu < nr_cpu_ids) 260 re_ctrl.target_vp = hv_vp_index[new_cpu]; 261 else 262 re_ctrl.enabled = 0; 263 264 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 265 } 266 267 return 0; 268 } 269 270 static int __init hv_pci_init(void) 271 { 272 int gen2vm = efi_enabled(EFI_BOOT); 273 274 /* 275 * For Generation-2 VM, we exit from pci_arch_init() by returning 0. 276 * The purpose is to suppress the harmless warning: 277 * "PCI: Fatal: No config space access function found" 278 */ 279 if (gen2vm) 280 return 0; 281 282 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */ 283 return 1; 284 } 285 286 static int hv_suspend(void) 287 { 288 union hv_x64_msr_hypercall_contents hypercall_msr; 289 int ret; 290 291 if (hv_root_partition) 292 return -EPERM; 293 294 /* 295 * Reset the hypercall page as it is going to be invalidated 296 * across hibernation. Setting hv_hypercall_pg to NULL ensures 297 * that any subsequent hypercall operation fails safely instead of 298 * crashing due to an access of an invalid page. The hypercall page 299 * pointer is restored on resume. 300 */ 301 hv_hypercall_pg_saved = hv_hypercall_pg; 302 hv_hypercall_pg = NULL; 303 304 /* Disable the hypercall page in the hypervisor */ 305 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 306 hypercall_msr.enable = 0; 307 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 308 309 ret = hv_cpu_die(0); 310 return ret; 311 } 312 313 static void hv_resume(void) 314 { 315 union hv_x64_msr_hypercall_contents hypercall_msr; 316 int ret; 317 318 ret = hv_cpu_init(0); 319 WARN_ON(ret); 320 321 /* Re-enable the hypercall page */ 322 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 323 hypercall_msr.enable = 1; 324 hypercall_msr.guest_physical_address = 325 vmalloc_to_pfn(hv_hypercall_pg_saved); 326 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 327 328 hv_hypercall_pg = hv_hypercall_pg_saved; 329 hv_hypercall_pg_saved = NULL; 330 331 /* 332 * Reenlightenment notifications are disabled by hv_cpu_die(0), 333 * reenable them here if hv_reenlightenment_cb was previously set. 334 */ 335 if (hv_reenlightenment_cb) 336 set_hv_tscchange_cb(hv_reenlightenment_cb); 337 } 338 339 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */ 340 static struct syscore_ops hv_syscore_ops = { 341 .suspend = hv_suspend, 342 .resume = hv_resume, 343 }; 344 345 static void (* __initdata old_setup_percpu_clockev)(void); 346 347 static void __init hv_stimer_setup_percpu_clockev(void) 348 { 349 /* 350 * Ignore any errors in setting up stimer clockevents 351 * as we can run with the LAPIC timer as a fallback. 352 */ 353 (void)hv_stimer_alloc(false); 354 355 /* 356 * Still register the LAPIC timer, because the direct-mode STIMER is 357 * not supported by old versions of Hyper-V. This also allows users 358 * to switch to LAPIC timer via /sys, if they want to. 359 */ 360 if (old_setup_percpu_clockev) 361 old_setup_percpu_clockev(); 362 } 363 364 static void __init hv_get_partition_id(void) 365 { 366 struct hv_get_partition_id *output_page; 367 u64 status; 368 unsigned long flags; 369 370 local_irq_save(flags); 371 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg); 372 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page); 373 if (!hv_result_success(status)) { 374 /* No point in proceeding if this failed */ 375 pr_err("Failed to get partition ID: %lld\n", status); 376 BUG(); 377 } 378 hv_current_partition_id = output_page->partition_id; 379 local_irq_restore(flags); 380 } 381 382 /* 383 * This function is to be invoked early in the boot sequence after the 384 * hypervisor has been detected. 385 * 386 * 1. Setup the hypercall page. 387 * 2. Register Hyper-V specific clocksource. 388 * 3. Setup Hyper-V specific APIC entry points. 389 */ 390 void __init hyperv_init(void) 391 { 392 u64 guest_id; 393 union hv_x64_msr_hypercall_contents hypercall_msr; 394 int cpuhp; 395 396 if (x86_hyper_type != X86_HYPER_MS_HYPERV) 397 return; 398 399 if (hv_common_init()) 400 return; 401 402 hv_vp_assist_page = kcalloc(num_possible_cpus(), 403 sizeof(*hv_vp_assist_page), GFP_KERNEL); 404 if (!hv_vp_assist_page) { 405 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; 406 goto common_free; 407 } 408 409 if (hv_isolation_type_snp()) { 410 /* Negotiate GHCB Version. */ 411 if (!hv_ghcb_negotiate_protocol()) 412 hv_ghcb_terminate(SEV_TERM_SET_GEN, 413 GHCB_SEV_ES_PROT_UNSUPPORTED); 414 415 hv_ghcb_pg = alloc_percpu(union hv_ghcb *); 416 if (!hv_ghcb_pg) 417 goto free_vp_assist_page; 418 } 419 420 cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online", 421 hv_cpu_init, hv_cpu_die); 422 if (cpuhp < 0) 423 goto free_ghcb_page; 424 425 /* 426 * Setup the hypercall page and enable hypercalls. 427 * 1. Register the guest ID 428 * 2. Enable the hypercall and register the hypercall page 429 */ 430 guest_id = hv_generate_guest_id(LINUX_VERSION_CODE); 431 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); 432 433 /* Hyper-V requires to write guest os id via ghcb in SNP IVM. */ 434 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id); 435 436 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, 437 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, 438 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, 439 __builtin_return_address(0)); 440 if (hv_hypercall_pg == NULL) 441 goto clean_guest_os_id; 442 443 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 444 hypercall_msr.enable = 1; 445 446 if (hv_root_partition) { 447 struct page *pg; 448 void *src; 449 450 /* 451 * For the root partition, the hypervisor will set up its 452 * hypercall page. The hypervisor guarantees it will not show 453 * up in the root's address space. The root can't change the 454 * location of the hypercall page. 455 * 456 * Order is important here. We must enable the hypercall page 457 * so it is populated with code, then copy the code to an 458 * executable page. 459 */ 460 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 461 462 pg = vmalloc_to_page(hv_hypercall_pg); 463 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE, 464 MEMREMAP_WB); 465 BUG_ON(!src); 466 memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE); 467 memunmap(src); 468 469 hv_remap_tsc_clocksource(); 470 } else { 471 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); 472 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 473 } 474 475 /* 476 * Some versions of Hyper-V that provide IBT in guest VMs have a bug 477 * in that there's no ENDBR64 instruction at the entry to the 478 * hypercall page. Because hypercalls are invoked via an indirect call 479 * to the hypercall page, all hypercall attempts fail when IBT is 480 * enabled, and Linux panics. For such buggy versions, disable IBT. 481 * 482 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall 483 * page, so if future Linux kernel versions enable IBT for 32-bit 484 * builds, additional hypercall page hackery will be required here 485 * to provide an ENDBR32. 486 */ 487 #ifdef CONFIG_X86_KERNEL_IBT 488 if (cpu_feature_enabled(X86_FEATURE_IBT) && 489 *(u32 *)hv_hypercall_pg != gen_endbr()) { 490 setup_clear_cpu_cap(X86_FEATURE_IBT); 491 pr_warn("Hyper-V: Disabling IBT because of Hyper-V bug\n"); 492 } 493 #endif 494 495 /* 496 * hyperv_init() is called before LAPIC is initialized: see 497 * apic_intr_mode_init() -> x86_platform.apic_post_init() and 498 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER 499 * depends on LAPIC, so hv_stimer_alloc() should be called from 500 * x86_init.timers.setup_percpu_clockev. 501 */ 502 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev; 503 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev; 504 505 hv_apic_init(); 506 507 x86_init.pci.arch_init = hv_pci_init; 508 509 register_syscore_ops(&hv_syscore_ops); 510 511 hyperv_init_cpuhp = cpuhp; 512 513 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID) 514 hv_get_partition_id(); 515 516 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull); 517 518 #ifdef CONFIG_PCI_MSI 519 /* 520 * If we're running as root, we want to create our own PCI MSI domain. 521 * We can't set this in hv_pci_init because that would be too late. 522 */ 523 if (hv_root_partition) 524 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain; 525 #endif 526 527 /* Query the VMs extended capability once, so that it can be cached. */ 528 hv_query_ext_cap(0); 529 530 return; 531 532 clean_guest_os_id: 533 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); 534 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); 535 cpuhp_remove_state(cpuhp); 536 free_ghcb_page: 537 free_percpu(hv_ghcb_pg); 538 free_vp_assist_page: 539 kfree(hv_vp_assist_page); 540 hv_vp_assist_page = NULL; 541 common_free: 542 hv_common_free(); 543 } 544 545 /* 546 * This routine is called before kexec/kdump, it does the required cleanup. 547 */ 548 void hyperv_cleanup(void) 549 { 550 union hv_x64_msr_hypercall_contents hypercall_msr; 551 union hv_reference_tsc_msr tsc_msr; 552 553 /* Reset our OS id */ 554 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); 555 hv_ghcb_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); 556 557 /* 558 * Reset hypercall page reference before reset the page, 559 * let hypercall operations fail safely rather than 560 * panic the kernel for using invalid hypercall page 561 */ 562 hv_hypercall_pg = NULL; 563 564 /* Reset the hypercall page */ 565 hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL); 566 hypercall_msr.enable = 0; 567 hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 568 569 /* Reset the TSC page */ 570 tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC); 571 tsc_msr.enable = 0; 572 hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); 573 } 574 575 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die) 576 { 577 static bool panic_reported; 578 u64 guest_id; 579 580 if (in_die && !panic_on_oops) 581 return; 582 583 /* 584 * We prefer to report panic on 'die' chain as we have proper 585 * registers to report, but if we miss it (e.g. on BUG()) we need 586 * to report it on 'panic'. 587 */ 588 if (panic_reported) 589 return; 590 panic_reported = true; 591 592 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); 593 594 wrmsrl(HV_X64_MSR_CRASH_P0, err); 595 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id); 596 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip); 597 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax); 598 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp); 599 600 /* 601 * Let Hyper-V know there is crash data available 602 */ 603 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); 604 } 605 EXPORT_SYMBOL_GPL(hyperv_report_panic); 606 607 bool hv_is_hyperv_initialized(void) 608 { 609 union hv_x64_msr_hypercall_contents hypercall_msr; 610 611 /* 612 * Ensure that we're really on Hyper-V, and not a KVM or Xen 613 * emulation of Hyper-V 614 */ 615 if (x86_hyper_type != X86_HYPER_MS_HYPERV) 616 return false; 617 618 /* 619 * Verify that earlier initialization succeeded by checking 620 * that the hypercall page is setup 621 */ 622 hypercall_msr.as_uint64 = 0; 623 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 624 625 return hypercall_msr.enable; 626 } 627 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); 628