This source file includes following definitions.
- __debug_save_spe_nvhe
- __debug_restore_spe_nvhe
- __debug_save_state
- __debug_restore_state
- __debug_switch_to_guest
- __debug_switch_to_host
- __kvm_get_mdcr_el2
1
2
3
4
5
6
7 #include <linux/compiler.h>
8 #include <linux/kvm_host.h>
9
10 #include <asm/debug-monitors.h>
11 #include <asm/kvm_asm.h>
12 #include <asm/kvm_hyp.h>
13 #include <asm/kvm_mmu.h>
14
15 #define read_debug(r,n) read_sysreg(r##n##_el1)
16 #define write_debug(v,r,n) write_sysreg(v, r##n##_el1)
17
18 #define save_debug(ptr,reg,nr) \
19 switch (nr) { \
20 case 15: ptr[15] = read_debug(reg, 15); \
21 \
22 case 14: ptr[14] = read_debug(reg, 14); \
23 \
24 case 13: ptr[13] = read_debug(reg, 13); \
25 \
26 case 12: ptr[12] = read_debug(reg, 12); \
27 \
28 case 11: ptr[11] = read_debug(reg, 11); \
29 \
30 case 10: ptr[10] = read_debug(reg, 10); \
31 \
32 case 9: ptr[9] = read_debug(reg, 9); \
33 \
34 case 8: ptr[8] = read_debug(reg, 8); \
35 \
36 case 7: ptr[7] = read_debug(reg, 7); \
37 \
38 case 6: ptr[6] = read_debug(reg, 6); \
39 \
40 case 5: ptr[5] = read_debug(reg, 5); \
41 \
42 case 4: ptr[4] = read_debug(reg, 4); \
43 \
44 case 3: ptr[3] = read_debug(reg, 3); \
45 \
46 case 2: ptr[2] = read_debug(reg, 2); \
47 \
48 case 1: ptr[1] = read_debug(reg, 1); \
49 \
50 default: ptr[0] = read_debug(reg, 0); \
51 }
52
53 #define restore_debug(ptr,reg,nr) \
54 switch (nr) { \
55 case 15: write_debug(ptr[15], reg, 15); \
56 \
57 case 14: write_debug(ptr[14], reg, 14); \
58 \
59 case 13: write_debug(ptr[13], reg, 13); \
60 \
61 case 12: write_debug(ptr[12], reg, 12); \
62 \
63 case 11: write_debug(ptr[11], reg, 11); \
64 \
65 case 10: write_debug(ptr[10], reg, 10); \
66 \
67 case 9: write_debug(ptr[9], reg, 9); \
68 \
69 case 8: write_debug(ptr[8], reg, 8); \
70 \
71 case 7: write_debug(ptr[7], reg, 7); \
72 \
73 case 6: write_debug(ptr[6], reg, 6); \
74 \
75 case 5: write_debug(ptr[5], reg, 5); \
76 \
77 case 4: write_debug(ptr[4], reg, 4); \
78 \
79 case 3: write_debug(ptr[3], reg, 3); \
80 \
81 case 2: write_debug(ptr[2], reg, 2); \
82 \
83 case 1: write_debug(ptr[1], reg, 1); \
84 \
85 default: write_debug(ptr[0], reg, 0); \
86 }
87
88 static void __hyp_text __debug_save_spe_nvhe(u64 *pmscr_el1)
89 {
90 u64 reg;
91
92
93 *pmscr_el1 = 0;
94
95
96 if (!cpuid_feature_extract_unsigned_field(read_sysreg(id_aa64dfr0_el1),
97 ID_AA64DFR0_PMSVER_SHIFT))
98 return;
99
100
101 reg = read_sysreg_s(SYS_PMBIDR_EL1);
102 if (reg & BIT(SYS_PMBIDR_EL1_P_SHIFT))
103 return;
104
105
106 reg = read_sysreg_s(SYS_PMBLIMITR_EL1);
107 if (!(reg & BIT(SYS_PMBLIMITR_EL1_E_SHIFT)))
108 return;
109
110
111 *pmscr_el1 = read_sysreg_s(SYS_PMSCR_EL1);
112 write_sysreg_s(0, SYS_PMSCR_EL1);
113 isb();
114
115
116 psb_csync();
117 dsb(nsh);
118 }
119
120 static void __hyp_text __debug_restore_spe_nvhe(u64 pmscr_el1)
121 {
122 if (!pmscr_el1)
123 return;
124
125
126 isb();
127
128
129 write_sysreg_s(pmscr_el1, SYS_PMSCR_EL1);
130 }
131
132 static void __hyp_text __debug_save_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 brps = (aa64dfr0 >> 12) & 0xf;
141 wrps = (aa64dfr0 >> 20) & 0xf;
142
143 save_debug(dbg->dbg_bcr, dbgbcr, brps);
144 save_debug(dbg->dbg_bvr, dbgbvr, brps);
145 save_debug(dbg->dbg_wcr, dbgwcr, wrps);
146 save_debug(dbg->dbg_wvr, dbgwvr, wrps);
147
148 ctxt->sys_regs[MDCCINT_EL1] = read_sysreg(mdccint_el1);
149 }
150
151 static void __hyp_text __debug_restore_state(struct kvm_vcpu *vcpu,
152 struct kvm_guest_debug_arch *dbg,
153 struct kvm_cpu_context *ctxt)
154 {
155 u64 aa64dfr0;
156 int brps, wrps;
157
158 aa64dfr0 = read_sysreg(id_aa64dfr0_el1);
159
160 brps = (aa64dfr0 >> 12) & 0xf;
161 wrps = (aa64dfr0 >> 20) & 0xf;
162
163 restore_debug(dbg->dbg_bcr, dbgbcr, brps);
164 restore_debug(dbg->dbg_bvr, dbgbvr, brps);
165 restore_debug(dbg->dbg_wcr, dbgwcr, wrps);
166 restore_debug(dbg->dbg_wvr, dbgwvr, wrps);
167
168 write_sysreg(ctxt->sys_regs[MDCCINT_EL1], mdccint_el1);
169 }
170
171 void __hyp_text __debug_switch_to_guest(struct kvm_vcpu *vcpu)
172 {
173 struct kvm_cpu_context *host_ctxt;
174 struct kvm_cpu_context *guest_ctxt;
175 struct kvm_guest_debug_arch *host_dbg;
176 struct kvm_guest_debug_arch *guest_dbg;
177
178
179
180
181
182 if (!has_vhe())
183 __debug_save_spe_nvhe(&vcpu->arch.host_debug_state.pmscr_el1);
184
185 if (!(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
186 return;
187
188 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
189 guest_ctxt = &vcpu->arch.ctxt;
190 host_dbg = &vcpu->arch.host_debug_state.regs;
191 guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
192
193 __debug_save_state(vcpu, host_dbg, host_ctxt);
194 __debug_restore_state(vcpu, guest_dbg, guest_ctxt);
195 }
196
197 void __hyp_text __debug_switch_to_host(struct kvm_vcpu *vcpu)
198 {
199 struct kvm_cpu_context *host_ctxt;
200 struct kvm_cpu_context *guest_ctxt;
201 struct kvm_guest_debug_arch *host_dbg;
202 struct kvm_guest_debug_arch *guest_dbg;
203
204 if (!has_vhe())
205 __debug_restore_spe_nvhe(vcpu->arch.host_debug_state.pmscr_el1);
206
207 if (!(vcpu->arch.flags & KVM_ARM64_DEBUG_DIRTY))
208 return;
209
210 host_ctxt = kern_hyp_va(vcpu->arch.host_cpu_context);
211 guest_ctxt = &vcpu->arch.ctxt;
212 host_dbg = &vcpu->arch.host_debug_state.regs;
213 guest_dbg = kern_hyp_va(vcpu->arch.debug_ptr);
214
215 __debug_save_state(vcpu, guest_dbg, guest_ctxt);
216 __debug_restore_state(vcpu, host_dbg, host_ctxt);
217
218 vcpu->arch.flags &= ~KVM_ARM64_DEBUG_DIRTY;
219 }
220
221 u32 __hyp_text __kvm_get_mdcr_el2(void)
222 {
223 return read_sysreg(mdcr_el2);
224 }