debug.c 7.3 KB

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