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