blob: 136e6797676bca56dda967bebd4263dbbbc32574 [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
23#include <linux/mm.h>
24#include <linux/kvm_host.h>
25#include <linux/uaccess.h>
26#include <asm/kvm_arm.h>
27#include <asm/kvm_host.h>
28#include <asm/kvm_emulate.h>
29#include <asm/kvm_coproc.h>
Marc Zyngier9d218a12014-01-15 12:50:23 +000030#include <asm/kvm_mmu.h>
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000031#include <asm/cacheflush.h>
32#include <asm/cputype.h>
Marc Zyngier0c557ed2014-04-24 10:24:46 +010033#include <asm/debug-monitors.h>
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000034#include <trace/events/kvm.h>
35
36#include "sys_regs.h"
37
38/*
39 * All of this file is extremly similar to the ARM coproc.c, but the
40 * types are different. My gut feeling is that it should be pretty
41 * easy to merge, but that would be an ABI breakage -- again. VFP
42 * would also need to be abstracted.
Marc Zyngier62a89c42013-02-07 10:32:33 +000043 *
44 * For AArch32, we only take care of what is being trapped. Anything
45 * that has to do with init and userspace access has to go via the
46 * 64bit interface.
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000047 */
48
49/* 3 bits per cache level, as per CLIDR, but non-existent caches always 0 */
50static u32 cache_levels;
51
52/* CSSELR values; used to index KVM_REG_ARM_DEMUX_ID_CCSIDR */
53#define CSSELR_MAX 12
54
55/* Which cache CCSIDR represents depends on CSSELR value. */
56static u32 get_ccsidr(u32 csselr)
57{
58 u32 ccsidr;
59
60 /* Make sure noone else changes CSSELR during this! */
61 local_irq_disable();
62 /* Put value into CSSELR */
63 asm volatile("msr csselr_el1, %x0" : : "r" (csselr));
64 isb();
65 /* Read result out of CCSIDR */
66 asm volatile("mrs %0, ccsidr_el1" : "=r" (ccsidr));
67 local_irq_enable();
68
69 return ccsidr;
70}
71
72static void do_dc_cisw(u32 val)
73{
74 asm volatile("dc cisw, %x0" : : "r" (val));
Will Deacon98f76852014-05-02 16:24:10 +010075 dsb(ish);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000076}
77
78static void do_dc_csw(u32 val)
79{
80 asm volatile("dc csw, %x0" : : "r" (val));
Will Deacon98f76852014-05-02 16:24:10 +010081 dsb(ish);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +000082}
83
84/* See note at ARM ARM B1.14.4 */
85static bool access_dcsw(struct kvm_vcpu *vcpu,
86 const struct sys_reg_params *p,
87 const struct sys_reg_desc *r)
88{
89 unsigned long val;
90 int cpu;
91
92 if (!p->is_write)
93 return read_from_write_only(vcpu, p);
94
95 cpu = get_cpu();
96
97 cpumask_setall(&vcpu->arch.require_dcache_flush);
98 cpumask_clear_cpu(cpu, &vcpu->arch.require_dcache_flush);
99
100 /* If we were already preempted, take the long way around */
101 if (cpu != vcpu->arch.last_pcpu) {
102 flush_cache_all();
103 goto done;
104 }
105
106 val = *vcpu_reg(vcpu, p->Rt);
107
108 switch (p->CRm) {
109 case 6: /* Upgrade DCISW to DCCISW, as per HCR.SWIO */
110 case 14: /* DCCISW */
111 do_dc_cisw(val);
112 break;
113
114 case 10: /* DCCSW */
115 do_dc_csw(val);
116 break;
117 }
118
119done:
120 put_cpu();
121
122 return true;
123}
124
125/*
Marc Zyngier4d449232014-01-14 18:00:55 +0000126 * Generic accessor for VM registers. Only called as long as HCR_TVM
127 * is set.
128 */
129static bool access_vm_reg(struct kvm_vcpu *vcpu,
130 const struct sys_reg_params *p,
131 const struct sys_reg_desc *r)
132{
133 unsigned long val;
134
135 BUG_ON(!p->is_write);
136
137 val = *vcpu_reg(vcpu, p->Rt);
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100138 if (!p->is_aarch32) {
Marc Zyngier4d449232014-01-14 18:00:55 +0000139 vcpu_sys_reg(vcpu, r->reg) = val;
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100140 } else {
141 if (!p->is_32bit)
142 vcpu_cp15_64_high(vcpu, r->reg) = val >> 32;
Victor Kamenskyf0a3eaf2014-07-02 17:19:30 +0100143 vcpu_cp15_64_low(vcpu, r->reg) = val & 0xffffffffUL;
Marc Zyngierdedf97e2014-08-01 12:00:36 +0100144 }
Victor Kamenskyf0a3eaf2014-07-02 17:19:30 +0100145
Marc Zyngier4d449232014-01-14 18:00:55 +0000146 return true;
147}
148
149/*
150 * SCTLR_EL1 accessor. Only called as long as HCR_TVM is set. If the
151 * guest enables the MMU, we stop trapping the VM sys_regs and leave
152 * it in complete control of the caches.
153 */
154static bool access_sctlr(struct kvm_vcpu *vcpu,
155 const struct sys_reg_params *p,
156 const struct sys_reg_desc *r)
157{
158 access_vm_reg(vcpu, p, r);
159
Marc Zyngier9d218a12014-01-15 12:50:23 +0000160 if (vcpu_has_cache_enabled(vcpu)) { /* MMU+Caches enabled? */
Marc Zyngier4d449232014-01-14 18:00:55 +0000161 vcpu->arch.hcr_el2 &= ~HCR_TVM;
Marc Zyngier9d218a12014-01-15 12:50:23 +0000162 stage2_flush_vm(vcpu->kvm);
163 }
Marc Zyngier4d449232014-01-14 18:00:55 +0000164
165 return true;
166}
167
Marc Zyngier7609c122014-04-24 10:21:16 +0100168static bool trap_raz_wi(struct kvm_vcpu *vcpu,
169 const struct sys_reg_params *p,
170 const struct sys_reg_desc *r)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000171{
172 if (p->is_write)
173 return ignore_write(vcpu, p);
174 else
175 return read_zero(vcpu, p);
176}
177
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100178static bool trap_oslsr_el1(struct kvm_vcpu *vcpu,
179 const struct sys_reg_params *p,
180 const struct sys_reg_desc *r)
181{
182 if (p->is_write) {
183 return ignore_write(vcpu, p);
184 } else {
185 *vcpu_reg(vcpu, p->Rt) = (1 << 3);
186 return true;
187 }
188}
189
190static bool trap_dbgauthstatus_el1(struct kvm_vcpu *vcpu,
191 const struct sys_reg_params *p,
192 const struct sys_reg_desc *r)
193{
194 if (p->is_write) {
195 return ignore_write(vcpu, p);
196 } else {
197 u32 val;
198 asm volatile("mrs %0, dbgauthstatus_el1" : "=r" (val));
199 *vcpu_reg(vcpu, p->Rt) = val;
200 return true;
201 }
202}
203
204/*
205 * We want to avoid world-switching all the DBG registers all the
206 * time:
207 *
208 * - If we've touched any debug register, it is likely that we're
209 * going to touch more of them. It then makes sense to disable the
210 * traps and start doing the save/restore dance
211 * - If debug is active (DBG_MDSCR_KDE or DBG_MDSCR_MDE set), it is
212 * then mandatory to save/restore the registers, as the guest
213 * depends on them.
214 *
215 * For this, we use a DIRTY bit, indicating the guest has modified the
216 * debug registers, used as follow:
217 *
218 * On guest entry:
219 * - If the dirty bit is set (because we're coming back from trapping),
220 * disable the traps, save host registers, restore guest registers.
221 * - If debug is actively in use (DBG_MDSCR_KDE or DBG_MDSCR_MDE set),
222 * set the dirty bit, disable the traps, save host registers,
223 * restore guest registers.
224 * - Otherwise, enable the traps
225 *
226 * On guest exit:
227 * - If the dirty bit is set, save guest registers, restore host
228 * registers and clear the dirty bit. This ensure that the host can
229 * now use the debug registers.
230 */
231static bool trap_debug_regs(struct kvm_vcpu *vcpu,
232 const struct sys_reg_params *p,
233 const struct sys_reg_desc *r)
234{
235 if (p->is_write) {
236 vcpu_sys_reg(vcpu, r->reg) = *vcpu_reg(vcpu, p->Rt);
237 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
238 } else {
239 *vcpu_reg(vcpu, p->Rt) = vcpu_sys_reg(vcpu, r->reg);
240 }
241
242 return true;
243}
244
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000245static void reset_amair_el1(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
246{
247 u64 amair;
248
249 asm volatile("mrs %0, amair_el1\n" : "=r" (amair));
250 vcpu_sys_reg(vcpu, AMAIR_EL1) = amair;
251}
252
253static void reset_mpidr(struct kvm_vcpu *vcpu, const struct sys_reg_desc *r)
254{
Andre Przywara4429fc62014-06-02 15:37:13 +0200255 u64 mpidr;
256
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000257 /*
Andre Przywara4429fc62014-06-02 15:37:13 +0200258 * Map the vcpu_id into the first three affinity level fields of
259 * the MPIDR. We limit the number of VCPUs in level 0 due to a
260 * limitation to 16 CPUs in that level in the ICC_SGIxR registers
261 * of the GICv3 to be able to address each CPU directly when
262 * sending IPIs.
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000263 */
Andre Przywara4429fc62014-06-02 15:37:13 +0200264 mpidr = (vcpu->vcpu_id & 0x0f) << MPIDR_LEVEL_SHIFT(0);
265 mpidr |= ((vcpu->vcpu_id >> 4) & 0xff) << MPIDR_LEVEL_SHIFT(1);
266 mpidr |= ((vcpu->vcpu_id >> 12) & 0xff) << MPIDR_LEVEL_SHIFT(2);
267 vcpu_sys_reg(vcpu, MPIDR_EL1) = (1ULL << 31) | mpidr;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000268}
269
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100270/* Silly macro to expand the DBG{BCR,BVR,WVR,WCR}n_EL1 registers in one go */
271#define DBG_BCR_BVR_WCR_WVR_EL1(n) \
272 /* DBGBVRn_EL1 */ \
273 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b100), \
274 trap_debug_regs, reset_val, (DBGBVR0_EL1 + (n)), 0 }, \
275 /* DBGBCRn_EL1 */ \
276 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b101), \
277 trap_debug_regs, reset_val, (DBGBCR0_EL1 + (n)), 0 }, \
278 /* DBGWVRn_EL1 */ \
279 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b110), \
280 trap_debug_regs, reset_val, (DBGWVR0_EL1 + (n)), 0 }, \
281 /* DBGWCRn_EL1 */ \
282 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm((n)), Op2(0b111), \
283 trap_debug_regs, reset_val, (DBGWCR0_EL1 + (n)), 0 }
284
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000285/*
286 * Architected system registers.
287 * Important: Must be sorted ascending by Op0, Op1, CRn, CRm, Op2
Marc Zyngier7609c122014-04-24 10:21:16 +0100288 *
289 * We could trap ID_DFR0 and tell the guest we don't support performance
290 * monitoring. Unfortunately the patch to make the kernel check ID_DFR0 was
291 * NAKed, so it will read the PMCR anyway.
292 *
293 * Therefore we tell the guest we have 0 counters. Unfortunately, we
294 * must always support PMCCNTR (the cycle counter): we just RAZ/WI for
295 * all PM registers, which doesn't crash the guest kernel at least.
296 *
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100297 * Debug handling: We do trap most, if not all debug related system
298 * registers. The implementation is good enough to ensure that a guest
299 * can use these with minimal performance degradation. The drawback is
300 * that we don't implement any of the external debug, none of the
301 * OSlock protocol. This should be revisited if we ever encounter a
302 * more demanding guest...
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000303 */
304static const struct sys_reg_desc sys_reg_descs[] = {
305 /* DC ISW */
306 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b0110), Op2(0b010),
307 access_dcsw },
308 /* DC CSW */
309 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1010), Op2(0b010),
310 access_dcsw },
311 /* DC CISW */
312 { Op0(0b01), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b010),
313 access_dcsw },
314
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100315 DBG_BCR_BVR_WCR_WVR_EL1(0),
316 DBG_BCR_BVR_WCR_WVR_EL1(1),
317 /* MDCCINT_EL1 */
318 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
319 trap_debug_regs, reset_val, MDCCINT_EL1, 0 },
320 /* MDSCR_EL1 */
321 { Op0(0b10), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
322 trap_debug_regs, reset_val, MDSCR_EL1, 0 },
323 DBG_BCR_BVR_WCR_WVR_EL1(2),
324 DBG_BCR_BVR_WCR_WVR_EL1(3),
325 DBG_BCR_BVR_WCR_WVR_EL1(4),
326 DBG_BCR_BVR_WCR_WVR_EL1(5),
327 DBG_BCR_BVR_WCR_WVR_EL1(6),
328 DBG_BCR_BVR_WCR_WVR_EL1(7),
329 DBG_BCR_BVR_WCR_WVR_EL1(8),
330 DBG_BCR_BVR_WCR_WVR_EL1(9),
331 DBG_BCR_BVR_WCR_WVR_EL1(10),
332 DBG_BCR_BVR_WCR_WVR_EL1(11),
333 DBG_BCR_BVR_WCR_WVR_EL1(12),
334 DBG_BCR_BVR_WCR_WVR_EL1(13),
335 DBG_BCR_BVR_WCR_WVR_EL1(14),
336 DBG_BCR_BVR_WCR_WVR_EL1(15),
337
338 /* MDRAR_EL1 */
339 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
340 trap_raz_wi },
341 /* OSLAR_EL1 */
342 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b100),
343 trap_raz_wi },
344 /* OSLSR_EL1 */
345 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0001), Op2(0b100),
346 trap_oslsr_el1 },
347 /* OSDLR_EL1 */
348 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0011), Op2(0b100),
349 trap_raz_wi },
350 /* DBGPRCR_EL1 */
351 { Op0(0b10), Op1(0b000), CRn(0b0001), CRm(0b0100), Op2(0b100),
352 trap_raz_wi },
353 /* DBGCLAIMSET_EL1 */
354 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1000), Op2(0b110),
355 trap_raz_wi },
356 /* DBGCLAIMCLR_EL1 */
357 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1001), Op2(0b110),
358 trap_raz_wi },
359 /* DBGAUTHSTATUS_EL1 */
360 { Op0(0b10), Op1(0b000), CRn(0b0111), CRm(0b1110), Op2(0b110),
361 trap_dbgauthstatus_el1 },
362
Marc Zyngier62a89c42013-02-07 10:32:33 +0000363 /* TEECR32_EL1 */
364 { Op0(0b10), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
365 NULL, reset_val, TEECR32_EL1, 0 },
366 /* TEEHBR32_EL1 */
367 { Op0(0b10), Op1(0b010), CRn(0b0001), CRm(0b0000), Op2(0b000),
368 NULL, reset_val, TEEHBR32_EL1, 0 },
Marc Zyngier0c557ed2014-04-24 10:24:46 +0100369
370 /* MDCCSR_EL1 */
371 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0001), Op2(0b000),
372 trap_raz_wi },
373 /* DBGDTR_EL0 */
374 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0100), Op2(0b000),
375 trap_raz_wi },
376 /* DBGDTR[TR]X_EL0 */
377 { Op0(0b10), Op1(0b011), CRn(0b0000), CRm(0b0101), Op2(0b000),
378 trap_raz_wi },
379
Marc Zyngier62a89c42013-02-07 10:32:33 +0000380 /* DBGVCR32_EL2 */
381 { Op0(0b10), Op1(0b100), CRn(0b0000), CRm(0b0111), Op2(0b000),
382 NULL, reset_val, DBGVCR32_EL2, 0 },
383
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000384 /* MPIDR_EL1 */
385 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b101),
386 NULL, reset_mpidr, MPIDR_EL1 },
387 /* SCTLR_EL1 */
388 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000389 access_sctlr, reset_val, SCTLR_EL1, 0x00C50078 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000390 /* CPACR_EL1 */
391 { Op0(0b11), Op1(0b000), CRn(0b0001), CRm(0b0000), Op2(0b010),
392 NULL, reset_val, CPACR_EL1, 0 },
393 /* TTBR0_EL1 */
394 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000395 access_vm_reg, reset_unknown, TTBR0_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000396 /* TTBR1_EL1 */
397 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000398 access_vm_reg, reset_unknown, TTBR1_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000399 /* TCR_EL1 */
400 { Op0(0b11), Op1(0b000), CRn(0b0010), CRm(0b0000), Op2(0b010),
Marc Zyngier4d449232014-01-14 18:00:55 +0000401 access_vm_reg, reset_val, TCR_EL1, 0 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000402
403 /* AFSR0_EL1 */
404 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000405 access_vm_reg, reset_unknown, AFSR0_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000406 /* AFSR1_EL1 */
407 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0001), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000408 access_vm_reg, reset_unknown, AFSR1_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000409 /* ESR_EL1 */
410 { Op0(0b11), Op1(0b000), CRn(0b0101), CRm(0b0010), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000411 access_vm_reg, reset_unknown, ESR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000412 /* FAR_EL1 */
413 { Op0(0b11), Op1(0b000), CRn(0b0110), CRm(0b0000), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000414 access_vm_reg, reset_unknown, FAR_EL1 },
Marc Zyngier1bbd8052013-06-07 11:02:34 +0100415 /* PAR_EL1 */
416 { Op0(0b11), Op1(0b000), CRn(0b0111), CRm(0b0100), Op2(0b000),
417 NULL, reset_unknown, PAR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000418
419 /* PMINTENSET_EL1 */
420 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100421 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000422 /* PMINTENCLR_EL1 */
423 { Op0(0b11), Op1(0b000), CRn(0b1001), CRm(0b1110), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100424 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000425
426 /* MAIR_EL1 */
427 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0010), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000428 access_vm_reg, reset_unknown, MAIR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000429 /* AMAIR_EL1 */
430 { Op0(0b11), Op1(0b000), CRn(0b1010), CRm(0b0011), Op2(0b000),
Marc Zyngier4d449232014-01-14 18:00:55 +0000431 access_vm_reg, reset_amair_el1, AMAIR_EL1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000432
433 /* VBAR_EL1 */
434 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b0000), Op2(0b000),
435 NULL, reset_val, VBAR_EL1, 0 },
Christoffer Dalldb7dedd2014-11-19 11:23:54 +0000436
437 /* ICC_SRE_EL1 */
438 { Op0(0b11), Op1(0b000), CRn(0b1100), CRm(0b1100), Op2(0b101),
439 trap_raz_wi },
440
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000441 /* CONTEXTIDR_EL1 */
442 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b001),
Marc Zyngier4d449232014-01-14 18:00:55 +0000443 access_vm_reg, reset_val, CONTEXTIDR_EL1, 0 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000444 /* TPIDR_EL1 */
445 { Op0(0b11), Op1(0b000), CRn(0b1101), CRm(0b0000), Op2(0b100),
446 NULL, reset_unknown, TPIDR_EL1 },
447
448 /* CNTKCTL_EL1 */
449 { Op0(0b11), Op1(0b000), CRn(0b1110), CRm(0b0001), Op2(0b000),
450 NULL, reset_val, CNTKCTL_EL1, 0},
451
452 /* CSSELR_EL1 */
453 { Op0(0b11), Op1(0b010), CRn(0b0000), CRm(0b0000), Op2(0b000),
454 NULL, reset_unknown, CSSELR_EL1 },
455
456 /* PMCR_EL0 */
457 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b000),
Marc Zyngier7609c122014-04-24 10:21:16 +0100458 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000459 /* PMCNTENSET_EL0 */
460 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100461 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000462 /* PMCNTENCLR_EL0 */
463 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100464 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000465 /* PMOVSCLR_EL0 */
466 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b011),
Marc Zyngier7609c122014-04-24 10:21:16 +0100467 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000468 /* PMSWINC_EL0 */
469 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b100),
Marc Zyngier7609c122014-04-24 10:21:16 +0100470 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000471 /* PMSELR_EL0 */
472 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b101),
Marc Zyngier7609c122014-04-24 10:21:16 +0100473 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000474 /* PMCEID0_EL0 */
475 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b110),
Marc Zyngier7609c122014-04-24 10:21:16 +0100476 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000477 /* PMCEID1_EL0 */
478 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1100), Op2(0b111),
Marc Zyngier7609c122014-04-24 10:21:16 +0100479 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000480 /* PMCCNTR_EL0 */
481 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b000),
Marc Zyngier7609c122014-04-24 10:21:16 +0100482 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000483 /* PMXEVTYPER_EL0 */
484 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b001),
Marc Zyngier7609c122014-04-24 10:21:16 +0100485 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000486 /* PMXEVCNTR_EL0 */
487 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1101), Op2(0b010),
Marc Zyngier7609c122014-04-24 10:21:16 +0100488 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000489 /* PMUSERENR_EL0 */
490 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b000),
Marc Zyngier7609c122014-04-24 10:21:16 +0100491 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000492 /* PMOVSSET_EL0 */
493 { Op0(0b11), Op1(0b011), CRn(0b1001), CRm(0b1110), Op2(0b011),
Marc Zyngier7609c122014-04-24 10:21:16 +0100494 trap_raz_wi },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000495
496 /* TPIDR_EL0 */
497 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b010),
498 NULL, reset_unknown, TPIDR_EL0 },
499 /* TPIDRRO_EL0 */
500 { Op0(0b11), Op1(0b011), CRn(0b1101), CRm(0b0000), Op2(0b011),
501 NULL, reset_unknown, TPIDRRO_EL0 },
Marc Zyngier62a89c42013-02-07 10:32:33 +0000502
503 /* DACR32_EL2 */
504 { Op0(0b11), Op1(0b100), CRn(0b0011), CRm(0b0000), Op2(0b000),
505 NULL, reset_unknown, DACR32_EL2 },
506 /* IFSR32_EL2 */
507 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0000), Op2(0b001),
508 NULL, reset_unknown, IFSR32_EL2 },
509 /* FPEXC32_EL2 */
510 { Op0(0b11), Op1(0b100), CRn(0b0101), CRm(0b0011), Op2(0b000),
511 NULL, reset_val, FPEXC32_EL2, 0x70 },
512};
513
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100514static bool trap_dbgidr(struct kvm_vcpu *vcpu,
515 const struct sys_reg_params *p,
516 const struct sys_reg_desc *r)
517{
518 if (p->is_write) {
519 return ignore_write(vcpu, p);
520 } else {
521 u64 dfr = read_cpuid(ID_AA64DFR0_EL1);
522 u64 pfr = read_cpuid(ID_AA64PFR0_EL1);
523 u32 el3 = !!((pfr >> 12) & 0xf);
524
525 *vcpu_reg(vcpu, p->Rt) = ((((dfr >> 20) & 0xf) << 28) |
526 (((dfr >> 12) & 0xf) << 24) |
527 (((dfr >> 28) & 0xf) << 20) |
528 (6 << 16) | (el3 << 14) | (el3 << 12));
529 return true;
530 }
531}
532
533static bool trap_debug32(struct kvm_vcpu *vcpu,
534 const struct sys_reg_params *p,
535 const struct sys_reg_desc *r)
536{
537 if (p->is_write) {
538 vcpu_cp14(vcpu, r->reg) = *vcpu_reg(vcpu, p->Rt);
539 vcpu->arch.debug_flags |= KVM_ARM64_DEBUG_DIRTY;
540 } else {
541 *vcpu_reg(vcpu, p->Rt) = vcpu_cp14(vcpu, r->reg);
542 }
543
544 return true;
545}
546
547#define DBG_BCR_BVR_WCR_WVR(n) \
548 /* DBGBVRn */ \
549 { Op1( 0), CRn( 0), CRm((n)), Op2( 4), trap_debug32, \
550 NULL, (cp14_DBGBVR0 + (n) * 2) }, \
551 /* DBGBCRn */ \
552 { Op1( 0), CRn( 0), CRm((n)), Op2( 5), trap_debug32, \
553 NULL, (cp14_DBGBCR0 + (n) * 2) }, \
554 /* DBGWVRn */ \
555 { Op1( 0), CRn( 0), CRm((n)), Op2( 6), trap_debug32, \
556 NULL, (cp14_DBGWVR0 + (n) * 2) }, \
557 /* DBGWCRn */ \
558 { Op1( 0), CRn( 0), CRm((n)), Op2( 7), trap_debug32, \
559 NULL, (cp14_DBGWCR0 + (n) * 2) }
560
561#define DBGBXVR(n) \
562 { Op1( 0), CRn( 1), CRm((n)), Op2( 1), trap_debug32, \
563 NULL, cp14_DBGBXVR0 + n * 2 }
564
565/*
566 * Trapped cp14 registers. We generally ignore most of the external
567 * debug, on the principle that they don't really make sense to a
568 * guest. Revisit this one day, whould this principle change.
569 */
Marc Zyngier72564012014-04-24 10:27:13 +0100570static const struct sys_reg_desc cp14_regs[] = {
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100571 /* DBGIDR */
572 { Op1( 0), CRn( 0), CRm( 0), Op2( 0), trap_dbgidr },
573 /* DBGDTRRXext */
574 { Op1( 0), CRn( 0), CRm( 0), Op2( 2), trap_raz_wi },
575
576 DBG_BCR_BVR_WCR_WVR(0),
577 /* DBGDSCRint */
578 { Op1( 0), CRn( 0), CRm( 1), Op2( 0), trap_raz_wi },
579 DBG_BCR_BVR_WCR_WVR(1),
580 /* DBGDCCINT */
581 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), trap_debug32 },
582 /* DBGDSCRext */
583 { Op1( 0), CRn( 0), CRm( 2), Op2( 2), trap_debug32 },
584 DBG_BCR_BVR_WCR_WVR(2),
585 /* DBGDTR[RT]Xint */
586 { Op1( 0), CRn( 0), CRm( 3), Op2( 0), trap_raz_wi },
587 /* DBGDTR[RT]Xext */
588 { Op1( 0), CRn( 0), CRm( 3), Op2( 2), trap_raz_wi },
589 DBG_BCR_BVR_WCR_WVR(3),
590 DBG_BCR_BVR_WCR_WVR(4),
591 DBG_BCR_BVR_WCR_WVR(5),
592 /* DBGWFAR */
593 { Op1( 0), CRn( 0), CRm( 6), Op2( 0), trap_raz_wi },
594 /* DBGOSECCR */
595 { Op1( 0), CRn( 0), CRm( 6), Op2( 2), trap_raz_wi },
596 DBG_BCR_BVR_WCR_WVR(6),
597 /* DBGVCR */
598 { Op1( 0), CRn( 0), CRm( 7), Op2( 0), trap_debug32 },
599 DBG_BCR_BVR_WCR_WVR(7),
600 DBG_BCR_BVR_WCR_WVR(8),
601 DBG_BCR_BVR_WCR_WVR(9),
602 DBG_BCR_BVR_WCR_WVR(10),
603 DBG_BCR_BVR_WCR_WVR(11),
604 DBG_BCR_BVR_WCR_WVR(12),
605 DBG_BCR_BVR_WCR_WVR(13),
606 DBG_BCR_BVR_WCR_WVR(14),
607 DBG_BCR_BVR_WCR_WVR(15),
608
609 /* DBGDRAR (32bit) */
610 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), trap_raz_wi },
611
612 DBGBXVR(0),
613 /* DBGOSLAR */
614 { Op1( 0), CRn( 1), CRm( 0), Op2( 4), trap_raz_wi },
615 DBGBXVR(1),
616 /* DBGOSLSR */
617 { Op1( 0), CRn( 1), CRm( 1), Op2( 4), trap_oslsr_el1 },
618 DBGBXVR(2),
619 DBGBXVR(3),
620 /* DBGOSDLR */
621 { Op1( 0), CRn( 1), CRm( 3), Op2( 4), trap_raz_wi },
622 DBGBXVR(4),
623 /* DBGPRCR */
624 { Op1( 0), CRn( 1), CRm( 4), Op2( 4), trap_raz_wi },
625 DBGBXVR(5),
626 DBGBXVR(6),
627 DBGBXVR(7),
628 DBGBXVR(8),
629 DBGBXVR(9),
630 DBGBXVR(10),
631 DBGBXVR(11),
632 DBGBXVR(12),
633 DBGBXVR(13),
634 DBGBXVR(14),
635 DBGBXVR(15),
636
637 /* DBGDSAR (32bit) */
638 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), trap_raz_wi },
639
640 /* DBGDEVID2 */
641 { Op1( 0), CRn( 7), CRm( 0), Op2( 7), trap_raz_wi },
642 /* DBGDEVID1 */
643 { Op1( 0), CRn( 7), CRm( 1), Op2( 7), trap_raz_wi },
644 /* DBGDEVID */
645 { Op1( 0), CRn( 7), CRm( 2), Op2( 7), trap_raz_wi },
646 /* DBGCLAIMSET */
647 { Op1( 0), CRn( 7), CRm( 8), Op2( 6), trap_raz_wi },
648 /* DBGCLAIMCLR */
649 { Op1( 0), CRn( 7), CRm( 9), Op2( 6), trap_raz_wi },
650 /* DBGAUTHSTATUS */
651 { Op1( 0), CRn( 7), CRm(14), Op2( 6), trap_dbgauthstatus_el1 },
Marc Zyngier72564012014-04-24 10:27:13 +0100652};
653
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100654/* Trapped cp14 64bit registers */
655static const struct sys_reg_desc cp14_64_regs[] = {
Marc Zyngierbdfb4b32014-04-24 10:31:37 +0100656 /* DBGDRAR (64bit) */
657 { Op1( 0), CRm( 1), .access = trap_raz_wi },
658
659 /* DBGDSAR (64bit) */
660 { Op1( 0), CRm( 2), .access = trap_raz_wi },
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100661};
662
Marc Zyngier4d449232014-01-14 18:00:55 +0000663/*
664 * Trapped cp15 registers. TTBR0/TTBR1 get a double encoding,
665 * depending on the way they are accessed (as a 32bit or a 64bit
666 * register).
667 */
Marc Zyngier62a89c42013-02-07 10:32:33 +0000668static const struct sys_reg_desc cp15_regs[] = {
Marc Zyngier4d449232014-01-14 18:00:55 +0000669 { Op1( 0), CRn( 1), CRm( 0), Op2( 0), access_sctlr, NULL, c1_SCTLR },
670 { Op1( 0), CRn( 2), CRm( 0), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
671 { Op1( 0), CRn( 2), CRm( 0), Op2( 1), access_vm_reg, NULL, c2_TTBR1 },
672 { Op1( 0), CRn( 2), CRm( 0), Op2( 2), access_vm_reg, NULL, c2_TTBCR },
673 { Op1( 0), CRn( 3), CRm( 0), Op2( 0), access_vm_reg, NULL, c3_DACR },
674 { Op1( 0), CRn( 5), CRm( 0), Op2( 0), access_vm_reg, NULL, c5_DFSR },
675 { Op1( 0), CRn( 5), CRm( 0), Op2( 1), access_vm_reg, NULL, c5_IFSR },
676 { Op1( 0), CRn( 5), CRm( 1), Op2( 0), access_vm_reg, NULL, c5_ADFSR },
677 { Op1( 0), CRn( 5), CRm( 1), Op2( 1), access_vm_reg, NULL, c5_AIFSR },
678 { Op1( 0), CRn( 6), CRm( 0), Op2( 0), access_vm_reg, NULL, c6_DFAR },
679 { Op1( 0), CRn( 6), CRm( 0), Op2( 2), access_vm_reg, NULL, c6_IFAR },
680
Marc Zyngier62a89c42013-02-07 10:32:33 +0000681 /*
682 * DC{C,I,CI}SW operations:
683 */
684 { Op1( 0), CRn( 7), CRm( 6), Op2( 2), access_dcsw },
685 { Op1( 0), CRn( 7), CRm(10), Op2( 2), access_dcsw },
686 { Op1( 0), CRn( 7), CRm(14), Op2( 2), access_dcsw },
Marc Zyngier4d449232014-01-14 18:00:55 +0000687
Marc Zyngier7609c122014-04-24 10:21:16 +0100688 /* PMU */
689 { Op1( 0), CRn( 9), CRm(12), Op2( 0), trap_raz_wi },
690 { Op1( 0), CRn( 9), CRm(12), Op2( 1), trap_raz_wi },
691 { Op1( 0), CRn( 9), CRm(12), Op2( 2), trap_raz_wi },
692 { Op1( 0), CRn( 9), CRm(12), Op2( 3), trap_raz_wi },
693 { Op1( 0), CRn( 9), CRm(12), Op2( 5), trap_raz_wi },
694 { Op1( 0), CRn( 9), CRm(12), Op2( 6), trap_raz_wi },
695 { Op1( 0), CRn( 9), CRm(12), Op2( 7), trap_raz_wi },
696 { Op1( 0), CRn( 9), CRm(13), Op2( 0), trap_raz_wi },
697 { Op1( 0), CRn( 9), CRm(13), Op2( 1), trap_raz_wi },
698 { Op1( 0), CRn( 9), CRm(13), Op2( 2), trap_raz_wi },
699 { Op1( 0), CRn( 9), CRm(14), Op2( 0), trap_raz_wi },
700 { Op1( 0), CRn( 9), CRm(14), Op2( 1), trap_raz_wi },
701 { Op1( 0), CRn( 9), CRm(14), Op2( 2), trap_raz_wi },
Marc Zyngier4d449232014-01-14 18:00:55 +0000702
703 { Op1( 0), CRn(10), CRm( 2), Op2( 0), access_vm_reg, NULL, c10_PRRR },
704 { Op1( 0), CRn(10), CRm( 2), Op2( 1), access_vm_reg, NULL, c10_NMRR },
705 { Op1( 0), CRn(10), CRm( 3), Op2( 0), access_vm_reg, NULL, c10_AMAIR0 },
706 { Op1( 0), CRn(10), CRm( 3), Op2( 1), access_vm_reg, NULL, c10_AMAIR1 },
Christoffer Dalldb7dedd2014-11-19 11:23:54 +0000707
708 /* ICC_SRE */
709 { Op1( 0), CRn(12), CRm(12), Op2( 5), trap_raz_wi },
710
Marc Zyngier4d449232014-01-14 18:00:55 +0000711 { Op1( 0), CRn(13), CRm( 0), Op2( 1), access_vm_reg, NULL, c13_CID },
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100712};
713
714static const struct sys_reg_desc cp15_64_regs[] = {
715 { Op1( 0), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR0 },
Marc Zyngier4d449232014-01-14 18:00:55 +0000716 { Op1( 1), CRn( 0), CRm( 2), Op2( 0), access_vm_reg, NULL, c2_TTBR1 },
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000717};
718
719/* Target specific emulation tables */
720static struct kvm_sys_reg_target_table *target_tables[KVM_ARM_NUM_TARGETS];
721
722void kvm_register_target_sys_reg_table(unsigned int target,
723 struct kvm_sys_reg_target_table *table)
724{
725 target_tables[target] = table;
726}
727
728/* Get specific register table for this target. */
Marc Zyngier62a89c42013-02-07 10:32:33 +0000729static const struct sys_reg_desc *get_target_table(unsigned target,
730 bool mode_is_64,
731 size_t *num)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000732{
733 struct kvm_sys_reg_target_table *table;
734
735 table = target_tables[target];
Marc Zyngier62a89c42013-02-07 10:32:33 +0000736 if (mode_is_64) {
737 *num = table->table64.num;
738 return table->table64.table;
739 } else {
740 *num = table->table32.num;
741 return table->table32.table;
742 }
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000743}
744
745static const struct sys_reg_desc *find_reg(const struct sys_reg_params *params,
746 const struct sys_reg_desc table[],
747 unsigned int num)
748{
749 unsigned int i;
750
751 for (i = 0; i < num; i++) {
752 const struct sys_reg_desc *r = &table[i];
753
754 if (params->Op0 != r->Op0)
755 continue;
756 if (params->Op1 != r->Op1)
757 continue;
758 if (params->CRn != r->CRn)
759 continue;
760 if (params->CRm != r->CRm)
761 continue;
762 if (params->Op2 != r->Op2)
763 continue;
764
765 return r;
766 }
767 return NULL;
768}
769
Marc Zyngier62a89c42013-02-07 10:32:33 +0000770int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu, struct kvm_run *run)
771{
772 kvm_inject_undefined(vcpu);
773 return 1;
774}
775
Marc Zyngier72564012014-04-24 10:27:13 +0100776/*
777 * emulate_cp -- tries to match a sys_reg access in a handling table, and
778 * call the corresponding trap handler.
779 *
780 * @params: pointer to the descriptor of the access
781 * @table: array of trap descriptors
782 * @num: size of the trap descriptor array
783 *
784 * Return 0 if the access has been handled, and -1 if not.
785 */
786static int emulate_cp(struct kvm_vcpu *vcpu,
787 const struct sys_reg_params *params,
788 const struct sys_reg_desc *table,
789 size_t num)
Marc Zyngier62a89c42013-02-07 10:32:33 +0000790{
Marc Zyngier72564012014-04-24 10:27:13 +0100791 const struct sys_reg_desc *r;
Marc Zyngier62a89c42013-02-07 10:32:33 +0000792
Marc Zyngier72564012014-04-24 10:27:13 +0100793 if (!table)
794 return -1; /* Not handled */
Marc Zyngier62a89c42013-02-07 10:32:33 +0000795
Marc Zyngier62a89c42013-02-07 10:32:33 +0000796 r = find_reg(params, table, num);
Marc Zyngier62a89c42013-02-07 10:32:33 +0000797
Marc Zyngier72564012014-04-24 10:27:13 +0100798 if (r) {
Marc Zyngier62a89c42013-02-07 10:32:33 +0000799 /*
800 * Not having an accessor means that we have
801 * configured a trap that we don't know how to
802 * handle. This certainly qualifies as a gross bug
803 * that should be fixed right away.
804 */
805 BUG_ON(!r->access);
806
807 if (likely(r->access(vcpu, params, r))) {
808 /* Skip instruction, since it was emulated */
809 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
Marc Zyngier62a89c42013-02-07 10:32:33 +0000810 }
Marc Zyngier72564012014-04-24 10:27:13 +0100811
812 /* Handled */
813 return 0;
Marc Zyngier62a89c42013-02-07 10:32:33 +0000814 }
815
Marc Zyngier72564012014-04-24 10:27:13 +0100816 /* Not handled */
817 return -1;
818}
819
820static void unhandled_cp_access(struct kvm_vcpu *vcpu,
821 struct sys_reg_params *params)
822{
823 u8 hsr_ec = kvm_vcpu_trap_get_class(vcpu);
824 int cp;
825
826 switch(hsr_ec) {
827 case ESR_EL2_EC_CP15_32:
828 case ESR_EL2_EC_CP15_64:
829 cp = 15;
830 break;
831 case ESR_EL2_EC_CP14_MR:
832 case ESR_EL2_EC_CP14_64:
833 cp = 14;
834 break;
835 default:
836 WARN_ON((cp = -1));
837 }
838
839 kvm_err("Unsupported guest CP%d access at: %08lx\n",
840 cp, *vcpu_pc(vcpu));
Marc Zyngier62a89c42013-02-07 10:32:33 +0000841 print_sys_reg_instr(params);
842 kvm_inject_undefined(vcpu);
843}
844
845/**
Marc Zyngier72564012014-04-24 10:27:13 +0100846 * kvm_handle_cp_64 -- handles a mrrc/mcrr trap on a guest CP15 access
Marc Zyngier62a89c42013-02-07 10:32:33 +0000847 * @vcpu: The VCPU pointer
848 * @run: The kvm_run struct
849 */
Marc Zyngier72564012014-04-24 10:27:13 +0100850static int kvm_handle_cp_64(struct kvm_vcpu *vcpu,
851 const struct sys_reg_desc *global,
852 size_t nr_global,
853 const struct sys_reg_desc *target_specific,
854 size_t nr_specific)
Marc Zyngier62a89c42013-02-07 10:32:33 +0000855{
856 struct sys_reg_params params;
857 u32 hsr = kvm_vcpu_get_hsr(vcpu);
858 int Rt2 = (hsr >> 10) & 0xf;
859
Marc Zyngier2072d292014-01-21 10:55:17 +0000860 params.is_aarch32 = true;
861 params.is_32bit = false;
Marc Zyngier62a89c42013-02-07 10:32:33 +0000862 params.CRm = (hsr >> 1) & 0xf;
863 params.Rt = (hsr >> 5) & 0xf;
864 params.is_write = ((hsr & 1) == 0);
865
866 params.Op0 = 0;
867 params.Op1 = (hsr >> 16) & 0xf;
868 params.Op2 = 0;
869 params.CRn = 0;
870
871 /*
872 * Massive hack here. Store Rt2 in the top 32bits so we only
873 * have one register to deal with. As we use the same trap
874 * backends between AArch32 and AArch64, we get away with it.
875 */
876 if (params.is_write) {
877 u64 val = *vcpu_reg(vcpu, params.Rt);
878 val &= 0xffffffff;
879 val |= *vcpu_reg(vcpu, Rt2) << 32;
880 *vcpu_reg(vcpu, params.Rt) = val;
881 }
882
Marc Zyngier72564012014-04-24 10:27:13 +0100883 if (!emulate_cp(vcpu, &params, target_specific, nr_specific))
884 goto out;
885 if (!emulate_cp(vcpu, &params, global, nr_global))
886 goto out;
Marc Zyngier62a89c42013-02-07 10:32:33 +0000887
Marc Zyngier72564012014-04-24 10:27:13 +0100888 unhandled_cp_access(vcpu, &params);
889
890out:
Marc Zyngier62a89c42013-02-07 10:32:33 +0000891 /* Do the opposite hack for the read side */
892 if (!params.is_write) {
893 u64 val = *vcpu_reg(vcpu, params.Rt);
894 val >>= 32;
895 *vcpu_reg(vcpu, Rt2) = val;
896 }
897
898 return 1;
899}
900
901/**
902 * kvm_handle_cp15_32 -- handles a mrc/mcr trap on a guest CP15 access
903 * @vcpu: The VCPU pointer
904 * @run: The kvm_run struct
905 */
Marc Zyngier72564012014-04-24 10:27:13 +0100906static int kvm_handle_cp_32(struct kvm_vcpu *vcpu,
907 const struct sys_reg_desc *global,
908 size_t nr_global,
909 const struct sys_reg_desc *target_specific,
910 size_t nr_specific)
Marc Zyngier62a89c42013-02-07 10:32:33 +0000911{
912 struct sys_reg_params params;
913 u32 hsr = kvm_vcpu_get_hsr(vcpu);
914
Marc Zyngier2072d292014-01-21 10:55:17 +0000915 params.is_aarch32 = true;
916 params.is_32bit = true;
Marc Zyngier62a89c42013-02-07 10:32:33 +0000917 params.CRm = (hsr >> 1) & 0xf;
918 params.Rt = (hsr >> 5) & 0xf;
919 params.is_write = ((hsr & 1) == 0);
920 params.CRn = (hsr >> 10) & 0xf;
921 params.Op0 = 0;
922 params.Op1 = (hsr >> 14) & 0x7;
923 params.Op2 = (hsr >> 17) & 0x7;
924
Marc Zyngier72564012014-04-24 10:27:13 +0100925 if (!emulate_cp(vcpu, &params, target_specific, nr_specific))
926 return 1;
927 if (!emulate_cp(vcpu, &params, global, nr_global))
928 return 1;
929
930 unhandled_cp_access(vcpu, &params);
Marc Zyngier62a89c42013-02-07 10:32:33 +0000931 return 1;
932}
933
Marc Zyngier72564012014-04-24 10:27:13 +0100934int kvm_handle_cp15_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
935{
936 const struct sys_reg_desc *target_specific;
937 size_t num;
938
939 target_specific = get_target_table(vcpu->arch.target, false, &num);
940 return kvm_handle_cp_64(vcpu,
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100941 cp15_64_regs, ARRAY_SIZE(cp15_64_regs),
Marc Zyngier72564012014-04-24 10:27:13 +0100942 target_specific, num);
943}
944
945int kvm_handle_cp15_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
946{
947 const struct sys_reg_desc *target_specific;
948 size_t num;
949
950 target_specific = get_target_table(vcpu->arch.target, false, &num);
951 return kvm_handle_cp_32(vcpu,
952 cp15_regs, ARRAY_SIZE(cp15_regs),
953 target_specific, num);
954}
955
956int kvm_handle_cp14_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
957{
958 return kvm_handle_cp_64(vcpu,
Marc Zyngiera9866ba02014-04-24 14:11:48 +0100959 cp14_64_regs, ARRAY_SIZE(cp14_64_regs),
Marc Zyngier72564012014-04-24 10:27:13 +0100960 NULL, 0);
961}
962
963int kvm_handle_cp14_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
964{
965 return kvm_handle_cp_32(vcpu,
966 cp14_regs, ARRAY_SIZE(cp14_regs),
967 NULL, 0);
968}
969
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000970static int emulate_sys_reg(struct kvm_vcpu *vcpu,
971 const struct sys_reg_params *params)
972{
973 size_t num;
974 const struct sys_reg_desc *table, *r;
975
Marc Zyngier62a89c42013-02-07 10:32:33 +0000976 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +0000977
978 /* Search target-specific then generic table. */
979 r = find_reg(params, table, num);
980 if (!r)
981 r = find_reg(params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
982
983 if (likely(r)) {
984 /*
985 * Not having an accessor means that we have
986 * configured a trap that we don't know how to
987 * handle. This certainly qualifies as a gross bug
988 * that should be fixed right away.
989 */
990 BUG_ON(!r->access);
991
992 if (likely(r->access(vcpu, params, r))) {
993 /* Skip instruction, since it was emulated */
994 kvm_skip_instr(vcpu, kvm_vcpu_trap_il_is32bit(vcpu));
995 return 1;
996 }
997 /* If access function fails, it should complain. */
998 } else {
999 kvm_err("Unsupported guest sys_reg access at: %lx\n",
1000 *vcpu_pc(vcpu));
1001 print_sys_reg_instr(params);
1002 }
1003 kvm_inject_undefined(vcpu);
1004 return 1;
1005}
1006
1007static void reset_sys_reg_descs(struct kvm_vcpu *vcpu,
1008 const struct sys_reg_desc *table, size_t num)
1009{
1010 unsigned long i;
1011
1012 for (i = 0; i < num; i++)
1013 if (table[i].reset)
1014 table[i].reset(vcpu, &table[i]);
1015}
1016
1017/**
1018 * kvm_handle_sys_reg -- handles a mrs/msr trap on a guest sys_reg access
1019 * @vcpu: The VCPU pointer
1020 * @run: The kvm_run struct
1021 */
1022int kvm_handle_sys_reg(struct kvm_vcpu *vcpu, struct kvm_run *run)
1023{
1024 struct sys_reg_params params;
1025 unsigned long esr = kvm_vcpu_get_hsr(vcpu);
1026
Marc Zyngier2072d292014-01-21 10:55:17 +00001027 params.is_aarch32 = false;
1028 params.is_32bit = false;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001029 params.Op0 = (esr >> 20) & 3;
1030 params.Op1 = (esr >> 14) & 0x7;
1031 params.CRn = (esr >> 10) & 0xf;
1032 params.CRm = (esr >> 1) & 0xf;
1033 params.Op2 = (esr >> 17) & 0x7;
1034 params.Rt = (esr >> 5) & 0x1f;
1035 params.is_write = !(esr & 1);
1036
1037 return emulate_sys_reg(vcpu, &params);
1038}
1039
1040/******************************************************************************
1041 * Userspace API
1042 *****************************************************************************/
1043
1044static bool index_to_params(u64 id, struct sys_reg_params *params)
1045{
1046 switch (id & KVM_REG_SIZE_MASK) {
1047 case KVM_REG_SIZE_U64:
1048 /* Any unused index bits means it's not valid. */
1049 if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
1050 | KVM_REG_ARM_COPROC_MASK
1051 | KVM_REG_ARM64_SYSREG_OP0_MASK
1052 | KVM_REG_ARM64_SYSREG_OP1_MASK
1053 | KVM_REG_ARM64_SYSREG_CRN_MASK
1054 | KVM_REG_ARM64_SYSREG_CRM_MASK
1055 | KVM_REG_ARM64_SYSREG_OP2_MASK))
1056 return false;
1057 params->Op0 = ((id & KVM_REG_ARM64_SYSREG_OP0_MASK)
1058 >> KVM_REG_ARM64_SYSREG_OP0_SHIFT);
1059 params->Op1 = ((id & KVM_REG_ARM64_SYSREG_OP1_MASK)
1060 >> KVM_REG_ARM64_SYSREG_OP1_SHIFT);
1061 params->CRn = ((id & KVM_REG_ARM64_SYSREG_CRN_MASK)
1062 >> KVM_REG_ARM64_SYSREG_CRN_SHIFT);
1063 params->CRm = ((id & KVM_REG_ARM64_SYSREG_CRM_MASK)
1064 >> KVM_REG_ARM64_SYSREG_CRM_SHIFT);
1065 params->Op2 = ((id & KVM_REG_ARM64_SYSREG_OP2_MASK)
1066 >> KVM_REG_ARM64_SYSREG_OP2_SHIFT);
1067 return true;
1068 default:
1069 return false;
1070 }
1071}
1072
1073/* Decode an index value, and find the sys_reg_desc entry. */
1074static const struct sys_reg_desc *index_to_sys_reg_desc(struct kvm_vcpu *vcpu,
1075 u64 id)
1076{
1077 size_t num;
1078 const struct sys_reg_desc *table, *r;
1079 struct sys_reg_params params;
1080
1081 /* We only do sys_reg for now. */
1082 if ((id & KVM_REG_ARM_COPROC_MASK) != KVM_REG_ARM64_SYSREG)
1083 return NULL;
1084
1085 if (!index_to_params(id, &params))
1086 return NULL;
1087
Marc Zyngier62a89c42013-02-07 10:32:33 +00001088 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001089 r = find_reg(&params, table, num);
1090 if (!r)
1091 r = find_reg(&params, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1092
1093 /* Not saved in the sys_reg array? */
1094 if (r && !r->reg)
1095 r = NULL;
1096
1097 return r;
1098}
1099
1100/*
1101 * These are the invariant sys_reg registers: we let the guest see the
1102 * host versions of these, so they're part of the guest state.
1103 *
1104 * A future CPU may provide a mechanism to present different values to
1105 * the guest, or a future kvm may trap them.
1106 */
1107
1108#define FUNCTION_INVARIANT(reg) \
1109 static void get_##reg(struct kvm_vcpu *v, \
1110 const struct sys_reg_desc *r) \
1111 { \
1112 u64 val; \
1113 \
1114 asm volatile("mrs %0, " __stringify(reg) "\n" \
1115 : "=r" (val)); \
1116 ((struct sys_reg_desc *)r)->val = val; \
1117 }
1118
1119FUNCTION_INVARIANT(midr_el1)
1120FUNCTION_INVARIANT(ctr_el0)
1121FUNCTION_INVARIANT(revidr_el1)
1122FUNCTION_INVARIANT(id_pfr0_el1)
1123FUNCTION_INVARIANT(id_pfr1_el1)
1124FUNCTION_INVARIANT(id_dfr0_el1)
1125FUNCTION_INVARIANT(id_afr0_el1)
1126FUNCTION_INVARIANT(id_mmfr0_el1)
1127FUNCTION_INVARIANT(id_mmfr1_el1)
1128FUNCTION_INVARIANT(id_mmfr2_el1)
1129FUNCTION_INVARIANT(id_mmfr3_el1)
1130FUNCTION_INVARIANT(id_isar0_el1)
1131FUNCTION_INVARIANT(id_isar1_el1)
1132FUNCTION_INVARIANT(id_isar2_el1)
1133FUNCTION_INVARIANT(id_isar3_el1)
1134FUNCTION_INVARIANT(id_isar4_el1)
1135FUNCTION_INVARIANT(id_isar5_el1)
1136FUNCTION_INVARIANT(clidr_el1)
1137FUNCTION_INVARIANT(aidr_el1)
1138
1139/* ->val is filled in by kvm_sys_reg_table_init() */
1140static struct sys_reg_desc invariant_sys_regs[] = {
1141 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b000),
1142 NULL, get_midr_el1 },
1143 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0000), Op2(0b110),
1144 NULL, get_revidr_el1 },
1145 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b000),
1146 NULL, get_id_pfr0_el1 },
1147 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b001),
1148 NULL, get_id_pfr1_el1 },
1149 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b010),
1150 NULL, get_id_dfr0_el1 },
1151 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b011),
1152 NULL, get_id_afr0_el1 },
1153 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b100),
1154 NULL, get_id_mmfr0_el1 },
1155 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b101),
1156 NULL, get_id_mmfr1_el1 },
1157 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b110),
1158 NULL, get_id_mmfr2_el1 },
1159 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0001), Op2(0b111),
1160 NULL, get_id_mmfr3_el1 },
1161 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b000),
1162 NULL, get_id_isar0_el1 },
1163 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b001),
1164 NULL, get_id_isar1_el1 },
1165 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b010),
1166 NULL, get_id_isar2_el1 },
1167 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b011),
1168 NULL, get_id_isar3_el1 },
1169 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b100),
1170 NULL, get_id_isar4_el1 },
1171 { Op0(0b11), Op1(0b000), CRn(0b0000), CRm(0b0010), Op2(0b101),
1172 NULL, get_id_isar5_el1 },
1173 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b001),
1174 NULL, get_clidr_el1 },
1175 { Op0(0b11), Op1(0b001), CRn(0b0000), CRm(0b0000), Op2(0b111),
1176 NULL, get_aidr_el1 },
1177 { Op0(0b11), Op1(0b011), CRn(0b0000), CRm(0b0000), Op2(0b001),
1178 NULL, get_ctr_el0 },
1179};
1180
Victor Kamensky26c99af2014-06-12 09:30:12 -07001181static int reg_from_user(u64 *val, const void __user *uaddr, u64 id)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001182{
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001183 if (copy_from_user(val, uaddr, KVM_REG_SIZE(id)) != 0)
1184 return -EFAULT;
1185 return 0;
1186}
1187
Victor Kamensky26c99af2014-06-12 09:30:12 -07001188static int reg_to_user(void __user *uaddr, const u64 *val, u64 id)
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001189{
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001190 if (copy_to_user(uaddr, val, KVM_REG_SIZE(id)) != 0)
1191 return -EFAULT;
1192 return 0;
1193}
1194
1195static int get_invariant_sys_reg(u64 id, void __user *uaddr)
1196{
1197 struct sys_reg_params params;
1198 const struct sys_reg_desc *r;
1199
1200 if (!index_to_params(id, &params))
1201 return -ENOENT;
1202
1203 r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
1204 if (!r)
1205 return -ENOENT;
1206
1207 return reg_to_user(uaddr, &r->val, id);
1208}
1209
1210static int set_invariant_sys_reg(u64 id, void __user *uaddr)
1211{
1212 struct sys_reg_params params;
1213 const struct sys_reg_desc *r;
1214 int err;
1215 u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
1216
1217 if (!index_to_params(id, &params))
1218 return -ENOENT;
1219 r = find_reg(&params, invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs));
1220 if (!r)
1221 return -ENOENT;
1222
1223 err = reg_from_user(&val, uaddr, id);
1224 if (err)
1225 return err;
1226
1227 /* This is what we mean by invariant: you can't change it. */
1228 if (r->val != val)
1229 return -EINVAL;
1230
1231 return 0;
1232}
1233
1234static bool is_valid_cache(u32 val)
1235{
1236 u32 level, ctype;
1237
1238 if (val >= CSSELR_MAX)
Will Deacon18d45762014-08-26 15:13:22 +01001239 return false;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001240
1241 /* Bottom bit is Instruction or Data bit. Next 3 bits are level. */
1242 level = (val >> 1);
1243 ctype = (cache_levels >> (level * 3)) & 7;
1244
1245 switch (ctype) {
1246 case 0: /* No cache */
1247 return false;
1248 case 1: /* Instruction cache only */
1249 return (val & 1);
1250 case 2: /* Data cache only */
1251 case 4: /* Unified cache */
1252 return !(val & 1);
1253 case 3: /* Separate instruction and data caches */
1254 return true;
1255 default: /* Reserved: we can't know instruction or data. */
1256 return false;
1257 }
1258}
1259
1260static int demux_c15_get(u64 id, void __user *uaddr)
1261{
1262 u32 val;
1263 u32 __user *uval = uaddr;
1264
1265 /* Fail if we have unknown bits set. */
1266 if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
1267 | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
1268 return -ENOENT;
1269
1270 switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
1271 case KVM_REG_ARM_DEMUX_ID_CCSIDR:
1272 if (KVM_REG_SIZE(id) != 4)
1273 return -ENOENT;
1274 val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
1275 >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
1276 if (!is_valid_cache(val))
1277 return -ENOENT;
1278
1279 return put_user(get_ccsidr(val), uval);
1280 default:
1281 return -ENOENT;
1282 }
1283}
1284
1285static int demux_c15_set(u64 id, void __user *uaddr)
1286{
1287 u32 val, newval;
1288 u32 __user *uval = uaddr;
1289
1290 /* Fail if we have unknown bits set. */
1291 if (id & ~(KVM_REG_ARCH_MASK|KVM_REG_SIZE_MASK|KVM_REG_ARM_COPROC_MASK
1292 | ((1 << KVM_REG_ARM_COPROC_SHIFT)-1)))
1293 return -ENOENT;
1294
1295 switch (id & KVM_REG_ARM_DEMUX_ID_MASK) {
1296 case KVM_REG_ARM_DEMUX_ID_CCSIDR:
1297 if (KVM_REG_SIZE(id) != 4)
1298 return -ENOENT;
1299 val = (id & KVM_REG_ARM_DEMUX_VAL_MASK)
1300 >> KVM_REG_ARM_DEMUX_VAL_SHIFT;
1301 if (!is_valid_cache(val))
1302 return -ENOENT;
1303
1304 if (get_user(newval, uval))
1305 return -EFAULT;
1306
1307 /* This is also invariant: you can't change it. */
1308 if (newval != get_ccsidr(val))
1309 return -EINVAL;
1310 return 0;
1311 default:
1312 return -ENOENT;
1313 }
1314}
1315
1316int kvm_arm_sys_reg_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
1317{
1318 const struct sys_reg_desc *r;
1319 void __user *uaddr = (void __user *)(unsigned long)reg->addr;
1320
1321 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
1322 return demux_c15_get(reg->id, uaddr);
1323
1324 if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
1325 return -ENOENT;
1326
1327 r = index_to_sys_reg_desc(vcpu, reg->id);
1328 if (!r)
1329 return get_invariant_sys_reg(reg->id, uaddr);
1330
1331 return reg_to_user(uaddr, &vcpu_sys_reg(vcpu, r->reg), reg->id);
1332}
1333
1334int kvm_arm_sys_reg_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
1335{
1336 const struct sys_reg_desc *r;
1337 void __user *uaddr = (void __user *)(unsigned long)reg->addr;
1338
1339 if ((reg->id & KVM_REG_ARM_COPROC_MASK) == KVM_REG_ARM_DEMUX)
1340 return demux_c15_set(reg->id, uaddr);
1341
1342 if (KVM_REG_SIZE(reg->id) != sizeof(__u64))
1343 return -ENOENT;
1344
1345 r = index_to_sys_reg_desc(vcpu, reg->id);
1346 if (!r)
1347 return set_invariant_sys_reg(reg->id, uaddr);
1348
1349 return reg_from_user(&vcpu_sys_reg(vcpu, r->reg), uaddr, reg->id);
1350}
1351
1352static unsigned int num_demux_regs(void)
1353{
1354 unsigned int i, count = 0;
1355
1356 for (i = 0; i < CSSELR_MAX; i++)
1357 if (is_valid_cache(i))
1358 count++;
1359
1360 return count;
1361}
1362
1363static int write_demux_regids(u64 __user *uindices)
1364{
Alex Bennéeefd48ce2014-07-01 16:53:13 +01001365 u64 val = KVM_REG_ARM64 | KVM_REG_SIZE_U32 | KVM_REG_ARM_DEMUX;
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001366 unsigned int i;
1367
1368 val |= KVM_REG_ARM_DEMUX_ID_CCSIDR;
1369 for (i = 0; i < CSSELR_MAX; i++) {
1370 if (!is_valid_cache(i))
1371 continue;
1372 if (put_user(val | i, uindices))
1373 return -EFAULT;
1374 uindices++;
1375 }
1376 return 0;
1377}
1378
1379static u64 sys_reg_to_index(const struct sys_reg_desc *reg)
1380{
1381 return (KVM_REG_ARM64 | KVM_REG_SIZE_U64 |
1382 KVM_REG_ARM64_SYSREG |
1383 (reg->Op0 << KVM_REG_ARM64_SYSREG_OP0_SHIFT) |
1384 (reg->Op1 << KVM_REG_ARM64_SYSREG_OP1_SHIFT) |
1385 (reg->CRn << KVM_REG_ARM64_SYSREG_CRN_SHIFT) |
1386 (reg->CRm << KVM_REG_ARM64_SYSREG_CRM_SHIFT) |
1387 (reg->Op2 << KVM_REG_ARM64_SYSREG_OP2_SHIFT));
1388}
1389
1390static bool copy_reg_to_user(const struct sys_reg_desc *reg, u64 __user **uind)
1391{
1392 if (!*uind)
1393 return true;
1394
1395 if (put_user(sys_reg_to_index(reg), *uind))
1396 return false;
1397
1398 (*uind)++;
1399 return true;
1400}
1401
1402/* Assumed ordered tables, see kvm_sys_reg_table_init. */
1403static int walk_sys_regs(struct kvm_vcpu *vcpu, u64 __user *uind)
1404{
1405 const struct sys_reg_desc *i1, *i2, *end1, *end2;
1406 unsigned int total = 0;
1407 size_t num;
1408
1409 /* We check for duplicates here, to allow arch-specific overrides. */
Marc Zyngier62a89c42013-02-07 10:32:33 +00001410 i1 = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001411 end1 = i1 + num;
1412 i2 = sys_reg_descs;
1413 end2 = sys_reg_descs + ARRAY_SIZE(sys_reg_descs);
1414
1415 BUG_ON(i1 == end1 || i2 == end2);
1416
1417 /* Walk carefully, as both tables may refer to the same register. */
1418 while (i1 || i2) {
1419 int cmp = cmp_sys_reg(i1, i2);
1420 /* target-specific overrides generic entry. */
1421 if (cmp <= 0) {
1422 /* Ignore registers we trap but don't save. */
1423 if (i1->reg) {
1424 if (!copy_reg_to_user(i1, &uind))
1425 return -EFAULT;
1426 total++;
1427 }
1428 } else {
1429 /* Ignore registers we trap but don't save. */
1430 if (i2->reg) {
1431 if (!copy_reg_to_user(i2, &uind))
1432 return -EFAULT;
1433 total++;
1434 }
1435 }
1436
1437 if (cmp <= 0 && ++i1 == end1)
1438 i1 = NULL;
1439 if (cmp >= 0 && ++i2 == end2)
1440 i2 = NULL;
1441 }
1442 return total;
1443}
1444
1445unsigned long kvm_arm_num_sys_reg_descs(struct kvm_vcpu *vcpu)
1446{
1447 return ARRAY_SIZE(invariant_sys_regs)
1448 + num_demux_regs()
1449 + walk_sys_regs(vcpu, (u64 __user *)NULL);
1450}
1451
1452int kvm_arm_copy_sys_reg_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
1453{
1454 unsigned int i;
1455 int err;
1456
1457 /* Then give them all the invariant registers' indices. */
1458 for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++) {
1459 if (put_user(sys_reg_to_index(&invariant_sys_regs[i]), uindices))
1460 return -EFAULT;
1461 uindices++;
1462 }
1463
1464 err = walk_sys_regs(vcpu, uindices);
1465 if (err < 0)
1466 return err;
1467 uindices += err;
1468
1469 return write_demux_regids(uindices);
1470}
1471
Marc Zyngiere6a95512014-05-07 13:43:39 +01001472static int check_sysreg_table(const struct sys_reg_desc *table, unsigned int n)
1473{
1474 unsigned int i;
1475
1476 for (i = 1; i < n; i++) {
1477 if (cmp_sys_reg(&table[i-1], &table[i]) >= 0) {
1478 kvm_err("sys_reg table %p out of order (%d)\n", table, i - 1);
1479 return 1;
1480 }
1481 }
1482
1483 return 0;
1484}
1485
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001486void kvm_sys_reg_table_init(void)
1487{
1488 unsigned int i;
1489 struct sys_reg_desc clidr;
1490
1491 /* Make sure tables are unique and in order. */
Marc Zyngiere6a95512014-05-07 13:43:39 +01001492 BUG_ON(check_sysreg_table(sys_reg_descs, ARRAY_SIZE(sys_reg_descs)));
1493 BUG_ON(check_sysreg_table(cp14_regs, ARRAY_SIZE(cp14_regs)));
1494 BUG_ON(check_sysreg_table(cp14_64_regs, ARRAY_SIZE(cp14_64_regs)));
1495 BUG_ON(check_sysreg_table(cp15_regs, ARRAY_SIZE(cp15_regs)));
1496 BUG_ON(check_sysreg_table(cp15_64_regs, ARRAY_SIZE(cp15_64_regs)));
1497 BUG_ON(check_sysreg_table(invariant_sys_regs, ARRAY_SIZE(invariant_sys_regs)));
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001498
1499 /* We abuse the reset function to overwrite the table itself. */
1500 for (i = 0; i < ARRAY_SIZE(invariant_sys_regs); i++)
1501 invariant_sys_regs[i].reset(NULL, &invariant_sys_regs[i]);
1502
1503 /*
1504 * CLIDR format is awkward, so clean it up. See ARM B4.1.20:
1505 *
1506 * If software reads the Cache Type fields from Ctype1
1507 * upwards, once it has seen a value of 0b000, no caches
1508 * exist at further-out levels of the hierarchy. So, for
1509 * example, if Ctype3 is the first Cache Type field with a
1510 * value of 0b000, the values of Ctype4 to Ctype7 must be
1511 * ignored.
1512 */
1513 get_clidr_el1(NULL, &clidr); /* Ugly... */
1514 cache_levels = clidr.val;
1515 for (i = 0; i < 7; i++)
1516 if (((cache_levels >> (i*3)) & 7) == 0)
1517 break;
1518 /* Clear all higher bits. */
1519 cache_levels &= (1 << (i*3))-1;
1520}
1521
1522/**
1523 * kvm_reset_sys_regs - sets system registers to reset value
1524 * @vcpu: The VCPU pointer
1525 *
1526 * This function finds the right table above and sets the registers on the
1527 * virtual CPU struct to their architecturally defined reset values.
1528 */
1529void kvm_reset_sys_regs(struct kvm_vcpu *vcpu)
1530{
1531 size_t num;
1532 const struct sys_reg_desc *table;
1533
1534 /* Catch someone adding a register without putting in reset entry. */
1535 memset(&vcpu->arch.ctxt.sys_regs, 0x42, sizeof(vcpu->arch.ctxt.sys_regs));
1536
1537 /* Generic chip reset first (so target could override). */
1538 reset_sys_reg_descs(vcpu, sys_reg_descs, ARRAY_SIZE(sys_reg_descs));
1539
Marc Zyngier62a89c42013-02-07 10:32:33 +00001540 table = get_target_table(vcpu->arch.target, true, &num);
Marc Zyngier7c8c5e6a2012-12-10 16:15:34 +00001541 reset_sys_reg_descs(vcpu, table, num);
1542
1543 for (num = 1; num < NR_SYS_REGS; num++)
1544 if (vcpu_sys_reg(vcpu, num) == 0x4242424242424242)
1545 panic("Didn't reset vcpu_sys_reg(%zi)", num);
1546}