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/>.
23 #include <linux/bsearch.h>
24 #include <linux/kvm_host.h>
26 #include <linux/uaccess.h>
28 #include <asm/cacheflush.h>
29 #include <asm/cputype.h>
30 #include <asm/debug-monitors.h>
32 #include <asm/kvm_arm.h>
33 #include <asm/kvm_asm.h>
34 #include <asm/kvm_coproc.h>
35 #include <asm/kvm_emulate.h>
36 #include <asm/kvm_host.h>
37 #include <asm/kvm_mmu.h>
38 #include <asm/perf_event.h>
39 #include <asm/sysreg.h>
41 #include <trace/events/kvm.h>
48 * All of this file is extremly similar to the ARM coproc.c, but the
49 * types are different. My gut feeling is that it should be pretty
50 * easy to merge, but that would be an ABI breakage -- again. VFP
51 * would also need to be abstracted.
53 * For AArch32, we only take care of what is being trapped. Anything
54 * that has to do with init and userspace access has to go via the
58 /* 3 bits per cache level, as per CLIDR, but non-existent caches always 0 */
59 static u32 cache_levels
;
61 /* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
64 /* Which cache CCSIDR represents depends on CSSELR value. */
65 static u32
get_ccsidr(u32 csselr
)
69 /* Make sure noone else changes CSSELR during this! */
71 write_sysreg(csselr
, csselr_el1
);
73 ccsidr
= read_sysreg(ccsidr_el1
);
80 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
82 static bool access_dcsw(struct kvm_vcpu
*vcpu
,
83 struct sys_reg_params
*p
,
84 const struct sys_reg_desc
*r
)
87 return read_from_write_only(vcpu
, p
);
89 kvm_set_way_flush(vcpu
);
94 * Generic accessor for VM registers. Only called as long as HCR_TVM
95 * is set. If the guest enables the MMU, we stop trapping the VM
96 * sys_regs and leave it in complete control of the caches.
98 static bool access_vm_reg(struct kvm_vcpu
*vcpu
,
99 struct sys_reg_params
*p
,
100 const struct sys_reg_desc
*r
)
102 bool was_enabled
= vcpu_has_cache_enabled(vcpu
);
104 BUG_ON(!p
->is_write
);
106 if (!p
->is_aarch32
) {
107 vcpu_sys_reg(vcpu
, r
->reg
) = p
->regval
;
110 vcpu_cp15_64_high(vcpu
, r
->reg
) = upper_32_bits(p
->regval
);
111 vcpu_cp15_64_low(vcpu
, r
->reg
) = lower_32_bits(p
->regval
);
114 kvm_toggle_cache(vcpu
, was_enabled
);
119 * Trap handler for the GICv3 SGI generation system register.
120 * Forward the request to the VGIC emulation.
121 * The cp15_64 code makes sure this automatically works
122 * for both AArch64 and AArch32 accesses.
124 static bool access_gic_sgi(struct kvm_vcpu
*vcpu
,
125 struct sys_reg_params
*p
,
126 const struct sys_reg_desc
*r
)
129 return read_from_write_only(vcpu
, p
);
131 vgic_v3_dispatch_sgi(vcpu
, p
->regval
);
136 static bool access_gic_sre(struct kvm_vcpu
*vcpu
,
137 struct sys_reg_params
*p
,
138 const struct sys_reg_desc
*r
)
141 return ignore_write(vcpu
, p
);
143 p
->regval
= vcpu
->arch
.vgic_cpu
.vgic_v3
.vgic_sre
;
147 static bool trap_raz_wi(struct kvm_vcpu
*vcpu
,
148 struct sys_reg_params
*p
,
149 const struct sys_reg_desc
*r
)
152 return ignore_write(vcpu
, p
);
154 return read_zero(vcpu
, p
);
157 static bool trap_oslsr_el1(struct kvm_vcpu
*vcpu
,
158 struct sys_reg_params
*p
,
159 const struct sys_reg_desc
*r
)
162 return ignore_write(vcpu
, p
);
164 p
->regval
= (1 << 3);
169 static bool trap_dbgauthstatus_el1(struct kvm_vcpu
*vcpu
,
170 struct sys_reg_params
*p
,
171 const struct sys_reg_desc
*r
)
174 return ignore_write(vcpu
, p
);
176 p
->regval
= read_sysreg(dbgauthstatus_el1
);
182 * We want to avoid world-switching all the DBG registers all the
185 * - If we've touched any debug register, it is likely that we're
186 * going to touch more of them. It then makes sense to disable the
187 * traps and start doing the save/restore dance
188 * - If debug is active (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), it is
189 * then mandatory to save/restore the registers, as the guest
192 * For this, we use a DIRTY bit, indicating the guest has modified the
193 * debug registers, used as follow:
196 * - If the dirty bit is set (because we're coming back from trapping),
197 * disable the traps, save host registers, restore guest registers.
198 * - If debug is actively in use (DBG_MDSCR_KDE or DBG_MDSCR_MDE set),
199 * set the dirty bit, disable the traps, save host registers,
200 * restore guest registers.
201 * - Otherwise, enable the traps
204 * - If the dirty bit is set, save guest registers, restore host
205 * registers and clear the dirty bit. This ensure that the host can
206 * now use the debug registers.
208 static bool trap_debug_regs(struct kvm_vcpu
*vcpu
,
209 struct sys_reg_params
*p
,
210 const struct sys_reg_desc
*r
)
213 vcpu_sys_reg(vcpu
, r
->reg
) = p
->regval
;
214 vcpu
->arch
.debug_flags
|= KVM_ARM64_DEBUG_DIRTY
;
216 p
->regval
= vcpu_sys_reg(vcpu
, r
->reg
);
219 trace_trap_reg(__func__
, r
->reg
, p
->is_write
, p
->regval
);
225 * reg_to_dbg/dbg_to_reg
227 * A 32 bit write to a debug register leave top bits alone
228 * A 32 bit read from a debug register only returns the bottom bits
230 * All writes will set the KVM_ARM64_DEBUG_DIRTY flag to ensure the
231 * hyp.S code switches between host and guest values in future.
233 static void reg_to_dbg(struct kvm_vcpu
*vcpu
,
234 struct sys_reg_params
*p
,
241 val
|= ((*dbg_reg
>> 32) << 32);
245 vcpu
->arch
.debug_flags
|= KVM_ARM64_DEBUG_DIRTY
;
248 static void dbg_to_reg(struct kvm_vcpu
*vcpu
,
249 struct sys_reg_params
*p
,
252 p
->regval
= *dbg_reg
;
254 p
->regval
&= 0xffffffffUL
;
257 static bool trap_bvr(struct kvm_vcpu
*vcpu
,
258 struct sys_reg_params
*p
,
259 const struct sys_reg_desc
*rd
)
261 u64
*dbg_reg
= &vcpu
->arch
.vcpu_debug_state
.dbg_bvr
[rd
->reg
];
264 reg_to_dbg(vcpu
, p
, dbg_reg
);
266 dbg_to_reg(vcpu
, p
, dbg_reg
);
268 trace_trap_reg(__func__
, rd
->reg
, p
->is_write
, *dbg_reg
);
273 static int set_bvr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*rd
,
274 const struct kvm_one_reg
*reg
, void __user
*uaddr
)
276 __u64
*r
= &vcpu
->arch
.vcpu_debug_state
.dbg_bvr
[rd
->reg
];
278 if (copy_from_user(r
, uaddr
, KVM_REG_SIZE(reg
->id
)) != 0)
283 static int get_bvr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*rd
,
284 const struct kvm_one_reg
*reg
, void __user
*uaddr
)
286 __u64
*r
= &vcpu
->arch
.vcpu_debug_state
.dbg_bvr
[rd
->reg
];
288 if (copy_to_user(uaddr
, r
, KVM_REG_SIZE(reg
->id
)) != 0)
293 static void reset_bvr(struct kvm_vcpu
*vcpu
,
294 const struct sys_reg_desc
*rd
)
296 vcpu
->arch
.vcpu_debug_state
.dbg_bvr
[rd
->reg
] = rd
->val
;
299 static bool trap_bcr(struct kvm_vcpu
*vcpu
,
300 struct sys_reg_params
*p
,
301 const struct sys_reg_desc
*rd
)
303 u64
*dbg_reg
= &vcpu
->arch
.vcpu_debug_state
.dbg_bcr
[rd
->reg
];
306 reg_to_dbg(vcpu
, p
, dbg_reg
);
308 dbg_to_reg(vcpu
, p
, dbg_reg
);
310 trace_trap_reg(__func__
, rd
->reg
, p
->is_write
, *dbg_reg
);
315 static int set_bcr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*rd
,
316 const struct kvm_one_reg
*reg
, void __user
*uaddr
)
318 __u64
*r
= &vcpu
->arch
.vcpu_debug_state
.dbg_bcr
[rd
->reg
];
320 if (copy_from_user(r
, uaddr
, KVM_REG_SIZE(reg
->id
)) != 0)
326 static int get_bcr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*rd
,
327 const struct kvm_one_reg
*reg
, void __user
*uaddr
)
329 __u64
*r
= &vcpu
->arch
.vcpu_debug_state
.dbg_bcr
[rd
->reg
];
331 if (copy_to_user(uaddr
, r
, KVM_REG_SIZE(reg
->id
)) != 0)
336 static void reset_bcr(struct kvm_vcpu
*vcpu
,
337 const struct sys_reg_desc
*rd
)
339 vcpu
->arch
.vcpu_debug_state
.dbg_bcr
[rd
->reg
] = rd
->val
;
342 static bool trap_wvr(struct kvm_vcpu
*vcpu
,
343 struct sys_reg_params
*p
,
344 const struct sys_reg_desc
*rd
)
346 u64
*dbg_reg
= &vcpu
->arch
.vcpu_debug_state
.dbg_wvr
[rd
->reg
];
349 reg_to_dbg(vcpu
, p
, dbg_reg
);
351 dbg_to_reg(vcpu
, p
, dbg_reg
);
353 trace_trap_reg(__func__
, rd
->reg
, p
->is_write
,
354 vcpu
->arch
.vcpu_debug_state
.dbg_wvr
[rd
->reg
]);
359 static int set_wvr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*rd
,
360 const struct kvm_one_reg
*reg
, void __user
*uaddr
)
362 __u64
*r
= &vcpu
->arch
.vcpu_debug_state
.dbg_wvr
[rd
->reg
];
364 if (copy_from_user(r
, uaddr
, KVM_REG_SIZE(reg
->id
)) != 0)
369 static int get_wvr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*rd
,
370 const struct kvm_one_reg
*reg
, void __user
*uaddr
)
372 __u64
*r
= &vcpu
->arch
.vcpu_debug_state
.dbg_wvr
[rd
->reg
];
374 if (copy_to_user(uaddr
, r
, KVM_REG_SIZE(reg
->id
)) != 0)
379 static void reset_wvr(struct kvm_vcpu
*vcpu
,
380 const struct sys_reg_desc
*rd
)
382 vcpu
->arch
.vcpu_debug_state
.dbg_wvr
[rd
->reg
] = rd
->val
;
385 static bool trap_wcr(struct kvm_vcpu
*vcpu
,
386 struct sys_reg_params
*p
,
387 const struct sys_reg_desc
*rd
)
389 u64
*dbg_reg
= &vcpu
->arch
.vcpu_debug_state
.dbg_wcr
[rd
->reg
];
392 reg_to_dbg(vcpu
, p
, dbg_reg
);
394 dbg_to_reg(vcpu
, p
, dbg_reg
);
396 trace_trap_reg(__func__
, rd
->reg
, p
->is_write
, *dbg_reg
);
401 static int set_wcr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*rd
,
402 const struct kvm_one_reg
*reg
, void __user
*uaddr
)
404 __u64
*r
= &vcpu
->arch
.vcpu_debug_state
.dbg_wcr
[rd
->reg
];
406 if (copy_from_user(r
, uaddr
, KVM_REG_SIZE(reg
->id
)) != 0)
411 static int get_wcr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*rd
,
412 const struct kvm_one_reg
*reg
, void __user
*uaddr
)
414 __u64
*r
= &vcpu
->arch
.vcpu_debug_state
.dbg_wcr
[rd
->reg
];
416 if (copy_to_user(uaddr
, r
, KVM_REG_SIZE(reg
->id
)) != 0)
421 static void reset_wcr(struct kvm_vcpu
*vcpu
,
422 const struct sys_reg_desc
*rd
)
424 vcpu
->arch
.vcpu_debug_state
.dbg_wcr
[rd
->reg
] = rd
->val
;
427 static void reset_amair_el1(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*r
)
429 vcpu_sys_reg(vcpu
, AMAIR_EL1
) = read_sysreg(amair_el1
);
432 static void reset_mpidr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*r
)
437 * Map the vcpu_id into the first three affinity level fields of
438 * the MPIDR. We limit the number of VCPUs in level 0 due to a
439 * limitation to 16 CPUs in that level in the ICC_SGIxR registers
440 * of the GICv3 to be able to address each CPU directly when
443 mpidr
= (vcpu
->vcpu_id
& 0x0f) << MPIDR_LEVEL_SHIFT(0);
444 mpidr
|= ((vcpu
->vcpu_id
>> 4) & 0xff) << MPIDR_LEVEL_SHIFT(1);
445 mpidr
|= ((vcpu
->vcpu_id
>> 12) & 0xff) << MPIDR_LEVEL_SHIFT(2);
446 vcpu_sys_reg(vcpu
, MPIDR_EL1
) = (1ULL << 31) | mpidr
;
449 static void reset_pmcr(struct kvm_vcpu
*vcpu
, const struct sys_reg_desc
*r
)
453 pmcr
= read_sysreg(pmcr_el0
);
455 * Writable bits of PMCR_EL0 (ARMV8_PMU_PMCR_MASK) are reset to UNKNOWN
456 * except PMCR.E resetting to zero.
458 val
= ((pmcr
& ~ARMV8_PMU_PMCR_MASK
)
459 | (ARMV8_PMU_PMCR_MASK
& 0xdecafbad)) & (~ARMV8_PMU_PMCR_E
);
460 vcpu_sys_reg(vcpu
, PMCR_EL0
) = val
;
463 static bool pmu_access_el0_disabled(struct kvm_vcpu
*vcpu
)
465 u64 reg
= vcpu_sys_reg(vcpu
, PMUSERENR_EL0
);
467 return !((reg
& ARMV8_PMU_USERENR_EN
) || vcpu_mode_priv(vcpu
));
470 static bool pmu_write_swinc_el0_disabled(struct kvm_vcpu
*vcpu
)
472 u64 reg
= vcpu_sys_reg(vcpu
, PMUSERENR_EL0
);
474 return !((reg
& (ARMV8_PMU_USERENR_SW
| ARMV8_PMU_USERENR_EN
))
475 || vcpu_mode_priv(vcpu
));
478 static bool pmu_access_cycle_counter_el0_disabled(struct kvm_vcpu
*vcpu
)
480 u64 reg
= vcpu_sys_reg(vcpu
, PMUSERENR_EL0
);
482 return !((reg
& (ARMV8_PMU_USERENR_CR
| ARMV8_PMU_USERENR_EN
))
483 || vcpu_mode_priv(vcpu
));
486 static bool pmu_access_event_counter_el0_disabled(struct kvm_vcpu
*vcpu
)
488 u64 reg
= vcpu_sys_reg(vcpu
, PMUSERENR_EL0
);
490 return !((reg
& (ARMV8_PMU_USERENR_ER
| ARMV8_PMU_USERENR_EN
))
491 || vcpu_mode_priv(vcpu
));
494 static bool access_pmcr(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
495 const struct sys_reg_desc
*r
)
499 if (!kvm_arm_pmu_v3_ready(vcpu
))
500 return trap_raz_wi(vcpu
, p
, r
);
502 if (pmu_access_el0_disabled(vcpu
))
506 /* Only update writeable bits of PMCR */
507 val
= vcpu_sys_reg(vcpu
, PMCR_EL0
);
508 val
&= ~ARMV8_PMU_PMCR_MASK
;
509 val
|= p
->regval
& ARMV8_PMU_PMCR_MASK
;
510 vcpu_sys_reg(vcpu
, PMCR_EL0
) = val
;
511 kvm_pmu_handle_pmcr(vcpu
, val
);
513 /* PMCR.P & PMCR.C are RAZ */
514 val
= vcpu_sys_reg(vcpu
, PMCR_EL0
)
515 & ~(ARMV8_PMU_PMCR_P
| ARMV8_PMU_PMCR_C
);
522 static bool access_pmselr(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
523 const struct sys_reg_desc
*r
)
525 if (!kvm_arm_pmu_v3_ready(vcpu
))
526 return trap_raz_wi(vcpu
, p
, r
);
528 if (pmu_access_event_counter_el0_disabled(vcpu
))
532 vcpu_sys_reg(vcpu
, PMSELR_EL0
) = p
->regval
;
534 /* return PMSELR.SEL field */
535 p
->regval
= vcpu_sys_reg(vcpu
, PMSELR_EL0
)
536 & ARMV8_PMU_COUNTER_MASK
;
541 static bool access_pmceid(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
542 const struct sys_reg_desc
*r
)
546 if (!kvm_arm_pmu_v3_ready(vcpu
))
547 return trap_raz_wi(vcpu
, p
, r
);
551 if (pmu_access_el0_disabled(vcpu
))
555 pmceid
= read_sysreg(pmceid0_el0
);
557 pmceid
= read_sysreg(pmceid1_el0
);
564 static bool pmu_counter_idx_valid(struct kvm_vcpu
*vcpu
, u64 idx
)
568 pmcr
= vcpu_sys_reg(vcpu
, PMCR_EL0
);
569 val
= (pmcr
>> ARMV8_PMU_PMCR_N_SHIFT
) & ARMV8_PMU_PMCR_N_MASK
;
570 if (idx
>= val
&& idx
!= ARMV8_PMU_CYCLE_IDX
)
576 static bool access_pmu_evcntr(struct kvm_vcpu
*vcpu
,
577 struct sys_reg_params
*p
,
578 const struct sys_reg_desc
*r
)
582 if (!kvm_arm_pmu_v3_ready(vcpu
))
583 return trap_raz_wi(vcpu
, p
, r
);
585 if (r
->CRn
== 9 && r
->CRm
== 13) {
588 if (pmu_access_event_counter_el0_disabled(vcpu
))
591 idx
= vcpu_sys_reg(vcpu
, PMSELR_EL0
)
592 & ARMV8_PMU_COUNTER_MASK
;
593 } else if (r
->Op2
== 0) {
595 if (pmu_access_cycle_counter_el0_disabled(vcpu
))
598 idx
= ARMV8_PMU_CYCLE_IDX
;
602 } else if (r
->CRn
== 0 && r
->CRm
== 9) {
604 if (pmu_access_event_counter_el0_disabled(vcpu
))
607 idx
= ARMV8_PMU_CYCLE_IDX
;
608 } else if (r
->CRn
== 14 && (r
->CRm
& 12) == 8) {
610 if (pmu_access_event_counter_el0_disabled(vcpu
))
613 idx
= ((r
->CRm
& 3) << 3) | (r
->Op2
& 7);
618 if (!pmu_counter_idx_valid(vcpu
, idx
))
622 if (pmu_access_el0_disabled(vcpu
))
625 kvm_pmu_set_counter_value(vcpu
, idx
, p
->regval
);
627 p
->regval
= kvm_pmu_get_counter_value(vcpu
, idx
);
633 static bool access_pmu_evtyper(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
634 const struct sys_reg_desc
*r
)
638 if (!kvm_arm_pmu_v3_ready(vcpu
))
639 return trap_raz_wi(vcpu
, p
, r
);
641 if (pmu_access_el0_disabled(vcpu
))
644 if (r
->CRn
== 9 && r
->CRm
== 13 && r
->Op2
== 1) {
646 idx
= vcpu_sys_reg(vcpu
, PMSELR_EL0
) & ARMV8_PMU_COUNTER_MASK
;
647 reg
= PMEVTYPER0_EL0
+ idx
;
648 } else if (r
->CRn
== 14 && (r
->CRm
& 12) == 12) {
649 idx
= ((r
->CRm
& 3) << 3) | (r
->Op2
& 7);
650 if (idx
== ARMV8_PMU_CYCLE_IDX
)
654 reg
= PMEVTYPER0_EL0
+ idx
;
659 if (!pmu_counter_idx_valid(vcpu
, idx
))
663 kvm_pmu_set_counter_event_type(vcpu
, p
->regval
, idx
);
664 vcpu_sys_reg(vcpu
, reg
) = p
->regval
& ARMV8_PMU_EVTYPE_MASK
;
666 p
->regval
= vcpu_sys_reg(vcpu
, reg
) & ARMV8_PMU_EVTYPE_MASK
;
672 static bool access_pmcnten(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
673 const struct sys_reg_desc
*r
)
677 if (!kvm_arm_pmu_v3_ready(vcpu
))
678 return trap_raz_wi(vcpu
, p
, r
);
680 if (pmu_access_el0_disabled(vcpu
))
683 mask
= kvm_pmu_valid_counter_mask(vcpu
);
685 val
= p
->regval
& mask
;
687 /* accessing PMCNTENSET_EL0 */
688 vcpu_sys_reg(vcpu
, PMCNTENSET_EL0
) |= val
;
689 kvm_pmu_enable_counter(vcpu
, val
);
691 /* accessing PMCNTENCLR_EL0 */
692 vcpu_sys_reg(vcpu
, PMCNTENSET_EL0
) &= ~val
;
693 kvm_pmu_disable_counter(vcpu
, val
);
696 p
->regval
= vcpu_sys_reg(vcpu
, PMCNTENSET_EL0
) & mask
;
702 static bool access_pminten(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
703 const struct sys_reg_desc
*r
)
705 u64 mask
= kvm_pmu_valid_counter_mask(vcpu
);
707 if (!kvm_arm_pmu_v3_ready(vcpu
))
708 return trap_raz_wi(vcpu
, p
, r
);
710 if (!vcpu_mode_priv(vcpu
))
714 u64 val
= p
->regval
& mask
;
717 /* accessing PMINTENSET_EL1 */
718 vcpu_sys_reg(vcpu
, PMINTENSET_EL1
) |= val
;
720 /* accessing PMINTENCLR_EL1 */
721 vcpu_sys_reg(vcpu
, PMINTENSET_EL1
) &= ~val
;
723 p
->regval
= vcpu_sys_reg(vcpu
, PMINTENSET_EL1
) & mask
;
729 static bool access_pmovs(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
730 const struct sys_reg_desc
*r
)
732 u64 mask
= kvm_pmu_valid_counter_mask(vcpu
);
734 if (!kvm_arm_pmu_v3_ready(vcpu
))
735 return trap_raz_wi(vcpu
, p
, r
);
737 if (pmu_access_el0_disabled(vcpu
))
742 /* accessing PMOVSSET_EL0 */
743 kvm_pmu_overflow_set(vcpu
, p
->regval
& mask
);
745 /* accessing PMOVSCLR_EL0 */
746 vcpu_sys_reg(vcpu
, PMOVSSET_EL0
) &= ~(p
->regval
& mask
);
748 p
->regval
= vcpu_sys_reg(vcpu
, PMOVSSET_EL0
) & mask
;
754 static bool access_pmswinc(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
755 const struct sys_reg_desc
*r
)
759 if (!kvm_arm_pmu_v3_ready(vcpu
))
760 return trap_raz_wi(vcpu
, p
, r
);
762 if (pmu_write_swinc_el0_disabled(vcpu
))
766 mask
= kvm_pmu_valid_counter_mask(vcpu
);
767 kvm_pmu_software_increment(vcpu
, p
->regval
& mask
);
774 static bool access_pmuserenr(struct kvm_vcpu
*vcpu
, struct sys_reg_params
*p
,
775 const struct sys_reg_desc
*r
)
777 if (!kvm_arm_pmu_v3_ready(vcpu
))
778 return trap_raz_wi(vcpu
, p
, r
);
781 if (!vcpu_mode_priv(vcpu
))
784 vcpu_sys_reg(vcpu
, PMUSERENR_EL0
) = p
->regval
785 & ARMV8_PMU_USERENR_MASK
;
787 p
->regval
= vcpu_sys_reg(vcpu
, PMUSERENR_EL0
)
788 & ARMV8_PMU_USERENR_MASK
;
794 /* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
795 #define DBG_BCR_BVR_WCR_WVR_EL1(n) \
797 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b100), \
798 trap_bvr, reset_bvr, n, 0, get_bvr, set_bvr }, \
800 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b101), \
801 trap_bcr, reset_bcr, n, 0, get_bcr, set_bcr }, \
803 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b110), \
804 trap_wvr, reset_wvr, n, 0, get_wvr, set_wvr }, \
806 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b111), \
807 trap_wcr, reset_wcr, n, 0, get_wcr, set_wcr }
809 /* Macro to expand the PMEVCNTRn_EL0 register */
810 #define PMU_PMEVCNTR_EL0(n) \
811 /* PMEVCNTRn_EL0 */ \
812 { Op0(0b11), Op1(0b011), CRn(0b1110), \
813 CRm((0b1000 | (((n) >> 3) & 0x3))), Op2(((n) & 0x7)), \
814 access_pmu_evcntr, reset_unknown, (PMEVCNTR0_EL0 + n), }
816 /* Macro to expand the PMEVTYPERn_EL0 register */
817 #define PMU_PMEVTYPER_EL0(n) \
818 /* PMEVTYPERn_EL0 */ \
819 { Op0(0b11), Op1(0b011), CRn(0b1110), \
820 CRm((0b1100 | (((n) >> 3) & 0x3))), Op2(((n) & 0x7)), \
821 access_pmu_evtyper, reset_unknown, (PMEVTYPER0_EL0 + n), }
824 * Architected system registers.
825 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
827 * Debug handling: We do trap most, if not all debug related system
828 * registers. The implementation is good enough to ensure that a guest
829 * can use these with minimal performance degradation. The drawback is
830 * that we don't implement any of the external debug, none of the
831 * OSlock protocol. This should be revisited if we ever encounter a
832 * more demanding guest...
834 static const struct sys_reg_desc sys_reg_descs
[] = {
836 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b0110), Op2(0b010),
839 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1010), Op2(0b010),
842 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b010),
845 DBG_BCR_BVR_WCR_WVR_EL1(0),
846 DBG_BCR_BVR_WCR_WVR_EL1(1),
848 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
849 trap_debug_regs
, reset_val
, MDCCINT_EL1
, 0 },
851 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
852 trap_debug_regs
, reset_val
, MDSCR_EL1
, 0 },
853 DBG_BCR_BVR_WCR_WVR_EL1(2),
854 DBG_BCR_BVR_WCR_WVR_EL1(3),
855 DBG_BCR_BVR_WCR_WVR_EL1(4),
856 DBG_BCR_BVR_WCR_WVR_EL1(5),
857 DBG_BCR_BVR_WCR_WVR_EL1(6),
858 DBG_BCR_BVR_WCR_WVR_EL1(7),
859 DBG_BCR_BVR_WCR_WVR_EL1(8),
860 DBG_BCR_BVR_WCR_WVR_EL1(9),
861 DBG_BCR_BVR_WCR_WVR_EL1(10),
862 DBG_BCR_BVR_WCR_WVR_EL1(11),
863 DBG_BCR_BVR_WCR_WVR_EL1(12),
864 DBG_BCR_BVR_WCR_WVR_EL1(13),
865 DBG_BCR_BVR_WCR_WVR_EL1(14),
866 DBG_BCR_BVR_WCR_WVR_EL1(15),
869 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
872 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b100),
875 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0001), Op2(0b100),
878 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0011), Op2(0b100),
881 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0100), Op2(0b100),
883 /* DBGCLAIMSET_EL1 */
884 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1000), Op2(0b110),
886 /* DBGCLAIMCLR_EL1 */
887 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1001), Op2(0b110),
889 /* DBGAUTHSTATUS_EL1 */
890 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b110),
891 trap_dbgauthstatus_el1
},
894 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0001), Op2(0b000),
897 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0100), Op2(0b000),
899 /* DBGDTR[TR]X_EL0 */
900 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0101), Op2(0b000),
904 { Op0(0b10), Op1(0b100), CRn(0b0000), CRm(0b0111), Op2(0b000),
905 NULL
, reset_val
, DBGVCR32_EL2
, 0 },
908 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b101),
909 NULL
, reset_mpidr
, MPIDR_EL1
},
911 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
912 access_vm_reg
, reset_val
, SCTLR_EL1
, 0x00C50078 },
914 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b010),
915 NULL
, reset_val
, CPACR_EL1
, 0 },
917 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b000),
918 access_vm_reg
, reset_unknown
, TTBR0_EL1
},
920 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b001),
921 access_vm_reg
, reset_unknown
, TTBR1_EL1
},
923 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b010),
924 access_vm_reg
, reset_val
, TCR_EL1
, 0 },
927 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b000),
928 access_vm_reg
, reset_unknown
, AFSR0_EL1
},
930 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b001),
931 access_vm_reg
, reset_unknown
, AFSR1_EL1
},
933 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0010), Op2(0b000),
934 access_vm_reg
, reset_unknown
, ESR_EL1
},
936 { Op0(0b11), Op1(0b000), CRn(0b0110), CRm(0b0000), Op2(0b000),
937 access_vm_reg
, reset_unknown
, FAR_EL1
},
939 { Op0(0b11), Op1(0b000), CRn(0b0111), CRm(0b0100), Op2(0b000),
940 NULL
, reset_unknown
, PAR_EL1
},
943 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b001),
944 access_pminten
, reset_unknown
, PMINTENSET_EL1
},
946 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b010),
947 access_pminten
, NULL
, PMINTENSET_EL1
},
950 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0010), Op2(0b000),
951 access_vm_reg
, reset_unknown
, MAIR_EL1
},
953 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0011), Op2(0b000),
954 access_vm_reg
, reset_amair_el1
, AMAIR_EL1
},
957 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b0000), Op2(0b000),
958 NULL
, reset_val
, VBAR_EL1
, 0 },
961 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b1011), Op2(0b101),
964 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b1100), Op2(0b101),
968 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b001),
969 access_vm_reg
, reset_val
, CONTEXTIDR_EL1
, 0 },
971 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b100),
972 NULL
, reset_unknown
, TPIDR_EL1
},
975 { Op0(0b11), Op1(0b000), CRn(0b1110), CRm(0b0001), Op2(0b000),
976 NULL
, reset_val
, CNTKCTL_EL1
, 0},
979 { Op0(0b11), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
980 NULL
, reset_unknown
, CSSELR_EL1
},
983 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b000),
984 access_pmcr
, reset_pmcr
, },
986 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b001),
987 access_pmcnten
, reset_unknown
, PMCNTENSET_EL0
},
989 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b010),
990 access_pmcnten
, NULL
, PMCNTENSET_EL0
},
992 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b011),
993 access_pmovs
, NULL
, PMOVSSET_EL0
},
995 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b100),
996 access_pmswinc
, reset_unknown
, PMSWINC_EL0
},
998 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b101),
999 access_pmselr
, reset_unknown
, PMSELR_EL0
},
1001 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b110),
1004 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b111),
1007 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b000),
1008 access_pmu_evcntr
, reset_unknown
, PMCCNTR_EL0
},
1009 /* PMXEVTYPER_EL0 */
1010 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b001),
1011 access_pmu_evtyper
},
1013 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b010),
1014 access_pmu_evcntr
},
1016 * This register resets as unknown in 64bit mode while it resets as zero
1017 * in 32bit mode. Here we choose to reset it as zero for consistency.
1019 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b000),
1020 access_pmuserenr
, reset_val
, PMUSERENR_EL0
, 0 },
1022 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b011),
1023 access_pmovs
, reset_unknown
, PMOVSSET_EL0
},
1026 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b010),
1027 NULL
, reset_unknown
, TPIDR_EL0
},
1029 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b011),
1030 NULL
, reset_unknown
, TPIDRRO_EL0
},
1033 PMU_PMEVCNTR_EL0(0),
1034 PMU_PMEVCNTR_EL0(1),
1035 PMU_PMEVCNTR_EL0(2),
1036 PMU_PMEVCNTR_EL0(3),
1037 PMU_PMEVCNTR_EL0(4),
1038 PMU_PMEVCNTR_EL0(5),
1039 PMU_PMEVCNTR_EL0(6),
1040 PMU_PMEVCNTR_EL0(7),
1041 PMU_PMEVCNTR_EL0(8),
1042 PMU_PMEVCNTR_EL0(9),
1043 PMU_PMEVCNTR_EL0(10),
1044 PMU_PMEVCNTR_EL0(11),
1045 PMU_PMEVCNTR_EL0(12),
1046 PMU_PMEVCNTR_EL0(13),
1047 PMU_PMEVCNTR_EL0(14),
1048 PMU_PMEVCNTR_EL0(15),
1049 PMU_PMEVCNTR_EL0(16),
1050 PMU_PMEVCNTR_EL0(17),
1051 PMU_PMEVCNTR_EL0(18),
1052 PMU_PMEVCNTR_EL0(19),
1053 PMU_PMEVCNTR_EL0(20),
1054 PMU_PMEVCNTR_EL0(21),
1055 PMU_PMEVCNTR_EL0(22),
1056 PMU_PMEVCNTR_EL0(23),
1057 PMU_PMEVCNTR_EL0(24),
1058 PMU_PMEVCNTR_EL0(25),
1059 PMU_PMEVCNTR_EL0(26),
1060 PMU_PMEVCNTR_EL0(27),
1061 PMU_PMEVCNTR_EL0(28),
1062 PMU_PMEVCNTR_EL0(29),
1063 PMU_PMEVCNTR_EL0(30),
1064 /* PMEVTYPERn_EL0 */
1065 PMU_PMEVTYPER_EL0(0),
1066 PMU_PMEVTYPER_EL0(1),
1067 PMU_PMEVTYPER_EL0(2),
1068 PMU_PMEVTYPER_EL0(3),
1069 PMU_PMEVTYPER_EL0(4),
1070 PMU_PMEVTYPER_EL0(5),
1071 PMU_PMEVTYPER_EL0(6),
1072 PMU_PMEVTYPER_EL0(7),
1073 PMU_PMEVTYPER_EL0(8),
1074 PMU_PMEVTYPER_EL0(9),
1075 PMU_PMEVTYPER_EL0(10),
1076 PMU_PMEVTYPER_EL0(11),
1077 PMU_PMEVTYPER_EL0(12),
1078 PMU_PMEVTYPER_EL0(13),
1079 PMU_PMEVTYPER_EL0(14),
1080 PMU_PMEVTYPER_EL0(15),
1081 PMU_PMEVTYPER_EL0(16),
1082 PMU_PMEVTYPER_EL0(17),
1083 PMU_PMEVTYPER_EL0(18),
1084 PMU_PMEVTYPER_EL0(19),
1085 PMU_PMEVTYPER_EL0(20),
1086 PMU_PMEVTYPER_EL0(21),
1087 PMU_PMEVTYPER_EL0(22),
1088 PMU_PMEVTYPER_EL0(23),
1089 PMU_PMEVTYPER_EL0(24),
1090 PMU_PMEVTYPER_EL0(25),
1091 PMU_PMEVTYPER_EL0(26),
1092 PMU_PMEVTYPER_EL0(27),
1093 PMU_PMEVTYPER_EL0(28),
1094 PMU_PMEVTYPER_EL0(29),
1095 PMU_PMEVTYPER_EL0(30),
1097 * This register resets as unknown in 64bit mode while it resets as zero
1098 * in 32bit mode. Here we choose to reset it as zero for consistency.
1100 { Op0(0b11), Op1(0b011), CRn(0b1110), CRm(0b1111), Op2(0b111),
1101 access_pmu_evtyper
, reset_val
, PMCCFILTR_EL0
, 0 },
1104 { Op0(0b11), Op1(0b100), CRn(0b0011), CRm(0b0000), Op2(0b000),
1105 NULL
, reset_unknown
, DACR32_EL2
},
1107 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0000), Op2(0b001),
1108 NULL
, reset_unknown
, IFSR32_EL2
},
1110 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0011), Op2(0b000),
1111 NULL
, reset_val
, FPEXC32_EL2
, 0x70 },
1114 static bool trap_dbgidr(struct kvm_vcpu
*vcpu
,
1115 struct sys_reg_params
*p
,
1116 const struct sys_reg_desc
*r
)
1119 return ignore_write(vcpu
, p
);
1121 u64 dfr
= read_system_reg(SYS_ID_AA64DFR0_EL1
);
1122 u64 pfr
= read_system_reg(SYS_ID_AA64PFR0_EL1
);
1123 u32 el3
= !!cpuid_feature_extract_unsigned_field(pfr
, ID_AA64PFR0_EL3_SHIFT
);
1125 p
->regval
= ((((dfr
>> ID_AA64DFR0_WRPS_SHIFT
) & 0xf) << 28) |
1126 (((dfr
>> ID_AA64DFR0_BRPS_SHIFT
) & 0xf) << 24) |
1127 (((dfr
>> ID_AA64DFR0_CTX_CMPS_SHIFT
) & 0xf) << 20)
1128 | (6 << 16) | (el3
<< 14) | (el3
<< 12));
1133 static bool trap_debug32(struct kvm_vcpu
*vcpu
,
1134 struct sys_reg_params
*p
,
1135 const struct sys_reg_desc
*r
)
1138 vcpu_cp14(vcpu
, r
->reg
) = p
->regval
;
1139 vcpu
->arch
.debug_flags
|= KVM_ARM64_DEBUG_DIRTY
;
1141 p
->regval
= vcpu_cp14(vcpu
, r
->reg
);
1147 /* AArch32 debug register mappings
1149 * AArch32 DBGBVRn is mapped to DBGBVRn_EL1[31:0]
1150 * AArch32 DBGBXVRn is mapped to DBGBVRn_EL1[63:32]
1152 * All control registers and watchpoint value registers are mapped to
1153 * the lower 32 bits of their AArch64 equivalents. We share the trap
1154 * handlers with the above AArch64 code which checks what mode the
1158 static bool trap_xvr(struct kvm_vcpu
*vcpu
,
1159 struct sys_reg_params
*p
,
1160 const struct sys_reg_desc
*rd
)
1162 u64
*dbg_reg
= &vcpu
->arch
.vcpu_debug_state
.dbg_bvr
[rd
->reg
];
1167 val
&= 0xffffffffUL
;
1168 val
|= p
->regval
<< 32;
1171 vcpu
->arch
.debug_flags
|= KVM_ARM64_DEBUG_DIRTY
;
1173 p
->regval
= *dbg_reg
>> 32;
1176 trace_trap_reg(__func__
, rd
->reg
, p
->is_write
, *dbg_reg
);
1181 #define DBG_BCR_BVR_WCR_WVR(n) \
1183 { Op1( 0), CRn( 0), CRm((n)), Op2( 4), trap_bvr, NULL, n }, \
1185 { Op1( 0), CRn( 0), CRm((n)), Op2( 5), trap_bcr, NULL, n }, \
1187 { Op1( 0), CRn( 0), CRm((n)), Op2( 6), trap_wvr, NULL, n }, \
1189 { Op1( 0), CRn( 0), CRm((n)), Op2( 7), trap_wcr, NULL, n }
1191 #define DBGBXVR(n) \
1192 { Op1( 0), CRn( 1), CRm((n)), Op2( 1), trap_xvr, NULL, n }
1195 * Trapped cp14 registers. We generally ignore most of the external
1196 * debug, on the principle that they don't really make sense to a
1197 * guest. Revisit this one day, would this principle change.
1199 static const struct sys_reg_desc cp14_regs
[] = {
1201 { Op1( 0), CRn( 0), CRm( 0), Op2( 0), trap_dbgidr
},
1203 { Op1( 0), CRn( 0), CRm( 0), Op2( 2), trap_raz_wi
},
1205 DBG_BCR_BVR_WCR_WVR(0),
1207 { Op1( 0), CRn( 0), CRm( 1), Op2( 0), trap_raz_wi
},
1208 DBG_BCR_BVR_WCR_WVR(1),
1210 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), trap_debug32
},
1212 { Op1( 0), CRn( 0), CRm( 2), Op2( 2), trap_debug32
},
1213 DBG_BCR_BVR_WCR_WVR(2),
1214 /* DBGDTR[RT]Xint */
1215 { Op1( 0), CRn( 0), CRm( 3), Op2( 0), trap_raz_wi
},
1216 /* DBGDTR[RT]Xext */
1217 { Op1( 0), CRn( 0), CRm( 3), Op2( 2), trap_raz_wi
},
1218 DBG_BCR_BVR_WCR_WVR(3),
1219 DBG_BCR_BVR_WCR_WVR(4),
1220 DBG_BCR_BVR_WCR_WVR(5),
1222 { Op1( 0), CRn( 0), CRm( 6), Op2( 0), trap_raz_wi
},
1224 { Op1( 0), CRn( 0), CRm( 6), Op2( 2), trap_raz_wi
},
1225 DBG_BCR_BVR_WCR_WVR(6),
1227 { Op1( 0), CRn( 0), CRm( 7), Op2( 0), trap_debug32
},
1228 DBG_BCR_BVR_WCR_WVR(7),
1229 DBG_BCR_BVR_WCR_WVR(8),
1230 DBG_BCR_BVR_WCR_WVR(9),
1231 DBG_BCR_BVR_WCR_WVR(10),
1232 DBG_BCR_BVR_WCR_WVR(11),
1233 DBG_BCR_BVR_WCR_WVR(12),
1234 DBG_BCR_BVR_WCR_WVR(13),
1235 DBG_BCR_BVR_WCR_WVR(14),
1236 DBG_BCR_BVR_WCR_WVR(15),
1238 /* DBGDRAR (32bit) */
1239 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), trap_raz_wi
},
1243 { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi
},
1246 { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1
},
1250 { Op1( 0), CRn( 1), CRm( 3), Op2( 4), trap_raz_wi
},
1253 { Op1( 0), CRn( 1), CRm( 4), Op2( 4), trap_raz_wi
},
1266 /* DBGDSAR (32bit) */
1267 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), trap_raz_wi
},
1270 { Op1( 0), CRn( 7), CRm( 0), Op2( 7), trap_raz_wi
},
1272 { Op1( 0), CRn( 7), CRm( 1), Op2( 7), trap_raz_wi
},
1274 { Op1( 0), CRn( 7), CRm( 2), Op2( 7), trap_raz_wi
},
1276 { Op1( 0), CRn( 7), CRm( 8), Op2( 6), trap_raz_wi
},
1278 { Op1( 0), CRn( 7), CRm( 9), Op2( 6), trap_raz_wi
},
1280 { Op1( 0), CRn( 7), CRm(14), Op2( 6), trap_dbgauthstatus_el1
},
1283 /* Trapped cp14 64bit registers */
1284 static const struct sys_reg_desc cp14_64_regs
[] = {
1285 /* DBGDRAR (64bit) */
1286 { Op1( 0), CRm( 1), .access
= trap_raz_wi
},
1288 /* DBGDSAR (64bit) */
1289 { Op1( 0), CRm( 2), .access
= trap_raz_wi
},
1292 /* Macro to expand the PMEVCNTRn register */
1293 #define PMU_PMEVCNTR(n) \
1295 { Op1(0), CRn(0b1110), \
1296 CRm((0b1000 | (((n) >> 3) & 0x3))), Op2(((n) & 0x7)), \
1299 /* Macro to expand the PMEVTYPERn register */
1300 #define PMU_PMEVTYPER(n) \
1302 { Op1(0), CRn(0b1110), \
1303 CRm((0b1100 | (((n) >> 3) & 0x3))), Op2(((n) & 0x7)), \
1304 access_pmu_evtyper }
1307 * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding,
1308 * depending on the way they are accessed (as a 32bit or a 64bit
1311 static const struct sys_reg_desc cp15_regs
[] = {
1312 { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi
},
1314 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), access_vm_reg
, NULL
, c1_SCTLR
},
1315 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), access_vm_reg
, NULL
, c2_TTBR0
},
1316 { Op1( 0), CRn( 2), CRm( 0), Op2( 1), access_vm_reg
, NULL
, c2_TTBR1
},
1317 { Op1( 0), CRn( 2), CRm( 0), Op2( 2), access_vm_reg
, NULL
, c2_TTBCR
},
1318 { Op1( 0), CRn( 3), CRm( 0), Op2( 0), access_vm_reg
, NULL
, c3_DACR
},
1319 { Op1( 0), CRn( 5), CRm( 0), Op2( 0), access_vm_reg
, NULL
, c5_DFSR
},
1320 { Op1( 0), CRn( 5), CRm( 0), Op2( 1), access_vm_reg
, NULL
, c5_IFSR
},
1321 { Op1( 0), CRn( 5), CRm( 1), Op2( 0), access_vm_reg
, NULL
, c5_ADFSR
},
1322 { Op1( 0), CRn( 5), CRm( 1), Op2( 1), access_vm_reg
, NULL
, c5_AIFSR
},
1323 { Op1( 0), CRn( 6), CRm( 0), Op2( 0), access_vm_reg
, NULL
, c6_DFAR
},
1324 { Op1( 0), CRn( 6), CRm( 0), Op2( 2), access_vm_reg
, NULL
, c6_IFAR
},
1327 * DC{C,I,CI}SW operations:
1329 { Op1( 0), CRn( 7), CRm( 6), Op2( 2), access_dcsw
},
1330 { Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw
},
1331 { Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw
},
1334 { Op1( 0), CRn( 9), CRm(12), Op2( 0), access_pmcr
},
1335 { Op1( 0), CRn( 9), CRm(12), Op2( 1), access_pmcnten
},
1336 { Op1( 0), CRn( 9), CRm(12), Op2( 2), access_pmcnten
},
1337 { Op1( 0), CRn( 9), CRm(12), Op2( 3), access_pmovs
},
1338 { Op1( 0), CRn( 9), CRm(12), Op2( 4), access_pmswinc
},
1339 { Op1( 0), CRn( 9), CRm(12), Op2( 5), access_pmselr
},
1340 { Op1( 0), CRn( 9), CRm(12), Op2( 6), access_pmceid
},
1341 { Op1( 0), CRn( 9), CRm(12), Op2( 7), access_pmceid
},
1342 { Op1( 0), CRn( 9), CRm(13), Op2( 0), access_pmu_evcntr
},
1343 { Op1( 0), CRn( 9), CRm(13), Op2( 1), access_pmu_evtyper
},
1344 { Op1( 0), CRn( 9), CRm(13), Op2( 2), access_pmu_evcntr
},
1345 { Op1( 0), CRn( 9), CRm(14), Op2( 0), access_pmuserenr
},
1346 { Op1( 0), CRn( 9), CRm(14), Op2( 1), access_pminten
},
1347 { Op1( 0), CRn( 9), CRm(14), Op2( 2), access_pminten
},
1348 { Op1( 0), CRn( 9), CRm(14), Op2( 3), access_pmovs
},
1350 { Op1( 0), CRn(10), CRm( 2), Op2( 0), access_vm_reg
, NULL
, c10_PRRR
},
1351 { Op1( 0), CRn(10), CRm( 2), Op2( 1), access_vm_reg
, NULL
, c10_NMRR
},
1352 { Op1( 0), CRn(10), CRm( 3), Op2( 0), access_vm_reg
, NULL
, c10_AMAIR0
},
1353 { Op1( 0), CRn(10), CRm( 3), Op2( 1), access_vm_reg
, NULL
, c10_AMAIR1
},
1356 { Op1( 0), CRn(12), CRm(12), Op2( 5), access_gic_sre
},
1358 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg
, NULL
, c13_CID
},
1425 { Op1(0), CRn(14), CRm(15), Op2(7), access_pmu_evtyper
},
1428 static const struct sys_reg_desc cp15_64_regs
[] = {
1429 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg
, NULL
, c2_TTBR0
},
1430 { Op1( 0), CRn( 0), CRm( 9), Op2( 0), access_pmu_evcntr
},
1431 { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi
},
1432 { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg
, NULL
, c2_TTBR1
},
1435 /* Target specific emulation tables */
1436 static struct kvm_sys_reg_target_table
*target_tables
[KVM_ARM_NUM_TARGETS
];
1438 void kvm_register_target_sys_reg_table(unsigned int target
,
1439 struct kvm_sys_reg_target_table
*table
)
1441 target_tables
[target
] = table
;
1444 /* Get specific register table for this target. */
1445 static const struct sys_reg_desc
*get_target_table(unsigned target
,
1449 struct kvm_sys_reg_target_table
*table
;
1451 table
= target_tables
[target
];
1453 *num
= table
->table64
.num
;
1454 return table
->table64
.table
;
1456 *num
= table
->table32
.num
;
1457 return table
->table32
.table
;
1461 #define reg_to_match_value(x) \
1463 unsigned long val; \
1464 val = (x)->Op0 << 14; \
1465 val |= (x)->Op1 << 11; \
1466 val |= (x)->CRn << 7; \
1467 val |= (x)->CRm << 3; \
1472 static int match_sys_reg(const void *key
, const void *elt
)
1474 const unsigned long pval
= (unsigned long)key
;
1475 const struct sys_reg_desc
*r
= elt
;
1477 return pval
- reg_to_match_value(r
);
1480 static const struct sys_reg_desc
*find_reg(const struct sys_reg_params
*params
,
1481 const struct sys_reg_desc table
[],
1484 unsigned long pval
= reg_to_match_value(params
);
1486 return bsearch((void *)pval
, table
, num
, sizeof(table
[0]), match_sys_reg
);
1489 int kvm_handle_cp14_load_store(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1491 kvm_inject_undefined(vcpu
);
1496 * emulate_cp -- tries to match a sys_reg access in a handling table, and
1497 * call the corresponding trap handler.
1499 * @params: pointer to the descriptor of the access
1500 * @table: array of trap descriptors
1501 * @num: size of the trap descriptor array
1503 * Return 0 if the access has been handled, and -1 if not.
1505 static int emulate_cp(struct kvm_vcpu
*vcpu
,
1506 struct sys_reg_params
*params
,
1507 const struct sys_reg_desc
*table
,
1510 const struct sys_reg_desc
*r
;
1513 return -1; /* Not handled */
1515 r
= find_reg(params
, table
, num
);
1519 * Not having an accessor means that we have
1520 * configured a trap that we don't know how to
1521 * handle. This certainly qualifies as a gross bug
1522 * that should be fixed right away.
1526 if (likely(r
->access(vcpu
, params
, r
))) {
1527 /* Skip instruction, since it was emulated */
1528 kvm_skip_instr(vcpu
, kvm_vcpu_trap_il_is32bit(vcpu
));
1538 static void unhandled_cp_access(struct kvm_vcpu
*vcpu
,
1539 struct sys_reg_params
*params
)
1541 u8 hsr_ec
= kvm_vcpu_trap_get_class(vcpu
);
1545 case ESR_ELx_EC_CP15_32
:
1546 case ESR_ELx_EC_CP15_64
:
1549 case ESR_ELx_EC_CP14_MR
:
1550 case ESR_ELx_EC_CP14_64
:
1557 kvm_err("Unsupported guest CP%d access at: %08lx\n",
1558 cp
, *vcpu_pc(vcpu
));
1559 print_sys_reg_instr(params
);
1560 kvm_inject_undefined(vcpu
);
1564 * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP14/CP15 access
1565 * @vcpu: The VCPU pointer
1566 * @run: The kvm_run struct
1568 static int kvm_handle_cp_64(struct kvm_vcpu
*vcpu
,
1569 const struct sys_reg_desc
*global
,
1571 const struct sys_reg_desc
*target_specific
,
1574 struct sys_reg_params params
;
1575 u32 hsr
= kvm_vcpu_get_hsr(vcpu
);
1576 int Rt
= (hsr
>> 5) & 0xf;
1577 int Rt2
= (hsr
>> 10) & 0xf;
1579 params
.is_aarch32
= true;
1580 params
.is_32bit
= false;
1581 params
.CRm
= (hsr
>> 1) & 0xf;
1582 params
.is_write
= ((hsr
& 1) == 0);
1585 params
.Op1
= (hsr
>> 16) & 0xf;
1590 * Make a 64-bit value out of Rt and Rt2. As we use the same trap
1591 * backends between AArch32 and AArch64, we get away with it.
1593 if (params
.is_write
) {
1594 params
.regval
= vcpu_get_reg(vcpu
, Rt
) & 0xffffffff;
1595 params
.regval
|= vcpu_get_reg(vcpu
, Rt2
) << 32;
1598 if (!emulate_cp(vcpu
, ¶ms
, target_specific
, nr_specific
))
1600 if (!emulate_cp(vcpu
, ¶ms
, global
, nr_global
))
1603 unhandled_cp_access(vcpu
, ¶ms
);
1606 /* Split up the value between registers for the read side */
1607 if (!params
.is_write
) {
1608 vcpu_set_reg(vcpu
, Rt
, lower_32_bits(params
.regval
));
1609 vcpu_set_reg(vcpu
, Rt2
, upper_32_bits(params
.regval
));
1616 * kvm_handle_cp_32 -- handles a mrc/mcr trap on a guest CP14/CP15 access
1617 * @vcpu: The VCPU pointer
1618 * @run: The kvm_run struct
1620 static int kvm_handle_cp_32(struct kvm_vcpu
*vcpu
,
1621 const struct sys_reg_desc
*global
,
1623 const struct sys_reg_desc
*target_specific
,
1626 struct sys_reg_params params
;
1627 u32 hsr
= kvm_vcpu_get_hsr(vcpu
);
1628 int Rt
= (hsr
>> 5) & 0xf;
1630 params
.is_aarch32
= true;
1631 params
.is_32bit
= true;
1632 params
.CRm
= (hsr
>> 1) & 0xf;
1633 params
.regval
= vcpu_get_reg(vcpu
, Rt
);
1634 params
.is_write
= ((hsr
& 1) == 0);
1635 params
.CRn
= (hsr
>> 10) & 0xf;
1637 params
.Op1
= (hsr
>> 14) & 0x7;
1638 params
.Op2
= (hsr
>> 17) & 0x7;
1640 if (!emulate_cp(vcpu
, ¶ms
, target_specific
, nr_specific
) ||
1641 !emulate_cp(vcpu
, ¶ms
, global
, nr_global
)) {
1642 if (!params
.is_write
)
1643 vcpu_set_reg(vcpu
, Rt
, params
.regval
);
1647 unhandled_cp_access(vcpu
, ¶ms
);
1651 int kvm_handle_cp15_64(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1653 const struct sys_reg_desc
*target_specific
;
1656 target_specific
= get_target_table(vcpu
->arch
.target
, false, &num
);
1657 return kvm_handle_cp_64(vcpu
,
1658 cp15_64_regs
, ARRAY_SIZE(cp15_64_regs
),
1659 target_specific
, num
);
1662 int kvm_handle_cp15_32(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1664 const struct sys_reg_desc
*target_specific
;
1667 target_specific
= get_target_table(vcpu
->arch
.target
, false, &num
);
1668 return kvm_handle_cp_32(vcpu
,
1669 cp15_regs
, ARRAY_SIZE(cp15_regs
),
1670 target_specific
, num
);
1673 int kvm_handle_cp14_64(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1675 return kvm_handle_cp_64(vcpu
,
1676 cp14_64_regs
, ARRAY_SIZE(cp14_64_regs
),
1680 int kvm_handle_cp14_32(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1682 return kvm_handle_cp_32(vcpu
,
1683 cp14_regs
, ARRAY_SIZE(cp14_regs
),
1687 static int emulate_sys_reg(struct kvm_vcpu
*vcpu
,
1688 struct sys_reg_params
*params
)
1691 const struct sys_reg_desc
*table
, *r
;
1693 table
= get_target_table(vcpu
->arch
.target
, true, &num
);
1695 /* Search target-specific then generic table. */
1696 r
= find_reg(params
, table
, num
);
1698 r
= find_reg(params
, sys_reg_descs
, ARRAY_SIZE(sys_reg_descs
));
1702 * Not having an accessor means that we have
1703 * configured a trap that we don't know how to
1704 * handle. This certainly qualifies as a gross bug
1705 * that should be fixed right away.
1709 if (likely(r
->access(vcpu
, params
, r
))) {
1710 /* Skip instruction, since it was emulated */
1711 kvm_skip_instr(vcpu
, kvm_vcpu_trap_il_is32bit(vcpu
));
1714 /* If access function fails, it should complain. */
1716 kvm_err("Unsupported guest sys_reg access at: %lx\n",
1718 print_sys_reg_instr(params
);
1720 kvm_inject_undefined(vcpu
);
1724 static void reset_sys_reg_descs(struct kvm_vcpu
*vcpu
,
1725 const struct sys_reg_desc
*table
, size_t num
)
1729 for (i
= 0; i
< num
; i
++)
1731 table
[i
].reset(vcpu
, &table
[i
]);
1735 * kvm_handle_sys_reg -- handles a mrs/msr trap on a guest sys_reg access
1736 * @vcpu: The VCPU pointer
1737 * @run: The kvm_run struct
1739 int kvm_handle_sys_reg(struct kvm_vcpu
*vcpu
, struct kvm_run
*run
)
1741 struct sys_reg_params params
;
1742 unsigned long esr
= kvm_vcpu_get_hsr(vcpu
);
1743 int Rt
= (esr
>> 5) & 0x1f;
1746 trace_kvm_handle_sys_reg(esr
);
1748 params
.is_aarch32
= false;
1749 params
.is_32bit
= false;
1750 params
.Op0
= (esr
>> 20) & 3;
1751 params
.Op1
= (esr
>> 14) & 0x7;
1752 params
.CRn
= (esr
>> 10) & 0xf;
1753 params
.CRm
= (esr
>> 1) & 0xf;
1754 params
.Op2
= (esr
>> 17) & 0x7;
1755 params
.regval
= vcpu_get_reg(vcpu
, Rt
);
1756 params
.is_write
= !(esr
& 1);
1758 ret
= emulate_sys_reg(vcpu
, ¶ms
);
1760 if (!params
.is_write
)
1761 vcpu_set_reg(vcpu
, Rt
, params
.regval
);
1765 /******************************************************************************
1767 *****************************************************************************/
1769 static bool index_to_params(u64 id
, struct sys_reg_params
*params
)
1771 switch (id
& KVM_REG_SIZE_MASK
) {
1772 case KVM_REG_SIZE_U64
:
1773 /* Any unused index bits means it's not valid. */
1774 if (id
& ~(KVM_REG_ARCH_MASK
| KVM_REG_SIZE_MASK
1775 | KVM_REG_ARM_COPROC_MASK
1776 | KVM_REG_ARM64_SYSREG_OP0_MASK
1777 | KVM_REG_ARM64_SYSREG_OP1_MASK
1778 | KVM_REG_ARM64_SYSREG_CRN_MASK
1779 | KVM_REG_ARM64_SYSREG_CRM_MASK
1780 | KVM_REG_ARM64_SYSREG_OP2_MASK
))
1782 params
->Op0
= ((id
& KVM_REG_ARM64_SYSREG_OP0_MASK
)
1783 >> KVM_REG_ARM64_SYSREG_OP0_SHIFT
);
1784 params
->Op1
= ((id
& KVM_REG_ARM64_SYSREG_OP1_MASK
)
1785 >> KVM_REG_ARM64_SYSREG_OP1_SHIFT
);
1786 params
->CRn
= ((id
& KVM_REG_ARM64_SYSREG_CRN_MASK
)
1787 >> KVM_REG_ARM64_SYSREG_CRN_SHIFT
);
1788 params
->CRm
= ((id
& KVM_REG_ARM64_SYSREG_CRM_MASK
)
1789 >> KVM_REG_ARM64_SYSREG_CRM_SHIFT
);
1790 params
->Op2
= ((id
& KVM_REG_ARM64_SYSREG_OP2_MASK
)
1791 >> KVM_REG_ARM64_SYSREG_OP2_SHIFT
);
1798 /* Decode an index value, and find the sys_reg_desc entry. */
1799 static const struct sys_reg_desc
*index_to_sys_reg_desc(struct kvm_vcpu
*vcpu
,
1803 const struct sys_reg_desc
*table
, *r
;
1804 struct sys_reg_params params
;
1806 /* We only do sys_reg for now. */
1807 if ((id
& KVM_REG_ARM_COPROC_MASK
) != KVM_REG_ARM64_SYSREG
)
1810 if (!index_to_params(id
, ¶ms
))
1813 table
= get_target_table(vcpu
->arch
.target
, true, &num
);
1814 r
= find_reg(¶ms
, table
, num
);
1816 r
= find_reg(¶ms
, sys_reg_descs
, ARRAY_SIZE(sys_reg_descs
));
1818 /* Not saved in the sys_reg array? */
1826 * These are the invariant sys_reg registers: we let the guest see the
1827 * host versions of these, so they're part of the guest state.
1829 * A future CPU may provide a mechanism to present different values to
1830 * the guest, or a future kvm may trap them.
1833 #define FUNCTION_INVARIANT(reg) \
1834 static void get_##reg(struct kvm_vcpu *v, \
1835 const struct sys_reg_desc *r) \
1837 ((struct sys_reg_desc *)r)->val = read_sysreg(reg); \
1840 FUNCTION_INVARIANT(midr_el1
)
1841 FUNCTION_INVARIANT(ctr_el0
)
1842 FUNCTION_INVARIANT(revidr_el1
)
1843 FUNCTION_INVARIANT(id_pfr0_el1
)
1844 FUNCTION_INVARIANT(id_pfr1_el1
)
1845 FUNCTION_INVARIANT(id_dfr0_el1
)
1846 FUNCTION_INVARIANT(id_afr0_el1
)
1847 FUNCTION_INVARIANT(id_mmfr0_el1
)
1848 FUNCTION_INVARIANT(id_mmfr1_el1
)
1849 FUNCTION_INVARIANT(id_mmfr2_el1
)
1850 FUNCTION_INVARIANT(id_mmfr3_el1
)
1851 FUNCTION_INVARIANT(id_isar0_el1
)
1852 FUNCTION_INVARIANT(id_isar1_el1
)
1853 FUNCTION_INVARIANT(id_isar2_el1
)
1854 FUNCTION_INVARIANT(id_isar3_el1
)
1855 FUNCTION_INVARIANT(id_isar4_el1
)
1856 FUNCTION_INVARIANT(id_isar5_el1
)
1857 FUNCTION_INVARIANT(clidr_el1
)
1858 FUNCTION_INVARIANT(aidr_el1
)
1860 /* ->val is filled in by kvm_sys_reg_table_init() */
1861 static struct sys_reg_desc invariant_sys_regs
[] = {
1862 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b000),
1863 NULL
, get_midr_el1
},
1864 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b110),
1865 NULL
, get_revidr_el1
},
1866 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b000),
1867 NULL
, get_id_pfr0_el1
},
1868 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b001),
1869 NULL
, get_id_pfr1_el1
},
1870 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b010),
1871 NULL
, get_id_dfr0_el1
},
1872 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b011),
1873 NULL
, get_id_afr0_el1
},
1874 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b100),
1875 NULL
, get_id_mmfr0_el1
},
1876 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b101),
1877 NULL
, get_id_mmfr1_el1
},
1878 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b110),
1879 NULL
, get_id_mmfr2_el1
},
1880 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b111),
1881 NULL
, get_id_mmfr3_el1
},
1882 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
1883 NULL
, get_id_isar0_el1
},
1884 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b001),
1885 NULL
, get_id_isar1_el1
},
1886 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
1887 NULL
, get_id_isar2_el1
},
1888 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b011),
1889 NULL
, get_id_isar3_el1
},
1890 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b100),
1891 NULL
, get_id_isar4_el1
},
1892 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b101),
1893 NULL
, get_id_isar5_el1
},
1894 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b001),
1895 NULL
, get_clidr_el1
},
1896 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b111),
1897 NULL
, get_aidr_el1
},
1898 { Op0(0b11), Op1(0b011), CRn(0b0000), CRm(0b0000), Op2(0b001),
1899 NULL
, get_ctr_el0
},
1902 static int reg_from_user(u64
*val
, const void __user
*uaddr
, u64 id
)
1904 if (copy_from_user(val
, uaddr
, KVM_REG_SIZE(id
)) != 0)
1909 static int reg_to_user(void __user
*uaddr
, const u64
*val
, u64 id
)
1911 if (copy_to_user(uaddr
, val
, KVM_REG_SIZE(id
)) != 0)
1916 static int get_invariant_sys_reg(u64 id
, void __user
*uaddr
)
1918 struct sys_reg_params params
;
1919 const struct sys_reg_desc
*r
;
1921 if (!index_to_params(id
, ¶ms
))
1924 r
= find_reg(¶ms
, invariant_sys_regs
, ARRAY_SIZE(invariant_sys_regs
));
1928 return reg_to_user(uaddr
, &r
->val
, id
);
1931 static int set_invariant_sys_reg(u64 id
, void __user
*uaddr
)
1933 struct sys_reg_params params
;
1934 const struct sys_reg_desc
*r
;
1936 u64 val
= 0; /* Make sure high bits are 0 for 32-bit regs */
1938 if (!index_to_params(id
, ¶ms
))
1940 r
= find_reg(¶ms
, invariant_sys_regs
, ARRAY_SIZE(invariant_sys_regs
));
1944 err
= reg_from_user(&val
, uaddr
, id
);
1948 /* This is what we mean by invariant: you can't change it. */
1955 static bool is_valid_cache(u32 val
)
1959 if (val
>= CSSELR_MAX
)
1962 /* Bottom bit is Instruction or Data bit. Next 3 bits are level. */
1964 ctype
= (cache_levels
>> (level
* 3)) & 7;
1967 case 0: /* No cache */
1969 case 1: /* Instruction cache only */
1971 case 2: /* Data cache only */
1972 case 4: /* Unified cache */
1974 case 3: /* Separate instruction and data caches */
1976 default: /* Reserved: we can't know instruction or data. */
1981 static int demux_c15_get(u64 id
, void __user
*uaddr
)
1984 u32 __user
*uval
= uaddr
;
1986 /* Fail if we have unknown bits set. */
1987 if (id
& ~(KVM_REG_ARCH_MASK
|KVM_REG_SIZE_MASK
|KVM_REG_ARM_COPROC_MASK
1988 | ((1 << KVM_REG_ARM_COPROC_SHIFT
)-1)))
1991 switch (id
& KVM_REG_ARM_DEMUX_ID_MASK
) {
1992 case KVM_REG_ARM_DEMUX_ID_CCSIDR
:
1993 if (KVM_REG_SIZE(id
) != 4)
1995 val
= (id
& KVM_REG_ARM_DEMUX_VAL_MASK
)
1996 >> KVM_REG_ARM_DEMUX_VAL_SHIFT
;
1997 if (!is_valid_cache(val
))
2000 return put_user(get_ccsidr(val
), uval
);
2006 static int demux_c15_set(u64 id
, void __user
*uaddr
)
2009 u32 __user
*uval
= uaddr
;
2011 /* Fail if we have unknown bits set. */
2012 if (id
& ~(KVM_REG_ARCH_MASK
|KVM_REG_SIZE_MASK
|KVM_REG_ARM_COPROC_MASK
2013 | ((1 << KVM_REG_ARM_COPROC_SHIFT
)-1)))
2016 switch (id
& KVM_REG_ARM_DEMUX_ID_MASK
) {
2017 case KVM_REG_ARM_DEMUX_ID_CCSIDR
:
2018 if (KVM_REG_SIZE(id
) != 4)
2020 val
= (id
& KVM_REG_ARM_DEMUX_VAL_MASK
)
2021 >> KVM_REG_ARM_DEMUX_VAL_SHIFT
;
2022 if (!is_valid_cache(val
))
2025 if (get_user(newval
, uval
))
2028 /* This is also invariant: you can't change it. */
2029 if (newval
!= get_ccsidr(val
))
2037 int kvm_arm_sys_reg_get_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
2039 const struct sys_reg_desc
*r
;
2040 void __user
*uaddr
= (void __user
*)(unsigned long)reg
->addr
;
2042 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_DEMUX
)
2043 return demux_c15_get(reg
->id
, uaddr
);
2045 if (KVM_REG_SIZE(reg
->id
) != sizeof(__u64
))
2048 r
= index_to_sys_reg_desc(vcpu
, reg
->id
);
2050 return get_invariant_sys_reg(reg
->id
, uaddr
);
2053 return (r
->get_user
)(vcpu
, r
, reg
, uaddr
);
2055 return reg_to_user(uaddr
, &vcpu_sys_reg(vcpu
, r
->reg
), reg
->id
);
2058 int kvm_arm_sys_reg_set_reg(struct kvm_vcpu
*vcpu
, const struct kvm_one_reg
*reg
)
2060 const struct sys_reg_desc
*r
;
2061 void __user
*uaddr
= (void __user
*)(unsigned long)reg
->addr
;
2063 if ((reg
->id
& KVM_REG_ARM_COPROC_MASK
) == KVM_REG_ARM_DEMUX
)
2064 return demux_c15_set(reg
->id
, uaddr
);
2066 if (KVM_REG_SIZE(reg
->id
) != sizeof(__u64
))
2069 r
= index_to_sys_reg_desc(vcpu
, reg
->id
);
2071 return set_invariant_sys_reg(reg
->id
, uaddr
);
2074 return (r
->set_user
)(vcpu
, r
, reg
, uaddr
);
2076 return reg_from_user(&vcpu_sys_reg(vcpu
, r
->reg
), uaddr
, reg
->id
);
2079 static unsigned int num_demux_regs(void)
2081 unsigned int i
, count
= 0;
2083 for (i
= 0; i
< CSSELR_MAX
; i
++)
2084 if (is_valid_cache(i
))
2090 static int write_demux_regids(u64 __user
*uindices
)
2092 u64 val
= KVM_REG_ARM64
| KVM_REG_SIZE_U32
| KVM_REG_ARM_DEMUX
;
2095 val
|= KVM_REG_ARM_DEMUX_ID_CCSIDR
;
2096 for (i
= 0; i
< CSSELR_MAX
; i
++) {
2097 if (!is_valid_cache(i
))
2099 if (put_user(val
| i
, uindices
))
2106 static u64
sys_reg_to_index(const struct sys_reg_desc
*reg
)
2108 return (KVM_REG_ARM64
| KVM_REG_SIZE_U64
|
2109 KVM_REG_ARM64_SYSREG
|
2110 (reg
->Op0
<< KVM_REG_ARM64_SYSREG_OP0_SHIFT
) |
2111 (reg
->Op1
<< KVM_REG_ARM64_SYSREG_OP1_SHIFT
) |
2112 (reg
->CRn
<< KVM_REG_ARM64_SYSREG_CRN_SHIFT
) |
2113 (reg
->CRm
<< KVM_REG_ARM64_SYSREG_CRM_SHIFT
) |
2114 (reg
->Op2
<< KVM_REG_ARM64_SYSREG_OP2_SHIFT
));
2117 static bool copy_reg_to_user(const struct sys_reg_desc
*reg
, u64 __user
**uind
)
2122 if (put_user(sys_reg_to_index(reg
), *uind
))
2129 /* Assumed ordered tables, see kvm_sys_reg_table_init. */
2130 static int walk_sys_regs(struct kvm_vcpu
*vcpu
, u64 __user
*uind
)
2132 const struct sys_reg_desc
*i1
, *i2
, *end1
, *end2
;
2133 unsigned int total
= 0;
2136 /* We check for duplicates here, to allow arch-specific overrides. */
2137 i1
= get_target_table(vcpu
->arch
.target
, true, &num
);
2140 end2
= sys_reg_descs
+ ARRAY_SIZE(sys_reg_descs
);
2142 BUG_ON(i1
== end1
|| i2
== end2
);
2144 /* Walk carefully, as both tables may refer to the same register. */
2146 int cmp
= cmp_sys_reg(i1
, i2
);
2147 /* target-specific overrides generic entry. */
2149 /* Ignore registers we trap but don't save. */
2151 if (!copy_reg_to_user(i1
, &uind
))
2156 /* Ignore registers we trap but don't save. */
2158 if (!copy_reg_to_user(i2
, &uind
))
2164 if (cmp
<= 0 && ++i1
== end1
)
2166 if (cmp
>= 0 && ++i2
== end2
)
2172 unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu
*vcpu
)
2174 return ARRAY_SIZE(invariant_sys_regs
)
2176 + walk_sys_regs(vcpu
, (u64 __user
*)NULL
);
2179 int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu
*vcpu
, u64 __user
*uindices
)
2184 /* Then give them all the invariant registers' indices. */
2185 for (i
= 0; i
< ARRAY_SIZE(invariant_sys_regs
); i
++) {
2186 if (put_user(sys_reg_to_index(&invariant_sys_regs
[i
]), uindices
))
2191 err
= walk_sys_regs(vcpu
, uindices
);
2196 return write_demux_regids(uindices
);
2199 static int check_sysreg_table(const struct sys_reg_desc
*table
, unsigned int n
)
2203 for (i
= 1; i
< n
; i
++) {
2204 if (cmp_sys_reg(&table
[i
-1], &table
[i
]) >= 0) {
2205 kvm_err("sys_reg table %p out of order (%d)\n", table
, i
- 1);
2213 void kvm_sys_reg_table_init(void)
2216 struct sys_reg_desc clidr
;
2218 /* Make sure tables are unique and in order. */
2219 BUG_ON(check_sysreg_table(sys_reg_descs
, ARRAY_SIZE(sys_reg_descs
)));
2220 BUG_ON(check_sysreg_table(cp14_regs
, ARRAY_SIZE(cp14_regs
)));
2221 BUG_ON(check_sysreg_table(cp14_64_regs
, ARRAY_SIZE(cp14_64_regs
)));
2222 BUG_ON(check_sysreg_table(cp15_regs
, ARRAY_SIZE(cp15_regs
)));
2223 BUG_ON(check_sysreg_table(cp15_64_regs
, ARRAY_SIZE(cp15_64_regs
)));
2224 BUG_ON(check_sysreg_table(invariant_sys_regs
, ARRAY_SIZE(invariant_sys_regs
)));
2226 /* We abuse the reset function to overwrite the table itself. */
2227 for (i
= 0; i
< ARRAY_SIZE(invariant_sys_regs
); i
++)
2228 invariant_sys_regs
[i
].reset(NULL
, &invariant_sys_regs
[i
]);
2231 * CLIDR format is awkward, so clean it up. See ARM B4.1.20:
2233 * If software reads the Cache Type fields from Ctype1
2234 * upwards, once it has seen a value of 0b000, no caches
2235 * exist at further-out levels of the hierarchy. So, for
2236 * example, if Ctype3 is the first Cache Type field with a
2237 * value of 0b000, the values of Ctype4 to Ctype7 must be
2240 get_clidr_el1(NULL
, &clidr
); /* Ugly... */
2241 cache_levels
= clidr
.val
;
2242 for (i
= 0; i
< 7; i
++)
2243 if (((cache_levels
>> (i
*3)) & 7) == 0)
2245 /* Clear all higher bits. */
2246 cache_levels
&= (1 << (i
*3))-1;
2250 * kvm_reset_sys_regs - sets system registers to reset value
2251 * @vcpu: The VCPU pointer
2253 * This function finds the right table above and sets the registers on the
2254 * virtual CPU struct to their architecturally defined reset values.
2256 void kvm_reset_sys_regs(struct kvm_vcpu
*vcpu
)
2259 const struct sys_reg_desc
*table
;
2261 /* Catch someone adding a register without putting in reset entry. */
2262 memset(&vcpu
->arch
.ctxt
.sys_regs
, 0x42, sizeof(vcpu
->arch
.ctxt
.sys_regs
));
2264 /* Generic chip reset first (so target could override). */
2265 reset_sys_reg_descs(vcpu
, sys_reg_descs
, ARRAY_SIZE(sys_reg_descs
));
2267 table
= get_target_table(vcpu
->arch
.target
, true, &num
);
2268 reset_sys_reg_descs(vcpu
, table
, num
);
2270 for (num
= 1; num
< NR_SYS_REGS
; num
++)
2271 if (vcpu_sys_reg(vcpu
, num
) == 0x4242424242424242)
2272 panic("Didn't reset vcpu_sys_reg(%zi)", num
);