debug.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. *
  85. * Additionally, KVM only traps guest accesses to the debug registers if
  86. * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY
  87. * flag on vcpu->arch.debug_flags). Since the guest must not interfere
  88. * with the hardware state when debugging the guest, we must ensure that
  89. * trapping is enabled whenever we are debugging the guest using the
  90. * debug registers.
  91. */
  92. void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
  93. {
  94. bool trap_debug = !(vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY);
  95. trace_kvm_arm_setup_debug(vcpu, vcpu->guest_debug);
  96. vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
  97. vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
  98. MDCR_EL2_TPMCR |
  99. MDCR_EL2_TDRA |
  100. MDCR_EL2_TDOSA);
  101. /* Is Guest debugging in effect? */
  102. if (vcpu->guest_debug) {
  103. /* Route all software debug exceptions to EL2 */
  104. vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
  105. /* Save guest debug state */
  106. save_guest_debug_regs(vcpu);
  107. /*
  108. * Single Step (ARM ARM D2.12.3 The software step state
  109. * machine)
  110. *
  111. * If we are doing Single Step we need to manipulate
  112. * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the
  113. * step has occurred the hypervisor will trap the
  114. * debug exception and we return to userspace.
  115. *
  116. * If the guest attempts to single step its userspace
  117. * we would have to deal with a trapped exception
  118. * while in the guest kernel. Because this would be
  119. * hard to unwind we suppress the guest's ability to
  120. * do so by masking MDSCR_EL.SS.
  121. *
  122. * This confuses guest debuggers which use
  123. * single-step behind the scenes but everything
  124. * returns to normal once the host is no longer
  125. * debugging the system.
  126. */
  127. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  128. *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
  129. vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_SS;
  130. } else {
  131. vcpu_sys_reg(vcpu, MDSCR_EL1) &= ~DBG_MDSCR_SS;
  132. }
  133. trace_kvm_arm_set_dreg32("SPSR_EL2", *vcpu_cpsr(vcpu));
  134. /*
  135. * HW Breakpoints and watchpoints
  136. *
  137. * We simply switch the debug_ptr to point to our new
  138. * external_debug_state which has been populated by the
  139. * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY
  140. * mechanism ensures the registers are updated on the
  141. * world switch.
  142. */
  143. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  144. /* Enable breakpoints/watchpoints */
  145. vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_MDE;
  146. vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
  147. vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
  148. trap_debug = true;
  149. trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
  150. &vcpu->arch.debug_ptr->dbg_bcr[0],
  151. &vcpu->arch.debug_ptr->dbg_bvr[0]);
  152. trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
  153. &vcpu->arch.debug_ptr->dbg_wcr[0],
  154. &vcpu->arch.debug_ptr->dbg_wvr[0]);
  155. }
  156. }
  157. BUG_ON(!vcpu->guest_debug &&
  158. vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
  159. /* Trap debug register access */
  160. if (trap_debug)
  161. vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
  162. trace_kvm_arm_set_dreg32("MDCR_EL2", vcpu->arch.mdcr_el2);
  163. trace_kvm_arm_set_dreg32("MDSCR_EL1", vcpu_sys_reg(vcpu, MDSCR_EL1));
  164. }
  165. void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
  166. {
  167. trace_kvm_arm_clear_debug(vcpu->guest_debug);
  168. if (vcpu->guest_debug) {
  169. restore_guest_debug_regs(vcpu);
  170. /*
  171. * If we were using HW debug we need to restore the
  172. * debug_ptr to the guest debug state.
  173. */
  174. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  175. kvm_arm_reset_debug_ptr(vcpu);
  176. trace_kvm_arm_set_regset("BKPTS", get_num_brps(),
  177. &vcpu->arch.debug_ptr->dbg_bcr[0],
  178. &vcpu->arch.debug_ptr->dbg_bvr[0]);
  179. trace_kvm_arm_set_regset("WAPTS", get_num_wrps(),
  180. &vcpu->arch.debug_ptr->dbg_wcr[0],
  181. &vcpu->arch.debug_ptr->dbg_wvr[0]);
  182. }
  183. }
  184. }