blob: e88ae2d809a5558f51872a3977b052248b0be0d4 [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>
Marc Zyngier9d8415d2015-10-25 19:57:11 +000032#include <asm/kvm_asm.h>
Mark Rutlandc6d01a92014-11-24 13:59:30 +000033#include <asm/kvm_coproc.h>
34#include <asm/kvm_emulate.h>
35#include <asm/kvm_host.h>
36#include <asm/kvm_mmu.h>
Shannon Zhaoab946832015-06-18 16:01:53 +080037#include <asm/perf_event.h>
Mark Rutlandc6d01a92014-11-24 13:59:30 +000038
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000039#include <trace/events/kvm.h>
40
41#include "sys_regs.h"
42
Alex Bennéeeef8c852015-07-07 17:30:03 +010043#include "trace.h"
44
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000045/*
46 * All of this file is extremly similar to the ARM coproc.c, but the
47 * types are different. My gut feeling is that it should be pretty
48 * easy to merge, but that would be an ABI breakage -- again. VFP
49 * would also need to be abstracted.
Marc Zyngier62a89c42013-02-07 10:32:33 +000050 *
51 * For AArch32, we only take care of what is being trapped. Anything
52 * that has to do with init and userspace access has to go via the
53 * 64bit interface.
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000054 */
55
56/* 3 bits per cache level, as per CLIDR, but non-existent caches always 0 */
57static u32 cache_levels;
58
59/* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
60#define CSSELR_MAX 12
61
62/* Which cache CCSIDR represents depends on CSSELR value. */
63static u32 get_ccsidr(u32 csselr)
64{
65 u32 ccsidr;
66
67 /* Make sure noone else changes CSSELR during this! */
68 local_irq_disable();
69 /* Put value into CSSELR */
70 asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
71 isb();
72 /* Read result out of CCSIDR */
73 asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
74 local_irq_enable();
75
76 return ccsidr;
77}
78
Marc Zyngier3c1e7162014-12-19 16:05:31 +000079/*
80 * See note at ARMv7 ARM B1.14.4 (TL;DR: S/W ops are not easily virtualized).
81 */
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000082static bool access_dcsw(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +030083 struct sys_reg_params *p,
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000084 const struct sys_reg_desc *r)
85{
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000086 if (!p->is_write)
87 return read_from_write_only(vcpu, p);
88
Marc Zyngier3c1e7162014-12-19 16:05:31 +000089 kvm_set_way_flush(vcpu);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000090 return true;
91}
92
93/*
Marc Zyngier4d449232014-01-14 18:00:55 +000094 * Generic accessor for VM registers. Only called as long as HCR_TVM
Marc Zyngier3c1e7162014-12-19 16:05:31 +000095 * 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.
Marc Zyngier4d449232014-01-14 18:00:55 +000097 */
98static bool access_vm_reg(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +030099 struct sys_reg_params *p,
Marc Zyngier4d449232014-01-14 18:00:55 +0000100 const struct sys_reg_desc *r)
101{
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000102 bool was_enabled = vcpu_has_cache_enabled(vcpu);
Marc Zyngier4d449232014-01-14 18:00:55 +0000103
104 BUG_ON(!p->is_write);
105
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100106 if (!p->is_aarch32) {
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300107 vcpu_sys_reg(vcpu, r->reg) = p->regval;
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100108 } else {
109 if (!p->is_32bit)
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300110 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);
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100112 }
Victor Kamenskyf0a3eaf2014-07-02 17:19:30 +0100113
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000114 kvm_toggle_cache(vcpu, was_enabled);
Marc Zyngier4d449232014-01-14 18:00:55 +0000115 return true;
116}
117
Andre Przywara6d52f352014-06-03 10:13:13 +0200118/*
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.
123 */
124static bool access_gic_sgi(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +0300125 struct sys_reg_params *p,
Andre Przywara6d52f352014-06-03 10:13:13 +0200126 const struct sys_reg_desc *r)
127{
Andre Przywara6d52f352014-06-03 10:13:13 +0200128 if (!p->is_write)
129 return read_from_write_only(vcpu, p);
130
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300131 vgic_v3_dispatch_sgi(vcpu, p->regval);
Andre Przywara6d52f352014-06-03 10:13:13 +0200132
133 return true;
134}
135
Marc Zyngier7609c122014-04-24 10:21:16 +0100136static bool trap_raz_wi(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +0300137 struct sys_reg_params *p,
Marc Zyngier7609c122014-04-24 10:21:16 +0100138 const struct sys_reg_desc *r)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000139{
140 if (p->is_write)
141 return ignore_write(vcpu, p);
142 else
143 return read_zero(vcpu, p);
144}
145
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100146static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +0300147 struct sys_reg_params *p,
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100148 const struct sys_reg_desc *r)
149{
150 if (p->is_write) {
151 return ignore_write(vcpu, p);
152 } else {
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300153 p->regval = (1 << 3);
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100154 return true;
155 }
156}
157
158static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +0300159 struct sys_reg_params *p,
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100160 const struct sys_reg_desc *r)
161{
162 if (p->is_write) {
163 return ignore_write(vcpu, p);
164 } else {
165 u32 val;
166 asm volatile("mrs %0, dbgauthstatus_el1" : "=r" (val));
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300167 p->regval = val;
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100168 return true;
169 }
170}
171
172/*
173 * We want to avoid world-switching all the DBG registers all the
174 * time:
175 *
176 * - If we've touched any debug register, it is likely that we're
177 * going to touch more of them. It then makes sense to disable the
178 * traps and start doing the save/restore dance
179 * - If debug is active (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), it is
180 * then mandatory to save/restore the registers, as the guest
181 * depends on them.
182 *
183 * For this, we use a DIRTY bit, indicating the guest has modified the
184 * debug registers, used as follow:
185 *
186 * On guest entry:
187 * - If the dirty bit is set (because we're coming back from trapping),
188 * disable the traps, save host registers, restore guest registers.
189 * - If debug is actively in use (DBG_MDSCR_KDE or DBG_MDSCR_MDE set),
190 * set the dirty bit, disable the traps, save host registers,
191 * restore guest registers.
192 * - Otherwise, enable the traps
193 *
194 * On guest exit:
195 * - If the dirty bit is set, save guest registers, restore host
196 * registers and clear the dirty bit. This ensure that the host can
197 * now use the debug registers.
198 */
199static bool trap_debug_regs(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +0300200 struct sys_reg_params *p,
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100201 const struct sys_reg_desc *r)
202{
203 if (p->is_write) {
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300204 vcpu_sys_reg(vcpu, r->reg) = p->regval;
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100205 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
206 } else {
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300207 p->regval = vcpu_sys_reg(vcpu, r->reg);
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100208 }
209
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300210 trace_trap_reg(__func__, r->reg, p->is_write, p->regval);
Alex Bennéeeef8c852015-07-07 17:30:03 +0100211
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100212 return true;
213}
214
Alex Bennée84e690b2015-07-07 17:30:00 +0100215/*
216 * reg_to_dbg/dbg_to_reg
217 *
218 * A 32 bit write to a debug register leave top bits alone
219 * A 32 bit read from a debug register only returns the bottom bits
220 *
221 * All writes will set the KVM_ARM64_DEBUG_DIRTY flag to ensure the
222 * hyp.S code switches between host and guest values in future.
223 */
Marc Zyngier281243c2015-12-16 15:41:12 +0000224static void reg_to_dbg(struct kvm_vcpu *vcpu,
225 struct sys_reg_params *p,
226 u64 *dbg_reg)
Alex Bennée84e690b2015-07-07 17:30:00 +0100227{
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300228 u64 val = p->regval;
Alex Bennée84e690b2015-07-07 17:30:00 +0100229
230 if (p->is_32bit) {
231 val &= 0xffffffffUL;
232 val |= ((*dbg_reg >> 32) << 32);
233 }
234
235 *dbg_reg = val;
236 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
237}
238
Marc Zyngier281243c2015-12-16 15:41:12 +0000239static void dbg_to_reg(struct kvm_vcpu *vcpu,
240 struct sys_reg_params *p,
241 u64 *dbg_reg)
Alex Bennée84e690b2015-07-07 17:30:00 +0100242{
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300243 p->regval = *dbg_reg;
Alex Bennée84e690b2015-07-07 17:30:00 +0100244 if (p->is_32bit)
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300245 p->regval &= 0xffffffffUL;
Alex Bennée84e690b2015-07-07 17:30:00 +0100246}
247
Marc Zyngier281243c2015-12-16 15:41:12 +0000248static bool trap_bvr(struct kvm_vcpu *vcpu,
249 struct sys_reg_params *p,
250 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100251{
252 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg];
253
254 if (p->is_write)
255 reg_to_dbg(vcpu, p, dbg_reg);
256 else
257 dbg_to_reg(vcpu, p, dbg_reg);
258
Alex Bennéeeef8c852015-07-07 17:30:03 +0100259 trace_trap_reg(__func__, rd->reg, p->is_write, *dbg_reg);
260
Alex Bennée84e690b2015-07-07 17:30:00 +0100261 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
Marc Zyngier1713e5a2015-09-16 10:54:37 +0100269 if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0)
Alex Bennée84e690b2015-07-07 17:30:00 +0100270 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
Marc Zyngier281243c2015-12-16 15:41:12 +0000284static void reset_bvr(struct kvm_vcpu *vcpu,
285 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100286{
287 vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg] = rd->val;
288}
289
Marc Zyngier281243c2015-12-16 15:41:12 +0000290static bool trap_bcr(struct kvm_vcpu *vcpu,
291 struct sys_reg_params *p,
292 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100293{
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
Alex Bennéeeef8c852015-07-07 17:30:03 +0100301 trace_trap_reg(__func__, rd->reg, p->is_write, *dbg_reg);
302
Alex Bennée84e690b2015-07-07 17:30:00 +0100303 return true;
304}
305
306static int set_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
307 const struct kvm_one_reg *reg, void __user *uaddr)
308{
309 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg];
310
Marc Zyngier1713e5a2015-09-16 10:54:37 +0100311 if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0)
Alex Bennée84e690b2015-07-07 17:30:00 +0100312 return -EFAULT;
313
314 return 0;
315}
316
317static int get_bcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
318 const struct kvm_one_reg *reg, void __user *uaddr)
319{
320 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg];
321
322 if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
323 return -EFAULT;
324 return 0;
325}
326
Marc Zyngier281243c2015-12-16 15:41:12 +0000327static void reset_bcr(struct kvm_vcpu *vcpu,
328 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100329{
330 vcpu->arch.vcpu_debug_state.dbg_bcr[rd->reg] = rd->val;
331}
332
Marc Zyngier281243c2015-12-16 15:41:12 +0000333static bool trap_wvr(struct kvm_vcpu *vcpu,
334 struct sys_reg_params *p,
335 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100336{
337 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg];
338
339 if (p->is_write)
340 reg_to_dbg(vcpu, p, dbg_reg);
341 else
342 dbg_to_reg(vcpu, p, dbg_reg);
343
Alex Bennéeeef8c852015-07-07 17:30:03 +0100344 trace_trap_reg(__func__, rd->reg, p->is_write,
345 vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg]);
346
Alex Bennée84e690b2015-07-07 17:30:00 +0100347 return true;
348}
349
350static int set_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
351 const struct kvm_one_reg *reg, void __user *uaddr)
352{
353 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg];
354
Marc Zyngier1713e5a2015-09-16 10:54:37 +0100355 if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0)
Alex Bennée84e690b2015-07-07 17:30:00 +0100356 return -EFAULT;
357 return 0;
358}
359
360static int get_wvr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
361 const struct kvm_one_reg *reg, void __user *uaddr)
362{
363 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg];
364
365 if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
366 return -EFAULT;
367 return 0;
368}
369
Marc Zyngier281243c2015-12-16 15:41:12 +0000370static void reset_wvr(struct kvm_vcpu *vcpu,
371 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100372{
373 vcpu->arch.vcpu_debug_state.dbg_wvr[rd->reg] = rd->val;
374}
375
Marc Zyngier281243c2015-12-16 15:41:12 +0000376static bool trap_wcr(struct kvm_vcpu *vcpu,
377 struct sys_reg_params *p,
378 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100379{
380 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg];
381
382 if (p->is_write)
383 reg_to_dbg(vcpu, p, dbg_reg);
384 else
385 dbg_to_reg(vcpu, p, dbg_reg);
386
Alex Bennéeeef8c852015-07-07 17:30:03 +0100387 trace_trap_reg(__func__, rd->reg, p->is_write, *dbg_reg);
388
Alex Bennée84e690b2015-07-07 17:30:00 +0100389 return true;
390}
391
392static int set_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
393 const struct kvm_one_reg *reg, void __user *uaddr)
394{
395 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg];
396
Marc Zyngier1713e5a2015-09-16 10:54:37 +0100397 if (copy_from_user(r, uaddr, KVM_REG_SIZE(reg->id)) != 0)
Alex Bennée84e690b2015-07-07 17:30:00 +0100398 return -EFAULT;
399 return 0;
400}
401
402static int get_wcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *rd,
403 const struct kvm_one_reg *reg, void __user *uaddr)
404{
405 __u64 *r = &vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg];
406
407 if (copy_to_user(uaddr, r, KVM_REG_SIZE(reg->id)) != 0)
408 return -EFAULT;
409 return 0;
410}
411
Marc Zyngier281243c2015-12-16 15:41:12 +0000412static void reset_wcr(struct kvm_vcpu *vcpu,
413 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100414{
415 vcpu->arch.vcpu_debug_state.dbg_wcr[rd->reg] = rd->val;
416}
417
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000418static void reset_amair_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
419{
420 u64 amair;
421
422 asm volatile("mrs %0, amair_el1\n" : "=r" (amair));
423 vcpu_sys_reg(vcpu, AMAIR_EL1) = amair;
424}
425
426static void reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
427{
Andre Przywara4429fc62014-06-02 15:37:13 +0200428 u64 mpidr;
429
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000430 /*
Andre Przywara4429fc62014-06-02 15:37:13 +0200431 * Map the vcpu_id into the first three affinity level fields of
432 * the MPIDR. We limit the number of VCPUs in level 0 due to a
433 * limitation to 16 CPUs in that level in the ICC_SGIxR registers
434 * of the GICv3 to be able to address each CPU directly when
435 * sending IPIs.
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000436 */
Andre Przywara4429fc62014-06-02 15:37:13 +0200437 mpidr = (vcpu->vcpu_id & 0x0f) << MPIDR_LEVEL_SHIFT(0);
438 mpidr |= ((vcpu->vcpu_id >> 4) & 0xff) << MPIDR_LEVEL_SHIFT(1);
439 mpidr |= ((vcpu->vcpu_id >> 12) & 0xff) << MPIDR_LEVEL_SHIFT(2);
440 vcpu_sys_reg(vcpu, MPIDR_EL1) = (1ULL << 31) | mpidr;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000441}
442
Shannon Zhaoab946832015-06-18 16:01:53 +0800443static void reset_pmcr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
444{
445 u64 pmcr, val;
446
447 asm volatile("mrs %0, pmcr_el0\n" : "=r" (pmcr));
448 /* Writable bits of PMCR_EL0 (ARMV8_PMU_PMCR_MASK) is reset to UNKNOWN
449 * except PMCR.E resetting to zero.
450 */
451 val = ((pmcr & ~ARMV8_PMU_PMCR_MASK)
452 | (ARMV8_PMU_PMCR_MASK & 0xdecafbad)) & (~ARMV8_PMU_PMCR_E);
453 vcpu_sys_reg(vcpu, PMCR_EL0) = val;
454}
455
456static bool access_pmcr(struct kvm_vcpu *vcpu, struct sys_reg_params *p,
457 const struct sys_reg_desc *r)
458{
459 u64 val;
460
461 if (!kvm_arm_pmu_v3_ready(vcpu))
462 return trap_raz_wi(vcpu, p, r);
463
464 if (p->is_write) {
465 /* Only update writeable bits of PMCR */
466 val = vcpu_sys_reg(vcpu, PMCR_EL0);
467 val &= ~ARMV8_PMU_PMCR_MASK;
468 val |= p->regval & ARMV8_PMU_PMCR_MASK;
469 vcpu_sys_reg(vcpu, PMCR_EL0) = val;
470 } else {
471 /* PMCR.P & PMCR.C are RAZ */
472 val = vcpu_sys_reg(vcpu, PMCR_EL0)
473 & ~(ARMV8_PMU_PMCR_P | ARMV8_PMU_PMCR_C);
474 p->regval = val;
475 }
476
477 return true;
478}
479
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100480/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
481#define DBG_BCR_BVR_WCR_WVR_EL1(n) \
482 /* DBGBVRn_EL1 */ \
483 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b100), \
Alex Bennée84e690b2015-07-07 17:30:00 +0100484 trap_bvr, reset_bvr, n, 0, get_bvr, set_bvr }, \
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100485 /* DBGBCRn_EL1 */ \
486 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b101), \
Alex Bennée84e690b2015-07-07 17:30:00 +0100487 trap_bcr, reset_bcr, n, 0, get_bcr, set_bcr }, \
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100488 /* DBGWVRn_EL1 */ \
489 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b110), \
Alex Bennée84e690b2015-07-07 17:30:00 +0100490 trap_wvr, reset_wvr, n, 0, get_wvr, set_wvr }, \
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100491 /* DBGWCRn_EL1 */ \
492 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b111), \
Alex Bennée84e690b2015-07-07 17:30:00 +0100493 trap_wcr, reset_wcr, n, 0, get_wcr, set_wcr }
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100494
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000495/*
496 * Architected system registers.
497 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
Marc Zyngier7609c122014-04-24 10:21:16 +0100498 *
499 * We could trap ID_DFR0 and tell the guest we don't support performance
500 * monitoring. Unfortunately the patch to make the kernel check ID_DFR0 was
501 * NAKed, so it will read the PMCR anyway.
502 *
503 * Therefore we tell the guest we have 0 counters. Unfortunately, we
504 * must always support PMCCNTR (the cycle counter): we just RAZ/WI for
505 * all PM registers, which doesn't crash the guest kernel at least.
506 *
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100507 * Debug handling: We do trap most, if not all debug related system
508 * registers. The implementation is good enough to ensure that a guest
509 * can use these with minimal performance degradation. The drawback is
510 * that we don't implement any of the external debug, none of the
511 * OSlock protocol. This should be revisited if we ever encounter a
512 * more demanding guest...
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000513 */
514static const struct sys_reg_desc sys_reg_descs[] = {
515 /* DC ISW */
516 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b0110), Op2(0b010),
517 access_dcsw },
518 /* DC CSW */
519 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1010), Op2(0b010),
520 access_dcsw },
521 /* DC CISW */
522 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b010),
523 access_dcsw },
524
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100525 DBG_BCR_BVR_WCR_WVR_EL1(0),
526 DBG_BCR_BVR_WCR_WVR_EL1(1),
527 /* MDCCINT_EL1 */
528 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
529 trap_debug_regs, reset_val, MDCCINT_EL1, 0 },
530 /* MDSCR_EL1 */
531 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
532 trap_debug_regs, reset_val, MDSCR_EL1, 0 },
533 DBG_BCR_BVR_WCR_WVR_EL1(2),
534 DBG_BCR_BVR_WCR_WVR_EL1(3),
535 DBG_BCR_BVR_WCR_WVR_EL1(4),
536 DBG_BCR_BVR_WCR_WVR_EL1(5),
537 DBG_BCR_BVR_WCR_WVR_EL1(6),
538 DBG_BCR_BVR_WCR_WVR_EL1(7),
539 DBG_BCR_BVR_WCR_WVR_EL1(8),
540 DBG_BCR_BVR_WCR_WVR_EL1(9),
541 DBG_BCR_BVR_WCR_WVR_EL1(10),
542 DBG_BCR_BVR_WCR_WVR_EL1(11),
543 DBG_BCR_BVR_WCR_WVR_EL1(12),
544 DBG_BCR_BVR_WCR_WVR_EL1(13),
545 DBG_BCR_BVR_WCR_WVR_EL1(14),
546 DBG_BCR_BVR_WCR_WVR_EL1(15),
547
548 /* MDRAR_EL1 */
549 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
550 trap_raz_wi },
551 /* OSLAR_EL1 */
552 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b100),
553 trap_raz_wi },
554 /* OSLSR_EL1 */
555 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0001), Op2(0b100),
556 trap_oslsr_el1 },
557 /* OSDLR_EL1 */
558 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0011), Op2(0b100),
559 trap_raz_wi },
560 /* DBGPRCR_EL1 */
561 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0100), Op2(0b100),
562 trap_raz_wi },
563 /* DBGCLAIMSET_EL1 */
564 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1000), Op2(0b110),
565 trap_raz_wi },
566 /* DBGCLAIMCLR_EL1 */
567 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1001), Op2(0b110),
568 trap_raz_wi },
569 /* DBGAUTHSTATUS_EL1 */
570 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b110),
571 trap_dbgauthstatus_el1 },
572
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100573 /* MDCCSR_EL1 */
574 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0001), Op2(0b000),
575 trap_raz_wi },
576 /* DBGDTR_EL0 */
577 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0100), Op2(0b000),
578 trap_raz_wi },
579 /* DBGDTR[TR]X_EL0 */
580 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0101), Op2(0b000),
581 trap_raz_wi },
582
Marc Zyngier62a89c42013-02-07 10:32:33 +0000583 /* DBGVCR32_EL2 */
584 { Op0(0b10), Op1(0b100), CRn(0b0000), CRm(0b0111), Op2(0b000),
585 NULL, reset_val, DBGVCR32_EL2, 0 },
586
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000587 /* MPIDR_EL1 */
588 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b101),
589 NULL, reset_mpidr, MPIDR_EL1 },
590 /* SCTLR_EL1 */
591 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000592 access_vm_reg, reset_val, SCTLR_EL1, 0x00C50078 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000593 /* CPACR_EL1 */
594 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b010),
595 NULL, reset_val, CPACR_EL1, 0 },
596 /* TTBR0_EL1 */
597 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000598 access_vm_reg, reset_unknown, TTBR0_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000599 /* TTBR1_EL1 */
600 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000601 access_vm_reg, reset_unknown, TTBR1_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000602 /* TCR_EL1 */
603 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b010),
Marc Zyngier4d449232014-01-14 18:00:55 +0000604 access_vm_reg, reset_val, TCR_EL1, 0 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000605
606 /* AFSR0_EL1 */
607 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000608 access_vm_reg, reset_unknown, AFSR0_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000609 /* AFSR1_EL1 */
610 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000611 access_vm_reg, reset_unknown, AFSR1_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000612 /* ESR_EL1 */
613 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0010), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000614 access_vm_reg, reset_unknown, ESR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000615 /* FAR_EL1 */
616 { Op0(0b11), Op1(0b000), CRn(0b0110), CRm(0b0000), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000617 access_vm_reg, reset_unknown, FAR_EL1 },
Marc Zyngier1bbd8052013-06-07 11:02:34 +0100618 /* PAR_EL1 */
619 { Op0(0b11), Op1(0b000), CRn(0b0111), CRm(0b0100), Op2(0b000),
620 NULL, reset_unknown, PAR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000621
622 /* PMINTENSET_EL1 */
623 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100624 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000625 /* PMINTENCLR_EL1 */
626 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100627 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000628
629 /* MAIR_EL1 */
630 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0010), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000631 access_vm_reg, reset_unknown, MAIR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000632 /* AMAIR_EL1 */
633 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0011), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000634 access_vm_reg, reset_amair_el1, AMAIR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000635
636 /* VBAR_EL1 */
637 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b0000), Op2(0b000),
638 NULL, reset_val, VBAR_EL1, 0 },
Christoffer Dalldb7dedd2014-11-19 11:23:54 +0000639
Andre Przywara6d52f352014-06-03 10:13:13 +0200640 /* ICC_SGI1R_EL1 */
641 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b1011), Op2(0b101),
642 access_gic_sgi },
Christoffer Dalldb7dedd2014-11-19 11:23:54 +0000643 /* ICC_SRE_EL1 */
644 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b1100), Op2(0b101),
645 trap_raz_wi },
646
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000647 /* CONTEXTIDR_EL1 */
648 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000649 access_vm_reg, reset_val, CONTEXTIDR_EL1, 0 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000650 /* TPIDR_EL1 */
651 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b100),
652 NULL, reset_unknown, TPIDR_EL1 },
653
654 /* CNTKCTL_EL1 */
655 { Op0(0b11), Op1(0b000), CRn(0b1110), CRm(0b0001), Op2(0b000),
656 NULL, reset_val, CNTKCTL_EL1, 0},
657
658 /* CSSELR_EL1 */
659 { Op0(0b11), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
660 NULL, reset_unknown, CSSELR_EL1 },
661
662 /* PMCR_EL0 */
663 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b000),
Shannon Zhaoab946832015-06-18 16:01:53 +0800664 access_pmcr, reset_pmcr, },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000665 /* PMCNTENSET_EL0 */
666 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100667 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000668 /* PMCNTENCLR_EL0 */
669 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100670 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000671 /* PMOVSCLR_EL0 */
672 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b011),
Marc Zyngier7609c122014-04-24 10:21:16 +0100673 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000674 /* PMSWINC_EL0 */
675 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b100),
Marc Zyngier7609c122014-04-24 10:21:16 +0100676 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000677 /* PMSELR_EL0 */
678 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b101),
Marc Zyngier7609c122014-04-24 10:21:16 +0100679 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000680 /* PMCEID0_EL0 */
681 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b110),
Marc Zyngier7609c122014-04-24 10:21:16 +0100682 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000683 /* PMCEID1_EL0 */
684 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b111),
Marc Zyngier7609c122014-04-24 10:21:16 +0100685 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000686 /* PMCCNTR_EL0 */
687 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b000),
Marc Zyngier7609c122014-04-24 10:21:16 +0100688 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000689 /* PMXEVTYPER_EL0 */
690 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100691 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000692 /* PMXEVCNTR_EL0 */
693 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100694 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000695 /* PMUSERENR_EL0 */
696 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b000),
Marc Zyngier7609c122014-04-24 10:21:16 +0100697 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000698 /* PMOVSSET_EL0 */
699 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b011),
Marc Zyngier7609c122014-04-24 10:21:16 +0100700 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000701
702 /* TPIDR_EL0 */
703 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b010),
704 NULL, reset_unknown, TPIDR_EL0 },
705 /* TPIDRRO_EL0 */
706 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b011),
707 NULL, reset_unknown, TPIDRRO_EL0 },
Marc Zyngier62a89c42013-02-07 10:32:33 +0000708
709 /* DACR32_EL2 */
710 { Op0(0b11), Op1(0b100), CRn(0b0011), CRm(0b0000), Op2(0b000),
711 NULL, reset_unknown, DACR32_EL2 },
712 /* IFSR32_EL2 */
713 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0000), Op2(0b001),
714 NULL, reset_unknown, IFSR32_EL2 },
715 /* FPEXC32_EL2 */
716 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0011), Op2(0b000),
717 NULL, reset_val, FPEXC32_EL2, 0x70 },
718};
719
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100720static bool trap_dbgidr(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +0300721 struct sys_reg_params *p,
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100722 const struct sys_reg_desc *r)
723{
724 if (p->is_write) {
725 return ignore_write(vcpu, p);
726 } else {
Suzuki K. Poulose4db8e5e2015-10-19 14:24:55 +0100727 u64 dfr = read_system_reg(SYS_ID_AA64DFR0_EL1);
728 u64 pfr = read_system_reg(SYS_ID_AA64PFR0_EL1);
729 u32 el3 = !!cpuid_feature_extract_field(pfr, ID_AA64PFR0_EL3_SHIFT);
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100730
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300731 p->regval = ((((dfr >> ID_AA64DFR0_WRPS_SHIFT) & 0xf) << 28) |
732 (((dfr >> ID_AA64DFR0_BRPS_SHIFT) & 0xf) << 24) |
733 (((dfr >> ID_AA64DFR0_CTX_CMPS_SHIFT) & 0xf) << 20)
734 | (6 << 16) | (el3 << 14) | (el3 << 12));
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100735 return true;
736 }
737}
738
739static bool trap_debug32(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +0300740 struct sys_reg_params *p,
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100741 const struct sys_reg_desc *r)
742{
743 if (p->is_write) {
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300744 vcpu_cp14(vcpu, r->reg) = p->regval;
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100745 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
746 } else {
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300747 p->regval = vcpu_cp14(vcpu, r->reg);
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100748 }
749
750 return true;
751}
752
Alex Bennée84e690b2015-07-07 17:30:00 +0100753/* AArch32 debug register mappings
754 *
755 * AArch32 DBGBVRn is mapped to DBGBVRn_EL1[31:0]
756 * AArch32 DBGBXVRn is mapped to DBGBVRn_EL1[63:32]
757 *
758 * All control registers and watchpoint value registers are mapped to
759 * the lower 32 bits of their AArch64 equivalents. We share the trap
760 * handlers with the above AArch64 code which checks what mode the
761 * system is in.
762 */
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100763
Marc Zyngier281243c2015-12-16 15:41:12 +0000764static bool trap_xvr(struct kvm_vcpu *vcpu,
765 struct sys_reg_params *p,
766 const struct sys_reg_desc *rd)
Alex Bennée84e690b2015-07-07 17:30:00 +0100767{
768 u64 *dbg_reg = &vcpu->arch.vcpu_debug_state.dbg_bvr[rd->reg];
769
770 if (p->is_write) {
771 u64 val = *dbg_reg;
772
773 val &= 0xffffffffUL;
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300774 val |= p->regval << 32;
Alex Bennée84e690b2015-07-07 17:30:00 +0100775 *dbg_reg = val;
776
777 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
778 } else {
Pavel Fedin2ec5be32015-12-04 15:03:13 +0300779 p->regval = *dbg_reg >> 32;
Alex Bennée84e690b2015-07-07 17:30:00 +0100780 }
781
Alex Bennéeeef8c852015-07-07 17:30:03 +0100782 trace_trap_reg(__func__, rd->reg, p->is_write, *dbg_reg);
783
Alex Bennée84e690b2015-07-07 17:30:00 +0100784 return true;
785}
786
787#define DBG_BCR_BVR_WCR_WVR(n) \
788 /* DBGBVRn */ \
789 { Op1( 0), CRn( 0), CRm((n)), Op2( 4), trap_bvr, NULL, n }, \
790 /* DBGBCRn */ \
791 { Op1( 0), CRn( 0), CRm((n)), Op2( 5), trap_bcr, NULL, n }, \
792 /* DBGWVRn */ \
793 { Op1( 0), CRn( 0), CRm((n)), Op2( 6), trap_wvr, NULL, n }, \
794 /* DBGWCRn */ \
795 { Op1( 0), CRn( 0), CRm((n)), Op2( 7), trap_wcr, NULL, n }
796
797#define DBGBXVR(n) \
798 { Op1( 0), CRn( 1), CRm((n)), Op2( 1), trap_xvr, NULL, n }
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100799
800/*
801 * Trapped cp14 registers. We generally ignore most of the external
802 * debug, on the principle that they don't really make sense to a
Alex Bennée84e690b2015-07-07 17:30:00 +0100803 * guest. Revisit this one day, would this principle change.
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100804 */
Marc Zyngier72564012014-04-24 10:27:13 +0100805static const struct sys_reg_desc cp14_regs[] = {
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100806 /* DBGIDR */
807 { Op1( 0), CRn( 0), CRm( 0), Op2( 0), trap_dbgidr },
808 /* DBGDTRRXext */
809 { Op1( 0), CRn( 0), CRm( 0), Op2( 2), trap_raz_wi },
810
811 DBG_BCR_BVR_WCR_WVR(0),
812 /* DBGDSCRint */
813 { Op1( 0), CRn( 0), CRm( 1), Op2( 0), trap_raz_wi },
814 DBG_BCR_BVR_WCR_WVR(1),
815 /* DBGDCCINT */
816 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), trap_debug32 },
817 /* DBGDSCRext */
818 { Op1( 0), CRn( 0), CRm( 2), Op2( 2), trap_debug32 },
819 DBG_BCR_BVR_WCR_WVR(2),
820 /* DBGDTR[RT]Xint */
821 { Op1( 0), CRn( 0), CRm( 3), Op2( 0), trap_raz_wi },
822 /* DBGDTR[RT]Xext */
823 { Op1( 0), CRn( 0), CRm( 3), Op2( 2), trap_raz_wi },
824 DBG_BCR_BVR_WCR_WVR(3),
825 DBG_BCR_BVR_WCR_WVR(4),
826 DBG_BCR_BVR_WCR_WVR(5),
827 /* DBGWFAR */
828 { Op1( 0), CRn( 0), CRm( 6), Op2( 0), trap_raz_wi },
829 /* DBGOSECCR */
830 { Op1( 0), CRn( 0), CRm( 6), Op2( 2), trap_raz_wi },
831 DBG_BCR_BVR_WCR_WVR(6),
832 /* DBGVCR */
833 { Op1( 0), CRn( 0), CRm( 7), Op2( 0), trap_debug32 },
834 DBG_BCR_BVR_WCR_WVR(7),
835 DBG_BCR_BVR_WCR_WVR(8),
836 DBG_BCR_BVR_WCR_WVR(9),
837 DBG_BCR_BVR_WCR_WVR(10),
838 DBG_BCR_BVR_WCR_WVR(11),
839 DBG_BCR_BVR_WCR_WVR(12),
840 DBG_BCR_BVR_WCR_WVR(13),
841 DBG_BCR_BVR_WCR_WVR(14),
842 DBG_BCR_BVR_WCR_WVR(15),
843
844 /* DBGDRAR (32bit) */
845 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), trap_raz_wi },
846
847 DBGBXVR(0),
848 /* DBGOSLAR */
849 { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi },
850 DBGBXVR(1),
851 /* DBGOSLSR */
852 { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1 },
853 DBGBXVR(2),
854 DBGBXVR(3),
855 /* DBGOSDLR */
856 { Op1( 0), CRn( 1), CRm( 3), Op2( 4), trap_raz_wi },
857 DBGBXVR(4),
858 /* DBGPRCR */
859 { Op1( 0), CRn( 1), CRm( 4), Op2( 4), trap_raz_wi },
860 DBGBXVR(5),
861 DBGBXVR(6),
862 DBGBXVR(7),
863 DBGBXVR(8),
864 DBGBXVR(9),
865 DBGBXVR(10),
866 DBGBXVR(11),
867 DBGBXVR(12),
868 DBGBXVR(13),
869 DBGBXVR(14),
870 DBGBXVR(15),
871
872 /* DBGDSAR (32bit) */
873 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), trap_raz_wi },
874
875 /* DBGDEVID2 */
876 { Op1( 0), CRn( 7), CRm( 0), Op2( 7), trap_raz_wi },
877 /* DBGDEVID1 */
878 { Op1( 0), CRn( 7), CRm( 1), Op2( 7), trap_raz_wi },
879 /* DBGDEVID */
880 { Op1( 0), CRn( 7), CRm( 2), Op2( 7), trap_raz_wi },
881 /* DBGCLAIMSET */
882 { Op1( 0), CRn( 7), CRm( 8), Op2( 6), trap_raz_wi },
883 /* DBGCLAIMCLR */
884 { Op1( 0), CRn( 7), CRm( 9), Op2( 6), trap_raz_wi },
885 /* DBGAUTHSTATUS */
886 { Op1( 0), CRn( 7), CRm(14), Op2( 6), trap_dbgauthstatus_el1 },
Marc Zyngier72564012014-04-24 10:27:13 +0100887};
888
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100889/* Trapped cp14 64bit registers */
890static const struct sys_reg_desc cp14_64_regs[] = {
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100891 /* DBGDRAR (64bit) */
892 { Op1( 0), CRm( 1), .access = trap_raz_wi },
893
894 /* DBGDSAR (64bit) */
895 { Op1( 0), CRm( 2), .access = trap_raz_wi },
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100896};
897
Marc Zyngier4d449232014-01-14 18:00:55 +0000898/*
899 * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding,
900 * depending on the way they are accessed (as a 32bit or a 64bit
901 * register).
902 */
Marc Zyngier62a89c42013-02-07 10:32:33 +0000903static const struct sys_reg_desc cp15_regs[] = {
Andre Przywara6d52f352014-06-03 10:13:13 +0200904 { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi },
905
Marc Zyngier3c1e7162014-12-19 16:05:31 +0000906 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), access_vm_reg, NULL, c1_SCTLR },
Marc Zyngier4d449232014-01-14 18:00:55 +0000907 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
908 { Op1( 0), CRn( 2), CRm( 0), Op2( 1), access_vm_reg, NULL, c2_TTBR1 },
909 { Op1( 0), CRn( 2), CRm( 0), Op2( 2), access_vm_reg, NULL, c2_TTBCR },
910 { Op1( 0), CRn( 3), CRm( 0), Op2( 0), access_vm_reg, NULL, c3_DACR },
911 { Op1( 0), CRn( 5), CRm( 0), Op2( 0), access_vm_reg, NULL, c5_DFSR },
912 { Op1( 0), CRn( 5), CRm( 0), Op2( 1), access_vm_reg, NULL, c5_IFSR },
913 { Op1( 0), CRn( 5), CRm( 1), Op2( 0), access_vm_reg, NULL, c5_ADFSR },
914 { Op1( 0), CRn( 5), CRm( 1), Op2( 1), access_vm_reg, NULL, c5_AIFSR },
915 { Op1( 0), CRn( 6), CRm( 0), Op2( 0), access_vm_reg, NULL, c6_DFAR },
916 { Op1( 0), CRn( 6), CRm( 0), Op2( 2), access_vm_reg, NULL, c6_IFAR },
917
Marc Zyngier62a89c42013-02-07 10:32:33 +0000918 /*
919 * DC{C,I,CI}SW operations:
920 */
921 { Op1( 0), CRn( 7), CRm( 6), Op2( 2), access_dcsw },
922 { Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw },
923 { Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw },
Marc Zyngier4d449232014-01-14 18:00:55 +0000924
Marc Zyngier7609c122014-04-24 10:21:16 +0100925 /* PMU */
Shannon Zhaoab946832015-06-18 16:01:53 +0800926 { Op1( 0), CRn( 9), CRm(12), Op2( 0), access_pmcr },
Marc Zyngier7609c122014-04-24 10:21:16 +0100927 { Op1( 0), CRn( 9), CRm(12), Op2( 1), trap_raz_wi },
928 { Op1( 0), CRn( 9), CRm(12), Op2( 2), trap_raz_wi },
929 { Op1( 0), CRn( 9), CRm(12), Op2( 3), trap_raz_wi },
930 { Op1( 0), CRn( 9), CRm(12), Op2( 5), trap_raz_wi },
931 { Op1( 0), CRn( 9), CRm(12), Op2( 6), trap_raz_wi },
932 { Op1( 0), CRn( 9), CRm(12), Op2( 7), trap_raz_wi },
933 { Op1( 0), CRn( 9), CRm(13), Op2( 0), trap_raz_wi },
934 { Op1( 0), CRn( 9), CRm(13), Op2( 1), trap_raz_wi },
935 { Op1( 0), CRn( 9), CRm(13), Op2( 2), trap_raz_wi },
936 { Op1( 0), CRn( 9), CRm(14), Op2( 0), trap_raz_wi },
937 { Op1( 0), CRn( 9), CRm(14), Op2( 1), trap_raz_wi },
938 { Op1( 0), CRn( 9), CRm(14), Op2( 2), trap_raz_wi },
Marc Zyngier4d449232014-01-14 18:00:55 +0000939
940 { Op1( 0), CRn(10), CRm( 2), Op2( 0), access_vm_reg, NULL, c10_PRRR },
941 { Op1( 0), CRn(10), CRm( 2), Op2( 1), access_vm_reg, NULL, c10_NMRR },
942 { Op1( 0), CRn(10), CRm( 3), Op2( 0), access_vm_reg, NULL, c10_AMAIR0 },
943 { Op1( 0), CRn(10), CRm( 3), Op2( 1), access_vm_reg, NULL, c10_AMAIR1 },
Christoffer Dalldb7dedd2014-11-19 11:23:54 +0000944
945 /* ICC_SRE */
946 { Op1( 0), CRn(12), CRm(12), Op2( 5), trap_raz_wi },
947
Marc Zyngier4d449232014-01-14 18:00:55 +0000948 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID },
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100949};
950
951static const struct sys_reg_desc cp15_64_regs[] = {
952 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
Andre Przywara6d52f352014-06-03 10:13:13 +0200953 { Op1( 0), CRn( 0), CRm(12), Op2( 0), access_gic_sgi },
Marc Zyngier4d449232014-01-14 18:00:55 +0000954 { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000955};
956
957/* Target specific emulation tables */
958static struct kvm_sys_reg_target_table *target_tables[KVM_ARM_NUM_TARGETS];
959
960void kvm_register_target_sys_reg_table(unsigned int target,
961 struct kvm_sys_reg_target_table *table)
962{
963 target_tables[target] = table;
964}
965
966/* Get specific register table for this target. */
Marc Zyngier62a89c42013-02-07 10:32:33 +0000967static const struct sys_reg_desc *get_target_table(unsigned target,
968 bool mode_is_64,
969 size_t *num)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000970{
971 struct kvm_sys_reg_target_table *table;
972
973 table = target_tables[target];
Marc Zyngier62a89c42013-02-07 10:32:33 +0000974 if (mode_is_64) {
975 *num = table->table64.num;
976 return table->table64.table;
977 } else {
978 *num = table->table32.num;
979 return table->table32.table;
980 }
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000981}
982
983static const struct sys_reg_desc *find_reg(const struct sys_reg_params *params,
984 const struct sys_reg_desc table[],
985 unsigned int num)
986{
987 unsigned int i;
988
989 for (i = 0; i < num; i++) {
990 const struct sys_reg_desc *r = &table[i];
991
992 if (params->Op0 != r->Op0)
993 continue;
994 if (params->Op1 != r->Op1)
995 continue;
996 if (params->CRn != r->CRn)
997 continue;
998 if (params->CRm != r->CRm)
999 continue;
1000 if (params->Op2 != r->Op2)
1001 continue;
1002
1003 return r;
1004 }
1005 return NULL;
1006}
1007
Marc Zyngier62a89c42013-02-07 10:32:33 +00001008int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu, struct kvm_run *run)
1009{
1010 kvm_inject_undefined(vcpu);
1011 return 1;
1012}
1013
Marc Zyngier72564012014-04-24 10:27:13 +01001014/*
1015 * emulate_cp -- tries to match a sys_reg access in a handling table, and
1016 * call the corresponding trap handler.
1017 *
1018 * @params: pointer to the descriptor of the access
1019 * @table: array of trap descriptors
1020 * @num: size of the trap descriptor array
1021 *
1022 * Return 0 if the access has been handled, and -1 if not.
1023 */
1024static int emulate_cp(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +03001025 struct sys_reg_params *params,
Marc Zyngier72564012014-04-24 10:27:13 +01001026 const struct sys_reg_desc *table,
1027 size_t num)
Marc Zyngier62a89c42013-02-07 10:32:33 +00001028{
Marc Zyngier72564012014-04-24 10:27:13 +01001029 const struct sys_reg_desc *r;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001030
Marc Zyngier72564012014-04-24 10:27:13 +01001031 if (!table)
1032 return -1; /* Not handled */
Marc Zyngier62a89c42013-02-07 10:32:33 +00001033
Marc Zyngier62a89c42013-02-07 10:32:33 +00001034 r = find_reg(params, table, num);
Marc Zyngier62a89c42013-02-07 10:32:33 +00001035
Marc Zyngier72564012014-04-24 10:27:13 +01001036 if (r) {
Marc Zyngier62a89c42013-02-07 10:32:33 +00001037 /*
1038 * Not having an accessor means that we have
1039 * configured a trap that we don't know how to
1040 * handle. This certainly qualifies as a gross bug
1041 * that should be fixed right away.
1042 */
1043 BUG_ON(!r->access);
1044
1045 if (likely(r->access(vcpu, params, r))) {
1046 /* Skip instruction, since it was emulated */
1047 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
Shannon Zhao6327f352016-01-13 17:16:41 +08001048 /* Handled */
1049 return 0;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001050 }
Marc Zyngier62a89c42013-02-07 10:32:33 +00001051 }
1052
Marc Zyngier72564012014-04-24 10:27:13 +01001053 /* Not handled */
1054 return -1;
1055}
1056
1057static void unhandled_cp_access(struct kvm_vcpu *vcpu,
1058 struct sys_reg_params *params)
1059{
1060 u8 hsr_ec = kvm_vcpu_trap_get_class(vcpu);
1061 int cp;
1062
1063 switch(hsr_ec) {
Mark Rutlandc6d01a92014-11-24 13:59:30 +00001064 case ESR_ELx_EC_CP15_32:
1065 case ESR_ELx_EC_CP15_64:
Marc Zyngier72564012014-04-24 10:27:13 +01001066 cp = 15;
1067 break;
Mark Rutlandc6d01a92014-11-24 13:59:30 +00001068 case ESR_ELx_EC_CP14_MR:
1069 case ESR_ELx_EC_CP14_64:
Marc Zyngier72564012014-04-24 10:27:13 +01001070 cp = 14;
1071 break;
1072 default:
1073 WARN_ON((cp = -1));
1074 }
1075
1076 kvm_err("Unsupported guest CP%d access at: %08lx\n",
1077 cp, *vcpu_pc(vcpu));
Marc Zyngier62a89c42013-02-07 10:32:33 +00001078 print_sys_reg_instr(params);
1079 kvm_inject_undefined(vcpu);
1080}
1081
1082/**
Shannon Zhao7769db92016-01-13 17:16:40 +08001083 * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP14/CP15 access
Marc Zyngier62a89c42013-02-07 10:32:33 +00001084 * @vcpu: The VCPU pointer
1085 * @run: The kvm_run struct
1086 */
Marc Zyngier72564012014-04-24 10:27:13 +01001087static int kvm_handle_cp_64(struct kvm_vcpu *vcpu,
1088 const struct sys_reg_desc *global,
1089 size_t nr_global,
1090 const struct sys_reg_desc *target_specific,
1091 size_t nr_specific)
Marc Zyngier62a89c42013-02-07 10:32:33 +00001092{
1093 struct sys_reg_params params;
1094 u32 hsr = kvm_vcpu_get_hsr(vcpu);
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001095 int Rt = (hsr >> 5) & 0xf;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001096 int Rt2 = (hsr >> 10) & 0xf;
1097
Marc Zyngier2072d292014-01-21 10:55:17 +00001098 params.is_aarch32 = true;
1099 params.is_32bit = false;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001100 params.CRm = (hsr >> 1) & 0xf;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001101 params.is_write = ((hsr & 1) == 0);
1102
1103 params.Op0 = 0;
1104 params.Op1 = (hsr >> 16) & 0xf;
1105 params.Op2 = 0;
1106 params.CRn = 0;
1107
1108 /*
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001109 * Make a 64-bit value out of Rt and Rt2. As we use the same trap
Marc Zyngier62a89c42013-02-07 10:32:33 +00001110 * backends between AArch32 and AArch64, we get away with it.
1111 */
1112 if (params.is_write) {
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001113 params.regval = vcpu_get_reg(vcpu, Rt) & 0xffffffff;
1114 params.regval |= vcpu_get_reg(vcpu, Rt2) << 32;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001115 }
1116
Marc Zyngier72564012014-04-24 10:27:13 +01001117 if (!emulate_cp(vcpu, &params, target_specific, nr_specific))
1118 goto out;
1119 if (!emulate_cp(vcpu, &params, global, nr_global))
1120 goto out;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001121
Marc Zyngier72564012014-04-24 10:27:13 +01001122 unhandled_cp_access(vcpu, &params);
1123
1124out:
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001125 /* Split up the value between registers for the read side */
Marc Zyngier62a89c42013-02-07 10:32:33 +00001126 if (!params.is_write) {
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001127 vcpu_set_reg(vcpu, Rt, lower_32_bits(params.regval));
1128 vcpu_set_reg(vcpu, Rt2, upper_32_bits(params.regval));
Marc Zyngier62a89c42013-02-07 10:32:33 +00001129 }
1130
1131 return 1;
1132}
1133
1134/**
Shannon Zhao7769db92016-01-13 17:16:40 +08001135 * kvm_handle_cp_32 -- handles a mrc/mcr trap on a guest CP14/CP15 access
Marc Zyngier62a89c42013-02-07 10:32:33 +00001136 * @vcpu: The VCPU pointer
1137 * @run: The kvm_run struct
1138 */
Marc Zyngier72564012014-04-24 10:27:13 +01001139static int kvm_handle_cp_32(struct kvm_vcpu *vcpu,
1140 const struct sys_reg_desc *global,
1141 size_t nr_global,
1142 const struct sys_reg_desc *target_specific,
1143 size_t nr_specific)
Marc Zyngier62a89c42013-02-07 10:32:33 +00001144{
1145 struct sys_reg_params params;
1146 u32 hsr = kvm_vcpu_get_hsr(vcpu);
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001147 int Rt = (hsr >> 5) & 0xf;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001148
Marc Zyngier2072d292014-01-21 10:55:17 +00001149 params.is_aarch32 = true;
1150 params.is_32bit = true;
Marc Zyngier62a89c42013-02-07 10:32:33 +00001151 params.CRm = (hsr >> 1) & 0xf;
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001152 params.regval = vcpu_get_reg(vcpu, Rt);
Marc Zyngier62a89c42013-02-07 10:32:33 +00001153 params.is_write = ((hsr & 1) == 0);
1154 params.CRn = (hsr >> 10) & 0xf;
1155 params.Op0 = 0;
1156 params.Op1 = (hsr >> 14) & 0x7;
1157 params.Op2 = (hsr >> 17) & 0x7;
1158
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001159 if (!emulate_cp(vcpu, &params, target_specific, nr_specific) ||
1160 !emulate_cp(vcpu, &params, global, nr_global)) {
1161 if (!params.is_write)
1162 vcpu_set_reg(vcpu, Rt, params.regval);
Marc Zyngier72564012014-04-24 10:27:13 +01001163 return 1;
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001164 }
Marc Zyngier72564012014-04-24 10:27:13 +01001165
1166 unhandled_cp_access(vcpu, &params);
Marc Zyngier62a89c42013-02-07 10:32:33 +00001167 return 1;
1168}
1169
Marc Zyngier72564012014-04-24 10:27:13 +01001170int kvm_handle_cp15_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
1171{
1172 const struct sys_reg_desc *target_specific;
1173 size_t num;
1174
1175 target_specific = get_target_table(vcpu->arch.target, false, &num);
1176 return kvm_handle_cp_64(vcpu,
Marc Zyngiera9866ba02014-04-24 14:11:48 +01001177 cp15_64_regs, ARRAY_SIZE(cp15_64_regs),
Marc Zyngier72564012014-04-24 10:27:13 +01001178 target_specific, num);
1179}
1180
1181int kvm_handle_cp15_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
1182{
1183 const struct sys_reg_desc *target_specific;
1184 size_t num;
1185
1186 target_specific = get_target_table(vcpu->arch.target, false, &num);
1187 return kvm_handle_cp_32(vcpu,
1188 cp15_regs, ARRAY_SIZE(cp15_regs),
1189 target_specific, num);
1190}
1191
1192int kvm_handle_cp14_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
1193{
1194 return kvm_handle_cp_64(vcpu,
Marc Zyngiera9866ba02014-04-24 14:11:48 +01001195 cp14_64_regs, ARRAY_SIZE(cp14_64_regs),
Marc Zyngier72564012014-04-24 10:27:13 +01001196 NULL, 0);
1197}
1198
1199int kvm_handle_cp14_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
1200{
1201 return kvm_handle_cp_32(vcpu,
1202 cp14_regs, ARRAY_SIZE(cp14_regs),
1203 NULL, 0);
1204}
1205
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001206static int emulate_sys_reg(struct kvm_vcpu *vcpu,
Pavel Fedin3fec0372015-12-04 15:03:12 +03001207 struct sys_reg_params *params)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001208{
1209 size_t num;
1210 const struct sys_reg_desc *table, *r;
1211
Marc Zyngier62a89c42013-02-07 10:32:33 +00001212 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001213
1214 /* Search target-specific then generic table. */
1215 r = find_reg(params, table, num);
1216 if (!r)
1217 r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1218
1219 if (likely(r)) {
1220 /*
1221 * Not having an accessor means that we have
1222 * configured a trap that we don't know how to
1223 * handle. This certainly qualifies as a gross bug
1224 * that should be fixed right away.
1225 */
1226 BUG_ON(!r->access);
1227
1228 if (likely(r->access(vcpu, params, r))) {
1229 /* Skip instruction, since it was emulated */
1230 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
1231 return 1;
1232 }
1233 /* If access function fails, it should complain. */
1234 } else {
1235 kvm_err("Unsupported guest sys_reg access at: %lx\n",
1236 *vcpu_pc(vcpu));
1237 print_sys_reg_instr(params);
1238 }
1239 kvm_inject_undefined(vcpu);
1240 return 1;
1241}
1242
1243static void reset_sys_reg_descs(struct kvm_vcpu *vcpu,
1244 const struct sys_reg_desc *table, size_t num)
1245{
1246 unsigned long i;
1247
1248 for (i = 0; i < num; i++)
1249 if (table[i].reset)
1250 table[i].reset(vcpu, &table[i]);
1251}
1252
1253/**
1254 * kvm_handle_sys_reg -- handles a mrs/msr trap on a guest sys_reg access
1255 * @vcpu: The VCPU pointer
1256 * @run: The kvm_run struct
1257 */
1258int kvm_handle_sys_reg(struct kvm_vcpu *vcpu, struct kvm_run *run)
1259{
1260 struct sys_reg_params params;
1261 unsigned long esr = kvm_vcpu_get_hsr(vcpu);
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001262 int Rt = (esr >> 5) & 0x1f;
1263 int ret;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001264
Alex Bennéeeef8c852015-07-07 17:30:03 +01001265 trace_kvm_handle_sys_reg(esr);
1266
Marc Zyngier2072d292014-01-21 10:55:17 +00001267 params.is_aarch32 = false;
1268 params.is_32bit = false;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001269 params.Op0 = (esr >> 20) & 3;
1270 params.Op1 = (esr >> 14) & 0x7;
1271 params.CRn = (esr >> 10) & 0xf;
1272 params.CRm = (esr >> 1) & 0xf;
1273 params.Op2 = (esr >> 17) & 0x7;
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001274 params.regval = vcpu_get_reg(vcpu, Rt);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001275 params.is_write = !(esr & 1);
1276
Pavel Fedin2ec5be32015-12-04 15:03:13 +03001277 ret = emulate_sys_reg(vcpu, &params);
1278
1279 if (!params.is_write)
1280 vcpu_set_reg(vcpu, Rt, params.regval);
1281 return ret;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001282}
1283
1284/******************************************************************************
1285 * Userspace API
1286 *****************************************************************************/
1287
1288static bool index_to_params(u64 id, struct sys_reg_params *params)
1289{
1290 switch (id & KVM_REG_SIZE_MASK) {
1291 case KVM_REG_SIZE_U64:
1292 /* Any unused index bits means it's not valid. */
1293 if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
1294 | KVM_REG_ARM_COPROC_MASK
1295 | KVM_REG_ARM64_SYSREG_OP0_MASK
1296 | KVM_REG_ARM64_SYSREG_OP1_MASK
1297 | KVM_REG_ARM64_SYSREG_CRN_MASK
1298 | KVM_REG_ARM64_SYSREG_CRM_MASK
1299 | KVM_REG_ARM64_SYSREG_OP2_MASK))
1300 return false;
1301 params->Op0 = ((id & KVM_REG_ARM64_SYSREG_OP0_MASK)
1302 >> KVM_REG_ARM64_SYSREG_OP0_SHIFT);
1303 params->Op1 = ((id & KVM_REG_ARM64_SYSREG_OP1_MASK)
1304 >> KVM_REG_ARM64_SYSREG_OP1_SHIFT);
1305 params->CRn = ((id & KVM_REG_ARM64_SYSREG_CRN_MASK)
1306 >> KVM_REG_ARM64_SYSREG_CRN_SHIFT);
1307 params->CRm = ((id & KVM_REG_ARM64_SYSREG_CRM_MASK)
1308 >> KVM_REG_ARM64_SYSREG_CRM_SHIFT);
1309 params->Op2 = ((id & KVM_REG_ARM64_SYSREG_OP2_MASK)
1310 >> KVM_REG_ARM64_SYSREG_OP2_SHIFT);
1311 return true;
1312 default:
1313 return false;
1314 }
1315}
1316
1317/* Decode an index value, and find the sys_reg_desc entry. */
1318static const struct sys_reg_desc *index_to_sys_reg_desc(struct kvm_vcpu *vcpu,
1319 u64 id)
1320{
1321 size_t num;
1322 const struct sys_reg_desc *table, *r;
1323 struct sys_reg_params params;
1324
1325 /* We only do sys_reg for now. */
1326 if ((id & KVM_REG_ARM_COPROC_MASK) != KVM_REG_ARM64_SYSREG)
1327 return NULL;
1328
1329 if (!index_to_params(id, &params))
1330 return NULL;
1331
Marc Zyngier62a89c42013-02-07 10:32:33 +00001332 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001333 r = find_reg(&params, table, num);
1334 if (!r)
1335 r = find_reg(&params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1336
1337 /* Not saved in the sys_reg array? */
1338 if (r && !r->reg)
1339 r = NULL;
1340
1341 return r;
1342}
1343
1344/*
1345 * These are the invariant sys_reg registers: we let the guest see the
1346 * host versions of these, so they're part of the guest state.
1347 *
1348 * A future CPU may provide a mechanism to present different values to
1349 * the guest, or a future kvm may trap them.
1350 */
1351
1352#define FUNCTION_INVARIANT(reg) \
1353 static void get_##reg(struct kvm_vcpu *v, \
1354 const struct sys_reg_desc *r) \
1355 { \
1356 u64 val; \
1357 \
1358 asm volatile("mrs %0, " __stringify(reg) "\n" \
1359 : "=r" (val)); \
1360 ((struct sys_reg_desc *)r)->val = val; \
1361 }
1362
1363FUNCTION_INVARIANT(midr_el1)
1364FUNCTION_INVARIANT(ctr_el0)
1365FUNCTION_INVARIANT(revidr_el1)
1366FUNCTION_INVARIANT(id_pfr0_el1)
1367FUNCTION_INVARIANT(id_pfr1_el1)
1368FUNCTION_INVARIANT(id_dfr0_el1)
1369FUNCTION_INVARIANT(id_afr0_el1)
1370FUNCTION_INVARIANT(id_mmfr0_el1)
1371FUNCTION_INVARIANT(id_mmfr1_el1)
1372FUNCTION_INVARIANT(id_mmfr2_el1)
1373FUNCTION_INVARIANT(id_mmfr3_el1)
1374FUNCTION_INVARIANT(id_isar0_el1)
1375FUNCTION_INVARIANT(id_isar1_el1)
1376FUNCTION_INVARIANT(id_isar2_el1)
1377FUNCTION_INVARIANT(id_isar3_el1)
1378FUNCTION_INVARIANT(id_isar4_el1)
1379FUNCTION_INVARIANT(id_isar5_el1)
1380FUNCTION_INVARIANT(clidr_el1)
1381FUNCTION_INVARIANT(aidr_el1)
1382
1383/* ->val is filled in by kvm_sys_reg_table_init() */
1384static struct sys_reg_desc invariant_sys_regs[] = {
1385 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b000),
1386 NULL, get_midr_el1 },
1387 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b110),
1388 NULL, get_revidr_el1 },
1389 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b000),
1390 NULL, get_id_pfr0_el1 },
1391 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b001),
1392 NULL, get_id_pfr1_el1 },
1393 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b010),
1394 NULL, get_id_dfr0_el1 },
1395 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b011),
1396 NULL, get_id_afr0_el1 },
1397 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b100),
1398 NULL, get_id_mmfr0_el1 },
1399 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b101),
1400 NULL, get_id_mmfr1_el1 },
1401 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b110),
1402 NULL, get_id_mmfr2_el1 },
1403 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b111),
1404 NULL, get_id_mmfr3_el1 },
1405 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
1406 NULL, get_id_isar0_el1 },
1407 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b001),
1408 NULL, get_id_isar1_el1 },
1409 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
1410 NULL, get_id_isar2_el1 },
1411 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b011),
1412 NULL, get_id_isar3_el1 },
1413 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b100),
1414 NULL, get_id_isar4_el1 },
1415 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b101),
1416 NULL, get_id_isar5_el1 },
1417 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b001),
1418 NULL, get_clidr_el1 },
1419 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b111),
1420 NULL, get_aidr_el1 },
1421 { Op0(0b11), Op1(0b011), CRn(0b0000), CRm(0b0000), Op2(0b001),
1422 NULL, get_ctr_el0 },
1423};
1424
Victor Kamensky26c99af2014-06-12 09:30:12 -07001425static int reg_from_user(u64 *val, const void __user *uaddr, u64 id)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001426{
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001427 if (copy_from_user(val, uaddr, KVM_REG_SIZE(id)) != 0)
1428 return -EFAULT;
1429 return 0;
1430}
1431
Victor Kamensky26c99af2014-06-12 09:30:12 -07001432static int reg_to_user(void __user *uaddr, const u64 *val, u64 id)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001433{
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001434 if (copy_to_user(uaddr, val, KVM_REG_SIZE(id)) != 0)
1435 return -EFAULT;
1436 return 0;
1437}
1438
1439static int get_invariant_sys_reg(u64 id, void __user *uaddr)
1440{
1441 struct sys_reg_params params;
1442 const struct sys_reg_desc *r;
1443
1444 if (!index_to_params(id, &params))
1445 return -ENOENT;
1446
1447 r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
1448 if (!r)
1449 return -ENOENT;
1450
1451 return reg_to_user(uaddr, &r->val, id);
1452}
1453
1454static int set_invariant_sys_reg(u64 id, void __user *uaddr)
1455{
1456 struct sys_reg_params params;
1457 const struct sys_reg_desc *r;
1458 int err;
1459 u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
1460
1461 if (!index_to_params(id, &params))
1462 return -ENOENT;
1463 r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
1464 if (!r)
1465 return -ENOENT;
1466
1467 err = reg_from_user(&val, uaddr, id);
1468 if (err)
1469 return err;
1470
1471 /* This is what we mean by invariant: you can't change it. */
1472 if (r->val != val)
1473 return -EINVAL;
1474
1475 return 0;
1476}
1477
1478static bool is_valid_cache(u32 val)
1479{
1480 u32 level, ctype;
1481
1482 if (val >= CSSELR_MAX)
Will Deacon18d45762014-08-26 15:13:22 +01001483 return false;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001484
1485 /* Bottom bit is Instruction or Data bit. Next 3 bits are level. */
1486 level = (val >> 1);
1487 ctype = (cache_levels >> (level * 3)) & 7;
1488
1489 switch (ctype) {
1490 case 0: /* No cache */
1491 return false;
1492 case 1: /* Instruction cache only */
1493 return (val & 1);
1494 case 2: /* Data cache only */
1495 case 4: /* Unified cache */
1496 return !(val & 1);
1497 case 3: /* Separate instruction and data caches */
1498 return true;
1499 default: /* Reserved: we can't know instruction or data. */
1500 return false;
1501 }
1502}
1503
1504static int demux_c15_get(u64 id, void __user *uaddr)
1505{
1506 u32 val;
1507 u32 __user *uval = uaddr;
1508
1509 /* Fail if we have unknown bits set. */
1510 if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
1511 | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
1512 return -ENOENT;
1513
1514 switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
1515 case KVM_REG_ARM_DEMUX_ID_CCSIDR:
1516 if (KVM_REG_SIZE(id) != 4)
1517 return -ENOENT;
1518 val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
1519 >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
1520 if (!is_valid_cache(val))
1521 return -ENOENT;
1522
1523 return put_user(get_ccsidr(val), uval);
1524 default:
1525 return -ENOENT;
1526 }
1527}
1528
1529static int demux_c15_set(u64 id, void __user *uaddr)
1530{
1531 u32 val, newval;
1532 u32 __user *uval = uaddr;
1533
1534 /* Fail if we have unknown bits set. */
1535 if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
1536 | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
1537 return -ENOENT;
1538
1539 switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
1540 case KVM_REG_ARM_DEMUX_ID_CCSIDR:
1541 if (KVM_REG_SIZE(id) != 4)
1542 return -ENOENT;
1543 val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
1544 >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
1545 if (!is_valid_cache(val))
1546 return -ENOENT;
1547
1548 if (get_user(newval, uval))
1549 return -EFAULT;
1550
1551 /* This is also invariant: you can't change it. */
1552 if (newval != get_ccsidr(val))
1553 return -EINVAL;
1554 return 0;
1555 default:
1556 return -ENOENT;
1557 }
1558}
1559
1560int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
1561{
1562 const struct sys_reg_desc *r;
1563 void __user *uaddr = (void __user *)(unsigned long)reg->addr;
1564
1565 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
1566 return demux_c15_get(reg->id, uaddr);
1567
1568 if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
1569 return -ENOENT;
1570
1571 r = index_to_sys_reg_desc(vcpu, reg->id);
1572 if (!r)
1573 return get_invariant_sys_reg(reg->id, uaddr);
1574
Alex Bennée84e690b2015-07-07 17:30:00 +01001575 if (r->get_user)
1576 return (r->get_user)(vcpu, r, reg, uaddr);
1577
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001578 return reg_to_user(uaddr, &vcpu_sys_reg(vcpu, r->reg), reg->id);
1579}
1580
1581int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
1582{
1583 const struct sys_reg_desc *r;
1584 void __user *uaddr = (void __user *)(unsigned long)reg->addr;
1585
1586 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
1587 return demux_c15_set(reg->id, uaddr);
1588
1589 if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
1590 return -ENOENT;
1591
1592 r = index_to_sys_reg_desc(vcpu, reg->id);
1593 if (!r)
1594 return set_invariant_sys_reg(reg->id, uaddr);
1595
Alex Bennée84e690b2015-07-07 17:30:00 +01001596 if (r->set_user)
1597 return (r->set_user)(vcpu, r, reg, uaddr);
1598
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001599 return reg_from_user(&vcpu_sys_reg(vcpu, r->reg), uaddr, reg->id);
1600}
1601
1602static unsigned int num_demux_regs(void)
1603{
1604 unsigned int i, count = 0;
1605
1606 for (i = 0; i < CSSELR_MAX; i++)
1607 if (is_valid_cache(i))
1608 count++;
1609
1610 return count;
1611}
1612
1613static int write_demux_regids(u64 __user *uindices)
1614{
Alex Bennéeefd48ce2014-07-01 16:53:13 +01001615 u64 val = KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001616 unsigned int i;
1617
1618 val |= KVM_REG_ARM_DEMUX_ID_CCSIDR;
1619 for (i = 0; i < CSSELR_MAX; i++) {
1620 if (!is_valid_cache(i))
1621 continue;
1622 if (put_user(val | i, uindices))
1623 return -EFAULT;
1624 uindices++;
1625 }
1626 return 0;
1627}
1628
1629static u64 sys_reg_to_index(const struct sys_reg_desc *reg)
1630{
1631 return (KVM_REG_ARM64 | KVM_REG_SIZE_U64 |
1632 KVM_REG_ARM64_SYSREG |
1633 (reg->Op0 << KVM_REG_ARM64_SYSREG_OP0_SHIFT) |
1634 (reg->Op1 << KVM_REG_ARM64_SYSREG_OP1_SHIFT) |
1635 (reg->CRn << KVM_REG_ARM64_SYSREG_CRN_SHIFT) |
1636 (reg->CRm << KVM_REG_ARM64_SYSREG_CRM_SHIFT) |
1637 (reg->Op2 << KVM_REG_ARM64_SYSREG_OP2_SHIFT));
1638}
1639
1640static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
1641{
1642 if (!*uind)
1643 return true;
1644
1645 if (put_user(sys_reg_to_index(reg), *uind))
1646 return false;
1647
1648 (*uind)++;
1649 return true;
1650}
1651
1652/* Assumed ordered tables, see kvm_sys_reg_table_init. */
1653static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
1654{
1655 const struct sys_reg_desc *i1, *i2, *end1, *end2;
1656 unsigned int total = 0;
1657 size_t num;
1658
1659 /* We check for duplicates here, to allow arch-specific overrides. */
Marc Zyngier62a89c42013-02-07 10:32:33 +00001660 i1 = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001661 end1 = i1 + num;
1662 i2 = sys_reg_descs;
1663 end2 = sys_reg_descs + ARRAY_SIZE(sys_reg_descs);
1664
1665 BUG_ON(i1 == end1 || i2 == end2);
1666
1667 /* Walk carefully, as both tables may refer to the same register. */
1668 while (i1 || i2) {
1669 int cmp = cmp_sys_reg(i1, i2);
1670 /* target-specific overrides generic entry. */
1671 if (cmp <= 0) {
1672 /* Ignore registers we trap but don't save. */
1673 if (i1->reg) {
1674 if (!copy_reg_to_user(i1, &uind))
1675 return -EFAULT;
1676 total++;
1677 }
1678 } else {
1679 /* Ignore registers we trap but don't save. */
1680 if (i2->reg) {
1681 if (!copy_reg_to_user(i2, &uind))
1682 return -EFAULT;
1683 total++;
1684 }
1685 }
1686
1687 if (cmp <= 0 && ++i1 == end1)
1688 i1 = NULL;
1689 if (cmp >= 0 && ++i2 == end2)
1690 i2 = NULL;
1691 }
1692 return total;
1693}
1694
1695unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
1696{
1697 return ARRAY_SIZE(invariant_sys_regs)
1698 + num_demux_regs()
1699 + walk_sys_regs(vcpu, (u64 __user *)NULL);
1700}
1701
1702int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
1703{
1704 unsigned int i;
1705 int err;
1706
1707 /* Then give them all the invariant registers' indices. */
1708 for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) {
1709 if (put_user(sys_reg_to_index(&invariant_sys_regs[i]), uindices))
1710 return -EFAULT;
1711 uindices++;
1712 }
1713
1714 err = walk_sys_regs(vcpu, uindices);
1715 if (err < 0)
1716 return err;
1717 uindices += err;
1718
1719 return write_demux_regids(uindices);
1720}
1721
Marc Zyngiere6a95512014-05-07 13:43:39 +01001722static int check_sysreg_table(const struct sys_reg_desc *table, unsigned int n)
1723{
1724 unsigned int i;
1725
1726 for (i = 1; i < n; i++) {
1727 if (cmp_sys_reg(&table[i-1], &table[i]) >= 0) {
1728 kvm_err("sys_reg table %p out of order (%d)\n", table, i - 1);
1729 return 1;
1730 }
1731 }
1732
1733 return 0;
1734}
1735
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001736void kvm_sys_reg_table_init(void)
1737{
1738 unsigned int i;
1739 struct sys_reg_desc clidr;
1740
1741 /* Make sure tables are unique and in order. */
Marc Zyngiere6a95512014-05-07 13:43:39 +01001742 BUG_ON(check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs)));
1743 BUG_ON(check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs)));
1744 BUG_ON(check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs)));
1745 BUG_ON(check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs)));
1746 BUG_ON(check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs)));
1747 BUG_ON(check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs)));
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001748
1749 /* We abuse the reset function to overwrite the table itself. */
1750 for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++)
1751 invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]);
1752
1753 /*
1754 * CLIDR format is awkward, so clean it up. See ARM B4.1.20:
1755 *
1756 * If software reads the Cache Type fields from Ctype1
1757 * upwards, once it has seen a value of 0b000, no caches
1758 * exist at further-out levels of the hierarchy. So, for
1759 * example, if Ctype3 is the first Cache Type field with a
1760 * value of 0b000, the values of Ctype4 to Ctype7 must be
1761 * ignored.
1762 */
1763 get_clidr_el1(NULL, &clidr); /* Ugly... */
1764 cache_levels = clidr.val;
1765 for (i = 0; i < 7; i++)
1766 if (((cache_levels >> (i*3)) & 7) == 0)
1767 break;
1768 /* Clear all higher bits. */
1769 cache_levels &= (1 << (i*3))-1;
1770}
1771
1772/**
1773 * kvm_reset_sys_regs - sets system registers to reset value
1774 * @vcpu: The VCPU pointer
1775 *
1776 * This function finds the right table above and sets the registers on the
1777 * virtual CPU struct to their architecturally defined reset values.
1778 */
1779void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
1780{
1781 size_t num;
1782 const struct sys_reg_desc *table;
1783
1784 /* Catch someone adding a register without putting in reset entry. */
1785 memset(&vcpu->arch.ctxt.sys_regs, 0x42, sizeof(vcpu->arch.ctxt.sys_regs));
1786
1787 /* Generic chip reset first (so target could override). */
1788 reset_sys_reg_descs(vcpu, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1789
Marc Zyngier62a89c42013-02-07 10:32:33 +00001790 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001791 reset_sys_reg_descs(vcpu, table, num);
1792
1793 for (num = 1; num < NR_SYS_REGS; num++)
1794 if (vcpu_sys_reg(vcpu, num) == 0x4242424242424242)
1795 panic("Didn't reset vcpu_sys_reg(%zi)", num);
1796}