/arch/arm64/hyperv/hv_core.c

https://github.com/tekkamanninja/linux · C · 181 lines · 87 code · 27 blank · 67 comment · 3 complexity · 5fd0ddb6b6a94dafce144cb6ec121fbf MD5 · raw file

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