handle_exit.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012,2013 - ARM Ltd
  4. * Author: Marc Zyngier <[email protected]>
  5. *
  6. * Derived from arch/arm/kvm/handle_exit.c:
  7. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  8. * Author: Christoffer Dall <[email protected]>
  9. */
  10. #include <linux/kvm.h>
  11. #include <linux/kvm_host.h>
  12. #include <asm/esr.h>
  13. #include <asm/exception.h>
  14. #include <asm/kvm_asm.h>
  15. #include <asm/kvm_emulate.h>
  16. #include <asm/kvm_mmu.h>
  17. #include <asm/debug-monitors.h>
  18. #include <asm/stacktrace/nvhe.h>
  19. #include <asm/traps.h>
  20. #include <kvm/arm_hypercalls.h>
  21. #define CREATE_TRACE_POINTS
  22. #include "trace_handle_exit.h"
  23. typedef int (*exit_handle_fn)(struct kvm_vcpu *);
  24. static void kvm_handle_guest_serror(struct kvm_vcpu *vcpu, u64 esr)
  25. {
  26. if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(NULL, esr))
  27. kvm_inject_vabt(vcpu);
  28. }
  29. static int handle_hvc(struct kvm_vcpu *vcpu)
  30. {
  31. int ret;
  32. trace_kvm_hvc_arm64(*vcpu_pc(vcpu), vcpu_get_reg(vcpu, 0),
  33. kvm_vcpu_hvc_get_imm(vcpu));
  34. vcpu->stat.hvc_exit_stat++;
  35. ret = kvm_hvc_call_handler(vcpu);
  36. if (ret < 0) {
  37. vcpu_set_reg(vcpu, 0, ~0UL);
  38. return 1;
  39. }
  40. return ret;
  41. }
  42. static int handle_smc(struct kvm_vcpu *vcpu)
  43. {
  44. /*
  45. * "If an SMC instruction executed at Non-secure EL1 is
  46. * trapped to EL2 because HCR_EL2.TSC is 1, the exception is a
  47. * Trap exception, not a Secure Monitor Call exception [...]"
  48. *
  49. * We need to advance the PC after the trap, as it would
  50. * otherwise return to the same address...
  51. */
  52. vcpu_set_reg(vcpu, 0, ~0UL);
  53. kvm_incr_pc(vcpu);
  54. return 1;
  55. }
  56. /*
  57. * Guest access to FP/ASIMD registers are routed to this handler only
  58. * when the system doesn't support FP/ASIMD.
  59. */
  60. static int handle_no_fpsimd(struct kvm_vcpu *vcpu)
  61. {
  62. kvm_inject_undefined(vcpu);
  63. return 1;
  64. }
  65. /**
  66. * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event
  67. * instruction executed by a guest
  68. *
  69. * @vcpu: the vcpu pointer
  70. *
  71. * WFE[T]: Yield the CPU and come back to this vcpu when the scheduler
  72. * decides to.
  73. * WFI: Simply call kvm_vcpu_halt(), which will halt execution of
  74. * world-switches and schedule other host processes until there is an
  75. * incoming IRQ or FIQ to the VM.
  76. * WFIT: Same as WFI, with a timed wakeup implemented as a background timer
  77. *
  78. * WF{I,E}T can immediately return if the deadline has already expired.
  79. */
  80. static int kvm_handle_wfx(struct kvm_vcpu *vcpu)
  81. {
  82. u64 esr = kvm_vcpu_get_esr(vcpu);
  83. if (esr & ESR_ELx_WFx_ISS_WFE) {
  84. trace_kvm_wfx_arm64(*vcpu_pc(vcpu), true);
  85. vcpu->stat.wfe_exit_stat++;
  86. } else {
  87. trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false);
  88. vcpu->stat.wfi_exit_stat++;
  89. }
  90. if (esr & ESR_ELx_WFx_ISS_WFxT) {
  91. if (esr & ESR_ELx_WFx_ISS_RV) {
  92. u64 val, now;
  93. now = kvm_arm_timer_get_reg(vcpu, KVM_REG_ARM_TIMER_CNT);
  94. val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
  95. if (now >= val)
  96. goto out;
  97. } else {
  98. /* Treat WFxT as WFx if RN is invalid */
  99. esr &= ~ESR_ELx_WFx_ISS_WFxT;
  100. }
  101. }
  102. if (esr & ESR_ELx_WFx_ISS_WFE) {
  103. kvm_vcpu_on_spin(vcpu, vcpu_mode_priv(vcpu));
  104. } else {
  105. if (esr & ESR_ELx_WFx_ISS_WFxT)
  106. vcpu_set_flag(vcpu, IN_WFIT);
  107. kvm_vcpu_wfi(vcpu);
  108. }
  109. out:
  110. kvm_incr_pc(vcpu);
  111. return 1;
  112. }
  113. /**
  114. * kvm_handle_guest_debug - handle a debug exception instruction
  115. *
  116. * @vcpu: the vcpu pointer
  117. *
  118. * We route all debug exceptions through the same handler. If both the
  119. * guest and host are using the same debug facilities it will be up to
  120. * userspace to re-inject the correct exception for guest delivery.
  121. *
  122. * @return: 0 (while setting vcpu->run->exit_reason)
  123. */
  124. static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu)
  125. {
  126. struct kvm_run *run = vcpu->run;
  127. u64 esr = kvm_vcpu_get_esr(vcpu);
  128. run->exit_reason = KVM_EXIT_DEBUG;
  129. run->debug.arch.hsr = lower_32_bits(esr);
  130. run->debug.arch.hsr_high = upper_32_bits(esr);
  131. run->flags = KVM_DEBUG_ARCH_HSR_HIGH_VALID;
  132. switch (ESR_ELx_EC(esr)) {
  133. case ESR_ELx_EC_WATCHPT_LOW:
  134. run->debug.arch.far = vcpu->arch.fault.far_el2;
  135. break;
  136. case ESR_ELx_EC_SOFTSTP_LOW:
  137. vcpu_clear_flag(vcpu, DBG_SS_ACTIVE_PENDING);
  138. break;
  139. }
  140. return 0;
  141. }
  142. static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
  143. {
  144. u64 esr = kvm_vcpu_get_esr(vcpu);
  145. kvm_pr_unimpl("Unknown exception class: esr: %#016llx -- %s\n",
  146. esr, esr_get_class_string(esr));
  147. kvm_inject_undefined(vcpu);
  148. return 1;
  149. }
  150. /*
  151. * Guest access to SVE registers should be routed to this handler only
  152. * when the system doesn't support SVE.
  153. */
  154. static int handle_sve(struct kvm_vcpu *vcpu)
  155. {
  156. kvm_inject_undefined(vcpu);
  157. return 1;
  158. }
  159. /*
  160. * Guest usage of a ptrauth instruction (which the guest EL1 did not turn into
  161. * a NOP). If we get here, it is that we didn't fixup ptrauth on exit, and all
  162. * that we can do is give the guest an UNDEF.
  163. */
  164. static int kvm_handle_ptrauth(struct kvm_vcpu *vcpu)
  165. {
  166. kvm_inject_undefined(vcpu);
  167. return 1;
  168. }
  169. static exit_handle_fn arm_exit_handlers[] = {
  170. [0 ... ESR_ELx_EC_MAX] = kvm_handle_unknown_ec,
  171. [ESR_ELx_EC_WFx] = kvm_handle_wfx,
  172. [ESR_ELx_EC_CP15_32] = kvm_handle_cp15_32,
  173. [ESR_ELx_EC_CP15_64] = kvm_handle_cp15_64,
  174. [ESR_ELx_EC_CP14_MR] = kvm_handle_cp14_32,
  175. [ESR_ELx_EC_CP14_LS] = kvm_handle_cp14_load_store,
  176. [ESR_ELx_EC_CP10_ID] = kvm_handle_cp10_id,
  177. [ESR_ELx_EC_CP14_64] = kvm_handle_cp14_64,
  178. [ESR_ELx_EC_HVC32] = handle_hvc,
  179. [ESR_ELx_EC_SMC32] = handle_smc,
  180. [ESR_ELx_EC_HVC64] = handle_hvc,
  181. [ESR_ELx_EC_SMC64] = handle_smc,
  182. [ESR_ELx_EC_SYS64] = kvm_handle_sys_reg,
  183. [ESR_ELx_EC_SVE] = handle_sve,
  184. [ESR_ELx_EC_IABT_LOW] = kvm_handle_guest_abort,
  185. [ESR_ELx_EC_DABT_LOW] = kvm_handle_guest_abort,
  186. [ESR_ELx_EC_SOFTSTP_LOW]= kvm_handle_guest_debug,
  187. [ESR_ELx_EC_WATCHPT_LOW]= kvm_handle_guest_debug,
  188. [ESR_ELx_EC_BREAKPT_LOW]= kvm_handle_guest_debug,
  189. [ESR_ELx_EC_BKPT32] = kvm_handle_guest_debug,
  190. [ESR_ELx_EC_BRK64] = kvm_handle_guest_debug,
  191. [ESR_ELx_EC_FP_ASIMD] = handle_no_fpsimd,
  192. [ESR_ELx_EC_PAC] = kvm_handle_ptrauth,
  193. };
  194. static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
  195. {
  196. u64 esr = kvm_vcpu_get_esr(vcpu);
  197. u8 esr_ec = ESR_ELx_EC(esr);
  198. return arm_exit_handlers[esr_ec];
  199. }
  200. /*
  201. * We may be single-stepping an emulated instruction. If the emulation
  202. * has been completed in the kernel, we can return to userspace with a
  203. * KVM_EXIT_DEBUG, otherwise userspace needs to complete its
  204. * emulation first.
  205. */
  206. static int handle_trap_exceptions(struct kvm_vcpu *vcpu)
  207. {
  208. int handled;
  209. /*
  210. * If we run a non-protected VM when protection is enabled
  211. * system-wide, resync the state from the hypervisor and mark
  212. * it as dirty on the host side if it wasn't dirty already
  213. * (which could happen if preemption has taken place).
  214. */
  215. if (is_protected_kvm_enabled() && !kvm_vm_is_protected(vcpu->kvm)) {
  216. preempt_disable();
  217. if (!(vcpu_get_flag(vcpu, PKVM_HOST_STATE_DIRTY))) {
  218. kvm_call_hyp_nvhe(__pkvm_vcpu_sync_state);
  219. vcpu_set_flag(vcpu, PKVM_HOST_STATE_DIRTY);
  220. }
  221. preempt_enable();
  222. }
  223. /*
  224. * See ARM ARM B1.14.1: "Hyp traps on instructions
  225. * that fail their condition code check"
  226. */
  227. if (!kvm_condition_valid(vcpu)) {
  228. kvm_incr_pc(vcpu);
  229. handled = 1;
  230. } else {
  231. exit_handle_fn exit_handler;
  232. exit_handler = kvm_get_exit_handler(vcpu);
  233. handled = exit_handler(vcpu);
  234. }
  235. return handled;
  236. }
  237. /*
  238. * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
  239. * proper exit to userspace.
  240. */
  241. int handle_exit(struct kvm_vcpu *vcpu, int exception_index)
  242. {
  243. struct kvm_run *run = vcpu->run;
  244. if (ARM_SERROR_PENDING(exception_index)) {
  245. /*
  246. * The SError is handled by handle_exit_early(). If the guest
  247. * survives it will re-execute the original instruction.
  248. */
  249. return 1;
  250. }
  251. exception_index = ARM_EXCEPTION_CODE(exception_index);
  252. switch (exception_index) {
  253. case ARM_EXCEPTION_IRQ:
  254. return 1;
  255. case ARM_EXCEPTION_EL1_SERROR:
  256. return 1;
  257. case ARM_EXCEPTION_TRAP:
  258. return handle_trap_exceptions(vcpu);
  259. case ARM_EXCEPTION_HYP_GONE:
  260. /*
  261. * EL2 has been reset to the hyp-stub. This happens when a guest
  262. * is pre-emptied by kvm_reboot()'s shutdown call.
  263. */
  264. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  265. return 0;
  266. case ARM_EXCEPTION_IL:
  267. /*
  268. * We attempted an illegal exception return. Guest state must
  269. * have been corrupted somehow. Give up.
  270. */
  271. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  272. return -EINVAL;
  273. default:
  274. kvm_pr_unimpl("Unsupported exception type: %d",
  275. exception_index);
  276. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  277. return 0;
  278. }
  279. }
  280. /* For exit types that need handling before we can be preempted */
  281. void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index)
  282. {
  283. /*
  284. * We just exited, so the state is clean from a hypervisor
  285. * perspective.
  286. */
  287. if (is_protected_kvm_enabled())
  288. vcpu_clear_flag(vcpu, PKVM_HOST_STATE_DIRTY);
  289. if (ARM_SERROR_PENDING(exception_index)) {
  290. if (this_cpu_has_cap(ARM64_HAS_RAS_EXTN)) {
  291. u64 disr = kvm_vcpu_get_disr(vcpu);
  292. kvm_handle_guest_serror(vcpu, disr_to_esr(disr));
  293. } else {
  294. kvm_inject_vabt(vcpu);
  295. }
  296. return;
  297. }
  298. exception_index = ARM_EXCEPTION_CODE(exception_index);
  299. if (exception_index == ARM_EXCEPTION_EL1_SERROR)
  300. kvm_handle_guest_serror(vcpu, kvm_vcpu_get_esr(vcpu));
  301. }
  302. void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr,
  303. u64 elr_virt, u64 elr_phys,
  304. u64 par, uintptr_t vcpu,
  305. u64 far, u64 hpfar) {
  306. u64 elr_in_kimg = __phys_to_kimg(elr_phys);
  307. u64 hyp_offset = elr_in_kimg - kaslr_offset() - elr_virt;
  308. u64 mode = spsr & PSR_MODE_MASK;
  309. u64 panic_addr = elr_virt + hyp_offset;
  310. if (mode != PSR_MODE_EL2t && mode != PSR_MODE_EL2h) {
  311. kvm_err("Invalid host exception to nVHE hyp!\n");
  312. } else if (ESR_ELx_EC(esr) == ESR_ELx_EC_BRK64 &&
  313. (esr & ESR_ELx_BRK64_ISS_COMMENT_MASK) == BUG_BRK_IMM) {
  314. const char *file = NULL;
  315. unsigned int line = 0;
  316. /* All hyp bugs, including warnings, are treated as fatal. */
  317. if (!is_protected_kvm_enabled() ||
  318. IS_ENABLED(CONFIG_NVHE_EL2_DEBUG)) {
  319. struct bug_entry *bug = find_bug(elr_in_kimg);
  320. if (bug)
  321. bug_get_file_line(bug, &file, &line);
  322. }
  323. if (file)
  324. kvm_err("nVHE hyp BUG at: %s:%u!\n", file, line);
  325. else
  326. kvm_err("nVHE hyp BUG at: [<%016llx>] %pB!\n", panic_addr,
  327. (void *)(panic_addr + kaslr_offset()));
  328. } else {
  329. kvm_err("nVHE hyp panic at: [<%016llx>] %pB!\n", panic_addr,
  330. (void *)(panic_addr + kaslr_offset()));
  331. }
  332. /* Dump the nVHE hypervisor backtrace */
  333. kvm_nvhe_dump_backtrace(hyp_offset);
  334. /*
  335. * Hyp has panicked and we're going to handle that by panicking the
  336. * kernel. The kernel offset will be revealed in the panic so we're
  337. * also safe to reveal the hyp offset as a debugging aid for translating
  338. * hyp VAs to vmlinux addresses.
  339. */
  340. kvm_err("Hyp Offset: 0x%llx\n", hyp_offset);
  341. panic("HYP panic:\nPS:%08llx PC:%016llx ESR:%016llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%016lx\n",
  342. spsr, elr_virt, esr, far, hpfar, par, vcpu);
  343. }