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 <asm/set_memory.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; 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 rdmsrl(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 && hv_isolation_type_en_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 wrmsrl(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 rdmsrl(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 rdmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 158 emu_status.inprogress = 0; 159 wrmsrl(HV_X64_MSR_TSC_EMULATION_STATUS, *(u64 *)&emu_status); 160 161 rdmsrl(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 ack_APIC_irq(); 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("Hyper-V: 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 wrmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *((u64 *)&re_ctrl)); 208 wrmsrl(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 rdmsrl(HV_X64_MSR_REENLIGHTENMENT_CONTROL, *(u64 *)&re_ctrl); 222 re_ctrl.enabled = 0; 223 wrmsrl(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 rdmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 256 msr.enable = 0; 257 } 258 wrmsrl(HV_X64_MSR_VP_ASSIST_PAGE, msr.as_uint64); 259 } 260 261 if (hv_reenlightenment_cb == NULL) 262 return 0; 263 264 rdmsrl(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 wrmsrl(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 int gen2vm = efi_enabled(EFI_BOOT); 287 288 /* 289 * For Generation-2 VM, we exit from pci_arch_init() by returning 0. 290 * The purpose is to suppress the harmless warning: 291 * "PCI: Fatal: No config space access function found" 292 */ 293 if (gen2vm) 294 return 0; 295 296 /* For Generation-1 VM, we'll proceed in pci_arch_init(). */ 297 return 1; 298 } 299 300 static int hv_suspend(void) 301 { 302 union hv_x64_msr_hypercall_contents hypercall_msr; 303 int ret; 304 305 if (hv_root_partition) 306 return -EPERM; 307 308 /* 309 * Reset the hypercall page as it is going to be invalidated 310 * across hibernation. Setting hv_hypercall_pg to NULL ensures 311 * that any subsequent hypercall operation fails safely instead of 312 * crashing due to an access of an invalid page. The hypercall page 313 * pointer is restored on resume. 314 */ 315 hv_hypercall_pg_saved = hv_hypercall_pg; 316 hv_hypercall_pg = NULL; 317 318 /* Disable the hypercall page in the hypervisor */ 319 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 320 hypercall_msr.enable = 0; 321 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 322 323 ret = hv_cpu_die(0); 324 return ret; 325 } 326 327 static void hv_resume(void) 328 { 329 union hv_x64_msr_hypercall_contents hypercall_msr; 330 int ret; 331 332 ret = hv_cpu_init(0); 333 WARN_ON(ret); 334 335 /* Re-enable the hypercall page */ 336 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 337 hypercall_msr.enable = 1; 338 hypercall_msr.guest_physical_address = 339 vmalloc_to_pfn(hv_hypercall_pg_saved); 340 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 341 342 hv_hypercall_pg = hv_hypercall_pg_saved; 343 hv_hypercall_pg_saved = NULL; 344 345 /* 346 * Reenlightenment notifications are disabled by hv_cpu_die(0), 347 * reenable them here if hv_reenlightenment_cb was previously set. 348 */ 349 if (hv_reenlightenment_cb) 350 set_hv_tscchange_cb(hv_reenlightenment_cb); 351 } 352 353 /* Note: when the ops are called, only CPU0 is online and IRQs are disabled. */ 354 static struct syscore_ops hv_syscore_ops = { 355 .suspend = hv_suspend, 356 .resume = hv_resume, 357 }; 358 359 static void (* __initdata old_setup_percpu_clockev)(void); 360 361 static void __init hv_stimer_setup_percpu_clockev(void) 362 { 363 /* 364 * Ignore any errors in setting up stimer clockevents 365 * as we can run with the LAPIC timer as a fallback. 366 */ 367 (void)hv_stimer_alloc(false); 368 369 /* 370 * Still register the LAPIC timer, because the direct-mode STIMER is 371 * not supported by old versions of Hyper-V. This also allows users 372 * to switch to LAPIC timer via /sys, if they want to. 373 */ 374 if (old_setup_percpu_clockev) 375 old_setup_percpu_clockev(); 376 } 377 378 static void __init hv_get_partition_id(void) 379 { 380 struct hv_get_partition_id *output_page; 381 u64 status; 382 unsigned long flags; 383 384 local_irq_save(flags); 385 output_page = *this_cpu_ptr(hyperv_pcpu_output_arg); 386 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output_page); 387 if (!hv_result_success(status)) { 388 /* No point in proceeding if this failed */ 389 pr_err("Failed to get partition ID: %lld\n", status); 390 BUG(); 391 } 392 hv_current_partition_id = output_page->partition_id; 393 local_irq_restore(flags); 394 } 395 396 static u8 __init get_vtl(void) 397 { 398 u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS; 399 struct hv_get_vp_registers_input *input; 400 struct hv_get_vp_registers_output *output; 401 unsigned long flags; 402 u64 ret; 403 404 local_irq_save(flags); 405 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 406 output = (struct hv_get_vp_registers_output *)input; 407 408 memset(input, 0, struct_size(input, element, 1)); 409 input->header.partitionid = HV_PARTITION_ID_SELF; 410 input->header.vpindex = HV_VP_INDEX_SELF; 411 input->header.inputvtl = 0; 412 input->element[0].name0 = HV_X64_REGISTER_VSM_VP_STATUS; 413 414 ret = hv_do_hypercall(control, input, output); 415 if (hv_result_success(ret)) { 416 ret = output->as64.low & HV_X64_VTL_MASK; 417 } else { 418 pr_err("Failed to get VTL(%lld) and set VTL to zero by default.\n", ret); 419 ret = 0; 420 } 421 422 local_irq_restore(flags); 423 return ret; 424 } 425 426 /* 427 * This function is to be invoked early in the boot sequence after the 428 * hypervisor has been detected. 429 * 430 * 1. Setup the hypercall page. 431 * 2. Register Hyper-V specific clocksource. 432 * 3. Setup Hyper-V specific APIC entry points. 433 */ 434 void __init hyperv_init(void) 435 { 436 u64 guest_id; 437 union hv_x64_msr_hypercall_contents hypercall_msr; 438 int cpuhp; 439 440 if (x86_hyper_type != X86_HYPER_MS_HYPERV) 441 return; 442 443 if (hv_common_init()) 444 return; 445 446 /* 447 * The VP assist page is useless to a TDX guest: the only use we 448 * would have for it is lazy EOI, which can not be used with TDX. 449 */ 450 if (hv_isolation_type_tdx()) 451 hv_vp_assist_page = NULL; 452 else 453 hv_vp_assist_page = kcalloc(num_possible_cpus(), 454 sizeof(*hv_vp_assist_page), 455 GFP_KERNEL); 456 if (!hv_vp_assist_page) { 457 ms_hyperv.hints &= ~HV_X64_ENLIGHTENED_VMCS_RECOMMENDED; 458 459 if (!hv_isolation_type_tdx()) 460 goto common_free; 461 } 462 463 if (hv_isolation_type_snp()) { 464 /* Negotiate GHCB Version. */ 465 if (!hv_ghcb_negotiate_protocol()) 466 hv_ghcb_terminate(SEV_TERM_SET_GEN, 467 GHCB_SEV_ES_PROT_UNSUPPORTED); 468 469 hv_ghcb_pg = alloc_percpu(union hv_ghcb *); 470 if (!hv_ghcb_pg) 471 goto free_vp_assist_page; 472 } 473 474 cpuhp = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "x86/hyperv_init:online", 475 hv_cpu_init, hv_cpu_die); 476 if (cpuhp < 0) 477 goto free_ghcb_page; 478 479 /* 480 * Setup the hypercall page and enable hypercalls. 481 * 1. Register the guest ID 482 * 2. Enable the hypercall and register the hypercall page 483 * 484 * A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg: 485 * when the hypercall input is a page, such a VM must pass a decrypted 486 * page to Hyper-V, e.g. hv_post_message() uses the per-CPU page 487 * hyperv_pcpu_input_arg, which is decrypted if no paravisor is present. 488 * 489 * A TDX VM with the paravisor uses hv_hypercall_pg for most hypercalls, 490 * which are handled by the paravisor and the VM must use an encrypted 491 * input page: in such a VM, the hyperv_pcpu_input_arg is encrypted and 492 * used in the hypercalls, e.g. see hv_mark_gpa_visibility() and 493 * hv_arch_irq_unmask(). Such a VM uses TDX GHCI for two hypercalls: 494 * 1. HVCALL_SIGNAL_EVENT: see vmbus_set_event() and _hv_do_fast_hypercall8(). 495 * 2. HVCALL_POST_MESSAGE: the input page must be a decrypted page, i.e. 496 * hv_post_message() in such a VM can't use the encrypted hyperv_pcpu_input_arg; 497 * instead, hv_post_message() uses the post_msg_page, which is decrypted 498 * in such a VM and is only used in such a VM. 499 */ 500 guest_id = hv_generate_guest_id(LINUX_VERSION_CODE); 501 wrmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); 502 503 /* With the paravisor, the VM must also write the ID via GHCB/GHCI */ 504 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, guest_id); 505 506 /* A TDX VM with no paravisor only uses TDX GHCI rather than hv_hypercall_pg */ 507 if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present) 508 goto skip_hypercall_pg_init; 509 510 hv_hypercall_pg = __vmalloc_node_range(PAGE_SIZE, 1, VMALLOC_START, 511 VMALLOC_END, GFP_KERNEL, PAGE_KERNEL_ROX, 512 VM_FLUSH_RESET_PERMS, NUMA_NO_NODE, 513 __builtin_return_address(0)); 514 if (hv_hypercall_pg == NULL) 515 goto clean_guest_os_id; 516 517 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 518 hypercall_msr.enable = 1; 519 520 if (hv_root_partition) { 521 struct page *pg; 522 void *src; 523 524 /* 525 * For the root partition, the hypervisor will set up its 526 * hypercall page. The hypervisor guarantees it will not show 527 * up in the root's address space. The root can't change the 528 * location of the hypercall page. 529 * 530 * Order is important here. We must enable the hypercall page 531 * so it is populated with code, then copy the code to an 532 * executable page. 533 */ 534 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 535 536 pg = vmalloc_to_page(hv_hypercall_pg); 537 src = memremap(hypercall_msr.guest_physical_address << PAGE_SHIFT, PAGE_SIZE, 538 MEMREMAP_WB); 539 BUG_ON(!src); 540 memcpy_to_page(pg, 0, src, HV_HYP_PAGE_SIZE); 541 memunmap(src); 542 543 hv_remap_tsc_clocksource(); 544 } else { 545 hypercall_msr.guest_physical_address = vmalloc_to_pfn(hv_hypercall_pg); 546 wrmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 547 } 548 549 skip_hypercall_pg_init: 550 /* 551 * hyperv_init() is called before LAPIC is initialized: see 552 * apic_intr_mode_init() -> x86_platform.apic_post_init() and 553 * apic_bsp_setup() -> setup_local_APIC(). The direct-mode STIMER 554 * depends on LAPIC, so hv_stimer_alloc() should be called from 555 * x86_init.timers.setup_percpu_clockev. 556 */ 557 old_setup_percpu_clockev = x86_init.timers.setup_percpu_clockev; 558 x86_init.timers.setup_percpu_clockev = hv_stimer_setup_percpu_clockev; 559 560 hv_apic_init(); 561 562 x86_init.pci.arch_init = hv_pci_init; 563 564 register_syscore_ops(&hv_syscore_ops); 565 566 hyperv_init_cpuhp = cpuhp; 567 568 if (cpuid_ebx(HYPERV_CPUID_FEATURES) & HV_ACCESS_PARTITION_ID) 569 hv_get_partition_id(); 570 571 BUG_ON(hv_root_partition && hv_current_partition_id == ~0ull); 572 573 #ifdef CONFIG_PCI_MSI 574 /* 575 * If we're running as root, we want to create our own PCI MSI domain. 576 * We can't set this in hv_pci_init because that would be too late. 577 */ 578 if (hv_root_partition) 579 x86_init.irqs.create_pci_msi_domain = hv_create_pci_msi_domain; 580 #endif 581 582 /* Query the VMs extended capability once, so that it can be cached. */ 583 hv_query_ext_cap(0); 584 585 /* Find the VTL */ 586 if (hv_isolation_type_en_snp()) 587 ms_hyperv.vtl = get_vtl(); 588 589 return; 590 591 clean_guest_os_id: 592 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); 593 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); 594 cpuhp_remove_state(cpuhp); 595 free_ghcb_page: 596 free_percpu(hv_ghcb_pg); 597 free_vp_assist_page: 598 kfree(hv_vp_assist_page); 599 hv_vp_assist_page = NULL; 600 common_free: 601 hv_common_free(); 602 } 603 604 /* 605 * This routine is called before kexec/kdump, it does the required cleanup. 606 */ 607 void hyperv_cleanup(void) 608 { 609 union hv_x64_msr_hypercall_contents hypercall_msr; 610 union hv_reference_tsc_msr tsc_msr; 611 612 /* Reset our OS id */ 613 wrmsrl(HV_X64_MSR_GUEST_OS_ID, 0); 614 hv_ivm_msr_write(HV_X64_MSR_GUEST_OS_ID, 0); 615 616 /* 617 * Reset hypercall page reference before reset the page, 618 * let hypercall operations fail safely rather than 619 * panic the kernel for using invalid hypercall page 620 */ 621 hv_hypercall_pg = NULL; 622 623 /* Reset the hypercall page */ 624 hypercall_msr.as_uint64 = hv_get_register(HV_X64_MSR_HYPERCALL); 625 hypercall_msr.enable = 0; 626 hv_set_register(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 627 628 /* Reset the TSC page */ 629 tsc_msr.as_uint64 = hv_get_register(HV_X64_MSR_REFERENCE_TSC); 630 tsc_msr.enable = 0; 631 hv_set_register(HV_X64_MSR_REFERENCE_TSC, tsc_msr.as_uint64); 632 } 633 634 void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die) 635 { 636 static bool panic_reported; 637 u64 guest_id; 638 639 if (in_die && !panic_on_oops) 640 return; 641 642 /* 643 * We prefer to report panic on 'die' chain as we have proper 644 * registers to report, but if we miss it (e.g. on BUG()) we need 645 * to report it on 'panic'. 646 */ 647 if (panic_reported) 648 return; 649 panic_reported = true; 650 651 rdmsrl(HV_X64_MSR_GUEST_OS_ID, guest_id); 652 653 wrmsrl(HV_X64_MSR_CRASH_P0, err); 654 wrmsrl(HV_X64_MSR_CRASH_P1, guest_id); 655 wrmsrl(HV_X64_MSR_CRASH_P2, regs->ip); 656 wrmsrl(HV_X64_MSR_CRASH_P3, regs->ax); 657 wrmsrl(HV_X64_MSR_CRASH_P4, regs->sp); 658 659 /* 660 * Let Hyper-V know there is crash data available 661 */ 662 wrmsrl(HV_X64_MSR_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY); 663 } 664 EXPORT_SYMBOL_GPL(hyperv_report_panic); 665 666 bool hv_is_hyperv_initialized(void) 667 { 668 union hv_x64_msr_hypercall_contents hypercall_msr; 669 670 /* 671 * Ensure that we're really on Hyper-V, and not a KVM or Xen 672 * emulation of Hyper-V 673 */ 674 if (x86_hyper_type != X86_HYPER_MS_HYPERV) 675 return false; 676 677 /* A TDX VM with no paravisor uses TDX GHCI call rather than hv_hypercall_pg */ 678 if (hv_isolation_type_tdx() && !ms_hyperv.paravisor_present) 679 return true; 680 /* 681 * Verify that earlier initialization succeeded by checking 682 * that the hypercall page is setup 683 */ 684 hypercall_msr.as_uint64 = 0; 685 rdmsrl(HV_X64_MSR_HYPERCALL, hypercall_msr.as_uint64); 686 687 return hypercall_msr.enable; 688 } 689 EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized); 690