debug.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Debug and Guest Debug support
  4. *
  5. * Copyright (C) 2015 - Linaro Ltd
  6. * Author: Alex Bennée <[email protected]>
  7. */
  8. #include <linux/kvm_host.h>
  9. #include <linux/hw_breakpoint.h>
  10. #include <asm/debug-monitors.h>
  11. #include <asm/kvm_asm.h>
  12. #include <asm/kvm_arm.h>
  13. #include <asm/kvm_emulate.h>
  14. #include "trace.h"
  15. /* These are the bits of MDSCR_EL1 we may manipulate */
  16. #define MDSCR_EL1_DEBUG_MASK (DBG_MDSCR_SS | \
  17. DBG_MDSCR_KDE | \
  18. DBG_MDSCR_MDE)
  19. static DEFINE_PER_CPU(u64, mdcr_el2);
  20. /**
  21. * save/restore_guest_debug_regs
  22. *
  23. * For some debug operations we need to tweak some guest registers. As
  24. * a result we need to save the state of those registers before we
  25. * make those modifications.
  26. *
  27. * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled
  28. * after we have restored the preserved value to the main context.
  29. *
  30. * When single-step is enabled by userspace, we tweak PSTATE.SS on every
  31. * guest entry. Preserve PSTATE.SS so we can restore the original value
  32. * for the vcpu after the single-step is disabled.
  33. */
  34. static void save_guest_debug_regs(struct kvm_vcpu *vcpu)
  35. {
  36. __vcpu_save_guest_debug_regs(vcpu);
  37. trace_kvm_arm_set_dreg32("Saved MDSCR_EL1",
  38. vcpu->arch.guest_debug_preserved.mdscr_el1);
  39. vcpu->arch.guest_debug_preserved.pstate_ss =
  40. (*vcpu_cpsr(vcpu) & DBG_SPSR_SS);
  41. }
  42. static void restore_guest_debug_regs(struct kvm_vcpu *vcpu)
  43. {
  44. __vcpu_restore_guest_debug_regs(vcpu);
  45. trace_kvm_arm_set_dreg32("Restored MDSCR_EL1",
  46. vcpu_read_sys_reg(vcpu, MDSCR_EL1));
  47. if (vcpu->arch.guest_debug_preserved.pstate_ss)
  48. *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
  49. else
  50. *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
  51. }
  52. /**
  53. * kvm_arm_init_debug - grab what we need for debug
  54. *
  55. * Currently the sole task of this function is to retrieve the initial
  56. * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has
  57. * presumably been set-up by some knowledgeable bootcode.
  58. *
  59. * It is called once per-cpu during CPU hyp initialisation.
  60. */
  61. void kvm_arm_init_debug(void)
  62. {
  63. __this_cpu_write(mdcr_el2, kvm_call_hyp_ret(__kvm_get_mdcr_el2));
  64. }
  65. /**
  66. * kvm_arm_setup_mdcr_el2 - configure vcpu mdcr_el2 value
  67. *
  68. * @vcpu: the vcpu pointer
  69. *
  70. * This ensures we will trap access to:
  71. * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
  72. * - Debug ROM Address (MDCR_EL2_TDRA)
  73. * - OS related registers (MDCR_EL2_TDOSA)
  74. * - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB)
  75. * - Self-hosted Trace Filter controls (MDCR_EL2_TTRF)
  76. * - Self-hosted Trace (MDCR_EL2_TTRF/MDCR_EL2_E2TB)
  77. */
  78. static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
  79. {
  80. /*
  81. * This also clears MDCR_EL2_E2PB_MASK and MDCR_EL2_E2TB_MASK
  82. * to disable guest access to the profiling and trace buffers
  83. */
  84. vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
  85. vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
  86. MDCR_EL2_TPMS |
  87. MDCR_EL2_TTRF |
  88. MDCR_EL2_TPMCR |
  89. MDCR_EL2_TDRA |
  90. MDCR_EL2_TDOSA);
  91. /* Is the VM being debugged by userspace? */
  92. if (vcpu->guest_debug)
  93. /* Route all software debug exceptions to EL2 */
  94. vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
  95. /*
  96. * Trap debug register access when one of the following is true:
  97. * - Userspace is using the hardware to debug the guest
  98. * (KVM_GUESTDBG_USE_HW is set).
  99. * - The guest is not using debug (DEBUG_DIRTY clear).
  100. * - The guest has enabled the OS Lock (debug exceptions are blocked).
  101. */
  102. if ((vcpu->guest_debug & KVM_GUESTDBG_USE_HW) ||
  103. !vcpu_get_flag(vcpu, DEBUG_DIRTY) ||
  104. kvm_vcpu_os_lock_enabled(vcpu))
  105. vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
  106. trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2);
  107. }
  108. /**
  109. * kvm_arm_vcpu_init_debug - setup vcpu debug traps
  110. *
  111. * @vcpu: the vcpu pointer
  112. *
  113. * Set vcpu initial mdcr_el2 value.
  114. */
  115. void kvm_arm_vcpu_init_debug(struct kvm_vcpu *vcpu)
  116. {
  117. preempt_disable();
  118. kvm_arm_setup_mdcr_el2(vcpu);
  119. preempt_enable();
  120. }
  121. /**
  122. * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state
  123. */
  124. void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu)
  125. {
  126. vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state;
  127. }
  128. /**
  129. * kvm_arm_setup_debug - set up debug related stuff
  130. *
  131. * @vcpu: the vcpu pointer
  132. *
  133. * This is called before each entry into the hypervisor to setup any
  134. * debug related registers.
  135. *
  136. * Additionally, KVM only traps guest accesses to the debug registers if
  137. * the guest is not actively using them (see the DEBUG_DIRTY
  138. * flag on vcpu->arch.iflags). Since the guest must not interfere
  139. * with the hardware state when debugging the guest, we must ensure that
  140. * trapping is enabled whenever we are debugging the guest using the
  141. * debug registers.
  142. */
  143. void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
  144. {
  145. unsigned long mdscr, orig_mdcr_el2 = vcpu->arch.mdcr_el2;
  146. trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug);
  147. kvm_arm_setup_mdcr_el2(vcpu);
  148. /* Check if we need to use the debug registers. */
  149. if (kvm_vcpu_needs_debug_regs(vcpu)) {
  150. /* Save guest debug state */
  151. save_guest_debug_regs(vcpu);
  152. /*
  153. * Single Step (ARM ARM D2.12.3 The software step state
  154. * machine)
  155. *
  156. * If we are doing Single Step we need to manipulate
  157. * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the
  158. * step has occurred the hypervisor will trap the
  159. * debug exception and we return to userspace.
  160. *
  161. * If the guest attempts to single step its userspace
  162. * we would have to deal with a trapped exception
  163. * while in the guest kernel. Because this would be
  164. * hard to unwind we suppress the guest's ability to
  165. * do so by masking MDSCR_EL.SS.
  166. *
  167. * This confuses guest debuggers which use
  168. * single-step behind the scenes but everything
  169. * returns to normal once the host is no longer
  170. * debugging the system.
  171. */
  172. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  173. /*
  174. * If the software step state at the last guest exit
  175. * was Active-pending, we don't set DBG_SPSR_SS so
  176. * that the state is maintained (to not run another
  177. * single-step until the pending Software Step
  178. * exception is taken).
  179. */
  180. if (!vcpu_get_flag(vcpu, DBG_SS_ACTIVE_PENDING))
  181. *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
  182. else
  183. *vcpu_cpsr(vcpu) &= ~DBG_SPSR_SS;
  184. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  185. mdscr |= DBG_MDSCR_SS;
  186. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  187. } else {
  188. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  189. mdscr &= ~DBG_MDSCR_SS;
  190. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  191. }
  192. trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu));
  193. /*
  194. * HW Breakpoints and watchpoints
  195. *
  196. * We simply switch the debug_ptr to point to our new
  197. * external_debug_state which has been populated by the
  198. * debug ioctl. The existing DEBUG_DIRTY mechanism ensures
  199. * the registers are updated on the world switch.
  200. */
  201. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  202. /* Enable breakpoints/watchpoints */
  203. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  204. mdscr |= DBG_MDSCR_MDE;
  205. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  206. vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
  207. vcpu_set_flag(vcpu, DEBUG_DIRTY);
  208. trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
  209. &vcpu->arch.debug_ptr->dbg_bcr[0],
  210. &vcpu->arch.debug_ptr->dbg_bvr[0]);
  211. trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
  212. &vcpu->arch.debug_ptr->dbg_wcr[0],
  213. &vcpu->arch.debug_ptr->dbg_wvr[0]);
  214. /*
  215. * The OS Lock blocks debug exceptions in all ELs when it is
  216. * enabled. If the guest has enabled the OS Lock, constrain its
  217. * effects to the guest. Emulate the behavior by clearing
  218. * MDSCR_EL1.MDE. In so doing, we ensure that host debug
  219. * exceptions are unaffected by guest configuration of the OS
  220. * Lock.
  221. */
  222. } else if (kvm_vcpu_os_lock_enabled(vcpu)) {
  223. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  224. mdscr &= ~DBG_MDSCR_MDE;
  225. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  226. }
  227. }
  228. BUG_ON(!vcpu->guest_debug &&
  229. vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
  230. /* If KDE or MDE are set, perform a full save/restore cycle. */
  231. if (vcpu_read_sys_reg(vcpu, MDSCR_EL1) & (DBG_MDSCR_KDE | DBG_MDSCR_MDE))
  232. vcpu_set_flag(vcpu, DEBUG_DIRTY);
  233. /* Write mdcr_el2 changes since vcpu_load on VHE systems */
  234. if (has_vhe() && orig_mdcr_el2 != vcpu->arch.mdcr_el2)
  235. write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
  236. trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1));
  237. }
  238. void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
  239. {
  240. trace_kvm_arm_clear_debug(vcpu->guest_debug);
  241. /*
  242. * Restore the guest's debug registers if we were using them.
  243. */
  244. if (kvm_vcpu_needs_debug_regs(vcpu)) {
  245. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  246. if (!(*vcpu_cpsr(vcpu) & DBG_SPSR_SS))
  247. /*
  248. * Mark the vcpu as ACTIVE_PENDING
  249. * until Software Step exception is taken.
  250. */
  251. vcpu_set_flag(vcpu, DBG_SS_ACTIVE_PENDING);
  252. }
  253. restore_guest_debug_regs(vcpu);
  254. /*
  255. * If we were using HW debug we need to restore the
  256. * debug_ptr to the guest debug state.
  257. */
  258. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  259. kvm_arm_reset_debug_ptr(vcpu);
  260. trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
  261. &vcpu->arch.debug_ptr->dbg_bcr[0],
  262. &vcpu->arch.debug_ptr->dbg_bvr[0]);
  263. trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
  264. &vcpu->arch.debug_ptr->dbg_wcr[0],
  265. &vcpu->arch.debug_ptr->dbg_wvr[0]);
  266. }
  267. }
  268. }
  269. void kvm_arch_vcpu_load_debug_state_flags(struct kvm_vcpu *vcpu)
  270. {
  271. u64 dfr0;
  272. /* For VHE, there is nothing to do */
  273. if (has_vhe())
  274. return;
  275. dfr0 = read_sysreg(id_aa64dfr0_el1);
  276. /*
  277. * If SPE is present on this CPU and is available at current EL,
  278. * we may need to check if the host state needs to be saved.
  279. */
  280. if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_PMSVer_SHIFT) &&
  281. !(read_sysreg_s(SYS_PMBIDR_EL1) & BIT(SYS_PMBIDR_EL1_P_SHIFT)))
  282. vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_SPE);
  283. /* Check if we have TRBE implemented and available at the host */
  284. if (cpuid_feature_extract_unsigned_field(dfr0, ID_AA64DFR0_EL1_TraceBuffer_SHIFT) &&
  285. !(read_sysreg_s(SYS_TRBIDR_EL1) & TRBIDR_PROG))
  286. vcpu_set_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
  287. }
  288. void kvm_arch_vcpu_put_debug_state_flags(struct kvm_vcpu *vcpu)
  289. {
  290. vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_SPE);
  291. vcpu_clear_flag(vcpu, DEBUG_STATE_SAVE_TRBE);
  292. }