debug.c 10 KB

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