hv_core.c 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Low level utility routines for interacting with Hyper-V.
  4. *
  5. * Copyright (C) 2021, Microsoft, Inc.
  6. *
  7. * Author : Michael Kelley <mikelley@microsoft.com>
  8. */
  9. #include <linux/types.h>
  10. #include <linux/export.h>
  11. #include <linux/mm.h>
  12. #include <linux/arm-smccc.h>
  13. #include <linux/module.h>
  14. #include <asm-generic/bug.h>
  15. #include <hyperv/hvhdk.h>
  16. #include <asm/mshyperv.h>
  17. /*
  18. * hv_do_hypercall- Invoke the specified hypercall
  19. */
  20. u64 hv_do_hypercall(u64 control, void *input, void *output)
  21. {
  22. struct arm_smccc_res res;
  23. u64 input_address;
  24. u64 output_address;
  25. input_address = input ? virt_to_phys(input) : 0;
  26. output_address = output ? virt_to_phys(output) : 0;
  27. arm_smccc_1_1_hvc(HV_FUNC_ID, control,
  28. input_address, output_address, &res);
  29. return res.a0;
  30. }
  31. EXPORT_SYMBOL_GPL(hv_do_hypercall);
  32. /*
  33. * hv_do_fast_hypercall8 -- Invoke the specified hypercall
  34. * with arguments in registers instead of physical memory.
  35. * Avoids the overhead of virt_to_phys for simple hypercalls.
  36. */
  37. u64 hv_do_fast_hypercall8(u16 code, u64 input)
  38. {
  39. struct arm_smccc_res res;
  40. u64 control;
  41. control = (u64)code | HV_HYPERCALL_FAST_BIT;
  42. arm_smccc_1_1_hvc(HV_FUNC_ID, control, input, &res);
  43. return res.a0;
  44. }
  45. EXPORT_SYMBOL_GPL(hv_do_fast_hypercall8);
  46. /*
  47. * hv_do_fast_hypercall16 -- Invoke the specified hypercall
  48. * with arguments in registers instead of physical memory.
  49. * Avoids the overhead of virt_to_phys for simple hypercalls.
  50. */
  51. u64 hv_do_fast_hypercall16(u16 code, u64 input1, u64 input2)
  52. {
  53. struct arm_smccc_res res;
  54. u64 control;
  55. control = (u64)code | HV_HYPERCALL_FAST_BIT;
  56. arm_smccc_1_1_hvc(HV_FUNC_ID, control, input1, input2, &res);
  57. return res.a0;
  58. }
  59. EXPORT_SYMBOL_GPL(hv_do_fast_hypercall16);
  60. /*
  61. * Set a single VP register to a 64-bit value.
  62. */
  63. void hv_set_vpreg(u32 msr, u64 value)
  64. {
  65. struct arm_smccc_res res;
  66. arm_smccc_1_1_hvc(HV_FUNC_ID,
  67. HVCALL_SET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
  68. HV_HYPERCALL_REP_COMP_1,
  69. HV_PARTITION_ID_SELF,
  70. HV_VP_INDEX_SELF,
  71. msr,
  72. 0,
  73. value,
  74. 0,
  75. &res);
  76. /*
  77. * Something is fundamentally broken in the hypervisor if
  78. * setting a VP register fails. There's really no way to
  79. * continue as a guest VM, so panic.
  80. */
  81. BUG_ON(!hv_result_success(res.a0));
  82. }
  83. EXPORT_SYMBOL_GPL(hv_set_vpreg);
  84. /*
  85. * Get the value of a single VP register. One version
  86. * returns just 64 bits and another returns the full 128 bits.
  87. * The two versions are separate to avoid complicating the
  88. * calling sequence for the more frequently used 64 bit version.
  89. */
  90. void hv_get_vpreg_128(u32 msr, struct hv_get_vp_registers_output *result)
  91. {
  92. struct arm_smccc_1_2_regs args;
  93. struct arm_smccc_1_2_regs res;
  94. args.a0 = HV_FUNC_ID;
  95. args.a1 = HVCALL_GET_VP_REGISTERS | HV_HYPERCALL_FAST_BIT |
  96. HV_HYPERCALL_REP_COMP_1;
  97. args.a2 = HV_PARTITION_ID_SELF;
  98. args.a3 = HV_VP_INDEX_SELF;
  99. args.a4 = msr;
  100. /*
  101. * Use the SMCCC 1.2 interface because the results are in registers
  102. * beyond X0-X3.
  103. */
  104. arm_smccc_1_2_hvc(&args, &res);
  105. /*
  106. * Something is fundamentally broken in the hypervisor if
  107. * getting a VP register fails. There's really no way to
  108. * continue as a guest VM, so panic.
  109. */
  110. BUG_ON(!hv_result_success(res.a0));
  111. result->as64.low = res.a6;
  112. result->as64.high = res.a7;
  113. }
  114. EXPORT_SYMBOL_GPL(hv_get_vpreg_128);
  115. u64 hv_get_vpreg(u32 msr)
  116. {
  117. struct hv_get_vp_registers_output output;
  118. hv_get_vpreg_128(msr, &output);
  119. return output.as64.low;
  120. }
  121. EXPORT_SYMBOL_GPL(hv_get_vpreg);
  122. /*
  123. * hyperv_report_panic - report a panic to Hyper-V. This function uses
  124. * the older version of the Hyper-V interface that admittedly doesn't
  125. * pass enough information to be useful beyond just recording the
  126. * occurrence of a panic. The parallel hv_kmsg_dump() uses the
  127. * new interface that allows reporting 4 Kbytes of data, which is much
  128. * more useful. Hyper-V on ARM64 always supports the newer interface, but
  129. * we retain support for the older version because the sysadmin is allowed
  130. * to disable the newer version via sysctl in case of information security
  131. * concerns about the more verbose version.
  132. */
  133. void hyperv_report_panic(struct pt_regs *regs, long err, bool in_die)
  134. {
  135. static bool panic_reported;
  136. u64 guest_id;
  137. /* Don't report a panic to Hyper-V if we're not going to panic */
  138. if (in_die && !panic_on_oops)
  139. return;
  140. /*
  141. * We prefer to report panic on 'die' chain as we have proper
  142. * registers to report, but if we miss it (e.g. on BUG()) we need
  143. * to report it on 'panic'.
  144. *
  145. * Calling code in the 'die' and 'panic' paths ensures that only
  146. * one CPU is running this code, so no atomicity is needed.
  147. */
  148. if (panic_reported)
  149. return;
  150. panic_reported = true;
  151. guest_id = hv_get_vpreg(HV_REGISTER_GUEST_OS_ID);
  152. /*
  153. * Hyper-V provides the ability to store only 5 values.
  154. * Pick the passed in error value, the guest_id, the PC,
  155. * and the SP.
  156. */
  157. hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P0, err);
  158. hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P1, guest_id);
  159. hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P2, regs->pc);
  160. hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P3, regs->sp);
  161. hv_set_vpreg(HV_REGISTER_GUEST_CRASH_P4, 0);
  162. /*
  163. * Let Hyper-V know there is crash data available
  164. */
  165. hv_set_vpreg(HV_REGISTER_GUEST_CRASH_CTL, HV_CRASH_CTL_CRASH_NOTIFY);
  166. }
  167. EXPORT_SYMBOL_GPL(hyperv_report_panic);