mshyperv.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Core routines for interacting with Microsoft's Hyper-V hypervisor,
  4. * including hypervisor initialization.
  5. *
  6. * Copyright (C) 2021, Microsoft, Inc.
  7. *
  8. * Author : Michael Kelley <mikelley@microsoft.com>
  9. */
  10. #include <linux/types.h>
  11. #include <linux/acpi.h>
  12. #include <linux/export.h>
  13. #include <linux/errno.h>
  14. #include <linux/version.h>
  15. #include <linux/cpuhotplug.h>
  16. #include <asm/mshyperv.h>
  17. static bool hyperv_initialized;
  18. int hv_get_hypervisor_version(union hv_hypervisor_version_info *info)
  19. {
  20. hv_get_vpreg_128(HV_REGISTER_HYPERVISOR_VERSION,
  21. (struct hv_get_vp_registers_output *)info);
  22. return 0;
  23. }
  24. EXPORT_SYMBOL_GPL(hv_get_hypervisor_version);
  25. #ifdef CONFIG_ACPI
  26. static bool __init hyperv_detect_via_acpi(void)
  27. {
  28. if (acpi_disabled)
  29. return false;
  30. /*
  31. * Hypervisor ID is only available in ACPI v6+, and the
  32. * structure layout was extended in v6 to accommodate that
  33. * new field.
  34. *
  35. * At the very minimum, this check makes sure not to read
  36. * past the FADT structure.
  37. *
  38. * It is also needed to catch running in some unknown
  39. * non-Hyper-V environment that has ACPI 5.x or less.
  40. * In such a case, it can't be Hyper-V.
  41. */
  42. if (acpi_gbl_FADT.header.revision < 6)
  43. return false;
  44. return strncmp((char *)&acpi_gbl_FADT.hypervisor_id, "MsHyperV", 8) == 0;
  45. }
  46. #else
  47. static bool __init hyperv_detect_via_acpi(void)
  48. {
  49. return false;
  50. }
  51. #endif
  52. static bool __init hyperv_detect_via_smccc(void)
  53. {
  54. uuid_t hyperv_uuid = UUID_INIT(
  55. 0x58ba324d, 0x6447, 0x24cd,
  56. 0x75, 0x6c, 0xef, 0x8e,
  57. 0x24, 0x70, 0x59, 0x16);
  58. return arm_smccc_hypervisor_has_uuid(&hyperv_uuid);
  59. }
  60. static int __init hyperv_init(void)
  61. {
  62. struct hv_get_vp_registers_output result;
  63. u64 guest_id;
  64. int ret;
  65. /*
  66. * Allow for a kernel built with CONFIG_HYPERV to be running in
  67. * a non-Hyper-V environment.
  68. *
  69. * In such cases, do nothing and return success.
  70. */
  71. if (!hyperv_detect_via_acpi() && !hyperv_detect_via_smccc())
  72. return 0;
  73. /* Setup the guest ID */
  74. guest_id = hv_generate_guest_id(LINUX_VERSION_CODE);
  75. hv_set_vpreg(HV_REGISTER_GUEST_OS_ID, guest_id);
  76. /* Get the features and hints from Hyper-V */
  77. hv_get_vpreg_128(HV_REGISTER_PRIVILEGES_AND_FEATURES_INFO, &result);
  78. ms_hyperv.features = result.as32.a;
  79. ms_hyperv.priv_high = result.as32.b;
  80. ms_hyperv.misc_features = result.as32.c;
  81. hv_get_vpreg_128(HV_REGISTER_FEATURES_INFO, &result);
  82. ms_hyperv.hints = result.as32.a;
  83. pr_info("Hyper-V: privilege flags low 0x%x, high 0x%x, hints 0x%x, misc 0x%x\n",
  84. ms_hyperv.features, ms_hyperv.priv_high, ms_hyperv.hints,
  85. ms_hyperv.misc_features);
  86. hv_identify_partition_type();
  87. ret = hv_common_init();
  88. if (ret)
  89. return ret;
  90. ret = cpuhp_setup_state(CPUHP_AP_HYPERV_ONLINE, "arm64/hyperv_init:online",
  91. hv_common_cpu_init, hv_common_cpu_die);
  92. if (ret < 0) {
  93. hv_common_free();
  94. return ret;
  95. }
  96. if (ms_hyperv.priv_high & HV_ACCESS_PARTITION_ID)
  97. hv_get_partition_id();
  98. ms_hyperv.vtl = get_vtl();
  99. if (ms_hyperv.vtl > 0) /* non default VTL */
  100. pr_info("Linux runs in Hyper-V Virtual Trust Level %d\n", ms_hyperv.vtl);
  101. ms_hyperv_late_init();
  102. hyperv_initialized = true;
  103. return 0;
  104. }
  105. early_initcall(hyperv_init);
  106. bool hv_is_hyperv_initialized(void)
  107. {
  108. return hyperv_initialized;
  109. }
  110. EXPORT_SYMBOL_GPL(hv_is_hyperv_initialized);