GNU Linux-libre 4.19.245-gnu1
[releases.git] / arch / arm64 / kvm / hyp / debug-sr.c
1 /*
2  * Copyright (C) 2015 - ARM Ltd
3  * Author: Marc Zyngier <marc.zyngier@arm.com>
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License version 2 as
7  * published by the Free Software Foundation.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17
18 #include <linux/compiler.h>
19 #include <linux/kvm_host.h>
20
21 #include <asm/debug-monitors.h>
22 #include <asm/kvm_asm.h>
23 #include <asm/kvm_hyp.h>
24 #include <asm/kvm_mmu.h>
25
26 #define read_debug(r,n)         read_sysreg(r##n##_el1)
27 #define write_debug(v,r,n)      write_sysreg(v, r##n##_el1)
28
29 #define save_debug(ptr,reg,nr)                                          \
30         switch (nr) {                                                   \
31         case 15:        ptr[15] = read_debug(reg, 15);                  \
32         case 14:        ptr[14] = read_debug(reg, 14);                  \
33         case 13:        ptr[13] = read_debug(reg, 13);                  \
34         case 12:        ptr[12] = read_debug(reg, 12);                  \
35         case 11:        ptr[11] = read_debug(reg, 11);                  \
36         case 10:        ptr[10] = read_debug(reg, 10);                  \
37         case 9:         ptr[9] = read_debug(reg, 9);                    \
38         case 8:         ptr[8] = read_debug(reg, 8);                    \
39         case 7:         ptr[7] = read_debug(reg, 7);                    \
40         case 6:         ptr[6] = read_debug(reg, 6);                    \
41         case 5:         ptr[5] = read_debug(reg, 5);                    \
42         case 4:         ptr[4] = read_debug(reg, 4);                    \
43         case 3:         ptr[3] = read_debug(reg, 3);                    \
44         case 2:         ptr[2] = read_debug(reg, 2);                    \
45         case 1:         ptr[1] = read_debug(reg, 1);                    \
46         default:        ptr[0] = read_debug(reg, 0);                    \
47         }
48
49 #define restore_debug(ptr,reg,nr)                                       \
50         switch (nr) {                                                   \
51         case 15:        write_debug(ptr[15], reg, 15);                  \
52         case 14:        write_debug(ptr[14], reg, 14);                  \
53         case 13:        write_debug(ptr[13], reg, 13);                  \
54         case 12:        write_debug(ptr[12], reg, 12);                  \
55         case 11:        write_debug(ptr[11], reg, 11);                  \
56         case 10:        write_debug(ptr[10], reg, 10);                  \
57         case 9:         write_debug(ptr[9], reg, 9);                    \
58         case 8:         write_debug(ptr[8], reg, 8);                    \
59         case 7:         write_debug(ptr[7], reg, 7);                    \
60         case 6:         write_debug(ptr[6], reg, 6);                    \
61         case 5:         write_debug(ptr[5], reg, 5);                    \
62         case 4:         write_debug(ptr[4], reg, 4);                    \
63         case 3:         write_debug(ptr[3], reg, 3);                    \
64         case 2:         write_debug(ptr[2], reg, 2);                    \
65         case 1:         write_debug(ptr[1], reg, 1);                    \
66         default:        write_debug(ptr[0], reg, 0);                    \
67         }
68
69 static void __hyp_text __debug_save_spe_nvhe(u64 *pmscr_el1)
70 {
71         u64 reg;
72
73         /* Clear pmscr in case of early return */
74         *pmscr_el1 = 0;
75
76         /* SPE present on this CPU? */
77         if (!cpuid_feature_extract_unsigned_field(read_sysreg(id_aa64dfr0_el1),
78                                                   ID_AA64DFR0_PMSVER_SHIFT))
79                 return;
80
81         /* Yes; is it owned by EL3? */
82         reg = read_sysreg_s(SYS_PMBIDR_EL1);
83         if (reg & BIT(SYS_PMBIDR_EL1_P_SHIFT))
84                 return;
85
86         /* No; is the host actually using the thing? */
87         reg = read_sysreg_s(SYS_PMBLIMITR_EL1);
88         if (!(reg & BIT(SYS_PMBLIMITR_EL1_E_SHIFT)))
89                 return;
90
91         /* Yes; save the control register and disable data generation */
92         *pmscr_el1 = read_sysreg_s(SYS_PMSCR_EL1);
93         write_sysreg_s(0, SYS_PMSCR_EL1);
94         isb();
95
96         /* Now drain all buffered data to memory */
97         psb_csync();
98         dsb(nsh);
99 }
100
101 static void __hyp_text __debug_restore_spe_nvhe(u64 pmscr_el1)
102 {
103         if (!pmscr_el1)
104                 return;
105
106         /* The host page table is installed, but not yet synchronised */
107         isb();
108
109         /* Re-enable data generation */
110         write_sysreg_s(pmscr_el1, SYS_PMSCR_EL1);
111 }
112
113 static void __hyp_text __debug_save_state(struct kvm_vcpu *vcpu,
114                                           struct kvm_guest_debug_arch *dbg,
115                                           struct kvm_cpu_context *ctxt)
116 {
117         u64 aa64dfr0;
118         int brps, wrps;
119
120         aa64dfr0 = read_sysreg(id_aa64dfr0_el1);
121         brps = (aa64dfr0 >> 12) & 0xf;
122         wrps = (aa64dfr0 >> 20) & 0xf;
123
124         save_debug(dbg->dbg_bcr, dbgbcr, brps);
125         save_debug(dbg->dbg_bvr, dbgbvr, brps);
126         save_debug(dbg->dbg_wcr, dbgwcr, wrps);
127         save_debug(dbg->dbg_wvr, dbgwvr, wrps);
128
129         ctxt->sys_regs[MDCCINT_EL1] = read_sysreg(mdccint_el1);
130 }
131
132 static void __hyp_text __debug_restore_state(struct kvm_vcpu *vcpu,
133                                              struct kvm_guest_debug_arch *dbg,
134                                              struct kvm_cpu_context *ctxt)
135 {
136         u64 aa64dfr0;
137         int brps, wrps;
138
139         aa64dfr0 = read_sysreg(id_aa64dfr0_el1);
140
141         brps = (aa64dfr0 >> 12) & 0xf;
142         wrps = (aa64dfr0 >> 20) & 0xf;
143
144         restore_debug(dbg->dbg_bcr, dbgbcr, brps);
145         restore_debug(dbg->dbg_bvr, dbgbvr, brps);
146         restore_debug(dbg->dbg_wcr, dbgwcr, wrps);
147         restore_debug(dbg->dbg_wvr, dbgwvr, wrps);
148
149         write_sysreg(ctxt->sys_regs[MDCCINT_EL1], mdccint_el1);
150 }
151
152 void __hyp_text __debug_save_host_buffers_nvhe(struct kvm_vcpu *vcpu)
153 {
154         /*
155          * Non-VHE: Disable and flush SPE data generation
156          * VHE: The vcpu can run, but it can't hide.
157          */
158         __debug_save_spe_nvhe(&vcpu->arch.host_debug_state.pmscr_el1);
159
160 }
161
162 void __hyp_text __debug_restore_host_buffers_nvhe(struct kvm_vcpu *vcpu)
163 {
164         __debug_restore_spe_nvhe(vcpu->arch.host_debug_state.pmscr_el1);
165 }
166
167 void __hyp_text __debug_switch_to_guest(struct kvm_vcpu *vcpu)
168 {
169         struct kvm_cpu_context *host_ctxt;
170         struct kvm_cpu_context *guest_ctxt;
171         struct kvm_guest_debug_arch *host_dbg;
172         struct kvm_guest_debug_arch *guest_dbg;
173
174         if (!(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
175                 return;
176
177         host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
178         guest_ctxt = &vcpu->arch.ctxt;
179         host_dbg = &vcpu->arch.host_debug_state.regs;
180         guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
181
182         __debug_save_state(vcpu, host_dbg, host_ctxt);
183         __debug_restore_state(vcpu, guest_dbg, guest_ctxt);
184 }
185
186 void __hyp_text __debug_switch_to_host(struct kvm_vcpu *vcpu)
187 {
188         struct kvm_cpu_context *host_ctxt;
189         struct kvm_cpu_context *guest_ctxt;
190         struct kvm_guest_debug_arch *host_dbg;
191         struct kvm_guest_debug_arch *guest_dbg;
192
193
194         if (!(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
195                 return;
196
197         host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
198         guest_ctxt = &vcpu->arch.ctxt;
199         host_dbg = &vcpu->arch.host_debug_state.regs;
200         guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
201
202         __debug_save_state(vcpu, guest_dbg, guest_ctxt);
203         __debug_restore_state(vcpu, host_dbg, host_ctxt);
204
205         vcpu->arch.flags &= ~KVM_ARM64_DEBUG_DIRTY;
206 }
207
208 u32 __hyp_text __kvm_get_mdcr_el2(void)
209 {
210         return read_sysreg(mdcr_el2);
211 }