debug.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. /*
  2. * Debug and Guest Debug support
  3. *
  4. * Copyright (C) 2015 - Linaro Ltd
  5. * Author: Alex Bennée <alex.bennee@linaro.org>
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License version 2 as
  9. * published by the Free Software Foundation.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. #include <linux/kvm_host.h>
  20. #include <linux/hw_breakpoint.h>
  21. #include <asm/debug-monitors.h>
  22. #include <asm/kvm_asm.h>
  23. #include <asm/kvm_arm.h>
  24. #include <asm/kvm_emulate.h>
  25. #include "trace.h"
  26. /* These are the bits of MDSCR_EL1 we may manipulate */
  27. #define MDSCR_EL1_DEBUG_MASK (DBG_MDSCR_SS | \
  28. DBG_MDSCR_KDE | \
  29. DBG_MDSCR_MDE)
  30. static DEFINE_PER_CPU(u32, mdcr_el2);
  31. /**
  32. * save/restore_guest_debug_regs
  33. *
  34. * For some debug operations we need to tweak some guest registers. As
  35. * a result we need to save the state of those registers before we
  36. * make those modifications.
  37. *
  38. * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled
  39. * after we have restored the preserved value to the main context.
  40. */
  41. static void save_guest_debug_regs(struct kvm_vcpu *vcpu)
  42. {
  43. u64 val = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  44. vcpu->arch.guest_debug_preserved.mdscr_el1 = val;
  45. trace_kvm_arm_set_dreg32("Saved MDSCR_EL1",
  46. vcpu->arch.guest_debug_preserved.mdscr_el1);
  47. }
  48. static void restore_guest_debug_regs(struct kvm_vcpu *vcpu)
  49. {
  50. u64 val = vcpu->arch.guest_debug_preserved.mdscr_el1;
  51. vcpu_write_sys_reg(vcpu, val, MDSCR_EL1);
  52. trace_kvm_arm_set_dreg32("Restored MDSCR_EL1",
  53. vcpu_read_sys_reg(vcpu, MDSCR_EL1));
  54. }
  55. /**
  56. * kvm_arm_init_debug - grab what we need for debug
  57. *
  58. * Currently the sole task of this function is to retrieve the initial
  59. * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has
  60. * presumably been set-up by some knowledgeable bootcode.
  61. *
  62. * It is called once per-cpu during CPU hyp initialisation.
  63. */
  64. void kvm_arm_init_debug(void)
  65. {
  66. __this_cpu_write(mdcr_el2, kvm_call_hyp(__kvm_get_mdcr_el2));
  67. }
  68. /**
  69. * kvm_arm_setup_mdcr_el2 - configure vcpu mdcr_el2 value
  70. *
  71. * @vcpu: the vcpu pointer
  72. *
  73. * This ensures we will trap access to:
  74. * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
  75. * - Debug ROM Address (MDCR_EL2_TDRA)
  76. * - OS related registers (MDCR_EL2_TDOSA)
  77. * - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB)
  78. * - Self-hosted Trace Filter controls (MDCR_EL2_TTRF)
  79. */
  80. static void kvm_arm_setup_mdcr_el2(struct kvm_vcpu *vcpu)
  81. {
  82. /*
  83. * This also clears MDCR_EL2_E2PB_MASK to disable guest access
  84. * to the profiling buffer.
  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 (KVM_ARM64_DEBUG_DIRTY is clear).
  102. */
  103. if ((vcpu->guest_debug & KVM_GUESTDBG_USE_HW) ||
  104. !(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
  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 KVM_ARM64_DEBUG_DIRTY
  138. * flag on vcpu->arch.flags). 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. /* Is Guest debugging in effect? */
  149. if (vcpu->guest_debug) {
  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. *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
  174. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  175. mdscr |= DBG_MDSCR_SS;
  176. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  177. } else {
  178. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  179. mdscr &= ~DBG_MDSCR_SS;
  180. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  181. }
  182. trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu));
  183. /*
  184. * HW Breakpoints and watchpoints
  185. *
  186. * We simply switch the debug_ptr to point to our new
  187. * external_debug_state which has been populated by the
  188. * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY
  189. * mechanism ensures the registers are updated on the
  190. * world switch.
  191. */
  192. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  193. /* Enable breakpoints/watchpoints */
  194. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  195. mdscr |= DBG_MDSCR_MDE;
  196. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  197. vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
  198. vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
  199. trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
  200. &vcpu->arch.debug_ptr->dbg_bcr[0],
  201. &vcpu->arch.debug_ptr->dbg_bvr[0]);
  202. trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
  203. &vcpu->arch.debug_ptr->dbg_wcr[0],
  204. &vcpu->arch.debug_ptr->dbg_wvr[0]);
  205. }
  206. }
  207. BUG_ON(!vcpu->guest_debug &&
  208. vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
  209. /* If KDE or MDE are set, perform a full save/restore cycle. */
  210. if (vcpu_read_sys_reg(vcpu, MDSCR_EL1) & (DBG_MDSCR_KDE | DBG_MDSCR_MDE))
  211. vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
  212. /* Write mdcr_el2 changes since vcpu_load on VHE systems */
  213. if (has_vhe() && orig_mdcr_el2 != vcpu->arch.mdcr_el2)
  214. write_sysreg(vcpu->arch.mdcr_el2, mdcr_el2);
  215. trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1));
  216. }
  217. void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
  218. {
  219. trace_kvm_arm_clear_debug(vcpu->guest_debug);
  220. if (vcpu->guest_debug) {
  221. restore_guest_debug_regs(vcpu);
  222. /*
  223. * If we were using HW debug we need to restore the
  224. * debug_ptr to the guest debug state.
  225. */
  226. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  227. kvm_arm_reset_debug_ptr(vcpu);
  228. trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
  229. &vcpu->arch.debug_ptr->dbg_bcr[0],
  230. &vcpu->arch.debug_ptr->dbg_bvr[0]);
  231. trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
  232. &vcpu->arch.debug_ptr->dbg_wcr[0],
  233. &vcpu->arch.debug_ptr->dbg_wvr[0]);
  234. }
  235. }
  236. }
  237. /*
  238. * After successfully emulating an instruction, we might want to
  239. * return to user space with a KVM_EXIT_DEBUG. We can only do this
  240. * once the emulation is complete, though, so for userspace emulations
  241. * we have to wait until we have re-entered KVM before calling this
  242. * helper.
  243. *
  244. * Return true (and set exit_reason) to return to userspace or false
  245. * if no further action is required.
  246. */
  247. bool kvm_arm_handle_step_debug(struct kvm_vcpu *vcpu, struct kvm_run *run)
  248. {
  249. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  250. run->exit_reason = KVM_EXIT_DEBUG;
  251. run->debug.arch.hsr = ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT;
  252. return true;
  253. }
  254. return false;
  255. }