debug.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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 <asm/debug-monitors.h>
  21. #include <asm/kvm_asm.h>
  22. #include <asm/kvm_arm.h>
  23. #include <asm/kvm_emulate.h>
  24. /* These are the bits of MDSCR_EL1 we may manipulate */
  25. #define MDSCR_EL1_DEBUG_MASK (DBG_MDSCR_SS | \
  26. DBG_MDSCR_KDE | \
  27. DBG_MDSCR_MDE)
  28. static DEFINE_PER_CPU(u32, mdcr_el2);
  29. /**
  30. * save/restore_guest_debug_regs
  31. *
  32. * For some debug operations we need to tweak some guest registers. As
  33. * a result we need to save the state of those registers before we
  34. * make those modifications.
  35. *
  36. * Guest access to MDSCR_EL1 is trapped by the hypervisor and handled
  37. * after we have restored the preserved value to the main context.
  38. */
  39. static void save_guest_debug_regs(struct kvm_vcpu *vcpu)
  40. {
  41. vcpu->arch.guest_debug_preserved.mdscr_el1 = vcpu_sys_reg(vcpu, MDSCR_EL1);
  42. }
  43. static void restore_guest_debug_regs(struct kvm_vcpu *vcpu)
  44. {
  45. vcpu_sys_reg(vcpu, MDSCR_EL1) = vcpu->arch.guest_debug_preserved.mdscr_el1;
  46. }
  47. /**
  48. * kvm_arm_init_debug - grab what we need for debug
  49. *
  50. * Currently the sole task of this function is to retrieve the initial
  51. * value of mdcr_el2 so we can preserve MDCR_EL2.HPMN which has
  52. * presumably been set-up by some knowledgeable bootcode.
  53. *
  54. * It is called once per-cpu during CPU hyp initialisation.
  55. */
  56. void kvm_arm_init_debug(void)
  57. {
  58. __this_cpu_write(mdcr_el2, kvm_call_hyp(__kvm_get_mdcr_el2));
  59. }
  60. /**
  61. * kvm_arm_reset_debug_ptr - reset the debug ptr to point to the vcpu state
  62. */
  63. void kvm_arm_reset_debug_ptr(struct kvm_vcpu *vcpu)
  64. {
  65. vcpu->arch.debug_ptr = &vcpu->arch.vcpu_debug_state;
  66. }
  67. /**
  68. * kvm_arm_setup_debug - set up debug related stuff
  69. *
  70. * @vcpu: the vcpu pointer
  71. *
  72. * This is called before each entry into the hypervisor to setup any
  73. * debug related registers. Currently this just ensures we will trap
  74. * access to:
  75. * - Performance monitors (MDCR_EL2_TPM/MDCR_EL2_TPMCR)
  76. * - Debug ROM Address (MDCR_EL2_TDRA)
  77. * - OS related registers (MDCR_EL2_TDOSA)
  78. *
  79. * Additionally, KVM only traps guest accesses to the debug registers if
  80. * the guest is not actively using them (see the KVM_ARM64_DEBUG_DIRTY
  81. * flag on vcpu->arch.debug_flags). Since the guest must not interfere
  82. * with the hardware state when debugging the guest, we must ensure that
  83. * trapping is enabled whenever we are debugging the guest using the
  84. * debug registers.
  85. */
  86. void kvm_arm_setup_debug(struct kvm_vcpu *vcpu)
  87. {
  88. bool trap_debug = !(vcpu->arch.debug_flags & KVM_ARM64_DEBUG_DIRTY);
  89. vcpu->arch.mdcr_el2 = __this_cpu_read(mdcr_el2) & MDCR_EL2_HPMN_MASK;
  90. vcpu->arch.mdcr_el2 |= (MDCR_EL2_TPM |
  91. MDCR_EL2_TPMCR |
  92. MDCR_EL2_TDRA |
  93. MDCR_EL2_TDOSA);
  94. /* Is Guest debugging in effect? */
  95. if (vcpu->guest_debug) {
  96. /* Route all software debug exceptions to EL2 */
  97. vcpu->arch.mdcr_el2 |= MDCR_EL2_TDE;
  98. /* Save guest debug state */
  99. save_guest_debug_regs(vcpu);
  100. /*
  101. * Single Step (ARM ARM D2.12.3 The software step state
  102. * machine)
  103. *
  104. * If we are doing Single Step we need to manipulate
  105. * the guest's MDSCR_EL1.SS and PSTATE.SS. Once the
  106. * step has occurred the hypervisor will trap the
  107. * debug exception and we return to userspace.
  108. *
  109. * If the guest attempts to single step its userspace
  110. * we would have to deal with a trapped exception
  111. * while in the guest kernel. Because this would be
  112. * hard to unwind we suppress the guest's ability to
  113. * do so by masking MDSCR_EL.SS.
  114. *
  115. * This confuses guest debuggers which use
  116. * single-step behind the scenes but everything
  117. * returns to normal once the host is no longer
  118. * debugging the system.
  119. */
  120. if (vcpu->guest_debug & KVM_GUESTDBG_SINGLESTEP) {
  121. *vcpu_cpsr(vcpu) |= DBG_SPSR_SS;
  122. vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_SS;
  123. } else {
  124. vcpu_sys_reg(vcpu, MDSCR_EL1) &= ~DBG_MDSCR_SS;
  125. }
  126. /*
  127. * HW Breakpoints and watchpoints
  128. *
  129. * We simply switch the debug_ptr to point to our new
  130. * external_debug_state which has been populated by the
  131. * debug ioctl. The existing KVM_ARM64_DEBUG_DIRTY
  132. * mechanism ensures the registers are updated on the
  133. * world switch.
  134. */
  135. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW) {
  136. /* Enable breakpoints/watchpoints */
  137. vcpu_sys_reg(vcpu, MDSCR_EL1) |= DBG_MDSCR_MDE;
  138. vcpu->arch.debug_ptr = &vcpu->arch.external_debug_state;
  139. vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
  140. trap_debug = true;
  141. }
  142. }
  143. BUG_ON(!vcpu->guest_debug &&
  144. vcpu->arch.debug_ptr != &vcpu->arch.vcpu_debug_state);
  145. /* Trap debug register access */
  146. if (trap_debug)
  147. vcpu->arch.mdcr_el2 |= MDCR_EL2_TDA;
  148. }
  149. void kvm_arm_clear_debug(struct kvm_vcpu *vcpu)
  150. {
  151. if (vcpu->guest_debug) {
  152. restore_guest_debug_regs(vcpu);
  153. /*
  154. * If we were using HW debug we need to restore the
  155. * debug_ptr to the guest debug state.
  156. */
  157. if (vcpu->guest_debug & KVM_GUESTDBG_USE_HW)
  158. kvm_arm_reset_debug_ptr(vcpu);
  159. }
  160. }