blob: 158bae7c52cc46a0bbace8407087b6110ac67c14 [file] [log] [blame]
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001/*
2 * Copyright (C) 2012,2013 - ARM Ltd
3 * Author: Marc Zyngier <marc.zyngier@arm.com>
4 *
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>
9 *
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.
13 *
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.
18 *
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/>.
21 */
22
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000023#include <linux/kvm_host.h>
Mark Rutlandc6d01a92014-11-24 13:59:30 +000024#include <linux/mm.h>
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000025#include <linux/uaccess.h>
Mark Rutlandc6d01a92014-11-24 13:59:30 +000026
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000027#include <asm/cacheflush.h>
28#include <asm/cputype.h>
Marc Zyngier0c557ed2014-04-24 10:24:46 +010029#include <asm/debug-monitors.h>
Mark Rutlandc6d01a92014-11-24 13:59:30 +000030#include <asm/esr.h>
31#include <asm/kvm_arm.h>
32#include <asm/kvm_coproc.h>
33#include <asm/kvm_emulate.h>
34#include <asm/kvm_host.h>
35#include <asm/kvm_mmu.h>
36
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000037#include <trace/events/kvm.h>
38
39#include "sys_regs.h"
40
41/*
42 * All of this file is extremly similar to the ARM coproc.c, but the
43 * types are different. My gut feeling is that it should be pretty
44 * easy to merge, but that would be an ABI breakage -- again. VFP
45 * would also need to be abstracted.
Marc Zyngier62a89c42013-02-07 10:32:33 +000046 *
47 * For AArch32, we only take care of what is being trapped. Anything
48 * that has to do with init and userspace access has to go via the
49 * 64bit interface.
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000050 */
51
52/* 3 bits per cache level, as per CLIDR, but non-existent caches always 0 */
53static u32 cache_levels;
54
55/* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
56#define CSSELR_MAX 12
57
58/* Which cache CCSIDR represents depends on CSSELR value. */
59static u32 get_ccsidr(u32 csselr)
60{
61 u32 ccsidr;
62
63 /* Make sure noone else changes CSSELR during this! */
64 local_irq_disable();
65 /* Put value into CSSELR */
66 asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
67 isb();
68 /* Read result out of CCSIDR */
69 asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
70 local_irq_enable();
71
72 return ccsidr;
73}
74
Marc Zyngier3c1e7162014-12-19 16:05:31 +000075/*
76 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
77 */
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000078static bool access_dcsw(struct kvm_vcpu *vcpu,
79 const struct sys_reg_params *p,
80 const struct sys_reg_desc *r)
81{
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000082 if (!p->is_write)
83 return read_from_write_only(vcpu, p);
84
Marc Zyngier3c1e7162014-12-19 16:05:31 +000085 kvm_set_way_flush(vcpu);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000086 return true;
87}
88
89/*
Marc Zyngier4d449232014-01-14 18:00:55 +000090 * Generic accessor for VM registers. Only called as long as HCR_TVM
Marc Zyngier3c1e7162014-12-19 16:05:31 +000091 * is set. If the guest enables the MMU, we stop trapping the VM
92 * sys_regs and leave it in complete control of the caches.
Marc Zyngier4d449232014-01-14 18:00:55 +000093 */
94static bool access_vm_reg(struct kvm_vcpu *vcpu,
95 const struct sys_reg_params *p,
96 const struct sys_reg_desc *r)
97{
98 unsigned long val;
Marc Zyngier3c1e7162014-12-19 16:05:31 +000099 bool was_enabled = vcpu_has_cache_enabled(vcpu);
Marc Zyngier4d449232014-01-14 18:00:55 +0000100
101 BUG_ON(!p->is_write);
102
103 val = *vcpu_reg(vcpu, p->Rt);
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100104 if (!p->is_aarch32) {
Marc Zyngier4d449232014-01-14 18:00:55 +0000105 vcpu_sys_reg(vcpu, r->reg) = val;
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100106 } else {
107 if (!p->is_32bit)
108 vcpu_cp15_64_high(vcpu, r->reg) = val >> 32;
Victor Kamenskyf0a3eaf2014-07-02 17:19:30 +0100109 vcpu_cp15_64_low(vcpu, r->reg) = val & 0xffffffffUL;
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100110 }
Victor Kamenskyf0a3eaf2014-07-02 17:19:30 +0100111
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000112 kvm_toggle_cache(vcpu, was_enabled);
Marc Zyngier4d449232014-01-14 18:00:55 +0000113 return true;
114}
115
Andre Przywara6d52f352014-06-03 10:13:13 +0200116/*
117 * Trap handler for the GICv3 SGI generation system register.
118 * Forward the request to the VGIC emulation.
119 * The cp15_64 code makes sure this automatically works
120 * for both AArch64 and AArch32 accesses.
121 */
122static bool access_gic_sgi(struct kvm_vcpu *vcpu,
123 const struct sys_reg_params *p,
124 const struct sys_reg_desc *r)
125{
126 u64 val;
127
128 if (!p->is_write)
129 return read_from_write_only(vcpu, p);
130
131 val = *vcpu_reg(vcpu, p->Rt);
132 vgic_v3_dispatch_sgi(vcpu, val);
133
134 return true;
135}
136
Marc Zyngier7609c122014-04-24 10:21:16 +0100137static bool trap_raz_wi(struct kvm_vcpu *vcpu,
138 const struct sys_reg_params *p,
139 const struct sys_reg_desc *r)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000140{
141 if (p->is_write)
142 return ignore_write(vcpu, p);
143 else
144 return read_zero(vcpu, p);
145}
146
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100147static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
148 const struct sys_reg_params *p,
149 const struct sys_reg_desc *r)
150{
151 if (p->is_write) {
152 return ignore_write(vcpu, p);
153 } else {
154 *vcpu_reg(vcpu, p->Rt) = (1 << 3);
155 return true;
156 }
157}
158
159static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu,
160 const struct sys_reg_params *p,
161 const struct sys_reg_desc *r)
162{
163 if (p->is_write) {
164 return ignore_write(vcpu, p);
165 } else {
166 u32 val;
167 asm volatile("mrs %0, dbgauthstatus_el1" : "=r" (val));
168 *vcpu_reg(vcpu, p->Rt) = val;
169 return true;
170 }
171}
172
173/*
174 * We want to avoid world-switching all the DBG registers all the
175 * time:
176 *
177 * - If we've touched any debug register, it is likely that we're
178 * going to touch more of them. It then makes sense to disable the
179 * traps and start doing the save/restore dance
180 * - If debug is active (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), it is
181 * then mandatory to save/restore the registers, as the guest
182 * depends on them.
183 *
184 * For this, we use a DIRTY bit, indicating the guest has modified the
185 * debug registers, used as follow:
186 *
187 * On guest entry:
188 * - If the dirty bit is set (because we're coming back from trapping),
189 * disable the traps, save host registers, restore guest registers.
190 * - If debug is actively in use (DBG_MDSCR_KDE or DBG_MDSCR_MDE set),
191 * set the dirty bit, disable the traps, save host registers,
192 * restore guest registers.
193 * - Otherwise, enable the traps
194 *
195 * On guest exit:
196 * - If the dirty bit is set, save guest registers, restore host
197 * registers and clear the dirty bit. This ensure that the host can
198 * now use the debug registers.
199 */
200static bool trap_debug_regs(struct kvm_vcpu *vcpu,
201 const struct sys_reg_params *p,
202 const struct sys_reg_desc *r)
203{
204 if (p->is_write) {
205 vcpu_sys_reg(vcpu, r->reg) = *vcpu_reg(vcpu, p->Rt);
206 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
207 } else {
208 *vcpu_reg(vcpu, p->Rt) = vcpu_sys_reg(vcpu, r->reg);
209 }
210
211 return true;
212}
213
Alex Bennée84e690b2015-07-07 17:30:00 +0100214/*
215 * reg_to_dbg/dbg_to_reg
216 *
217 * A 32 bit write to a debug register leave top bits alone
218 * A 32 bit read from a debug register only returns the bottom bits
219 *
220 * All writes will set the KVM_ARM64_DEBUG_DIRTY flag to ensure the
221 * hyp.S code switches between host and guest values in future.
222 */
223static inline void reg_to_dbg(struct kvm_vcpu *vcpu,
224 const struct sys_reg_params *p,
225 u64 *dbg_reg)
226{
227 u64 val = *vcpu_reg(vcpu, p->Rt);
228
229 if (p->is_32bit) {
230 val &= 0xffffffffUL;
231 val |= ((*dbg_reg >> 32) << 32);
232 }
233
234 *dbg_reg = val;
235 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
236}
237
238static inline void dbg_to_reg(struct kvm_vcpu *vcpu,
239 const struct sys_reg_params *p,
240 u64 *dbg_reg)
241{
242 u64 val = *dbg_reg;
243
244 if (p->is_32bit)
245 val &= 0xffffffffUL;
246
247 *vcpu_reg(vcpu, p->Rt) = val;
248}
249
250static inline bool trap_bvr(struct kvm_vcpu *vcpu,
251 const struct sys_reg_params *p,
252 const struct sys_reg_desc *rd)
253{
254 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg];
255
256 if (p->is_write)
257 reg_to_dbg(vcpu, p, dbg_reg);
258 else
259 dbg_to_reg(vcpu, p, dbg_reg);
260
261 return true;
262}
263
264static int set_bvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
265 const struct kvm_one_reg *reg, void __user *uaddr)
266{
267 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg];
268
269 if (copy_from_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
270 return -EFAULT;
271 return 0;
272}
273
274static int get_bvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
275 const struct kvm_one_reg *reg, void __user *uaddr)
276{
277 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg];
278
279 if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
280 return -EFAULT;
281 return 0;
282}
283
284static inline void reset_bvr(struct kvm_vcpu *vcpu,
285 const struct sys_reg_desc *rd)
286{
287 vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg] = rd->val;
288}
289
290static inline bool trap_bcr(struct kvm_vcpu *vcpu,
291 const struct sys_reg_params *p,
292 const struct sys_reg_desc *rd)
293{
294 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg];
295
296 if (p->is_write)
297 reg_to_dbg(vcpu, p, dbg_reg);
298 else
299 dbg_to_reg(vcpu, p, dbg_reg);
300
301 return true;
302}
303
304static int set_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
305 const struct kvm_one_reg *reg, void __user *uaddr)
306{
307 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg];
308
309 if (copy_from_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
310 return -EFAULT;
311
312 return 0;
313}
314
315static int get_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
316 const struct kvm_one_reg *reg, void __user *uaddr)
317{
318 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg];
319
320 if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
321 return -EFAULT;
322 return 0;
323}
324
325static inline void reset_bcr(struct kvm_vcpu *vcpu,
326 const struct sys_reg_desc *rd)
327{
328 vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg] = rd->val;
329}
330
331static inline bool trap_wvr(struct kvm_vcpu *vcpu,
332 const struct sys_reg_params *p,
333 const struct sys_reg_desc *rd)
334{
335 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg];
336
337 if (p->is_write)
338 reg_to_dbg(vcpu, p, dbg_reg);
339 else
340 dbg_to_reg(vcpu, p, dbg_reg);
341
342 return true;
343}
344
345static int set_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
346 const struct kvm_one_reg *reg, void __user *uaddr)
347{
348 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg];
349
350 if (copy_from_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
351 return -EFAULT;
352 return 0;
353}
354
355static int get_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
356 const struct kvm_one_reg *reg, void __user *uaddr)
357{
358 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg];
359
360 if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
361 return -EFAULT;
362 return 0;
363}
364
365static inline void reset_wvr(struct kvm_vcpu *vcpu,
366 const struct sys_reg_desc *rd)
367{
368 vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg] = rd->val;
369}
370
371static inline bool trap_wcr(struct kvm_vcpu *vcpu,
372 const struct sys_reg_params *p,
373 const struct sys_reg_desc *rd)
374{
375 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg];
376
377 if (p->is_write)
378 reg_to_dbg(vcpu, p, dbg_reg);
379 else
380 dbg_to_reg(vcpu, p, dbg_reg);
381
382 return true;
383}
384
385static int set_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
386 const struct kvm_one_reg *reg, void __user *uaddr)
387{
388 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg];
389
390 if (copy_from_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
391 return -EFAULT;
392 return 0;
393}
394
395static int get_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
396 const struct kvm_one_reg *reg, void __user *uaddr)
397{
398 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg];
399
400 if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
401 return -EFAULT;
402 return 0;
403}
404
405static inline void reset_wcr(struct kvm_vcpu *vcpu,
406 const struct sys_reg_desc *rd)
407{
408 vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg] = rd->val;
409}
410
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000411static void reset_amair_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
412{
413 u64 amair;
414
415 asm volatile("mrs %0, amair_el1\n" : "=r" (amair));
416 vcpu_sys_reg(vcpu, AMAIR_EL1) = amair;
417}
418
419static void reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
420{
Andre Przywara4429fc62014-06-02 15:37:13 +0200421 u64 mpidr;
422
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000423 /*
Andre Przywara4429fc62014-06-02 15:37:13 +0200424 * Map the vcpu_id into the first three affinity level fields of
425 * the MPIDR. We limit the number of VCPUs in level 0 due to a
426 * limitation to 16 CPUs in that level in the ICC_SGIxR registers
427 * of the GICv3 to be able to address each CPU directly when
428 * sending IPIs.
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000429 */
Andre Przywara4429fc62014-06-02 15:37:13 +0200430 mpidr = (vcpu->vcpu_id & 0x0f) << MPIDR_LEVEL_SHIFT(0);
431 mpidr |= ((vcpu->vcpu_id >> 4) & 0xff) << MPIDR_LEVEL_SHIFT(1);
432 mpidr |= ((vcpu->vcpu_id >> 12) & 0xff) << MPIDR_LEVEL_SHIFT(2);
433 vcpu_sys_reg(vcpu, MPIDR_EL1) = (1ULL << 31) | mpidr;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000434}
435
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100436/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
437#define DBG_BCR_BVR_WCR_WVR_EL1(n) \
438 /* DBGBVRn_EL1 */ \
439 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b100), \
Alex Bennée84e690b2015-07-07 17:30:00 +0100440 trap_bvr, reset_bvr, n, 0, get_bvr, set_bvr }, \
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100441 /* DBGBCRn_EL1 */ \
442 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b101), \
Alex Bennée84e690b2015-07-07 17:30:00 +0100443 trap_bcr, reset_bcr, n, 0, get_bcr, set_bcr }, \
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100444 /* DBGWVRn_EL1 */ \
445 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b110), \
Alex Bennée84e690b2015-07-07 17:30:00 +0100446 trap_wvr, reset_wvr, n, 0, get_wvr, set_wvr }, \
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100447 /* DBGWCRn_EL1 */ \
448 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b111), \
Alex Bennée84e690b2015-07-07 17:30:00 +0100449 trap_wcr, reset_wcr, n, 0, get_wcr, set_wcr }
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100450
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000451/*
452 * Architected system registers.
453 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
Marc Zyngier7609c122014-04-24 10:21:16 +0100454 *
455 * We could trap ID_DFR0 and tell the guest we don't support performance
456 * monitoring. Unfortunately the patch to make the kernel check ID_DFR0 was
457 * NAKed, so it will read the PMCR anyway.
458 *
459 * Therefore we tell the guest we have 0 counters. Unfortunately, we
460 * must always support PMCCNTR (the cycle counter): we just RAZ/WI for
461 * all PM registers, which doesn't crash the guest kernel at least.
462 *
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100463 * Debug handling: We do trap most, if not all debug related system
464 * registers. The implementation is good enough to ensure that a guest
465 * can use these with minimal performance degradation. The drawback is
466 * that we don't implement any of the external debug, none of the
467 * OSlock protocol. This should be revisited if we ever encounter a
468 * more demanding guest...
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000469 */
470static const struct sys_reg_desc sys_reg_descs[] = {
471 /* DC ISW */
472 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b0110), Op2(0b010),
473 access_dcsw },
474 /* DC CSW */
475 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1010), Op2(0b010),
476 access_dcsw },
477 /* DC CISW */
478 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b010),
479 access_dcsw },
480
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100481 DBG_BCR_BVR_WCR_WVR_EL1(0),
482 DBG_BCR_BVR_WCR_WVR_EL1(1),
483 /* MDCCINT_EL1 */
484 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
485 trap_debug_regs, reset_val, MDCCINT_EL1, 0 },
486 /* MDSCR_EL1 */
487 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
488 trap_debug_regs, reset_val, MDSCR_EL1, 0 },
489 DBG_BCR_BVR_WCR_WVR_EL1(2),
490 DBG_BCR_BVR_WCR_WVR_EL1(3),
491 DBG_BCR_BVR_WCR_WVR_EL1(4),
492 DBG_BCR_BVR_WCR_WVR_EL1(5),
493 DBG_BCR_BVR_WCR_WVR_EL1(6),
494 DBG_BCR_BVR_WCR_WVR_EL1(7),
495 DBG_BCR_BVR_WCR_WVR_EL1(8),
496 DBG_BCR_BVR_WCR_WVR_EL1(9),
497 DBG_BCR_BVR_WCR_WVR_EL1(10),
498 DBG_BCR_BVR_WCR_WVR_EL1(11),
499 DBG_BCR_BVR_WCR_WVR_EL1(12),
500 DBG_BCR_BVR_WCR_WVR_EL1(13),
501 DBG_BCR_BVR_WCR_WVR_EL1(14),
502 DBG_BCR_BVR_WCR_WVR_EL1(15),
503
504 /* MDRAR_EL1 */
505 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
506 trap_raz_wi },
507 /* OSLAR_EL1 */
508 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b100),
509 trap_raz_wi },
510 /* OSLSR_EL1 */
511 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0001), Op2(0b100),
512 trap_oslsr_el1 },
513 /* OSDLR_EL1 */
514 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0011), Op2(0b100),
515 trap_raz_wi },
516 /* DBGPRCR_EL1 */
517 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0100), Op2(0b100),
518 trap_raz_wi },
519 /* DBGCLAIMSET_EL1 */
520 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1000), Op2(0b110),
521 trap_raz_wi },
522 /* DBGCLAIMCLR_EL1 */
523 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1001), Op2(0b110),
524 trap_raz_wi },
525 /* DBGAUTHSTATUS_EL1 */
526 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b110),
527 trap_dbgauthstatus_el1 },
528
Marc Zyngier62a89c42013-02-07 10:32:33 +0000529 /* TEECR32_EL1 */
530 { Op0(0b10), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
531 NULL, reset_val, TEECR32_EL1, 0 },
532 /* TEEHBR32_EL1 */
533 { Op0(0b10), Op1(0b010), CRn(0b0001), CRm(0b0000), Op2(0b000),
534 NULL, reset_val, TEEHBR32_EL1, 0 },
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100535
536 /* MDCCSR_EL1 */
537 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0001), Op2(0b000),
538 trap_raz_wi },
539 /* DBGDTR_EL0 */
540 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0100), Op2(0b000),
541 trap_raz_wi },
542 /* DBGDTR[TR]X_EL0 */
543 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0101), Op2(0b000),
544 trap_raz_wi },
545
Marc Zyngier62a89c42013-02-07 10:32:33 +0000546 /* DBGVCR32_EL2 */
547 { Op0(0b10), Op1(0b100), CRn(0b0000), CRm(0b0111), Op2(0b000),
548 NULL, reset_val, DBGVCR32_EL2, 0 },
549
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000550 /* MPIDR_EL1 */
551 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b101),
552 NULL, reset_mpidr, MPIDR_EL1 },
553 /* SCTLR_EL1 */
554 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000555 access_vm_reg, reset_val, SCTLR_EL1, 0x00C50078 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000556 /* CPACR_EL1 */
557 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b010),
558 NULL, reset_val, CPACR_EL1, 0 },
559 /* TTBR0_EL1 */
560 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000561 access_vm_reg, reset_unknown, TTBR0_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000562 /* TTBR1_EL1 */
563 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000564 access_vm_reg, reset_unknown, TTBR1_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000565 /* TCR_EL1 */
566 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b010),
Marc Zyngier4d449232014-01-14 18:00:55 +0000567 access_vm_reg, reset_val, TCR_EL1, 0 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000568
569 /* AFSR0_EL1 */
570 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000571 access_vm_reg, reset_unknown, AFSR0_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000572 /* AFSR1_EL1 */
573 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000574 access_vm_reg, reset_unknown, AFSR1_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000575 /* ESR_EL1 */
576 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0010), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000577 access_vm_reg, reset_unknown, ESR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000578 /* FAR_EL1 */
579 { Op0(0b11), Op1(0b000), CRn(0b0110), CRm(0b0000), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000580 access_vm_reg, reset_unknown, FAR_EL1 },
Marc Zyngier1bbd8052013-06-07 11:02:34 +0100581 /* PAR_EL1 */
582 { Op0(0b11), Op1(0b000), CRn(0b0111), CRm(0b0100), Op2(0b000),
583 NULL, reset_unknown, PAR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000584
585 /* PMINTENSET_EL1 */
586 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100587 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000588 /* PMINTENCLR_EL1 */
589 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100590 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000591
592 /* MAIR_EL1 */
593 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0010), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000594 access_vm_reg, reset_unknown, MAIR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000595 /* AMAIR_EL1 */
596 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0011), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000597 access_vm_reg, reset_amair_el1, AMAIR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000598
599 /* VBAR_EL1 */
600 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b0000), Op2(0b000),
601 NULL, reset_val, VBAR_EL1, 0 },
Christoffer Dalldb7dedd2014-11-19 11:23:54 +0000602
Andre Przywara6d52f352014-06-03 10:13:13 +0200603 /* ICC_SGI1R_EL1 */
604 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b1011), Op2(0b101),
605 access_gic_sgi },
Christoffer Dalldb7dedd2014-11-19 11:23:54 +0000606 /* ICC_SRE_EL1 */
607 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b1100), Op2(0b101),
608 trap_raz_wi },
609
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000610 /* CONTEXTIDR_EL1 */
611 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000612 access_vm_reg, reset_val, CONTEXTIDR_EL1, 0 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000613 /* TPIDR_EL1 */
614 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b100),
615 NULL, reset_unknown, TPIDR_EL1 },
616
617 /* CNTKCTL_EL1 */
618 { Op0(0b11), Op1(0b000), CRn(0b1110), CRm(0b0001), Op2(0b000),
619 NULL, reset_val, CNTKCTL_EL1, 0},
620
621 /* CSSELR_EL1 */
622 { Op0(0b11), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
623 NULL, reset_unknown, CSSELR_EL1 },
624
625 /* PMCR_EL0 */
626 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b000),
Marc Zyngier7609c122014-04-24 10:21:16 +0100627 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000628 /* PMCNTENSET_EL0 */
629 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100630 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000631 /* PMCNTENCLR_EL0 */
632 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100633 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000634 /* PMOVSCLR_EL0 */
635 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b011),
Marc Zyngier7609c122014-04-24 10:21:16 +0100636 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000637 /* PMSWINC_EL0 */
638 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b100),
Marc Zyngier7609c122014-04-24 10:21:16 +0100639 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000640 /* PMSELR_EL0 */
641 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b101),
Marc Zyngier7609c122014-04-24 10:21:16 +0100642 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000643 /* PMCEID0_EL0 */
644 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b110),
Marc Zyngier7609c122014-04-24 10:21:16 +0100645 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000646 /* PMCEID1_EL0 */
647 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b111),
Marc Zyngier7609c122014-04-24 10:21:16 +0100648 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000649 /* PMCCNTR_EL0 */
650 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b000),
Marc Zyngier7609c122014-04-24 10:21:16 +0100651 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000652 /* PMXEVTYPER_EL0 */
653 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100654 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000655 /* PMXEVCNTR_EL0 */
656 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100657 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000658 /* PMUSERENR_EL0 */
659 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b000),
Marc Zyngier7609c122014-04-24 10:21:16 +0100660 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000661 /* PMOVSSET_EL0 */
662 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b011),
Marc Zyngier7609c122014-04-24 10:21:16 +0100663 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000664
665 /* TPIDR_EL0 */
666 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b010),
667 NULL, reset_unknown, TPIDR_EL0 },
668 /* TPIDRRO_EL0 */
669 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b011),
670 NULL, reset_unknown, TPIDRRO_EL0 },
Marc Zyngier62a89c42013-02-07 10:32:33 +0000671
672 /* DACR32_EL2 */
673 { Op0(0b11), Op1(0b100), CRn(0b0011), CRm(0b0000), Op2(0b000),
674 NULL, reset_unknown, DACR32_EL2 },
675 /* IFSR32_EL2 */
676 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0000), Op2(0b001),
677 NULL, reset_unknown, IFSR32_EL2 },
678 /* FPEXC32_EL2 */
679 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0011), Op2(0b000),
680 NULL, reset_val, FPEXC32_EL2, 0x70 },
681};
682
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100683static bool trap_dbgidr(struct kvm_vcpu *vcpu,
684 const struct sys_reg_params *p,
685 const struct sys_reg_desc *r)
686{
687 if (p->is_write) {
688 return ignore_write(vcpu, p);
689 } else {
690 u64 dfr = read_cpuid(ID_AA64DFR0_EL1);
691 u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
692 u32 el3 = !!((pfr >> 12) & 0xf);
693
694 *vcpu_reg(vcpu, p->Rt) = ((((dfr >> 20) & 0xf) << 28) |
695 (((dfr >> 12) & 0xf) << 24) |
696 (((dfr >> 28) & 0xf) << 20) |
697 (6 << 16) | (el3 << 14) | (el3 << 12));
698 return true;
699 }
700}
701
702static bool trap_debug32(struct kvm_vcpu *vcpu,
703 const struct sys_reg_params *p,
704 const struct sys_reg_desc *r)
705{
706 if (p->is_write) {
707 vcpu_cp14(vcpu, r->reg) = *vcpu_reg(vcpu, p->Rt);
708 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
709 } else {
710 *vcpu_reg(vcpu, p->Rt) = vcpu_cp14(vcpu, r->reg);
711 }
712
713 return true;
714}
715
Alex Bennée84e690b2015-07-07 17:30:00 +0100716/* AArch32 debug register mappings
717 *
718 * AArch32 DBGBVRn is mapped to DBGBVRn_EL1[31:0]
719 * AArch32 DBGBXVRn is mapped to DBGBVRn_EL1[63:32]
720 *
721 * All control registers and watchpoint value registers are mapped to
722 * the lower 32 bits of their AArch64 equivalents. We share the trap
723 * handlers with the above AArch64 code which checks what mode the
724 * system is in.
725 */
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100726
Alex Bennée84e690b2015-07-07 17:30:00 +0100727static inline bool trap_xvr(struct kvm_vcpu *vcpu,
728 const struct sys_reg_params *p,
729 const struct sys_reg_desc *rd)
730{
731 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg];
732
733 if (p->is_write) {
734 u64 val = *dbg_reg;
735
736 val &= 0xffffffffUL;
737 val |= *vcpu_reg(vcpu, p->Rt) << 32;
738 *dbg_reg = val;
739
740 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
741 } else {
742 *vcpu_reg(vcpu, p->Rt) = *dbg_reg >> 32;
743 }
744
745 return true;
746}
747
748#define DBG_BCR_BVR_WCR_WVR(n) \
749 /* DBGBVRn */ \
750 { Op1( 0), CRn( 0), CRm((n)), Op2( 4), trap_bvr, NULL, n }, \
751 /* DBGBCRn */ \
752 { Op1( 0), CRn( 0), CRm((n)), Op2( 5), trap_bcr, NULL, n }, \
753 /* DBGWVRn */ \
754 { Op1( 0), CRn( 0), CRm((n)), Op2( 6), trap_wvr, NULL, n }, \
755 /* DBGWCRn */ \
756 { Op1( 0), CRn( 0), CRm((n)), Op2( 7), trap_wcr, NULL, n }
757
758#define DBGBXVR(n) \
759 { Op1( 0), CRn( 1), CRm((n)), Op2( 1), trap_xvr, NULL, n }
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100760
761/*
762 * Trapped cp14 registers. We generally ignore most of the external
763 * debug, on the principle that they don't really make sense to a
Alex Bennée84e690b2015-07-07 17:30:00 +0100764 * guest. Revisit this one day, would this principle change.
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100765 */
Marc Zyngier72564012014-04-24 10:27:13 +0100766static const struct sys_reg_desc cp14_regs[] = {
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100767 /* DBGIDR */
768 { Op1( 0), CRn( 0), CRm( 0), Op2( 0), trap_dbgidr },
769 /* DBGDTRRXext */
770 { Op1( 0), CRn( 0), CRm( 0), Op2( 2), trap_raz_wi },
771
772 DBG_BCR_BVR_WCR_WVR(0),
773 /* DBGDSCRint */
774 { Op1( 0), CRn( 0), CRm( 1), Op2( 0), trap_raz_wi },
775 DBG_BCR_BVR_WCR_WVR(1),
776 /* DBGDCCINT */
777 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), trap_debug32 },
778 /* DBGDSCRext */
779 { Op1( 0), CRn( 0), CRm( 2), Op2( 2), trap_debug32 },
780 DBG_BCR_BVR_WCR_WVR(2),
781 /* DBGDTR[RT]Xint */
782 { Op1( 0), CRn( 0), CRm( 3), Op2( 0), trap_raz_wi },
783 /* DBGDTR[RT]Xext */
784 { Op1( 0), CRn( 0), CRm( 3), Op2( 2), trap_raz_wi },
785 DBG_BCR_BVR_WCR_WVR(3),
786 DBG_BCR_BVR_WCR_WVR(4),
787 DBG_BCR_BVR_WCR_WVR(5),
788 /* DBGWFAR */
789 { Op1( 0), CRn( 0), CRm( 6), Op2( 0), trap_raz_wi },
790 /* DBGOSECCR */
791 { Op1( 0), CRn( 0), CRm( 6), Op2( 2), trap_raz_wi },
792 DBG_BCR_BVR_WCR_WVR(6),
793 /* DBGVCR */
794 { Op1( 0), CRn( 0), CRm( 7), Op2( 0), trap_debug32 },
795 DBG_BCR_BVR_WCR_WVR(7),
796 DBG_BCR_BVR_WCR_WVR(8),
797 DBG_BCR_BVR_WCR_WVR(9),
798 DBG_BCR_BVR_WCR_WVR(10),
799 DBG_BCR_BVR_WCR_WVR(11),
800 DBG_BCR_BVR_WCR_WVR(12),
801 DBG_BCR_BVR_WCR_WVR(13),
802 DBG_BCR_BVR_WCR_WVR(14),
803 DBG_BCR_BVR_WCR_WVR(15),
804
805 /* DBGDRAR (32bit) */
806 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), trap_raz_wi },
807
808 DBGBXVR(0),
809 /* DBGOSLAR */
810 { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi },
811 DBGBXVR(1),
812 /* DBGOSLSR */
813 { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1 },
814 DBGBXVR(2),
815 DBGBXVR(3),
816 /* DBGOSDLR */
817 { Op1( 0), CRn( 1), CRm( 3), Op2( 4), trap_raz_wi },
818 DBGBXVR(4),
819 /* DBGPRCR */
820 { Op1( 0), CRn( 1), CRm( 4), Op2( 4), trap_raz_wi },
821 DBGBXVR(5),
822 DBGBXVR(6),
823 DBGBXVR(7),
824 DBGBXVR(8),
825 DBGBXVR(9),
826 DBGBXVR(10),
827 DBGBXVR(11),
828 DBGBXVR(12),
829 DBGBXVR(13),
830 DBGBXVR(14),
831 DBGBXVR(15),
832
833 /* DBGDSAR (32bit) */
834 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), trap_raz_wi },
835
836 /* DBGDEVID2 */
837 { Op1( 0), CRn( 7), CRm( 0), Op2( 7), trap_raz_wi },
838 /* DBGDEVID1 */
839 { Op1( 0), CRn( 7), CRm( 1), Op2( 7), trap_raz_wi },
840 /* DBGDEVID */
841 { Op1( 0), CRn( 7), CRm( 2), Op2( 7), trap_raz_wi },
842 /* DBGCLAIMSET */
843 { Op1( 0), CRn( 7), CRm( 8), Op2( 6), trap_raz_wi },
844 /* DBGCLAIMCLR */
845 { Op1( 0), CRn( 7), CRm( 9), Op2( 6), trap_raz_wi },
846 /* DBGAUTHSTATUS */
847 { Op1( 0), CRn( 7), CRm(14), Op2( 6), trap_dbgauthstatus_el1 },
Marc Zyngier72564012014-04-24 10:27:13 +0100848};
849
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100850/* Trapped cp14 64bit registers */
851static const struct sys_reg_desc cp14_64_regs[] = {
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100852 /* DBGDRAR (64bit) */
853 { Op1( 0), CRm( 1), .access = trap_raz_wi },
854
855 /* DBGDSAR (64bit) */
856 { Op1( 0), CRm( 2), .access = trap_raz_wi },
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100857};
858
Marc Zyngier4d449232014-01-14 18:00:55 +0000859/*
860 * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding,
861 * depending on the way they are accessed (as a 32bit or a 64bit
862 * register).
863 */
Marc Zyngier62a89c42013-02-07 10:32:33 +0000864static const struct sys_reg_desc cp15_regs[] = {
Andre Przywara6d52f352014-06-03 10:13:13 +0200865 { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi },
866
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000867 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), access_vm_reg, NULL, c1_SCTLR },
Marc Zyngier4d449232014-01-14 18:00:55 +0000868 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
869 { Op1( 0), CRn( 2), CRm( 0), Op2( 1), access_vm_reg, NULL, c2_TTBR1 },
870 { Op1( 0), CRn( 2), CRm( 0), Op2( 2), access_vm_reg, NULL, c2_TTBCR },
871 { Op1( 0), CRn( 3), CRm( 0), Op2( 0), access_vm_reg, NULL, c3_DACR },
872 { Op1( 0), CRn( 5), CRm( 0), Op2( 0), access_vm_reg, NULL, c5_DFSR },
873 { Op1( 0), CRn( 5), CRm( 0), Op2( 1), access_vm_reg, NULL, c5_IFSR },
874 { Op1( 0), CRn( 5), CRm( 1), Op2( 0), access_vm_reg, NULL, c5_ADFSR },
875 { Op1( 0), CRn( 5), CRm( 1), Op2( 1), access_vm_reg, NULL, c5_AIFSR },
876 { Op1( 0), CRn( 6), CRm( 0), Op2( 0), access_vm_reg, NULL, c6_DFAR },
877 { Op1( 0), CRn( 6), CRm( 0), Op2( 2), access_vm_reg, NULL, c6_IFAR },
878
Marc Zyngier62a89c42013-02-07 10:32:33 +0000879 /*
880 * DC{C,I,CI}SW operations:
881 */
882 { Op1( 0), CRn( 7), CRm( 6), Op2( 2), access_dcsw },
883 { Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw },
884 { Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw },
Marc Zyngier4d449232014-01-14 18:00:55 +0000885
Marc Zyngier7609c122014-04-24 10:21:16 +0100886 /* PMU */
887 { Op1( 0), CRn( 9), CRm(12), Op2( 0), trap_raz_wi },
888 { Op1( 0), CRn( 9), CRm(12), Op2( 1), trap_raz_wi },
889 { Op1( 0), CRn( 9), CRm(12), Op2( 2), trap_raz_wi },
890 { Op1( 0), CRn( 9), CRm(12), Op2( 3), trap_raz_wi },
891 { Op1( 0), CRn( 9), CRm(12), Op2( 5), trap_raz_wi },
892 { Op1( 0), CRn( 9), CRm(12), Op2( 6), trap_raz_wi },
893 { Op1( 0), CRn( 9), CRm(12), Op2( 7), trap_raz_wi },
894 { Op1( 0), CRn( 9), CRm(13), Op2( 0), trap_raz_wi },
895 { Op1( 0), CRn( 9), CRm(13), Op2( 1), trap_raz_wi },
896 { Op1( 0), CRn( 9), CRm(13), Op2( 2), trap_raz_wi },
897 { Op1( 0), CRn( 9), CRm(14), Op2( 0), trap_raz_wi },
898 { Op1( 0), CRn( 9), CRm(14), Op2( 1), trap_raz_wi },
899 { Op1( 0), CRn( 9), CRm(14), Op2( 2), trap_raz_wi },
Marc Zyngier4d449232014-01-14 18:00:55 +0000900
901 { Op1( 0), CRn(10), CRm( 2), Op2( 0), access_vm_reg, NULL, c10_PRRR },
902 { Op1( 0), CRn(10), CRm( 2), Op2( 1), access_vm_reg, NULL, c10_NMRR },
903 { Op1( 0), CRn(10), CRm( 3), Op2( 0), access_vm_reg, NULL, c10_AMAIR0 },
904 { Op1( 0), CRn(10), CRm( 3), Op2( 1), access_vm_reg, NULL, c10_AMAIR1 },
Christoffer Dalldb7dedd2014-11-19 11:23:54 +0000905
906 /* ICC_SRE */
907 { Op1( 0), CRn(12), CRm(12), Op2( 5), trap_raz_wi },
908
Marc Zyngier4d449232014-01-14 18:00:55 +0000909 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID },
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100910};
911
912static const struct sys_reg_desc cp15_64_regs[] = {
913 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
Andre Przywara6d52f352014-06-03 10:13:13 +0200914 { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi },
Marc Zyngier4d449232014-01-14 18:00:55 +0000915 { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000916};
917
918/* Target specific emulation tables */
919static struct kvm_sys_reg_target_table *target_tables[KVM_ARM_NUM_TARGETS];
920
921void kvm_register_target_sys_reg_table(unsigned int target,
922 struct kvm_sys_reg_target_table *table)
923{
924 target_tables[target] = table;
925}
926
927/* Get specific register table for this target. */
Marc Zyngier62a89c42013-02-07 10:32:33 +0000928static const struct sys_reg_desc *get_target_table(unsigned target,
929 bool mode_is_64,
930 size_t *num)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000931{
932 struct kvm_sys_reg_target_table *table;
933
934 table = target_tables[target];
Marc Zyngier62a89c42013-02-07 10:32:33 +0000935 if (mode_is_64) {
936 *num = table->table64.num;
937 return table->table64.table;
938 } else {
939 *num = table->table32.num;
940 return table->table32.table;
941 }
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000942}
943
944static const struct sys_reg_desc *find_reg(const struct sys_reg_params *params,
945 const struct sys_reg_desc table[],
946 unsigned int num)
947{
948 unsigned int i;
949
950 for (i = 0; i < num; i++) {
951 const struct sys_reg_desc *r = &table[i];
952
953 if (params->Op0 != r->Op0)
954 continue;
955 if (params->Op1 != r->Op1)
956 continue;
957 if (params->CRn != r->CRn)
958 continue;
959 if (params->CRm != r->CRm)
960 continue;
961 if (params->Op2 != r->Op2)
962 continue;
963
964 return r;
965 }
966 return NULL;
967}
968
Marc Zyngier62a89c42013-02-07 10:32:33 +0000969int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu, struct kvm_run *run)
970{
971 kvm_inject_undefined(vcpu);
972 return 1;
973}
974
Marc Zyngier72564012014-04-24 10:27:13 +0100975/*
976 * emulate_cp -- tries to match a sys_reg access in a handling table, and
977 * call the corresponding trap handler.
978 *
979 * @params: pointer to the descriptor of the access
980 * @table: array of trap descriptors
981 * @num: size of the trap descriptor array
982 *
983 * Return 0 if the access has been handled, and -1 if not.
984 */
985static int emulate_cp(struct kvm_vcpu *vcpu,
986 const struct sys_reg_params *params,
987 const struct sys_reg_desc *table,
988 size_t num)
Marc Zyngier62a89c42013-02-07 10:32:33 +0000989{
Marc Zyngier72564012014-04-24 10:27:13 +0100990 const struct sys_reg_desc *r;
Marc Zyngier62a89c42013-02-07 10:32:33 +0000991
Marc Zyngier72564012014-04-24 10:27:13 +0100992 if (!table)
993 return -1; /* Not handled */
Marc Zyngier62a89c42013-02-07 10:32:33 +0000994
Marc Zyngier62a89c42013-02-07 10:32:33 +0000995 r = find_reg(params, table, num);
Marc Zyngier62a89c42013-02-07 10:32:33 +0000996
Marc Zyngier72564012014-04-24 10:27:13 +0100997 if (r) {
Marc Zyngier62a89c42013-02-07 10:32:33 +0000998 /*
999 * Not having an accessor means that we have
1000 * configured a trap that we don't know how to
1001 * handle. This certainly qualifies as a gross bug
1002 * that should be fixed right away.
1003 */
1004 BUG_ON(!r->access);
1005
1006 if (likely(r->access(vcpu, params, r))) {
1007 /* Skip instruction, since it was emulated */
1008 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
Marc Zyngier62a89c42013-02-07 10:32:33 +00001009 }
Marc Zyngier72564012014-04-24 10:27:13 +01001010
1011 /* Handled */
1012 return 0;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001013 }
1014
Marc Zyngier72564012014-04-24 10:27:13 +01001015 /* Not handled */
1016 return -1;
1017}
1018
1019static void unhandled_cp_access(struct kvm_vcpu *vcpu,
1020 struct sys_reg_params *params)
1021{
1022 u8 hsr_ec = kvm_vcpu_trap_get_class(vcpu);
1023 int cp;
1024
1025 switch(hsr_ec) {
Mark Rutlandc6d01a92014-11-24 13:59:30 +00001026 case ESR_ELx_EC_CP15_32:
1027 case ESR_ELx_EC_CP15_64:
Marc Zyngier72564012014-04-24 10:27:13 +01001028 cp = 15;
1029 break;
Mark Rutlandc6d01a92014-11-24 13:59:30 +00001030 case ESR_ELx_EC_CP14_MR:
1031 case ESR_ELx_EC_CP14_64:
Marc Zyngier72564012014-04-24 10:27:13 +01001032 cp = 14;
1033 break;
1034 default:
1035 WARN_ON((cp = -1));
1036 }
1037
1038 kvm_err("Unsupported guest CP%d access at: %08lx\n",
1039 cp, *vcpu_pc(vcpu));
Marc Zyngier62a89c42013-02-07 10:32:33 +00001040 print_sys_reg_instr(params);
1041 kvm_inject_undefined(vcpu);
1042}
1043
1044/**
Marc Zyngier72564012014-04-24 10:27:13 +01001045 * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP15 access
Marc Zyngier62a89c42013-02-07 10:32:33 +00001046 * @vcpu: The VCPU pointer
1047 * @run: The kvm_run struct
1048 */
Marc Zyngier72564012014-04-24 10:27:13 +01001049static int kvm_handle_cp_64(struct kvm_vcpu *vcpu,
1050 const struct sys_reg_desc *global,
1051 size_t nr_global,
1052 const struct sys_reg_desc *target_specific,
1053 size_t nr_specific)
Marc Zyngier62a89c42013-02-07 10:32:33 +00001054{
1055 struct sys_reg_params params;
1056 u32 hsr = kvm_vcpu_get_hsr(vcpu);
1057 int Rt2 = (hsr >> 10) & 0xf;
1058
Marc Zyngier2072d292014-01-21 10:55:17 +00001059 params.is_aarch32 = true;
1060 params.is_32bit = false;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001061 params.CRm = (hsr >> 1) & 0xf;
1062 params.Rt = (hsr >> 5) & 0xf;
1063 params.is_write = ((hsr & 1) == 0);
1064
1065 params.Op0 = 0;
1066 params.Op1 = (hsr >> 16) & 0xf;
1067 params.Op2 = 0;
1068 params.CRn = 0;
1069
1070 /*
1071 * Massive hack here. Store Rt2 in the top 32bits so we only
1072 * have one register to deal with. As we use the same trap
1073 * backends between AArch32 and AArch64, we get away with it.
1074 */
1075 if (params.is_write) {
1076 u64 val = *vcpu_reg(vcpu, params.Rt);
1077 val &= 0xffffffff;
1078 val |= *vcpu_reg(vcpu, Rt2) << 32;
1079 *vcpu_reg(vcpu, params.Rt) = val;
1080 }
1081
Marc Zyngier72564012014-04-24 10:27:13 +01001082 if (!emulate_cp(vcpu, &params, target_specific, nr_specific))
1083 goto out;
1084 if (!emulate_cp(vcpu, &params, global, nr_global))
1085 goto out;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001086
Marc Zyngier72564012014-04-24 10:27:13 +01001087 unhandled_cp_access(vcpu, &params);
1088
1089out:
Marc Zyngier62a89c42013-02-07 10:32:33 +00001090 /* Do the opposite hack for the read side */
1091 if (!params.is_write) {
1092 u64 val = *vcpu_reg(vcpu, params.Rt);
1093 val >>= 32;
1094 *vcpu_reg(vcpu, Rt2) = val;
1095 }
1096
1097 return 1;
1098}
1099
1100/**
1101 * kvm_handle_cp15_32 -- handles a mrc/mcr trap on a guest CP15 access
1102 * @vcpu: The VCPU pointer
1103 * @run: The kvm_run struct
1104 */
Marc Zyngier72564012014-04-24 10:27:13 +01001105static int kvm_handle_cp_32(struct kvm_vcpu *vcpu,
1106 const struct sys_reg_desc *global,
1107 size_t nr_global,
1108 const struct sys_reg_desc *target_specific,
1109 size_t nr_specific)
Marc Zyngier62a89c42013-02-07 10:32:33 +00001110{
1111 struct sys_reg_params params;
1112 u32 hsr = kvm_vcpu_get_hsr(vcpu);
1113
Marc Zyngier2072d292014-01-21 10:55:17 +00001114 params.is_aarch32 = true;
1115 params.is_32bit = true;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001116 params.CRm = (hsr >> 1) & 0xf;
1117 params.Rt = (hsr >> 5) & 0xf;
1118 params.is_write = ((hsr & 1) == 0);
1119 params.CRn = (hsr >> 10) & 0xf;
1120 params.Op0 = 0;
1121 params.Op1 = (hsr >> 14) & 0x7;
1122 params.Op2 = (hsr >> 17) & 0x7;
1123
Marc Zyngier72564012014-04-24 10:27:13 +01001124 if (!emulate_cp(vcpu, &params, target_specific, nr_specific))
1125 return 1;
1126 if (!emulate_cp(vcpu, &params, global, nr_global))
1127 return 1;
1128
1129 unhandled_cp_access(vcpu, &params);
Marc Zyngier62a89c42013-02-07 10:32:33 +00001130 return 1;
1131}
1132
Marc Zyngier72564012014-04-24 10:27:13 +01001133int kvm_handle_cp15_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
1134{
1135 const struct sys_reg_desc *target_specific;
1136 size_t num;
1137
1138 target_specific = get_target_table(vcpu->arch.target, false, &num);
1139 return kvm_handle_cp_64(vcpu,
Marc Zyngiera9866ba02014-04-24 14:11:48 +01001140 cp15_64_regs, ARRAY_SIZE(cp15_64_regs),
Marc Zyngier72564012014-04-24 10:27:13 +01001141 target_specific, num);
1142}
1143
1144int kvm_handle_cp15_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
1145{
1146 const struct sys_reg_desc *target_specific;
1147 size_t num;
1148
1149 target_specific = get_target_table(vcpu->arch.target, false, &num);
1150 return kvm_handle_cp_32(vcpu,
1151 cp15_regs, ARRAY_SIZE(cp15_regs),
1152 target_specific, num);
1153}
1154
1155int kvm_handle_cp14_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
1156{
1157 return kvm_handle_cp_64(vcpu,
Marc Zyngiera9866ba02014-04-24 14:11:48 +01001158 cp14_64_regs, ARRAY_SIZE(cp14_64_regs),
Marc Zyngier72564012014-04-24 10:27:13 +01001159 NULL, 0);
1160}
1161
1162int kvm_handle_cp14_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
1163{
1164 return kvm_handle_cp_32(vcpu,
1165 cp14_regs, ARRAY_SIZE(cp14_regs),
1166 NULL, 0);
1167}
1168
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001169static int emulate_sys_reg(struct kvm_vcpu *vcpu,
1170 const struct sys_reg_params *params)
1171{
1172 size_t num;
1173 const struct sys_reg_desc *table, *r;
1174
Marc Zyngier62a89c42013-02-07 10:32:33 +00001175 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001176
1177 /* Search target-specific then generic table. */
1178 r = find_reg(params, table, num);
1179 if (!r)
1180 r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1181
1182 if (likely(r)) {
1183 /*
1184 * Not having an accessor means that we have
1185 * configured a trap that we don't know how to
1186 * handle. This certainly qualifies as a gross bug
1187 * that should be fixed right away.
1188 */
1189 BUG_ON(!r->access);
1190
1191 if (likely(r->access(vcpu, params, r))) {
1192 /* Skip instruction, since it was emulated */
1193 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
1194 return 1;
1195 }
1196 /* If access function fails, it should complain. */
1197 } else {
1198 kvm_err("Unsupported guest sys_reg access at: %lx\n",
1199 *vcpu_pc(vcpu));
1200 print_sys_reg_instr(params);
1201 }
1202 kvm_inject_undefined(vcpu);
1203 return 1;
1204}
1205
1206static void reset_sys_reg_descs(struct kvm_vcpu *vcpu,
1207 const struct sys_reg_desc *table, size_t num)
1208{
1209 unsigned long i;
1210
1211 for (i = 0; i < num; i++)
1212 if (table[i].reset)
1213 table[i].reset(vcpu, &table[i]);
1214}
1215
1216/**
1217 * kvm_handle_sys_reg -- handles a mrs/msr trap on a guest sys_reg access
1218 * @vcpu: The VCPU pointer
1219 * @run: The kvm_run struct
1220 */
1221int kvm_handle_sys_reg(struct kvm_vcpu *vcpu, struct kvm_run *run)
1222{
1223 struct sys_reg_params params;
1224 unsigned long esr = kvm_vcpu_get_hsr(vcpu);
1225
Marc Zyngier2072d292014-01-21 10:55:17 +00001226 params.is_aarch32 = false;
1227 params.is_32bit = false;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001228 params.Op0 = (esr >> 20) & 3;
1229 params.Op1 = (esr >> 14) & 0x7;
1230 params.CRn = (esr >> 10) & 0xf;
1231 params.CRm = (esr >> 1) & 0xf;
1232 params.Op2 = (esr >> 17) & 0x7;
1233 params.Rt = (esr >> 5) & 0x1f;
1234 params.is_write = !(esr & 1);
1235
1236 return emulate_sys_reg(vcpu, &params);
1237}
1238
1239/******************************************************************************
1240 * Userspace API
1241 *****************************************************************************/
1242
1243static bool index_to_params(u64 id, struct sys_reg_params *params)
1244{
1245 switch (id & KVM_REG_SIZE_MASK) {
1246 case KVM_REG_SIZE_U64:
1247 /* Any unused index bits means it's not valid. */
1248 if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
1249 | KVM_REG_ARM_COPROC_MASK
1250 | KVM_REG_ARM64_SYSREG_OP0_MASK
1251 | KVM_REG_ARM64_SYSREG_OP1_MASK
1252 | KVM_REG_ARM64_SYSREG_CRN_MASK
1253 | KVM_REG_ARM64_SYSREG_CRM_MASK
1254 | KVM_REG_ARM64_SYSREG_OP2_MASK))
1255 return false;
1256 params->Op0 = ((id & KVM_REG_ARM64_SYSREG_OP0_MASK)
1257 >> KVM_REG_ARM64_SYSREG_OP0_SHIFT);
1258 params->Op1 = ((id & KVM_REG_ARM64_SYSREG_OP1_MASK)
1259 >> KVM_REG_ARM64_SYSREG_OP1_SHIFT);
1260 params->CRn = ((id & KVM_REG_ARM64_SYSREG_CRN_MASK)
1261 >> KVM_REG_ARM64_SYSREG_CRN_SHIFT);
1262 params->CRm = ((id & KVM_REG_ARM64_SYSREG_CRM_MASK)
1263 >> KVM_REG_ARM64_SYSREG_CRM_SHIFT);
1264 params->Op2 = ((id & KVM_REG_ARM64_SYSREG_OP2_MASK)
1265 >> KVM_REG_ARM64_SYSREG_OP2_SHIFT);
1266 return true;
1267 default:
1268 return false;
1269 }
1270}
1271
1272/* Decode an index value, and find the sys_reg_desc entry. */
1273static const struct sys_reg_desc *index_to_sys_reg_desc(struct kvm_vcpu *vcpu,
1274 u64 id)
1275{
1276 size_t num;
1277 const struct sys_reg_desc *table, *r;
1278 struct sys_reg_params params;
1279
1280 /* We only do sys_reg for now. */
1281 if ((id & KVM_REG_ARM_COPROC_MASK) != KVM_REG_ARM64_SYSREG)
1282 return NULL;
1283
1284 if (!index_to_params(id, &params))
1285 return NULL;
1286
Marc Zyngier62a89c42013-02-07 10:32:33 +00001287 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001288 r = find_reg(&params, table, num);
1289 if (!r)
1290 r = find_reg(&params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1291
1292 /* Not saved in the sys_reg array? */
1293 if (r && !r->reg)
1294 r = NULL;
1295
1296 return r;
1297}
1298
1299/*
1300 * These are the invariant sys_reg registers: we let the guest see the
1301 * host versions of these, so they're part of the guest state.
1302 *
1303 * A future CPU may provide a mechanism to present different values to
1304 * the guest, or a future kvm may trap them.
1305 */
1306
1307#define FUNCTION_INVARIANT(reg) \
1308 static void get_##reg(struct kvm_vcpu *v, \
1309 const struct sys_reg_desc *r) \
1310 { \
1311 u64 val; \
1312 \
1313 asm volatile("mrs %0, " __stringify(reg) "\n" \
1314 : "=r" (val)); \
1315 ((struct sys_reg_desc *)r)->val = val; \
1316 }
1317
1318FUNCTION_INVARIANT(midr_el1)
1319FUNCTION_INVARIANT(ctr_el0)
1320FUNCTION_INVARIANT(revidr_el1)
1321FUNCTION_INVARIANT(id_pfr0_el1)
1322FUNCTION_INVARIANT(id_pfr1_el1)
1323FUNCTION_INVARIANT(id_dfr0_el1)
1324FUNCTION_INVARIANT(id_afr0_el1)
1325FUNCTION_INVARIANT(id_mmfr0_el1)
1326FUNCTION_INVARIANT(id_mmfr1_el1)
1327FUNCTION_INVARIANT(id_mmfr2_el1)
1328FUNCTION_INVARIANT(id_mmfr3_el1)
1329FUNCTION_INVARIANT(id_isar0_el1)
1330FUNCTION_INVARIANT(id_isar1_el1)
1331FUNCTION_INVARIANT(id_isar2_el1)
1332FUNCTION_INVARIANT(id_isar3_el1)
1333FUNCTION_INVARIANT(id_isar4_el1)
1334FUNCTION_INVARIANT(id_isar5_el1)
1335FUNCTION_INVARIANT(clidr_el1)
1336FUNCTION_INVARIANT(aidr_el1)
1337
1338/* ->val is filled in by kvm_sys_reg_table_init() */
1339static struct sys_reg_desc invariant_sys_regs[] = {
1340 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b000),
1341 NULL, get_midr_el1 },
1342 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b110),
1343 NULL, get_revidr_el1 },
1344 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b000),
1345 NULL, get_id_pfr0_el1 },
1346 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b001),
1347 NULL, get_id_pfr1_el1 },
1348 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b010),
1349 NULL, get_id_dfr0_el1 },
1350 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b011),
1351 NULL, get_id_afr0_el1 },
1352 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b100),
1353 NULL, get_id_mmfr0_el1 },
1354 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b101),
1355 NULL, get_id_mmfr1_el1 },
1356 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b110),
1357 NULL, get_id_mmfr2_el1 },
1358 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b111),
1359 NULL, get_id_mmfr3_el1 },
1360 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
1361 NULL, get_id_isar0_el1 },
1362 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b001),
1363 NULL, get_id_isar1_el1 },
1364 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
1365 NULL, get_id_isar2_el1 },
1366 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b011),
1367 NULL, get_id_isar3_el1 },
1368 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b100),
1369 NULL, get_id_isar4_el1 },
1370 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b101),
1371 NULL, get_id_isar5_el1 },
1372 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b001),
1373 NULL, get_clidr_el1 },
1374 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b111),
1375 NULL, get_aidr_el1 },
1376 { Op0(0b11), Op1(0b011), CRn(0b0000), CRm(0b0000), Op2(0b001),
1377 NULL, get_ctr_el0 },
1378};
1379
Victor Kamensky26c99af2014-06-12 09:30:12 -07001380static int reg_from_user(u64 *val, const void __user *uaddr, u64 id)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001381{
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001382 if (copy_from_user(val, uaddr, KVM_REG_SIZE(id)) != 0)
1383 return -EFAULT;
1384 return 0;
1385}
1386
Victor Kamensky26c99af2014-06-12 09:30:12 -07001387static int reg_to_user(void __user *uaddr, const u64 *val, u64 id)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001388{
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001389 if (copy_to_user(uaddr, val, KVM_REG_SIZE(id)) != 0)
1390 return -EFAULT;
1391 return 0;
1392}
1393
1394static int get_invariant_sys_reg(u64 id, void __user *uaddr)
1395{
1396 struct sys_reg_params params;
1397 const struct sys_reg_desc *r;
1398
1399 if (!index_to_params(id, &params))
1400 return -ENOENT;
1401
1402 r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
1403 if (!r)
1404 return -ENOENT;
1405
1406 return reg_to_user(uaddr, &r->val, id);
1407}
1408
1409static int set_invariant_sys_reg(u64 id, void __user *uaddr)
1410{
1411 struct sys_reg_params params;
1412 const struct sys_reg_desc *r;
1413 int err;
1414 u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
1415
1416 if (!index_to_params(id, &params))
1417 return -ENOENT;
1418 r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
1419 if (!r)
1420 return -ENOENT;
1421
1422 err = reg_from_user(&val, uaddr, id);
1423 if (err)
1424 return err;
1425
1426 /* This is what we mean by invariant: you can't change it. */
1427 if (r->val != val)
1428 return -EINVAL;
1429
1430 return 0;
1431}
1432
1433static bool is_valid_cache(u32 val)
1434{
1435 u32 level, ctype;
1436
1437 if (val >= CSSELR_MAX)
Will Deacon18d45762014-08-26 15:13:22 +01001438 return false;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001439
1440 /* Bottom bit is Instruction or Data bit. Next 3 bits are level. */
1441 level = (val >> 1);
1442 ctype = (cache_levels >> (level * 3)) & 7;
1443
1444 switch (ctype) {
1445 case 0: /* No cache */
1446 return false;
1447 case 1: /* Instruction cache only */
1448 return (val & 1);
1449 case 2: /* Data cache only */
1450 case 4: /* Unified cache */
1451 return !(val & 1);
1452 case 3: /* Separate instruction and data caches */
1453 return true;
1454 default: /* Reserved: we can't know instruction or data. */
1455 return false;
1456 }
1457}
1458
1459static int demux_c15_get(u64 id, void __user *uaddr)
1460{
1461 u32 val;
1462 u32 __user *uval = uaddr;
1463
1464 /* Fail if we have unknown bits set. */
1465 if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
1466 | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
1467 return -ENOENT;
1468
1469 switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
1470 case KVM_REG_ARM_DEMUX_ID_CCSIDR:
1471 if (KVM_REG_SIZE(id) != 4)
1472 return -ENOENT;
1473 val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
1474 >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
1475 if (!is_valid_cache(val))
1476 return -ENOENT;
1477
1478 return put_user(get_ccsidr(val), uval);
1479 default:
1480 return -ENOENT;
1481 }
1482}
1483
1484static int demux_c15_set(u64 id, void __user *uaddr)
1485{
1486 u32 val, newval;
1487 u32 __user *uval = uaddr;
1488
1489 /* Fail if we have unknown bits set. */
1490 if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
1491 | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
1492 return -ENOENT;
1493
1494 switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
1495 case KVM_REG_ARM_DEMUX_ID_CCSIDR:
1496 if (KVM_REG_SIZE(id) != 4)
1497 return -ENOENT;
1498 val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
1499 >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
1500 if (!is_valid_cache(val))
1501 return -ENOENT;
1502
1503 if (get_user(newval, uval))
1504 return -EFAULT;
1505
1506 /* This is also invariant: you can't change it. */
1507 if (newval != get_ccsidr(val))
1508 return -EINVAL;
1509 return 0;
1510 default:
1511 return -ENOENT;
1512 }
1513}
1514
1515int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
1516{
1517 const struct sys_reg_desc *r;
1518 void __user *uaddr = (void __user *)(unsigned long)reg->addr;
1519
1520 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
1521 return demux_c15_get(reg->id, uaddr);
1522
1523 if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
1524 return -ENOENT;
1525
1526 r = index_to_sys_reg_desc(vcpu, reg->id);
1527 if (!r)
1528 return get_invariant_sys_reg(reg->id, uaddr);
1529
Alex Bennée84e690b2015-07-07 17:30:00 +01001530 if (r->get_user)
1531 return (r->get_user)(vcpu, r, reg, uaddr);
1532
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001533 return reg_to_user(uaddr, &vcpu_sys_reg(vcpu, r->reg), reg->id);
1534}
1535
1536int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
1537{
1538 const struct sys_reg_desc *r;
1539 void __user *uaddr = (void __user *)(unsigned long)reg->addr;
1540
1541 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
1542 return demux_c15_set(reg->id, uaddr);
1543
1544 if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
1545 return -ENOENT;
1546
1547 r = index_to_sys_reg_desc(vcpu, reg->id);
1548 if (!r)
1549 return set_invariant_sys_reg(reg->id, uaddr);
1550
Alex Bennée84e690b2015-07-07 17:30:00 +01001551 if (r->set_user)
1552 return (r->set_user)(vcpu, r, reg, uaddr);
1553
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001554 return reg_from_user(&vcpu_sys_reg(vcpu, r->reg), uaddr, reg->id);
1555}
1556
1557static unsigned int num_demux_regs(void)
1558{
1559 unsigned int i, count = 0;
1560
1561 for (i = 0; i < CSSELR_MAX; i++)
1562 if (is_valid_cache(i))
1563 count++;
1564
1565 return count;
1566}
1567
1568static int write_demux_regids(u64 __user *uindices)
1569{
Alex Bennéeefd48ce2014-07-01 16:53:13 +01001570 u64 val = KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001571 unsigned int i;
1572
1573 val |= KVM_REG_ARM_DEMUX_ID_CCSIDR;
1574 for (i = 0; i < CSSELR_MAX; i++) {
1575 if (!is_valid_cache(i))
1576 continue;
1577 if (put_user(val | i, uindices))
1578 return -EFAULT;
1579 uindices++;
1580 }
1581 return 0;
1582}
1583
1584static u64 sys_reg_to_index(const struct sys_reg_desc *reg)
1585{
1586 return (KVM_REG_ARM64 | KVM_REG_SIZE_U64 |
1587 KVM_REG_ARM64_SYSREG |
1588 (reg->Op0 << KVM_REG_ARM64_SYSREG_OP0_SHIFT) |
1589 (reg->Op1 << KVM_REG_ARM64_SYSREG_OP1_SHIFT) |
1590 (reg->CRn << KVM_REG_ARM64_SYSREG_CRN_SHIFT) |
1591 (reg->CRm << KVM_REG_ARM64_SYSREG_CRM_SHIFT) |
1592 (reg->Op2 << KVM_REG_ARM64_SYSREG_OP2_SHIFT));
1593}
1594
1595static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
1596{
1597 if (!*uind)
1598 return true;
1599
1600 if (put_user(sys_reg_to_index(reg), *uind))
1601 return false;
1602
1603 (*uind)++;
1604 return true;
1605}
1606
1607/* Assumed ordered tables, see kvm_sys_reg_table_init. */
1608static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
1609{
1610 const struct sys_reg_desc *i1, *i2, *end1, *end2;
1611 unsigned int total = 0;
1612 size_t num;
1613
1614 /* We check for duplicates here, to allow arch-specific overrides. */
Marc Zyngier62a89c42013-02-07 10:32:33 +00001615 i1 = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001616 end1 = i1 + num;
1617 i2 = sys_reg_descs;
1618 end2 = sys_reg_descs + ARRAY_SIZE(sys_reg_descs);
1619
1620 BUG_ON(i1 == end1 || i2 == end2);
1621
1622 /* Walk carefully, as both tables may refer to the same register. */
1623 while (i1 || i2) {
1624 int cmp = cmp_sys_reg(i1, i2);
1625 /* target-specific overrides generic entry. */
1626 if (cmp <= 0) {
1627 /* Ignore registers we trap but don't save. */
1628 if (i1->reg) {
1629 if (!copy_reg_to_user(i1, &uind))
1630 return -EFAULT;
1631 total++;
1632 }
1633 } else {
1634 /* Ignore registers we trap but don't save. */
1635 if (i2->reg) {
1636 if (!copy_reg_to_user(i2, &uind))
1637 return -EFAULT;
1638 total++;
1639 }
1640 }
1641
1642 if (cmp <= 0 && ++i1 == end1)
1643 i1 = NULL;
1644 if (cmp >= 0 && ++i2 == end2)
1645 i2 = NULL;
1646 }
1647 return total;
1648}
1649
1650unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
1651{
1652 return ARRAY_SIZE(invariant_sys_regs)
1653 + num_demux_regs()
1654 + walk_sys_regs(vcpu, (u64 __user *)NULL);
1655}
1656
1657int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
1658{
1659 unsigned int i;
1660 int err;
1661
1662 /* Then give them all the invariant registers' indices. */
1663 for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) {
1664 if (put_user(sys_reg_to_index(&invariant_sys_regs[i]), uindices))
1665 return -EFAULT;
1666 uindices++;
1667 }
1668
1669 err = walk_sys_regs(vcpu, uindices);
1670 if (err < 0)
1671 return err;
1672 uindices += err;
1673
1674 return write_demux_regids(uindices);
1675}
1676
Marc Zyngiere6a95512014-05-07 13:43:39 +01001677static int check_sysreg_table(const struct sys_reg_desc *table, unsigned int n)
1678{
1679 unsigned int i;
1680
1681 for (i = 1; i < n; i++) {
1682 if (cmp_sys_reg(&table[i-1], &table[i]) >= 0) {
1683 kvm_err("sys_reg table %p out of order (%d)\n", table, i - 1);
1684 return 1;
1685 }
1686 }
1687
1688 return 0;
1689}
1690
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001691void kvm_sys_reg_table_init(void)
1692{
1693 unsigned int i;
1694 struct sys_reg_desc clidr;
1695
1696 /* Make sure tables are unique and in order. */
Marc Zyngiere6a95512014-05-07 13:43:39 +01001697 BUG_ON(check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs)));
1698 BUG_ON(check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs)));
1699 BUG_ON(check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs)));
1700 BUG_ON(check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs)));
1701 BUG_ON(check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs)));
1702 BUG_ON(check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs)));
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001703
1704 /* We abuse the reset function to overwrite the table itself. */
1705 for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++)
1706 invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]);
1707
1708 /*
1709 * CLIDR format is awkward, so clean it up. See ARM B4.1.20:
1710 *
1711 * If software reads the Cache Type fields from Ctype1
1712 * upwards, once it has seen a value of 0b000, no caches
1713 * exist at further-out levels of the hierarchy. So, for
1714 * example, if Ctype3 is the first Cache Type field with a
1715 * value of 0b000, the values of Ctype4 to Ctype7 must be
1716 * ignored.
1717 */
1718 get_clidr_el1(NULL, &clidr); /* Ugly... */
1719 cache_levels = clidr.val;
1720 for (i = 0; i < 7; i++)
1721 if (((cache_levels >> (i*3)) & 7) == 0)
1722 break;
1723 /* Clear all higher bits. */
1724 cache_levels &= (1 << (i*3))-1;
1725}
1726
1727/**
1728 * kvm_reset_sys_regs - sets system registers to reset value
1729 * @vcpu: The VCPU pointer
1730 *
1731 * This function finds the right table above and sets the registers on the
1732 * virtual CPU struct to their architecturally defined reset values.
1733 */
1734void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
1735{
1736 size_t num;
1737 const struct sys_reg_desc *table;
1738
1739 /* Catch someone adding a register without putting in reset entry. */
1740 memset(&vcpu->arch.ctxt.sys_regs, 0x42, sizeof(vcpu->arch.ctxt.sys_regs));
1741
1742 /* Generic chip reset first (so target could override). */
1743 reset_sys_reg_descs(vcpu, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1744
Marc Zyngier62a89c42013-02-07 10:32:33 +00001745 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001746 reset_sys_reg_descs(vcpu, table, num);
1747
1748 for (num = 1; num < NR_SYS_REGS; num++)
1749 if (vcpu_sys_reg(vcpu, num) == 0x4242424242424242)
1750 panic("Didn't reset vcpu_sys_reg(%zi)", num);
1751}