2 * Copyright (C) 2012,2013 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
5 * Derived from arch/arm/kvm/coproc.c:
6 * Copyright (C) 2012 - Virtual Open Systems and Columbia University
7 * Authors: Rusty Russell <rusty@rustcorp.com.au>
8 * Christoffer Dall <c.dall@virtualopensystems.com>
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License, version 2, as
12 * published by the Free Software Foundation.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program. If not, see <http://www.gnu.org/licenses/>.
24 #include <linux/kvm_host.h>
25 #include <linux/uaccess.h>
26 #include <asm/kvm_arm.h>
27 #include <asm/kvm_host.h>
28 #include <asm/kvm_emulate.h>
29 #include <asm/kvm_coproc.h>
30 #include <asm/kvm_mmu.h>
31 #include <asm/cacheflush.h>
32 #include <asm/cputype.h>
33 #include <asm/debug-monitors.h>
34 #include <trace/events/kvm.h>
39 * All of this file is extremly similar to the ARM coproc.c, but the
40 * types are different. My gut feeling is that it should be pretty
41 * easy to merge, but that would be an ABI breakage -- again. VFP
42 * would also need to be abstracted.
44 * For AArch32, we only take care of what is being trapped. Anything
45 * that has to do with init and userspace access has to go via the
49 /* 3 bits per cache level, as per CLIDR, but non-existent caches always 0 */
50 static u32 cache_levels;
52 /* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
55 /* Which cache CCSIDR represents depends on CSSELR value. */
56 static u32 get_ccsidr(u32 csselr)
60 /* Make sure noone else changes CSSELR during this! */
62 /* Put value into CSSELR */
63 asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
65 /* Read result out of CCSIDR */
66 asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
72 static void do_dc_cisw(u32 val)
74 asm volatile("dc cisw, %x0" : : "r" (val));
78 static void do_dc_csw(u32 val)
80 asm volatile("dc csw, %x0" : : "r" (val));
84 /* See note at ARM ARM B1.14.4 */
85 static bool access_dcsw(struct kvm_vcpu *vcpu,
86 const struct sys_reg_params *p,
87 const struct sys_reg_desc *r)
93 return read_from_write_only(vcpu, p);
97 cpumask_setall(&vcpu->arch.require_dcache_flush);
98 cpumask_clear_cpu(cpu, &vcpu->arch.require_dcache_flush);
100 /* If we were already preempted, take the long way around */
101 if (cpu != vcpu->arch.last_pcpu) {
106 val = *vcpu_reg(vcpu, p->Rt);
109 case 6: /* Upgrade DCISW to DCCISW, as per HCR.SWIO */
110 case 14: /* DCCISW */
126 * Generic accessor for VM registers. Only called as long as HCR_TVM
129 static bool access_vm_reg(struct kvm_vcpu *vcpu,
130 const struct sys_reg_params *p,
131 const struct sys_reg_desc *r)
135 BUG_ON(!p->is_write);
137 val = *vcpu_reg(vcpu, p->Rt);
138 if (!p->is_aarch32) {
139 vcpu_sys_reg(vcpu, r->reg) = val;
142 vcpu_cp15_64_high(vcpu, r->reg) = val >> 32;
143 vcpu_cp15_64_low(vcpu, r->reg) = val & 0xffffffffUL;
150 * SCTLR_EL1 accessor. Only called as long as HCR_TVM is set. If the
151 * guest enables the MMU, we stop trapping the VM sys_regs and leave
152 * it in complete control of the caches.
154 static bool access_sctlr(struct kvm_vcpu *vcpu,
155 const struct sys_reg_params *p,
156 const struct sys_reg_desc *r)
158 access_vm_reg(vcpu, p, r);
160 if (vcpu_has_cache_enabled(vcpu)) { /* MMU+Caches enabled? */
161 vcpu->arch.hcr_el2 &= ~HCR_TVM;
162 stage2_flush_vm(vcpu->kvm);
168 static bool trap_raz_wi(struct kvm_vcpu *vcpu,
169 const struct sys_reg_params *p,
170 const struct sys_reg_desc *r)
173 return ignore_write(vcpu, p);
175 return read_zero(vcpu, p);
178 static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
179 const struct sys_reg_params *p,
180 const struct sys_reg_desc *r)
183 return ignore_write(vcpu, p);
185 *vcpu_reg(vcpu, p->Rt) = (1 << 3);
190 static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu,
191 const struct sys_reg_params *p,
192 const struct sys_reg_desc *r)
195 return ignore_write(vcpu, p);
198 asm volatile("mrs %0, dbgauthstatus_el1" : "=r" (val));
199 *vcpu_reg(vcpu, p->Rt) = val;
205 * We want to avoid world-switching all the DBG registers all the
208 * - If we've touched any debug register, it is likely that we're
209 * going to touch more of them. It then makes sense to disable the
210 * traps and start doing the save/restore dance
211 * - If debug is active (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), it is
212 * then mandatory to save/restore the registers, as the guest
215 * For this, we use a DIRTY bit, indicating the guest has modified the
216 * debug registers, used as follow:
219 * - If the dirty bit is set (because we're coming back from trapping),
220 * disable the traps, save host registers, restore guest registers.
221 * - If debug is actively in use (DBG_MDSCR_KDE or DBG_MDSCR_MDE set),
222 * set the dirty bit, disable the traps, save host registers,
223 * restore guest registers.
224 * - Otherwise, enable the traps
227 * - If the dirty bit is set, save guest registers, restore host
228 * registers and clear the dirty bit. This ensure that the host can
229 * now use the debug registers.
231 static bool trap_debug_regs(struct kvm_vcpu *vcpu,
232 const struct sys_reg_params *p,
233 const struct sys_reg_desc *r)
236 vcpu_sys_reg(vcpu, r->reg) = *vcpu_reg(vcpu, p->Rt);
237 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
239 *vcpu_reg(vcpu, p->Rt) = vcpu_sys_reg(vcpu, r->reg);
245 static void reset_amair_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
249 asm volatile("mrs %0, amair_el1\n" : "=r" (amair));
250 vcpu_sys_reg(vcpu, AMAIR_EL1) = amair;
253 static void reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
256 * Simply map the vcpu_id into the Aff0 field of the MPIDR.
258 vcpu_sys_reg(vcpu, MPIDR_EL1) = (1UL << 31) | (vcpu->vcpu_id & 0xff);
261 /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
262 #define DBG_BCR_BVR_WCR_WVR_EL1(n) \
264 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b100), \
265 trap_debug_regs, reset_val, (DBGBVR0_EL1 + (n)), 0 }, \
267 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b101), \
268 trap_debug_regs, reset_val, (DBGBCR0_EL1 + (n)), 0 }, \
270 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b110), \
271 trap_debug_regs, reset_val, (DBGWVR0_EL1 + (n)), 0 }, \
273 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b111), \
274 trap_debug_regs, reset_val, (DBGWCR0_EL1 + (n)), 0 }
277 * Architected system registers.
278 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
280 * We could trap ID_DFR0 and tell the guest we don't support performance
281 * monitoring. Unfortunately the patch to make the kernel check ID_DFR0 was
282 * NAKed, so it will read the PMCR anyway.
284 * Therefore we tell the guest we have 0 counters. Unfortunately, we
285 * must always support PMCCNTR (the cycle counter): we just RAZ/WI for
286 * all PM registers, which doesn't crash the guest kernel at least.
288 * Debug handling: We do trap most, if not all debug related system
289 * registers. The implementation is good enough to ensure that a guest
290 * can use these with minimal performance degradation. The drawback is
291 * that we don't implement any of the external debug, none of the
292 * OSlock protocol. This should be revisited if we ever encounter a
293 * more demanding guest...
295 static const struct sys_reg_desc sys_reg_descs[] = {
297 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b0110), Op2(0b010),
300 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1010), Op2(0b010),
303 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b010),
306 DBG_BCR_BVR_WCR_WVR_EL1(0),
307 DBG_BCR_BVR_WCR_WVR_EL1(1),
309 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
310 trap_debug_regs, reset_val, MDCCINT_EL1, 0 },
312 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
313 trap_debug_regs, reset_val, MDSCR_EL1, 0 },
314 DBG_BCR_BVR_WCR_WVR_EL1(2),
315 DBG_BCR_BVR_WCR_WVR_EL1(3),
316 DBG_BCR_BVR_WCR_WVR_EL1(4),
317 DBG_BCR_BVR_WCR_WVR_EL1(5),
318 DBG_BCR_BVR_WCR_WVR_EL1(6),
319 DBG_BCR_BVR_WCR_WVR_EL1(7),
320 DBG_BCR_BVR_WCR_WVR_EL1(8),
321 DBG_BCR_BVR_WCR_WVR_EL1(9),
322 DBG_BCR_BVR_WCR_WVR_EL1(10),
323 DBG_BCR_BVR_WCR_WVR_EL1(11),
324 DBG_BCR_BVR_WCR_WVR_EL1(12),
325 DBG_BCR_BVR_WCR_WVR_EL1(13),
326 DBG_BCR_BVR_WCR_WVR_EL1(14),
327 DBG_BCR_BVR_WCR_WVR_EL1(15),
330 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
333 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b100),
336 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0001), Op2(0b100),
339 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0011), Op2(0b100),
342 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0100), Op2(0b100),
344 /* DBGCLAIMSET_EL1 */
345 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1000), Op2(0b110),
347 /* DBGCLAIMCLR_EL1 */
348 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1001), Op2(0b110),
350 /* DBGAUTHSTATUS_EL1 */
351 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b110),
352 trap_dbgauthstatus_el1 },
355 { Op0(0b10), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
356 NULL, reset_val, TEECR32_EL1, 0 },
358 { Op0(0b10), Op1(0b010), CRn(0b0001), CRm(0b0000), Op2(0b000),
359 NULL, reset_val, TEEHBR32_EL1, 0 },
362 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0001), Op2(0b000),
365 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0100), Op2(0b000),
367 /* DBGDTR[TR]X_EL0 */
368 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0101), Op2(0b000),
372 { Op0(0b10), Op1(0b100), CRn(0b0000), CRm(0b0111), Op2(0b000),
373 NULL, reset_val, DBGVCR32_EL2, 0 },
376 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b101),
377 NULL, reset_mpidr, MPIDR_EL1 },
379 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
380 access_sctlr, reset_val, SCTLR_EL1, 0x00C50078 },
382 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b010),
383 NULL, reset_val, CPACR_EL1, 0 },
385 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b000),
386 access_vm_reg, reset_unknown, TTBR0_EL1 },
388 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b001),
389 access_vm_reg, reset_unknown, TTBR1_EL1 },
391 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b010),
392 access_vm_reg, reset_val, TCR_EL1, 0 },
395 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b000),
396 access_vm_reg, reset_unknown, AFSR0_EL1 },
398 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b001),
399 access_vm_reg, reset_unknown, AFSR1_EL1 },
401 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0010), Op2(0b000),
402 access_vm_reg, reset_unknown, ESR_EL1 },
404 { Op0(0b11), Op1(0b000), CRn(0b0110), CRm(0b0000), Op2(0b000),
405 access_vm_reg, reset_unknown, FAR_EL1 },
407 { Op0(0b11), Op1(0b000), CRn(0b0111), CRm(0b0100), Op2(0b000),
408 NULL, reset_unknown, PAR_EL1 },
411 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b001),
414 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b010),
418 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0010), Op2(0b000),
419 access_vm_reg, reset_unknown, MAIR_EL1 },
421 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0011), Op2(0b000),
422 access_vm_reg, reset_amair_el1, AMAIR_EL1 },
425 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b0000), Op2(0b000),
426 NULL, reset_val, VBAR_EL1, 0 },
428 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b001),
429 access_vm_reg, reset_val, CONTEXTIDR_EL1, 0 },
431 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b100),
432 NULL, reset_unknown, TPIDR_EL1 },
435 { Op0(0b11), Op1(0b000), CRn(0b1110), CRm(0b0001), Op2(0b000),
436 NULL, reset_val, CNTKCTL_EL1, 0},
439 { Op0(0b11), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
440 NULL, reset_unknown, CSSELR_EL1 },
443 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b000),
446 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b001),
449 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b010),
452 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b011),
455 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b100),
458 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b101),
461 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b110),
464 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b111),
467 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b000),
470 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b001),
473 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b010),
476 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b000),
479 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b011),
483 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b010),
484 NULL, reset_unknown, TPIDR_EL0 },
486 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b011),
487 NULL, reset_unknown, TPIDRRO_EL0 },
490 { Op0(0b11), Op1(0b100), CRn(0b0011), CRm(0b0000), Op2(0b000),
491 NULL, reset_unknown, DACR32_EL2 },
493 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0000), Op2(0b001),
494 NULL, reset_unknown, IFSR32_EL2 },
496 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0011), Op2(0b000),
497 NULL, reset_val, FPEXC32_EL2, 0x70 },
500 static bool trap_dbgidr(struct kvm_vcpu *vcpu,
501 const struct sys_reg_params *p,
502 const struct sys_reg_desc *r)
505 return ignore_write(vcpu, p);
507 u64 dfr = read_cpuid(ID_AA64DFR0_EL1);
508 u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
509 u32 el3 = !!((pfr >> 12) & 0xf);
511 *vcpu_reg(vcpu, p->Rt) = ((((dfr >> 20) & 0xf) << 28) |
512 (((dfr >> 12) & 0xf) << 24) |
513 (((dfr >> 28) & 0xf) << 20) |
514 (6 << 16) | (el3 << 14) | (el3 << 12));
519 static bool trap_debug32(struct kvm_vcpu *vcpu,
520 const struct sys_reg_params *p,
521 const struct sys_reg_desc *r)
524 vcpu_cp14(vcpu, r->reg) = *vcpu_reg(vcpu, p->Rt);
525 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
527 *vcpu_reg(vcpu, p->Rt) = vcpu_cp14(vcpu, r->reg);
533 #define DBG_BCR_BVR_WCR_WVR(n) \
535 { Op1( 0), CRn( 0), CRm((n)), Op2( 4), trap_debug32, \
536 NULL, (cp14_DBGBVR0 + (n) * 2) }, \
538 { Op1( 0), CRn( 0), CRm((n)), Op2( 5), trap_debug32, \
539 NULL, (cp14_DBGBCR0 + (n) * 2) }, \
541 { Op1( 0), CRn( 0), CRm((n)), Op2( 6), trap_debug32, \
542 NULL, (cp14_DBGWVR0 + (n) * 2) }, \
544 { Op1( 0), CRn( 0), CRm((n)), Op2( 7), trap_debug32, \
545 NULL, (cp14_DBGWCR0 + (n) * 2) }
548 { Op1( 0), CRn( 1), CRm((n)), Op2( 1), trap_debug32, \
549 NULL, cp14_DBGBXVR0 + n * 2 }
552 * Trapped cp14 registers. We generally ignore most of the external
553 * debug, on the principle that they don't really make sense to a
554 * guest. Revisit this one day, whould this principle change.
556 static const struct sys_reg_desc cp14_regs[] = {
558 { Op1( 0), CRn( 0), CRm( 0), Op2( 0), trap_dbgidr },
560 { Op1( 0), CRn( 0), CRm( 0), Op2( 2), trap_raz_wi },
562 DBG_BCR_BVR_WCR_WVR(0),
564 { Op1( 0), CRn( 0), CRm( 1), Op2( 0), trap_raz_wi },
565 DBG_BCR_BVR_WCR_WVR(1),
567 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), trap_debug32 },
569 { Op1( 0), CRn( 0), CRm( 2), Op2( 2), trap_debug32 },
570 DBG_BCR_BVR_WCR_WVR(2),
572 { Op1( 0), CRn( 0), CRm( 3), Op2( 0), trap_raz_wi },
574 { Op1( 0), CRn( 0), CRm( 3), Op2( 2), trap_raz_wi },
575 DBG_BCR_BVR_WCR_WVR(3),
576 DBG_BCR_BVR_WCR_WVR(4),
577 DBG_BCR_BVR_WCR_WVR(5),
579 { Op1( 0), CRn( 0), CRm( 6), Op2( 0), trap_raz_wi },
581 { Op1( 0), CRn( 0), CRm( 6), Op2( 2), trap_raz_wi },
582 DBG_BCR_BVR_WCR_WVR(6),
584 { Op1( 0), CRn( 0), CRm( 7), Op2( 0), trap_debug32 },
585 DBG_BCR_BVR_WCR_WVR(7),
586 DBG_BCR_BVR_WCR_WVR(8),
587 DBG_BCR_BVR_WCR_WVR(9),
588 DBG_BCR_BVR_WCR_WVR(10),
589 DBG_BCR_BVR_WCR_WVR(11),
590 DBG_BCR_BVR_WCR_WVR(12),
591 DBG_BCR_BVR_WCR_WVR(13),
592 DBG_BCR_BVR_WCR_WVR(14),
593 DBG_BCR_BVR_WCR_WVR(15),
595 /* DBGDRAR (32bit) */
596 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), trap_raz_wi },
600 { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi },
603 { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1 },
607 { Op1( 0), CRn( 1), CRm( 3), Op2( 4), trap_raz_wi },
610 { Op1( 0), CRn( 1), CRm( 4), Op2( 4), trap_raz_wi },
623 /* DBGDSAR (32bit) */
624 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), trap_raz_wi },
627 { Op1( 0), CRn( 7), CRm( 0), Op2( 7), trap_raz_wi },
629 { Op1( 0), CRn( 7), CRm( 1), Op2( 7), trap_raz_wi },
631 { Op1( 0), CRn( 7), CRm( 2), Op2( 7), trap_raz_wi },
633 { Op1( 0), CRn( 7), CRm( 8), Op2( 6), trap_raz_wi },
635 { Op1( 0), CRn( 7), CRm( 9), Op2( 6), trap_raz_wi },
637 { Op1( 0), CRn( 7), CRm(14), Op2( 6), trap_dbgauthstatus_el1 },
640 /* Trapped cp14 64bit registers */
641 static const struct sys_reg_desc cp14_64_regs[] = {
642 /* DBGDRAR (64bit) */
643 { Op1( 0), CRm( 1), .access = trap_raz_wi },
645 /* DBGDSAR (64bit) */
646 { Op1( 0), CRm( 2), .access = trap_raz_wi },
650 * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding,
651 * depending on the way they are accessed (as a 32bit or a 64bit
654 static const struct sys_reg_desc cp15_regs[] = {
655 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), access_sctlr, NULL, c1_SCTLR },
656 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
657 { Op1( 0), CRn( 2), CRm( 0), Op2( 1), access_vm_reg, NULL, c2_TTBR1 },
658 { Op1( 0), CRn( 2), CRm( 0), Op2( 2), access_vm_reg, NULL, c2_TTBCR },
659 { Op1( 0), CRn( 3), CRm( 0), Op2( 0), access_vm_reg, NULL, c3_DACR },
660 { Op1( 0), CRn( 5), CRm( 0), Op2( 0), access_vm_reg, NULL, c5_DFSR },
661 { Op1( 0), CRn( 5), CRm( 0), Op2( 1), access_vm_reg, NULL, c5_IFSR },
662 { Op1( 0), CRn( 5), CRm( 1), Op2( 0), access_vm_reg, NULL, c5_ADFSR },
663 { Op1( 0), CRn( 5), CRm( 1), Op2( 1), access_vm_reg, NULL, c5_AIFSR },
664 { Op1( 0), CRn( 6), CRm( 0), Op2( 0), access_vm_reg, NULL, c6_DFAR },
665 { Op1( 0), CRn( 6), CRm( 0), Op2( 2), access_vm_reg, NULL, c6_IFAR },
668 * DC{C,I,CI}SW operations:
670 { Op1( 0), CRn( 7), CRm( 6), Op2( 2), access_dcsw },
671 { Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw },
672 { Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw },
675 { Op1( 0), CRn( 9), CRm(12), Op2( 0), trap_raz_wi },
676 { Op1( 0), CRn( 9), CRm(12), Op2( 1), trap_raz_wi },
677 { Op1( 0), CRn( 9), CRm(12), Op2( 2), trap_raz_wi },
678 { Op1( 0), CRn( 9), CRm(12), Op2( 3), trap_raz_wi },
679 { Op1( 0), CRn( 9), CRm(12), Op2( 5), trap_raz_wi },
680 { Op1( 0), CRn( 9), CRm(12), Op2( 6), trap_raz_wi },
681 { Op1( 0), CRn( 9), CRm(12), Op2( 7), trap_raz_wi },
682 { Op1( 0), CRn( 9), CRm(13), Op2( 0), trap_raz_wi },
683 { Op1( 0), CRn( 9), CRm(13), Op2( 1), trap_raz_wi },
684 { Op1( 0), CRn( 9), CRm(13), Op2( 2), trap_raz_wi },
685 { Op1( 0), CRn( 9), CRm(14), Op2( 0), trap_raz_wi },
686 { Op1( 0), CRn( 9), CRm(14), Op2( 1), trap_raz_wi },
687 { Op1( 0), CRn( 9), CRm(14), Op2( 2), trap_raz_wi },
689 { Op1( 0), CRn(10), CRm( 2), Op2( 0), access_vm_reg, NULL, c10_PRRR },
690 { Op1( 0), CRn(10), CRm( 2), Op2( 1), access_vm_reg, NULL, c10_NMRR },
691 { Op1( 0), CRn(10), CRm( 3), Op2( 0), access_vm_reg, NULL, c10_AMAIR0 },
692 { Op1( 0), CRn(10), CRm( 3), Op2( 1), access_vm_reg, NULL, c10_AMAIR1 },
693 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID },
696 static const struct sys_reg_desc cp15_64_regs[] = {
697 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
698 { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR1 },
701 /* Target specific emulation tables */
702 static struct kvm_sys_reg_target_table *target_tables[KVM_ARM_NUM_TARGETS];
704 void kvm_register_target_sys_reg_table(unsigned int target,
705 struct kvm_sys_reg_target_table *table)
707 target_tables[target] = table;
710 /* Get specific register table for this target. */
711 static const struct sys_reg_desc *get_target_table(unsigned target,
715 struct kvm_sys_reg_target_table *table;
717 table = target_tables[target];
719 *num = table->table64.num;
720 return table->table64.table;
722 *num = table->table32.num;
723 return table->table32.table;
727 static const struct sys_reg_desc *find_reg(const struct sys_reg_params *params,
728 const struct sys_reg_desc table[],
733 for (i = 0; i < num; i++) {
734 const struct sys_reg_desc *r = &table[i];
736 if (params->Op0 != r->Op0)
738 if (params->Op1 != r->Op1)
740 if (params->CRn != r->CRn)
742 if (params->CRm != r->CRm)
744 if (params->Op2 != r->Op2)
752 int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu, struct kvm_run *run)
754 kvm_inject_undefined(vcpu);
759 * emulate_cp -- tries to match a sys_reg access in a handling table, and
760 * call the corresponding trap handler.
762 * @params: pointer to the descriptor of the access
763 * @table: array of trap descriptors
764 * @num: size of the trap descriptor array
766 * Return 0 if the access has been handled, and -1 if not.
768 static int emulate_cp(struct kvm_vcpu *vcpu,
769 const struct sys_reg_params *params,
770 const struct sys_reg_desc *table,
773 const struct sys_reg_desc *r;
776 return -1; /* Not handled */
778 r = find_reg(params, table, num);
782 * Not having an accessor means that we have
783 * configured a trap that we don't know how to
784 * handle. This certainly qualifies as a gross bug
785 * that should be fixed right away.
789 if (likely(r->access(vcpu, params, r))) {
790 /* Skip instruction, since it was emulated */
791 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
802 static void unhandled_cp_access(struct kvm_vcpu *vcpu,
803 struct sys_reg_params *params)
805 u8 hsr_ec = kvm_vcpu_trap_get_class(vcpu);
809 case ESR_EL2_EC_CP15_32:
810 case ESR_EL2_EC_CP15_64:
813 case ESR_EL2_EC_CP14_MR:
814 case ESR_EL2_EC_CP14_64:
821 kvm_err("Unsupported guest CP%d access at: %08lx\n",
823 print_sys_reg_instr(params);
824 kvm_inject_undefined(vcpu);
828 * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP15 access
829 * @vcpu: The VCPU pointer
830 * @run: The kvm_run struct
832 static int kvm_handle_cp_64(struct kvm_vcpu *vcpu,
833 const struct sys_reg_desc *global,
835 const struct sys_reg_desc *target_specific,
838 struct sys_reg_params params;
839 u32 hsr = kvm_vcpu_get_hsr(vcpu);
840 int Rt2 = (hsr >> 10) & 0xf;
842 params.is_aarch32 = true;
843 params.is_32bit = false;
844 params.CRm = (hsr >> 1) & 0xf;
845 params.Rt = (hsr >> 5) & 0xf;
846 params.is_write = ((hsr & 1) == 0);
849 params.Op1 = (hsr >> 16) & 0xf;
854 * Massive hack here. Store Rt2 in the top 32bits so we only
855 * have one register to deal with. As we use the same trap
856 * backends between AArch32 and AArch64, we get away with it.
858 if (params.is_write) {
859 u64 val = *vcpu_reg(vcpu, params.Rt);
861 val |= *vcpu_reg(vcpu, Rt2) << 32;
862 *vcpu_reg(vcpu, params.Rt) = val;
865 if (!emulate_cp(vcpu, ¶ms, target_specific, nr_specific))
867 if (!emulate_cp(vcpu, ¶ms, global, nr_global))
870 unhandled_cp_access(vcpu, ¶ms);
873 /* Do the opposite hack for the read side */
874 if (!params.is_write) {
875 u64 val = *vcpu_reg(vcpu, params.Rt);
877 *vcpu_reg(vcpu, Rt2) = val;
884 * kvm_handle_cp15_32 -- handles a mrc/mcr trap on a guest CP15 access
885 * @vcpu: The VCPU pointer
886 * @run: The kvm_run struct
888 static int kvm_handle_cp_32(struct kvm_vcpu *vcpu,
889 const struct sys_reg_desc *global,
891 const struct sys_reg_desc *target_specific,
894 struct sys_reg_params params;
895 u32 hsr = kvm_vcpu_get_hsr(vcpu);
897 params.is_aarch32 = true;
898 params.is_32bit = true;
899 params.CRm = (hsr >> 1) & 0xf;
900 params.Rt = (hsr >> 5) & 0xf;
901 params.is_write = ((hsr & 1) == 0);
902 params.CRn = (hsr >> 10) & 0xf;
904 params.Op1 = (hsr >> 14) & 0x7;
905 params.Op2 = (hsr >> 17) & 0x7;
907 if (!emulate_cp(vcpu, ¶ms, target_specific, nr_specific))
909 if (!emulate_cp(vcpu, ¶ms, global, nr_global))
912 unhandled_cp_access(vcpu, ¶ms);
916 int kvm_handle_cp15_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
918 const struct sys_reg_desc *target_specific;
921 target_specific = get_target_table(vcpu->arch.target, false, &num);
922 return kvm_handle_cp_64(vcpu,
923 cp15_64_regs, ARRAY_SIZE(cp15_64_regs),
924 target_specific, num);
927 int kvm_handle_cp15_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
929 const struct sys_reg_desc *target_specific;
932 target_specific = get_target_table(vcpu->arch.target, false, &num);
933 return kvm_handle_cp_32(vcpu,
934 cp15_regs, ARRAY_SIZE(cp15_regs),
935 target_specific, num);
938 int kvm_handle_cp14_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
940 return kvm_handle_cp_64(vcpu,
941 cp14_64_regs, ARRAY_SIZE(cp14_64_regs),
945 int kvm_handle_cp14_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
947 return kvm_handle_cp_32(vcpu,
948 cp14_regs, ARRAY_SIZE(cp14_regs),
952 static int emulate_sys_reg(struct kvm_vcpu *vcpu,
953 const struct sys_reg_params *params)
956 const struct sys_reg_desc *table, *r;
958 table = get_target_table(vcpu->arch.target, true, &num);
960 /* Search target-specific then generic table. */
961 r = find_reg(params, table, num);
963 r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
967 * Not having an accessor means that we have
968 * configured a trap that we don't know how to
969 * handle. This certainly qualifies as a gross bug
970 * that should be fixed right away.
974 if (likely(r->access(vcpu, params, r))) {
975 /* Skip instruction, since it was emulated */
976 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
979 /* If access function fails, it should complain. */
981 kvm_err("Unsupported guest sys_reg access at: %lx\n",
983 print_sys_reg_instr(params);
985 kvm_inject_undefined(vcpu);
989 static void reset_sys_reg_descs(struct kvm_vcpu *vcpu,
990 const struct sys_reg_desc *table, size_t num)
994 for (i = 0; i < num; i++)
996 table[i].reset(vcpu, &table[i]);
1000 * kvm_handle_sys_reg -- handles a mrs/msr trap on a guest sys_reg access
1001 * @vcpu: The VCPU pointer
1002 * @run: The kvm_run struct
1004 int kvm_handle_sys_reg(struct kvm_vcpu *vcpu, struct kvm_run *run)
1006 struct sys_reg_params params;
1007 unsigned long esr = kvm_vcpu_get_hsr(vcpu);
1009 params.is_aarch32 = false;
1010 params.is_32bit = false;
1011 params.Op0 = (esr >> 20) & 3;
1012 params.Op1 = (esr >> 14) & 0x7;
1013 params.CRn = (esr >> 10) & 0xf;
1014 params.CRm = (esr >> 1) & 0xf;
1015 params.Op2 = (esr >> 17) & 0x7;
1016 params.Rt = (esr >> 5) & 0x1f;
1017 params.is_write = !(esr & 1);
1019 return emulate_sys_reg(vcpu, ¶ms);
1022 /******************************************************************************
1024 *****************************************************************************/
1026 static bool index_to_params(u64 id, struct sys_reg_params *params)
1028 switch (id & KVM_REG_SIZE_MASK) {
1029 case KVM_REG_SIZE_U64:
1030 /* Any unused index bits means it's not valid. */
1031 if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
1032 | KVM_REG_ARM_COPROC_MASK
1033 | KVM_REG_ARM64_SYSREG_OP0_MASK
1034 | KVM_REG_ARM64_SYSREG_OP1_MASK
1035 | KVM_REG_ARM64_SYSREG_CRN_MASK
1036 | KVM_REG_ARM64_SYSREG_CRM_MASK
1037 | KVM_REG_ARM64_SYSREG_OP2_MASK))
1039 params->Op0 = ((id & KVM_REG_ARM64_SYSREG_OP0_MASK)
1040 >> KVM_REG_ARM64_SYSREG_OP0_SHIFT);
1041 params->Op1 = ((id & KVM_REG_ARM64_SYSREG_OP1_MASK)
1042 >> KVM_REG_ARM64_SYSREG_OP1_SHIFT);
1043 params->CRn = ((id & KVM_REG_ARM64_SYSREG_CRN_MASK)
1044 >> KVM_REG_ARM64_SYSREG_CRN_SHIFT);
1045 params->CRm = ((id & KVM_REG_ARM64_SYSREG_CRM_MASK)
1046 >> KVM_REG_ARM64_SYSREG_CRM_SHIFT);
1047 params->Op2 = ((id & KVM_REG_ARM64_SYSREG_OP2_MASK)
1048 >> KVM_REG_ARM64_SYSREG_OP2_SHIFT);
1055 /* Decode an index value, and find the sys_reg_desc entry. */
1056 static const struct sys_reg_desc *index_to_sys_reg_desc(struct kvm_vcpu *vcpu,
1060 const struct sys_reg_desc *table, *r;
1061 struct sys_reg_params params;
1063 /* We only do sys_reg for now. */
1064 if ((id & KVM_REG_ARM_COPROC_MASK) != KVM_REG_ARM64_SYSREG)
1067 if (!index_to_params(id, ¶ms))
1070 table = get_target_table(vcpu->arch.target, true, &num);
1071 r = find_reg(¶ms, table, num);
1073 r = find_reg(¶ms, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1075 /* Not saved in the sys_reg array? */
1083 * These are the invariant sys_reg registers: we let the guest see the
1084 * host versions of these, so they're part of the guest state.
1086 * A future CPU may provide a mechanism to present different values to
1087 * the guest, or a future kvm may trap them.
1090 #define FUNCTION_INVARIANT(reg) \
1091 static void get_##reg(struct kvm_vcpu *v, \
1092 const struct sys_reg_desc *r) \
1096 asm volatile("mrs %0, " __stringify(reg) "\n" \
1098 ((struct sys_reg_desc *)r)->val = val; \
1101 FUNCTION_INVARIANT(midr_el1)
1102 FUNCTION_INVARIANT(ctr_el0)
1103 FUNCTION_INVARIANT(revidr_el1)
1104 FUNCTION_INVARIANT(id_pfr0_el1)
1105 FUNCTION_INVARIANT(id_pfr1_el1)
1106 FUNCTION_INVARIANT(id_dfr0_el1)
1107 FUNCTION_INVARIANT(id_afr0_el1)
1108 FUNCTION_INVARIANT(id_mmfr0_el1)
1109 FUNCTION_INVARIANT(id_mmfr1_el1)
1110 FUNCTION_INVARIANT(id_mmfr2_el1)
1111 FUNCTION_INVARIANT(id_mmfr3_el1)
1112 FUNCTION_INVARIANT(id_isar0_el1)
1113 FUNCTION_INVARIANT(id_isar1_el1)
1114 FUNCTION_INVARIANT(id_isar2_el1)
1115 FUNCTION_INVARIANT(id_isar3_el1)
1116 FUNCTION_INVARIANT(id_isar4_el1)
1117 FUNCTION_INVARIANT(id_isar5_el1)
1118 FUNCTION_INVARIANT(clidr_el1)
1119 FUNCTION_INVARIANT(aidr_el1)
1121 /* ->val is filled in by kvm_sys_reg_table_init() */
1122 static struct sys_reg_desc invariant_sys_regs[] = {
1123 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b000),
1124 NULL, get_midr_el1 },
1125 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b110),
1126 NULL, get_revidr_el1 },
1127 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b000),
1128 NULL, get_id_pfr0_el1 },
1129 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b001),
1130 NULL, get_id_pfr1_el1 },
1131 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b010),
1132 NULL, get_id_dfr0_el1 },
1133 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b011),
1134 NULL, get_id_afr0_el1 },
1135 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b100),
1136 NULL, get_id_mmfr0_el1 },
1137 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b101),
1138 NULL, get_id_mmfr1_el1 },
1139 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b110),
1140 NULL, get_id_mmfr2_el1 },
1141 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b111),
1142 NULL, get_id_mmfr3_el1 },
1143 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
1144 NULL, get_id_isar0_el1 },
1145 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b001),
1146 NULL, get_id_isar1_el1 },
1147 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
1148 NULL, get_id_isar2_el1 },
1149 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b011),
1150 NULL, get_id_isar3_el1 },
1151 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b100),
1152 NULL, get_id_isar4_el1 },
1153 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b101),
1154 NULL, get_id_isar5_el1 },
1155 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b001),
1156 NULL, get_clidr_el1 },
1157 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b111),
1158 NULL, get_aidr_el1 },
1159 { Op0(0b11), Op1(0b011), CRn(0b0000), CRm(0b0000), Op2(0b001),
1160 NULL, get_ctr_el0 },
1163 static int reg_from_user(u64 *val, const void __user *uaddr, u64 id)
1165 if (copy_from_user(val, uaddr, KVM_REG_SIZE(id)) != 0)
1170 static int reg_to_user(void __user *uaddr, const u64 *val, u64 id)
1172 if (copy_to_user(uaddr, val, KVM_REG_SIZE(id)) != 0)
1177 static int get_invariant_sys_reg(u64 id, void __user *uaddr)
1179 struct sys_reg_params params;
1180 const struct sys_reg_desc *r;
1182 if (!index_to_params(id, ¶ms))
1185 r = find_reg(¶ms, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
1189 return reg_to_user(uaddr, &r->val, id);
1192 static int set_invariant_sys_reg(u64 id, void __user *uaddr)
1194 struct sys_reg_params params;
1195 const struct sys_reg_desc *r;
1197 u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
1199 if (!index_to_params(id, ¶ms))
1201 r = find_reg(¶ms, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
1205 err = reg_from_user(&val, uaddr, id);
1209 /* This is what we mean by invariant: you can't change it. */
1216 static bool is_valid_cache(u32 val)
1220 if (val >= CSSELR_MAX)
1223 /* Bottom bit is Instruction or Data bit. Next 3 bits are level. */
1225 ctype = (cache_levels >> (level * 3)) & 7;
1228 case 0: /* No cache */
1230 case 1: /* Instruction cache only */
1232 case 2: /* Data cache only */
1233 case 4: /* Unified cache */
1235 case 3: /* Separate instruction and data caches */
1237 default: /* Reserved: we can't know instruction or data. */
1242 static int demux_c15_get(u64 id, void __user *uaddr)
1245 u32 __user *uval = uaddr;
1247 /* Fail if we have unknown bits set. */
1248 if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
1249 | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
1252 switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
1253 case KVM_REG_ARM_DEMUX_ID_CCSIDR:
1254 if (KVM_REG_SIZE(id) != 4)
1256 val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
1257 >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
1258 if (!is_valid_cache(val))
1261 return put_user(get_ccsidr(val), uval);
1267 static int demux_c15_set(u64 id, void __user *uaddr)
1270 u32 __user *uval = uaddr;
1272 /* Fail if we have unknown bits set. */
1273 if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
1274 | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
1277 switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
1278 case KVM_REG_ARM_DEMUX_ID_CCSIDR:
1279 if (KVM_REG_SIZE(id) != 4)
1281 val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
1282 >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
1283 if (!is_valid_cache(val))
1286 if (get_user(newval, uval))
1289 /* This is also invariant: you can't change it. */
1290 if (newval != get_ccsidr(val))
1298 int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
1300 const struct sys_reg_desc *r;
1301 void __user *uaddr = (void __user *)(unsigned long)reg->addr;
1303 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
1304 return demux_c15_get(reg->id, uaddr);
1306 if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
1309 r = index_to_sys_reg_desc(vcpu, reg->id);
1311 return get_invariant_sys_reg(reg->id, uaddr);
1313 return reg_to_user(uaddr, &vcpu_sys_reg(vcpu, r->reg), reg->id);
1316 int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
1318 const struct sys_reg_desc *r;
1319 void __user *uaddr = (void __user *)(unsigned long)reg->addr;
1321 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
1322 return demux_c15_set(reg->id, uaddr);
1324 if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
1327 r = index_to_sys_reg_desc(vcpu, reg->id);
1329 return set_invariant_sys_reg(reg->id, uaddr);
1331 return reg_from_user(&vcpu_sys_reg(vcpu, r->reg), uaddr, reg->id);
1334 static unsigned int num_demux_regs(void)
1336 unsigned int i, count = 0;
1338 for (i = 0; i < CSSELR_MAX; i++)
1339 if (is_valid_cache(i))
1345 static int write_demux_regids(u64 __user *uindices)
1347 u64 val = KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX;
1350 val |= KVM_REG_ARM_DEMUX_ID_CCSIDR;
1351 for (i = 0; i < CSSELR_MAX; i++) {
1352 if (!is_valid_cache(i))
1354 if (put_user(val | i, uindices))
1361 static u64 sys_reg_to_index(const struct sys_reg_desc *reg)
1363 return (KVM_REG_ARM64 | KVM_REG_SIZE_U64 |
1364 KVM_REG_ARM64_SYSREG |
1365 (reg->Op0 << KVM_REG_ARM64_SYSREG_OP0_SHIFT) |
1366 (reg->Op1 << KVM_REG_ARM64_SYSREG_OP1_SHIFT) |
1367 (reg->CRn << KVM_REG_ARM64_SYSREG_CRN_SHIFT) |
1368 (reg->CRm << KVM_REG_ARM64_SYSREG_CRM_SHIFT) |
1369 (reg->Op2 << KVM_REG_ARM64_SYSREG_OP2_SHIFT));
1372 static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
1377 if (put_user(sys_reg_to_index(reg), *uind))
1384 /* Assumed ordered tables, see kvm_sys_reg_table_init. */
1385 static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
1387 const struct sys_reg_desc *i1, *i2, *end1, *end2;
1388 unsigned int total = 0;
1391 /* We check for duplicates here, to allow arch-specific overrides. */
1392 i1 = get_target_table(vcpu->arch.target, true, &num);
1395 end2 = sys_reg_descs + ARRAY_SIZE(sys_reg_descs);
1397 BUG_ON(i1 == end1 || i2 == end2);
1399 /* Walk carefully, as both tables may refer to the same register. */
1401 int cmp = cmp_sys_reg(i1, i2);
1402 /* target-specific overrides generic entry. */
1404 /* Ignore registers we trap but don't save. */
1406 if (!copy_reg_to_user(i1, &uind))
1411 /* Ignore registers we trap but don't save. */
1413 if (!copy_reg_to_user(i2, &uind))
1419 if (cmp <= 0 && ++i1 == end1)
1421 if (cmp >= 0 && ++i2 == end2)
1427 unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
1429 return ARRAY_SIZE(invariant_sys_regs)
1431 + walk_sys_regs(vcpu, (u64 __user *)NULL);
1434 int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
1439 /* Then give them all the invariant registers' indices. */
1440 for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) {
1441 if (put_user(sys_reg_to_index(&invariant_sys_regs[i]), uindices))
1446 err = walk_sys_regs(vcpu, uindices);
1451 return write_demux_regids(uindices);
1454 static int check_sysreg_table(const struct sys_reg_desc *table, unsigned int n)
1458 for (i = 1; i < n; i++) {
1459 if (cmp_sys_reg(&table[i-1], &table[i]) >= 0) {
1460 kvm_err("sys_reg table %p out of order (%d)\n", table, i - 1);
1468 void kvm_sys_reg_table_init(void)
1471 struct sys_reg_desc clidr;
1473 /* Make sure tables are unique and in order. */
1474 BUG_ON(check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs)));
1475 BUG_ON(check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs)));
1476 BUG_ON(check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs)));
1477 BUG_ON(check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs)));
1478 BUG_ON(check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs)));
1479 BUG_ON(check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs)));
1481 /* We abuse the reset function to overwrite the table itself. */
1482 for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++)
1483 invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]);
1486 * CLIDR format is awkward, so clean it up. See ARM B4.1.20:
1488 * If software reads the Cache Type fields from Ctype1
1489 * upwards, once it has seen a value of 0b000, no caches
1490 * exist at further-out levels of the hierarchy. So, for
1491 * example, if Ctype3 is the first Cache Type field with a
1492 * value of 0b000, the values of Ctype4 to Ctype7 must be
1495 get_clidr_el1(NULL, &clidr); /* Ugly... */
1496 cache_levels = clidr.val;
1497 for (i = 0; i < 7; i++)
1498 if (((cache_levels >> (i*3)) & 7) == 0)
1500 /* Clear all higher bits. */
1501 cache_levels &= (1 << (i*3))-1;
1505 * kvm_reset_sys_regs - sets system registers to reset value
1506 * @vcpu: The VCPU pointer
1508 * This function finds the right table above and sets the registers on the
1509 * virtual CPU struct to their architecturally defined reset values.
1511 void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
1514 const struct sys_reg_desc *table;
1516 /* Catch someone adding a register without putting in reset entry. */
1517 memset(&vcpu->arch.ctxt.sys_regs, 0x42, sizeof(vcpu->arch.ctxt.sys_regs));
1519 /* Generic chip reset first (so target could override). */
1520 reset_sys_reg_descs(vcpu, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1522 table = get_target_table(vcpu->arch.target, true, &num);
1523 reset_sys_reg_descs(vcpu, table, num);
1525 for (num = 1; num < NR_SYS_REGS; num++)
1526 if (vcpu_sys_reg(vcpu, num) == 0x4242424242424242)
1527 panic("Didn't reset vcpu_sys_reg(%zi)", num);