95a0f5e5c1fc84c68d9182e729fab26fb478c634
[platform/adaptation/renesas_rcar/renesas_kernel.git] / arch / arm / kvm / coproc.c
1 /*
2  * Copyright (C) 2012 - Virtual Open Systems and Columbia University
3  * Authors: Rusty Russell <rusty@rustcorp.com.au>
4  *          Christoffer Dall <c.dall@virtualopensystems.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License, version 2, as
8  * published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
18  */
19 #include <linux/mm.h>
20 #include <linux/kvm_host.h>
21 #include <linux/uaccess.h>
22 #include <asm/kvm_arm.h>
23 #include <asm/kvm_host.h>
24 #include <asm/kvm_emulate.h>
25 #include <asm/kvm_coproc.h>
26 #include <asm/cacheflush.h>
27 #include <asm/cputype.h>
28 #include <trace/events/kvm.h>
29
30 #include "trace.h"
31 #include "coproc.h"
32
33
34 /******************************************************************************
35  * Co-processor emulation
36  *****************************************************************************/
37
38 int kvm_handle_cp10_id(struct kvm_vcpu *vcpu, struct kvm_run *run)
39 {
40         kvm_inject_undefined(vcpu);
41         return 1;
42 }
43
44 int kvm_handle_cp_0_13_access(struct kvm_vcpu *vcpu, struct kvm_run *run)
45 {
46         /*
47          * We can get here, if the host has been built without VFPv3 support,
48          * but the guest attempted a floating point operation.
49          */
50         kvm_inject_undefined(vcpu);
51         return 1;
52 }
53
54 int kvm_handle_cp14_load_store(struct kvm_vcpu *vcpu, struct kvm_run *run)
55 {
56         kvm_inject_undefined(vcpu);
57         return 1;
58 }
59
60 int kvm_handle_cp14_access(struct kvm_vcpu *vcpu, struct kvm_run *run)
61 {
62         kvm_inject_undefined(vcpu);
63         return 1;
64 }
65
66 /* See note at ARM ARM B1.14.4 */
67 static bool access_dcsw(struct kvm_vcpu *vcpu,
68                         const struct coproc_params *p,
69                         const struct coproc_reg *r)
70 {
71         u32 val;
72         int cpu;
73
74         cpu = get_cpu();
75
76         if (!p->is_write)
77                 return read_from_write_only(vcpu, p);
78
79         cpumask_setall(&vcpu->arch.require_dcache_flush);
80         cpumask_clear_cpu(cpu, &vcpu->arch.require_dcache_flush);
81
82         /* If we were already preempted, take the long way around */
83         if (cpu != vcpu->arch.last_pcpu) {
84                 flush_cache_all();
85                 goto done;
86         }
87
88         val = *vcpu_reg(vcpu, p->Rt1);
89
90         switch (p->CRm) {
91         case 6:                 /* Upgrade DCISW to DCCISW, as per HCR.SWIO */
92         case 14:                /* DCCISW */
93                 asm volatile("mcr p15, 0, %0, c7, c14, 2" : : "r" (val));
94                 break;
95
96         case 10:                /* DCCSW */
97                 asm volatile("mcr p15, 0, %0, c7, c10, 2" : : "r" (val));
98                 break;
99         }
100
101 done:
102         put_cpu();
103
104         return true;
105 }
106
107 /*
108  * We could trap ID_DFR0 and tell the guest we don't support performance
109  * monitoring.  Unfortunately the patch to make the kernel check ID_DFR0 was
110  * NAKed, so it will read the PMCR anyway.
111  *
112  * Therefore we tell the guest we have 0 counters.  Unfortunately, we
113  * must always support PMCCNTR (the cycle counter): we just RAZ/WI for
114  * all PM registers, which doesn't crash the guest kernel at least.
115  */
116 static bool pm_fake(struct kvm_vcpu *vcpu,
117                     const struct coproc_params *p,
118                     const struct coproc_reg *r)
119 {
120         if (p->is_write)
121                 return ignore_write(vcpu, p);
122         else
123                 return read_zero(vcpu, p);
124 }
125
126 #define access_pmcr pm_fake
127 #define access_pmcntenset pm_fake
128 #define access_pmcntenclr pm_fake
129 #define access_pmovsr pm_fake
130 #define access_pmselr pm_fake
131 #define access_pmceid0 pm_fake
132 #define access_pmceid1 pm_fake
133 #define access_pmccntr pm_fake
134 #define access_pmxevtyper pm_fake
135 #define access_pmxevcntr pm_fake
136 #define access_pmuserenr pm_fake
137 #define access_pmintenset pm_fake
138 #define access_pmintenclr pm_fake
139
140 /* Architected CP15 registers.
141  * Important: Must be sorted ascending by CRn, CRM, Op1, Op2
142  */
143 static const struct coproc_reg cp15_regs[] = {
144         /* CSSELR: swapped by interrupt.S. */
145         { CRn( 0), CRm( 0), Op1( 2), Op2( 0), is32,
146                         NULL, reset_unknown, c0_CSSELR },
147
148         /* TTBR0/TTBR1: swapped by interrupt.S. */
149         { CRm( 2), Op1( 0), is64, NULL, reset_unknown64, c2_TTBR0 },
150         { CRm( 2), Op1( 1), is64, NULL, reset_unknown64, c2_TTBR1 },
151
152         /* TTBCR: swapped by interrupt.S. */
153         { CRn( 2), CRm( 0), Op1( 0), Op2( 2), is32,
154                         NULL, reset_val, c2_TTBCR, 0x00000000 },
155
156         /* DACR: swapped by interrupt.S. */
157         { CRn( 3), CRm( 0), Op1( 0), Op2( 0), is32,
158                         NULL, reset_unknown, c3_DACR },
159
160         /* DFSR/IFSR/ADFSR/AIFSR: swapped by interrupt.S. */
161         { CRn( 5), CRm( 0), Op1( 0), Op2( 0), is32,
162                         NULL, reset_unknown, c5_DFSR },
163         { CRn( 5), CRm( 0), Op1( 0), Op2( 1), is32,
164                         NULL, reset_unknown, c5_IFSR },
165         { CRn( 5), CRm( 1), Op1( 0), Op2( 0), is32,
166                         NULL, reset_unknown, c5_ADFSR },
167         { CRn( 5), CRm( 1), Op1( 0), Op2( 1), is32,
168                         NULL, reset_unknown, c5_AIFSR },
169
170         /* DFAR/IFAR: swapped by interrupt.S. */
171         { CRn( 6), CRm( 0), Op1( 0), Op2( 0), is32,
172                         NULL, reset_unknown, c6_DFAR },
173         { CRn( 6), CRm( 0), Op1( 0), Op2( 2), is32,
174                         NULL, reset_unknown, c6_IFAR },
175         /*
176          * DC{C,I,CI}SW operations:
177          */
178         { CRn( 7), CRm( 6), Op1( 0), Op2( 2), is32, access_dcsw},
179         { CRn( 7), CRm(10), Op1( 0), Op2( 2), is32, access_dcsw},
180         { CRn( 7), CRm(14), Op1( 0), Op2( 2), is32, access_dcsw},
181         /*
182          * Dummy performance monitor implementation.
183          */
184         { CRn( 9), CRm(12), Op1( 0), Op2( 0), is32, access_pmcr},
185         { CRn( 9), CRm(12), Op1( 0), Op2( 1), is32, access_pmcntenset},
186         { CRn( 9), CRm(12), Op1( 0), Op2( 2), is32, access_pmcntenclr},
187         { CRn( 9), CRm(12), Op1( 0), Op2( 3), is32, access_pmovsr},
188         { CRn( 9), CRm(12), Op1( 0), Op2( 5), is32, access_pmselr},
189         { CRn( 9), CRm(12), Op1( 0), Op2( 6), is32, access_pmceid0},
190         { CRn( 9), CRm(12), Op1( 0), Op2( 7), is32, access_pmceid1},
191         { CRn( 9), CRm(13), Op1( 0), Op2( 0), is32, access_pmccntr},
192         { CRn( 9), CRm(13), Op1( 0), Op2( 1), is32, access_pmxevtyper},
193         { CRn( 9), CRm(13), Op1( 0), Op2( 2), is32, access_pmxevcntr},
194         { CRn( 9), CRm(14), Op1( 0), Op2( 0), is32, access_pmuserenr},
195         { CRn( 9), CRm(14), Op1( 0), Op2( 1), is32, access_pmintenset},
196         { CRn( 9), CRm(14), Op1( 0), Op2( 2), is32, access_pmintenclr},
197
198         /* PRRR/NMRR (aka MAIR0/MAIR1): swapped by interrupt.S. */
199         { CRn(10), CRm( 2), Op1( 0), Op2( 0), is32,
200                         NULL, reset_unknown, c10_PRRR},
201         { CRn(10), CRm( 2), Op1( 0), Op2( 1), is32,
202                         NULL, reset_unknown, c10_NMRR},
203
204         /* VBAR: swapped by interrupt.S. */
205         { CRn(12), CRm( 0), Op1( 0), Op2( 0), is32,
206                         NULL, reset_val, c12_VBAR, 0x00000000 },
207
208         /* CONTEXTIDR/TPIDRURW/TPIDRURO/TPIDRPRW: swapped by interrupt.S. */
209         { CRn(13), CRm( 0), Op1( 0), Op2( 1), is32,
210                         NULL, reset_val, c13_CID, 0x00000000 },
211         { CRn(13), CRm( 0), Op1( 0), Op2( 2), is32,
212                         NULL, reset_unknown, c13_TID_URW },
213         { CRn(13), CRm( 0), Op1( 0), Op2( 3), is32,
214                         NULL, reset_unknown, c13_TID_URO },
215         { CRn(13), CRm( 0), Op1( 0), Op2( 4), is32,
216                         NULL, reset_unknown, c13_TID_PRIV },
217 };
218
219 /* Target specific emulation tables */
220 static struct kvm_coproc_target_table *target_tables[KVM_ARM_NUM_TARGETS];
221
222 void kvm_register_target_coproc_table(struct kvm_coproc_target_table *table)
223 {
224         target_tables[table->target] = table;
225 }
226
227 /* Get specific register table for this target. */
228 static const struct coproc_reg *get_target_table(unsigned target, size_t *num)
229 {
230         struct kvm_coproc_target_table *table;
231
232         table = target_tables[target];
233         *num = table->num;
234         return table->table;
235 }
236
237 static const struct coproc_reg *find_reg(const struct coproc_params *params,
238                                          const struct coproc_reg table[],
239                                          unsigned int num)
240 {
241         unsigned int i;
242
243         for (i = 0; i < num; i++) {
244                 const struct coproc_reg *r = &table[i];
245
246                 if (params->is_64bit != r->is_64)
247                         continue;
248                 if (params->CRn != r->CRn)
249                         continue;
250                 if (params->CRm != r->CRm)
251                         continue;
252                 if (params->Op1 != r->Op1)
253                         continue;
254                 if (params->Op2 != r->Op2)
255                         continue;
256
257                 return r;
258         }
259         return NULL;
260 }
261
262 static int emulate_cp15(struct kvm_vcpu *vcpu,
263                         const struct coproc_params *params)
264 {
265         size_t num;
266         const struct coproc_reg *table, *r;
267
268         trace_kvm_emulate_cp15_imp(params->Op1, params->Rt1, params->CRn,
269                                    params->CRm, params->Op2, params->is_write);
270
271         table = get_target_table(vcpu->arch.target, &num);
272
273         /* Search target-specific then generic table. */
274         r = find_reg(params, table, num);
275         if (!r)
276                 r = find_reg(params, cp15_regs, ARRAY_SIZE(cp15_regs));
277
278         if (likely(r)) {
279                 /* If we don't have an accessor, we should never get here! */
280                 BUG_ON(!r->access);
281
282                 if (likely(r->access(vcpu, params, r))) {
283                         /* Skip instruction, since it was emulated */
284                         kvm_skip_instr(vcpu, (vcpu->arch.hsr >> 25) & 1);
285                         return 1;
286                 }
287                 /* If access function fails, it should complain. */
288         } else {
289                 kvm_err("Unsupported guest CP15 access at: %08x\n",
290                         *vcpu_pc(vcpu));
291                 print_cp_instr(params);
292         }
293         kvm_inject_undefined(vcpu);
294         return 1;
295 }
296
297 /**
298  * kvm_handle_cp15_64 -- handles a mrrc/mcrr trap on a guest CP15 access
299  * @vcpu: The VCPU pointer
300  * @run:  The kvm_run struct
301  */
302 int kvm_handle_cp15_64(struct kvm_vcpu *vcpu, struct kvm_run *run)
303 {
304         struct coproc_params params;
305
306         params.CRm = (vcpu->arch.hsr >> 1) & 0xf;
307         params.Rt1 = (vcpu->arch.hsr >> 5) & 0xf;
308         params.is_write = ((vcpu->arch.hsr & 1) == 0);
309         params.is_64bit = true;
310
311         params.Op1 = (vcpu->arch.hsr >> 16) & 0xf;
312         params.Op2 = 0;
313         params.Rt2 = (vcpu->arch.hsr >> 10) & 0xf;
314         params.CRn = 0;
315
316         return emulate_cp15(vcpu, &params);
317 }
318
319 static void reset_coproc_regs(struct kvm_vcpu *vcpu,
320                               const struct coproc_reg *table, size_t num)
321 {
322         unsigned long i;
323
324         for (i = 0; i < num; i++)
325                 if (table[i].reset)
326                         table[i].reset(vcpu, &table[i]);
327 }
328
329 /**
330  * kvm_handle_cp15_32 -- handles a mrc/mcr trap on a guest CP15 access
331  * @vcpu: The VCPU pointer
332  * @run:  The kvm_run struct
333  */
334 int kvm_handle_cp15_32(struct kvm_vcpu *vcpu, struct kvm_run *run)
335 {
336         struct coproc_params params;
337
338         params.CRm = (vcpu->arch.hsr >> 1) & 0xf;
339         params.Rt1 = (vcpu->arch.hsr >> 5) & 0xf;
340         params.is_write = ((vcpu->arch.hsr & 1) == 0);
341         params.is_64bit = false;
342
343         params.CRn = (vcpu->arch.hsr >> 10) & 0xf;
344         params.Op1 = (vcpu->arch.hsr >> 14) & 0x7;
345         params.Op2 = (vcpu->arch.hsr >> 17) & 0x7;
346         params.Rt2 = 0;
347
348         return emulate_cp15(vcpu, &params);
349 }
350
351 /******************************************************************************
352  * Userspace API
353  *****************************************************************************/
354
355 static bool index_to_params(u64 id, struct coproc_params *params)
356 {
357         switch (id & KVM_REG_SIZE_MASK) {
358         case KVM_REG_SIZE_U32:
359                 /* Any unused index bits means it's not valid. */
360                 if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
361                            | KVM_REG_ARM_COPROC_MASK
362                            | KVM_REG_ARM_32_CRN_MASK
363                            | KVM_REG_ARM_CRM_MASK
364                            | KVM_REG_ARM_OPC1_MASK
365                            | KVM_REG_ARM_32_OPC2_MASK))
366                         return false;
367
368                 params->is_64bit = false;
369                 params->CRn = ((id & KVM_REG_ARM_32_CRN_MASK)
370                                >> KVM_REG_ARM_32_CRN_SHIFT);
371                 params->CRm = ((id & KVM_REG_ARM_CRM_MASK)
372                                >> KVM_REG_ARM_CRM_SHIFT);
373                 params->Op1 = ((id & KVM_REG_ARM_OPC1_MASK)
374                                >> KVM_REG_ARM_OPC1_SHIFT);
375                 params->Op2 = ((id & KVM_REG_ARM_32_OPC2_MASK)
376                                >> KVM_REG_ARM_32_OPC2_SHIFT);
377                 return true;
378         case KVM_REG_SIZE_U64:
379                 /* Any unused index bits means it's not valid. */
380                 if (id & ~(KVM_REG_ARCH_MASK | KVM_REG_SIZE_MASK
381                               | KVM_REG_ARM_COPROC_MASK
382                               | KVM_REG_ARM_CRM_MASK
383                               | KVM_REG_ARM_OPC1_MASK))
384                         return false;
385                 params->is_64bit = true;
386                 params->CRm = ((id & KVM_REG_ARM_CRM_MASK)
387                                >> KVM_REG_ARM_CRM_SHIFT);
388                 params->Op1 = ((id & KVM_REG_ARM_OPC1_MASK)
389                                >> KVM_REG_ARM_OPC1_SHIFT);
390                 params->Op2 = 0;
391                 params->CRn = 0;
392                 return true;
393         default:
394                 return false;
395         }
396 }
397
398 /* Decode an index value, and find the cp15 coproc_reg entry. */
399 static const struct coproc_reg *index_to_coproc_reg(struct kvm_vcpu *vcpu,
400                                                     u64 id)
401 {
402         size_t num;
403         const struct coproc_reg *table, *r;
404         struct coproc_params params;
405
406         /* We only do cp15 for now. */
407         if ((id & KVM_REG_ARM_COPROC_MASK) >> KVM_REG_ARM_COPROC_SHIFT != 15)
408                 return NULL;
409
410         if (!index_to_params(id, &params))
411                 return NULL;
412
413         table = get_target_table(vcpu->arch.target, &num);
414         r = find_reg(&params, table, num);
415         if (!r)
416                 r = find_reg(&params, cp15_regs, ARRAY_SIZE(cp15_regs));
417
418         /* Not saved in the cp15 array? */
419         if (r && !r->reg)
420                 r = NULL;
421
422         return r;
423 }
424
425 /*
426  * These are the invariant cp15 registers: we let the guest see the host
427  * versions of these, so they're part of the guest state.
428  *
429  * A future CPU may provide a mechanism to present different values to
430  * the guest, or a future kvm may trap them.
431  */
432 /* Unfortunately, there's no register-argument for mrc, so generate. */
433 #define FUNCTION_FOR32(crn, crm, op1, op2, name)                        \
434         static void get_##name(struct kvm_vcpu *v,                      \
435                                const struct coproc_reg *r)              \
436         {                                                               \
437                 u32 val;                                                \
438                                                                         \
439                 asm volatile("mrc p15, " __stringify(op1)               \
440                              ", %0, c" __stringify(crn)                 \
441                              ", c" __stringify(crm)                     \
442                              ", " __stringify(op2) "\n" : "=r" (val));  \
443                 ((struct coproc_reg *)r)->val = val;                    \
444         }
445
446 FUNCTION_FOR32(0, 0, 0, 0, MIDR)
447 FUNCTION_FOR32(0, 0, 0, 1, CTR)
448 FUNCTION_FOR32(0, 0, 0, 2, TCMTR)
449 FUNCTION_FOR32(0, 0, 0, 3, TLBTR)
450 FUNCTION_FOR32(0, 0, 0, 6, REVIDR)
451 FUNCTION_FOR32(0, 1, 0, 0, ID_PFR0)
452 FUNCTION_FOR32(0, 1, 0, 1, ID_PFR1)
453 FUNCTION_FOR32(0, 1, 0, 2, ID_DFR0)
454 FUNCTION_FOR32(0, 1, 0, 3, ID_AFR0)
455 FUNCTION_FOR32(0, 1, 0, 4, ID_MMFR0)
456 FUNCTION_FOR32(0, 1, 0, 5, ID_MMFR1)
457 FUNCTION_FOR32(0, 1, 0, 6, ID_MMFR2)
458 FUNCTION_FOR32(0, 1, 0, 7, ID_MMFR3)
459 FUNCTION_FOR32(0, 2, 0, 0, ID_ISAR0)
460 FUNCTION_FOR32(0, 2, 0, 1, ID_ISAR1)
461 FUNCTION_FOR32(0, 2, 0, 2, ID_ISAR2)
462 FUNCTION_FOR32(0, 2, 0, 3, ID_ISAR3)
463 FUNCTION_FOR32(0, 2, 0, 4, ID_ISAR4)
464 FUNCTION_FOR32(0, 2, 0, 5, ID_ISAR5)
465 FUNCTION_FOR32(0, 0, 1, 1, CLIDR)
466 FUNCTION_FOR32(0, 0, 1, 7, AIDR)
467
468 /* ->val is filled in by kvm_invariant_coproc_table_init() */
469 static struct coproc_reg invariant_cp15[] = {
470         { CRn( 0), CRm( 0), Op1( 0), Op2( 0), is32, NULL, get_MIDR },
471         { CRn( 0), CRm( 0), Op1( 0), Op2( 1), is32, NULL, get_CTR },
472         { CRn( 0), CRm( 0), Op1( 0), Op2( 2), is32, NULL, get_TCMTR },
473         { CRn( 0), CRm( 0), Op1( 0), Op2( 3), is32, NULL, get_TLBTR },
474         { CRn( 0), CRm( 0), Op1( 0), Op2( 6), is32, NULL, get_REVIDR },
475
476         { CRn( 0), CRm( 1), Op1( 0), Op2( 0), is32, NULL, get_ID_PFR0 },
477         { CRn( 0), CRm( 1), Op1( 0), Op2( 1), is32, NULL, get_ID_PFR1 },
478         { CRn( 0), CRm( 1), Op1( 0), Op2( 2), is32, NULL, get_ID_DFR0 },
479         { CRn( 0), CRm( 1), Op1( 0), Op2( 3), is32, NULL, get_ID_AFR0 },
480         { CRn( 0), CRm( 1), Op1( 0), Op2( 4), is32, NULL, get_ID_MMFR0 },
481         { CRn( 0), CRm( 1), Op1( 0), Op2( 5), is32, NULL, get_ID_MMFR1 },
482         { CRn( 0), CRm( 1), Op1( 0), Op2( 6), is32, NULL, get_ID_MMFR2 },
483         { CRn( 0), CRm( 1), Op1( 0), Op2( 7), is32, NULL, get_ID_MMFR3 },
484
485         { CRn( 0), CRm( 2), Op1( 0), Op2( 0), is32, NULL, get_ID_ISAR0 },
486         { CRn( 0), CRm( 2), Op1( 0), Op2( 1), is32, NULL, get_ID_ISAR1 },
487         { CRn( 0), CRm( 2), Op1( 0), Op2( 2), is32, NULL, get_ID_ISAR2 },
488         { CRn( 0), CRm( 2), Op1( 0), Op2( 3), is32, NULL, get_ID_ISAR3 },
489         { CRn( 0), CRm( 2), Op1( 0), Op2( 4), is32, NULL, get_ID_ISAR4 },
490         { CRn( 0), CRm( 2), Op1( 0), Op2( 5), is32, NULL, get_ID_ISAR5 },
491
492         { CRn( 0), CRm( 0), Op1( 1), Op2( 1), is32, NULL, get_CLIDR },
493         { CRn( 0), CRm( 0), Op1( 1), Op2( 7), is32, NULL, get_AIDR },
494 };
495
496 static int reg_from_user(void *val, const void __user *uaddr, u64 id)
497 {
498         /* This Just Works because we are little endian. */
499         if (copy_from_user(val, uaddr, KVM_REG_SIZE(id)) != 0)
500                 return -EFAULT;
501         return 0;
502 }
503
504 static int reg_to_user(void __user *uaddr, const void *val, u64 id)
505 {
506         /* This Just Works because we are little endian. */
507         if (copy_to_user(uaddr, val, KVM_REG_SIZE(id)) != 0)
508                 return -EFAULT;
509         return 0;
510 }
511
512 static int get_invariant_cp15(u64 id, void __user *uaddr)
513 {
514         struct coproc_params params;
515         const struct coproc_reg *r;
516
517         if (!index_to_params(id, &params))
518                 return -ENOENT;
519
520         r = find_reg(&params, invariant_cp15, ARRAY_SIZE(invariant_cp15));
521         if (!r)
522                 return -ENOENT;
523
524         return reg_to_user(uaddr, &r->val, id);
525 }
526
527 static int set_invariant_cp15(u64 id, void __user *uaddr)
528 {
529         struct coproc_params params;
530         const struct coproc_reg *r;
531         int err;
532         u64 val = 0; /* Make sure high bits are 0 for 32-bit regs */
533
534         if (!index_to_params(id, &params))
535                 return -ENOENT;
536         r = find_reg(&params, invariant_cp15, ARRAY_SIZE(invariant_cp15));
537         if (!r)
538                 return -ENOENT;
539
540         err = reg_from_user(&val, uaddr, id);
541         if (err)
542                 return err;
543
544         /* This is what we mean by invariant: you can't change it. */
545         if (r->val != val)
546                 return -EINVAL;
547
548         return 0;
549 }
550
551 int kvm_arm_coproc_get_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
552 {
553         const struct coproc_reg *r;
554         void __user *uaddr = (void __user *)(long)reg->addr;
555
556         r = index_to_coproc_reg(vcpu, reg->id);
557         if (!r)
558                 return get_invariant_cp15(reg->id, uaddr);
559
560         /* Note: copies two regs if size is 64 bit. */
561         return reg_to_user(uaddr, &vcpu->arch.cp15[r->reg], reg->id);
562 }
563
564 int kvm_arm_coproc_set_reg(struct kvm_vcpu *vcpu, const struct kvm_one_reg *reg)
565 {
566         const struct coproc_reg *r;
567         void __user *uaddr = (void __user *)(long)reg->addr;
568
569         r = index_to_coproc_reg(vcpu, reg->id);
570         if (!r)
571                 return set_invariant_cp15(reg->id, uaddr);
572
573         /* Note: copies two regs if size is 64 bit */
574         return reg_from_user(&vcpu->arch.cp15[r->reg], uaddr, reg->id);
575 }
576
577 static u64 cp15_to_index(const struct coproc_reg *reg)
578 {
579         u64 val = KVM_REG_ARM | (15 << KVM_REG_ARM_COPROC_SHIFT);
580         if (reg->is_64) {
581                 val |= KVM_REG_SIZE_U64;
582                 val |= (reg->Op1 << KVM_REG_ARM_OPC1_SHIFT);
583                 val |= (reg->CRm << KVM_REG_ARM_CRM_SHIFT);
584         } else {
585                 val |= KVM_REG_SIZE_U32;
586                 val |= (reg->Op1 << KVM_REG_ARM_OPC1_SHIFT);
587                 val |= (reg->Op2 << KVM_REG_ARM_32_OPC2_SHIFT);
588                 val |= (reg->CRm << KVM_REG_ARM_CRM_SHIFT);
589                 val |= (reg->CRn << KVM_REG_ARM_32_CRN_SHIFT);
590         }
591         return val;
592 }
593
594 static bool copy_reg_to_user(const struct coproc_reg *reg, u64 __user **uind)
595 {
596         if (!*uind)
597                 return true;
598
599         if (put_user(cp15_to_index(reg), *uind))
600                 return false;
601
602         (*uind)++;
603         return true;
604 }
605
606 /* Assumed ordered tables, see kvm_coproc_table_init. */
607 static int walk_cp15(struct kvm_vcpu *vcpu, u64 __user *uind)
608 {
609         const struct coproc_reg *i1, *i2, *end1, *end2;
610         unsigned int total = 0;
611         size_t num;
612
613         /* We check for duplicates here, to allow arch-specific overrides. */
614         i1 = get_target_table(vcpu->arch.target, &num);
615         end1 = i1 + num;
616         i2 = cp15_regs;
617         end2 = cp15_regs + ARRAY_SIZE(cp15_regs);
618
619         BUG_ON(i1 == end1 || i2 == end2);
620
621         /* Walk carefully, as both tables may refer to the same register. */
622         while (i1 || i2) {
623                 int cmp = cmp_reg(i1, i2);
624                 /* target-specific overrides generic entry. */
625                 if (cmp <= 0) {
626                         /* Ignore registers we trap but don't save. */
627                         if (i1->reg) {
628                                 if (!copy_reg_to_user(i1, &uind))
629                                         return -EFAULT;
630                                 total++;
631                         }
632                 } else {
633                         /* Ignore registers we trap but don't save. */
634                         if (i2->reg) {
635                                 if (!copy_reg_to_user(i2, &uind))
636                                         return -EFAULT;
637                                 total++;
638                         }
639                 }
640
641                 if (cmp <= 0 && ++i1 == end1)
642                         i1 = NULL;
643                 if (cmp >= 0 && ++i2 == end2)
644                         i2 = NULL;
645         }
646         return total;
647 }
648
649 unsigned long kvm_arm_num_coproc_regs(struct kvm_vcpu *vcpu)
650 {
651         return ARRAY_SIZE(invariant_cp15)
652                 + walk_cp15(vcpu, (u64 __user *)NULL);
653 }
654
655 int kvm_arm_copy_coproc_indices(struct kvm_vcpu *vcpu, u64 __user *uindices)
656 {
657         unsigned int i;
658         int err;
659
660         /* Then give them all the invariant registers' indices. */
661         for (i = 0; i < ARRAY_SIZE(invariant_cp15); i++) {
662                 if (put_user(cp15_to_index(&invariant_cp15[i]), uindices))
663                         return -EFAULT;
664                 uindices++;
665         }
666
667         err = walk_cp15(vcpu, uindices);
668         if (err > 0)
669                 err = 0;
670         return err;
671 }
672
673 void kvm_coproc_table_init(void)
674 {
675         unsigned int i;
676
677         /* Make sure tables are unique and in order. */
678         for (i = 1; i < ARRAY_SIZE(cp15_regs); i++)
679                 BUG_ON(cmp_reg(&cp15_regs[i-1], &cp15_regs[i]) >= 0);
680
681         /* We abuse the reset function to overwrite the table itself. */
682         for (i = 0; i < ARRAY_SIZE(invariant_cp15); i++)
683                 invariant_cp15[i].reset(NULL, &invariant_cp15[i]);
684 }
685
686 /**
687  * kvm_reset_coprocs - sets cp15 registers to reset value
688  * @vcpu: The VCPU pointer
689  *
690  * This function finds the right table above and sets the registers on the
691  * virtual CPU struct to their architecturally defined reset values.
692  */
693 void kvm_reset_coprocs(struct kvm_vcpu *vcpu)
694 {
695         size_t num;
696         const struct coproc_reg *table;
697
698         /* Catch someone adding a register without putting in reset entry. */
699         memset(vcpu->arch.cp15, 0x42, sizeof(vcpu->arch.cp15));
700
701         /* Generic chip reset first (so target could override). */
702         reset_coproc_regs(vcpu, cp15_regs, ARRAY_SIZE(cp15_regs));
703
704         table = get_target_table(vcpu->arch.target, &num);
705         reset_coproc_regs(vcpu, table, num);
706
707         for (num = 1; num < NR_CP15_REGS; num++)
708                 if (vcpu->arch.cp15[num] == 0x42424242)
709                         panic("Didn't reset vcpu->arch.cp15[%zi]", num);
710 }