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