1 // SPDX-License-Identifier: GPL-2.0 2 3 /* 4 * Architecture neutral utility routines for interacting with 5 * Hyper-V. This file is specifically for code that must be 6 * built-in to the kernel image when CONFIG_HYPERV is set 7 * (vs. being in a module) because it is called from architecture 8 * specific code under arch/. 9 * 10 * Copyright (C) 2021, Microsoft, Inc. 11 * 12 * Author : Michael Kelley <mikelley@microsoft.com> 13 */ 14 15 #include <linux/types.h> 16 #include <linux/acpi.h> 17 #include <linux/export.h> 18 #include <linux/bitfield.h> 19 #include <linux/cpumask.h> 20 #include <linux/sched/task_stack.h> 21 #include <linux/panic_notifier.h> 22 #include <linux/ptrace.h> 23 #include <linux/random.h> 24 #include <linux/rhashtable.h> 25 #include <linux/efi.h> 26 #include <linux/kdebug.h> 27 #include <linux/kmsg_dump.h> 28 #include <linux/sizes.h> 29 #include <linux/slab.h> 30 #include <linux/dma-map-ops.h> 31 #include <linux/set_memory.h> 32 #include <hyperv/hvhdk.h> 33 #include <asm/mshyperv.h> 34 35 u64 hv_current_partition_id = HV_PARTITION_ID_SELF; 36 EXPORT_SYMBOL_GPL(hv_current_partition_id); 37 38 enum hv_partition_type hv_curr_partition_type; 39 EXPORT_SYMBOL_GPL(hv_curr_partition_type); 40 41 /* 42 * ms_hyperv and hv_nested are defined here with other 43 * Hyper-V specific globals so they are shared across all architectures and are 44 * built only when CONFIG_HYPERV is defined. But on x86, 45 * ms_hyperv_init_platform() is built even when CONFIG_HYPERV is not 46 * defined, and it uses these three variables. So mark them as __weak 47 * here, allowing for an overriding definition in the module containing 48 * ms_hyperv_init_platform(). 49 */ 50 bool __weak hv_nested; 51 EXPORT_SYMBOL_GPL(hv_nested); 52 53 struct ms_hyperv_info __weak ms_hyperv; 54 EXPORT_SYMBOL_GPL(ms_hyperv); 55 56 u32 *hv_vp_index; 57 EXPORT_SYMBOL_GPL(hv_vp_index); 58 59 u32 hv_max_vp_index; 60 EXPORT_SYMBOL_GPL(hv_max_vp_index); 61 62 void * __percpu *hyperv_pcpu_input_arg; 63 EXPORT_SYMBOL_GPL(hyperv_pcpu_input_arg); 64 65 void * __percpu *hyperv_pcpu_output_arg; 66 EXPORT_SYMBOL_GPL(hyperv_pcpu_output_arg); 67 68 static void hv_kmsg_dump_unregister(void); 69 70 static struct ctl_table_header *hv_ctl_table_hdr; 71 72 /* 73 * Per-cpu array holding the tail pointer for the SynIC event ring buffer 74 * for each SINT. 75 * 76 * We cannot maintain this in mshv driver because the tail pointer should 77 * persist even if the mshv driver is unloaded. 78 */ 79 u8 * __percpu *hv_synic_eventring_tail; 80 EXPORT_SYMBOL_GPL(hv_synic_eventring_tail); 81 82 struct hv_pci_busdata { 83 int pci_domain_nr; 84 u32 logical_dev_id_prefix; 85 struct rhash_head node; 86 struct rcu_head rcu; 87 }; 88 89 static struct rhashtable hv_pci_bus_ht; 90 static bool hv_pci_bus_ht_initialized; 91 92 static const struct rhashtable_params hv_pci_bus_ht_params = { 93 .key_len = sizeof_field(struct hv_pci_busdata, pci_domain_nr), 94 .key_offset = offsetof(struct hv_pci_busdata, pci_domain_nr), 95 .head_offset = offsetof(struct hv_pci_busdata, node), 96 }; 97 98 /* 99 * Hyper-V specific initialization and shutdown code that is 100 * common across all architectures. Called from architecture 101 * specific initialization functions. 102 */ 103 104 void __init hv_common_free(void) 105 { 106 if (hv_pci_bus_ht_initialized) { 107 rhashtable_destroy(&hv_pci_bus_ht); 108 hv_pci_bus_ht_initialized = false; 109 } 110 111 unregister_sysctl_table(hv_ctl_table_hdr); 112 hv_ctl_table_hdr = NULL; 113 114 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) 115 hv_kmsg_dump_unregister(); 116 117 kfree(hv_vp_index); 118 hv_vp_index = NULL; 119 120 free_percpu(hyperv_pcpu_output_arg); 121 hyperv_pcpu_output_arg = NULL; 122 123 free_percpu(hyperv_pcpu_input_arg); 124 hyperv_pcpu_input_arg = NULL; 125 126 free_percpu(hv_synic_eventring_tail); 127 hv_synic_eventring_tail = NULL; 128 } 129 130 static void *hv_panic_page; 131 132 /* 133 * Boolean to control whether to report panic messages over Hyper-V. 134 * 135 * It can be set via /proc/sys/kernel/hyperv_record_panic_msg 136 */ 137 static int sysctl_record_panic_msg = 1; 138 139 /* 140 * sysctl option to allow the user to control whether kmsg data should be 141 * reported to Hyper-V on panic. 142 */ 143 static const struct ctl_table hv_ctl_table[] = { 144 { 145 .procname = "hyperv_record_panic_msg", 146 .data = &sysctl_record_panic_msg, 147 .maxlen = sizeof(int), 148 .mode = 0644, 149 .proc_handler = proc_dointvec_minmax, 150 .extra1 = SYSCTL_ZERO, 151 .extra2 = SYSCTL_ONE 152 }, 153 }; 154 155 static int hv_die_panic_notify_crash(struct notifier_block *self, 156 unsigned long val, void *args); 157 158 static struct notifier_block hyperv_die_report_block = { 159 .notifier_call = hv_die_panic_notify_crash, 160 }; 161 162 static struct notifier_block hyperv_panic_report_block = { 163 .notifier_call = hv_die_panic_notify_crash, 164 }; 165 166 /* 167 * The following callback works both as die and panic notifier; its 168 * goal is to provide panic information to the hypervisor unless the 169 * kmsg dumper is used [see hv_kmsg_dump()], which provides more 170 * information but isn't always available. 171 * 172 * Notice that both the panic/die report notifiers are registered only 173 * if we have the capability HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE set. 174 */ 175 static int hv_die_panic_notify_crash(struct notifier_block *self, 176 unsigned long val, void *args) 177 { 178 struct pt_regs *regs; 179 bool is_die; 180 181 /* Don't notify Hyper-V unless we have a die oops event or panic. */ 182 if (self == &hyperv_panic_report_block) { 183 is_die = false; 184 regs = current_pt_regs(); 185 } else { /* die event */ 186 if (val != DIE_OOPS) 187 return NOTIFY_DONE; 188 189 is_die = true; 190 regs = ((struct die_args *)args)->regs; 191 } 192 193 /* 194 * Hyper-V should be notified only once about a panic/die. If we will 195 * be calling hv_kmsg_dump() later with kmsg data, don't do the 196 * notification here. 197 */ 198 if (!sysctl_record_panic_msg || !hv_panic_page) 199 hyperv_report_panic(regs, val, is_die); 200 201 return NOTIFY_DONE; 202 } 203 204 /* 205 * Callback from kmsg_dump. Grab as much as possible from the end of the kmsg 206 * buffer and call into Hyper-V to transfer the data. 207 */ 208 static void hv_kmsg_dump(struct kmsg_dumper *dumper, 209 struct kmsg_dump_detail *detail) 210 { 211 struct kmsg_dump_iter iter; 212 size_t bytes_written; 213 214 /* We are only interested in panics. */ 215 if (detail->reason != KMSG_DUMP_PANIC || !sysctl_record_panic_msg) 216 return; 217 218 /* 219 * Write dump contents to the page. No need to synchronize; panic should 220 * be single-threaded. Ignore failures from kmsg_dump_get_buffer() since 221 * panic notification should be done even if there is no message data. 222 * Don't assume bytes_written is set in case of failure, so initialize it. 223 */ 224 kmsg_dump_rewind(&iter); 225 bytes_written = 0; 226 (void)kmsg_dump_get_buffer(&iter, false, hv_panic_page, HV_HYP_PAGE_SIZE, 227 &bytes_written); 228 229 /* 230 * P3 to contain the physical address of the panic page & P4 to 231 * contain the size of the panic data in that page. Rest of the 232 * registers are no-op when the NOTIFY_MSG flag is set. 233 */ 234 hv_set_msr(HV_MSR_CRASH_P0, 0); 235 hv_set_msr(HV_MSR_CRASH_P1, 0); 236 hv_set_msr(HV_MSR_CRASH_P2, 0); 237 hv_set_msr(HV_MSR_CRASH_P3, bytes_written ? virt_to_phys(hv_panic_page) : 0); 238 hv_set_msr(HV_MSR_CRASH_P4, bytes_written); 239 240 /* 241 * Let Hyper-V know there is crash data available along with 242 * the panic message. 243 */ 244 hv_set_msr(HV_MSR_CRASH_CTL, 245 (HV_CRASH_CTL_CRASH_NOTIFY | 246 HV_CRASH_CTL_CRASH_NOTIFY_MSG)); 247 } 248 249 static struct kmsg_dumper hv_kmsg_dumper = { 250 .dump = hv_kmsg_dump, 251 }; 252 253 static void hv_kmsg_dump_unregister(void) 254 { 255 kmsg_dump_unregister(&hv_kmsg_dumper); 256 unregister_die_notifier(&hyperv_die_report_block); 257 atomic_notifier_chain_unregister(&panic_notifier_list, 258 &hyperv_panic_report_block); 259 260 kfree(hv_panic_page); 261 hv_panic_page = NULL; 262 } 263 264 static void hv_kmsg_dump_register(void) 265 { 266 int ret; 267 268 hv_panic_page = kzalloc(HV_HYP_PAGE_SIZE, GFP_KERNEL); 269 if (!hv_panic_page) { 270 pr_err("Hyper-V: panic message page memory allocation failed\n"); 271 return; 272 } 273 274 ret = kmsg_dump_register(&hv_kmsg_dumper); 275 if (ret) { 276 pr_err("Hyper-V: kmsg dump register error 0x%x\n", ret); 277 kfree(hv_panic_page); 278 hv_panic_page = NULL; 279 } 280 } 281 282 static inline bool hv_output_page_exists(void) 283 { 284 return hv_parent_partition() || IS_ENABLED(CONFIG_HYPERV_VTL_MODE); 285 } 286 287 void __init hv_get_partition_id(void) 288 { 289 struct hv_output_get_partition_id *output; 290 unsigned long flags; 291 u64 status, pt_id; 292 293 local_irq_save(flags); 294 output = *this_cpu_ptr(hyperv_pcpu_input_arg); 295 status = hv_do_hypercall(HVCALL_GET_PARTITION_ID, NULL, output); 296 pt_id = output->partition_id; 297 local_irq_restore(flags); 298 299 if (hv_result_success(status)) 300 hv_current_partition_id = pt_id; 301 else 302 pr_err("Hyper-V: failed to get partition ID: %#x\n", 303 hv_result(status)); 304 } 305 #if IS_ENABLED(CONFIG_HYPERV_VTL_MODE) 306 u8 __init get_vtl(void) 307 { 308 u64 control = HV_HYPERCALL_REP_COMP_1 | HVCALL_GET_VP_REGISTERS; 309 struct hv_input_get_vp_registers *input; 310 struct hv_output_get_vp_registers *output; 311 unsigned long flags; 312 u64 ret; 313 314 local_irq_save(flags); 315 input = *this_cpu_ptr(hyperv_pcpu_input_arg); 316 output = *this_cpu_ptr(hyperv_pcpu_output_arg); 317 318 memset(input, 0, struct_size(input, names, 1)); 319 input->partition_id = HV_PARTITION_ID_SELF; 320 input->vp_index = HV_VP_INDEX_SELF; 321 input->input_vtl.as_uint8 = 0; 322 input->names[0] = HV_REGISTER_VSM_VP_STATUS; 323 324 ret = hv_do_hypercall(control, input, output); 325 if (hv_result_success(ret)) { 326 ret = output->values[0].reg8 & HV_VTL_MASK; 327 } else { 328 pr_err("Failed to get VTL(error: %lld) exiting...\n", ret); 329 BUG(); 330 } 331 332 local_irq_restore(flags); 333 return ret; 334 } 335 #endif 336 337 int __init hv_common_init(void) 338 { 339 int i; 340 int ret; 341 union hv_hypervisor_version_info version; 342 343 /* Get information about the Microsoft Hypervisor version */ 344 if (!hv_get_hypervisor_version(&version)) 345 pr_info("Hyper-V: Hypervisor Build %d.%d.%d.%d-%d-%d\n", 346 version.major_version, version.minor_version, 347 version.build_number, version.service_number, 348 version.service_pack, version.service_branch); 349 350 if (hv_is_isolation_supported()) 351 sysctl_record_panic_msg = 0; 352 353 /* 354 * Hyper-V expects to get crash register data or kmsg when 355 * crash enlightment is available and system crashes. Set 356 * crash_kexec_post_notifiers to be true to make sure that 357 * calling crash enlightment interface before running kdump 358 * kernel. 359 */ 360 if (ms_hyperv.misc_features & HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE) { 361 u64 hyperv_crash_ctl; 362 363 crash_kexec_post_notifiers = true; 364 pr_info("Hyper-V: enabling crash_kexec_post_notifiers\n"); 365 366 /* 367 * Panic message recording (sysctl_record_panic_msg) 368 * is enabled by default in non-isolated guests and 369 * disabled by default in isolated guests; the panic 370 * message recording won't be available in isolated 371 * guests should the following registration fail. 372 */ 373 hv_ctl_table_hdr = register_sysctl("kernel", hv_ctl_table); 374 if (!hv_ctl_table_hdr) 375 pr_err("Hyper-V: sysctl table register error"); 376 377 /* 378 * Register for panic kmsg callback only if the right 379 * capability is supported by the hypervisor. 380 */ 381 hyperv_crash_ctl = hv_get_msr(HV_MSR_CRASH_CTL); 382 if (hyperv_crash_ctl & HV_CRASH_CTL_CRASH_NOTIFY_MSG) 383 hv_kmsg_dump_register(); 384 385 register_die_notifier(&hyperv_die_report_block); 386 atomic_notifier_chain_register(&panic_notifier_list, 387 &hyperv_panic_report_block); 388 } 389 390 /* 391 * Allocate the per-CPU state for the hypercall input arg. 392 * If this allocation fails, we will not be able to setup 393 * (per-CPU) hypercall input page and thus this failure is 394 * fatal on Hyper-V. 395 */ 396 hyperv_pcpu_input_arg = alloc_percpu(void *); 397 BUG_ON(!hyperv_pcpu_input_arg); 398 399 /* Allocate the per-CPU state for output arg for root */ 400 if (hv_output_page_exists()) { 401 hyperv_pcpu_output_arg = alloc_percpu(void *); 402 BUG_ON(!hyperv_pcpu_output_arg); 403 } 404 405 if (hv_parent_partition()) { 406 hv_synic_eventring_tail = alloc_percpu(u8 *); 407 BUG_ON(!hv_synic_eventring_tail); 408 } 409 410 hv_vp_index = kmalloc_array(nr_cpu_ids, sizeof(*hv_vp_index), 411 GFP_KERNEL); 412 if (!hv_vp_index) { 413 hv_common_free(); 414 return -ENOMEM; 415 } 416 417 for (i = 0; i < nr_cpu_ids; i++) 418 hv_vp_index[i] = VP_INVAL; 419 420 ret = rhashtable_init(&hv_pci_bus_ht, &hv_pci_bus_ht_params); 421 if (ret) { 422 hv_common_free(); 423 return ret; 424 } 425 hv_pci_bus_ht_initialized = true; 426 427 return 0; 428 } 429 430 void __init ms_hyperv_late_init(void) 431 { 432 struct acpi_table_header *header; 433 acpi_status status; 434 u8 *randomdata; 435 u32 length, i; 436 437 /* 438 * Seed the Linux random number generator with entropy provided by 439 * the Hyper-V host in ACPI table OEM0. 440 */ 441 if (!IS_ENABLED(CONFIG_ACPI)) 442 return; 443 444 status = acpi_get_table("OEM0", 0, &header); 445 if (ACPI_FAILURE(status) || !header) 446 return; 447 448 /* 449 * Since the "OEM0" table name is for OEM specific usage, verify 450 * that what we're seeing purports to be from Microsoft. 451 */ 452 if (strncmp(header->oem_table_id, "MICROSFT", 8)) 453 goto error; 454 455 /* 456 * Ensure the length is reasonable. Requiring at least 8 bytes and 457 * no more than 4K bytes is somewhat arbitrary and just protects 458 * against a malformed table. Hyper-V currently provides 64 bytes, 459 * but allow for a change in a later version. 460 */ 461 if (header->length < sizeof(*header) + 8 || 462 header->length > sizeof(*header) + SZ_4K) 463 goto error; 464 465 length = header->length - sizeof(*header); 466 randomdata = (u8 *)(header + 1); 467 468 pr_debug("Hyper-V: Seeding rng with %d random bytes from ACPI table OEM0\n", 469 length); 470 471 add_bootloader_randomness(randomdata, length); 472 473 /* 474 * To prevent the seed data from being visible in /sys/firmware/acpi, 475 * zero out the random data in the ACPI table and fixup the checksum. 476 * The zero'ing is done out of an abundance of caution in avoiding 477 * potential security risks to the rng. Similarly, reset the table 478 * length to just the header size so that a subsequent kexec doesn't 479 * try to use the zero'ed out random data. 480 */ 481 for (i = 0; i < length; i++) { 482 header->checksum += randomdata[i]; 483 randomdata[i] = 0; 484 } 485 486 for (i = 0; i < sizeof(header->length); i++) 487 header->checksum += ((u8 *)&header->length)[i]; 488 header->length = sizeof(*header); 489 for (i = 0; i < sizeof(header->length); i++) 490 header->checksum -= ((u8 *)&header->length)[i]; 491 492 error: 493 acpi_put_table(header); 494 } 495 496 /* 497 * Hyper-V specific initialization and die code for 498 * individual CPUs that is common across all architectures. 499 * Called by the CPU hotplug mechanism. 500 */ 501 502 int hv_common_cpu_init(unsigned int cpu) 503 { 504 void **inputarg, **outputarg; 505 u8 **synic_eventring_tail; 506 u64 msr_vp_index; 507 gfp_t flags; 508 const int pgcount = hv_output_page_exists() ? 2 : 1; 509 void *mem; 510 int ret = 0; 511 512 /* hv_cpu_init() can be called with IRQs disabled from hv_resume() */ 513 flags = irqs_disabled() ? GFP_ATOMIC : GFP_KERNEL; 514 515 inputarg = (void **)this_cpu_ptr(hyperv_pcpu_input_arg); 516 517 /* 518 * The per-cpu memory is already allocated if this CPU was previously 519 * online and then taken offline 520 */ 521 if (!*inputarg) { 522 mem = kmalloc_array(pgcount, HV_HYP_PAGE_SIZE, flags); 523 if (!mem) 524 return -ENOMEM; 525 526 if (hv_output_page_exists()) { 527 outputarg = (void **)this_cpu_ptr(hyperv_pcpu_output_arg); 528 *outputarg = (char *)mem + HV_HYP_PAGE_SIZE; 529 } 530 531 if (!ms_hyperv.paravisor_present && 532 (hv_isolation_type_snp() || hv_isolation_type_tdx())) { 533 ret = set_memory_decrypted((unsigned long)mem, pgcount); 534 if (ret) { 535 /* It may be unsafe to free 'mem' */ 536 return ret; 537 } 538 539 memset(mem, 0x00, pgcount * HV_HYP_PAGE_SIZE); 540 } 541 542 /* 543 * In a fully enlightened TDX/SNP VM with more than 64 VPs, if 544 * hyperv_pcpu_input_arg is not NULL, set_memory_decrypted() -> 545 * ... -> cpa_flush()-> ... -> __send_ipi_mask_ex() tries to 546 * use hyperv_pcpu_input_arg as the hypercall input page, which 547 * must be a decrypted page in such a VM, but the page is still 548 * encrypted before set_memory_decrypted() returns. Fix this by 549 * setting *inputarg after the above set_memory_decrypted(): if 550 * hyperv_pcpu_input_arg is NULL, __send_ipi_mask_ex() returns 551 * HV_STATUS_INVALID_PARAMETER immediately, and the function 552 * hv_send_ipi_mask() falls back to orig_apic.send_IPI_mask(), 553 * which may be slightly slower than the hypercall, but still 554 * works correctly in such a VM. 555 */ 556 *inputarg = mem; 557 } 558 559 msr_vp_index = hv_get_msr(HV_MSR_VP_INDEX); 560 561 hv_vp_index[cpu] = msr_vp_index; 562 563 if (msr_vp_index > hv_max_vp_index) 564 hv_max_vp_index = msr_vp_index; 565 566 if (hv_parent_partition()) { 567 synic_eventring_tail = (u8 **)this_cpu_ptr(hv_synic_eventring_tail); 568 *synic_eventring_tail = kcalloc(HV_SYNIC_SINT_COUNT, 569 sizeof(u8), flags); 570 /* No need to unwind any of the above on failure here */ 571 if (unlikely(!*synic_eventring_tail)) 572 ret = -ENOMEM; 573 } 574 575 return ret; 576 } 577 578 int hv_common_cpu_die(unsigned int cpu) 579 { 580 u8 **synic_eventring_tail; 581 /* 582 * The hyperv_pcpu_input_arg and hyperv_pcpu_output_arg memory 583 * is not freed when the CPU goes offline as the hyperv_pcpu_input_arg 584 * may be used by the Hyper-V vPCI driver in reassigning interrupts 585 * as part of the offlining process. The interrupt reassignment 586 * happens *after* the CPUHP_AP_HYPERV_ONLINE state has run and 587 * called this function. 588 * 589 * If a previously offlined CPU is brought back online again, the 590 * originally allocated memory is reused in hv_common_cpu_init(). 591 */ 592 593 if (hv_parent_partition()) { 594 synic_eventring_tail = this_cpu_ptr(hv_synic_eventring_tail); 595 kfree(*synic_eventring_tail); 596 *synic_eventring_tail = NULL; 597 } 598 599 return 0; 600 } 601 602 /* Bit mask of the extended capability to query: see HV_EXT_CAPABILITY_xxx */ 603 bool hv_query_ext_cap(u64 cap_query) 604 { 605 /* 606 * The address of the 'hv_extended_cap' variable will be used as an 607 * output parameter to the hypercall below and so it should be 608 * compatible with 'virt_to_phys'. Which means, it's address should be 609 * directly mapped. Use 'static' to keep it compatible; stack variables 610 * can be virtually mapped, making them incompatible with 611 * 'virt_to_phys'. 612 * Hypercall input/output addresses should also be 8-byte aligned. 613 */ 614 static u64 hv_extended_cap __aligned(8); 615 static bool hv_extended_cap_queried; 616 u64 status; 617 618 /* 619 * Querying extended capabilities is an extended hypercall. Check if the 620 * partition supports extended hypercall, first. 621 */ 622 if (!(ms_hyperv.priv_high & HV_ENABLE_EXTENDED_HYPERCALLS)) 623 return false; 624 625 /* Extended capabilities do not change at runtime. */ 626 if (hv_extended_cap_queried) 627 return hv_extended_cap & cap_query; 628 629 status = hv_do_hypercall(HV_EXT_CALL_QUERY_CAPABILITIES, NULL, 630 &hv_extended_cap); 631 632 /* 633 * The query extended capabilities hypercall should not fail under 634 * any normal circumstances. Avoid repeatedly making the hypercall, on 635 * error. 636 */ 637 hv_extended_cap_queried = true; 638 if (!hv_result_success(status)) { 639 pr_err("Hyper-V: Extended query capabilities hypercall failed 0x%llx\n", 640 status); 641 return false; 642 } 643 644 return hv_extended_cap & cap_query; 645 } 646 EXPORT_SYMBOL_GPL(hv_query_ext_cap); 647 648 void hv_setup_dma_ops(struct device *dev, bool coherent) 649 { 650 arch_setup_dma_ops(dev, coherent); 651 } 652 EXPORT_SYMBOL_GPL(hv_setup_dma_ops); 653 654 bool hv_is_hibernation_supported(void) 655 { 656 return !hv_root_partition() && acpi_sleep_state_supported(ACPI_STATE_S4); 657 } 658 EXPORT_SYMBOL_GPL(hv_is_hibernation_supported); 659 660 /* 661 * Default function to read the Hyper-V reference counter, independent 662 * of whether Hyper-V enlightened clocks/timers are being used. But on 663 * architectures where it is used, Hyper-V enlightenment code in 664 * hyperv_timer.c may override this function. 665 */ 666 static u64 __hv_read_ref_counter(void) 667 { 668 return hv_get_msr(HV_MSR_TIME_REF_COUNT); 669 } 670 671 u64 (*hv_read_reference_counter)(void) = __hv_read_ref_counter; 672 EXPORT_SYMBOL_GPL(hv_read_reference_counter); 673 674 /* These __weak functions provide default "no-op" behavior and 675 * may be overridden by architecture specific versions. Architectures 676 * for which the default "no-op" behavior is sufficient can leave 677 * them unimplemented and not be cluttered with a bunch of stub 678 * functions in arch-specific code. 679 */ 680 681 bool __weak hv_is_isolation_supported(void) 682 { 683 return false; 684 } 685 EXPORT_SYMBOL_GPL(hv_is_isolation_supported); 686 687 bool __weak hv_isolation_type_snp(void) 688 { 689 return false; 690 } 691 EXPORT_SYMBOL_GPL(hv_isolation_type_snp); 692 693 bool __weak hv_isolation_type_tdx(void) 694 { 695 return false; 696 } 697 EXPORT_SYMBOL_GPL(hv_isolation_type_tdx); 698 699 void __weak hv_setup_vmbus_handler(void (*handler)(void)) 700 { 701 } 702 EXPORT_SYMBOL_GPL(hv_setup_vmbus_handler); 703 704 void __weak hv_remove_vmbus_handler(void) 705 { 706 } 707 EXPORT_SYMBOL_GPL(hv_remove_vmbus_handler); 708 709 void __weak hv_setup_mshv_handler(void (*handler)(void)) 710 { 711 } 712 EXPORT_SYMBOL_GPL(hv_setup_mshv_handler); 713 714 void __weak hv_setup_kexec_handler(void (*handler)(void)) 715 { 716 } 717 EXPORT_SYMBOL_GPL(hv_setup_kexec_handler); 718 719 void __weak hv_remove_kexec_handler(void) 720 { 721 } 722 EXPORT_SYMBOL_GPL(hv_remove_kexec_handler); 723 724 void __weak hv_setup_crash_handler(void (*handler)(struct pt_regs *regs)) 725 { 726 } 727 EXPORT_SYMBOL_GPL(hv_setup_crash_handler); 728 729 void __weak hv_remove_crash_handler(void) 730 { 731 } 732 EXPORT_SYMBOL_GPL(hv_remove_crash_handler); 733 734 void __weak hyperv_cleanup(void) 735 { 736 } 737 EXPORT_SYMBOL_GPL(hyperv_cleanup); 738 739 u64 __weak hv_ghcb_hypercall(u64 control, void *input, void *output, u32 input_size) 740 { 741 return HV_STATUS_INVALID_PARAMETER; 742 } 743 EXPORT_SYMBOL_GPL(hv_ghcb_hypercall); 744 745 u64 __weak hv_tdx_hypercall(u64 control, u64 param1, u64 param2) 746 { 747 return HV_STATUS_INVALID_PARAMETER; 748 } 749 EXPORT_SYMBOL_GPL(hv_tdx_hypercall); 750 751 void __weak hv_enable_coco_interrupt(unsigned int cpu, unsigned int vector, bool set) 752 { 753 } 754 EXPORT_SYMBOL_GPL(hv_enable_coco_interrupt); 755 756 void __weak hv_para_set_sint_proxy(bool enable) 757 { 758 } 759 EXPORT_SYMBOL_GPL(hv_para_set_sint_proxy); 760 761 u64 __weak hv_para_get_synic_register(unsigned int reg) 762 { 763 return ~0ULL; 764 } 765 EXPORT_SYMBOL_GPL(hv_para_get_synic_register); 766 767 void __weak hv_para_set_synic_register(unsigned int reg, u64 val) 768 { 769 } 770 EXPORT_SYMBOL_GPL(hv_para_set_synic_register); 771 772 void hv_identify_partition_type(void) 773 { 774 /* Assume guest role */ 775 hv_curr_partition_type = HV_PARTITION_TYPE_GUEST; 776 /* 777 * Check partition creation and cpu management privileges 778 * 779 * Hyper-V should never specify running as root and as a Confidential 780 * VM. But to protect against a compromised/malicious Hyper-V trying 781 * to exploit root behavior to expose Confidential VM memory, ignore 782 * the root partition setting if also a Confidential VM. 783 */ 784 if ((ms_hyperv.priv_high & HV_CREATE_PARTITIONS) && 785 !(ms_hyperv.priv_high & HV_ISOLATION)) { 786 787 if (!IS_ENABLED(CONFIG_MSHV_ROOT)) { 788 pr_crit("Hyper-V: CONFIG_MSHV_ROOT not enabled!\n"); 789 } else if (ms_hyperv.priv_high & HV_CPU_MANAGEMENT) { 790 pr_info("Hyper-V: running as root partition\n"); 791 hv_curr_partition_type = HV_PARTITION_TYPE_ROOT; 792 } else { 793 pr_info("Hyper-V: running as L1VH partition\n"); 794 hv_curr_partition_type = HV_PARTITION_TYPE_L1VH; 795 } 796 } 797 } 798 799 struct hv_status_info { 800 char *string; 801 int errno; 802 u16 code; 803 }; 804 805 /* 806 * Note on the errno mappings: 807 * A failed hypercall is usually only recoverable (or loggable) near 808 * the call site where the HV_STATUS_* code is known. So the errno 809 * it gets converted to is not too useful further up the stack. 810 * Provide a few mappings that could be useful, and revert to -EIO 811 * as a fallback. 812 */ 813 static const struct hv_status_info hv_status_infos[] = { 814 #define _STATUS_INFO(status, errno) { #status, (errno), (status) } 815 _STATUS_INFO(HV_STATUS_SUCCESS, 0), 816 _STATUS_INFO(HV_STATUS_INVALID_HYPERCALL_CODE, -EINVAL), 817 _STATUS_INFO(HV_STATUS_INVALID_HYPERCALL_INPUT, -EINVAL), 818 _STATUS_INFO(HV_STATUS_INVALID_ALIGNMENT, -EIO), 819 _STATUS_INFO(HV_STATUS_INVALID_PARAMETER, -EINVAL), 820 _STATUS_INFO(HV_STATUS_ACCESS_DENIED, -EACCES), 821 _STATUS_INFO(HV_STATUS_INVALID_PARTITION_STATE, -EIO), 822 _STATUS_INFO(HV_STATUS_OPERATION_DENIED, -EACCES), 823 _STATUS_INFO(HV_STATUS_UNKNOWN_PROPERTY, -EINVAL), 824 _STATUS_INFO(HV_STATUS_PROPERTY_VALUE_OUT_OF_RANGE, -EINVAL), 825 _STATUS_INFO(HV_STATUS_INSUFFICIENT_MEMORY, -ENOMEM), 826 _STATUS_INFO(HV_STATUS_INSUFFICIENT_CONTIGUOUS_MEMORY, -ENOMEM), 827 _STATUS_INFO(HV_STATUS_INSUFFICIENT_ROOT_MEMORY, -ENOMEM), 828 _STATUS_INFO(HV_STATUS_INSUFFICIENT_CONTIGUOUS_ROOT_MEMORY, -ENOMEM), 829 _STATUS_INFO(HV_STATUS_INVALID_PARTITION_ID, -EINVAL), 830 _STATUS_INFO(HV_STATUS_INVALID_VP_INDEX, -EINVAL), 831 _STATUS_INFO(HV_STATUS_NOT_FOUND, -EIO), 832 _STATUS_INFO(HV_STATUS_INVALID_PORT_ID, -EINVAL), 833 _STATUS_INFO(HV_STATUS_INVALID_CONNECTION_ID, -EINVAL), 834 _STATUS_INFO(HV_STATUS_INSUFFICIENT_BUFFERS, -EIO), 835 _STATUS_INFO(HV_STATUS_NOT_ACKNOWLEDGED, -EIO), 836 _STATUS_INFO(HV_STATUS_INVALID_VP_STATE, -EIO), 837 _STATUS_INFO(HV_STATUS_NO_RESOURCES, -EIO), 838 _STATUS_INFO(HV_STATUS_PROCESSOR_FEATURE_NOT_SUPPORTED, -EOPNOTSUPP), 839 _STATUS_INFO(HV_STATUS_INVALID_LP_INDEX, -EINVAL), 840 _STATUS_INFO(HV_STATUS_INVALID_REGISTER_VALUE, -EINVAL), 841 _STATUS_INFO(HV_STATUS_DEVICE_ALREADY_IN_DOMAIN, -EEXIST), 842 _STATUS_INFO(HV_STATUS_OPERATION_FAILED, -EIO), 843 _STATUS_INFO(HV_STATUS_TIME_OUT, -EIO), 844 _STATUS_INFO(HV_STATUS_CALL_PENDING, -EIO), 845 _STATUS_INFO(HV_STATUS_VTL_ALREADY_ENABLED, -EIO), 846 #undef _STATUS_INFO 847 }; 848 849 static inline const struct hv_status_info *find_hv_status_info(u64 hv_status) 850 { 851 int i; 852 u16 code = hv_result(hv_status); 853 854 for (i = 0; i < ARRAY_SIZE(hv_status_infos); ++i) { 855 const struct hv_status_info *info = &hv_status_infos[i]; 856 857 if (info->code == code) 858 return info; 859 } 860 861 return NULL; 862 } 863 864 /* Convert a hypercall result into a linux-friendly error code. */ 865 int hv_result_to_errno(u64 status) 866 { 867 const struct hv_status_info *info; 868 869 /* hv_do_hypercall() may return U64_MAX, hypercalls aren't possible */ 870 if (unlikely(status == U64_MAX)) 871 return -EOPNOTSUPP; 872 873 info = find_hv_status_info(status); 874 if (info) 875 return info->errno; 876 877 return -EIO; 878 } 879 EXPORT_SYMBOL_GPL(hv_result_to_errno); 880 881 const char *hv_result_to_string(u64 status) 882 { 883 const struct hv_status_info *info; 884 885 if (unlikely(status == U64_MAX)) 886 return "Hypercall page missing!"; 887 888 info = find_hv_status_info(status); 889 if (info) 890 return info->string; 891 892 return "Unknown"; 893 } 894 EXPORT_SYMBOL_GPL(hv_result_to_string); 895 896 /* 897 * Logical device ID registry for Hyper-V PCI buses. The pci-hyperv 898 * driver registers each bus's logical device ID prefix before scanning 899 * its devices. Consumers look up the prefix by PCI domain number when 900 * building logical device IDs for Hyper-V interfaces. 901 */ 902 int hv_pci_register_dev_id(int pci_domain_nr, u32 logical_dev_id_prefix) 903 { 904 struct hv_pci_busdata *bus, *new; 905 int ret; 906 907 new = kzalloc_obj(*new, GFP_KERNEL); 908 if (!new) 909 return -ENOMEM; 910 911 new->pci_domain_nr = pci_domain_nr; 912 new->logical_dev_id_prefix = logical_dev_id_prefix; 913 914 bus = rhashtable_lookup_get_insert_fast(&hv_pci_bus_ht, &new->node, 915 hv_pci_bus_ht_params); 916 if (IS_ERR(bus)) { 917 ret = PTR_ERR(bus); 918 goto free_new; 919 } 920 921 if (WARN_ONCE(bus != NULL, 922 "Hyper-V PCI domain %04x is already registered\n", 923 pci_domain_nr)) { 924 ret = -EEXIST; 925 goto free_new; 926 } 927 928 return 0; 929 930 free_new: 931 kfree(new); 932 return ret; 933 } 934 EXPORT_SYMBOL_FOR_MODULES(hv_pci_register_dev_id, "pci-hyperv"); 935 936 void hv_pci_unregister_dev_id(int pci_domain_nr) 937 { 938 struct hv_pci_busdata *bus; 939 int ret = -ENOENT; 940 941 rcu_read_lock(); 942 bus = rhashtable_lookup(&hv_pci_bus_ht, &pci_domain_nr, 943 hv_pci_bus_ht_params); 944 if (bus) 945 ret = rhashtable_remove_fast(&hv_pci_bus_ht, &bus->node, 946 hv_pci_bus_ht_params); 947 rcu_read_unlock(); 948 949 if (WARN_ONCE(ret, 950 "Failed to unregister Hyper-V PCI domain %04x\n", 951 pci_domain_nr)) 952 return; 953 954 kfree_rcu(bus, rcu); 955 } 956 EXPORT_SYMBOL_FOR_MODULES(hv_pci_unregister_dev_id, "pci-hyperv"); 957 958 /* 959 * Look up the logical device ID prefix registered for @pci_domain_nr. 960 * Returns 0 on success with *prefix filled in; -ENODEV if no entry is 961 * registered for that PCI domain. 962 */ 963 int hv_pci_lookup_dev_id(int pci_domain_nr, u32 *prefix) 964 { 965 struct hv_pci_busdata *bus; 966 int ret = -ENODEV; 967 968 rcu_read_lock(); 969 bus = rhashtable_lookup(&hv_pci_bus_ht, &pci_domain_nr, 970 hv_pci_bus_ht_params); 971 if (bus) { 972 *prefix = bus->logical_dev_id_prefix; 973 ret = 0; 974 } 975 rcu_read_unlock(); 976 977 return ret; 978 } 979