hv_common.c 24 KB

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