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 #define pr_fmt(fmt) "Hyper-V: " fmt 11 12 #include <linux/efi.h> 13 #include <linux/types.h> 14 #include <linux/bitfield.h> 15 #include <linux/io.h> 16 #include <asm/apic.h> 17 #include <asm/desc.h> 18 #include <asm/e820/api.h> 19 #include <asm/sev.h> 20 #include <asm/ibt.h> 21 #include <asm/hypervisor.h> 22 #include <hyperv/hvhdk.h> 23 #include <asm/mshyperv.h> 24 #include <asm/msr.h> 25 #include <asm/idtentry.h> 26 #include <asm/set_memory.h> 27 #include <linux/kexec.h> 28 #include <linux/version.h> 29 #include <linux/vmalloc.h> 30 #include <linux/mm.h> 31 #include <linux/slab.h> 32 #include <linux/kernel.h> 33 #include <linux/cpuhotplug.h> 34 #include <linux/syscore_ops.h> 35 #include <clocksource/hyperv_timer.h> 36 #include <linux/highmem.h> 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 (!ms_hyperv.paravisor_present || !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 rdmsrq(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; 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 hvp = &hv_vp_assist_page[cpu]; 94 if (hv_root_partition()) { 95 /* 96 * For root partition we get the hypervisor provided VP assist 97 * page, instead of allocating a new page. 98 */ 99 rdmsrq(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 100 *hvp = memremap(msr.pfn << HV_X64_MSR_VP_ASSIST_PAGE_ADDRESS_SHIFT, 101 PAGE_SIZE, MEMREMAP_WB); 102 } else { 103 /* 104 * The VP assist page is an "overlay" page (see Hyper-V TLFS's 105 * Section 5.2.1 "GPA Overlay Pages"). Here it must be zeroed 106 * out to make sure we always write the EOI MSR in 107 * hv_apic_eoi_write() *after* the EOI optimization is disabled 108 * in hv_cpu_die(), otherwise a CPU may not be stopped in the 109 * case of CPU offlining and the VM will hang. 110 */ 111 if (!*hvp) { 112 *hvp = __vmalloc(PAGE_SIZE, GFP_KERNEL | __GFP_ZERO); 113 114 /* 115 * Hyper-V should never specify a VM that is a Confidential 116 * VM and also running in the root partition. Root partition 117 * is blocked to run in Confidential VM. So only decrypt assist 118 * page in non-root partition here. 119 */ 120 if (*hvp && !ms_hyperv.paravisor_present && hv_isolation_type_snp()) { 121 WARN_ON_ONCE(set_memory_decrypted((unsigned long)(*hvp), 1)); 122 memset(*hvp, 0, PAGE_SIZE); 123 } 124 } 125 126 if (*hvp) 127 msr.pfn = vmalloc_to_pfn(*hvp); 128 129 } 130 if (!WARN_ON(!(*hvp))) { 131 msr.enable = 1; 132 wrmsrq(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 133 } 134 135 return hyperv_init_ghcb(); 136 } 137 138 static void (*hv_reenlightenment_cb)(void); 139 140 static void hv_reenlightenment_notify(struct work_struct *dummy) 141 { 142 struct hv_tsc_emulation_status emu_status; 143 144 rdmsrq(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 145 146 /* Don't issue the callback if TSC accesses are not emulated */ 147 if (hv_reenlightenment_cb && emu_status.inprogress) 148 hv_reenlightenment_cb(); 149 } 150 static DECLARE_DELAYED_WORK(hv_reenlightenment_work, hv_reenlightenment_notify); 151 152 void hyperv_stop_tsc_emulation(void) 153 { 154 u64 freq; 155 struct hv_tsc_emulation_status emu_status; 156 157 rdmsrq(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 158 emu_status.inprogress = 0; 159 wrmsrq(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 160 161 rdmsrq(HV_X64_MSR_TSC_FREQUENCY, freq); 162 tsc_khz = div64_u64(freq, 1000); 163 } 164 EXPORT_SYMBOL_GPL(hyperv_stop_tsc_emulation); 165 166 static inline bool hv_reenlightenment_available(void) 167 { 168 /* 169 * Check for required features and privileges to make TSC frequency 170 * change notifications work. 171 */ 172 return ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS && 173 ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE && 174 ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT; 175 } 176 177 DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_reenlightenment) 178 { 179 apic_eoi(); 180 inc_irq_stat(irq_hv_reenlightenment_count); 181 schedule_delayed_work(&hv_reenlightenment_work, HZ/10); 182 } 183 184 void set_hv_tscchange_cb(void (*cb)(void)) 185 { 186 struct hv_reenlightenment_control re_ctrl = { 187 .vector = HYPERV_REENLIGHTENMENT_VECTOR, 188 .enabled = 1, 189 }; 190 struct hv_tsc_emulation_control emu_ctrl = {.enabled = 1}; 191 192 if (!hv_reenlightenment_available()) { 193 pr_warn("reenlightenment support is unavailable\n"); 194 return; 195 } 196 197 if (!hv_vp_index) 198 return; 199 200 hv_reenlightenment_cb = cb; 201 202 /* Make sure callback is registered before we write to MSRs */ 203 wmb(); 204 205 re_ctrl.target_vp = hv_vp_index[get_cpu()]; 206 207 wrmsrq(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 208 wrmsrq(HV_X64_MSR_TSC_EMULATION_CONTROL, *((u64 *)&emu_ctrl)); 209 210 put_cpu(); 211 } 212 EXPORT_SYMBOL_GPL(set_hv_tscchange_cb); 213 214 void clear_hv_tscchange_cb(void) 215 { 216 struct hv_reenlightenment_control re_ctrl; 217 218 if (!hv_reenlightenment_available()) 219 return; 220 221 rdmsrq(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); 222 re_ctrl.enabled = 0; 223 wrmsrq(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); 224 225 hv_reenlightenment_cb = NULL; 226 } 227 EXPORT_SYMBOL_GPL(clear_hv_tscchange_cb); 228 229 static int hv_cpu_die(unsigned int cpu) 230 { 231 struct hv_reenlightenment_control re_ctrl; 232 unsigned int new_cpu; 233 void **ghcb_va; 234 235 if (hv_ghcb_pg) { 236 ghcb_va = (void **)this_cpu_ptr(hv_ghcb_pg); 237 if (*ghcb_va) 238 iounmap(*ghcb_va); 239 *ghcb_va = NULL; 240 } 241 242 hv_common_cpu_die(cpu); 243 244 if (hv_vp_assist_page && hv_vp_assist_page[cpu]) { 245 union hv_vp_assist_msr_contents msr = { 0 }; 246 if (hv_root_partition()) { 247 /* 248 * For root partition the VP assist page is mapped to 249 * hypervisor provided page, and thus we unmap the 250 * page here and nullify it, so that in future we have 251 * correct page address mapped in hv_cpu_init. 252 */ 253 memunmap(hv_vp_assist_page[cpu]); 254 hv_vp_assist_page[cpu] = NULL; 255 rdmsrq(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 256 msr.enable = 0; 257 } 258 wrmsrq(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 259 } 260 261 if (hv_reenlightenment_cb == NULL) 262 return 0; 263 264 rdmsrq(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 265 if (re_ctrl.target_vp == hv_vp_index[cpu]) { 266 /* 267 * Reassign reenlightenment notifications to some other online 268 * CPU or just disable the feature if there are no online CPUs 269 * left (happens on hibernation). 270 */ 271 new_cpu = cpumask_any_but(cpu_online_mask, cpu); 272 273 if (new_cpu < nr_cpu_ids) 274 re_ctrl.target_vp = hv_vp_index[new_cpu]; 275 else 276 re_ctrl.enabled = 0; 277 278 wrmsrq(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 279 } 280 281 return 0; 282 } 283 284 static int __init hv_pci_init(void) 285 { 286 bool gen2vm = efi_enabled(EFI_BOOT); 287 288 /* 289 * A Generation-2 VM doesn't support legacy PCI/PCIe, so both 290 * raw_pci_ops and raw_pci_ext_ops are NULL, and pci_subsys_init() -> 291 * pcibios_init() doesn't call pcibios_resource_survey() -> 292 * e820__reserve_resources_late(); as a result, any emulated persistent 293 * memory of E820_TYPE_PRAM (12) via the kernel parameter 294 * memmap=nn[KMG]!ss is not added into iomem_resource and hence can't be 295 * detected by register_e820_pmem(). Fix this by directly calling 296 * e820__reserve_resources_late() here: e820__reserve_resources_late() 297 * depends on e820__reserve_resources(), which has been called earlier 298 * from setup_arch(). Note: e820__reserve_resources_late() also adds 299 * any memory of E820_TYPE_PMEM (7) into iomem_resource, and 300 * acpi_nfit_register_region() -> acpi_nfit_insert_resource() -> 301 * region_intersects() returns REGION_INTERSECTS, so the memory of 302 * E820_TYPE_PMEM won't get added twice. 303 * 304 * We return 0 here so that pci_arch_init() won't print the warning: 305 * "PCI: Fatal: No config space access function found" 306 */ 307 if (gen2vm) { 308 e820__reserve_resources_late(); 309 return 0; 310 } 311 312 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */ 313 return 1; 314 } 315 316 static int hv_suspend(void) 317 { 318 union hv_x64_msr_hypercall_contents hypercall_msr; 319 int ret; 320 321 if (hv_root_partition()) 322 return -EPERM; 323 324 /* 325 * Reset the hypercall page as it is going to be invalidated 326 * across hibernation. Setting hv_hypercall_pg to NULL ensures 327 * that any subsequent hypercall operation fails safely instead of 328 * crashing due to an access of an invalid page. The hypercall page 329 * pointer is restored on resume. 330 */ 331 hv_hypercall_pg_saved = hv_hypercall_pg; 332 hv_hypercall_pg = NULL; 333 334 /* Disable the hypercall page in the hypervisor */ 335 rdmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 336 hypercall_msr.enable = 0; 337 wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 338 339 ret = hv_cpu_die(0); 340 return ret; 341 } 342 343 static void hv_resume(void) 344 { 345 union hv_x64_msr_hypercall_contents hypercall_msr; 346 int ret; 347 348 ret = hv_cpu_init(0); 349 WARN_ON(ret); 350 351 /* Re-enable the hypercall page */ 352 rdmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 353 hypercall_msr.enable = 1; 354 hypercall_msr.guest_physical_address = 355 vmalloc_to_pfn(hv_hypercall_pg_saved); 356 wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 357 358 hv_hypercall_pg = hv_hypercall_pg_saved; 359 hv_hypercall_pg_saved = NULL; 360 361 /* 362 * Reenlightenment notifications are disabled by hv_cpu_die(0), 363 * reenable them here if hv_reenlightenment_cb was previously set. 364 */ 365 if (hv_reenlightenment_cb) 366 set_hv_tscchange_cb(hv_reenlightenment_cb); 367 } 368 369 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */ 370 static struct syscore_ops hv_syscore_ops = { 371 .suspend = hv_suspend, 372 .resume = hv_resume, 373 }; 374 375 static void (* __initdata old_setup_percpu_clockev)(void); 376 377 static void __init hv_stimer_setup_percpu_clockev(void) 378 { 379 /* 380 * Ignore any errors in setting up stimer clockevents 381 * as we can run with the LAPIC timer as a fallback. 382 */ 383 (void)hv_stimer_alloc(false); 384 385 /* 386 * Still register the LAPIC timer, because the direct-mode STIMER is 387 * not supported by old versions of Hyper-V. This also allows users 388 * to switch to LAPIC timer via /sys, if they want to. 389 */ 390 if (old_setup_percpu_clockev) 391 old_setup_percpu_clockev(); 392 } 393 394 #if IS_ENABLED(CONFIG_HYPERV_VTL_MODE) 395 static u8 __init get_vtl(void) 396 { 397 u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS; 398 struct hv_input_get_vp_registers *input; 399 struct hv_output_get_vp_registers *output; 400 unsigned long flags; 401 u64 ret; 402 403 local_irq_save(flags); 404 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 405 output = *this_cpu_ptr(hyperv_pcpu_output_arg); 406 407 memset(input, 0, struct_size(input, names, 1)); 408 input->partition_id = HV_PARTITION_ID_SELF; 409 input->vp_index = HV_VP_INDEX_SELF; 410 input->input_vtl.as_uint8 = 0; 411 input->names[0] = HV_REGISTER_VSM_VP_STATUS; 412 413 ret = hv_do_hypercall(control, input, output); 414 if (hv_result_success(ret)) { 415 ret = output->values[0].reg8 & HV_X64_VTL_MASK; 416 } else { 417 pr_err("Failed to get VTL(error: %lld) exiting...\n", ret); 418 BUG(); 419 } 420 421 local_irq_restore(flags); 422 return ret; 423 } 424 #else 425 static inline u8 get_vtl(void) { return 0; } 426 #endif 427 428 /* 429 * This function is to be invoked early in the boot sequence after the 430 * hypervisor has been detected. 431 * 432 * 1. Setup the hypercall page. 433 * 2. Register Hyper-V specific clocksource. 434 * 3. Setup Hyper-V specific APIC entry points. 435 */ 436 void __init hyperv_init(void) 437 { 438 u64 guest_id; 439 union hv_x64_msr_hypercall_contents hypercall_msr; 440 int cpuhp; 441 442 if (x86_hyper_type != X86_HYPER_MS_HYPERV) 443 return; 444 445 if (hv_common_init()) 446 return; 447 448 /* 449 * The VP assist page is useless to a TDX guest: the only use we 450 * would have for it is lazy EOI, which can not be used with TDX. 451 */ 452 if (hv_isolation_type_tdx()) 453 hv_vp_assist_page = NULL; 454 else 455 hv_vp_assist_page = kcalloc(nr_cpu_ids, 456 sizeof(*hv_vp_assist_page), 457 GFP_KERNEL); 458 if (!hv_vp_assist_page) { 459 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; 460 461 if (!hv_isolation_type_tdx()) 462 goto common_free; 463 } 464 465 if (ms_hyperv.paravisor_present && hv_isolation_type_snp()) { 466 /* Negotiate GHCB Version. */ 467 if (!hv_ghcb_negotiate_protocol()) 468 hv_ghcb_terminate(SEV_TERM_SET_GEN, 469 GHCB_SEV_ES_PROT_UNSUPPORTED); 470 471 hv_ghcb_pg = alloc_percpu(union hv_ghcb *); 472 if (!hv_ghcb_pg) 473 goto free_vp_assist_page; 474 } 475 476 cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online", 477 hv_cpu_init, hv_cpu_die); 478 if (cpuhp < 0) 479 goto free_ghcb_page; 480 481 /* 482 * Setup the hypercall page and enable hypercalls. 483 * 1. Register the guest ID 484 * 2. Enable the hypercall and register the hypercall page 485 * 486 * A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg: 487 * when the hypercall input is a page, such a VM must pass a decrypted 488 * page to Hyper-V, e.g. hv_post_message() uses the per-CPU page 489 * hyperv_pcpu_input_arg, which is decrypted if no paravisor is present. 490 * 491 * A TDX VM with the paravisor uses hv_hypercall_pg for most hypercalls, 492 * which are handled by the paravisor and the VM must use an encrypted 493 * input page: in such a VM, the hyperv_pcpu_input_arg is encrypted and 494 * used in the hypercalls, e.g. see hv_mark_gpa_visibility() and 495 * hv_arch_irq_unmask(). Such a VM uses TDX GHCI for two hypercalls: 496 * 1. HVCALL_SIGNAL_EVENT: see vmbus_set_event() and _hv_do_fast_hypercall8(). 497 * 2. HVCALL_POST_MESSAGE: the input page must be a decrypted page, i.e. 498 * hv_post_message() in such a VM can't use the encrypted hyperv_pcpu_input_arg; 499 * instead, hv_post_message() uses the post_msg_page, which is decrypted 500 * in such a VM and is only used in such a VM. 501 */ 502 guest_id = hv_generate_guest_id(LINUX_VERSION_CODE); 503 wrmsrq(HV_X64_MSR_GUEST_OS_ID, guest_id); 504 505 /* With the paravisor, the VM must also write the ID via GHCB/GHCI */ 506 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id); 507 508 /* A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg */ 509 if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present) 510 goto skip_hypercall_pg_init; 511 512 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, 513 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, 514 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, 515 __builtin_return_address(0)); 516 if (hv_hypercall_pg == NULL) 517 goto clean_guest_os_id; 518 519 rdmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 520 hypercall_msr.enable = 1; 521 522 if (hv_root_partition()) { 523 struct page *pg; 524 void *src; 525 526 /* 527 * For the root partition, the hypervisor will set up its 528 * hypercall page. The hypervisor guarantees it will not show 529 * up in the root's address space. The root can't change the 530 * location of the hypercall page. 531 * 532 * Order is important here. We must enable the hypercall page 533 * so it is populated with code, then copy the code to an 534 * executable page. 535 */ 536 wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 537 538 pg = vmalloc_to_page(hv_hypercall_pg); 539 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE, 540 MEMREMAP_WB); 541 BUG_ON(!src); 542 memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE); 543 memunmap(src); 544 545 hv_remap_tsc_clocksource(); 546 } else { 547 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); 548 wrmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 549 } 550 551 skip_hypercall_pg_init: 552 /* 553 * Some versions of Hyper-V that provide IBT in guest VMs have a bug 554 * in that there's no ENDBR64 instruction at the entry to the 555 * hypercall page. Because hypercalls are invoked via an indirect call 556 * to the hypercall page, all hypercall attempts fail when IBT is 557 * enabled, and Linux panics. For such buggy versions, disable IBT. 558 * 559 * Fixed versions of Hyper-V always provide ENDBR64 on the hypercall 560 * page, so if future Linux kernel versions enable IBT for 32-bit 561 * builds, additional hypercall page hackery will be required here 562 * to provide an ENDBR32. 563 */ 564 #ifdef CONFIG_X86_KERNEL_IBT 565 if (cpu_feature_enabled(X86_FEATURE_IBT) && 566 *(u32 *)hv_hypercall_pg != gen_endbr()) { 567 setup_clear_cpu_cap(X86_FEATURE_IBT); 568 pr_warn("Disabling IBT because of Hyper-V bug\n"); 569 } 570 #endif 571 572 /* 573 * hyperv_init() is called before LAPIC is initialized: see 574 * apic_intr_mode_init() -> x86_platform.apic_post_init() and 575 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER 576 * depends on LAPIC, so hv_stimer_alloc() should be called from 577 * x86_init.timers.setup_percpu_clockev. 578 */ 579 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev; 580 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev; 581 582 hv_apic_init(); 583 584 x86_init.pci.arch_init = hv_pci_init; 585 586 register_syscore_ops(&hv_syscore_ops); 587 588 if (ms_hyperv.priv_high & HV_ACCESS_PARTITION_ID) 589 hv_get_partition_id(); 590 591 #ifdef CONFIG_PCI_MSI 592 /* 593 * If we're running as root, we want to create our own PCI MSI domain. 594 * We can't set this in hv_pci_init because that would be too late. 595 */ 596 if (hv_root_partition()) 597 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain; 598 #endif 599 600 /* Query the VMs extended capability once, so that it can be cached. */ 601 hv_query_ext_cap(0); 602 603 /* Find the VTL */ 604 ms_hyperv.vtl = get_vtl(); 605 606 if (ms_hyperv.vtl > 0) /* non default VTL */ 607 hv_vtl_early_init(); 608 609 return; 610 611 clean_guest_os_id: 612 wrmsrq(HV_X64_MSR_GUEST_OS_ID, 0); 613 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); 614 cpuhp_remove_state(CPUHP_AP_HYPERV_ONLINE); 615 free_ghcb_page: 616 free_percpu(hv_ghcb_pg); 617 free_vp_assist_page: 618 kfree(hv_vp_assist_page); 619 hv_vp_assist_page = NULL; 620 common_free: 621 hv_common_free(); 622 } 623 624 /* 625 * This routine is called before kexec/kdump, it does the required cleanup. 626 */ 627 void hyperv_cleanup(void) 628 { 629 union hv_x64_msr_hypercall_contents hypercall_msr; 630 union hv_reference_tsc_msr tsc_msr; 631 632 /* Reset our OS id */ 633 wrmsrq(HV_X64_MSR_GUEST_OS_ID, 0); 634 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); 635 636 /* 637 * Reset hypercall page reference before reset the page, 638 * let hypercall operations fail safely rather than 639 * panic the kernel for using invalid hypercall page 640 */ 641 hv_hypercall_pg = NULL; 642 643 /* Reset the hypercall page */ 644 hypercall_msr.as_uint64 = hv_get_msr(HV_X64_MSR_HYPERCALL); 645 hypercall_msr.enable = 0; 646 hv_set_msr(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 647 648 /* Reset the TSC page */ 649 tsc_msr.as_uint64 = hv_get_msr(HV_X64_MSR_REFERENCE_TSC); 650 tsc_msr.enable = 0; 651 hv_set_msr(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); 652 } 653 654 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die) 655 { 656 static bool panic_reported; 657 u64 guest_id; 658 659 if (in_die && !panic_on_oops) 660 return; 661 662 /* 663 * We prefer to report panic on 'die' chain as we have proper 664 * registers to report, but if we miss it (e.g. on BUG()) we need 665 * to report it on 'panic'. 666 */ 667 if (panic_reported) 668 return; 669 panic_reported = true; 670 671 rdmsrq(HV_X64_MSR_GUEST_OS_ID, guest_id); 672 673 wrmsrq(HV_X64_MSR_CRASH_P0, err); 674 wrmsrq(HV_X64_MSR_CRASH_P1, guest_id); 675 wrmsrq(HV_X64_MSR_CRASH_P2, regs->ip); 676 wrmsrq(HV_X64_MSR_CRASH_P3, regs->ax); 677 wrmsrq(HV_X64_MSR_CRASH_P4, regs->sp); 678 679 /* 680 * Let Hyper-V know there is crash data available 681 */ 682 wrmsrq(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); 683 } 684 EXPORT_SYMBOL_GPL(hyperv_report_panic); 685 686 bool hv_is_hyperv_initialized(void) 687 { 688 union hv_x64_msr_hypercall_contents hypercall_msr; 689 690 /* 691 * Ensure that we're really on Hyper-V, and not a KVM or Xen 692 * emulation of Hyper-V 693 */ 694 if (x86_hyper_type != X86_HYPER_MS_HYPERV) 695 return false; 696 697 /* A TDX VM with no paravisor uses TDX GHCI call rather than hv_hypercall_pg */ 698 if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present) 699 return true; 700 /* 701 * Verify that earlier initialization succeeded by checking 702 * that the hypercall page is setup 703 */ 704 hypercall_msr.as_uint64 = 0; 705 rdmsrq(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 706 707 return hypercall_msr.enable; 708 } 709 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); 710