mshyperv.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * HyperV Detection code.
  4. *
  5. * Copyright (C) 2010, Novell, Inc.
  6. * Author : K. Y. Srinivasan <ksrinivasan@novell.com>
  7. */
  8. #include <linux/types.h>
  9. #include <linux/time.h>
  10. #include <linux/clocksource.h>
  11. #include <linux/init.h>
  12. #include <linux/export.h>
  13. #include <linux/hardirq.h>
  14. #include <linux/efi.h>
  15. #include <linux/interrupt.h>
  16. #include <linux/irq.h>
  17. #include <linux/kexec.h>
  18. #include <linux/random.h>
  19. #include <asm/processor.h>
  20. #include <asm/hypervisor.h>
  21. #include <asm/hyperv-tlfs.h>
  22. #include <asm/mshyperv.h>
  23. #include <asm/desc.h>
  24. #include <asm/idtentry.h>
  25. #include <asm/irq_regs.h>
  26. #include <asm/i8259.h>
  27. #include <asm/apic.h>
  28. #include <asm/timer.h>
  29. #include <asm/reboot.h>
  30. #include <asm/nmi.h>
  31. #include <clocksource/hyperv_timer.h>
  32. #include <asm/numa.h>
  33. #include <asm/svm.h>
  34. /* Is Linux running as the root partition? */
  35. bool hv_root_partition;
  36. /* Is Linux running on nested Microsoft Hypervisor */
  37. bool hv_nested;
  38. struct ms_hyperv_info ms_hyperv;
  39. /* Used in modules via hv_do_hypercall(): see arch/x86/include/asm/mshyperv.h */
  40. bool hyperv_paravisor_present __ro_after_init;
  41. EXPORT_SYMBOL_GPL(hyperv_paravisor_present);
  42. #if IS_ENABLED(CONFIG_HYPERV)
  43. static inline unsigned int hv_get_nested_msr(unsigned int reg)
  44. {
  45. if (hv_is_sint_msr(reg))
  46. return reg - HV_X64_MSR_SINT0 + HV_X64_MSR_NESTED_SINT0;
  47. switch (reg) {
  48. case HV_X64_MSR_SIMP:
  49. return HV_X64_MSR_NESTED_SIMP;
  50. case HV_X64_MSR_SIEFP:
  51. return HV_X64_MSR_NESTED_SIEFP;
  52. case HV_X64_MSR_SVERSION:
  53. return HV_X64_MSR_NESTED_SVERSION;
  54. case HV_X64_MSR_SCONTROL:
  55. return HV_X64_MSR_NESTED_SCONTROL;
  56. case HV_X64_MSR_EOM:
  57. return HV_X64_MSR_NESTED_EOM;
  58. default:
  59. return reg;
  60. }
  61. }
  62. u64 hv_get_non_nested_msr(unsigned int reg)
  63. {
  64. u64 value;
  65. if (hv_is_synic_msr(reg) && ms_hyperv.paravisor_present)
  66. hv_ivm_msr_read(reg, &value);
  67. else
  68. rdmsrl(reg, value);
  69. return value;
  70. }
  71. EXPORT_SYMBOL_GPL(hv_get_non_nested_msr);
  72. void hv_set_non_nested_msr(unsigned int reg, u64 value)
  73. {
  74. if (hv_is_synic_msr(reg) && ms_hyperv.paravisor_present) {
  75. hv_ivm_msr_write(reg, value);
  76. /* Write proxy bit via wrmsl instruction */
  77. if (hv_is_sint_msr(reg))
  78. wrmsrl(reg, value | 1 << 20);
  79. } else {
  80. wrmsrl(reg, value);
  81. }
  82. }
  83. EXPORT_SYMBOL_GPL(hv_set_non_nested_msr);
  84. u64 hv_get_msr(unsigned int reg)
  85. {
  86. if (hv_nested)
  87. reg = hv_get_nested_msr(reg);
  88. return hv_get_non_nested_msr(reg);
  89. }
  90. EXPORT_SYMBOL_GPL(hv_get_msr);
  91. void hv_set_msr(unsigned int reg, u64 value)
  92. {
  93. if (hv_nested)
  94. reg = hv_get_nested_msr(reg);
  95. hv_set_non_nested_msr(reg, value);
  96. }
  97. EXPORT_SYMBOL_GPL(hv_set_msr);
  98. static void (*vmbus_handler)(void);
  99. static void (*hv_stimer0_handler)(void);
  100. static void (*hv_kexec_handler)(void);
  101. static void (*hv_crash_handler)(struct pt_regs *regs);
  102. DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_callback)
  103. {
  104. struct pt_regs *old_regs = set_irq_regs(regs);
  105. inc_irq_stat(irq_hv_callback_count);
  106. if (vmbus_handler)
  107. vmbus_handler();
  108. if (ms_hyperv.hints & HV_DEPRECATING_AEOI_RECOMMENDED)
  109. apic_eoi();
  110. set_irq_regs(old_regs);
  111. }
  112. void hv_setup_vmbus_handler(void (*handler)(void))
  113. {
  114. vmbus_handler = handler;
  115. }
  116. void hv_remove_vmbus_handler(void)
  117. {
  118. /* We have no way to deallocate the interrupt gate */
  119. vmbus_handler = NULL;
  120. }
  121. /*
  122. * Routines to do per-architecture handling of stimer0
  123. * interrupts when in Direct Mode
  124. */
  125. DEFINE_IDTENTRY_SYSVEC(sysvec_hyperv_stimer0)
  126. {
  127. struct pt_regs *old_regs = set_irq_regs(regs);
  128. inc_irq_stat(hyperv_stimer0_count);
  129. if (hv_stimer0_handler)
  130. hv_stimer0_handler();
  131. add_interrupt_randomness(HYPERV_STIMER0_VECTOR);
  132. apic_eoi();
  133. set_irq_regs(old_regs);
  134. }
  135. /* For x86/x64, override weak placeholders in hyperv_timer.c */
  136. void hv_setup_stimer0_handler(void (*handler)(void))
  137. {
  138. hv_stimer0_handler = handler;
  139. }
  140. void hv_remove_stimer0_handler(void)
  141. {
  142. /* We have no way to deallocate the interrupt gate */
  143. hv_stimer0_handler = NULL;
  144. }
  145. void hv_setup_kexec_handler(void (*handler)(void))
  146. {
  147. hv_kexec_handler = handler;
  148. }
  149. void hv_remove_kexec_handler(void)
  150. {
  151. hv_kexec_handler = NULL;
  152. }
  153. void hv_setup_crash_handler(void (*handler)(struct pt_regs *regs))
  154. {
  155. hv_crash_handler = handler;
  156. }
  157. void hv_remove_crash_handler(void)
  158. {
  159. hv_crash_handler = NULL;
  160. }
  161. #ifdef CONFIG_KEXEC_CORE
  162. static void hv_machine_shutdown(void)
  163. {
  164. if (kexec_in_progress && hv_kexec_handler)
  165. hv_kexec_handler();
  166. /*
  167. * Call hv_cpu_die() on all the CPUs, otherwise later the hypervisor
  168. * corrupts the old VP Assist Pages and can crash the kexec kernel.
  169. */
  170. if (kexec_in_progress)
  171. cpuhp_remove_state(CPUHP_AP_HYPERV_ONLINE);
  172. /* The function calls stop_other_cpus(). */
  173. native_machine_shutdown();
  174. /* Disable the hypercall page when there is only 1 active CPU. */
  175. if (kexec_in_progress)
  176. hyperv_cleanup();
  177. }
  178. #endif /* CONFIG_KEXEC_CORE */
  179. #ifdef CONFIG_CRASH_DUMP
  180. static void hv_machine_crash_shutdown(struct pt_regs *regs)
  181. {
  182. if (hv_crash_handler)
  183. hv_crash_handler(regs);
  184. /* The function calls crash_smp_send_stop(). */
  185. native_machine_crash_shutdown(regs);
  186. /* Disable the hypercall page when there is only 1 active CPU. */
  187. hyperv_cleanup();
  188. }
  189. #endif /* CONFIG_CRASH_DUMP */
  190. static u64 hv_ref_counter_at_suspend;
  191. static void (*old_save_sched_clock_state)(void);
  192. static void (*old_restore_sched_clock_state)(void);
  193. /*
  194. * Hyper-V clock counter resets during hibernation. Save and restore clock
  195. * offset during suspend/resume, while also considering the time passed
  196. * before suspend. This is to make sure that sched_clock using hv tsc page
  197. * based clocksource, proceeds from where it left off during suspend and
  198. * it shows correct time for the timestamps of kernel messages after resume.
  199. */
  200. static void save_hv_clock_tsc_state(void)
  201. {
  202. hv_ref_counter_at_suspend = hv_read_reference_counter();
  203. }
  204. static void restore_hv_clock_tsc_state(void)
  205. {
  206. /*
  207. * Adjust the offsets used by hv tsc clocksource to
  208. * account for the time spent before hibernation.
  209. * adjusted value = reference counter (time) at suspend
  210. * - reference counter (time) now.
  211. */
  212. hv_adj_sched_clock_offset(hv_ref_counter_at_suspend - hv_read_reference_counter());
  213. }
  214. /*
  215. * Functions to override save_sched_clock_state and restore_sched_clock_state
  216. * functions of x86_platform. The Hyper-V clock counter is reset during
  217. * suspend-resume and the offset used to measure time needs to be
  218. * corrected, post resume.
  219. */
  220. static void hv_save_sched_clock_state(void)
  221. {
  222. old_save_sched_clock_state();
  223. save_hv_clock_tsc_state();
  224. }
  225. static void hv_restore_sched_clock_state(void)
  226. {
  227. restore_hv_clock_tsc_state();
  228. old_restore_sched_clock_state();
  229. }
  230. static void __init x86_setup_ops_for_tsc_pg_clock(void)
  231. {
  232. if (!(ms_hyperv.features & HV_MSR_REFERENCE_TSC_AVAILABLE))
  233. return;
  234. old_save_sched_clock_state = x86_platform.save_sched_clock_state;
  235. x86_platform.save_sched_clock_state = hv_save_sched_clock_state;
  236. old_restore_sched_clock_state = x86_platform.restore_sched_clock_state;
  237. x86_platform.restore_sched_clock_state = hv_restore_sched_clock_state;
  238. }
  239. #endif /* CONFIG_HYPERV */
  240. static uint32_t __init ms_hyperv_platform(void)
  241. {
  242. u32 eax;
  243. u32 hyp_signature[3];
  244. if (!boot_cpu_has(X86_FEATURE_HYPERVISOR))
  245. return 0;
  246. cpuid(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS,
  247. &eax, &hyp_signature[0], &hyp_signature[1], &hyp_signature[2]);
  248. if (eax < HYPERV_CPUID_MIN || eax > HYPERV_CPUID_MAX ||
  249. memcmp("Microsoft Hv", hyp_signature, 12))
  250. return 0;
  251. /* HYPERCALL and VP_INDEX MSRs are mandatory for all features. */
  252. eax = cpuid_eax(HYPERV_CPUID_FEATURES);
  253. if (!(eax & HV_MSR_HYPERCALL_AVAILABLE)) {
  254. pr_warn("x86/hyperv: HYPERCALL MSR not available.\n");
  255. return 0;
  256. }
  257. if (!(eax & HV_MSR_VP_INDEX_AVAILABLE)) {
  258. pr_warn("x86/hyperv: VP_INDEX MSR not available.\n");
  259. return 0;
  260. }
  261. return HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS;
  262. }
  263. #ifdef CONFIG_X86_LOCAL_APIC
  264. /*
  265. * Prior to WS2016 Debug-VM sends NMIs to all CPUs which makes
  266. * it difficult to process CHANNELMSG_UNLOAD in case of crash. Handle
  267. * unknown NMI on the first CPU which gets it.
  268. */
  269. static int hv_nmi_unknown(unsigned int val, struct pt_regs *regs)
  270. {
  271. static atomic_t nmi_cpu = ATOMIC_INIT(-1);
  272. unsigned int old_cpu, this_cpu;
  273. if (!unknown_nmi_panic)
  274. return NMI_DONE;
  275. old_cpu = -1;
  276. this_cpu = raw_smp_processor_id();
  277. if (!atomic_try_cmpxchg(&nmi_cpu, &old_cpu, this_cpu))
  278. return NMI_HANDLED;
  279. return NMI_DONE;
  280. }
  281. #endif
  282. static unsigned long hv_get_tsc_khz(void)
  283. {
  284. unsigned long freq;
  285. rdmsrl(HV_X64_MSR_TSC_FREQUENCY, freq);
  286. return freq / 1000;
  287. }
  288. #if defined(CONFIG_SMP) && IS_ENABLED(CONFIG_HYPERV)
  289. static void __init hv_smp_prepare_boot_cpu(void)
  290. {
  291. native_smp_prepare_boot_cpu();
  292. #if defined(CONFIG_X86_64) && defined(CONFIG_PARAVIRT_SPINLOCKS)
  293. hv_init_spinlocks();
  294. #endif
  295. }
  296. static void __init hv_smp_prepare_cpus(unsigned int max_cpus)
  297. {
  298. #ifdef CONFIG_X86_64
  299. int i;
  300. int ret;
  301. #endif
  302. native_smp_prepare_cpus(max_cpus);
  303. /*
  304. * Override wakeup_secondary_cpu_64 callback for SEV-SNP
  305. * enlightened guest.
  306. */
  307. if (!ms_hyperv.paravisor_present && hv_isolation_type_snp()) {
  308. apic->wakeup_secondary_cpu_64 = hv_snp_boot_ap;
  309. return;
  310. }
  311. #ifdef CONFIG_X86_64
  312. for_each_present_cpu(i) {
  313. if (i == 0)
  314. continue;
  315. ret = hv_call_add_logical_proc(numa_cpu_node(i), i, cpu_physical_id(i));
  316. BUG_ON(ret);
  317. }
  318. for_each_present_cpu(i) {
  319. if (i == 0)
  320. continue;
  321. ret = hv_call_create_vp(numa_cpu_node(i), hv_current_partition_id, i, i);
  322. BUG_ON(ret);
  323. }
  324. #endif
  325. }
  326. #endif
  327. /*
  328. * When a fully enlightened TDX VM runs on Hyper-V, the firmware sets the
  329. * HW_REDUCED flag: refer to acpi_tb_create_local_fadt(). Consequently ttyS0
  330. * interrupts can't work because request_irq() -> ... -> irq_to_desc() returns
  331. * NULL for ttyS0. This happens because mp_config_acpi_legacy_irqs() sees a
  332. * nr_legacy_irqs() of 0, so it doesn't initialize the array 'mp_irqs[]', and
  333. * later setup_IO_APIC_irqs() -> find_irq_entry() fails to find the legacy irqs
  334. * from the array and hence doesn't create the necessary irq description info.
  335. *
  336. * Clone arch/x86/kernel/acpi/boot.c: acpi_generic_reduced_hw_init() here,
  337. * except don't change 'legacy_pic', which keeps its default value
  338. * 'default_legacy_pic'. This way, mp_config_acpi_legacy_irqs() sees a non-zero
  339. * nr_legacy_irqs() and eventually serial console interrupts works properly.
  340. */
  341. static void __init reduced_hw_init(void)
  342. {
  343. x86_init.timers.timer_init = x86_init_noop;
  344. x86_init.irqs.pre_vector_init = x86_init_noop;
  345. }
  346. int hv_get_hypervisor_version(union hv_hypervisor_version_info *info)
  347. {
  348. unsigned int hv_max_functions;
  349. hv_max_functions = cpuid_eax(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS);
  350. if (hv_max_functions < HYPERV_CPUID_VERSION) {
  351. pr_err("%s: Could not detect Hyper-V version\n", __func__);
  352. return -ENODEV;
  353. }
  354. cpuid(HYPERV_CPUID_VERSION, &info->eax, &info->ebx, &info->ecx, &info->edx);
  355. return 0;
  356. }
  357. static void __init ms_hyperv_init_platform(void)
  358. {
  359. int hv_max_functions_eax;
  360. #ifdef CONFIG_PARAVIRT
  361. pv_info.name = "Hyper-V";
  362. #endif
  363. /*
  364. * Extract the features and hints
  365. */
  366. ms_hyperv.features = cpuid_eax(HYPERV_CPUID_FEATURES);
  367. ms_hyperv.priv_high = cpuid_ebx(HYPERV_CPUID_FEATURES);
  368. ms_hyperv.misc_features = cpuid_edx(HYPERV_CPUID_FEATURES);
  369. ms_hyperv.hints = cpuid_eax(HYPERV_CPUID_ENLIGHTMENT_INFO);
  370. hv_max_functions_eax = cpuid_eax(HYPERV_CPUID_VENDOR_AND_MAX_FUNCTIONS);
  371. pr_info("Hyper-V: privilege flags low 0x%x, high 0x%x, hints 0x%x, misc 0x%x\n",
  372. ms_hyperv.features, ms_hyperv.priv_high, ms_hyperv.hints,
  373. ms_hyperv.misc_features);
  374. ms_hyperv.max_vp_index = cpuid_eax(HYPERV_CPUID_IMPLEMENT_LIMITS);
  375. ms_hyperv.max_lp_index = cpuid_ebx(HYPERV_CPUID_IMPLEMENT_LIMITS);
  376. pr_debug("Hyper-V: max %u virtual processors, %u logical processors\n",
  377. ms_hyperv.max_vp_index, ms_hyperv.max_lp_index);
  378. /*
  379. * Check CPU management privilege.
  380. *
  381. * To mirror what Windows does we should extract CPU management
  382. * features and use the ReservedIdentityBit to detect if Linux is the
  383. * root partition. But that requires negotiating CPU management
  384. * interface (a process to be finalized). For now, use the privilege
  385. * flag as the indicator for running as root.
  386. *
  387. * Hyper-V should never specify running as root and as a Confidential
  388. * VM. But to protect against a compromised/malicious Hyper-V trying
  389. * to exploit root behavior to expose Confidential VM memory, ignore
  390. * the root partition setting if also a Confidential VM.
  391. */
  392. if ((ms_hyperv.priv_high & HV_CPU_MANAGEMENT) &&
  393. !(ms_hyperv.priv_high & HV_ISOLATION)) {
  394. hv_root_partition = true;
  395. pr_info("Hyper-V: running as root partition\n");
  396. }
  397. if (ms_hyperv.hints & HV_X64_HYPERV_NESTED) {
  398. hv_nested = true;
  399. pr_info("Hyper-V: running on a nested hypervisor\n");
  400. }
  401. if (ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
  402. ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE) {
  403. x86_platform.calibrate_tsc = hv_get_tsc_khz;
  404. x86_platform.calibrate_cpu = hv_get_tsc_khz;
  405. setup_force_cpu_cap(X86_FEATURE_TSC_KNOWN_FREQ);
  406. }
  407. if (ms_hyperv.priv_high & HV_ISOLATION) {
  408. ms_hyperv.isolation_config_a = cpuid_eax(HYPERV_CPUID_ISOLATION_CONFIG);
  409. ms_hyperv.isolation_config_b = cpuid_ebx(HYPERV_CPUID_ISOLATION_CONFIG);
  410. if (ms_hyperv.shared_gpa_boundary_active)
  411. ms_hyperv.shared_gpa_boundary =
  412. BIT_ULL(ms_hyperv.shared_gpa_boundary_bits);
  413. hyperv_paravisor_present = !!ms_hyperv.paravisor_present;
  414. pr_info("Hyper-V: Isolation Config: Group A 0x%x, Group B 0x%x\n",
  415. ms_hyperv.isolation_config_a, ms_hyperv.isolation_config_b);
  416. if (hv_get_isolation_type() == HV_ISOLATION_TYPE_SNP) {
  417. static_branch_enable(&isolation_type_snp);
  418. } else if (hv_get_isolation_type() == HV_ISOLATION_TYPE_TDX) {
  419. static_branch_enable(&isolation_type_tdx);
  420. /* A TDX VM must use x2APIC and doesn't use lazy EOI. */
  421. ms_hyperv.hints &= ~HV_X64_APIC_ACCESS_RECOMMENDED;
  422. if (!ms_hyperv.paravisor_present) {
  423. /*
  424. * Mark the Hyper-V TSC page feature as disabled
  425. * in a TDX VM without paravisor so that the
  426. * Invariant TSC, which is a better clocksource
  427. * anyway, is used instead.
  428. */
  429. ms_hyperv.features &= ~HV_MSR_REFERENCE_TSC_AVAILABLE;
  430. /*
  431. * The Invariant TSC is expected to be available
  432. * in a TDX VM without paravisor, but if not,
  433. * print a warning message. The slower Hyper-V MSR-based
  434. * Ref Counter should end up being the clocksource.
  435. */
  436. if (!(ms_hyperv.features & HV_ACCESS_TSC_INVARIANT))
  437. pr_warn("Hyper-V: Invariant TSC is unavailable\n");
  438. /* HV_MSR_CRASH_CTL is unsupported. */
  439. ms_hyperv.misc_features &= ~HV_FEATURE_GUEST_CRASH_MSR_AVAILABLE;
  440. /* Don't trust Hyper-V's TLB-flushing hypercalls. */
  441. ms_hyperv.hints &= ~HV_X64_REMOTE_TLB_FLUSH_RECOMMENDED;
  442. x86_init.acpi.reduced_hw_early_init = reduced_hw_init;
  443. }
  444. }
  445. }
  446. if (hv_max_functions_eax >= HYPERV_CPUID_NESTED_FEATURES) {
  447. ms_hyperv.nested_features =
  448. cpuid_eax(HYPERV_CPUID_NESTED_FEATURES);
  449. pr_info("Hyper-V: Nested features: 0x%x\n",
  450. ms_hyperv.nested_features);
  451. }
  452. #ifdef CONFIG_X86_LOCAL_APIC
  453. if (ms_hyperv.features & HV_ACCESS_FREQUENCY_MSRS &&
  454. ms_hyperv.misc_features & HV_FEATURE_FREQUENCY_MSRS_AVAILABLE) {
  455. /*
  456. * Get the APIC frequency.
  457. */
  458. u64 hv_lapic_frequency;
  459. rdmsrl(HV_X64_MSR_APIC_FREQUENCY, hv_lapic_frequency);
  460. hv_lapic_frequency = div_u64(hv_lapic_frequency, HZ);
  461. lapic_timer_period = hv_lapic_frequency;
  462. pr_info("Hyper-V: LAPIC Timer Frequency: %#x\n",
  463. lapic_timer_period);
  464. }
  465. register_nmi_handler(NMI_UNKNOWN, hv_nmi_unknown, NMI_FLAG_FIRST,
  466. "hv_nmi_unknown");
  467. #endif
  468. #ifdef CONFIG_X86_IO_APIC
  469. no_timer_check = 1;
  470. #endif
  471. #if IS_ENABLED(CONFIG_HYPERV)
  472. #if defined(CONFIG_KEXEC_CORE)
  473. machine_ops.shutdown = hv_machine_shutdown;
  474. #endif
  475. #if defined(CONFIG_CRASH_DUMP)
  476. machine_ops.crash_shutdown = hv_machine_crash_shutdown;
  477. #endif
  478. #endif
  479. if (ms_hyperv.features & HV_ACCESS_TSC_INVARIANT) {
  480. /*
  481. * Writing to synthetic MSR 0x40000118 updates/changes the
  482. * guest visible CPUIDs. Setting bit 0 of this MSR enables
  483. * guests to report invariant TSC feature through CPUID
  484. * instruction, CPUID 0x800000007/EDX, bit 8. See code in
  485. * early_init_intel() where this bit is examined. The
  486. * setting of this MSR bit should happen before init_intel()
  487. * is called.
  488. */
  489. wrmsrl(HV_X64_MSR_TSC_INVARIANT_CONTROL, HV_EXPOSE_INVARIANT_TSC);
  490. setup_force_cpu_cap(X86_FEATURE_TSC_RELIABLE);
  491. }
  492. /*
  493. * Generation 2 instances don't support reading the NMI status from
  494. * 0x61 port.
  495. */
  496. if (efi_enabled(EFI_BOOT))
  497. x86_platform.get_nmi_reason = hv_get_nmi_reason;
  498. #if IS_ENABLED(CONFIG_HYPERV)
  499. if ((hv_get_isolation_type() == HV_ISOLATION_TYPE_VBS) ||
  500. ms_hyperv.paravisor_present)
  501. hv_vtom_init();
  502. /*
  503. * Setup the hook to get control post apic initialization.
  504. */
  505. x86_platform.apic_post_init = hyperv_init;
  506. hyperv_setup_mmu_ops();
  507. /* Install system interrupt handler for hypervisor callback */
  508. sysvec_install(HYPERVISOR_CALLBACK_VECTOR, sysvec_hyperv_callback);
  509. /* Install system interrupt handler for reenlightenment notifications */
  510. if (ms_hyperv.features & HV_ACCESS_REENLIGHTENMENT) {
  511. sysvec_install(HYPERV_REENLIGHTENMENT_VECTOR, sysvec_hyperv_reenlightenment);
  512. }
  513. /* Install system interrupt handler for stimer0 */
  514. if (ms_hyperv.misc_features & HV_STIMER_DIRECT_MODE_AVAILABLE) {
  515. sysvec_install(HYPERV_STIMER0_VECTOR, sysvec_hyperv_stimer0);
  516. }
  517. # ifdef CONFIG_SMP
  518. smp_ops.smp_prepare_boot_cpu = hv_smp_prepare_boot_cpu;
  519. if (hv_root_partition ||
  520. (!ms_hyperv.paravisor_present && hv_isolation_type_snp()))
  521. smp_ops.smp_prepare_cpus = hv_smp_prepare_cpus;
  522. # endif
  523. /*
  524. * Hyper-V doesn't provide irq remapping for IO-APIC. To enable x2apic,
  525. * set x2apic destination mode to physical mode when x2apic is available
  526. * and Hyper-V IOMMU driver makes sure cpus assigned with IO-APIC irqs
  527. * have 8-bit APIC id.
  528. */
  529. # ifdef CONFIG_X86_X2APIC
  530. if (x2apic_supported())
  531. x2apic_phys = 1;
  532. # endif
  533. /* Register Hyper-V specific clocksource */
  534. hv_init_clocksource();
  535. x86_setup_ops_for_tsc_pg_clock();
  536. hv_vtl_init_platform();
  537. #endif
  538. /*
  539. * TSC should be marked as unstable only after Hyper-V
  540. * clocksource has been initialized. This ensures that the
  541. * stability of the sched_clock is not altered.
  542. */
  543. if (!(ms_hyperv.features & HV_ACCESS_TSC_INVARIANT))
  544. mark_tsc_unstable("running on Hyper-V");
  545. hardlockup_detector_disable();
  546. }
  547. static bool __init ms_hyperv_x2apic_available(void)
  548. {
  549. return x2apic_supported();
  550. }
  551. /*
  552. * If ms_hyperv_msi_ext_dest_id() returns true, hyperv_prepare_irq_remapping()
  553. * returns -ENODEV and the Hyper-V IOMMU driver is not used; instead, the
  554. * generic support of the 15-bit APIC ID is used: see __irq_msi_compose_msg().
  555. *
  556. * Note: for a VM on Hyper-V, the I/O-APIC is the only device which
  557. * (logically) generates MSIs directly to the system APIC irq domain.
  558. * There is no HPET, and PCI MSI/MSI-X interrupts are remapped by the
  559. * pci-hyperv host bridge.
  560. *
  561. * Note: for a Hyper-V root partition, this will always return false.
  562. * The hypervisor doesn't expose these HYPERV_CPUID_VIRT_STACK_* cpuids by
  563. * default, they are implemented as intercepts by the Windows Hyper-V stack.
  564. * Even a nested root partition (L2 root) will not get them because the
  565. * nested (L1) hypervisor filters them out.
  566. */
  567. static bool __init ms_hyperv_msi_ext_dest_id(void)
  568. {
  569. u32 eax;
  570. eax = cpuid_eax(HYPERV_CPUID_VIRT_STACK_INTERFACE);
  571. if (eax != HYPERV_VS_INTERFACE_EAX_SIGNATURE)
  572. return false;
  573. eax = cpuid_eax(HYPERV_CPUID_VIRT_STACK_PROPERTIES);
  574. return eax & HYPERV_VS_PROPERTIES_EAX_EXTENDED_IOAPIC_RTE;
  575. }
  576. #ifdef CONFIG_AMD_MEM_ENCRYPT
  577. static void hv_sev_es_hcall_prepare(struct ghcb *ghcb, struct pt_regs *regs)
  578. {
  579. /* RAX and CPL are already in the GHCB */
  580. ghcb_set_rcx(ghcb, regs->cx);
  581. ghcb_set_rdx(ghcb, regs->dx);
  582. ghcb_set_r8(ghcb, regs->r8);
  583. }
  584. static bool hv_sev_es_hcall_finish(struct ghcb *ghcb, struct pt_regs *regs)
  585. {
  586. /* No checking of the return state needed */
  587. return true;
  588. }
  589. #endif
  590. const __initconst struct hypervisor_x86 x86_hyper_ms_hyperv = {
  591. .name = "Microsoft Hyper-V",
  592. .detect = ms_hyperv_platform,
  593. .type = X86_HYPER_MS_HYPERV,
  594. .init.x2apic_available = ms_hyperv_x2apic_available,
  595. .init.msi_ext_dest_id = ms_hyperv_msi_ext_dest_id,
  596. .init.init_platform = ms_hyperv_init_platform,
  597. .init.guest_late_init = ms_hyperv_late_init,
  598. #ifdef CONFIG_AMD_MEM_ENCRYPT
  599. .runtime.sev_es_hcall_prepare = hv_sev_es_hcall_prepare,
  600. .runtime.sev_es_hcall_finish = hv_sev_es_hcall_finish,
  601. #endif
  602. };