blob: a2ab701ca5c2d137beb157b0ed3dd066f46d831e [file] [log] [blame]
Peter Maydell74c21bd2015-12-07 16:23:44 +00001#include "qemu/osdep.h"
bellardb5ff1b32005-11-26 10:38:39 +00002#include "cpu.h"
Peter Maydellccd38082014-04-15 19:18:37 +01003#include "internals.h"
Paolo Bonzini022c62c2012-12-17 18:19:49 +01004#include "exec/gdbstub.h"
Richard Henderson2ef61752014-04-07 22:31:41 -07005#include "exec/helper-proto.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01006#include "qemu/host-utils.h"
Cole Robinson78027bb2013-09-10 19:09:33 +01007#include "sysemu/arch_init.h"
Paolo Bonzini9c17d612012-12-17 18:20:04 +01008#include "sysemu/sysemu.h"
Paolo Bonzini1de7afc2012-12-17 18:20:00 +01009#include "qemu/bitops.h"
Will Newtoneb0ecd52014-02-26 17:20:07 +000010#include "qemu/crc32c.h"
Paolo Bonzinif08b6172014-03-28 19:42:10 +010011#include "exec/cpu_ldst.h"
Paolo Bonzini1d854762014-03-28 19:09:49 +010012#include "arm_ldst.h"
Will Newtoneb0ecd52014-02-26 17:20:07 +000013#include <zlib.h> /* For crc32 */
Leon Alraecfe67ce2015-06-19 14:17:45 +010014#include "exec/semihost.h"
Peter Maydellf3a9b692016-01-21 14:15:08 +000015#include "sysemu/kvm.h"
Peter Maydell0b03bdf2012-01-25 12:42:29 +000016
Laurent Vivier352c98e2015-08-25 17:09:36 +020017#define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */
18
Peter Maydell4a501602012-06-20 11:57:16 +000019#ifndef CONFIG_USER_ONLY
Edgar E. Iglesiasaf51f562015-10-26 14:02:02 +010020static bool get_phys_addr(CPUARMState *env, target_ulong address,
21 int access_type, ARMMMUIdx mmu_idx,
22 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +010023 target_ulong *page_size, uint32_t *fsr,
24 ARMMMUFaultInfo *fi);
Alistair Francis7c2cb422014-03-10 14:56:28 +000025
Edgar E. Iglesias37785972015-10-26 14:02:04 +010026static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
27 int access_type, ARMMMUIdx mmu_idx,
28 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
29 target_ulong *page_size_ptr, uint32_t *fsr,
30 ARMMMUFaultInfo *fi);
31
Alistair Francis7c2cb422014-03-10 14:56:28 +000032/* Definitions for the PMCCNTR and PMCR registers */
33#define PMCRD 0x8
34#define PMCRC 0x4
35#define PMCRE 0x1
Peter Maydell4a501602012-06-20 11:57:16 +000036#endif
37
Andreas Färber0ecb72a2012-03-14 01:38:21 +010038static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000039{
40 int nregs;
41
42 /* VFP data registers are always little-endian. */
43 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
44 if (reg < nregs) {
45 stfq_le_p(buf, env->vfp.regs[reg]);
46 return 8;
47 }
48 if (arm_feature(env, ARM_FEATURE_NEON)) {
49 /* Aliases for Q regs. */
50 nregs += 16;
51 if (reg < nregs) {
52 stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]);
53 stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]);
54 return 16;
55 }
56 }
57 switch (reg - nregs) {
58 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4;
59 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4;
60 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4;
61 }
62 return 0;
63}
64
Andreas Färber0ecb72a2012-03-14 01:38:21 +010065static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
pbrook56aebc82008-10-11 17:55:29 +000066{
67 int nregs;
68
69 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16;
70 if (reg < nregs) {
71 env->vfp.regs[reg] = ldfq_le_p(buf);
72 return 8;
73 }
74 if (arm_feature(env, ARM_FEATURE_NEON)) {
75 nregs += 16;
76 if (reg < nregs) {
77 env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf);
78 env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8);
79 return 16;
80 }
81 }
82 switch (reg - nregs) {
83 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4;
84 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4;
Juha Riihimäki71b3c3d2009-10-26 11:46:42 +020085 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4;
pbrook56aebc82008-10-11 17:55:29 +000086 }
87 return 0;
88}
89
Peter Maydell6a669422013-12-17 19:42:32 +000090static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg)
91{
92 switch (reg) {
93 case 0 ... 31:
94 /* 128 bit FP register */
95 stfq_le_p(buf, env->vfp.regs[reg * 2]);
96 stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]);
97 return 16;
98 case 32:
99 /* FPSR */
100 stl_p(buf, vfp_get_fpsr(env));
101 return 4;
102 case 33:
103 /* FPCR */
104 stl_p(buf, vfp_get_fpcr(env));
105 return 4;
106 default:
107 return 0;
108 }
109}
110
111static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg)
112{
113 switch (reg) {
114 case 0 ... 31:
115 /* 128 bit FP register */
116 env->vfp.regs[reg * 2] = ldfq_le_p(buf);
117 env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8);
118 return 16;
119 case 32:
120 /* FPSR */
121 vfp_set_fpsr(env, ldl_p(buf));
122 return 4;
123 case 33:
124 /* FPCR */
125 vfp_set_fpcr(env, ldl_p(buf));
126 return 4;
127 default:
128 return 0;
129 }
130}
131
Peter Maydellc4241c72014-02-20 10:35:54 +0000132static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydelld4e6df62013-06-25 18:16:07 +0100133{
Peter Maydell375421c2015-02-05 13:37:22 +0000134 assert(ri->fieldoffset);
Peter Maydell67ed7712014-02-26 17:20:01 +0000135 if (cpreg_field_is_64bit(ri)) {
Peter Maydellc4241c72014-02-20 10:35:54 +0000136 return CPREG_FIELD64(env, ri);
Peter Maydell22d9e1a2013-08-20 14:54:31 +0100137 } else {
Peter Maydellc4241c72014-02-20 10:35:54 +0000138 return CPREG_FIELD32(env, ri);
Peter Maydell22d9e1a2013-08-20 14:54:31 +0100139 }
Peter Maydelld4e6df62013-06-25 18:16:07 +0100140}
141
Peter Maydellc4241c72014-02-20 10:35:54 +0000142static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
143 uint64_t value)
Peter Maydelld4e6df62013-06-25 18:16:07 +0100144{
Peter Maydell375421c2015-02-05 13:37:22 +0000145 assert(ri->fieldoffset);
Peter Maydell67ed7712014-02-26 17:20:01 +0000146 if (cpreg_field_is_64bit(ri)) {
Peter Maydell22d9e1a2013-08-20 14:54:31 +0100147 CPREG_FIELD64(env, ri) = value;
148 } else {
149 CPREG_FIELD32(env, ri) = value;
150 }
Peter Maydelld4e6df62013-06-25 18:16:07 +0100151}
152
Fabian Aggeler11f136e2014-12-11 12:07:51 +0000153static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri)
154{
155 return (char *)env + ri->fieldoffset;
156}
157
Peter Maydell49a66192015-08-13 11:26:21 +0100158uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell721fae12013-06-25 18:16:07 +0100159{
Peter Maydell59a1c322014-02-20 10:35:54 +0000160 /* Raw read of a coprocessor register (as needed for migration, etc). */
Peter Maydell721fae12013-06-25 18:16:07 +0100161 if (ri->type & ARM_CP_CONST) {
Peter Maydell59a1c322014-02-20 10:35:54 +0000162 return ri->resetvalue;
Peter Maydell721fae12013-06-25 18:16:07 +0100163 } else if (ri->raw_readfn) {
Peter Maydell59a1c322014-02-20 10:35:54 +0000164 return ri->raw_readfn(env, ri);
Peter Maydell721fae12013-06-25 18:16:07 +0100165 } else if (ri->readfn) {
Peter Maydell59a1c322014-02-20 10:35:54 +0000166 return ri->readfn(env, ri);
Peter Maydell721fae12013-06-25 18:16:07 +0100167 } else {
Peter Maydell59a1c322014-02-20 10:35:54 +0000168 return raw_read(env, ri);
Peter Maydell721fae12013-06-25 18:16:07 +0100169 }
Peter Maydell721fae12013-06-25 18:16:07 +0100170}
171
Peter Maydell59a1c322014-02-20 10:35:54 +0000172static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri,
Peter Maydell7900e9f2014-02-20 10:35:54 +0000173 uint64_t v)
Peter Maydell721fae12013-06-25 18:16:07 +0100174{
175 /* Raw write of a coprocessor register (as needed for migration, etc).
Peter Maydell721fae12013-06-25 18:16:07 +0100176 * Note that constant registers are treated as write-ignored; the
177 * caller should check for success by whether a readback gives the
178 * value written.
179 */
180 if (ri->type & ARM_CP_CONST) {
Peter Maydell59a1c322014-02-20 10:35:54 +0000181 return;
Peter Maydell721fae12013-06-25 18:16:07 +0100182 } else if (ri->raw_writefn) {
Peter Maydellc4241c72014-02-20 10:35:54 +0000183 ri->raw_writefn(env, ri, v);
Peter Maydell721fae12013-06-25 18:16:07 +0100184 } else if (ri->writefn) {
Peter Maydellc4241c72014-02-20 10:35:54 +0000185 ri->writefn(env, ri, v);
Peter Maydell721fae12013-06-25 18:16:07 +0100186 } else {
Peter Crosthwaiteafb25302014-01-01 23:58:20 -0800187 raw_write(env, ri, v);
Peter Maydell721fae12013-06-25 18:16:07 +0100188 }
Peter Maydell721fae12013-06-25 18:16:07 +0100189}
190
Peter Maydell375421c2015-02-05 13:37:22 +0000191static bool raw_accessors_invalid(const ARMCPRegInfo *ri)
192{
193 /* Return true if the regdef would cause an assertion if you called
194 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a
195 * program bug for it not to have the NO_RAW flag).
196 * NB that returning false here doesn't necessarily mean that calling
197 * read/write_raw_cp_reg() is safe, because we can't distinguish "has
198 * read/write access functions which are safe for raw use" from "has
199 * read/write access functions which have side effects but has forgotten
200 * to provide raw access functions".
201 * The tests here line up with the conditions in read/write_raw_cp_reg()
202 * and assertions in raw_read()/raw_write().
203 */
204 if ((ri->type & ARM_CP_CONST) ||
205 ri->fieldoffset ||
206 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) {
207 return false;
208 }
209 return true;
210}
211
Peter Maydell721fae12013-06-25 18:16:07 +0100212bool write_cpustate_to_list(ARMCPU *cpu)
213{
214 /* Write the coprocessor state from cpu->env to the (index,value) list. */
215 int i;
216 bool ok = true;
217
218 for (i = 0; i < cpu->cpreg_array_len; i++) {
219 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
220 const ARMCPRegInfo *ri;
Peter Maydell59a1c322014-02-20 10:35:54 +0000221
Peter Maydell60322b32014-01-04 22:15:44 +0000222 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100223 if (!ri) {
224 ok = false;
225 continue;
226 }
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000227 if (ri->type & ARM_CP_NO_RAW) {
Peter Maydell721fae12013-06-25 18:16:07 +0100228 continue;
229 }
Peter Maydell59a1c322014-02-20 10:35:54 +0000230 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri);
Peter Maydell721fae12013-06-25 18:16:07 +0100231 }
232 return ok;
233}
234
235bool write_list_to_cpustate(ARMCPU *cpu)
236{
237 int i;
238 bool ok = true;
239
240 for (i = 0; i < cpu->cpreg_array_len; i++) {
241 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]);
242 uint64_t v = cpu->cpreg_values[i];
Peter Maydell721fae12013-06-25 18:16:07 +0100243 const ARMCPRegInfo *ri;
244
Peter Maydell60322b32014-01-04 22:15:44 +0000245 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100246 if (!ri) {
247 ok = false;
248 continue;
249 }
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000250 if (ri->type & ARM_CP_NO_RAW) {
Peter Maydell721fae12013-06-25 18:16:07 +0100251 continue;
252 }
253 /* Write value and confirm it reads back as written
254 * (to catch read-only registers and partially read-only
255 * registers where the incoming migration value doesn't match)
256 */
Peter Maydell59a1c322014-02-20 10:35:54 +0000257 write_raw_cp_reg(&cpu->env, ri, v);
258 if (read_raw_cp_reg(&cpu->env, ri) != v) {
Peter Maydell721fae12013-06-25 18:16:07 +0100259 ok = false;
260 }
261 }
262 return ok;
263}
264
265static void add_cpreg_to_list(gpointer key, gpointer opaque)
266{
267 ARMCPU *cpu = opaque;
268 uint64_t regidx;
269 const ARMCPRegInfo *ri;
270
271 regidx = *(uint32_t *)key;
Peter Maydell60322b32014-01-04 22:15:44 +0000272 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100273
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000274 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
Peter Maydell721fae12013-06-25 18:16:07 +0100275 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx);
276 /* The value array need not be initialized at this point */
277 cpu->cpreg_array_len++;
278 }
279}
280
281static void count_cpreg(gpointer key, gpointer opaque)
282{
283 ARMCPU *cpu = opaque;
284 uint64_t regidx;
285 const ARMCPRegInfo *ri;
286
287 regidx = *(uint32_t *)key;
Peter Maydell60322b32014-01-04 22:15:44 +0000288 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx);
Peter Maydell721fae12013-06-25 18:16:07 +0100289
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000290 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) {
Peter Maydell721fae12013-06-25 18:16:07 +0100291 cpu->cpreg_array_len++;
292 }
293}
294
295static gint cpreg_key_compare(gconstpointer a, gconstpointer b)
296{
Alvise Rigocbf239b2013-10-11 19:38:44 +0200297 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a);
298 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b);
Peter Maydell721fae12013-06-25 18:16:07 +0100299
Alvise Rigocbf239b2013-10-11 19:38:44 +0200300 if (aidx > bidx) {
301 return 1;
302 }
303 if (aidx < bidx) {
304 return -1;
305 }
306 return 0;
Peter Maydell721fae12013-06-25 18:16:07 +0100307}
308
309void init_cpreg_list(ARMCPU *cpu)
310{
311 /* Initialise the cpreg_tuples[] array based on the cp_regs hash.
312 * Note that we require cpreg_tuples[] to be sorted by key ID.
313 */
Markus Armbruster57b6d952015-06-02 14:56:22 +0100314 GList *keys;
Peter Maydell721fae12013-06-25 18:16:07 +0100315 int arraylen;
316
Markus Armbruster57b6d952015-06-02 14:56:22 +0100317 keys = g_hash_table_get_keys(cpu->cp_regs);
Peter Maydell721fae12013-06-25 18:16:07 +0100318 keys = g_list_sort(keys, cpreg_key_compare);
319
320 cpu->cpreg_array_len = 0;
321
322 g_list_foreach(keys, count_cpreg, cpu);
323
324 arraylen = cpu->cpreg_array_len;
325 cpu->cpreg_indexes = g_new(uint64_t, arraylen);
326 cpu->cpreg_values = g_new(uint64_t, arraylen);
327 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen);
328 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen);
329 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len;
330 cpu->cpreg_array_len = 0;
331
332 g_list_foreach(keys, add_cpreg_to_list, cpu);
333
334 assert(cpu->cpreg_array_len == arraylen);
335
336 g_list_free(keys);
337}
338
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +0100339/*
340 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but
341 * they are accessible when EL3 is using AArch64 regardless of EL3.NS.
342 *
343 * access_el3_aa32ns: Used to check AArch32 register views.
344 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views.
345 */
346static CPAccessResult access_el3_aa32ns(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +0000347 const ARMCPRegInfo *ri,
348 bool isread)
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +0100349{
350 bool secure = arm_is_secure_below_el3(env);
351
352 assert(!arm_el_is_aa64(env, 3));
353 if (secure) {
354 return CP_ACCESS_TRAP_UNCATEGORIZED;
355 }
356 return CP_ACCESS_OK;
357}
358
359static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +0000360 const ARMCPRegInfo *ri,
361 bool isread)
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +0100362{
363 if (!arm_el_is_aa64(env, 3)) {
Peter Maydell3f208fd2016-02-11 11:17:31 +0000364 return access_el3_aa32ns(env, ri, isread);
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +0100365 }
366 return CP_ACCESS_OK;
367}
368
Peter Maydell5513c3a2016-02-11 11:17:30 +0000369/* Some secure-only AArch32 registers trap to EL3 if used from
370 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts).
371 * Note that an access from Secure EL1 can only happen if EL3 is AArch64.
372 * We assume that the .access field is set to PL1_RW.
373 */
374static CPAccessResult access_trap_aa32s_el1(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +0000375 const ARMCPRegInfo *ri,
376 bool isread)
Peter Maydell5513c3a2016-02-11 11:17:30 +0000377{
378 if (arm_current_el(env) == 3) {
379 return CP_ACCESS_OK;
380 }
381 if (arm_is_secure_below_el3(env)) {
382 return CP_ACCESS_TRAP_EL3;
383 }
384 /* This will be EL1 NS and EL2 NS, which just UNDEF */
385 return CP_ACCESS_TRAP_UNCATEGORIZED;
386}
387
Peter Maydell187f6782016-02-18 14:16:15 +0000388/* Check for traps to "powerdown debug" registers, which are controlled
389 * by MDCR.TDOSA
390 */
391static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri,
392 bool isread)
393{
394 int el = arm_current_el(env);
395
396 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA)
397 && !arm_is_secure_below_el3(env)) {
398 return CP_ACCESS_TRAP_EL2;
399 }
400 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) {
401 return CP_ACCESS_TRAP_EL3;
402 }
403 return CP_ACCESS_OK;
404}
405
Peter Maydell91b0a232016-02-18 14:16:15 +0000406/* Check for traps to "debug ROM" registers, which are controlled
407 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3.
408 */
409static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri,
410 bool isread)
411{
412 int el = arm_current_el(env);
413
414 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA)
415 && !arm_is_secure_below_el3(env)) {
416 return CP_ACCESS_TRAP_EL2;
417 }
418 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
419 return CP_ACCESS_TRAP_EL3;
420 }
421 return CP_ACCESS_OK;
422}
423
Peter Maydelld6c8cf82016-02-18 14:16:15 +0000424/* Check for traps to general debug registers, which are controlled
425 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3.
426 */
427static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri,
428 bool isread)
429{
430 int el = arm_current_el(env);
431
432 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA)
433 && !arm_is_secure_below_el3(env)) {
434 return CP_ACCESS_TRAP_EL2;
435 }
436 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) {
437 return CP_ACCESS_TRAP_EL3;
438 }
439 return CP_ACCESS_OK;
440}
441
Peter Maydell1fce1ba2016-02-19 14:39:44 +0000442/* Check for traps to performance monitor registers, which are controlled
443 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3.
444 */
445static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri,
446 bool isread)
447{
448 int el = arm_current_el(env);
449
450 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
451 && !arm_is_secure_below_el3(env)) {
452 return CP_ACCESS_TRAP_EL2;
453 }
454 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
455 return CP_ACCESS_TRAP_EL3;
456 }
457 return CP_ACCESS_OK;
458}
459
Peter Maydellc4241c72014-02-20 10:35:54 +0000460static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
Peter Maydellc983fe62012-06-20 11:57:13 +0000461{
Andreas Färber00c8cb02013-09-04 02:19:44 +0200462 ARMCPU *cpu = arm_env_get_cpu(env);
463
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100464 raw_write(env, ri, value);
Andreas Färber00c8cb02013-09-04 02:19:44 +0200465 tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */
Peter Maydellc983fe62012-06-20 11:57:13 +0000466}
467
Peter Maydellc4241c72014-02-20 10:35:54 +0000468static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
Peter Maydell08de2072012-06-20 11:57:14 +0000469{
Andreas Färber00c8cb02013-09-04 02:19:44 +0200470 ARMCPU *cpu = arm_env_get_cpu(env);
471
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100472 if (raw_read(env, ri) != value) {
Peter Maydell08de2072012-06-20 11:57:14 +0000473 /* Unlike real hardware the qemu TLB uses virtual addresses,
474 * not modified virtual addresses, so this causes a TLB flush.
475 */
Andreas Färber00c8cb02013-09-04 02:19:44 +0200476 tlb_flush(CPU(cpu), 1);
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100477 raw_write(env, ri, value);
Peter Maydell08de2072012-06-20 11:57:14 +0000478 }
Peter Maydell08de2072012-06-20 11:57:14 +0000479}
Peter Maydellc4241c72014-02-20 10:35:54 +0000480
481static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri,
482 uint64_t value)
Peter Maydell08de2072012-06-20 11:57:14 +0000483{
Andreas Färber00c8cb02013-09-04 02:19:44 +0200484 ARMCPU *cpu = arm_env_get_cpu(env);
485
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100486 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU)
Peter Maydell014406b2014-04-15 19:18:45 +0100487 && !extended_addresses_enabled(env)) {
Peter Maydell08de2072012-06-20 11:57:14 +0000488 /* For VMSA (when not using the LPAE long descriptor page table
489 * format) this register includes the ASID, so do a TLB flush.
490 * For PMSA it is purely a process ID and no action is needed.
491 */
Andreas Färber00c8cb02013-09-04 02:19:44 +0200492 tlb_flush(CPU(cpu), 1);
Peter Maydell08de2072012-06-20 11:57:14 +0000493 }
Fabian Aggeler8d5c7732014-06-09 15:43:22 +0100494 raw_write(env, ri, value);
Peter Maydell08de2072012-06-20 11:57:14 +0000495}
496
Peter Maydellc4241c72014-02-20 10:35:54 +0000497static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri,
498 uint64_t value)
Peter Maydelld9298232012-06-20 11:57:16 +0000499{
500 /* Invalidate all (TLBIALL) */
Andreas Färber00c8cb02013-09-04 02:19:44 +0200501 ARMCPU *cpu = arm_env_get_cpu(env);
502
503 tlb_flush(CPU(cpu), 1);
Peter Maydelld9298232012-06-20 11:57:16 +0000504}
505
Peter Maydellc4241c72014-02-20 10:35:54 +0000506static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri,
507 uint64_t value)
Peter Maydelld9298232012-06-20 11:57:16 +0000508{
509 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */
Andreas Färber31b030d2013-09-04 01:29:02 +0200510 ARMCPU *cpu = arm_env_get_cpu(env);
511
512 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
Peter Maydelld9298232012-06-20 11:57:16 +0000513}
514
Peter Maydellc4241c72014-02-20 10:35:54 +0000515static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri,
516 uint64_t value)
Peter Maydelld9298232012-06-20 11:57:16 +0000517{
518 /* Invalidate by ASID (TLBIASID) */
Andreas Färber00c8cb02013-09-04 02:19:44 +0200519 ARMCPU *cpu = arm_env_get_cpu(env);
520
521 tlb_flush(CPU(cpu), value == 0);
Peter Maydelld9298232012-06-20 11:57:16 +0000522}
523
Peter Maydellc4241c72014-02-20 10:35:54 +0000524static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri,
525 uint64_t value)
Peter Maydelld9298232012-06-20 11:57:16 +0000526{
527 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */
Andreas Färber31b030d2013-09-04 01:29:02 +0200528 ARMCPU *cpu = arm_env_get_cpu(env);
529
530 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK);
Peter Maydelld9298232012-06-20 11:57:16 +0000531}
532
Peter Maydellfa439fc2014-09-12 14:06:50 +0100533/* IS variants of TLB operations must affect all cores */
534static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
535 uint64_t value)
536{
537 CPUState *other_cs;
538
539 CPU_FOREACH(other_cs) {
540 tlb_flush(other_cs, 1);
541 }
542}
543
544static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
545 uint64_t value)
546{
547 CPUState *other_cs;
548
549 CPU_FOREACH(other_cs) {
550 tlb_flush(other_cs, value == 0);
551 }
552}
553
554static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
555 uint64_t value)
556{
557 CPUState *other_cs;
558
559 CPU_FOREACH(other_cs) {
560 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
561 }
562}
563
564static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri,
565 uint64_t value)
566{
567 CPUState *other_cs;
568
569 CPU_FOREACH(other_cs) {
570 tlb_flush_page(other_cs, value & TARGET_PAGE_MASK);
571 }
572}
573
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000574static const ARMCPRegInfo cp_reginfo[] = {
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000575 /* Define the secure and non-secure FCSE identifier CP registers
576 * separately because there is no secure bank in V8 (no _EL3). This allows
577 * the secure register to be properly reset and migrated. There is also no
578 * v8 EL1 version of the register so the non-secure instance stands alone.
579 */
580 { .name = "FCSEIDR(NS)",
581 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
582 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
583 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100584 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000585 { .name = "FCSEIDR(S)",
586 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0,
587 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
588 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s),
589 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, },
590 /* Define the secure and non-secure context identifier CP registers
591 * separately because there is no secure bank in V8 (no _EL3). This allows
592 * the secure register to be properly reset and migrated. In the
593 * non-secure case, the 32-bit register will have reset and migration
594 * disabled during registration as it is handled by the 64-bit instance.
595 */
596 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH,
Peter Maydell014406b2014-04-15 19:18:45 +0100597 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +0000598 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS,
599 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]),
600 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
601 { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32,
602 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1,
603 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S,
604 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s),
Peter Maydelld4e6df62013-06-25 18:16:07 +0100605 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, },
Peter Maydell9449fdf2014-04-15 19:18:47 +0100606 REGINFO_SENTINEL
607};
608
609static const ARMCPRegInfo not_v8_cp_reginfo[] = {
610 /* NB: Some of these registers exist in v8 but with more precise
611 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]).
612 */
613 /* MMU Domain access control / MPU write buffer control */
Fabian Aggeler0c17d682014-12-11 12:07:51 +0000614 { .name = "DACR",
615 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY,
616 .access = PL1_RW, .resetvalue = 0,
617 .writefn = dacr_write, .raw_writefn = raw_write,
618 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
619 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +0100620 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs.
621 * For v6 and v5, these mappings are overly broad.
Peter Maydell4fdd17d2012-06-20 11:57:15 +0000622 */
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +0100623 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0,
624 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
625 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1,
626 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
627 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4,
628 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
629 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8,
Peter Maydell4fdd17d2012-06-20 11:57:15 +0000630 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP },
Peter Maydellc4804212012-06-20 11:57:17 +0000631 /* Cache maintenance ops; some of this space may be overridden later. */
632 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
633 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
634 .type = ARM_CP_NOP | ARM_CP_OVERRIDE },
Peter Maydelle9aa6c22012-06-20 11:57:09 +0000635 REGINFO_SENTINEL
636};
637
Peter Maydell7d57f402012-06-20 11:57:11 +0000638static const ARMCPRegInfo not_v6_cp_reginfo[] = {
639 /* Not all pre-v6 cores implemented this WFI, so this is slightly
640 * over-broad.
641 */
642 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2,
643 .access = PL1_W, .type = ARM_CP_WFI },
644 REGINFO_SENTINEL
645};
646
647static const ARMCPRegInfo not_v7_cp_reginfo[] = {
648 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which
649 * is UNPREDICTABLE; we choose to NOP as most implementations do).
650 */
651 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
652 .access = PL1_W, .type = ARM_CP_WFI },
Peter Maydell34f90522012-06-20 11:57:18 +0000653 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice
654 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and
655 * OMAPCP will override this space.
656 */
657 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0,
658 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data),
659 .resetvalue = 0 },
660 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1,
661 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn),
662 .resetvalue = 0 },
Peter Maydell776d4e52012-06-20 11:57:19 +0000663 /* v6 doesn't have the cache ID registers but Linux reads them anyway */
664 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000665 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +0100666 .resetvalue = 0 },
Peter Maydell50300692014-08-19 18:56:25 +0100667 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR;
668 * implementing it as RAZ means the "debug architecture version" bits
669 * will read as a reserved value, which should cause Linux to not try
670 * to use the debug hardware.
671 */
672 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
673 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell995939a2014-09-12 14:06:50 +0100674 /* MMU TLB control. Note that the wildcarding means we cover not just
675 * the unified TLB ops but also the dside/iside/inner-shareable variants.
676 */
677 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY,
678 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000679 .type = ARM_CP_NO_RAW },
Peter Maydell995939a2014-09-12 14:06:50 +0100680 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY,
681 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000682 .type = ARM_CP_NO_RAW },
Peter Maydell995939a2014-09-12 14:06:50 +0100683 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY,
684 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000685 .type = ARM_CP_NO_RAW },
Peter Maydell995939a2014-09-12 14:06:50 +0100686 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY,
687 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +0000688 .type = ARM_CP_NO_RAW },
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +0100689 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2,
690 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP },
691 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2,
692 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP },
Peter Maydell7d57f402012-06-20 11:57:11 +0000693 REGINFO_SENTINEL
694};
695
Peter Maydellc4241c72014-02-20 10:35:54 +0000696static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri,
697 uint64_t value)
Peter Maydell2771db22012-06-20 11:57:18 +0000698{
Fabian Aggelerf0aff252014-05-27 17:09:49 +0100699 uint32_t mask = 0;
700
701 /* In ARMv8 most bits of CPACR_EL1 are RES0. */
702 if (!arm_feature(env, ARM_FEATURE_V8)) {
703 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI.
704 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP.
705 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell.
706 */
707 if (arm_feature(env, ARM_FEATURE_VFP)) {
708 /* VFP coprocessor: cp10 & cp11 [23:20] */
709 mask |= (1 << 31) | (1 << 30) | (0xf << 20);
710
711 if (!arm_feature(env, ARM_FEATURE_NEON)) {
712 /* ASEDIS [31] bit is RAO/WI */
713 value |= (1 << 31);
714 }
715
716 /* VFPv3 and upwards with NEON implement 32 double precision
717 * registers (D0-D31).
718 */
719 if (!arm_feature(env, ARM_FEATURE_NEON) ||
720 !arm_feature(env, ARM_FEATURE_VFP3)) {
721 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */
722 value |= (1 << 30);
723 }
724 }
725 value &= mask;
Peter Maydell2771db22012-06-20 11:57:18 +0000726 }
Sergey Fedorov7ebd5f22015-04-26 16:49:25 +0100727 env->cp15.cpacr_el1 = value;
Peter Maydell2771db22012-06-20 11:57:18 +0000728}
729
Peter Maydell3f208fd2016-02-11 11:17:31 +0000730static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
731 bool isread)
Greg Bellowsc6f19162015-05-29 11:28:52 +0100732{
733 if (arm_feature(env, ARM_FEATURE_V8)) {
734 /* Check if CPACR accesses are to be trapped to EL2 */
735 if (arm_current_el(env) == 1 &&
736 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) {
737 return CP_ACCESS_TRAP_EL2;
738 /* Check if CPACR accesses are to be trapped to EL3 */
739 } else if (arm_current_el(env) < 3 &&
740 (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
741 return CP_ACCESS_TRAP_EL3;
742 }
743 }
744
745 return CP_ACCESS_OK;
746}
747
Peter Maydell3f208fd2016-02-11 11:17:31 +0000748static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri,
749 bool isread)
Greg Bellowsc6f19162015-05-29 11:28:52 +0100750{
751 /* Check if CPTR accesses are set to trap to EL3 */
752 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) {
753 return CP_ACCESS_TRAP_EL3;
754 }
755
756 return CP_ACCESS_OK;
757}
758
Peter Maydell7d57f402012-06-20 11:57:11 +0000759static const ARMCPRegInfo v6_cp_reginfo[] = {
760 /* prefetch by MVA in v6, NOP in v7 */
761 { .name = "MVA_prefetch",
762 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1,
763 .access = PL1_W, .type = ARM_CP_NOP },
Sergey Sorokin6df99de2015-10-16 11:14:52 +0100764 /* We need to break the TB after ISB to execute self-modifying code
765 * correctly and also to take any pending interrupts immediately.
766 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag.
767 */
Peter Maydell7d57f402012-06-20 11:57:11 +0000768 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4,
Sergey Sorokin6df99de2015-10-16 11:14:52 +0100769 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore },
Peter Maydell091fd172012-07-12 10:58:36 +0000770 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4,
Peter Maydell7d57f402012-06-20 11:57:11 +0000771 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell091fd172012-07-12 10:58:36 +0000772 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5,
Peter Maydell7d57f402012-06-20 11:57:11 +0000773 .access = PL0_W, .type = ARM_CP_NOP },
Peter Maydell06d76f32012-06-20 11:57:17 +0000774 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2,
Rob Herring6cd8a262014-04-15 19:18:42 +0100775 .access = PL1_RW,
Fabian Aggelerb848ce22014-12-11 12:07:51 +0000776 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s),
777 offsetof(CPUARMState, cp15.ifar_ns) },
Peter Maydell06d76f32012-06-20 11:57:17 +0000778 .resetvalue = 0, },
779 /* Watchpoint Fault Address Register : should actually only be present
780 * for 1136, 1176, 11MPCore.
781 */
782 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1,
783 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, },
Peter Maydell34222fb2014-02-26 17:20:06 +0000784 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3,
Greg Bellowsc6f19162015-05-29 11:28:52 +0100785 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access,
Sergey Fedorov7ebd5f22015-04-26 16:49:25 +0100786 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1),
Peter Maydell2771db22012-06-20 11:57:18 +0000787 .resetvalue = 0, .writefn = cpacr_write },
Peter Maydell7d57f402012-06-20 11:57:11 +0000788 REGINFO_SENTINEL
789};
790
Peter Maydell3f208fd2016-02-11 11:17:31 +0000791static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri,
792 bool isread)
Peter Maydell200ac0e2012-06-20 11:57:12 +0000793{
Stefan Weil3b163b02014-03-07 19:48:59 +0100794 /* Performance monitor registers user accessibility is controlled
Peter Maydell1fce1ba2016-02-19 14:39:44 +0000795 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable
796 * trapping to EL2 or EL3 for other accesses.
Peter Maydell200ac0e2012-06-20 11:57:12 +0000797 */
Peter Maydell1fce1ba2016-02-19 14:39:44 +0000798 int el = arm_current_el(env);
799
800 if (el == 0 && !env->cp15.c9_pmuserenr) {
Peter Maydellfcd25202014-02-20 10:35:52 +0000801 return CP_ACCESS_TRAP;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000802 }
Peter Maydell1fce1ba2016-02-19 14:39:44 +0000803 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM)
804 && !arm_is_secure_below_el3(env)) {
805 return CP_ACCESS_TRAP_EL2;
806 }
807 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) {
808 return CP_ACCESS_TRAP_EL3;
809 }
810
Peter Maydellfcd25202014-02-20 10:35:52 +0000811 return CP_ACCESS_OK;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000812}
813
Alistair Francis7c2cb422014-03-10 14:56:28 +0000814#ifndef CONFIG_USER_ONLY
Alistair Francis87124fd2014-08-29 15:00:29 +0100815
816static inline bool arm_ccnt_enabled(CPUARMState *env)
817{
818 /* This does not support checking PMCCFILTR_EL0 register */
819
820 if (!(env->cp15.c9_pmcr & PMCRE)) {
821 return false;
822 }
823
824 return true;
825}
826
Alistair Francisec7b4ce2014-08-29 15:00:29 +0100827void pmccntr_sync(CPUARMState *env)
828{
829 uint64_t temp_ticks;
830
Laurent Vivier352c98e2015-08-25 17:09:36 +0200831 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
832 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
Alistair Francisec7b4ce2014-08-29 15:00:29 +0100833
834 if (env->cp15.c9_pmcr & PMCRD) {
835 /* Increment once every 64 processor clock cycles */
836 temp_ticks /= 64;
837 }
838
839 if (arm_ccnt_enabled(env)) {
840 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt;
841 }
842}
843
Peter Maydellc4241c72014-02-20 10:35:54 +0000844static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
845 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +0000846{
Alistair Francis942a1552014-08-29 15:00:30 +0100847 pmccntr_sync(env);
Alistair Francis7c2cb422014-03-10 14:56:28 +0000848
849 if (value & PMCRC) {
850 /* The counter has been reset */
851 env->cp15.c15_ccnt = 0;
852 }
853
Peter Maydell200ac0e2012-06-20 11:57:12 +0000854 /* only the DP, X, D and E bits are writable */
855 env->cp15.c9_pmcr &= ~0x39;
856 env->cp15.c9_pmcr |= (value & 0x39);
Alistair Francis7c2cb422014-03-10 14:56:28 +0000857
Alistair Francis942a1552014-08-29 15:00:30 +0100858 pmccntr_sync(env);
Peter Maydell200ac0e2012-06-20 11:57:12 +0000859}
860
Alistair Francis7c2cb422014-03-10 14:56:28 +0000861static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri)
862{
Alistair Francisc92c0682014-08-29 15:00:29 +0100863 uint64_t total_ticks;
Alistair Francis7c2cb422014-03-10 14:56:28 +0000864
Alistair Francis942a1552014-08-29 15:00:30 +0100865 if (!arm_ccnt_enabled(env)) {
Alistair Francis7c2cb422014-03-10 14:56:28 +0000866 /* Counter is disabled, do not change value */
867 return env->cp15.c15_ccnt;
868 }
869
Laurent Vivier352c98e2015-08-25 17:09:36 +0200870 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
871 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
Alistair Francis7c2cb422014-03-10 14:56:28 +0000872
873 if (env->cp15.c9_pmcr & PMCRD) {
874 /* Increment once every 64 processor clock cycles */
875 total_ticks /= 64;
876 }
877 return total_ticks - env->cp15.c15_ccnt;
878}
879
880static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri,
881 uint64_t value)
882{
Alistair Francisc92c0682014-08-29 15:00:29 +0100883 uint64_t total_ticks;
Alistair Francis7c2cb422014-03-10 14:56:28 +0000884
Alistair Francis942a1552014-08-29 15:00:30 +0100885 if (!arm_ccnt_enabled(env)) {
Alistair Francis7c2cb422014-03-10 14:56:28 +0000886 /* Counter is disabled, set the absolute value */
887 env->cp15.c15_ccnt = value;
888 return;
889 }
890
Laurent Vivier352c98e2015-08-25 17:09:36 +0200891 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL),
892 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND);
Alistair Francis7c2cb422014-03-10 14:56:28 +0000893
894 if (env->cp15.c9_pmcr & PMCRD) {
895 /* Increment once every 64 processor clock cycles */
896 total_ticks /= 64;
897 }
898 env->cp15.c15_ccnt = total_ticks - value;
899}
Peter Crosthwaite421c7eb2014-08-29 15:00:29 +0100900
901static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri,
902 uint64_t value)
903{
904 uint64_t cur_val = pmccntr_read(env, NULL);
905
906 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value));
907}
908
Alistair Francisec7b4ce2014-08-29 15:00:29 +0100909#else /* CONFIG_USER_ONLY */
910
911void pmccntr_sync(CPUARMState *env)
912{
913}
914
Alistair Francis7c2cb422014-03-10 14:56:28 +0000915#endif
916
Alistair Francis06146012014-08-29 15:00:30 +0100917static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri,
918 uint64_t value)
919{
920 pmccntr_sync(env);
921 env->cp15.pmccfiltr_el0 = value & 0x7E000000;
922 pmccntr_sync(env);
923}
924
Peter Maydellc4241c72014-02-20 10:35:54 +0000925static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
Peter Maydell200ac0e2012-06-20 11:57:12 +0000926 uint64_t value)
927{
Peter Maydell200ac0e2012-06-20 11:57:12 +0000928 value &= (1 << 31);
929 env->cp15.c9_pmcnten |= value;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000930}
931
Peter Maydellc4241c72014-02-20 10:35:54 +0000932static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
933 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +0000934{
Peter Maydell200ac0e2012-06-20 11:57:12 +0000935 value &= (1 << 31);
936 env->cp15.c9_pmcnten &= ~value;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000937}
938
Peter Maydellc4241c72014-02-20 10:35:54 +0000939static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
940 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +0000941{
Peter Maydell200ac0e2012-06-20 11:57:12 +0000942 env->cp15.c9_pmovsr &= ~value;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000943}
944
Peter Maydellc4241c72014-02-20 10:35:54 +0000945static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri,
946 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +0000947{
Peter Maydell200ac0e2012-06-20 11:57:12 +0000948 env->cp15.c9_pmxevtyper = value & 0xff;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000949}
950
Peter Maydellc4241c72014-02-20 10:35:54 +0000951static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri,
Peter Maydell200ac0e2012-06-20 11:57:12 +0000952 uint64_t value)
953{
954 env->cp15.c9_pmuserenr = value & 1;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000955}
956
Peter Maydellc4241c72014-02-20 10:35:54 +0000957static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri,
958 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +0000959{
960 /* We have no event counters so only the C bit can be changed */
961 value &= (1 << 31);
962 env->cp15.c9_pminten |= value;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000963}
964
Peter Maydellc4241c72014-02-20 10:35:54 +0000965static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri,
966 uint64_t value)
Peter Maydell200ac0e2012-06-20 11:57:12 +0000967{
968 value &= (1 << 31);
969 env->cp15.c9_pminten &= ~value;
Peter Maydell200ac0e2012-06-20 11:57:12 +0000970}
971
Peter Maydellc4241c72014-02-20 10:35:54 +0000972static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri,
973 uint64_t value)
Nathan Rossi86411362013-10-25 15:44:38 +0100974{
Peter Maydella505d7f2014-02-26 17:20:04 +0000975 /* Note that even though the AArch64 view of this register has bits
976 * [10:0] all RES0 we can only mask the bottom 5, to comply with the
977 * architectural requirements for bits which are RES0 only in some
978 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7
979 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.)
980 */
Edgar E. Iglesias855ea662014-05-27 17:09:54 +0100981 raw_write(env, ri, value & ~0x1FULL);
Nathan Rossi86411362013-10-25 15:44:38 +0100982}
983
Edgar E. Iglesias64e0e2d2014-09-29 18:48:49 +0100984static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
985{
986 /* We only mask off bits that are RES0 both for AArch64 and AArch32.
987 * For bits that vary between AArch32/64, code needs to check the
988 * current execution mode before directly using the feature bit.
989 */
990 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK;
991
992 if (!arm_feature(env, ARM_FEATURE_EL2)) {
993 valid_mask &= ~SCR_HCE;
994
995 /* On ARMv7, SMD (or SCD as it is called in v7) is only
996 * supported if EL2 exists. The bit is UNK/SBZP when
997 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero
998 * when EL2 is unavailable.
Greg Bellows4eb27642015-04-26 16:49:26 +0100999 * On ARMv8, this bit is always available.
Edgar E. Iglesias64e0e2d2014-09-29 18:48:49 +01001000 */
Greg Bellows4eb27642015-04-26 16:49:26 +01001001 if (arm_feature(env, ARM_FEATURE_V7) &&
1002 !arm_feature(env, ARM_FEATURE_V8)) {
Edgar E. Iglesias64e0e2d2014-09-29 18:48:49 +01001003 valid_mask &= ~SCR_SMD;
1004 }
1005 }
1006
1007 /* Clear all-context RES0 bits. */
1008 value &= valid_mask;
1009 raw_write(env, ri, value);
1010}
1011
Peter Maydellc4241c72014-02-20 10:35:54 +00001012static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell776d4e52012-06-20 11:57:19 +00001013{
1014 ARMCPU *cpu = arm_env_get_cpu(env);
Fabian Aggelerb85a1fd2014-12-11 12:07:50 +00001015
1016 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR
1017 * bank
1018 */
1019 uint32_t index = A32_BANKED_REG_GET(env, csselr,
1020 ri->secure & ARM_CP_SECSTATE_S);
1021
1022 return cpu->ccsidr[index];
Peter Maydell776d4e52012-06-20 11:57:19 +00001023}
1024
Peter Maydellc4241c72014-02-20 10:35:54 +00001025static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1026 uint64_t value)
Peter Maydell776d4e52012-06-20 11:57:19 +00001027{
Fabian Aggeler8d5c7732014-06-09 15:43:22 +01001028 raw_write(env, ri, value & 0xf);
Peter Maydell776d4e52012-06-20 11:57:19 +00001029}
1030
Peter Maydell1090b9c2014-04-15 19:18:46 +01001031static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri)
1032{
1033 CPUState *cs = ENV_GET_CPU(env);
1034 uint64_t ret = 0;
1035
1036 if (cs->interrupt_request & CPU_INTERRUPT_HARD) {
1037 ret |= CPSR_I;
1038 }
1039 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) {
1040 ret |= CPSR_F;
1041 }
1042 /* External aborts are not possible in QEMU so A bit is always clear */
1043 return ret;
1044}
1045
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001046static const ARMCPRegInfo v7_cp_reginfo[] = {
Peter Maydell7d57f402012-06-20 11:57:11 +00001047 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */
1048 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4,
1049 .access = PL1_W, .type = ARM_CP_NOP },
Peter Maydell200ac0e2012-06-20 11:57:12 +00001050 /* Performance monitors are implementation defined in v7,
1051 * but with an ARM recommended set of registers, which we
1052 * follow (although we don't actually implement any counters)
1053 *
1054 * Performance registers fall into three categories:
1055 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR)
1056 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR)
1057 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others)
1058 * For the cases controlled by PMUSERENR we must set .access to PL0_RW
1059 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn.
1060 */
1061 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001062 .access = PL0_RW, .type = ARM_CP_ALIAS,
Alistair Francis85214662014-08-29 15:00:29 +01001063 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
Peter Maydellfcd25202014-02-20 10:35:52 +00001064 .writefn = pmcntenset_write,
1065 .accessfn = pmreg_access,
1066 .raw_writefn = raw_write },
Alistair Francis85214662014-08-29 15:00:29 +01001067 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64,
1068 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1,
1069 .access = PL0_RW, .accessfn = pmreg_access,
1070 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0,
1071 .writefn = pmcntenset_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +00001072 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2,
Alistair Francis85214662014-08-29 15:00:29 +01001073 .access = PL0_RW,
1074 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten),
Peter Maydellfcd25202014-02-20 10:35:52 +00001075 .accessfn = pmreg_access,
1076 .writefn = pmcntenclr_write,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001077 .type = ARM_CP_ALIAS },
Alistair Francis85214662014-08-29 15:00:29 +01001078 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64,
1079 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2,
1080 .access = PL0_RW, .accessfn = pmreg_access,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001081 .type = ARM_CP_ALIAS,
Alistair Francis85214662014-08-29 15:00:29 +01001082 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten),
1083 .writefn = pmcntenclr_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +00001084 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3,
1085 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
Peter Maydellfcd25202014-02-20 10:35:52 +00001086 .accessfn = pmreg_access,
1087 .writefn = pmovsr_write,
1088 .raw_writefn = raw_write },
Alistair Francis978364f2016-02-18 14:16:17 +00001089 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64,
1090 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3,
1091 .access = PL0_RW, .accessfn = pmreg_access,
1092 .type = ARM_CP_ALIAS,
1093 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr),
1094 .writefn = pmovsr_write,
1095 .raw_writefn = raw_write },
Peter Maydellfcd25202014-02-20 10:35:52 +00001096 /* Unimplemented so WI. */
Peter Maydell200ac0e2012-06-20 11:57:12 +00001097 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4,
Peter Maydellfcd25202014-02-20 10:35:52 +00001098 .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP },
Peter Maydell200ac0e2012-06-20 11:57:12 +00001099 /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE.
Peter Maydellfcd25202014-02-20 10:35:52 +00001100 * We choose to RAZ/WI.
Peter Maydell200ac0e2012-06-20 11:57:12 +00001101 */
1102 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5,
Peter Maydellfcd25202014-02-20 10:35:52 +00001103 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1104 .accessfn = pmreg_access },
Alistair Francis7c2cb422014-03-10 14:56:28 +00001105#ifndef CONFIG_USER_ONLY
Peter Maydell200ac0e2012-06-20 11:57:12 +00001106 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0,
Alistair Francis7c2cb422014-03-10 14:56:28 +00001107 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO,
Peter Crosthwaite421c7eb2014-08-29 15:00:29 +01001108 .readfn = pmccntr_read, .writefn = pmccntr_write32,
Peter Maydellfcd25202014-02-20 10:35:52 +00001109 .accessfn = pmreg_access },
Alistair Francis85214662014-08-29 15:00:29 +01001110 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64,
1111 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0,
1112 .access = PL0_RW, .accessfn = pmreg_access,
1113 .type = ARM_CP_IO,
1114 .readfn = pmccntr_read, .writefn = pmccntr_write, },
Alistair Francis7c2cb422014-03-10 14:56:28 +00001115#endif
Alistair Francis85214662014-08-29 15:00:29 +01001116 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64,
1117 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7,
Alistair Francis06146012014-08-29 15:00:30 +01001118 .writefn = pmccfiltr_write,
Alistair Francis85214662014-08-29 15:00:29 +01001119 .access = PL0_RW, .accessfn = pmreg_access,
1120 .type = ARM_CP_IO,
1121 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0),
1122 .resetvalue = 0, },
Peter Maydell200ac0e2012-06-20 11:57:12 +00001123 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1,
1124 .access = PL0_RW,
1125 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper),
Peter Maydellfcd25202014-02-20 10:35:52 +00001126 .accessfn = pmreg_access, .writefn = pmxevtyper_write,
1127 .raw_writefn = raw_write },
1128 /* Unimplemented, RAZ/WI. */
Peter Maydell200ac0e2012-06-20 11:57:12 +00001129 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2,
Peter Maydellfcd25202014-02-20 10:35:52 +00001130 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0,
1131 .accessfn = pmreg_access },
Peter Maydell200ac0e2012-06-20 11:57:12 +00001132 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0,
Peter Maydell1fce1ba2016-02-19 14:39:44 +00001133 .access = PL0_R | PL1_RW, .accessfn = access_tpm,
Peter Maydell200ac0e2012-06-20 11:57:12 +00001134 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1135 .resetvalue = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001136 .writefn = pmuserenr_write, .raw_writefn = raw_write },
Alistair Francis8a83ffc2016-02-18 14:16:17 +00001137 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64,
1138 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0,
Peter Maydell1fce1ba2016-02-19 14:39:44 +00001139 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
Alistair Francis8a83ffc2016-02-18 14:16:17 +00001140 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr),
1141 .resetvalue = 0,
1142 .writefn = pmuserenr_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +00001143 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1,
Peter Maydell1fce1ba2016-02-19 14:39:44 +00001144 .access = PL1_RW, .accessfn = access_tpm,
Peter Maydell200ac0e2012-06-20 11:57:12 +00001145 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1146 .resetvalue = 0,
Peter Maydelld4e6df62013-06-25 18:16:07 +01001147 .writefn = pmintenset_write, .raw_writefn = raw_write },
Peter Maydell200ac0e2012-06-20 11:57:12 +00001148 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2,
Peter Maydell1fce1ba2016-02-19 14:39:44 +00001149 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
Peter Maydell200ac0e2012-06-20 11:57:12 +00001150 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
Sergey Fedorovb061a822015-06-19 14:17:44 +01001151 .writefn = pmintenclr_write, },
Alistair Francis978364f2016-02-18 14:16:17 +00001152 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64,
1153 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2,
Peter Maydell1fce1ba2016-02-19 14:39:44 +00001154 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS,
Alistair Francis978364f2016-02-18 14:16:17 +00001155 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten),
1156 .writefn = pmintenclr_write },
Peter Maydella505d7f2014-02-26 17:20:04 +00001157 { .name = "VBAR", .state = ARM_CP_STATE_BOTH,
1158 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0,
Nathan Rossi86411362013-10-25 15:44:38 +01001159 .access = PL1_RW, .writefn = vbar_write,
Greg Bellowsfb6c91b2014-12-11 12:07:52 +00001160 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s),
1161 offsetof(CPUARMState, cp15.vbar_ns) },
Nathan Rossi86411362013-10-25 15:44:38 +01001162 .resetvalue = 0 },
Peter Maydell7da845b2014-02-26 17:20:01 +00001163 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH,
1164 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001165 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW },
Peter Maydell7da845b2014-02-26 17:20:01 +00001166 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH,
1167 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0,
Fabian Aggelerb85a1fd2014-12-11 12:07:50 +00001168 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0,
1169 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s),
1170 offsetof(CPUARMState, cp15.csselr_ns) } },
Peter Maydell776d4e52012-06-20 11:57:19 +00001171 /* Auxiliary ID register: this actually has an IMPDEF value but for now
1172 * just RAZ for all cores:
1173 */
Peter Maydell0ff644a2014-04-15 19:18:45 +01001174 { .name = "AIDR", .state = ARM_CP_STATE_BOTH,
1175 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7,
Peter Maydell776d4e52012-06-20 11:57:19 +00001176 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellf32cdad2014-04-15 19:18:47 +01001177 /* Auxiliary fault status registers: these also are IMPDEF, and we
1178 * choose to RAZ/WI for all cores.
1179 */
1180 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH,
1181 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0,
1182 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
1183 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH,
1184 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1,
1185 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydellb0fe2422014-02-26 17:20:03 +00001186 /* MAIR can just read-as-written because we don't implement caches
1187 * and so don't need to care about memory attributes.
1188 */
1189 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64,
1190 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0,
Greg Bellowsbe693c82014-12-11 12:07:52 +00001191 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]),
Peter Maydellb0fe2422014-02-26 17:20:03 +00001192 .resetvalue = 0 },
Peter Maydell4cfb8ad2015-08-25 15:45:07 +01001193 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64,
1194 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0,
1195 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]),
1196 .resetvalue = 0 },
Peter Maydellb0fe2422014-02-26 17:20:03 +00001197 /* For non-long-descriptor page tables these are PRRR and NMRR;
1198 * regardless they still act as reads-as-written for QEMU.
Peter Maydellb0fe2422014-02-26 17:20:03 +00001199 */
Stefan Weil1281f8e2015-01-03 14:41:38 +01001200 /* MAIR0/1 are defined separately from their 64-bit counterpart which
Greg Bellowsbe693c82014-12-11 12:07:52 +00001201 * allows them to assign the correct fieldoffset based on the endianness
1202 * handled in the field definitions.
1203 */
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +01001204 { .name = "MAIR0", .state = ARM_CP_STATE_AA32,
Peter Maydellb0fe2422014-02-26 17:20:03 +00001205 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW,
Greg Bellowsbe693c82014-12-11 12:07:52 +00001206 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s),
1207 offsetof(CPUARMState, cp15.mair0_ns) },
Peter Maydellb0fe2422014-02-26 17:20:03 +00001208 .resetfn = arm_cp_reset_ignore },
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +01001209 { .name = "MAIR1", .state = ARM_CP_STATE_AA32,
Peter Maydellb0fe2422014-02-26 17:20:03 +00001210 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW,
Greg Bellowsbe693c82014-12-11 12:07:52 +00001211 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s),
1212 offsetof(CPUARMState, cp15.mair1_ns) },
Peter Maydellb0fe2422014-02-26 17:20:03 +00001213 .resetfn = arm_cp_reset_ignore },
Peter Maydell1090b9c2014-04-15 19:18:46 +01001214 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH,
1215 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001216 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read },
Peter Maydell995939a2014-09-12 14:06:50 +01001217 /* 32 bit ITLB invalidates */
1218 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001219 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001220 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001221 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001222 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001223 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001224 /* 32 bit DTLB invalidates */
1225 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001226 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001227 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001228 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001229 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001230 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001231 /* 32 bit TLB invalidates */
1232 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001233 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001234 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001235 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001236 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001237 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001238 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001239 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001240 REGINFO_SENTINEL
1241};
1242
1243static const ARMCPRegInfo v7mp_cp_reginfo[] = {
1244 /* 32 bit TLB invalidates, Inner Shareable */
1245 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001246 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001247 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001248 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001249 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001250 .type = ARM_CP_NO_RAW, .access = PL1_W,
Peter Maydellfa439fc2014-09-12 14:06:50 +01001251 .writefn = tlbiasid_is_write },
Peter Maydell995939a2014-09-12 14:06:50 +01001252 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001253 .type = ARM_CP_NO_RAW, .access = PL1_W,
Peter Maydellfa439fc2014-09-12 14:06:50 +01001254 .writefn = tlbimvaa_is_write },
Peter Maydelle9aa6c22012-06-20 11:57:09 +00001255 REGINFO_SENTINEL
1256};
1257
Peter Maydellc4241c72014-02-20 10:35:54 +00001258static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri,
1259 uint64_t value)
Peter Maydellc326b972012-06-20 11:57:10 +00001260{
1261 value &= 1;
1262 env->teecr = value;
Peter Maydellc326b972012-06-20 11:57:10 +00001263}
1264
Peter Maydell3f208fd2016-02-11 11:17:31 +00001265static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri,
1266 bool isread)
Peter Maydellc326b972012-06-20 11:57:10 +00001267{
Greg Bellowsdcbff192014-10-24 12:19:14 +01001268 if (arm_current_el(env) == 0 && (env->teecr & 1)) {
Peter Maydell92611c02014-02-20 10:35:53 +00001269 return CP_ACCESS_TRAP;
Peter Maydellc326b972012-06-20 11:57:10 +00001270 }
Peter Maydell92611c02014-02-20 10:35:53 +00001271 return CP_ACCESS_OK;
Peter Maydellc326b972012-06-20 11:57:10 +00001272}
1273
1274static const ARMCPRegInfo t2ee_cp_reginfo[] = {
1275 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0,
1276 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr),
1277 .resetvalue = 0,
1278 .writefn = teecr_write },
1279 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0,
1280 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr),
Peter Maydell92611c02014-02-20 10:35:53 +00001281 .accessfn = teehbr_access, .resetvalue = 0 },
Peter Maydellc326b972012-06-20 11:57:10 +00001282 REGINFO_SENTINEL
1283};
1284
Peter Maydell4d31c592012-06-20 11:57:11 +00001285static const ARMCPRegInfo v6k_cp_reginfo[] = {
Peter Maydelle4fe8302014-01-04 22:15:45 +00001286 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64,
1287 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0,
1288 .access = PL0_RW,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00001289 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 },
Peter Maydell4d31c592012-06-20 11:57:11 +00001290 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2,
1291 .access = PL0_RW,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00001292 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s),
1293 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) },
Peter Maydelle4fe8302014-01-04 22:15:45 +00001294 .resetfn = arm_cp_reset_ignore },
1295 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64,
1296 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0,
1297 .access = PL0_R|PL1_W,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00001298 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]),
1299 .resetvalue = 0},
Peter Maydell4d31c592012-06-20 11:57:11 +00001300 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3,
1301 .access = PL0_R|PL1_W,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00001302 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s),
1303 offsetoflow32(CPUARMState, cp15.tpidruro_ns) },
Peter Maydelle4fe8302014-01-04 22:15:45 +00001304 .resetfn = arm_cp_reset_ignore },
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00001305 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64,
Peter Maydelle4fe8302014-01-04 22:15:45 +00001306 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0,
Peter Maydell4d31c592012-06-20 11:57:11 +00001307 .access = PL1_RW,
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00001308 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 },
1309 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4,
1310 .access = PL1_RW,
1311 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s),
1312 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) },
1313 .resetvalue = 0 },
Peter Maydell4d31c592012-06-20 11:57:11 +00001314 REGINFO_SENTINEL
1315};
1316
Peter Maydell55d284a2013-08-20 14:54:31 +01001317#ifndef CONFIG_USER_ONLY
1318
Peter Maydell3f208fd2016-02-11 11:17:31 +00001319static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri,
1320 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00001321{
Peter Maydell75502672016-02-18 14:16:15 +00001322 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero.
1323 * Writable only at the highest implemented exception level.
1324 */
1325 int el = arm_current_el(env);
1326
1327 switch (el) {
1328 case 0:
1329 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) {
1330 return CP_ACCESS_TRAP;
1331 }
1332 break;
1333 case 1:
1334 if (!isread && ri->state == ARM_CP_STATE_AA32 &&
1335 arm_is_secure_below_el3(env)) {
1336 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */
1337 return CP_ACCESS_TRAP_UNCATEGORIZED;
1338 }
1339 break;
1340 case 2:
1341 case 3:
1342 break;
Peter Maydell00108f22014-02-20 10:35:53 +00001343 }
Peter Maydell75502672016-02-18 14:16:15 +00001344
1345 if (!isread && el < arm_highest_el(env)) {
1346 return CP_ACCESS_TRAP_UNCATEGORIZED;
1347 }
1348
Peter Maydell00108f22014-02-20 10:35:53 +00001349 return CP_ACCESS_OK;
1350}
1351
Peter Maydell3f208fd2016-02-11 11:17:31 +00001352static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx,
1353 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00001354{
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01001355 unsigned int cur_el = arm_current_el(env);
1356 bool secure = arm_is_secure(env);
1357
Peter Maydell00108f22014-02-20 10:35:53 +00001358 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01001359 if (cur_el == 0 &&
Peter Maydell00108f22014-02-20 10:35:53 +00001360 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) {
1361 return CP_ACCESS_TRAP;
1362 }
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01001363
1364 if (arm_feature(env, ARM_FEATURE_EL2) &&
1365 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1366 !extract32(env->cp15.cnthctl_el2, 0, 1)) {
1367 return CP_ACCESS_TRAP_EL2;
1368 }
Peter Maydell00108f22014-02-20 10:35:53 +00001369 return CP_ACCESS_OK;
1370}
1371
Peter Maydell3f208fd2016-02-11 11:17:31 +00001372static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx,
1373 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00001374{
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01001375 unsigned int cur_el = arm_current_el(env);
1376 bool secure = arm_is_secure(env);
1377
Peter Maydell00108f22014-02-20 10:35:53 +00001378 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if
1379 * EL0[PV]TEN is zero.
1380 */
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01001381 if (cur_el == 0 &&
Peter Maydell00108f22014-02-20 10:35:53 +00001382 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) {
1383 return CP_ACCESS_TRAP;
1384 }
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01001385
1386 if (arm_feature(env, ARM_FEATURE_EL2) &&
1387 timeridx == GTIMER_PHYS && !secure && cur_el < 2 &&
1388 !extract32(env->cp15.cnthctl_el2, 1, 1)) {
1389 return CP_ACCESS_TRAP_EL2;
1390 }
Peter Maydell00108f22014-02-20 10:35:53 +00001391 return CP_ACCESS_OK;
1392}
1393
1394static CPAccessResult gt_pct_access(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +00001395 const ARMCPRegInfo *ri,
1396 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00001397{
Peter Maydell3f208fd2016-02-11 11:17:31 +00001398 return gt_counter_access(env, GTIMER_PHYS, isread);
Peter Maydell00108f22014-02-20 10:35:53 +00001399}
1400
1401static CPAccessResult gt_vct_access(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +00001402 const ARMCPRegInfo *ri,
1403 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00001404{
Peter Maydell3f208fd2016-02-11 11:17:31 +00001405 return gt_counter_access(env, GTIMER_VIRT, isread);
Peter Maydell00108f22014-02-20 10:35:53 +00001406}
1407
Peter Maydell3f208fd2016-02-11 11:17:31 +00001408static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1409 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00001410{
Peter Maydell3f208fd2016-02-11 11:17:31 +00001411 return gt_timer_access(env, GTIMER_PHYS, isread);
Peter Maydell00108f22014-02-20 10:35:53 +00001412}
1413
Peter Maydell3f208fd2016-02-11 11:17:31 +00001414static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri,
1415 bool isread)
Peter Maydell00108f22014-02-20 10:35:53 +00001416{
Peter Maydell3f208fd2016-02-11 11:17:31 +00001417 return gt_timer_access(env, GTIMER_VIRT, isread);
Peter Maydell00108f22014-02-20 10:35:53 +00001418}
1419
Peter Maydellb4d39782015-08-13 11:26:22 +01001420static CPAccessResult gt_stimer_access(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +00001421 const ARMCPRegInfo *ri,
1422 bool isread)
Peter Maydellb4d39782015-08-13 11:26:22 +01001423{
1424 /* The AArch64 register view of the secure physical timer is
1425 * always accessible from EL3, and configurably accessible from
1426 * Secure EL1.
1427 */
1428 switch (arm_current_el(env)) {
1429 case 1:
1430 if (!arm_is_secure(env)) {
1431 return CP_ACCESS_TRAP;
1432 }
1433 if (!(env->cp15.scr_el3 & SCR_ST)) {
1434 return CP_ACCESS_TRAP_EL3;
1435 }
1436 return CP_ACCESS_OK;
1437 case 0:
1438 case 2:
1439 return CP_ACCESS_TRAP;
1440 case 3:
1441 return CP_ACCESS_OK;
1442 default:
1443 g_assert_not_reached();
1444 }
1445}
1446
Peter Maydell55d284a2013-08-20 14:54:31 +01001447static uint64_t gt_get_countervalue(CPUARMState *env)
1448{
Alex Blighbc72ad62013-08-21 16:03:08 +01001449 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE;
Peter Maydell55d284a2013-08-20 14:54:31 +01001450}
1451
1452static void gt_recalc_timer(ARMCPU *cpu, int timeridx)
1453{
1454 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx];
1455
1456 if (gt->ctl & 1) {
1457 /* Timer enabled: calculate and set current ISTATUS, irq, and
1458 * reset timer to when ISTATUS next has to change
1459 */
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001460 uint64_t offset = timeridx == GTIMER_VIRT ?
1461 cpu->env.cp15.cntvoff_el2 : 0;
Peter Maydell55d284a2013-08-20 14:54:31 +01001462 uint64_t count = gt_get_countervalue(&cpu->env);
1463 /* Note that this must be unsigned 64 bit arithmetic: */
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001464 int istatus = count - offset >= gt->cval;
Peter Maydell55d284a2013-08-20 14:54:31 +01001465 uint64_t nexttick;
1466
1467 gt->ctl = deposit32(gt->ctl, 2, 1, istatus);
1468 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
1469 (istatus && !(gt->ctl & 2)));
1470 if (istatus) {
1471 /* Next transition is when count rolls back over to zero */
1472 nexttick = UINT64_MAX;
1473 } else {
1474 /* Next transition is when we hit cval */
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001475 nexttick = gt->cval + offset;
Peter Maydell55d284a2013-08-20 14:54:31 +01001476 }
1477 /* Note that the desired next expiry time might be beyond the
1478 * signed-64-bit range of a QEMUTimer -- in this case we just
1479 * set the timer for as far in the future as possible. When the
1480 * timer expires we will reset the timer for any remaining period.
1481 */
1482 if (nexttick > INT64_MAX / GTIMER_SCALE) {
1483 nexttick = INT64_MAX / GTIMER_SCALE;
1484 }
Alex Blighbc72ad62013-08-21 16:03:08 +01001485 timer_mod(cpu->gt_timer[timeridx], nexttick);
Peter Maydell55d284a2013-08-20 14:54:31 +01001486 } else {
1487 /* Timer disabled: ISTATUS and timer output always clear */
1488 gt->ctl &= ~4;
1489 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0);
Alex Blighbc72ad62013-08-21 16:03:08 +01001490 timer_del(cpu->gt_timer[timeridx]);
Peter Maydell55d284a2013-08-20 14:54:31 +01001491 }
1492}
1493
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001494static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri,
1495 int timeridx)
Peter Maydell55d284a2013-08-20 14:54:31 +01001496{
1497 ARMCPU *cpu = arm_env_get_cpu(env);
Peter Maydell55d284a2013-08-20 14:54:31 +01001498
Alex Blighbc72ad62013-08-21 16:03:08 +01001499 timer_del(cpu->gt_timer[timeridx]);
Peter Maydell55d284a2013-08-20 14:54:31 +01001500}
1501
Peter Maydellc4241c72014-02-20 10:35:54 +00001502static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell55d284a2013-08-20 14:54:31 +01001503{
Peter Maydellc4241c72014-02-20 10:35:54 +00001504 return gt_get_countervalue(env);
Peter Maydell55d284a2013-08-20 14:54:31 +01001505}
1506
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001507static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri)
1508{
1509 return gt_get_countervalue(env) - env->cp15.cntvoff_el2;
1510}
1511
Peter Maydellc4241c72014-02-20 10:35:54 +00001512static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001513 int timeridx,
Peter Maydellc4241c72014-02-20 10:35:54 +00001514 uint64_t value)
Peter Maydell55d284a2013-08-20 14:54:31 +01001515{
Peter Maydell55d284a2013-08-20 14:54:31 +01001516 env->cp15.c14_timer[timeridx].cval = value;
1517 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
Peter Maydell55d284a2013-08-20 14:54:31 +01001518}
Peter Maydellc4241c72014-02-20 10:35:54 +00001519
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001520static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri,
1521 int timeridx)
Peter Maydell55d284a2013-08-20 14:54:31 +01001522{
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001523 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
Peter Maydell55d284a2013-08-20 14:54:31 +01001524
Peter Maydellc4241c72014-02-20 10:35:54 +00001525 return (uint32_t)(env->cp15.c14_timer[timeridx].cval -
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001526 (gt_get_countervalue(env) - offset));
Peter Maydell55d284a2013-08-20 14:54:31 +01001527}
1528
Peter Maydellc4241c72014-02-20 10:35:54 +00001529static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001530 int timeridx,
Peter Maydellc4241c72014-02-20 10:35:54 +00001531 uint64_t value)
Peter Maydell55d284a2013-08-20 14:54:31 +01001532{
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001533 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0;
Peter Maydell55d284a2013-08-20 14:54:31 +01001534
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001535 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset +
Edgar E. Iglesias18084b22015-05-13 16:52:28 +10001536 sextract64(value, 0, 32);
Peter Maydell55d284a2013-08-20 14:54:31 +01001537 gt_recalc_timer(arm_env_get_cpu(env), timeridx);
Peter Maydell55d284a2013-08-20 14:54:31 +01001538}
1539
Peter Maydellc4241c72014-02-20 10:35:54 +00001540static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001541 int timeridx,
Peter Maydellc4241c72014-02-20 10:35:54 +00001542 uint64_t value)
Peter Maydell55d284a2013-08-20 14:54:31 +01001543{
1544 ARMCPU *cpu = arm_env_get_cpu(env);
Peter Maydell55d284a2013-08-20 14:54:31 +01001545 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl;
1546
Peter Maydelld3afacc2014-06-09 15:43:26 +01001547 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value);
Peter Maydell55d284a2013-08-20 14:54:31 +01001548 if ((oldval ^ value) & 1) {
1549 /* Enable toggled */
1550 gt_recalc_timer(cpu, timeridx);
Peter Maydelld3afacc2014-06-09 15:43:26 +01001551 } else if ((oldval ^ value) & 2) {
Peter Maydell55d284a2013-08-20 14:54:31 +01001552 /* IMASK toggled: don't need to recalculate,
1553 * just set the interrupt line based on ISTATUS
1554 */
1555 qemu_set_irq(cpu->gt_timer_outputs[timeridx],
Peter Maydelld3afacc2014-06-09 15:43:26 +01001556 (oldval & 4) && !(value & 2));
Peter Maydell55d284a2013-08-20 14:54:31 +01001557 }
Peter Maydell55d284a2013-08-20 14:54:31 +01001558}
1559
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001560static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1561{
1562 gt_timer_reset(env, ri, GTIMER_PHYS);
1563}
1564
1565static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1566 uint64_t value)
1567{
1568 gt_cval_write(env, ri, GTIMER_PHYS, value);
1569}
1570
1571static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1572{
1573 return gt_tval_read(env, ri, GTIMER_PHYS);
1574}
1575
1576static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1577 uint64_t value)
1578{
1579 gt_tval_write(env, ri, GTIMER_PHYS, value);
1580}
1581
1582static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1583 uint64_t value)
1584{
1585 gt_ctl_write(env, ri, GTIMER_PHYS, value);
1586}
1587
1588static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1589{
1590 gt_timer_reset(env, ri, GTIMER_VIRT);
1591}
1592
1593static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1594 uint64_t value)
1595{
1596 gt_cval_write(env, ri, GTIMER_VIRT, value);
1597}
1598
1599static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1600{
1601 return gt_tval_read(env, ri, GTIMER_VIRT);
1602}
1603
1604static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1605 uint64_t value)
1606{
1607 gt_tval_write(env, ri, GTIMER_VIRT, value);
1608}
1609
1610static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1611 uint64_t value)
1612{
1613 gt_ctl_write(env, ri, GTIMER_VIRT, value);
1614}
1615
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001616static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri,
1617 uint64_t value)
1618{
1619 ARMCPU *cpu = arm_env_get_cpu(env);
1620
1621 raw_write(env, ri, value);
1622 gt_recalc_timer(cpu, GTIMER_VIRT);
1623}
1624
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01001625static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1626{
1627 gt_timer_reset(env, ri, GTIMER_HYP);
1628}
1629
1630static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1631 uint64_t value)
1632{
1633 gt_cval_write(env, ri, GTIMER_HYP, value);
1634}
1635
1636static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1637{
1638 return gt_tval_read(env, ri, GTIMER_HYP);
1639}
1640
1641static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1642 uint64_t value)
1643{
1644 gt_tval_write(env, ri, GTIMER_HYP, value);
1645}
1646
1647static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1648 uint64_t value)
1649{
1650 gt_ctl_write(env, ri, GTIMER_HYP, value);
1651}
1652
Peter Maydellb4d39782015-08-13 11:26:22 +01001653static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri)
1654{
1655 gt_timer_reset(env, ri, GTIMER_SEC);
1656}
1657
1658static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1659 uint64_t value)
1660{
1661 gt_cval_write(env, ri, GTIMER_SEC, value);
1662}
1663
1664static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri)
1665{
1666 return gt_tval_read(env, ri, GTIMER_SEC);
1667}
1668
1669static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri,
1670 uint64_t value)
1671{
1672 gt_tval_write(env, ri, GTIMER_SEC, value);
1673}
1674
1675static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri,
1676 uint64_t value)
1677{
1678 gt_ctl_write(env, ri, GTIMER_SEC, value);
1679}
1680
Peter Maydell55d284a2013-08-20 14:54:31 +01001681void arm_gt_ptimer_cb(void *opaque)
1682{
1683 ARMCPU *cpu = opaque;
1684
1685 gt_recalc_timer(cpu, GTIMER_PHYS);
1686}
1687
1688void arm_gt_vtimer_cb(void *opaque)
1689{
1690 ARMCPU *cpu = opaque;
1691
1692 gt_recalc_timer(cpu, GTIMER_VIRT);
1693}
1694
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01001695void arm_gt_htimer_cb(void *opaque)
1696{
1697 ARMCPU *cpu = opaque;
1698
1699 gt_recalc_timer(cpu, GTIMER_HYP);
1700}
1701
Peter Maydellb4d39782015-08-13 11:26:22 +01001702void arm_gt_stimer_cb(void *opaque)
1703{
1704 ARMCPU *cpu = opaque;
1705
1706 gt_recalc_timer(cpu, GTIMER_SEC);
1707}
1708
Peter Maydell6cc7a3a2012-06-20 11:57:12 +00001709static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
Peter Maydell55d284a2013-08-20 14:54:31 +01001710 /* Note that CNTFRQ is purely reads-as-written for the benefit
1711 * of software; writing it doesn't actually change the timer frequency.
1712 * Our reset value matches the fixed frequency we implement the timer at.
1713 */
1714 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001715 .type = ARM_CP_ALIAS,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001716 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
1717 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq),
Peter Maydella7adc4b2014-02-26 17:20:05 +00001718 },
1719 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64,
1720 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0,
1721 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access,
Peter Maydell55d284a2013-08-20 14:54:31 +01001722 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq),
1723 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE,
Peter Maydell55d284a2013-08-20 14:54:31 +01001724 },
1725 /* overall control: mostly access permissions */
Peter Maydella7adc4b2014-02-26 17:20:05 +00001726 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH,
1727 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0,
Peter Maydell55d284a2013-08-20 14:54:31 +01001728 .access = PL1_RW,
1729 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl),
1730 .resetvalue = 0,
1731 },
1732 /* per-timer control */
1733 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01001734 .secure = ARM_CP_SECSTATE_NS,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001735 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001736 .accessfn = gt_ptimer_access,
1737 .fieldoffset = offsetoflow32(CPUARMState,
1738 cp15.c14_timer[GTIMER_PHYS].ctl),
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001739 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001740 },
Peter Maydell9ff9dd32015-08-13 11:26:22 +01001741 { .name = "CNTP_CTL(S)",
1742 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1,
1743 .secure = ARM_CP_SECSTATE_S,
1744 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
1745 .accessfn = gt_ptimer_access,
1746 .fieldoffset = offsetoflow32(CPUARMState,
1747 cp15.c14_timer[GTIMER_SEC].ctl),
1748 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1749 },
Peter Maydella7adc4b2014-02-26 17:20:05 +00001750 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64,
1751 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1,
Peter Maydell55d284a2013-08-20 14:54:31 +01001752 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001753 .accessfn = gt_ptimer_access,
Peter Maydell55d284a2013-08-20 14:54:31 +01001754 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl),
1755 .resetvalue = 0,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001756 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01001757 },
1758 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001759 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001760 .accessfn = gt_vtimer_access,
1761 .fieldoffset = offsetoflow32(CPUARMState,
1762 cp15.c14_timer[GTIMER_VIRT].ctl),
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001763 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001764 },
1765 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64,
1766 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1,
Peter Maydell55d284a2013-08-20 14:54:31 +01001767 .type = ARM_CP_IO, .access = PL1_RW | PL0_R,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001768 .accessfn = gt_vtimer_access,
Peter Maydell55d284a2013-08-20 14:54:31 +01001769 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl),
1770 .resetvalue = 0,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001771 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01001772 },
1773 /* TimerValue views: a 32 bit downcounting view of the underlying state */
1774 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01001775 .secure = ARM_CP_SECSTATE_NS,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001776 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
Peter Maydell00108f22014-02-20 10:35:53 +00001777 .accessfn = gt_ptimer_access,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001778 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01001779 },
Peter Maydell9ff9dd32015-08-13 11:26:22 +01001780 { .name = "CNTP_TVAL(S)",
1781 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0,
1782 .secure = ARM_CP_SECSTATE_S,
1783 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
1784 .accessfn = gt_ptimer_access,
1785 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write,
1786 },
Peter Maydella7adc4b2014-02-26 17:20:05 +00001787 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1788 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001789 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001790 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset,
1791 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001792 },
Peter Maydell55d284a2013-08-20 14:54:31 +01001793 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001794 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
Peter Maydell00108f22014-02-20 10:35:53 +00001795 .accessfn = gt_vtimer_access,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001796 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01001797 },
Peter Maydella7adc4b2014-02-26 17:20:05 +00001798 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64,
1799 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001800 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001801 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset,
1802 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001803 },
Peter Maydell55d284a2013-08-20 14:54:31 +01001804 /* The counter itself */
1805 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001806 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
Peter Maydell00108f22014-02-20 10:35:53 +00001807 .accessfn = gt_pct_access,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001808 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore,
1809 },
1810 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64,
1811 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001812 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
Edgar E. Iglesiasd57b9ee2015-08-13 11:26:18 +01001813 .accessfn = gt_pct_access, .readfn = gt_cnt_read,
Peter Maydell55d284a2013-08-20 14:54:31 +01001814 },
1815 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001816 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO,
Peter Maydell00108f22014-02-20 10:35:53 +00001817 .accessfn = gt_vct_access,
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01001818 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001819 },
1820 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64,
1821 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001822 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO,
Edgar E. Iglesiasd57b9ee2015-08-13 11:26:18 +01001823 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read,
Peter Maydell55d284a2013-08-20 14:54:31 +01001824 },
1825 /* Comparison value, indicating when the timer goes off */
1826 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2,
Peter Maydell9ff9dd32015-08-13 11:26:22 +01001827 .secure = ARM_CP_SECSTATE_NS,
Peter Maydell55d284a2013-08-20 14:54:31 +01001828 .access = PL1_RW | PL0_R,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001829 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
Peter Maydell55d284a2013-08-20 14:54:31 +01001830 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
Sergey Fedorovb061a822015-06-19 14:17:44 +01001831 .accessfn = gt_ptimer_access,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001832 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001833 },
Peter Maydell9ff9dd32015-08-13 11:26:22 +01001834 { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2,
1835 .secure = ARM_CP_SECSTATE_S,
1836 .access = PL1_RW | PL0_R,
1837 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
1838 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1839 .accessfn = gt_ptimer_access,
1840 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1841 },
Peter Maydella7adc4b2014-02-26 17:20:05 +00001842 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1843 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2,
1844 .access = PL1_RW | PL0_R,
1845 .type = ARM_CP_IO,
1846 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval),
Edgar E. Iglesias12cde082015-05-13 16:52:26 +10001847 .resetvalue = 0, .accessfn = gt_ptimer_access,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001848 .writefn = gt_phys_cval_write, .raw_writefn = raw_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01001849 },
1850 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3,
1851 .access = PL1_RW | PL0_R,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00001852 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS,
Peter Maydell55d284a2013-08-20 14:54:31 +01001853 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
Sergey Fedorovb061a822015-06-19 14:17:44 +01001854 .accessfn = gt_vtimer_access,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001855 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
Peter Maydella7adc4b2014-02-26 17:20:05 +00001856 },
1857 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64,
1858 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2,
1859 .access = PL1_RW | PL0_R,
1860 .type = ARM_CP_IO,
1861 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval),
1862 .resetvalue = 0, .accessfn = gt_vtimer_access,
Edgar E. Iglesias0e3eca42015-08-13 11:26:18 +01001863 .writefn = gt_virt_cval_write, .raw_writefn = raw_write,
Peter Maydell55d284a2013-08-20 14:54:31 +01001864 },
Peter Maydellb4d39782015-08-13 11:26:22 +01001865 /* Secure timer -- this is actually restricted to only EL3
1866 * and configurably Secure-EL1 via the accessfn.
1867 */
1868 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64,
1869 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0,
1870 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW,
1871 .accessfn = gt_stimer_access,
1872 .readfn = gt_sec_tval_read,
1873 .writefn = gt_sec_tval_write,
1874 .resetfn = gt_sec_timer_reset,
1875 },
1876 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64,
1877 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1,
1878 .type = ARM_CP_IO, .access = PL1_RW,
1879 .accessfn = gt_stimer_access,
1880 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl),
1881 .resetvalue = 0,
1882 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write,
1883 },
1884 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64,
1885 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2,
1886 .type = ARM_CP_IO, .access = PL1_RW,
1887 .accessfn = gt_stimer_access,
1888 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval),
1889 .writefn = gt_sec_cval_write, .raw_writefn = raw_write,
1890 },
Peter Maydell6cc7a3a2012-06-20 11:57:12 +00001891 REGINFO_SENTINEL
1892};
1893
Peter Maydell55d284a2013-08-20 14:54:31 +01001894#else
1895/* In user-mode none of the generic timer registers are accessible,
Alex Blighbc72ad62013-08-21 16:03:08 +01001896 * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs,
Peter Maydell55d284a2013-08-20 14:54:31 +01001897 * so instead just don't register any of them.
1898 */
1899static const ARMCPRegInfo generic_timer_cp_reginfo[] = {
1900 REGINFO_SENTINEL
1901};
1902
1903#endif
1904
Peter Maydellc4241c72014-02-20 10:35:54 +00001905static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
Peter Maydell4a501602012-06-20 11:57:16 +00001906{
Peter Maydell891a2fe2012-07-12 10:59:09 +00001907 if (arm_feature(env, ARM_FEATURE_LPAE)) {
Fabian Aggeler8d5c7732014-06-09 15:43:22 +01001908 raw_write(env, ri, value);
Peter Maydell891a2fe2012-07-12 10:59:09 +00001909 } else if (arm_feature(env, ARM_FEATURE_V7)) {
Fabian Aggeler8d5c7732014-06-09 15:43:22 +01001910 raw_write(env, ri, value & 0xfffff6ff);
Peter Maydell4a501602012-06-20 11:57:16 +00001911 } else {
Fabian Aggeler8d5c7732014-06-09 15:43:22 +01001912 raw_write(env, ri, value & 0xfffff1ff);
Peter Maydell4a501602012-06-20 11:57:16 +00001913 }
Peter Maydell4a501602012-06-20 11:57:16 +00001914}
1915
1916#ifndef CONFIG_USER_ONLY
1917/* get_phys_addr() isn't present for user-mode-only targets */
Peter Maydell702a9352012-07-12 10:59:10 +00001918
Peter Maydell3f208fd2016-02-11 11:17:31 +00001919static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri,
1920 bool isread)
Peter Maydell92611c02014-02-20 10:35:53 +00001921{
1922 if (ri->opc2 & 4) {
Peter Maydell87562e42015-08-25 15:45:08 +01001923 /* The ATS12NSO* operations must trap to EL3 if executed in
1924 * Secure EL1 (which can only happen if EL3 is AArch64).
1925 * They are simply UNDEF if executed from NS EL1.
1926 * They function normally from EL2 or EL3.
Peter Maydell92611c02014-02-20 10:35:53 +00001927 */
Peter Maydell87562e42015-08-25 15:45:08 +01001928 if (arm_current_el(env) == 1) {
1929 if (arm_is_secure_below_el3(env)) {
1930 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3;
1931 }
1932 return CP_ACCESS_TRAP_UNCATEGORIZED;
1933 }
Peter Maydell92611c02014-02-20 10:35:53 +00001934 }
1935 return CP_ACCESS_OK;
1936}
1937
Peter Maydell060e8a42015-02-05 13:37:24 +00001938static uint64_t do_ats_write(CPUARMState *env, uint64_t value,
Peter Maydelld3649702015-02-05 13:37:24 +00001939 int access_type, ARMMMUIdx mmu_idx)
Peter Maydell4a501602012-06-20 11:57:16 +00001940{
Avi Kivitya8170e52012-10-23 12:30:10 +02001941 hwaddr phys_addr;
Peter Maydell4a501602012-06-20 11:57:16 +00001942 target_ulong page_size;
1943 int prot;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01001944 uint32_t fsr;
1945 bool ret;
Fabian Aggeler01c097f2014-12-11 12:07:52 +00001946 uint64_t par64;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01001947 MemTxAttrs attrs = {};
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01001948 ARMMMUFaultInfo fi = {};
Peter Maydell4a501602012-06-20 11:57:16 +00001949
Peter Maydelld3649702015-02-05 13:37:24 +00001950 ret = get_phys_addr(env, value, access_type, mmu_idx,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01001951 &phys_addr, &attrs, &prot, &page_size, &fsr, &fi);
Peter Maydell702a9352012-07-12 10:59:10 +00001952 if (extended_addresses_enabled(env)) {
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01001953 /* fsr is a DFSR/IFSR value for the long descriptor
Peter Maydell702a9352012-07-12 10:59:10 +00001954 * translation table format, but with WnR always clear.
1955 * Convert it to a 64-bit PAR.
1956 */
Fabian Aggeler01c097f2014-12-11 12:07:52 +00001957 par64 = (1 << 11); /* LPAE bit always set */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01001958 if (!ret) {
Peter Maydell702a9352012-07-12 10:59:10 +00001959 par64 |= phys_addr & ~0xfffULL;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01001960 if (!attrs.secure) {
1961 par64 |= (1 << 9); /* NS */
1962 }
Peter Maydell702a9352012-07-12 10:59:10 +00001963 /* We don't set the ATTR or SH fields in the PAR. */
Peter Maydell4a501602012-06-20 11:57:16 +00001964 } else {
Peter Maydell702a9352012-07-12 10:59:10 +00001965 par64 |= 1; /* F */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01001966 par64 |= (fsr & 0x3f) << 1; /* FS */
Peter Maydell702a9352012-07-12 10:59:10 +00001967 /* Note that S2WLK and FSTAGE are always zero, because we don't
1968 * implement virtualization and therefore there can't be a stage 2
1969 * fault.
1970 */
Peter Maydell4a501602012-06-20 11:57:16 +00001971 }
1972 } else {
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01001973 /* fsr is a DFSR/IFSR value for the short descriptor
Peter Maydell702a9352012-07-12 10:59:10 +00001974 * translation table format (with WnR always clear).
1975 * Convert it to a 32-bit PAR.
1976 */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01001977 if (!ret) {
Peter Maydell702a9352012-07-12 10:59:10 +00001978 /* We do not set any attribute bits in the PAR */
1979 if (page_size == (1 << 24)
1980 && arm_feature(env, ARM_FEATURE_V7)) {
Fabian Aggeler01c097f2014-12-11 12:07:52 +00001981 par64 = (phys_addr & 0xff000000) | (1 << 1);
Peter Maydell702a9352012-07-12 10:59:10 +00001982 } else {
Fabian Aggeler01c097f2014-12-11 12:07:52 +00001983 par64 = phys_addr & 0xfffff000;
Peter Maydell702a9352012-07-12 10:59:10 +00001984 }
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01001985 if (!attrs.secure) {
1986 par64 |= (1 << 9); /* NS */
1987 }
Peter Maydell702a9352012-07-12 10:59:10 +00001988 } else {
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01001989 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) |
1990 ((fsr & 0xf) << 1) | 1;
Peter Maydell702a9352012-07-12 10:59:10 +00001991 }
Peter Maydell4a501602012-06-20 11:57:16 +00001992 }
Peter Maydell060e8a42015-02-05 13:37:24 +00001993 return par64;
1994}
1995
1996static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
1997{
Peter Maydell060e8a42015-02-05 13:37:24 +00001998 int access_type = ri->opc2 & 1;
1999 uint64_t par64;
Peter Maydelld3649702015-02-05 13:37:24 +00002000 ARMMMUIdx mmu_idx;
2001 int el = arm_current_el(env);
2002 bool secure = arm_is_secure_below_el3(env);
Peter Maydell060e8a42015-02-05 13:37:24 +00002003
Peter Maydelld3649702015-02-05 13:37:24 +00002004 switch (ri->opc2 & 6) {
2005 case 0:
2006 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */
2007 switch (el) {
2008 case 3:
2009 mmu_idx = ARMMMUIdx_S1E3;
2010 break;
2011 case 2:
2012 mmu_idx = ARMMMUIdx_S1NSE1;
2013 break;
2014 case 1:
2015 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2016 break;
2017 default:
2018 g_assert_not_reached();
2019 }
2020 break;
2021 case 2:
2022 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */
2023 switch (el) {
2024 case 3:
2025 mmu_idx = ARMMMUIdx_S1SE0;
2026 break;
2027 case 2:
2028 mmu_idx = ARMMMUIdx_S1NSE0;
2029 break;
2030 case 1:
2031 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2032 break;
2033 default:
2034 g_assert_not_reached();
2035 }
2036 break;
2037 case 4:
2038 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */
2039 mmu_idx = ARMMMUIdx_S12NSE1;
2040 break;
2041 case 6:
2042 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */
2043 mmu_idx = ARMMMUIdx_S12NSE0;
2044 break;
2045 default:
2046 g_assert_not_reached();
2047 }
2048
2049 par64 = do_ats_write(env, value, access_type, mmu_idx);
Fabian Aggeler01c097f2014-12-11 12:07:52 +00002050
2051 A32_BANKED_CURRENT_REG_SET(env, par, par64);
Peter Maydell4a501602012-06-20 11:57:16 +00002052}
Peter Maydell060e8a42015-02-05 13:37:24 +00002053
Peter Maydell14db7fe2015-08-25 15:45:08 +01002054static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri,
2055 uint64_t value)
2056{
2057 int access_type = ri->opc2 & 1;
2058 uint64_t par64;
2059
2060 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS);
2061
2062 A32_BANKED_CURRENT_REG_SET(env, par, par64);
2063}
2064
Peter Maydell3f208fd2016-02-11 11:17:31 +00002065static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri,
2066 bool isread)
Peter Maydell2a47df92015-08-25 15:45:08 +01002067{
2068 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) {
2069 return CP_ACCESS_TRAP;
2070 }
2071 return CP_ACCESS_OK;
2072}
2073
Peter Maydell060e8a42015-02-05 13:37:24 +00002074static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri,
2075 uint64_t value)
2076{
Peter Maydell060e8a42015-02-05 13:37:24 +00002077 int access_type = ri->opc2 & 1;
Peter Maydelld3649702015-02-05 13:37:24 +00002078 ARMMMUIdx mmu_idx;
2079 int secure = arm_is_secure_below_el3(env);
Peter Maydell060e8a42015-02-05 13:37:24 +00002080
Peter Maydelld3649702015-02-05 13:37:24 +00002081 switch (ri->opc2 & 6) {
2082 case 0:
2083 switch (ri->opc1) {
2084 case 0: /* AT S1E1R, AT S1E1W */
2085 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1;
2086 break;
2087 case 4: /* AT S1E2R, AT S1E2W */
2088 mmu_idx = ARMMMUIdx_S1E2;
2089 break;
2090 case 6: /* AT S1E3R, AT S1E3W */
2091 mmu_idx = ARMMMUIdx_S1E3;
2092 break;
2093 default:
2094 g_assert_not_reached();
2095 }
2096 break;
2097 case 2: /* AT S1E0R, AT S1E0W */
2098 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0;
2099 break;
2100 case 4: /* AT S12E1R, AT S12E1W */
Peter Maydell2a47df92015-08-25 15:45:08 +01002101 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1;
Peter Maydelld3649702015-02-05 13:37:24 +00002102 break;
2103 case 6: /* AT S12E0R, AT S12E0W */
Peter Maydell2a47df92015-08-25 15:45:08 +01002104 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0;
Peter Maydelld3649702015-02-05 13:37:24 +00002105 break;
2106 default:
2107 g_assert_not_reached();
2108 }
2109
2110 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx);
Peter Maydell060e8a42015-02-05 13:37:24 +00002111}
Peter Maydell4a501602012-06-20 11:57:16 +00002112#endif
2113
2114static const ARMCPRegInfo vapa_cp_reginfo[] = {
2115 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0,
2116 .access = PL1_RW, .resetvalue = 0,
Fabian Aggeler01c097f2014-12-11 12:07:52 +00002117 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s),
2118 offsetoflow32(CPUARMState, cp15.par_ns) },
Peter Maydell4a501602012-06-20 11:57:16 +00002119 .writefn = par_write },
2120#ifndef CONFIG_USER_ONLY
Peter Maydell87562e42015-08-25 15:45:08 +01002121 /* This underdecoding is safe because the reginfo is NO_RAW. */
Peter Maydell4a501602012-06-20 11:57:16 +00002122 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY,
Peter Maydell92611c02014-02-20 10:35:53 +00002123 .access = PL1_W, .accessfn = ats_access,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002124 .writefn = ats_write, .type = ARM_CP_NO_RAW },
Peter Maydell4a501602012-06-20 11:57:16 +00002125#endif
2126 REGINFO_SENTINEL
2127};
2128
Peter Maydell18032be2012-06-20 11:57:13 +00002129/* Return basic MPU access permission bits. */
2130static uint32_t simple_mpu_ap_bits(uint32_t val)
2131{
2132 uint32_t ret;
2133 uint32_t mask;
2134 int i;
2135 ret = 0;
2136 mask = 3;
2137 for (i = 0; i < 16; i += 2) {
2138 ret |= (val >> i) & mask;
2139 mask <<= 2;
2140 }
2141 return ret;
2142}
2143
2144/* Pad basic MPU access permission bits to extended format. */
2145static uint32_t extended_mpu_ap_bits(uint32_t val)
2146{
2147 uint32_t ret;
2148 uint32_t mask;
2149 int i;
2150 ret = 0;
2151 mask = 3;
2152 for (i = 0; i < 16; i += 2) {
2153 ret |= (val & mask) << i;
2154 mask <<= 2;
2155 }
2156 return ret;
2157}
2158
Peter Maydellc4241c72014-02-20 10:35:54 +00002159static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2160 uint64_t value)
Peter Maydell18032be2012-06-20 11:57:13 +00002161{
Peter Maydell7e097972014-04-15 19:18:41 +01002162 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value);
Peter Maydell18032be2012-06-20 11:57:13 +00002163}
2164
Peter Maydellc4241c72014-02-20 10:35:54 +00002165static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell18032be2012-06-20 11:57:13 +00002166{
Peter Maydell7e097972014-04-15 19:18:41 +01002167 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap);
Peter Maydell18032be2012-06-20 11:57:13 +00002168}
2169
Peter Maydellc4241c72014-02-20 10:35:54 +00002170static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri,
2171 uint64_t value)
Peter Maydell18032be2012-06-20 11:57:13 +00002172{
Peter Maydell7e097972014-04-15 19:18:41 +01002173 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value);
Peter Maydell18032be2012-06-20 11:57:13 +00002174}
2175
Peter Maydellc4241c72014-02-20 10:35:54 +00002176static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell18032be2012-06-20 11:57:13 +00002177{
Peter Maydell7e097972014-04-15 19:18:41 +01002178 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap);
Peter Maydell18032be2012-06-20 11:57:13 +00002179}
2180
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01002181static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri)
2182{
2183 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2184
2185 if (!u32p) {
2186 return 0;
2187 }
2188
2189 u32p += env->cp15.c6_rgnr;
2190 return *u32p;
2191}
2192
2193static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri,
2194 uint64_t value)
2195{
2196 ARMCPU *cpu = arm_env_get_cpu(env);
2197 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2198
2199 if (!u32p) {
2200 return;
2201 }
2202
2203 u32p += env->cp15.c6_rgnr;
2204 tlb_flush(CPU(cpu), 1); /* Mappings may have changed - purge! */
2205 *u32p = value;
2206}
2207
2208static void pmsav7_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2209{
2210 ARMCPU *cpu = arm_env_get_cpu(env);
2211 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri);
2212
2213 if (!u32p) {
2214 return;
2215 }
2216
2217 memset(u32p, 0, sizeof(*u32p) * cpu->pmsav7_dregion);
2218}
2219
2220static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2221 uint64_t value)
2222{
2223 ARMCPU *cpu = arm_env_get_cpu(env);
2224 uint32_t nrgs = cpu->pmsav7_dregion;
2225
2226 if (value >= nrgs) {
2227 qemu_log_mask(LOG_GUEST_ERROR,
2228 "PMSAv7 RGNR write >= # supported regions, %" PRIu32
2229 " > %" PRIu32 "\n", (uint32_t)value, nrgs);
2230 return;
2231 }
2232
2233 raw_write(env, ri, value);
2234}
2235
2236static const ARMCPRegInfo pmsav7_cp_reginfo[] = {
2237 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0,
2238 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2239 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar),
2240 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2241 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2,
2242 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2243 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr),
2244 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2245 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4,
2246 .access = PL1_RW, .type = ARM_CP_NO_RAW,
2247 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr),
2248 .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset },
2249 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0,
2250 .access = PL1_RW,
2251 .fieldoffset = offsetof(CPUARMState, cp15.c6_rgnr),
2252 .writefn = pmsav7_rgnr_write },
2253 REGINFO_SENTINEL
2254};
2255
Peter Maydell18032be2012-06-20 11:57:13 +00002256static const ARMCPRegInfo pmsav5_cp_reginfo[] = {
2257 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002258 .access = PL1_RW, .type = ARM_CP_ALIAS,
Peter Maydell7e097972014-04-15 19:18:41 +01002259 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
Peter Maydell18032be2012-06-20 11:57:13 +00002260 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, },
2261 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002262 .access = PL1_RW, .type = ARM_CP_ALIAS,
Peter Maydell7e097972014-04-15 19:18:41 +01002263 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
Peter Maydell18032be2012-06-20 11:57:13 +00002264 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, },
2265 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2,
2266 .access = PL1_RW,
Peter Maydell7e097972014-04-15 19:18:41 +01002267 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap),
2268 .resetvalue = 0, },
Peter Maydell18032be2012-06-20 11:57:13 +00002269 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3,
2270 .access = PL1_RW,
Peter Maydell7e097972014-04-15 19:18:41 +01002271 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap),
2272 .resetvalue = 0, },
Peter Maydellecce5c32012-06-20 11:57:14 +00002273 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
2274 .access = PL1_RW,
2275 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, },
2276 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1,
2277 .access = PL1_RW,
2278 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, },
Peter Maydell06d76f32012-06-20 11:57:17 +00002279 /* Protection region base and size registers */
Peter Maydelle508a922014-02-20 10:35:52 +00002280 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0,
2281 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2282 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) },
2283 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0,
2284 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2285 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) },
2286 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0,
2287 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2288 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) },
2289 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0,
2290 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2291 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) },
2292 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0,
2293 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2294 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) },
2295 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0,
2296 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2297 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) },
2298 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0,
2299 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2300 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) },
2301 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0,
2302 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0,
2303 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) },
Peter Maydell18032be2012-06-20 11:57:13 +00002304 REGINFO_SENTINEL
2305};
2306
Peter Maydellc4241c72014-02-20 10:35:54 +00002307static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri,
2308 uint64_t value)
Peter Maydellecce5c32012-06-20 11:57:14 +00002309{
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002310 TCR *tcr = raw_ptr(env, ri);
Peter Maydell2ebcebe2013-06-27 16:38:47 +01002311 int maskshift = extract32(value, 0, 3);
2312
Fabian Aggelere389be12014-06-19 18:06:24 +01002313 if (!arm_feature(env, ARM_FEATURE_V8)) {
2314 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) {
2315 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when
2316 * using Long-desciptor translation table format */
2317 value &= ~((7 << 19) | (3 << 14) | (0xf << 3));
2318 } else if (arm_feature(env, ARM_FEATURE_EL3)) {
2319 /* In an implementation that includes the Security Extensions
2320 * TTBCR has additional fields PD0 [4] and PD1 [5] for
2321 * Short-descriptor translation table format.
2322 */
2323 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N;
2324 } else {
2325 value &= TTBCR_N;
2326 }
Peter Maydelle42c4db2012-07-12 10:59:11 +00002327 }
Fabian Aggelere389be12014-06-19 18:06:24 +01002328
Daniel P. Berrangeb6af0972015-08-26 12:17:13 +01002329 /* Update the masks corresponding to the TCR bank being written
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002330 * Note that we always calculate mask and base_mask, but
Peter Maydelle42c4db2012-07-12 10:59:11 +00002331 * they are only used for short-descriptor tables (ie if EAE is 0);
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002332 * for long-descriptor tables the TCR fields are used differently
2333 * and the mask and base_mask values are meaningless.
Peter Maydelle42c4db2012-07-12 10:59:11 +00002334 */
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002335 tcr->raw_tcr = value;
2336 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift);
2337 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift);
Peter Maydellecce5c32012-06-20 11:57:14 +00002338}
2339
Peter Maydellc4241c72014-02-20 10:35:54 +00002340static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2341 uint64_t value)
Peter Maydelld4e6df62013-06-25 18:16:07 +01002342{
Andreas Färber00c8cb02013-09-04 02:19:44 +02002343 ARMCPU *cpu = arm_env_get_cpu(env);
2344
Peter Maydelld4e6df62013-06-25 18:16:07 +01002345 if (arm_feature(env, ARM_FEATURE_LPAE)) {
2346 /* With LPAE the TTBCR could result in a change of ASID
2347 * via the TTBCR.A1 bit, so do a TLB flush.
2348 */
Andreas Färber00c8cb02013-09-04 02:19:44 +02002349 tlb_flush(CPU(cpu), 1);
Peter Maydelld4e6df62013-06-25 18:16:07 +01002350 }
Peter Maydellc4241c72014-02-20 10:35:54 +00002351 vmsa_ttbcr_raw_write(env, ri, value);
Peter Maydelld4e6df62013-06-25 18:16:07 +01002352}
2353
Peter Maydellecce5c32012-06-20 11:57:14 +00002354static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri)
2355{
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002356 TCR *tcr = raw_ptr(env, ri);
2357
2358 /* Reset both the TCR as well as the masks corresponding to the bank of
2359 * the TCR being reset.
2360 */
2361 tcr->raw_tcr = 0;
2362 tcr->mask = 0;
2363 tcr->base_mask = 0xffffc000u;
Peter Maydellecce5c32012-06-20 11:57:14 +00002364}
2365
Peter Maydellcb2e37d2014-02-26 17:20:04 +00002366static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2367 uint64_t value)
2368{
Andreas Färber00c8cb02013-09-04 02:19:44 +02002369 ARMCPU *cpu = arm_env_get_cpu(env);
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002370 TCR *tcr = raw_ptr(env, ri);
Andreas Färber00c8cb02013-09-04 02:19:44 +02002371
Peter Maydellcb2e37d2014-02-26 17:20:04 +00002372 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */
Andreas Färber00c8cb02013-09-04 02:19:44 +02002373 tlb_flush(CPU(cpu), 1);
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002374 tcr->raw_tcr = value;
Peter Maydellcb2e37d2014-02-26 17:20:04 +00002375}
2376
Peter Maydell327ed102014-02-26 17:20:04 +00002377static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2378 uint64_t value)
2379{
2380 /* 64 bit accesses to the TTBRs can change the ASID and so we
2381 * must flush the TLB.
2382 */
2383 if (cpreg_field_is_64bit(ri)) {
Andreas Färber00c8cb02013-09-04 02:19:44 +02002384 ARMCPU *cpu = arm_env_get_cpu(env);
2385
2386 tlb_flush(CPU(cpu), 1);
Peter Maydell327ed102014-02-26 17:20:04 +00002387 }
2388 raw_write(env, ri, value);
2389}
2390
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01002391static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2392 uint64_t value)
2393{
2394 ARMCPU *cpu = arm_env_get_cpu(env);
2395 CPUState *cs = CPU(cpu);
2396
2397 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */
2398 if (raw_read(env, ri) != value) {
2399 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2400 ARMMMUIdx_S2NS, -1);
2401 raw_write(env, ri, value);
2402 }
2403}
2404
Peter Crosthwaite8e5d75c2015-06-15 18:06:10 +01002405static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = {
Peter Maydell18032be2012-06-20 11:57:13 +00002406 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002407 .access = PL1_RW, .type = ARM_CP_ALIAS,
Fabian Aggeler4a7e2d72014-12-11 12:07:51 +00002408 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s),
Sergey Fedorovb061a822015-06-19 14:17:44 +01002409 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, },
Peter Maydell18032be2012-06-20 11:57:13 +00002410 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1,
Fabian Aggeler88ca1c22014-12-11 12:07:51 +00002411 .access = PL1_RW, .resetvalue = 0,
2412 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s),
2413 offsetoflow32(CPUARMState, cp15.ifsr_ns) } },
Peter Crosthwaite8e5d75c2015-06-15 18:06:10 +01002414 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0,
2415 .access = PL1_RW, .resetvalue = 0,
2416 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s),
2417 offsetof(CPUARMState, cp15.dfar_ns) } },
2418 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64,
2419 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0,
2420 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]),
2421 .resetvalue = 0, },
2422 REGINFO_SENTINEL
2423};
2424
2425static const ARMCPRegInfo vmsa_cp_reginfo[] = {
Rob Herring6cd8a262014-04-15 19:18:42 +01002426 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64,
2427 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0,
2428 .access = PL1_RW,
Edgar E. Iglesiasd81c5192014-05-27 17:09:51 +01002429 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, },
Peter Maydell327ed102014-02-26 17:20:04 +00002430 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00002431 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0,
2432 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2433 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2434 offsetof(CPUARMState, cp15.ttbr0_ns) } },
Peter Maydell327ed102014-02-26 17:20:04 +00002435 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00002436 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1,
2437 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
2438 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2439 offsetof(CPUARMState, cp15.ttbr1_ns) } },
Peter Maydellcb2e37d2014-02-26 17:20:04 +00002440 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64,
2441 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
2442 .access = PL1_RW, .writefn = vmsa_tcr_el1_write,
2443 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write,
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002444 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) },
Peter Maydellcb2e37d2014-02-26 17:20:04 +00002445 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002446 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write,
Sergey Fedorovb061a822015-06-19 14:17:44 +01002447 .raw_writefn = vmsa_ttbcr_raw_write,
Fabian Aggeler11f136e2014-12-11 12:07:51 +00002448 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]),
2449 offsetoflow32(CPUARMState, cp15.tcr_el[1])} },
Peter Maydell18032be2012-06-20 11:57:13 +00002450 REGINFO_SENTINEL
2451};
2452
Peter Maydellc4241c72014-02-20 10:35:54 +00002453static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri,
2454 uint64_t value)
Peter Maydell1047b9d2012-06-20 11:57:15 +00002455{
2456 env->cp15.c15_ticonfig = value & 0xe7;
2457 /* The OS_TYPE bit in this register changes the reported CPUID! */
2458 env->cp15.c0_cpuid = (value & (1 << 5)) ?
2459 ARM_CPUID_TI915T : ARM_CPUID_TI925T;
Peter Maydell1047b9d2012-06-20 11:57:15 +00002460}
2461
Peter Maydellc4241c72014-02-20 10:35:54 +00002462static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri,
2463 uint64_t value)
Peter Maydell1047b9d2012-06-20 11:57:15 +00002464{
2465 env->cp15.c15_threadid = value & 0xffff;
Peter Maydell1047b9d2012-06-20 11:57:15 +00002466}
2467
Peter Maydellc4241c72014-02-20 10:35:54 +00002468static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri,
2469 uint64_t value)
Peter Maydell1047b9d2012-06-20 11:57:15 +00002470{
2471 /* Wait-for-interrupt (deprecated) */
Andreas Färberc3affe52013-01-18 15:03:43 +01002472 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT);
Peter Maydell1047b9d2012-06-20 11:57:15 +00002473}
2474
Peter Maydellc4241c72014-02-20 10:35:54 +00002475static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri,
2476 uint64_t value)
Peter Maydellc4804212012-06-20 11:57:17 +00002477{
2478 /* On OMAP there are registers indicating the max/min index of dcache lines
2479 * containing a dirty line; cache flush operations have to reset these.
2480 */
2481 env->cp15.c15_i_max = 0x000;
2482 env->cp15.c15_i_min = 0xff0;
Peter Maydellc4804212012-06-20 11:57:17 +00002483}
2484
Peter Maydell18032be2012-06-20 11:57:13 +00002485static const ARMCPRegInfo omap_cp_reginfo[] = {
2486 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY,
2487 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE,
Edgar E. Iglesiasd81c5192014-05-27 17:09:51 +01002488 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]),
Rob Herring6cd8a262014-04-15 19:18:42 +01002489 .resetvalue = 0, },
Peter Maydell1047b9d2012-06-20 11:57:15 +00002490 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0,
2491 .access = PL1_RW, .type = ARM_CP_NOP },
2492 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0,
2493 .access = PL1_RW,
2494 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0,
2495 .writefn = omap_ticonfig_write },
2496 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0,
2497 .access = PL1_RW,
2498 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, },
2499 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0,
2500 .access = PL1_RW, .resetvalue = 0xff0,
2501 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) },
2502 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0,
2503 .access = PL1_RW,
2504 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0,
2505 .writefn = omap_threadid_write },
2506 { .name = "TI925T_STATUS", .cp = 15, .crn = 15,
2507 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002508 .type = ARM_CP_NO_RAW,
Peter Maydell1047b9d2012-06-20 11:57:15 +00002509 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, },
2510 /* TODO: Peripheral port remap register:
2511 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller
2512 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff),
2513 * when MMU is off.
2514 */
Peter Maydellc4804212012-06-20 11:57:17 +00002515 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002516 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002517 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW,
Peter Maydellc4804212012-06-20 11:57:17 +00002518 .writefn = omap_cachemaint_write },
Peter Maydell34f90522012-06-20 11:57:18 +00002519 { .name = "C9", .cp = 15, .crn = 9,
2520 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW,
2521 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 },
Peter Maydell1047b9d2012-06-20 11:57:15 +00002522 REGINFO_SENTINEL
2523};
2524
Peter Maydellc4241c72014-02-20 10:35:54 +00002525static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri,
2526 uint64_t value)
Peter Maydell1047b9d2012-06-20 11:57:15 +00002527{
Peter Maydellc0f4af12014-09-29 18:48:48 +01002528 env->cp15.c15_cpar = value & 0x3fff;
Peter Maydell1047b9d2012-06-20 11:57:15 +00002529}
2530
2531static const ARMCPRegInfo xscale_cp_reginfo[] = {
2532 { .name = "XSCALE_CPAR",
2533 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW,
2534 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0,
2535 .writefn = xscale_cpar_write, },
Peter Maydell2771db22012-06-20 11:57:18 +00002536 { .name = "XSCALE_AUXCR",
2537 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW,
2538 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr),
2539 .resetvalue = 0, },
Peter Maydell3b771572014-05-01 15:24:44 +01002540 /* XScale specific cache-lockdown: since we have no cache we NOP these
2541 * and hope the guest does not really rely on cache behaviour.
2542 */
2543 { .name = "XSCALE_LOCK_ICACHE_LINE",
2544 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0,
2545 .access = PL1_W, .type = ARM_CP_NOP },
2546 { .name = "XSCALE_UNLOCK_ICACHE",
2547 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1,
2548 .access = PL1_W, .type = ARM_CP_NOP },
2549 { .name = "XSCALE_DCACHE_LOCK",
2550 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0,
2551 .access = PL1_RW, .type = ARM_CP_NOP },
2552 { .name = "XSCALE_UNLOCK_DCACHE",
2553 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1,
2554 .access = PL1_W, .type = ARM_CP_NOP },
Peter Maydell1047b9d2012-06-20 11:57:15 +00002555 REGINFO_SENTINEL
2556};
2557
2558static const ARMCPRegInfo dummy_c15_cp_reginfo[] = {
2559 /* RAZ/WI the whole crn=15 space, when we don't have a more specific
2560 * implementation of this implementation-defined space.
2561 * Ideally this should eventually disappear in favour of actually
2562 * implementing the correct behaviour for all cores.
2563 */
2564 { .name = "C15_IMPDEF", .cp = 15, .crn = 15,
2565 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
Peter Crosthwaite3671cd82013-12-17 19:42:27 +00002566 .access = PL1_RW,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002567 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002568 .resetvalue = 0 },
Peter Maydell18032be2012-06-20 11:57:13 +00002569 REGINFO_SENTINEL
2570};
2571
Peter Maydellc4804212012-06-20 11:57:17 +00002572static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = {
2573 /* Cache status: RAZ because we have no cache so it's always clean */
2574 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002575 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002576 .resetvalue = 0 },
Peter Maydellc4804212012-06-20 11:57:17 +00002577 REGINFO_SENTINEL
2578};
2579
2580static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = {
2581 /* We never have a a block transfer operation in progress */
2582 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002583 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002584 .resetvalue = 0 },
Peter Maydell30b05bb2012-06-20 11:57:22 +00002585 /* The cache ops themselves: these all NOP for QEMU */
2586 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0,
2587 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2588 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0,
2589 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2590 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0,
2591 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2592 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1,
2593 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2594 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2,
2595 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
2596 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0,
2597 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT },
Peter Maydellc4804212012-06-20 11:57:17 +00002598 REGINFO_SENTINEL
2599};
2600
2601static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = {
2602 /* The cache test-and-clean instructions always return (1 << 30)
2603 * to indicate that there are no dirty cache lines.
2604 */
2605 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002606 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002607 .resetvalue = (1 << 30) },
Peter Maydellc4804212012-06-20 11:57:17 +00002608 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002609 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002610 .resetvalue = (1 << 30) },
Peter Maydellc4804212012-06-20 11:57:17 +00002611 REGINFO_SENTINEL
2612};
2613
Peter Maydell34f90522012-06-20 11:57:18 +00002614static const ARMCPRegInfo strongarm_cp_reginfo[] = {
2615 /* Ignore ReadBuffer accesses */
2616 { .name = "C9_READBUFFER", .cp = 15, .crn = 9,
2617 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY,
Peter Maydelld4e6df62013-06-25 18:16:07 +01002618 .access = PL1_RW, .resetvalue = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002619 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW },
Peter Maydell34f90522012-06-20 11:57:18 +00002620 REGINFO_SENTINEL
2621};
2622
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01002623static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2624{
2625 ARMCPU *cpu = arm_env_get_cpu(env);
2626 unsigned int cur_el = arm_current_el(env);
2627 bool secure = arm_is_secure(env);
2628
2629 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2630 return env->cp15.vpidr_el2;
2631 }
2632 return raw_read(env, ri);
2633}
2634
Edgar E. Iglesias06a7e642015-09-14 14:39:51 +01002635static uint64_t mpidr_read_val(CPUARMState *env)
Peter Maydell81bdde92012-06-20 11:57:20 +00002636{
Pavel Fedineb5e1d32015-06-15 18:06:09 +01002637 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env));
2638 uint64_t mpidr = cpu->mp_affinity;
2639
Peter Maydell81bdde92012-06-20 11:57:20 +00002640 if (arm_feature(env, ARM_FEATURE_V7MP)) {
Peter Maydell78dbbbe2013-09-10 19:09:32 +01002641 mpidr |= (1U << 31);
Peter Maydell81bdde92012-06-20 11:57:20 +00002642 /* Cores which are uniprocessor (non-coherent)
2643 * but still implement the MP extensions set
Peter Crosthwaitea8e81b32015-06-15 18:06:10 +01002644 * bit 30. (For instance, Cortex-R5).
Peter Maydell81bdde92012-06-20 11:57:20 +00002645 */
Peter Crosthwaitea8e81b32015-06-15 18:06:10 +01002646 if (cpu->mp_is_up) {
2647 mpidr |= (1u << 30);
2648 }
Peter Maydell81bdde92012-06-20 11:57:20 +00002649 }
Peter Maydellc4241c72014-02-20 10:35:54 +00002650 return mpidr;
Peter Maydell81bdde92012-06-20 11:57:20 +00002651}
2652
Edgar E. Iglesias06a7e642015-09-14 14:39:51 +01002653static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri)
2654{
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01002655 unsigned int cur_el = arm_current_el(env);
2656 bool secure = arm_is_secure(env);
2657
2658 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) {
2659 return env->cp15.vmpidr_el2;
2660 }
Edgar E. Iglesias06a7e642015-09-14 14:39:51 +01002661 return mpidr_read_val(env);
2662}
2663
Peter Maydell81bdde92012-06-20 11:57:20 +00002664static const ARMCPRegInfo mpidr_cp_reginfo[] = {
Peter Maydell4b7fff22014-02-26 17:20:04 +00002665 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH,
2666 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002667 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW },
Peter Maydell81bdde92012-06-20 11:57:20 +00002668 REGINFO_SENTINEL
2669};
2670
Peter Maydell7ac681c2012-07-12 10:59:07 +00002671static const ARMCPRegInfo lpae_cp_reginfo[] = {
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +01002672 /* NOP AMAIR0/1 */
Peter Maydellb0fe2422014-02-26 17:20:03 +00002673 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH,
2674 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0,
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +01002675 .access = PL1_RW, .type = ARM_CP_CONST,
Peter Maydell7ac681c2012-07-12 10:59:07 +00002676 .resetvalue = 0 },
Peter Maydellb0fe2422014-02-26 17:20:03 +00002677 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */
Peter Maydell7ac681c2012-07-12 10:59:07 +00002678 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1,
Edgar E. Iglesiasa903c442015-06-02 14:56:21 +01002679 .access = PL1_RW, .type = ARM_CP_CONST,
Peter Maydell7ac681c2012-07-12 10:59:07 +00002680 .resetvalue = 0 },
Peter Maydell891a2fe2012-07-12 10:59:09 +00002681 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0,
Fabian Aggeler01c097f2014-12-11 12:07:52 +00002682 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0,
2683 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s),
2684 offsetof(CPUARMState, cp15.par_ns)} },
Peter Maydell891a2fe2012-07-12 10:59:09 +00002685 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002686 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00002687 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s),
2688 offsetof(CPUARMState, cp15.ttbr0_ns) },
Sergey Fedorovb061a822015-06-19 14:17:44 +01002689 .writefn = vmsa_ttbr_write, },
Peter Maydell891a2fe2012-07-12 10:59:09 +00002690 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00002691 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00002692 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s),
2693 offsetof(CPUARMState, cp15.ttbr1_ns) },
Sergey Fedorovb061a822015-06-19 14:17:44 +01002694 .writefn = vmsa_ttbr_write, },
Peter Maydell7ac681c2012-07-12 10:59:07 +00002695 REGINFO_SENTINEL
2696};
2697
Peter Maydellc4241c72014-02-20 10:35:54 +00002698static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00002699{
Peter Maydellc4241c72014-02-20 10:35:54 +00002700 return vfp_get_fpcr(env);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00002701}
2702
Peter Maydellc4241c72014-02-20 10:35:54 +00002703static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2704 uint64_t value)
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00002705{
2706 vfp_set_fpcr(env, value);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00002707}
2708
Peter Maydellc4241c72014-02-20 10:35:54 +00002709static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00002710{
Peter Maydellc4241c72014-02-20 10:35:54 +00002711 return vfp_get_fpsr(env);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00002712}
2713
Peter Maydellc4241c72014-02-20 10:35:54 +00002714static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri,
2715 uint64_t value)
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00002716{
2717 vfp_set_fpsr(env, value);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00002718}
2719
Peter Maydell3f208fd2016-02-11 11:17:31 +00002720static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri,
2721 bool isread)
Peter Maydellc2b820f2014-04-15 19:18:37 +01002722{
Fabian Aggeler137feaa2014-12-11 12:07:50 +00002723 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) {
Peter Maydellc2b820f2014-04-15 19:18:37 +01002724 return CP_ACCESS_TRAP;
2725 }
2726 return CP_ACCESS_OK;
2727}
2728
2729static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri,
2730 uint64_t value)
2731{
2732 env->daif = value & PSTATE_DAIF;
2733}
2734
Peter Maydell8af35c32014-02-26 17:20:02 +00002735static CPAccessResult aa64_cacheop_access(CPUARMState *env,
Peter Maydell3f208fd2016-02-11 11:17:31 +00002736 const ARMCPRegInfo *ri,
2737 bool isread)
Peter Maydell8af35c32014-02-26 17:20:02 +00002738{
2739 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless
2740 * SCTLR_EL1.UCI is set.
2741 */
Fabian Aggeler137feaa2014-12-11 12:07:50 +00002742 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) {
Peter Maydell8af35c32014-02-26 17:20:02 +00002743 return CP_ACCESS_TRAP;
2744 }
2745 return CP_ACCESS_OK;
2746}
2747
Alex Bennéedbb1fb22014-08-04 14:41:56 +01002748/* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions
2749 * Page D4-1736 (DDI0487A.b)
2750 */
2751
Peter Maydellfd3ed962015-08-25 15:45:09 +01002752static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2753 uint64_t value)
Peter Maydell168aa232014-02-26 17:20:03 +00002754{
Andreas Färber31b030d2013-09-04 01:29:02 +02002755 ARMCPU *cpu = arm_env_get_cpu(env);
Peter Maydellfd3ed962015-08-25 15:45:09 +01002756 CPUState *cs = CPU(cpu);
Alex Bennéedbb1fb22014-08-04 14:41:56 +01002757
Peter Maydellfd3ed962015-08-25 15:45:09 +01002758 if (arm_is_secure_below_el3(env)) {
2759 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2760 } else {
2761 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2762 }
Peter Maydell168aa232014-02-26 17:20:03 +00002763}
2764
Peter Maydellfd3ed962015-08-25 15:45:09 +01002765static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2766 uint64_t value)
Peter Maydell168aa232014-02-26 17:20:03 +00002767{
Peter Maydellfd3ed962015-08-25 15:45:09 +01002768 bool sec = arm_is_secure_below_el3(env);
2769 CPUState *other_cs;
Alex Bennéedbb1fb22014-08-04 14:41:56 +01002770
Peter Maydellfd3ed962015-08-25 15:45:09 +01002771 CPU_FOREACH(other_cs) {
2772 if (sec) {
2773 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2774 } else {
2775 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2776 ARMMMUIdx_S12NSE0, -1);
2777 }
2778 }
Peter Maydell168aa232014-02-26 17:20:03 +00002779}
2780
Peter Maydellfd3ed962015-08-25 15:45:09 +01002781static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2782 uint64_t value)
2783{
2784 /* Note that the 'ALL' scope must invalidate both stage 1 and
2785 * stage 2 translations, whereas most other scopes only invalidate
2786 * stage 1 translations.
2787 */
2788 ARMCPU *cpu = arm_env_get_cpu(env);
2789 CPUState *cs = CPU(cpu);
2790
2791 if (arm_is_secure_below_el3(env)) {
2792 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2793 } else {
2794 if (arm_feature(env, ARM_FEATURE_EL2)) {
2795 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0,
2796 ARMMMUIdx_S2NS, -1);
2797 } else {
2798 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1);
2799 }
2800 }
2801}
2802
2803static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2804 uint64_t value)
2805{
2806 ARMCPU *cpu = arm_env_get_cpu(env);
2807 CPUState *cs = CPU(cpu);
2808
2809 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1);
2810}
2811
Peter Maydell43efaa32015-08-25 15:45:10 +01002812static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2813 uint64_t value)
2814{
2815 ARMCPU *cpu = arm_env_get_cpu(env);
2816 CPUState *cs = CPU(cpu);
2817
2818 tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1);
2819}
2820
Peter Maydellfd3ed962015-08-25 15:45:09 +01002821static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2822 uint64_t value)
2823{
2824 /* Note that the 'ALL' scope must invalidate both stage 1 and
2825 * stage 2 translations, whereas most other scopes only invalidate
2826 * stage 1 translations.
2827 */
2828 bool sec = arm_is_secure_below_el3(env);
2829 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2);
2830 CPUState *other_cs;
2831
2832 CPU_FOREACH(other_cs) {
2833 if (sec) {
2834 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1);
2835 } else if (has_el2) {
2836 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2837 ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1);
2838 } else {
2839 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1,
2840 ARMMMUIdx_S12NSE0, -1);
2841 }
2842 }
2843}
2844
Peter Maydell2bfb9d72015-08-25 15:45:09 +01002845static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2846 uint64_t value)
2847{
2848 CPUState *other_cs;
2849
2850 CPU_FOREACH(other_cs) {
2851 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1);
2852 }
2853}
2854
Peter Maydell43efaa32015-08-25 15:45:10 +01002855static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2856 uint64_t value)
2857{
2858 CPUState *other_cs;
2859
2860 CPU_FOREACH(other_cs) {
2861 tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1);
2862 }
2863}
2864
Peter Maydellfd3ed962015-08-25 15:45:09 +01002865static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri,
Peter Maydell168aa232014-02-26 17:20:03 +00002866 uint64_t value)
2867{
Peter Maydellfd3ed962015-08-25 15:45:09 +01002868 /* Invalidate by VA, EL1&0 (AArch64 version).
2869 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1,
2870 * since we don't support flush-for-specific-ASID-only or
2871 * flush-last-level-only.
2872 */
Andreas Färber00c8cb02013-09-04 02:19:44 +02002873 ARMCPU *cpu = arm_env_get_cpu(env);
Peter Maydellfd3ed962015-08-25 15:45:09 +01002874 CPUState *cs = CPU(cpu);
2875 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2876
2877 if (arm_is_secure_below_el3(env)) {
2878 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1,
2879 ARMMMUIdx_S1SE0, -1);
2880 } else {
2881 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1,
2882 ARMMMUIdx_S12NSE0, -1);
2883 }
Peter Maydell168aa232014-02-26 17:20:03 +00002884}
2885
Peter Maydellfd3ed962015-08-25 15:45:09 +01002886static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri,
2887 uint64_t value)
2888{
2889 /* Invalidate by VA, EL2
2890 * Currently handles both VAE2 and VALE2, since we don't support
2891 * flush-last-level-only.
2892 */
2893 ARMCPU *cpu = arm_env_get_cpu(env);
2894 CPUState *cs = CPU(cpu);
2895 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2896
2897 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1);
2898}
2899
Peter Maydell43efaa32015-08-25 15:45:10 +01002900static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri,
2901 uint64_t value)
2902{
2903 /* Invalidate by VA, EL3
2904 * Currently handles both VAE3 and VALE3, since we don't support
2905 * flush-last-level-only.
2906 */
2907 ARMCPU *cpu = arm_env_get_cpu(env);
2908 CPUState *cs = CPU(cpu);
2909 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2910
2911 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1);
2912}
2913
Peter Maydellfd3ed962015-08-25 15:45:09 +01002914static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2915 uint64_t value)
2916{
2917 bool sec = arm_is_secure_below_el3(env);
2918 CPUState *other_cs;
2919 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2920
2921 CPU_FOREACH(other_cs) {
2922 if (sec) {
2923 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1,
2924 ARMMMUIdx_S1SE0, -1);
2925 } else {
2926 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1,
2927 ARMMMUIdx_S12NSE0, -1);
2928 }
2929 }
2930}
2931
2932static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2933 uint64_t value)
Peter Maydellfa439fc2014-09-12 14:06:50 +01002934{
2935 CPUState *other_cs;
2936 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2937
2938 CPU_FOREACH(other_cs) {
Peter Maydellfd3ed962015-08-25 15:45:09 +01002939 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1);
Peter Maydellfa439fc2014-09-12 14:06:50 +01002940 }
2941}
2942
Peter Maydell43efaa32015-08-25 15:45:10 +01002943static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2944 uint64_t value)
2945{
2946 CPUState *other_cs;
2947 uint64_t pageaddr = sextract64(value << 12, 0, 56);
2948
2949 CPU_FOREACH(other_cs) {
2950 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1);
2951 }
2952}
2953
Peter Maydellcea66e92015-08-25 15:45:10 +01002954static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri,
2955 uint64_t value)
2956{
2957 /* Invalidate by IPA. This has to invalidate any structures that
2958 * contain only stage 2 translation information, but does not need
2959 * to apply to structures that contain combined stage 1 and stage 2
2960 * translation information.
2961 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero.
2962 */
2963 ARMCPU *cpu = arm_env_get_cpu(env);
2964 CPUState *cs = CPU(cpu);
2965 uint64_t pageaddr;
2966
2967 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2968 return;
2969 }
2970
2971 pageaddr = sextract64(value << 12, 0, 48);
2972
2973 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1);
2974}
2975
2976static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri,
2977 uint64_t value)
2978{
2979 CPUState *other_cs;
2980 uint64_t pageaddr;
2981
2982 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) {
2983 return;
2984 }
2985
2986 pageaddr = sextract64(value << 12, 0, 48);
2987
2988 CPU_FOREACH(other_cs) {
2989 tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1);
2990 }
2991}
2992
Peter Maydell3f208fd2016-02-11 11:17:31 +00002993static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri,
2994 bool isread)
Peter Maydellaca3f402014-04-15 19:18:41 +01002995{
2996 /* We don't implement EL2, so the only control on DC ZVA is the
2997 * bit in the SCTLR which can prohibit access for EL0.
2998 */
Fabian Aggeler137feaa2014-12-11 12:07:50 +00002999 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) {
Peter Maydellaca3f402014-04-15 19:18:41 +01003000 return CP_ACCESS_TRAP;
3001 }
3002 return CP_ACCESS_OK;
3003}
3004
3005static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri)
3006{
3007 ARMCPU *cpu = arm_env_get_cpu(env);
3008 int dzp_bit = 1 << 4;
3009
3010 /* DZP indicates whether DC ZVA access is allowed */
Peter Maydell3f208fd2016-02-11 11:17:31 +00003011 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) {
Peter Maydellaca3f402014-04-15 19:18:41 +01003012 dzp_bit = 0;
3013 }
3014 return cpu->dcz_blocksize | dzp_bit;
3015}
3016
Peter Maydell3f208fd2016-02-11 11:17:31 +00003017static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3018 bool isread)
Peter Maydellf502cfc2014-04-15 19:18:43 +01003019{
Stefan Weilcdcf1402014-08-04 14:41:55 +01003020 if (!(env->pstate & PSTATE_SP)) {
Peter Maydellf502cfc2014-04-15 19:18:43 +01003021 /* Access to SP_EL0 is undefined if it's being used as
3022 * the stack pointer.
3023 */
3024 return CP_ACCESS_TRAP_UNCATEGORIZED;
3025 }
3026 return CP_ACCESS_OK;
3027}
3028
3029static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri)
3030{
3031 return env->pstate & PSTATE_SP;
3032}
3033
3034static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val)
3035{
3036 update_spsel(env, val);
3037}
3038
Fabian Aggeler137feaa2014-12-11 12:07:50 +00003039static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3040 uint64_t value)
3041{
3042 ARMCPU *cpu = arm_env_get_cpu(env);
3043
3044 if (raw_read(env, ri) == value) {
3045 /* Skip the TLB flush if nothing actually changed; Linux likes
3046 * to do a lot of pointless SCTLR writes.
3047 */
3048 return;
3049 }
3050
3051 raw_write(env, ri, value);
3052 /* ??? Lots of these bits are not implemented. */
3053 /* This may enable/disable the MMU, so do a TLB flush. */
3054 tlb_flush(CPU(cpu), 1);
3055}
3056
Peter Maydell3f208fd2016-02-11 11:17:31 +00003057static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri,
3058 bool isread)
Peter Maydell03fbf202016-01-21 14:15:09 +00003059{
3060 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) {
Peter Maydellf2cae602016-02-18 14:16:16 +00003061 return CP_ACCESS_TRAP_FP_EL2;
Peter Maydell03fbf202016-01-21 14:15:09 +00003062 }
3063 if (env->cp15.cptr_el[3] & CPTR_TFP) {
Peter Maydellf2cae602016-02-18 14:16:16 +00003064 return CP_ACCESS_TRAP_FP_EL3;
Peter Maydell03fbf202016-01-21 14:15:09 +00003065 }
3066 return CP_ACCESS_OK;
3067}
3068
Peter Maydella8d64e72016-02-19 14:39:43 +00003069static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
3070 uint64_t value)
3071{
3072 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK;
3073}
3074
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00003075static const ARMCPRegInfo v8_cp_reginfo[] = {
3076 /* Minimal set of EL0-visible registers. This will need to be expanded
3077 * significantly for system emulation of AArch64 CPUs.
3078 */
3079 { .name = "NZCV", .state = ARM_CP_STATE_AA64,
3080 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2,
3081 .access = PL0_RW, .type = ARM_CP_NZCV },
Peter Maydellc2b820f2014-04-15 19:18:37 +01003082 { .name = "DAIF", .state = ARM_CP_STATE_AA64,
3083 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003084 .type = ARM_CP_NO_RAW,
Peter Maydellc2b820f2014-04-15 19:18:37 +01003085 .access = PL0_RW, .accessfn = aa64_daif_access,
3086 .fieldoffset = offsetof(CPUARMState, daif),
3087 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore },
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00003088 { .name = "FPCR", .state = ARM_CP_STATE_AA64,
3089 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4,
3090 .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write },
3091 { .name = "FPSR", .state = ARM_CP_STATE_AA64,
3092 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4,
3093 .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write },
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00003094 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64,
3095 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003096 .access = PL0_R, .type = ARM_CP_NO_RAW,
Peter Maydellaca3f402014-04-15 19:18:41 +01003097 .readfn = aa64_dczid_read },
3098 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64,
3099 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1,
3100 .access = PL0_W, .type = ARM_CP_DC_ZVA,
3101#ifndef CONFIG_USER_ONLY
3102 /* Avoid overhead of an access check that always passes in user-mode */
3103 .accessfn = aa64_zva_access,
3104#endif
3105 },
Peter Maydell0eef9d92014-02-26 17:20:02 +00003106 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64,
3107 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2,
3108 .access = PL1_R, .type = ARM_CP_CURRENTEL },
Peter Maydell8af35c32014-02-26 17:20:02 +00003109 /* Cache ops: all NOPs since we don't emulate caches */
3110 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64,
3111 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3112 .access = PL1_W, .type = ARM_CP_NOP },
3113 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64,
3114 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3115 .access = PL1_W, .type = ARM_CP_NOP },
3116 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64,
3117 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1,
3118 .access = PL0_W, .type = ARM_CP_NOP,
3119 .accessfn = aa64_cacheop_access },
3120 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64,
3121 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3122 .access = PL1_W, .type = ARM_CP_NOP },
3123 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64,
3124 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3125 .access = PL1_W, .type = ARM_CP_NOP },
3126 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64,
3127 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1,
3128 .access = PL0_W, .type = ARM_CP_NOP,
3129 .accessfn = aa64_cacheop_access },
3130 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64,
3131 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3132 .access = PL1_W, .type = ARM_CP_NOP },
3133 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64,
3134 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1,
3135 .access = PL0_W, .type = ARM_CP_NOP,
3136 .accessfn = aa64_cacheop_access },
3137 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64,
3138 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1,
3139 .access = PL0_W, .type = ARM_CP_NOP,
3140 .accessfn = aa64_cacheop_access },
3141 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64,
3142 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3143 .access = PL1_W, .type = ARM_CP_NOP },
Peter Maydell168aa232014-02-26 17:20:03 +00003144 /* TLBI operations */
3145 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003146 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003147 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003148 .writefn = tlbi_aa64_vmalle1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003149 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003150 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003151 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003152 .writefn = tlbi_aa64_vae1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003153 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003154 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003155 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003156 .writefn = tlbi_aa64_vmalle1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003157 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003158 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003159 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003160 .writefn = tlbi_aa64_vae1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003161 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003162 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003163 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003164 .writefn = tlbi_aa64_vae1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003165 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003166 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003167 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003168 .writefn = tlbi_aa64_vae1is_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003169 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003170 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003171 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003172 .writefn = tlbi_aa64_vmalle1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003173 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003174 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003175 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003176 .writefn = tlbi_aa64_vae1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003177 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003178 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003179 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003180 .writefn = tlbi_aa64_vmalle1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003181 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003182 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003183 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003184 .writefn = tlbi_aa64_vae1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003185 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003186 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003187 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003188 .writefn = tlbi_aa64_vae1_write },
Peter Maydell168aa232014-02-26 17:20:03 +00003189 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias6ab9f492014-05-01 15:24:46 +01003190 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003191 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003192 .writefn = tlbi_aa64_vae1_write },
Peter Maydellcea66e92015-08-25 15:45:10 +01003193 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64,
3194 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1,
3195 .access = PL2_W, .type = ARM_CP_NO_RAW,
3196 .writefn = tlbi_aa64_ipas2e1is_write },
3197 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64,
3198 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5,
3199 .access = PL2_W, .type = ARM_CP_NO_RAW,
3200 .writefn = tlbi_aa64_ipas2e1is_write },
Peter Maydell83ddf972015-08-25 15:45:09 +01003201 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64,
3202 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4,
3203 .access = PL2_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003204 .writefn = tlbi_aa64_alle1is_write },
Peter Maydell43efaa32015-08-25 15:45:10 +01003205 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64,
3206 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6,
3207 .access = PL2_W, .type = ARM_CP_NO_RAW,
3208 .writefn = tlbi_aa64_alle1is_write },
Peter Maydellcea66e92015-08-25 15:45:10 +01003209 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64,
3210 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1,
3211 .access = PL2_W, .type = ARM_CP_NO_RAW,
3212 .writefn = tlbi_aa64_ipas2e1_write },
3213 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64,
3214 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5,
3215 .access = PL2_W, .type = ARM_CP_NO_RAW,
3216 .writefn = tlbi_aa64_ipas2e1_write },
Peter Maydell83ddf972015-08-25 15:45:09 +01003217 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64,
3218 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4,
3219 .access = PL2_W, .type = ARM_CP_NO_RAW,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003220 .writefn = tlbi_aa64_alle1_write },
Peter Maydell43efaa32015-08-25 15:45:10 +01003221 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64,
3222 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6,
3223 .access = PL2_W, .type = ARM_CP_NO_RAW,
3224 .writefn = tlbi_aa64_alle1is_write },
Peter Maydell19525522014-04-15 19:18:48 +01003225#ifndef CONFIG_USER_ONLY
3226 /* 64 bit address translation operations */
3227 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64,
3228 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0,
Peter Maydell060e8a42015-02-05 13:37:24 +00003229 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
Peter Maydell19525522014-04-15 19:18:48 +01003230 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64,
3231 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1,
Peter Maydell060e8a42015-02-05 13:37:24 +00003232 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
Peter Maydell19525522014-04-15 19:18:48 +01003233 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64,
3234 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2,
Peter Maydell060e8a42015-02-05 13:37:24 +00003235 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
Peter Maydell19525522014-04-15 19:18:48 +01003236 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64,
3237 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3,
Peter Maydell060e8a42015-02-05 13:37:24 +00003238 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
Peter Maydell2a47df92015-08-25 15:45:08 +01003239 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias7a379c72015-09-08 17:38:44 +01003240 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4,
Peter Maydell2a47df92015-08-25 15:45:08 +01003241 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3242 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias7a379c72015-09-08 17:38:44 +01003243 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5,
Peter Maydell2a47df92015-08-25 15:45:08 +01003244 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3245 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias7a379c72015-09-08 17:38:44 +01003246 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6,
Peter Maydell2a47df92015-08-25 15:45:08 +01003247 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3248 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesias7a379c72015-09-08 17:38:44 +01003249 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7,
Peter Maydell2a47df92015-08-25 15:45:08 +01003250 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3251 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */
3252 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64,
3253 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0,
3254 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3255 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64,
3256 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1,
3257 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
Edgar E. Iglesiasc96fc9b2015-09-08 17:38:44 +01003258 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64,
3259 .type = ARM_CP_ALIAS,
3260 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0,
3261 .access = PL1_RW, .resetvalue = 0,
3262 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]),
3263 .writefn = par_write },
Peter Maydell19525522014-04-15 19:18:48 +01003264#endif
Peter Maydell995939a2014-09-12 14:06:50 +01003265 /* TLB invalidate last level of translation table walk */
Peter Maydell9449fdf2014-04-15 19:18:47 +01003266 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003267 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write },
Peter Maydell9449fdf2014-04-15 19:18:47 +01003268 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003269 .type = ARM_CP_NO_RAW, .access = PL1_W,
Peter Maydellfa439fc2014-09-12 14:06:50 +01003270 .writefn = tlbimvaa_is_write },
Peter Maydell9449fdf2014-04-15 19:18:47 +01003271 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003272 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write },
Peter Maydell9449fdf2014-04-15 19:18:47 +01003273 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003274 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write },
Peter Maydell9449fdf2014-04-15 19:18:47 +01003275 /* 32 bit cache operations */
3276 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0,
3277 .type = ARM_CP_NOP, .access = PL1_W },
3278 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6,
3279 .type = ARM_CP_NOP, .access = PL1_W },
3280 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0,
3281 .type = ARM_CP_NOP, .access = PL1_W },
3282 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1,
3283 .type = ARM_CP_NOP, .access = PL1_W },
3284 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6,
3285 .type = ARM_CP_NOP, .access = PL1_W },
3286 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7,
3287 .type = ARM_CP_NOP, .access = PL1_W },
3288 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1,
3289 .type = ARM_CP_NOP, .access = PL1_W },
3290 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2,
3291 .type = ARM_CP_NOP, .access = PL1_W },
3292 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1,
3293 .type = ARM_CP_NOP, .access = PL1_W },
3294 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2,
3295 .type = ARM_CP_NOP, .access = PL1_W },
3296 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1,
3297 .type = ARM_CP_NOP, .access = PL1_W },
3298 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1,
3299 .type = ARM_CP_NOP, .access = PL1_W },
3300 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2,
3301 .type = ARM_CP_NOP, .access = PL1_W },
3302 /* MMU Domain access control / MPU write buffer control */
Fabian Aggeler0c17d682014-12-11 12:07:51 +00003303 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0,
3304 .access = PL1_RW, .resetvalue = 0,
3305 .writefn = dacr_write, .raw_writefn = raw_write,
3306 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s),
3307 offsetoflow32(CPUARMState, cp15.dacr_ns) } },
Peter Maydella0618a12014-04-15 19:18:42 +01003308 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003309 .type = ARM_CP_ALIAS,
Peter Maydella0618a12014-04-15 19:18:42 +01003310 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1,
Edgar E. Iglesias6947f052014-05-27 17:09:51 +01003311 .access = PL1_RW,
3312 .fieldoffset = offsetof(CPUARMState, elr_el[1]) },
Peter Maydella65f1de2014-04-15 19:18:43 +01003313 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003314 .type = ARM_CP_ALIAS,
Peter Maydella65f1de2014-04-15 19:18:43 +01003315 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0,
Soren Brinkmann99a99c12015-11-03 13:49:41 +00003316 .access = PL1_RW,
3317 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) },
Peter Maydellf502cfc2014-04-15 19:18:43 +01003318 /* We rely on the access checks not allowing the guest to write to the
3319 * state field when SPSel indicates that it's being used as the stack
3320 * pointer.
3321 */
3322 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64,
3323 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0,
3324 .access = PL1_RW, .accessfn = sp_el0_access,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003325 .type = ARM_CP_ALIAS,
Peter Maydellf502cfc2014-04-15 19:18:43 +01003326 .fieldoffset = offsetof(CPUARMState, sp_el[0]) },
Greg Bellows884b4de2015-02-05 13:37:22 +00003327 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64,
3328 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003329 .access = PL2_RW, .type = ARM_CP_ALIAS,
Greg Bellows884b4de2015-02-05 13:37:22 +00003330 .fieldoffset = offsetof(CPUARMState, sp_el[1]) },
Peter Maydellf502cfc2014-04-15 19:18:43 +01003331 { .name = "SPSel", .state = ARM_CP_STATE_AA64,
3332 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003333 .type = ARM_CP_NO_RAW,
Peter Maydellf502cfc2014-04-15 19:18:43 +01003334 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write },
Peter Maydell03fbf202016-01-21 14:15:09 +00003335 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64,
3336 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0,
3337 .type = ARM_CP_ALIAS,
3338 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]),
3339 .access = PL2_RW, .accessfn = fpexc32_access },
Peter Maydell6a43e0b2016-02-03 13:46:33 +00003340 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64,
3341 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0,
3342 .access = PL2_RW, .resetvalue = 0,
3343 .writefn = dacr_write, .raw_writefn = raw_write,
3344 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) },
3345 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64,
3346 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1,
3347 .access = PL2_RW, .resetvalue = 0,
3348 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) },
3349 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64,
3350 .type = ARM_CP_ALIAS,
3351 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0,
3352 .access = PL2_RW,
3353 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) },
3354 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64,
3355 .type = ARM_CP_ALIAS,
3356 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1,
3357 .access = PL2_RW,
3358 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) },
3359 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64,
3360 .type = ARM_CP_ALIAS,
3361 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2,
3362 .access = PL2_RW,
3363 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) },
3364 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64,
3365 .type = ARM_CP_ALIAS,
3366 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3,
3367 .access = PL2_RW,
3368 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) },
Peter Maydella8d64e72016-02-19 14:39:43 +00003369 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64,
3370 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1,
3371 .resetvalue = 0,
3372 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) },
3373 { .name = "SDCR", .type = ARM_CP_ALIAS,
3374 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1,
3375 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3376 .writefn = sdcr_write,
3377 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) },
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00003378 REGINFO_SENTINEL
3379};
3380
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01003381/* Used to describe the behaviour of EL2 regs when EL2 does not exist. */
Peter Maydell4771cd02015-06-01 19:18:36 +01003382static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = {
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01003383 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3384 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3385 .access = PL2_RW,
3386 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01003387 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003388 .type = ARM_CP_NO_RAW,
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01003389 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3390 .access = PL2_RW,
3391 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore },
Greg Bellowsc6f19162015-05-29 11:28:52 +01003392 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3393 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3394 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesias95f949a2015-06-02 14:56:21 +01003395 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3396 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3397 .access = PL2_RW, .type = ARM_CP_CONST,
3398 .resetvalue = 0 },
3399 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3400 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3401 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell2179ef92015-08-25 15:45:07 +01003402 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3403 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3404 .access = PL2_RW, .type = ARM_CP_CONST,
3405 .resetvalue = 0 },
3406 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3407 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3408 .access = PL2_RW, .type = ARM_CP_CONST,
3409 .resetvalue = 0 },
Peter Maydell37cd6c22015-08-25 15:45:07 +01003410 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3411 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3412 .access = PL2_RW, .type = ARM_CP_CONST,
3413 .resetvalue = 0 },
3414 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3415 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3416 .access = PL2_RW, .type = ARM_CP_CONST,
3417 .resetvalue = 0 },
Edgar E. Iglesias06ec4c82015-06-02 14:56:21 +01003418 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3419 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
3420 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01003421 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH,
3422 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
3423 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3424 .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01003425 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3426 .cp = 15, .opc1 = 6, .crm = 2,
3427 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3428 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 },
3429 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3430 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3431 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasb9cb5322015-06-02 14:56:21 +01003432 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3433 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3434 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasff05f372015-06-02 14:56:21 +01003435 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3436 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3437 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasa57633c2015-06-02 14:56:21 +01003438 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3439 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3440 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3441 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3442 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3443 .resetvalue = 0 },
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01003444 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3445 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3446 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01003447 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3448 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3449 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3450 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3451 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3452 .resetvalue = 0 },
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01003453 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3454 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3455 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3456 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3457 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST,
3458 .resetvalue = 0 },
3459 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3460 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
3461 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
3462 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3463 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3464 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 },
Sergey Fedorov14cc7b52015-10-16 11:14:54 +01003465 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3466 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00003467 .access = PL2_RW, .accessfn = access_tda,
3468 .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesias59e05532015-10-26 14:01:54 +01003469 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH,
3470 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3471 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
3472 .type = ARM_CP_CONST, .resetvalue = 0 },
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01003473 REGINFO_SENTINEL
3474};
3475
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01003476static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value)
3477{
3478 ARMCPU *cpu = arm_env_get_cpu(env);
3479 uint64_t valid_mask = HCR_MASK;
3480
3481 if (arm_feature(env, ARM_FEATURE_EL3)) {
3482 valid_mask &= ~HCR_HCD;
3483 } else {
3484 valid_mask &= ~HCR_TSC;
3485 }
3486
3487 /* Clear RES0 bits. */
3488 value &= valid_mask;
3489
3490 /* These bits change the MMU setup:
3491 * HCR_VM enables stage 2 translation
3492 * HCR_PTW forbids certain page-table setups
3493 * HCR_DC Disables stage1 and enables stage2 translation
3494 */
3495 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) {
3496 tlb_flush(CPU(cpu), 1);
3497 }
3498 raw_write(env, ri, value);
3499}
3500
Peter Maydell4771cd02015-06-01 19:18:36 +01003501static const ARMCPRegInfo el2_cp_reginfo[] = {
Edgar E. Iglesiasf149e3e2014-09-29 18:48:48 +01003502 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64,
3503 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0,
3504 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2),
3505 .writefn = hcr_write },
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01003506 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003507 .type = ARM_CP_ALIAS,
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01003508 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1,
3509 .access = PL2_RW,
3510 .fieldoffset = offsetof(CPUARMState, elr_el[2]) },
Edgar E. Iglesiasf2c30f42014-08-04 14:41:55 +01003511 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesiasf2c30f42014-08-04 14:41:55 +01003512 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0,
3513 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) },
Edgar E. Iglesias63b60552014-08-04 14:41:55 +01003514 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64,
3515 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0,
3516 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) },
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01003517 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003518 .type = ARM_CP_ALIAS,
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01003519 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0,
Soren Brinkmann99a99c12015-11-03 13:49:41 +00003520 .access = PL2_RW,
3521 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) },
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01003522 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64,
3523 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0,
3524 .access = PL2_RW, .writefn = vbar_write,
3525 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]),
3526 .resetvalue = 0 },
Greg Bellows884b4de2015-02-05 13:37:22 +00003527 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64,
3528 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003529 .access = PL3_RW, .type = ARM_CP_ALIAS,
Greg Bellows884b4de2015-02-05 13:37:22 +00003530 .fieldoffset = offsetof(CPUARMState, sp_el[2]) },
Greg Bellowsc6f19162015-05-29 11:28:52 +01003531 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH,
3532 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2,
3533 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0,
3534 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) },
Edgar E. Iglesias95f949a2015-06-02 14:56:21 +01003535 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH,
3536 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0,
3537 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]),
3538 .resetvalue = 0 },
3539 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3540 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1,
3541 .access = PL2_RW, .type = ARM_CP_ALIAS,
3542 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) },
Peter Maydell2179ef92015-08-25 15:45:07 +01003543 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH,
3544 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0,
3545 .access = PL2_RW, .type = ARM_CP_CONST,
3546 .resetvalue = 0 },
3547 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */
3548 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32,
3549 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1,
3550 .access = PL2_RW, .type = ARM_CP_CONST,
3551 .resetvalue = 0 },
Peter Maydell37cd6c22015-08-25 15:45:07 +01003552 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH,
3553 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0,
3554 .access = PL2_RW, .type = ARM_CP_CONST,
3555 .resetvalue = 0 },
3556 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH,
3557 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1,
3558 .access = PL2_RW, .type = ARM_CP_CONST,
3559 .resetvalue = 0 },
Edgar E. Iglesias06ec4c82015-06-02 14:56:21 +01003560 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH,
3561 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2,
Peter Maydell6459b942016-05-12 13:22:30 +01003562 .access = PL2_RW,
3563 /* no .writefn needed as this can't cause an ASID change;
3564 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3565 */
Edgar E. Iglesias06ec4c82015-06-02 14:56:21 +01003566 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) },
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01003567 { .name = "VTCR", .state = ARM_CP_STATE_AA32,
3568 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
Peter Maydellbf06c112016-04-04 17:33:52 +01003569 .type = ARM_CP_ALIAS,
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01003570 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3571 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
3572 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64,
3573 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2,
Peter Maydellbf06c112016-04-04 17:33:52 +01003574 .access = PL2_RW,
3575 /* no .writefn needed as this can't cause an ASID change;
3576 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3577 */
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01003578 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) },
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01003579 { .name = "VTTBR", .state = ARM_CP_STATE_AA32,
3580 .cp = 15, .opc1 = 6, .crm = 2,
3581 .type = ARM_CP_64BIT | ARM_CP_ALIAS,
3582 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3583 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2),
3584 .writefn = vttbr_write },
3585 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64,
3586 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0,
3587 .access = PL2_RW, .writefn = vttbr_write,
3588 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) },
Edgar E. Iglesiasb9cb5322015-06-02 14:56:21 +01003589 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH,
3590 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0,
3591 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write,
3592 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) },
Edgar E. Iglesiasff05f372015-06-02 14:56:21 +01003593 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH,
3594 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2,
3595 .access = PL2_RW, .resetvalue = 0,
3596 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) },
Edgar E. Iglesiasa57633c2015-06-02 14:56:21 +01003597 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64,
3598 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0,
3599 .access = PL2_RW, .resetvalue = 0,
3600 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
3601 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2,
3602 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS,
Edgar E. Iglesiasa57633c2015-06-02 14:56:21 +01003603 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) },
Edgar E. Iglesias51da9012015-06-02 14:56:22 +01003604 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64,
3605 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0,
3606 .type = ARM_CP_NO_RAW, .access = PL2_W,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003607 .writefn = tlbi_aa64_alle2_write },
Edgar E. Iglesias8742d492015-06-02 14:56:22 +01003608 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64,
3609 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1,
3610 .type = ARM_CP_NO_RAW, .access = PL2_W,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003611 .writefn = tlbi_aa64_vae2_write },
Peter Maydell2bfb9d72015-08-25 15:45:09 +01003612 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64,
3613 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5,
3614 .access = PL2_W, .type = ARM_CP_NO_RAW,
3615 .writefn = tlbi_aa64_vae2_write },
3616 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64,
3617 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0,
3618 .access = PL2_W, .type = ARM_CP_NO_RAW,
3619 .writefn = tlbi_aa64_alle2is_write },
Edgar E. Iglesias8742d492015-06-02 14:56:22 +01003620 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64,
3621 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1,
3622 .type = ARM_CP_NO_RAW, .access = PL2_W,
Peter Maydellfd3ed962015-08-25 15:45:09 +01003623 .writefn = tlbi_aa64_vae2is_write },
Peter Maydell2bfb9d72015-08-25 15:45:09 +01003624 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64,
3625 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5,
3626 .access = PL2_W, .type = ARM_CP_NO_RAW,
3627 .writefn = tlbi_aa64_vae2is_write },
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01003628#ifndef CONFIG_USER_ONLY
Peter Maydell2a47df92015-08-25 15:45:08 +01003629 /* Unlike the other EL2-related AT operations, these must
3630 * UNDEF from EL3 if EL2 is not implemented, which is why we
3631 * define them here rather than with the rest of the AT ops.
3632 */
3633 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64,
3634 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3635 .access = PL2_W, .accessfn = at_s1e2_access,
3636 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
3637 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64,
3638 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3639 .access = PL2_W, .accessfn = at_s1e2_access,
3640 .type = ARM_CP_NO_RAW, .writefn = ats_write64 },
Peter Maydell14db7fe2015-08-25 15:45:08 +01003641 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE
3642 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3
3643 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose
3644 * to behave as if SCR.NS was 1.
3645 */
3646 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0,
3647 .access = PL2_W,
3648 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
3649 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1,
3650 .access = PL2_W,
3651 .writefn = ats1h_write, .type = ARM_CP_NO_RAW },
Edgar E. Iglesias0b6440a2015-08-13 11:26:18 +01003652 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH,
3653 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0,
3654 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the
3655 * reset values as IMPDEF. We choose to reset to 3 to comply with
3656 * both ARMv7 and ARMv8.
3657 */
3658 .access = PL2_RW, .resetvalue = 3,
3659 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) },
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01003660 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64,
3661 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3,
3662 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0,
3663 .writefn = gt_cntvoff_write,
3664 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
3665 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14,
3666 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO,
3667 .writefn = gt_cntvoff_write,
3668 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) },
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01003669 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64,
3670 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2,
3671 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3672 .type = ARM_CP_IO, .access = PL2_RW,
3673 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3674 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14,
3675 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval),
3676 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO,
3677 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write },
3678 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH,
3679 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0,
Edgar E. Iglesiasd44ec152016-02-26 13:45:39 +01003680 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW,
Edgar E. Iglesiasb0e66d92015-08-13 11:26:18 +01003681 .resetfn = gt_hyp_timer_reset,
3682 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write },
3683 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH,
3684 .type = ARM_CP_IO,
3685 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1,
3686 .access = PL2_RW,
3687 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl),
3688 .resetvalue = 0,
3689 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write },
Edgar E. Iglesiasedac4d82015-08-13 11:26:17 +01003690#endif
Sergey Fedorov14cc7b52015-10-16 11:14:54 +01003691 /* The only field of MDCR_EL2 that has a defined architectural reset value
3692 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we
3693 * don't impelment any PMU event counters, so using zero as a reset
3694 * value for MDCR_EL2 is okay
3695 */
3696 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH,
3697 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1,
3698 .access = PL2_RW, .resetvalue = 0,
3699 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), },
Edgar E. Iglesias59e05532015-10-26 14:01:54 +01003700 { .name = "HPFAR", .state = ARM_CP_STATE_AA32,
3701 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3702 .access = PL2_RW, .accessfn = access_el3_aa32ns,
3703 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
3704 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64,
3705 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4,
3706 .access = PL2_RW,
3707 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) },
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01003708 REGINFO_SENTINEL
3709};
3710
Peter Maydell2f027fc2016-02-11 11:17:31 +00003711static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri,
3712 bool isread)
3713{
3714 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2.
3715 * At Secure EL1 it traps to EL3.
3716 */
3717 if (arm_current_el(env) == 3) {
3718 return CP_ACCESS_OK;
3719 }
3720 if (arm_is_secure_below_el3(env)) {
3721 return CP_ACCESS_TRAP_EL3;
3722 }
3723 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */
3724 if (isread) {
3725 return CP_ACCESS_OK;
3726 }
3727 return CP_ACCESS_TRAP_UNCATEGORIZED;
3728}
3729
Greg Bellows60fb1a82014-12-16 16:58:05 +00003730static const ARMCPRegInfo el3_cp_reginfo[] = {
3731 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64,
3732 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0,
3733 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3),
3734 .resetvalue = 0, .writefn = scr_write },
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003735 { .name = "SCR", .type = ARM_CP_ALIAS,
Greg Bellows60fb1a82014-12-16 16:58:05 +00003736 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0,
Peter Maydellefe4a272016-02-11 11:17:30 +00003737 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3738 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3),
Sergey Fedorovb061a822015-06-19 14:17:44 +01003739 .writefn = scr_write },
Greg Bellows60fb1a82014-12-16 16:58:05 +00003740 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64,
3741 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1,
3742 .access = PL3_RW, .resetvalue = 0,
3743 .fieldoffset = offsetof(CPUARMState, cp15.sder) },
3744 { .name = "SDER",
3745 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1,
3746 .access = PL3_RW, .resetvalue = 0,
3747 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) },
Greg Bellows60fb1a82014-12-16 16:58:05 +00003748 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
Peter Maydellefe4a272016-02-11 11:17:30 +00003749 .access = PL1_RW, .accessfn = access_trap_aa32s_el1,
3750 .writefn = vbar_write, .resetvalue = 0,
Greg Bellows60fb1a82014-12-16 16:58:05 +00003751 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) },
Fabian Aggeler7dd8c9a2014-12-11 12:07:51 +00003752 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64,
3753 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0,
3754 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0,
3755 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) },
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003756 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64,
3757 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2,
Peter Maydell6459b942016-05-12 13:22:30 +01003758 .access = PL3_RW,
3759 /* no .writefn needed as this can't cause an ASID change;
3760 * no .raw_writefn or .resetfn needed as we never use mask/base_mask
3761 */
Fabian Aggeler11f136e2014-12-11 12:07:51 +00003762 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) },
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01003763 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003764 .type = ARM_CP_ALIAS,
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01003765 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1,
3766 .access = PL3_RW,
3767 .fieldoffset = offsetof(CPUARMState, elr_el[3]) },
Edgar E. Iglesiasf2c30f42014-08-04 14:41:55 +01003768 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64,
Edgar E. Iglesiasf2c30f42014-08-04 14:41:55 +01003769 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0,
3770 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) },
Edgar E. Iglesias63b60552014-08-04 14:41:55 +01003771 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64,
3772 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0,
3773 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) },
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01003774 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003775 .type = ARM_CP_ALIAS,
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01003776 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0,
Soren Brinkmann99a99c12015-11-03 13:49:41 +00003777 .access = PL3_RW,
3778 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) },
Edgar E. Iglesiasa1ba1252014-05-27 17:09:55 +01003779 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64,
3780 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0,
3781 .access = PL3_RW, .writefn = vbar_write,
3782 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]),
3783 .resetvalue = 0 },
Greg Bellowsc6f19162015-05-29 11:28:52 +01003784 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64,
3785 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2,
3786 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0,
3787 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) },
Peter Maydell4cfb8ad2015-08-25 15:45:07 +01003788 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64,
3789 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2,
3790 .access = PL3_RW, .resetvalue = 0,
3791 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) },
Peter Maydell2179ef92015-08-25 15:45:07 +01003792 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64,
3793 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0,
3794 .access = PL3_RW, .type = ARM_CP_CONST,
3795 .resetvalue = 0 },
Peter Maydell37cd6c22015-08-25 15:45:07 +01003796 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH,
3797 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0,
3798 .access = PL3_RW, .type = ARM_CP_CONST,
3799 .resetvalue = 0 },
3800 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH,
3801 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1,
3802 .access = PL3_RW, .type = ARM_CP_CONST,
3803 .resetvalue = 0 },
Peter Maydell43efaa32015-08-25 15:45:10 +01003804 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64,
3805 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0,
3806 .access = PL3_W, .type = ARM_CP_NO_RAW,
3807 .writefn = tlbi_aa64_alle3is_write },
3808 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64,
3809 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1,
3810 .access = PL3_W, .type = ARM_CP_NO_RAW,
3811 .writefn = tlbi_aa64_vae3is_write },
3812 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64,
3813 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5,
3814 .access = PL3_W, .type = ARM_CP_NO_RAW,
3815 .writefn = tlbi_aa64_vae3is_write },
3816 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64,
3817 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0,
3818 .access = PL3_W, .type = ARM_CP_NO_RAW,
3819 .writefn = tlbi_aa64_alle3_write },
3820 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64,
3821 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1,
3822 .access = PL3_W, .type = ARM_CP_NO_RAW,
3823 .writefn = tlbi_aa64_vae3_write },
3824 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64,
3825 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5,
3826 .access = PL3_W, .type = ARM_CP_NO_RAW,
3827 .writefn = tlbi_aa64_vae3_write },
Fabian Aggeler0f1a3b22014-12-11 12:07:49 +00003828 REGINFO_SENTINEL
3829};
3830
Peter Maydell3f208fd2016-02-11 11:17:31 +00003831static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri,
3832 bool isread)
Peter Maydell7da845b2014-02-26 17:20:01 +00003833{
3834 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64,
3835 * but the AArch32 CTR has its own reginfo struct)
3836 */
Fabian Aggeler137feaa2014-12-11 12:07:50 +00003837 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) {
Peter Maydell7da845b2014-02-26 17:20:01 +00003838 return CP_ACCESS_TRAP;
3839 }
3840 return CP_ACCESS_OK;
3841}
3842
Davorin Mista1424ca82015-10-16 11:14:53 +01003843static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri,
3844 uint64_t value)
3845{
3846 /* Writes to OSLAR_EL1 may update the OS lock status, which can be
3847 * read via a bit in OSLSR_EL1.
3848 */
3849 int oslock;
3850
3851 if (ri->state == ARM_CP_STATE_AA32) {
3852 oslock = (value == 0xC5ACCE55);
3853 } else {
3854 oslock = value & 1;
3855 }
3856
3857 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock);
3858}
3859
Peter Maydell50300692014-08-19 18:56:25 +01003860static const ARMCPRegInfo debug_cp_reginfo[] = {
Peter Maydell50300692014-08-19 18:56:25 +01003861 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped
Peter Maydell10aae102014-08-19 18:56:25 +01003862 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1;
3863 * unlike DBGDRAR it is never accessible from EL0.
3864 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64
3865 * accessor.
Peter Maydell50300692014-08-19 18:56:25 +01003866 */
3867 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell91b0a232016-02-18 14:16:15 +00003868 .access = PL0_R, .accessfn = access_tdra,
3869 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell10aae102014-08-19 18:56:25 +01003870 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64,
3871 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
Peter Maydell91b0a232016-02-18 14:16:15 +00003872 .access = PL1_R, .accessfn = access_tdra,
3873 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell50300692014-08-19 18:56:25 +01003874 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydell91b0a232016-02-18 14:16:15 +00003875 .access = PL0_R, .accessfn = access_tdra,
3876 .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell17a9eb52014-09-12 14:06:49 +01003877 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */
Peter Maydell10aae102014-08-19 18:56:25 +01003878 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH,
3879 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00003880 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell0e5e8932014-08-19 18:56:27 +01003881 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1),
3882 .resetvalue = 0 },
Peter Maydell5e8b12f2014-09-12 14:06:50 +01003883 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1.
3884 * We don't implement the configurable EL0 access.
3885 */
3886 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH,
3887 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00003888 .type = ARM_CP_ALIAS,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00003889 .access = PL1_R, .accessfn = access_tda,
Sergey Fedorovb061a822015-06-19 14:17:44 +01003890 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), },
Peter Maydell10aae102014-08-19 18:56:25 +01003891 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH,
3892 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4,
Davorin Mista1424ca82015-10-16 11:14:53 +01003893 .access = PL1_W, .type = ARM_CP_NO_RAW,
Peter Maydell187f6782016-02-18 14:16:15 +00003894 .accessfn = access_tdosa,
Davorin Mista1424ca82015-10-16 11:14:53 +01003895 .writefn = oslar_write },
3896 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH,
3897 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4,
3898 .access = PL1_R, .resetvalue = 10,
Peter Maydell187f6782016-02-18 14:16:15 +00003899 .accessfn = access_tdosa,
Davorin Mista1424ca82015-10-16 11:14:53 +01003900 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) },
Peter Maydell5e8b12f2014-09-12 14:06:50 +01003901 /* Dummy OSDLR_EL1: 32-bit Linux will read this */
3902 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH,
3903 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4,
Peter Maydell187f6782016-02-18 14:16:15 +00003904 .access = PL1_RW, .accessfn = access_tdosa,
3905 .type = ARM_CP_NOP },
Peter Maydell5e8b12f2014-09-12 14:06:50 +01003906 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't
3907 * implement vector catch debug events yet.
3908 */
3909 { .name = "DBGVCR",
3910 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00003911 .access = PL1_RW, .accessfn = access_tda,
3912 .type = ARM_CP_NOP },
Peter Maydell50300692014-08-19 18:56:25 +01003913 REGINFO_SENTINEL
3914};
3915
3916static const ARMCPRegInfo debug_lpae_cp_reginfo[] = {
3917 /* 64 bit access versions of the (dummy) debug registers */
3918 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0,
3919 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3920 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0,
3921 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 },
3922 REGINFO_SENTINEL
3923};
3924
Peter Maydell9ee98ce2014-09-12 14:06:49 +01003925void hw_watchpoint_update(ARMCPU *cpu, int n)
3926{
3927 CPUARMState *env = &cpu->env;
3928 vaddr len = 0;
3929 vaddr wvr = env->cp15.dbgwvr[n];
3930 uint64_t wcr = env->cp15.dbgwcr[n];
3931 int mask;
3932 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS;
3933
3934 if (env->cpu_watchpoint[n]) {
3935 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]);
3936 env->cpu_watchpoint[n] = NULL;
3937 }
3938
3939 if (!extract64(wcr, 0, 1)) {
3940 /* E bit clear : watchpoint disabled */
3941 return;
3942 }
3943
3944 switch (extract64(wcr, 3, 2)) {
3945 case 0:
3946 /* LSC 00 is reserved and must behave as if the wp is disabled */
3947 return;
3948 case 1:
3949 flags |= BP_MEM_READ;
3950 break;
3951 case 2:
3952 flags |= BP_MEM_WRITE;
3953 break;
3954 case 3:
3955 flags |= BP_MEM_ACCESS;
3956 break;
3957 }
3958
3959 /* Attempts to use both MASK and BAS fields simultaneously are
3960 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case,
3961 * thus generating a watchpoint for every byte in the masked region.
3962 */
3963 mask = extract64(wcr, 24, 4);
3964 if (mask == 1 || mask == 2) {
3965 /* Reserved values of MASK; we must act as if the mask value was
3966 * some non-reserved value, or as if the watchpoint were disabled.
3967 * We choose the latter.
3968 */
3969 return;
3970 } else if (mask) {
3971 /* Watchpoint covers an aligned area up to 2GB in size */
3972 len = 1ULL << mask;
3973 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE
3974 * whether the watchpoint fires when the unmasked bits match; we opt
3975 * to generate the exceptions.
3976 */
3977 wvr &= ~(len - 1);
3978 } else {
3979 /* Watchpoint covers bytes defined by the byte address select bits */
3980 int bas = extract64(wcr, 5, 8);
3981 int basstart;
3982
3983 if (bas == 0) {
3984 /* This must act as if the watchpoint is disabled */
3985 return;
3986 }
3987
3988 if (extract64(wvr, 2, 1)) {
3989 /* Deprecated case of an only 4-aligned address. BAS[7:4] are
3990 * ignored, and BAS[3:0] define which bytes to watch.
3991 */
3992 bas &= 0xf;
3993 }
3994 /* The BAS bits are supposed to be programmed to indicate a contiguous
3995 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether
3996 * we fire for each byte in the word/doubleword addressed by the WVR.
3997 * We choose to ignore any non-zero bits after the first range of 1s.
3998 */
3999 basstart = ctz32(bas);
4000 len = cto32(bas >> basstart);
4001 wvr += basstart;
4002 }
4003
4004 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags,
4005 &env->cpu_watchpoint[n]);
4006}
4007
4008void hw_watchpoint_update_all(ARMCPU *cpu)
4009{
4010 int i;
4011 CPUARMState *env = &cpu->env;
4012
4013 /* Completely clear out existing QEMU watchpoints and our array, to
4014 * avoid possible stale entries following migration load.
4015 */
4016 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU);
4017 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint));
4018
4019 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) {
4020 hw_watchpoint_update(cpu, i);
4021 }
4022}
4023
4024static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4025 uint64_t value)
4026{
4027 ARMCPU *cpu = arm_env_get_cpu(env);
4028 int i = ri->crm;
4029
4030 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the
4031 * register reads and behaves as if values written are sign extended.
4032 * Bits [1:0] are RES0.
4033 */
4034 value = sextract64(value, 0, 49) & ~3ULL;
4035
4036 raw_write(env, ri, value);
4037 hw_watchpoint_update(cpu, i);
4038}
4039
4040static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4041 uint64_t value)
4042{
4043 ARMCPU *cpu = arm_env_get_cpu(env);
4044 int i = ri->crm;
4045
4046 raw_write(env, ri, value);
4047 hw_watchpoint_update(cpu, i);
4048}
4049
Peter Maydell46747d12014-09-29 18:48:46 +01004050void hw_breakpoint_update(ARMCPU *cpu, int n)
4051{
4052 CPUARMState *env = &cpu->env;
4053 uint64_t bvr = env->cp15.dbgbvr[n];
4054 uint64_t bcr = env->cp15.dbgbcr[n];
4055 vaddr addr;
4056 int bt;
4057 int flags = BP_CPU;
4058
4059 if (env->cpu_breakpoint[n]) {
4060 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]);
4061 env->cpu_breakpoint[n] = NULL;
4062 }
4063
4064 if (!extract64(bcr, 0, 1)) {
4065 /* E bit clear : watchpoint disabled */
4066 return;
4067 }
4068
4069 bt = extract64(bcr, 20, 4);
4070
4071 switch (bt) {
4072 case 4: /* unlinked address mismatch (reserved if AArch64) */
4073 case 5: /* linked address mismatch (reserved if AArch64) */
4074 qemu_log_mask(LOG_UNIMP,
4075 "arm: address mismatch breakpoint types not implemented");
4076 return;
4077 case 0: /* unlinked address match */
4078 case 1: /* linked address match */
4079 {
4080 /* Bits [63:49] are hardwired to the value of bit [48]; that is,
4081 * we behave as if the register was sign extended. Bits [1:0] are
4082 * RES0. The BAS field is used to allow setting breakpoints on 16
4083 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether
4084 * a bp will fire if the addresses covered by the bp and the addresses
4085 * covered by the insn overlap but the insn doesn't start at the
4086 * start of the bp address range. We choose to require the insn and
4087 * the bp to have the same address. The constraints on writing to
4088 * BAS enforced in dbgbcr_write mean we have only four cases:
4089 * 0b0000 => no breakpoint
4090 * 0b0011 => breakpoint on addr
4091 * 0b1100 => breakpoint on addr + 2
4092 * 0b1111 => breakpoint on addr
4093 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c).
4094 */
4095 int bas = extract64(bcr, 5, 4);
4096 addr = sextract64(bvr, 0, 49) & ~3ULL;
4097 if (bas == 0) {
4098 return;
4099 }
4100 if (bas == 0xc) {
4101 addr += 2;
4102 }
4103 break;
4104 }
4105 case 2: /* unlinked context ID match */
4106 case 8: /* unlinked VMID match (reserved if no EL2) */
4107 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */
4108 qemu_log_mask(LOG_UNIMP,
4109 "arm: unlinked context breakpoint types not implemented");
4110 return;
4111 case 9: /* linked VMID match (reserved if no EL2) */
4112 case 11: /* linked context ID and VMID match (reserved if no EL2) */
4113 case 3: /* linked context ID match */
4114 default:
4115 /* We must generate no events for Linked context matches (unless
4116 * they are linked to by some other bp/wp, which is handled in
4117 * updates for the linking bp/wp). We choose to also generate no events
4118 * for reserved values.
4119 */
4120 return;
4121 }
4122
4123 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]);
4124}
4125
4126void hw_breakpoint_update_all(ARMCPU *cpu)
4127{
4128 int i;
4129 CPUARMState *env = &cpu->env;
4130
4131 /* Completely clear out existing QEMU breakpoints and our array, to
4132 * avoid possible stale entries following migration load.
4133 */
4134 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU);
4135 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint));
4136
4137 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) {
4138 hw_breakpoint_update(cpu, i);
4139 }
4140}
4141
4142static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4143 uint64_t value)
4144{
4145 ARMCPU *cpu = arm_env_get_cpu(env);
4146 int i = ri->crm;
4147
4148 raw_write(env, ri, value);
4149 hw_breakpoint_update(cpu, i);
4150}
4151
4152static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri,
4153 uint64_t value)
4154{
4155 ARMCPU *cpu = arm_env_get_cpu(env);
4156 int i = ri->crm;
4157
4158 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only
4159 * copy of BAS[0].
4160 */
4161 value = deposit64(value, 6, 1, extract64(value, 5, 1));
4162 value = deposit64(value, 8, 1, extract64(value, 7, 1));
4163
4164 raw_write(env, ri, value);
4165 hw_breakpoint_update(cpu, i);
4166}
4167
Peter Maydell50300692014-08-19 18:56:25 +01004168static void define_debug_regs(ARMCPU *cpu)
Peter Maydell0b454512014-02-26 17:20:05 +00004169{
Peter Maydell50300692014-08-19 18:56:25 +01004170 /* Define v7 and v8 architectural debug registers.
4171 * These are just dummy implementations for now.
Peter Maydell0b454512014-02-26 17:20:05 +00004172 */
4173 int i;
Peter Maydell3ff6fc92014-09-12 14:06:49 +01004174 int wrps, brps, ctx_cmps;
Peter Maydell48eb3ae2014-08-19 18:56:25 +01004175 ARMCPRegInfo dbgdidr = {
4176 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00004177 .access = PL0_R, .accessfn = access_tda,
4178 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr,
Peter Maydell48eb3ae2014-08-19 18:56:25 +01004179 };
Peter Maydell0b454512014-02-26 17:20:05 +00004180
Peter Maydell3ff6fc92014-09-12 14:06:49 +01004181 /* Note that all these register fields hold "number of Xs minus 1". */
Peter Maydell48eb3ae2014-08-19 18:56:25 +01004182 brps = extract32(cpu->dbgdidr, 24, 4);
4183 wrps = extract32(cpu->dbgdidr, 28, 4);
Peter Maydell3ff6fc92014-09-12 14:06:49 +01004184 ctx_cmps = extract32(cpu->dbgdidr, 20, 4);
4185
4186 assert(ctx_cmps <= brps);
Peter Maydell48eb3ae2014-08-19 18:56:25 +01004187
4188 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties
4189 * of the debug registers such as number of breakpoints;
4190 * check that if they both exist then they agree.
4191 */
4192 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) {
4193 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps);
4194 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps);
Peter Maydell3ff6fc92014-09-12 14:06:49 +01004195 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps);
Peter Maydell48eb3ae2014-08-19 18:56:25 +01004196 }
4197
4198 define_one_arm_cp_reg(cpu, &dbgdidr);
Peter Maydell50300692014-08-19 18:56:25 +01004199 define_arm_cp_regs(cpu, debug_cp_reginfo);
4200
4201 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) {
4202 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo);
4203 }
4204
Peter Maydell48eb3ae2014-08-19 18:56:25 +01004205 for (i = 0; i < brps + 1; i++) {
Peter Maydell0b454512014-02-26 17:20:05 +00004206 ARMCPRegInfo dbgregs[] = {
Peter Maydell10aae102014-08-19 18:56:25 +01004207 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH,
4208 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00004209 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell46747d12014-09-29 18:48:46 +01004210 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]),
4211 .writefn = dbgbvr_write, .raw_writefn = raw_write
4212 },
Peter Maydell10aae102014-08-19 18:56:25 +01004213 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH,
4214 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00004215 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell46747d12014-09-29 18:48:46 +01004216 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]),
4217 .writefn = dbgbcr_write, .raw_writefn = raw_write
4218 },
Peter Maydell48eb3ae2014-08-19 18:56:25 +01004219 REGINFO_SENTINEL
4220 };
4221 define_arm_cp_regs(cpu, dbgregs);
4222 }
4223
4224 for (i = 0; i < wrps + 1; i++) {
4225 ARMCPRegInfo dbgregs[] = {
Peter Maydell10aae102014-08-19 18:56:25 +01004226 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH,
4227 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00004228 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell9ee98ce2014-09-12 14:06:49 +01004229 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]),
4230 .writefn = dbgwvr_write, .raw_writefn = raw_write
4231 },
Peter Maydell10aae102014-08-19 18:56:25 +01004232 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH,
4233 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7,
Peter Maydelld6c8cf82016-02-18 14:16:15 +00004234 .access = PL1_RW, .accessfn = access_tda,
Peter Maydell9ee98ce2014-09-12 14:06:49 +01004235 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]),
4236 .writefn = dbgwcr_write, .raw_writefn = raw_write
4237 },
4238 REGINFO_SENTINEL
Peter Maydell0b454512014-02-26 17:20:05 +00004239 };
4240 define_arm_cp_regs(cpu, dbgregs);
4241 }
4242}
4243
Peter Maydell2ceb98c2012-06-20 11:57:09 +00004244void register_cp_regs_for_features(ARMCPU *cpu)
4245{
4246 /* Register all the coprocessor registers based on feature bits */
4247 CPUARMState *env = &cpu->env;
4248 if (arm_feature(env, ARM_FEATURE_M)) {
4249 /* M profile has no coprocessor registers */
4250 return;
4251 }
4252
Peter Maydelle9aa6c22012-06-20 11:57:09 +00004253 define_arm_cp_regs(cpu, cp_reginfo);
Peter Maydell9449fdf2014-04-15 19:18:47 +01004254 if (!arm_feature(env, ARM_FEATURE_V8)) {
4255 /* Must go early as it is full of wildcards that may be
4256 * overridden by later definitions.
4257 */
4258 define_arm_cp_regs(cpu, not_v8_cp_reginfo);
4259 }
4260
Peter Maydell7d57f402012-06-20 11:57:11 +00004261 if (arm_feature(env, ARM_FEATURE_V6)) {
Peter Maydell8515a092012-06-20 11:57:19 +00004262 /* The ID registers all have impdef reset values */
4263 ARMCPRegInfo v6_idregs[] = {
Peter Maydell0ff644a2014-04-15 19:18:45 +01004264 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH,
4265 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0,
4266 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004267 .resetvalue = cpu->id_pfr0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004268 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH,
4269 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1,
4270 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004271 .resetvalue = cpu->id_pfr1 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004272 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH,
4273 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2,
4274 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004275 .resetvalue = cpu->id_dfr0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004276 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH,
4277 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3,
4278 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004279 .resetvalue = cpu->id_afr0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004280 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH,
4281 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4,
4282 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004283 .resetvalue = cpu->id_mmfr0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004284 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH,
4285 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5,
4286 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004287 .resetvalue = cpu->id_mmfr1 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004288 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH,
4289 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6,
4290 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004291 .resetvalue = cpu->id_mmfr2 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004292 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH,
4293 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7,
4294 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004295 .resetvalue = cpu->id_mmfr3 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004296 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH,
4297 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0,
4298 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004299 .resetvalue = cpu->id_isar0 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004300 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH,
4301 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1,
4302 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004303 .resetvalue = cpu->id_isar1 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004304 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH,
4305 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2,
4306 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004307 .resetvalue = cpu->id_isar2 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004308 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH,
4309 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3,
4310 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004311 .resetvalue = cpu->id_isar3 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004312 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH,
4313 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4,
4314 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004315 .resetvalue = cpu->id_isar4 },
Peter Maydell0ff644a2014-04-15 19:18:45 +01004316 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH,
4317 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5,
4318 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004319 .resetvalue = cpu->id_isar5 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00004320 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH,
4321 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6,
4322 .access = PL1_R, .type = ARM_CP_CONST,
4323 .resetvalue = cpu->id_mmfr4 },
4324 /* 7 is as yet unallocated and must RAZ */
4325 { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH,
4326 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7,
4327 .access = PL1_R, .type = ARM_CP_CONST,
Peter Maydell8515a092012-06-20 11:57:19 +00004328 .resetvalue = 0 },
4329 REGINFO_SENTINEL
4330 };
4331 define_arm_cp_regs(cpu, v6_idregs);
Peter Maydell7d57f402012-06-20 11:57:11 +00004332 define_arm_cp_regs(cpu, v6_cp_reginfo);
4333 } else {
4334 define_arm_cp_regs(cpu, not_v6_cp_reginfo);
4335 }
Peter Maydell4d31c592012-06-20 11:57:11 +00004336 if (arm_feature(env, ARM_FEATURE_V6K)) {
4337 define_arm_cp_regs(cpu, v6k_cp_reginfo);
4338 }
Peter Crosthwaite5e5cf9e2015-06-15 18:06:10 +01004339 if (arm_feature(env, ARM_FEATURE_V7MP) &&
4340 !arm_feature(env, ARM_FEATURE_MPU)) {
Peter Maydell995939a2014-09-12 14:06:50 +01004341 define_arm_cp_regs(cpu, v7mp_cp_reginfo);
4342 }
Peter Maydelle9aa6c22012-06-20 11:57:09 +00004343 if (arm_feature(env, ARM_FEATURE_V7)) {
Peter Maydell200ac0e2012-06-20 11:57:12 +00004344 /* v7 performance monitor control register: same implementor
Alistair Francis7c2cb422014-03-10 14:56:28 +00004345 * field as main ID register, and we implement only the cycle
4346 * count register.
Peter Maydell200ac0e2012-06-20 11:57:12 +00004347 */
Alistair Francis7c2cb422014-03-10 14:56:28 +00004348#ifndef CONFIG_USER_ONLY
Peter Maydell200ac0e2012-06-20 11:57:12 +00004349 ARMCPRegInfo pmcr = {
4350 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0,
Alistair Francis85214662014-08-29 15:00:29 +01004351 .access = PL0_RW,
Peter Maydell7a0e58f2015-02-05 13:37:22 +00004352 .type = ARM_CP_IO | ARM_CP_ALIAS,
Alistair Francis85214662014-08-29 15:00:29 +01004353 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr),
Peter Maydellfcd25202014-02-20 10:35:52 +00004354 .accessfn = pmreg_access, .writefn = pmcr_write,
4355 .raw_writefn = raw_write,
Peter Maydell200ac0e2012-06-20 11:57:12 +00004356 };
Alistair Francis85214662014-08-29 15:00:29 +01004357 ARMCPRegInfo pmcr64 = {
4358 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64,
4359 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0,
4360 .access = PL0_RW, .accessfn = pmreg_access,
4361 .type = ARM_CP_IO,
4362 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr),
4363 .resetvalue = cpu->midr & 0xff000000,
4364 .writefn = pmcr_write, .raw_writefn = raw_write,
4365 };
Alistair Francis7c2cb422014-03-10 14:56:28 +00004366 define_one_arm_cp_reg(cpu, &pmcr);
Alistair Francis85214662014-08-29 15:00:29 +01004367 define_one_arm_cp_reg(cpu, &pmcr64);
Alistair Francis7c2cb422014-03-10 14:56:28 +00004368#endif
Peter Maydell776d4e52012-06-20 11:57:19 +00004369 ARMCPRegInfo clidr = {
Peter Maydell7da845b2014-02-26 17:20:01 +00004370 .name = "CLIDR", .state = ARM_CP_STATE_BOTH,
4371 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1,
Peter Maydell776d4e52012-06-20 11:57:19 +00004372 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr
4373 };
Peter Maydell776d4e52012-06-20 11:57:19 +00004374 define_one_arm_cp_reg(cpu, &clidr);
Peter Maydelle9aa6c22012-06-20 11:57:09 +00004375 define_arm_cp_regs(cpu, v7_cp_reginfo);
Peter Maydell50300692014-08-19 18:56:25 +01004376 define_debug_regs(cpu);
Peter Maydell7d57f402012-06-20 11:57:11 +00004377 } else {
4378 define_arm_cp_regs(cpu, not_v7_cp_reginfo);
Peter Maydelle9aa6c22012-06-20 11:57:09 +00004379 }
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004380 if (arm_feature(env, ARM_FEATURE_V8)) {
Peter Maydelle20d84c2016-02-19 14:07:43 +00004381 /* AArch64 ID registers, which all have impdef reset values.
4382 * Note that within the ID register ranges the unused slots
4383 * must all RAZ, not UNDEF; future architecture versions may
4384 * define new registers here.
4385 */
Peter Maydelle60cef82014-02-26 17:20:05 +00004386 ARMCPRegInfo v8_idregs[] = {
4387 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64,
4388 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0,
4389 .access = PL1_R, .type = ARM_CP_CONST,
4390 .resetvalue = cpu->id_aa64pfr0 },
4391 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64,
4392 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1,
4393 .access = PL1_R, .type = ARM_CP_CONST,
4394 .resetvalue = cpu->id_aa64pfr1},
Peter Maydelle20d84c2016-02-19 14:07:43 +00004395 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4396 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2,
4397 .access = PL1_R, .type = ARM_CP_CONST,
4398 .resetvalue = 0 },
4399 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4400 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3,
4401 .access = PL1_R, .type = ARM_CP_CONST,
4402 .resetvalue = 0 },
4403 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4404 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4,
4405 .access = PL1_R, .type = ARM_CP_CONST,
4406 .resetvalue = 0 },
4407 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4408 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5,
4409 .access = PL1_R, .type = ARM_CP_CONST,
4410 .resetvalue = 0 },
4411 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4412 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6,
4413 .access = PL1_R, .type = ARM_CP_CONST,
4414 .resetvalue = 0 },
4415 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4416 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7,
4417 .access = PL1_R, .type = ARM_CP_CONST,
4418 .resetvalue = 0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00004419 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64,
4420 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0,
4421 .access = PL1_R, .type = ARM_CP_CONST,
Stefan Weil5d831be2014-06-13 20:42:57 +02004422 /* We mask out the PMUVer field, because we don't currently
Peter Maydell9225d732014-04-15 19:18:41 +01004423 * implement the PMU. Not advertising it prevents the guest
4424 * from trying to use it and getting UNDEFs on registers we
4425 * don't implement.
4426 */
4427 .resetvalue = cpu->id_aa64dfr0 & ~0xf00 },
Peter Maydelle60cef82014-02-26 17:20:05 +00004428 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64,
4429 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1,
4430 .access = PL1_R, .type = ARM_CP_CONST,
4431 .resetvalue = cpu->id_aa64dfr1 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00004432 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4433 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2,
4434 .access = PL1_R, .type = ARM_CP_CONST,
4435 .resetvalue = 0 },
4436 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4437 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3,
4438 .access = PL1_R, .type = ARM_CP_CONST,
4439 .resetvalue = 0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00004440 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64,
4441 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4,
4442 .access = PL1_R, .type = ARM_CP_CONST,
4443 .resetvalue = cpu->id_aa64afr0 },
4444 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64,
4445 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5,
4446 .access = PL1_R, .type = ARM_CP_CONST,
4447 .resetvalue = cpu->id_aa64afr1 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00004448 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4449 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6,
4450 .access = PL1_R, .type = ARM_CP_CONST,
4451 .resetvalue = 0 },
4452 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4453 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7,
4454 .access = PL1_R, .type = ARM_CP_CONST,
4455 .resetvalue = 0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00004456 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64,
4457 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0,
4458 .access = PL1_R, .type = ARM_CP_CONST,
4459 .resetvalue = cpu->id_aa64isar0 },
4460 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64,
4461 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1,
4462 .access = PL1_R, .type = ARM_CP_CONST,
4463 .resetvalue = cpu->id_aa64isar1 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00004464 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4465 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2,
4466 .access = PL1_R, .type = ARM_CP_CONST,
4467 .resetvalue = 0 },
4468 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4469 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3,
4470 .access = PL1_R, .type = ARM_CP_CONST,
4471 .resetvalue = 0 },
4472 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4473 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4,
4474 .access = PL1_R, .type = ARM_CP_CONST,
4475 .resetvalue = 0 },
4476 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4477 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5,
4478 .access = PL1_R, .type = ARM_CP_CONST,
4479 .resetvalue = 0 },
4480 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4481 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6,
4482 .access = PL1_R, .type = ARM_CP_CONST,
4483 .resetvalue = 0 },
4484 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4485 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7,
4486 .access = PL1_R, .type = ARM_CP_CONST,
4487 .resetvalue = 0 },
Peter Maydelle60cef82014-02-26 17:20:05 +00004488 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64,
4489 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0,
4490 .access = PL1_R, .type = ARM_CP_CONST,
4491 .resetvalue = cpu->id_aa64mmfr0 },
4492 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64,
4493 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1,
4494 .access = PL1_R, .type = ARM_CP_CONST,
4495 .resetvalue = cpu->id_aa64mmfr1 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00004496 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4497 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2,
4498 .access = PL1_R, .type = ARM_CP_CONST,
4499 .resetvalue = 0 },
4500 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4501 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3,
4502 .access = PL1_R, .type = ARM_CP_CONST,
4503 .resetvalue = 0 },
4504 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4505 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4,
4506 .access = PL1_R, .type = ARM_CP_CONST,
4507 .resetvalue = 0 },
4508 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4509 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5,
4510 .access = PL1_R, .type = ARM_CP_CONST,
4511 .resetvalue = 0 },
4512 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4513 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6,
4514 .access = PL1_R, .type = ARM_CP_CONST,
4515 .resetvalue = 0 },
4516 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4517 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7,
4518 .access = PL1_R, .type = ARM_CP_CONST,
4519 .resetvalue = 0 },
Peter Maydella50c0f52014-04-15 19:18:44 +01004520 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64,
4521 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0,
4522 .access = PL1_R, .type = ARM_CP_CONST,
4523 .resetvalue = cpu->mvfr0 },
4524 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64,
4525 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1,
4526 .access = PL1_R, .type = ARM_CP_CONST,
4527 .resetvalue = cpu->mvfr1 },
4528 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64,
4529 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2,
4530 .access = PL1_R, .type = ARM_CP_CONST,
4531 .resetvalue = cpu->mvfr2 },
Peter Maydelle20d84c2016-02-19 14:07:43 +00004532 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4533 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3,
4534 .access = PL1_R, .type = ARM_CP_CONST,
4535 .resetvalue = 0 },
4536 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4537 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4,
4538 .access = PL1_R, .type = ARM_CP_CONST,
4539 .resetvalue = 0 },
4540 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4541 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5,
4542 .access = PL1_R, .type = ARM_CP_CONST,
4543 .resetvalue = 0 },
4544 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4545 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6,
4546 .access = PL1_R, .type = ARM_CP_CONST,
4547 .resetvalue = 0 },
4548 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64,
4549 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7,
4550 .access = PL1_R, .type = ARM_CP_CONST,
4551 .resetvalue = 0 },
Alistair Francis4054bfa2016-02-18 14:16:17 +00004552 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32,
4553 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6,
4554 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4555 .resetvalue = cpu->pmceid0 },
4556 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64,
4557 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6,
4558 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4559 .resetvalue = cpu->pmceid0 },
4560 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32,
4561 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7,
4562 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4563 .resetvalue = cpu->pmceid1 },
4564 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64,
4565 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7,
4566 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST,
4567 .resetvalue = cpu->pmceid1 },
Peter Maydelle60cef82014-02-26 17:20:05 +00004568 REGINFO_SENTINEL
4569 };
Greg Bellowsbe8e8122015-02-05 13:37:22 +00004570 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */
4571 if (!arm_feature(env, ARM_FEATURE_EL3) &&
4572 !arm_feature(env, ARM_FEATURE_EL2)) {
4573 ARMCPRegInfo rvbar = {
4574 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64,
4575 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1,
4576 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar
4577 };
4578 define_one_arm_cp_reg(cpu, &rvbar);
4579 }
Peter Maydelle60cef82014-02-26 17:20:05 +00004580 define_arm_cp_regs(cpu, v8_idregs);
Peter Maydellb0d2b7d2014-01-04 22:15:45 +00004581 define_arm_cp_regs(cpu, v8_cp_reginfo);
4582 }
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01004583 if (arm_feature(env, ARM_FEATURE_EL2)) {
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01004584 uint64_t vmpidr_def = mpidr_read_val(env);
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01004585 ARMCPRegInfo vpidr_regs[] = {
4586 { .name = "VPIDR", .state = ARM_CP_STATE_AA32,
4587 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4588 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4589 .resetvalue = cpu->midr,
4590 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
4591 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64,
4592 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4593 .access = PL2_RW, .resetvalue = cpu->midr,
4594 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01004595 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32,
4596 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4597 .access = PL2_RW, .accessfn = access_el3_aa32ns,
4598 .resetvalue = vmpidr_def,
4599 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
4600 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64,
4601 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4602 .access = PL2_RW,
4603 .resetvalue = vmpidr_def,
4604 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) },
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01004605 REGINFO_SENTINEL
4606 };
4607 define_arm_cp_regs(cpu, vpidr_regs);
Peter Maydell4771cd02015-06-01 19:18:36 +01004608 define_arm_cp_regs(cpu, el2_cp_reginfo);
Greg Bellowsbe8e8122015-02-05 13:37:22 +00004609 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */
4610 if (!arm_feature(env, ARM_FEATURE_EL3)) {
4611 ARMCPRegInfo rvbar = {
4612 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64,
4613 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1,
4614 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar
4615 };
4616 define_one_arm_cp_reg(cpu, &rvbar);
4617 }
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01004618 } else {
4619 /* If EL2 is missing but higher ELs are enabled, we need to
4620 * register the no_el2 reginfos.
4621 */
4622 if (arm_feature(env, ARM_FEATURE_EL3)) {
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01004623 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value
4624 * of MIDR_EL1 and MPIDR_EL1.
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01004625 */
4626 ARMCPRegInfo vpidr_regs[] = {
4627 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4628 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0,
4629 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4630 .type = ARM_CP_CONST, .resetvalue = cpu->midr,
4631 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) },
Edgar E. Iglesiasf0d574d2015-09-14 14:39:51 +01004632 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH,
4633 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5,
4634 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any,
4635 .type = ARM_CP_NO_RAW,
4636 .writefn = arm_cp_write_ignore, .readfn = mpidr_read },
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01004637 REGINFO_SENTINEL
4638 };
4639 define_arm_cp_regs(cpu, vpidr_regs);
Peter Maydell4771cd02015-06-01 19:18:36 +01004640 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo);
Edgar E. Iglesiasd42e3c22014-05-27 17:09:54 +01004641 }
Edgar E. Iglesias3b685ba2014-05-27 17:09:53 +01004642 }
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01004643 if (arm_feature(env, ARM_FEATURE_EL3)) {
Fabian Aggeler0f1a3b22014-12-11 12:07:49 +00004644 define_arm_cp_regs(cpu, el3_cp_reginfo);
Peter Maydelle24fdd22016-04-04 17:33:51 +01004645 ARMCPRegInfo el3_regs[] = {
4646 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64,
4647 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1,
4648 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar },
4649 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64,
4650 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0,
4651 .access = PL3_RW,
4652 .raw_writefn = raw_write, .writefn = sctlr_write,
4653 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]),
4654 .resetvalue = cpu->reset_sctlr },
4655 REGINFO_SENTINEL
Greg Bellowsbe8e8122015-02-05 13:37:22 +00004656 };
Peter Maydelle24fdd22016-04-04 17:33:51 +01004657
4658 define_arm_cp_regs(cpu, el3_regs);
Edgar E. Iglesias81547d62014-05-27 17:09:53 +01004659 }
Peter Maydell2f027fc2016-02-11 11:17:31 +00004660 /* The behaviour of NSACR is sufficiently various that we don't
4661 * try to describe it in a single reginfo:
4662 * if EL3 is 64 bit, then trap to EL3 from S EL1,
4663 * reads as constant 0xc00 from NS EL1 and NS EL2
4664 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2
4665 * if v7 without EL3, register doesn't exist
4666 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2
4667 */
4668 if (arm_feature(env, ARM_FEATURE_EL3)) {
4669 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4670 ARMCPRegInfo nsacr = {
4671 .name = "NSACR", .type = ARM_CP_CONST,
4672 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4673 .access = PL1_RW, .accessfn = nsacr_access,
4674 .resetvalue = 0xc00
4675 };
4676 define_one_arm_cp_reg(cpu, &nsacr);
4677 } else {
4678 ARMCPRegInfo nsacr = {
4679 .name = "NSACR",
4680 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4681 .access = PL3_RW | PL1_R,
4682 .resetvalue = 0,
4683 .fieldoffset = offsetof(CPUARMState, cp15.nsacr)
4684 };
4685 define_one_arm_cp_reg(cpu, &nsacr);
4686 }
4687 } else {
4688 if (arm_feature(env, ARM_FEATURE_V8)) {
4689 ARMCPRegInfo nsacr = {
4690 .name = "NSACR", .type = ARM_CP_CONST,
4691 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2,
4692 .access = PL1_R,
4693 .resetvalue = 0xc00
4694 };
4695 define_one_arm_cp_reg(cpu, &nsacr);
4696 }
4697 }
4698
Peter Maydell18032be2012-06-20 11:57:13 +00004699 if (arm_feature(env, ARM_FEATURE_MPU)) {
Peter Crosthwaite6cb0b012015-06-19 14:17:44 +01004700 if (arm_feature(env, ARM_FEATURE_V6)) {
4701 /* PMSAv6 not implemented */
4702 assert(arm_feature(env, ARM_FEATURE_V7));
4703 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
4704 define_arm_cp_regs(cpu, pmsav7_cp_reginfo);
4705 } else {
4706 define_arm_cp_regs(cpu, pmsav5_cp_reginfo);
4707 }
Peter Maydell18032be2012-06-20 11:57:13 +00004708 } else {
Peter Crosthwaite8e5d75c2015-06-15 18:06:10 +01004709 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo);
Peter Maydell18032be2012-06-20 11:57:13 +00004710 define_arm_cp_regs(cpu, vmsa_cp_reginfo);
4711 }
Peter Maydellc326b972012-06-20 11:57:10 +00004712 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) {
4713 define_arm_cp_regs(cpu, t2ee_cp_reginfo);
4714 }
Peter Maydell6cc7a3a2012-06-20 11:57:12 +00004715 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) {
4716 define_arm_cp_regs(cpu, generic_timer_cp_reginfo);
4717 }
Peter Maydell4a501602012-06-20 11:57:16 +00004718 if (arm_feature(env, ARM_FEATURE_VAPA)) {
4719 define_arm_cp_regs(cpu, vapa_cp_reginfo);
4720 }
Peter Maydellc4804212012-06-20 11:57:17 +00004721 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) {
4722 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo);
4723 }
4724 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) {
4725 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo);
4726 }
4727 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) {
4728 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo);
4729 }
Peter Maydell18032be2012-06-20 11:57:13 +00004730 if (arm_feature(env, ARM_FEATURE_OMAPCP)) {
4731 define_arm_cp_regs(cpu, omap_cp_reginfo);
4732 }
Peter Maydell34f90522012-06-20 11:57:18 +00004733 if (arm_feature(env, ARM_FEATURE_STRONGARM)) {
4734 define_arm_cp_regs(cpu, strongarm_cp_reginfo);
4735 }
Peter Maydell1047b9d2012-06-20 11:57:15 +00004736 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4737 define_arm_cp_regs(cpu, xscale_cp_reginfo);
4738 }
4739 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) {
4740 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo);
4741 }
Peter Maydell7ac681c2012-07-12 10:59:07 +00004742 if (arm_feature(env, ARM_FEATURE_LPAE)) {
4743 define_arm_cp_regs(cpu, lpae_cp_reginfo);
4744 }
Peter Maydell78848492012-06-20 11:57:20 +00004745 /* Slightly awkwardly, the OMAP and StrongARM cores need all of
4746 * cp15 crn=0 to be writes-ignored, whereas for other cores they should
4747 * be read-only (ie write causes UNDEF exception).
4748 */
4749 {
Peter Maydell00a29f32014-04-15 19:18:47 +01004750 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = {
4751 /* Pre-v8 MIDR space.
4752 * Note that the MIDR isn't a simple constant register because
Peter Maydell78848492012-06-20 11:57:20 +00004753 * of the TI925 behaviour where writes to another register can
4754 * cause the MIDR value to change.
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10004755 *
4756 * Unimplemented registers in the c15 0 0 0 space default to
4757 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR
4758 * and friends override accordingly.
Peter Maydell78848492012-06-20 11:57:20 +00004759 */
4760 { .name = "MIDR",
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10004761 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY,
Peter Maydell78848492012-06-20 11:57:20 +00004762 .access = PL1_R, .resetvalue = cpu->midr,
Peter Maydelld4e6df62013-06-25 18:16:07 +01004763 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write,
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01004764 .readfn = midr_read,
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10004765 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4766 .type = ARM_CP_OVERRIDE },
Peter Maydell78848492012-06-20 11:57:20 +00004767 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */
4768 { .name = "DUMMY",
4769 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY,
4770 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4771 { .name = "DUMMY",
4772 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY,
4773 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4774 { .name = "DUMMY",
4775 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY,
4776 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4777 { .name = "DUMMY",
4778 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY,
4779 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4780 { .name = "DUMMY",
4781 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY,
4782 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
4783 REGINFO_SENTINEL
4784 };
Peter Maydell00a29f32014-04-15 19:18:47 +01004785 ARMCPRegInfo id_v8_midr_cp_reginfo[] = {
Peter Maydell00a29f32014-04-15 19:18:47 +01004786 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH,
4787 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0,
Edgar E. Iglesias731de9e2015-09-14 14:39:50 +01004788 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr,
4789 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid),
4790 .readfn = midr_read },
Sergey Fedorovac00c792015-06-15 18:06:08 +01004791 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */
4792 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4793 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4794 .access = PL1_R, .resetvalue = cpu->midr },
4795 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST,
4796 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7,
4797 .access = PL1_R, .resetvalue = cpu->midr },
Peter Maydell00a29f32014-04-15 19:18:47 +01004798 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH,
4799 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6,
Sergey Fedorov13b72b22015-06-15 18:06:08 +01004800 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr },
Peter Maydell00a29f32014-04-15 19:18:47 +01004801 REGINFO_SENTINEL
4802 };
4803 ARMCPRegInfo id_cp_reginfo[] = {
4804 /* These are common to v8 and pre-v8 */
4805 { .name = "CTR",
4806 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1,
4807 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4808 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64,
4809 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0,
4810 .access = PL0_R, .accessfn = ctr_el0_access,
4811 .type = ARM_CP_CONST, .resetvalue = cpu->ctr },
4812 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */
4813 { .name = "TCMTR",
4814 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2,
4815 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 },
Peter Maydell00a29f32014-04-15 19:18:47 +01004816 REGINFO_SENTINEL
4817 };
Peter Crosthwaite8085ce62015-06-15 18:06:10 +01004818 /* TLBTR is specific to VMSA */
4819 ARMCPRegInfo id_tlbtr_reginfo = {
4820 .name = "TLBTR",
4821 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3,
4822 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0,
4823 };
Peter Crosthwaite3281af82015-06-19 14:17:44 +01004824 /* MPUIR is specific to PMSA V6+ */
4825 ARMCPRegInfo id_mpuir_reginfo = {
4826 .name = "MPUIR",
4827 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4,
4828 .access = PL1_R, .type = ARM_CP_CONST,
4829 .resetvalue = cpu->pmsav7_dregion << 8
4830 };
Peter Maydell78848492012-06-20 11:57:20 +00004831 ARMCPRegInfo crn0_wi_reginfo = {
4832 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY,
4833 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W,
4834 .type = ARM_CP_NOP | ARM_CP_OVERRIDE
4835 };
4836 if (arm_feature(env, ARM_FEATURE_OMAPCP) ||
4837 arm_feature(env, ARM_FEATURE_STRONGARM)) {
4838 ARMCPRegInfo *r;
4839 /* Register the blanket "writes ignored" value first to cover the
Peter Crosthwaitea703eda2013-07-10 14:21:42 +10004840 * whole space. Then update the specific ID registers to allow write
4841 * access, so that they ignore writes rather than causing them to
4842 * UNDEF.
Peter Maydell78848492012-06-20 11:57:20 +00004843 */
4844 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo);
Peter Maydell00a29f32014-04-15 19:18:47 +01004845 for (r = id_pre_v8_midr_cp_reginfo;
4846 r->type != ARM_CP_SENTINEL; r++) {
4847 r->access = PL1_RW;
4848 }
Peter Maydell78848492012-06-20 11:57:20 +00004849 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) {
4850 r->access = PL1_RW;
Peter Maydell78848492012-06-20 11:57:20 +00004851 }
Peter Crosthwaite8085ce62015-06-15 18:06:10 +01004852 id_tlbtr_reginfo.access = PL1_RW;
Peter Crosthwaite3281af82015-06-19 14:17:44 +01004853 id_tlbtr_reginfo.access = PL1_RW;
Peter Maydell78848492012-06-20 11:57:20 +00004854 }
Peter Maydell00a29f32014-04-15 19:18:47 +01004855 if (arm_feature(env, ARM_FEATURE_V8)) {
4856 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo);
4857 } else {
4858 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo);
4859 }
Peter Crosthwaitea703eda2013-07-10 14:21:42 +10004860 define_arm_cp_regs(cpu, id_cp_reginfo);
Peter Crosthwaite8085ce62015-06-15 18:06:10 +01004861 if (!arm_feature(env, ARM_FEATURE_MPU)) {
4862 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo);
Peter Crosthwaite3281af82015-06-19 14:17:44 +01004863 } else if (arm_feature(env, ARM_FEATURE_V7)) {
4864 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo);
Peter Crosthwaite8085ce62015-06-15 18:06:10 +01004865 }
Peter Maydell78848492012-06-20 11:57:20 +00004866 }
4867
Peter Crosthwaite97ce8d62013-07-10 14:22:21 +10004868 if (arm_feature(env, ARM_FEATURE_MPIDR)) {
4869 define_arm_cp_regs(cpu, mpidr_cp_reginfo);
4870 }
4871
Peter Maydell2771db22012-06-20 11:57:18 +00004872 if (arm_feature(env, ARM_FEATURE_AUXCR)) {
Peter Maydell834a6c62015-08-25 15:45:07 +01004873 ARMCPRegInfo auxcr_reginfo[] = {
4874 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH,
4875 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1,
4876 .access = PL1_RW, .type = ARM_CP_CONST,
4877 .resetvalue = cpu->reset_auxcr },
4878 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH,
4879 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1,
4880 .access = PL2_RW, .type = ARM_CP_CONST,
4881 .resetvalue = 0 },
4882 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64,
4883 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1,
4884 .access = PL3_RW, .type = ARM_CP_CONST,
4885 .resetvalue = 0 },
4886 REGINFO_SENTINEL
Peter Maydell2771db22012-06-20 11:57:18 +00004887 };
Peter Maydell834a6c62015-08-25 15:45:07 +01004888 define_arm_cp_regs(cpu, auxcr_reginfo);
Peter Maydell2771db22012-06-20 11:57:18 +00004889 }
4890
Peter Crosthwaited8ba7802013-12-17 19:42:28 +00004891 if (arm_feature(env, ARM_FEATURE_CBAR)) {
Peter Maydellf318cec2014-04-15 19:18:49 +01004892 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4893 /* 32 bit view is [31:18] 0...0 [43:32]. */
4894 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18)
4895 | extract64(cpu->reset_cbar, 32, 12);
4896 ARMCPRegInfo cbar_reginfo[] = {
4897 { .name = "CBAR",
4898 .type = ARM_CP_CONST,
4899 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4900 .access = PL1_R, .resetvalue = cpu->reset_cbar },
4901 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64,
4902 .type = ARM_CP_CONST,
4903 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0,
4904 .access = PL1_R, .resetvalue = cbar32 },
4905 REGINFO_SENTINEL
4906 };
4907 /* We don't implement a r/w 64 bit CBAR currently */
4908 assert(arm_feature(env, ARM_FEATURE_CBAR_RO));
4909 define_arm_cp_regs(cpu, cbar_reginfo);
4910 } else {
4911 ARMCPRegInfo cbar = {
4912 .name = "CBAR",
4913 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0,
4914 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar,
4915 .fieldoffset = offsetof(CPUARMState,
4916 cp15.c15_config_base_address)
4917 };
4918 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) {
4919 cbar.access = PL1_R;
4920 cbar.fieldoffset = 0;
4921 cbar.type = ARM_CP_CONST;
4922 }
4923 define_one_arm_cp_reg(cpu, &cbar);
4924 }
Peter Crosthwaited8ba7802013-12-17 19:42:28 +00004925 }
4926
Peter Maydell2771db22012-06-20 11:57:18 +00004927 /* Generic registers whose values depend on the implementation */
4928 {
4929 ARMCPRegInfo sctlr = {
Peter Maydell5ebafdf2014-02-26 17:20:03 +00004930 .name = "SCTLR", .state = ARM_CP_STATE_BOTH,
Fabian Aggeler137feaa2014-12-11 12:07:50 +00004931 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0,
4932 .access = PL1_RW,
4933 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s),
4934 offsetof(CPUARMState, cp15.sctlr_ns) },
Peter Maydelld4e6df62013-06-25 18:16:07 +01004935 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr,
4936 .raw_writefn = raw_write,
Peter Maydell2771db22012-06-20 11:57:18 +00004937 };
4938 if (arm_feature(env, ARM_FEATURE_XSCALE)) {
4939 /* Normally we would always end the TB on an SCTLR write, but Linux
4940 * arch/arm/mach-pxa/sleep.S expects two instructions following
4941 * an MMU enable to execute from cache. Imitate this behaviour.
4942 */
4943 sctlr.type |= ARM_CP_SUPPRESS_TB_END;
4944 }
4945 define_one_arm_cp_reg(cpu, &sctlr);
4946 }
Peter Maydell2ceb98c2012-06-20 11:57:09 +00004947}
4948
Andreas Färber778c3a02012-04-20 07:39:14 +00004949ARMCPU *cpu_arm_init(const char *cpu_model)
pbrook40f137e2006-02-20 00:33:36 +00004950{
Andreas Färber92626852014-03-04 03:17:10 +01004951 return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model));
Andreas Färber14969262013-01-05 10:18:18 +01004952}
4953
4954void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu)
4955{
Andreas Färber22169d42013-06-28 21:27:39 +02004956 CPUState *cs = CPU(cpu);
Andreas Färber14969262013-01-05 10:18:18 +01004957 CPUARMState *env = &cpu->env;
4958
Peter Maydell6a669422013-12-17 19:42:32 +00004959 if (arm_feature(env, ARM_FEATURE_AARCH64)) {
4960 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg,
4961 aarch64_fpu_gdb_set_reg,
4962 34, "aarch64-fpu.xml", 0);
4963 } else if (arm_feature(env, ARM_FEATURE_NEON)) {
Andreas Färber22169d42013-06-28 21:27:39 +02004964 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00004965 51, "arm-neon.xml", 0);
4966 } else if (arm_feature(env, ARM_FEATURE_VFP3)) {
Andreas Färber22169d42013-06-28 21:27:39 +02004967 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00004968 35, "arm-vfp3.xml", 0);
4969 } else if (arm_feature(env, ARM_FEATURE_VFP)) {
Andreas Färber22169d42013-06-28 21:27:39 +02004970 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg,
pbrook56aebc82008-10-11 17:55:29 +00004971 19, "arm-vfp.xml", 0);
4972 }
pbrook40f137e2006-02-20 00:33:36 +00004973}
4974
Peter Maydell777dc782012-04-20 17:58:31 +00004975/* Sort alphabetically by type name, except for "any". */
4976static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b)
pbrook5adb4832007-03-08 03:15:18 +00004977{
Peter Maydell777dc782012-04-20 17:58:31 +00004978 ObjectClass *class_a = (ObjectClass *)a;
4979 ObjectClass *class_b = (ObjectClass *)b;
4980 const char *name_a, *name_b;
pbrook5adb4832007-03-08 03:15:18 +00004981
Peter Maydell777dc782012-04-20 17:58:31 +00004982 name_a = object_class_get_name(class_a);
4983 name_b = object_class_get_name(class_b);
Andreas Färber51492fd2013-01-27 17:30:10 +01004984 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) {
Peter Maydell777dc782012-04-20 17:58:31 +00004985 return 1;
Andreas Färber51492fd2013-01-27 17:30:10 +01004986 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) {
Peter Maydell777dc782012-04-20 17:58:31 +00004987 return -1;
4988 } else {
4989 return strcmp(name_a, name_b);
pbrook5adb4832007-03-08 03:15:18 +00004990 }
4991}
4992
Peter Maydell777dc782012-04-20 17:58:31 +00004993static void arm_cpu_list_entry(gpointer data, gpointer user_data)
pbrook40f137e2006-02-20 00:33:36 +00004994{
Peter Maydell777dc782012-04-20 17:58:31 +00004995 ObjectClass *oc = data;
Andreas Färber92a31362012-12-16 02:17:02 +01004996 CPUListState *s = user_data;
Andreas Färber51492fd2013-01-27 17:30:10 +01004997 const char *typename;
4998 char *name;
pbrook3371d272007-03-08 03:04:12 +00004999
Andreas Färber51492fd2013-01-27 17:30:10 +01005000 typename = object_class_get_name(oc);
5001 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU));
Peter Maydell777dc782012-04-20 17:58:31 +00005002 (*s->cpu_fprintf)(s->file, " %s\n",
Andreas Färber51492fd2013-01-27 17:30:10 +01005003 name);
5004 g_free(name);
Peter Maydell777dc782012-04-20 17:58:31 +00005005}
5006
5007void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf)
5008{
Andreas Färber92a31362012-12-16 02:17:02 +01005009 CPUListState s = {
Peter Maydell777dc782012-04-20 17:58:31 +00005010 .file = f,
5011 .cpu_fprintf = cpu_fprintf,
5012 };
5013 GSList *list;
5014
5015 list = object_class_get_list(TYPE_ARM_CPU, false);
5016 list = g_slist_sort(list, arm_cpu_list_compare);
5017 (*cpu_fprintf)(f, "Available CPUs:\n");
5018 g_slist_foreach(list, arm_cpu_list_entry, &s);
5019 g_slist_free(list);
Peter Maydella96c0512013-11-22 17:17:17 +00005020#ifdef CONFIG_KVM
5021 /* The 'host' CPU type is dynamically registered only if KVM is
5022 * enabled, so we have to special-case it here:
5023 */
5024 (*cpu_fprintf)(f, " host (only available in KVM mode)\n");
5025#endif
pbrook40f137e2006-02-20 00:33:36 +00005026}
5027
Cole Robinson78027bb2013-09-10 19:09:33 +01005028static void arm_cpu_add_definition(gpointer data, gpointer user_data)
5029{
5030 ObjectClass *oc = data;
5031 CpuDefinitionInfoList **cpu_list = user_data;
5032 CpuDefinitionInfoList *entry;
5033 CpuDefinitionInfo *info;
5034 const char *typename;
5035
5036 typename = object_class_get_name(oc);
5037 info = g_malloc0(sizeof(*info));
5038 info->name = g_strndup(typename,
5039 strlen(typename) - strlen("-" TYPE_ARM_CPU));
5040
5041 entry = g_malloc0(sizeof(*entry));
5042 entry->value = info;
5043 entry->next = *cpu_list;
5044 *cpu_list = entry;
5045}
5046
5047CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp)
5048{
5049 CpuDefinitionInfoList *cpu_list = NULL;
5050 GSList *list;
5051
5052 list = object_class_get_list(TYPE_ARM_CPU, false);
5053 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list);
5054 g_slist_free(list);
5055
5056 return cpu_list;
5057}
5058
Peter Maydell6e6efd62013-12-22 22:32:30 +00005059static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r,
Peter Maydell51a79b02014-12-11 12:07:49 +00005060 void *opaque, int state, int secstate,
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005061 int crm, int opc1, int opc2)
Peter Maydell6e6efd62013-12-22 22:32:30 +00005062{
5063 /* Private utility function for define_one_arm_cp_reg_with_opaque():
5064 * add a single reginfo struct to the hash table.
5065 */
5066 uint32_t *key = g_new(uint32_t, 1);
5067 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo));
5068 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0;
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005069 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0;
5070
5071 /* Reset the secure state to the specific incoming state. This is
5072 * necessary as the register may have been defined with both states.
5073 */
5074 r2->secure = secstate;
5075
5076 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5077 /* Register is banked (using both entries in array).
5078 * Overwriting fieldoffset as the array is only used to define
5079 * banked registers but later only fieldoffset is used.
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005080 */
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005081 r2->fieldoffset = r->bank_fieldoffsets[ns];
5082 }
5083
5084 if (state == ARM_CP_STATE_AA32) {
5085 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) {
5086 /* If the register is banked then we don't need to migrate or
5087 * reset the 32-bit instance in certain cases:
5088 *
5089 * 1) If the register has both 32-bit and 64-bit instances then we
5090 * can count on the 64-bit instance taking care of the
5091 * non-secure bank.
5092 * 2) If ARMv8 is enabled then we can count on a 64-bit version
5093 * taking care of the secure bank. This requires that separate
5094 * 32 and 64-bit definitions are provided.
5095 */
5096 if ((r->state == ARM_CP_STATE_BOTH && ns) ||
5097 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) {
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005098 r2->type |= ARM_CP_ALIAS;
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005099 }
5100 } else if ((secstate != r->secure) && !ns) {
5101 /* The register is not banked so we only want to allow migration of
5102 * the non-secure instance.
5103 */
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005104 r2->type |= ARM_CP_ALIAS;
Peter Maydell58a1d8c2014-08-19 18:56:25 +01005105 }
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005106
5107 if (r->state == ARM_CP_STATE_BOTH) {
5108 /* We assume it is a cp15 register if the .cp field is left unset.
5109 */
5110 if (r2->cp == 0) {
5111 r2->cp = 15;
5112 }
5113
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005114#ifdef HOST_WORDS_BIGENDIAN
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005115 if (r2->fieldoffset) {
5116 r2->fieldoffset += sizeof(uint32_t);
5117 }
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005118#endif
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005119 }
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005120 }
5121 if (state == ARM_CP_STATE_AA64) {
5122 /* To allow abbreviation of ARMCPRegInfo
5123 * definitions, we treat cp == 0 as equivalent to
5124 * the value for "standard guest-visible sysreg".
Peter Maydell58a1d8c2014-08-19 18:56:25 +01005125 * STATE_BOTH definitions are also always "standard
5126 * sysreg" in their AArch64 view (the .cp value may
5127 * be non-zero for the benefit of the AArch32 view).
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005128 */
Peter Maydell58a1d8c2014-08-19 18:56:25 +01005129 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) {
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005130 r2->cp = CP_REG_ARM64_SYSREG_CP;
5131 }
5132 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm,
5133 r2->opc0, opc1, opc2);
5134 } else {
Peter Maydell51a79b02014-12-11 12:07:49 +00005135 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2);
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005136 }
Peter Maydell6e6efd62013-12-22 22:32:30 +00005137 if (opaque) {
5138 r2->opaque = opaque;
5139 }
Peter Maydell67ed7712014-02-26 17:20:01 +00005140 /* reginfo passed to helpers is correct for the actual access,
5141 * and is never ARM_CP_STATE_BOTH:
5142 */
5143 r2->state = state;
Peter Maydell6e6efd62013-12-22 22:32:30 +00005144 /* Make sure reginfo passed to helpers for wildcarded regs
5145 * has the correct crm/opc1/opc2 for this reg, not CP_ANY:
5146 */
5147 r2->crm = crm;
5148 r2->opc1 = opc1;
5149 r2->opc2 = opc2;
5150 /* By convention, for wildcarded registers only the first
5151 * entry is used for migration; the others are marked as
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005152 * ALIAS so we don't try to transfer the register
Peter Maydell6e6efd62013-12-22 22:32:30 +00005153 * multiple times. Special registers (ie NOP/WFI) are
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005154 * never migratable and not even raw-accessible.
Peter Maydell6e6efd62013-12-22 22:32:30 +00005155 */
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005156 if ((r->type & ARM_CP_SPECIAL)) {
5157 r2->type |= ARM_CP_NO_RAW;
5158 }
5159 if (((r->crm == CP_ANY) && crm != 0) ||
Peter Maydell6e6efd62013-12-22 22:32:30 +00005160 ((r->opc1 == CP_ANY) && opc1 != 0) ||
5161 ((r->opc2 == CP_ANY) && opc2 != 0)) {
Peter Maydell7a0e58f2015-02-05 13:37:22 +00005162 r2->type |= ARM_CP_ALIAS;
Peter Maydell6e6efd62013-12-22 22:32:30 +00005163 }
5164
Peter Maydell375421c2015-02-05 13:37:22 +00005165 /* Check that raw accesses are either forbidden or handled. Note that
5166 * we can't assert this earlier because the setup of fieldoffset for
5167 * banked registers has to be done first.
5168 */
5169 if (!(r2->type & ARM_CP_NO_RAW)) {
5170 assert(!raw_accessors_invalid(r2));
5171 }
5172
Peter Maydell6e6efd62013-12-22 22:32:30 +00005173 /* Overriding of an existing definition must be explicitly
5174 * requested.
5175 */
5176 if (!(r->type & ARM_CP_OVERRIDE)) {
5177 ARMCPRegInfo *oldreg;
5178 oldreg = g_hash_table_lookup(cpu->cp_regs, key);
5179 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) {
5180 fprintf(stderr, "Register redefined: cp=%d %d bit "
5181 "crn=%d crm=%d opc1=%d opc2=%d, "
5182 "was %s, now %s\n", r2->cp, 32 + 32 * is64,
5183 r2->crn, r2->crm, r2->opc1, r2->opc2,
5184 oldreg->name, r2->name);
5185 g_assert_not_reached();
5186 }
5187 }
5188 g_hash_table_insert(cpu->cp_regs, key, r2);
5189}
5190
5191
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005192void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu,
5193 const ARMCPRegInfo *r, void *opaque)
5194{
5195 /* Define implementations of coprocessor registers.
5196 * We store these in a hashtable because typically
5197 * there are less than 150 registers in a space which
5198 * is 16*16*16*8*8 = 262144 in size.
5199 * Wildcarding is supported for the crm, opc1 and opc2 fields.
5200 * If a register is defined twice then the second definition is
5201 * used, so this can be used to define some generic registers and
5202 * then override them with implementation specific variations.
5203 * At least one of the original and the second definition should
5204 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard
5205 * against accidental use.
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005206 *
5207 * The state field defines whether the register is to be
5208 * visible in the AArch32 or AArch64 execution state. If the
5209 * state is set to ARM_CP_STATE_BOTH then we synthesise a
5210 * reginfo structure for the AArch32 view, which sees the lower
5211 * 32 bits of the 64 bit register.
5212 *
5213 * Only registers visible in AArch64 may set r->opc0; opc0 cannot
5214 * be wildcarded. AArch64 registers are always considered to be 64
5215 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of
5216 * the register, if any.
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005217 */
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005218 int crm, opc1, opc2, state;
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005219 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm;
5220 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm;
5221 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1;
5222 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1;
5223 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2;
5224 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2;
5225 /* 64 bit registers have only CRm and Opc1 fields */
5226 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn)));
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005227 /* op0 only exists in the AArch64 encodings */
5228 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0));
5229 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */
5230 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT));
5231 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1
5232 * encodes a minimum access level for the register. We roll this
5233 * runtime check into our general permission check code, so check
5234 * here that the reginfo's specified permissions are strict enough
5235 * to encompass the generic architectural permission check.
5236 */
5237 if (r->state != ARM_CP_STATE_AA32) {
5238 int mask = 0;
5239 switch (r->opc1) {
5240 case 0: case 1: case 2:
5241 /* min_EL EL1 */
5242 mask = PL1_RW;
5243 break;
5244 case 3:
5245 /* min_EL EL0 */
5246 mask = PL0_RW;
5247 break;
5248 case 4:
5249 /* min_EL EL2 */
5250 mask = PL2_RW;
5251 break;
5252 case 5:
5253 /* unallocated encoding, so not possible */
5254 assert(false);
5255 break;
5256 case 6:
5257 /* min_EL EL3 */
5258 mask = PL3_RW;
5259 break;
5260 case 7:
5261 /* min_EL EL1, secure mode only (we don't check the latter) */
5262 mask = PL1_RW;
5263 break;
5264 default:
5265 /* broken reginfo with out-of-range opc1 */
5266 assert(false);
5267 break;
5268 }
5269 /* assert our permissions are not too lax (stricter is fine) */
5270 assert((r->access & ~mask) == 0);
5271 }
5272
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005273 /* Check that the register definition has enough info to handle
5274 * reads and writes if they are permitted.
5275 */
5276 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) {
5277 if (r->access & PL3_R) {
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005278 assert((r->fieldoffset ||
5279 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5280 r->readfn);
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005281 }
5282 if (r->access & PL3_W) {
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005283 assert((r->fieldoffset ||
5284 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) ||
5285 r->writefn);
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005286 }
5287 }
5288 /* Bad type field probably means missing sentinel at end of reg list */
5289 assert(cptype_valid(r->type));
5290 for (crm = crmmin; crm <= crmmax; crm++) {
5291 for (opc1 = opc1min; opc1 <= opc1max; opc1++) {
5292 for (opc2 = opc2min; opc2 <= opc2max; opc2++) {
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005293 for (state = ARM_CP_STATE_AA32;
5294 state <= ARM_CP_STATE_AA64; state++) {
5295 if (r->state != state && r->state != ARM_CP_STATE_BOTH) {
5296 continue;
5297 }
Fabian Aggeler3f3c82a2014-12-11 12:07:49 +00005298 if (state == ARM_CP_STATE_AA32) {
5299 /* Under AArch32 CP registers can be common
5300 * (same for secure and non-secure world) or banked.
5301 */
5302 switch (r->secure) {
5303 case ARM_CP_SECSTATE_S:
5304 case ARM_CP_SECSTATE_NS:
5305 add_cpreg_to_hashtable(cpu, r, opaque, state,
5306 r->secure, crm, opc1, opc2);
5307 break;
5308 default:
5309 add_cpreg_to_hashtable(cpu, r, opaque, state,
5310 ARM_CP_SECSTATE_S,
5311 crm, opc1, opc2);
5312 add_cpreg_to_hashtable(cpu, r, opaque, state,
5313 ARM_CP_SECSTATE_NS,
5314 crm, opc1, opc2);
5315 break;
5316 }
5317 } else {
5318 /* AArch64 registers get mapped to non-secure instance
5319 * of AArch32 */
5320 add_cpreg_to_hashtable(cpu, r, opaque, state,
5321 ARM_CP_SECSTATE_NS,
5322 crm, opc1, opc2);
5323 }
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005324 }
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005325 }
5326 }
5327 }
5328}
5329
5330void define_arm_cp_regs_with_opaque(ARMCPU *cpu,
5331 const ARMCPRegInfo *regs, void *opaque)
5332{
5333 /* Define a whole list of registers */
5334 const ARMCPRegInfo *r;
5335 for (r = regs; r->type != ARM_CP_SENTINEL; r++) {
5336 define_one_arm_cp_reg_with_opaque(cpu, r, opaque);
5337 }
5338}
5339
Peter Maydell60322b32014-01-04 22:15:44 +00005340const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp)
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005341{
Peter Maydell60322b32014-01-04 22:15:44 +00005342 return g_hash_table_lookup(cpregs, &encoded_cp);
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005343}
5344
Peter Maydellc4241c72014-02-20 10:35:54 +00005345void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri,
5346 uint64_t value)
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005347{
5348 /* Helper coprocessor write function for write-ignore registers */
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005349}
5350
Peter Maydellc4241c72014-02-20 10:35:54 +00005351uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri)
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005352{
5353 /* Helper coprocessor write function for read-as-zero registers */
Peter Maydell4b6a83f2012-06-20 11:57:06 +00005354 return 0;
5355}
5356
Peter Maydellf5a0a5a2014-01-04 22:15:44 +00005357void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque)
5358{
5359 /* Helper coprocessor reset function for do-nothing-on-reset registers */
5360}
5361
Peter Maydellaf393ff2016-02-23 15:36:44 +00005362static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type)
Peter Maydell37064a82012-01-05 15:49:06 +00005363{
5364 /* Return true if it is not valid for us to switch to
5365 * this CPU mode (ie all the UNPREDICTABLE cases in
5366 * the ARM ARM CPSRWriteByInstr pseudocode).
5367 */
Peter Maydellaf393ff2016-02-23 15:36:44 +00005368
5369 /* Changes to or from Hyp via MSR and CPS are illegal. */
5370 if (write_type == CPSRWriteByInstr &&
5371 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP ||
5372 mode == ARM_CPU_MODE_HYP)) {
5373 return 1;
5374 }
5375
Peter Maydell37064a82012-01-05 15:49:06 +00005376 switch (mode) {
5377 case ARM_CPU_MODE_USR:
Peter Maydell10eacda2016-02-23 15:36:44 +00005378 return 0;
Peter Maydell37064a82012-01-05 15:49:06 +00005379 case ARM_CPU_MODE_SYS:
5380 case ARM_CPU_MODE_SVC:
5381 case ARM_CPU_MODE_ABT:
5382 case ARM_CPU_MODE_UND:
5383 case ARM_CPU_MODE_IRQ:
5384 case ARM_CPU_MODE_FIQ:
Peter Maydell52ff9512016-02-23 15:36:44 +00005385 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7
5386 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.)
5387 */
Peter Maydell10eacda2016-02-23 15:36:44 +00005388 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR
5389 * and CPS are treated as illegal mode changes.
5390 */
5391 if (write_type == CPSRWriteByInstr &&
5392 (env->cp15.hcr_el2 & HCR_TGE) &&
5393 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON &&
5394 !arm_is_secure_below_el3(env)) {
5395 return 1;
5396 }
Peter Maydell37064a82012-01-05 15:49:06 +00005397 return 0;
Peter Maydelle6c8fc02016-02-23 15:36:44 +00005398 case ARM_CPU_MODE_HYP:
5399 return !arm_feature(env, ARM_FEATURE_EL2)
5400 || arm_current_el(env) < 2 || arm_is_secure(env);
Sergey Fedorov027fc522014-10-24 12:19:14 +01005401 case ARM_CPU_MODE_MON:
Peter Maydell58ae2d12016-02-23 15:36:44 +00005402 return arm_current_el(env) < 3;
Peter Maydell37064a82012-01-05 15:49:06 +00005403 default:
5404 return 1;
5405 }
5406}
5407
balrog2f4a40e2007-11-13 01:50:15 +00005408uint32_t cpsr_read(CPUARMState *env)
5409{
5410 int ZF;
pbrook6fbe23d2008-04-01 17:19:11 +00005411 ZF = (env->ZF == 0);
5412 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) |
balrog2f4a40e2007-11-13 01:50:15 +00005413 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27)
5414 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25)
5415 | ((env->condexec_bits & 0xfc) << 8)
Peter Maydellaf519932014-03-10 14:56:28 +00005416 | (env->GE << 16) | (env->daif & CPSR_AIF);
balrog2f4a40e2007-11-13 01:50:15 +00005417}
5418
Peter Maydell50866ba2016-02-23 15:36:43 +00005419void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask,
5420 CPSRWriteType write_type)
balrog2f4a40e2007-11-13 01:50:15 +00005421{
Fabian Aggeler6e8801f2014-12-11 12:07:50 +00005422 uint32_t changed_daif;
5423
balrog2f4a40e2007-11-13 01:50:15 +00005424 if (mask & CPSR_NZCV) {
pbrook6fbe23d2008-04-01 17:19:11 +00005425 env->ZF = (~val) & CPSR_Z;
5426 env->NF = val;
balrog2f4a40e2007-11-13 01:50:15 +00005427 env->CF = (val >> 29) & 1;
5428 env->VF = (val << 3) & 0x80000000;
5429 }
5430 if (mask & CPSR_Q)
5431 env->QF = ((val & CPSR_Q) != 0);
5432 if (mask & CPSR_T)
5433 env->thumb = ((val & CPSR_T) != 0);
5434 if (mask & CPSR_IT_0_1) {
5435 env->condexec_bits &= ~3;
5436 env->condexec_bits |= (val >> 25) & 3;
5437 }
5438 if (mask & CPSR_IT_2_7) {
5439 env->condexec_bits &= 3;
5440 env->condexec_bits |= (val >> 8) & 0xfc;
5441 }
5442 if (mask & CPSR_GE) {
5443 env->GE = (val >> 16) & 0xf;
5444 }
5445
Fabian Aggeler6e8801f2014-12-11 12:07:50 +00005446 /* In a V7 implementation that includes the security extensions but does
5447 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control
5448 * whether non-secure software is allowed to change the CPSR_F and CPSR_A
5449 * bits respectively.
5450 *
5451 * In a V8 implementation, it is permitted for privileged software to
5452 * change the CPSR A/F bits regardless of the SCR.AW/FW bits.
5453 */
Peter Maydellf8c88bb2016-02-23 15:36:43 +00005454 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) &&
Fabian Aggeler6e8801f2014-12-11 12:07:50 +00005455 arm_feature(env, ARM_FEATURE_EL3) &&
5456 !arm_feature(env, ARM_FEATURE_EL2) &&
5457 !arm_is_secure(env)) {
5458
5459 changed_daif = (env->daif ^ val) & mask;
5460
5461 if (changed_daif & CPSR_A) {
5462 /* Check to see if we are allowed to change the masking of async
5463 * abort exceptions from a non-secure state.
5464 */
5465 if (!(env->cp15.scr_el3 & SCR_AW)) {
5466 qemu_log_mask(LOG_GUEST_ERROR,
5467 "Ignoring attempt to switch CPSR_A flag from "
5468 "non-secure world with SCR.AW bit clear\n");
5469 mask &= ~CPSR_A;
5470 }
5471 }
5472
5473 if (changed_daif & CPSR_F) {
5474 /* Check to see if we are allowed to change the masking of FIQ
5475 * exceptions from a non-secure state.
5476 */
5477 if (!(env->cp15.scr_el3 & SCR_FW)) {
5478 qemu_log_mask(LOG_GUEST_ERROR,
5479 "Ignoring attempt to switch CPSR_F flag from "
5480 "non-secure world with SCR.FW bit clear\n");
5481 mask &= ~CPSR_F;
5482 }
5483
5484 /* Check whether non-maskable FIQ (NMFI) support is enabled.
5485 * If this bit is set software is not allowed to mask
5486 * FIQs, but is allowed to set CPSR_F to 0.
5487 */
5488 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) &&
5489 (val & CPSR_F)) {
5490 qemu_log_mask(LOG_GUEST_ERROR,
5491 "Ignoring attempt to enable CPSR_F flag "
5492 "(non-maskable FIQ [NMFI] support enabled)\n");
5493 mask &= ~CPSR_F;
5494 }
5495 }
5496 }
5497
Peter Maydell4cc35612014-02-26 17:20:06 +00005498 env->daif &= ~(CPSR_AIF & mask);
5499 env->daif |= val & CPSR_AIF & mask;
5500
Peter Maydellf8c88bb2016-02-23 15:36:43 +00005501 if (write_type != CPSRWriteRaw &&
5502 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) {
Peter Maydell8c4f0eb2016-03-04 11:30:16 +00005503 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) {
5504 /* Note that we can only get here in USR mode if this is a
5505 * gdb stub write; for this case we follow the architectural
5506 * behaviour for guest writes in USR mode of ignoring an attempt
5507 * to switch mode. (Those are caught by translate.c for writes
5508 * triggered by guest instructions.)
5509 */
5510 mask &= ~CPSR_M;
5511 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) {
Peter Maydell81907a52016-02-23 15:36:44 +00005512 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in
5513 * v7, and has defined behaviour in v8:
5514 * + leave CPSR.M untouched
5515 * + allow changes to the other CPSR fields
5516 * + set PSTATE.IL
5517 * For user changes via the GDB stub, we don't set PSTATE.IL,
5518 * as this would be unnecessarily harsh for a user error.
Peter Maydell37064a82012-01-05 15:49:06 +00005519 */
5520 mask &= ~CPSR_M;
Peter Maydell81907a52016-02-23 15:36:44 +00005521 if (write_type != CPSRWriteByGDBStub &&
5522 arm_feature(env, ARM_FEATURE_V8)) {
5523 mask |= CPSR_IL;
5524 val |= CPSR_IL;
5525 }
Peter Maydell37064a82012-01-05 15:49:06 +00005526 } else {
5527 switch_mode(env, val & CPSR_M);
5528 }
balrog2f4a40e2007-11-13 01:50:15 +00005529 }
5530 mask &= ~CACHED_CPSR_BITS;
5531 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask);
5532}
5533
pbrookb26eefb2008-03-31 03:44:26 +00005534/* Sign/zero extend */
5535uint32_t HELPER(sxtb16)(uint32_t x)
5536{
5537 uint32_t res;
5538 res = (uint16_t)(int8_t)x;
5539 res |= (uint32_t)(int8_t)(x >> 16) << 16;
5540 return res;
5541}
5542
5543uint32_t HELPER(uxtb16)(uint32_t x)
5544{
5545 uint32_t res;
5546 res = (uint16_t)(uint8_t)x;
5547 res |= (uint32_t)(uint8_t)(x >> 16) << 16;
5548 return res;
5549}
5550
pbrookf51bbbf2008-03-31 03:45:13 +00005551uint32_t HELPER(clz)(uint32_t x)
5552{
Aurelien Jarno7bbcb0a2009-10-15 23:14:52 +02005553 return clz32(x);
pbrookf51bbbf2008-03-31 03:45:13 +00005554}
5555
pbrook36706692008-03-31 03:46:19 +00005556int32_t HELPER(sdiv)(int32_t num, int32_t den)
5557{
5558 if (den == 0)
5559 return 0;
Aurelien Jarno686eeb92009-10-15 23:08:46 +02005560 if (num == INT_MIN && den == -1)
5561 return INT_MIN;
pbrook36706692008-03-31 03:46:19 +00005562 return num / den;
5563}
5564
5565uint32_t HELPER(udiv)(uint32_t num, uint32_t den)
5566{
5567 if (den == 0)
5568 return 0;
5569 return num / den;
5570}
5571
5572uint32_t HELPER(rbit)(uint32_t x)
5573{
Richard Henderson42fedbc2015-09-14 13:38:53 -07005574 return revbit32(x);
pbrook36706692008-03-31 03:46:19 +00005575}
5576
ths5fafdf22007-09-16 21:08:06 +00005577#if defined(CONFIG_USER_ONLY)
bellardb5ff1b32005-11-26 10:38:39 +00005578
pbrook9ee6e8b2007-11-11 00:04:49 +00005579/* These should probably raise undefined insn exceptions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01005580void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00005581{
Andreas Färbera47dddd2013-09-03 17:38:47 +02005582 ARMCPU *cpu = arm_env_get_cpu(env);
5583
5584 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg);
pbrook9ee6e8b2007-11-11 00:04:49 +00005585}
5586
Andreas Färber0ecb72a2012-03-14 01:38:21 +01005587uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00005588{
Andreas Färbera47dddd2013-09-03 17:38:47 +02005589 ARMCPU *cpu = arm_env_get_cpu(env);
5590
5591 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg);
pbrook9ee6e8b2007-11-11 00:04:49 +00005592 return 0;
5593}
5594
Andreas Färber0ecb72a2012-03-14 01:38:21 +01005595void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00005596{
Andreas Färbera47dddd2013-09-03 17:38:47 +02005597 ARMCPU *cpu = arm_env_get_cpu(env);
5598
5599 if (mode != ARM_CPU_MODE_USR) {
5600 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n");
5601 }
bellardb5ff1b32005-11-26 10:38:39 +00005602}
5603
Greg Bellows012a9062015-05-29 11:28:51 +01005604uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5605 uint32_t cur_el, bool secure)
Edgar E. Iglesias9e729b52014-09-29 18:48:49 +01005606{
5607 return 1;
5608}
5609
Greg Bellowsce020492015-02-13 05:46:08 +00005610void aarch64_sync_64_to_32(CPUARMState *env)
5611{
5612 g_assert_not_reached();
5613}
5614
bellardb5ff1b32005-11-26 10:38:39 +00005615#else
5616
Andreas Färber0ecb72a2012-03-14 01:38:21 +01005617void switch_mode(CPUARMState *env, int mode)
bellardb5ff1b32005-11-26 10:38:39 +00005618{
5619 int old_mode;
5620 int i;
5621
5622 old_mode = env->uncached_cpsr & CPSR_M;
5623 if (mode == old_mode)
5624 return;
5625
5626 if (old_mode == ARM_CPU_MODE_FIQ) {
5627 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00005628 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00005629 } else if (mode == ARM_CPU_MODE_FIQ) {
5630 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t));
pbrook8637c672006-03-14 14:20:32 +00005631 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t));
bellardb5ff1b32005-11-26 10:38:39 +00005632 }
5633
Peter Maydellf5206412013-03-05 00:34:40 +00005634 i = bank_number(old_mode);
bellardb5ff1b32005-11-26 10:38:39 +00005635 env->banked_r13[i] = env->regs[13];
5636 env->banked_r14[i] = env->regs[14];
5637 env->banked_spsr[i] = env->spsr;
5638
Peter Maydellf5206412013-03-05 00:34:40 +00005639 i = bank_number(mode);
bellardb5ff1b32005-11-26 10:38:39 +00005640 env->regs[13] = env->banked_r13[i];
5641 env->regs[14] = env->banked_r14[i];
5642 env->spsr = env->banked_spsr[i];
5643}
5644
Greg Bellows0eeb17d2014-12-11 12:07:48 +00005645/* Physical Interrupt Target EL Lookup Table
5646 *
5647 * [ From ARM ARM section G1.13.4 (Table G1-15) ]
5648 *
5649 * The below multi-dimensional table is used for looking up the target
5650 * exception level given numerous condition criteria. Specifically, the
5651 * target EL is based on SCR and HCR routing controls as well as the
5652 * currently executing EL and secure state.
5653 *
5654 * Dimensions:
5655 * target_el_table[2][2][2][2][2][4]
5656 * | | | | | +--- Current EL
5657 * | | | | +------ Non-secure(0)/Secure(1)
5658 * | | | +--------- HCR mask override
5659 * | | +------------ SCR exec state control
5660 * | +--------------- SCR mask override
5661 * +------------------ 32-bit(0)/64-bit(1) EL3
5662 *
5663 * The table values are as such:
5664 * 0-3 = EL0-EL3
5665 * -1 = Cannot occur
5666 *
5667 * The ARM ARM target EL table includes entries indicating that an "exception
5668 * is not taken". The two cases where this is applicable are:
5669 * 1) An exception is taken from EL3 but the SCR does not have the exception
5670 * routed to EL3.
5671 * 2) An exception is taken from EL2 but the HCR does not have the exception
5672 * routed to EL2.
5673 * In these two cases, the below table contain a target of EL1. This value is
5674 * returned as it is expected that the consumer of the table data will check
5675 * for "target EL >= current EL" to ensure the exception is not taken.
5676 *
5677 * SCR HCR
5678 * 64 EA AMO From
5679 * BIT IRQ IMO Non-secure Secure
5680 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3
5681 */
Stefan Weil82c39f62015-10-16 11:14:52 +01005682static const int8_t target_el_table[2][2][2][2][2][4] = {
Greg Bellows0eeb17d2014-12-11 12:07:48 +00005683 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5684 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},
5685 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },},
5686 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},},
5687 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5688 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},
5689 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},
5690 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},},
5691 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },},
5692 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},
5693 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },},
5694 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},},
5695 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5696 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},
5697 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},
5698 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},},
5699};
5700
5701/*
5702 * Determine the target EL for physical exceptions
5703 */
Greg Bellows012a9062015-05-29 11:28:51 +01005704uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx,
5705 uint32_t cur_el, bool secure)
Greg Bellows0eeb17d2014-12-11 12:07:48 +00005706{
5707 CPUARMState *env = cs->env_ptr;
Sergey Sorokin2cde0312015-10-16 11:14:52 +01005708 int rw;
Greg Bellows0eeb17d2014-12-11 12:07:48 +00005709 int scr;
5710 int hcr;
5711 int target_el;
Sergey Sorokin2cde0312015-10-16 11:14:52 +01005712 /* Is the highest EL AArch64? */
5713 int is64 = arm_feature(env, ARM_FEATURE_AARCH64);
5714
5715 if (arm_feature(env, ARM_FEATURE_EL3)) {
5716 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW);
5717 } else {
5718 /* Either EL2 is the highest EL (and so the EL2 register width
5719 * is given by is64); or there is no EL2 or EL3, in which case
5720 * the value of 'rw' does not affect the table lookup anyway.
5721 */
5722 rw = is64;
5723 }
Greg Bellows0eeb17d2014-12-11 12:07:48 +00005724
5725 switch (excp_idx) {
5726 case EXCP_IRQ:
5727 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ);
5728 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO);
5729 break;
5730 case EXCP_FIQ:
5731 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ);
5732 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO);
5733 break;
5734 default:
5735 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA);
5736 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO);
5737 break;
5738 };
5739
5740 /* If HCR.TGE is set then HCR is treated as being 1 */
5741 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE);
5742
5743 /* Perform a table-lookup for the target EL given the current state */
5744 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el];
5745
5746 assert(target_el > 0);
5747
5748 return target_el;
5749}
5750
pbrook9ee6e8b2007-11-11 00:04:49 +00005751static void v7m_push(CPUARMState *env, uint32_t val)
5752{
Andreas Färber70d74662014-03-09 19:10:29 +01005753 CPUState *cs = CPU(arm_env_get_cpu(env));
5754
pbrook9ee6e8b2007-11-11 00:04:49 +00005755 env->regs[13] -= 4;
Edgar E. Iglesiasab1da852013-12-17 15:07:29 +10005756 stl_phys(cs->as, env->regs[13], val);
pbrook9ee6e8b2007-11-11 00:04:49 +00005757}
5758
5759static uint32_t v7m_pop(CPUARMState *env)
5760{
Andreas Färber70d74662014-03-09 19:10:29 +01005761 CPUState *cs = CPU(arm_env_get_cpu(env));
pbrook9ee6e8b2007-11-11 00:04:49 +00005762 uint32_t val;
Andreas Färber70d74662014-03-09 19:10:29 +01005763
Edgar E. Iglesiasfdfba1a2013-11-15 14:46:38 +01005764 val = ldl_phys(cs->as, env->regs[13]);
pbrook9ee6e8b2007-11-11 00:04:49 +00005765 env->regs[13] += 4;
5766 return val;
5767}
5768
5769/* Switch to V7M main or process stack pointer. */
5770static void switch_v7m_sp(CPUARMState *env, int process)
5771{
5772 uint32_t tmp;
5773 if (env->v7m.current_sp != process) {
5774 tmp = env->v7m.other_sp;
5775 env->v7m.other_sp = env->regs[13];
5776 env->regs[13] = tmp;
5777 env->v7m.current_sp = process;
5778 }
5779}
5780
5781static void do_v7m_exception_exit(CPUARMState *env)
5782{
5783 uint32_t type;
5784 uint32_t xpsr;
5785
5786 type = env->regs[15];
5787 if (env->v7m.exception != 0)
Paul Brook983fe822010-04-05 19:34:51 +01005788 armv7m_nvic_complete_irq(env->nvic, env->v7m.exception);
pbrook9ee6e8b2007-11-11 00:04:49 +00005789
5790 /* Switch to the target stack. */
5791 switch_v7m_sp(env, (type & 4) != 0);
5792 /* Pop registers. */
5793 env->regs[0] = v7m_pop(env);
5794 env->regs[1] = v7m_pop(env);
5795 env->regs[2] = v7m_pop(env);
5796 env->regs[3] = v7m_pop(env);
5797 env->regs[12] = v7m_pop(env);
5798 env->regs[14] = v7m_pop(env);
5799 env->regs[15] = v7m_pop(env);
Peter Maydellfcf83ab2015-03-16 12:30:47 +00005800 if (env->regs[15] & 1) {
5801 qemu_log_mask(LOG_GUEST_ERROR,
5802 "M profile return from interrupt with misaligned "
5803 "PC is UNPREDICTABLE\n");
5804 /* Actual hardware seems to ignore the lsbit, and there are several
5805 * RTOSes out there which incorrectly assume the r15 in the stack
5806 * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value.
5807 */
5808 env->regs[15] &= ~1U;
5809 }
pbrook9ee6e8b2007-11-11 00:04:49 +00005810 xpsr = v7m_pop(env);
5811 xpsr_write(env, xpsr, 0xfffffdff);
5812 /* Undo stack alignment. */
5813 if (xpsr & 0x200)
5814 env->regs[13] |= 4;
5815 /* ??? The exception return type specifies Thread/Handler mode. However
5816 this is also implied by the xPSR value. Not sure what to do
5817 if there is a mismatch. */
5818 /* ??? Likewise for mismatches between the CONTROL register and the stack
5819 pointer. */
5820}
5821
Andreas Färbere6f010c2013-02-02 12:33:14 +01005822void arm_v7m_cpu_do_interrupt(CPUState *cs)
pbrook9ee6e8b2007-11-11 00:04:49 +00005823{
Andreas Färbere6f010c2013-02-02 12:33:14 +01005824 ARMCPU *cpu = ARM_CPU(cs);
5825 CPUARMState *env = &cpu->env;
pbrook9ee6e8b2007-11-11 00:04:49 +00005826 uint32_t xpsr = xpsr_read(env);
5827 uint32_t lr;
5828 uint32_t addr;
5829
Andreas Färber27103422013-08-26 08:31:06 +02005830 arm_log_exception(cs->exception_index);
Peter Maydell3f1beac2013-08-20 14:54:28 +01005831
pbrook9ee6e8b2007-11-11 00:04:49 +00005832 lr = 0xfffffff1;
5833 if (env->v7m.current_sp)
5834 lr |= 4;
5835 if (env->v7m.exception == 0)
5836 lr |= 8;
5837
5838 /* For exceptions we just mark as pending on the NVIC, and let that
5839 handle it. */
5840 /* TODO: Need to escalate if the current priority is higher than the
5841 one we're raising. */
Andreas Färber27103422013-08-26 08:31:06 +02005842 switch (cs->exception_index) {
pbrook9ee6e8b2007-11-11 00:04:49 +00005843 case EXCP_UDEF:
Paul Brook983fe822010-04-05 19:34:51 +01005844 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE);
pbrook9ee6e8b2007-11-11 00:04:49 +00005845 return;
5846 case EXCP_SWI:
Alex_Rozenman@mentor.com314e2292013-01-11 15:21:22 +00005847 /* The PC already points to the next instruction. */
Paul Brook983fe822010-04-05 19:34:51 +01005848 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC);
pbrook9ee6e8b2007-11-11 00:04:49 +00005849 return;
5850 case EXCP_PREFETCH_ABORT:
5851 case EXCP_DATA_ABORT:
Peter Maydellabf11722014-04-15 19:18:38 +01005852 /* TODO: if we implemented the MPU registers, this is where we
5853 * should set the MMFAR, etc from exception.fsr and exception.vaddress.
5854 */
Paul Brook983fe822010-04-05 19:34:51 +01005855 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM);
pbrook9ee6e8b2007-11-11 00:04:49 +00005856 return;
5857 case EXCP_BKPT:
Leon Alraecfe67ce2015-06-19 14:17:45 +01005858 if (semihosting_enabled()) {
pbrook2ad207d2007-11-24 23:22:11 +00005859 int nr;
Paolo Bonzinif9fd40e2016-03-04 11:30:19 +00005860 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff;
pbrook2ad207d2007-11-24 23:22:11 +00005861 if (nr == 0xab) {
5862 env->regs[15] += 2;
Christopher Covington205ace52015-09-07 10:39:27 +01005863 qemu_log_mask(CPU_LOG_INT,
5864 "...handling as semihosting call 0x%x\n",
5865 env->regs[0]);
pbrook2ad207d2007-11-24 23:22:11 +00005866 env->regs[0] = do_arm_semihosting(env);
5867 return;
5868 }
5869 }
Paul Brook983fe822010-04-05 19:34:51 +01005870 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG);
pbrook9ee6e8b2007-11-11 00:04:49 +00005871 return;
5872 case EXCP_IRQ:
Paul Brook983fe822010-04-05 19:34:51 +01005873 env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic);
pbrook9ee6e8b2007-11-11 00:04:49 +00005874 break;
5875 case EXCP_EXCEPTION_EXIT:
5876 do_v7m_exception_exit(env);
5877 return;
5878 default:
Andreas Färbera47dddd2013-09-03 17:38:47 +02005879 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
pbrook9ee6e8b2007-11-11 00:04:49 +00005880 return; /* Never happens. Keep compiler happy. */
5881 }
5882
5883 /* Align stack pointer. */
5884 /* ??? Should only do this if Configuration Control Register
5885 STACKALIGN bit is set. */
5886 if (env->regs[13] & 4) {
pbrookab19b0e2008-07-02 16:44:09 +00005887 env->regs[13] -= 4;
pbrook9ee6e8b2007-11-11 00:04:49 +00005888 xpsr |= 0x200;
5889 }
balrog6c956762008-04-13 00:57:49 +00005890 /* Switch to the handler mode. */
pbrook9ee6e8b2007-11-11 00:04:49 +00005891 v7m_push(env, xpsr);
5892 v7m_push(env, env->regs[15]);
5893 v7m_push(env, env->regs[14]);
5894 v7m_push(env, env->regs[12]);
5895 v7m_push(env, env->regs[3]);
5896 v7m_push(env, env->regs[2]);
5897 v7m_push(env, env->regs[1]);
5898 v7m_push(env, env->regs[0]);
5899 switch_v7m_sp(env, 0);
Peter Maydellc98d1742012-03-14 12:26:10 +00005900 /* Clear IT bits */
5901 env->condexec_bits = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00005902 env->regs[14] = lr;
Edgar E. Iglesiasfdfba1a2013-11-15 14:46:38 +01005903 addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4);
pbrook9ee6e8b2007-11-11 00:04:49 +00005904 env->regs[15] = addr & 0xfffffffe;
5905 env->thumb = addr & 1;
5906}
5907
Greg Bellowsce020492015-02-13 05:46:08 +00005908/* Function used to synchronize QEMU's AArch64 register set with AArch32
5909 * register set. This is necessary when switching between AArch32 and AArch64
5910 * execution state.
5911 */
5912void aarch64_sync_32_to_64(CPUARMState *env)
5913{
5914 int i;
5915 uint32_t mode = env->uncached_cpsr & CPSR_M;
5916
5917 /* We can blanket copy R[0:7] to X[0:7] */
5918 for (i = 0; i < 8; i++) {
5919 env->xregs[i] = env->regs[i];
5920 }
5921
5922 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12.
5923 * Otherwise, they come from the banked user regs.
5924 */
5925 if (mode == ARM_CPU_MODE_FIQ) {
5926 for (i = 8; i < 13; i++) {
5927 env->xregs[i] = env->usr_regs[i - 8];
5928 }
5929 } else {
5930 for (i = 8; i < 13; i++) {
5931 env->xregs[i] = env->regs[i];
5932 }
5933 }
5934
5935 /* Registers x13-x23 are the various mode SP and FP registers. Registers
5936 * r13 and r14 are only copied if we are in that mode, otherwise we copy
5937 * from the mode banked register.
5938 */
5939 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
5940 env->xregs[13] = env->regs[13];
5941 env->xregs[14] = env->regs[14];
5942 } else {
5943 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)];
5944 /* HYP is an exception in that it is copied from r14 */
5945 if (mode == ARM_CPU_MODE_HYP) {
5946 env->xregs[14] = env->regs[14];
5947 } else {
5948 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)];
5949 }
5950 }
5951
5952 if (mode == ARM_CPU_MODE_HYP) {
5953 env->xregs[15] = env->regs[13];
5954 } else {
5955 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)];
5956 }
5957
5958 if (mode == ARM_CPU_MODE_IRQ) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01005959 env->xregs[16] = env->regs[14];
5960 env->xregs[17] = env->regs[13];
Greg Bellowsce020492015-02-13 05:46:08 +00005961 } else {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01005962 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)];
5963 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)];
Greg Bellowsce020492015-02-13 05:46:08 +00005964 }
5965
5966 if (mode == ARM_CPU_MODE_SVC) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01005967 env->xregs[18] = env->regs[14];
5968 env->xregs[19] = env->regs[13];
Greg Bellowsce020492015-02-13 05:46:08 +00005969 } else {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01005970 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)];
5971 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)];
Greg Bellowsce020492015-02-13 05:46:08 +00005972 }
5973
5974 if (mode == ARM_CPU_MODE_ABT) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01005975 env->xregs[20] = env->regs[14];
5976 env->xregs[21] = env->regs[13];
Greg Bellowsce020492015-02-13 05:46:08 +00005977 } else {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01005978 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)];
5979 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)];
Greg Bellowsce020492015-02-13 05:46:08 +00005980 }
5981
5982 if (mode == ARM_CPU_MODE_UND) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01005983 env->xregs[22] = env->regs[14];
5984 env->xregs[23] = env->regs[13];
Greg Bellowsce020492015-02-13 05:46:08 +00005985 } else {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01005986 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)];
5987 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)];
Greg Bellowsce020492015-02-13 05:46:08 +00005988 }
5989
5990 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
5991 * mode, then we can copy from r8-r14. Otherwise, we copy from the
5992 * FIQ bank for r8-r14.
5993 */
5994 if (mode == ARM_CPU_MODE_FIQ) {
5995 for (i = 24; i < 31; i++) {
5996 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */
5997 }
5998 } else {
5999 for (i = 24; i < 29; i++) {
6000 env->xregs[i] = env->fiq_regs[i - 24];
6001 }
6002 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)];
6003 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)];
6004 }
6005
6006 env->pc = env->regs[15];
6007}
6008
6009/* Function used to synchronize QEMU's AArch32 register set with AArch64
6010 * register set. This is necessary when switching between AArch32 and AArch64
6011 * execution state.
6012 */
6013void aarch64_sync_64_to_32(CPUARMState *env)
6014{
6015 int i;
6016 uint32_t mode = env->uncached_cpsr & CPSR_M;
6017
6018 /* We can blanket copy X[0:7] to R[0:7] */
6019 for (i = 0; i < 8; i++) {
6020 env->regs[i] = env->xregs[i];
6021 }
6022
6023 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12.
6024 * Otherwise, we copy x8-x12 into the banked user regs.
6025 */
6026 if (mode == ARM_CPU_MODE_FIQ) {
6027 for (i = 8; i < 13; i++) {
6028 env->usr_regs[i - 8] = env->xregs[i];
6029 }
6030 } else {
6031 for (i = 8; i < 13; i++) {
6032 env->regs[i] = env->xregs[i];
6033 }
6034 }
6035
6036 /* Registers r13 & r14 depend on the current mode.
6037 * If we are in a given mode, we copy the corresponding x registers to r13
6038 * and r14. Otherwise, we copy the x register to the banked r13 and r14
6039 * for the mode.
6040 */
6041 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) {
6042 env->regs[13] = env->xregs[13];
6043 env->regs[14] = env->xregs[14];
6044 } else {
6045 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13];
6046
6047 /* HYP is an exception in that it does not have its own banked r14 but
6048 * shares the USR r14
6049 */
6050 if (mode == ARM_CPU_MODE_HYP) {
6051 env->regs[14] = env->xregs[14];
6052 } else {
6053 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14];
6054 }
6055 }
6056
6057 if (mode == ARM_CPU_MODE_HYP) {
6058 env->regs[13] = env->xregs[15];
6059 } else {
6060 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15];
6061 }
6062
6063 if (mode == ARM_CPU_MODE_IRQ) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01006064 env->regs[14] = env->xregs[16];
6065 env->regs[13] = env->xregs[17];
Greg Bellowsce020492015-02-13 05:46:08 +00006066 } else {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01006067 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16];
6068 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17];
Greg Bellowsce020492015-02-13 05:46:08 +00006069 }
6070
6071 if (mode == ARM_CPU_MODE_SVC) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01006072 env->regs[14] = env->xregs[18];
6073 env->regs[13] = env->xregs[19];
Greg Bellowsce020492015-02-13 05:46:08 +00006074 } else {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01006075 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18];
6076 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19];
Greg Bellowsce020492015-02-13 05:46:08 +00006077 }
6078
6079 if (mode == ARM_CPU_MODE_ABT) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01006080 env->regs[14] = env->xregs[20];
6081 env->regs[13] = env->xregs[21];
Greg Bellowsce020492015-02-13 05:46:08 +00006082 } else {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01006083 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20];
6084 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21];
Greg Bellowsce020492015-02-13 05:46:08 +00006085 }
6086
6087 if (mode == ARM_CPU_MODE_UND) {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01006088 env->regs[14] = env->xregs[22];
6089 env->regs[13] = env->xregs[23];
Greg Bellowsce020492015-02-13 05:46:08 +00006090 } else {
Sergey Sorokin3a9148d2015-09-07 10:39:29 +01006091 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22];
6092 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23];
Greg Bellowsce020492015-02-13 05:46:08 +00006093 }
6094
6095 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ
6096 * mode, then we can copy to r8-r14. Otherwise, we copy to the
6097 * FIQ bank for r8-r14.
6098 */
6099 if (mode == ARM_CPU_MODE_FIQ) {
6100 for (i = 24; i < 31; i++) {
6101 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */
6102 }
6103 } else {
6104 for (i = 24; i < 29; i++) {
6105 env->fiq_regs[i - 24] = env->xregs[i];
6106 }
6107 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29];
6108 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30];
6109 }
6110
6111 env->regs[15] = env->pc;
6112}
6113
Peter Maydell966f7582016-01-21 14:15:08 +00006114static void arm_cpu_do_interrupt_aarch32(CPUState *cs)
bellardb5ff1b32005-11-26 10:38:39 +00006115{
Andreas Färber97a8ea52013-02-02 10:57:51 +01006116 ARMCPU *cpu = ARM_CPU(cs);
6117 CPUARMState *env = &cpu->env;
bellardb5ff1b32005-11-26 10:38:39 +00006118 uint32_t addr;
6119 uint32_t mask;
6120 int new_mode;
6121 uint32_t offset;
Peter Maydell16a906f2014-09-12 14:06:49 +01006122 uint32_t moe;
bellardb5ff1b32005-11-26 10:38:39 +00006123
Peter Maydell16a906f2014-09-12 14:06:49 +01006124 /* If this is a debug exception we must update the DBGDSCR.MOE bits */
6125 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) {
6126 case EC_BREAKPOINT:
6127 case EC_BREAKPOINT_SAME_EL:
6128 moe = 1;
6129 break;
6130 case EC_WATCHPOINT:
6131 case EC_WATCHPOINT_SAME_EL:
6132 moe = 10;
6133 break;
6134 case EC_AA32_BKPT:
6135 moe = 3;
6136 break;
6137 case EC_VECTORCATCH:
6138 moe = 5;
6139 break;
6140 default:
6141 moe = 0;
6142 break;
6143 }
6144
6145 if (moe) {
6146 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe);
6147 }
6148
bellardb5ff1b32005-11-26 10:38:39 +00006149 /* TODO: Vectored interrupt controller. */
Andreas Färber27103422013-08-26 08:31:06 +02006150 switch (cs->exception_index) {
bellardb5ff1b32005-11-26 10:38:39 +00006151 case EXCP_UDEF:
6152 new_mode = ARM_CPU_MODE_UND;
6153 addr = 0x04;
6154 mask = CPSR_I;
6155 if (env->thumb)
6156 offset = 2;
6157 else
6158 offset = 4;
6159 break;
6160 case EXCP_SWI:
6161 new_mode = ARM_CPU_MODE_SVC;
6162 addr = 0x08;
6163 mask = CPSR_I;
balrog601d70b2008-04-20 01:03:45 +00006164 /* The PC already points to the next instruction. */
bellardb5ff1b32005-11-26 10:38:39 +00006165 offset = 0;
6166 break;
pbrook06c949e2006-02-04 19:35:26 +00006167 case EXCP_BKPT:
Peter Maydellabf11722014-04-15 19:18:38 +01006168 env->exception.fsr = 2;
pbrook9ee6e8b2007-11-11 00:04:49 +00006169 /* Fall through to prefetch abort. */
6170 case EXCP_PREFETCH_ABORT:
Fabian Aggeler88ca1c22014-12-11 12:07:51 +00006171 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr);
Fabian Aggelerb848ce22014-12-11 12:07:51 +00006172 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress);
Peter Maydell3f1beac2013-08-20 14:54:28 +01006173 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n",
Fabian Aggeler88ca1c22014-12-11 12:07:51 +00006174 env->exception.fsr, (uint32_t)env->exception.vaddress);
bellardb5ff1b32005-11-26 10:38:39 +00006175 new_mode = ARM_CPU_MODE_ABT;
6176 addr = 0x0c;
6177 mask = CPSR_A | CPSR_I;
6178 offset = 4;
6179 break;
6180 case EXCP_DATA_ABORT:
Fabian Aggeler4a7e2d72014-12-11 12:07:51 +00006181 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr);
Fabian Aggelerb848ce22014-12-11 12:07:51 +00006182 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress);
Peter Maydell3f1beac2013-08-20 14:54:28 +01006183 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n",
Fabian Aggeler4a7e2d72014-12-11 12:07:51 +00006184 env->exception.fsr,
Rob Herring6cd8a262014-04-15 19:18:42 +01006185 (uint32_t)env->exception.vaddress);
bellardb5ff1b32005-11-26 10:38:39 +00006186 new_mode = ARM_CPU_MODE_ABT;
6187 addr = 0x10;
6188 mask = CPSR_A | CPSR_I;
6189 offset = 8;
6190 break;
6191 case EXCP_IRQ:
6192 new_mode = ARM_CPU_MODE_IRQ;
6193 addr = 0x18;
6194 /* Disable IRQ and imprecise data aborts. */
6195 mask = CPSR_A | CPSR_I;
6196 offset = 4;
Fabian Aggelerde38d232014-12-11 12:07:49 +00006197 if (env->cp15.scr_el3 & SCR_IRQ) {
6198 /* IRQ routed to monitor mode */
6199 new_mode = ARM_CPU_MODE_MON;
6200 mask |= CPSR_F;
6201 }
bellardb5ff1b32005-11-26 10:38:39 +00006202 break;
6203 case EXCP_FIQ:
6204 new_mode = ARM_CPU_MODE_FIQ;
6205 addr = 0x1c;
6206 /* Disable FIQ, IRQ and imprecise data aborts. */
6207 mask = CPSR_A | CPSR_I | CPSR_F;
Fabian Aggelerde38d232014-12-11 12:07:49 +00006208 if (env->cp15.scr_el3 & SCR_FIQ) {
6209 /* FIQ routed to monitor mode */
6210 new_mode = ARM_CPU_MODE_MON;
6211 }
bellardb5ff1b32005-11-26 10:38:39 +00006212 offset = 4;
6213 break;
Fabian Aggelerdbe9d162014-10-24 12:19:15 +01006214 case EXCP_SMC:
6215 new_mode = ARM_CPU_MODE_MON;
6216 addr = 0x08;
6217 mask = CPSR_A | CPSR_I | CPSR_F;
6218 offset = 0;
6219 break;
bellardb5ff1b32005-11-26 10:38:39 +00006220 default:
Andreas Färbera47dddd2013-09-03 17:38:47 +02006221 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
bellardb5ff1b32005-11-26 10:38:39 +00006222 return; /* Never happens. Keep compiler happy. */
6223 }
Fabian Aggelere89e51a2014-12-11 12:07:50 +00006224
6225 if (new_mode == ARM_CPU_MODE_MON) {
6226 addr += env->cp15.mvbar;
Fabian Aggeler137feaa2014-12-11 12:07:50 +00006227 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) {
Fabian Aggelere89e51a2014-12-11 12:07:50 +00006228 /* High vectors. When enabled, base address cannot be remapped. */
bellardb5ff1b32005-11-26 10:38:39 +00006229 addr += 0xffff0000;
Nathan Rossi86411362013-10-25 15:44:38 +01006230 } else {
6231 /* ARM v7 architectures provide a vector base address register to remap
6232 * the interrupt vector table.
Fabian Aggelere89e51a2014-12-11 12:07:50 +00006233 * This register is only followed in non-monitor mode, and is banked.
Nathan Rossi86411362013-10-25 15:44:38 +01006234 * Note: only bits 31:5 are valid.
6235 */
Greg Bellowsfb6c91b2014-12-11 12:07:52 +00006236 addr += A32_BANKED_CURRENT_REG_GET(env, vbar);
bellardb5ff1b32005-11-26 10:38:39 +00006237 }
Fabian Aggelerdbe9d162014-10-24 12:19:15 +01006238
6239 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) {
6240 env->cp15.scr_el3 &= ~SCR_NS;
6241 }
6242
bellardb5ff1b32005-11-26 10:38:39 +00006243 switch_mode (env, new_mode);
Peter Maydell662cefb2014-08-19 18:56:26 +01006244 /* For exceptions taken to AArch32 we must clear the SS bit in both
6245 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now.
6246 */
6247 env->uncached_cpsr &= ~PSTATE_SS;
bellardb5ff1b32005-11-26 10:38:39 +00006248 env->spsr = cpsr_read(env);
pbrook9ee6e8b2007-11-11 00:04:49 +00006249 /* Clear IT bits. */
6250 env->condexec_bits = 0;
Rabin Vincent30a8cac2010-02-15 00:02:36 +05306251 /* Switch to the new mode, and to the correct instruction set. */
bellard6d7e6322005-12-18 16:54:08 +00006252 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode;
Peter Crosthwaite73462dd2016-03-04 11:30:20 +00006253 /* Set new mode endianness */
6254 env->uncached_cpsr &= ~CPSR_E;
6255 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) {
6256 env->uncached_cpsr |= ~CPSR_E;
6257 }
Peter Maydell4cc35612014-02-26 17:20:06 +00006258 env->daif |= mask;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +04006259 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares
6260 * and we should just guard the thumb mode on V4 */
6261 if (arm_feature(env, ARM_FEATURE_V4T)) {
Fabian Aggeler137feaa2014-12-11 12:07:50 +00006262 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0;
Dmitry Eremin-Solenikovbe5e7a72011-04-04 17:38:44 +04006263 }
bellardb5ff1b32005-11-26 10:38:39 +00006264 env->regs[14] = env->regs[15] + offset;
6265 env->regs[15] = addr;
bellardb5ff1b32005-11-26 10:38:39 +00006266}
6267
Peter Maydell966f7582016-01-21 14:15:08 +00006268/* Handle exception entry to a target EL which is using AArch64 */
6269static void arm_cpu_do_interrupt_aarch64(CPUState *cs)
Peter Maydellf3a9b692016-01-21 14:15:08 +00006270{
6271 ARMCPU *cpu = ARM_CPU(cs);
6272 CPUARMState *env = &cpu->env;
6273 unsigned int new_el = env->exception.target_el;
6274 target_ulong addr = env->cp15.vbar_el[new_el];
6275 unsigned int new_mode = aarch64_pstate_mode(new_el, true);
6276
6277 if (arm_current_el(env) < new_el) {
Peter Maydell3d6f7612016-01-21 14:15:08 +00006278 /* Entry vector offset depends on whether the implemented EL
6279 * immediately lower than the target level is using AArch32 or AArch64
6280 */
6281 bool is_aa64;
6282
6283 switch (new_el) {
6284 case 3:
6285 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0;
6286 break;
6287 case 2:
6288 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0;
6289 break;
6290 case 1:
6291 is_aa64 = is_a64(env);
6292 break;
6293 default:
6294 g_assert_not_reached();
6295 }
6296
6297 if (is_aa64) {
Peter Maydellf3a9b692016-01-21 14:15:08 +00006298 addr += 0x400;
6299 } else {
6300 addr += 0x600;
6301 }
6302 } else if (pstate_read(env) & PSTATE_SP) {
6303 addr += 0x200;
6304 }
6305
Peter Maydellf3a9b692016-01-21 14:15:08 +00006306 switch (cs->exception_index) {
6307 case EXCP_PREFETCH_ABORT:
6308 case EXCP_DATA_ABORT:
6309 env->cp15.far_el[new_el] = env->exception.vaddress;
6310 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n",
6311 env->cp15.far_el[new_el]);
6312 /* fall through */
6313 case EXCP_BKPT:
6314 case EXCP_UDEF:
6315 case EXCP_SWI:
6316 case EXCP_HVC:
6317 case EXCP_HYP_TRAP:
6318 case EXCP_SMC:
6319 env->cp15.esr_el[new_el] = env->exception.syndrome;
6320 break;
6321 case EXCP_IRQ:
6322 case EXCP_VIRQ:
6323 addr += 0x80;
6324 break;
6325 case EXCP_FIQ:
6326 case EXCP_VFIQ:
6327 addr += 0x100;
6328 break;
6329 case EXCP_SEMIHOST:
6330 qemu_log_mask(CPU_LOG_INT,
6331 "...handling as semihosting call 0x%" PRIx64 "\n",
6332 env->xregs[0]);
6333 env->xregs[0] = do_arm_semihosting(env);
6334 return;
6335 default:
6336 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index);
6337 }
6338
6339 if (is_a64(env)) {
6340 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env);
6341 aarch64_save_sp(env, arm_current_el(env));
6342 env->elr_el[new_el] = env->pc;
6343 } else {
6344 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env);
6345 if (!env->thumb) {
6346 env->cp15.esr_el[new_el] |= 1 << 25;
6347 }
6348 env->elr_el[new_el] = env->regs[15];
6349
6350 aarch64_sync_32_to_64(env);
6351
6352 env->condexec_bits = 0;
6353 }
6354 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n",
6355 env->elr_el[new_el]);
6356
6357 pstate_write(env, PSTATE_DAIF | new_mode);
6358 env->aarch64 = 1;
6359 aarch64_restore_sp(env, new_el);
6360
6361 env->pc = addr;
6362
6363 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n",
6364 new_el, env->pc, pstate_read(env));
Peter Maydell966f7582016-01-21 14:15:08 +00006365}
6366
Peter Maydell904c04d2016-01-21 14:15:08 +00006367static inline bool check_for_semihosting(CPUState *cs)
6368{
6369 /* Check whether this exception is a semihosting call; if so
6370 * then handle it and return true; otherwise return false.
6371 */
6372 ARMCPU *cpu = ARM_CPU(cs);
6373 CPUARMState *env = &cpu->env;
6374
6375 if (is_a64(env)) {
6376 if (cs->exception_index == EXCP_SEMIHOST) {
6377 /* This is always the 64-bit semihosting exception.
6378 * The "is this usermode" and "is semihosting enabled"
6379 * checks have been done at translate time.
6380 */
6381 qemu_log_mask(CPU_LOG_INT,
6382 "...handling as semihosting call 0x%" PRIx64 "\n",
6383 env->xregs[0]);
6384 env->xregs[0] = do_arm_semihosting(env);
6385 return true;
6386 }
6387 return false;
6388 } else {
6389 uint32_t imm;
6390
6391 /* Only intercept calls from privileged modes, to provide some
6392 * semblance of security.
6393 */
6394 if (!semihosting_enabled() ||
6395 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR)) {
6396 return false;
6397 }
6398
6399 switch (cs->exception_index) {
6400 case EXCP_SWI:
6401 /* Check for semihosting interrupt. */
6402 if (env->thumb) {
Paolo Bonzinif9fd40e2016-03-04 11:30:19 +00006403 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env))
Peter Maydell904c04d2016-01-21 14:15:08 +00006404 & 0xff;
6405 if (imm == 0xab) {
6406 break;
6407 }
6408 } else {
Paolo Bonzinif9fd40e2016-03-04 11:30:19 +00006409 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env))
Peter Maydell904c04d2016-01-21 14:15:08 +00006410 & 0xffffff;
6411 if (imm == 0x123456) {
6412 break;
6413 }
6414 }
6415 return false;
6416 case EXCP_BKPT:
6417 /* See if this is a semihosting syscall. */
6418 if (env->thumb) {
Paolo Bonzinif9fd40e2016-03-04 11:30:19 +00006419 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env))
Peter Maydell904c04d2016-01-21 14:15:08 +00006420 & 0xff;
6421 if (imm == 0xab) {
6422 env->regs[15] += 2;
6423 break;
6424 }
6425 }
6426 return false;
6427 default:
6428 return false;
6429 }
6430
6431 qemu_log_mask(CPU_LOG_INT,
6432 "...handling as semihosting call 0x%x\n",
6433 env->regs[0]);
6434 env->regs[0] = do_arm_semihosting(env);
6435 return true;
6436 }
6437}
6438
Peter Maydell966f7582016-01-21 14:15:08 +00006439/* Handle a CPU exception for A and R profile CPUs.
6440 * Do any appropriate logging, handle PSCI calls, and then hand off
6441 * to the AArch64-entry or AArch32-entry function depending on the
6442 * target exception level's register width.
6443 */
6444void arm_cpu_do_interrupt(CPUState *cs)
6445{
6446 ARMCPU *cpu = ARM_CPU(cs);
6447 CPUARMState *env = &cpu->env;
6448 unsigned int new_el = env->exception.target_el;
6449
6450 assert(!IS_M(env));
6451
6452 arm_log_exception(cs->exception_index);
6453 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env),
6454 new_el);
6455 if (qemu_loglevel_mask(CPU_LOG_INT)
6456 && !excp_is_internal(cs->exception_index)) {
6457 qemu_log_mask(CPU_LOG_INT, "...with ESR %x/0x%" PRIx32 "\n",
6458 env->exception.syndrome >> ARM_EL_EC_SHIFT,
6459 env->exception.syndrome);
6460 }
6461
6462 if (arm_is_psci_call(cpu, cs->exception_index)) {
6463 arm_handle_psci_call(cpu);
6464 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n");
6465 return;
6466 }
6467
Peter Maydell904c04d2016-01-21 14:15:08 +00006468 /* Semihosting semantics depend on the register width of the
6469 * code that caused the exception, not the target exception level,
6470 * so must be handled here.
Peter Maydell966f7582016-01-21 14:15:08 +00006471 */
Peter Maydell904c04d2016-01-21 14:15:08 +00006472 if (check_for_semihosting(cs)) {
6473 return;
6474 }
6475
6476 assert(!excp_is_internal(cs->exception_index));
6477 if (arm_el_is_aa64(env, new_el)) {
Peter Maydell966f7582016-01-21 14:15:08 +00006478 arm_cpu_do_interrupt_aarch64(cs);
6479 } else {
6480 arm_cpu_do_interrupt_aarch32(cs);
6481 }
Peter Maydellf3a9b692016-01-21 14:15:08 +00006482
6483 if (!kvm_enabled()) {
6484 cs->interrupt_request |= CPU_INTERRUPT_EXITTB;
6485 }
6486}
Peter Maydell0480f692015-02-05 13:37:24 +00006487
6488/* Return the exception level which controls this address translation regime */
6489static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx)
6490{
6491 switch (mmu_idx) {
6492 case ARMMMUIdx_S2NS:
6493 case ARMMMUIdx_S1E2:
6494 return 2;
6495 case ARMMMUIdx_S1E3:
6496 return 3;
6497 case ARMMMUIdx_S1SE0:
6498 return arm_el_is_aa64(env, 3) ? 1 : 3;
6499 case ARMMMUIdx_S1SE1:
6500 case ARMMMUIdx_S1NSE0:
6501 case ARMMMUIdx_S1NSE1:
6502 return 1;
6503 default:
6504 g_assert_not_reached();
6505 }
6506}
6507
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01006508/* Return true if this address translation regime is secure */
6509static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx)
6510{
6511 switch (mmu_idx) {
6512 case ARMMMUIdx_S12NSE0:
6513 case ARMMMUIdx_S12NSE1:
6514 case ARMMMUIdx_S1NSE0:
6515 case ARMMMUIdx_S1NSE1:
6516 case ARMMMUIdx_S1E2:
6517 case ARMMMUIdx_S2NS:
6518 return false;
6519 case ARMMMUIdx_S1E3:
6520 case ARMMMUIdx_S1SE0:
6521 case ARMMMUIdx_S1SE1:
6522 return true;
6523 default:
6524 g_assert_not_reached();
6525 }
6526}
6527
Peter Maydell0480f692015-02-05 13:37:24 +00006528/* Return the SCTLR value which controls this address translation regime */
6529static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx)
6530{
6531 return env->cp15.sctlr_el[regime_el(env, mmu_idx)];
6532}
6533
6534/* Return true if the specified stage of address translation is disabled */
6535static inline bool regime_translation_disabled(CPUARMState *env,
6536 ARMMMUIdx mmu_idx)
6537{
6538 if (mmu_idx == ARMMMUIdx_S2NS) {
6539 return (env->cp15.hcr_el2 & HCR_VM) == 0;
6540 }
6541 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0;
6542}
6543
Peter Crosthwaite73462dd2016-03-04 11:30:20 +00006544static inline bool regime_translation_big_endian(CPUARMState *env,
6545 ARMMMUIdx mmu_idx)
6546{
6547 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0;
6548}
6549
Peter Maydell0480f692015-02-05 13:37:24 +00006550/* Return the TCR controlling this translation regime */
6551static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx)
6552{
6553 if (mmu_idx == ARMMMUIdx_S2NS) {
Edgar E. Iglesias68e9c2f2015-09-14 14:39:50 +01006554 return &env->cp15.vtcr_el2;
Peter Maydell0480f692015-02-05 13:37:24 +00006555 }
6556 return &env->cp15.tcr_el[regime_el(env, mmu_idx)];
6557}
6558
Greg Bellowsaef878b2015-04-22 12:09:18 -05006559/* Return the TTBR associated with this translation regime */
6560static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx,
6561 int ttbrn)
6562{
6563 if (mmu_idx == ARMMMUIdx_S2NS) {
Edgar E. Iglesiasb698e9c2015-09-14 14:39:50 +01006564 return env->cp15.vttbr_el2;
Greg Bellowsaef878b2015-04-22 12:09:18 -05006565 }
6566 if (ttbrn == 0) {
6567 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)];
6568 } else {
6569 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)];
6570 }
6571}
6572
Peter Maydell0480f692015-02-05 13:37:24 +00006573/* Return true if the translation regime is using LPAE format page tables */
6574static inline bool regime_using_lpae_format(CPUARMState *env,
6575 ARMMMUIdx mmu_idx)
6576{
6577 int el = regime_el(env, mmu_idx);
6578 if (el == 2 || arm_el_is_aa64(env, el)) {
6579 return true;
6580 }
6581 if (arm_feature(env, ARM_FEATURE_LPAE)
6582 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) {
6583 return true;
6584 }
6585 return false;
6586}
6587
Alvise Rigodeb2db92016-01-15 11:37:42 +01006588/* Returns true if the stage 1 translation regime is using LPAE format page
6589 * tables. Used when raising alignment exceptions, whose FSR changes depending
6590 * on whether the long or short descriptor format is in use. */
6591bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx)
Andrew Baumann30901472015-12-17 13:37:13 +00006592{
Alvise Rigodeb2db92016-01-15 11:37:42 +01006593 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
6594 mmu_idx += ARMMMUIdx_S1NSE0;
6595 }
6596
Andrew Baumann30901472015-12-17 13:37:13 +00006597 return regime_using_lpae_format(env, mmu_idx);
6598}
6599
Peter Maydell0480f692015-02-05 13:37:24 +00006600static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx)
6601{
6602 switch (mmu_idx) {
6603 case ARMMMUIdx_S1SE0:
6604 case ARMMMUIdx_S1NSE0:
6605 return true;
6606 default:
6607 return false;
6608 case ARMMMUIdx_S12NSE0:
6609 case ARMMMUIdx_S12NSE1:
6610 g_assert_not_reached();
6611 }
6612}
6613
Andrew Jones0fbf5232015-03-16 12:30:46 +00006614/* Translate section/page access permissions to page
6615 * R/W protection flags
Andrew Jonesd76951b2015-03-16 12:30:46 +00006616 *
6617 * @env: CPUARMState
6618 * @mmu_idx: MMU index indicating required translation regime
6619 * @ap: The 3-bit access permissions (AP[2:0])
6620 * @domain_prot: The 2-bit domain access permissions
Andrew Jones0fbf5232015-03-16 12:30:46 +00006621 */
6622static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx,
6623 int ap, int domain_prot)
bellardb5ff1b32005-11-26 10:38:39 +00006624{
Peter Maydell554b0b02015-02-05 13:37:24 +00006625 bool is_user = regime_is_user(env, mmu_idx);
pbrook9ee6e8b2007-11-11 00:04:49 +00006626
Peter Maydell554b0b02015-02-05 13:37:24 +00006627 if (domain_prot == 3) {
6628 return PAGE_READ | PAGE_WRITE;
6629 }
bellardb5ff1b32005-11-26 10:38:39 +00006630
Peter Maydell554b0b02015-02-05 13:37:24 +00006631 switch (ap) {
6632 case 0:
6633 if (arm_feature(env, ARM_FEATURE_V7)) {
6634 return 0;
6635 }
Peter Maydell554b0b02015-02-05 13:37:24 +00006636 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) {
6637 case SCTLR_S:
6638 return is_user ? 0 : PAGE_READ;
6639 case SCTLR_R:
6640 return PAGE_READ;
6641 default:
6642 return 0;
6643 }
6644 case 1:
6645 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6646 case 2:
Peter Maydell87c3d482015-02-05 13:37:24 +00006647 if (is_user) {
Andrew Jones0fbf5232015-03-16 12:30:46 +00006648 return PAGE_READ;
Peter Maydell87c3d482015-02-05 13:37:24 +00006649 } else {
Peter Maydell554b0b02015-02-05 13:37:24 +00006650 return PAGE_READ | PAGE_WRITE;
Peter Maydell87c3d482015-02-05 13:37:24 +00006651 }
Peter Maydell554b0b02015-02-05 13:37:24 +00006652 case 3:
6653 return PAGE_READ | PAGE_WRITE;
6654 case 4: /* Reserved. */
6655 return 0;
6656 case 5:
Andrew Jones0fbf5232015-03-16 12:30:46 +00006657 return is_user ? 0 : PAGE_READ;
Peter Maydell554b0b02015-02-05 13:37:24 +00006658 case 6:
Andrew Jones0fbf5232015-03-16 12:30:46 +00006659 return PAGE_READ;
Peter Maydell554b0b02015-02-05 13:37:24 +00006660 case 7:
Peter Maydell87c3d482015-02-05 13:37:24 +00006661 if (!arm_feature(env, ARM_FEATURE_V6K)) {
Peter Maydell554b0b02015-02-05 13:37:24 +00006662 return 0;
Peter Maydell87c3d482015-02-05 13:37:24 +00006663 }
Andrew Jones0fbf5232015-03-16 12:30:46 +00006664 return PAGE_READ;
Peter Maydell554b0b02015-02-05 13:37:24 +00006665 default:
Andrew Jones0fbf5232015-03-16 12:30:46 +00006666 g_assert_not_reached();
Peter Maydell554b0b02015-02-05 13:37:24 +00006667 }
bellardb5ff1b32005-11-26 10:38:39 +00006668}
6669
Andrew Jonesd76951b2015-03-16 12:30:46 +00006670/* Translate section/page access permissions to page
6671 * R/W protection flags.
6672 *
Andrew Jonesd76951b2015-03-16 12:30:46 +00006673 * @ap: The 2-bit simple AP (AP[2:1])
Andrew Jonesd8e052b2015-03-16 12:30:46 +00006674 * @is_user: TRUE if accessing from PL0
Andrew Jonesd76951b2015-03-16 12:30:46 +00006675 */
Andrew Jonesd8e052b2015-03-16 12:30:46 +00006676static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user)
Andrew Jonesd76951b2015-03-16 12:30:46 +00006677{
Andrew Jonesd76951b2015-03-16 12:30:46 +00006678 switch (ap) {
6679 case 0:
6680 return is_user ? 0 : PAGE_READ | PAGE_WRITE;
6681 case 1:
6682 return PAGE_READ | PAGE_WRITE;
6683 case 2:
6684 return is_user ? 0 : PAGE_READ;
6685 case 3:
6686 return PAGE_READ;
6687 default:
6688 g_assert_not_reached();
6689 }
6690}
6691
Andrew Jonesd8e052b2015-03-16 12:30:46 +00006692static inline int
6693simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap)
6694{
6695 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx));
6696}
6697
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +01006698/* Translate S2 section/page access permissions to protection flags
6699 *
6700 * @env: CPUARMState
6701 * @s2ap: The 2-bit stage2 access permissions (S2AP)
6702 * @xn: XN (execute-never) bit
6703 */
6704static int get_S2prot(CPUARMState *env, int s2ap, int xn)
6705{
6706 int prot = 0;
6707
6708 if (s2ap & 1) {
6709 prot |= PAGE_READ;
6710 }
6711 if (s2ap & 2) {
6712 prot |= PAGE_WRITE;
6713 }
6714 if (!xn) {
Sergey Sorokindfda6832016-05-12 13:22:26 +01006715 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) {
6716 prot |= PAGE_EXEC;
6717 }
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +01006718 }
6719 return prot;
6720}
6721
Andrew Jonesd8e052b2015-03-16 12:30:46 +00006722/* Translate section/page access permissions to protection flags
6723 *
6724 * @env: CPUARMState
6725 * @mmu_idx: MMU index indicating required translation regime
6726 * @is_aa64: TRUE if AArch64
6727 * @ap: The 2-bit simple AP (AP[2:1])
6728 * @ns: NS (non-secure) bit
6729 * @xn: XN (execute-never) bit
6730 * @pxn: PXN (privileged execute-never) bit
6731 */
6732static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64,
6733 int ap, int ns, int xn, int pxn)
6734{
6735 bool is_user = regime_is_user(env, mmu_idx);
6736 int prot_rw, user_rw;
6737 bool have_wxn;
6738 int wxn = 0;
6739
6740 assert(mmu_idx != ARMMMUIdx_S2NS);
6741
6742 user_rw = simple_ap_to_rw_prot_is_user(ap, true);
6743 if (is_user) {
6744 prot_rw = user_rw;
6745 } else {
6746 prot_rw = simple_ap_to_rw_prot_is_user(ap, false);
6747 }
6748
6749 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) {
6750 return prot_rw;
6751 }
6752
6753 /* TODO have_wxn should be replaced with
6754 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2)
6755 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE
6756 * compatible processors have EL2, which is required for [U]WXN.
6757 */
6758 have_wxn = arm_feature(env, ARM_FEATURE_LPAE);
6759
6760 if (have_wxn) {
6761 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN;
6762 }
6763
6764 if (is_aa64) {
6765 switch (regime_el(env, mmu_idx)) {
6766 case 1:
6767 if (!is_user) {
6768 xn = pxn || (user_rw & PAGE_WRITE);
6769 }
6770 break;
6771 case 2:
6772 case 3:
6773 break;
6774 }
6775 } else if (arm_feature(env, ARM_FEATURE_V7)) {
6776 switch (regime_el(env, mmu_idx)) {
6777 case 1:
6778 case 3:
6779 if (is_user) {
6780 xn = xn || !(user_rw & PAGE_READ);
6781 } else {
6782 int uwxn = 0;
6783 if (have_wxn) {
6784 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN;
6785 }
6786 xn = xn || !(prot_rw & PAGE_READ) || pxn ||
6787 (uwxn && (user_rw & PAGE_WRITE));
6788 }
6789 break;
6790 case 2:
6791 break;
6792 }
6793 } else {
6794 xn = wxn = 0;
6795 }
6796
6797 if (xn || (wxn && (prot_rw & PAGE_WRITE))) {
6798 return prot_rw;
6799 }
6800 return prot_rw | PAGE_EXEC;
6801}
6802
Peter Maydell0480f692015-02-05 13:37:24 +00006803static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx,
6804 uint32_t *table, uint32_t address)
pbrookb2fa1792008-10-22 19:22:30 +00006805{
Peter Maydell0480f692015-02-05 13:37:24 +00006806 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */
Peter Maydell0480f692015-02-05 13:37:24 +00006807 TCR *tcr = regime_tcr(env, mmu_idx);
Fabian Aggeler11f136e2014-12-11 12:07:51 +00006808
Fabian Aggeler11f136e2014-12-11 12:07:51 +00006809 if (address & tcr->mask) {
6810 if (tcr->raw_tcr & TTBCR_PD1) {
Fabian Aggelere389be12014-06-19 18:06:24 +01006811 /* Translation table walk disabled for TTBR1 */
6812 return false;
6813 }
Greg Bellowsaef878b2015-04-22 12:09:18 -05006814 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000;
Fabian Aggelere389be12014-06-19 18:06:24 +01006815 } else {
Fabian Aggeler11f136e2014-12-11 12:07:51 +00006816 if (tcr->raw_tcr & TTBCR_PD0) {
Fabian Aggelere389be12014-06-19 18:06:24 +01006817 /* Translation table walk disabled for TTBR0 */
6818 return false;
6819 }
Greg Bellowsaef878b2015-04-22 12:09:18 -05006820 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask;
Fabian Aggelere389be12014-06-19 18:06:24 +01006821 }
6822 *table |= (address >> 18) & 0x3ffc;
6823 return true;
pbrookb2fa1792008-10-22 19:22:30 +00006824}
6825
Edgar E. Iglesias37785972015-10-26 14:02:04 +01006826/* Translate a S1 pagetable walk through S2 if needed. */
6827static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx,
6828 hwaddr addr, MemTxAttrs txattrs,
6829 uint32_t *fsr,
6830 ARMMMUFaultInfo *fi)
6831{
6832 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) &&
6833 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
6834 target_ulong s2size;
6835 hwaddr s2pa;
6836 int s2prot;
6837 int ret;
6838
6839 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa,
6840 &txattrs, &s2prot, &s2size, fsr, fi);
6841 if (ret) {
6842 fi->s2addr = addr;
6843 fi->stage2 = true;
6844 fi->s1ptw = true;
6845 return ~0;
6846 }
6847 addr = s2pa;
6848 }
6849 return addr;
6850}
6851
Peter Maydellebca90e2015-04-26 16:49:25 +01006852/* All loads done in the course of a page table walk go through here.
6853 * TODO: rather than ignoring errors from physical memory reads (which
6854 * are external aborts in ARM terminology) we should propagate this
6855 * error out so that we can turn it into a Data Abort if this walk
6856 * was being done for a CPU load/store or an address translation instruction
6857 * (but not if it was for a debug access).
6858 */
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +01006859static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6860 ARMMMUIdx mmu_idx, uint32_t *fsr,
6861 ARMMMUFaultInfo *fi)
Peter Maydellebca90e2015-04-26 16:49:25 +01006862{
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +01006863 ARMCPU *cpu = ARM_CPU(cs);
6864 CPUARMState *env = &cpu->env;
Peter Maydellebca90e2015-04-26 16:49:25 +01006865 MemTxAttrs attrs = {};
Peter Maydell5ce4ff62016-01-21 14:15:07 +00006866 AddressSpace *as;
Peter Maydellebca90e2015-04-26 16:49:25 +01006867
6868 attrs.secure = is_secure;
Peter Maydell5ce4ff62016-01-21 14:15:07 +00006869 as = arm_addressspace(cs, attrs);
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +01006870 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6871 if (fi->s1ptw) {
6872 return 0;
6873 }
Peter Crosthwaite73462dd2016-03-04 11:30:20 +00006874 if (regime_translation_big_endian(env, mmu_idx)) {
6875 return address_space_ldl_be(as, addr, attrs, NULL);
6876 } else {
6877 return address_space_ldl_le(as, addr, attrs, NULL);
6878 }
Peter Maydellebca90e2015-04-26 16:49:25 +01006879}
6880
Edgar E. Iglesias37785972015-10-26 14:02:04 +01006881static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure,
6882 ARMMMUIdx mmu_idx, uint32_t *fsr,
6883 ARMMMUFaultInfo *fi)
Peter Maydellebca90e2015-04-26 16:49:25 +01006884{
Edgar E. Iglesias37785972015-10-26 14:02:04 +01006885 ARMCPU *cpu = ARM_CPU(cs);
6886 CPUARMState *env = &cpu->env;
Peter Maydellebca90e2015-04-26 16:49:25 +01006887 MemTxAttrs attrs = {};
Peter Maydell5ce4ff62016-01-21 14:15:07 +00006888 AddressSpace *as;
Peter Maydellebca90e2015-04-26 16:49:25 +01006889
6890 attrs.secure = is_secure;
Peter Maydell5ce4ff62016-01-21 14:15:07 +00006891 as = arm_addressspace(cs, attrs);
Edgar E. Iglesias37785972015-10-26 14:02:04 +01006892 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi);
6893 if (fi->s1ptw) {
6894 return 0;
6895 }
Peter Crosthwaite73462dd2016-03-04 11:30:20 +00006896 if (regime_translation_big_endian(env, mmu_idx)) {
6897 return address_space_ldq_be(as, addr, attrs, NULL);
6898 } else {
6899 return address_space_ldq_le(as, addr, attrs, NULL);
6900 }
Peter Maydellebca90e2015-04-26 16:49:25 +01006901}
6902
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01006903static bool get_phys_addr_v5(CPUARMState *env, uint32_t address,
6904 int access_type, ARMMMUIdx mmu_idx,
6905 hwaddr *phys_ptr, int *prot,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01006906 target_ulong *page_size, uint32_t *fsr,
6907 ARMMMUFaultInfo *fi)
bellardb5ff1b32005-11-26 10:38:39 +00006908{
Andreas Färber70d74662014-03-09 19:10:29 +01006909 CPUState *cs = CPU(arm_env_get_cpu(env));
bellardb5ff1b32005-11-26 10:38:39 +00006910 int code;
6911 uint32_t table;
6912 uint32_t desc;
6913 int type;
6914 int ap;
Fabian Aggelere389be12014-06-19 18:06:24 +01006915 int domain = 0;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00006916 int domain_prot;
Avi Kivitya8170e52012-10-23 12:30:10 +02006917 hwaddr phys_addr;
Peter Maydell0480f692015-02-05 13:37:24 +00006918 uint32_t dacr;
bellardb5ff1b32005-11-26 10:38:39 +00006919
pbrook9ee6e8b2007-11-11 00:04:49 +00006920 /* Pagetable walk. */
6921 /* Lookup l1 descriptor. */
Peter Maydell0480f692015-02-05 13:37:24 +00006922 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
Fabian Aggelere389be12014-06-19 18:06:24 +01006923 /* Section translation fault if page walk is disabled by PD0 or PD1 */
6924 code = 5;
6925 goto do_fault;
6926 }
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +01006927 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6928 mmu_idx, fsr, fi);
pbrook9ee6e8b2007-11-11 00:04:49 +00006929 type = (desc & 3);
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00006930 domain = (desc >> 5) & 0x0f;
Peter Maydell0480f692015-02-05 13:37:24 +00006931 if (regime_el(env, mmu_idx) == 1) {
6932 dacr = env->cp15.dacr_ns;
6933 } else {
6934 dacr = env->cp15.dacr_s;
6935 }
6936 domain_prot = (dacr >> (domain * 2)) & 3;
pbrook9ee6e8b2007-11-11 00:04:49 +00006937 if (type == 0) {
balrog601d70b2008-04-20 01:03:45 +00006938 /* Section translation fault. */
pbrook9ee6e8b2007-11-11 00:04:49 +00006939 code = 5;
6940 goto do_fault;
6941 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00006942 if (domain_prot == 0 || domain_prot == 2) {
pbrook9ee6e8b2007-11-11 00:04:49 +00006943 if (type == 2)
6944 code = 9; /* Section domain fault. */
6945 else
6946 code = 11; /* Page domain fault. */
6947 goto do_fault;
6948 }
6949 if (type == 2) {
6950 /* 1Mb section. */
6951 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
6952 ap = (desc >> 10) & 3;
6953 code = 13;
Paul Brookd4c430a2010-03-17 02:14:28 +00006954 *page_size = 1024 * 1024;
pbrook9ee6e8b2007-11-11 00:04:49 +00006955 } else {
6956 /* Lookup l2 entry. */
Peter Maydell554b0b02015-02-05 13:37:24 +00006957 if (type == 1) {
6958 /* Coarse pagetable. */
6959 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
6960 } else {
6961 /* Fine pagetable. */
6962 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc);
6963 }
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +01006964 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
6965 mmu_idx, fsr, fi);
pbrook9ee6e8b2007-11-11 00:04:49 +00006966 switch (desc & 3) {
6967 case 0: /* Page translation fault. */
6968 code = 7;
6969 goto do_fault;
6970 case 1: /* 64k page. */
6971 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
6972 ap = (desc >> (4 + ((address >> 13) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00006973 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00006974 break;
6975 case 2: /* 4k page. */
6976 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
Peter Maydellc10f7fc2014-02-26 17:19:59 +00006977 ap = (desc >> (4 + ((address >> 9) & 6))) & 3;
Paul Brookd4c430a2010-03-17 02:14:28 +00006978 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00006979 break;
Peter Maydellfc1891c2015-06-15 18:06:07 +01006980 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */
Peter Maydell554b0b02015-02-05 13:37:24 +00006981 if (type == 1) {
Peter Maydellfc1891c2015-06-15 18:06:07 +01006982 /* ARMv6/XScale extended small page format */
6983 if (arm_feature(env, ARM_FEATURE_XSCALE)
6984 || arm_feature(env, ARM_FEATURE_V6)) {
Peter Maydell554b0b02015-02-05 13:37:24 +00006985 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
Peter Maydellfc1891c2015-06-15 18:06:07 +01006986 *page_size = 0x1000;
Peter Maydell554b0b02015-02-05 13:37:24 +00006987 } else {
Peter Maydellfc1891c2015-06-15 18:06:07 +01006988 /* UNPREDICTABLE in ARMv5; we choose to take a
6989 * page translation fault.
6990 */
Peter Maydell554b0b02015-02-05 13:37:24 +00006991 code = 7;
6992 goto do_fault;
6993 }
6994 } else {
6995 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff);
Peter Maydellfc1891c2015-06-15 18:06:07 +01006996 *page_size = 0x400;
Peter Maydell554b0b02015-02-05 13:37:24 +00006997 }
pbrook9ee6e8b2007-11-11 00:04:49 +00006998 ap = (desc >> 4) & 3;
6999 break;
7000 default:
7001 /* Never happens, but compiler isn't smart enough to tell. */
7002 abort();
7003 }
7004 code = 15;
7005 }
Andrew Jones0fbf5232015-03-16 12:30:46 +00007006 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
7007 *prot |= *prot ? PAGE_EXEC : 0;
7008 if (!(*prot & (1 << access_type))) {
pbrook9ee6e8b2007-11-11 00:04:49 +00007009 /* Access permission fault. */
7010 goto do_fault;
7011 }
7012 *phys_ptr = phys_addr;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007013 return false;
pbrook9ee6e8b2007-11-11 00:04:49 +00007014do_fault:
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007015 *fsr = code | (domain << 4);
7016 return true;
pbrook9ee6e8b2007-11-11 00:04:49 +00007017}
7018
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007019static bool get_phys_addr_v6(CPUARMState *env, uint32_t address,
7020 int access_type, ARMMMUIdx mmu_idx,
7021 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007022 target_ulong *page_size, uint32_t *fsr,
7023 ARMMMUFaultInfo *fi)
pbrook9ee6e8b2007-11-11 00:04:49 +00007024{
Andreas Färber70d74662014-03-09 19:10:29 +01007025 CPUState *cs = CPU(arm_env_get_cpu(env));
pbrook9ee6e8b2007-11-11 00:04:49 +00007026 int code;
7027 uint32_t table;
7028 uint32_t desc;
7029 uint32_t xn;
Peter Maydellde9b05b2012-07-12 10:59:05 +00007030 uint32_t pxn = 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00007031 int type;
7032 int ap;
Peter Maydellde9b05b2012-07-12 10:59:05 +00007033 int domain = 0;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00007034 int domain_prot;
Avi Kivitya8170e52012-10-23 12:30:10 +02007035 hwaddr phys_addr;
Peter Maydell0480f692015-02-05 13:37:24 +00007036 uint32_t dacr;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007037 bool ns;
pbrook9ee6e8b2007-11-11 00:04:49 +00007038
7039 /* Pagetable walk. */
7040 /* Lookup l1 descriptor. */
Peter Maydell0480f692015-02-05 13:37:24 +00007041 if (!get_level1_table_address(env, mmu_idx, &table, address)) {
Fabian Aggelere389be12014-06-19 18:06:24 +01007042 /* Section translation fault if page walk is disabled by PD0 or PD1 */
7043 code = 5;
7044 goto do_fault;
7045 }
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +01007046 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
7047 mmu_idx, fsr, fi);
pbrook9ee6e8b2007-11-11 00:04:49 +00007048 type = (desc & 3);
Peter Maydellde9b05b2012-07-12 10:59:05 +00007049 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) {
7050 /* Section translation fault, or attempt to use the encoding
7051 * which is Reserved on implementations without PXN.
7052 */
pbrook9ee6e8b2007-11-11 00:04:49 +00007053 code = 5;
pbrook9ee6e8b2007-11-11 00:04:49 +00007054 goto do_fault;
Peter Maydellde9b05b2012-07-12 10:59:05 +00007055 }
7056 if ((type == 1) || !(desc & (1 << 18))) {
7057 /* Page or Section. */
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00007058 domain = (desc >> 5) & 0x0f;
pbrook9ee6e8b2007-11-11 00:04:49 +00007059 }
Peter Maydell0480f692015-02-05 13:37:24 +00007060 if (regime_el(env, mmu_idx) == 1) {
7061 dacr = env->cp15.dacr_ns;
7062 } else {
7063 dacr = env->cp15.dacr_s;
7064 }
7065 domain_prot = (dacr >> (domain * 2)) & 3;
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00007066 if (domain_prot == 0 || domain_prot == 2) {
Peter Maydellde9b05b2012-07-12 10:59:05 +00007067 if (type != 1) {
pbrook9ee6e8b2007-11-11 00:04:49 +00007068 code = 9; /* Section domain fault. */
Peter Maydellde9b05b2012-07-12 10:59:05 +00007069 } else {
pbrook9ee6e8b2007-11-11 00:04:49 +00007070 code = 11; /* Page domain fault. */
Peter Maydellde9b05b2012-07-12 10:59:05 +00007071 }
pbrook9ee6e8b2007-11-11 00:04:49 +00007072 goto do_fault;
7073 }
Peter Maydellde9b05b2012-07-12 10:59:05 +00007074 if (type != 1) {
pbrook9ee6e8b2007-11-11 00:04:49 +00007075 if (desc & (1 << 18)) {
7076 /* Supersection. */
7077 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff);
Sergey Fedorov4e42a6c2015-06-15 18:06:07 +01007078 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32;
7079 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36;
Paul Brookd4c430a2010-03-17 02:14:28 +00007080 *page_size = 0x1000000;
pbrook9ee6e8b2007-11-11 00:04:49 +00007081 } else {
7082 /* Section. */
7083 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff);
Paul Brookd4c430a2010-03-17 02:14:28 +00007084 *page_size = 0x100000;
pbrook9ee6e8b2007-11-11 00:04:49 +00007085 }
7086 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4);
7087 xn = desc & (1 << 4);
Peter Maydellde9b05b2012-07-12 10:59:05 +00007088 pxn = desc & 1;
pbrook9ee6e8b2007-11-11 00:04:49 +00007089 code = 13;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007090 ns = extract32(desc, 19, 1);
pbrook9ee6e8b2007-11-11 00:04:49 +00007091 } else {
Peter Maydellde9b05b2012-07-12 10:59:05 +00007092 if (arm_feature(env, ARM_FEATURE_PXN)) {
7093 pxn = (desc >> 2) & 1;
7094 }
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007095 ns = extract32(desc, 3, 1);
pbrook9ee6e8b2007-11-11 00:04:49 +00007096 /* Lookup l2 entry. */
7097 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc);
Edgar E. Iglesiasa614e692015-10-26 14:02:05 +01007098 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx),
7099 mmu_idx, fsr, fi);
pbrook9ee6e8b2007-11-11 00:04:49 +00007100 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4);
7101 switch (desc & 3) {
7102 case 0: /* Page translation fault. */
7103 code = 7;
7104 goto do_fault;
7105 case 1: /* 64k page. */
7106 phys_addr = (desc & 0xffff0000) | (address & 0xffff);
7107 xn = desc & (1 << 15);
Paul Brookd4c430a2010-03-17 02:14:28 +00007108 *page_size = 0x10000;
pbrook9ee6e8b2007-11-11 00:04:49 +00007109 break;
7110 case 2: case 3: /* 4k page. */
7111 phys_addr = (desc & 0xfffff000) | (address & 0xfff);
7112 xn = desc & 1;
Paul Brookd4c430a2010-03-17 02:14:28 +00007113 *page_size = 0x1000;
pbrook9ee6e8b2007-11-11 00:04:49 +00007114 break;
7115 default:
7116 /* Never happens, but compiler isn't smart enough to tell. */
7117 abort();
7118 }
7119 code = 15;
7120 }
Jean-Christophe DUBOISdd4ebc22011-12-13 18:19:23 +00007121 if (domain_prot == 3) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02007122 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
7123 } else {
Peter Maydell0480f692015-02-05 13:37:24 +00007124 if (pxn && !regime_is_user(env, mmu_idx)) {
Peter Maydellde9b05b2012-07-12 10:59:05 +00007125 xn = 1;
7126 }
Juha Riihimäkic0034322010-12-08 13:15:16 +02007127 if (xn && access_type == 2)
7128 goto do_fault;
pbrook9ee6e8b2007-11-11 00:04:49 +00007129
Andrew Jonesd76951b2015-03-16 12:30:46 +00007130 if (arm_feature(env, ARM_FEATURE_V6K) &&
7131 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) {
7132 /* The simplified model uses AP[0] as an access control bit. */
7133 if ((ap & 1) == 0) {
7134 /* Access flag fault. */
7135 code = (code == 15) ? 6 : 3;
7136 goto do_fault;
7137 }
7138 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1);
7139 } else {
7140 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot);
Juha Riihimäkic0034322010-12-08 13:15:16 +02007141 }
Andrew Jones0fbf5232015-03-16 12:30:46 +00007142 if (*prot && !xn) {
7143 *prot |= PAGE_EXEC;
7144 }
7145 if (!(*prot & (1 << access_type))) {
Juha Riihimäkic0034322010-12-08 13:15:16 +02007146 /* Access permission fault. */
7147 goto do_fault;
7148 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05307149 }
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007150 if (ns) {
7151 /* The NS bit will (as required by the architecture) have no effect if
7152 * the CPU doesn't support TZ or this is a non-secure translation
7153 * regime, because the attribute will already be non-secure.
7154 */
7155 attrs->secure = false;
7156 }
pbrook9ee6e8b2007-11-11 00:04:49 +00007157 *phys_ptr = phys_addr;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007158 return false;
pbrook9ee6e8b2007-11-11 00:04:49 +00007159do_fault:
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007160 *fsr = code | (domain << 4);
7161 return true;
pbrook9ee6e8b2007-11-11 00:04:49 +00007162}
7163
Peter Maydell3dde9622012-07-12 10:59:12 +00007164/* Fault type for long-descriptor MMU fault reporting; this corresponds
7165 * to bits [5..2] in the STATUS field in long-format DFSR/IFSR.
7166 */
7167typedef enum {
7168 translation_fault = 1,
7169 access_fault = 2,
7170 permission_fault = 3,
7171} MMUFaultType;
7172
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007173/*
Edgar E. Iglesiasa0e966c2016-02-03 13:46:33 +00007174 * check_s2_mmu_setup
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007175 * @cpu: ARMCPU
7176 * @is_aa64: True if the translation regime is in AArch64 state
7177 * @startlevel: Suggested starting level
7178 * @inputsize: Bitsize of IPAs
7179 * @stride: Page-table stride (See the ARM ARM)
7180 *
Edgar E. Iglesiasa0e966c2016-02-03 13:46:33 +00007181 * Returns true if the suggested S2 translation parameters are OK and
7182 * false otherwise.
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007183 */
Edgar E. Iglesiasa0e966c2016-02-03 13:46:33 +00007184static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level,
7185 int inputsize, int stride)
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007186{
Edgar E. Iglesias98d68ec2016-02-03 13:46:33 +00007187 const int grainsize = stride + 3;
7188 int startsizecheck;
7189
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007190 /* Negative levels are never allowed. */
7191 if (level < 0) {
7192 return false;
7193 }
7194
Edgar E. Iglesias98d68ec2016-02-03 13:46:33 +00007195 startsizecheck = inputsize - ((3 - level) * stride + grainsize);
7196 if (startsizecheck < 1 || startsizecheck > stride + 4) {
7197 return false;
7198 }
7199
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007200 if (is_aa64) {
Edgar E. Iglesias35264232016-02-03 13:46:33 +00007201 CPUARMState *env = &cpu->env;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007202 unsigned int pamax = arm_pamax(cpu);
7203
7204 switch (stride) {
7205 case 13: /* 64KB Pages. */
7206 if (level == 0 || (level == 1 && pamax <= 42)) {
7207 return false;
7208 }
7209 break;
7210 case 11: /* 16KB Pages. */
7211 if (level == 0 || (level == 1 && pamax <= 40)) {
7212 return false;
7213 }
7214 break;
7215 case 9: /* 4KB Pages. */
7216 if (level == 0 && pamax <= 42) {
7217 return false;
7218 }
7219 break;
7220 default:
7221 g_assert_not_reached();
7222 }
Edgar E. Iglesias35264232016-02-03 13:46:33 +00007223
7224 /* Inputsize checks. */
7225 if (inputsize > pamax &&
7226 (arm_el_is_aa64(env, 1) || inputsize > 40)) {
7227 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */
7228 return false;
7229 }
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007230 } else {
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007231 /* AArch32 only supports 4KB pages. Assert on that. */
7232 assert(stride == 9);
7233
7234 if (level == 0) {
7235 return false;
7236 }
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007237 }
7238 return true;
7239}
7240
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007241static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address,
7242 int access_type, ARMMMUIdx mmu_idx,
7243 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007244 target_ulong *page_size_ptr, uint32_t *fsr,
7245 ARMMMUFaultInfo *fi)
Peter Maydell3dde9622012-07-12 10:59:12 +00007246{
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007247 ARMCPU *cpu = arm_env_get_cpu(env);
7248 CPUState *cs = CPU(cpu);
Peter Maydell3dde9622012-07-12 10:59:12 +00007249 /* Read an LPAE long-descriptor translation table. */
7250 MMUFaultType fault_type = translation_fault;
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007251 uint32_t level;
Edgar E. Iglesias0c5fbf32015-09-14 14:39:50 +01007252 uint32_t epd = 0;
Edgar E. Iglesias1f4c8c12015-10-26 14:01:56 +01007253 int32_t t0sz, t1sz;
Rob Herring2c8dd312014-04-15 19:18:40 +01007254 uint32_t tg;
Peter Maydell3dde9622012-07-12 10:59:12 +00007255 uint64_t ttbr;
7256 int ttbr_select;
Sergey Sorokindddb5222016-05-12 13:22:26 +01007257 hwaddr descaddr, indexmask, indexmask_grainsize;
Peter Maydell3dde9622012-07-12 10:59:12 +00007258 uint32_t tableattrs;
7259 target_ulong page_size;
7260 uint32_t attrs;
Edgar E. Iglesias973a5432015-10-26 14:01:59 +01007261 int32_t stride = 9;
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007262 int32_t va_size;
Edgar E. Iglesias4ca6a052015-10-26 14:01:58 +01007263 int inputsize;
Rob Herring2c8dd312014-04-15 19:18:40 +01007264 int32_t tbi = 0;
Peter Maydell0480f692015-02-05 13:37:24 +00007265 TCR *tcr = regime_tcr(env, mmu_idx);
Andrew Jonesd8e052b2015-03-16 12:30:46 +00007266 int ap, ns, xn, pxn;
Greg Bellows88e8add2015-04-22 12:09:19 -05007267 uint32_t el = regime_el(env, mmu_idx);
7268 bool ttbr1_valid = true;
Peter Maydell61097692015-11-24 14:12:15 +00007269 uint64_t descaddrmask;
Rob Herring2c8dd312014-04-15 19:18:40 +01007270
Peter Maydell0480f692015-02-05 13:37:24 +00007271 /* TODO:
Greg Bellows88e8add2015-04-22 12:09:19 -05007272 * This code does not handle the different format TCR for VTCR_EL2.
7273 * This code also does not support shareability levels.
7274 * Attribute and permission bit handling should also be checked when adding
7275 * support for those page table walks.
Peter Maydell0480f692015-02-05 13:37:24 +00007276 */
Greg Bellows88e8add2015-04-22 12:09:19 -05007277 if (arm_el_is_aa64(env, el)) {
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007278 level = 0;
Rob Herring2c8dd312014-04-15 19:18:40 +01007279 va_size = 64;
Greg Bellows88e8add2015-04-22 12:09:19 -05007280 if (el > 1) {
Edgar E. Iglesias1edee472015-09-14 14:39:50 +01007281 if (mmu_idx != ARMMMUIdx_S2NS) {
7282 tbi = extract64(tcr->raw_tcr, 20, 1);
7283 }
Greg Bellows88e8add2015-04-22 12:09:19 -05007284 } else {
7285 if (extract64(address, 55, 1)) {
7286 tbi = extract64(tcr->raw_tcr, 38, 1);
7287 } else {
7288 tbi = extract64(tcr->raw_tcr, 37, 1);
7289 }
7290 }
Rob Herring2c8dd312014-04-15 19:18:40 +01007291 tbi *= 8;
Greg Bellows88e8add2015-04-22 12:09:19 -05007292
7293 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it
7294 * invalid.
7295 */
7296 if (el > 1) {
7297 ttbr1_valid = false;
7298 }
Peter Maydelld0a2cbc2015-08-25 15:45:08 +01007299 } else {
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007300 level = 1;
7301 va_size = 32;
Peter Maydelld0a2cbc2015-08-25 15:45:08 +01007302 /* There is no TTBR1 for EL2 */
7303 if (el == 2) {
7304 ttbr1_valid = false;
7305 }
Rob Herring2c8dd312014-04-15 19:18:40 +01007306 }
Peter Maydell3dde9622012-07-12 10:59:12 +00007307
7308 /* Determine whether this address is in the region controlled by
7309 * TTBR0 or TTBR1 (or if it is in neither region and should fault).
7310 * This is a Non-secure PL0/1 stage 1 translation, so controlled by
7311 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32:
7312 */
Peter Maydell0480f692015-02-05 13:37:24 +00007313 if (va_size == 64) {
Edgar E. Iglesias4ee38092015-10-26 14:01:57 +01007314 /* AArch64 translation. */
7315 t0sz = extract32(tcr->raw_tcr, 0, 6);
Rob Herring2c8dd312014-04-15 19:18:40 +01007316 t0sz = MIN(t0sz, 39);
7317 t0sz = MAX(t0sz, 16);
Edgar E. Iglesias4ee38092015-10-26 14:01:57 +01007318 } else if (mmu_idx != ARMMMUIdx_S2NS) {
7319 /* AArch32 stage 1 translation. */
7320 t0sz = extract32(tcr->raw_tcr, 0, 3);
7321 } else {
7322 /* AArch32 stage 2 translation. */
7323 bool sext = extract32(tcr->raw_tcr, 4, 1);
7324 bool sign = extract32(tcr->raw_tcr, 3, 1);
7325 t0sz = sextract32(tcr->raw_tcr, 0, 4);
7326
7327 /* If the sign-extend bit is not the same as t0sz[3], the result
7328 * is unpredictable. Flag this as a guest error. */
7329 if (sign != sext) {
7330 qemu_log_mask(LOG_GUEST_ERROR,
7331 "AArch32: VTCR.S / VTCR.T0SZ[3] missmatch\n");
7332 }
Rob Herring2c8dd312014-04-15 19:18:40 +01007333 }
Edgar E. Iglesias1f4c8c12015-10-26 14:01:56 +01007334 t1sz = extract32(tcr->raw_tcr, 16, 6);
Peter Maydell0480f692015-02-05 13:37:24 +00007335 if (va_size == 64) {
Rob Herring2c8dd312014-04-15 19:18:40 +01007336 t1sz = MIN(t1sz, 39);
7337 t1sz = MAX(t1sz, 16);
7338 }
7339 if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) {
Peter Maydell3dde9622012-07-12 10:59:12 +00007340 /* there is a ttbr0 region and we are in it (high bits all zero) */
7341 ttbr_select = 0;
Greg Bellows88e8add2015-04-22 12:09:19 -05007342 } else if (ttbr1_valid && t1sz &&
7343 !extract64(~address, va_size - t1sz, t1sz - tbi)) {
Peter Maydell3dde9622012-07-12 10:59:12 +00007344 /* there is a ttbr1 region and we are in it (high bits all one) */
7345 ttbr_select = 1;
7346 } else if (!t0sz) {
7347 /* ttbr0 region is "everything not in the ttbr1 region" */
7348 ttbr_select = 0;
Greg Bellows88e8add2015-04-22 12:09:19 -05007349 } else if (!t1sz && ttbr1_valid) {
Peter Maydell3dde9622012-07-12 10:59:12 +00007350 /* ttbr1 region is "everything not in the ttbr0 region" */
7351 ttbr_select = 1;
7352 } else {
7353 /* in the gap between the two regions, this is a Translation fault */
7354 fault_type = translation_fault;
7355 goto do_fault;
7356 }
7357
7358 /* Note that QEMU ignores shareability and cacheability attributes,
7359 * so we don't need to do anything with the SH, ORGN, IRGN fields
7360 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the
7361 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently
7362 * implement any ASID-like capability so we can ignore it (instead
7363 * we will always flush the TLB any time the ASID is changed).
7364 */
7365 if (ttbr_select == 0) {
Greg Bellowsaef878b2015-04-22 12:09:18 -05007366 ttbr = regime_ttbr(env, mmu_idx, 0);
Edgar E. Iglesias0c5fbf32015-09-14 14:39:50 +01007367 if (el < 2) {
7368 epd = extract32(tcr->raw_tcr, 7, 1);
7369 }
Edgar E. Iglesias4ca6a052015-10-26 14:01:58 +01007370 inputsize = va_size - t0sz;
Rob Herring2c8dd312014-04-15 19:18:40 +01007371
Fabian Aggeler11f136e2014-12-11 12:07:51 +00007372 tg = extract32(tcr->raw_tcr, 14, 2);
Rob Herring2c8dd312014-04-15 19:18:40 +01007373 if (tg == 1) { /* 64KB pages */
Edgar E. Iglesias973a5432015-10-26 14:01:59 +01007374 stride = 13;
Rob Herring2c8dd312014-04-15 19:18:40 +01007375 }
7376 if (tg == 2) { /* 16KB pages */
Edgar E. Iglesias973a5432015-10-26 14:01:59 +01007377 stride = 11;
Rob Herring2c8dd312014-04-15 19:18:40 +01007378 }
Peter Maydell3dde9622012-07-12 10:59:12 +00007379 } else {
Greg Bellows88e8add2015-04-22 12:09:19 -05007380 /* We should only be here if TTBR1 is valid */
7381 assert(ttbr1_valid);
7382
Greg Bellowsaef878b2015-04-22 12:09:18 -05007383 ttbr = regime_ttbr(env, mmu_idx, 1);
Fabian Aggeler11f136e2014-12-11 12:07:51 +00007384 epd = extract32(tcr->raw_tcr, 23, 1);
Edgar E. Iglesias4ca6a052015-10-26 14:01:58 +01007385 inputsize = va_size - t1sz;
Rob Herring2c8dd312014-04-15 19:18:40 +01007386
Fabian Aggeler11f136e2014-12-11 12:07:51 +00007387 tg = extract32(tcr->raw_tcr, 30, 2);
Rob Herring2c8dd312014-04-15 19:18:40 +01007388 if (tg == 3) { /* 64KB pages */
Edgar E. Iglesias973a5432015-10-26 14:01:59 +01007389 stride = 13;
Rob Herring2c8dd312014-04-15 19:18:40 +01007390 }
7391 if (tg == 1) { /* 16KB pages */
Edgar E. Iglesias973a5432015-10-26 14:01:59 +01007392 stride = 11;
Rob Herring2c8dd312014-04-15 19:18:40 +01007393 }
Peter Maydell3dde9622012-07-12 10:59:12 +00007394 }
7395
Peter Maydell0480f692015-02-05 13:37:24 +00007396 /* Here we should have set up all the parameters for the translation:
Edgar E. Iglesias973a5432015-10-26 14:01:59 +01007397 * va_size, inputsize, ttbr, epd, stride, tbi
Peter Maydell0480f692015-02-05 13:37:24 +00007398 */
7399
Peter Maydell3dde9622012-07-12 10:59:12 +00007400 if (epd) {
Greg Bellows88e8add2015-04-22 12:09:19 -05007401 /* Translation table walk disabled => Translation fault on TLB miss
7402 * Note: This is always 0 on 64-bit EL2 and EL3.
7403 */
Peter Maydell3dde9622012-07-12 10:59:12 +00007404 goto do_fault;
7405 }
7406
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007407 if (mmu_idx != ARMMMUIdx_S2NS) {
7408 /* The starting level depends on the virtual address size (which can
7409 * be up to 48 bits) and the translation granule size. It indicates
7410 * the number of strides (stride bits at a time) needed to
7411 * consume the bits of the input address. In the pseudocode this is:
7412 * level = 4 - RoundUp((inputsize - grainsize) / stride)
7413 * where their 'inputsize' is our 'inputsize', 'grainsize' is
7414 * our 'stride + 3' and 'stride' is our 'stride'.
7415 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying:
7416 * = 4 - (inputsize - stride - 3 + stride - 1) / stride
7417 * = 4 - (inputsize - 4) / stride;
7418 */
7419 level = 4 - (inputsize - 4) / stride;
7420 } else {
7421 /* For stage 2 translations the starting level is specified by the
7422 * VTCR_EL2.SL0 field (whose interpretation depends on the page size)
7423 */
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007424 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2);
7425 uint32_t startlevel;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007426 bool ok;
7427
7428 if (va_size == 32 || stride == 9) {
7429 /* AArch32 or 4KB pages */
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007430 startlevel = 2 - sl0;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007431 } else {
7432 /* 16KB or 64KB pages */
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007433 startlevel = 3 - sl0;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007434 }
7435
7436 /* Check that the starting level is valid. */
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007437 ok = check_s2_mmu_setup(cpu, va_size == 64, startlevel,
7438 inputsize, stride);
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007439 if (!ok) {
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007440 fault_type = translation_fault;
7441 goto do_fault;
7442 }
Sergey Sorokin1b4093e2016-03-16 17:05:58 +00007443 level = startlevel;
Edgar E. Iglesias1853d5a2015-10-26 14:02:00 +01007444 }
Peter Maydell3dde9622012-07-12 10:59:12 +00007445
Sergey Sorokindddb5222016-05-12 13:22:26 +01007446 indexmask_grainsize = (1ULL << (stride + 3)) - 1;
7447 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1;
Peter Maydell3dde9622012-07-12 10:59:12 +00007448
7449 /* Now we can extract the actual base address from the TTBR */
Rob Herring2c8dd312014-04-15 19:18:40 +01007450 descaddr = extract64(ttbr, 0, 48);
Sergey Sorokindddb5222016-05-12 13:22:26 +01007451 descaddr &= ~indexmask;
Peter Maydell3dde9622012-07-12 10:59:12 +00007452
Peter Maydell61097692015-11-24 14:12:15 +00007453 /* The address field in the descriptor goes up to bit 39 for ARMv7
Sergey Sorokindddb5222016-05-12 13:22:26 +01007454 * but up to bit 47 for ARMv8, but we use the descaddrmask
7455 * up to bit 39 for AArch32, because we don't need other bits in that case
7456 * to construct next descriptor address (anyway they should be all zeroes).
Peter Maydell61097692015-11-24 14:12:15 +00007457 */
Sergey Sorokindddb5222016-05-12 13:22:26 +01007458 descaddrmask = ((1ull << (va_size == 64 ? 48 : 40)) - 1) &
7459 ~indexmask_grainsize;
Peter Maydell61097692015-11-24 14:12:15 +00007460
Peter Maydellebca90e2015-04-26 16:49:25 +01007461 /* Secure accesses start with the page table in secure memory and
7462 * can be downgraded to non-secure at any step. Non-secure accesses
7463 * remain non-secure. We implement this by just ORing in the NSTable/NS
7464 * bits at each step.
7465 */
7466 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4);
Peter Maydell3dde9622012-07-12 10:59:12 +00007467 for (;;) {
7468 uint64_t descriptor;
Peter Maydellebca90e2015-04-26 16:49:25 +01007469 bool nstable;
Peter Maydell3dde9622012-07-12 10:59:12 +00007470
Sergey Sorokindddb5222016-05-12 13:22:26 +01007471 descaddr |= (address >> (stride * (4 - level))) & indexmask;
Rob Herring2c8dd312014-04-15 19:18:40 +01007472 descaddr &= ~7ULL;
Peter Maydellebca90e2015-04-26 16:49:25 +01007473 nstable = extract32(tableattrs, 4, 1);
Edgar E. Iglesias37785972015-10-26 14:02:04 +01007474 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fsr, fi);
7475 if (fi->s1ptw) {
7476 goto do_fault;
7477 }
7478
Peter Maydell3dde9622012-07-12 10:59:12 +00007479 if (!(descriptor & 1) ||
7480 (!(descriptor & 2) && (level == 3))) {
7481 /* Invalid, or the Reserved level 3 encoding */
7482 goto do_fault;
7483 }
Peter Maydell61097692015-11-24 14:12:15 +00007484 descaddr = descriptor & descaddrmask;
Peter Maydell3dde9622012-07-12 10:59:12 +00007485
7486 if ((descriptor & 2) && (level < 3)) {
7487 /* Table entry. The top five bits are attributes which may
7488 * propagate down through lower levels of the table (and
7489 * which are all arranged so that 0 means "no effect", so
7490 * we can gather them up by ORing in the bits at each level).
7491 */
7492 tableattrs |= extract64(descriptor, 59, 5);
7493 level++;
Sergey Sorokindddb5222016-05-12 13:22:26 +01007494 indexmask = indexmask_grainsize;
Peter Maydell3dde9622012-07-12 10:59:12 +00007495 continue;
7496 }
7497 /* Block entry at level 1 or 2, or page entry at level 3.
7498 * These are basically the same thing, although the number
7499 * of bits we pull in from the vaddr varies.
7500 */
Edgar E. Iglesias973a5432015-10-26 14:01:59 +01007501 page_size = (1ULL << ((stride * (4 - level)) + 3));
Peter Maydell3dde9622012-07-12 10:59:12 +00007502 descaddr |= (address & (page_size - 1));
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +01007503 /* Extract attributes from the descriptor */
Ian Campbelld615efa2014-06-09 15:43:23 +01007504 attrs = extract64(descriptor, 2, 10)
7505 | (extract64(descriptor, 52, 12) << 10);
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +01007506
7507 if (mmu_idx == ARMMMUIdx_S2NS) {
7508 /* Stage 2 table descriptors do not include any attribute fields */
7509 break;
7510 }
7511 /* Merge in attributes from table descriptors */
Peter Maydell3dde9622012-07-12 10:59:12 +00007512 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */
7513 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */
7514 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1
7515 * means "force PL1 access only", which means forcing AP[1] to 0.
7516 */
7517 if (extract32(tableattrs, 2, 1)) {
7518 attrs &= ~(1 << 4);
7519 }
Peter Maydellebca90e2015-04-26 16:49:25 +01007520 attrs |= nstable << 3; /* NS */
Peter Maydell3dde9622012-07-12 10:59:12 +00007521 break;
7522 }
7523 /* Here descaddr is the final physical address, and attributes
7524 * are all in attrs.
7525 */
7526 fault_type = access_fault;
7527 if ((attrs & (1 << 8)) == 0) {
7528 /* Access flag */
7529 goto do_fault;
7530 }
Andrew Jonesd8e052b2015-03-16 12:30:46 +00007531
7532 ap = extract32(attrs, 4, 2);
Andrew Jonesd8e052b2015-03-16 12:30:46 +00007533 xn = extract32(attrs, 12, 1);
Andrew Jonesd8e052b2015-03-16 12:30:46 +00007534
Edgar E. Iglesias6ab1a5e2015-10-26 14:02:01 +01007535 if (mmu_idx == ARMMMUIdx_S2NS) {
7536 ns = true;
7537 *prot = get_S2prot(env, ap, xn);
7538 } else {
7539 ns = extract32(attrs, 3, 1);
7540 pxn = extract32(attrs, 11, 1);
7541 *prot = get_S1prot(env, mmu_idx, va_size == 64, ap, ns, xn, pxn);
7542 }
Andrew Jonesd8e052b2015-03-16 12:30:46 +00007543
Peter Maydell3dde9622012-07-12 10:59:12 +00007544 fault_type = permission_fault;
Andrew Jonesd8e052b2015-03-16 12:30:46 +00007545 if (!(*prot & (1 << access_type))) {
Peter Maydell3dde9622012-07-12 10:59:12 +00007546 goto do_fault;
7547 }
Peter Maydell3dde9622012-07-12 10:59:12 +00007548
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007549 if (ns) {
7550 /* The NS bit will (as required by the architecture) have no effect if
7551 * the CPU doesn't support TZ or this is a non-secure translation
7552 * regime, because the attribute will already be non-secure.
7553 */
7554 txattrs->secure = false;
7555 }
Peter Maydell3dde9622012-07-12 10:59:12 +00007556 *phys_ptr = descaddr;
7557 *page_size_ptr = page_size;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007558 return false;
Peter Maydell3dde9622012-07-12 10:59:12 +00007559
7560do_fault:
7561 /* Long-descriptor format IFSR/DFSR value */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007562 *fsr = (1 << 9) | (fault_type << 2) | level;
Edgar E. Iglesias37785972015-10-26 14:02:04 +01007563 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */
7564 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS);
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007565 return true;
Peter Maydell3dde9622012-07-12 10:59:12 +00007566}
7567
Peter Crosthwaitef6bda882015-06-19 14:17:45 +01007568static inline void get_phys_addr_pmsav7_default(CPUARMState *env,
7569 ARMMMUIdx mmu_idx,
7570 int32_t address, int *prot)
7571{
7572 *prot = PAGE_READ | PAGE_WRITE;
7573 switch (address) {
7574 case 0xF0000000 ... 0xFFFFFFFF:
7575 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */
7576 *prot |= PAGE_EXEC;
7577 }
7578 break;
7579 case 0x00000000 ... 0x7FFFFFFF:
7580 *prot |= PAGE_EXEC;
7581 break;
7582 }
7583
7584}
7585
7586static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address,
7587 int access_type, ARMMMUIdx mmu_idx,
7588 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
7589{
7590 ARMCPU *cpu = arm_env_get_cpu(env);
7591 int n;
7592 bool is_user = regime_is_user(env, mmu_idx);
7593
7594 *phys_ptr = address;
7595 *prot = 0;
7596
7597 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */
7598 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7599 } else { /* MPU enabled */
7600 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) {
7601 /* region search */
7602 uint32_t base = env->pmsav7.drbar[n];
7603 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5);
7604 uint32_t rmask;
7605 bool srdis = false;
7606
7607 if (!(env->pmsav7.drsr[n] & 0x1)) {
7608 continue;
7609 }
7610
7611 if (!rsize) {
7612 qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0");
7613 continue;
7614 }
7615 rsize++;
7616 rmask = (1ull << rsize) - 1;
7617
7618 if (base & rmask) {
7619 qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned "
7620 "to DRSR region size, mask = %" PRIx32,
7621 base, rmask);
7622 continue;
7623 }
7624
7625 if (address < base || address > base + rmask) {
7626 continue;
7627 }
7628
7629 /* Region matched */
7630
7631 if (rsize >= 8) { /* no subregions for regions < 256 bytes */
7632 int i, snd;
7633 uint32_t srdis_mask;
7634
7635 rsize -= 3; /* sub region size (power of 2) */
7636 snd = ((address - base) >> rsize) & 0x7;
7637 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1);
7638
7639 srdis_mask = srdis ? 0x3 : 0x0;
7640 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) {
7641 /* This will check in groups of 2, 4 and then 8, whether
7642 * the subregion bits are consistent. rsize is incremented
7643 * back up to give the region size, considering consistent
7644 * adjacent subregions as one region. Stop testing if rsize
7645 * is already big enough for an entire QEMU page.
7646 */
7647 int snd_rounded = snd & ~(i - 1);
7648 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n],
7649 snd_rounded + 8, i);
7650 if (srdis_mask ^ srdis_multi) {
7651 break;
7652 }
7653 srdis_mask = (srdis_mask << i) | srdis_mask;
7654 rsize++;
7655 }
7656 }
7657 if (rsize < TARGET_PAGE_BITS) {
7658 qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region"
7659 "alignment of %" PRIu32 " bits. Minimum is %d\n",
7660 rsize, TARGET_PAGE_BITS);
7661 continue;
7662 }
7663 if (srdis) {
7664 continue;
7665 }
7666 break;
7667 }
7668
7669 if (n == -1) { /* no hits */
7670 if (cpu->pmsav7_dregion &&
7671 (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) {
7672 /* background fault */
7673 *fsr = 0;
7674 return true;
7675 }
7676 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot);
7677 } else { /* a MPU hit! */
7678 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3);
7679
7680 if (is_user) { /* User mode AP bit decoding */
7681 switch (ap) {
7682 case 0:
7683 case 1:
7684 case 5:
7685 break; /* no access */
7686 case 3:
7687 *prot |= PAGE_WRITE;
7688 /* fall through */
7689 case 2:
7690 case 6:
7691 *prot |= PAGE_READ | PAGE_EXEC;
7692 break;
7693 default:
7694 qemu_log_mask(LOG_GUEST_ERROR,
7695 "Bad value for AP bits in DRACR %"
7696 PRIx32 "\n", ap);
7697 }
7698 } else { /* Priv. mode AP bits decoding */
7699 switch (ap) {
7700 case 0:
7701 break; /* no access */
7702 case 1:
7703 case 2:
7704 case 3:
7705 *prot |= PAGE_WRITE;
7706 /* fall through */
7707 case 5:
7708 case 6:
7709 *prot |= PAGE_READ | PAGE_EXEC;
7710 break;
7711 default:
7712 qemu_log_mask(LOG_GUEST_ERROR,
7713 "Bad value for AP bits in DRACR %"
7714 PRIx32 "\n", ap);
7715 }
7716 }
7717
7718 /* execute never */
7719 if (env->pmsav7.dracr[n] & (1 << 12)) {
7720 *prot &= ~PAGE_EXEC;
7721 }
7722 }
7723 }
7724
7725 *fsr = 0x00d; /* Permission fault */
7726 return !(*prot & (1 << access_type));
7727}
7728
Peter Crosthwaite13689d42015-06-15 18:06:10 +01007729static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address,
7730 int access_type, ARMMMUIdx mmu_idx,
7731 hwaddr *phys_ptr, int *prot, uint32_t *fsr)
pbrook9ee6e8b2007-11-11 00:04:49 +00007732{
7733 int n;
7734 uint32_t mask;
7735 uint32_t base;
Peter Maydell0480f692015-02-05 13:37:24 +00007736 bool is_user = regime_is_user(env, mmu_idx);
pbrook9ee6e8b2007-11-11 00:04:49 +00007737
7738 *phys_ptr = address;
7739 for (n = 7; n >= 0; n--) {
Peter Maydell554b0b02015-02-05 13:37:24 +00007740 base = env->cp15.c6_region[n];
Peter Maydell87c3d482015-02-05 13:37:24 +00007741 if ((base & 1) == 0) {
Peter Maydell554b0b02015-02-05 13:37:24 +00007742 continue;
Peter Maydell87c3d482015-02-05 13:37:24 +00007743 }
Peter Maydell554b0b02015-02-05 13:37:24 +00007744 mask = 1 << ((base >> 1) & 0x1f);
7745 /* Keep this shift separate from the above to avoid an
7746 (undefined) << 32. */
7747 mask = (mask << 1) - 1;
Peter Maydell87c3d482015-02-05 13:37:24 +00007748 if (((base ^ address) & ~mask) == 0) {
Peter Maydell554b0b02015-02-05 13:37:24 +00007749 break;
Peter Maydell87c3d482015-02-05 13:37:24 +00007750 }
pbrook9ee6e8b2007-11-11 00:04:49 +00007751 }
Peter Maydell87c3d482015-02-05 13:37:24 +00007752 if (n < 0) {
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007753 *fsr = 2;
7754 return true;
Peter Maydell87c3d482015-02-05 13:37:24 +00007755 }
pbrook9ee6e8b2007-11-11 00:04:49 +00007756
7757 if (access_type == 2) {
Peter Maydell7e097972014-04-15 19:18:41 +01007758 mask = env->cp15.pmsav5_insn_ap;
pbrook9ee6e8b2007-11-11 00:04:49 +00007759 } else {
Peter Maydell7e097972014-04-15 19:18:41 +01007760 mask = env->cp15.pmsav5_data_ap;
pbrook9ee6e8b2007-11-11 00:04:49 +00007761 }
7762 mask = (mask >> (n * 4)) & 0xf;
7763 switch (mask) {
7764 case 0:
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007765 *fsr = 1;
7766 return true;
pbrook9ee6e8b2007-11-11 00:04:49 +00007767 case 1:
Peter Maydell87c3d482015-02-05 13:37:24 +00007768 if (is_user) {
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007769 *fsr = 1;
7770 return true;
Peter Maydell87c3d482015-02-05 13:37:24 +00007771 }
Peter Maydell554b0b02015-02-05 13:37:24 +00007772 *prot = PAGE_READ | PAGE_WRITE;
7773 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00007774 case 2:
Peter Maydell554b0b02015-02-05 13:37:24 +00007775 *prot = PAGE_READ;
Peter Maydell87c3d482015-02-05 13:37:24 +00007776 if (!is_user) {
Peter Maydell554b0b02015-02-05 13:37:24 +00007777 *prot |= PAGE_WRITE;
Peter Maydell87c3d482015-02-05 13:37:24 +00007778 }
Peter Maydell554b0b02015-02-05 13:37:24 +00007779 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00007780 case 3:
Peter Maydell554b0b02015-02-05 13:37:24 +00007781 *prot = PAGE_READ | PAGE_WRITE;
7782 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00007783 case 5:
Peter Maydell87c3d482015-02-05 13:37:24 +00007784 if (is_user) {
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007785 *fsr = 1;
7786 return true;
Peter Maydell87c3d482015-02-05 13:37:24 +00007787 }
Peter Maydell554b0b02015-02-05 13:37:24 +00007788 *prot = PAGE_READ;
7789 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00007790 case 6:
Peter Maydell554b0b02015-02-05 13:37:24 +00007791 *prot = PAGE_READ;
7792 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00007793 default:
Peter Maydell554b0b02015-02-05 13:37:24 +00007794 /* Bad permission. */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007795 *fsr = 1;
7796 return true;
pbrook9ee6e8b2007-11-11 00:04:49 +00007797 }
Rabin Vincent3ad493f2010-03-20 02:28:03 +05307798 *prot |= PAGE_EXEC;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007799 return false;
pbrook9ee6e8b2007-11-11 00:04:49 +00007800}
7801
Peter Maydell702a9352012-07-12 10:59:10 +00007802/* get_phys_addr - get the physical address for this virtual address
7803 *
7804 * Find the physical address corresponding to the given virtual address,
7805 * by doing a translation table walk on MMU based systems or using the
7806 * MPU state on MPU based systems.
7807 *
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007808 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs,
7809 * prot and page_size may not be filled in, and the populated fsr value provides
Peter Maydell702a9352012-07-12 10:59:10 +00007810 * information on why the translation aborted, in the format of a
7811 * DFSR/IFSR fault register, with the following caveats:
7812 * * we honour the short vs long DFSR format differences.
7813 * * the WnR bit is never set (the caller must do this).
Peter Crosthwaitef6bda882015-06-19 14:17:45 +01007814 * * for PSMAv5 based systems we don't bother to return a full FSR format
Peter Maydell702a9352012-07-12 10:59:10 +00007815 * value.
7816 *
7817 * @env: CPUARMState
7818 * @address: virtual address to get physical address for
7819 * @access_type: 0 for read, 1 for write, 2 for execute
Peter Maydelld3649702015-02-05 13:37:24 +00007820 * @mmu_idx: MMU index indicating required translation regime
Peter Maydell702a9352012-07-12 10:59:10 +00007821 * @phys_ptr: set to the physical address corresponding to the virtual address
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007822 * @attrs: set to the memory transaction attributes to use
Peter Maydell702a9352012-07-12 10:59:10 +00007823 * @prot: set to the permissions for the page containing phys_ptr
7824 * @page_size: set to the size of the page containing phys_ptr
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007825 * @fsr: set to the DFSR/IFSR value on failure
Peter Maydell702a9352012-07-12 10:59:10 +00007826 */
Edgar E. Iglesiasaf51f562015-10-26 14:02:02 +01007827static bool get_phys_addr(CPUARMState *env, target_ulong address,
7828 int access_type, ARMMMUIdx mmu_idx,
7829 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007830 target_ulong *page_size, uint32_t *fsr,
7831 ARMMMUFaultInfo *fi)
pbrook9ee6e8b2007-11-11 00:04:49 +00007832{
Peter Maydell0480f692015-02-05 13:37:24 +00007833 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) {
Edgar E. Iglesias9b539262015-10-26 14:02:07 +01007834 /* Call ourselves recursively to do the stage 1 and then stage 2
7835 * translations.
Peter Maydell0480f692015-02-05 13:37:24 +00007836 */
Edgar E. Iglesias9b539262015-10-26 14:02:07 +01007837 if (arm_feature(env, ARM_FEATURE_EL2)) {
7838 hwaddr ipa;
7839 int s2_prot;
7840 int ret;
7841
7842 ret = get_phys_addr(env, address, access_type,
7843 mmu_idx + ARMMMUIdx_S1NSE0, &ipa, attrs,
7844 prot, page_size, fsr, fi);
7845
7846 /* If S1 fails or S2 is disabled, return early. */
7847 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) {
7848 *phys_ptr = ipa;
7849 return ret;
7850 }
7851
7852 /* S1 is done. Now do S2 translation. */
7853 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS,
7854 phys_ptr, attrs, &s2_prot,
7855 page_size, fsr, fi);
7856 fi->s2addr = ipa;
7857 /* Combine the S1 and S2 perms. */
7858 *prot &= s2_prot;
7859 return ret;
7860 } else {
7861 /*
7862 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1.
7863 */
7864 mmu_idx += ARMMMUIdx_S1NSE0;
7865 }
Fabian Aggeler54bf36e2014-12-11 12:07:52 +00007866 }
bellardb5ff1b32005-11-26 10:38:39 +00007867
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007868 /* The page table entries may downgrade secure to non-secure, but
7869 * cannot upgrade an non-secure translation regime's attributes
7870 * to secure.
7871 */
7872 attrs->secure = regime_is_secure(env, mmu_idx);
Peter Maydell0995bf82015-04-26 16:49:25 +01007873 attrs->user = regime_is_user(env, mmu_idx);
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007874
Peter Maydell0480f692015-02-05 13:37:24 +00007875 /* Fast Context Switch Extension. This doesn't exist at all in v8.
7876 * In v7 and earlier it affects all stage 1 translations.
7877 */
7878 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS
7879 && !arm_feature(env, ARM_FEATURE_V8)) {
7880 if (regime_el(env, mmu_idx) == 3) {
7881 address += env->cp15.fcseidr_s;
7882 } else {
7883 address += env->cp15.fcseidr_ns;
7884 }
7885 }
7886
Peter Crosthwaitef6bda882015-06-19 14:17:45 +01007887 /* pmsav7 has special handling for when MPU is disabled so call it before
7888 * the common MMU/MPU disabled check below.
7889 */
7890 if (arm_feature(env, ARM_FEATURE_MPU) &&
7891 arm_feature(env, ARM_FEATURE_V7)) {
7892 *page_size = TARGET_PAGE_SIZE;
7893 return get_phys_addr_pmsav7(env, address, access_type, mmu_idx,
7894 phys_ptr, prot, fsr);
7895 }
7896
Peter Maydell0480f692015-02-05 13:37:24 +00007897 if (regime_translation_disabled(env, mmu_idx)) {
pbrookce819862007-05-08 02:30:40 +00007898 /* MMU/MPU disabled. */
bellardb5ff1b32005-11-26 10:38:39 +00007899 *phys_ptr = address;
Rabin Vincent3ad493f2010-03-20 02:28:03 +05307900 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC;
Paul Brookd4c430a2010-03-17 02:14:28 +00007901 *page_size = TARGET_PAGE_SIZE;
pbrook9ee6e8b2007-11-11 00:04:49 +00007902 return 0;
Peter Maydell0480f692015-02-05 13:37:24 +00007903 }
7904
7905 if (arm_feature(env, ARM_FEATURE_MPU)) {
Peter Crosthwaitef6bda882015-06-19 14:17:45 +01007906 /* Pre-v7 MPU */
Paul Brookd4c430a2010-03-17 02:14:28 +00007907 *page_size = TARGET_PAGE_SIZE;
Peter Crosthwaite13689d42015-06-15 18:06:10 +01007908 return get_phys_addr_pmsav5(env, address, access_type, mmu_idx,
7909 phys_ptr, prot, fsr);
Peter Maydell0480f692015-02-05 13:37:24 +00007910 }
7911
7912 if (regime_using_lpae_format(env, mmu_idx)) {
7913 return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007914 attrs, prot, page_size, fsr, fi);
Peter Maydell0480f692015-02-05 13:37:24 +00007915 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) {
7916 return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007917 attrs, prot, page_size, fsr, fi);
bellardb5ff1b32005-11-26 10:38:39 +00007918 } else {
Peter Maydell0480f692015-02-05 13:37:24 +00007919 return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007920 prot, page_size, fsr, fi);
bellardb5ff1b32005-11-26 10:38:39 +00007921 }
bellardb5ff1b32005-11-26 10:38:39 +00007922}
7923
Peter Maydell8c6084b2015-05-29 11:28:51 +01007924/* Walk the page table and (if the mapping exists) add the page
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007925 * to the TLB. Return false on success, or true on failure. Populate
7926 * fsr with ARM DFSR/IFSR fault register format value on failure.
Peter Maydell8c6084b2015-05-29 11:28:51 +01007927 */
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007928bool arm_tlb_fill(CPUState *cs, vaddr address,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007929 int access_type, int mmu_idx, uint32_t *fsr,
7930 ARMMMUFaultInfo *fi)
bellardb5ff1b32005-11-26 10:38:39 +00007931{
Andreas Färber75104542013-08-26 03:01:33 +02007932 ARMCPU *cpu = ARM_CPU(cs);
7933 CPUARMState *env = &cpu->env;
Avi Kivitya8170e52012-10-23 12:30:10 +02007934 hwaddr phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00007935 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00007936 int prot;
Peter Maydelld3649702015-02-05 13:37:24 +00007937 int ret;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007938 MemTxAttrs attrs = {};
bellardb5ff1b32005-11-26 10:38:39 +00007939
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007940 ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr,
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007941 &attrs, &prot, &page_size, fsr, fi);
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007942 if (!ret) {
bellardb5ff1b32005-11-26 10:38:39 +00007943 /* Map a single [sub]page. */
Alex Bennéedcd82c12014-08-04 14:41:55 +01007944 phys_addr &= TARGET_PAGE_MASK;
7945 address &= TARGET_PAGE_MASK;
Peter Maydell8bf5b6a2015-04-26 16:49:25 +01007946 tlb_set_page_with_attrs(cs, address, phys_addr, attrs,
7947 prot, mmu_idx, page_size);
Paul Brookd4c430a2010-03-17 02:14:28 +00007948 return 0;
bellardb5ff1b32005-11-26 10:38:39 +00007949 }
7950
Peter Maydell8c6084b2015-05-29 11:28:51 +01007951 return ret;
bellardb5ff1b32005-11-26 10:38:39 +00007952}
7953
Peter Maydell0faea0c2016-01-21 14:15:07 +00007954hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr,
7955 MemTxAttrs *attrs)
bellardb5ff1b32005-11-26 10:38:39 +00007956{
Andreas Färber00b941e2013-06-29 18:55:54 +02007957 ARMCPU *cpu = ARM_CPU(cs);
Peter Maydelld3649702015-02-05 13:37:24 +00007958 CPUARMState *env = &cpu->env;
Avi Kivitya8170e52012-10-23 12:30:10 +02007959 hwaddr phys_addr;
Paul Brookd4c430a2010-03-17 02:14:28 +00007960 target_ulong page_size;
bellardb5ff1b32005-11-26 10:38:39 +00007961 int prot;
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007962 bool ret;
7963 uint32_t fsr;
Edgar E. Iglesiase14b5a22015-10-26 14:02:03 +01007964 ARMMMUFaultInfo fi = {};
bellardb5ff1b32005-11-26 10:38:39 +00007965
Peter Maydell0faea0c2016-01-21 14:15:07 +00007966 *attrs = (MemTxAttrs) {};
7967
Benjamin Herrenschmidt97ed5cc2015-08-17 17:34:10 +10007968 ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr,
Peter Maydell0faea0c2016-01-21 14:15:07 +00007969 attrs, &prot, &page_size, &fsr, &fi);
bellardb5ff1b32005-11-26 10:38:39 +00007970
Peter Crosthwaiteb7cc4e82015-06-15 18:06:10 +01007971 if (ret) {
bellardb5ff1b32005-11-26 10:38:39 +00007972 return -1;
Andreas Färber00b941e2013-06-29 18:55:54 +02007973 }
bellardb5ff1b32005-11-26 10:38:39 +00007974 return phys_addr;
7975}
7976
Andreas Färber0ecb72a2012-03-14 01:38:21 +01007977uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg)
pbrook9ee6e8b2007-11-11 00:04:49 +00007978{
Andreas Färbera47dddd2013-09-03 17:38:47 +02007979 ARMCPU *cpu = arm_env_get_cpu(env);
7980
pbrook9ee6e8b2007-11-11 00:04:49 +00007981 switch (reg) {
7982 case 0: /* APSR */
7983 return xpsr_read(env) & 0xf8000000;
7984 case 1: /* IAPSR */
7985 return xpsr_read(env) & 0xf80001ff;
7986 case 2: /* EAPSR */
7987 return xpsr_read(env) & 0xff00fc00;
7988 case 3: /* xPSR */
7989 return xpsr_read(env) & 0xff00fdff;
7990 case 5: /* IPSR */
7991 return xpsr_read(env) & 0x000001ff;
7992 case 6: /* EPSR */
7993 return xpsr_read(env) & 0x0700fc00;
7994 case 7: /* IEPSR */
7995 return xpsr_read(env) & 0x0700edff;
7996 case 8: /* MSP */
7997 return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13];
7998 case 9: /* PSP */
7999 return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp;
8000 case 16: /* PRIMASK */
Peter Maydell4cc35612014-02-26 17:20:06 +00008001 return (env->daif & PSTATE_I) != 0;
Sebastian Huber82845822011-05-29 02:58:41 +00008002 case 17: /* BASEPRI */
8003 case 18: /* BASEPRI_MAX */
pbrook9ee6e8b2007-11-11 00:04:49 +00008004 return env->v7m.basepri;
Sebastian Huber82845822011-05-29 02:58:41 +00008005 case 19: /* FAULTMASK */
Peter Maydell4cc35612014-02-26 17:20:06 +00008006 return (env->daif & PSTATE_F) != 0;
pbrook9ee6e8b2007-11-11 00:04:49 +00008007 case 20: /* CONTROL */
8008 return env->v7m.control;
8009 default:
8010 /* ??? For debugging only. */
Andreas Färbera47dddd2013-09-03 17:38:47 +02008011 cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg);
pbrook9ee6e8b2007-11-11 00:04:49 +00008012 return 0;
8013 }
8014}
8015
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008016void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val)
pbrook9ee6e8b2007-11-11 00:04:49 +00008017{
Andreas Färbera47dddd2013-09-03 17:38:47 +02008018 ARMCPU *cpu = arm_env_get_cpu(env);
8019
pbrook9ee6e8b2007-11-11 00:04:49 +00008020 switch (reg) {
8021 case 0: /* APSR */
8022 xpsr_write(env, val, 0xf8000000);
8023 break;
8024 case 1: /* IAPSR */
8025 xpsr_write(env, val, 0xf8000000);
8026 break;
8027 case 2: /* EAPSR */
8028 xpsr_write(env, val, 0xfe00fc00);
8029 break;
8030 case 3: /* xPSR */
8031 xpsr_write(env, val, 0xfe00fc00);
8032 break;
8033 case 5: /* IPSR */
8034 /* IPSR bits are readonly. */
8035 break;
8036 case 6: /* EPSR */
8037 xpsr_write(env, val, 0x0600fc00);
8038 break;
8039 case 7: /* IEPSR */
8040 xpsr_write(env, val, 0x0600fc00);
8041 break;
8042 case 8: /* MSP */
8043 if (env->v7m.current_sp)
8044 env->v7m.other_sp = val;
8045 else
8046 env->regs[13] = val;
8047 break;
8048 case 9: /* PSP */
8049 if (env->v7m.current_sp)
8050 env->regs[13] = val;
8051 else
8052 env->v7m.other_sp = val;
8053 break;
8054 case 16: /* PRIMASK */
Peter Maydell4cc35612014-02-26 17:20:06 +00008055 if (val & 1) {
8056 env->daif |= PSTATE_I;
8057 } else {
8058 env->daif &= ~PSTATE_I;
8059 }
pbrook9ee6e8b2007-11-11 00:04:49 +00008060 break;
Sebastian Huber82845822011-05-29 02:58:41 +00008061 case 17: /* BASEPRI */
8062 env->v7m.basepri = val & 0xff;
8063 break;
8064 case 18: /* BASEPRI_MAX */
8065 val &= 0xff;
8066 if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0))
8067 env->v7m.basepri = val;
8068 break;
8069 case 19: /* FAULTMASK */
Peter Maydell4cc35612014-02-26 17:20:06 +00008070 if (val & 1) {
8071 env->daif |= PSTATE_F;
8072 } else {
8073 env->daif &= ~PSTATE_F;
8074 }
pbrook9ee6e8b2007-11-11 00:04:49 +00008075 break;
pbrook9ee6e8b2007-11-11 00:04:49 +00008076 case 20: /* CONTROL */
8077 env->v7m.control = val & 3;
8078 switch_v7m_sp(env, (val & 2) != 0);
8079 break;
8080 default:
8081 /* ??? For debugging only. */
Andreas Färbera47dddd2013-09-03 17:38:47 +02008082 cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg);
pbrook9ee6e8b2007-11-11 00:04:49 +00008083 return;
8084 }
8085}
8086
bellardb5ff1b32005-11-26 10:38:39 +00008087#endif
pbrook6ddbc6e2008-03-31 03:46:33 +00008088
Peter Maydellaca3f402014-04-15 19:18:41 +01008089void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in)
8090{
8091 /* Implement DC ZVA, which zeroes a fixed-length block of memory.
8092 * Note that we do not implement the (architecturally mandated)
8093 * alignment fault for attempts to use this on Device memory
8094 * (which matches the usual QEMU behaviour of not implementing either
8095 * alignment faults or any memory attribute handling).
8096 */
8097
8098 ARMCPU *cpu = arm_env_get_cpu(env);
8099 uint64_t blocklen = 4 << cpu->dcz_blocksize;
8100 uint64_t vaddr = vaddr_in & ~(blocklen - 1);
8101
8102#ifndef CONFIG_USER_ONLY
8103 {
8104 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than
8105 * the block size so we might have to do more than one TLB lookup.
8106 * We know that in fact for any v8 CPU the page size is at least 4K
8107 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only
8108 * 1K as an artefact of legacy v5 subpage support being present in the
8109 * same QEMU executable.
8110 */
8111 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE);
8112 void *hostaddr[maxidx];
8113 int try, i;
Benjamin Herrenschmidt97ed5cc2015-08-17 17:34:10 +10008114 unsigned mmu_idx = cpu_mmu_index(env, false);
Richard Henderson3972ef62015-05-13 09:10:33 -07008115 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx);
Peter Maydellaca3f402014-04-15 19:18:41 +01008116
8117 for (try = 0; try < 2; try++) {
8118
8119 for (i = 0; i < maxidx; i++) {
8120 hostaddr[i] = tlb_vaddr_to_host(env,
8121 vaddr + TARGET_PAGE_SIZE * i,
Richard Henderson3972ef62015-05-13 09:10:33 -07008122 1, mmu_idx);
Peter Maydellaca3f402014-04-15 19:18:41 +01008123 if (!hostaddr[i]) {
8124 break;
8125 }
8126 }
8127 if (i == maxidx) {
8128 /* If it's all in the TLB it's fair game for just writing to;
8129 * we know we don't need to update dirty status, etc.
8130 */
8131 for (i = 0; i < maxidx - 1; i++) {
8132 memset(hostaddr[i], 0, TARGET_PAGE_SIZE);
8133 }
8134 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE));
8135 return;
8136 }
8137 /* OK, try a store and see if we can populate the tlb. This
8138 * might cause an exception if the memory isn't writable,
8139 * in which case we will longjmp out of here. We must for
8140 * this purpose use the actual register value passed to us
8141 * so that we get the fault address right.
8142 */
Richard Henderson3972ef62015-05-13 09:10:33 -07008143 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETRA());
Peter Maydellaca3f402014-04-15 19:18:41 +01008144 /* Now we can populate the other TLB entries, if any */
8145 for (i = 0; i < maxidx; i++) {
8146 uint64_t va = vaddr + TARGET_PAGE_SIZE * i;
8147 if (va != (vaddr_in & TARGET_PAGE_MASK)) {
Richard Henderson3972ef62015-05-13 09:10:33 -07008148 helper_ret_stb_mmu(env, va, 0, oi, GETRA());
Peter Maydellaca3f402014-04-15 19:18:41 +01008149 }
8150 }
8151 }
8152
8153 /* Slow path (probably attempt to do this to an I/O device or
8154 * similar, or clearing of a block of code we have translations
8155 * cached for). Just do a series of byte writes as the architecture
8156 * demands. It's not worth trying to use a cpu_physical_memory_map(),
8157 * memset(), unmap() sequence here because:
8158 * + we'd need to account for the blocksize being larger than a page
8159 * + the direct-RAM access case is almost always going to be dealt
8160 * with in the fastpath code above, so there's no speed benefit
8161 * + we would have to deal with the map returning NULL because the
8162 * bounce buffer was in use
8163 */
8164 for (i = 0; i < blocklen; i++) {
Richard Henderson3972ef62015-05-13 09:10:33 -07008165 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETRA());
Peter Maydellaca3f402014-04-15 19:18:41 +01008166 }
8167 }
8168#else
8169 memset(g2h(vaddr), 0, blocklen);
8170#endif
8171}
8172
pbrook6ddbc6e2008-03-31 03:46:33 +00008173/* Note that signed overflow is undefined in C. The following routines are
8174 careful to use unsigned types where modulo arithmetic is required.
8175 Failure to do so _will_ break on newer gcc. */
8176
8177/* Signed saturating arithmetic. */
8178
aurel321654b2d2008-04-11 04:55:07 +00008179/* Perform 16-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00008180static inline uint16_t add16_sat(uint16_t a, uint16_t b)
8181{
8182 uint16_t res;
8183
8184 res = a + b;
8185 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) {
8186 if (a & 0x8000)
8187 res = 0x8000;
8188 else
8189 res = 0x7fff;
8190 }
8191 return res;
8192}
8193
aurel321654b2d2008-04-11 04:55:07 +00008194/* Perform 8-bit signed saturating addition. */
pbrook6ddbc6e2008-03-31 03:46:33 +00008195static inline uint8_t add8_sat(uint8_t a, uint8_t b)
8196{
8197 uint8_t res;
8198
8199 res = a + b;
8200 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) {
8201 if (a & 0x80)
8202 res = 0x80;
8203 else
8204 res = 0x7f;
8205 }
8206 return res;
8207}
8208
aurel321654b2d2008-04-11 04:55:07 +00008209/* Perform 16-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00008210static inline uint16_t sub16_sat(uint16_t a, uint16_t b)
8211{
8212 uint16_t res;
8213
8214 res = a - b;
8215 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) {
8216 if (a & 0x8000)
8217 res = 0x8000;
8218 else
8219 res = 0x7fff;
8220 }
8221 return res;
8222}
8223
aurel321654b2d2008-04-11 04:55:07 +00008224/* Perform 8-bit signed saturating subtraction. */
pbrook6ddbc6e2008-03-31 03:46:33 +00008225static inline uint8_t sub8_sat(uint8_t a, uint8_t b)
8226{
8227 uint8_t res;
8228
8229 res = a - b;
8230 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) {
8231 if (a & 0x80)
8232 res = 0x80;
8233 else
8234 res = 0x7f;
8235 }
8236 return res;
8237}
8238
8239#define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16);
8240#define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16);
8241#define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8);
8242#define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8);
8243#define PFX q
8244
8245#include "op_addsub.h"
8246
8247/* Unsigned saturating arithmetic. */
pbrook460a09c2008-05-01 12:04:35 +00008248static inline uint16_t add16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00008249{
8250 uint16_t res;
8251 res = a + b;
8252 if (res < a)
8253 res = 0xffff;
8254 return res;
8255}
8256
pbrook460a09c2008-05-01 12:04:35 +00008257static inline uint16_t sub16_usat(uint16_t a, uint16_t b)
pbrook6ddbc6e2008-03-31 03:46:33 +00008258{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08008259 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00008260 return a - b;
8261 else
8262 return 0;
8263}
8264
8265static inline uint8_t add8_usat(uint8_t a, uint8_t b)
8266{
8267 uint8_t res;
8268 res = a + b;
8269 if (res < a)
8270 res = 0xff;
8271 return res;
8272}
8273
8274static inline uint8_t sub8_usat(uint8_t a, uint8_t b)
8275{
Chih-Min Chao4c4fd3f2010-06-28 23:54:06 +08008276 if (a > b)
pbrook6ddbc6e2008-03-31 03:46:33 +00008277 return a - b;
8278 else
8279 return 0;
8280}
8281
8282#define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16);
8283#define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16);
8284#define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8);
8285#define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8);
8286#define PFX uq
8287
8288#include "op_addsub.h"
8289
8290/* Signed modulo arithmetic. */
8291#define SARITH16(a, b, n, op) do { \
8292 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00008293 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00008294 RESULT(sum, n, 16); \
8295 if (sum >= 0) \
8296 ge |= 3 << (n * 2); \
8297 } while(0)
8298
8299#define SARITH8(a, b, n, op) do { \
8300 int32_t sum; \
Peter Maydelldb6e2e62011-03-10 18:51:49 +00008301 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \
pbrook6ddbc6e2008-03-31 03:46:33 +00008302 RESULT(sum, n, 8); \
8303 if (sum >= 0) \
8304 ge |= 1 << n; \
8305 } while(0)
8306
8307
8308#define ADD16(a, b, n) SARITH16(a, b, n, +)
8309#define SUB16(a, b, n) SARITH16(a, b, n, -)
8310#define ADD8(a, b, n) SARITH8(a, b, n, +)
8311#define SUB8(a, b, n) SARITH8(a, b, n, -)
8312#define PFX s
8313#define ARITH_GE
8314
8315#include "op_addsub.h"
8316
8317/* Unsigned modulo arithmetic. */
8318#define ADD16(a, b, n) do { \
8319 uint32_t sum; \
8320 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \
8321 RESULT(sum, n, 16); \
balroga87aa102008-07-19 10:46:13 +00008322 if ((sum >> 16) == 1) \
pbrook6ddbc6e2008-03-31 03:46:33 +00008323 ge |= 3 << (n * 2); \
8324 } while(0)
8325
8326#define ADD8(a, b, n) do { \
8327 uint32_t sum; \
8328 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \
8329 RESULT(sum, n, 8); \
balroga87aa102008-07-19 10:46:13 +00008330 if ((sum >> 8) == 1) \
8331 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00008332 } while(0)
8333
8334#define SUB16(a, b, n) do { \
8335 uint32_t sum; \
8336 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \
8337 RESULT(sum, n, 16); \
8338 if ((sum >> 16) == 0) \
8339 ge |= 3 << (n * 2); \
8340 } while(0)
8341
8342#define SUB8(a, b, n) do { \
8343 uint32_t sum; \
8344 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \
8345 RESULT(sum, n, 8); \
8346 if ((sum >> 8) == 0) \
balroga87aa102008-07-19 10:46:13 +00008347 ge |= 1 << n; \
pbrook6ddbc6e2008-03-31 03:46:33 +00008348 } while(0)
8349
8350#define PFX u
8351#define ARITH_GE
8352
8353#include "op_addsub.h"
8354
8355/* Halved signed arithmetic. */
8356#define ADD16(a, b, n) \
8357 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16)
8358#define SUB16(a, b, n) \
8359 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16)
8360#define ADD8(a, b, n) \
8361 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8)
8362#define SUB8(a, b, n) \
8363 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8)
8364#define PFX sh
8365
8366#include "op_addsub.h"
8367
8368/* Halved unsigned arithmetic. */
8369#define ADD16(a, b, n) \
8370 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8371#define SUB16(a, b, n) \
8372 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16)
8373#define ADD8(a, b, n) \
8374 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8375#define SUB8(a, b, n) \
8376 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8)
8377#define PFX uh
8378
8379#include "op_addsub.h"
8380
8381static inline uint8_t do_usad(uint8_t a, uint8_t b)
8382{
8383 if (a > b)
8384 return a - b;
8385 else
8386 return b - a;
8387}
8388
8389/* Unsigned sum of absolute byte differences. */
8390uint32_t HELPER(usad8)(uint32_t a, uint32_t b)
8391{
8392 uint32_t sum;
8393 sum = do_usad(a, b);
8394 sum += do_usad(a >> 8, b >> 8);
8395 sum += do_usad(a >> 16, b >>16);
8396 sum += do_usad(a >> 24, b >> 24);
8397 return sum;
8398}
8399
8400/* For ARMv6 SEL instruction. */
8401uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b)
8402{
8403 uint32_t mask;
8404
8405 mask = 0;
8406 if (flags & 1)
8407 mask |= 0xff;
8408 if (flags & 2)
8409 mask |= 0xff00;
8410 if (flags & 4)
8411 mask |= 0xff0000;
8412 if (flags & 8)
8413 mask |= 0xff000000;
8414 return (a & mask) | (b & ~mask);
8415}
8416
Peter Maydellb90372a2012-08-06 17:42:18 +01008417/* VFP support. We follow the convention used for VFP instructions:
8418 Single precision routines have a "s" suffix, double precision a
pbrook4373f3c2008-03-31 03:47:19 +00008419 "d" suffix. */
8420
8421/* Convert host exception flags to vfp form. */
8422static inline int vfp_exceptbits_from_host(int host_bits)
8423{
8424 int target_bits = 0;
8425
8426 if (host_bits & float_flag_invalid)
8427 target_bits |= 1;
8428 if (host_bits & float_flag_divbyzero)
8429 target_bits |= 2;
8430 if (host_bits & float_flag_overflow)
8431 target_bits |= 4;
Peter Maydell36802b62011-05-19 14:46:18 +01008432 if (host_bits & (float_flag_underflow | float_flag_output_denormal))
pbrook4373f3c2008-03-31 03:47:19 +00008433 target_bits |= 8;
8434 if (host_bits & float_flag_inexact)
8435 target_bits |= 0x10;
Peter Maydellcecd8502011-01-06 19:37:55 +00008436 if (host_bits & float_flag_input_denormal)
8437 target_bits |= 0x80;
pbrook4373f3c2008-03-31 03:47:19 +00008438 return target_bits;
8439}
8440
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008441uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00008442{
8443 int i;
8444 uint32_t fpscr;
8445
8446 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff)
8447 | (env->vfp.vec_len << 16)
8448 | (env->vfp.vec_stride << 20);
8449 i = get_float_exception_flags(&env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01008450 i |= get_float_exception_flags(&env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00008451 fpscr |= vfp_exceptbits_from_host(i);
8452 return fpscr;
8453}
8454
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008455uint32_t vfp_get_fpscr(CPUARMState *env)
Peter Maydell01653292010-11-24 15:20:04 +00008456{
8457 return HELPER(vfp_get_fpscr)(env);
8458}
8459
pbrook4373f3c2008-03-31 03:47:19 +00008460/* Convert vfp exception flags to target form. */
8461static inline int vfp_exceptbits_to_host(int target_bits)
8462{
8463 int host_bits = 0;
8464
8465 if (target_bits & 1)
8466 host_bits |= float_flag_invalid;
8467 if (target_bits & 2)
8468 host_bits |= float_flag_divbyzero;
8469 if (target_bits & 4)
8470 host_bits |= float_flag_overflow;
8471 if (target_bits & 8)
8472 host_bits |= float_flag_underflow;
8473 if (target_bits & 0x10)
8474 host_bits |= float_flag_inexact;
Peter Maydellcecd8502011-01-06 19:37:55 +00008475 if (target_bits & 0x80)
8476 host_bits |= float_flag_input_denormal;
pbrook4373f3c2008-03-31 03:47:19 +00008477 return host_bits;
8478}
8479
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008480void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val)
pbrook4373f3c2008-03-31 03:47:19 +00008481{
8482 int i;
8483 uint32_t changed;
8484
8485 changed = env->vfp.xregs[ARM_VFP_FPSCR];
8486 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff);
8487 env->vfp.vec_len = (val >> 16) & 7;
8488 env->vfp.vec_stride = (val >> 20) & 3;
8489
8490 changed ^= val;
8491 if (changed & (3 << 22)) {
8492 i = (val >> 22) & 3;
8493 switch (i) {
Alexander Graf4d3da0f2014-01-04 22:15:51 +00008494 case FPROUNDING_TIEEVEN:
pbrook4373f3c2008-03-31 03:47:19 +00008495 i = float_round_nearest_even;
8496 break;
Alexander Graf4d3da0f2014-01-04 22:15:51 +00008497 case FPROUNDING_POSINF:
pbrook4373f3c2008-03-31 03:47:19 +00008498 i = float_round_up;
8499 break;
Alexander Graf4d3da0f2014-01-04 22:15:51 +00008500 case FPROUNDING_NEGINF:
pbrook4373f3c2008-03-31 03:47:19 +00008501 i = float_round_down;
8502 break;
Alexander Graf4d3da0f2014-01-04 22:15:51 +00008503 case FPROUNDING_ZERO:
pbrook4373f3c2008-03-31 03:47:19 +00008504 i = float_round_to_zero;
8505 break;
8506 }
8507 set_float_rounding_mode(i, &env->vfp.fp_status);
8508 }
Peter Maydellcecd8502011-01-06 19:37:55 +00008509 if (changed & (1 << 24)) {
pbrookfe76d972008-12-19 14:33:59 +00008510 set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
Peter Maydellcecd8502011-01-06 19:37:55 +00008511 set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status);
8512 }
pbrook5c7908e2008-12-19 13:53:37 +00008513 if (changed & (1 << 25))
8514 set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00008515
Peter Maydellb12c3902011-01-06 19:37:54 +00008516 i = vfp_exceptbits_to_host(val);
pbrook4373f3c2008-03-31 03:47:19 +00008517 set_float_exception_flags(i, &env->vfp.fp_status);
Peter Maydell3a492f32011-01-14 20:39:18 +01008518 set_float_exception_flags(0, &env->vfp.standard_fp_status);
pbrook4373f3c2008-03-31 03:47:19 +00008519}
8520
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008521void vfp_set_fpscr(CPUARMState *env, uint32_t val)
Peter Maydell01653292010-11-24 15:20:04 +00008522{
8523 HELPER(vfp_set_fpscr)(env, val);
8524}
8525
pbrook4373f3c2008-03-31 03:47:19 +00008526#define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p))
8527
8528#define VFP_BINOP(name) \
Peter Maydellae1857e2011-05-25 14:51:48 +00008529float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00008530{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00008531 float_status *fpst = fpstp; \
8532 return float32_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00008533} \
Peter Maydellae1857e2011-05-25 14:51:48 +00008534float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00008535{ \
Peter Maydellae1857e2011-05-25 14:51:48 +00008536 float_status *fpst = fpstp; \
8537 return float64_ ## name(a, b, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00008538}
8539VFP_BINOP(add)
8540VFP_BINOP(sub)
8541VFP_BINOP(mul)
8542VFP_BINOP(div)
Peter Maydellf71a2ae2014-01-04 22:15:49 +00008543VFP_BINOP(min)
8544VFP_BINOP(max)
8545VFP_BINOP(minnum)
8546VFP_BINOP(maxnum)
pbrook4373f3c2008-03-31 03:47:19 +00008547#undef VFP_BINOP
8548
8549float32 VFP_HELPER(neg, s)(float32 a)
8550{
8551 return float32_chs(a);
8552}
8553
8554float64 VFP_HELPER(neg, d)(float64 a)
8555{
balrog66230e02008-04-20 00:58:01 +00008556 return float64_chs(a);
pbrook4373f3c2008-03-31 03:47:19 +00008557}
8558
8559float32 VFP_HELPER(abs, s)(float32 a)
8560{
8561 return float32_abs(a);
8562}
8563
8564float64 VFP_HELPER(abs, d)(float64 a)
8565{
balrog66230e02008-04-20 00:58:01 +00008566 return float64_abs(a);
pbrook4373f3c2008-03-31 03:47:19 +00008567}
8568
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008569float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00008570{
8571 return float32_sqrt(a, &env->vfp.fp_status);
8572}
8573
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008574float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00008575{
8576 return float64_sqrt(a, &env->vfp.fp_status);
8577}
8578
8579/* XXX: check quiet/signaling case */
8580#define DO_VFP_cmp(p, type) \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008581void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00008582{ \
8583 uint32_t flags; \
8584 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \
8585 case 0: flags = 0x6; break; \
8586 case -1: flags = 0x8; break; \
8587 case 1: flags = 0x2; break; \
8588 default: case 2: flags = 0x3; break; \
8589 } \
8590 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8591 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8592} \
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008593void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \
pbrook4373f3c2008-03-31 03:47:19 +00008594{ \
8595 uint32_t flags; \
8596 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \
8597 case 0: flags = 0x6; break; \
8598 case -1: flags = 0x8; break; \
8599 case 1: flags = 0x2; break; \
8600 default: case 2: flags = 0x3; break; \
8601 } \
8602 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \
8603 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \
8604}
8605DO_VFP_cmp(s, float32)
8606DO_VFP_cmp(d, float64)
8607#undef DO_VFP_cmp
8608
Peter Maydell5500b062011-05-19 14:46:19 +01008609/* Integer to float and float to integer conversions */
8610
8611#define CONV_ITOF(name, fsz, sign) \
8612 float##fsz HELPER(name)(uint32_t x, void *fpstp) \
8613{ \
8614 float_status *fpst = fpstp; \
Peter Maydell85836972012-01-25 11:49:46 +00008615 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00008616}
8617
Peter Maydell5500b062011-05-19 14:46:19 +01008618#define CONV_FTOI(name, fsz, sign, round) \
8619uint32_t HELPER(name)(float##fsz x, void *fpstp) \
8620{ \
8621 float_status *fpst = fpstp; \
8622 if (float##fsz##_is_any_nan(x)) { \
8623 float_raise(float_flag_invalid, fpst); \
8624 return 0; \
8625 } \
8626 return float##fsz##_to_##sign##int32##round(x, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00008627}
8628
Peter Maydell5500b062011-05-19 14:46:19 +01008629#define FLOAT_CONVS(name, p, fsz, sign) \
8630CONV_ITOF(vfp_##name##to##p, fsz, sign) \
8631CONV_FTOI(vfp_to##name##p, fsz, sign, ) \
8632CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero)
pbrook4373f3c2008-03-31 03:47:19 +00008633
Peter Maydell5500b062011-05-19 14:46:19 +01008634FLOAT_CONVS(si, s, 32, )
8635FLOAT_CONVS(si, d, 64, )
8636FLOAT_CONVS(ui, s, 32, u)
8637FLOAT_CONVS(ui, d, 64, u)
pbrook4373f3c2008-03-31 03:47:19 +00008638
Peter Maydell5500b062011-05-19 14:46:19 +01008639#undef CONV_ITOF
8640#undef CONV_FTOI
8641#undef FLOAT_CONVS
pbrook4373f3c2008-03-31 03:47:19 +00008642
8643/* floating point conversion */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008644float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00008645{
Peter Maydell2d627732010-12-07 15:37:34 +00008646 float64 r = float32_to_float64(x, &env->vfp.fp_status);
8647 /* ARM requires that S<->D conversion of any kind of NaN generates
8648 * a quiet NaN by forcing the most significant frac bit to 1.
8649 */
8650 return float64_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00008651}
8652
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008653float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00008654{
Peter Maydell2d627732010-12-07 15:37:34 +00008655 float32 r = float64_to_float32(x, &env->vfp.fp_status);
8656 /* ARM requires that S<->D conversion of any kind of NaN generates
8657 * a quiet NaN by forcing the most significant frac bit to 1.
8658 */
8659 return float32_maybe_silence_nan(r);
pbrook4373f3c2008-03-31 03:47:19 +00008660}
8661
8662/* VFP3 fixed point conversion. */
Will Newton16d5b3c2014-01-07 17:19:13 +00008663#define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
Will Newton8ed697e2014-01-07 17:19:13 +00008664float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \
8665 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00008666{ \
Peter Maydell5500b062011-05-19 14:46:19 +01008667 float_status *fpst = fpstp; \
Peter Maydell622465e2011-03-14 07:23:11 +00008668 float##fsz tmp; \
Will Newton8ed697e2014-01-07 17:19:13 +00008669 tmp = itype##_to_##float##fsz(x, fpst); \
Peter Maydell5500b062011-05-19 14:46:19 +01008670 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \
Will Newton16d5b3c2014-01-07 17:19:13 +00008671}
8672
Peter Maydellabe66f72014-01-07 17:19:13 +00008673/* Notice that we want only input-denormal exception flags from the
8674 * scalbn operation: the other possible flags (overflow+inexact if
8675 * we overflow to infinity, output-denormal) aren't correct for the
8676 * complete scale-and-convert operation.
8677 */
Will Newton16d5b3c2014-01-07 17:19:13 +00008678#define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \
8679uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \
8680 uint32_t shift, \
8681 void *fpstp) \
pbrook4373f3c2008-03-31 03:47:19 +00008682{ \
Peter Maydell5500b062011-05-19 14:46:19 +01008683 float_status *fpst = fpstp; \
Peter Maydellabe66f72014-01-07 17:19:13 +00008684 int old_exc_flags = get_float_exception_flags(fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00008685 float##fsz tmp; \
8686 if (float##fsz##_is_any_nan(x)) { \
Peter Maydell5500b062011-05-19 14:46:19 +01008687 float_raise(float_flag_invalid, fpst); \
Peter Maydell622465e2011-03-14 07:23:11 +00008688 return 0; \
Peter Maydell09d94872010-12-07 15:37:34 +00008689 } \
Peter Maydell5500b062011-05-19 14:46:19 +01008690 tmp = float##fsz##_scalbn(x, shift, fpst); \
Peter Maydellabe66f72014-01-07 17:19:13 +00008691 old_exc_flags |= get_float_exception_flags(fpst) \
8692 & float_flag_input_denormal; \
8693 set_float_exception_flags(old_exc_flags, fpst); \
Will Newton16d5b3c2014-01-07 17:19:13 +00008694 return float##fsz##_to_##itype##round(tmp, fpst); \
pbrook4373f3c2008-03-31 03:47:19 +00008695}
8696
Will Newton16d5b3c2014-01-07 17:19:13 +00008697#define VFP_CONV_FIX(name, p, fsz, isz, itype) \
8698VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
Will Newton3c6a0742014-01-07 17:19:14 +00008699VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \
8700VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
8701
8702#define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \
8703VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \
8704VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, )
Will Newton16d5b3c2014-01-07 17:19:13 +00008705
Will Newton8ed697e2014-01-07 17:19:13 +00008706VFP_CONV_FIX(sh, d, 64, 64, int16)
8707VFP_CONV_FIX(sl, d, 64, 64, int32)
Will Newton3c6a0742014-01-07 17:19:14 +00008708VFP_CONV_FIX_A64(sq, d, 64, 64, int64)
Will Newton8ed697e2014-01-07 17:19:13 +00008709VFP_CONV_FIX(uh, d, 64, 64, uint16)
8710VFP_CONV_FIX(ul, d, 64, 64, uint32)
Will Newton3c6a0742014-01-07 17:19:14 +00008711VFP_CONV_FIX_A64(uq, d, 64, 64, uint64)
Will Newton8ed697e2014-01-07 17:19:13 +00008712VFP_CONV_FIX(sh, s, 32, 32, int16)
8713VFP_CONV_FIX(sl, s, 32, 32, int32)
Will Newton3c6a0742014-01-07 17:19:14 +00008714VFP_CONV_FIX_A64(sq, s, 32, 64, int64)
Will Newton8ed697e2014-01-07 17:19:13 +00008715VFP_CONV_FIX(uh, s, 32, 32, uint16)
8716VFP_CONV_FIX(ul, s, 32, 32, uint32)
Will Newton3c6a0742014-01-07 17:19:14 +00008717VFP_CONV_FIX_A64(uq, s, 32, 64, uint64)
pbrook4373f3c2008-03-31 03:47:19 +00008718#undef VFP_CONV_FIX
Will Newton16d5b3c2014-01-07 17:19:13 +00008719#undef VFP_CONV_FIX_FLOAT
8720#undef VFP_CONV_FLOAT_FIX_ROUND
pbrook4373f3c2008-03-31 03:47:19 +00008721
Alexander Graf52a1f6a2014-01-07 17:19:14 +00008722/* Set the current fp rounding mode and return the old one.
8723 * The argument is a softfloat float_round_ value.
8724 */
8725uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env)
8726{
8727 float_status *fp_status = &env->vfp.fp_status;
8728
8729 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8730 set_float_rounding_mode(rmode, fp_status);
8731
8732 return prev_rmode;
8733}
8734
Will Newton43630e52014-01-31 14:47:35 +00008735/* Set the current fp rounding mode in the standard fp status and return
8736 * the old one. This is for NEON instructions that need to change the
8737 * rounding mode but wish to use the standard FPSCR values for everything
8738 * else. Always set the rounding mode back to the correct value after
8739 * modifying it.
8740 * The argument is a softfloat float_round_ value.
8741 */
8742uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env)
8743{
8744 float_status *fp_status = &env->vfp.standard_fp_status;
8745
8746 uint32_t prev_rmode = get_float_rounding_mode(fp_status);
8747 set_float_rounding_mode(rmode, fp_status);
8748
8749 return prev_rmode;
8750}
8751
Paul Brook60011492009-11-19 16:45:20 +00008752/* Half precision conversions. */
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008753static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00008754{
Paul Brook60011492009-11-19 16:45:20 +00008755 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00008756 float32 r = float16_to_float32(make_float16(a), ieee, s);
8757 if (ieee) {
8758 return float32_maybe_silence_nan(r);
8759 }
8760 return r;
Paul Brook60011492009-11-19 16:45:20 +00008761}
8762
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008763static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s)
Paul Brook60011492009-11-19 16:45:20 +00008764{
Paul Brook60011492009-11-19 16:45:20 +00008765 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
Peter Maydellfb916782011-02-10 11:29:00 +00008766 float16 r = float32_to_float16(a, ieee, s);
8767 if (ieee) {
8768 r = float16_maybe_silence_nan(r);
8769 }
8770 return float16_val(r);
Paul Brook60011492009-11-19 16:45:20 +00008771}
8772
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008773float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00008774{
8775 return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status);
8776}
8777
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008778uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00008779{
8780 return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status);
8781}
8782
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008783float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00008784{
8785 return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status);
8786}
8787
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008788uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env)
Peter Maydell2d981da2011-02-10 11:29:01 +00008789{
8790 return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status);
8791}
8792
Peter Maydell8900aad2014-01-07 17:19:15 +00008793float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env)
8794{
8795 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8796 float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status);
8797 if (ieee) {
8798 return float64_maybe_silence_nan(r);
8799 }
8800 return r;
8801}
8802
8803uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env)
8804{
8805 int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0;
8806 float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status);
8807 if (ieee) {
8808 r = float16_maybe_silence_nan(r);
8809 }
8810 return float16_val(r);
8811}
8812
Peter Maydelldda3ec42011-03-14 15:37:12 +00008813#define float32_two make_float32(0x40000000)
Peter Maydell6aae3df2011-03-14 15:37:13 +00008814#define float32_three make_float32(0x40400000)
8815#define float32_one_point_five make_float32(0x3fc00000)
Peter Maydelldda3ec42011-03-14 15:37:12 +00008816
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008817float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00008818{
Peter Maydelldda3ec42011-03-14 15:37:12 +00008819 float_status *s = &env->vfp.standard_fp_status;
8820 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8821 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01008822 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8823 float_raise(float_flag_input_denormal, s);
8824 }
Peter Maydelldda3ec42011-03-14 15:37:12 +00008825 return float32_two;
8826 }
8827 return float32_sub(float32_two, float32_mul(a, b, s), s);
pbrook4373f3c2008-03-31 03:47:19 +00008828}
8829
Andreas Färber0ecb72a2012-03-14 01:38:21 +01008830float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env)
pbrook4373f3c2008-03-31 03:47:19 +00008831{
Peter Maydell71826962011-01-14 20:39:18 +01008832 float_status *s = &env->vfp.standard_fp_status;
Peter Maydell9ea62f52011-01-14 20:39:18 +01008833 float32 product;
8834 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) ||
8835 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) {
Peter Maydell43fe9bd2011-05-19 14:46:15 +01008836 if (!(float32_is_zero(a) || float32_is_zero(b))) {
8837 float_raise(float_flag_input_denormal, s);
8838 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00008839 return float32_one_point_five;
Peter Maydell9ea62f52011-01-14 20:39:18 +01008840 }
Peter Maydell6aae3df2011-03-14 15:37:13 +00008841 product = float32_mul(a, b, s);
8842 return float32_div(float32_sub(float32_three, product, s), float32_two, s);
pbrook4373f3c2008-03-31 03:47:19 +00008843}
8844
pbrook8f8e3aa2008-03-31 03:48:01 +00008845/* NEON helpers. */
8846
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01008847/* Constants 256 and 512 are used in some helpers; we avoid relying on
8848 * int->float conversions at run-time. */
8849#define float64_256 make_float64(0x4070000000000000LL)
8850#define float64_512 make_float64(0x4080000000000000LL)
Alex Bennéeb6d44432014-03-17 16:31:52 +00008851#define float32_maxnorm make_float32(0x7f7fffff)
8852#define float64_maxnorm make_float64(0x7fefffffffffffffLL)
Christophe Lyon56bf4fe2011-02-21 17:38:46 +01008853
Alex Bennéeb6d44432014-03-17 16:31:52 +00008854/* Reciprocal functions
8855 *
8856 * The algorithm that must be used to calculate the estimate
8857 * is specified by the ARM ARM, see FPRecipEstimate()
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008858 */
Alex Bennéeb6d44432014-03-17 16:31:52 +00008859
8860static float64 recip_estimate(float64 a, float_status *real_fp_status)
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008861{
Peter Maydell1146a812011-05-19 14:46:14 +01008862 /* These calculations mustn't set any fp exception flags,
8863 * so we use a local copy of the fp_status.
8864 */
Alex Bennéeb6d44432014-03-17 16:31:52 +00008865 float_status dummy_status = *real_fp_status;
Peter Maydell1146a812011-05-19 14:46:14 +01008866 float_status *s = &dummy_status;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008867 /* q = (int)(a * 512.0) */
8868 float64 q = float64_mul(float64_512, a, s);
8869 int64_t q_int = float64_to_int64_round_to_zero(q, s);
8870
8871 /* r = 1.0 / (((double)q + 0.5) / 512.0) */
8872 q = int64_to_float64(q_int, s);
8873 q = float64_add(q, float64_half, s);
8874 q = float64_div(q, float64_512, s);
8875 q = float64_div(float64_one, q, s);
8876
8877 /* s = (int)(256.0 * r + 0.5) */
8878 q = float64_mul(q, float64_256, s);
8879 q = float64_add(q, float64_half, s);
8880 q_int = float64_to_int64_round_to_zero(q, s);
8881
8882 /* return (double)s / 256.0 */
8883 return float64_div(int64_to_float64(q_int, s), float64_256, s);
8884}
8885
Alex Bennéeb6d44432014-03-17 16:31:52 +00008886/* Common wrapper to call recip_estimate */
8887static float64 call_recip_estimate(float64 num, int off, float_status *fpst)
pbrook4373f3c2008-03-31 03:47:19 +00008888{
Alex Bennéeb6d44432014-03-17 16:31:52 +00008889 uint64_t val64 = float64_val(num);
8890 uint64_t frac = extract64(val64, 0, 52);
8891 int64_t exp = extract64(val64, 52, 11);
8892 uint64_t sbit;
8893 float64 scaled, estimate;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008894
Alex Bennéeb6d44432014-03-17 16:31:52 +00008895 /* Generate the scaled number for the estimate function */
8896 if (exp == 0) {
8897 if (extract64(frac, 51, 1) == 0) {
8898 exp = -1;
8899 frac = extract64(frac, 0, 50) << 2;
8900 } else {
8901 frac = extract64(frac, 0, 51) << 1;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008902 }
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008903 }
8904
Alex Bennéeb6d44432014-03-17 16:31:52 +00008905 /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */
8906 scaled = make_float64((0x3feULL << 52)
8907 | extract64(frac, 44, 8) << 44);
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008908
Alex Bennéeb6d44432014-03-17 16:31:52 +00008909 estimate = recip_estimate(scaled, fpst);
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008910
Alex Bennéeb6d44432014-03-17 16:31:52 +00008911 /* Build new result */
8912 val64 = float64_val(estimate);
8913 sbit = 0x8000000000000000ULL & val64;
8914 exp = off - exp;
8915 frac = extract64(val64, 0, 52);
Christophe Lyonfe0e4872011-02-21 17:38:47 +01008916
Alex Bennéeb6d44432014-03-17 16:31:52 +00008917 if (exp == 0) {
8918 frac = 1ULL << 51 | extract64(frac, 1, 51);
8919 } else if (exp == -1) {
8920 frac = 1ULL << 50 | extract64(frac, 2, 50);
8921 exp = 0;
8922 }
8923
8924 return make_float64(sbit | (exp << 52) | frac);
8925}
8926
8927static bool round_to_inf(float_status *fpst, bool sign_bit)
8928{
8929 switch (fpst->float_rounding_mode) {
8930 case float_round_nearest_even: /* Round to Nearest */
8931 return true;
8932 case float_round_up: /* Round to +Inf */
8933 return !sign_bit;
8934 case float_round_down: /* Round to -Inf */
8935 return sign_bit;
8936 case float_round_to_zero: /* Round to Zero */
8937 return false;
8938 }
8939
8940 g_assert_not_reached();
8941}
8942
8943float32 HELPER(recpe_f32)(float32 input, void *fpstp)
8944{
8945 float_status *fpst = fpstp;
8946 float32 f32 = float32_squash_input_denormal(input, fpst);
8947 uint32_t f32_val = float32_val(f32);
8948 uint32_t f32_sbit = 0x80000000ULL & f32_val;
8949 int32_t f32_exp = extract32(f32_val, 23, 8);
8950 uint32_t f32_frac = extract32(f32_val, 0, 23);
8951 float64 f64, r64;
8952 uint64_t r64_val;
8953 int64_t r64_exp;
8954 uint64_t r64_frac;
8955
8956 if (float32_is_any_nan(f32)) {
8957 float32 nan = f32;
8958 if (float32_is_signaling_nan(f32)) {
8959 float_raise(float_flag_invalid, fpst);
8960 nan = float32_maybe_silence_nan(f32);
8961 }
8962 if (fpst->default_nan_mode) {
8963 nan = float32_default_nan;
8964 }
8965 return nan;
8966 } else if (float32_is_infinity(f32)) {
8967 return float32_set_sign(float32_zero, float32_is_neg(f32));
8968 } else if (float32_is_zero(f32)) {
8969 float_raise(float_flag_divbyzero, fpst);
8970 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8971 } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) {
8972 /* Abs(value) < 2.0^-128 */
8973 float_raise(float_flag_overflow | float_flag_inexact, fpst);
8974 if (round_to_inf(fpst, f32_sbit)) {
8975 return float32_set_sign(float32_infinity, float32_is_neg(f32));
8976 } else {
8977 return float32_set_sign(float32_maxnorm, float32_is_neg(f32));
8978 }
8979 } else if (f32_exp >= 253 && fpst->flush_to_zero) {
8980 float_raise(float_flag_underflow, fpst);
8981 return float32_set_sign(float32_zero, float32_is_neg(f32));
8982 }
8983
8984
8985 f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29);
8986 r64 = call_recip_estimate(f64, 253, fpst);
8987 r64_val = float64_val(r64);
8988 r64_exp = extract64(r64_val, 52, 11);
8989 r64_frac = extract64(r64_val, 0, 52);
8990
8991 /* result = sign : result_exp<7:0> : fraction<51:29>; */
8992 return make_float32(f32_sbit |
8993 (r64_exp & 0xff) << 23 |
8994 extract64(r64_frac, 29, 24));
8995}
8996
8997float64 HELPER(recpe_f64)(float64 input, void *fpstp)
8998{
8999 float_status *fpst = fpstp;
9000 float64 f64 = float64_squash_input_denormal(input, fpst);
9001 uint64_t f64_val = float64_val(f64);
9002 uint64_t f64_sbit = 0x8000000000000000ULL & f64_val;
9003 int64_t f64_exp = extract64(f64_val, 52, 11);
9004 float64 r64;
9005 uint64_t r64_val;
9006 int64_t r64_exp;
9007 uint64_t r64_frac;
9008
9009 /* Deal with any special cases */
9010 if (float64_is_any_nan(f64)) {
9011 float64 nan = f64;
9012 if (float64_is_signaling_nan(f64)) {
9013 float_raise(float_flag_invalid, fpst);
9014 nan = float64_maybe_silence_nan(f64);
9015 }
9016 if (fpst->default_nan_mode) {
9017 nan = float64_default_nan;
9018 }
9019 return nan;
9020 } else if (float64_is_infinity(f64)) {
9021 return float64_set_sign(float64_zero, float64_is_neg(f64));
9022 } else if (float64_is_zero(f64)) {
9023 float_raise(float_flag_divbyzero, fpst);
9024 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9025 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) {
9026 /* Abs(value) < 2.0^-1024 */
9027 float_raise(float_flag_overflow | float_flag_inexact, fpst);
9028 if (round_to_inf(fpst, f64_sbit)) {
9029 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9030 } else {
9031 return float64_set_sign(float64_maxnorm, float64_is_neg(f64));
9032 }
Ildar Isaevfc1792e2015-02-05 13:37:25 +00009033 } else if (f64_exp >= 2045 && fpst->flush_to_zero) {
Alex Bennéeb6d44432014-03-17 16:31:52 +00009034 float_raise(float_flag_underflow, fpst);
9035 return float64_set_sign(float64_zero, float64_is_neg(f64));
9036 }
9037
9038 r64 = call_recip_estimate(f64, 2045, fpst);
9039 r64_val = float64_val(r64);
9040 r64_exp = extract64(r64_val, 52, 11);
9041 r64_frac = extract64(r64_val, 0, 52);
9042
9043 /* result = sign : result_exp<10:0> : fraction<51:0> */
9044 return make_float64(f64_sbit |
9045 ((r64_exp & 0x7ff) << 52) |
9046 r64_frac);
pbrook4373f3c2008-03-31 03:47:19 +00009047}
9048
Christophe Lyone07be5d2011-02-21 17:38:48 +01009049/* The algorithm that must be used to calculate the estimate
9050 * is specified by the ARM ARM.
9051 */
Alex Bennéec2fb4182014-03-17 16:31:53 +00009052static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status)
Christophe Lyone07be5d2011-02-21 17:38:48 +01009053{
Peter Maydell1146a812011-05-19 14:46:14 +01009054 /* These calculations mustn't set any fp exception flags,
9055 * so we use a local copy of the fp_status.
9056 */
Alex Bennéec2fb4182014-03-17 16:31:53 +00009057 float_status dummy_status = *real_fp_status;
Peter Maydell1146a812011-05-19 14:46:14 +01009058 float_status *s = &dummy_status;
Christophe Lyone07be5d2011-02-21 17:38:48 +01009059 float64 q;
9060 int64_t q_int;
9061
9062 if (float64_lt(a, float64_half, s)) {
9063 /* range 0.25 <= a < 0.5 */
9064
9065 /* a in units of 1/512 rounded down */
9066 /* q0 = (int)(a * 512.0); */
9067 q = float64_mul(float64_512, a, s);
9068 q_int = float64_to_int64_round_to_zero(q, s);
9069
9070 /* reciprocal root r */
9071 /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */
9072 q = int64_to_float64(q_int, s);
9073 q = float64_add(q, float64_half, s);
9074 q = float64_div(q, float64_512, s);
9075 q = float64_sqrt(q, s);
9076 q = float64_div(float64_one, q, s);
9077 } else {
9078 /* range 0.5 <= a < 1.0 */
9079
9080 /* a in units of 1/256 rounded down */
9081 /* q1 = (int)(a * 256.0); */
9082 q = float64_mul(float64_256, a, s);
9083 int64_t q_int = float64_to_int64_round_to_zero(q, s);
9084
9085 /* reciprocal root r */
9086 /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */
9087 q = int64_to_float64(q_int, s);
9088 q = float64_add(q, float64_half, s);
9089 q = float64_div(q, float64_256, s);
9090 q = float64_sqrt(q, s);
9091 q = float64_div(float64_one, q, s);
9092 }
9093 /* r in units of 1/256 rounded to nearest */
9094 /* s = (int)(256.0 * r + 0.5); */
9095
9096 q = float64_mul(q, float64_256,s );
9097 q = float64_add(q, float64_half, s);
9098 q_int = float64_to_int64_round_to_zero(q, s);
9099
9100 /* return (double)s / 256.0;*/
9101 return float64_div(int64_to_float64(q_int, s), float64_256, s);
9102}
9103
Alex Bennéec2fb4182014-03-17 16:31:53 +00009104float32 HELPER(rsqrte_f32)(float32 input, void *fpstp)
pbrook4373f3c2008-03-31 03:47:19 +00009105{
Alex Bennéec2fb4182014-03-17 16:31:53 +00009106 float_status *s = fpstp;
9107 float32 f32 = float32_squash_input_denormal(input, s);
9108 uint32_t val = float32_val(f32);
9109 uint32_t f32_sbit = 0x80000000 & val;
9110 int32_t f32_exp = extract32(val, 23, 8);
9111 uint32_t f32_frac = extract32(val, 0, 23);
9112 uint64_t f64_frac;
9113 uint64_t val64;
Christophe Lyone07be5d2011-02-21 17:38:48 +01009114 int result_exp;
9115 float64 f64;
Christophe Lyone07be5d2011-02-21 17:38:48 +01009116
Alex Bennéec2fb4182014-03-17 16:31:53 +00009117 if (float32_is_any_nan(f32)) {
9118 float32 nan = f32;
9119 if (float32_is_signaling_nan(f32)) {
Christophe Lyone07be5d2011-02-21 17:38:48 +01009120 float_raise(float_flag_invalid, s);
Alex Bennéec2fb4182014-03-17 16:31:53 +00009121 nan = float32_maybe_silence_nan(f32);
Christophe Lyone07be5d2011-02-21 17:38:48 +01009122 }
Alex Bennéec2fb4182014-03-17 16:31:53 +00009123 if (s->default_nan_mode) {
9124 nan = float32_default_nan;
Peter Maydell43fe9bd2011-05-19 14:46:15 +01009125 }
Alex Bennéec2fb4182014-03-17 16:31:53 +00009126 return nan;
9127 } else if (float32_is_zero(f32)) {
Christophe Lyone07be5d2011-02-21 17:38:48 +01009128 float_raise(float_flag_divbyzero, s);
Alex Bennéec2fb4182014-03-17 16:31:53 +00009129 return float32_set_sign(float32_infinity, float32_is_neg(f32));
9130 } else if (float32_is_neg(f32)) {
Christophe Lyone07be5d2011-02-21 17:38:48 +01009131 float_raise(float_flag_invalid, s);
9132 return float32_default_nan;
Alex Bennéec2fb4182014-03-17 16:31:53 +00009133 } else if (float32_is_infinity(f32)) {
Christophe Lyone07be5d2011-02-21 17:38:48 +01009134 return float32_zero;
9135 }
9136
Alex Bennéec2fb4182014-03-17 16:31:53 +00009137 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
Christophe Lyone07be5d2011-02-21 17:38:48 +01009138 * preserving the parity of the exponent. */
Alex Bennéec2fb4182014-03-17 16:31:53 +00009139
9140 f64_frac = ((uint64_t) f32_frac) << 29;
9141 if (f32_exp == 0) {
9142 while (extract64(f64_frac, 51, 1) == 0) {
9143 f64_frac = f64_frac << 1;
9144 f32_exp = f32_exp-1;
9145 }
9146 f64_frac = extract64(f64_frac, 0, 51) << 1;
Christophe Lyone07be5d2011-02-21 17:38:48 +01009147 }
9148
Alex Bennéec2fb4182014-03-17 16:31:53 +00009149 if (extract64(f32_exp, 0, 1) == 0) {
9150 f64 = make_float64(((uint64_t) f32_sbit) << 32
9151 | (0x3feULL << 52)
9152 | f64_frac);
9153 } else {
9154 f64 = make_float64(((uint64_t) f32_sbit) << 32
9155 | (0x3fdULL << 52)
9156 | f64_frac);
9157 }
Christophe Lyone07be5d2011-02-21 17:38:48 +01009158
Alex Bennéec2fb4182014-03-17 16:31:53 +00009159 result_exp = (380 - f32_exp) / 2;
9160
9161 f64 = recip_sqrt_estimate(f64, s);
Christophe Lyone07be5d2011-02-21 17:38:48 +01009162
9163 val64 = float64_val(f64);
9164
Christophe LYON26cc6ab2011-10-19 16:14:05 +00009165 val = ((result_exp & 0xff) << 23)
Christophe Lyone07be5d2011-02-21 17:38:48 +01009166 | ((val64 >> 29) & 0x7fffff);
9167 return make_float32(val);
pbrook4373f3c2008-03-31 03:47:19 +00009168}
9169
Alex Bennéec2fb4182014-03-17 16:31:53 +00009170float64 HELPER(rsqrte_f64)(float64 input, void *fpstp)
9171{
9172 float_status *s = fpstp;
9173 float64 f64 = float64_squash_input_denormal(input, s);
9174 uint64_t val = float64_val(f64);
9175 uint64_t f64_sbit = 0x8000000000000000ULL & val;
9176 int64_t f64_exp = extract64(val, 52, 11);
9177 uint64_t f64_frac = extract64(val, 0, 52);
9178 int64_t result_exp;
9179 uint64_t result_frac;
9180
9181 if (float64_is_any_nan(f64)) {
9182 float64 nan = f64;
9183 if (float64_is_signaling_nan(f64)) {
9184 float_raise(float_flag_invalid, s);
9185 nan = float64_maybe_silence_nan(f64);
9186 }
9187 if (s->default_nan_mode) {
9188 nan = float64_default_nan;
9189 }
9190 return nan;
9191 } else if (float64_is_zero(f64)) {
9192 float_raise(float_flag_divbyzero, s);
9193 return float64_set_sign(float64_infinity, float64_is_neg(f64));
9194 } else if (float64_is_neg(f64)) {
9195 float_raise(float_flag_invalid, s);
9196 return float64_default_nan;
9197 } else if (float64_is_infinity(f64)) {
9198 return float64_zero;
9199 }
9200
9201 /* Scale and normalize to a double-precision value between 0.25 and 1.0,
9202 * preserving the parity of the exponent. */
9203
9204 if (f64_exp == 0) {
9205 while (extract64(f64_frac, 51, 1) == 0) {
9206 f64_frac = f64_frac << 1;
9207 f64_exp = f64_exp - 1;
9208 }
9209 f64_frac = extract64(f64_frac, 0, 51) << 1;
9210 }
9211
9212 if (extract64(f64_exp, 0, 1) == 0) {
9213 f64 = make_float64(f64_sbit
9214 | (0x3feULL << 52)
9215 | f64_frac);
9216 } else {
9217 f64 = make_float64(f64_sbit
9218 | (0x3fdULL << 52)
9219 | f64_frac);
9220 }
9221
9222 result_exp = (3068 - f64_exp) / 2;
9223
9224 f64 = recip_sqrt_estimate(f64, s);
9225
9226 result_frac = extract64(float64_val(f64), 0, 52);
9227
9228 return make_float64(f64_sbit |
9229 ((result_exp & 0x7ff) << 52) |
9230 result_frac);
9231}
9232
Alex Bennéeb6d44432014-03-17 16:31:52 +00009233uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp)
pbrook4373f3c2008-03-31 03:47:19 +00009234{
Alex Bennéeb6d44432014-03-17 16:31:52 +00009235 float_status *s = fpstp;
Christophe Lyonfe0e4872011-02-21 17:38:47 +01009236 float64 f64;
9237
9238 if ((a & 0x80000000) == 0) {
9239 return 0xffffffff;
9240 }
9241
9242 f64 = make_float64((0x3feULL << 52)
9243 | ((int64_t)(a & 0x7fffffff) << 21));
9244
Alex Bennéeb6d44432014-03-17 16:31:52 +00009245 f64 = recip_estimate(f64, s);
Christophe Lyonfe0e4872011-02-21 17:38:47 +01009246
9247 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00009248}
9249
Alex Bennéec2fb4182014-03-17 16:31:53 +00009250uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp)
pbrook4373f3c2008-03-31 03:47:19 +00009251{
Alex Bennéec2fb4182014-03-17 16:31:53 +00009252 float_status *fpst = fpstp;
Christophe Lyone07be5d2011-02-21 17:38:48 +01009253 float64 f64;
9254
9255 if ((a & 0xc0000000) == 0) {
9256 return 0xffffffff;
9257 }
9258
9259 if (a & 0x80000000) {
9260 f64 = make_float64((0x3feULL << 52)
9261 | ((uint64_t)(a & 0x7fffffff) << 21));
9262 } else { /* bits 31-30 == '01' */
9263 f64 = make_float64((0x3fdULL << 52)
9264 | ((uint64_t)(a & 0x3fffffff) << 22));
9265 }
9266
Alex Bennéec2fb4182014-03-17 16:31:53 +00009267 f64 = recip_sqrt_estimate(f64, fpst);
Christophe Lyone07be5d2011-02-21 17:38:48 +01009268
9269 return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff);
pbrook4373f3c2008-03-31 03:47:19 +00009270}
pbrookfe1479c2008-12-19 13:18:36 +00009271
Peter Maydellda97f522011-10-19 16:14:07 +00009272/* VFPv4 fused multiply-accumulate */
9273float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp)
9274{
9275 float_status *fpst = fpstp;
9276 return float32_muladd(a, b, c, 0, fpst);
9277}
9278
9279float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp)
9280{
9281 float_status *fpst = fpstp;
9282 return float64_muladd(a, b, c, 0, fpst);
9283}
Peter Maydelld9b08482014-01-07 17:19:14 +00009284
9285/* ARMv8 round to integral */
9286float32 HELPER(rints_exact)(float32 x, void *fp_status)
9287{
9288 return float32_round_to_int(x, fp_status);
9289}
9290
9291float64 HELPER(rintd_exact)(float64 x, void *fp_status)
9292{
9293 return float64_round_to_int(x, fp_status);
9294}
9295
9296float32 HELPER(rints)(float32 x, void *fp_status)
9297{
9298 int old_flags = get_float_exception_flags(fp_status), new_flags;
9299 float32 ret;
9300
9301 ret = float32_round_to_int(x, fp_status);
9302
9303 /* Suppress any inexact exceptions the conversion produced */
9304 if (!(old_flags & float_flag_inexact)) {
9305 new_flags = get_float_exception_flags(fp_status);
9306 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9307 }
9308
9309 return ret;
9310}
9311
9312float64 HELPER(rintd)(float64 x, void *fp_status)
9313{
9314 int old_flags = get_float_exception_flags(fp_status), new_flags;
9315 float64 ret;
9316
9317 ret = float64_round_to_int(x, fp_status);
9318
9319 new_flags = get_float_exception_flags(fp_status);
9320
9321 /* Suppress any inexact exceptions the conversion produced */
9322 if (!(old_flags & float_flag_inexact)) {
9323 new_flags = get_float_exception_flags(fp_status);
9324 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status);
9325 }
9326
9327 return ret;
9328}
Will Newton9972da62014-01-31 14:47:33 +00009329
9330/* Convert ARM rounding mode to softfloat */
9331int arm_rmode_to_sf(int rmode)
9332{
9333 switch (rmode) {
9334 case FPROUNDING_TIEAWAY:
9335 rmode = float_round_ties_away;
9336 break;
9337 case FPROUNDING_ODD:
9338 /* FIXME: add support for TIEAWAY and ODD */
9339 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n",
9340 rmode);
9341 case FPROUNDING_TIEEVEN:
9342 default:
9343 rmode = float_round_nearest_even;
9344 break;
9345 case FPROUNDING_POSINF:
9346 rmode = float_round_up;
9347 break;
9348 case FPROUNDING_NEGINF:
9349 rmode = float_round_down;
9350 break;
9351 case FPROUNDING_ZERO:
9352 rmode = float_round_to_zero;
9353 break;
9354 }
9355 return rmode;
9356}
Will Newtoneb0ecd52014-02-26 17:20:07 +00009357
Peter Maydellaa633462014-06-09 15:43:25 +01009358/* CRC helpers.
9359 * The upper bytes of val (above the number specified by 'bytes') must have
9360 * been zeroed out by the caller.
9361 */
Will Newtoneb0ecd52014-02-26 17:20:07 +00009362uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes)
9363{
9364 uint8_t buf[4];
9365
Peter Maydellaa633462014-06-09 15:43:25 +01009366 stl_le_p(buf, val);
Will Newtoneb0ecd52014-02-26 17:20:07 +00009367
9368 /* zlib crc32 converts the accumulator and output to one's complement. */
9369 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff;
9370}
9371
9372uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes)
9373{
9374 uint8_t buf[4];
9375
Peter Maydellaa633462014-06-09 15:43:25 +01009376 stl_le_p(buf, val);
Will Newtoneb0ecd52014-02-26 17:20:07 +00009377
9378 /* Linux crc32c converts the output to one's complement. */
9379 return crc32c(acc, buf, bytes) ^ 0xffffffff;
9380}