Commit | Line | Data |
---|---|---|
caab277b | 1 | // SPDX-License-Identifier: GPL-2.0-only |
7c8c5e6a MZ |
2 | /* |
3 | * Copyright (C) 2012,2013 - ARM Ltd | |
4 | * Author: Marc Zyngier <marc.zyngier@arm.com> | |
5 | * | |
6 | * Derived from arch/arm/kvm/coproc.c: | |
7 | * Copyright (C) 2012 - Virtual Open Systems and Columbia University | |
8 | * Authors: Rusty Russell <rusty@rustcorp.com.au> | |
9 | * Christoffer Dall <c.dall@virtualopensystems.com> | |
7c8c5e6a MZ |
10 | */ |
11 | ||
c8857935 | 12 | #include <linux/bitfield.h> |
623eefa8 | 13 | #include <linux/bsearch.h> |
7af0c253 | 14 | #include <linux/cacheinfo.h> |
7c8c5e6a | 15 | #include <linux/kvm_host.h> |
c6d01a94 | 16 | #include <linux/mm.h> |
07d79fe7 | 17 | #include <linux/printk.h> |
7c8c5e6a | 18 | #include <linux/uaccess.h> |
c6d01a94 | 19 | |
7c8c5e6a MZ |
20 | #include <asm/cacheflush.h> |
21 | #include <asm/cputype.h> | |
0c557ed4 | 22 | #include <asm/debug-monitors.h> |
c6d01a94 MR |
23 | #include <asm/esr.h> |
24 | #include <asm/kvm_arm.h> | |
c6d01a94 | 25 | #include <asm/kvm_emulate.h> |
d47533da | 26 | #include <asm/kvm_hyp.h> |
c6d01a94 | 27 | #include <asm/kvm_mmu.h> |
6ff9dc23 | 28 | #include <asm/kvm_nested.h> |
ab946834 | 29 | #include <asm/perf_event.h> |
1f3d8699 | 30 | #include <asm/sysreg.h> |
c6d01a94 | 31 | |
7c8c5e6a MZ |
32 | #include <trace/events/kvm.h> |
33 | ||
34 | #include "sys_regs.h" | |
35 | ||
eef8c85a AB |
36 | #include "trace.h" |
37 | ||
7c8c5e6a | 38 | /* |
62a89c44 MZ |
39 | * For AArch32, we only take care of what is being trapped. Anything |
40 | * that has to do with init and userspace access has to go via the | |
41 | * 64bit interface. | |
7c8c5e6a MZ |
42 | */ |
43 | ||
f24adc65 | 44 | static u64 sys_reg_to_index(const struct sys_reg_desc *reg); |
c118cead JZ |
45 | static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, |
46 | u64 val); | |
f24adc65 | 47 | |
7b5b4df1 | 48 | static bool read_from_write_only(struct kvm_vcpu *vcpu, |
e7f1d1ee MZ |
49 | struct sys_reg_params *params, |
50 | const struct sys_reg_desc *r) | |
7b5b4df1 MZ |
51 | { |
52 | WARN_ONCE(1, "Unexpected sys_reg read to write-only register\n"); | |
53 | print_sys_reg_instr(params); | |
54 | kvm_inject_undefined(vcpu); | |
55 | return false; | |
56 | } | |
57 | ||
7b1dba1f MZ |
58 | static bool write_to_read_only(struct kvm_vcpu *vcpu, |
59 | struct sys_reg_params *params, | |
60 | const struct sys_reg_desc *r) | |
61 | { | |
62 | WARN_ONCE(1, "Unexpected sys_reg write to read-only register\n"); | |
63 | print_sys_reg_instr(params); | |
64 | kvm_inject_undefined(vcpu); | |
65 | return false; | |
66 | } | |
67 | ||
7ea90bdd MZ |
68 | u64 vcpu_read_sys_reg(const struct kvm_vcpu *vcpu, int reg) |
69 | { | |
70 | u64 val = 0x8badf00d8badf00d; | |
71 | ||
30b6ab45 | 72 | if (vcpu_get_flag(vcpu, SYSREGS_ON_CPU) && |
7ea90bdd MZ |
73 | __vcpu_read_sys_reg_from_cpu(reg, &val)) |
74 | return val; | |
75 | ||
76 | return __vcpu_sys_reg(vcpu, reg); | |
77 | } | |
78 | ||
79 | void vcpu_write_sys_reg(struct kvm_vcpu *vcpu, u64 val, int reg) | |
80 | { | |
30b6ab45 | 81 | if (vcpu_get_flag(vcpu, SYSREGS_ON_CPU) && |
7ea90bdd MZ |
82 | __vcpu_write_sys_reg_to_cpu(val, reg)) |
83 | return; | |
84 | ||
242b6f34 | 85 | __vcpu_sys_reg(vcpu, reg) = val; |
d47533da CD |
86 | } |
87 | ||
7c8c5e6a | 88 | /* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */ |
c73a4416 | 89 | #define CSSELR_MAX 14 |
7c8c5e6a | 90 | |
7af0c253 AO |
91 | /* |
92 | * Returns the minimum line size for the selected cache, expressed as | |
93 | * Log2(bytes). | |
94 | */ | |
95 | static u8 get_min_cache_line_size(bool icache) | |
96 | { | |
97 | u64 ctr = read_sanitised_ftr_reg(SYS_CTR_EL0); | |
98 | u8 field; | |
99 | ||
100 | if (icache) | |
101 | field = SYS_FIELD_GET(CTR_EL0, IminLine, ctr); | |
102 | else | |
103 | field = SYS_FIELD_GET(CTR_EL0, DminLine, ctr); | |
104 | ||
105 | /* | |
106 | * Cache line size is represented as Log2(words) in CTR_EL0. | |
107 | * Log2(bytes) can be derived with the following: | |
108 | * | |
109 | * Log2(words) + 2 = Log2(bytes / 4) + 2 | |
110 | * = Log2(bytes) - 2 + 2 | |
111 | * = Log2(bytes) | |
112 | */ | |
113 | return field + 2; | |
114 | } | |
115 | ||
7c8c5e6a | 116 | /* Which cache CCSIDR represents depends on CSSELR value. */ |
7af0c253 AO |
117 | static u32 get_ccsidr(struct kvm_vcpu *vcpu, u32 csselr) |
118 | { | |
119 | u8 line_size; | |
120 | ||
121 | if (vcpu->arch.ccsidr) | |
122 | return vcpu->arch.ccsidr[csselr]; | |
123 | ||
124 | line_size = get_min_cache_line_size(csselr & CSSELR_EL1_InD); | |
125 | ||
126 | /* | |
127 | * Fabricate a CCSIDR value as the overriding value does not exist. | |
128 | * The real CCSIDR value will not be used as it can vary by the | |
129 | * physical CPU which the vcpu currently resides in. | |
130 | * | |
131 | * The line size is determined with get_min_cache_line_size(), which | |
132 | * should be valid for all CPUs even if they have different cache | |
133 | * configuration. | |
134 | * | |
135 | * The associativity bits are cleared, meaning the geometry of all data | |
136 | * and unified caches (which are guaranteed to be PIPT and thus | |
137 | * non-aliasing) are 1 set and 1 way. | |
138 | * Guests should not be doing cache operations by set/way at all, and | |
139 | * for this reason, we trap them and attempt to infer the intent, so | |
140 | * that we can flush the entire guest's address space at the appropriate | |
141 | * time. The exposed geometry minimizes the number of the traps. | |
142 | * [If guests should attempt to infer aliasing properties from the | |
143 | * geometry (which is not permitted by the architecture), they would | |
144 | * only do so for virtually indexed caches.] | |
145 | * | |
146 | * We don't check if the cache level exists as it is allowed to return | |
147 | * an UNKNOWN value if not. | |
148 | */ | |
149 | return SYS_FIELD_PREP(CCSIDR_EL1, LineSize, line_size - 4); | |
150 | } | |
151 | ||
152 | static int set_ccsidr(struct kvm_vcpu *vcpu, u32 csselr, u32 val) | |
7c8c5e6a | 153 | { |
7af0c253 AO |
154 | u8 line_size = FIELD_GET(CCSIDR_EL1_LineSize, val) + 4; |
155 | u32 *ccsidr = vcpu->arch.ccsidr; | |
156 | u32 i; | |
157 | ||
158 | if ((val & CCSIDR_EL1_RES0) || | |
159 | line_size < get_min_cache_line_size(csselr & CSSELR_EL1_InD)) | |
160 | return -EINVAL; | |
161 | ||
162 | if (!ccsidr) { | |
163 | if (val == get_ccsidr(vcpu, csselr)) | |
164 | return 0; | |
7c8c5e6a | 165 | |
5f623a59 | 166 | ccsidr = kmalloc_array(CSSELR_MAX, sizeof(u32), GFP_KERNEL_ACCOUNT); |
7af0c253 AO |
167 | if (!ccsidr) |
168 | return -ENOMEM; | |
7c8c5e6a | 169 | |
7af0c253 AO |
170 | for (i = 0; i < CSSELR_MAX; i++) |
171 | ccsidr[i] = get_ccsidr(vcpu, i); | |
172 | ||
173 | vcpu->arch.ccsidr = ccsidr; | |
174 | } | |
7c8c5e6a | 175 | |
7af0c253 | 176 | ccsidr[csselr] = val; |
7c8c5e6a | 177 | |
7af0c253 | 178 | return 0; |
7c8c5e6a MZ |
179 | } |
180 | ||
6ff9dc23 JL |
181 | static bool access_rw(struct kvm_vcpu *vcpu, |
182 | struct sys_reg_params *p, | |
183 | const struct sys_reg_desc *r) | |
184 | { | |
185 | if (p->is_write) | |
186 | vcpu_write_sys_reg(vcpu, p->regval, r->reg); | |
187 | else | |
188 | p->regval = vcpu_read_sys_reg(vcpu, r->reg); | |
189 | ||
190 | return true; | |
191 | } | |
192 | ||
3c1e7165 MZ |
193 | /* |
194 | * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized). | |
195 | */ | |
7c8c5e6a | 196 | static bool access_dcsw(struct kvm_vcpu *vcpu, |
3fec037d | 197 | struct sys_reg_params *p, |
7c8c5e6a MZ |
198 | const struct sys_reg_desc *r) |
199 | { | |
7c8c5e6a | 200 | if (!p->is_write) |
e7f1d1ee | 201 | return read_from_write_only(vcpu, p, r); |
7c8c5e6a | 202 | |
09605e94 MZ |
203 | /* |
204 | * Only track S/W ops if we don't have FWB. It still indicates | |
205 | * that the guest is a bit broken (S/W operations should only | |
206 | * be done by firmware, knowing that there is only a single | |
207 | * CPU left in the system, and certainly not from non-secure | |
208 | * software). | |
209 | */ | |
210 | if (!cpus_have_const_cap(ARM64_HAS_STAGE2_FWB)) | |
211 | kvm_set_way_flush(vcpu); | |
212 | ||
7c8c5e6a MZ |
213 | return true; |
214 | } | |
215 | ||
d282fa3c MZ |
216 | static bool access_dcgsw(struct kvm_vcpu *vcpu, |
217 | struct sys_reg_params *p, | |
218 | const struct sys_reg_desc *r) | |
219 | { | |
220 | if (!kvm_has_mte(vcpu->kvm)) { | |
221 | kvm_inject_undefined(vcpu); | |
222 | return false; | |
223 | } | |
224 | ||
225 | /* Treat MTE S/W ops as we treat the classic ones: with contempt */ | |
226 | return access_dcsw(vcpu, p, r); | |
227 | } | |
228 | ||
b1ea1d76 MZ |
229 | static void get_access_mask(const struct sys_reg_desc *r, u64 *mask, u64 *shift) |
230 | { | |
231 | switch (r->aarch32_map) { | |
232 | case AA32_LO: | |
233 | *mask = GENMASK_ULL(31, 0); | |
234 | *shift = 0; | |
235 | break; | |
236 | case AA32_HI: | |
237 | *mask = GENMASK_ULL(63, 32); | |
238 | *shift = 32; | |
239 | break; | |
240 | default: | |
241 | *mask = GENMASK_ULL(63, 0); | |
242 | *shift = 0; | |
243 | break; | |
244 | } | |
245 | } | |
246 | ||
4d44923b MZ |
247 | /* |
248 | * Generic accessor for VM registers. Only called as long as HCR_TVM | |
3c1e7165 MZ |
249 | * is set. If the guest enables the MMU, we stop trapping the VM |
250 | * sys_regs and leave it in complete control of the caches. | |
4d44923b MZ |
251 | */ |
252 | static bool access_vm_reg(struct kvm_vcpu *vcpu, | |
3fec037d | 253 | struct sys_reg_params *p, |
4d44923b MZ |
254 | const struct sys_reg_desc *r) |
255 | { | |
3c1e7165 | 256 | bool was_enabled = vcpu_has_cache_enabled(vcpu); |
b1ea1d76 | 257 | u64 val, mask, shift; |
4d44923b MZ |
258 | |
259 | BUG_ON(!p->is_write); | |
260 | ||
b1ea1d76 | 261 | get_access_mask(r, &mask, &shift); |
52f6c4f0 | 262 | |
b1ea1d76 MZ |
263 | if (~mask) { |
264 | val = vcpu_read_sys_reg(vcpu, r->reg); | |
265 | val &= ~mask; | |
dedf97e8 | 266 | } else { |
b1ea1d76 | 267 | val = 0; |
dedf97e8 | 268 | } |
b1ea1d76 MZ |
269 | |
270 | val |= (p->regval & (mask >> shift)) << shift; | |
271 | vcpu_write_sys_reg(vcpu, val, r->reg); | |
f0a3eaff | 272 | |
3c1e7165 | 273 | kvm_toggle_cache(vcpu, was_enabled); |
4d44923b MZ |
274 | return true; |
275 | } | |
276 | ||
af473829 JM |
277 | static bool access_actlr(struct kvm_vcpu *vcpu, |
278 | struct sys_reg_params *p, | |
279 | const struct sys_reg_desc *r) | |
280 | { | |
b1ea1d76 MZ |
281 | u64 mask, shift; |
282 | ||
af473829 JM |
283 | if (p->is_write) |
284 | return ignore_write(vcpu, p); | |
285 | ||
b1ea1d76 MZ |
286 | get_access_mask(r, &mask, &shift); |
287 | p->regval = (vcpu_read_sys_reg(vcpu, r->reg) & mask) >> shift; | |
af473829 JM |
288 | |
289 | return true; | |
290 | } | |
291 | ||
6d52f35a AP |
292 | /* |
293 | * Trap handler for the GICv3 SGI generation system register. | |
294 | * Forward the request to the VGIC emulation. | |
295 | * The cp15_64 code makes sure this automatically works | |
296 | * for both AArch64 and AArch32 accesses. | |
297 | */ | |
298 | static bool access_gic_sgi(struct kvm_vcpu *vcpu, | |
3fec037d | 299 | struct sys_reg_params *p, |
6d52f35a AP |
300 | const struct sys_reg_desc *r) |
301 | { | |
03bd646d MZ |
302 | bool g1; |
303 | ||
6d52f35a | 304 | if (!p->is_write) |
e7f1d1ee | 305 | return read_from_write_only(vcpu, p, r); |
6d52f35a | 306 | |
03bd646d MZ |
307 | /* |
308 | * In a system where GICD_CTLR.DS=1, a ICC_SGI0R_EL1 access generates | |
309 | * Group0 SGIs only, while ICC_SGI1R_EL1 can generate either group, | |
310 | * depending on the SGI configuration. ICC_ASGI1R_EL1 is effectively | |
311 | * equivalent to ICC_SGI0R_EL1, as there is no "alternative" secure | |
312 | * group. | |
313 | */ | |
50f30453 | 314 | if (p->Op0 == 0) { /* AArch32 */ |
03bd646d MZ |
315 | switch (p->Op1) { |
316 | default: /* Keep GCC quiet */ | |
317 | case 0: /* ICC_SGI1R */ | |
318 | g1 = true; | |
319 | break; | |
320 | case 1: /* ICC_ASGI1R */ | |
321 | case 2: /* ICC_SGI0R */ | |
322 | g1 = false; | |
323 | break; | |
324 | } | |
50f30453 | 325 | } else { /* AArch64 */ |
03bd646d MZ |
326 | switch (p->Op2) { |
327 | default: /* Keep GCC quiet */ | |
328 | case 5: /* ICC_SGI1R_EL1 */ | |
329 | g1 = true; | |
330 | break; | |
331 | case 6: /* ICC_ASGI1R_EL1 */ | |
332 | case 7: /* ICC_SGI0R_EL1 */ | |
333 | g1 = false; | |
334 | break; | |
335 | } | |
336 | } | |
337 | ||
338 | vgic_v3_dispatch_sgi(vcpu, p->regval, g1); | |
6d52f35a AP |
339 | |
340 | return true; | |
341 | } | |
342 | ||
b34f2bcb MZ |
343 | static bool access_gic_sre(struct kvm_vcpu *vcpu, |
344 | struct sys_reg_params *p, | |
345 | const struct sys_reg_desc *r) | |
346 | { | |
347 | if (p->is_write) | |
348 | return ignore_write(vcpu, p); | |
349 | ||
350 | p->regval = vcpu->arch.vgic_cpu.vgic_v3.vgic_sre; | |
351 | return true; | |
352 | } | |
353 | ||
7609c125 | 354 | static bool trap_raz_wi(struct kvm_vcpu *vcpu, |
3fec037d | 355 | struct sys_reg_params *p, |
7609c125 | 356 | const struct sys_reg_desc *r) |
7c8c5e6a MZ |
357 | { |
358 | if (p->is_write) | |
359 | return ignore_write(vcpu, p); | |
360 | else | |
361 | return read_zero(vcpu, p); | |
362 | } | |
363 | ||
6ff9dc23 JL |
364 | static bool trap_undef(struct kvm_vcpu *vcpu, |
365 | struct sys_reg_params *p, | |
366 | const struct sys_reg_desc *r) | |
367 | { | |
368 | kvm_inject_undefined(vcpu); | |
369 | return false; | |
370 | } | |
371 | ||
22925521 MZ |
372 | /* |
373 | * ARMv8.1 mandates at least a trivial LORegion implementation, where all the | |
374 | * RW registers are RES0 (which we can implement as RAZ/WI). On an ARMv8.0 | |
375 | * system, these registers should UNDEF. LORID_EL1 being a RO register, we | |
376 | * treat it separately. | |
377 | */ | |
378 | static bool trap_loregion(struct kvm_vcpu *vcpu, | |
379 | struct sys_reg_params *p, | |
380 | const struct sys_reg_desc *r) | |
cc33c4e2 | 381 | { |
8b6958d6 | 382 | u64 val = IDREG(vcpu->kvm, SYS_ID_AA64MMFR1_EL1); |
7ba8b438 | 383 | u32 sr = reg_to_encoding(r); |
22925521 | 384 | |
6fcd0193 | 385 | if (!(val & (0xfUL << ID_AA64MMFR1_EL1_LO_SHIFT))) { |
22925521 MZ |
386 | kvm_inject_undefined(vcpu); |
387 | return false; | |
388 | } | |
389 | ||
390 | if (p->is_write && sr == SYS_LORID_EL1) | |
391 | return write_to_read_only(vcpu, p, r); | |
392 | ||
393 | return trap_raz_wi(vcpu, p, r); | |
cc33c4e2 MR |
394 | } |
395 | ||
f24adc65 OU |
396 | static bool trap_oslar_el1(struct kvm_vcpu *vcpu, |
397 | struct sys_reg_params *p, | |
398 | const struct sys_reg_desc *r) | |
399 | { | |
400 | u64 oslsr; | |
401 | ||
402 | if (!p->is_write) | |
403 | return read_from_write_only(vcpu, p, r); | |
404 | ||
405 | /* Forward the OSLK bit to OSLSR */ | |
187de7c2 MB |
406 | oslsr = __vcpu_sys_reg(vcpu, OSLSR_EL1) & ~OSLSR_EL1_OSLK; |
407 | if (p->regval & OSLAR_EL1_OSLK) | |
408 | oslsr |= OSLSR_EL1_OSLK; | |
f24adc65 OU |
409 | |
410 | __vcpu_sys_reg(vcpu, OSLSR_EL1) = oslsr; | |
411 | return true; | |
412 | } | |
413 | ||
0c557ed4 | 414 | static bool trap_oslsr_el1(struct kvm_vcpu *vcpu, |
3fec037d | 415 | struct sys_reg_params *p, |
0c557ed4 MZ |
416 | const struct sys_reg_desc *r) |
417 | { | |
d42e2671 | 418 | if (p->is_write) |
e2ffceaa | 419 | return write_to_read_only(vcpu, p, r); |
d42e2671 OU |
420 | |
421 | p->regval = __vcpu_sys_reg(vcpu, r->reg); | |
422 | return true; | |
423 | } | |
424 | ||
425 | static int set_oslsr_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 426 | u64 val) |
d42e2671 | 427 | { |
f24adc65 OU |
428 | /* |
429 | * The only modifiable bit is the OSLK bit. Refuse the write if | |
430 | * userspace attempts to change any other bit in the register. | |
431 | */ | |
187de7c2 | 432 | if ((val ^ rd->val) & ~OSLSR_EL1_OSLK) |
d42e2671 OU |
433 | return -EINVAL; |
434 | ||
f24adc65 | 435 | __vcpu_sys_reg(vcpu, rd->reg) = val; |
d42e2671 | 436 | return 0; |
0c557ed4 MZ |
437 | } |
438 | ||
439 | static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu, | |
3fec037d | 440 | struct sys_reg_params *p, |
0c557ed4 MZ |
441 | const struct sys_reg_desc *r) |
442 | { | |
443 | if (p->is_write) { | |
444 | return ignore_write(vcpu, p); | |
445 | } else { | |
1f3d8699 | 446 | p->regval = read_sysreg(dbgauthstatus_el1); |
0c557ed4 MZ |
447 | return true; |
448 | } | |
449 | } | |
450 | ||
451 | /* | |
452 | * We want to avoid world-switching all the DBG registers all the | |
453 | * time: | |
e6bc555c | 454 | * |
0c557ed4 MZ |
455 | * - If we've touched any debug register, it is likely that we're |
456 | * going to touch more of them. It then makes sense to disable the | |
457 | * traps and start doing the save/restore dance | |
458 | * - If debug is active (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), it is | |
459 | * then mandatory to save/restore the registers, as the guest | |
460 | * depends on them. | |
e6bc555c | 461 | * |
0c557ed4 MZ |
462 | * For this, we use a DIRTY bit, indicating the guest has modified the |
463 | * debug registers, used as follow: | |
464 | * | |
465 | * On guest entry: | |
466 | * - If the dirty bit is set (because we're coming back from trapping), | |
467 | * disable the traps, save host registers, restore guest registers. | |
468 | * - If debug is actively in use (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), | |
469 | * set the dirty bit, disable the traps, save host registers, | |
470 | * restore guest registers. | |
471 | * - Otherwise, enable the traps | |
472 | * | |
473 | * On guest exit: | |
474 | * - If the dirty bit is set, save guest registers, restore host | |
475 | * registers and clear the dirty bit. This ensure that the host can | |
476 | * now use the debug registers. | |
477 | */ | |
478 | static bool trap_debug_regs(struct kvm_vcpu *vcpu, | |
3fec037d | 479 | struct sys_reg_params *p, |
0c557ed4 MZ |
480 | const struct sys_reg_desc *r) |
481 | { | |
6ff9dc23 JL |
482 | access_rw(vcpu, p, r); |
483 | if (p->is_write) | |
b1da4908 | 484 | vcpu_set_flag(vcpu, DEBUG_DIRTY); |
0c557ed4 | 485 | |
2ec5be3d | 486 | trace_trap_reg(__func__, r->reg, p->is_write, p->regval); |
eef8c85a | 487 | |
0c557ed4 MZ |
488 | return true; |
489 | } | |
490 | ||
84e690bf AB |
491 | /* |
492 | * reg_to_dbg/dbg_to_reg | |
493 | * | |
494 | * A 32 bit write to a debug register leave top bits alone | |
495 | * A 32 bit read from a debug register only returns the bottom bits | |
496 | * | |
b1da4908 MZ |
497 | * All writes will set the DEBUG_DIRTY flag to ensure the hyp code |
498 | * switches between host and guest values in future. | |
84e690bf | 499 | */ |
281243cb MZ |
500 | static void reg_to_dbg(struct kvm_vcpu *vcpu, |
501 | struct sys_reg_params *p, | |
1da42c34 | 502 | const struct sys_reg_desc *rd, |
281243cb | 503 | u64 *dbg_reg) |
84e690bf | 504 | { |
1da42c34 | 505 | u64 mask, shift, val; |
84e690bf | 506 | |
1da42c34 | 507 | get_access_mask(rd, &mask, &shift); |
84e690bf | 508 | |
1da42c34 MZ |
509 | val = *dbg_reg; |
510 | val &= ~mask; | |
511 | val |= (p->regval & (mask >> shift)) << shift; | |
84e690bf | 512 | *dbg_reg = val; |
1da42c34 | 513 | |
b1da4908 | 514 | vcpu_set_flag(vcpu, DEBUG_DIRTY); |
84e690bf AB |
515 | } |
516 | ||
281243cb MZ |
517 | static void dbg_to_reg(struct kvm_vcpu *vcpu, |
518 | struct sys_reg_params *p, | |
1da42c34 | 519 | const struct sys_reg_desc *rd, |
281243cb | 520 | u64 *dbg_reg) |
84e690bf | 521 | { |
1da42c34 MZ |
522 | u64 mask, shift; |
523 | ||
524 | get_access_mask(rd, &mask, &shift); | |
525 | p->regval = (*dbg_reg & mask) >> shift; | |
84e690bf AB |
526 | } |
527 | ||
281243cb MZ |
528 | static bool trap_bvr(struct kvm_vcpu *vcpu, |
529 | struct sys_reg_params *p, | |
530 | const struct sys_reg_desc *rd) | |
84e690bf | 531 | { |
cb853ded | 532 | u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->CRm]; |
84e690bf AB |
533 | |
534 | if (p->is_write) | |
1da42c34 | 535 | reg_to_dbg(vcpu, p, rd, dbg_reg); |
84e690bf | 536 | else |
1da42c34 | 537 | dbg_to_reg(vcpu, p, rd, dbg_reg); |
84e690bf | 538 | |
cb853ded | 539 | trace_trap_reg(__func__, rd->CRm, p->is_write, *dbg_reg); |
eef8c85a | 540 | |
84e690bf AB |
541 | return true; |
542 | } | |
543 | ||
544 | static int set_bvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 545 | u64 val) |
84e690bf | 546 | { |
978ceeb3 | 547 | vcpu->arch.vcpu_debug_state.dbg_bvr[rd->CRm] = val; |
84e690bf AB |
548 | return 0; |
549 | } | |
550 | ||
551 | static int get_bvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 552 | u64 *val) |
84e690bf | 553 | { |
978ceeb3 | 554 | *val = vcpu->arch.vcpu_debug_state.dbg_bvr[rd->CRm]; |
84e690bf AB |
555 | return 0; |
556 | } | |
557 | ||
d86cde6e | 558 | static u64 reset_bvr(struct kvm_vcpu *vcpu, |
281243cb | 559 | const struct sys_reg_desc *rd) |
84e690bf | 560 | { |
cb853ded | 561 | vcpu->arch.vcpu_debug_state.dbg_bvr[rd->CRm] = rd->val; |
d86cde6e | 562 | return rd->val; |
84e690bf AB |
563 | } |
564 | ||
281243cb MZ |
565 | static bool trap_bcr(struct kvm_vcpu *vcpu, |
566 | struct sys_reg_params *p, | |
567 | const struct sys_reg_desc *rd) | |
84e690bf | 568 | { |
cb853ded | 569 | u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->CRm]; |
84e690bf AB |
570 | |
571 | if (p->is_write) | |
1da42c34 | 572 | reg_to_dbg(vcpu, p, rd, dbg_reg); |
84e690bf | 573 | else |
1da42c34 | 574 | dbg_to_reg(vcpu, p, rd, dbg_reg); |
84e690bf | 575 | |
cb853ded | 576 | trace_trap_reg(__func__, rd->CRm, p->is_write, *dbg_reg); |
eef8c85a | 577 | |
84e690bf AB |
578 | return true; |
579 | } | |
580 | ||
581 | static int set_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 582 | u64 val) |
84e690bf | 583 | { |
978ceeb3 | 584 | vcpu->arch.vcpu_debug_state.dbg_bcr[rd->CRm] = val; |
84e690bf AB |
585 | return 0; |
586 | } | |
587 | ||
588 | static int get_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 589 | u64 *val) |
84e690bf | 590 | { |
978ceeb3 | 591 | *val = vcpu->arch.vcpu_debug_state.dbg_bcr[rd->CRm]; |
84e690bf AB |
592 | return 0; |
593 | } | |
594 | ||
d86cde6e | 595 | static u64 reset_bcr(struct kvm_vcpu *vcpu, |
281243cb | 596 | const struct sys_reg_desc *rd) |
84e690bf | 597 | { |
cb853ded | 598 | vcpu->arch.vcpu_debug_state.dbg_bcr[rd->CRm] = rd->val; |
d86cde6e | 599 | return rd->val; |
84e690bf AB |
600 | } |
601 | ||
281243cb MZ |
602 | static bool trap_wvr(struct kvm_vcpu *vcpu, |
603 | struct sys_reg_params *p, | |
604 | const struct sys_reg_desc *rd) | |
84e690bf | 605 | { |
cb853ded | 606 | u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm]; |
84e690bf AB |
607 | |
608 | if (p->is_write) | |
1da42c34 | 609 | reg_to_dbg(vcpu, p, rd, dbg_reg); |
84e690bf | 610 | else |
1da42c34 | 611 | dbg_to_reg(vcpu, p, rd, dbg_reg); |
84e690bf | 612 | |
cb853ded MZ |
613 | trace_trap_reg(__func__, rd->CRm, p->is_write, |
614 | vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm]); | |
eef8c85a | 615 | |
84e690bf AB |
616 | return true; |
617 | } | |
618 | ||
619 | static int set_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 620 | u64 val) |
84e690bf | 621 | { |
978ceeb3 | 622 | vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm] = val; |
84e690bf AB |
623 | return 0; |
624 | } | |
625 | ||
626 | static int get_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 627 | u64 *val) |
84e690bf | 628 | { |
978ceeb3 | 629 | *val = vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm]; |
84e690bf AB |
630 | return 0; |
631 | } | |
632 | ||
d86cde6e | 633 | static u64 reset_wvr(struct kvm_vcpu *vcpu, |
281243cb | 634 | const struct sys_reg_desc *rd) |
84e690bf | 635 | { |
cb853ded | 636 | vcpu->arch.vcpu_debug_state.dbg_wvr[rd->CRm] = rd->val; |
d86cde6e | 637 | return rd->val; |
84e690bf AB |
638 | } |
639 | ||
281243cb MZ |
640 | static bool trap_wcr(struct kvm_vcpu *vcpu, |
641 | struct sys_reg_params *p, | |
642 | const struct sys_reg_desc *rd) | |
84e690bf | 643 | { |
cb853ded | 644 | u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->CRm]; |
84e690bf AB |
645 | |
646 | if (p->is_write) | |
1da42c34 | 647 | reg_to_dbg(vcpu, p, rd, dbg_reg); |
84e690bf | 648 | else |
1da42c34 | 649 | dbg_to_reg(vcpu, p, rd, dbg_reg); |
84e690bf | 650 | |
cb853ded | 651 | trace_trap_reg(__func__, rd->CRm, p->is_write, *dbg_reg); |
eef8c85a | 652 | |
84e690bf AB |
653 | return true; |
654 | } | |
655 | ||
656 | static int set_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 657 | u64 val) |
84e690bf | 658 | { |
978ceeb3 | 659 | vcpu->arch.vcpu_debug_state.dbg_wcr[rd->CRm] = val; |
84e690bf AB |
660 | return 0; |
661 | } | |
662 | ||
663 | static int get_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 664 | u64 *val) |
84e690bf | 665 | { |
978ceeb3 | 666 | *val = vcpu->arch.vcpu_debug_state.dbg_wcr[rd->CRm]; |
84e690bf AB |
667 | return 0; |
668 | } | |
669 | ||
d86cde6e | 670 | static u64 reset_wcr(struct kvm_vcpu *vcpu, |
281243cb | 671 | const struct sys_reg_desc *rd) |
84e690bf | 672 | { |
cb853ded | 673 | vcpu->arch.vcpu_debug_state.dbg_wcr[rd->CRm] = rd->val; |
d86cde6e | 674 | return rd->val; |
84e690bf AB |
675 | } |
676 | ||
d86cde6e | 677 | static u64 reset_amair_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
7c8c5e6a | 678 | { |
8d404c4c CD |
679 | u64 amair = read_sysreg(amair_el1); |
680 | vcpu_write_sys_reg(vcpu, amair, AMAIR_EL1); | |
d86cde6e | 681 | return amair; |
7c8c5e6a MZ |
682 | } |
683 | ||
d86cde6e | 684 | static u64 reset_actlr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
af473829 JM |
685 | { |
686 | u64 actlr = read_sysreg(actlr_el1); | |
687 | vcpu_write_sys_reg(vcpu, actlr, ACTLR_EL1); | |
d86cde6e | 688 | return actlr; |
af473829 JM |
689 | } |
690 | ||
d86cde6e | 691 | static u64 reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
7c8c5e6a | 692 | { |
4429fc64 AP |
693 | u64 mpidr; |
694 | ||
7c8c5e6a | 695 | /* |
4429fc64 AP |
696 | * Map the vcpu_id into the first three affinity level fields of |
697 | * the MPIDR. We limit the number of VCPUs in level 0 due to a | |
698 | * limitation to 16 CPUs in that level in the ICC_SGIxR registers | |
699 | * of the GICv3 to be able to address each CPU directly when | |
700 | * sending IPIs. | |
7c8c5e6a | 701 | */ |
4429fc64 AP |
702 | mpidr = (vcpu->vcpu_id & 0x0f) << MPIDR_LEVEL_SHIFT(0); |
703 | mpidr |= ((vcpu->vcpu_id >> 4) & 0xff) << MPIDR_LEVEL_SHIFT(1); | |
704 | mpidr |= ((vcpu->vcpu_id >> 12) & 0xff) << MPIDR_LEVEL_SHIFT(2); | |
d86cde6e JZ |
705 | mpidr |= (1ULL << 31); |
706 | vcpu_write_sys_reg(vcpu, mpidr, MPIDR_EL1); | |
707 | ||
708 | return mpidr; | |
7c8c5e6a MZ |
709 | } |
710 | ||
11663111 MZ |
711 | static unsigned int pmu_visibility(const struct kvm_vcpu *vcpu, |
712 | const struct sys_reg_desc *r) | |
713 | { | |
714 | if (kvm_vcpu_has_pmu(vcpu)) | |
715 | return 0; | |
716 | ||
717 | return REG_HIDDEN; | |
718 | } | |
719 | ||
d86cde6e | 720 | static u64 reset_pmu_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
0ab410a9 MZ |
721 | { |
722 | u64 n, mask = BIT(ARMV8_PMU_CYCLE_IDX); | |
723 | ||
724 | /* No PMU available, any PMU reg may UNDEF... */ | |
725 | if (!kvm_arm_support_pmu_v3()) | |
d86cde6e | 726 | return 0; |
0ab410a9 MZ |
727 | |
728 | n = read_sysreg(pmcr_el0) >> ARMV8_PMU_PMCR_N_SHIFT; | |
729 | n &= ARMV8_PMU_PMCR_N_MASK; | |
730 | if (n) | |
731 | mask |= GENMASK(n - 1, 0); | |
732 | ||
733 | reset_unknown(vcpu, r); | |
734 | __vcpu_sys_reg(vcpu, r->reg) &= mask; | |
d86cde6e JZ |
735 | |
736 | return __vcpu_sys_reg(vcpu, r->reg); | |
0ab410a9 MZ |
737 | } |
738 | ||
d86cde6e | 739 | static u64 reset_pmevcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
0ab410a9 MZ |
740 | { |
741 | reset_unknown(vcpu, r); | |
742 | __vcpu_sys_reg(vcpu, r->reg) &= GENMASK(31, 0); | |
d86cde6e JZ |
743 | |
744 | return __vcpu_sys_reg(vcpu, r->reg); | |
0ab410a9 MZ |
745 | } |
746 | ||
d86cde6e | 747 | static u64 reset_pmevtyper(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
0ab410a9 MZ |
748 | { |
749 | reset_unknown(vcpu, r); | |
750 | __vcpu_sys_reg(vcpu, r->reg) &= ARMV8_PMU_EVTYPE_MASK; | |
d86cde6e JZ |
751 | |
752 | return __vcpu_sys_reg(vcpu, r->reg); | |
0ab410a9 MZ |
753 | } |
754 | ||
d86cde6e | 755 | static u64 reset_pmselr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
0ab410a9 MZ |
756 | { |
757 | reset_unknown(vcpu, r); | |
758 | __vcpu_sys_reg(vcpu, r->reg) &= ARMV8_PMU_COUNTER_MASK; | |
d86cde6e JZ |
759 | |
760 | return __vcpu_sys_reg(vcpu, r->reg); | |
0ab410a9 MZ |
761 | } |
762 | ||
d86cde6e | 763 | static u64 reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
ab946834 | 764 | { |
292e8f14 | 765 | u64 pmcr; |
ab946834 | 766 | |
2a5f1b67 MZ |
767 | /* No PMU available, PMCR_EL0 may UNDEF... */ |
768 | if (!kvm_arm_support_pmu_v3()) | |
d86cde6e | 769 | return 0; |
2a5f1b67 | 770 | |
292e8f14 | 771 | /* Only preserve PMCR_EL0.N, and reset the rest to 0 */ |
aff23483 | 772 | pmcr = read_sysreg(pmcr_el0) & (ARMV8_PMU_PMCR_N_MASK << ARMV8_PMU_PMCR_N_SHIFT); |
f3c6efc7 | 773 | if (!kvm_supports_32bit_el0()) |
292e8f14 MZ |
774 | pmcr |= ARMV8_PMU_PMCR_LC; |
775 | ||
776 | __vcpu_sys_reg(vcpu, r->reg) = pmcr; | |
d86cde6e JZ |
777 | |
778 | return __vcpu_sys_reg(vcpu, r->reg); | |
ab946834 SZ |
779 | } |
780 | ||
6c007036 | 781 | static bool check_pmu_access_disabled(struct kvm_vcpu *vcpu, u64 flags) |
d692b8ad | 782 | { |
8d404c4c | 783 | u64 reg = __vcpu_sys_reg(vcpu, PMUSERENR_EL0); |
7ded92e2 | 784 | bool enabled = (reg & flags) || vcpu_mode_priv(vcpu); |
d692b8ad | 785 | |
24d5950f MZ |
786 | if (!enabled) |
787 | kvm_inject_undefined(vcpu); | |
d692b8ad | 788 | |
6c007036 | 789 | return !enabled; |
d692b8ad SZ |
790 | } |
791 | ||
6c007036 | 792 | static bool pmu_access_el0_disabled(struct kvm_vcpu *vcpu) |
d692b8ad | 793 | { |
6c007036 MZ |
794 | return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_EN); |
795 | } | |
d692b8ad | 796 | |
6c007036 MZ |
797 | static bool pmu_write_swinc_el0_disabled(struct kvm_vcpu *vcpu) |
798 | { | |
799 | return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_SW | ARMV8_PMU_USERENR_EN); | |
d692b8ad SZ |
800 | } |
801 | ||
802 | static bool pmu_access_cycle_counter_el0_disabled(struct kvm_vcpu *vcpu) | |
803 | { | |
6c007036 | 804 | return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_CR | ARMV8_PMU_USERENR_EN); |
d692b8ad SZ |
805 | } |
806 | ||
807 | static bool pmu_access_event_counter_el0_disabled(struct kvm_vcpu *vcpu) | |
808 | { | |
6c007036 | 809 | return check_pmu_access_disabled(vcpu, ARMV8_PMU_USERENR_ER | ARMV8_PMU_USERENR_EN); |
d692b8ad SZ |
810 | } |
811 | ||
ab946834 SZ |
812 | static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
813 | const struct sys_reg_desc *r) | |
814 | { | |
815 | u64 val; | |
816 | ||
d692b8ad SZ |
817 | if (pmu_access_el0_disabled(vcpu)) |
818 | return false; | |
819 | ||
ab946834 | 820 | if (p->is_write) { |
64d6820d MZ |
821 | /* |
822 | * Only update writeable bits of PMCR (continuing into | |
823 | * kvm_pmu_handle_pmcr() as well) | |
824 | */ | |
8d404c4c | 825 | val = __vcpu_sys_reg(vcpu, PMCR_EL0); |
ab946834 SZ |
826 | val &= ~ARMV8_PMU_PMCR_MASK; |
827 | val |= p->regval & ARMV8_PMU_PMCR_MASK; | |
f3c6efc7 | 828 | if (!kvm_supports_32bit_el0()) |
6f163714 | 829 | val |= ARMV8_PMU_PMCR_LC; |
76993739 | 830 | kvm_pmu_handle_pmcr(vcpu, val); |
ab946834 SZ |
831 | } else { |
832 | /* PMCR.P & PMCR.C are RAZ */ | |
8d404c4c | 833 | val = __vcpu_sys_reg(vcpu, PMCR_EL0) |
ab946834 SZ |
834 | & ~(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C); |
835 | p->regval = val; | |
836 | } | |
837 | ||
838 | return true; | |
839 | } | |
840 | ||
3965c3ce SZ |
841 | static bool access_pmselr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
842 | const struct sys_reg_desc *r) | |
843 | { | |
d692b8ad SZ |
844 | if (pmu_access_event_counter_el0_disabled(vcpu)) |
845 | return false; | |
846 | ||
3965c3ce | 847 | if (p->is_write) |
8d404c4c | 848 | __vcpu_sys_reg(vcpu, PMSELR_EL0) = p->regval; |
3965c3ce SZ |
849 | else |
850 | /* return PMSELR.SEL field */ | |
8d404c4c | 851 | p->regval = __vcpu_sys_reg(vcpu, PMSELR_EL0) |
3965c3ce SZ |
852 | & ARMV8_PMU_COUNTER_MASK; |
853 | ||
854 | return true; | |
855 | } | |
856 | ||
a86b5505 SZ |
857 | static bool access_pmceid(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
858 | const struct sys_reg_desc *r) | |
859 | { | |
99b6a401 | 860 | u64 pmceid, mask, shift; |
a86b5505 | 861 | |
a86b5505 SZ |
862 | BUG_ON(p->is_write); |
863 | ||
d692b8ad SZ |
864 | if (pmu_access_el0_disabled(vcpu)) |
865 | return false; | |
866 | ||
99b6a401 MZ |
867 | get_access_mask(r, &mask, &shift); |
868 | ||
88865bec | 869 | pmceid = kvm_pmu_get_pmceid(vcpu, (p->Op2 & 1)); |
99b6a401 MZ |
870 | pmceid &= mask; |
871 | pmceid >>= shift; | |
a86b5505 SZ |
872 | |
873 | p->regval = pmceid; | |
874 | ||
875 | return true; | |
876 | } | |
877 | ||
051ff581 SZ |
878 | static bool pmu_counter_idx_valid(struct kvm_vcpu *vcpu, u64 idx) |
879 | { | |
880 | u64 pmcr, val; | |
881 | ||
8d404c4c | 882 | pmcr = __vcpu_sys_reg(vcpu, PMCR_EL0); |
051ff581 | 883 | val = (pmcr >> ARMV8_PMU_PMCR_N_SHIFT) & ARMV8_PMU_PMCR_N_MASK; |
24d5950f MZ |
884 | if (idx >= val && idx != ARMV8_PMU_CYCLE_IDX) { |
885 | kvm_inject_undefined(vcpu); | |
051ff581 | 886 | return false; |
24d5950f | 887 | } |
051ff581 SZ |
888 | |
889 | return true; | |
890 | } | |
891 | ||
9228b261 RW |
892 | static int get_pmu_evcntr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r, |
893 | u64 *val) | |
894 | { | |
895 | u64 idx; | |
896 | ||
897 | if (r->CRn == 9 && r->CRm == 13 && r->Op2 == 0) | |
898 | /* PMCCNTR_EL0 */ | |
899 | idx = ARMV8_PMU_CYCLE_IDX; | |
900 | else | |
901 | /* PMEVCNTRn_EL0 */ | |
902 | idx = ((r->CRm & 3) << 3) | (r->Op2 & 7); | |
903 | ||
904 | *val = kvm_pmu_get_counter_value(vcpu, idx); | |
905 | return 0; | |
906 | } | |
907 | ||
051ff581 SZ |
908 | static bool access_pmu_evcntr(struct kvm_vcpu *vcpu, |
909 | struct sys_reg_params *p, | |
910 | const struct sys_reg_desc *r) | |
911 | { | |
a3da9358 | 912 | u64 idx = ~0UL; |
051ff581 SZ |
913 | |
914 | if (r->CRn == 9 && r->CRm == 13) { | |
915 | if (r->Op2 == 2) { | |
916 | /* PMXEVCNTR_EL0 */ | |
d692b8ad SZ |
917 | if (pmu_access_event_counter_el0_disabled(vcpu)) |
918 | return false; | |
919 | ||
8d404c4c | 920 | idx = __vcpu_sys_reg(vcpu, PMSELR_EL0) |
051ff581 SZ |
921 | & ARMV8_PMU_COUNTER_MASK; |
922 | } else if (r->Op2 == 0) { | |
923 | /* PMCCNTR_EL0 */ | |
d692b8ad SZ |
924 | if (pmu_access_cycle_counter_el0_disabled(vcpu)) |
925 | return false; | |
926 | ||
051ff581 | 927 | idx = ARMV8_PMU_CYCLE_IDX; |
051ff581 | 928 | } |
9e3f7a29 WH |
929 | } else if (r->CRn == 0 && r->CRm == 9) { |
930 | /* PMCCNTR */ | |
931 | if (pmu_access_event_counter_el0_disabled(vcpu)) | |
932 | return false; | |
933 | ||
934 | idx = ARMV8_PMU_CYCLE_IDX; | |
051ff581 SZ |
935 | } else if (r->CRn == 14 && (r->CRm & 12) == 8) { |
936 | /* PMEVCNTRn_EL0 */ | |
d692b8ad SZ |
937 | if (pmu_access_event_counter_el0_disabled(vcpu)) |
938 | return false; | |
939 | ||
051ff581 | 940 | idx = ((r->CRm & 3) << 3) | (r->Op2 & 7); |
051ff581 SZ |
941 | } |
942 | ||
a3da9358 MZ |
943 | /* Catch any decoding mistake */ |
944 | WARN_ON(idx == ~0UL); | |
945 | ||
051ff581 SZ |
946 | if (!pmu_counter_idx_valid(vcpu, idx)) |
947 | return false; | |
948 | ||
d692b8ad SZ |
949 | if (p->is_write) { |
950 | if (pmu_access_el0_disabled(vcpu)) | |
951 | return false; | |
952 | ||
051ff581 | 953 | kvm_pmu_set_counter_value(vcpu, idx, p->regval); |
d692b8ad | 954 | } else { |
051ff581 | 955 | p->regval = kvm_pmu_get_counter_value(vcpu, idx); |
d692b8ad | 956 | } |
051ff581 SZ |
957 | |
958 | return true; | |
959 | } | |
960 | ||
9feb21ac SZ |
961 | static bool access_pmu_evtyper(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
962 | const struct sys_reg_desc *r) | |
963 | { | |
964 | u64 idx, reg; | |
965 | ||
d692b8ad SZ |
966 | if (pmu_access_el0_disabled(vcpu)) |
967 | return false; | |
968 | ||
9feb21ac SZ |
969 | if (r->CRn == 9 && r->CRm == 13 && r->Op2 == 1) { |
970 | /* PMXEVTYPER_EL0 */ | |
8d404c4c | 971 | idx = __vcpu_sys_reg(vcpu, PMSELR_EL0) & ARMV8_PMU_COUNTER_MASK; |
9feb21ac SZ |
972 | reg = PMEVTYPER0_EL0 + idx; |
973 | } else if (r->CRn == 14 && (r->CRm & 12) == 12) { | |
974 | idx = ((r->CRm & 3) << 3) | (r->Op2 & 7); | |
975 | if (idx == ARMV8_PMU_CYCLE_IDX) | |
976 | reg = PMCCFILTR_EL0; | |
977 | else | |
978 | /* PMEVTYPERn_EL0 */ | |
979 | reg = PMEVTYPER0_EL0 + idx; | |
980 | } else { | |
981 | BUG(); | |
982 | } | |
983 | ||
984 | if (!pmu_counter_idx_valid(vcpu, idx)) | |
985 | return false; | |
986 | ||
987 | if (p->is_write) { | |
988 | kvm_pmu_set_counter_event_type(vcpu, p->regval, idx); | |
435e53fb | 989 | kvm_vcpu_pmu_restore_guest(vcpu); |
9feb21ac | 990 | } else { |
8d404c4c | 991 | p->regval = __vcpu_sys_reg(vcpu, reg) & ARMV8_PMU_EVTYPE_MASK; |
9feb21ac SZ |
992 | } |
993 | ||
994 | return true; | |
995 | } | |
996 | ||
96b0eebc SZ |
997 | static bool access_pmcnten(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
998 | const struct sys_reg_desc *r) | |
999 | { | |
1000 | u64 val, mask; | |
1001 | ||
d692b8ad SZ |
1002 | if (pmu_access_el0_disabled(vcpu)) |
1003 | return false; | |
1004 | ||
96b0eebc SZ |
1005 | mask = kvm_pmu_valid_counter_mask(vcpu); |
1006 | if (p->is_write) { | |
1007 | val = p->regval & mask; | |
1008 | if (r->Op2 & 0x1) { | |
1009 | /* accessing PMCNTENSET_EL0 */ | |
8d404c4c | 1010 | __vcpu_sys_reg(vcpu, PMCNTENSET_EL0) |= val; |
418e5ca8 | 1011 | kvm_pmu_enable_counter_mask(vcpu, val); |
435e53fb | 1012 | kvm_vcpu_pmu_restore_guest(vcpu); |
96b0eebc SZ |
1013 | } else { |
1014 | /* accessing PMCNTENCLR_EL0 */ | |
8d404c4c | 1015 | __vcpu_sys_reg(vcpu, PMCNTENSET_EL0) &= ~val; |
418e5ca8 | 1016 | kvm_pmu_disable_counter_mask(vcpu, val); |
96b0eebc SZ |
1017 | } |
1018 | } else { | |
f5eff400 | 1019 | p->regval = __vcpu_sys_reg(vcpu, PMCNTENSET_EL0); |
96b0eebc SZ |
1020 | } |
1021 | ||
1022 | return true; | |
1023 | } | |
1024 | ||
9db52c78 SZ |
1025 | static bool access_pminten(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
1026 | const struct sys_reg_desc *r) | |
1027 | { | |
1028 | u64 mask = kvm_pmu_valid_counter_mask(vcpu); | |
1029 | ||
b0737e99 | 1030 | if (check_pmu_access_disabled(vcpu, 0)) |
d692b8ad SZ |
1031 | return false; |
1032 | ||
9db52c78 SZ |
1033 | if (p->is_write) { |
1034 | u64 val = p->regval & mask; | |
1035 | ||
1036 | if (r->Op2 & 0x1) | |
1037 | /* accessing PMINTENSET_EL1 */ | |
8d404c4c | 1038 | __vcpu_sys_reg(vcpu, PMINTENSET_EL1) |= val; |
9db52c78 SZ |
1039 | else |
1040 | /* accessing PMINTENCLR_EL1 */ | |
8d404c4c | 1041 | __vcpu_sys_reg(vcpu, PMINTENSET_EL1) &= ~val; |
9db52c78 | 1042 | } else { |
f5eff400 | 1043 | p->regval = __vcpu_sys_reg(vcpu, PMINTENSET_EL1); |
9db52c78 SZ |
1044 | } |
1045 | ||
1046 | return true; | |
1047 | } | |
1048 | ||
76d883c4 SZ |
1049 | static bool access_pmovs(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
1050 | const struct sys_reg_desc *r) | |
1051 | { | |
1052 | u64 mask = kvm_pmu_valid_counter_mask(vcpu); | |
1053 | ||
d692b8ad SZ |
1054 | if (pmu_access_el0_disabled(vcpu)) |
1055 | return false; | |
1056 | ||
76d883c4 SZ |
1057 | if (p->is_write) { |
1058 | if (r->CRm & 0x2) | |
1059 | /* accessing PMOVSSET_EL0 */ | |
8d404c4c | 1060 | __vcpu_sys_reg(vcpu, PMOVSSET_EL0) |= (p->regval & mask); |
76d883c4 SZ |
1061 | else |
1062 | /* accessing PMOVSCLR_EL0 */ | |
8d404c4c | 1063 | __vcpu_sys_reg(vcpu, PMOVSSET_EL0) &= ~(p->regval & mask); |
76d883c4 | 1064 | } else { |
f5eff400 | 1065 | p->regval = __vcpu_sys_reg(vcpu, PMOVSSET_EL0); |
76d883c4 SZ |
1066 | } |
1067 | ||
1068 | return true; | |
1069 | } | |
1070 | ||
7a0adc70 SZ |
1071 | static bool access_pmswinc(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
1072 | const struct sys_reg_desc *r) | |
1073 | { | |
1074 | u64 mask; | |
1075 | ||
e0443230 | 1076 | if (!p->is_write) |
e7f1d1ee | 1077 | return read_from_write_only(vcpu, p, r); |
e0443230 | 1078 | |
d692b8ad SZ |
1079 | if (pmu_write_swinc_el0_disabled(vcpu)) |
1080 | return false; | |
1081 | ||
e0443230 MZ |
1082 | mask = kvm_pmu_valid_counter_mask(vcpu); |
1083 | kvm_pmu_software_increment(vcpu, p->regval & mask); | |
1084 | return true; | |
7a0adc70 SZ |
1085 | } |
1086 | ||
d692b8ad SZ |
1087 | static bool access_pmuserenr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
1088 | const struct sys_reg_desc *r) | |
1089 | { | |
d692b8ad | 1090 | if (p->is_write) { |
9008c235 MZ |
1091 | if (!vcpu_mode_priv(vcpu)) { |
1092 | kvm_inject_undefined(vcpu); | |
d692b8ad | 1093 | return false; |
9008c235 | 1094 | } |
d692b8ad | 1095 | |
8d404c4c CD |
1096 | __vcpu_sys_reg(vcpu, PMUSERENR_EL0) = |
1097 | p->regval & ARMV8_PMU_USERENR_MASK; | |
d692b8ad | 1098 | } else { |
8d404c4c | 1099 | p->regval = __vcpu_sys_reg(vcpu, PMUSERENR_EL0) |
d692b8ad SZ |
1100 | & ARMV8_PMU_USERENR_MASK; |
1101 | } | |
1102 | ||
1103 | return true; | |
1104 | } | |
1105 | ||
0c557ed4 MZ |
1106 | /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */ |
1107 | #define DBG_BCR_BVR_WCR_WVR_EL1(n) \ | |
ee1b64e6 | 1108 | { SYS_DESC(SYS_DBGBVRn_EL1(n)), \ |
03fdfb26 | 1109 | trap_bvr, reset_bvr, 0, 0, get_bvr, set_bvr }, \ |
ee1b64e6 | 1110 | { SYS_DESC(SYS_DBGBCRn_EL1(n)), \ |
03fdfb26 | 1111 | trap_bcr, reset_bcr, 0, 0, get_bcr, set_bcr }, \ |
ee1b64e6 | 1112 | { SYS_DESC(SYS_DBGWVRn_EL1(n)), \ |
03fdfb26 | 1113 | trap_wvr, reset_wvr, 0, 0, get_wvr, set_wvr }, \ |
ee1b64e6 | 1114 | { SYS_DESC(SYS_DBGWCRn_EL1(n)), \ |
03fdfb26 | 1115 | trap_wcr, reset_wcr, 0, 0, get_wcr, set_wcr } |
0c557ed4 | 1116 | |
9d2a55b4 XC |
1117 | #define PMU_SYS_REG(name) \ |
1118 | SYS_DESC(SYS_##name), .reset = reset_pmu_reg, \ | |
1119 | .visibility = pmu_visibility | |
11663111 | 1120 | |
051ff581 SZ |
1121 | /* Macro to expand the PMEVCNTRn_EL0 register */ |
1122 | #define PMU_PMEVCNTR_EL0(n) \ | |
9d2a55b4 | 1123 | { PMU_SYS_REG(PMEVCNTRn_EL0(n)), \ |
9228b261 | 1124 | .reset = reset_pmevcntr, .get_user = get_pmu_evcntr, \ |
11663111 | 1125 | .access = access_pmu_evcntr, .reg = (PMEVCNTR0_EL0 + n), } |
051ff581 | 1126 | |
9feb21ac SZ |
1127 | /* Macro to expand the PMEVTYPERn_EL0 register */ |
1128 | #define PMU_PMEVTYPER_EL0(n) \ | |
9d2a55b4 | 1129 | { PMU_SYS_REG(PMEVTYPERn_EL0(n)), \ |
0ab410a9 | 1130 | .reset = reset_pmevtyper, \ |
11663111 | 1131 | .access = access_pmu_evtyper, .reg = (PMEVTYPER0_EL0 + n), } |
9feb21ac | 1132 | |
338b1793 MZ |
1133 | static bool undef_access(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
1134 | const struct sys_reg_desc *r) | |
4fcdf106 IV |
1135 | { |
1136 | kvm_inject_undefined(vcpu); | |
1137 | ||
1138 | return false; | |
1139 | } | |
1140 | ||
1141 | /* Macro to expand the AMU counter and type registers*/ | |
338b1793 MZ |
1142 | #define AMU_AMEVCNTR0_EL0(n) { SYS_DESC(SYS_AMEVCNTR0_EL0(n)), undef_access } |
1143 | #define AMU_AMEVTYPER0_EL0(n) { SYS_DESC(SYS_AMEVTYPER0_EL0(n)), undef_access } | |
1144 | #define AMU_AMEVCNTR1_EL0(n) { SYS_DESC(SYS_AMEVCNTR1_EL0(n)), undef_access } | |
1145 | #define AMU_AMEVTYPER1_EL0(n) { SYS_DESC(SYS_AMEVTYPER1_EL0(n)), undef_access } | |
384b40ca MR |
1146 | |
1147 | static unsigned int ptrauth_visibility(const struct kvm_vcpu *vcpu, | |
1148 | const struct sys_reg_desc *rd) | |
1149 | { | |
01fe5ace | 1150 | return vcpu_has_ptrauth(vcpu) ? 0 : REG_HIDDEN; |
384b40ca MR |
1151 | } |
1152 | ||
338b1793 MZ |
1153 | /* |
1154 | * If we land here on a PtrAuth access, that is because we didn't | |
1155 | * fixup the access on exit by allowing the PtrAuth sysregs. The only | |
1156 | * way this happens is when the guest does not have PtrAuth support | |
1157 | * enabled. | |
1158 | */ | |
384b40ca | 1159 | #define __PTRAUTH_KEY(k) \ |
338b1793 | 1160 | { SYS_DESC(SYS_## k), undef_access, reset_unknown, k, \ |
384b40ca MR |
1161 | .visibility = ptrauth_visibility} |
1162 | ||
1163 | #define PTRAUTH_KEY(k) \ | |
1164 | __PTRAUTH_KEY(k ## KEYLO_EL1), \ | |
1165 | __PTRAUTH_KEY(k ## KEYHI_EL1) | |
1166 | ||
84135d3d AP |
1167 | static bool access_arch_timer(struct kvm_vcpu *vcpu, |
1168 | struct sys_reg_params *p, | |
1169 | const struct sys_reg_desc *r) | |
c9a3c58f | 1170 | { |
84135d3d AP |
1171 | enum kvm_arch_timers tmr; |
1172 | enum kvm_arch_timer_regs treg; | |
1173 | u64 reg = reg_to_encoding(r); | |
7b6b4631 | 1174 | |
84135d3d AP |
1175 | switch (reg) { |
1176 | case SYS_CNTP_TVAL_EL0: | |
1177 | case SYS_AARCH32_CNTP_TVAL: | |
1178 | tmr = TIMER_PTIMER; | |
1179 | treg = TIMER_REG_TVAL; | |
1180 | break; | |
1181 | case SYS_CNTP_CTL_EL0: | |
1182 | case SYS_AARCH32_CNTP_CTL: | |
1183 | tmr = TIMER_PTIMER; | |
1184 | treg = TIMER_REG_CTL; | |
1185 | break; | |
1186 | case SYS_CNTP_CVAL_EL0: | |
1187 | case SYS_AARCH32_CNTP_CVAL: | |
1188 | tmr = TIMER_PTIMER; | |
1189 | treg = TIMER_REG_CVAL; | |
1190 | break; | |
c605ee24 MZ |
1191 | case SYS_CNTPCT_EL0: |
1192 | case SYS_CNTPCTSS_EL0: | |
1193 | case SYS_AARCH32_CNTPCT: | |
1194 | tmr = TIMER_PTIMER; | |
1195 | treg = TIMER_REG_CNT; | |
1196 | break; | |
84135d3d | 1197 | default: |
ba82e06c MZ |
1198 | print_sys_reg_msg(p, "%s", "Unhandled trapped timer register"); |
1199 | kvm_inject_undefined(vcpu); | |
1200 | return false; | |
c1b135af | 1201 | } |
7b6b4631 | 1202 | |
7b6b4631 | 1203 | if (p->is_write) |
84135d3d | 1204 | kvm_arm_timer_write_sysreg(vcpu, tmr, treg, p->regval); |
7b6b4631 | 1205 | else |
84135d3d | 1206 | p->regval = kvm_arm_timer_read_sysreg(vcpu, tmr, treg); |
7b6b4631 | 1207 | |
c9a3c58f JL |
1208 | return true; |
1209 | } | |
1210 | ||
2e8bf0cb JZ |
1211 | static s64 kvm_arm64_ftr_safe_value(u32 id, const struct arm64_ftr_bits *ftrp, |
1212 | s64 new, s64 cur) | |
3d0dba57 | 1213 | { |
2e8bf0cb JZ |
1214 | struct arm64_ftr_bits kvm_ftr = *ftrp; |
1215 | ||
1216 | /* Some features have different safe value type in KVM than host features */ | |
1217 | switch (id) { | |
1218 | case SYS_ID_AA64DFR0_EL1: | |
1219 | if (kvm_ftr.shift == ID_AA64DFR0_EL1_PMUVer_SHIFT) | |
1220 | kvm_ftr.type = FTR_LOWER_SAFE; | |
1221 | break; | |
1222 | case SYS_ID_DFR0_EL1: | |
1223 | if (kvm_ftr.shift == ID_DFR0_EL1_PerfMon_SHIFT) | |
1224 | kvm_ftr.type = FTR_LOWER_SAFE; | |
1225 | break; | |
1226 | } | |
3d0dba57 | 1227 | |
2e8bf0cb | 1228 | return arm64_ftr_safe_value(&kvm_ftr, new, cur); |
3d0dba57 MZ |
1229 | } |
1230 | ||
2e8bf0cb JZ |
1231 | /** |
1232 | * arm64_check_features() - Check if a feature register value constitutes | |
1233 | * a subset of features indicated by the idreg's KVM sanitised limit. | |
1234 | * | |
1235 | * This function will check if each feature field of @val is the "safe" value | |
1236 | * against idreg's KVM sanitised limit return from reset() callback. | |
1237 | * If a field value in @val is the same as the one in limit, it is always | |
1238 | * considered the safe value regardless For register fields that are not in | |
1239 | * writable, only the value in limit is considered the safe value. | |
1240 | * | |
1241 | * Return: 0 if all the fields are safe. Otherwise, return negative errno. | |
1242 | */ | |
1243 | static int arm64_check_features(struct kvm_vcpu *vcpu, | |
1244 | const struct sys_reg_desc *rd, | |
1245 | u64 val) | |
d82e0dfd | 1246 | { |
2e8bf0cb JZ |
1247 | const struct arm64_ftr_reg *ftr_reg; |
1248 | const struct arm64_ftr_bits *ftrp = NULL; | |
1249 | u32 id = reg_to_encoding(rd); | |
1250 | u64 writable_mask = rd->val; | |
1251 | u64 limit = rd->reset(vcpu, rd); | |
1252 | u64 mask = 0; | |
1253 | ||
1254 | /* | |
1255 | * Hidden and unallocated ID registers may not have a corresponding | |
1256 | * struct arm64_ftr_reg. Of course, if the register is RAZ we know the | |
1257 | * only safe value is 0. | |
1258 | */ | |
1259 | if (sysreg_visible_as_raz(vcpu, rd)) | |
1260 | return val ? -E2BIG : 0; | |
1261 | ||
1262 | ftr_reg = get_arm64_ftr_reg(id); | |
1263 | if (!ftr_reg) | |
1264 | return -EINVAL; | |
1265 | ||
1266 | ftrp = ftr_reg->ftr_bits; | |
1267 | ||
1268 | for (; ftrp && ftrp->width; ftrp++) { | |
1269 | s64 f_val, f_lim, safe_val; | |
1270 | u64 ftr_mask; | |
1271 | ||
1272 | ftr_mask = arm64_ftr_mask(ftrp); | |
1273 | if ((ftr_mask & writable_mask) != ftr_mask) | |
1274 | continue; | |
1275 | ||
1276 | f_val = arm64_ftr_value(ftrp, val); | |
1277 | f_lim = arm64_ftr_value(ftrp, limit); | |
1278 | mask |= ftr_mask; | |
1279 | ||
1280 | if (f_val == f_lim) | |
1281 | safe_val = f_val; | |
1282 | else | |
1283 | safe_val = kvm_arm64_ftr_safe_value(id, ftrp, f_val, f_lim); | |
1284 | ||
1285 | if (safe_val != f_val) | |
1286 | return -E2BIG; | |
d82e0dfd | 1287 | } |
2e8bf0cb JZ |
1288 | |
1289 | /* For fields that are not writable, values in limit are the safe values. */ | |
1290 | if ((val & ~mask) != (limit & ~mask)) | |
1291 | return -E2BIG; | |
1292 | ||
1293 | return 0; | |
d82e0dfd MZ |
1294 | } |
1295 | ||
3d0dba57 MZ |
1296 | static u8 pmuver_to_perfmon(u8 pmuver) |
1297 | { | |
1298 | switch (pmuver) { | |
1299 | case ID_AA64DFR0_EL1_PMUVer_IMP: | |
753d734f | 1300 | return ID_DFR0_EL1_PerfMon_PMUv3; |
3d0dba57 | 1301 | case ID_AA64DFR0_EL1_PMUVer_IMP_DEF: |
753d734f | 1302 | return ID_DFR0_EL1_PerfMon_IMPDEF; |
3d0dba57 MZ |
1303 | default: |
1304 | /* Anything ARMv8.1+ and NI have the same value. For now. */ | |
1305 | return pmuver; | |
1306 | } | |
1307 | } | |
1308 | ||
93390c0a | 1309 | /* Read a sanitised cpufeature ID register by sys_reg_desc */ |
d86cde6e JZ |
1310 | static u64 __kvm_read_sanitised_id_reg(const struct kvm_vcpu *vcpu, |
1311 | const struct sys_reg_desc *r) | |
93390c0a | 1312 | { |
7ba8b438 | 1313 | u32 id = reg_to_encoding(r); |
00d5101b AE |
1314 | u64 val; |
1315 | ||
cdd5036d | 1316 | if (sysreg_visible_as_raz(vcpu, r)) |
00d5101b AE |
1317 | return 0; |
1318 | ||
1319 | val = read_sanitised_ftr_reg(id); | |
93390c0a | 1320 | |
c8857935 | 1321 | switch (id) { |
c8857935 | 1322 | case SYS_ID_AA64PFR1_EL1: |
16dd1fbb | 1323 | if (!kvm_has_mte(vcpu->kvm)) |
6ca2b9ca | 1324 | val &= ~ARM64_FEATURE_MASK(ID_AA64PFR1_EL1_MTE); |
90807748 | 1325 | |
6ca2b9ca | 1326 | val &= ~ARM64_FEATURE_MASK(ID_AA64PFR1_EL1_SME); |
c8857935 MZ |
1327 | break; |
1328 | case SYS_ID_AA64ISAR1_EL1: | |
1329 | if (!vcpu_has_ptrauth(vcpu)) | |
aa50479b MB |
1330 | val &= ~(ARM64_FEATURE_MASK(ID_AA64ISAR1_EL1_APA) | |
1331 | ARM64_FEATURE_MASK(ID_AA64ISAR1_EL1_API) | | |
1332 | ARM64_FEATURE_MASK(ID_AA64ISAR1_EL1_GPA) | | |
1333 | ARM64_FEATURE_MASK(ID_AA64ISAR1_EL1_GPI)); | |
c8857935 | 1334 | break; |
def8c222 VM |
1335 | case SYS_ID_AA64ISAR2_EL1: |
1336 | if (!vcpu_has_ptrauth(vcpu)) | |
b2d71f27 MB |
1337 | val &= ~(ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_APA3) | |
1338 | ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_GPA3)); | |
06e0b802 | 1339 | if (!cpus_have_final_cap(ARM64_HAS_WFXT)) |
b2d71f27 | 1340 | val &= ~ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_WFxT); |
3172613f | 1341 | val &= ~ARM64_FEATURE_MASK(ID_AA64ISAR2_EL1_MOPS); |
def8c222 | 1342 | break; |
bf48040c AO |
1343 | case SYS_ID_AA64MMFR2_EL1: |
1344 | val &= ~ID_AA64MMFR2_EL1_CCIDX_MASK; | |
1345 | break; | |
1346 | case SYS_ID_MMFR4_EL1: | |
1347 | val &= ~ARM64_FEATURE_MASK(ID_MMFR4_EL1_CCIDX); | |
1348 | break; | |
07d79fe7 DM |
1349 | } |
1350 | ||
1351 | return val; | |
93390c0a DM |
1352 | } |
1353 | ||
d86cde6e JZ |
1354 | static u64 kvm_read_sanitised_id_reg(struct kvm_vcpu *vcpu, |
1355 | const struct sys_reg_desc *r) | |
1356 | { | |
1357 | return __kvm_read_sanitised_id_reg(vcpu, r); | |
1358 | } | |
1359 | ||
1360 | static u64 read_id_reg(const struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) | |
1361 | { | |
6db7af0d | 1362 | return IDREG(vcpu->kvm, reg_to_encoding(r)); |
d86cde6e JZ |
1363 | } |
1364 | ||
47334146 JZ |
1365 | /* |
1366 | * Return true if the register's (Op0, Op1, CRn, CRm, Op2) is | |
1367 | * (3, 0, 0, crm, op2), where 1<=crm<8, 0<=op2<8. | |
1368 | */ | |
1369 | static inline bool is_id_reg(u32 id) | |
1370 | { | |
1371 | return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 && | |
1372 | sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 && | |
1373 | sys_reg_CRm(id) < 8); | |
1374 | } | |
1375 | ||
3f9cd0ca JZ |
1376 | static inline bool is_aa32_id_reg(u32 id) |
1377 | { | |
1378 | return (sys_reg_Op0(id) == 3 && sys_reg_Op1(id) == 0 && | |
1379 | sys_reg_CRn(id) == 0 && sys_reg_CRm(id) >= 1 && | |
1380 | sys_reg_CRm(id) <= 3); | |
1381 | } | |
1382 | ||
912dee57 AJ |
1383 | static unsigned int id_visibility(const struct kvm_vcpu *vcpu, |
1384 | const struct sys_reg_desc *r) | |
1385 | { | |
7ba8b438 | 1386 | u32 id = reg_to_encoding(r); |
c512298e AJ |
1387 | |
1388 | switch (id) { | |
1389 | case SYS_ID_AA64ZFR0_EL1: | |
1390 | if (!vcpu_has_sve(vcpu)) | |
1391 | return REG_RAZ; | |
1392 | break; | |
1393 | } | |
1394 | ||
912dee57 AJ |
1395 | return 0; |
1396 | } | |
1397 | ||
d5efec7e OU |
1398 | static unsigned int aa32_id_visibility(const struct kvm_vcpu *vcpu, |
1399 | const struct sys_reg_desc *r) | |
1400 | { | |
1401 | /* | |
1402 | * AArch32 ID registers are UNKNOWN if AArch32 isn't implemented at any | |
1403 | * EL. Promote to RAZ/WI in order to guarantee consistency between | |
1404 | * systems. | |
1405 | */ | |
1406 | if (!kvm_supports_32bit_el0()) | |
1407 | return REG_RAZ | REG_USER_WI; | |
1408 | ||
1409 | return id_visibility(vcpu, r); | |
1410 | } | |
1411 | ||
34b4d203 OU |
1412 | static unsigned int raz_visibility(const struct kvm_vcpu *vcpu, |
1413 | const struct sys_reg_desc *r) | |
1414 | { | |
1415 | return REG_RAZ; | |
1416 | } | |
1417 | ||
93390c0a DM |
1418 | /* cpufeature ID register access trap handlers */ |
1419 | ||
93390c0a DM |
1420 | static bool access_id_reg(struct kvm_vcpu *vcpu, |
1421 | struct sys_reg_params *p, | |
1422 | const struct sys_reg_desc *r) | |
1423 | { | |
4782ccc8 OU |
1424 | if (p->is_write) |
1425 | return write_to_read_only(vcpu, p, r); | |
1426 | ||
cdd5036d | 1427 | p->regval = read_id_reg(vcpu, r); |
9f75b6d4 MZ |
1428 | if (vcpu_has_nv(vcpu)) |
1429 | access_nested_id_reg(vcpu, p, r); | |
1430 | ||
4782ccc8 | 1431 | return true; |
93390c0a DM |
1432 | } |
1433 | ||
73433762 DM |
1434 | /* Visibility overrides for SVE-specific control registers */ |
1435 | static unsigned int sve_visibility(const struct kvm_vcpu *vcpu, | |
1436 | const struct sys_reg_desc *rd) | |
1437 | { | |
1438 | if (vcpu_has_sve(vcpu)) | |
1439 | return 0; | |
1440 | ||
01fe5ace | 1441 | return REG_HIDDEN; |
73433762 DM |
1442 | } |
1443 | ||
c39f5974 JZ |
1444 | static u64 read_sanitised_id_aa64pfr0_el1(struct kvm_vcpu *vcpu, |
1445 | const struct sys_reg_desc *rd) | |
23711a5e | 1446 | { |
c39f5974 JZ |
1447 | u64 val = read_sanitised_ftr_reg(SYS_ID_AA64PFR0_EL1); |
1448 | ||
1449 | if (!vcpu_has_sve(vcpu)) | |
1450 | val &= ~ID_AA64PFR0_EL1_SVE_MASK; | |
23711a5e MZ |
1451 | |
1452 | /* | |
c39f5974 JZ |
1453 | * The default is to expose CSV2 == 1 if the HW isn't affected. |
1454 | * Although this is a per-CPU feature, we make it global because | |
1455 | * asymmetric systems are just a nuisance. | |
1456 | * | |
1457 | * Userspace can override this as long as it doesn't promise | |
1458 | * the impossible. | |
23711a5e | 1459 | */ |
c39f5974 JZ |
1460 | if (arm64_get_spectre_v2_state() == SPECTRE_UNAFFECTED) { |
1461 | val &= ~ID_AA64PFR0_EL1_CSV2_MASK; | |
1462 | val |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, CSV2, IMP); | |
1463 | } | |
1464 | if (arm64_get_meltdown_state() == SPECTRE_UNAFFECTED) { | |
1465 | val &= ~ID_AA64PFR0_EL1_CSV3_MASK; | |
1466 | val |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, CSV3, IMP); | |
1467 | } | |
23711a5e | 1468 | |
c39f5974 JZ |
1469 | if (kvm_vgic_global_state.type == VGIC_V3) { |
1470 | val &= ~ID_AA64PFR0_EL1_GIC_MASK; | |
1471 | val |= SYS_FIELD_PREP_ENUM(ID_AA64PFR0_EL1, GIC, IMP); | |
1472 | } | |
4f1df628 | 1473 | |
c39f5974 | 1474 | val &= ~ID_AA64PFR0_EL1_AMU_MASK; |
23711a5e | 1475 | |
c39f5974 JZ |
1476 | return val; |
1477 | } | |
23711a5e | 1478 | |
c118cead JZ |
1479 | static u64 read_sanitised_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, |
1480 | const struct sys_reg_desc *rd) | |
1481 | { | |
1482 | u64 val = read_sanitised_ftr_reg(SYS_ID_AA64DFR0_EL1); | |
1483 | ||
1484 | /* Limit debug to ARMv8.0 */ | |
1485 | val &= ~ID_AA64DFR0_EL1_DebugVer_MASK; | |
1486 | val |= SYS_FIELD_PREP_ENUM(ID_AA64DFR0_EL1, DebugVer, IMP); | |
1487 | ||
1488 | /* | |
1489 | * Only initialize the PMU version if the vCPU was configured with one. | |
1490 | */ | |
1491 | val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; | |
1492 | if (kvm_vcpu_has_pmu(vcpu)) | |
1493 | val |= SYS_FIELD_PREP(ID_AA64DFR0_EL1, PMUVer, | |
1494 | kvm_arm_pmu_get_pmuver_limit()); | |
1495 | ||
1496 | /* Hide SPE from guests */ | |
1497 | val &= ~ID_AA64DFR0_EL1_PMSVer_MASK; | |
1498 | ||
1499 | return val; | |
23711a5e MZ |
1500 | } |
1501 | ||
60e651ff MZ |
1502 | static int set_id_aa64dfr0_el1(struct kvm_vcpu *vcpu, |
1503 | const struct sys_reg_desc *rd, | |
1504 | u64 val) | |
1505 | { | |
c118cead | 1506 | u8 pmuver = SYS_FIELD_GET(ID_AA64DFR0_EL1, PMUVer, val); |
60e651ff MZ |
1507 | |
1508 | /* | |
f90f9360 OU |
1509 | * Prior to commit 3d0dba5764b9 ("KVM: arm64: PMU: Move the |
1510 | * ID_AA64DFR0_EL1.PMUver limit to VM creation"), KVM erroneously | |
1511 | * exposed an IMP_DEF PMU to userspace and the guest on systems w/ | |
1512 | * non-architectural PMUs. Of course, PMUv3 is the only game in town for | |
1513 | * PMU virtualization, so the IMP_DEF value was rather user-hostile. | |
1514 | * | |
1515 | * At minimum, we're on the hook to allow values that were given to | |
1516 | * userspace by KVM. Cover our tracks here and replace the IMP_DEF value | |
1517 | * with a more sensible NI. The value of an ID register changing under | |
1518 | * the nose of the guest is unfortunate, but is certainly no more | |
1519 | * surprising than an ill-guided PMU driver poking at impdef system | |
1520 | * registers that end in an UNDEF... | |
60e651ff | 1521 | */ |
68667240 | 1522 | if (pmuver == ID_AA64DFR0_EL1_PMUVer_IMP_DEF) |
f90f9360 | 1523 | val &= ~ID_AA64DFR0_EL1_PMUVer_MASK; |
60e651ff | 1524 | |
68667240 | 1525 | return set_id_reg(vcpu, rd, val); |
c118cead | 1526 | } |
60e651ff | 1527 | |
c118cead JZ |
1528 | static u64 read_sanitised_id_dfr0_el1(struct kvm_vcpu *vcpu, |
1529 | const struct sys_reg_desc *rd) | |
1530 | { | |
1531 | u8 perfmon = pmuver_to_perfmon(kvm_arm_pmu_get_pmuver_limit()); | |
1532 | u64 val = read_sanitised_ftr_reg(SYS_ID_DFR0_EL1); | |
60e651ff | 1533 | |
c118cead JZ |
1534 | val &= ~ID_DFR0_EL1_PerfMon_MASK; |
1535 | if (kvm_vcpu_has_pmu(vcpu)) | |
1536 | val |= SYS_FIELD_PREP(ID_DFR0_EL1, PerfMon, perfmon); | |
60e651ff | 1537 | |
c118cead | 1538 | return val; |
60e651ff MZ |
1539 | } |
1540 | ||
d82e0dfd MZ |
1541 | static int set_id_dfr0_el1(struct kvm_vcpu *vcpu, |
1542 | const struct sys_reg_desc *rd, | |
1543 | u64 val) | |
1544 | { | |
c118cead | 1545 | u8 perfmon = SYS_FIELD_GET(ID_DFR0_EL1, PerfMon, val); |
d82e0dfd | 1546 | |
f90f9360 OU |
1547 | if (perfmon == ID_DFR0_EL1_PerfMon_IMPDEF) { |
1548 | val &= ~ID_DFR0_EL1_PerfMon_MASK; | |
1549 | perfmon = 0; | |
1550 | } | |
d82e0dfd MZ |
1551 | |
1552 | /* | |
1553 | * Allow DFR0_EL1.PerfMon to be set from userspace as long as | |
1554 | * it doesn't promise more than what the HW gives us on the | |
1555 | * AArch64 side (as everything is emulated with that), and | |
1556 | * that this is a PMUv3. | |
1557 | */ | |
c118cead | 1558 | if (perfmon != 0 && perfmon < ID_DFR0_EL1_PerfMon_PMUv3) |
d82e0dfd MZ |
1559 | return -EINVAL; |
1560 | ||
68667240 | 1561 | return set_id_reg(vcpu, rd, val); |
d82e0dfd MZ |
1562 | } |
1563 | ||
93390c0a DM |
1564 | /* |
1565 | * cpufeature ID register user accessors | |
1566 | * | |
1567 | * For now, these registers are immutable for userspace, so no values | |
1568 | * are stored, and for set_id_reg() we don't allow the effective value | |
1569 | * to be changed. | |
1570 | */ | |
93390c0a | 1571 | static int get_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, |
978ceeb3 | 1572 | u64 *val) |
93390c0a | 1573 | { |
6db7af0d OU |
1574 | /* |
1575 | * Avoid locking if the VM has already started, as the ID registers are | |
1576 | * guaranteed to be invariant at that point. | |
1577 | */ | |
1578 | if (kvm_vm_has_ran_once(vcpu->kvm)) { | |
1579 | *val = read_id_reg(vcpu, rd); | |
1580 | return 0; | |
1581 | } | |
1582 | ||
1583 | mutex_lock(&vcpu->kvm->arch.config_lock); | |
cdd5036d | 1584 | *val = read_id_reg(vcpu, rd); |
6db7af0d OU |
1585 | mutex_unlock(&vcpu->kvm->arch.config_lock); |
1586 | ||
4782ccc8 | 1587 | return 0; |
93390c0a DM |
1588 | } |
1589 | ||
1590 | static int set_id_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
978ceeb3 | 1591 | u64 val) |
93390c0a | 1592 | { |
2e8bf0cb JZ |
1593 | u32 id = reg_to_encoding(rd); |
1594 | int ret; | |
4782ccc8 | 1595 | |
2e8bf0cb JZ |
1596 | mutex_lock(&vcpu->kvm->arch.config_lock); |
1597 | ||
1598 | /* | |
1599 | * Once the VM has started the ID registers are immutable. Reject any | |
1600 | * write that does not match the final register value. | |
1601 | */ | |
1602 | if (kvm_vm_has_ran_once(vcpu->kvm)) { | |
1603 | if (val != read_id_reg(vcpu, rd)) | |
1604 | ret = -EBUSY; | |
1605 | else | |
1606 | ret = 0; | |
1607 | ||
1608 | mutex_unlock(&vcpu->kvm->arch.config_lock); | |
1609 | return ret; | |
1610 | } | |
1611 | ||
1612 | ret = arm64_check_features(vcpu, rd, val); | |
1613 | if (!ret) | |
1614 | IDREG(vcpu->kvm, id) = val; | |
1615 | ||
1616 | mutex_unlock(&vcpu->kvm->arch.config_lock); | |
1617 | ||
1618 | /* | |
1619 | * arm64_check_features() returns -E2BIG to indicate the register's | |
1620 | * feature set is a superset of the maximally-allowed register value. | |
1621 | * While it would be nice to precisely describe this to userspace, the | |
1622 | * existing UAPI for KVM_SET_ONE_REG has it that invalid register | |
1623 | * writes return -EINVAL. | |
1624 | */ | |
1625 | if (ret == -E2BIG) | |
1626 | ret = -EINVAL; | |
1627 | return ret; | |
93390c0a DM |
1628 | } |
1629 | ||
5a430976 | 1630 | static int get_raz_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, |
978ceeb3 | 1631 | u64 *val) |
5a430976 | 1632 | { |
978ceeb3 MZ |
1633 | *val = 0; |
1634 | return 0; | |
5a430976 AE |
1635 | } |
1636 | ||
7a3ba309 | 1637 | static int set_wi_reg(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, |
978ceeb3 | 1638 | u64 val) |
7a3ba309 | 1639 | { |
7a3ba309 MZ |
1640 | return 0; |
1641 | } | |
1642 | ||
f7f2b15c AB |
1643 | static bool access_ctr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
1644 | const struct sys_reg_desc *r) | |
1645 | { | |
1646 | if (p->is_write) | |
1647 | return write_to_read_only(vcpu, p, r); | |
1648 | ||
1649 | p->regval = read_sanitised_ftr_reg(SYS_CTR_EL0); | |
1650 | return true; | |
1651 | } | |
1652 | ||
1653 | static bool access_clidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, | |
1654 | const struct sys_reg_desc *r) | |
1655 | { | |
1656 | if (p->is_write) | |
1657 | return write_to_read_only(vcpu, p, r); | |
1658 | ||
7af0c253 | 1659 | p->regval = __vcpu_sys_reg(vcpu, r->reg); |
f7f2b15c AB |
1660 | return true; |
1661 | } | |
1662 | ||
7af0c253 AO |
1663 | /* |
1664 | * Fabricate a CLIDR_EL1 value instead of using the real value, which can vary | |
1665 | * by the physical CPU which the vcpu currently resides in. | |
1666 | */ | |
d86cde6e | 1667 | static u64 reset_clidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r) |
7af0c253 AO |
1668 | { |
1669 | u64 ctr_el0 = read_sanitised_ftr_reg(SYS_CTR_EL0); | |
1670 | u64 clidr; | |
1671 | u8 loc; | |
1672 | ||
1673 | if ((ctr_el0 & CTR_EL0_IDC)) { | |
1674 | /* | |
1675 | * Data cache clean to the PoU is not required so LoUU and LoUIS | |
1676 | * will not be set and a unified cache, which will be marked as | |
1677 | * LoC, will be added. | |
1678 | * | |
1679 | * If not DIC, let the unified cache L2 so that an instruction | |
1680 | * cache can be added as L1 later. | |
1681 | */ | |
1682 | loc = (ctr_el0 & CTR_EL0_DIC) ? 1 : 2; | |
1683 | clidr = CACHE_TYPE_UNIFIED << CLIDR_CTYPE_SHIFT(loc); | |
1684 | } else { | |
1685 | /* | |
1686 | * Data cache clean to the PoU is required so let L1 have a data | |
1687 | * cache and mark it as LoUU and LoUIS. As L1 has a data cache, | |
1688 | * it can be marked as LoC too. | |
1689 | */ | |
1690 | loc = 1; | |
1691 | clidr = 1 << CLIDR_LOUU_SHIFT; | |
1692 | clidr |= 1 << CLIDR_LOUIS_SHIFT; | |
1693 | clidr |= CACHE_TYPE_DATA << CLIDR_CTYPE_SHIFT(1); | |
1694 | } | |
1695 | ||
1696 | /* | |
1697 | * Instruction cache invalidation to the PoU is required so let L1 have | |
1698 | * an instruction cache. If L1 already has a data cache, it will be | |
1699 | * CACHE_TYPE_SEPARATE. | |
1700 | */ | |
1701 | if (!(ctr_el0 & CTR_EL0_DIC)) | |
1702 | clidr |= CACHE_TYPE_INST << CLIDR_CTYPE_SHIFT(1); | |
1703 | ||
1704 | clidr |= loc << CLIDR_LOC_SHIFT; | |
1705 | ||
1706 | /* | |
1707 | * Add tag cache unified to data cache. Allocation tags and data are | |
1708 | * unified in a cache line so that it looks valid even if there is only | |
1709 | * one cache line. | |
1710 | */ | |
1711 | if (kvm_has_mte(vcpu->kvm)) | |
1712 | clidr |= 2 << CLIDR_TTYPE_SHIFT(loc); | |
1713 | ||
1714 | __vcpu_sys_reg(vcpu, r->reg) = clidr; | |
d86cde6e JZ |
1715 | |
1716 | return __vcpu_sys_reg(vcpu, r->reg); | |
7af0c253 AO |
1717 | } |
1718 | ||
1719 | static int set_clidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd, | |
1720 | u64 val) | |
1721 | { | |
1722 | u64 ctr_el0 = read_sanitised_ftr_reg(SYS_CTR_EL0); | |
1723 | u64 idc = !CLIDR_LOC(val) || (!CLIDR_LOUIS(val) && !CLIDR_LOUU(val)); | |
1724 | ||
1725 | if ((val & CLIDR_EL1_RES0) || (!(ctr_el0 & CTR_EL0_IDC) && idc)) | |
1726 | return -EINVAL; | |
1727 | ||
1728 | __vcpu_sys_reg(vcpu, rd->reg) = val; | |
1729 | ||
1730 | return 0; | |
1731 | } | |
1732 | ||
f7f2b15c AB |
1733 | static bool access_csselr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, |
1734 | const struct sys_reg_desc *r) | |
1735 | { | |
7c582bf4 JM |
1736 | int reg = r->reg; |
1737 | ||
f7f2b15c | 1738 | if (p->is_write) |
7c582bf4 | 1739 | vcpu_write_sys_reg(vcpu, p->regval, reg); |
f7f2b15c | 1740 | else |
7c582bf4 | 1741 | p->regval = vcpu_read_sys_reg(vcpu, reg); |
f7f2b15c AB |
1742 | return true; |
1743 | } | |
1744 | ||
1745 | static bool access_ccsidr(struct kvm_vcpu *vcpu, struct sys_reg_params *p, | |
1746 | const struct sys_reg_desc *r) | |
1747 | { | |
1748 | u32 csselr; | |
1749 | ||
1750 | if (p->is_write) | |
1751 | return write_to_read_only(vcpu, p, r); | |
1752 | ||
1753 | csselr = vcpu_read_sys_reg(vcpu, CSSELR_EL1); | |
7af0c253 AO |
1754 | csselr &= CSSELR_EL1_Level | CSSELR_EL1_InD; |
1755 | if (csselr < CSSELR_MAX) | |
1756 | p->regval = get_ccsidr(vcpu, csselr); | |
793acf87 | 1757 | |
f7f2b15c AB |
1758 | return true; |
1759 | } | |
1760 | ||
e1f358b5 SP |
1761 | static unsigned int mte_visibility(const struct kvm_vcpu *vcpu, |
1762 | const struct sys_reg_desc *rd) | |
1763 | { | |
673638f4 SP |
1764 | if (kvm_has_mte(vcpu->kvm)) |
1765 | return 0; | |
1766 | ||
e1f358b5 SP |
1767 | return REG_HIDDEN; |
1768 | } | |
1769 | ||
1770 | #define MTE_REG(name) { \ | |
1771 | SYS_DESC(SYS_##name), \ | |
1772 | .access = undef_access, \ | |
1773 | .reset = reset_unknown, \ | |
1774 | .reg = name, \ | |
1775 | .visibility = mte_visibility, \ | |
1776 | } | |
1777 | ||
6ff9dc23 JL |
1778 | static unsigned int el2_visibility(const struct kvm_vcpu *vcpu, |
1779 | const struct sys_reg_desc *rd) | |
1780 | { | |
1781 | if (vcpu_has_nv(vcpu)) | |
1782 | return 0; | |
1783 | ||
1784 | return REG_HIDDEN; | |
1785 | } | |
1786 | ||
1787 | #define EL2_REG(name, acc, rst, v) { \ | |
1788 | SYS_DESC(SYS_##name), \ | |
1789 | .access = acc, \ | |
1790 | .reset = rst, \ | |
1791 | .reg = name, \ | |
1792 | .visibility = el2_visibility, \ | |
1793 | .val = v, \ | |
1794 | } | |
1795 | ||
280b748e JL |
1796 | /* |
1797 | * EL{0,1}2 registers are the EL2 view on an EL0 or EL1 register when | |
1798 | * HCR_EL2.E2H==1, and only in the sysreg table for convenience of | |
1799 | * handling traps. Given that, they are always hidden from userspace. | |
1800 | */ | |
1801 | static unsigned int elx2_visibility(const struct kvm_vcpu *vcpu, | |
1802 | const struct sys_reg_desc *rd) | |
1803 | { | |
1804 | return REG_HIDDEN_USER; | |
1805 | } | |
1806 | ||
1807 | #define EL12_REG(name, acc, rst, v) { \ | |
1808 | SYS_DESC(SYS_##name##_EL12), \ | |
1809 | .access = acc, \ | |
1810 | .reset = rst, \ | |
1811 | .reg = name##_EL1, \ | |
1812 | .val = v, \ | |
1813 | .visibility = elx2_visibility, \ | |
1814 | } | |
1815 | ||
d86cde6e JZ |
1816 | /* |
1817 | * Since reset() callback and field val are not used for idregs, they will be | |
1818 | * used for specific purposes for idregs. | |
1819 | * The reset() would return KVM sanitised register value. The value would be the | |
1820 | * same as the host kernel sanitised value if there is no KVM sanitisation. | |
1821 | * The val would be used as a mask indicating writable fields for the idreg. | |
1822 | * Only bits with 1 are writable from userspace. This mask might not be | |
1823 | * necessary in the future whenever all ID registers are enabled as writable | |
1824 | * from userspace. | |
1825 | */ | |
1826 | ||
93390c0a DM |
1827 | /* sys_reg_desc initialiser for known cpufeature ID registers */ |
1828 | #define ID_SANITISED(name) { \ | |
1829 | SYS_DESC(SYS_##name), \ | |
1830 | .access = access_id_reg, \ | |
1831 | .get_user = get_id_reg, \ | |
1832 | .set_user = set_id_reg, \ | |
912dee57 | 1833 | .visibility = id_visibility, \ |
d86cde6e JZ |
1834 | .reset = kvm_read_sanitised_id_reg, \ |
1835 | .val = 0, \ | |
93390c0a DM |
1836 | } |
1837 | ||
d5efec7e OU |
1838 | /* sys_reg_desc initialiser for known cpufeature ID registers */ |
1839 | #define AA32_ID_SANITISED(name) { \ | |
1840 | SYS_DESC(SYS_##name), \ | |
1841 | .access = access_id_reg, \ | |
1842 | .get_user = get_id_reg, \ | |
1843 | .set_user = set_id_reg, \ | |
1844 | .visibility = aa32_id_visibility, \ | |
d86cde6e JZ |
1845 | .reset = kvm_read_sanitised_id_reg, \ |
1846 | .val = 0, \ | |
d5efec7e OU |
1847 | } |
1848 | ||
93390c0a DM |
1849 | /* |
1850 | * sys_reg_desc initialiser for architecturally unallocated cpufeature ID | |
1851 | * register with encoding Op0=3, Op1=0, CRn=0, CRm=crm, Op2=op2 | |
1852 | * (1 <= crm < 8, 0 <= Op2 < 8). | |
1853 | */ | |
1854 | #define ID_UNALLOCATED(crm, op2) { \ | |
1855 | Op0(3), Op1(0), CRn(0), CRm(crm), Op2(op2), \ | |
34b4d203 OU |
1856 | .access = access_id_reg, \ |
1857 | .get_user = get_id_reg, \ | |
1858 | .set_user = set_id_reg, \ | |
d86cde6e JZ |
1859 | .visibility = raz_visibility, \ |
1860 | .reset = kvm_read_sanitised_id_reg, \ | |
1861 | .val = 0, \ | |
93390c0a DM |
1862 | } |
1863 | ||
1864 | /* | |
1865 | * sys_reg_desc initialiser for known ID registers that we hide from guests. | |
1866 | * For now, these are exposed just like unallocated ID regs: they appear | |
1867 | * RAZ for the guest. | |
1868 | */ | |
1869 | #define ID_HIDDEN(name) { \ | |
1870 | SYS_DESC(SYS_##name), \ | |
34b4d203 OU |
1871 | .access = access_id_reg, \ |
1872 | .get_user = get_id_reg, \ | |
1873 | .set_user = set_id_reg, \ | |
1874 | .visibility = raz_visibility, \ | |
d86cde6e JZ |
1875 | .reset = kvm_read_sanitised_id_reg, \ |
1876 | .val = 0, \ | |
93390c0a DM |
1877 | } |
1878 | ||
6ff9dc23 JL |
1879 | static bool access_sp_el1(struct kvm_vcpu *vcpu, |
1880 | struct sys_reg_params *p, | |
1881 | const struct sys_reg_desc *r) | |
1882 | { | |
1883 | if (p->is_write) | |
1884 | __vcpu_sys_reg(vcpu, SP_EL1) = p->regval; | |
1885 | else | |
1886 | p->regval = __vcpu_sys_reg(vcpu, SP_EL1); | |
1887 | ||
1888 | return true; | |
1889 | } | |
1890 | ||
9da117ee JL |
1891 | static bool access_elr(struct kvm_vcpu *vcpu, |
1892 | struct sys_reg_params *p, | |
1893 | const struct sys_reg_desc *r) | |
1894 | { | |
1895 | if (p->is_write) | |
1896 | vcpu_write_sys_reg(vcpu, p->regval, ELR_EL1); | |
1897 | else | |
1898 | p->regval = vcpu_read_sys_reg(vcpu, ELR_EL1); | |
1899 | ||
1900 | return true; | |
1901 | } | |
1902 | ||
1903 | static bool access_spsr(struct kvm_vcpu *vcpu, | |
1904 | struct sys_reg_params *p, | |
1905 | const struct sys_reg_desc *r) | |
1906 | { | |
1907 | if (p->is_write) | |
1908 | __vcpu_sys_reg(vcpu, SPSR_EL1) = p->regval; | |
1909 | else | |
1910 | p->regval = __vcpu_sys_reg(vcpu, SPSR_EL1); | |
1911 | ||
1912 | return true; | |
1913 | } | |
1914 | ||
7c8c5e6a MZ |
1915 | /* |
1916 | * Architected system registers. | |
1917 | * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2 | |
7609c125 | 1918 | * |
0c557ed4 MZ |
1919 | * Debug handling: We do trap most, if not all debug related system |
1920 | * registers. The implementation is good enough to ensure that a guest | |
1921 | * can use these with minimal performance degradation. The drawback is | |
7dabf02f OU |
1922 | * that we don't implement any of the external debug architecture. |
1923 | * This should be revisited if we ever encounter a more demanding | |
1924 | * guest... | |
7c8c5e6a MZ |
1925 | */ |
1926 | static const struct sys_reg_desc sys_reg_descs[] = { | |
7606e078 | 1927 | { SYS_DESC(SYS_DC_ISW), access_dcsw }, |
d282fa3c MZ |
1928 | { SYS_DESC(SYS_DC_IGSW), access_dcgsw }, |
1929 | { SYS_DESC(SYS_DC_IGDSW), access_dcgsw }, | |
7606e078 | 1930 | { SYS_DESC(SYS_DC_CSW), access_dcsw }, |
d282fa3c MZ |
1931 | { SYS_DESC(SYS_DC_CGSW), access_dcgsw }, |
1932 | { SYS_DESC(SYS_DC_CGDSW), access_dcgsw }, | |
7606e078 | 1933 | { SYS_DESC(SYS_DC_CISW), access_dcsw }, |
d282fa3c MZ |
1934 | { SYS_DESC(SYS_DC_CIGSW), access_dcgsw }, |
1935 | { SYS_DESC(SYS_DC_CIGDSW), access_dcgsw }, | |
7c8c5e6a | 1936 | |
0c557ed4 MZ |
1937 | DBG_BCR_BVR_WCR_WVR_EL1(0), |
1938 | DBG_BCR_BVR_WCR_WVR_EL1(1), | |
ee1b64e6 MR |
1939 | { SYS_DESC(SYS_MDCCINT_EL1), trap_debug_regs, reset_val, MDCCINT_EL1, 0 }, |
1940 | { SYS_DESC(SYS_MDSCR_EL1), trap_debug_regs, reset_val, MDSCR_EL1, 0 }, | |
0c557ed4 MZ |
1941 | DBG_BCR_BVR_WCR_WVR_EL1(2), |
1942 | DBG_BCR_BVR_WCR_WVR_EL1(3), | |
1943 | DBG_BCR_BVR_WCR_WVR_EL1(4), | |
1944 | DBG_BCR_BVR_WCR_WVR_EL1(5), | |
1945 | DBG_BCR_BVR_WCR_WVR_EL1(6), | |
1946 | DBG_BCR_BVR_WCR_WVR_EL1(7), | |
1947 | DBG_BCR_BVR_WCR_WVR_EL1(8), | |
1948 | DBG_BCR_BVR_WCR_WVR_EL1(9), | |
1949 | DBG_BCR_BVR_WCR_WVR_EL1(10), | |
1950 | DBG_BCR_BVR_WCR_WVR_EL1(11), | |
1951 | DBG_BCR_BVR_WCR_WVR_EL1(12), | |
1952 | DBG_BCR_BVR_WCR_WVR_EL1(13), | |
1953 | DBG_BCR_BVR_WCR_WVR_EL1(14), | |
1954 | DBG_BCR_BVR_WCR_WVR_EL1(15), | |
1955 | ||
ee1b64e6 | 1956 | { SYS_DESC(SYS_MDRAR_EL1), trap_raz_wi }, |
f24adc65 | 1957 | { SYS_DESC(SYS_OSLAR_EL1), trap_oslar_el1 }, |
d42e2671 | 1958 | { SYS_DESC(SYS_OSLSR_EL1), trap_oslsr_el1, reset_val, OSLSR_EL1, |
187de7c2 | 1959 | OSLSR_EL1_OSLM_IMPLEMENTED, .set_user = set_oslsr_el1, }, |
ee1b64e6 MR |
1960 | { SYS_DESC(SYS_OSDLR_EL1), trap_raz_wi }, |
1961 | { SYS_DESC(SYS_DBGPRCR_EL1), trap_raz_wi }, | |
1962 | { SYS_DESC(SYS_DBGCLAIMSET_EL1), trap_raz_wi }, | |
1963 | { SYS_DESC(SYS_DBGCLAIMCLR_EL1), trap_raz_wi }, | |
1964 | { SYS_DESC(SYS_DBGAUTHSTATUS_EL1), trap_dbgauthstatus_el1 }, | |
1965 | ||
1966 | { SYS_DESC(SYS_MDCCSR_EL0), trap_raz_wi }, | |
1967 | { SYS_DESC(SYS_DBGDTR_EL0), trap_raz_wi }, | |
1968 | // DBGDTR[TR]X_EL0 share the same encoding | |
1969 | { SYS_DESC(SYS_DBGDTRTX_EL0), trap_raz_wi }, | |
1970 | ||
1971 | { SYS_DESC(SYS_DBGVCR32_EL2), NULL, reset_val, DBGVCR32_EL2, 0 }, | |
62a89c44 | 1972 | |
851050a5 | 1973 | { SYS_DESC(SYS_MPIDR_EL1), NULL, reset_mpidr, MPIDR_EL1 }, |
93390c0a DM |
1974 | |
1975 | /* | |
1976 | * ID regs: all ID_SANITISED() entries here must have corresponding | |
1977 | * entries in arm64_ftr_regs[]. | |
1978 | */ | |
1979 | ||
1980 | /* AArch64 mappings of the AArch32 ID registers */ | |
1981 | /* CRm=1 */ | |
d5efec7e OU |
1982 | AA32_ID_SANITISED(ID_PFR0_EL1), |
1983 | AA32_ID_SANITISED(ID_PFR1_EL1), | |
c118cead JZ |
1984 | { SYS_DESC(SYS_ID_DFR0_EL1), |
1985 | .access = access_id_reg, | |
1986 | .get_user = get_id_reg, | |
1987 | .set_user = set_id_dfr0_el1, | |
1988 | .visibility = aa32_id_visibility, | |
1989 | .reset = read_sanitised_id_dfr0_el1, | |
1990 | .val = ID_DFR0_EL1_PerfMon_MASK, }, | |
93390c0a | 1991 | ID_HIDDEN(ID_AFR0_EL1), |
d5efec7e OU |
1992 | AA32_ID_SANITISED(ID_MMFR0_EL1), |
1993 | AA32_ID_SANITISED(ID_MMFR1_EL1), | |
1994 | AA32_ID_SANITISED(ID_MMFR2_EL1), | |
1995 | AA32_ID_SANITISED(ID_MMFR3_EL1), | |
93390c0a DM |
1996 | |
1997 | /* CRm=2 */ | |
d5efec7e OU |
1998 | AA32_ID_SANITISED(ID_ISAR0_EL1), |
1999 | AA32_ID_SANITISED(ID_ISAR1_EL1), | |
2000 | AA32_ID_SANITISED(ID_ISAR2_EL1), | |
2001 | AA32_ID_SANITISED(ID_ISAR3_EL1), | |
2002 | AA32_ID_SANITISED(ID_ISAR4_EL1), | |
2003 | AA32_ID_SANITISED(ID_ISAR5_EL1), | |
2004 | AA32_ID_SANITISED(ID_MMFR4_EL1), | |
2005 | AA32_ID_SANITISED(ID_ISAR6_EL1), | |
93390c0a DM |
2006 | |
2007 | /* CRm=3 */ | |
d5efec7e OU |
2008 | AA32_ID_SANITISED(MVFR0_EL1), |
2009 | AA32_ID_SANITISED(MVFR1_EL1), | |
2010 | AA32_ID_SANITISED(MVFR2_EL1), | |
93390c0a | 2011 | ID_UNALLOCATED(3,3), |
d5efec7e | 2012 | AA32_ID_SANITISED(ID_PFR2_EL1), |
dd35ec07 | 2013 | ID_HIDDEN(ID_DFR1_EL1), |
d5efec7e | 2014 | AA32_ID_SANITISED(ID_MMFR5_EL1), |
93390c0a DM |
2015 | ID_UNALLOCATED(3,7), |
2016 | ||
2017 | /* AArch64 ID registers */ | |
2018 | /* CRm=4 */ | |
c39f5974 JZ |
2019 | { SYS_DESC(SYS_ID_AA64PFR0_EL1), |
2020 | .access = access_id_reg, | |
2021 | .get_user = get_id_reg, | |
68667240 | 2022 | .set_user = set_id_reg, |
c39f5974 JZ |
2023 | .reset = read_sanitised_id_aa64pfr0_el1, |
2024 | .val = ID_AA64PFR0_EL1_CSV2_MASK | ID_AA64PFR0_EL1_CSV3_MASK, }, | |
93390c0a DM |
2025 | ID_SANITISED(ID_AA64PFR1_EL1), |
2026 | ID_UNALLOCATED(4,2), | |
2027 | ID_UNALLOCATED(4,3), | |
c512298e | 2028 | ID_SANITISED(ID_AA64ZFR0_EL1), |
90807748 | 2029 | ID_HIDDEN(ID_AA64SMFR0_EL1), |
93390c0a DM |
2030 | ID_UNALLOCATED(4,6), |
2031 | ID_UNALLOCATED(4,7), | |
2032 | ||
2033 | /* CRm=5 */ | |
c118cead JZ |
2034 | { SYS_DESC(SYS_ID_AA64DFR0_EL1), |
2035 | .access = access_id_reg, | |
2036 | .get_user = get_id_reg, | |
2037 | .set_user = set_id_aa64dfr0_el1, | |
2038 | .reset = read_sanitised_id_aa64dfr0_el1, | |
2039 | .val = ID_AA64DFR0_EL1_PMUVer_MASK, }, | |
93390c0a DM |
2040 | ID_SANITISED(ID_AA64DFR1_EL1), |
2041 | ID_UNALLOCATED(5,2), | |
2042 | ID_UNALLOCATED(5,3), | |
2043 | ID_HIDDEN(ID_AA64AFR0_EL1), | |
2044 | ID_HIDDEN(ID_AA64AFR1_EL1), | |
2045 | ID_UNALLOCATED(5,6), | |
2046 | ID_UNALLOCATED(5,7), | |
2047 | ||
2048 | /* CRm=6 */ | |
2049 | ID_SANITISED(ID_AA64ISAR0_EL1), | |
2050 | ID_SANITISED(ID_AA64ISAR1_EL1), | |
9e45365f | 2051 | ID_SANITISED(ID_AA64ISAR2_EL1), |
93390c0a DM |
2052 | ID_UNALLOCATED(6,3), |
2053 | ID_UNALLOCATED(6,4), | |
2054 | ID_UNALLOCATED(6,5), | |
2055 | ID_UNALLOCATED(6,6), | |
2056 | ID_UNALLOCATED(6,7), | |
2057 | ||
2058 | /* CRm=7 */ | |
2059 | ID_SANITISED(ID_AA64MMFR0_EL1), | |
2060 | ID_SANITISED(ID_AA64MMFR1_EL1), | |
2061 | ID_SANITISED(ID_AA64MMFR2_EL1), | |
8ef67c67 | 2062 | ID_SANITISED(ID_AA64MMFR3_EL1), |
93390c0a DM |
2063 | ID_UNALLOCATED(7,4), |
2064 | ID_UNALLOCATED(7,5), | |
2065 | ID_UNALLOCATED(7,6), | |
2066 | ID_UNALLOCATED(7,7), | |
2067 | ||
851050a5 | 2068 | { SYS_DESC(SYS_SCTLR_EL1), access_vm_reg, reset_val, SCTLR_EL1, 0x00C50078 }, |
af473829 | 2069 | { SYS_DESC(SYS_ACTLR_EL1), access_actlr, reset_actlr, ACTLR_EL1 }, |
851050a5 | 2070 | { SYS_DESC(SYS_CPACR_EL1), NULL, reset_val, CPACR_EL1, 0 }, |
2ac638fc | 2071 | |
e1f358b5 SP |
2072 | MTE_REG(RGSR_EL1), |
2073 | MTE_REG(GCR_EL1), | |
2ac638fc | 2074 | |
73433762 | 2075 | { SYS_DESC(SYS_ZCR_EL1), NULL, reset_val, ZCR_EL1, 0, .visibility = sve_visibility }, |
cc427cbb | 2076 | { SYS_DESC(SYS_TRFCR_EL1), undef_access }, |
90807748 MB |
2077 | { SYS_DESC(SYS_SMPRI_EL1), undef_access }, |
2078 | { SYS_DESC(SYS_SMCR_EL1), undef_access }, | |
851050a5 MR |
2079 | { SYS_DESC(SYS_TTBR0_EL1), access_vm_reg, reset_unknown, TTBR0_EL1 }, |
2080 | { SYS_DESC(SYS_TTBR1_EL1), access_vm_reg, reset_unknown, TTBR1_EL1 }, | |
2081 | { SYS_DESC(SYS_TCR_EL1), access_vm_reg, reset_val, TCR_EL1, 0 }, | |
fbff5606 | 2082 | { SYS_DESC(SYS_TCR2_EL1), access_vm_reg, reset_val, TCR2_EL1, 0 }, |
851050a5 | 2083 | |
384b40ca MR |
2084 | PTRAUTH_KEY(APIA), |
2085 | PTRAUTH_KEY(APIB), | |
2086 | PTRAUTH_KEY(APDA), | |
2087 | PTRAUTH_KEY(APDB), | |
2088 | PTRAUTH_KEY(APGA), | |
2089 | ||
9da117ee JL |
2090 | { SYS_DESC(SYS_SPSR_EL1), access_spsr}, |
2091 | { SYS_DESC(SYS_ELR_EL1), access_elr}, | |
2092 | ||
851050a5 MR |
2093 | { SYS_DESC(SYS_AFSR0_EL1), access_vm_reg, reset_unknown, AFSR0_EL1 }, |
2094 | { SYS_DESC(SYS_AFSR1_EL1), access_vm_reg, reset_unknown, AFSR1_EL1 }, | |
2095 | { SYS_DESC(SYS_ESR_EL1), access_vm_reg, reset_unknown, ESR_EL1 }, | |
558daf69 DG |
2096 | |
2097 | { SYS_DESC(SYS_ERRIDR_EL1), trap_raz_wi }, | |
2098 | { SYS_DESC(SYS_ERRSELR_EL1), trap_raz_wi }, | |
2099 | { SYS_DESC(SYS_ERXFR_EL1), trap_raz_wi }, | |
2100 | { SYS_DESC(SYS_ERXCTLR_EL1), trap_raz_wi }, | |
2101 | { SYS_DESC(SYS_ERXSTATUS_EL1), trap_raz_wi }, | |
2102 | { SYS_DESC(SYS_ERXADDR_EL1), trap_raz_wi }, | |
2103 | { SYS_DESC(SYS_ERXMISC0_EL1), trap_raz_wi }, | |
2104 | { SYS_DESC(SYS_ERXMISC1_EL1), trap_raz_wi }, | |
2105 | ||
e1f358b5 SP |
2106 | MTE_REG(TFSR_EL1), |
2107 | MTE_REG(TFSRE0_EL1), | |
2ac638fc | 2108 | |
851050a5 MR |
2109 | { SYS_DESC(SYS_FAR_EL1), access_vm_reg, reset_unknown, FAR_EL1 }, |
2110 | { SYS_DESC(SYS_PAR_EL1), NULL, reset_unknown, PAR_EL1 }, | |
7c8c5e6a | 2111 | |
13611bc8 AE |
2112 | { SYS_DESC(SYS_PMSCR_EL1), undef_access }, |
2113 | { SYS_DESC(SYS_PMSNEVFR_EL1), undef_access }, | |
2114 | { SYS_DESC(SYS_PMSICR_EL1), undef_access }, | |
2115 | { SYS_DESC(SYS_PMSIRR_EL1), undef_access }, | |
2116 | { SYS_DESC(SYS_PMSFCR_EL1), undef_access }, | |
2117 | { SYS_DESC(SYS_PMSEVFR_EL1), undef_access }, | |
2118 | { SYS_DESC(SYS_PMSLATFR_EL1), undef_access }, | |
2119 | { SYS_DESC(SYS_PMSIDR_EL1), undef_access }, | |
2120 | { SYS_DESC(SYS_PMBLIMITR_EL1), undef_access }, | |
2121 | { SYS_DESC(SYS_PMBPTR_EL1), undef_access }, | |
2122 | { SYS_DESC(SYS_PMBSR_EL1), undef_access }, | |
2123 | /* PMBIDR_EL1 is not trapped */ | |
2124 | ||
9d2a55b4 | 2125 | { PMU_SYS_REG(PMINTENSET_EL1), |
11663111 | 2126 | .access = access_pminten, .reg = PMINTENSET_EL1 }, |
9d2a55b4 | 2127 | { PMU_SYS_REG(PMINTENCLR_EL1), |
11663111 | 2128 | .access = access_pminten, .reg = PMINTENSET_EL1 }, |
46081078 | 2129 | { SYS_DESC(SYS_PMMIR_EL1), trap_raz_wi }, |
7c8c5e6a | 2130 | |
851050a5 | 2131 | { SYS_DESC(SYS_MAIR_EL1), access_vm_reg, reset_unknown, MAIR_EL1 }, |
86f9de9d JG |
2132 | { SYS_DESC(SYS_PIRE0_EL1), access_vm_reg, reset_unknown, PIRE0_EL1 }, |
2133 | { SYS_DESC(SYS_PIR_EL1), access_vm_reg, reset_unknown, PIR_EL1 }, | |
851050a5 | 2134 | { SYS_DESC(SYS_AMAIR_EL1), access_vm_reg, reset_amair_el1, AMAIR_EL1 }, |
7c8c5e6a | 2135 | |
22925521 MZ |
2136 | { SYS_DESC(SYS_LORSA_EL1), trap_loregion }, |
2137 | { SYS_DESC(SYS_LOREA_EL1), trap_loregion }, | |
2138 | { SYS_DESC(SYS_LORN_EL1), trap_loregion }, | |
2139 | { SYS_DESC(SYS_LORC_EL1), trap_loregion }, | |
2140 | { SYS_DESC(SYS_LORID_EL1), trap_loregion }, | |
cc33c4e2 | 2141 | |
9da117ee | 2142 | { SYS_DESC(SYS_VBAR_EL1), access_rw, reset_val, VBAR_EL1, 0 }, |
c773ae2b | 2143 | { SYS_DESC(SYS_DISR_EL1), NULL, reset_val, DISR_EL1, 0 }, |
db7dedd0 | 2144 | |
7b1dba1f | 2145 | { SYS_DESC(SYS_ICC_IAR0_EL1), write_to_read_only }, |
e7f1d1ee | 2146 | { SYS_DESC(SYS_ICC_EOIR0_EL1), read_from_write_only }, |
7b1dba1f | 2147 | { SYS_DESC(SYS_ICC_HPPIR0_EL1), write_to_read_only }, |
e7f1d1ee | 2148 | { SYS_DESC(SYS_ICC_DIR_EL1), read_from_write_only }, |
7b1dba1f | 2149 | { SYS_DESC(SYS_ICC_RPR_EL1), write_to_read_only }, |
e804d208 | 2150 | { SYS_DESC(SYS_ICC_SGI1R_EL1), access_gic_sgi }, |
03bd646d MZ |
2151 | { SYS_DESC(SYS_ICC_ASGI1R_EL1), access_gic_sgi }, |
2152 | { SYS_DESC(SYS_ICC_SGI0R_EL1), access_gic_sgi }, | |
7b1dba1f | 2153 | { SYS_DESC(SYS_ICC_IAR1_EL1), write_to_read_only }, |
e7f1d1ee | 2154 | { SYS_DESC(SYS_ICC_EOIR1_EL1), read_from_write_only }, |
7b1dba1f | 2155 | { SYS_DESC(SYS_ICC_HPPIR1_EL1), write_to_read_only }, |
e804d208 | 2156 | { SYS_DESC(SYS_ICC_SRE_EL1), access_gic_sre }, |
db7dedd0 | 2157 | |
851050a5 MR |
2158 | { SYS_DESC(SYS_CONTEXTIDR_EL1), access_vm_reg, reset_val, CONTEXTIDR_EL1, 0 }, |
2159 | { SYS_DESC(SYS_TPIDR_EL1), NULL, reset_unknown, TPIDR_EL1 }, | |
7c8c5e6a | 2160 | |
484f8682 MZ |
2161 | { SYS_DESC(SYS_ACCDATA_EL1), undef_access }, |
2162 | ||
ed4ffaf4 MZ |
2163 | { SYS_DESC(SYS_SCXTNUM_EL1), undef_access }, |
2164 | ||
851050a5 | 2165 | { SYS_DESC(SYS_CNTKCTL_EL1), NULL, reset_val, CNTKCTL_EL1, 0}, |
7c8c5e6a | 2166 | |
f7f2b15c | 2167 | { SYS_DESC(SYS_CCSIDR_EL1), access_ccsidr }, |
7af0c253 AO |
2168 | { SYS_DESC(SYS_CLIDR_EL1), access_clidr, reset_clidr, CLIDR_EL1, |
2169 | .set_user = set_clidr }, | |
bf48040c | 2170 | { SYS_DESC(SYS_CCSIDR2_EL1), undef_access }, |
90807748 | 2171 | { SYS_DESC(SYS_SMIDR_EL1), undef_access }, |
f7f2b15c AB |
2172 | { SYS_DESC(SYS_CSSELR_EL1), access_csselr, reset_unknown, CSSELR_EL1 }, |
2173 | { SYS_DESC(SYS_CTR_EL0), access_ctr }, | |
ec0067a6 | 2174 | { SYS_DESC(SYS_SVCR), undef_access }, |
7c8c5e6a | 2175 | |
9d2a55b4 | 2176 | { PMU_SYS_REG(PMCR_EL0), .access = access_pmcr, |
11663111 | 2177 | .reset = reset_pmcr, .reg = PMCR_EL0 }, |
9d2a55b4 | 2178 | { PMU_SYS_REG(PMCNTENSET_EL0), |
11663111 | 2179 | .access = access_pmcnten, .reg = PMCNTENSET_EL0 }, |
9d2a55b4 | 2180 | { PMU_SYS_REG(PMCNTENCLR_EL0), |
11663111 | 2181 | .access = access_pmcnten, .reg = PMCNTENSET_EL0 }, |
9d2a55b4 | 2182 | { PMU_SYS_REG(PMOVSCLR_EL0), |
11663111 | 2183 | .access = access_pmovs, .reg = PMOVSSET_EL0 }, |
7a3ba309 MZ |
2184 | /* |
2185 | * PM_SWINC_EL0 is exposed to userspace as RAZ/WI, as it was | |
2186 | * previously (and pointlessly) advertised in the past... | |
2187 | */ | |
9d2a55b4 | 2188 | { PMU_SYS_REG(PMSWINC_EL0), |
5a430976 | 2189 | .get_user = get_raz_reg, .set_user = set_wi_reg, |
7a3ba309 | 2190 | .access = access_pmswinc, .reset = NULL }, |
9d2a55b4 | 2191 | { PMU_SYS_REG(PMSELR_EL0), |
0ab410a9 | 2192 | .access = access_pmselr, .reset = reset_pmselr, .reg = PMSELR_EL0 }, |
9d2a55b4 | 2193 | { PMU_SYS_REG(PMCEID0_EL0), |
11663111 | 2194 | .access = access_pmceid, .reset = NULL }, |
9d2a55b4 | 2195 | { PMU_SYS_REG(PMCEID1_EL0), |
11663111 | 2196 | .access = access_pmceid, .reset = NULL }, |
9d2a55b4 | 2197 | { PMU_SYS_REG(PMCCNTR_EL0), |
9228b261 RW |
2198 | .access = access_pmu_evcntr, .reset = reset_unknown, |
2199 | .reg = PMCCNTR_EL0, .get_user = get_pmu_evcntr}, | |
9d2a55b4 | 2200 | { PMU_SYS_REG(PMXEVTYPER_EL0), |
11663111 | 2201 | .access = access_pmu_evtyper, .reset = NULL }, |
9d2a55b4 | 2202 | { PMU_SYS_REG(PMXEVCNTR_EL0), |
11663111 | 2203 | .access = access_pmu_evcntr, .reset = NULL }, |
174ed3e4 MR |
2204 | /* |
2205 | * PMUSERENR_EL0 resets as unknown in 64bit mode while it resets as zero | |
d692b8ad SZ |
2206 | * in 32bit mode. Here we choose to reset it as zero for consistency. |
2207 | */ | |
9d2a55b4 | 2208 | { PMU_SYS_REG(PMUSERENR_EL0), .access = access_pmuserenr, |
11663111 | 2209 | .reset = reset_val, .reg = PMUSERENR_EL0, .val = 0 }, |
9d2a55b4 | 2210 | { PMU_SYS_REG(PMOVSSET_EL0), |
11663111 | 2211 | .access = access_pmovs, .reg = PMOVSSET_EL0 }, |
7c8c5e6a | 2212 | |
851050a5 MR |
2213 | { SYS_DESC(SYS_TPIDR_EL0), NULL, reset_unknown, TPIDR_EL0 }, |
2214 | { SYS_DESC(SYS_TPIDRRO_EL0), NULL, reset_unknown, TPIDRRO_EL0 }, | |
90807748 | 2215 | { SYS_DESC(SYS_TPIDR2_EL0), undef_access }, |
4fcdf106 | 2216 | |
ed4ffaf4 MZ |
2217 | { SYS_DESC(SYS_SCXTNUM_EL0), undef_access }, |
2218 | ||
338b1793 MZ |
2219 | { SYS_DESC(SYS_AMCR_EL0), undef_access }, |
2220 | { SYS_DESC(SYS_AMCFGR_EL0), undef_access }, | |
2221 | { SYS_DESC(SYS_AMCGCR_EL0), undef_access }, | |
2222 | { SYS_DESC(SYS_AMUSERENR_EL0), undef_access }, | |
2223 | { SYS_DESC(SYS_AMCNTENCLR0_EL0), undef_access }, | |
2224 | { SYS_DESC(SYS_AMCNTENSET0_EL0), undef_access }, | |
2225 | { SYS_DESC(SYS_AMCNTENCLR1_EL0), undef_access }, | |
2226 | { SYS_DESC(SYS_AMCNTENSET1_EL0), undef_access }, | |
4fcdf106 IV |
2227 | AMU_AMEVCNTR0_EL0(0), |
2228 | AMU_AMEVCNTR0_EL0(1), | |
2229 | AMU_AMEVCNTR0_EL0(2), | |
2230 | AMU_AMEVCNTR0_EL0(3), | |
2231 | AMU_AMEVCNTR0_EL0(4), | |
2232 | AMU_AMEVCNTR0_EL0(5), | |
2233 | AMU_AMEVCNTR0_EL0(6), | |
2234 | AMU_AMEVCNTR0_EL0(7), | |
2235 | AMU_AMEVCNTR0_EL0(8), | |
2236 | AMU_AMEVCNTR0_EL0(9), | |
2237 | AMU_AMEVCNTR0_EL0(10), | |
2238 | AMU_AMEVCNTR0_EL0(11), | |
2239 | AMU_AMEVCNTR0_EL0(12), | |
2240 | AMU_AMEVCNTR0_EL0(13), | |
2241 | AMU_AMEVCNTR0_EL0(14), | |
2242 | AMU_AMEVCNTR0_EL0(15), | |
493cf9b7 VM |
2243 | AMU_AMEVTYPER0_EL0(0), |
2244 | AMU_AMEVTYPER0_EL0(1), | |
2245 | AMU_AMEVTYPER0_EL0(2), | |
2246 | AMU_AMEVTYPER0_EL0(3), | |
2247 | AMU_AMEVTYPER0_EL0(4), | |
2248 | AMU_AMEVTYPER0_EL0(5), | |
2249 | AMU_AMEVTYPER0_EL0(6), | |
2250 | AMU_AMEVTYPER0_EL0(7), | |
2251 | AMU_AMEVTYPER0_EL0(8), | |
2252 | AMU_AMEVTYPER0_EL0(9), | |
2253 | AMU_AMEVTYPER0_EL0(10), | |
2254 | AMU_AMEVTYPER0_EL0(11), | |
2255 | AMU_AMEVTYPER0_EL0(12), | |
2256 | AMU_AMEVTYPER0_EL0(13), | |
2257 | AMU_AMEVTYPER0_EL0(14), | |
2258 | AMU_AMEVTYPER0_EL0(15), | |
4fcdf106 IV |
2259 | AMU_AMEVCNTR1_EL0(0), |
2260 | AMU_AMEVCNTR1_EL0(1), | |
2261 | AMU_AMEVCNTR1_EL0(2), | |
2262 | AMU_AMEVCNTR1_EL0(3), | |
2263 | AMU_AMEVCNTR1_EL0(4), | |
2264 | AMU_AMEVCNTR1_EL0(5), | |
2265 | AMU_AMEVCNTR1_EL0(6), | |
2266 | AMU_AMEVCNTR1_EL0(7), | |
2267 | AMU_AMEVCNTR1_EL0(8), | |
2268 | AMU_AMEVCNTR1_EL0(9), | |
2269 | AMU_AMEVCNTR1_EL0(10), | |
2270 | AMU_AMEVCNTR1_EL0(11), | |
2271 | AMU_AMEVCNTR1_EL0(12), | |
2272 | AMU_AMEVCNTR1_EL0(13), | |
2273 | AMU_AMEVCNTR1_EL0(14), | |
2274 | AMU_AMEVCNTR1_EL0(15), | |
493cf9b7 VM |
2275 | AMU_AMEVTYPER1_EL0(0), |
2276 | AMU_AMEVTYPER1_EL0(1), | |
2277 | AMU_AMEVTYPER1_EL0(2), | |
2278 | AMU_AMEVTYPER1_EL0(3), | |
2279 | AMU_AMEVTYPER1_EL0(4), | |
2280 | AMU_AMEVTYPER1_EL0(5), | |
2281 | AMU_AMEVTYPER1_EL0(6), | |
2282 | AMU_AMEVTYPER1_EL0(7), | |
2283 | AMU_AMEVTYPER1_EL0(8), | |
2284 | AMU_AMEVTYPER1_EL0(9), | |
2285 | AMU_AMEVTYPER1_EL0(10), | |
2286 | AMU_AMEVTYPER1_EL0(11), | |
2287 | AMU_AMEVTYPER1_EL0(12), | |
2288 | AMU_AMEVTYPER1_EL0(13), | |
2289 | AMU_AMEVTYPER1_EL0(14), | |
2290 | AMU_AMEVTYPER1_EL0(15), | |
62a89c44 | 2291 | |
c605ee24 MZ |
2292 | { SYS_DESC(SYS_CNTPCT_EL0), access_arch_timer }, |
2293 | { SYS_DESC(SYS_CNTPCTSS_EL0), access_arch_timer }, | |
84135d3d AP |
2294 | { SYS_DESC(SYS_CNTP_TVAL_EL0), access_arch_timer }, |
2295 | { SYS_DESC(SYS_CNTP_CTL_EL0), access_arch_timer }, | |
2296 | { SYS_DESC(SYS_CNTP_CVAL_EL0), access_arch_timer }, | |
c9a3c58f | 2297 | |
051ff581 SZ |
2298 | /* PMEVCNTRn_EL0 */ |
2299 | PMU_PMEVCNTR_EL0(0), | |
2300 | PMU_PMEVCNTR_EL0(1), | |
2301 | PMU_PMEVCNTR_EL0(2), | |
2302 | PMU_PMEVCNTR_EL0(3), | |
2303 | PMU_PMEVCNTR_EL0(4), | |
2304 | PMU_PMEVCNTR_EL0(5), | |
2305 | PMU_PMEVCNTR_EL0(6), | |
2306 | PMU_PMEVCNTR_EL0(7), | |
2307 | PMU_PMEVCNTR_EL0(8), | |
2308 | PMU_PMEVCNTR_EL0(9), | |
2309 | PMU_PMEVCNTR_EL0(10), | |
2310 | PMU_PMEVCNTR_EL0(11), | |
2311 | PMU_PMEVCNTR_EL0(12), | |
2312 | PMU_PMEVCNTR_EL0(13), | |
2313 | PMU_PMEVCNTR_EL0(14), | |
2314 | PMU_PMEVCNTR_EL0(15), | |
2315 | PMU_PMEVCNTR_EL0(16), | |
2316 | PMU_PMEVCNTR_EL0(17), | |
2317 | PMU_PMEVCNTR_EL0(18), | |
2318 | PMU_PMEVCNTR_EL0(19), | |
2319 | PMU_PMEVCNTR_EL0(20), | |
2320 | PMU_PMEVCNTR_EL0(21), | |
2321 | PMU_PMEVCNTR_EL0(22), | |
2322 | PMU_PMEVCNTR_EL0(23), | |
2323 | PMU_PMEVCNTR_EL0(24), | |
2324 | PMU_PMEVCNTR_EL0(25), | |
2325 | PMU_PMEVCNTR_EL0(26), | |
2326 | PMU_PMEVCNTR_EL0(27), | |
2327 | PMU_PMEVCNTR_EL0(28), | |
2328 | PMU_PMEVCNTR_EL0(29), | |
2329 | PMU_PMEVCNTR_EL0(30), | |
9feb21ac SZ |
2330 | /* PMEVTYPERn_EL0 */ |
2331 | PMU_PMEVTYPER_EL0(0), | |
2332 | PMU_PMEVTYPER_EL0(1), | |
2333 | PMU_PMEVTYPER_EL0(2), | |
2334 | PMU_PMEVTYPER_EL0(3), | |
2335 | PMU_PMEVTYPER_EL0(4), | |
2336 | PMU_PMEVTYPER_EL0(5), | |
2337 | PMU_PMEVTYPER_EL0(6), | |
2338 | PMU_PMEVTYPER_EL0(7), | |
2339 | PMU_PMEVTYPER_EL0(8), | |
2340 | PMU_PMEVTYPER_EL0(9), | |
2341 | PMU_PMEVTYPER_EL0(10), | |
2342 | PMU_PMEVTYPER_EL0(11), | |
2343 | PMU_PMEVTYPER_EL0(12), | |
2344 | PMU_PMEVTYPER_EL0(13), | |
2345 | PMU_PMEVTYPER_EL0(14), | |
2346 | PMU_PMEVTYPER_EL0(15), | |
2347 | PMU_PMEVTYPER_EL0(16), | |
2348 | PMU_PMEVTYPER_EL0(17), | |
2349 | PMU_PMEVTYPER_EL0(18), | |
2350 | PMU_PMEVTYPER_EL0(19), | |
2351 | PMU_PMEVTYPER_EL0(20), | |
2352 | PMU_PMEVTYPER_EL0(21), | |
2353 | PMU_PMEVTYPER_EL0(22), | |
2354 | PMU_PMEVTYPER_EL0(23), | |
2355 | PMU_PMEVTYPER_EL0(24), | |
2356 | PMU_PMEVTYPER_EL0(25), | |
2357 | PMU_PMEVTYPER_EL0(26), | |
2358 | PMU_PMEVTYPER_EL0(27), | |
2359 | PMU_PMEVTYPER_EL0(28), | |
2360 | PMU_PMEVTYPER_EL0(29), | |
2361 | PMU_PMEVTYPER_EL0(30), | |
174ed3e4 MR |
2362 | /* |
2363 | * PMCCFILTR_EL0 resets as unknown in 64bit mode while it resets as zero | |
9feb21ac SZ |
2364 | * in 32bit mode. Here we choose to reset it as zero for consistency. |
2365 | */ | |
9d2a55b4 | 2366 | { PMU_SYS_REG(PMCCFILTR_EL0), .access = access_pmu_evtyper, |
11663111 | 2367 | .reset = reset_val, .reg = PMCCFILTR_EL0, .val = 0 }, |
051ff581 | 2368 | |
6ff9dc23 JL |
2369 | EL2_REG(VPIDR_EL2, access_rw, reset_unknown, 0), |
2370 | EL2_REG(VMPIDR_EL2, access_rw, reset_unknown, 0), | |
2371 | EL2_REG(SCTLR_EL2, access_rw, reset_val, SCTLR_EL2_RES1), | |
2372 | EL2_REG(ACTLR_EL2, access_rw, reset_val, 0), | |
2373 | EL2_REG(HCR_EL2, access_rw, reset_val, 0), | |
2374 | EL2_REG(MDCR_EL2, access_rw, reset_val, 0), | |
75c76ab5 | 2375 | EL2_REG(CPTR_EL2, access_rw, reset_val, CPTR_NVHE_EL2_RES1), |
6ff9dc23 | 2376 | EL2_REG(HSTR_EL2, access_rw, reset_val, 0), |
50d2fe46 MZ |
2377 | EL2_REG(HFGRTR_EL2, access_rw, reset_val, 0), |
2378 | EL2_REG(HFGWTR_EL2, access_rw, reset_val, 0), | |
2379 | EL2_REG(HFGITR_EL2, access_rw, reset_val, 0), | |
6ff9dc23 JL |
2380 | EL2_REG(HACR_EL2, access_rw, reset_val, 0), |
2381 | ||
03fb54d0 MZ |
2382 | EL2_REG(HCRX_EL2, access_rw, reset_val, 0), |
2383 | ||
6ff9dc23 JL |
2384 | EL2_REG(TTBR0_EL2, access_rw, reset_val, 0), |
2385 | EL2_REG(TTBR1_EL2, access_rw, reset_val, 0), | |
2386 | EL2_REG(TCR_EL2, access_rw, reset_val, TCR_EL2_RES1), | |
2387 | EL2_REG(VTTBR_EL2, access_rw, reset_val, 0), | |
2388 | EL2_REG(VTCR_EL2, access_rw, reset_val, 0), | |
2389 | ||
851050a5 | 2390 | { SYS_DESC(SYS_DACR32_EL2), NULL, reset_unknown, DACR32_EL2 }, |
50d2fe46 MZ |
2391 | EL2_REG(HDFGRTR_EL2, access_rw, reset_val, 0), |
2392 | EL2_REG(HDFGWTR_EL2, access_rw, reset_val, 0), | |
6ff9dc23 JL |
2393 | EL2_REG(SPSR_EL2, access_rw, reset_val, 0), |
2394 | EL2_REG(ELR_EL2, access_rw, reset_val, 0), | |
2395 | { SYS_DESC(SYS_SP_EL1), access_sp_el1}, | |
2396 | ||
851050a5 | 2397 | { SYS_DESC(SYS_IFSR32_EL2), NULL, reset_unknown, IFSR32_EL2 }, |
6ff9dc23 JL |
2398 | EL2_REG(AFSR0_EL2, access_rw, reset_val, 0), |
2399 | EL2_REG(AFSR1_EL2, access_rw, reset_val, 0), | |
2400 | EL2_REG(ESR_EL2, access_rw, reset_val, 0), | |
c88b0936 | 2401 | { SYS_DESC(SYS_FPEXC32_EL2), NULL, reset_val, FPEXC32_EL2, 0x700 }, |
6ff9dc23 JL |
2402 | |
2403 | EL2_REG(FAR_EL2, access_rw, reset_val, 0), | |
2404 | EL2_REG(HPFAR_EL2, access_rw, reset_val, 0), | |
2405 | ||
2406 | EL2_REG(MAIR_EL2, access_rw, reset_val, 0), | |
2407 | EL2_REG(AMAIR_EL2, access_rw, reset_val, 0), | |
2408 | ||
2409 | EL2_REG(VBAR_EL2, access_rw, reset_val, 0), | |
2410 | EL2_REG(RVBAR_EL2, access_rw, reset_val, 0), | |
2411 | { SYS_DESC(SYS_RMR_EL2), trap_undef }, | |
2412 | ||
2413 | EL2_REG(CONTEXTIDR_EL2, access_rw, reset_val, 0), | |
2414 | EL2_REG(TPIDR_EL2, access_rw, reset_val, 0), | |
2415 | ||
2416 | EL2_REG(CNTVOFF_EL2, access_rw, reset_val, 0), | |
2417 | EL2_REG(CNTHCTL_EL2, access_rw, reset_val, 0), | |
2418 | ||
280b748e JL |
2419 | EL12_REG(SCTLR, access_vm_reg, reset_val, 0x00C50078), |
2420 | EL12_REG(CPACR, access_rw, reset_val, 0), | |
2421 | EL12_REG(TTBR0, access_vm_reg, reset_unknown, 0), | |
2422 | EL12_REG(TTBR1, access_vm_reg, reset_unknown, 0), | |
2423 | EL12_REG(TCR, access_vm_reg, reset_val, 0), | |
2424 | { SYS_DESC(SYS_SPSR_EL12), access_spsr}, | |
2425 | { SYS_DESC(SYS_ELR_EL12), access_elr}, | |
2426 | EL12_REG(AFSR0, access_vm_reg, reset_unknown, 0), | |
2427 | EL12_REG(AFSR1, access_vm_reg, reset_unknown, 0), | |
2428 | EL12_REG(ESR, access_vm_reg, reset_unknown, 0), | |
2429 | EL12_REG(FAR, access_vm_reg, reset_unknown, 0), | |
2430 | EL12_REG(MAIR, access_vm_reg, reset_unknown, 0), | |
2431 | EL12_REG(AMAIR, access_vm_reg, reset_amair_el1, 0), | |
2432 | EL12_REG(VBAR, access_rw, reset_val, 0), | |
2433 | EL12_REG(CONTEXTIDR, access_vm_reg, reset_val, 0), | |
2434 | EL12_REG(CNTKCTL, access_rw, reset_val, 0), | |
2435 | ||
6ff9dc23 | 2436 | EL2_REG(SP_EL2, NULL, reset_unknown, 0), |
62a89c44 MZ |
2437 | }; |
2438 | ||
47334146 JZ |
2439 | static const struct sys_reg_desc *first_idreg; |
2440 | ||
8c358b29 | 2441 | static bool trap_dbgdidr(struct kvm_vcpu *vcpu, |
3fec037d | 2442 | struct sys_reg_params *p, |
bdfb4b38 MZ |
2443 | const struct sys_reg_desc *r) |
2444 | { | |
2445 | if (p->is_write) { | |
2446 | return ignore_write(vcpu, p); | |
2447 | } else { | |
8b6958d6 JZ |
2448 | u64 dfr = IDREG(vcpu->kvm, SYS_ID_AA64DFR0_EL1); |
2449 | u64 pfr = IDREG(vcpu->kvm, SYS_ID_AA64PFR0_EL1); | |
55adc08d | 2450 | u32 el3 = !!cpuid_feature_extract_unsigned_field(pfr, ID_AA64PFR0_EL1_EL3_SHIFT); |
bdfb4b38 | 2451 | |
fcf37b38 MB |
2452 | p->regval = ((((dfr >> ID_AA64DFR0_EL1_WRPs_SHIFT) & 0xf) << 28) | |
2453 | (((dfr >> ID_AA64DFR0_EL1_BRPs_SHIFT) & 0xf) << 24) | | |
2454 | (((dfr >> ID_AA64DFR0_EL1_CTX_CMPs_SHIFT) & 0xf) << 20) | |
bea7e97f | 2455 | | (6 << 16) | (1 << 15) | (el3 << 14) | (el3 << 12)); |
bdfb4b38 MZ |
2456 | return true; |
2457 | } | |
2458 | } | |
2459 | ||
1da42c34 MZ |
2460 | /* |
2461 | * AArch32 debug register mappings | |
84e690bf AB |
2462 | * |
2463 | * AArch32 DBGBVRn is mapped to DBGBVRn_EL1[31:0] | |
2464 | * AArch32 DBGBXVRn is mapped to DBGBVRn_EL1[63:32] | |
2465 | * | |
1da42c34 MZ |
2466 | * None of the other registers share their location, so treat them as |
2467 | * if they were 64bit. | |
84e690bf | 2468 | */ |
1da42c34 MZ |
2469 | #define DBG_BCR_BVR_WCR_WVR(n) \ |
2470 | /* DBGBVRn */ \ | |
2471 | { AA32(LO), Op1( 0), CRn( 0), CRm((n)), Op2( 4), trap_bvr, NULL, n }, \ | |
2472 | /* DBGBCRn */ \ | |
2473 | { Op1( 0), CRn( 0), CRm((n)), Op2( 5), trap_bcr, NULL, n }, \ | |
2474 | /* DBGWVRn */ \ | |
2475 | { Op1( 0), CRn( 0), CRm((n)), Op2( 6), trap_wvr, NULL, n }, \ | |
2476 | /* DBGWCRn */ \ | |
84e690bf AB |
2477 | { Op1( 0), CRn( 0), CRm((n)), Op2( 7), trap_wcr, NULL, n } |
2478 | ||
1da42c34 MZ |
2479 | #define DBGBXVR(n) \ |
2480 | { AA32(HI), Op1( 0), CRn( 1), CRm((n)), Op2( 1), trap_bvr, NULL, n } | |
bdfb4b38 MZ |
2481 | |
2482 | /* | |
2483 | * Trapped cp14 registers. We generally ignore most of the external | |
2484 | * debug, on the principle that they don't really make sense to a | |
84e690bf | 2485 | * guest. Revisit this one day, would this principle change. |
bdfb4b38 | 2486 | */ |
72564016 | 2487 | static const struct sys_reg_desc cp14_regs[] = { |
8c358b29 AE |
2488 | /* DBGDIDR */ |
2489 | { Op1( 0), CRn( 0), CRm( 0), Op2( 0), trap_dbgdidr }, | |
bdfb4b38 MZ |
2490 | /* DBGDTRRXext */ |
2491 | { Op1( 0), CRn( 0), CRm( 0), Op2( 2), trap_raz_wi }, | |
2492 | ||
2493 | DBG_BCR_BVR_WCR_WVR(0), | |
2494 | /* DBGDSCRint */ | |
2495 | { Op1( 0), CRn( 0), CRm( 1), Op2( 0), trap_raz_wi }, | |
2496 | DBG_BCR_BVR_WCR_WVR(1), | |
2497 | /* DBGDCCINT */ | |
1da42c34 | 2498 | { Op1( 0), CRn( 0), CRm( 2), Op2( 0), trap_debug_regs, NULL, MDCCINT_EL1 }, |
bdfb4b38 | 2499 | /* DBGDSCRext */ |
1da42c34 | 2500 | { Op1( 0), CRn( 0), CRm( 2), Op2( 2), trap_debug_regs, NULL, MDSCR_EL1 }, |
bdfb4b38 MZ |
2501 | DBG_BCR_BVR_WCR_WVR(2), |
2502 | /* DBGDTR[RT]Xint */ | |
2503 | { Op1( 0), CRn( 0), CRm( 3), Op2( 0), trap_raz_wi }, | |
2504 | /* DBGDTR[RT]Xext */ | |
2505 | { Op1( 0), CRn( 0), CRm( 3), Op2( 2), trap_raz_wi }, | |
2506 | DBG_BCR_BVR_WCR_WVR(3), | |
2507 | DBG_BCR_BVR_WCR_WVR(4), | |
2508 | DBG_BCR_BVR_WCR_WVR(5), | |
2509 | /* DBGWFAR */ | |
2510 | { Op1( 0), CRn( 0), CRm( 6), Op2( 0), trap_raz_wi }, | |
2511 | /* DBGOSECCR */ | |
2512 | { Op1( 0), CRn( 0), CRm( 6), Op2( 2), trap_raz_wi }, | |
2513 | DBG_BCR_BVR_WCR_WVR(6), | |
2514 | /* DBGVCR */ | |
1da42c34 | 2515 | { Op1( 0), CRn( 0), CRm( 7), Op2( 0), trap_debug_regs, NULL, DBGVCR32_EL2 }, |
bdfb4b38 MZ |
2516 | DBG_BCR_BVR_WCR_WVR(7), |
2517 | DBG_BCR_BVR_WCR_WVR(8), | |
2518 | DBG_BCR_BVR_WCR_WVR(9), | |
2519 | DBG_BCR_BVR_WCR_WVR(10), | |
2520 | DBG_BCR_BVR_WCR_WVR(11), | |
2521 | DBG_BCR_BVR_WCR_WVR(12), | |
2522 | DBG_BCR_BVR_WCR_WVR(13), | |
2523 | DBG_BCR_BVR_WCR_WVR(14), | |
2524 | DBG_BCR_BVR_WCR_WVR(15), | |
2525 | ||
2526 | /* DBGDRAR (32bit) */ | |
2527 | { Op1( 0), CRn( 1), CRm( 0), Op2( 0), trap_raz_wi }, | |
2528 | ||
2529 | DBGBXVR(0), | |
2530 | /* DBGOSLAR */ | |
f24adc65 | 2531 | { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_oslar_el1 }, |
bdfb4b38 MZ |
2532 | DBGBXVR(1), |
2533 | /* DBGOSLSR */ | |
d42e2671 | 2534 | { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1, NULL, OSLSR_EL1 }, |
bdfb4b38 MZ |
2535 | DBGBXVR(2), |
2536 | DBGBXVR(3), | |
2537 | /* DBGOSDLR */ | |
2538 | { Op1( 0), CRn( 1), CRm( 3), Op2( 4), trap_raz_wi }, | |
2539 | DBGBXVR(4), | |
2540 | /* DBGPRCR */ | |
2541 | { Op1( 0), CRn( 1), CRm( 4), Op2( 4), trap_raz_wi }, | |
2542 | DBGBXVR(5), | |
2543 | DBGBXVR(6), | |
2544 | DBGBXVR(7), | |
2545 | DBGBXVR(8), | |
2546 | DBGBXVR(9), | |
2547 | DBGBXVR(10), | |
2548 | DBGBXVR(11), | |
2549 | DBGBXVR(12), | |
2550 | DBGBXVR(13), | |
2551 | DBGBXVR(14), | |
2552 | DBGBXVR(15), | |
2553 | ||
2554 | /* DBGDSAR (32bit) */ | |
2555 | { Op1( 0), CRn( 2), CRm( 0), Op2( 0), trap_raz_wi }, | |
2556 | ||
2557 | /* DBGDEVID2 */ | |
2558 | { Op1( 0), CRn( 7), CRm( 0), Op2( 7), trap_raz_wi }, | |
2559 | /* DBGDEVID1 */ | |
2560 | { Op1( 0), CRn( 7), CRm( 1), Op2( 7), trap_raz_wi }, | |
2561 | /* DBGDEVID */ | |
2562 | { Op1( 0), CRn( 7), CRm( 2), Op2( 7), trap_raz_wi }, | |
2563 | /* DBGCLAIMSET */ | |
2564 | { Op1( 0), CRn( 7), CRm( 8), Op2( 6), trap_raz_wi }, | |
2565 | /* DBGCLAIMCLR */ | |
2566 | { Op1( 0), CRn( 7), CRm( 9), Op2( 6), trap_raz_wi }, | |
2567 | /* DBGAUTHSTATUS */ | |
2568 | { Op1( 0), CRn( 7), CRm(14), Op2( 6), trap_dbgauthstatus_el1 }, | |
72564016 MZ |
2569 | }; |
2570 | ||
a9866ba0 MZ |
2571 | /* Trapped cp14 64bit registers */ |
2572 | static const struct sys_reg_desc cp14_64_regs[] = { | |
bdfb4b38 MZ |
2573 | /* DBGDRAR (64bit) */ |
2574 | { Op1( 0), CRm( 1), .access = trap_raz_wi }, | |
2575 | ||
2576 | /* DBGDSAR (64bit) */ | |
2577 | { Op1( 0), CRm( 2), .access = trap_raz_wi }, | |
a9866ba0 MZ |
2578 | }; |
2579 | ||
a9e192cd AE |
2580 | #define CP15_PMU_SYS_REG(_map, _Op1, _CRn, _CRm, _Op2) \ |
2581 | AA32(_map), \ | |
2582 | Op1(_Op1), CRn(_CRn), CRm(_CRm), Op2(_Op2), \ | |
2583 | .visibility = pmu_visibility | |
2584 | ||
051ff581 SZ |
2585 | /* Macro to expand the PMEVCNTRn register */ |
2586 | #define PMU_PMEVCNTR(n) \ | |
a9e192cd AE |
2587 | { CP15_PMU_SYS_REG(DIRECT, 0, 0b1110, \ |
2588 | (0b1000 | (((n) >> 3) & 0x3)), ((n) & 0x7)), \ | |
2589 | .access = access_pmu_evcntr } | |
051ff581 | 2590 | |
9feb21ac SZ |
2591 | /* Macro to expand the PMEVTYPERn register */ |
2592 | #define PMU_PMEVTYPER(n) \ | |
a9e192cd AE |
2593 | { CP15_PMU_SYS_REG(DIRECT, 0, 0b1110, \ |
2594 | (0b1100 | (((n) >> 3) & 0x3)), ((n) & 0x7)), \ | |
2595 | .access = access_pmu_evtyper } | |
4d44923b MZ |
2596 | /* |
2597 | * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding, | |
2598 | * depending on the way they are accessed (as a 32bit or a 64bit | |
2599 | * register). | |
2600 | */ | |
62a89c44 | 2601 | static const struct sys_reg_desc cp15_regs[] = { |
f7f2b15c | 2602 | { Op1( 0), CRn( 0), CRm( 0), Op2( 1), access_ctr }, |
b1ea1d76 MZ |
2603 | { Op1( 0), CRn( 1), CRm( 0), Op2( 0), access_vm_reg, NULL, SCTLR_EL1 }, |
2604 | /* ACTLR */ | |
2605 | { AA32(LO), Op1( 0), CRn( 1), CRm( 0), Op2( 1), access_actlr, NULL, ACTLR_EL1 }, | |
2606 | /* ACTLR2 */ | |
2607 | { AA32(HI), Op1( 0), CRn( 1), CRm( 0), Op2( 3), access_actlr, NULL, ACTLR_EL1 }, | |
2608 | { Op1( 0), CRn( 2), CRm( 0), Op2( 0), access_vm_reg, NULL, TTBR0_EL1 }, | |
2609 | { Op1( 0), CRn( 2), CRm( 0), Op2( 1), access_vm_reg, NULL, TTBR1_EL1 }, | |
2610 | /* TTBCR */ | |
2611 | { AA32(LO), Op1( 0), CRn( 2), CRm( 0), Op2( 2), access_vm_reg, NULL, TCR_EL1 }, | |
2612 | /* TTBCR2 */ | |
2613 | { AA32(HI), Op1( 0), CRn( 2), CRm( 0), Op2( 3), access_vm_reg, NULL, TCR_EL1 }, | |
2614 | { Op1( 0), CRn( 3), CRm( 0), Op2( 0), access_vm_reg, NULL, DACR32_EL2 }, | |
2615 | /* DFSR */ | |
2616 | { Op1( 0), CRn( 5), CRm( 0), Op2( 0), access_vm_reg, NULL, ESR_EL1 }, | |
2617 | { Op1( 0), CRn( 5), CRm( 0), Op2( 1), access_vm_reg, NULL, IFSR32_EL2 }, | |
2618 | /* ADFSR */ | |
2619 | { Op1( 0), CRn( 5), CRm( 1), Op2( 0), access_vm_reg, NULL, AFSR0_EL1 }, | |
2620 | /* AIFSR */ | |
2621 | { Op1( 0), CRn( 5), CRm( 1), Op2( 1), access_vm_reg, NULL, AFSR1_EL1 }, | |
2622 | /* DFAR */ | |
2623 | { AA32(LO), Op1( 0), CRn( 6), CRm( 0), Op2( 0), access_vm_reg, NULL, FAR_EL1 }, | |
2624 | /* IFAR */ | |
2625 | { AA32(HI), Op1( 0), CRn( 6), CRm( 0), Op2( 2), access_vm_reg, NULL, FAR_EL1 }, | |
4d44923b | 2626 | |
62a89c44 MZ |
2627 | /* |
2628 | * DC{C,I,CI}SW operations: | |
2629 | */ | |
2630 | { Op1( 0), CRn( 7), CRm( 6), Op2( 2), access_dcsw }, | |
2631 | { Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw }, | |
2632 | { Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw }, | |
4d44923b | 2633 | |
7609c125 | 2634 | /* PMU */ |
a9e192cd AE |
2635 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 12, 0), .access = access_pmcr }, |
2636 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 12, 1), .access = access_pmcnten }, | |
2637 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 12, 2), .access = access_pmcnten }, | |
2638 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 12, 3), .access = access_pmovs }, | |
2639 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 12, 4), .access = access_pmswinc }, | |
2640 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 12, 5), .access = access_pmselr }, | |
2641 | { CP15_PMU_SYS_REG(LO, 0, 9, 12, 6), .access = access_pmceid }, | |
2642 | { CP15_PMU_SYS_REG(LO, 0, 9, 12, 7), .access = access_pmceid }, | |
2643 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 13, 0), .access = access_pmu_evcntr }, | |
2644 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 13, 1), .access = access_pmu_evtyper }, | |
2645 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 13, 2), .access = access_pmu_evcntr }, | |
2646 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 14, 0), .access = access_pmuserenr }, | |
2647 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 14, 1), .access = access_pminten }, | |
2648 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 14, 2), .access = access_pminten }, | |
2649 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 14, 3), .access = access_pmovs }, | |
2650 | { CP15_PMU_SYS_REG(HI, 0, 9, 14, 4), .access = access_pmceid }, | |
2651 | { CP15_PMU_SYS_REG(HI, 0, 9, 14, 5), .access = access_pmceid }, | |
46081078 | 2652 | /* PMMIR */ |
a9e192cd | 2653 | { CP15_PMU_SYS_REG(DIRECT, 0, 9, 14, 6), .access = trap_raz_wi }, |
4d44923b | 2654 | |
b1ea1d76 MZ |
2655 | /* PRRR/MAIR0 */ |
2656 | { AA32(LO), Op1( 0), CRn(10), CRm( 2), Op2( 0), access_vm_reg, NULL, MAIR_EL1 }, | |
2657 | /* NMRR/MAIR1 */ | |
2658 | { AA32(HI), Op1( 0), CRn(10), CRm( 2), Op2( 1), access_vm_reg, NULL, MAIR_EL1 }, | |
2659 | /* AMAIR0 */ | |
2660 | { AA32(LO), Op1( 0), CRn(10), CRm( 3), Op2( 0), access_vm_reg, NULL, AMAIR_EL1 }, | |
2661 | /* AMAIR1 */ | |
2662 | { AA32(HI), Op1( 0), CRn(10), CRm( 3), Op2( 1), access_vm_reg, NULL, AMAIR_EL1 }, | |
db7dedd0 CD |
2663 | |
2664 | /* ICC_SRE */ | |
f7f6f2d9 | 2665 | { Op1( 0), CRn(12), CRm(12), Op2( 5), access_gic_sre }, |
db7dedd0 | 2666 | |
b1ea1d76 | 2667 | { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, CONTEXTIDR_EL1 }, |
051ff581 | 2668 | |
84135d3d AP |
2669 | /* Arch Tmers */ |
2670 | { SYS_DESC(SYS_AARCH32_CNTP_TVAL), access_arch_timer }, | |
2671 | { SYS_DESC(SYS_AARCH32_CNTP_CTL), access_arch_timer }, | |
eac137b4 | 2672 | |
051ff581 SZ |
2673 | /* PMEVCNTRn */ |
2674 | PMU_PMEVCNTR(0), | |
2675 | PMU_PMEVCNTR(1), | |
2676 | PMU_PMEVCNTR(2), | |
2677 | PMU_PMEVCNTR(3), | |
2678 | PMU_PMEVCNTR(4), | |
2679 | PMU_PMEVCNTR(5), | |
2680 | PMU_PMEVCNTR(6), | |
2681 | PMU_PMEVCNTR(7), | |
2682 | PMU_PMEVCNTR(8), | |
2683 | PMU_PMEVCNTR(9), | |
2684 | PMU_PMEVCNTR(10), | |
2685 | PMU_PMEVCNTR(11), | |
2686 | PMU_PMEVCNTR(12), | |
2687 | PMU_PMEVCNTR(13), | |
2688 | PMU_PMEVCNTR(14), | |
2689 | PMU_PMEVCNTR(15), | |
2690 | PMU_PMEVCNTR(16), | |
2691 | PMU_PMEVCNTR(17), | |
2692 | PMU_PMEVCNTR(18), | |
2693 | PMU_PMEVCNTR(19), | |
2694 | PMU_PMEVCNTR(20), | |
2695 | PMU_PMEVCNTR(21), | |
2696 | PMU_PMEVCNTR(22), | |
2697 | PMU_PMEVCNTR(23), | |
2698 | PMU_PMEVCNTR(24), | |
2699 | PMU_PMEVCNTR(25), | |
2700 | PMU_PMEVCNTR(26), | |
2701 | PMU_PMEVCNTR(27), | |
2702 | PMU_PMEVCNTR(28), | |
2703 | PMU_PMEVCNTR(29), | |
2704 | PMU_PMEVCNTR(30), | |
9feb21ac SZ |
2705 | /* PMEVTYPERn */ |
2706 | PMU_PMEVTYPER(0), | |
2707 | PMU_PMEVTYPER(1), | |
2708 | PMU_PMEVTYPER(2), | |
2709 | PMU_PMEVTYPER(3), | |
2710 | PMU_PMEVTYPER(4), | |
2711 | PMU_PMEVTYPER(5), | |
2712 | PMU_PMEVTYPER(6), | |
2713 | PMU_PMEVTYPER(7), | |
2714 | PMU_PMEVTYPER(8), | |
2715 | PMU_PMEVTYPER(9), | |
2716 | PMU_PMEVTYPER(10), | |
2717 | PMU_PMEVTYPER(11), | |
2718 | PMU_PMEVTYPER(12), | |
2719 | PMU_PMEVTYPER(13), | |
2720 | PMU_PMEVTYPER(14), | |
2721 | PMU_PMEVTYPER(15), | |
2722 | PMU_PMEVTYPER(16), | |
2723 | PMU_PMEVTYPER(17), | |
2724 | PMU_PMEVTYPER(18), | |
2725 | PMU_PMEVTYPER(19), | |
2726 | PMU_PMEVTYPER(20), | |
2727 | PMU_PMEVTYPER(21), | |
2728 | PMU_PMEVTYPER(22), | |
2729 | PMU_PMEVTYPER(23), | |
2730 | PMU_PMEVTYPER(24), | |
2731 | PMU_PMEVTYPER(25), | |
2732 | PMU_PMEVTYPER(26), | |
2733 | PMU_PMEVTYPER(27), | |
2734 | PMU_PMEVTYPER(28), | |
2735 | PMU_PMEVTYPER(29), | |
2736 | PMU_PMEVTYPER(30), | |
2737 | /* PMCCFILTR */ | |
a9e192cd | 2738 | { CP15_PMU_SYS_REG(DIRECT, 0, 14, 15, 7), .access = access_pmu_evtyper }, |
f7f2b15c AB |
2739 | |
2740 | { Op1(1), CRn( 0), CRm( 0), Op2(0), access_ccsidr }, | |
2741 | { Op1(1), CRn( 0), CRm( 0), Op2(1), access_clidr }, | |
bf48040c AO |
2742 | |
2743 | /* CCSIDR2 */ | |
2744 | { Op1(1), CRn( 0), CRm( 0), Op2(2), undef_access }, | |
2745 | ||
b1ea1d76 | 2746 | { Op1(2), CRn( 0), CRm( 0), Op2(0), access_csselr, NULL, CSSELR_EL1 }, |
a9866ba0 MZ |
2747 | }; |
2748 | ||
2749 | static const struct sys_reg_desc cp15_64_regs[] = { | |
b1ea1d76 | 2750 | { Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, TTBR0_EL1 }, |
a9e192cd | 2751 | { CP15_PMU_SYS_REG(DIRECT, 0, 0, 9, 0), .access = access_pmu_evcntr }, |
03bd646d | 2752 | { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, /* ICC_SGI1R */ |
c605ee24 | 2753 | { SYS_DESC(SYS_AARCH32_CNTPCT), access_arch_timer }, |
b1ea1d76 | 2754 | { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, TTBR1_EL1 }, |
03bd646d MZ |
2755 | { Op1( 1), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, /* ICC_ASGI1R */ |
2756 | { Op1( 2), CRn( 0), CRm(12), Op2( 0), access_gic_sgi }, /* ICC_SGI0R */ | |
84135d3d | 2757 | { SYS_DESC(SYS_AARCH32_CNTP_CVAL), access_arch_timer }, |
a6610435 | 2758 | { SYS_DESC(SYS_AARCH32_CNTPCTSS), access_arch_timer }, |
7c8c5e6a MZ |
2759 | }; |
2760 | ||
f1f0c0cf AE |
2761 | static bool check_sysreg_table(const struct sys_reg_desc *table, unsigned int n, |
2762 | bool is_32) | |
bb44a8db MZ |
2763 | { |
2764 | unsigned int i; | |
2765 | ||
2766 | for (i = 0; i < n; i++) { | |
2767 | if (!is_32 && table[i].reg && !table[i].reset) { | |
325031d4 | 2768 | kvm_err("sys_reg table %pS entry %d lacks reset\n", &table[i], i); |
f1f0c0cf | 2769 | return false; |
bb44a8db MZ |
2770 | } |
2771 | ||
2772 | if (i && cmp_sys_reg(&table[i-1], &table[i]) >= 0) { | |
325031d4 | 2773 | kvm_err("sys_reg table %pS entry %d out of order\n", &table[i - 1], i - 1); |
f1f0c0cf | 2774 | return false; |
bb44a8db MZ |
2775 | } |
2776 | } | |
2777 | ||
f1f0c0cf | 2778 | return true; |
bb44a8db MZ |
2779 | } |
2780 | ||
74cc7e0c | 2781 | int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu) |
62a89c44 MZ |
2782 | { |
2783 | kvm_inject_undefined(vcpu); | |
2784 | return 1; | |
2785 | } | |
2786 | ||
e70b9522 MZ |
2787 | static void perform_access(struct kvm_vcpu *vcpu, |
2788 | struct sys_reg_params *params, | |
2789 | const struct sys_reg_desc *r) | |
2790 | { | |
599d79dc MZ |
2791 | trace_kvm_sys_access(*vcpu_pc(vcpu), params, r); |
2792 | ||
7f34e409 | 2793 | /* Check for regs disabled by runtime config */ |
01fe5ace | 2794 | if (sysreg_hidden(vcpu, r)) { |
7f34e409 DM |
2795 | kvm_inject_undefined(vcpu); |
2796 | return; | |
2797 | } | |
2798 | ||
e70b9522 MZ |
2799 | /* |
2800 | * Not having an accessor means that we have configured a trap | |
2801 | * that we don't know how to handle. This certainly qualifies | |
2802 | * as a gross bug that should be fixed right away. | |
2803 | */ | |
2804 | BUG_ON(!r->access); | |
2805 | ||
2806 | /* Skip instruction if instructed so */ | |
2807 | if (likely(r->access(vcpu, params, r))) | |
cdb5e02e | 2808 | kvm_incr_pc(vcpu); |
e70b9522 MZ |
2809 | } |
2810 | ||
72564016 MZ |
2811 | /* |
2812 | * emulate_cp -- tries to match a sys_reg access in a handling table, and | |
2813 | * call the corresponding trap handler. | |
2814 | * | |
2815 | * @params: pointer to the descriptor of the access | |
2816 | * @table: array of trap descriptors | |
2817 | * @num: size of the trap descriptor array | |
2818 | * | |
001bb819 | 2819 | * Return true if the access has been handled, false if not. |
72564016 | 2820 | */ |
001bb819 OU |
2821 | static bool emulate_cp(struct kvm_vcpu *vcpu, |
2822 | struct sys_reg_params *params, | |
2823 | const struct sys_reg_desc *table, | |
2824 | size_t num) | |
62a89c44 | 2825 | { |
72564016 | 2826 | const struct sys_reg_desc *r; |
62a89c44 | 2827 | |
72564016 | 2828 | if (!table) |
001bb819 | 2829 | return false; /* Not handled */ |
62a89c44 | 2830 | |
62a89c44 | 2831 | r = find_reg(params, table, num); |
62a89c44 | 2832 | |
72564016 | 2833 | if (r) { |
e70b9522 | 2834 | perform_access(vcpu, params, r); |
001bb819 | 2835 | return true; |
72564016 MZ |
2836 | } |
2837 | ||
2838 | /* Not handled */ | |
001bb819 | 2839 | return false; |
72564016 MZ |
2840 | } |
2841 | ||
2842 | static void unhandled_cp_access(struct kvm_vcpu *vcpu, | |
2843 | struct sys_reg_params *params) | |
2844 | { | |
3a949f4c | 2845 | u8 esr_ec = kvm_vcpu_trap_get_class(vcpu); |
40c4f8d2 | 2846 | int cp = -1; |
72564016 | 2847 | |
3a949f4c | 2848 | switch (esr_ec) { |
c6d01a94 MR |
2849 | case ESR_ELx_EC_CP15_32: |
2850 | case ESR_ELx_EC_CP15_64: | |
72564016 MZ |
2851 | cp = 15; |
2852 | break; | |
c6d01a94 MR |
2853 | case ESR_ELx_EC_CP14_MR: |
2854 | case ESR_ELx_EC_CP14_64: | |
72564016 MZ |
2855 | cp = 14; |
2856 | break; | |
2857 | default: | |
40c4f8d2 | 2858 | WARN_ON(1); |
62a89c44 MZ |
2859 | } |
2860 | ||
bf4b96bb MR |
2861 | print_sys_reg_msg(params, |
2862 | "Unsupported guest CP%d access at: %08lx [%08lx]\n", | |
2863 | cp, *vcpu_pc(vcpu), *vcpu_cpsr(vcpu)); | |
62a89c44 MZ |
2864 | kvm_inject_undefined(vcpu); |
2865 | } | |
2866 | ||
2867 | /** | |
7769db90 | 2868 | * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP14/CP15 access |
62a89c44 MZ |
2869 | * @vcpu: The VCPU pointer |
2870 | * @run: The kvm_run struct | |
2871 | */ | |
72564016 MZ |
2872 | static int kvm_handle_cp_64(struct kvm_vcpu *vcpu, |
2873 | const struct sys_reg_desc *global, | |
dcaffa7b | 2874 | size_t nr_global) |
62a89c44 MZ |
2875 | { |
2876 | struct sys_reg_params params; | |
0b12620f | 2877 | u64 esr = kvm_vcpu_get_esr(vcpu); |
c667186f | 2878 | int Rt = kvm_vcpu_sys_get_rt(vcpu); |
3a949f4c | 2879 | int Rt2 = (esr >> 10) & 0x1f; |
62a89c44 | 2880 | |
3a949f4c GS |
2881 | params.CRm = (esr >> 1) & 0xf; |
2882 | params.is_write = ((esr & 1) == 0); | |
62a89c44 MZ |
2883 | |
2884 | params.Op0 = 0; | |
3a949f4c | 2885 | params.Op1 = (esr >> 16) & 0xf; |
62a89c44 MZ |
2886 | params.Op2 = 0; |
2887 | params.CRn = 0; | |
2888 | ||
2889 | /* | |
2ec5be3d | 2890 | * Make a 64-bit value out of Rt and Rt2. As we use the same trap |
62a89c44 MZ |
2891 | * backends between AArch32 and AArch64, we get away with it. |
2892 | */ | |
2893 | if (params.is_write) { | |
2ec5be3d PF |
2894 | params.regval = vcpu_get_reg(vcpu, Rt) & 0xffffffff; |
2895 | params.regval |= vcpu_get_reg(vcpu, Rt2) << 32; | |
62a89c44 MZ |
2896 | } |
2897 | ||
b6b7a806 | 2898 | /* |
dcaffa7b | 2899 | * If the table contains a handler, handle the |
b6b7a806 MZ |
2900 | * potential register operation in the case of a read and return |
2901 | * with success. | |
2902 | */ | |
001bb819 | 2903 | if (emulate_cp(vcpu, ¶ms, global, nr_global)) { |
b6b7a806 MZ |
2904 | /* Split up the value between registers for the read side */ |
2905 | if (!params.is_write) { | |
2906 | vcpu_set_reg(vcpu, Rt, lower_32_bits(params.regval)); | |
2907 | vcpu_set_reg(vcpu, Rt2, upper_32_bits(params.regval)); | |
2908 | } | |
62a89c44 | 2909 | |
b6b7a806 | 2910 | return 1; |
62a89c44 MZ |
2911 | } |
2912 | ||
b6b7a806 | 2913 | unhandled_cp_access(vcpu, ¶ms); |
62a89c44 MZ |
2914 | return 1; |
2915 | } | |
2916 | ||
e6519766 OU |
2917 | static bool emulate_sys_reg(struct kvm_vcpu *vcpu, struct sys_reg_params *params); |
2918 | ||
9369bc5c OU |
2919 | /* |
2920 | * The CP10 ID registers are architecturally mapped to AArch64 feature | |
2921 | * registers. Abuse that fact so we can rely on the AArch64 handler for accesses | |
2922 | * from AArch32. | |
2923 | */ | |
ee87a9bd | 2924 | static bool kvm_esr_cp10_id_to_sys64(u64 esr, struct sys_reg_params *params) |
9369bc5c OU |
2925 | { |
2926 | u8 reg_id = (esr >> 10) & 0xf; | |
2927 | bool valid; | |
2928 | ||
2929 | params->is_write = ((esr & 1) == 0); | |
2930 | params->Op0 = 3; | |
2931 | params->Op1 = 0; | |
2932 | params->CRn = 0; | |
2933 | params->CRm = 3; | |
2934 | ||
2935 | /* CP10 ID registers are read-only */ | |
2936 | valid = !params->is_write; | |
2937 | ||
2938 | switch (reg_id) { | |
2939 | /* MVFR0 */ | |
2940 | case 0b0111: | |
2941 | params->Op2 = 0; | |
2942 | break; | |
2943 | /* MVFR1 */ | |
2944 | case 0b0110: | |
2945 | params->Op2 = 1; | |
2946 | break; | |
2947 | /* MVFR2 */ | |
2948 | case 0b0101: | |
2949 | params->Op2 = 2; | |
2950 | break; | |
2951 | default: | |
2952 | valid = false; | |
2953 | } | |
2954 | ||
2955 | if (valid) | |
2956 | return true; | |
2957 | ||
2958 | kvm_pr_unimpl("Unhandled cp10 register %s: %u\n", | |
2959 | params->is_write ? "write" : "read", reg_id); | |
2960 | return false; | |
2961 | } | |
2962 | ||
2963 | /** | |
2964 | * kvm_handle_cp10_id() - Handles a VMRS trap on guest access to a 'Media and | |
2965 | * VFP Register' from AArch32. | |
2966 | * @vcpu: The vCPU pointer | |
2967 | * | |
2968 | * MVFR{0-2} are architecturally mapped to the AArch64 MVFR{0-2}_EL1 registers. | |
2969 | * Work out the correct AArch64 system register encoding and reroute to the | |
2970 | * AArch64 system register emulation. | |
2971 | */ | |
2972 | int kvm_handle_cp10_id(struct kvm_vcpu *vcpu) | |
2973 | { | |
2974 | int Rt = kvm_vcpu_sys_get_rt(vcpu); | |
ee87a9bd | 2975 | u64 esr = kvm_vcpu_get_esr(vcpu); |
9369bc5c OU |
2976 | struct sys_reg_params params; |
2977 | ||
2978 | /* UNDEF on any unhandled register access */ | |
2979 | if (!kvm_esr_cp10_id_to_sys64(esr, ¶ms)) { | |
2980 | kvm_inject_undefined(vcpu); | |
2981 | return 1; | |
2982 | } | |
2983 | ||
2984 | if (emulate_sys_reg(vcpu, ¶ms)) | |
2985 | vcpu_set_reg(vcpu, Rt, params.regval); | |
2986 | ||
2987 | return 1; | |
2988 | } | |
2989 | ||
e6519766 OU |
2990 | /** |
2991 | * kvm_emulate_cp15_id_reg() - Handles an MRC trap on a guest CP15 access where | |
2992 | * CRn=0, which corresponds to the AArch32 feature | |
2993 | * registers. | |
2994 | * @vcpu: the vCPU pointer | |
2995 | * @params: the system register access parameters. | |
2996 | * | |
2997 | * Our cp15 system register tables do not enumerate the AArch32 feature | |
2998 | * registers. Conveniently, our AArch64 table does, and the AArch32 system | |
2999 | * register encoding can be trivially remapped into the AArch64 for the feature | |
3000 | * registers: Append op0=3, leaving op1, CRn, CRm, and op2 the same. | |
3001 | * | |
3002 | * According to DDI0487G.b G7.3.1, paragraph "Behavior of VMSAv8-32 32-bit | |
3003 | * System registers with (coproc=0b1111, CRn==c0)", read accesses from this | |
3004 | * range are either UNKNOWN or RES0. Rerouting remains architectural as we | |
3005 | * treat undefined registers in this range as RAZ. | |
3006 | */ | |
3007 | static int kvm_emulate_cp15_id_reg(struct kvm_vcpu *vcpu, | |
3008 | struct sys_reg_params *params) | |
3009 | { | |
3010 | int Rt = kvm_vcpu_sys_get_rt(vcpu); | |
3011 | ||
3012 | /* Treat impossible writes to RO registers as UNDEFINED */ | |
3013 | if (params->is_write) { | |
3014 | unhandled_cp_access(vcpu, params); | |
3015 | return 1; | |
3016 | } | |
3017 | ||
3018 | params->Op0 = 3; | |
3019 | ||
3020 | /* | |
3021 | * All registers where CRm > 3 are known to be UNKNOWN/RAZ from AArch32. | |
3022 | * Avoid conflicting with future expansion of AArch64 feature registers | |
3023 | * and simply treat them as RAZ here. | |
3024 | */ | |
3025 | if (params->CRm > 3) | |
3026 | params->regval = 0; | |
3027 | else if (!emulate_sys_reg(vcpu, params)) | |
3028 | return 1; | |
3029 | ||
3030 | vcpu_set_reg(vcpu, Rt, params->regval); | |
3031 | return 1; | |
3032 | } | |
3033 | ||
62a89c44 | 3034 | /** |
7769db90 | 3035 | * kvm_handle_cp_32 -- handles a mrc/mcr trap on a guest CP14/CP15 access |
62a89c44 MZ |
3036 | * @vcpu: The VCPU pointer |
3037 | * @run: The kvm_run struct | |
3038 | */ | |
72564016 | 3039 | static int kvm_handle_cp_32(struct kvm_vcpu *vcpu, |
e6519766 | 3040 | struct sys_reg_params *params, |
72564016 | 3041 | const struct sys_reg_desc *global, |
dcaffa7b | 3042 | size_t nr_global) |
62a89c44 | 3043 | { |
c667186f | 3044 | int Rt = kvm_vcpu_sys_get_rt(vcpu); |
62a89c44 | 3045 | |
e6519766 | 3046 | params->regval = vcpu_get_reg(vcpu, Rt); |
62a89c44 | 3047 | |
e6519766 OU |
3048 | if (emulate_cp(vcpu, params, global, nr_global)) { |
3049 | if (!params->is_write) | |
3050 | vcpu_set_reg(vcpu, Rt, params->regval); | |
72564016 | 3051 | return 1; |
2ec5be3d | 3052 | } |
72564016 | 3053 | |
e6519766 | 3054 | unhandled_cp_access(vcpu, params); |
62a89c44 MZ |
3055 | return 1; |
3056 | } | |
3057 | ||
74cc7e0c | 3058 | int kvm_handle_cp15_64(struct kvm_vcpu *vcpu) |
72564016 | 3059 | { |
dcaffa7b | 3060 | return kvm_handle_cp_64(vcpu, cp15_64_regs, ARRAY_SIZE(cp15_64_regs)); |
72564016 MZ |
3061 | } |
3062 | ||
74cc7e0c | 3063 | int kvm_handle_cp15_32(struct kvm_vcpu *vcpu) |
72564016 | 3064 | { |
e6519766 OU |
3065 | struct sys_reg_params params; |
3066 | ||
3067 | params = esr_cp1x_32_to_params(kvm_vcpu_get_esr(vcpu)); | |
3068 | ||
3069 | /* | |
3070 | * Certain AArch32 ID registers are handled by rerouting to the AArch64 | |
3071 | * system register table. Registers in the ID range where CRm=0 are | |
3072 | * excluded from this scheme as they do not trivially map into AArch64 | |
3073 | * system register encodings. | |
3074 | */ | |
3075 | if (params.Op1 == 0 && params.CRn == 0 && params.CRm) | |
3076 | return kvm_emulate_cp15_id_reg(vcpu, ¶ms); | |
3077 | ||
3078 | return kvm_handle_cp_32(vcpu, ¶ms, cp15_regs, ARRAY_SIZE(cp15_regs)); | |
72564016 MZ |
3079 | } |
3080 | ||
74cc7e0c | 3081 | int kvm_handle_cp14_64(struct kvm_vcpu *vcpu) |
72564016 | 3082 | { |
dcaffa7b | 3083 | return kvm_handle_cp_64(vcpu, cp14_64_regs, ARRAY_SIZE(cp14_64_regs)); |
72564016 MZ |
3084 | } |
3085 | ||
74cc7e0c | 3086 | int kvm_handle_cp14_32(struct kvm_vcpu *vcpu) |
72564016 | 3087 | { |
e6519766 OU |
3088 | struct sys_reg_params params; |
3089 | ||
3090 | params = esr_cp1x_32_to_params(kvm_vcpu_get_esr(vcpu)); | |
3091 | ||
3092 | return kvm_handle_cp_32(vcpu, ¶ms, cp14_regs, ARRAY_SIZE(cp14_regs)); | |
72564016 MZ |
3093 | } |
3094 | ||
54ad68b7 MR |
3095 | static bool is_imp_def_sys_reg(struct sys_reg_params *params) |
3096 | { | |
3097 | // See ARM DDI 0487E.a, section D12.3.2 | |
3098 | return params->Op0 == 3 && (params->CRn & 0b1011) == 0b1011; | |
3099 | } | |
3100 | ||
28eda7b5 OU |
3101 | /** |
3102 | * emulate_sys_reg - Emulate a guest access to an AArch64 system register | |
3103 | * @vcpu: The VCPU pointer | |
3104 | * @params: Decoded system register parameters | |
3105 | * | |
3106 | * Return: true if the system register access was successful, false otherwise. | |
3107 | */ | |
3108 | static bool emulate_sys_reg(struct kvm_vcpu *vcpu, | |
3fec037d | 3109 | struct sys_reg_params *params) |
7c8c5e6a | 3110 | { |
dcaffa7b | 3111 | const struct sys_reg_desc *r; |
7c8c5e6a | 3112 | |
dcaffa7b | 3113 | r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs)); |
7c8c5e6a MZ |
3114 | |
3115 | if (likely(r)) { | |
e70b9522 | 3116 | perform_access(vcpu, params, r); |
28eda7b5 OU |
3117 | return true; |
3118 | } | |
3119 | ||
3120 | if (is_imp_def_sys_reg(params)) { | |
54ad68b7 | 3121 | kvm_inject_undefined(vcpu); |
7c8c5e6a | 3122 | } else { |
bf4b96bb MR |
3123 | print_sys_reg_msg(params, |
3124 | "Unsupported guest sys_reg access at: %lx [%08lx]\n", | |
3125 | *vcpu_pc(vcpu), *vcpu_cpsr(vcpu)); | |
e70b9522 | 3126 | kvm_inject_undefined(vcpu); |
7c8c5e6a | 3127 | } |
28eda7b5 | 3128 | return false; |
7c8c5e6a MZ |
3129 | } |
3130 | ||
47334146 JZ |
3131 | static void kvm_reset_id_regs(struct kvm_vcpu *vcpu) |
3132 | { | |
3133 | const struct sys_reg_desc *idreg = first_idreg; | |
3134 | u32 id = reg_to_encoding(idreg); | |
3135 | struct kvm *kvm = vcpu->kvm; | |
3136 | ||
3137 | if (test_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags)) | |
3138 | return; | |
3139 | ||
3140 | lockdep_assert_held(&kvm->arch.config_lock); | |
3141 | ||
3142 | /* Initialize all idregs */ | |
3143 | while (is_id_reg(id)) { | |
3144 | IDREG(kvm, id) = idreg->reset(vcpu, idreg); | |
3145 | ||
3146 | idreg++; | |
3147 | id = reg_to_encoding(idreg); | |
3148 | } | |
3149 | ||
3150 | set_bit(KVM_ARCH_FLAG_ID_REGS_INITIALIZED, &kvm->arch.flags); | |
3151 | } | |
3152 | ||
750ed566 JM |
3153 | /** |
3154 | * kvm_reset_sys_regs - sets system registers to reset value | |
3155 | * @vcpu: The VCPU pointer | |
3156 | * | |
3157 | * This function finds the right table above and sets the registers on the | |
3158 | * virtual CPU struct to their architecturally defined reset values. | |
3159 | */ | |
3160 | void kvm_reset_sys_regs(struct kvm_vcpu *vcpu) | |
7c8c5e6a MZ |
3161 | { |
3162 | unsigned long i; | |
3163 | ||
47334146 JZ |
3164 | kvm_reset_id_regs(vcpu); |
3165 | ||
3166 | for (i = 0; i < ARRAY_SIZE(sys_reg_descs); i++) { | |
3167 | const struct sys_reg_desc *r = &sys_reg_descs[i]; | |
3168 | ||
3169 | if (is_id_reg(reg_to_encoding(r))) | |
3170 | continue; | |
3171 | ||
3172 | if (r->reset) | |
3173 | r->reset(vcpu, r); | |
3174 | } | |
7c8c5e6a MZ |
3175 | } |
3176 | ||
3177 | /** | |
3178 | * kvm_handle_sys_reg -- handles a mrs/msr trap on a guest sys_reg access | |
3179 | * @vcpu: The VCPU pointer | |
7c8c5e6a | 3180 | */ |
74cc7e0c | 3181 | int kvm_handle_sys_reg(struct kvm_vcpu *vcpu) |
7c8c5e6a MZ |
3182 | { |
3183 | struct sys_reg_params params; | |
3a949f4c | 3184 | unsigned long esr = kvm_vcpu_get_esr(vcpu); |
c667186f | 3185 | int Rt = kvm_vcpu_sys_get_rt(vcpu); |
7c8c5e6a | 3186 | |
eef8c85a AB |
3187 | trace_kvm_handle_sys_reg(esr); |
3188 | ||
e58ec47b MZ |
3189 | if (__check_nv_sr_forward(vcpu)) |
3190 | return 1; | |
3191 | ||
f76f89e2 | 3192 | params = esr_sys64_to_params(esr); |
2ec5be3d | 3193 | params.regval = vcpu_get_reg(vcpu, Rt); |
7c8c5e6a | 3194 | |
28eda7b5 OU |
3195 | if (!emulate_sys_reg(vcpu, ¶ms)) |
3196 | return 1; | |
2ec5be3d PF |
3197 | |
3198 | if (!params.is_write) | |
3199 | vcpu_set_reg(vcpu, Rt, params.regval); | |
28eda7b5 | 3200 | return 1; |
7c8c5e6a MZ |
3201 | } |
3202 | ||
3203 | /****************************************************************************** | |
3204 | * Userspace API | |
3205 | *****************************************************************************/ | |
3206 | ||
3207 | static bool index_to_params(u64 id, struct sys_reg_params *params) | |
3208 | { | |
3209 | switch (id & KVM_REG_SIZE_MASK) { | |
3210 | case KVM_REG_SIZE_U64: | |
3211 | /* Any unused index bits means it's not valid. */ | |
3212 | if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK | |
3213 | | KVM_REG_ARM_COPROC_MASK | |
3214 | | KVM_REG_ARM64_SYSREG_OP0_MASK | |
3215 | | KVM_REG_ARM64_SYSREG_OP1_MASK | |
3216 | | KVM_REG_ARM64_SYSREG_CRN_MASK | |
3217 | | KVM_REG_ARM64_SYSREG_CRM_MASK | |
3218 | | KVM_REG_ARM64_SYSREG_OP2_MASK)) | |
3219 | return false; | |
3220 | params->Op0 = ((id & KVM_REG_ARM64_SYSREG_OP0_MASK) | |
3221 | >> KVM_REG_ARM64_SYSREG_OP0_SHIFT); | |
3222 | params->Op1 = ((id & KVM_REG_ARM64_SYSREG_OP1_MASK) | |
3223 | >> KVM_REG_ARM64_SYSREG_OP1_SHIFT); | |
3224 | params->CRn = ((id & KVM_REG_ARM64_SYSREG_CRN_MASK) | |
3225 | >> KVM_REG_ARM64_SYSREG_CRN_SHIFT); | |
3226 | params->CRm = ((id & KVM_REG_ARM64_SYSREG_CRM_MASK) | |
3227 | >> KVM_REG_ARM64_SYSREG_CRM_SHIFT); | |
3228 | params->Op2 = ((id & KVM_REG_ARM64_SYSREG_OP2_MASK) | |
3229 | >> KVM_REG_ARM64_SYSREG_OP2_SHIFT); | |
3230 | return true; | |
3231 | default: | |
3232 | return false; | |
3233 | } | |
3234 | } | |
3235 | ||
da8d120f MZ |
3236 | const struct sys_reg_desc *get_reg_by_id(u64 id, |
3237 | const struct sys_reg_desc table[], | |
3238 | unsigned int num) | |
4b927b94 | 3239 | { |
da8d120f MZ |
3240 | struct sys_reg_params params; |
3241 | ||
3242 | if (!index_to_params(id, ¶ms)) | |
4b927b94 VK |
3243 | return NULL; |
3244 | ||
da8d120f | 3245 | return find_reg(¶ms, table, num); |
4b927b94 VK |
3246 | } |
3247 | ||
7c8c5e6a | 3248 | /* Decode an index value, and find the sys_reg_desc entry. */ |
ba23aec9 MZ |
3249 | static const struct sys_reg_desc * |
3250 | id_to_sys_reg_desc(struct kvm_vcpu *vcpu, u64 id, | |
3251 | const struct sys_reg_desc table[], unsigned int num) | |
3252 | ||
7c8c5e6a | 3253 | { |
dcaffa7b | 3254 | const struct sys_reg_desc *r; |
7c8c5e6a MZ |
3255 | |
3256 | /* We only do sys_reg for now. */ | |
3257 | if ((id & KVM_REG_ARM_COPROC_MASK) != KVM_REG_ARM64_SYSREG) | |
3258 | return NULL; | |
3259 | ||
ba23aec9 | 3260 | r = get_reg_by_id(id, table, num); |
7c8c5e6a | 3261 | |
93390c0a | 3262 | /* Not saved in the sys_reg array and not otherwise accessible? */ |
ba23aec9 | 3263 | if (r && (!(r->reg || r->get_user) || sysreg_hidden(vcpu, r))) |
7c8c5e6a MZ |
3264 | r = NULL; |
3265 | ||
3266 | return r; | |
3267 | } | |
3268 | ||
3269 | /* | |
3270 | * These are the invariant sys_reg registers: we let the guest see the | |
3271 | * host versions of these, so they're part of the guest state. | |
3272 | * | |
3273 | * A future CPU may provide a mechanism to present different values to | |
3274 | * the guest, or a future kvm may trap them. | |
3275 | */ | |
3276 | ||
3277 | #define FUNCTION_INVARIANT(reg) \ | |
d86cde6e | 3278 | static u64 get_##reg(struct kvm_vcpu *v, \ |
7c8c5e6a MZ |
3279 | const struct sys_reg_desc *r) \ |
3280 | { \ | |
1f3d8699 | 3281 | ((struct sys_reg_desc *)r)->val = read_sysreg(reg); \ |
d86cde6e | 3282 | return ((struct sys_reg_desc *)r)->val; \ |
7c8c5e6a MZ |
3283 | } |
3284 | ||
3285 | FUNCTION_INVARIANT(midr_el1) | |
7c8c5e6a | 3286 | FUNCTION_INVARIANT(revidr_el1) |
7c8c5e6a MZ |
3287 | FUNCTION_INVARIANT(aidr_el1) |
3288 | ||
d86cde6e | 3289 | static u64 get_ctr_el0(struct kvm_vcpu *v, const struct sys_reg_desc *r) |
f7f2b15c AB |
3290 | { |
3291 | ((struct sys_reg_desc *)r)->val = read_sanitised_ftr_reg(SYS_CTR_EL0); | |
d86cde6e | 3292 | return ((struct sys_reg_desc *)r)->val; |
f7f2b15c AB |
3293 | } |
3294 | ||
7c8c5e6a | 3295 | /* ->val is filled in by kvm_sys_reg_table_init() */ |
8d20bd63 | 3296 | static struct sys_reg_desc invariant_sys_regs[] __ro_after_init = { |
0d449541 MR |
3297 | { SYS_DESC(SYS_MIDR_EL1), NULL, get_midr_el1 }, |
3298 | { SYS_DESC(SYS_REVIDR_EL1), NULL, get_revidr_el1 }, | |
0d449541 MR |
3299 | { SYS_DESC(SYS_AIDR_EL1), NULL, get_aidr_el1 }, |
3300 | { SYS_DESC(SYS_CTR_EL0), NULL, get_ctr_el0 }, | |
7c8c5e6a MZ |
3301 | }; |
3302 | ||
5a420ed9 | 3303 | static int get_invariant_sys_reg(u64 id, u64 __user *uaddr) |
7c8c5e6a | 3304 | { |
7c8c5e6a MZ |
3305 | const struct sys_reg_desc *r; |
3306 | ||
da8d120f MZ |
3307 | r = get_reg_by_id(id, invariant_sys_regs, |
3308 | ARRAY_SIZE(invariant_sys_regs)); | |
7c8c5e6a MZ |
3309 | if (!r) |
3310 | return -ENOENT; | |
3311 | ||
5a420ed9 | 3312 | return put_user(r->val, uaddr); |
7c8c5e6a MZ |
3313 | } |
3314 | ||
5a420ed9 | 3315 | static int set_invariant_sys_reg(u64 id, u64 __user *uaddr) |
7c8c5e6a | 3316 | { |
7c8c5e6a | 3317 | const struct sys_reg_desc *r; |
5a420ed9 | 3318 | u64 val; |
7c8c5e6a | 3319 | |
da8d120f MZ |
3320 | r = get_reg_by_id(id, invariant_sys_regs, |
3321 | ARRAY_SIZE(invariant_sys_regs)); | |
7c8c5e6a MZ |
3322 | if (!r) |
3323 | return -ENOENT; | |
3324 | ||
5a420ed9 MZ |
3325 | if (get_user(val, uaddr)) |
3326 | return -EFAULT; | |
7c8c5e6a MZ |
3327 | |
3328 | /* This is what we mean by invariant: you can't change it. */ | |
3329 | if (r->val != val) | |
3330 | return -EINVAL; | |
3331 | ||
3332 | return 0; | |
3333 | } | |
3334 | ||
7af0c253 | 3335 | static int demux_c15_get(struct kvm_vcpu *vcpu, u64 id, void __user *uaddr) |
7c8c5e6a MZ |
3336 | { |
3337 | u32 val; | |
3338 | u32 __user *uval = uaddr; | |
3339 | ||
3340 | /* Fail if we have unknown bits set. */ | |
3341 | if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK | |
3342 | | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1))) | |
3343 | return -ENOENT; | |
3344 | ||
3345 | switch (id & KVM_REG_ARM_DEMUX_ID_MASK) { | |
3346 | case KVM_REG_ARM_DEMUX_ID_CCSIDR: | |
3347 | if (KVM_REG_SIZE(id) != 4) | |
3348 | return -ENOENT; | |
3349 | val = (id & KVM_REG_ARM_DEMUX_VAL_MASK) | |
3350 | >> KVM_REG_ARM_DEMUX_VAL_SHIFT; | |
7af0c253 | 3351 | if (val >= CSSELR_MAX) |
7c8c5e6a MZ |
3352 | return -ENOENT; |
3353 | ||
7af0c253 | 3354 | return put_user(get_ccsidr(vcpu, val), uval); |
7c8c5e6a MZ |
3355 | default: |
3356 | return -ENOENT; | |
3357 | } | |
3358 | } | |
3359 | ||
7af0c253 | 3360 | static int demux_c15_set(struct kvm_vcpu *vcpu, u64 id, void __user *uaddr) |
7c8c5e6a MZ |
3361 | { |
3362 | u32 val, newval; | |
3363 | u32 __user *uval = uaddr; | |
3364 | ||
3365 | /* Fail if we have unknown bits set. */ | |
3366 | if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK | |
3367 | | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1))) | |
3368 | return -ENOENT; | |
3369 | ||
3370 | switch (id & KVM_REG_ARM_DEMUX_ID_MASK) { | |
3371 | case KVM_REG_ARM_DEMUX_ID_CCSIDR: | |
3372 | if (KVM_REG_SIZE(id) != 4) | |
3373 | return -ENOENT; | |
3374 | val = (id & KVM_REG_ARM_DEMUX_VAL_MASK) | |
3375 | >> KVM_REG_ARM_DEMUX_VAL_SHIFT; | |
7af0c253 | 3376 | if (val >= CSSELR_MAX) |
7c8c5e6a MZ |
3377 | return -ENOENT; |
3378 | ||
3379 | if (get_user(newval, uval)) | |
3380 | return -EFAULT; | |
3381 | ||
7af0c253 | 3382 | return set_ccsidr(vcpu, val, newval); |
7c8c5e6a MZ |
3383 | default: |
3384 | return -ENOENT; | |
3385 | } | |
3386 | } | |
3387 | ||
ba23aec9 MZ |
3388 | int kvm_sys_reg_get_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg, |
3389 | const struct sys_reg_desc table[], unsigned int num) | |
7c8c5e6a | 3390 | { |
978ceeb3 | 3391 | u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr; |
7c8c5e6a | 3392 | const struct sys_reg_desc *r; |
978ceeb3 MZ |
3393 | u64 val; |
3394 | int ret; | |
ba23aec9 MZ |
3395 | |
3396 | r = id_to_sys_reg_desc(vcpu, reg->id, table, num); | |
e6b367db | 3397 | if (!r || sysreg_hidden_user(vcpu, r)) |
ba23aec9 MZ |
3398 | return -ENOENT; |
3399 | ||
978ceeb3 MZ |
3400 | if (r->get_user) { |
3401 | ret = (r->get_user)(vcpu, r, &val); | |
3402 | } else { | |
3403 | val = __vcpu_sys_reg(vcpu, r->reg); | |
3404 | ret = 0; | |
3405 | } | |
3406 | ||
3407 | if (!ret) | |
3408 | ret = put_user(val, uaddr); | |
ba23aec9 | 3409 | |
978ceeb3 | 3410 | return ret; |
ba23aec9 MZ |
3411 | } |
3412 | ||
3413 | int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | |
3414 | { | |
7c8c5e6a | 3415 | void __user *uaddr = (void __user *)(unsigned long)reg->addr; |
1deeffb5 | 3416 | int err; |
7c8c5e6a MZ |
3417 | |
3418 | if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX) | |
7af0c253 | 3419 | return demux_c15_get(vcpu, reg->id, uaddr); |
7c8c5e6a | 3420 | |
1deeffb5 MZ |
3421 | err = get_invariant_sys_reg(reg->id, uaddr); |
3422 | if (err != -ENOENT) | |
3423 | return err; | |
7c8c5e6a | 3424 | |
ba23aec9 MZ |
3425 | return kvm_sys_reg_get_user(vcpu, reg, |
3426 | sys_reg_descs, ARRAY_SIZE(sys_reg_descs)); | |
3427 | } | |
7c8c5e6a | 3428 | |
ba23aec9 MZ |
3429 | int kvm_sys_reg_set_user(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg, |
3430 | const struct sys_reg_desc table[], unsigned int num) | |
3431 | { | |
978ceeb3 | 3432 | u64 __user *uaddr = (u64 __user *)(unsigned long)reg->addr; |
ba23aec9 | 3433 | const struct sys_reg_desc *r; |
978ceeb3 MZ |
3434 | u64 val; |
3435 | int ret; | |
3436 | ||
3437 | if (get_user(val, uaddr)) | |
3438 | return -EFAULT; | |
ba23aec9 MZ |
3439 | |
3440 | r = id_to_sys_reg_desc(vcpu, reg->id, table, num); | |
e6b367db | 3441 | if (!r || sysreg_hidden_user(vcpu, r)) |
7f34e409 DM |
3442 | return -ENOENT; |
3443 | ||
4de06e4c OU |
3444 | if (sysreg_user_write_ignore(vcpu, r)) |
3445 | return 0; | |
3446 | ||
978ceeb3 MZ |
3447 | if (r->set_user) { |
3448 | ret = (r->set_user)(vcpu, r, val); | |
3449 | } else { | |
3450 | __vcpu_sys_reg(vcpu, r->reg) = val; | |
3451 | ret = 0; | |
3452 | } | |
84e690bf | 3453 | |
978ceeb3 | 3454 | return ret; |
7c8c5e6a MZ |
3455 | } |
3456 | ||
3457 | int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg) | |
3458 | { | |
7c8c5e6a | 3459 | void __user *uaddr = (void __user *)(unsigned long)reg->addr; |
1deeffb5 | 3460 | int err; |
7c8c5e6a MZ |
3461 | |
3462 | if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX) | |
7af0c253 | 3463 | return demux_c15_set(vcpu, reg->id, uaddr); |
7c8c5e6a | 3464 | |
1deeffb5 MZ |
3465 | err = set_invariant_sys_reg(reg->id, uaddr); |
3466 | if (err != -ENOENT) | |
3467 | return err; | |
84e690bf | 3468 | |
ba23aec9 MZ |
3469 | return kvm_sys_reg_set_user(vcpu, reg, |
3470 | sys_reg_descs, ARRAY_SIZE(sys_reg_descs)); | |
7c8c5e6a MZ |
3471 | } |
3472 | ||
3473 | static unsigned int num_demux_regs(void) | |
3474 | { | |
7af0c253 | 3475 | return CSSELR_MAX; |
7c8c5e6a MZ |
3476 | } |
3477 | ||
3478 | static int write_demux_regids(u64 __user *uindices) | |
3479 | { | |
efd48cea | 3480 | u64 val = KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX; |
7c8c5e6a MZ |
3481 | unsigned int i; |
3482 | ||
3483 | val |= KVM_REG_ARM_DEMUX_ID_CCSIDR; | |
3484 | for (i = 0; i < CSSELR_MAX; i++) { | |
7c8c5e6a MZ |
3485 | if (put_user(val | i, uindices)) |
3486 | return -EFAULT; | |
3487 | uindices++; | |
3488 | } | |
3489 | return 0; | |
3490 | } | |
3491 | ||
3492 | static u64 sys_reg_to_index(const struct sys_reg_desc *reg) | |
3493 | { | |
3494 | return (KVM_REG_ARM64 | KVM_REG_SIZE_U64 | | |
3495 | KVM_REG_ARM64_SYSREG | | |
3496 | (reg->Op0 << KVM_REG_ARM64_SYSREG_OP0_SHIFT) | | |
3497 | (reg->Op1 << KVM_REG_ARM64_SYSREG_OP1_SHIFT) | | |
3498 | (reg->CRn << KVM_REG_ARM64_SYSREG_CRN_SHIFT) | | |
3499 | (reg->CRm << KVM_REG_ARM64_SYSREG_CRM_SHIFT) | | |
3500 | (reg->Op2 << KVM_REG_ARM64_SYSREG_OP2_SHIFT)); | |
3501 | } | |
3502 | ||
3503 | static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind) | |
3504 | { | |
3505 | if (!*uind) | |
3506 | return true; | |
3507 | ||
3508 | if (put_user(sys_reg_to_index(reg), *uind)) | |
3509 | return false; | |
3510 | ||
3511 | (*uind)++; | |
3512 | return true; | |
3513 | } | |
3514 | ||
7f34e409 DM |
3515 | static int walk_one_sys_reg(const struct kvm_vcpu *vcpu, |
3516 | const struct sys_reg_desc *rd, | |
93390c0a DM |
3517 | u64 __user **uind, |
3518 | unsigned int *total) | |
3519 | { | |
3520 | /* | |
3521 | * Ignore registers we trap but don't save, | |
3522 | * and for which no custom user accessor is provided. | |
3523 | */ | |
3524 | if (!(rd->reg || rd->get_user)) | |
3525 | return 0; | |
3526 | ||
e6b367db | 3527 | if (sysreg_hidden_user(vcpu, rd)) |
7f34e409 DM |
3528 | return 0; |
3529 | ||
93390c0a DM |
3530 | if (!copy_reg_to_user(rd, uind)) |
3531 | return -EFAULT; | |
3532 | ||
3533 | (*total)++; | |
3534 | return 0; | |
3535 | } | |
3536 | ||
7c8c5e6a MZ |
3537 | /* Assumed ordered tables, see kvm_sys_reg_table_init. */ |
3538 | static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind) | |
3539 | { | |
dcaffa7b | 3540 | const struct sys_reg_desc *i2, *end2; |
7c8c5e6a | 3541 | unsigned int total = 0; |
93390c0a | 3542 | int err; |
7c8c5e6a | 3543 | |
7c8c5e6a MZ |
3544 | i2 = sys_reg_descs; |
3545 | end2 = sys_reg_descs + ARRAY_SIZE(sys_reg_descs); | |
3546 | ||
dcaffa7b JM |
3547 | while (i2 != end2) { |
3548 | err = walk_one_sys_reg(vcpu, i2++, &uind, &total); | |
93390c0a DM |
3549 | if (err) |
3550 | return err; | |
7c8c5e6a MZ |
3551 | } |
3552 | return total; | |
3553 | } | |
3554 | ||
3555 | unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu) | |
3556 | { | |
3557 | return ARRAY_SIZE(invariant_sys_regs) | |
3558 | + num_demux_regs() | |
3559 | + walk_sys_regs(vcpu, (u64 __user *)NULL); | |
3560 | } | |
3561 | ||
3562 | int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices) | |
3563 | { | |
3564 | unsigned int i; | |
3565 | int err; | |
3566 | ||
3567 | /* Then give them all the invariant registers' indices. */ | |
3568 | for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) { | |
3569 | if (put_user(sys_reg_to_index(&invariant_sys_regs[i]), uindices)) | |
3570 | return -EFAULT; | |
3571 | uindices++; | |
3572 | } | |
3573 | ||
3574 | err = walk_sys_regs(vcpu, uindices); | |
3575 | if (err < 0) | |
3576 | return err; | |
3577 | uindices += err; | |
3578 | ||
3579 | return write_demux_regids(uindices); | |
3580 | } | |
3581 | ||
3f9cd0ca JZ |
3582 | #define KVM_ARM_FEATURE_ID_RANGE_INDEX(r) \ |
3583 | KVM_ARM_FEATURE_ID_RANGE_IDX(sys_reg_Op0(r), \ | |
3584 | sys_reg_Op1(r), \ | |
3585 | sys_reg_CRn(r), \ | |
3586 | sys_reg_CRm(r), \ | |
3587 | sys_reg_Op2(r)) | |
3588 | ||
3589 | static bool is_feature_id_reg(u32 encoding) | |
3590 | { | |
3591 | return (sys_reg_Op0(encoding) == 3 && | |
3592 | (sys_reg_Op1(encoding) < 2 || sys_reg_Op1(encoding) == 3) && | |
3593 | sys_reg_CRn(encoding) == 0 && | |
3594 | sys_reg_CRm(encoding) <= 7); | |
3595 | } | |
3596 | ||
3597 | int kvm_vm_ioctl_get_reg_writable_masks(struct kvm *kvm, struct reg_mask_range *range) | |
3598 | { | |
3599 | const void *zero_page = page_to_virt(ZERO_PAGE(0)); | |
3600 | u64 __user *masks = (u64 __user *)range->addr; | |
3601 | ||
3602 | /* Only feature id range is supported, reserved[13] must be zero. */ | |
3603 | if (range->range || | |
3604 | memcmp(range->reserved, zero_page, sizeof(range->reserved))) | |
3605 | return -EINVAL; | |
3606 | ||
3607 | /* Wipe the whole thing first */ | |
3608 | if (clear_user(masks, KVM_ARM_FEATURE_ID_RANGE_SIZE * sizeof(__u64))) | |
3609 | return -EFAULT; | |
3610 | ||
3611 | for (int i = 0; i < ARRAY_SIZE(sys_reg_descs); i++) { | |
3612 | const struct sys_reg_desc *reg = &sys_reg_descs[i]; | |
3613 | u32 encoding = reg_to_encoding(reg); | |
3614 | u64 val; | |
3615 | ||
3616 | if (!is_feature_id_reg(encoding) || !reg->set_user) | |
3617 | continue; | |
3618 | ||
3619 | /* | |
3620 | * For ID registers, we return the writable mask. Other feature | |
3621 | * registers return a full 64bit mask. That's not necessary | |
3622 | * compliant with a given revision of the architecture, but the | |
3623 | * RES0/RES1 definitions allow us to do that. | |
3624 | */ | |
3625 | if (is_id_reg(encoding)) { | |
3626 | if (!reg->val || | |
3627 | (is_aa32_id_reg(encoding) && !kvm_supports_32bit_el0())) | |
3628 | continue; | |
3629 | val = reg->val; | |
3630 | } else { | |
3631 | val = ~0UL; | |
3632 | } | |
3633 | ||
3634 | if (put_user(val, (masks + KVM_ARM_FEATURE_ID_RANGE_INDEX(encoding)))) | |
3635 | return -EFAULT; | |
3636 | } | |
3637 | ||
3638 | return 0; | |
3639 | } | |
3640 | ||
8d20bd63 | 3641 | int __init kvm_sys_reg_table_init(void) |
7c8c5e6a | 3642 | { |
47334146 | 3643 | struct sys_reg_params params; |
f1f0c0cf | 3644 | bool valid = true; |
7c8c5e6a | 3645 | unsigned int i; |
7c8c5e6a MZ |
3646 | |
3647 | /* Make sure tables are unique and in order. */ | |
f1f0c0cf AE |
3648 | valid &= check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs), false); |
3649 | valid &= check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs), true); | |
3650 | valid &= check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs), true); | |
3651 | valid &= check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs), true); | |
3652 | valid &= check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs), true); | |
3653 | valid &= check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs), false); | |
3654 | ||
3655 | if (!valid) | |
3656 | return -EINVAL; | |
7c8c5e6a MZ |
3657 | |
3658 | /* We abuse the reset function to overwrite the table itself. */ | |
3659 | for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) | |
3660 | invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]); | |
3661 | ||
47334146 JZ |
3662 | /* Find the first idreg (SYS_ID_PFR0_EL1) in sys_reg_descs. */ |
3663 | params = encoding_to_params(SYS_ID_PFR0_EL1); | |
3664 | first_idreg = find_reg(¶ms, sys_reg_descs, ARRAY_SIZE(sys_reg_descs)); | |
3665 | if (!first_idreg) | |
3666 | return -EINVAL; | |
3667 | ||
e58ec47b MZ |
3668 | if (kvm_get_mode() == KVM_MODE_NV) |
3669 | return populate_nv_trap_config(); | |
3670 | ||
f1f0c0cf | 3671 | return 0; |
7c8c5e6a | 3672 | } |