handle_exit.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Copyright (C) 2012,2013 - ARM Ltd
  4. * Author: Marc Zyngier <marc.zyngier@arm.com>
  5. *
  6. * Derived from arch/arm/kvm/handle_exit.c:
  7. * Copyright (C) 2012 - Virtual Open Systems and Columbia University
  8. * Author: Christoffer Dall <c.dall@virtualopensystems.com>
  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/kvm_nested.h>
  18. #include <asm/debug-monitors.h>
  19. #include <asm/stacktrace/nvhe.h>
  20. #include <asm/traps.h>
  21. #include <kvm/arm_hypercalls.h>
  22. #define CREATE_TRACE_POINTS
  23. #include "trace_handle_exit.h"
  24. typedef int (*exit_handle_fn)(struct kvm_vcpu *);
  25. static void kvm_handle_guest_serror(struct kvm_vcpu *vcpu, u64 esr)
  26. {
  27. if (!arm64_is_ras_serror(esr) || arm64_is_fatal_ras_serror(NULL, esr))
  28. kvm_inject_vabt(vcpu);
  29. }
  30. static int handle_hvc(struct kvm_vcpu *vcpu)
  31. {
  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. /* Forward hvc instructions to the virtual EL2 if the guest has EL2. */
  36. if (vcpu_has_nv(vcpu)) {
  37. if (vcpu_read_sys_reg(vcpu, HCR_EL2) & HCR_HCD)
  38. kvm_inject_undefined(vcpu);
  39. else
  40. kvm_inject_nested_sync(vcpu, kvm_vcpu_get_esr(vcpu));
  41. return 1;
  42. }
  43. return kvm_smccc_call_handler(vcpu);
  44. }
  45. static int handle_smc(struct kvm_vcpu *vcpu)
  46. {
  47. /*
  48. * Forward this trapped smc instruction to the virtual EL2 if
  49. * the guest has asked for it.
  50. */
  51. if (forward_smc_trap(vcpu))
  52. return 1;
  53. /*
  54. * "If an SMC instruction executed at Non-secure EL1 is
  55. * trapped to EL2 because HCR_EL2.TSC is 1, the exception is a
  56. * Trap exception, not a Secure Monitor Call exception [...]"
  57. *
  58. * We need to advance the PC after the trap, as it would
  59. * otherwise return to the same address. Furthermore, pre-incrementing
  60. * the PC before potentially exiting to userspace maintains the same
  61. * abstraction for both SMCs and HVCs.
  62. */
  63. kvm_incr_pc(vcpu);
  64. /*
  65. * SMCs with a nonzero immediate are reserved according to DEN0028E 2.9
  66. * "SMC and HVC immediate value".
  67. */
  68. if (kvm_vcpu_hvc_get_imm(vcpu)) {
  69. vcpu_set_reg(vcpu, 0, ~0UL);
  70. return 1;
  71. }
  72. /*
  73. * If imm is zero then it is likely an SMCCC call.
  74. *
  75. * Note that on ARMv8.3, even if EL3 is not implemented, SMC executed
  76. * at Non-secure EL1 is trapped to EL2 if HCR_EL2.TSC==1, rather than
  77. * being treated as UNDEFINED.
  78. */
  79. return kvm_smccc_call_handler(vcpu);
  80. }
  81. /*
  82. * This handles the cases where the system does not support FP/ASIMD or when
  83. * we are running nested virtualization and the guest hypervisor is trapping
  84. * FP/ASIMD accesses by its guest guest.
  85. *
  86. * All other handling of guest vs. host FP/ASIMD register state is handled in
  87. * fixup_guest_exit().
  88. */
  89. static int kvm_handle_fpasimd(struct kvm_vcpu *vcpu)
  90. {
  91. if (guest_hyp_fpsimd_traps_enabled(vcpu))
  92. return kvm_inject_nested_sync(vcpu, kvm_vcpu_get_esr(vcpu));
  93. /* This is the case when the system doesn't support FP/ASIMD. */
  94. kvm_inject_undefined(vcpu);
  95. return 1;
  96. }
  97. /**
  98. * kvm_handle_wfx - handle a wait-for-interrupts or wait-for-event
  99. * instruction executed by a guest
  100. *
  101. * @vcpu: the vcpu pointer
  102. *
  103. * WFE[T]: Yield the CPU and come back to this vcpu when the scheduler
  104. * decides to.
  105. * WFI: Simply call kvm_vcpu_halt(), which will halt execution of
  106. * world-switches and schedule other host processes until there is an
  107. * incoming IRQ or FIQ to the VM.
  108. * WFIT: Same as WFI, with a timed wakeup implemented as a background timer
  109. *
  110. * WF{I,E}T can immediately return if the deadline has already expired.
  111. */
  112. static int kvm_handle_wfx(struct kvm_vcpu *vcpu)
  113. {
  114. u64 esr = kvm_vcpu_get_esr(vcpu);
  115. if (esr & ESR_ELx_WFx_ISS_WFE) {
  116. trace_kvm_wfx_arm64(*vcpu_pc(vcpu), true);
  117. vcpu->stat.wfe_exit_stat++;
  118. } else {
  119. trace_kvm_wfx_arm64(*vcpu_pc(vcpu), false);
  120. vcpu->stat.wfi_exit_stat++;
  121. }
  122. if (esr & ESR_ELx_WFx_ISS_WFxT) {
  123. if (esr & ESR_ELx_WFx_ISS_RV) {
  124. u64 val, now;
  125. now = kvm_arm_timer_get_reg(vcpu, KVM_REG_ARM_TIMER_CNT);
  126. val = vcpu_get_reg(vcpu, kvm_vcpu_sys_get_rt(vcpu));
  127. if (now >= val)
  128. goto out;
  129. } else {
  130. /* Treat WFxT as WFx if RN is invalid */
  131. esr &= ~ESR_ELx_WFx_ISS_WFxT;
  132. }
  133. }
  134. if (esr & ESR_ELx_WFx_ISS_WFE) {
  135. kvm_vcpu_on_spin(vcpu, vcpu_mode_priv(vcpu));
  136. } else {
  137. if (esr & ESR_ELx_WFx_ISS_WFxT)
  138. vcpu_set_flag(vcpu, IN_WFIT);
  139. kvm_vcpu_wfi(vcpu);
  140. }
  141. out:
  142. kvm_incr_pc(vcpu);
  143. return 1;
  144. }
  145. /**
  146. * kvm_handle_guest_debug - handle a debug exception instruction
  147. *
  148. * @vcpu: the vcpu pointer
  149. *
  150. * We route all debug exceptions through the same handler. If both the
  151. * guest and host are using the same debug facilities it will be up to
  152. * userspace to re-inject the correct exception for guest delivery.
  153. *
  154. * @return: 0 (while setting vcpu->run->exit_reason)
  155. */
  156. static int kvm_handle_guest_debug(struct kvm_vcpu *vcpu)
  157. {
  158. struct kvm_run *run = vcpu->run;
  159. u64 esr = kvm_vcpu_get_esr(vcpu);
  160. run->exit_reason = KVM_EXIT_DEBUG;
  161. run->debug.arch.hsr = lower_32_bits(esr);
  162. run->debug.arch.hsr_high = upper_32_bits(esr);
  163. run->flags = KVM_DEBUG_ARCH_HSR_HIGH_VALID;
  164. switch (ESR_ELx_EC(esr)) {
  165. case ESR_ELx_EC_WATCHPT_LOW:
  166. run->debug.arch.far = vcpu->arch.fault.far_el2;
  167. break;
  168. case ESR_ELx_EC_SOFTSTP_LOW:
  169. vcpu_clear_flag(vcpu, DBG_SS_ACTIVE_PENDING);
  170. break;
  171. }
  172. return 0;
  173. }
  174. static int kvm_handle_unknown_ec(struct kvm_vcpu *vcpu)
  175. {
  176. u64 esr = kvm_vcpu_get_esr(vcpu);
  177. kvm_pr_unimpl("Unknown exception class: esr: %#016llx -- %s\n",
  178. esr, esr_get_class_string(esr));
  179. kvm_inject_undefined(vcpu);
  180. return 1;
  181. }
  182. /*
  183. * Guest access to SVE registers should be routed to this handler only
  184. * when the system doesn't support SVE.
  185. */
  186. static int handle_sve(struct kvm_vcpu *vcpu)
  187. {
  188. if (guest_hyp_sve_traps_enabled(vcpu))
  189. return kvm_inject_nested_sync(vcpu, kvm_vcpu_get_esr(vcpu));
  190. kvm_inject_undefined(vcpu);
  191. return 1;
  192. }
  193. /*
  194. * Two possibilities to handle a trapping ptrauth instruction:
  195. *
  196. * - Guest usage of a ptrauth instruction (which the guest EL1 did not
  197. * turn into a NOP). If we get here, it is because we didn't enable
  198. * ptrauth for the guest. This results in an UNDEF, as it isn't
  199. * supposed to use ptrauth without being told it could.
  200. *
  201. * - Running an L2 NV guest while L1 has left HCR_EL2.API==0, and for
  202. * which we reinject the exception into L1.
  203. *
  204. * Anything else is an emulation bug (hence the WARN_ON + UNDEF).
  205. */
  206. static int kvm_handle_ptrauth(struct kvm_vcpu *vcpu)
  207. {
  208. if (!vcpu_has_ptrauth(vcpu)) {
  209. kvm_inject_undefined(vcpu);
  210. return 1;
  211. }
  212. if (vcpu_has_nv(vcpu) && !is_hyp_ctxt(vcpu)) {
  213. kvm_inject_nested_sync(vcpu, kvm_vcpu_get_esr(vcpu));
  214. return 1;
  215. }
  216. /* Really shouldn't be here! */
  217. WARN_ON_ONCE(1);
  218. kvm_inject_undefined(vcpu);
  219. return 1;
  220. }
  221. static int kvm_handle_eret(struct kvm_vcpu *vcpu)
  222. {
  223. if (esr_iss_is_eretax(kvm_vcpu_get_esr(vcpu)) &&
  224. !vcpu_has_ptrauth(vcpu))
  225. return kvm_handle_ptrauth(vcpu);
  226. /*
  227. * If we got here, two possibilities:
  228. *
  229. * - the guest is in EL2, and we need to fully emulate ERET
  230. *
  231. * - the guest is in EL1, and we need to reinject the
  232. * exception into the L1 hypervisor.
  233. *
  234. * If KVM ever traps ERET for its own use, we'll have to
  235. * revisit this.
  236. */
  237. if (is_hyp_ctxt(vcpu))
  238. kvm_emulate_nested_eret(vcpu);
  239. else
  240. kvm_inject_nested_sync(vcpu, kvm_vcpu_get_esr(vcpu));
  241. return 1;
  242. }
  243. static int handle_svc(struct kvm_vcpu *vcpu)
  244. {
  245. /*
  246. * So far, SVC traps only for NV via HFGITR_EL2. A SVC from a
  247. * 32bit guest would be caught by vpcu_mode_is_bad_32bit(), so
  248. * we should only have to deal with a 64 bit exception.
  249. */
  250. kvm_inject_nested_sync(vcpu, kvm_vcpu_get_esr(vcpu));
  251. return 1;
  252. }
  253. static exit_handle_fn arm_exit_handlers[] = {
  254. [0 ... ESR_ELx_EC_MAX] = kvm_handle_unknown_ec,
  255. [ESR_ELx_EC_WFx] = kvm_handle_wfx,
  256. [ESR_ELx_EC_CP15_32] = kvm_handle_cp15_32,
  257. [ESR_ELx_EC_CP15_64] = kvm_handle_cp15_64,
  258. [ESR_ELx_EC_CP14_MR] = kvm_handle_cp14_32,
  259. [ESR_ELx_EC_CP14_LS] = kvm_handle_cp14_load_store,
  260. [ESR_ELx_EC_CP10_ID] = kvm_handle_cp10_id,
  261. [ESR_ELx_EC_CP14_64] = kvm_handle_cp14_64,
  262. [ESR_ELx_EC_HVC32] = handle_hvc,
  263. [ESR_ELx_EC_SMC32] = handle_smc,
  264. [ESR_ELx_EC_HVC64] = handle_hvc,
  265. [ESR_ELx_EC_SMC64] = handle_smc,
  266. [ESR_ELx_EC_SVC64] = handle_svc,
  267. [ESR_ELx_EC_SYS64] = kvm_handle_sys_reg,
  268. [ESR_ELx_EC_SVE] = handle_sve,
  269. [ESR_ELx_EC_ERET] = kvm_handle_eret,
  270. [ESR_ELx_EC_IABT_LOW] = kvm_handle_guest_abort,
  271. [ESR_ELx_EC_DABT_LOW] = kvm_handle_guest_abort,
  272. [ESR_ELx_EC_SOFTSTP_LOW]= kvm_handle_guest_debug,
  273. [ESR_ELx_EC_WATCHPT_LOW]= kvm_handle_guest_debug,
  274. [ESR_ELx_EC_BREAKPT_LOW]= kvm_handle_guest_debug,
  275. [ESR_ELx_EC_BKPT32] = kvm_handle_guest_debug,
  276. [ESR_ELx_EC_BRK64] = kvm_handle_guest_debug,
  277. [ESR_ELx_EC_FP_ASIMD] = kvm_handle_fpasimd,
  278. [ESR_ELx_EC_PAC] = kvm_handle_ptrauth,
  279. };
  280. static exit_handle_fn kvm_get_exit_handler(struct kvm_vcpu *vcpu)
  281. {
  282. u64 esr = kvm_vcpu_get_esr(vcpu);
  283. u8 esr_ec = ESR_ELx_EC(esr);
  284. return arm_exit_handlers[esr_ec];
  285. }
  286. /*
  287. * We may be single-stepping an emulated instruction. If the emulation
  288. * has been completed in the kernel, we can return to userspace with a
  289. * KVM_EXIT_DEBUG, otherwise userspace needs to complete its
  290. * emulation first.
  291. */
  292. static int handle_trap_exceptions(struct kvm_vcpu *vcpu)
  293. {
  294. int handled;
  295. /*
  296. * See ARM ARM B1.14.1: "Hyp traps on instructions
  297. * that fail their condition code check"
  298. */
  299. if (!kvm_condition_valid(vcpu)) {
  300. kvm_incr_pc(vcpu);
  301. handled = 1;
  302. } else {
  303. exit_handle_fn exit_handler;
  304. exit_handler = kvm_get_exit_handler(vcpu);
  305. handled = exit_handler(vcpu);
  306. }
  307. return handled;
  308. }
  309. /*
  310. * Return > 0 to return to guest, < 0 on error, 0 (and set exit_reason) on
  311. * proper exit to userspace.
  312. */
  313. int handle_exit(struct kvm_vcpu *vcpu, int exception_index)
  314. {
  315. struct kvm_run *run = vcpu->run;
  316. if (ARM_SERROR_PENDING(exception_index)) {
  317. /*
  318. * The SError is handled by handle_exit_early(). If the guest
  319. * survives it will re-execute the original instruction.
  320. */
  321. return 1;
  322. }
  323. exception_index = ARM_EXCEPTION_CODE(exception_index);
  324. switch (exception_index) {
  325. case ARM_EXCEPTION_IRQ:
  326. return 1;
  327. case ARM_EXCEPTION_EL1_SERROR:
  328. return 1;
  329. case ARM_EXCEPTION_TRAP:
  330. return handle_trap_exceptions(vcpu);
  331. case ARM_EXCEPTION_HYP_GONE:
  332. /*
  333. * EL2 has been reset to the hyp-stub. This happens when a guest
  334. * is pre-emptied by kvm_reboot()'s shutdown call.
  335. */
  336. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  337. return 0;
  338. case ARM_EXCEPTION_IL:
  339. /*
  340. * We attempted an illegal exception return. Guest state must
  341. * have been corrupted somehow. Give up.
  342. */
  343. run->exit_reason = KVM_EXIT_FAIL_ENTRY;
  344. return -EINVAL;
  345. default:
  346. kvm_pr_unimpl("Unsupported exception type: %d",
  347. exception_index);
  348. run->exit_reason = KVM_EXIT_INTERNAL_ERROR;
  349. return 0;
  350. }
  351. }
  352. /* For exit types that need handling before we can be preempted */
  353. void handle_exit_early(struct kvm_vcpu *vcpu, int exception_index)
  354. {
  355. if (ARM_SERROR_PENDING(exception_index)) {
  356. if (this_cpu_has_cap(ARM64_HAS_RAS_EXTN)) {
  357. u64 disr = kvm_vcpu_get_disr(vcpu);
  358. kvm_handle_guest_serror(vcpu, disr_to_esr(disr));
  359. } else {
  360. kvm_inject_vabt(vcpu);
  361. }
  362. return;
  363. }
  364. exception_index = ARM_EXCEPTION_CODE(exception_index);
  365. if (exception_index == ARM_EXCEPTION_EL1_SERROR)
  366. kvm_handle_guest_serror(vcpu, kvm_vcpu_get_esr(vcpu));
  367. }
  368. static void print_nvhe_hyp_panic(const char *name, u64 panic_addr)
  369. {
  370. kvm_err("nVHE hyp %s at: [<%016llx>] %pB!\n", name, panic_addr,
  371. (void *)(panic_addr + kaslr_offset()));
  372. }
  373. static void kvm_nvhe_report_cfi_failure(u64 panic_addr)
  374. {
  375. print_nvhe_hyp_panic("CFI failure", panic_addr);
  376. if (IS_ENABLED(CONFIG_CFI_PERMISSIVE))
  377. kvm_err(" (CONFIG_CFI_PERMISSIVE ignored for hyp failures)\n");
  378. }
  379. void __noreturn __cold nvhe_hyp_panic_handler(u64 esr, u64 spsr,
  380. u64 elr_virt, u64 elr_phys,
  381. u64 par, uintptr_t vcpu,
  382. u64 far, u64 hpfar) {
  383. u64 elr_in_kimg = __phys_to_kimg(elr_phys);
  384. u64 hyp_offset = elr_in_kimg - kaslr_offset() - elr_virt;
  385. u64 mode = spsr & PSR_MODE_MASK;
  386. u64 panic_addr = elr_virt + hyp_offset;
  387. if (mode != PSR_MODE_EL2t && mode != PSR_MODE_EL2h) {
  388. kvm_err("Invalid host exception to nVHE hyp!\n");
  389. } else if (ESR_ELx_EC(esr) == ESR_ELx_EC_BRK64 &&
  390. esr_brk_comment(esr) == BUG_BRK_IMM) {
  391. const char *file = NULL;
  392. unsigned int line = 0;
  393. /* All hyp bugs, including warnings, are treated as fatal. */
  394. if (!is_protected_kvm_enabled() ||
  395. IS_ENABLED(CONFIG_NVHE_EL2_DEBUG)) {
  396. struct bug_entry *bug = find_bug(elr_in_kimg);
  397. if (bug)
  398. bug_get_file_line(bug, &file, &line);
  399. }
  400. if (file)
  401. kvm_err("nVHE hyp BUG at: %s:%u!\n", file, line);
  402. else
  403. print_nvhe_hyp_panic("BUG", panic_addr);
  404. } else if (IS_ENABLED(CONFIG_CFI_CLANG) && esr_is_cfi_brk(esr)) {
  405. kvm_nvhe_report_cfi_failure(panic_addr);
  406. } else {
  407. print_nvhe_hyp_panic("panic", panic_addr);
  408. }
  409. /* Dump the nVHE hypervisor backtrace */
  410. kvm_nvhe_dump_backtrace(hyp_offset);
  411. /*
  412. * Hyp has panicked and we're going to handle that by panicking the
  413. * kernel. The kernel offset will be revealed in the panic so we're
  414. * also safe to reveal the hyp offset as a debugging aid for translating
  415. * hyp VAs to vmlinux addresses.
  416. */
  417. kvm_err("Hyp Offset: 0x%llx\n", hyp_offset);
  418. panic("HYP panic:\nPS:%08llx PC:%016llx ESR:%016llx\nFAR:%016llx HPFAR:%016llx PAR:%016llx\nVCPU:%016lx\n",
  419. spsr, elr_virt, esr, far, hpfar, par, vcpu);
  420. }