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