debug.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259
  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_reset_debug_ptr - reset the debug ptr to point to the vcpu state
  70. */
  71. void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu)
  72. {
  73. vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state;
  74. }
  75. /**
  76. * kvm_arm_setup_debug - set up debug related stuff
  77. *
  78. * @vcpu: the vcpu pointer
  79. *
  80. * This is called before each entry into the hypervisor to setup any
  81. * debug related registers. Currently this just ensures we will trap
  82. * access to:
  83. * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
  84. * - Debug ROM Address (MDCR_EL2_TDRA)
  85. * - OS related registers (MDCR_EL2_TDOSA)
  86. * - Statistical profiler (MDCR_EL2_TPMS/MDCR_EL2_E2PB)
  87. *
  88. * Additionally, KVM only traps guest accesses to the debug registers if
  89. * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY
  90. * flag on vcpu->arch.flags). Since the guest must not interfere
  91. * with the hardware state when debugging the guest, we must ensure that
  92. * trapping is enabled whenever we are debugging the guest using the
  93. * debug registers.
  94. */
  95. void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
  96. {
  97. bool trap_debug = !(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY);
  98. unsigned long mdscr;
  99. trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug);
  100. /*
  101. * This also clears MDCR_EL2_E2PB_MASK to disable guest access
  102. * to the profiling buffer.
  103. */
  104. vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
  105. vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
  106. MDCR_EL2_TPMS |
  107. MDCR_EL2_TPMCR |
  108. MDCR_EL2_TDRA |
  109. MDCR_EL2_TDOSA);
  110. /* Is Guest debugging in effect? */
  111. if (vcpu->guest_debug) {
  112. /* Route all software debug exceptions to EL2 */
  113. vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
  114. /* Save guest debug state */
  115. save_guest_debug_regs(vcpu);
  116. /*
  117. * Single Step (ARM ARM D2.12.3 The software step state
  118. * machine)
  119. *
  120. * If we are doing Single Step we need to manipulate
  121. * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the
  122. * step has occurred the hypervisor will trap the
  123. * debug exception and we return to userspace.
  124. *
  125. * If the guest attempts to single step its userspace
  126. * we would have to deal with a trapped exception
  127. * while in the guest kernel. Because this would be
  128. * hard to unwind we suppress the guest's ability to
  129. * do so by masking MDSCR_EL.SS.
  130. *
  131. * This confuses guest debuggers which use
  132. * single-step behind the scenes but everything
  133. * returns to normal once the host is no longer
  134. * debugging the system.
  135. */
  136. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  137. *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
  138. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  139. mdscr |= DBG_MDSCR_SS;
  140. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  141. } else {
  142. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  143. mdscr &= ~DBG_MDSCR_SS;
  144. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  145. }
  146. trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu));
  147. /*
  148. * HW Breakpoints and watchpoints
  149. *
  150. * We simply switch the debug_ptr to point to our new
  151. * external_debug_state which has been populated by the
  152. * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY
  153. * mechanism ensures the registers are updated on the
  154. * world switch.
  155. */
  156. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  157. /* Enable breakpoints/watchpoints */
  158. mdscr = vcpu_read_sys_reg(vcpu, MDSCR_EL1);
  159. mdscr |= DBG_MDSCR_MDE;
  160. vcpu_write_sys_reg(vcpu, mdscr, MDSCR_EL1);
  161. vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
  162. vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
  163. trap_debug = true;
  164. trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
  165. &vcpu->arch.debug_ptr->dbg_bcr[0],
  166. &vcpu->arch.debug_ptr->dbg_bvr[0]);
  167. trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
  168. &vcpu->arch.debug_ptr->dbg_wcr[0],
  169. &vcpu->arch.debug_ptr->dbg_wvr[0]);
  170. }
  171. }
  172. BUG_ON(!vcpu->guest_debug &&
  173. vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
  174. /* Trap debug register access */
  175. if (trap_debug)
  176. vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
  177. /* If KDE or MDE are set, perform a full save/restore cycle. */
  178. if (vcpu_read_sys_reg(vcpu, MDSCR_EL1) & (DBG_MDSCR_KDE | DBG_MDSCR_MDE))
  179. vcpu->arch.flags |= KVM_ARM64_DEBUG_DIRTY;
  180. trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2);
  181. trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_read_sys_reg(vcpu, MDSCR_EL1));
  182. }
  183. void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
  184. {
  185. trace_kvm_arm_clear_debug(vcpu->guest_debug);
  186. if (vcpu->guest_debug) {
  187. restore_guest_debug_regs(vcpu);
  188. /*
  189. * If we were using HW debug we need to restore the
  190. * debug_ptr to the guest debug state.
  191. */
  192. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  193. kvm_arm_reset_debug_ptr(vcpu);
  194. trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
  195. &vcpu->arch.debug_ptr->dbg_bcr[0],
  196. &vcpu->arch.debug_ptr->dbg_bvr[0]);
  197. trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
  198. &vcpu->arch.debug_ptr->dbg_wcr[0],
  199. &vcpu->arch.debug_ptr->dbg_wvr[0]);
  200. }
  201. }
  202. }
  203. /*
  204. * After successfully emulating an instruction, we might want to
  205. * return to user space with a KVM_EXIT_DEBUG. We can only do this
  206. * once the emulation is complete, though, so for userspace emulations
  207. * we have to wait until we have re-entered KVM before calling this
  208. * helper.
  209. *
  210. * Return true (and set exit_reason) to return to userspace or false
  211. * if no further action is required.
  212. */
  213. bool kvm_arm_handle_step_debug(struct kvm_vcpu *vcpu, struct kvm_run *run)
  214. {
  215. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  216. run->exit_reason = KVM_EXIT_DEBUG;
  217. run->debug.arch.hsr = ESR_ELx_EC_SOFTSTP_LOW << ESR_ELx_EC_SHIFT;
  218. return true;
  219. }
  220. return false;
  221. }