]>
Commit | Line | Data |
---|---|---|
74c21bd0 | 1 | #include "qemu/osdep.h" |
194cbc49 | 2 | #include "trace.h" |
b5ff1b31 | 3 | #include "cpu.h" |
ccd38087 | 4 | #include "internals.h" |
022c62cb | 5 | #include "exec/gdbstub.h" |
2ef6175a | 6 | #include "exec/helper-proto.h" |
1de7afc9 | 7 | #include "qemu/host-utils.h" |
78027bb6 | 8 | #include "sysemu/arch_init.h" |
9c17d615 | 9 | #include "sysemu/sysemu.h" |
1de7afc9 | 10 | #include "qemu/bitops.h" |
eb0ecd5a | 11 | #include "qemu/crc32c.h" |
63c91552 | 12 | #include "exec/exec-all.h" |
f08b6170 | 13 | #include "exec/cpu_ldst.h" |
1d854765 | 14 | #include "arm_ldst.h" |
eb0ecd5a | 15 | #include <zlib.h> /* For crc32 */ |
cfe67cef | 16 | #include "exec/semihost.h" |
f3a9b694 | 17 | #include "sysemu/kvm.h" |
0b03bdfc | 18 | |
352c98e5 LV |
19 | #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */ |
20 | ||
4a501606 | 21 | #ifndef CONFIG_USER_ONLY |
af51f566 EI |
22 | static bool get_phys_addr(CPUARMState *env, target_ulong address, |
23 | int access_type, ARMMMUIdx mmu_idx, | |
24 | hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, | |
e14b5a23 EI |
25 | target_ulong *page_size, uint32_t *fsr, |
26 | ARMMMUFaultInfo *fi); | |
7c2cb42b | 27 | |
37785977 EI |
28 | static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, |
29 | int access_type, ARMMMUIdx mmu_idx, | |
30 | hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, | |
31 | target_ulong *page_size_ptr, uint32_t *fsr, | |
32 | ARMMMUFaultInfo *fi); | |
33 | ||
7c2cb42b AF |
34 | /* Definitions for the PMCCNTR and PMCR registers */ |
35 | #define PMCRD 0x8 | |
36 | #define PMCRC 0x4 | |
37 | #define PMCRE 0x1 | |
4a501606 PM |
38 | #endif |
39 | ||
0ecb72a5 | 40 | static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) |
56aebc89 PB |
41 | { |
42 | int nregs; | |
43 | ||
44 | /* VFP data registers are always little-endian. */ | |
45 | nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; | |
46 | if (reg < nregs) { | |
47 | stfq_le_p(buf, env->vfp.regs[reg]); | |
48 | return 8; | |
49 | } | |
50 | if (arm_feature(env, ARM_FEATURE_NEON)) { | |
51 | /* Aliases for Q regs. */ | |
52 | nregs += 16; | |
53 | if (reg < nregs) { | |
54 | stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]); | |
55 | stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]); | |
56 | return 16; | |
57 | } | |
58 | } | |
59 | switch (reg - nregs) { | |
60 | case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4; | |
61 | case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4; | |
62 | case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4; | |
63 | } | |
64 | return 0; | |
65 | } | |
66 | ||
0ecb72a5 | 67 | static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) |
56aebc89 PB |
68 | { |
69 | int nregs; | |
70 | ||
71 | nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; | |
72 | if (reg < nregs) { | |
73 | env->vfp.regs[reg] = ldfq_le_p(buf); | |
74 | return 8; | |
75 | } | |
76 | if (arm_feature(env, ARM_FEATURE_NEON)) { | |
77 | nregs += 16; | |
78 | if (reg < nregs) { | |
79 | env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf); | |
80 | env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8); | |
81 | return 16; | |
82 | } | |
83 | } | |
84 | switch (reg - nregs) { | |
85 | case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; | |
86 | case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4; | |
71b3c3de | 87 | case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; |
56aebc89 PB |
88 | } |
89 | return 0; | |
90 | } | |
91 | ||
6a669427 PM |
92 | static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) |
93 | { | |
94 | switch (reg) { | |
95 | case 0 ... 31: | |
96 | /* 128 bit FP register */ | |
97 | stfq_le_p(buf, env->vfp.regs[reg * 2]); | |
98 | stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]); | |
99 | return 16; | |
100 | case 32: | |
101 | /* FPSR */ | |
102 | stl_p(buf, vfp_get_fpsr(env)); | |
103 | return 4; | |
104 | case 33: | |
105 | /* FPCR */ | |
106 | stl_p(buf, vfp_get_fpcr(env)); | |
107 | return 4; | |
108 | default: | |
109 | return 0; | |
110 | } | |
111 | } | |
112 | ||
113 | static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) | |
114 | { | |
115 | switch (reg) { | |
116 | case 0 ... 31: | |
117 | /* 128 bit FP register */ | |
118 | env->vfp.regs[reg * 2] = ldfq_le_p(buf); | |
119 | env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8); | |
120 | return 16; | |
121 | case 32: | |
122 | /* FPSR */ | |
123 | vfp_set_fpsr(env, ldl_p(buf)); | |
124 | return 4; | |
125 | case 33: | |
126 | /* FPCR */ | |
127 | vfp_set_fpcr(env, ldl_p(buf)); | |
128 | return 4; | |
129 | default: | |
130 | return 0; | |
131 | } | |
132 | } | |
133 | ||
c4241c7d | 134 | static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) |
d4e6df63 | 135 | { |
375421cc | 136 | assert(ri->fieldoffset); |
67ed771d | 137 | if (cpreg_field_is_64bit(ri)) { |
c4241c7d | 138 | return CPREG_FIELD64(env, ri); |
22d9e1a9 | 139 | } else { |
c4241c7d | 140 | return CPREG_FIELD32(env, ri); |
22d9e1a9 | 141 | } |
d4e6df63 PM |
142 | } |
143 | ||
c4241c7d PM |
144 | static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, |
145 | uint64_t value) | |
d4e6df63 | 146 | { |
375421cc | 147 | assert(ri->fieldoffset); |
67ed771d | 148 | if (cpreg_field_is_64bit(ri)) { |
22d9e1a9 PM |
149 | CPREG_FIELD64(env, ri) = value; |
150 | } else { | |
151 | CPREG_FIELD32(env, ri) = value; | |
152 | } | |
d4e6df63 PM |
153 | } |
154 | ||
11f136ee FA |
155 | static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) |
156 | { | |
157 | return (char *)env + ri->fieldoffset; | |
158 | } | |
159 | ||
49a66191 | 160 | uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) |
721fae12 | 161 | { |
59a1c327 | 162 | /* Raw read of a coprocessor register (as needed for migration, etc). */ |
721fae12 | 163 | if (ri->type & ARM_CP_CONST) { |
59a1c327 | 164 | return ri->resetvalue; |
721fae12 | 165 | } else if (ri->raw_readfn) { |
59a1c327 | 166 | return ri->raw_readfn(env, ri); |
721fae12 | 167 | } else if (ri->readfn) { |
59a1c327 | 168 | return ri->readfn(env, ri); |
721fae12 | 169 | } else { |
59a1c327 | 170 | return raw_read(env, ri); |
721fae12 | 171 | } |
721fae12 PM |
172 | } |
173 | ||
59a1c327 | 174 | static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, |
7900e9f1 | 175 | uint64_t v) |
721fae12 PM |
176 | { |
177 | /* Raw write of a coprocessor register (as needed for migration, etc). | |
721fae12 PM |
178 | * Note that constant registers are treated as write-ignored; the |
179 | * caller should check for success by whether a readback gives the | |
180 | * value written. | |
181 | */ | |
182 | if (ri->type & ARM_CP_CONST) { | |
59a1c327 | 183 | return; |
721fae12 | 184 | } else if (ri->raw_writefn) { |
c4241c7d | 185 | ri->raw_writefn(env, ri, v); |
721fae12 | 186 | } else if (ri->writefn) { |
c4241c7d | 187 | ri->writefn(env, ri, v); |
721fae12 | 188 | } else { |
afb2530f | 189 | raw_write(env, ri, v); |
721fae12 | 190 | } |
721fae12 PM |
191 | } |
192 | ||
375421cc PM |
193 | static bool raw_accessors_invalid(const ARMCPRegInfo *ri) |
194 | { | |
195 | /* Return true if the regdef would cause an assertion if you called | |
196 | * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a | |
197 | * program bug for it not to have the NO_RAW flag). | |
198 | * NB that returning false here doesn't necessarily mean that calling | |
199 | * read/write_raw_cp_reg() is safe, because we can't distinguish "has | |
200 | * read/write access functions which are safe for raw use" from "has | |
201 | * read/write access functions which have side effects but has forgotten | |
202 | * to provide raw access functions". | |
203 | * The tests here line up with the conditions in read/write_raw_cp_reg() | |
204 | * and assertions in raw_read()/raw_write(). | |
205 | */ | |
206 | if ((ri->type & ARM_CP_CONST) || | |
207 | ri->fieldoffset || | |
208 | ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { | |
209 | return false; | |
210 | } | |
211 | return true; | |
212 | } | |
213 | ||
721fae12 PM |
214 | bool write_cpustate_to_list(ARMCPU *cpu) |
215 | { | |
216 | /* Write the coprocessor state from cpu->env to the (index,value) list. */ | |
217 | int i; | |
218 | bool ok = true; | |
219 | ||
220 | for (i = 0; i < cpu->cpreg_array_len; i++) { | |
221 | uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); | |
222 | const ARMCPRegInfo *ri; | |
59a1c327 | 223 | |
60322b39 | 224 | ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); |
721fae12 PM |
225 | if (!ri) { |
226 | ok = false; | |
227 | continue; | |
228 | } | |
7a0e58fa | 229 | if (ri->type & ARM_CP_NO_RAW) { |
721fae12 PM |
230 | continue; |
231 | } | |
59a1c327 | 232 | cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri); |
721fae12 PM |
233 | } |
234 | return ok; | |
235 | } | |
236 | ||
237 | bool write_list_to_cpustate(ARMCPU *cpu) | |
238 | { | |
239 | int i; | |
240 | bool ok = true; | |
241 | ||
242 | for (i = 0; i < cpu->cpreg_array_len; i++) { | |
243 | uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); | |
244 | uint64_t v = cpu->cpreg_values[i]; | |
721fae12 PM |
245 | const ARMCPRegInfo *ri; |
246 | ||
60322b39 | 247 | ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); |
721fae12 PM |
248 | if (!ri) { |
249 | ok = false; | |
250 | continue; | |
251 | } | |
7a0e58fa | 252 | if (ri->type & ARM_CP_NO_RAW) { |
721fae12 PM |
253 | continue; |
254 | } | |
255 | /* Write value and confirm it reads back as written | |
256 | * (to catch read-only registers and partially read-only | |
257 | * registers where the incoming migration value doesn't match) | |
258 | */ | |
59a1c327 PM |
259 | write_raw_cp_reg(&cpu->env, ri, v); |
260 | if (read_raw_cp_reg(&cpu->env, ri) != v) { | |
721fae12 PM |
261 | ok = false; |
262 | } | |
263 | } | |
264 | return ok; | |
265 | } | |
266 | ||
267 | static void add_cpreg_to_list(gpointer key, gpointer opaque) | |
268 | { | |
269 | ARMCPU *cpu = opaque; | |
270 | uint64_t regidx; | |
271 | const ARMCPRegInfo *ri; | |
272 | ||
273 | regidx = *(uint32_t *)key; | |
60322b39 | 274 | ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); |
721fae12 | 275 | |
7a0e58fa | 276 | if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { |
721fae12 PM |
277 | cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); |
278 | /* The value array need not be initialized at this point */ | |
279 | cpu->cpreg_array_len++; | |
280 | } | |
281 | } | |
282 | ||
283 | static void count_cpreg(gpointer key, gpointer opaque) | |
284 | { | |
285 | ARMCPU *cpu = opaque; | |
286 | uint64_t regidx; | |
287 | const ARMCPRegInfo *ri; | |
288 | ||
289 | regidx = *(uint32_t *)key; | |
60322b39 | 290 | ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); |
721fae12 | 291 | |
7a0e58fa | 292 | if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { |
721fae12 PM |
293 | cpu->cpreg_array_len++; |
294 | } | |
295 | } | |
296 | ||
297 | static gint cpreg_key_compare(gconstpointer a, gconstpointer b) | |
298 | { | |
cbf239b7 AR |
299 | uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a); |
300 | uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b); | |
721fae12 | 301 | |
cbf239b7 AR |
302 | if (aidx > bidx) { |
303 | return 1; | |
304 | } | |
305 | if (aidx < bidx) { | |
306 | return -1; | |
307 | } | |
308 | return 0; | |
721fae12 PM |
309 | } |
310 | ||
311 | void init_cpreg_list(ARMCPU *cpu) | |
312 | { | |
313 | /* Initialise the cpreg_tuples[] array based on the cp_regs hash. | |
314 | * Note that we require cpreg_tuples[] to be sorted by key ID. | |
315 | */ | |
57b6d95e | 316 | GList *keys; |
721fae12 PM |
317 | int arraylen; |
318 | ||
57b6d95e | 319 | keys = g_hash_table_get_keys(cpu->cp_regs); |
721fae12 PM |
320 | keys = g_list_sort(keys, cpreg_key_compare); |
321 | ||
322 | cpu->cpreg_array_len = 0; | |
323 | ||
324 | g_list_foreach(keys, count_cpreg, cpu); | |
325 | ||
326 | arraylen = cpu->cpreg_array_len; | |
327 | cpu->cpreg_indexes = g_new(uint64_t, arraylen); | |
328 | cpu->cpreg_values = g_new(uint64_t, arraylen); | |
329 | cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); | |
330 | cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); | |
331 | cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; | |
332 | cpu->cpreg_array_len = 0; | |
333 | ||
334 | g_list_foreach(keys, add_cpreg_to_list, cpu); | |
335 | ||
336 | assert(cpu->cpreg_array_len == arraylen); | |
337 | ||
338 | g_list_free(keys); | |
339 | } | |
340 | ||
68e9c2fe EI |
341 | /* |
342 | * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but | |
343 | * they are accessible when EL3 is using AArch64 regardless of EL3.NS. | |
344 | * | |
345 | * access_el3_aa32ns: Used to check AArch32 register views. | |
346 | * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views. | |
347 | */ | |
348 | static CPAccessResult access_el3_aa32ns(CPUARMState *env, | |
3f208fd7 PM |
349 | const ARMCPRegInfo *ri, |
350 | bool isread) | |
68e9c2fe EI |
351 | { |
352 | bool secure = arm_is_secure_below_el3(env); | |
353 | ||
354 | assert(!arm_el_is_aa64(env, 3)); | |
355 | if (secure) { | |
356 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
357 | } | |
358 | return CP_ACCESS_OK; | |
359 | } | |
360 | ||
361 | static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env, | |
3f208fd7 PM |
362 | const ARMCPRegInfo *ri, |
363 | bool isread) | |
68e9c2fe EI |
364 | { |
365 | if (!arm_el_is_aa64(env, 3)) { | |
3f208fd7 | 366 | return access_el3_aa32ns(env, ri, isread); |
68e9c2fe EI |
367 | } |
368 | return CP_ACCESS_OK; | |
369 | } | |
370 | ||
5513c3ab PM |
371 | /* Some secure-only AArch32 registers trap to EL3 if used from |
372 | * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts). | |
373 | * Note that an access from Secure EL1 can only happen if EL3 is AArch64. | |
374 | * We assume that the .access field is set to PL1_RW. | |
375 | */ | |
376 | static CPAccessResult access_trap_aa32s_el1(CPUARMState *env, | |
3f208fd7 PM |
377 | const ARMCPRegInfo *ri, |
378 | bool isread) | |
5513c3ab PM |
379 | { |
380 | if (arm_current_el(env) == 3) { | |
381 | return CP_ACCESS_OK; | |
382 | } | |
383 | if (arm_is_secure_below_el3(env)) { | |
384 | return CP_ACCESS_TRAP_EL3; | |
385 | } | |
386 | /* This will be EL1 NS and EL2 NS, which just UNDEF */ | |
387 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
388 | } | |
389 | ||
187f678d PM |
390 | /* Check for traps to "powerdown debug" registers, which are controlled |
391 | * by MDCR.TDOSA | |
392 | */ | |
393 | static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri, | |
394 | bool isread) | |
395 | { | |
396 | int el = arm_current_el(env); | |
397 | ||
398 | if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA) | |
399 | && !arm_is_secure_below_el3(env)) { | |
400 | return CP_ACCESS_TRAP_EL2; | |
401 | } | |
402 | if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) { | |
403 | return CP_ACCESS_TRAP_EL3; | |
404 | } | |
405 | return CP_ACCESS_OK; | |
406 | } | |
407 | ||
91b0a238 PM |
408 | /* Check for traps to "debug ROM" registers, which are controlled |
409 | * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3. | |
410 | */ | |
411 | static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri, | |
412 | bool isread) | |
413 | { | |
414 | int el = arm_current_el(env); | |
415 | ||
416 | if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA) | |
417 | && !arm_is_secure_below_el3(env)) { | |
418 | return CP_ACCESS_TRAP_EL2; | |
419 | } | |
420 | if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { | |
421 | return CP_ACCESS_TRAP_EL3; | |
422 | } | |
423 | return CP_ACCESS_OK; | |
424 | } | |
425 | ||
d6c8cf81 PM |
426 | /* Check for traps to general debug registers, which are controlled |
427 | * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3. | |
428 | */ | |
429 | static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri, | |
430 | bool isread) | |
431 | { | |
432 | int el = arm_current_el(env); | |
433 | ||
434 | if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA) | |
435 | && !arm_is_secure_below_el3(env)) { | |
436 | return CP_ACCESS_TRAP_EL2; | |
437 | } | |
438 | if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { | |
439 | return CP_ACCESS_TRAP_EL3; | |
440 | } | |
441 | return CP_ACCESS_OK; | |
442 | } | |
443 | ||
1fce1ba9 PM |
444 | /* Check for traps to performance monitor registers, which are controlled |
445 | * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3. | |
446 | */ | |
447 | static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri, | |
448 | bool isread) | |
449 | { | |
450 | int el = arm_current_el(env); | |
451 | ||
452 | if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) | |
453 | && !arm_is_secure_below_el3(env)) { | |
454 | return CP_ACCESS_TRAP_EL2; | |
455 | } | |
456 | if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { | |
457 | return CP_ACCESS_TRAP_EL3; | |
458 | } | |
459 | return CP_ACCESS_OK; | |
460 | } | |
461 | ||
c4241c7d | 462 | static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
c983fe6c | 463 | { |
00c8cb0a AF |
464 | ARMCPU *cpu = arm_env_get_cpu(env); |
465 | ||
8d5c773e | 466 | raw_write(env, ri, value); |
00c8cb0a | 467 | tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */ |
c983fe6c PM |
468 | } |
469 | ||
c4241c7d | 470 | static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
08de207b | 471 | { |
00c8cb0a AF |
472 | ARMCPU *cpu = arm_env_get_cpu(env); |
473 | ||
8d5c773e | 474 | if (raw_read(env, ri) != value) { |
08de207b PM |
475 | /* Unlike real hardware the qemu TLB uses virtual addresses, |
476 | * not modified virtual addresses, so this causes a TLB flush. | |
477 | */ | |
00c8cb0a | 478 | tlb_flush(CPU(cpu), 1); |
8d5c773e | 479 | raw_write(env, ri, value); |
08de207b | 480 | } |
08de207b | 481 | } |
c4241c7d PM |
482 | |
483 | static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
484 | uint64_t value) | |
08de207b | 485 | { |
00c8cb0a AF |
486 | ARMCPU *cpu = arm_env_get_cpu(env); |
487 | ||
8d5c773e | 488 | if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU) |
014406b5 | 489 | && !extended_addresses_enabled(env)) { |
08de207b PM |
490 | /* For VMSA (when not using the LPAE long descriptor page table |
491 | * format) this register includes the ASID, so do a TLB flush. | |
492 | * For PMSA it is purely a process ID and no action is needed. | |
493 | */ | |
00c8cb0a | 494 | tlb_flush(CPU(cpu), 1); |
08de207b | 495 | } |
8d5c773e | 496 | raw_write(env, ri, value); |
08de207b PM |
497 | } |
498 | ||
c4241c7d PM |
499 | static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, |
500 | uint64_t value) | |
d929823f PM |
501 | { |
502 | /* Invalidate all (TLBIALL) */ | |
00c8cb0a AF |
503 | ARMCPU *cpu = arm_env_get_cpu(env); |
504 | ||
505 | tlb_flush(CPU(cpu), 1); | |
d929823f PM |
506 | } |
507 | ||
c4241c7d PM |
508 | static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, |
509 | uint64_t value) | |
d929823f PM |
510 | { |
511 | /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ | |
31b030d4 AF |
512 | ARMCPU *cpu = arm_env_get_cpu(env); |
513 | ||
514 | tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); | |
d929823f PM |
515 | } |
516 | ||
c4241c7d PM |
517 | static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, |
518 | uint64_t value) | |
d929823f PM |
519 | { |
520 | /* Invalidate by ASID (TLBIASID) */ | |
00c8cb0a AF |
521 | ARMCPU *cpu = arm_env_get_cpu(env); |
522 | ||
523 | tlb_flush(CPU(cpu), value == 0); | |
d929823f PM |
524 | } |
525 | ||
c4241c7d PM |
526 | static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, |
527 | uint64_t value) | |
d929823f PM |
528 | { |
529 | /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ | |
31b030d4 AF |
530 | ARMCPU *cpu = arm_env_get_cpu(env); |
531 | ||
532 | tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); | |
d929823f PM |
533 | } |
534 | ||
fa439fc5 PM |
535 | /* IS variants of TLB operations must affect all cores */ |
536 | static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
537 | uint64_t value) | |
538 | { | |
539 | CPUState *other_cs; | |
540 | ||
541 | CPU_FOREACH(other_cs) { | |
542 | tlb_flush(other_cs, 1); | |
543 | } | |
544 | } | |
545 | ||
546 | static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
547 | uint64_t value) | |
548 | { | |
549 | CPUState *other_cs; | |
550 | ||
551 | CPU_FOREACH(other_cs) { | |
552 | tlb_flush(other_cs, value == 0); | |
553 | } | |
554 | } | |
555 | ||
556 | static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
557 | uint64_t value) | |
558 | { | |
559 | CPUState *other_cs; | |
560 | ||
561 | CPU_FOREACH(other_cs) { | |
562 | tlb_flush_page(other_cs, value & TARGET_PAGE_MASK); | |
563 | } | |
564 | } | |
565 | ||
566 | static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
567 | uint64_t value) | |
568 | { | |
569 | CPUState *other_cs; | |
570 | ||
571 | CPU_FOREACH(other_cs) { | |
572 | tlb_flush_page(other_cs, value & TARGET_PAGE_MASK); | |
573 | } | |
574 | } | |
575 | ||
541ef8c2 SS |
576 | static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri, |
577 | uint64_t value) | |
578 | { | |
579 | CPUState *cs = ENV_GET_CPU(env); | |
580 | ||
581 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, | |
582 | ARMMMUIdx_S2NS, -1); | |
583 | } | |
584 | ||
585 | static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
586 | uint64_t value) | |
587 | { | |
588 | CPUState *other_cs; | |
589 | ||
590 | CPU_FOREACH(other_cs) { | |
591 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1, | |
592 | ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1); | |
593 | } | |
594 | } | |
595 | ||
596 | static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
597 | uint64_t value) | |
598 | { | |
599 | /* Invalidate by IPA. This has to invalidate any structures that | |
600 | * contain only stage 2 translation information, but does not need | |
601 | * to apply to structures that contain combined stage 1 and stage 2 | |
602 | * translation information. | |
603 | * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. | |
604 | */ | |
605 | CPUState *cs = ENV_GET_CPU(env); | |
606 | uint64_t pageaddr; | |
607 | ||
608 | if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { | |
609 | return; | |
610 | } | |
611 | ||
612 | pageaddr = sextract64(value << 12, 0, 40); | |
613 | ||
614 | tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1); | |
615 | } | |
616 | ||
617 | static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
618 | uint64_t value) | |
619 | { | |
620 | CPUState *other_cs; | |
621 | uint64_t pageaddr; | |
622 | ||
623 | if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { | |
624 | return; | |
625 | } | |
626 | ||
627 | pageaddr = sextract64(value << 12, 0, 40); | |
628 | ||
629 | CPU_FOREACH(other_cs) { | |
630 | tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1); | |
631 | } | |
632 | } | |
633 | ||
634 | static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
635 | uint64_t value) | |
636 | { | |
637 | CPUState *cs = ENV_GET_CPU(env); | |
638 | ||
639 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1); | |
640 | } | |
641 | ||
642 | static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
643 | uint64_t value) | |
644 | { | |
645 | CPUState *other_cs; | |
646 | ||
647 | CPU_FOREACH(other_cs) { | |
648 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1); | |
649 | } | |
650 | } | |
651 | ||
652 | static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
653 | uint64_t value) | |
654 | { | |
655 | CPUState *cs = ENV_GET_CPU(env); | |
656 | uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); | |
657 | ||
658 | tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1); | |
659 | } | |
660 | ||
661 | static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
662 | uint64_t value) | |
663 | { | |
664 | CPUState *other_cs; | |
665 | uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); | |
666 | ||
667 | CPU_FOREACH(other_cs) { | |
668 | tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1); | |
669 | } | |
670 | } | |
671 | ||
e9aa6c21 | 672 | static const ARMCPRegInfo cp_reginfo[] = { |
54bf36ed FA |
673 | /* Define the secure and non-secure FCSE identifier CP registers |
674 | * separately because there is no secure bank in V8 (no _EL3). This allows | |
675 | * the secure register to be properly reset and migrated. There is also no | |
676 | * v8 EL1 version of the register so the non-secure instance stands alone. | |
677 | */ | |
678 | { .name = "FCSEIDR(NS)", | |
679 | .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, | |
680 | .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, | |
681 | .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns), | |
682 | .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, | |
683 | { .name = "FCSEIDR(S)", | |
684 | .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, | |
685 | .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, | |
686 | .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s), | |
d4e6df63 | 687 | .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, |
54bf36ed FA |
688 | /* Define the secure and non-secure context identifier CP registers |
689 | * separately because there is no secure bank in V8 (no _EL3). This allows | |
690 | * the secure register to be properly reset and migrated. In the | |
691 | * non-secure case, the 32-bit register will have reset and migration | |
692 | * disabled during registration as it is handled by the 64-bit instance. | |
693 | */ | |
694 | { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH, | |
014406b5 | 695 | .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, |
54bf36ed FA |
696 | .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, |
697 | .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]), | |
698 | .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, | |
699 | { .name = "CONTEXTIDR(S)", .state = ARM_CP_STATE_AA32, | |
700 | .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, | |
701 | .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, | |
702 | .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s), | |
d4e6df63 | 703 | .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, |
9449fdf6 PM |
704 | REGINFO_SENTINEL |
705 | }; | |
706 | ||
707 | static const ARMCPRegInfo not_v8_cp_reginfo[] = { | |
708 | /* NB: Some of these registers exist in v8 but with more precise | |
709 | * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). | |
710 | */ | |
711 | /* MMU Domain access control / MPU write buffer control */ | |
0c17d68c FA |
712 | { .name = "DACR", |
713 | .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, | |
714 | .access = PL1_RW, .resetvalue = 0, | |
715 | .writefn = dacr_write, .raw_writefn = raw_write, | |
716 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), | |
717 | offsetoflow32(CPUARMState, cp15.dacr_ns) } }, | |
a903c449 EI |
718 | /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs. |
719 | * For v6 and v5, these mappings are overly broad. | |
4fdd17dd | 720 | */ |
a903c449 EI |
721 | { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0, |
722 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, | |
723 | { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1, | |
724 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, | |
725 | { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4, | |
726 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, | |
727 | { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8, | |
4fdd17dd | 728 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, |
c4804214 PM |
729 | /* Cache maintenance ops; some of this space may be overridden later. */ |
730 | { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, | |
731 | .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, | |
732 | .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, | |
e9aa6c21 PM |
733 | REGINFO_SENTINEL |
734 | }; | |
735 | ||
7d57f408 PM |
736 | static const ARMCPRegInfo not_v6_cp_reginfo[] = { |
737 | /* Not all pre-v6 cores implemented this WFI, so this is slightly | |
738 | * over-broad. | |
739 | */ | |
740 | { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, | |
741 | .access = PL1_W, .type = ARM_CP_WFI }, | |
742 | REGINFO_SENTINEL | |
743 | }; | |
744 | ||
745 | static const ARMCPRegInfo not_v7_cp_reginfo[] = { | |
746 | /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which | |
747 | * is UNPREDICTABLE; we choose to NOP as most implementations do). | |
748 | */ | |
749 | { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, | |
750 | .access = PL1_W, .type = ARM_CP_WFI }, | |
34f90529 PM |
751 | /* L1 cache lockdown. Not architectural in v6 and earlier but in practice |
752 | * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and | |
753 | * OMAPCP will override this space. | |
754 | */ | |
755 | { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, | |
756 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), | |
757 | .resetvalue = 0 }, | |
758 | { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, | |
759 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), | |
760 | .resetvalue = 0 }, | |
776d4e5c PM |
761 | /* v6 doesn't have the cache ID registers but Linux reads them anyway */ |
762 | { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, | |
7a0e58fa | 763 | .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, |
d4e6df63 | 764 | .resetvalue = 0 }, |
50300698 PM |
765 | /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; |
766 | * implementing it as RAZ means the "debug architecture version" bits | |
767 | * will read as a reserved value, which should cause Linux to not try | |
768 | * to use the debug hardware. | |
769 | */ | |
770 | { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, | |
771 | .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
995939a6 PM |
772 | /* MMU TLB control. Note that the wildcarding means we cover not just |
773 | * the unified TLB ops but also the dside/iside/inner-shareable variants. | |
774 | */ | |
775 | { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, | |
776 | .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, | |
7a0e58fa | 777 | .type = ARM_CP_NO_RAW }, |
995939a6 PM |
778 | { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, |
779 | .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, | |
7a0e58fa | 780 | .type = ARM_CP_NO_RAW }, |
995939a6 PM |
781 | { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, |
782 | .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, | |
7a0e58fa | 783 | .type = ARM_CP_NO_RAW }, |
995939a6 PM |
784 | { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, |
785 | .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, | |
7a0e58fa | 786 | .type = ARM_CP_NO_RAW }, |
a903c449 EI |
787 | { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2, |
788 | .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP }, | |
789 | { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2, | |
790 | .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP }, | |
7d57f408 PM |
791 | REGINFO_SENTINEL |
792 | }; | |
793 | ||
c4241c7d PM |
794 | static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
795 | uint64_t value) | |
2771db27 | 796 | { |
f0aff255 FA |
797 | uint32_t mask = 0; |
798 | ||
799 | /* In ARMv8 most bits of CPACR_EL1 are RES0. */ | |
800 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
801 | /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. | |
802 | * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. | |
803 | * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. | |
804 | */ | |
805 | if (arm_feature(env, ARM_FEATURE_VFP)) { | |
806 | /* VFP coprocessor: cp10 & cp11 [23:20] */ | |
807 | mask |= (1 << 31) | (1 << 30) | (0xf << 20); | |
808 | ||
809 | if (!arm_feature(env, ARM_FEATURE_NEON)) { | |
810 | /* ASEDIS [31] bit is RAO/WI */ | |
811 | value |= (1 << 31); | |
812 | } | |
813 | ||
814 | /* VFPv3 and upwards with NEON implement 32 double precision | |
815 | * registers (D0-D31). | |
816 | */ | |
817 | if (!arm_feature(env, ARM_FEATURE_NEON) || | |
818 | !arm_feature(env, ARM_FEATURE_VFP3)) { | |
819 | /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ | |
820 | value |= (1 << 30); | |
821 | } | |
822 | } | |
823 | value &= mask; | |
2771db27 | 824 | } |
7ebd5f2e | 825 | env->cp15.cpacr_el1 = value; |
2771db27 PM |
826 | } |
827 | ||
3f208fd7 PM |
828 | static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri, |
829 | bool isread) | |
c6f19164 GB |
830 | { |
831 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
832 | /* Check if CPACR accesses are to be trapped to EL2 */ | |
833 | if (arm_current_el(env) == 1 && | |
834 | (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) { | |
835 | return CP_ACCESS_TRAP_EL2; | |
836 | /* Check if CPACR accesses are to be trapped to EL3 */ | |
837 | } else if (arm_current_el(env) < 3 && | |
838 | (env->cp15.cptr_el[3] & CPTR_TCPAC)) { | |
839 | return CP_ACCESS_TRAP_EL3; | |
840 | } | |
841 | } | |
842 | ||
843 | return CP_ACCESS_OK; | |
844 | } | |
845 | ||
3f208fd7 PM |
846 | static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri, |
847 | bool isread) | |
c6f19164 GB |
848 | { |
849 | /* Check if CPTR accesses are set to trap to EL3 */ | |
850 | if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) { | |
851 | return CP_ACCESS_TRAP_EL3; | |
852 | } | |
853 | ||
854 | return CP_ACCESS_OK; | |
855 | } | |
856 | ||
7d57f408 PM |
857 | static const ARMCPRegInfo v6_cp_reginfo[] = { |
858 | /* prefetch by MVA in v6, NOP in v7 */ | |
859 | { .name = "MVA_prefetch", | |
860 | .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, | |
861 | .access = PL1_W, .type = ARM_CP_NOP }, | |
6df99dec SS |
862 | /* We need to break the TB after ISB to execute self-modifying code |
863 | * correctly and also to take any pending interrupts immediately. | |
864 | * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag. | |
865 | */ | |
7d57f408 | 866 | { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, |
6df99dec | 867 | .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore }, |
091fd17c | 868 | { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, |
7d57f408 | 869 | .access = PL0_W, .type = ARM_CP_NOP }, |
091fd17c | 870 | { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, |
7d57f408 | 871 | .access = PL0_W, .type = ARM_CP_NOP }, |
06d76f31 | 872 | { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, |
6cd8a264 | 873 | .access = PL1_RW, |
b848ce2b FA |
874 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), |
875 | offsetof(CPUARMState, cp15.ifar_ns) }, | |
06d76f31 PM |
876 | .resetvalue = 0, }, |
877 | /* Watchpoint Fault Address Register : should actually only be present | |
878 | * for 1136, 1176, 11MPCore. | |
879 | */ | |
880 | { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, | |
881 | .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, | |
34222fb8 | 882 | { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, |
c6f19164 | 883 | .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access, |
7ebd5f2e | 884 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1), |
2771db27 | 885 | .resetvalue = 0, .writefn = cpacr_write }, |
7d57f408 PM |
886 | REGINFO_SENTINEL |
887 | }; | |
888 | ||
3f208fd7 PM |
889 | static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri, |
890 | bool isread) | |
200ac0ef | 891 | { |
3b163b01 | 892 | /* Performance monitor registers user accessibility is controlled |
1fce1ba9 PM |
893 | * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable |
894 | * trapping to EL2 or EL3 for other accesses. | |
200ac0ef | 895 | */ |
1fce1ba9 PM |
896 | int el = arm_current_el(env); |
897 | ||
898 | if (el == 0 && !env->cp15.c9_pmuserenr) { | |
fcd25206 | 899 | return CP_ACCESS_TRAP; |
200ac0ef | 900 | } |
1fce1ba9 PM |
901 | if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) |
902 | && !arm_is_secure_below_el3(env)) { | |
903 | return CP_ACCESS_TRAP_EL2; | |
904 | } | |
905 | if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { | |
906 | return CP_ACCESS_TRAP_EL3; | |
907 | } | |
908 | ||
fcd25206 | 909 | return CP_ACCESS_OK; |
200ac0ef PM |
910 | } |
911 | ||
7c2cb42b | 912 | #ifndef CONFIG_USER_ONLY |
87124fde AF |
913 | |
914 | static inline bool arm_ccnt_enabled(CPUARMState *env) | |
915 | { | |
916 | /* This does not support checking PMCCFILTR_EL0 register */ | |
917 | ||
918 | if (!(env->cp15.c9_pmcr & PMCRE)) { | |
919 | return false; | |
920 | } | |
921 | ||
922 | return true; | |
923 | } | |
924 | ||
ec7b4ce4 AF |
925 | void pmccntr_sync(CPUARMState *env) |
926 | { | |
927 | uint64_t temp_ticks; | |
928 | ||
352c98e5 LV |
929 | temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), |
930 | ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); | |
ec7b4ce4 AF |
931 | |
932 | if (env->cp15.c9_pmcr & PMCRD) { | |
933 | /* Increment once every 64 processor clock cycles */ | |
934 | temp_ticks /= 64; | |
935 | } | |
936 | ||
937 | if (arm_ccnt_enabled(env)) { | |
938 | env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt; | |
939 | } | |
940 | } | |
941 | ||
c4241c7d PM |
942 | static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
943 | uint64_t value) | |
200ac0ef | 944 | { |
942a155b | 945 | pmccntr_sync(env); |
7c2cb42b AF |
946 | |
947 | if (value & PMCRC) { | |
948 | /* The counter has been reset */ | |
949 | env->cp15.c15_ccnt = 0; | |
950 | } | |
951 | ||
200ac0ef PM |
952 | /* only the DP, X, D and E bits are writable */ |
953 | env->cp15.c9_pmcr &= ~0x39; | |
954 | env->cp15.c9_pmcr |= (value & 0x39); | |
7c2cb42b | 955 | |
942a155b | 956 | pmccntr_sync(env); |
7c2cb42b AF |
957 | } |
958 | ||
959 | static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
960 | { | |
c92c0687 | 961 | uint64_t total_ticks; |
7c2cb42b | 962 | |
942a155b | 963 | if (!arm_ccnt_enabled(env)) { |
7c2cb42b AF |
964 | /* Counter is disabled, do not change value */ |
965 | return env->cp15.c15_ccnt; | |
966 | } | |
967 | ||
352c98e5 LV |
968 | total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), |
969 | ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); | |
7c2cb42b AF |
970 | |
971 | if (env->cp15.c9_pmcr & PMCRD) { | |
972 | /* Increment once every 64 processor clock cycles */ | |
973 | total_ticks /= 64; | |
974 | } | |
975 | return total_ticks - env->cp15.c15_ccnt; | |
976 | } | |
977 | ||
978 | static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
979 | uint64_t value) | |
980 | { | |
c92c0687 | 981 | uint64_t total_ticks; |
7c2cb42b | 982 | |
942a155b | 983 | if (!arm_ccnt_enabled(env)) { |
7c2cb42b AF |
984 | /* Counter is disabled, set the absolute value */ |
985 | env->cp15.c15_ccnt = value; | |
986 | return; | |
987 | } | |
988 | ||
352c98e5 LV |
989 | total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), |
990 | ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); | |
7c2cb42b AF |
991 | |
992 | if (env->cp15.c9_pmcr & PMCRD) { | |
993 | /* Increment once every 64 processor clock cycles */ | |
994 | total_ticks /= 64; | |
995 | } | |
996 | env->cp15.c15_ccnt = total_ticks - value; | |
200ac0ef | 997 | } |
421c7ebd PC |
998 | |
999 | static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, | |
1000 | uint64_t value) | |
1001 | { | |
1002 | uint64_t cur_val = pmccntr_read(env, NULL); | |
1003 | ||
1004 | pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); | |
1005 | } | |
1006 | ||
ec7b4ce4 AF |
1007 | #else /* CONFIG_USER_ONLY */ |
1008 | ||
1009 | void pmccntr_sync(CPUARMState *env) | |
1010 | { | |
1011 | } | |
1012 | ||
7c2cb42b | 1013 | #endif |
200ac0ef | 1014 | |
0614601c AF |
1015 | static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1016 | uint64_t value) | |
1017 | { | |
1018 | pmccntr_sync(env); | |
1019 | env->cp15.pmccfiltr_el0 = value & 0x7E000000; | |
1020 | pmccntr_sync(env); | |
1021 | } | |
1022 | ||
c4241c7d | 1023 | static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, |
200ac0ef PM |
1024 | uint64_t value) |
1025 | { | |
200ac0ef PM |
1026 | value &= (1 << 31); |
1027 | env->cp15.c9_pmcnten |= value; | |
200ac0ef PM |
1028 | } |
1029 | ||
c4241c7d PM |
1030 | static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1031 | uint64_t value) | |
200ac0ef | 1032 | { |
200ac0ef PM |
1033 | value &= (1 << 31); |
1034 | env->cp15.c9_pmcnten &= ~value; | |
200ac0ef PM |
1035 | } |
1036 | ||
c4241c7d PM |
1037 | static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1038 | uint64_t value) | |
200ac0ef | 1039 | { |
200ac0ef | 1040 | env->cp15.c9_pmovsr &= ~value; |
200ac0ef PM |
1041 | } |
1042 | ||
c4241c7d PM |
1043 | static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1044 | uint64_t value) | |
200ac0ef | 1045 | { |
200ac0ef | 1046 | env->cp15.c9_pmxevtyper = value & 0xff; |
200ac0ef PM |
1047 | } |
1048 | ||
c4241c7d | 1049 | static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
200ac0ef PM |
1050 | uint64_t value) |
1051 | { | |
1052 | env->cp15.c9_pmuserenr = value & 1; | |
200ac0ef PM |
1053 | } |
1054 | ||
c4241c7d PM |
1055 | static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1056 | uint64_t value) | |
200ac0ef PM |
1057 | { |
1058 | /* We have no event counters so only the C bit can be changed */ | |
1059 | value &= (1 << 31); | |
1060 | env->cp15.c9_pminten |= value; | |
200ac0ef PM |
1061 | } |
1062 | ||
c4241c7d PM |
1063 | static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1064 | uint64_t value) | |
200ac0ef PM |
1065 | { |
1066 | value &= (1 << 31); | |
1067 | env->cp15.c9_pminten &= ~value; | |
200ac0ef PM |
1068 | } |
1069 | ||
c4241c7d PM |
1070 | static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1071 | uint64_t value) | |
8641136c | 1072 | { |
a505d7fe PM |
1073 | /* Note that even though the AArch64 view of this register has bits |
1074 | * [10:0] all RES0 we can only mask the bottom 5, to comply with the | |
1075 | * architectural requirements for bits which are RES0 only in some | |
1076 | * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 | |
1077 | * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) | |
1078 | */ | |
855ea66d | 1079 | raw_write(env, ri, value & ~0x1FULL); |
8641136c NR |
1080 | } |
1081 | ||
64e0e2de EI |
1082 | static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
1083 | { | |
1084 | /* We only mask off bits that are RES0 both for AArch64 and AArch32. | |
1085 | * For bits that vary between AArch32/64, code needs to check the | |
1086 | * current execution mode before directly using the feature bit. | |
1087 | */ | |
1088 | uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK; | |
1089 | ||
1090 | if (!arm_feature(env, ARM_FEATURE_EL2)) { | |
1091 | valid_mask &= ~SCR_HCE; | |
1092 | ||
1093 | /* On ARMv7, SMD (or SCD as it is called in v7) is only | |
1094 | * supported if EL2 exists. The bit is UNK/SBZP when | |
1095 | * EL2 is unavailable. In QEMU ARMv7, we force it to always zero | |
1096 | * when EL2 is unavailable. | |
4eb27640 | 1097 | * On ARMv8, this bit is always available. |
64e0e2de | 1098 | */ |
4eb27640 GB |
1099 | if (arm_feature(env, ARM_FEATURE_V7) && |
1100 | !arm_feature(env, ARM_FEATURE_V8)) { | |
64e0e2de EI |
1101 | valid_mask &= ~SCR_SMD; |
1102 | } | |
1103 | } | |
1104 | ||
1105 | /* Clear all-context RES0 bits. */ | |
1106 | value &= valid_mask; | |
1107 | raw_write(env, ri, value); | |
1108 | } | |
1109 | ||
c4241c7d | 1110 | static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) |
776d4e5c PM |
1111 | { |
1112 | ARMCPU *cpu = arm_env_get_cpu(env); | |
b85a1fd6 FA |
1113 | |
1114 | /* Acquire the CSSELR index from the bank corresponding to the CCSIDR | |
1115 | * bank | |
1116 | */ | |
1117 | uint32_t index = A32_BANKED_REG_GET(env, csselr, | |
1118 | ri->secure & ARM_CP_SECSTATE_S); | |
1119 | ||
1120 | return cpu->ccsidr[index]; | |
776d4e5c PM |
1121 | } |
1122 | ||
c4241c7d PM |
1123 | static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1124 | uint64_t value) | |
776d4e5c | 1125 | { |
8d5c773e | 1126 | raw_write(env, ri, value & 0xf); |
776d4e5c PM |
1127 | } |
1128 | ||
1090b9c6 PM |
1129 | static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) |
1130 | { | |
1131 | CPUState *cs = ENV_GET_CPU(env); | |
1132 | uint64_t ret = 0; | |
1133 | ||
1134 | if (cs->interrupt_request & CPU_INTERRUPT_HARD) { | |
1135 | ret |= CPSR_I; | |
1136 | } | |
1137 | if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { | |
1138 | ret |= CPSR_F; | |
1139 | } | |
1140 | /* External aborts are not possible in QEMU so A bit is always clear */ | |
1141 | return ret; | |
1142 | } | |
1143 | ||
e9aa6c21 | 1144 | static const ARMCPRegInfo v7_cp_reginfo[] = { |
7d57f408 PM |
1145 | /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ |
1146 | { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, | |
1147 | .access = PL1_W, .type = ARM_CP_NOP }, | |
200ac0ef PM |
1148 | /* Performance monitors are implementation defined in v7, |
1149 | * but with an ARM recommended set of registers, which we | |
1150 | * follow (although we don't actually implement any counters) | |
1151 | * | |
1152 | * Performance registers fall into three categories: | |
1153 | * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) | |
1154 | * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) | |
1155 | * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) | |
1156 | * For the cases controlled by PMUSERENR we must set .access to PL0_RW | |
1157 | * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. | |
1158 | */ | |
1159 | { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, | |
7a0e58fa | 1160 | .access = PL0_RW, .type = ARM_CP_ALIAS, |
8521466b | 1161 | .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), |
fcd25206 PM |
1162 | .writefn = pmcntenset_write, |
1163 | .accessfn = pmreg_access, | |
1164 | .raw_writefn = raw_write }, | |
8521466b AF |
1165 | { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, |
1166 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, | |
1167 | .access = PL0_RW, .accessfn = pmreg_access, | |
1168 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, | |
1169 | .writefn = pmcntenset_write, .raw_writefn = raw_write }, | |
200ac0ef | 1170 | { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, |
8521466b AF |
1171 | .access = PL0_RW, |
1172 | .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), | |
fcd25206 PM |
1173 | .accessfn = pmreg_access, |
1174 | .writefn = pmcntenclr_write, | |
7a0e58fa | 1175 | .type = ARM_CP_ALIAS }, |
8521466b AF |
1176 | { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, |
1177 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, | |
1178 | .access = PL0_RW, .accessfn = pmreg_access, | |
7a0e58fa | 1179 | .type = ARM_CP_ALIAS, |
8521466b AF |
1180 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), |
1181 | .writefn = pmcntenclr_write }, | |
200ac0ef PM |
1182 | { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, |
1183 | .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), | |
fcd25206 PM |
1184 | .accessfn = pmreg_access, |
1185 | .writefn = pmovsr_write, | |
1186 | .raw_writefn = raw_write }, | |
978364f1 AF |
1187 | { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64, |
1188 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3, | |
1189 | .access = PL0_RW, .accessfn = pmreg_access, | |
1190 | .type = ARM_CP_ALIAS, | |
1191 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), | |
1192 | .writefn = pmovsr_write, | |
1193 | .raw_writefn = raw_write }, | |
fcd25206 | 1194 | /* Unimplemented so WI. */ |
200ac0ef | 1195 | { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, |
fcd25206 | 1196 | .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP }, |
200ac0ef | 1197 | /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE. |
fcd25206 | 1198 | * We choose to RAZ/WI. |
200ac0ef PM |
1199 | */ |
1200 | { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, | |
fcd25206 PM |
1201 | .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0, |
1202 | .accessfn = pmreg_access }, | |
7c2cb42b | 1203 | #ifndef CONFIG_USER_ONLY |
200ac0ef | 1204 | { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, |
7c2cb42b | 1205 | .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO, |
421c7ebd | 1206 | .readfn = pmccntr_read, .writefn = pmccntr_write32, |
fcd25206 | 1207 | .accessfn = pmreg_access }, |
8521466b AF |
1208 | { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, |
1209 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, | |
1210 | .access = PL0_RW, .accessfn = pmreg_access, | |
1211 | .type = ARM_CP_IO, | |
1212 | .readfn = pmccntr_read, .writefn = pmccntr_write, }, | |
7c2cb42b | 1213 | #endif |
8521466b AF |
1214 | { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, |
1215 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, | |
0614601c | 1216 | .writefn = pmccfiltr_write, |
8521466b AF |
1217 | .access = PL0_RW, .accessfn = pmreg_access, |
1218 | .type = ARM_CP_IO, | |
1219 | .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), | |
1220 | .resetvalue = 0, }, | |
200ac0ef PM |
1221 | { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, |
1222 | .access = PL0_RW, | |
1223 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper), | |
fcd25206 PM |
1224 | .accessfn = pmreg_access, .writefn = pmxevtyper_write, |
1225 | .raw_writefn = raw_write }, | |
1226 | /* Unimplemented, RAZ/WI. */ | |
200ac0ef | 1227 | { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, |
fcd25206 PM |
1228 | .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0, |
1229 | .accessfn = pmreg_access }, | |
200ac0ef | 1230 | { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, |
1fce1ba9 | 1231 | .access = PL0_R | PL1_RW, .accessfn = access_tpm, |
200ac0ef PM |
1232 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), |
1233 | .resetvalue = 0, | |
d4e6df63 | 1234 | .writefn = pmuserenr_write, .raw_writefn = raw_write }, |
8a83ffc2 AF |
1235 | { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64, |
1236 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0, | |
1fce1ba9 | 1237 | .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, |
8a83ffc2 AF |
1238 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), |
1239 | .resetvalue = 0, | |
1240 | .writefn = pmuserenr_write, .raw_writefn = raw_write }, | |
200ac0ef | 1241 | { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, |
1fce1ba9 | 1242 | .access = PL1_RW, .accessfn = access_tpm, |
200ac0ef PM |
1243 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), |
1244 | .resetvalue = 0, | |
d4e6df63 | 1245 | .writefn = pmintenset_write, .raw_writefn = raw_write }, |
200ac0ef | 1246 | { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, |
1fce1ba9 | 1247 | .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, |
200ac0ef | 1248 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), |
b061a82b | 1249 | .writefn = pmintenclr_write, }, |
978364f1 AF |
1250 | { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64, |
1251 | .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2, | |
1fce1ba9 | 1252 | .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, |
978364f1 AF |
1253 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), |
1254 | .writefn = pmintenclr_write }, | |
7da845b0 PM |
1255 | { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, |
1256 | .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, | |
7a0e58fa | 1257 | .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW }, |
7da845b0 PM |
1258 | { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, |
1259 | .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, | |
b85a1fd6 FA |
1260 | .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0, |
1261 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), | |
1262 | offsetof(CPUARMState, cp15.csselr_ns) } }, | |
776d4e5c PM |
1263 | /* Auxiliary ID register: this actually has an IMPDEF value but for now |
1264 | * just RAZ for all cores: | |
1265 | */ | |
0ff644a7 PM |
1266 | { .name = "AIDR", .state = ARM_CP_STATE_BOTH, |
1267 | .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, | |
776d4e5c | 1268 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
f32cdad5 PM |
1269 | /* Auxiliary fault status registers: these also are IMPDEF, and we |
1270 | * choose to RAZ/WI for all cores. | |
1271 | */ | |
1272 | { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, | |
1273 | .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, | |
1274 | .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
1275 | { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, | |
1276 | .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, | |
1277 | .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
b0fe2427 PM |
1278 | /* MAIR can just read-as-written because we don't implement caches |
1279 | * and so don't need to care about memory attributes. | |
1280 | */ | |
1281 | { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, | |
1282 | .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, | |
be693c87 | 1283 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]), |
b0fe2427 | 1284 | .resetvalue = 0 }, |
4cfb8ad8 PM |
1285 | { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64, |
1286 | .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0, | |
1287 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]), | |
1288 | .resetvalue = 0 }, | |
b0fe2427 PM |
1289 | /* For non-long-descriptor page tables these are PRRR and NMRR; |
1290 | * regardless they still act as reads-as-written for QEMU. | |
b0fe2427 | 1291 | */ |
1281f8e3 | 1292 | /* MAIR0/1 are defined separately from their 64-bit counterpart which |
be693c87 GB |
1293 | * allows them to assign the correct fieldoffset based on the endianness |
1294 | * handled in the field definitions. | |
1295 | */ | |
a903c449 | 1296 | { .name = "MAIR0", .state = ARM_CP_STATE_AA32, |
b0fe2427 | 1297 | .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW, |
be693c87 GB |
1298 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s), |
1299 | offsetof(CPUARMState, cp15.mair0_ns) }, | |
b0fe2427 | 1300 | .resetfn = arm_cp_reset_ignore }, |
a903c449 | 1301 | { .name = "MAIR1", .state = ARM_CP_STATE_AA32, |
b0fe2427 | 1302 | .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW, |
be693c87 GB |
1303 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s), |
1304 | offsetof(CPUARMState, cp15.mair1_ns) }, | |
b0fe2427 | 1305 | .resetfn = arm_cp_reset_ignore }, |
1090b9c6 PM |
1306 | { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, |
1307 | .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, | |
7a0e58fa | 1308 | .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read }, |
995939a6 PM |
1309 | /* 32 bit ITLB invalidates */ |
1310 | { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0, | |
7a0e58fa | 1311 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, |
995939a6 | 1312 | { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, |
7a0e58fa | 1313 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, |
995939a6 | 1314 | { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2, |
7a0e58fa | 1315 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, |
995939a6 PM |
1316 | /* 32 bit DTLB invalidates */ |
1317 | { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0, | |
7a0e58fa | 1318 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, |
995939a6 | 1319 | { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, |
7a0e58fa | 1320 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, |
995939a6 | 1321 | { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2, |
7a0e58fa | 1322 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, |
995939a6 PM |
1323 | /* 32 bit TLB invalidates */ |
1324 | { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, | |
7a0e58fa | 1325 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, |
995939a6 | 1326 | { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, |
7a0e58fa | 1327 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, |
995939a6 | 1328 | { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, |
7a0e58fa | 1329 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, |
995939a6 | 1330 | { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, |
7a0e58fa | 1331 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, |
995939a6 PM |
1332 | REGINFO_SENTINEL |
1333 | }; | |
1334 | ||
1335 | static const ARMCPRegInfo v7mp_cp_reginfo[] = { | |
1336 | /* 32 bit TLB invalidates, Inner Shareable */ | |
1337 | { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, | |
7a0e58fa | 1338 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write }, |
995939a6 | 1339 | { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, |
7a0e58fa | 1340 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, |
995939a6 | 1341 | { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, |
7a0e58fa | 1342 | .type = ARM_CP_NO_RAW, .access = PL1_W, |
fa439fc5 | 1343 | .writefn = tlbiasid_is_write }, |
995939a6 | 1344 | { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, |
7a0e58fa | 1345 | .type = ARM_CP_NO_RAW, .access = PL1_W, |
fa439fc5 | 1346 | .writefn = tlbimvaa_is_write }, |
e9aa6c21 PM |
1347 | REGINFO_SENTINEL |
1348 | }; | |
1349 | ||
c4241c7d PM |
1350 | static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1351 | uint64_t value) | |
c326b979 PM |
1352 | { |
1353 | value &= 1; | |
1354 | env->teecr = value; | |
c326b979 PM |
1355 | } |
1356 | ||
3f208fd7 PM |
1357 | static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri, |
1358 | bool isread) | |
c326b979 | 1359 | { |
dcbff19b | 1360 | if (arm_current_el(env) == 0 && (env->teecr & 1)) { |
92611c00 | 1361 | return CP_ACCESS_TRAP; |
c326b979 | 1362 | } |
92611c00 | 1363 | return CP_ACCESS_OK; |
c326b979 PM |
1364 | } |
1365 | ||
1366 | static const ARMCPRegInfo t2ee_cp_reginfo[] = { | |
1367 | { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, | |
1368 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), | |
1369 | .resetvalue = 0, | |
1370 | .writefn = teecr_write }, | |
1371 | { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, | |
1372 | .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), | |
92611c00 | 1373 | .accessfn = teehbr_access, .resetvalue = 0 }, |
c326b979 PM |
1374 | REGINFO_SENTINEL |
1375 | }; | |
1376 | ||
4d31c596 | 1377 | static const ARMCPRegInfo v6k_cp_reginfo[] = { |
e4fe830b PM |
1378 | { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, |
1379 | .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, | |
1380 | .access = PL0_RW, | |
54bf36ed | 1381 | .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 }, |
4d31c596 PM |
1382 | { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, |
1383 | .access = PL0_RW, | |
54bf36ed FA |
1384 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s), |
1385 | offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, | |
e4fe830b PM |
1386 | .resetfn = arm_cp_reset_ignore }, |
1387 | { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, | |
1388 | .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, | |
1389 | .access = PL0_R|PL1_W, | |
54bf36ed FA |
1390 | .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]), |
1391 | .resetvalue = 0}, | |
4d31c596 PM |
1392 | { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, |
1393 | .access = PL0_R|PL1_W, | |
54bf36ed FA |
1394 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s), |
1395 | offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, | |
e4fe830b | 1396 | .resetfn = arm_cp_reset_ignore }, |
54bf36ed | 1397 | { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64, |
e4fe830b | 1398 | .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, |
4d31c596 | 1399 | .access = PL1_RW, |
54bf36ed FA |
1400 | .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 }, |
1401 | { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4, | |
1402 | .access = PL1_RW, | |
1403 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s), | |
1404 | offsetoflow32(CPUARMState, cp15.tpidrprw_ns) }, | |
1405 | .resetvalue = 0 }, | |
4d31c596 PM |
1406 | REGINFO_SENTINEL |
1407 | }; | |
1408 | ||
55d284af PM |
1409 | #ifndef CONFIG_USER_ONLY |
1410 | ||
3f208fd7 PM |
1411 | static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, |
1412 | bool isread) | |
00108f2d | 1413 | { |
75502672 PM |
1414 | /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero. |
1415 | * Writable only at the highest implemented exception level. | |
1416 | */ | |
1417 | int el = arm_current_el(env); | |
1418 | ||
1419 | switch (el) { | |
1420 | case 0: | |
1421 | if (!extract32(env->cp15.c14_cntkctl, 0, 2)) { | |
1422 | return CP_ACCESS_TRAP; | |
1423 | } | |
1424 | break; | |
1425 | case 1: | |
1426 | if (!isread && ri->state == ARM_CP_STATE_AA32 && | |
1427 | arm_is_secure_below_el3(env)) { | |
1428 | /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */ | |
1429 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
1430 | } | |
1431 | break; | |
1432 | case 2: | |
1433 | case 3: | |
1434 | break; | |
00108f2d | 1435 | } |
75502672 PM |
1436 | |
1437 | if (!isread && el < arm_highest_el(env)) { | |
1438 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
1439 | } | |
1440 | ||
00108f2d PM |
1441 | return CP_ACCESS_OK; |
1442 | } | |
1443 | ||
3f208fd7 PM |
1444 | static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx, |
1445 | bool isread) | |
00108f2d | 1446 | { |
0b6440af EI |
1447 | unsigned int cur_el = arm_current_el(env); |
1448 | bool secure = arm_is_secure(env); | |
1449 | ||
00108f2d | 1450 | /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */ |
0b6440af | 1451 | if (cur_el == 0 && |
00108f2d PM |
1452 | !extract32(env->cp15.c14_cntkctl, timeridx, 1)) { |
1453 | return CP_ACCESS_TRAP; | |
1454 | } | |
0b6440af EI |
1455 | |
1456 | if (arm_feature(env, ARM_FEATURE_EL2) && | |
1457 | timeridx == GTIMER_PHYS && !secure && cur_el < 2 && | |
1458 | !extract32(env->cp15.cnthctl_el2, 0, 1)) { | |
1459 | return CP_ACCESS_TRAP_EL2; | |
1460 | } | |
00108f2d PM |
1461 | return CP_ACCESS_OK; |
1462 | } | |
1463 | ||
3f208fd7 PM |
1464 | static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx, |
1465 | bool isread) | |
00108f2d | 1466 | { |
0b6440af EI |
1467 | unsigned int cur_el = arm_current_el(env); |
1468 | bool secure = arm_is_secure(env); | |
1469 | ||
00108f2d PM |
1470 | /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if |
1471 | * EL0[PV]TEN is zero. | |
1472 | */ | |
0b6440af | 1473 | if (cur_el == 0 && |
00108f2d PM |
1474 | !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { |
1475 | return CP_ACCESS_TRAP; | |
1476 | } | |
0b6440af EI |
1477 | |
1478 | if (arm_feature(env, ARM_FEATURE_EL2) && | |
1479 | timeridx == GTIMER_PHYS && !secure && cur_el < 2 && | |
1480 | !extract32(env->cp15.cnthctl_el2, 1, 1)) { | |
1481 | return CP_ACCESS_TRAP_EL2; | |
1482 | } | |
00108f2d PM |
1483 | return CP_ACCESS_OK; |
1484 | } | |
1485 | ||
1486 | static CPAccessResult gt_pct_access(CPUARMState *env, | |
3f208fd7 PM |
1487 | const ARMCPRegInfo *ri, |
1488 | bool isread) | |
00108f2d | 1489 | { |
3f208fd7 | 1490 | return gt_counter_access(env, GTIMER_PHYS, isread); |
00108f2d PM |
1491 | } |
1492 | ||
1493 | static CPAccessResult gt_vct_access(CPUARMState *env, | |
3f208fd7 PM |
1494 | const ARMCPRegInfo *ri, |
1495 | bool isread) | |
00108f2d | 1496 | { |
3f208fd7 | 1497 | return gt_counter_access(env, GTIMER_VIRT, isread); |
00108f2d PM |
1498 | } |
1499 | ||
3f208fd7 PM |
1500 | static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri, |
1501 | bool isread) | |
00108f2d | 1502 | { |
3f208fd7 | 1503 | return gt_timer_access(env, GTIMER_PHYS, isread); |
00108f2d PM |
1504 | } |
1505 | ||
3f208fd7 PM |
1506 | static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri, |
1507 | bool isread) | |
00108f2d | 1508 | { |
3f208fd7 | 1509 | return gt_timer_access(env, GTIMER_VIRT, isread); |
00108f2d PM |
1510 | } |
1511 | ||
b4d3978c | 1512 | static CPAccessResult gt_stimer_access(CPUARMState *env, |
3f208fd7 PM |
1513 | const ARMCPRegInfo *ri, |
1514 | bool isread) | |
b4d3978c PM |
1515 | { |
1516 | /* The AArch64 register view of the secure physical timer is | |
1517 | * always accessible from EL3, and configurably accessible from | |
1518 | * Secure EL1. | |
1519 | */ | |
1520 | switch (arm_current_el(env)) { | |
1521 | case 1: | |
1522 | if (!arm_is_secure(env)) { | |
1523 | return CP_ACCESS_TRAP; | |
1524 | } | |
1525 | if (!(env->cp15.scr_el3 & SCR_ST)) { | |
1526 | return CP_ACCESS_TRAP_EL3; | |
1527 | } | |
1528 | return CP_ACCESS_OK; | |
1529 | case 0: | |
1530 | case 2: | |
1531 | return CP_ACCESS_TRAP; | |
1532 | case 3: | |
1533 | return CP_ACCESS_OK; | |
1534 | default: | |
1535 | g_assert_not_reached(); | |
1536 | } | |
1537 | } | |
1538 | ||
55d284af PM |
1539 | static uint64_t gt_get_countervalue(CPUARMState *env) |
1540 | { | |
bc72ad67 | 1541 | return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE; |
55d284af PM |
1542 | } |
1543 | ||
1544 | static void gt_recalc_timer(ARMCPU *cpu, int timeridx) | |
1545 | { | |
1546 | ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; | |
1547 | ||
1548 | if (gt->ctl & 1) { | |
1549 | /* Timer enabled: calculate and set current ISTATUS, irq, and | |
1550 | * reset timer to when ISTATUS next has to change | |
1551 | */ | |
edac4d8a EI |
1552 | uint64_t offset = timeridx == GTIMER_VIRT ? |
1553 | cpu->env.cp15.cntvoff_el2 : 0; | |
55d284af PM |
1554 | uint64_t count = gt_get_countervalue(&cpu->env); |
1555 | /* Note that this must be unsigned 64 bit arithmetic: */ | |
edac4d8a | 1556 | int istatus = count - offset >= gt->cval; |
55d284af | 1557 | uint64_t nexttick; |
194cbc49 | 1558 | int irqstate; |
55d284af PM |
1559 | |
1560 | gt->ctl = deposit32(gt->ctl, 2, 1, istatus); | |
194cbc49 PM |
1561 | |
1562 | irqstate = (istatus && !(gt->ctl & 2)); | |
1563 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); | |
1564 | ||
55d284af PM |
1565 | if (istatus) { |
1566 | /* Next transition is when count rolls back over to zero */ | |
1567 | nexttick = UINT64_MAX; | |
1568 | } else { | |
1569 | /* Next transition is when we hit cval */ | |
edac4d8a | 1570 | nexttick = gt->cval + offset; |
55d284af PM |
1571 | } |
1572 | /* Note that the desired next expiry time might be beyond the | |
1573 | * signed-64-bit range of a QEMUTimer -- in this case we just | |
1574 | * set the timer for as far in the future as possible. When the | |
1575 | * timer expires we will reset the timer for any remaining period. | |
1576 | */ | |
1577 | if (nexttick > INT64_MAX / GTIMER_SCALE) { | |
1578 | nexttick = INT64_MAX / GTIMER_SCALE; | |
1579 | } | |
bc72ad67 | 1580 | timer_mod(cpu->gt_timer[timeridx], nexttick); |
194cbc49 | 1581 | trace_arm_gt_recalc(timeridx, irqstate, nexttick); |
55d284af PM |
1582 | } else { |
1583 | /* Timer disabled: ISTATUS and timer output always clear */ | |
1584 | gt->ctl &= ~4; | |
1585 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0); | |
bc72ad67 | 1586 | timer_del(cpu->gt_timer[timeridx]); |
194cbc49 | 1587 | trace_arm_gt_recalc_disabled(timeridx); |
55d284af PM |
1588 | } |
1589 | } | |
1590 | ||
0e3eca4c EI |
1591 | static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri, |
1592 | int timeridx) | |
55d284af PM |
1593 | { |
1594 | ARMCPU *cpu = arm_env_get_cpu(env); | |
55d284af | 1595 | |
bc72ad67 | 1596 | timer_del(cpu->gt_timer[timeridx]); |
55d284af PM |
1597 | } |
1598 | ||
c4241c7d | 1599 | static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) |
55d284af | 1600 | { |
c4241c7d | 1601 | return gt_get_countervalue(env); |
55d284af PM |
1602 | } |
1603 | ||
edac4d8a EI |
1604 | static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) |
1605 | { | |
1606 | return gt_get_countervalue(env) - env->cp15.cntvoff_el2; | |
1607 | } | |
1608 | ||
c4241c7d | 1609 | static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, |
0e3eca4c | 1610 | int timeridx, |
c4241c7d | 1611 | uint64_t value) |
55d284af | 1612 | { |
194cbc49 | 1613 | trace_arm_gt_cval_write(timeridx, value); |
55d284af PM |
1614 | env->cp15.c14_timer[timeridx].cval = value; |
1615 | gt_recalc_timer(arm_env_get_cpu(env), timeridx); | |
55d284af | 1616 | } |
c4241c7d | 1617 | |
0e3eca4c EI |
1618 | static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, |
1619 | int timeridx) | |
55d284af | 1620 | { |
edac4d8a | 1621 | uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; |
55d284af | 1622 | |
c4241c7d | 1623 | return (uint32_t)(env->cp15.c14_timer[timeridx].cval - |
edac4d8a | 1624 | (gt_get_countervalue(env) - offset)); |
55d284af PM |
1625 | } |
1626 | ||
c4241c7d | 1627 | static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, |
0e3eca4c | 1628 | int timeridx, |
c4241c7d | 1629 | uint64_t value) |
55d284af | 1630 | { |
edac4d8a | 1631 | uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; |
55d284af | 1632 | |
194cbc49 | 1633 | trace_arm_gt_tval_write(timeridx, value); |
edac4d8a | 1634 | env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset + |
18084b2f | 1635 | sextract64(value, 0, 32); |
55d284af | 1636 | gt_recalc_timer(arm_env_get_cpu(env), timeridx); |
55d284af PM |
1637 | } |
1638 | ||
c4241c7d | 1639 | static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, |
0e3eca4c | 1640 | int timeridx, |
c4241c7d | 1641 | uint64_t value) |
55d284af PM |
1642 | { |
1643 | ARMCPU *cpu = arm_env_get_cpu(env); | |
55d284af PM |
1644 | uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; |
1645 | ||
194cbc49 | 1646 | trace_arm_gt_ctl_write(timeridx, value); |
d3afacc7 | 1647 | env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); |
55d284af PM |
1648 | if ((oldval ^ value) & 1) { |
1649 | /* Enable toggled */ | |
1650 | gt_recalc_timer(cpu, timeridx); | |
d3afacc7 | 1651 | } else if ((oldval ^ value) & 2) { |
55d284af PM |
1652 | /* IMASK toggled: don't need to recalculate, |
1653 | * just set the interrupt line based on ISTATUS | |
1654 | */ | |
194cbc49 PM |
1655 | int irqstate = (oldval & 4) && !(value & 2); |
1656 | ||
1657 | trace_arm_gt_imask_toggle(timeridx, irqstate); | |
1658 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); | |
55d284af | 1659 | } |
55d284af PM |
1660 | } |
1661 | ||
0e3eca4c EI |
1662 | static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
1663 | { | |
1664 | gt_timer_reset(env, ri, GTIMER_PHYS); | |
1665 | } | |
1666 | ||
1667 | static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1668 | uint64_t value) | |
1669 | { | |
1670 | gt_cval_write(env, ri, GTIMER_PHYS, value); | |
1671 | } | |
1672 | ||
1673 | static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1674 | { | |
1675 | return gt_tval_read(env, ri, GTIMER_PHYS); | |
1676 | } | |
1677 | ||
1678 | static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1679 | uint64_t value) | |
1680 | { | |
1681 | gt_tval_write(env, ri, GTIMER_PHYS, value); | |
1682 | } | |
1683 | ||
1684 | static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1685 | uint64_t value) | |
1686 | { | |
1687 | gt_ctl_write(env, ri, GTIMER_PHYS, value); | |
1688 | } | |
1689 | ||
1690 | static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) | |
1691 | { | |
1692 | gt_timer_reset(env, ri, GTIMER_VIRT); | |
1693 | } | |
1694 | ||
1695 | static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1696 | uint64_t value) | |
1697 | { | |
1698 | gt_cval_write(env, ri, GTIMER_VIRT, value); | |
1699 | } | |
1700 | ||
1701 | static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1702 | { | |
1703 | return gt_tval_read(env, ri, GTIMER_VIRT); | |
1704 | } | |
1705 | ||
1706 | static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1707 | uint64_t value) | |
1708 | { | |
1709 | gt_tval_write(env, ri, GTIMER_VIRT, value); | |
1710 | } | |
1711 | ||
1712 | static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1713 | uint64_t value) | |
1714 | { | |
1715 | gt_ctl_write(env, ri, GTIMER_VIRT, value); | |
1716 | } | |
1717 | ||
edac4d8a EI |
1718 | static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri, |
1719 | uint64_t value) | |
1720 | { | |
1721 | ARMCPU *cpu = arm_env_get_cpu(env); | |
1722 | ||
194cbc49 | 1723 | trace_arm_gt_cntvoff_write(value); |
edac4d8a EI |
1724 | raw_write(env, ri, value); |
1725 | gt_recalc_timer(cpu, GTIMER_VIRT); | |
1726 | } | |
1727 | ||
b0e66d95 EI |
1728 | static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
1729 | { | |
1730 | gt_timer_reset(env, ri, GTIMER_HYP); | |
1731 | } | |
1732 | ||
1733 | static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1734 | uint64_t value) | |
1735 | { | |
1736 | gt_cval_write(env, ri, GTIMER_HYP, value); | |
1737 | } | |
1738 | ||
1739 | static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1740 | { | |
1741 | return gt_tval_read(env, ri, GTIMER_HYP); | |
1742 | } | |
1743 | ||
1744 | static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1745 | uint64_t value) | |
1746 | { | |
1747 | gt_tval_write(env, ri, GTIMER_HYP, value); | |
1748 | } | |
1749 | ||
1750 | static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1751 | uint64_t value) | |
1752 | { | |
1753 | gt_ctl_write(env, ri, GTIMER_HYP, value); | |
1754 | } | |
1755 | ||
b4d3978c PM |
1756 | static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
1757 | { | |
1758 | gt_timer_reset(env, ri, GTIMER_SEC); | |
1759 | } | |
1760 | ||
1761 | static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1762 | uint64_t value) | |
1763 | { | |
1764 | gt_cval_write(env, ri, GTIMER_SEC, value); | |
1765 | } | |
1766 | ||
1767 | static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1768 | { | |
1769 | return gt_tval_read(env, ri, GTIMER_SEC); | |
1770 | } | |
1771 | ||
1772 | static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1773 | uint64_t value) | |
1774 | { | |
1775 | gt_tval_write(env, ri, GTIMER_SEC, value); | |
1776 | } | |
1777 | ||
1778 | static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1779 | uint64_t value) | |
1780 | { | |
1781 | gt_ctl_write(env, ri, GTIMER_SEC, value); | |
1782 | } | |
1783 | ||
55d284af PM |
1784 | void arm_gt_ptimer_cb(void *opaque) |
1785 | { | |
1786 | ARMCPU *cpu = opaque; | |
1787 | ||
1788 | gt_recalc_timer(cpu, GTIMER_PHYS); | |
1789 | } | |
1790 | ||
1791 | void arm_gt_vtimer_cb(void *opaque) | |
1792 | { | |
1793 | ARMCPU *cpu = opaque; | |
1794 | ||
1795 | gt_recalc_timer(cpu, GTIMER_VIRT); | |
1796 | } | |
1797 | ||
b0e66d95 EI |
1798 | void arm_gt_htimer_cb(void *opaque) |
1799 | { | |
1800 | ARMCPU *cpu = opaque; | |
1801 | ||
1802 | gt_recalc_timer(cpu, GTIMER_HYP); | |
1803 | } | |
1804 | ||
b4d3978c PM |
1805 | void arm_gt_stimer_cb(void *opaque) |
1806 | { | |
1807 | ARMCPU *cpu = opaque; | |
1808 | ||
1809 | gt_recalc_timer(cpu, GTIMER_SEC); | |
1810 | } | |
1811 | ||
55d284af PM |
1812 | static const ARMCPRegInfo generic_timer_cp_reginfo[] = { |
1813 | /* Note that CNTFRQ is purely reads-as-written for the benefit | |
1814 | * of software; writing it doesn't actually change the timer frequency. | |
1815 | * Our reset value matches the fixed frequency we implement the timer at. | |
1816 | */ | |
1817 | { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, | |
7a0e58fa | 1818 | .type = ARM_CP_ALIAS, |
a7adc4b7 PM |
1819 | .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, |
1820 | .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), | |
a7adc4b7 PM |
1821 | }, |
1822 | { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, | |
1823 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, | |
1824 | .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, | |
55d284af PM |
1825 | .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), |
1826 | .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE, | |
55d284af PM |
1827 | }, |
1828 | /* overall control: mostly access permissions */ | |
a7adc4b7 PM |
1829 | { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, |
1830 | .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, | |
55d284af PM |
1831 | .access = PL1_RW, |
1832 | .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), | |
1833 | .resetvalue = 0, | |
1834 | }, | |
1835 | /* per-timer control */ | |
1836 | { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, | |
9ff9dd3c | 1837 | .secure = ARM_CP_SECSTATE_NS, |
7a0e58fa | 1838 | .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, |
a7adc4b7 PM |
1839 | .accessfn = gt_ptimer_access, |
1840 | .fieldoffset = offsetoflow32(CPUARMState, | |
1841 | cp15.c14_timer[GTIMER_PHYS].ctl), | |
0e3eca4c | 1842 | .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, |
a7adc4b7 | 1843 | }, |
9ff9dd3c PM |
1844 | { .name = "CNTP_CTL(S)", |
1845 | .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, | |
1846 | .secure = ARM_CP_SECSTATE_S, | |
1847 | .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, | |
1848 | .accessfn = gt_ptimer_access, | |
1849 | .fieldoffset = offsetoflow32(CPUARMState, | |
1850 | cp15.c14_timer[GTIMER_SEC].ctl), | |
1851 | .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, | |
1852 | }, | |
a7adc4b7 PM |
1853 | { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, |
1854 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, | |
55d284af | 1855 | .type = ARM_CP_IO, .access = PL1_RW | PL0_R, |
a7adc4b7 | 1856 | .accessfn = gt_ptimer_access, |
55d284af PM |
1857 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), |
1858 | .resetvalue = 0, | |
0e3eca4c | 1859 | .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, |
55d284af PM |
1860 | }, |
1861 | { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, | |
7a0e58fa | 1862 | .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, |
a7adc4b7 PM |
1863 | .accessfn = gt_vtimer_access, |
1864 | .fieldoffset = offsetoflow32(CPUARMState, | |
1865 | cp15.c14_timer[GTIMER_VIRT].ctl), | |
0e3eca4c | 1866 | .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, |
a7adc4b7 PM |
1867 | }, |
1868 | { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, | |
1869 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, | |
55d284af | 1870 | .type = ARM_CP_IO, .access = PL1_RW | PL0_R, |
a7adc4b7 | 1871 | .accessfn = gt_vtimer_access, |
55d284af PM |
1872 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), |
1873 | .resetvalue = 0, | |
0e3eca4c | 1874 | .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, |
55d284af PM |
1875 | }, |
1876 | /* TimerValue views: a 32 bit downcounting view of the underlying state */ | |
1877 | { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, | |
9ff9dd3c | 1878 | .secure = ARM_CP_SECSTATE_NS, |
7a0e58fa | 1879 | .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, |
00108f2d | 1880 | .accessfn = gt_ptimer_access, |
0e3eca4c | 1881 | .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, |
55d284af | 1882 | }, |
9ff9dd3c PM |
1883 | { .name = "CNTP_TVAL(S)", |
1884 | .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, | |
1885 | .secure = ARM_CP_SECSTATE_S, | |
1886 | .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, | |
1887 | .accessfn = gt_ptimer_access, | |
1888 | .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write, | |
1889 | }, | |
a7adc4b7 PM |
1890 | { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, |
1891 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, | |
7a0e58fa | 1892 | .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, |
0e3eca4c EI |
1893 | .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset, |
1894 | .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, | |
a7adc4b7 | 1895 | }, |
55d284af | 1896 | { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, |
7a0e58fa | 1897 | .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, |
00108f2d | 1898 | .accessfn = gt_vtimer_access, |
0e3eca4c | 1899 | .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, |
55d284af | 1900 | }, |
a7adc4b7 PM |
1901 | { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, |
1902 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, | |
7a0e58fa | 1903 | .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, |
0e3eca4c EI |
1904 | .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset, |
1905 | .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, | |
a7adc4b7 | 1906 | }, |
55d284af PM |
1907 | /* The counter itself */ |
1908 | { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, | |
7a0e58fa | 1909 | .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, |
00108f2d | 1910 | .accessfn = gt_pct_access, |
a7adc4b7 PM |
1911 | .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, |
1912 | }, | |
1913 | { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, | |
1914 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, | |
7a0e58fa | 1915 | .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, |
d57b9ee8 | 1916 | .accessfn = gt_pct_access, .readfn = gt_cnt_read, |
55d284af PM |
1917 | }, |
1918 | { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, | |
7a0e58fa | 1919 | .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, |
00108f2d | 1920 | .accessfn = gt_vct_access, |
edac4d8a | 1921 | .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, |
a7adc4b7 PM |
1922 | }, |
1923 | { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, | |
1924 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, | |
7a0e58fa | 1925 | .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, |
d57b9ee8 | 1926 | .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, |
55d284af PM |
1927 | }, |
1928 | /* Comparison value, indicating when the timer goes off */ | |
1929 | { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, | |
9ff9dd3c | 1930 | .secure = ARM_CP_SECSTATE_NS, |
55d284af | 1931 | .access = PL1_RW | PL0_R, |
7a0e58fa | 1932 | .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, |
55d284af | 1933 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), |
b061a82b | 1934 | .accessfn = gt_ptimer_access, |
0e3eca4c | 1935 | .writefn = gt_phys_cval_write, .raw_writefn = raw_write, |
a7adc4b7 | 1936 | }, |
9ff9dd3c PM |
1937 | { .name = "CNTP_CVAL(S)", .cp = 15, .crm = 14, .opc1 = 2, |
1938 | .secure = ARM_CP_SECSTATE_S, | |
1939 | .access = PL1_RW | PL0_R, | |
1940 | .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, | |
1941 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), | |
1942 | .accessfn = gt_ptimer_access, | |
1943 | .writefn = gt_sec_cval_write, .raw_writefn = raw_write, | |
1944 | }, | |
a7adc4b7 PM |
1945 | { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, |
1946 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, | |
1947 | .access = PL1_RW | PL0_R, | |
1948 | .type = ARM_CP_IO, | |
1949 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), | |
12cde08a | 1950 | .resetvalue = 0, .accessfn = gt_ptimer_access, |
0e3eca4c | 1951 | .writefn = gt_phys_cval_write, .raw_writefn = raw_write, |
55d284af PM |
1952 | }, |
1953 | { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, | |
1954 | .access = PL1_RW | PL0_R, | |
7a0e58fa | 1955 | .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, |
55d284af | 1956 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), |
b061a82b | 1957 | .accessfn = gt_vtimer_access, |
0e3eca4c | 1958 | .writefn = gt_virt_cval_write, .raw_writefn = raw_write, |
a7adc4b7 PM |
1959 | }, |
1960 | { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, | |
1961 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, | |
1962 | .access = PL1_RW | PL0_R, | |
1963 | .type = ARM_CP_IO, | |
1964 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), | |
1965 | .resetvalue = 0, .accessfn = gt_vtimer_access, | |
0e3eca4c | 1966 | .writefn = gt_virt_cval_write, .raw_writefn = raw_write, |
55d284af | 1967 | }, |
b4d3978c PM |
1968 | /* Secure timer -- this is actually restricted to only EL3 |
1969 | * and configurably Secure-EL1 via the accessfn. | |
1970 | */ | |
1971 | { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64, | |
1972 | .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0, | |
1973 | .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW, | |
1974 | .accessfn = gt_stimer_access, | |
1975 | .readfn = gt_sec_tval_read, | |
1976 | .writefn = gt_sec_tval_write, | |
1977 | .resetfn = gt_sec_timer_reset, | |
1978 | }, | |
1979 | { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64, | |
1980 | .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1, | |
1981 | .type = ARM_CP_IO, .access = PL1_RW, | |
1982 | .accessfn = gt_stimer_access, | |
1983 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl), | |
1984 | .resetvalue = 0, | |
1985 | .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, | |
1986 | }, | |
1987 | { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64, | |
1988 | .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2, | |
1989 | .type = ARM_CP_IO, .access = PL1_RW, | |
1990 | .accessfn = gt_stimer_access, | |
1991 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), | |
1992 | .writefn = gt_sec_cval_write, .raw_writefn = raw_write, | |
1993 | }, | |
55d284af PM |
1994 | REGINFO_SENTINEL |
1995 | }; | |
1996 | ||
1997 | #else | |
1998 | /* In user-mode none of the generic timer registers are accessible, | |
bc72ad67 | 1999 | * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs, |
55d284af PM |
2000 | * so instead just don't register any of them. |
2001 | */ | |
6cc7a3ae | 2002 | static const ARMCPRegInfo generic_timer_cp_reginfo[] = { |
6cc7a3ae PM |
2003 | REGINFO_SENTINEL |
2004 | }; | |
2005 | ||
55d284af PM |
2006 | #endif |
2007 | ||
c4241c7d | 2008 | static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
4a501606 | 2009 | { |
891a2fe7 | 2010 | if (arm_feature(env, ARM_FEATURE_LPAE)) { |
8d5c773e | 2011 | raw_write(env, ri, value); |
891a2fe7 | 2012 | } else if (arm_feature(env, ARM_FEATURE_V7)) { |
8d5c773e | 2013 | raw_write(env, ri, value & 0xfffff6ff); |
4a501606 | 2014 | } else { |
8d5c773e | 2015 | raw_write(env, ri, value & 0xfffff1ff); |
4a501606 | 2016 | } |
4a501606 PM |
2017 | } |
2018 | ||
2019 | #ifndef CONFIG_USER_ONLY | |
2020 | /* get_phys_addr() isn't present for user-mode-only targets */ | |
702a9357 | 2021 | |
3f208fd7 PM |
2022 | static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri, |
2023 | bool isread) | |
92611c00 PM |
2024 | { |
2025 | if (ri->opc2 & 4) { | |
87562e4f PM |
2026 | /* The ATS12NSO* operations must trap to EL3 if executed in |
2027 | * Secure EL1 (which can only happen if EL3 is AArch64). | |
2028 | * They are simply UNDEF if executed from NS EL1. | |
2029 | * They function normally from EL2 or EL3. | |
92611c00 | 2030 | */ |
87562e4f PM |
2031 | if (arm_current_el(env) == 1) { |
2032 | if (arm_is_secure_below_el3(env)) { | |
2033 | return CP_ACCESS_TRAP_UNCATEGORIZED_EL3; | |
2034 | } | |
2035 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
2036 | } | |
92611c00 PM |
2037 | } |
2038 | return CP_ACCESS_OK; | |
2039 | } | |
2040 | ||
060e8a48 | 2041 | static uint64_t do_ats_write(CPUARMState *env, uint64_t value, |
d3649702 | 2042 | int access_type, ARMMMUIdx mmu_idx) |
4a501606 | 2043 | { |
a8170e5e | 2044 | hwaddr phys_addr; |
4a501606 PM |
2045 | target_ulong page_size; |
2046 | int prot; | |
b7cc4e82 PC |
2047 | uint32_t fsr; |
2048 | bool ret; | |
01c097f7 | 2049 | uint64_t par64; |
8bf5b6a9 | 2050 | MemTxAttrs attrs = {}; |
e14b5a23 | 2051 | ARMMMUFaultInfo fi = {}; |
4a501606 | 2052 | |
d3649702 | 2053 | ret = get_phys_addr(env, value, access_type, mmu_idx, |
e14b5a23 | 2054 | &phys_addr, &attrs, &prot, &page_size, &fsr, &fi); |
702a9357 | 2055 | if (extended_addresses_enabled(env)) { |
b7cc4e82 | 2056 | /* fsr is a DFSR/IFSR value for the long descriptor |
702a9357 PM |
2057 | * translation table format, but with WnR always clear. |
2058 | * Convert it to a 64-bit PAR. | |
2059 | */ | |
01c097f7 | 2060 | par64 = (1 << 11); /* LPAE bit always set */ |
b7cc4e82 | 2061 | if (!ret) { |
702a9357 | 2062 | par64 |= phys_addr & ~0xfffULL; |
8bf5b6a9 PM |
2063 | if (!attrs.secure) { |
2064 | par64 |= (1 << 9); /* NS */ | |
2065 | } | |
702a9357 | 2066 | /* We don't set the ATTR or SH fields in the PAR. */ |
4a501606 | 2067 | } else { |
702a9357 | 2068 | par64 |= 1; /* F */ |
b7cc4e82 | 2069 | par64 |= (fsr & 0x3f) << 1; /* FS */ |
702a9357 PM |
2070 | /* Note that S2WLK and FSTAGE are always zero, because we don't |
2071 | * implement virtualization and therefore there can't be a stage 2 | |
2072 | * fault. | |
2073 | */ | |
4a501606 PM |
2074 | } |
2075 | } else { | |
b7cc4e82 | 2076 | /* fsr is a DFSR/IFSR value for the short descriptor |
702a9357 PM |
2077 | * translation table format (with WnR always clear). |
2078 | * Convert it to a 32-bit PAR. | |
2079 | */ | |
b7cc4e82 | 2080 | if (!ret) { |
702a9357 PM |
2081 | /* We do not set any attribute bits in the PAR */ |
2082 | if (page_size == (1 << 24) | |
2083 | && arm_feature(env, ARM_FEATURE_V7)) { | |
01c097f7 | 2084 | par64 = (phys_addr & 0xff000000) | (1 << 1); |
702a9357 | 2085 | } else { |
01c097f7 | 2086 | par64 = phys_addr & 0xfffff000; |
702a9357 | 2087 | } |
8bf5b6a9 PM |
2088 | if (!attrs.secure) { |
2089 | par64 |= (1 << 9); /* NS */ | |
2090 | } | |
702a9357 | 2091 | } else { |
b7cc4e82 PC |
2092 | par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) | |
2093 | ((fsr & 0xf) << 1) | 1; | |
702a9357 | 2094 | } |
4a501606 | 2095 | } |
060e8a48 PM |
2096 | return par64; |
2097 | } | |
2098 | ||
2099 | static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) | |
2100 | { | |
060e8a48 PM |
2101 | int access_type = ri->opc2 & 1; |
2102 | uint64_t par64; | |
d3649702 PM |
2103 | ARMMMUIdx mmu_idx; |
2104 | int el = arm_current_el(env); | |
2105 | bool secure = arm_is_secure_below_el3(env); | |
060e8a48 | 2106 | |
d3649702 PM |
2107 | switch (ri->opc2 & 6) { |
2108 | case 0: | |
2109 | /* stage 1 current state PL1: ATS1CPR, ATS1CPW */ | |
2110 | switch (el) { | |
2111 | case 3: | |
2112 | mmu_idx = ARMMMUIdx_S1E3; | |
2113 | break; | |
2114 | case 2: | |
2115 | mmu_idx = ARMMMUIdx_S1NSE1; | |
2116 | break; | |
2117 | case 1: | |
2118 | mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; | |
2119 | break; | |
2120 | default: | |
2121 | g_assert_not_reached(); | |
2122 | } | |
2123 | break; | |
2124 | case 2: | |
2125 | /* stage 1 current state PL0: ATS1CUR, ATS1CUW */ | |
2126 | switch (el) { | |
2127 | case 3: | |
2128 | mmu_idx = ARMMMUIdx_S1SE0; | |
2129 | break; | |
2130 | case 2: | |
2131 | mmu_idx = ARMMMUIdx_S1NSE0; | |
2132 | break; | |
2133 | case 1: | |
2134 | mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; | |
2135 | break; | |
2136 | default: | |
2137 | g_assert_not_reached(); | |
2138 | } | |
2139 | break; | |
2140 | case 4: | |
2141 | /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */ | |
2142 | mmu_idx = ARMMMUIdx_S12NSE1; | |
2143 | break; | |
2144 | case 6: | |
2145 | /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */ | |
2146 | mmu_idx = ARMMMUIdx_S12NSE0; | |
2147 | break; | |
2148 | default: | |
2149 | g_assert_not_reached(); | |
2150 | } | |
2151 | ||
2152 | par64 = do_ats_write(env, value, access_type, mmu_idx); | |
01c097f7 FA |
2153 | |
2154 | A32_BANKED_CURRENT_REG_SET(env, par, par64); | |
4a501606 | 2155 | } |
060e8a48 | 2156 | |
14db7fe0 PM |
2157 | static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2158 | uint64_t value) | |
2159 | { | |
2160 | int access_type = ri->opc2 & 1; | |
2161 | uint64_t par64; | |
2162 | ||
2163 | par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS); | |
2164 | ||
2165 | A32_BANKED_CURRENT_REG_SET(env, par, par64); | |
2166 | } | |
2167 | ||
3f208fd7 PM |
2168 | static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri, |
2169 | bool isread) | |
2a47df95 PM |
2170 | { |
2171 | if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) { | |
2172 | return CP_ACCESS_TRAP; | |
2173 | } | |
2174 | return CP_ACCESS_OK; | |
2175 | } | |
2176 | ||
060e8a48 PM |
2177 | static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri, |
2178 | uint64_t value) | |
2179 | { | |
060e8a48 | 2180 | int access_type = ri->opc2 & 1; |
d3649702 PM |
2181 | ARMMMUIdx mmu_idx; |
2182 | int secure = arm_is_secure_below_el3(env); | |
2183 | ||
2184 | switch (ri->opc2 & 6) { | |
2185 | case 0: | |
2186 | switch (ri->opc1) { | |
2187 | case 0: /* AT S1E1R, AT S1E1W */ | |
2188 | mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; | |
2189 | break; | |
2190 | case 4: /* AT S1E2R, AT S1E2W */ | |
2191 | mmu_idx = ARMMMUIdx_S1E2; | |
2192 | break; | |
2193 | case 6: /* AT S1E3R, AT S1E3W */ | |
2194 | mmu_idx = ARMMMUIdx_S1E3; | |
2195 | break; | |
2196 | default: | |
2197 | g_assert_not_reached(); | |
2198 | } | |
2199 | break; | |
2200 | case 2: /* AT S1E0R, AT S1E0W */ | |
2201 | mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; | |
2202 | break; | |
2203 | case 4: /* AT S12E1R, AT S12E1W */ | |
2a47df95 | 2204 | mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1; |
d3649702 PM |
2205 | break; |
2206 | case 6: /* AT S12E0R, AT S12E0W */ | |
2a47df95 | 2207 | mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0; |
d3649702 PM |
2208 | break; |
2209 | default: | |
2210 | g_assert_not_reached(); | |
2211 | } | |
060e8a48 | 2212 | |
d3649702 | 2213 | env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx); |
060e8a48 | 2214 | } |
4a501606 PM |
2215 | #endif |
2216 | ||
2217 | static const ARMCPRegInfo vapa_cp_reginfo[] = { | |
2218 | { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, | |
2219 | .access = PL1_RW, .resetvalue = 0, | |
01c097f7 FA |
2220 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), |
2221 | offsetoflow32(CPUARMState, cp15.par_ns) }, | |
4a501606 PM |
2222 | .writefn = par_write }, |
2223 | #ifndef CONFIG_USER_ONLY | |
87562e4f | 2224 | /* This underdecoding is safe because the reginfo is NO_RAW. */ |
4a501606 | 2225 | { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, |
92611c00 | 2226 | .access = PL1_W, .accessfn = ats_access, |
7a0e58fa | 2227 | .writefn = ats_write, .type = ARM_CP_NO_RAW }, |
4a501606 PM |
2228 | #endif |
2229 | REGINFO_SENTINEL | |
2230 | }; | |
2231 | ||
18032bec PM |
2232 | /* Return basic MPU access permission bits. */ |
2233 | static uint32_t simple_mpu_ap_bits(uint32_t val) | |
2234 | { | |
2235 | uint32_t ret; | |
2236 | uint32_t mask; | |
2237 | int i; | |
2238 | ret = 0; | |
2239 | mask = 3; | |
2240 | for (i = 0; i < 16; i += 2) { | |
2241 | ret |= (val >> i) & mask; | |
2242 | mask <<= 2; | |
2243 | } | |
2244 | return ret; | |
2245 | } | |
2246 | ||
2247 | /* Pad basic MPU access permission bits to extended format. */ | |
2248 | static uint32_t extended_mpu_ap_bits(uint32_t val) | |
2249 | { | |
2250 | uint32_t ret; | |
2251 | uint32_t mask; | |
2252 | int i; | |
2253 | ret = 0; | |
2254 | mask = 3; | |
2255 | for (i = 0; i < 16; i += 2) { | |
2256 | ret |= (val & mask) << i; | |
2257 | mask <<= 2; | |
2258 | } | |
2259 | return ret; | |
2260 | } | |
2261 | ||
c4241c7d PM |
2262 | static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2263 | uint64_t value) | |
18032bec | 2264 | { |
7e09797c | 2265 | env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); |
18032bec PM |
2266 | } |
2267 | ||
c4241c7d | 2268 | static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) |
18032bec | 2269 | { |
7e09797c | 2270 | return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); |
18032bec PM |
2271 | } |
2272 | ||
c4241c7d PM |
2273 | static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2274 | uint64_t value) | |
18032bec | 2275 | { |
7e09797c | 2276 | env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); |
18032bec PM |
2277 | } |
2278 | ||
c4241c7d | 2279 | static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) |
18032bec | 2280 | { |
7e09797c | 2281 | return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); |
18032bec PM |
2282 | } |
2283 | ||
6cb0b013 PC |
2284 | static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri) |
2285 | { | |
2286 | uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); | |
2287 | ||
2288 | if (!u32p) { | |
2289 | return 0; | |
2290 | } | |
2291 | ||
2292 | u32p += env->cp15.c6_rgnr; | |
2293 | return *u32p; | |
2294 | } | |
2295 | ||
2296 | static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2297 | uint64_t value) | |
2298 | { | |
2299 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2300 | uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); | |
2301 | ||
2302 | if (!u32p) { | |
2303 | return; | |
2304 | } | |
2305 | ||
2306 | u32p += env->cp15.c6_rgnr; | |
2307 | tlb_flush(CPU(cpu), 1); /* Mappings may have changed - purge! */ | |
2308 | *u32p = value; | |
2309 | } | |
2310 | ||
2311 | static void pmsav7_reset(CPUARMState *env, const ARMCPRegInfo *ri) | |
2312 | { | |
2313 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2314 | uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); | |
2315 | ||
2316 | if (!u32p) { | |
2317 | return; | |
2318 | } | |
2319 | ||
2320 | memset(u32p, 0, sizeof(*u32p) * cpu->pmsav7_dregion); | |
2321 | } | |
2322 | ||
2323 | static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2324 | uint64_t value) | |
2325 | { | |
2326 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2327 | uint32_t nrgs = cpu->pmsav7_dregion; | |
2328 | ||
2329 | if (value >= nrgs) { | |
2330 | qemu_log_mask(LOG_GUEST_ERROR, | |
2331 | "PMSAv7 RGNR write >= # supported regions, %" PRIu32 | |
2332 | " > %" PRIu32 "\n", (uint32_t)value, nrgs); | |
2333 | return; | |
2334 | } | |
2335 | ||
2336 | raw_write(env, ri, value); | |
2337 | } | |
2338 | ||
2339 | static const ARMCPRegInfo pmsav7_cp_reginfo[] = { | |
2340 | { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0, | |
2341 | .access = PL1_RW, .type = ARM_CP_NO_RAW, | |
2342 | .fieldoffset = offsetof(CPUARMState, pmsav7.drbar), | |
2343 | .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset }, | |
2344 | { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2, | |
2345 | .access = PL1_RW, .type = ARM_CP_NO_RAW, | |
2346 | .fieldoffset = offsetof(CPUARMState, pmsav7.drsr), | |
2347 | .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset }, | |
2348 | { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4, | |
2349 | .access = PL1_RW, .type = ARM_CP_NO_RAW, | |
2350 | .fieldoffset = offsetof(CPUARMState, pmsav7.dracr), | |
2351 | .readfn = pmsav7_read, .writefn = pmsav7_write, .resetfn = pmsav7_reset }, | |
2352 | { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0, | |
2353 | .access = PL1_RW, | |
2354 | .fieldoffset = offsetof(CPUARMState, cp15.c6_rgnr), | |
2355 | .writefn = pmsav7_rgnr_write }, | |
2356 | REGINFO_SENTINEL | |
2357 | }; | |
2358 | ||
18032bec PM |
2359 | static const ARMCPRegInfo pmsav5_cp_reginfo[] = { |
2360 | { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, | |
7a0e58fa | 2361 | .access = PL1_RW, .type = ARM_CP_ALIAS, |
7e09797c | 2362 | .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), |
18032bec PM |
2363 | .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, |
2364 | { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, | |
7a0e58fa | 2365 | .access = PL1_RW, .type = ARM_CP_ALIAS, |
7e09797c | 2366 | .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), |
18032bec PM |
2367 | .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, |
2368 | { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, | |
2369 | .access = PL1_RW, | |
7e09797c PM |
2370 | .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), |
2371 | .resetvalue = 0, }, | |
18032bec PM |
2372 | { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, |
2373 | .access = PL1_RW, | |
7e09797c PM |
2374 | .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), |
2375 | .resetvalue = 0, }, | |
ecce5c3c PM |
2376 | { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, |
2377 | .access = PL1_RW, | |
2378 | .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, | |
2379 | { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, | |
2380 | .access = PL1_RW, | |
2381 | .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, | |
06d76f31 | 2382 | /* Protection region base and size registers */ |
e508a92b PM |
2383 | { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, |
2384 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
2385 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, | |
2386 | { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, | |
2387 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
2388 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, | |
2389 | { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, | |
2390 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
2391 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, | |
2392 | { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, | |
2393 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
2394 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, | |
2395 | { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, | |
2396 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
2397 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, | |
2398 | { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, | |
2399 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
2400 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, | |
2401 | { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, | |
2402 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
2403 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, | |
2404 | { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, | |
2405 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
2406 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, | |
18032bec PM |
2407 | REGINFO_SENTINEL |
2408 | }; | |
2409 | ||
c4241c7d PM |
2410 | static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2411 | uint64_t value) | |
ecce5c3c | 2412 | { |
11f136ee | 2413 | TCR *tcr = raw_ptr(env, ri); |
2ebcebe2 PM |
2414 | int maskshift = extract32(value, 0, 3); |
2415 | ||
e389be16 FA |
2416 | if (!arm_feature(env, ARM_FEATURE_V8)) { |
2417 | if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { | |
2418 | /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when | |
2419 | * using Long-desciptor translation table format */ | |
2420 | value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); | |
2421 | } else if (arm_feature(env, ARM_FEATURE_EL3)) { | |
2422 | /* In an implementation that includes the Security Extensions | |
2423 | * TTBCR has additional fields PD0 [4] and PD1 [5] for | |
2424 | * Short-descriptor translation table format. | |
2425 | */ | |
2426 | value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; | |
2427 | } else { | |
2428 | value &= TTBCR_N; | |
2429 | } | |
e42c4db3 | 2430 | } |
e389be16 | 2431 | |
b6af0975 | 2432 | /* Update the masks corresponding to the TCR bank being written |
11f136ee | 2433 | * Note that we always calculate mask and base_mask, but |
e42c4db3 | 2434 | * they are only used for short-descriptor tables (ie if EAE is 0); |
11f136ee FA |
2435 | * for long-descriptor tables the TCR fields are used differently |
2436 | * and the mask and base_mask values are meaningless. | |
e42c4db3 | 2437 | */ |
11f136ee FA |
2438 | tcr->raw_tcr = value; |
2439 | tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift); | |
2440 | tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift); | |
ecce5c3c PM |
2441 | } |
2442 | ||
c4241c7d PM |
2443 | static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2444 | uint64_t value) | |
d4e6df63 | 2445 | { |
00c8cb0a AF |
2446 | ARMCPU *cpu = arm_env_get_cpu(env); |
2447 | ||
d4e6df63 PM |
2448 | if (arm_feature(env, ARM_FEATURE_LPAE)) { |
2449 | /* With LPAE the TTBCR could result in a change of ASID | |
2450 | * via the TTBCR.A1 bit, so do a TLB flush. | |
2451 | */ | |
00c8cb0a | 2452 | tlb_flush(CPU(cpu), 1); |
d4e6df63 | 2453 | } |
c4241c7d | 2454 | vmsa_ttbcr_raw_write(env, ri, value); |
d4e6df63 PM |
2455 | } |
2456 | ||
ecce5c3c PM |
2457 | static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
2458 | { | |
11f136ee FA |
2459 | TCR *tcr = raw_ptr(env, ri); |
2460 | ||
2461 | /* Reset both the TCR as well as the masks corresponding to the bank of | |
2462 | * the TCR being reset. | |
2463 | */ | |
2464 | tcr->raw_tcr = 0; | |
2465 | tcr->mask = 0; | |
2466 | tcr->base_mask = 0xffffc000u; | |
ecce5c3c PM |
2467 | } |
2468 | ||
cb2e37df PM |
2469 | static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2470 | uint64_t value) | |
2471 | { | |
00c8cb0a | 2472 | ARMCPU *cpu = arm_env_get_cpu(env); |
11f136ee | 2473 | TCR *tcr = raw_ptr(env, ri); |
00c8cb0a | 2474 | |
cb2e37df | 2475 | /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ |
00c8cb0a | 2476 | tlb_flush(CPU(cpu), 1); |
11f136ee | 2477 | tcr->raw_tcr = value; |
cb2e37df PM |
2478 | } |
2479 | ||
327ed10f PM |
2480 | static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2481 | uint64_t value) | |
2482 | { | |
2483 | /* 64 bit accesses to the TTBRs can change the ASID and so we | |
2484 | * must flush the TLB. | |
2485 | */ | |
2486 | if (cpreg_field_is_64bit(ri)) { | |
00c8cb0a AF |
2487 | ARMCPU *cpu = arm_env_get_cpu(env); |
2488 | ||
2489 | tlb_flush(CPU(cpu), 1); | |
327ed10f PM |
2490 | } |
2491 | raw_write(env, ri, value); | |
2492 | } | |
2493 | ||
b698e9cf EI |
2494 | static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2495 | uint64_t value) | |
2496 | { | |
2497 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2498 | CPUState *cs = CPU(cpu); | |
2499 | ||
2500 | /* Accesses to VTTBR may change the VMID so we must flush the TLB. */ | |
2501 | if (raw_read(env, ri) != value) { | |
2502 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, | |
2503 | ARMMMUIdx_S2NS, -1); | |
2504 | raw_write(env, ri, value); | |
2505 | } | |
2506 | } | |
2507 | ||
8e5d75c9 | 2508 | static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = { |
18032bec | 2509 | { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, |
7a0e58fa | 2510 | .access = PL1_RW, .type = ARM_CP_ALIAS, |
4a7e2d73 | 2511 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), |
b061a82b | 2512 | offsetoflow32(CPUARMState, cp15.dfsr_ns) }, }, |
18032bec | 2513 | { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, |
88ca1c2d FA |
2514 | .access = PL1_RW, .resetvalue = 0, |
2515 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), | |
2516 | offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, | |
8e5d75c9 PC |
2517 | { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, |
2518 | .access = PL1_RW, .resetvalue = 0, | |
2519 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), | |
2520 | offsetof(CPUARMState, cp15.dfar_ns) } }, | |
2521 | { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, | |
2522 | .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, | |
2523 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), | |
2524 | .resetvalue = 0, }, | |
2525 | REGINFO_SENTINEL | |
2526 | }; | |
2527 | ||
2528 | static const ARMCPRegInfo vmsa_cp_reginfo[] = { | |
6cd8a264 RH |
2529 | { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, |
2530 | .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, | |
2531 | .access = PL1_RW, | |
d81c519c | 2532 | .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, |
327ed10f | 2533 | { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, |
7dd8c9af FA |
2534 | .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, |
2535 | .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, | |
2536 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), | |
2537 | offsetof(CPUARMState, cp15.ttbr0_ns) } }, | |
327ed10f | 2538 | { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, |
7dd8c9af FA |
2539 | .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, |
2540 | .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, | |
2541 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), | |
2542 | offsetof(CPUARMState, cp15.ttbr1_ns) } }, | |
cb2e37df PM |
2543 | { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, |
2544 | .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, | |
2545 | .access = PL1_RW, .writefn = vmsa_tcr_el1_write, | |
2546 | .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write, | |
11f136ee | 2547 | .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, |
cb2e37df | 2548 | { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, |
7a0e58fa | 2549 | .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write, |
b061a82b | 2550 | .raw_writefn = vmsa_ttbcr_raw_write, |
11f136ee FA |
2551 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), |
2552 | offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, | |
18032bec PM |
2553 | REGINFO_SENTINEL |
2554 | }; | |
2555 | ||
c4241c7d PM |
2556 | static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2557 | uint64_t value) | |
1047b9d7 PM |
2558 | { |
2559 | env->cp15.c15_ticonfig = value & 0xe7; | |
2560 | /* The OS_TYPE bit in this register changes the reported CPUID! */ | |
2561 | env->cp15.c0_cpuid = (value & (1 << 5)) ? | |
2562 | ARM_CPUID_TI915T : ARM_CPUID_TI925T; | |
1047b9d7 PM |
2563 | } |
2564 | ||
c4241c7d PM |
2565 | static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2566 | uint64_t value) | |
1047b9d7 PM |
2567 | { |
2568 | env->cp15.c15_threadid = value & 0xffff; | |
1047b9d7 PM |
2569 | } |
2570 | ||
c4241c7d PM |
2571 | static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2572 | uint64_t value) | |
1047b9d7 PM |
2573 | { |
2574 | /* Wait-for-interrupt (deprecated) */ | |
c3affe56 | 2575 | cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT); |
1047b9d7 PM |
2576 | } |
2577 | ||
c4241c7d PM |
2578 | static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2579 | uint64_t value) | |
c4804214 PM |
2580 | { |
2581 | /* On OMAP there are registers indicating the max/min index of dcache lines | |
2582 | * containing a dirty line; cache flush operations have to reset these. | |
2583 | */ | |
2584 | env->cp15.c15_i_max = 0x000; | |
2585 | env->cp15.c15_i_min = 0xff0; | |
c4804214 PM |
2586 | } |
2587 | ||
18032bec PM |
2588 | static const ARMCPRegInfo omap_cp_reginfo[] = { |
2589 | { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, | |
2590 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, | |
d81c519c | 2591 | .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), |
6cd8a264 | 2592 | .resetvalue = 0, }, |
1047b9d7 PM |
2593 | { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, |
2594 | .access = PL1_RW, .type = ARM_CP_NOP }, | |
2595 | { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, | |
2596 | .access = PL1_RW, | |
2597 | .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, | |
2598 | .writefn = omap_ticonfig_write }, | |
2599 | { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, | |
2600 | .access = PL1_RW, | |
2601 | .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, | |
2602 | { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, | |
2603 | .access = PL1_RW, .resetvalue = 0xff0, | |
2604 | .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, | |
2605 | { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, | |
2606 | .access = PL1_RW, | |
2607 | .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, | |
2608 | .writefn = omap_threadid_write }, | |
2609 | { .name = "TI925T_STATUS", .cp = 15, .crn = 15, | |
2610 | .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, | |
7a0e58fa | 2611 | .type = ARM_CP_NO_RAW, |
1047b9d7 PM |
2612 | .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, |
2613 | /* TODO: Peripheral port remap register: | |
2614 | * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller | |
2615 | * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), | |
2616 | * when MMU is off. | |
2617 | */ | |
c4804214 | 2618 | { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, |
d4e6df63 | 2619 | .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, |
7a0e58fa | 2620 | .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW, |
c4804214 | 2621 | .writefn = omap_cachemaint_write }, |
34f90529 PM |
2622 | { .name = "C9", .cp = 15, .crn = 9, |
2623 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, | |
2624 | .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, | |
1047b9d7 PM |
2625 | REGINFO_SENTINEL |
2626 | }; | |
2627 | ||
c4241c7d PM |
2628 | static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2629 | uint64_t value) | |
1047b9d7 | 2630 | { |
c0f4af17 | 2631 | env->cp15.c15_cpar = value & 0x3fff; |
1047b9d7 PM |
2632 | } |
2633 | ||
2634 | static const ARMCPRegInfo xscale_cp_reginfo[] = { | |
2635 | { .name = "XSCALE_CPAR", | |
2636 | .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, | |
2637 | .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, | |
2638 | .writefn = xscale_cpar_write, }, | |
2771db27 PM |
2639 | { .name = "XSCALE_AUXCR", |
2640 | .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, | |
2641 | .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), | |
2642 | .resetvalue = 0, }, | |
3b771579 PM |
2643 | /* XScale specific cache-lockdown: since we have no cache we NOP these |
2644 | * and hope the guest does not really rely on cache behaviour. | |
2645 | */ | |
2646 | { .name = "XSCALE_LOCK_ICACHE_LINE", | |
2647 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, | |
2648 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2649 | { .name = "XSCALE_UNLOCK_ICACHE", | |
2650 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, | |
2651 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2652 | { .name = "XSCALE_DCACHE_LOCK", | |
2653 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, | |
2654 | .access = PL1_RW, .type = ARM_CP_NOP }, | |
2655 | { .name = "XSCALE_UNLOCK_DCACHE", | |
2656 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, | |
2657 | .access = PL1_W, .type = ARM_CP_NOP }, | |
1047b9d7 PM |
2658 | REGINFO_SENTINEL |
2659 | }; | |
2660 | ||
2661 | static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { | |
2662 | /* RAZ/WI the whole crn=15 space, when we don't have a more specific | |
2663 | * implementation of this implementation-defined space. | |
2664 | * Ideally this should eventually disappear in favour of actually | |
2665 | * implementing the correct behaviour for all cores. | |
2666 | */ | |
2667 | { .name = "C15_IMPDEF", .cp = 15, .crn = 15, | |
2668 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, | |
3671cd87 | 2669 | .access = PL1_RW, |
7a0e58fa | 2670 | .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE, |
d4e6df63 | 2671 | .resetvalue = 0 }, |
18032bec PM |
2672 | REGINFO_SENTINEL |
2673 | }; | |
2674 | ||
c4804214 PM |
2675 | static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { |
2676 | /* Cache status: RAZ because we have no cache so it's always clean */ | |
2677 | { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, | |
7a0e58fa | 2678 | .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, |
d4e6df63 | 2679 | .resetvalue = 0 }, |
c4804214 PM |
2680 | REGINFO_SENTINEL |
2681 | }; | |
2682 | ||
2683 | static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { | |
2684 | /* We never have a a block transfer operation in progress */ | |
2685 | { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, | |
7a0e58fa | 2686 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, |
d4e6df63 | 2687 | .resetvalue = 0 }, |
30b05bba PM |
2688 | /* The cache ops themselves: these all NOP for QEMU */ |
2689 | { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, | |
2690 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
2691 | { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, | |
2692 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
2693 | { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, | |
2694 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
2695 | { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, | |
2696 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
2697 | { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, | |
2698 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
2699 | { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, | |
2700 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
c4804214 PM |
2701 | REGINFO_SENTINEL |
2702 | }; | |
2703 | ||
2704 | static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { | |
2705 | /* The cache test-and-clean instructions always return (1 << 30) | |
2706 | * to indicate that there are no dirty cache lines. | |
2707 | */ | |
2708 | { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, | |
7a0e58fa | 2709 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, |
d4e6df63 | 2710 | .resetvalue = (1 << 30) }, |
c4804214 | 2711 | { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, |
7a0e58fa | 2712 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, |
d4e6df63 | 2713 | .resetvalue = (1 << 30) }, |
c4804214 PM |
2714 | REGINFO_SENTINEL |
2715 | }; | |
2716 | ||
34f90529 PM |
2717 | static const ARMCPRegInfo strongarm_cp_reginfo[] = { |
2718 | /* Ignore ReadBuffer accesses */ | |
2719 | { .name = "C9_READBUFFER", .cp = 15, .crn = 9, | |
2720 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, | |
d4e6df63 | 2721 | .access = PL1_RW, .resetvalue = 0, |
7a0e58fa | 2722 | .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW }, |
34f90529 PM |
2723 | REGINFO_SENTINEL |
2724 | }; | |
2725 | ||
731de9e6 EI |
2726 | static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri) |
2727 | { | |
2728 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2729 | unsigned int cur_el = arm_current_el(env); | |
2730 | bool secure = arm_is_secure(env); | |
2731 | ||
2732 | if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { | |
2733 | return env->cp15.vpidr_el2; | |
2734 | } | |
2735 | return raw_read(env, ri); | |
2736 | } | |
2737 | ||
06a7e647 | 2738 | static uint64_t mpidr_read_val(CPUARMState *env) |
81bdde9d | 2739 | { |
eb5e1d3c PF |
2740 | ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env)); |
2741 | uint64_t mpidr = cpu->mp_affinity; | |
2742 | ||
81bdde9d | 2743 | if (arm_feature(env, ARM_FEATURE_V7MP)) { |
78dbbbe4 | 2744 | mpidr |= (1U << 31); |
81bdde9d PM |
2745 | /* Cores which are uniprocessor (non-coherent) |
2746 | * but still implement the MP extensions set | |
a8e81b31 | 2747 | * bit 30. (For instance, Cortex-R5). |
81bdde9d | 2748 | */ |
a8e81b31 PC |
2749 | if (cpu->mp_is_up) { |
2750 | mpidr |= (1u << 30); | |
2751 | } | |
81bdde9d | 2752 | } |
c4241c7d | 2753 | return mpidr; |
81bdde9d PM |
2754 | } |
2755 | ||
06a7e647 EI |
2756 | static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) |
2757 | { | |
f0d574d6 EI |
2758 | unsigned int cur_el = arm_current_el(env); |
2759 | bool secure = arm_is_secure(env); | |
2760 | ||
2761 | if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { | |
2762 | return env->cp15.vmpidr_el2; | |
2763 | } | |
06a7e647 EI |
2764 | return mpidr_read_val(env); |
2765 | } | |
2766 | ||
81bdde9d | 2767 | static const ARMCPRegInfo mpidr_cp_reginfo[] = { |
4b7fff2f PM |
2768 | { .name = "MPIDR", .state = ARM_CP_STATE_BOTH, |
2769 | .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, | |
7a0e58fa | 2770 | .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW }, |
81bdde9d PM |
2771 | REGINFO_SENTINEL |
2772 | }; | |
2773 | ||
7ac681cf | 2774 | static const ARMCPRegInfo lpae_cp_reginfo[] = { |
a903c449 | 2775 | /* NOP AMAIR0/1 */ |
b0fe2427 PM |
2776 | { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, |
2777 | .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, | |
a903c449 | 2778 | .access = PL1_RW, .type = ARM_CP_CONST, |
7ac681cf | 2779 | .resetvalue = 0 }, |
b0fe2427 | 2780 | /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ |
7ac681cf | 2781 | { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, |
a903c449 | 2782 | .access = PL1_RW, .type = ARM_CP_CONST, |
7ac681cf | 2783 | .resetvalue = 0 }, |
891a2fe7 | 2784 | { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, |
01c097f7 FA |
2785 | .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, |
2786 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), | |
2787 | offsetof(CPUARMState, cp15.par_ns)} }, | |
891a2fe7 | 2788 | { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, |
7a0e58fa | 2789 | .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, |
7dd8c9af FA |
2790 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), |
2791 | offsetof(CPUARMState, cp15.ttbr0_ns) }, | |
b061a82b | 2792 | .writefn = vmsa_ttbr_write, }, |
891a2fe7 | 2793 | { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, |
7a0e58fa | 2794 | .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, |
7dd8c9af FA |
2795 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), |
2796 | offsetof(CPUARMState, cp15.ttbr1_ns) }, | |
b061a82b | 2797 | .writefn = vmsa_ttbr_write, }, |
7ac681cf PM |
2798 | REGINFO_SENTINEL |
2799 | }; | |
2800 | ||
c4241c7d | 2801 | static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) |
b0d2b7d0 | 2802 | { |
c4241c7d | 2803 | return vfp_get_fpcr(env); |
b0d2b7d0 PM |
2804 | } |
2805 | ||
c4241c7d PM |
2806 | static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2807 | uint64_t value) | |
b0d2b7d0 PM |
2808 | { |
2809 | vfp_set_fpcr(env, value); | |
b0d2b7d0 PM |
2810 | } |
2811 | ||
c4241c7d | 2812 | static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) |
b0d2b7d0 | 2813 | { |
c4241c7d | 2814 | return vfp_get_fpsr(env); |
b0d2b7d0 PM |
2815 | } |
2816 | ||
c4241c7d PM |
2817 | static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2818 | uint64_t value) | |
b0d2b7d0 PM |
2819 | { |
2820 | vfp_set_fpsr(env, value); | |
b0d2b7d0 PM |
2821 | } |
2822 | ||
3f208fd7 PM |
2823 | static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri, |
2824 | bool isread) | |
c2b820fe | 2825 | { |
137feaa9 | 2826 | if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) { |
c2b820fe PM |
2827 | return CP_ACCESS_TRAP; |
2828 | } | |
2829 | return CP_ACCESS_OK; | |
2830 | } | |
2831 | ||
2832 | static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2833 | uint64_t value) | |
2834 | { | |
2835 | env->daif = value & PSTATE_DAIF; | |
2836 | } | |
2837 | ||
8af35c37 | 2838 | static CPAccessResult aa64_cacheop_access(CPUARMState *env, |
3f208fd7 PM |
2839 | const ARMCPRegInfo *ri, |
2840 | bool isread) | |
8af35c37 PM |
2841 | { |
2842 | /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless | |
2843 | * SCTLR_EL1.UCI is set. | |
2844 | */ | |
137feaa9 | 2845 | if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) { |
8af35c37 PM |
2846 | return CP_ACCESS_TRAP; |
2847 | } | |
2848 | return CP_ACCESS_OK; | |
2849 | } | |
2850 | ||
dbb1fb27 AB |
2851 | /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions |
2852 | * Page D4-1736 (DDI0487A.b) | |
2853 | */ | |
2854 | ||
fd3ed969 PM |
2855 | static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2856 | uint64_t value) | |
168aa23b | 2857 | { |
31b030d4 | 2858 | ARMCPU *cpu = arm_env_get_cpu(env); |
fd3ed969 | 2859 | CPUState *cs = CPU(cpu); |
dbb1fb27 | 2860 | |
fd3ed969 PM |
2861 | if (arm_is_secure_below_el3(env)) { |
2862 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1); | |
2863 | } else { | |
2864 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1); | |
2865 | } | |
168aa23b PM |
2866 | } |
2867 | ||
fd3ed969 PM |
2868 | static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2869 | uint64_t value) | |
168aa23b | 2870 | { |
fd3ed969 PM |
2871 | bool sec = arm_is_secure_below_el3(env); |
2872 | CPUState *other_cs; | |
dbb1fb27 | 2873 | |
fd3ed969 PM |
2874 | CPU_FOREACH(other_cs) { |
2875 | if (sec) { | |
2876 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1); | |
2877 | } else { | |
2878 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1, | |
2879 | ARMMMUIdx_S12NSE0, -1); | |
2880 | } | |
2881 | } | |
168aa23b PM |
2882 | } |
2883 | ||
fd3ed969 PM |
2884 | static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2885 | uint64_t value) | |
168aa23b | 2886 | { |
fd3ed969 PM |
2887 | /* Note that the 'ALL' scope must invalidate both stage 1 and |
2888 | * stage 2 translations, whereas most other scopes only invalidate | |
2889 | * stage 1 translations. | |
2890 | */ | |
00c8cb0a | 2891 | ARMCPU *cpu = arm_env_get_cpu(env); |
fd3ed969 PM |
2892 | CPUState *cs = CPU(cpu); |
2893 | ||
2894 | if (arm_is_secure_below_el3(env)) { | |
2895 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1); | |
2896 | } else { | |
2897 | if (arm_feature(env, ARM_FEATURE_EL2)) { | |
2898 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, | |
2899 | ARMMMUIdx_S2NS, -1); | |
2900 | } else { | |
2901 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S12NSE1, ARMMMUIdx_S12NSE0, -1); | |
2902 | } | |
2903 | } | |
168aa23b PM |
2904 | } |
2905 | ||
fd3ed969 | 2906 | static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri, |
fa439fc5 PM |
2907 | uint64_t value) |
2908 | { | |
fd3ed969 PM |
2909 | ARMCPU *cpu = arm_env_get_cpu(env); |
2910 | CPUState *cs = CPU(cpu); | |
2911 | ||
2912 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E2, -1); | |
2913 | } | |
2914 | ||
43efaa33 PM |
2915 | static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2916 | uint64_t value) | |
2917 | { | |
2918 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2919 | CPUState *cs = CPU(cpu); | |
2920 | ||
2921 | tlb_flush_by_mmuidx(cs, ARMMMUIdx_S1E3, -1); | |
2922 | } | |
2923 | ||
fd3ed969 PM |
2924 | static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2925 | uint64_t value) | |
2926 | { | |
2927 | /* Note that the 'ALL' scope must invalidate both stage 1 and | |
2928 | * stage 2 translations, whereas most other scopes only invalidate | |
2929 | * stage 1 translations. | |
2930 | */ | |
2931 | bool sec = arm_is_secure_below_el3(env); | |
2932 | bool has_el2 = arm_feature(env, ARM_FEATURE_EL2); | |
fa439fc5 | 2933 | CPUState *other_cs; |
fa439fc5 PM |
2934 | |
2935 | CPU_FOREACH(other_cs) { | |
fd3ed969 PM |
2936 | if (sec) { |
2937 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1SE1, ARMMMUIdx_S1SE0, -1); | |
2938 | } else if (has_el2) { | |
2939 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1, | |
2940 | ARMMMUIdx_S12NSE0, ARMMMUIdx_S2NS, -1); | |
2941 | } else { | |
2942 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S12NSE1, | |
2943 | ARMMMUIdx_S12NSE0, -1); | |
2944 | } | |
fa439fc5 PM |
2945 | } |
2946 | } | |
2947 | ||
2bfb9d75 PM |
2948 | static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2949 | uint64_t value) | |
2950 | { | |
2951 | CPUState *other_cs; | |
2952 | ||
2953 | CPU_FOREACH(other_cs) { | |
2954 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E2, -1); | |
2955 | } | |
2956 | } | |
2957 | ||
43efaa33 PM |
2958 | static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2959 | uint64_t value) | |
2960 | { | |
2961 | CPUState *other_cs; | |
2962 | ||
2963 | CPU_FOREACH(other_cs) { | |
2964 | tlb_flush_by_mmuidx(other_cs, ARMMMUIdx_S1E3, -1); | |
2965 | } | |
2966 | } | |
2967 | ||
fd3ed969 PM |
2968 | static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri, |
2969 | uint64_t value) | |
2970 | { | |
2971 | /* Invalidate by VA, EL1&0 (AArch64 version). | |
2972 | * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1, | |
2973 | * since we don't support flush-for-specific-ASID-only or | |
2974 | * flush-last-level-only. | |
2975 | */ | |
2976 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2977 | CPUState *cs = CPU(cpu); | |
2978 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
2979 | ||
2980 | if (arm_is_secure_below_el3(env)) { | |
2981 | tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1SE1, | |
2982 | ARMMMUIdx_S1SE0, -1); | |
2983 | } else { | |
2984 | tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S12NSE1, | |
2985 | ARMMMUIdx_S12NSE0, -1); | |
2986 | } | |
2987 | } | |
2988 | ||
2989 | static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2990 | uint64_t value) | |
fa439fc5 | 2991 | { |
fd3ed969 PM |
2992 | /* Invalidate by VA, EL2 |
2993 | * Currently handles both VAE2 and VALE2, since we don't support | |
2994 | * flush-last-level-only. | |
2995 | */ | |
2996 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2997 | CPUState *cs = CPU(cpu); | |
2998 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
2999 | ||
3000 | tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E2, -1); | |
3001 | } | |
3002 | ||
43efaa33 PM |
3003 | static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri, |
3004 | uint64_t value) | |
3005 | { | |
3006 | /* Invalidate by VA, EL3 | |
3007 | * Currently handles both VAE3 and VALE3, since we don't support | |
3008 | * flush-last-level-only. | |
3009 | */ | |
3010 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3011 | CPUState *cs = CPU(cpu); | |
3012 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
3013 | ||
3014 | tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S1E3, -1); | |
3015 | } | |
3016 | ||
fd3ed969 PM |
3017 | static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri, |
3018 | uint64_t value) | |
3019 | { | |
3020 | bool sec = arm_is_secure_below_el3(env); | |
fa439fc5 PM |
3021 | CPUState *other_cs; |
3022 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
3023 | ||
3024 | CPU_FOREACH(other_cs) { | |
fd3ed969 PM |
3025 | if (sec) { |
3026 | tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1SE1, | |
3027 | ARMMMUIdx_S1SE0, -1); | |
3028 | } else { | |
3029 | tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S12NSE1, | |
3030 | ARMMMUIdx_S12NSE0, -1); | |
3031 | } | |
fa439fc5 PM |
3032 | } |
3033 | } | |
3034 | ||
fd3ed969 PM |
3035 | static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri, |
3036 | uint64_t value) | |
fa439fc5 PM |
3037 | { |
3038 | CPUState *other_cs; | |
fd3ed969 | 3039 | uint64_t pageaddr = sextract64(value << 12, 0, 56); |
fa439fc5 PM |
3040 | |
3041 | CPU_FOREACH(other_cs) { | |
fd3ed969 | 3042 | tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E2, -1); |
fa439fc5 PM |
3043 | } |
3044 | } | |
3045 | ||
43efaa33 PM |
3046 | static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri, |
3047 | uint64_t value) | |
3048 | { | |
3049 | CPUState *other_cs; | |
3050 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
3051 | ||
3052 | CPU_FOREACH(other_cs) { | |
3053 | tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S1E3, -1); | |
3054 | } | |
3055 | } | |
3056 | ||
cea66e91 PM |
3057 | static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri, |
3058 | uint64_t value) | |
3059 | { | |
3060 | /* Invalidate by IPA. This has to invalidate any structures that | |
3061 | * contain only stage 2 translation information, but does not need | |
3062 | * to apply to structures that contain combined stage 1 and stage 2 | |
3063 | * translation information. | |
3064 | * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. | |
3065 | */ | |
3066 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3067 | CPUState *cs = CPU(cpu); | |
3068 | uint64_t pageaddr; | |
3069 | ||
3070 | if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { | |
3071 | return; | |
3072 | } | |
3073 | ||
3074 | pageaddr = sextract64(value << 12, 0, 48); | |
3075 | ||
3076 | tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdx_S2NS, -1); | |
3077 | } | |
3078 | ||
3079 | static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
3080 | uint64_t value) | |
3081 | { | |
3082 | CPUState *other_cs; | |
3083 | uint64_t pageaddr; | |
3084 | ||
3085 | if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { | |
3086 | return; | |
3087 | } | |
3088 | ||
3089 | pageaddr = sextract64(value << 12, 0, 48); | |
3090 | ||
3091 | CPU_FOREACH(other_cs) { | |
3092 | tlb_flush_page_by_mmuidx(other_cs, pageaddr, ARMMMUIdx_S2NS, -1); | |
3093 | } | |
3094 | } | |
3095 | ||
3f208fd7 PM |
3096 | static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri, |
3097 | bool isread) | |
aca3f40b PM |
3098 | { |
3099 | /* We don't implement EL2, so the only control on DC ZVA is the | |
3100 | * bit in the SCTLR which can prohibit access for EL0. | |
3101 | */ | |
137feaa9 | 3102 | if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) { |
aca3f40b PM |
3103 | return CP_ACCESS_TRAP; |
3104 | } | |
3105 | return CP_ACCESS_OK; | |
3106 | } | |
3107 | ||
3108 | static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
3109 | { | |
3110 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3111 | int dzp_bit = 1 << 4; | |
3112 | ||
3113 | /* DZP indicates whether DC ZVA access is allowed */ | |
3f208fd7 | 3114 | if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) { |
aca3f40b PM |
3115 | dzp_bit = 0; |
3116 | } | |
3117 | return cpu->dcz_blocksize | dzp_bit; | |
3118 | } | |
3119 | ||
3f208fd7 PM |
3120 | static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, |
3121 | bool isread) | |
f502cfc2 | 3122 | { |
cdcf1405 | 3123 | if (!(env->pstate & PSTATE_SP)) { |
f502cfc2 PM |
3124 | /* Access to SP_EL0 is undefined if it's being used as |
3125 | * the stack pointer. | |
3126 | */ | |
3127 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
3128 | } | |
3129 | return CP_ACCESS_OK; | |
3130 | } | |
3131 | ||
3132 | static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
3133 | { | |
3134 | return env->pstate & PSTATE_SP; | |
3135 | } | |
3136 | ||
3137 | static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) | |
3138 | { | |
3139 | update_spsel(env, val); | |
3140 | } | |
3141 | ||
137feaa9 FA |
3142 | static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
3143 | uint64_t value) | |
3144 | { | |
3145 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3146 | ||
3147 | if (raw_read(env, ri) == value) { | |
3148 | /* Skip the TLB flush if nothing actually changed; Linux likes | |
3149 | * to do a lot of pointless SCTLR writes. | |
3150 | */ | |
3151 | return; | |
3152 | } | |
3153 | ||
3154 | raw_write(env, ri, value); | |
3155 | /* ??? Lots of these bits are not implemented. */ | |
3156 | /* This may enable/disable the MMU, so do a TLB flush. */ | |
3157 | tlb_flush(CPU(cpu), 1); | |
3158 | } | |
3159 | ||
3f208fd7 PM |
3160 | static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri, |
3161 | bool isread) | |
03fbf20f PM |
3162 | { |
3163 | if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) { | |
f2cae609 | 3164 | return CP_ACCESS_TRAP_FP_EL2; |
03fbf20f PM |
3165 | } |
3166 | if (env->cp15.cptr_el[3] & CPTR_TFP) { | |
f2cae609 | 3167 | return CP_ACCESS_TRAP_FP_EL3; |
03fbf20f PM |
3168 | } |
3169 | return CP_ACCESS_OK; | |
3170 | } | |
3171 | ||
a8d64e73 PM |
3172 | static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
3173 | uint64_t value) | |
3174 | { | |
3175 | env->cp15.mdcr_el3 = value & SDCR_VALID_MASK; | |
3176 | } | |
3177 | ||
b0d2b7d0 PM |
3178 | static const ARMCPRegInfo v8_cp_reginfo[] = { |
3179 | /* Minimal set of EL0-visible registers. This will need to be expanded | |
3180 | * significantly for system emulation of AArch64 CPUs. | |
3181 | */ | |
3182 | { .name = "NZCV", .state = ARM_CP_STATE_AA64, | |
3183 | .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, | |
3184 | .access = PL0_RW, .type = ARM_CP_NZCV }, | |
c2b820fe PM |
3185 | { .name = "DAIF", .state = ARM_CP_STATE_AA64, |
3186 | .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, | |
7a0e58fa | 3187 | .type = ARM_CP_NO_RAW, |
c2b820fe PM |
3188 | .access = PL0_RW, .accessfn = aa64_daif_access, |
3189 | .fieldoffset = offsetof(CPUARMState, daif), | |
3190 | .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, | |
b0d2b7d0 PM |
3191 | { .name = "FPCR", .state = ARM_CP_STATE_AA64, |
3192 | .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, | |
3193 | .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, | |
3194 | { .name = "FPSR", .state = ARM_CP_STATE_AA64, | |
3195 | .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, | |
3196 | .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, | |
b0d2b7d0 PM |
3197 | { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, |
3198 | .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, | |
7a0e58fa | 3199 | .access = PL0_R, .type = ARM_CP_NO_RAW, |
aca3f40b PM |
3200 | .readfn = aa64_dczid_read }, |
3201 | { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, | |
3202 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, | |
3203 | .access = PL0_W, .type = ARM_CP_DC_ZVA, | |
3204 | #ifndef CONFIG_USER_ONLY | |
3205 | /* Avoid overhead of an access check that always passes in user-mode */ | |
3206 | .accessfn = aa64_zva_access, | |
3207 | #endif | |
3208 | }, | |
0eef9d98 PM |
3209 | { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, |
3210 | .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, | |
3211 | .access = PL1_R, .type = ARM_CP_CURRENTEL }, | |
8af35c37 PM |
3212 | /* Cache ops: all NOPs since we don't emulate caches */ |
3213 | { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, | |
3214 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, | |
3215 | .access = PL1_W, .type = ARM_CP_NOP }, | |
3216 | { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, | |
3217 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, | |
3218 | .access = PL1_W, .type = ARM_CP_NOP }, | |
3219 | { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, | |
3220 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, | |
3221 | .access = PL0_W, .type = ARM_CP_NOP, | |
3222 | .accessfn = aa64_cacheop_access }, | |
3223 | { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, | |
3224 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, | |
3225 | .access = PL1_W, .type = ARM_CP_NOP }, | |
3226 | { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, | |
3227 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, | |
3228 | .access = PL1_W, .type = ARM_CP_NOP }, | |
3229 | { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, | |
3230 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, | |
3231 | .access = PL0_W, .type = ARM_CP_NOP, | |
3232 | .accessfn = aa64_cacheop_access }, | |
3233 | { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, | |
3234 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, | |
3235 | .access = PL1_W, .type = ARM_CP_NOP }, | |
3236 | { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, | |
3237 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, | |
3238 | .access = PL0_W, .type = ARM_CP_NOP, | |
3239 | .accessfn = aa64_cacheop_access }, | |
3240 | { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, | |
3241 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, | |
3242 | .access = PL0_W, .type = ARM_CP_NOP, | |
3243 | .accessfn = aa64_cacheop_access }, | |
3244 | { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, | |
3245 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, | |
3246 | .access = PL1_W, .type = ARM_CP_NOP }, | |
168aa23b PM |
3247 | /* TLBI operations */ |
3248 | { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64, | |
6ab9f499 | 3249 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, |
7a0e58fa | 3250 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3251 | .writefn = tlbi_aa64_vmalle1is_write }, |
168aa23b | 3252 | { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3253 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, |
7a0e58fa | 3254 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3255 | .writefn = tlbi_aa64_vae1is_write }, |
168aa23b | 3256 | { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3257 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, |
7a0e58fa | 3258 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3259 | .writefn = tlbi_aa64_vmalle1is_write }, |
168aa23b | 3260 | { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3261 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, |
7a0e58fa | 3262 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3263 | .writefn = tlbi_aa64_vae1is_write }, |
168aa23b | 3264 | { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3265 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, |
7a0e58fa | 3266 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3267 | .writefn = tlbi_aa64_vae1is_write }, |
168aa23b | 3268 | { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3269 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, |
7a0e58fa | 3270 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3271 | .writefn = tlbi_aa64_vae1is_write }, |
168aa23b | 3272 | { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3273 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, |
7a0e58fa | 3274 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3275 | .writefn = tlbi_aa64_vmalle1_write }, |
168aa23b | 3276 | { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3277 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, |
7a0e58fa | 3278 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3279 | .writefn = tlbi_aa64_vae1_write }, |
168aa23b | 3280 | { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3281 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, |
7a0e58fa | 3282 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3283 | .writefn = tlbi_aa64_vmalle1_write }, |
168aa23b | 3284 | { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3285 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, |
7a0e58fa | 3286 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3287 | .writefn = tlbi_aa64_vae1_write }, |
168aa23b | 3288 | { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3289 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, |
7a0e58fa | 3290 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3291 | .writefn = tlbi_aa64_vae1_write }, |
168aa23b | 3292 | { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64, |
6ab9f499 | 3293 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, |
7a0e58fa | 3294 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
fd3ed969 | 3295 | .writefn = tlbi_aa64_vae1_write }, |
cea66e91 PM |
3296 | { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64, |
3297 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, | |
3298 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3299 | .writefn = tlbi_aa64_ipas2e1is_write }, | |
3300 | { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64, | |
3301 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, | |
3302 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3303 | .writefn = tlbi_aa64_ipas2e1is_write }, | |
83ddf975 PM |
3304 | { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64, |
3305 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, | |
3306 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
fd3ed969 | 3307 | .writefn = tlbi_aa64_alle1is_write }, |
43efaa33 PM |
3308 | { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64, |
3309 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6, | |
3310 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3311 | .writefn = tlbi_aa64_alle1is_write }, | |
cea66e91 PM |
3312 | { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64, |
3313 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, | |
3314 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3315 | .writefn = tlbi_aa64_ipas2e1_write }, | |
3316 | { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64, | |
3317 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, | |
3318 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3319 | .writefn = tlbi_aa64_ipas2e1_write }, | |
83ddf975 PM |
3320 | { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64, |
3321 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, | |
3322 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
fd3ed969 | 3323 | .writefn = tlbi_aa64_alle1_write }, |
43efaa33 PM |
3324 | { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64, |
3325 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6, | |
3326 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3327 | .writefn = tlbi_aa64_alle1is_write }, | |
19525524 PM |
3328 | #ifndef CONFIG_USER_ONLY |
3329 | /* 64 bit address translation operations */ | |
3330 | { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, | |
3331 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, | |
060e8a48 | 3332 | .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, |
19525524 PM |
3333 | { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, |
3334 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, | |
060e8a48 | 3335 | .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, |
19525524 PM |
3336 | { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, |
3337 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, | |
060e8a48 | 3338 | .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, |
19525524 PM |
3339 | { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, |
3340 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, | |
060e8a48 | 3341 | .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, |
2a47df95 | 3342 | { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64, |
7a379c7e | 3343 | .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4, |
2a47df95 PM |
3344 | .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, |
3345 | { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64, | |
7a379c7e | 3346 | .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5, |
2a47df95 PM |
3347 | .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, |
3348 | { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64, | |
7a379c7e | 3349 | .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6, |
2a47df95 PM |
3350 | .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, |
3351 | { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64, | |
7a379c7e | 3352 | .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7, |
2a47df95 PM |
3353 | .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, |
3354 | /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */ | |
3355 | { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64, | |
3356 | .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0, | |
3357 | .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, | |
3358 | { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64, | |
3359 | .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1, | |
3360 | .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, | |
c96fc9b5 EI |
3361 | { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64, |
3362 | .type = ARM_CP_ALIAS, | |
3363 | .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0, | |
3364 | .access = PL1_RW, .resetvalue = 0, | |
3365 | .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]), | |
3366 | .writefn = par_write }, | |
19525524 | 3367 | #endif |
995939a6 | 3368 | /* TLB invalidate last level of translation table walk */ |
9449fdf6 | 3369 | { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, |
7a0e58fa | 3370 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, |
9449fdf6 | 3371 | { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, |
7a0e58fa | 3372 | .type = ARM_CP_NO_RAW, .access = PL1_W, |
fa439fc5 | 3373 | .writefn = tlbimvaa_is_write }, |
9449fdf6 | 3374 | { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, |
7a0e58fa | 3375 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, |
9449fdf6 | 3376 | { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, |
7a0e58fa | 3377 | .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, |
541ef8c2 SS |
3378 | { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, |
3379 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3380 | .writefn = tlbimva_hyp_write }, | |
3381 | { .name = "TLBIMVALHIS", | |
3382 | .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, | |
3383 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3384 | .writefn = tlbimva_hyp_is_write }, | |
3385 | { .name = "TLBIIPAS2", | |
3386 | .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, | |
3387 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3388 | .writefn = tlbiipas2_write }, | |
3389 | { .name = "TLBIIPAS2IS", | |
3390 | .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, | |
3391 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3392 | .writefn = tlbiipas2_is_write }, | |
3393 | { .name = "TLBIIPAS2L", | |
3394 | .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, | |
3395 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3396 | .writefn = tlbiipas2_write }, | |
3397 | { .name = "TLBIIPAS2LIS", | |
3398 | .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, | |
3399 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3400 | .writefn = tlbiipas2_is_write }, | |
9449fdf6 PM |
3401 | /* 32 bit cache operations */ |
3402 | { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, | |
3403 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3404 | { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, | |
3405 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3406 | { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, | |
3407 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3408 | { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, | |
3409 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3410 | { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, | |
3411 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3412 | { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, | |
3413 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3414 | { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, | |
3415 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3416 | { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, | |
3417 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3418 | { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, | |
3419 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3420 | { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, | |
3421 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3422 | { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, | |
3423 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3424 | { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, | |
3425 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3426 | { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, | |
3427 | .type = ARM_CP_NOP, .access = PL1_W }, | |
3428 | /* MMU Domain access control / MPU write buffer control */ | |
0c17d68c FA |
3429 | { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, |
3430 | .access = PL1_RW, .resetvalue = 0, | |
3431 | .writefn = dacr_write, .raw_writefn = raw_write, | |
3432 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), | |
3433 | offsetoflow32(CPUARMState, cp15.dacr_ns) } }, | |
a0618a19 | 3434 | { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, |
7a0e58fa | 3435 | .type = ARM_CP_ALIAS, |
a0618a19 | 3436 | .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, |
6947f059 EI |
3437 | .access = PL1_RW, |
3438 | .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, | |
a65f1de9 | 3439 | { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, |
7a0e58fa | 3440 | .type = ARM_CP_ALIAS, |
a65f1de9 | 3441 | .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, |
99a99c1f SB |
3442 | .access = PL1_RW, |
3443 | .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) }, | |
f502cfc2 PM |
3444 | /* We rely on the access checks not allowing the guest to write to the |
3445 | * state field when SPSel indicates that it's being used as the stack | |
3446 | * pointer. | |
3447 | */ | |
3448 | { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, | |
3449 | .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, | |
3450 | .access = PL1_RW, .accessfn = sp_el0_access, | |
7a0e58fa | 3451 | .type = ARM_CP_ALIAS, |
f502cfc2 | 3452 | .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, |
884b4dee GB |
3453 | { .name = "SP_EL1", .state = ARM_CP_STATE_AA64, |
3454 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0, | |
7a0e58fa | 3455 | .access = PL2_RW, .type = ARM_CP_ALIAS, |
884b4dee | 3456 | .fieldoffset = offsetof(CPUARMState, sp_el[1]) }, |
f502cfc2 PM |
3457 | { .name = "SPSel", .state = ARM_CP_STATE_AA64, |
3458 | .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, | |
7a0e58fa | 3459 | .type = ARM_CP_NO_RAW, |
f502cfc2 | 3460 | .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, |
03fbf20f PM |
3461 | { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64, |
3462 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0, | |
3463 | .type = ARM_CP_ALIAS, | |
3464 | .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]), | |
3465 | .access = PL2_RW, .accessfn = fpexc32_access }, | |
6a43e0b6 PM |
3466 | { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, |
3467 | .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, | |
3468 | .access = PL2_RW, .resetvalue = 0, | |
3469 | .writefn = dacr_write, .raw_writefn = raw_write, | |
3470 | .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, | |
3471 | { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, | |
3472 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, | |
3473 | .access = PL2_RW, .resetvalue = 0, | |
3474 | .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, | |
3475 | { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64, | |
3476 | .type = ARM_CP_ALIAS, | |
3477 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0, | |
3478 | .access = PL2_RW, | |
3479 | .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) }, | |
3480 | { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64, | |
3481 | .type = ARM_CP_ALIAS, | |
3482 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1, | |
3483 | .access = PL2_RW, | |
3484 | .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) }, | |
3485 | { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64, | |
3486 | .type = ARM_CP_ALIAS, | |
3487 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2, | |
3488 | .access = PL2_RW, | |
3489 | .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) }, | |
3490 | { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64, | |
3491 | .type = ARM_CP_ALIAS, | |
3492 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3, | |
3493 | .access = PL2_RW, | |
3494 | .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) }, | |
a8d64e73 PM |
3495 | { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64, |
3496 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1, | |
3497 | .resetvalue = 0, | |
3498 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) }, | |
3499 | { .name = "SDCR", .type = ARM_CP_ALIAS, | |
3500 | .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1, | |
3501 | .access = PL1_RW, .accessfn = access_trap_aa32s_el1, | |
3502 | .writefn = sdcr_write, | |
3503 | .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) }, | |
b0d2b7d0 PM |
3504 | REGINFO_SENTINEL |
3505 | }; | |
3506 | ||
d42e3c26 | 3507 | /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */ |
4771cd01 | 3508 | static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = { |
d42e3c26 EI |
3509 | { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64, |
3510 | .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, | |
3511 | .access = PL2_RW, | |
3512 | .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, | |
f149e3e8 | 3513 | { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, |
7a0e58fa | 3514 | .type = ARM_CP_NO_RAW, |
f149e3e8 EI |
3515 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, |
3516 | .access = PL2_RW, | |
3517 | .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, | |
c6f19164 GB |
3518 | { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, |
3519 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, | |
3520 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
95f949ac EI |
3521 | { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, |
3522 | .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, | |
3523 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3524 | .resetvalue = 0 }, | |
3525 | { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, | |
3526 | .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, | |
3527 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
2179ef95 PM |
3528 | { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, |
3529 | .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, | |
3530 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3531 | .resetvalue = 0 }, | |
3532 | { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, | |
3533 | .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, | |
3534 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3535 | .resetvalue = 0 }, | |
37cd6c24 PM |
3536 | { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, |
3537 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, | |
3538 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3539 | .resetvalue = 0 }, | |
3540 | { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, | |
3541 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, | |
3542 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3543 | .resetvalue = 0 }, | |
06ec4c8c EI |
3544 | { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, |
3545 | .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, | |
3546 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
68e9c2fe EI |
3547 | { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH, |
3548 | .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, | |
3549 | .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, | |
3550 | .type = ARM_CP_CONST, .resetvalue = 0 }, | |
b698e9cf EI |
3551 | { .name = "VTTBR", .state = ARM_CP_STATE_AA32, |
3552 | .cp = 15, .opc1 = 6, .crm = 2, | |
3553 | .access = PL2_RW, .accessfn = access_el3_aa32ns, | |
3554 | .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, | |
3555 | { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, | |
3556 | .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, | |
3557 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
b9cb5323 EI |
3558 | { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, |
3559 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, | |
3560 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
ff05f37b EI |
3561 | { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, |
3562 | .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, | |
3563 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
a57633c0 EI |
3564 | { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, |
3565 | .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, | |
3566 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3567 | { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, | |
3568 | .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, | |
3569 | .resetvalue = 0 }, | |
0b6440af EI |
3570 | { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, |
3571 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, | |
3572 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
edac4d8a EI |
3573 | { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, |
3574 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, | |
3575 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3576 | { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, | |
3577 | .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, | |
3578 | .resetvalue = 0 }, | |
b0e66d95 EI |
3579 | { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, |
3580 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, | |
3581 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3582 | { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, | |
3583 | .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, | |
3584 | .resetvalue = 0 }, | |
3585 | { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, | |
3586 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, | |
3587 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3588 | { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, | |
3589 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, | |
3590 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
14cc7b54 SF |
3591 | { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, |
3592 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, | |
d6c8cf81 PM |
3593 | .access = PL2_RW, .accessfn = access_tda, |
3594 | .type = ARM_CP_CONST, .resetvalue = 0 }, | |
59e05530 EI |
3595 | { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH, |
3596 | .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, | |
3597 | .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, | |
3598 | .type = ARM_CP_CONST, .resetvalue = 0 }, | |
2a5a9abd AF |
3599 | { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, |
3600 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, | |
3601 | .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
d42e3c26 EI |
3602 | REGINFO_SENTINEL |
3603 | }; | |
3604 | ||
f149e3e8 EI |
3605 | static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
3606 | { | |
3607 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3608 | uint64_t valid_mask = HCR_MASK; | |
3609 | ||
3610 | if (arm_feature(env, ARM_FEATURE_EL3)) { | |
3611 | valid_mask &= ~HCR_HCD; | |
3612 | } else { | |
3613 | valid_mask &= ~HCR_TSC; | |
3614 | } | |
3615 | ||
3616 | /* Clear RES0 bits. */ | |
3617 | value &= valid_mask; | |
3618 | ||
3619 | /* These bits change the MMU setup: | |
3620 | * HCR_VM enables stage 2 translation | |
3621 | * HCR_PTW forbids certain page-table setups | |
3622 | * HCR_DC Disables stage1 and enables stage2 translation | |
3623 | */ | |
3624 | if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) { | |
3625 | tlb_flush(CPU(cpu), 1); | |
3626 | } | |
3627 | raw_write(env, ri, value); | |
3628 | } | |
3629 | ||
4771cd01 | 3630 | static const ARMCPRegInfo el2_cp_reginfo[] = { |
f149e3e8 EI |
3631 | { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, |
3632 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, | |
3633 | .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), | |
3634 | .writefn = hcr_write }, | |
3b685ba7 | 3635 | { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, |
7a0e58fa | 3636 | .type = ARM_CP_ALIAS, |
3b685ba7 EI |
3637 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, |
3638 | .access = PL2_RW, | |
3639 | .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, | |
f2c30f42 | 3640 | { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64, |
f2c30f42 EI |
3641 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, |
3642 | .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, | |
63b60551 EI |
3643 | { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64, |
3644 | .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, | |
3645 | .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, | |
3b685ba7 | 3646 | { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, |
7a0e58fa | 3647 | .type = ARM_CP_ALIAS, |
3b685ba7 | 3648 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, |
99a99c1f SB |
3649 | .access = PL2_RW, |
3650 | .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) }, | |
d42e3c26 EI |
3651 | { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64, |
3652 | .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, | |
3653 | .access = PL2_RW, .writefn = vbar_write, | |
3654 | .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), | |
3655 | .resetvalue = 0 }, | |
884b4dee GB |
3656 | { .name = "SP_EL2", .state = ARM_CP_STATE_AA64, |
3657 | .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0, | |
7a0e58fa | 3658 | .access = PL3_RW, .type = ARM_CP_ALIAS, |
884b4dee | 3659 | .fieldoffset = offsetof(CPUARMState, sp_el[2]) }, |
c6f19164 GB |
3660 | { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, |
3661 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, | |
3662 | .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0, | |
3663 | .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) }, | |
95f949ac EI |
3664 | { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, |
3665 | .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, | |
3666 | .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]), | |
3667 | .resetvalue = 0 }, | |
3668 | { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, | |
3669 | .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, | |
3670 | .access = PL2_RW, .type = ARM_CP_ALIAS, | |
3671 | .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) }, | |
2179ef95 PM |
3672 | { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, |
3673 | .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, | |
3674 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3675 | .resetvalue = 0 }, | |
3676 | /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */ | |
3677 | { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, | |
3678 | .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, | |
3679 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3680 | .resetvalue = 0 }, | |
37cd6c24 PM |
3681 | { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, |
3682 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, | |
3683 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3684 | .resetvalue = 0 }, | |
3685 | { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, | |
3686 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, | |
3687 | .access = PL2_RW, .type = ARM_CP_CONST, | |
3688 | .resetvalue = 0 }, | |
06ec4c8c EI |
3689 | { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, |
3690 | .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, | |
6459b94c PM |
3691 | .access = PL2_RW, |
3692 | /* no .writefn needed as this can't cause an ASID change; | |
3693 | * no .raw_writefn or .resetfn needed as we never use mask/base_mask | |
3694 | */ | |
06ec4c8c | 3695 | .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) }, |
68e9c2fe EI |
3696 | { .name = "VTCR", .state = ARM_CP_STATE_AA32, |
3697 | .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, | |
bf06c112 | 3698 | .type = ARM_CP_ALIAS, |
68e9c2fe EI |
3699 | .access = PL2_RW, .accessfn = access_el3_aa32ns, |
3700 | .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, | |
3701 | { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64, | |
3702 | .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, | |
bf06c112 PM |
3703 | .access = PL2_RW, |
3704 | /* no .writefn needed as this can't cause an ASID change; | |
3705 | * no .raw_writefn or .resetfn needed as we never use mask/base_mask | |
3706 | */ | |
68e9c2fe | 3707 | .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, |
b698e9cf EI |
3708 | { .name = "VTTBR", .state = ARM_CP_STATE_AA32, |
3709 | .cp = 15, .opc1 = 6, .crm = 2, | |
3710 | .type = ARM_CP_64BIT | ARM_CP_ALIAS, | |
3711 | .access = PL2_RW, .accessfn = access_el3_aa32ns, | |
3712 | .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2), | |
3713 | .writefn = vttbr_write }, | |
3714 | { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, | |
3715 | .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, | |
3716 | .access = PL2_RW, .writefn = vttbr_write, | |
3717 | .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) }, | |
b9cb5323 EI |
3718 | { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, |
3719 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, | |
3720 | .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write, | |
3721 | .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) }, | |
ff05f37b EI |
3722 | { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, |
3723 | .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, | |
3724 | .access = PL2_RW, .resetvalue = 0, | |
3725 | .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) }, | |
a57633c0 EI |
3726 | { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, |
3727 | .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, | |
3728 | .access = PL2_RW, .resetvalue = 0, | |
3729 | .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, | |
3730 | { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, | |
3731 | .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, | |
a57633c0 | 3732 | .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, |
541ef8c2 SS |
3733 | { .name = "TLBIALLNSNH", |
3734 | .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, | |
3735 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3736 | .writefn = tlbiall_nsnh_write }, | |
3737 | { .name = "TLBIALLNSNHIS", | |
3738 | .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, | |
3739 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3740 | .writefn = tlbiall_nsnh_is_write }, | |
3741 | { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, | |
3742 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3743 | .writefn = tlbiall_hyp_write }, | |
3744 | { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, | |
3745 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3746 | .writefn = tlbiall_hyp_is_write }, | |
3747 | { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, | |
3748 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3749 | .writefn = tlbimva_hyp_write }, | |
3750 | { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, | |
3751 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
3752 | .writefn = tlbimva_hyp_is_write }, | |
51da9014 EI |
3753 | { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64, |
3754 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, | |
3755 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
fd3ed969 | 3756 | .writefn = tlbi_aa64_alle2_write }, |
8742d49d EI |
3757 | { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64, |
3758 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, | |
3759 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
fd3ed969 | 3760 | .writefn = tlbi_aa64_vae2_write }, |
2bfb9d75 PM |
3761 | { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64, |
3762 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, | |
3763 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3764 | .writefn = tlbi_aa64_vae2_write }, | |
3765 | { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64, | |
3766 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, | |
3767 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3768 | .writefn = tlbi_aa64_alle2is_write }, | |
8742d49d EI |
3769 | { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64, |
3770 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, | |
3771 | .type = ARM_CP_NO_RAW, .access = PL2_W, | |
fd3ed969 | 3772 | .writefn = tlbi_aa64_vae2is_write }, |
2bfb9d75 PM |
3773 | { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64, |
3774 | .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, | |
3775 | .access = PL2_W, .type = ARM_CP_NO_RAW, | |
3776 | .writefn = tlbi_aa64_vae2is_write }, | |
edac4d8a | 3777 | #ifndef CONFIG_USER_ONLY |
2a47df95 PM |
3778 | /* Unlike the other EL2-related AT operations, these must |
3779 | * UNDEF from EL3 if EL2 is not implemented, which is why we | |
3780 | * define them here rather than with the rest of the AT ops. | |
3781 | */ | |
3782 | { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64, | |
3783 | .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, | |
3784 | .access = PL2_W, .accessfn = at_s1e2_access, | |
3785 | .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, | |
3786 | { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64, | |
3787 | .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, | |
3788 | .access = PL2_W, .accessfn = at_s1e2_access, | |
3789 | .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, | |
14db7fe0 PM |
3790 | /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE |
3791 | * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3 | |
3792 | * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose | |
3793 | * to behave as if SCR.NS was 1. | |
3794 | */ | |
3795 | { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, | |
3796 | .access = PL2_W, | |
3797 | .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, | |
3798 | { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, | |
3799 | .access = PL2_W, | |
3800 | .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, | |
0b6440af EI |
3801 | { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, |
3802 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, | |
3803 | /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the | |
3804 | * reset values as IMPDEF. We choose to reset to 3 to comply with | |
3805 | * both ARMv7 and ARMv8. | |
3806 | */ | |
3807 | .access = PL2_RW, .resetvalue = 3, | |
3808 | .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) }, | |
edac4d8a EI |
3809 | { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, |
3810 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, | |
3811 | .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, | |
3812 | .writefn = gt_cntvoff_write, | |
3813 | .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, | |
3814 | { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, | |
3815 | .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO, | |
3816 | .writefn = gt_cntvoff_write, | |
3817 | .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, | |
b0e66d95 EI |
3818 | { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, |
3819 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, | |
3820 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), | |
3821 | .type = ARM_CP_IO, .access = PL2_RW, | |
3822 | .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, | |
3823 | { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, | |
3824 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), | |
3825 | .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO, | |
3826 | .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, | |
3827 | { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, | |
3828 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, | |
d44ec156 | 3829 | .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, |
b0e66d95 EI |
3830 | .resetfn = gt_hyp_timer_reset, |
3831 | .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write }, | |
3832 | { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, | |
3833 | .type = ARM_CP_IO, | |
3834 | .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, | |
3835 | .access = PL2_RW, | |
3836 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl), | |
3837 | .resetvalue = 0, | |
3838 | .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write }, | |
edac4d8a | 3839 | #endif |
14cc7b54 SF |
3840 | /* The only field of MDCR_EL2 that has a defined architectural reset value |
3841 | * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we | |
3842 | * don't impelment any PMU event counters, so using zero as a reset | |
3843 | * value for MDCR_EL2 is okay | |
3844 | */ | |
3845 | { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, | |
3846 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, | |
3847 | .access = PL2_RW, .resetvalue = 0, | |
3848 | .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), }, | |
59e05530 EI |
3849 | { .name = "HPFAR", .state = ARM_CP_STATE_AA32, |
3850 | .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, | |
3851 | .access = PL2_RW, .accessfn = access_el3_aa32ns, | |
3852 | .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, | |
3853 | { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64, | |
3854 | .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, | |
3855 | .access = PL2_RW, | |
3856 | .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, | |
2a5a9abd AF |
3857 | { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, |
3858 | .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, | |
3859 | .access = PL2_RW, | |
3860 | .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) }, | |
3b685ba7 EI |
3861 | REGINFO_SENTINEL |
3862 | }; | |
3863 | ||
2f027fc5 PM |
3864 | static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri, |
3865 | bool isread) | |
3866 | { | |
3867 | /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2. | |
3868 | * At Secure EL1 it traps to EL3. | |
3869 | */ | |
3870 | if (arm_current_el(env) == 3) { | |
3871 | return CP_ACCESS_OK; | |
3872 | } | |
3873 | if (arm_is_secure_below_el3(env)) { | |
3874 | return CP_ACCESS_TRAP_EL3; | |
3875 | } | |
3876 | /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */ | |
3877 | if (isread) { | |
3878 | return CP_ACCESS_OK; | |
3879 | } | |
3880 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
3881 | } | |
3882 | ||
60fb1a87 GB |
3883 | static const ARMCPRegInfo el3_cp_reginfo[] = { |
3884 | { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, | |
3885 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, | |
3886 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), | |
3887 | .resetvalue = 0, .writefn = scr_write }, | |
7a0e58fa | 3888 | { .name = "SCR", .type = ARM_CP_ALIAS, |
60fb1a87 | 3889 | .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, |
efe4a274 PM |
3890 | .access = PL1_RW, .accessfn = access_trap_aa32s_el1, |
3891 | .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), | |
b061a82b | 3892 | .writefn = scr_write }, |
60fb1a87 GB |
3893 | { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, |
3894 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, | |
3895 | .access = PL3_RW, .resetvalue = 0, | |
3896 | .fieldoffset = offsetof(CPUARMState, cp15.sder) }, | |
3897 | { .name = "SDER", | |
3898 | .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, | |
3899 | .access = PL3_RW, .resetvalue = 0, | |
3900 | .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, | |
60fb1a87 | 3901 | { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, |
efe4a274 PM |
3902 | .access = PL1_RW, .accessfn = access_trap_aa32s_el1, |
3903 | .writefn = vbar_write, .resetvalue = 0, | |
60fb1a87 | 3904 | .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, |
7dd8c9af FA |
3905 | { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, |
3906 | .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, | |
3907 | .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, | |
3908 | .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, | |
11f136ee FA |
3909 | { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, |
3910 | .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, | |
6459b94c PM |
3911 | .access = PL3_RW, |
3912 | /* no .writefn needed as this can't cause an ASID change; | |
811595a2 PM |
3913 | * we must provide a .raw_writefn and .resetfn because we handle |
3914 | * reset and migration for the AArch32 TTBCR(S), which might be | |
3915 | * using mask and base_mask. | |
6459b94c | 3916 | */ |
811595a2 | 3917 | .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write, |
11f136ee | 3918 | .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, |
81547d66 | 3919 | { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, |
7a0e58fa | 3920 | .type = ARM_CP_ALIAS, |
81547d66 EI |
3921 | .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, |
3922 | .access = PL3_RW, | |
3923 | .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, | |
f2c30f42 | 3924 | { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, |
f2c30f42 EI |
3925 | .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, |
3926 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, | |
63b60551 EI |
3927 | { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, |
3928 | .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, | |
3929 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, | |
81547d66 | 3930 | { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, |
7a0e58fa | 3931 | .type = ARM_CP_ALIAS, |
81547d66 | 3932 | .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, |
99a99c1f SB |
3933 | .access = PL3_RW, |
3934 | .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) }, | |
a1ba125c EI |
3935 | { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, |
3936 | .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, | |
3937 | .access = PL3_RW, .writefn = vbar_write, | |
3938 | .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), | |
3939 | .resetvalue = 0 }, | |
c6f19164 GB |
3940 | { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64, |
3941 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2, | |
3942 | .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0, | |
3943 | .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) }, | |
4cfb8ad8 PM |
3944 | { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64, |
3945 | .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2, | |
3946 | .access = PL3_RW, .resetvalue = 0, | |
3947 | .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) }, | |
2179ef95 PM |
3948 | { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64, |
3949 | .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0, | |
3950 | .access = PL3_RW, .type = ARM_CP_CONST, | |
3951 | .resetvalue = 0 }, | |
37cd6c24 PM |
3952 | { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH, |
3953 | .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0, | |
3954 | .access = PL3_RW, .type = ARM_CP_CONST, | |
3955 | .resetvalue = 0 }, | |
3956 | { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH, | |
3957 | .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1, | |
3958 | .access = PL3_RW, .type = ARM_CP_CONST, | |
3959 | .resetvalue = 0 }, | |
43efaa33 PM |
3960 | { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64, |
3961 | .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0, | |
3962 | .access = PL3_W, .type = ARM_CP_NO_RAW, | |
3963 | .writefn = tlbi_aa64_alle3is_write }, | |
3964 | { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64, | |
3965 | .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1, | |
3966 | .access = PL3_W, .type = ARM_CP_NO_RAW, | |
3967 | .writefn = tlbi_aa64_vae3is_write }, | |
3968 | { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64, | |
3969 | .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5, | |
3970 | .access = PL3_W, .type = ARM_CP_NO_RAW, | |
3971 | .writefn = tlbi_aa64_vae3is_write }, | |
3972 | { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64, | |
3973 | .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0, | |
3974 | .access = PL3_W, .type = ARM_CP_NO_RAW, | |
3975 | .writefn = tlbi_aa64_alle3_write }, | |
3976 | { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64, | |
3977 | .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1, | |
3978 | .access = PL3_W, .type = ARM_CP_NO_RAW, | |
3979 | .writefn = tlbi_aa64_vae3_write }, | |
3980 | { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64, | |
3981 | .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5, | |
3982 | .access = PL3_W, .type = ARM_CP_NO_RAW, | |
3983 | .writefn = tlbi_aa64_vae3_write }, | |
0f1a3b24 FA |
3984 | REGINFO_SENTINEL |
3985 | }; | |
3986 | ||
3f208fd7 PM |
3987 | static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, |
3988 | bool isread) | |
7da845b0 PM |
3989 | { |
3990 | /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64, | |
3991 | * but the AArch32 CTR has its own reginfo struct) | |
3992 | */ | |
137feaa9 | 3993 | if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) { |
7da845b0 PM |
3994 | return CP_ACCESS_TRAP; |
3995 | } | |
3996 | return CP_ACCESS_OK; | |
3997 | } | |
3998 | ||
1424ca8d DM |
3999 | static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri, |
4000 | uint64_t value) | |
4001 | { | |
4002 | /* Writes to OSLAR_EL1 may update the OS lock status, which can be | |
4003 | * read via a bit in OSLSR_EL1. | |
4004 | */ | |
4005 | int oslock; | |
4006 | ||
4007 | if (ri->state == ARM_CP_STATE_AA32) { | |
4008 | oslock = (value == 0xC5ACCE55); | |
4009 | } else { | |
4010 | oslock = value & 1; | |
4011 | } | |
4012 | ||
4013 | env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock); | |
4014 | } | |
4015 | ||
50300698 | 4016 | static const ARMCPRegInfo debug_cp_reginfo[] = { |
50300698 | 4017 | /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped |
10aae104 PM |
4018 | * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1; |
4019 | * unlike DBGDRAR it is never accessible from EL0. | |
4020 | * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64 | |
4021 | * accessor. | |
50300698 PM |
4022 | */ |
4023 | { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, | |
91b0a238 PM |
4024 | .access = PL0_R, .accessfn = access_tdra, |
4025 | .type = ARM_CP_CONST, .resetvalue = 0 }, | |
10aae104 PM |
4026 | { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64, |
4027 | .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, | |
91b0a238 PM |
4028 | .access = PL1_R, .accessfn = access_tdra, |
4029 | .type = ARM_CP_CONST, .resetvalue = 0 }, | |
50300698 | 4030 | { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, |
91b0a238 PM |
4031 | .access = PL0_R, .accessfn = access_tdra, |
4032 | .type = ARM_CP_CONST, .resetvalue = 0 }, | |
17a9eb53 | 4033 | /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */ |
10aae104 PM |
4034 | { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH, |
4035 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, | |
d6c8cf81 | 4036 | .access = PL1_RW, .accessfn = access_tda, |
0e5e8935 PM |
4037 | .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), |
4038 | .resetvalue = 0 }, | |
5e8b12ff PM |
4039 | /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1. |
4040 | * We don't implement the configurable EL0 access. | |
4041 | */ | |
4042 | { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH, | |
4043 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, | |
7a0e58fa | 4044 | .type = ARM_CP_ALIAS, |
d6c8cf81 | 4045 | .access = PL1_R, .accessfn = access_tda, |
b061a82b | 4046 | .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), }, |
10aae104 PM |
4047 | { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH, |
4048 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4, | |
1424ca8d | 4049 | .access = PL1_W, .type = ARM_CP_NO_RAW, |
187f678d | 4050 | .accessfn = access_tdosa, |
1424ca8d DM |
4051 | .writefn = oslar_write }, |
4052 | { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH, | |
4053 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4, | |
4054 | .access = PL1_R, .resetvalue = 10, | |
187f678d | 4055 | .accessfn = access_tdosa, |
1424ca8d | 4056 | .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) }, |
5e8b12ff PM |
4057 | /* Dummy OSDLR_EL1: 32-bit Linux will read this */ |
4058 | { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH, | |
4059 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4, | |
187f678d PM |
4060 | .access = PL1_RW, .accessfn = access_tdosa, |
4061 | .type = ARM_CP_NOP }, | |
5e8b12ff PM |
4062 | /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't |
4063 | * implement vector catch debug events yet. | |
4064 | */ | |
4065 | { .name = "DBGVCR", | |
4066 | .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, | |
d6c8cf81 PM |
4067 | .access = PL1_RW, .accessfn = access_tda, |
4068 | .type = ARM_CP_NOP }, | |
5dbdc434 PM |
4069 | /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications |
4070 | * Channel but Linux may try to access this register. The 32-bit | |
4071 | * alias is DBGDCCINT. | |
4072 | */ | |
4073 | { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH, | |
4074 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, | |
4075 | .access = PL1_RW, .accessfn = access_tda, | |
4076 | .type = ARM_CP_NOP }, | |
50300698 PM |
4077 | REGINFO_SENTINEL |
4078 | }; | |
4079 | ||
4080 | static const ARMCPRegInfo debug_lpae_cp_reginfo[] = { | |
4081 | /* 64 bit access versions of the (dummy) debug registers */ | |
4082 | { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0, | |
4083 | .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, | |
4084 | { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0, | |
4085 | .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, | |
4086 | REGINFO_SENTINEL | |
4087 | }; | |
4088 | ||
9ee98ce8 PM |
4089 | void hw_watchpoint_update(ARMCPU *cpu, int n) |
4090 | { | |
4091 | CPUARMState *env = &cpu->env; | |
4092 | vaddr len = 0; | |
4093 | vaddr wvr = env->cp15.dbgwvr[n]; | |
4094 | uint64_t wcr = env->cp15.dbgwcr[n]; | |
4095 | int mask; | |
4096 | int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; | |
4097 | ||
4098 | if (env->cpu_watchpoint[n]) { | |
4099 | cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]); | |
4100 | env->cpu_watchpoint[n] = NULL; | |
4101 | } | |
4102 | ||
4103 | if (!extract64(wcr, 0, 1)) { | |
4104 | /* E bit clear : watchpoint disabled */ | |
4105 | return; | |
4106 | } | |
4107 | ||
4108 | switch (extract64(wcr, 3, 2)) { | |
4109 | case 0: | |
4110 | /* LSC 00 is reserved and must behave as if the wp is disabled */ | |
4111 | return; | |
4112 | case 1: | |
4113 | flags |= BP_MEM_READ; | |
4114 | break; | |
4115 | case 2: | |
4116 | flags |= BP_MEM_WRITE; | |
4117 | break; | |
4118 | case 3: | |
4119 | flags |= BP_MEM_ACCESS; | |
4120 | break; | |
4121 | } | |
4122 | ||
4123 | /* Attempts to use both MASK and BAS fields simultaneously are | |
4124 | * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case, | |
4125 | * thus generating a watchpoint for every byte in the masked region. | |
4126 | */ | |
4127 | mask = extract64(wcr, 24, 4); | |
4128 | if (mask == 1 || mask == 2) { | |
4129 | /* Reserved values of MASK; we must act as if the mask value was | |
4130 | * some non-reserved value, or as if the watchpoint were disabled. | |
4131 | * We choose the latter. | |
4132 | */ | |
4133 | return; | |
4134 | } else if (mask) { | |
4135 | /* Watchpoint covers an aligned area up to 2GB in size */ | |
4136 | len = 1ULL << mask; | |
4137 | /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE | |
4138 | * whether the watchpoint fires when the unmasked bits match; we opt | |
4139 | * to generate the exceptions. | |
4140 | */ | |
4141 | wvr &= ~(len - 1); | |
4142 | } else { | |
4143 | /* Watchpoint covers bytes defined by the byte address select bits */ | |
4144 | int bas = extract64(wcr, 5, 8); | |
4145 | int basstart; | |
4146 | ||
4147 | if (bas == 0) { | |
4148 | /* This must act as if the watchpoint is disabled */ | |
4149 | return; | |
4150 | } | |
4151 | ||
4152 | if (extract64(wvr, 2, 1)) { | |
4153 | /* Deprecated case of an only 4-aligned address. BAS[7:4] are | |
4154 | * ignored, and BAS[3:0] define which bytes to watch. | |
4155 | */ | |
4156 | bas &= 0xf; | |
4157 | } | |
4158 | /* The BAS bits are supposed to be programmed to indicate a contiguous | |
4159 | * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether | |
4160 | * we fire for each byte in the word/doubleword addressed by the WVR. | |
4161 | * We choose to ignore any non-zero bits after the first range of 1s. | |
4162 | */ | |
4163 | basstart = ctz32(bas); | |
4164 | len = cto32(bas >> basstart); | |
4165 | wvr += basstart; | |
4166 | } | |
4167 | ||
4168 | cpu_watchpoint_insert(CPU(cpu), wvr, len, flags, | |
4169 | &env->cpu_watchpoint[n]); | |
4170 | } | |
4171 | ||
4172 | void hw_watchpoint_update_all(ARMCPU *cpu) | |
4173 | { | |
4174 | int i; | |
4175 | CPUARMState *env = &cpu->env; | |
4176 | ||
4177 | /* Completely clear out existing QEMU watchpoints and our array, to | |
4178 | * avoid possible stale entries following migration load. | |
4179 | */ | |
4180 | cpu_watchpoint_remove_all(CPU(cpu), BP_CPU); | |
4181 | memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint)); | |
4182 | ||
4183 | for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) { | |
4184 | hw_watchpoint_update(cpu, i); | |
4185 | } | |
4186 | } | |
4187 | ||
4188 | static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
4189 | uint64_t value) | |
4190 | { | |
4191 | ARMCPU *cpu = arm_env_get_cpu(env); | |
4192 | int i = ri->crm; | |
4193 | ||
4194 | /* Bits [63:49] are hardwired to the value of bit [48]; that is, the | |
4195 | * register reads and behaves as if values written are sign extended. | |
4196 | * Bits [1:0] are RES0. | |
4197 | */ | |
4198 | value = sextract64(value, 0, 49) & ~3ULL; | |
4199 | ||
4200 | raw_write(env, ri, value); | |
4201 | hw_watchpoint_update(cpu, i); | |
4202 | } | |
4203 | ||
4204 | static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
4205 | uint64_t value) | |
4206 | { | |
4207 | ARMCPU *cpu = arm_env_get_cpu(env); | |
4208 | int i = ri->crm; | |
4209 | ||
4210 | raw_write(env, ri, value); | |
4211 | hw_watchpoint_update(cpu, i); | |
4212 | } | |
4213 | ||
46747d15 PM |
4214 | void hw_breakpoint_update(ARMCPU *cpu, int n) |
4215 | { | |
4216 | CPUARMState *env = &cpu->env; | |
4217 | uint64_t bvr = env->cp15.dbgbvr[n]; | |
4218 | uint64_t bcr = env->cp15.dbgbcr[n]; | |
4219 | vaddr addr; | |
4220 | int bt; | |
4221 | int flags = BP_CPU; | |
4222 | ||
4223 | if (env->cpu_breakpoint[n]) { | |
4224 | cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]); | |
4225 | env->cpu_breakpoint[n] = NULL; | |
4226 | } | |
4227 | ||
4228 | if (!extract64(bcr, 0, 1)) { | |
4229 | /* E bit clear : watchpoint disabled */ | |
4230 | return; | |
4231 | } | |
4232 | ||
4233 | bt = extract64(bcr, 20, 4); | |
4234 | ||
4235 | switch (bt) { | |
4236 | case 4: /* unlinked address mismatch (reserved if AArch64) */ | |
4237 | case 5: /* linked address mismatch (reserved if AArch64) */ | |
4238 | qemu_log_mask(LOG_UNIMP, | |
4239 | "arm: address mismatch breakpoint types not implemented"); | |
4240 | return; | |
4241 | case 0: /* unlinked address match */ | |
4242 | case 1: /* linked address match */ | |
4243 | { | |
4244 | /* Bits [63:49] are hardwired to the value of bit [48]; that is, | |
4245 | * we behave as if the register was sign extended. Bits [1:0] are | |
4246 | * RES0. The BAS field is used to allow setting breakpoints on 16 | |
4247 | * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether | |
4248 | * a bp will fire if the addresses covered by the bp and the addresses | |
4249 | * covered by the insn overlap but the insn doesn't start at the | |
4250 | * start of the bp address range. We choose to require the insn and | |
4251 | * the bp to have the same address. The constraints on writing to | |
4252 | * BAS enforced in dbgbcr_write mean we have only four cases: | |
4253 | * 0b0000 => no breakpoint | |
4254 | * 0b0011 => breakpoint on addr | |
4255 | * 0b1100 => breakpoint on addr + 2 | |
4256 | * 0b1111 => breakpoint on addr | |
4257 | * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c). | |
4258 | */ | |
4259 | int bas = extract64(bcr, 5, 4); | |
4260 | addr = sextract64(bvr, 0, 49) & ~3ULL; | |
4261 | if (bas == 0) { | |
4262 | return; | |
4263 | } | |
4264 | if (bas == 0xc) { | |
4265 | addr += 2; | |
4266 | } | |
4267 | break; | |
4268 | } | |
4269 | case 2: /* unlinked context ID match */ | |
4270 | case 8: /* unlinked VMID match (reserved if no EL2) */ | |
4271 | case 10: /* unlinked context ID and VMID match (reserved if no EL2) */ | |
4272 | qemu_log_mask(LOG_UNIMP, | |
4273 | "arm: unlinked context breakpoint types not implemented"); | |
4274 | return; | |
4275 | case 9: /* linked VMID match (reserved if no EL2) */ | |
4276 | case 11: /* linked context ID and VMID match (reserved if no EL2) */ | |
4277 | case 3: /* linked context ID match */ | |
4278 | default: | |
4279 | /* We must generate no events for Linked context matches (unless | |
4280 | * they are linked to by some other bp/wp, which is handled in | |
4281 | * updates for the linking bp/wp). We choose to also generate no events | |
4282 | * for reserved values. | |
4283 | */ | |
4284 | return; | |
4285 | } | |
4286 | ||
4287 | cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]); | |
4288 | } | |
4289 | ||
4290 | void hw_breakpoint_update_all(ARMCPU *cpu) | |
4291 | { | |
4292 | int i; | |
4293 | CPUARMState *env = &cpu->env; | |
4294 | ||
4295 | /* Completely clear out existing QEMU breakpoints and our array, to | |
4296 | * avoid possible stale entries following migration load. | |
4297 | */ | |
4298 | cpu_breakpoint_remove_all(CPU(cpu), BP_CPU); | |
4299 | memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint)); | |
4300 | ||
4301 | for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) { | |
4302 | hw_breakpoint_update(cpu, i); | |
4303 | } | |
4304 | } | |
4305 | ||
4306 | static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
4307 | uint64_t value) | |
4308 | { | |
4309 | ARMCPU *cpu = arm_env_get_cpu(env); | |
4310 | int i = ri->crm; | |
4311 | ||
4312 | raw_write(env, ri, value); | |
4313 | hw_breakpoint_update(cpu, i); | |
4314 | } | |
4315 | ||
4316 | static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
4317 | uint64_t value) | |
4318 | { | |
4319 | ARMCPU *cpu = arm_env_get_cpu(env); | |
4320 | int i = ri->crm; | |
4321 | ||
4322 | /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only | |
4323 | * copy of BAS[0]. | |
4324 | */ | |
4325 | value = deposit64(value, 6, 1, extract64(value, 5, 1)); | |
4326 | value = deposit64(value, 8, 1, extract64(value, 7, 1)); | |
4327 | ||
4328 | raw_write(env, ri, value); | |
4329 | hw_breakpoint_update(cpu, i); | |
4330 | } | |
4331 | ||
50300698 | 4332 | static void define_debug_regs(ARMCPU *cpu) |
0b45451e | 4333 | { |
50300698 PM |
4334 | /* Define v7 and v8 architectural debug registers. |
4335 | * These are just dummy implementations for now. | |
0b45451e PM |
4336 | */ |
4337 | int i; | |
3ff6fc91 | 4338 | int wrps, brps, ctx_cmps; |
48eb3ae6 PM |
4339 | ARMCPRegInfo dbgdidr = { |
4340 | .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, | |
d6c8cf81 PM |
4341 | .access = PL0_R, .accessfn = access_tda, |
4342 | .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr, | |
48eb3ae6 PM |
4343 | }; |
4344 | ||
3ff6fc91 | 4345 | /* Note that all these register fields hold "number of Xs minus 1". */ |
48eb3ae6 PM |
4346 | brps = extract32(cpu->dbgdidr, 24, 4); |
4347 | wrps = extract32(cpu->dbgdidr, 28, 4); | |
3ff6fc91 PM |
4348 | ctx_cmps = extract32(cpu->dbgdidr, 20, 4); |
4349 | ||
4350 | assert(ctx_cmps <= brps); | |
48eb3ae6 PM |
4351 | |
4352 | /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties | |
4353 | * of the debug registers such as number of breakpoints; | |
4354 | * check that if they both exist then they agree. | |
4355 | */ | |
4356 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { | |
4357 | assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps); | |
4358 | assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps); | |
3ff6fc91 | 4359 | assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps); |
48eb3ae6 | 4360 | } |
0b45451e | 4361 | |
48eb3ae6 | 4362 | define_one_arm_cp_reg(cpu, &dbgdidr); |
50300698 PM |
4363 | define_arm_cp_regs(cpu, debug_cp_reginfo); |
4364 | ||
4365 | if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) { | |
4366 | define_arm_cp_regs(cpu, debug_lpae_cp_reginfo); | |
4367 | } | |
4368 | ||
48eb3ae6 | 4369 | for (i = 0; i < brps + 1; i++) { |
0b45451e | 4370 | ARMCPRegInfo dbgregs[] = { |
10aae104 PM |
4371 | { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH, |
4372 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4, | |
d6c8cf81 | 4373 | .access = PL1_RW, .accessfn = access_tda, |
46747d15 PM |
4374 | .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]), |
4375 | .writefn = dbgbvr_write, .raw_writefn = raw_write | |
4376 | }, | |
10aae104 PM |
4377 | { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH, |
4378 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5, | |
d6c8cf81 | 4379 | .access = PL1_RW, .accessfn = access_tda, |
46747d15 PM |
4380 | .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]), |
4381 | .writefn = dbgbcr_write, .raw_writefn = raw_write | |
4382 | }, | |
48eb3ae6 PM |
4383 | REGINFO_SENTINEL |
4384 | }; | |
4385 | define_arm_cp_regs(cpu, dbgregs); | |
4386 | } | |
4387 | ||
4388 | for (i = 0; i < wrps + 1; i++) { | |
4389 | ARMCPRegInfo dbgregs[] = { | |
10aae104 PM |
4390 | { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH, |
4391 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6, | |
d6c8cf81 | 4392 | .access = PL1_RW, .accessfn = access_tda, |
9ee98ce8 PM |
4393 | .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]), |
4394 | .writefn = dbgwvr_write, .raw_writefn = raw_write | |
4395 | }, | |
10aae104 PM |
4396 | { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH, |
4397 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7, | |
d6c8cf81 | 4398 | .access = PL1_RW, .accessfn = access_tda, |
9ee98ce8 PM |
4399 | .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]), |
4400 | .writefn = dbgwcr_write, .raw_writefn = raw_write | |
4401 | }, | |
4402 | REGINFO_SENTINEL | |
0b45451e PM |
4403 | }; |
4404 | define_arm_cp_regs(cpu, dbgregs); | |
4405 | } | |
4406 | } | |
4407 | ||
2ceb98c0 PM |
4408 | void register_cp_regs_for_features(ARMCPU *cpu) |
4409 | { | |
4410 | /* Register all the coprocessor registers based on feature bits */ | |
4411 | CPUARMState *env = &cpu->env; | |
4412 | if (arm_feature(env, ARM_FEATURE_M)) { | |
4413 | /* M profile has no coprocessor registers */ | |
4414 | return; | |
4415 | } | |
4416 | ||
e9aa6c21 | 4417 | define_arm_cp_regs(cpu, cp_reginfo); |
9449fdf6 PM |
4418 | if (!arm_feature(env, ARM_FEATURE_V8)) { |
4419 | /* Must go early as it is full of wildcards that may be | |
4420 | * overridden by later definitions. | |
4421 | */ | |
4422 | define_arm_cp_regs(cpu, not_v8_cp_reginfo); | |
4423 | } | |
4424 | ||
7d57f408 | 4425 | if (arm_feature(env, ARM_FEATURE_V6)) { |
8515a092 PM |
4426 | /* The ID registers all have impdef reset values */ |
4427 | ARMCPRegInfo v6_idregs[] = { | |
0ff644a7 PM |
4428 | { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, |
4429 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, | |
4430 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4431 | .resetvalue = cpu->id_pfr0 }, |
0ff644a7 PM |
4432 | { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, |
4433 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, | |
4434 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4435 | .resetvalue = cpu->id_pfr1 }, |
0ff644a7 PM |
4436 | { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, |
4437 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, | |
4438 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4439 | .resetvalue = cpu->id_dfr0 }, |
0ff644a7 PM |
4440 | { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, |
4441 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, | |
4442 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4443 | .resetvalue = cpu->id_afr0 }, |
0ff644a7 PM |
4444 | { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, |
4445 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, | |
4446 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4447 | .resetvalue = cpu->id_mmfr0 }, |
0ff644a7 PM |
4448 | { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, |
4449 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, | |
4450 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4451 | .resetvalue = cpu->id_mmfr1 }, |
0ff644a7 PM |
4452 | { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, |
4453 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, | |
4454 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4455 | .resetvalue = cpu->id_mmfr2 }, |
0ff644a7 PM |
4456 | { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, |
4457 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, | |
4458 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4459 | .resetvalue = cpu->id_mmfr3 }, |
0ff644a7 PM |
4460 | { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, |
4461 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, | |
4462 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4463 | .resetvalue = cpu->id_isar0 }, |
0ff644a7 PM |
4464 | { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, |
4465 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, | |
4466 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4467 | .resetvalue = cpu->id_isar1 }, |
0ff644a7 PM |
4468 | { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, |
4469 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, | |
4470 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4471 | .resetvalue = cpu->id_isar2 }, |
0ff644a7 PM |
4472 | { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, |
4473 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, | |
4474 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4475 | .resetvalue = cpu->id_isar3 }, |
0ff644a7 PM |
4476 | { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, |
4477 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, | |
4478 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4479 | .resetvalue = cpu->id_isar4 }, |
0ff644a7 PM |
4480 | { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, |
4481 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, | |
4482 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 | 4483 | .resetvalue = cpu->id_isar5 }, |
e20d84c1 PM |
4484 | { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH, |
4485 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6, | |
4486 | .access = PL1_R, .type = ARM_CP_CONST, | |
4487 | .resetvalue = cpu->id_mmfr4 }, | |
4488 | /* 7 is as yet unallocated and must RAZ */ | |
4489 | { .name = "ID_ISAR7_RESERVED", .state = ARM_CP_STATE_BOTH, | |
4490 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7, | |
4491 | .access = PL1_R, .type = ARM_CP_CONST, | |
8515a092 PM |
4492 | .resetvalue = 0 }, |
4493 | REGINFO_SENTINEL | |
4494 | }; | |
4495 | define_arm_cp_regs(cpu, v6_idregs); | |
7d57f408 PM |
4496 | define_arm_cp_regs(cpu, v6_cp_reginfo); |
4497 | } else { | |
4498 | define_arm_cp_regs(cpu, not_v6_cp_reginfo); | |
4499 | } | |
4d31c596 PM |
4500 | if (arm_feature(env, ARM_FEATURE_V6K)) { |
4501 | define_arm_cp_regs(cpu, v6k_cp_reginfo); | |
4502 | } | |
5e5cf9e3 PC |
4503 | if (arm_feature(env, ARM_FEATURE_V7MP) && |
4504 | !arm_feature(env, ARM_FEATURE_MPU)) { | |
995939a6 PM |
4505 | define_arm_cp_regs(cpu, v7mp_cp_reginfo); |
4506 | } | |
e9aa6c21 | 4507 | if (arm_feature(env, ARM_FEATURE_V7)) { |
200ac0ef | 4508 | /* v7 performance monitor control register: same implementor |
7c2cb42b AF |
4509 | * field as main ID register, and we implement only the cycle |
4510 | * count register. | |
200ac0ef | 4511 | */ |
7c2cb42b | 4512 | #ifndef CONFIG_USER_ONLY |
200ac0ef PM |
4513 | ARMCPRegInfo pmcr = { |
4514 | .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, | |
8521466b | 4515 | .access = PL0_RW, |
7a0e58fa | 4516 | .type = ARM_CP_IO | ARM_CP_ALIAS, |
8521466b | 4517 | .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), |
fcd25206 PM |
4518 | .accessfn = pmreg_access, .writefn = pmcr_write, |
4519 | .raw_writefn = raw_write, | |
200ac0ef | 4520 | }; |
8521466b AF |
4521 | ARMCPRegInfo pmcr64 = { |
4522 | .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, | |
4523 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, | |
4524 | .access = PL0_RW, .accessfn = pmreg_access, | |
4525 | .type = ARM_CP_IO, | |
4526 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), | |
4527 | .resetvalue = cpu->midr & 0xff000000, | |
4528 | .writefn = pmcr_write, .raw_writefn = raw_write, | |
4529 | }; | |
7c2cb42b | 4530 | define_one_arm_cp_reg(cpu, &pmcr); |
8521466b | 4531 | define_one_arm_cp_reg(cpu, &pmcr64); |
7c2cb42b | 4532 | #endif |
776d4e5c | 4533 | ARMCPRegInfo clidr = { |
7da845b0 PM |
4534 | .name = "CLIDR", .state = ARM_CP_STATE_BOTH, |
4535 | .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, | |
776d4e5c PM |
4536 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr |
4537 | }; | |
776d4e5c | 4538 | define_one_arm_cp_reg(cpu, &clidr); |
e9aa6c21 | 4539 | define_arm_cp_regs(cpu, v7_cp_reginfo); |
50300698 | 4540 | define_debug_regs(cpu); |
7d57f408 PM |
4541 | } else { |
4542 | define_arm_cp_regs(cpu, not_v7_cp_reginfo); | |
e9aa6c21 | 4543 | } |
b0d2b7d0 | 4544 | if (arm_feature(env, ARM_FEATURE_V8)) { |
e20d84c1 PM |
4545 | /* AArch64 ID registers, which all have impdef reset values. |
4546 | * Note that within the ID register ranges the unused slots | |
4547 | * must all RAZ, not UNDEF; future architecture versions may | |
4548 | * define new registers here. | |
4549 | */ | |
e60cef86 PM |
4550 | ARMCPRegInfo v8_idregs[] = { |
4551 | { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, | |
4552 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, | |
4553 | .access = PL1_R, .type = ARM_CP_CONST, | |
4554 | .resetvalue = cpu->id_aa64pfr0 }, | |
4555 | { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, | |
4556 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, | |
4557 | .access = PL1_R, .type = ARM_CP_CONST, | |
4558 | .resetvalue = cpu->id_aa64pfr1}, | |
e20d84c1 PM |
4559 | { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, |
4560 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2, | |
4561 | .access = PL1_R, .type = ARM_CP_CONST, | |
4562 | .resetvalue = 0 }, | |
4563 | { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4564 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3, | |
4565 | .access = PL1_R, .type = ARM_CP_CONST, | |
4566 | .resetvalue = 0 }, | |
4567 | { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4568 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4, | |
4569 | .access = PL1_R, .type = ARM_CP_CONST, | |
4570 | .resetvalue = 0 }, | |
4571 | { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4572 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5, | |
4573 | .access = PL1_R, .type = ARM_CP_CONST, | |
4574 | .resetvalue = 0 }, | |
4575 | { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4576 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6, | |
4577 | .access = PL1_R, .type = ARM_CP_CONST, | |
4578 | .resetvalue = 0 }, | |
4579 | { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4580 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7, | |
4581 | .access = PL1_R, .type = ARM_CP_CONST, | |
4582 | .resetvalue = 0 }, | |
e60cef86 PM |
4583 | { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, |
4584 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, | |
4585 | .access = PL1_R, .type = ARM_CP_CONST, | |
5d831be2 | 4586 | /* We mask out the PMUVer field, because we don't currently |
9225d739 PM |
4587 | * implement the PMU. Not advertising it prevents the guest |
4588 | * from trying to use it and getting UNDEFs on registers we | |
4589 | * don't implement. | |
4590 | */ | |
4591 | .resetvalue = cpu->id_aa64dfr0 & ~0xf00 }, | |
e60cef86 PM |
4592 | { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, |
4593 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, | |
4594 | .access = PL1_R, .type = ARM_CP_CONST, | |
4595 | .resetvalue = cpu->id_aa64dfr1 }, | |
e20d84c1 PM |
4596 | { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, |
4597 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2, | |
4598 | .access = PL1_R, .type = ARM_CP_CONST, | |
4599 | .resetvalue = 0 }, | |
4600 | { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4601 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3, | |
4602 | .access = PL1_R, .type = ARM_CP_CONST, | |
4603 | .resetvalue = 0 }, | |
e60cef86 PM |
4604 | { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, |
4605 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, | |
4606 | .access = PL1_R, .type = ARM_CP_CONST, | |
4607 | .resetvalue = cpu->id_aa64afr0 }, | |
4608 | { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, | |
4609 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, | |
4610 | .access = PL1_R, .type = ARM_CP_CONST, | |
4611 | .resetvalue = cpu->id_aa64afr1 }, | |
e20d84c1 PM |
4612 | { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, |
4613 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6, | |
4614 | .access = PL1_R, .type = ARM_CP_CONST, | |
4615 | .resetvalue = 0 }, | |
4616 | { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4617 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7, | |
4618 | .access = PL1_R, .type = ARM_CP_CONST, | |
4619 | .resetvalue = 0 }, | |
e60cef86 PM |
4620 | { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, |
4621 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, | |
4622 | .access = PL1_R, .type = ARM_CP_CONST, | |
4623 | .resetvalue = cpu->id_aa64isar0 }, | |
4624 | { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, | |
4625 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, | |
4626 | .access = PL1_R, .type = ARM_CP_CONST, | |
4627 | .resetvalue = cpu->id_aa64isar1 }, | |
e20d84c1 PM |
4628 | { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, |
4629 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2, | |
4630 | .access = PL1_R, .type = ARM_CP_CONST, | |
4631 | .resetvalue = 0 }, | |
4632 | { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4633 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3, | |
4634 | .access = PL1_R, .type = ARM_CP_CONST, | |
4635 | .resetvalue = 0 }, | |
4636 | { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4637 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4, | |
4638 | .access = PL1_R, .type = ARM_CP_CONST, | |
4639 | .resetvalue = 0 }, | |
4640 | { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4641 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5, | |
4642 | .access = PL1_R, .type = ARM_CP_CONST, | |
4643 | .resetvalue = 0 }, | |
4644 | { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4645 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6, | |
4646 | .access = PL1_R, .type = ARM_CP_CONST, | |
4647 | .resetvalue = 0 }, | |
4648 | { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4649 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7, | |
4650 | .access = PL1_R, .type = ARM_CP_CONST, | |
4651 | .resetvalue = 0 }, | |
e60cef86 PM |
4652 | { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, |
4653 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, | |
4654 | .access = PL1_R, .type = ARM_CP_CONST, | |
4655 | .resetvalue = cpu->id_aa64mmfr0 }, | |
4656 | { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, | |
4657 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, | |
4658 | .access = PL1_R, .type = ARM_CP_CONST, | |
4659 | .resetvalue = cpu->id_aa64mmfr1 }, | |
e20d84c1 PM |
4660 | { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, |
4661 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2, | |
4662 | .access = PL1_R, .type = ARM_CP_CONST, | |
4663 | .resetvalue = 0 }, | |
4664 | { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4665 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3, | |
4666 | .access = PL1_R, .type = ARM_CP_CONST, | |
4667 | .resetvalue = 0 }, | |
4668 | { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4669 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4, | |
4670 | .access = PL1_R, .type = ARM_CP_CONST, | |
4671 | .resetvalue = 0 }, | |
4672 | { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4673 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5, | |
4674 | .access = PL1_R, .type = ARM_CP_CONST, | |
4675 | .resetvalue = 0 }, | |
4676 | { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4677 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6, | |
4678 | .access = PL1_R, .type = ARM_CP_CONST, | |
4679 | .resetvalue = 0 }, | |
4680 | { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4681 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7, | |
4682 | .access = PL1_R, .type = ARM_CP_CONST, | |
4683 | .resetvalue = 0 }, | |
a50c0f51 PM |
4684 | { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, |
4685 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, | |
4686 | .access = PL1_R, .type = ARM_CP_CONST, | |
4687 | .resetvalue = cpu->mvfr0 }, | |
4688 | { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, | |
4689 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, | |
4690 | .access = PL1_R, .type = ARM_CP_CONST, | |
4691 | .resetvalue = cpu->mvfr1 }, | |
4692 | { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, | |
4693 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, | |
4694 | .access = PL1_R, .type = ARM_CP_CONST, | |
4695 | .resetvalue = cpu->mvfr2 }, | |
e20d84c1 PM |
4696 | { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, |
4697 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3, | |
4698 | .access = PL1_R, .type = ARM_CP_CONST, | |
4699 | .resetvalue = 0 }, | |
4700 | { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4701 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4, | |
4702 | .access = PL1_R, .type = ARM_CP_CONST, | |
4703 | .resetvalue = 0 }, | |
4704 | { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4705 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5, | |
4706 | .access = PL1_R, .type = ARM_CP_CONST, | |
4707 | .resetvalue = 0 }, | |
4708 | { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4709 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6, | |
4710 | .access = PL1_R, .type = ARM_CP_CONST, | |
4711 | .resetvalue = 0 }, | |
4712 | { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, | |
4713 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7, | |
4714 | .access = PL1_R, .type = ARM_CP_CONST, | |
4715 | .resetvalue = 0 }, | |
4054bfa9 AF |
4716 | { .name = "PMCEID0", .state = ARM_CP_STATE_AA32, |
4717 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6, | |
4718 | .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, | |
4719 | .resetvalue = cpu->pmceid0 }, | |
4720 | { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64, | |
4721 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6, | |
4722 | .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, | |
4723 | .resetvalue = cpu->pmceid0 }, | |
4724 | { .name = "PMCEID1", .state = ARM_CP_STATE_AA32, | |
4725 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7, | |
4726 | .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, | |
4727 | .resetvalue = cpu->pmceid1 }, | |
4728 | { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64, | |
4729 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7, | |
4730 | .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, | |
4731 | .resetvalue = cpu->pmceid1 }, | |
e60cef86 PM |
4732 | REGINFO_SENTINEL |
4733 | }; | |
be8e8128 GB |
4734 | /* RVBAR_EL1 is only implemented if EL1 is the highest EL */ |
4735 | if (!arm_feature(env, ARM_FEATURE_EL3) && | |
4736 | !arm_feature(env, ARM_FEATURE_EL2)) { | |
4737 | ARMCPRegInfo rvbar = { | |
4738 | .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64, | |
4739 | .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, | |
4740 | .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar | |
4741 | }; | |
4742 | define_one_arm_cp_reg(cpu, &rvbar); | |
4743 | } | |
e60cef86 | 4744 | define_arm_cp_regs(cpu, v8_idregs); |
b0d2b7d0 PM |
4745 | define_arm_cp_regs(cpu, v8_cp_reginfo); |
4746 | } | |
3b685ba7 | 4747 | if (arm_feature(env, ARM_FEATURE_EL2)) { |
f0d574d6 | 4748 | uint64_t vmpidr_def = mpidr_read_val(env); |
731de9e6 EI |
4749 | ARMCPRegInfo vpidr_regs[] = { |
4750 | { .name = "VPIDR", .state = ARM_CP_STATE_AA32, | |
4751 | .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, | |
4752 | .access = PL2_RW, .accessfn = access_el3_aa32ns, | |
4753 | .resetvalue = cpu->midr, | |
4754 | .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, | |
4755 | { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64, | |
4756 | .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, | |
4757 | .access = PL2_RW, .resetvalue = cpu->midr, | |
4758 | .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, | |
f0d574d6 EI |
4759 | { .name = "VMPIDR", .state = ARM_CP_STATE_AA32, |
4760 | .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, | |
4761 | .access = PL2_RW, .accessfn = access_el3_aa32ns, | |
4762 | .resetvalue = vmpidr_def, | |
4763 | .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, | |
4764 | { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64, | |
4765 | .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, | |
4766 | .access = PL2_RW, | |
4767 | .resetvalue = vmpidr_def, | |
4768 | .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, | |
731de9e6 EI |
4769 | REGINFO_SENTINEL |
4770 | }; | |
4771 | define_arm_cp_regs(cpu, vpidr_regs); | |
4771cd01 | 4772 | define_arm_cp_regs(cpu, el2_cp_reginfo); |
be8e8128 GB |
4773 | /* RVBAR_EL2 is only implemented if EL2 is the highest EL */ |
4774 | if (!arm_feature(env, ARM_FEATURE_EL3)) { | |
4775 | ARMCPRegInfo rvbar = { | |
4776 | .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64, | |
4777 | .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1, | |
4778 | .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar | |
4779 | }; | |
4780 | define_one_arm_cp_reg(cpu, &rvbar); | |
4781 | } | |
d42e3c26 EI |
4782 | } else { |
4783 | /* If EL2 is missing but higher ELs are enabled, we need to | |
4784 | * register the no_el2 reginfos. | |
4785 | */ | |
4786 | if (arm_feature(env, ARM_FEATURE_EL3)) { | |
f0d574d6 EI |
4787 | /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value |
4788 | * of MIDR_EL1 and MPIDR_EL1. | |
731de9e6 EI |
4789 | */ |
4790 | ARMCPRegInfo vpidr_regs[] = { | |
4791 | { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH, | |
4792 | .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, | |
4793 | .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, | |
4794 | .type = ARM_CP_CONST, .resetvalue = cpu->midr, | |
4795 | .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, | |
f0d574d6 EI |
4796 | { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH, |
4797 | .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, | |
4798 | .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, | |
4799 | .type = ARM_CP_NO_RAW, | |
4800 | .writefn = arm_cp_write_ignore, .readfn = mpidr_read }, | |
731de9e6 EI |
4801 | REGINFO_SENTINEL |
4802 | }; | |
4803 | define_arm_cp_regs(cpu, vpidr_regs); | |
4771cd01 | 4804 | define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo); |
d42e3c26 | 4805 | } |
3b685ba7 | 4806 | } |
81547d66 | 4807 | if (arm_feature(env, ARM_FEATURE_EL3)) { |
0f1a3b24 | 4808 | define_arm_cp_regs(cpu, el3_cp_reginfo); |
e24fdd23 PM |
4809 | ARMCPRegInfo el3_regs[] = { |
4810 | { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64, | |
4811 | .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1, | |
4812 | .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar }, | |
4813 | { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, | |
4814 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, | |
4815 | .access = PL3_RW, | |
4816 | .raw_writefn = raw_write, .writefn = sctlr_write, | |
4817 | .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]), | |
4818 | .resetvalue = cpu->reset_sctlr }, | |
4819 | REGINFO_SENTINEL | |
be8e8128 | 4820 | }; |
e24fdd23 PM |
4821 | |
4822 | define_arm_cp_regs(cpu, el3_regs); | |
81547d66 | 4823 | } |
2f027fc5 PM |
4824 | /* The behaviour of NSACR is sufficiently various that we don't |
4825 | * try to describe it in a single reginfo: | |
4826 | * if EL3 is 64 bit, then trap to EL3 from S EL1, | |
4827 | * reads as constant 0xc00 from NS EL1 and NS EL2 | |
4828 | * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2 | |
4829 | * if v7 without EL3, register doesn't exist | |
4830 | * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2 | |
4831 | */ | |
4832 | if (arm_feature(env, ARM_FEATURE_EL3)) { | |
4833 | if (arm_feature(env, ARM_FEATURE_AARCH64)) { | |
4834 | ARMCPRegInfo nsacr = { | |
4835 | .name = "NSACR", .type = ARM_CP_CONST, | |
4836 | .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, | |
4837 | .access = PL1_RW, .accessfn = nsacr_access, | |
4838 | .resetvalue = 0xc00 | |
4839 | }; | |
4840 | define_one_arm_cp_reg(cpu, &nsacr); | |
4841 | } else { | |
4842 | ARMCPRegInfo nsacr = { | |
4843 | .name = "NSACR", | |
4844 | .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, | |
4845 | .access = PL3_RW | PL1_R, | |
4846 | .resetvalue = 0, | |
4847 | .fieldoffset = offsetof(CPUARMState, cp15.nsacr) | |
4848 | }; | |
4849 | define_one_arm_cp_reg(cpu, &nsacr); | |
4850 | } | |
4851 | } else { | |
4852 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
4853 | ARMCPRegInfo nsacr = { | |
4854 | .name = "NSACR", .type = ARM_CP_CONST, | |
4855 | .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, | |
4856 | .access = PL1_R, | |
4857 | .resetvalue = 0xc00 | |
4858 | }; | |
4859 | define_one_arm_cp_reg(cpu, &nsacr); | |
4860 | } | |
4861 | } | |
4862 | ||
18032bec | 4863 | if (arm_feature(env, ARM_FEATURE_MPU)) { |
6cb0b013 PC |
4864 | if (arm_feature(env, ARM_FEATURE_V6)) { |
4865 | /* PMSAv6 not implemented */ | |
4866 | assert(arm_feature(env, ARM_FEATURE_V7)); | |
4867 | define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); | |
4868 | define_arm_cp_regs(cpu, pmsav7_cp_reginfo); | |
4869 | } else { | |
4870 | define_arm_cp_regs(cpu, pmsav5_cp_reginfo); | |
4871 | } | |
18032bec | 4872 | } else { |
8e5d75c9 | 4873 | define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); |
18032bec PM |
4874 | define_arm_cp_regs(cpu, vmsa_cp_reginfo); |
4875 | } | |
c326b979 PM |
4876 | if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { |
4877 | define_arm_cp_regs(cpu, t2ee_cp_reginfo); | |
4878 | } | |
6cc7a3ae PM |
4879 | if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { |
4880 | define_arm_cp_regs(cpu, generic_timer_cp_reginfo); | |
4881 | } | |
4a501606 PM |
4882 | if (arm_feature(env, ARM_FEATURE_VAPA)) { |
4883 | define_arm_cp_regs(cpu, vapa_cp_reginfo); | |
4884 | } | |
c4804214 PM |
4885 | if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { |
4886 | define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); | |
4887 | } | |
4888 | if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { | |
4889 | define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); | |
4890 | } | |
4891 | if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { | |
4892 | define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); | |
4893 | } | |
18032bec PM |
4894 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) { |
4895 | define_arm_cp_regs(cpu, omap_cp_reginfo); | |
4896 | } | |
34f90529 PM |
4897 | if (arm_feature(env, ARM_FEATURE_STRONGARM)) { |
4898 | define_arm_cp_regs(cpu, strongarm_cp_reginfo); | |
4899 | } | |
1047b9d7 PM |
4900 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { |
4901 | define_arm_cp_regs(cpu, xscale_cp_reginfo); | |
4902 | } | |
4903 | if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { | |
4904 | define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); | |
4905 | } | |
7ac681cf PM |
4906 | if (arm_feature(env, ARM_FEATURE_LPAE)) { |
4907 | define_arm_cp_regs(cpu, lpae_cp_reginfo); | |
4908 | } | |
7884849c PM |
4909 | /* Slightly awkwardly, the OMAP and StrongARM cores need all of |
4910 | * cp15 crn=0 to be writes-ignored, whereas for other cores they should | |
4911 | * be read-only (ie write causes UNDEF exception). | |
4912 | */ | |
4913 | { | |
00a29f3d PM |
4914 | ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { |
4915 | /* Pre-v8 MIDR space. | |
4916 | * Note that the MIDR isn't a simple constant register because | |
7884849c PM |
4917 | * of the TI925 behaviour where writes to another register can |
4918 | * cause the MIDR value to change. | |
97ce8d61 PC |
4919 | * |
4920 | * Unimplemented registers in the c15 0 0 0 space default to | |
4921 | * MIDR. Define MIDR first as this entire space, then CTR, TCMTR | |
4922 | * and friends override accordingly. | |
7884849c PM |
4923 | */ |
4924 | { .name = "MIDR", | |
97ce8d61 | 4925 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, |
7884849c | 4926 | .access = PL1_R, .resetvalue = cpu->midr, |
d4e6df63 | 4927 | .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, |
731de9e6 | 4928 | .readfn = midr_read, |
97ce8d61 PC |
4929 | .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), |
4930 | .type = ARM_CP_OVERRIDE }, | |
7884849c PM |
4931 | /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ |
4932 | { .name = "DUMMY", | |
4933 | .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, | |
4934 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
4935 | { .name = "DUMMY", | |
4936 | .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, | |
4937 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
4938 | { .name = "DUMMY", | |
4939 | .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, | |
4940 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
4941 | { .name = "DUMMY", | |
4942 | .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, | |
4943 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
4944 | { .name = "DUMMY", | |
4945 | .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, | |
4946 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
4947 | REGINFO_SENTINEL | |
4948 | }; | |
00a29f3d | 4949 | ARMCPRegInfo id_v8_midr_cp_reginfo[] = { |
00a29f3d PM |
4950 | { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, |
4951 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, | |
731de9e6 EI |
4952 | .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr, |
4953 | .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), | |
4954 | .readfn = midr_read }, | |
ac00c79f SF |
4955 | /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */ |
4956 | { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, | |
4957 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, | |
4958 | .access = PL1_R, .resetvalue = cpu->midr }, | |
4959 | { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, | |
4960 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7, | |
4961 | .access = PL1_R, .resetvalue = cpu->midr }, | |
00a29f3d PM |
4962 | { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, |
4963 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, | |
13b72b2b | 4964 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr }, |
00a29f3d PM |
4965 | REGINFO_SENTINEL |
4966 | }; | |
4967 | ARMCPRegInfo id_cp_reginfo[] = { | |
4968 | /* These are common to v8 and pre-v8 */ | |
4969 | { .name = "CTR", | |
4970 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, | |
4971 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, | |
4972 | { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, | |
4973 | .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, | |
4974 | .access = PL0_R, .accessfn = ctr_el0_access, | |
4975 | .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, | |
4976 | /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ | |
4977 | { .name = "TCMTR", | |
4978 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, | |
4979 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
00a29f3d PM |
4980 | REGINFO_SENTINEL |
4981 | }; | |
8085ce63 PC |
4982 | /* TLBTR is specific to VMSA */ |
4983 | ARMCPRegInfo id_tlbtr_reginfo = { | |
4984 | .name = "TLBTR", | |
4985 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, | |
4986 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0, | |
4987 | }; | |
3281af81 PC |
4988 | /* MPUIR is specific to PMSA V6+ */ |
4989 | ARMCPRegInfo id_mpuir_reginfo = { | |
4990 | .name = "MPUIR", | |
4991 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, | |
4992 | .access = PL1_R, .type = ARM_CP_CONST, | |
4993 | .resetvalue = cpu->pmsav7_dregion << 8 | |
4994 | }; | |
7884849c PM |
4995 | ARMCPRegInfo crn0_wi_reginfo = { |
4996 | .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, | |
4997 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, | |
4998 | .type = ARM_CP_NOP | ARM_CP_OVERRIDE | |
4999 | }; | |
5000 | if (arm_feature(env, ARM_FEATURE_OMAPCP) || | |
5001 | arm_feature(env, ARM_FEATURE_STRONGARM)) { | |
5002 | ARMCPRegInfo *r; | |
5003 | /* Register the blanket "writes ignored" value first to cover the | |
a703eda1 PC |
5004 | * whole space. Then update the specific ID registers to allow write |
5005 | * access, so that they ignore writes rather than causing them to | |
5006 | * UNDEF. | |
7884849c PM |
5007 | */ |
5008 | define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); | |
00a29f3d PM |
5009 | for (r = id_pre_v8_midr_cp_reginfo; |
5010 | r->type != ARM_CP_SENTINEL; r++) { | |
5011 | r->access = PL1_RW; | |
5012 | } | |
7884849c PM |
5013 | for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) { |
5014 | r->access = PL1_RW; | |
7884849c | 5015 | } |
8085ce63 | 5016 | id_tlbtr_reginfo.access = PL1_RW; |
3281af81 | 5017 | id_tlbtr_reginfo.access = PL1_RW; |
7884849c | 5018 | } |
00a29f3d PM |
5019 | if (arm_feature(env, ARM_FEATURE_V8)) { |
5020 | define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); | |
5021 | } else { | |
5022 | define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); | |
5023 | } | |
a703eda1 | 5024 | define_arm_cp_regs(cpu, id_cp_reginfo); |
8085ce63 PC |
5025 | if (!arm_feature(env, ARM_FEATURE_MPU)) { |
5026 | define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo); | |
3281af81 PC |
5027 | } else if (arm_feature(env, ARM_FEATURE_V7)) { |
5028 | define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); | |
8085ce63 | 5029 | } |
7884849c PM |
5030 | } |
5031 | ||
97ce8d61 PC |
5032 | if (arm_feature(env, ARM_FEATURE_MPIDR)) { |
5033 | define_arm_cp_regs(cpu, mpidr_cp_reginfo); | |
5034 | } | |
5035 | ||
2771db27 | 5036 | if (arm_feature(env, ARM_FEATURE_AUXCR)) { |
834a6c69 PM |
5037 | ARMCPRegInfo auxcr_reginfo[] = { |
5038 | { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, | |
5039 | .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, | |
5040 | .access = PL1_RW, .type = ARM_CP_CONST, | |
5041 | .resetvalue = cpu->reset_auxcr }, | |
5042 | { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH, | |
5043 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1, | |
5044 | .access = PL2_RW, .type = ARM_CP_CONST, | |
5045 | .resetvalue = 0 }, | |
5046 | { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64, | |
5047 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1, | |
5048 | .access = PL3_RW, .type = ARM_CP_CONST, | |
5049 | .resetvalue = 0 }, | |
5050 | REGINFO_SENTINEL | |
2771db27 | 5051 | }; |
834a6c69 | 5052 | define_arm_cp_regs(cpu, auxcr_reginfo); |
2771db27 PM |
5053 | } |
5054 | ||
d8ba780b | 5055 | if (arm_feature(env, ARM_FEATURE_CBAR)) { |
f318cec6 PM |
5056 | if (arm_feature(env, ARM_FEATURE_AARCH64)) { |
5057 | /* 32 bit view is [31:18] 0...0 [43:32]. */ | |
5058 | uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) | |
5059 | | extract64(cpu->reset_cbar, 32, 12); | |
5060 | ARMCPRegInfo cbar_reginfo[] = { | |
5061 | { .name = "CBAR", | |
5062 | .type = ARM_CP_CONST, | |
5063 | .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, | |
5064 | .access = PL1_R, .resetvalue = cpu->reset_cbar }, | |
5065 | { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, | |
5066 | .type = ARM_CP_CONST, | |
5067 | .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, | |
5068 | .access = PL1_R, .resetvalue = cbar32 }, | |
5069 | REGINFO_SENTINEL | |
5070 | }; | |
5071 | /* We don't implement a r/w 64 bit CBAR currently */ | |
5072 | assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); | |
5073 | define_arm_cp_regs(cpu, cbar_reginfo); | |
5074 | } else { | |
5075 | ARMCPRegInfo cbar = { | |
5076 | .name = "CBAR", | |
5077 | .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, | |
5078 | .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar, | |
5079 | .fieldoffset = offsetof(CPUARMState, | |
5080 | cp15.c15_config_base_address) | |
5081 | }; | |
5082 | if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { | |
5083 | cbar.access = PL1_R; | |
5084 | cbar.fieldoffset = 0; | |
5085 | cbar.type = ARM_CP_CONST; | |
5086 | } | |
5087 | define_one_arm_cp_reg(cpu, &cbar); | |
5088 | } | |
d8ba780b PC |
5089 | } |
5090 | ||
91db4642 CLG |
5091 | if (arm_feature(env, ARM_FEATURE_VBAR)) { |
5092 | ARMCPRegInfo vbar_cp_reginfo[] = { | |
5093 | { .name = "VBAR", .state = ARM_CP_STATE_BOTH, | |
5094 | .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, | |
5095 | .access = PL1_RW, .writefn = vbar_write, | |
5096 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s), | |
5097 | offsetof(CPUARMState, cp15.vbar_ns) }, | |
5098 | .resetvalue = 0 }, | |
5099 | REGINFO_SENTINEL | |
5100 | }; | |
5101 | define_arm_cp_regs(cpu, vbar_cp_reginfo); | |
5102 | } | |
5103 | ||
2771db27 PM |
5104 | /* Generic registers whose values depend on the implementation */ |
5105 | { | |
5106 | ARMCPRegInfo sctlr = { | |
5ebafdf3 | 5107 | .name = "SCTLR", .state = ARM_CP_STATE_BOTH, |
137feaa9 FA |
5108 | .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, |
5109 | .access = PL1_RW, | |
5110 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), | |
5111 | offsetof(CPUARMState, cp15.sctlr_ns) }, | |
d4e6df63 PM |
5112 | .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, |
5113 | .raw_writefn = raw_write, | |
2771db27 PM |
5114 | }; |
5115 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { | |
5116 | /* Normally we would always end the TB on an SCTLR write, but Linux | |
5117 | * arch/arm/mach-pxa/sleep.S expects two instructions following | |
5118 | * an MMU enable to execute from cache. Imitate this behaviour. | |
5119 | */ | |
5120 | sctlr.type |= ARM_CP_SUPPRESS_TB_END; | |
5121 | } | |
5122 | define_one_arm_cp_reg(cpu, &sctlr); | |
5123 | } | |
2ceb98c0 PM |
5124 | } |
5125 | ||
778c3a06 | 5126 | ARMCPU *cpu_arm_init(const char *cpu_model) |
40f137e1 | 5127 | { |
9262685b | 5128 | return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model)); |
14969266 AF |
5129 | } |
5130 | ||
5131 | void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) | |
5132 | { | |
22169d41 | 5133 | CPUState *cs = CPU(cpu); |
14969266 AF |
5134 | CPUARMState *env = &cpu->env; |
5135 | ||
6a669427 PM |
5136 | if (arm_feature(env, ARM_FEATURE_AARCH64)) { |
5137 | gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg, | |
5138 | aarch64_fpu_gdb_set_reg, | |
5139 | 34, "aarch64-fpu.xml", 0); | |
5140 | } else if (arm_feature(env, ARM_FEATURE_NEON)) { | |
22169d41 | 5141 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, |
56aebc89 PB |
5142 | 51, "arm-neon.xml", 0); |
5143 | } else if (arm_feature(env, ARM_FEATURE_VFP3)) { | |
22169d41 | 5144 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, |
56aebc89 PB |
5145 | 35, "arm-vfp3.xml", 0); |
5146 | } else if (arm_feature(env, ARM_FEATURE_VFP)) { | |
22169d41 | 5147 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, |
56aebc89 PB |
5148 | 19, "arm-vfp.xml", 0); |
5149 | } | |
40f137e1 PB |
5150 | } |
5151 | ||
777dc784 PM |
5152 | /* Sort alphabetically by type name, except for "any". */ |
5153 | static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) | |
5adb4839 | 5154 | { |
777dc784 PM |
5155 | ObjectClass *class_a = (ObjectClass *)a; |
5156 | ObjectClass *class_b = (ObjectClass *)b; | |
5157 | const char *name_a, *name_b; | |
5adb4839 | 5158 | |
777dc784 PM |
5159 | name_a = object_class_get_name(class_a); |
5160 | name_b = object_class_get_name(class_b); | |
51492fd1 | 5161 | if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) { |
777dc784 | 5162 | return 1; |
51492fd1 | 5163 | } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) { |
777dc784 PM |
5164 | return -1; |
5165 | } else { | |
5166 | return strcmp(name_a, name_b); | |
5adb4839 PB |
5167 | } |
5168 | } | |
5169 | ||
777dc784 | 5170 | static void arm_cpu_list_entry(gpointer data, gpointer user_data) |
40f137e1 | 5171 | { |
777dc784 | 5172 | ObjectClass *oc = data; |
92a31361 | 5173 | CPUListState *s = user_data; |
51492fd1 AF |
5174 | const char *typename; |
5175 | char *name; | |
3371d272 | 5176 | |
51492fd1 AF |
5177 | typename = object_class_get_name(oc); |
5178 | name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU)); | |
777dc784 | 5179 | (*s->cpu_fprintf)(s->file, " %s\n", |
51492fd1 AF |
5180 | name); |
5181 | g_free(name); | |
777dc784 PM |
5182 | } |
5183 | ||
5184 | void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf) | |
5185 | { | |
92a31361 | 5186 | CPUListState s = { |
777dc784 PM |
5187 | .file = f, |
5188 | .cpu_fprintf = cpu_fprintf, | |
5189 | }; | |
5190 | GSList *list; | |
5191 | ||
5192 | list = object_class_get_list(TYPE_ARM_CPU, false); | |
5193 | list = g_slist_sort(list, arm_cpu_list_compare); | |
5194 | (*cpu_fprintf)(f, "Available CPUs:\n"); | |
5195 | g_slist_foreach(list, arm_cpu_list_entry, &s); | |
5196 | g_slist_free(list); | |
a96c0514 PM |
5197 | #ifdef CONFIG_KVM |
5198 | /* The 'host' CPU type is dynamically registered only if KVM is | |
5199 | * enabled, so we have to special-case it here: | |
5200 | */ | |
5201 | (*cpu_fprintf)(f, " host (only available in KVM mode)\n"); | |
5202 | #endif | |
40f137e1 PB |
5203 | } |
5204 | ||
78027bb6 CR |
5205 | static void arm_cpu_add_definition(gpointer data, gpointer user_data) |
5206 | { | |
5207 | ObjectClass *oc = data; | |
5208 | CpuDefinitionInfoList **cpu_list = user_data; | |
5209 | CpuDefinitionInfoList *entry; | |
5210 | CpuDefinitionInfo *info; | |
5211 | const char *typename; | |
5212 | ||
5213 | typename = object_class_get_name(oc); | |
5214 | info = g_malloc0(sizeof(*info)); | |
5215 | info->name = g_strndup(typename, | |
5216 | strlen(typename) - strlen("-" TYPE_ARM_CPU)); | |
5217 | ||
5218 | entry = g_malloc0(sizeof(*entry)); | |
5219 | entry->value = info; | |
5220 | entry->next = *cpu_list; | |
5221 | *cpu_list = entry; | |
5222 | } | |
5223 | ||
5224 | CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp) | |
5225 | { | |
5226 | CpuDefinitionInfoList *cpu_list = NULL; | |
5227 | GSList *list; | |
5228 | ||
5229 | list = object_class_get_list(TYPE_ARM_CPU, false); | |
5230 | g_slist_foreach(list, arm_cpu_add_definition, &cpu_list); | |
5231 | g_slist_free(list); | |
5232 | ||
5233 | return cpu_list; | |
5234 | } | |
5235 | ||
6e6efd61 | 5236 | static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, |
51a79b03 | 5237 | void *opaque, int state, int secstate, |
f5a0a5a5 | 5238 | int crm, int opc1, int opc2) |
6e6efd61 PM |
5239 | { |
5240 | /* Private utility function for define_one_arm_cp_reg_with_opaque(): | |
5241 | * add a single reginfo struct to the hash table. | |
5242 | */ | |
5243 | uint32_t *key = g_new(uint32_t, 1); | |
5244 | ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); | |
5245 | int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; | |
3f3c82a5 FA |
5246 | int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0; |
5247 | ||
5248 | /* Reset the secure state to the specific incoming state. This is | |
5249 | * necessary as the register may have been defined with both states. | |
5250 | */ | |
5251 | r2->secure = secstate; | |
5252 | ||
5253 | if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { | |
5254 | /* Register is banked (using both entries in array). | |
5255 | * Overwriting fieldoffset as the array is only used to define | |
5256 | * banked registers but later only fieldoffset is used. | |
f5a0a5a5 | 5257 | */ |
3f3c82a5 FA |
5258 | r2->fieldoffset = r->bank_fieldoffsets[ns]; |
5259 | } | |
5260 | ||
5261 | if (state == ARM_CP_STATE_AA32) { | |
5262 | if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { | |
5263 | /* If the register is banked then we don't need to migrate or | |
5264 | * reset the 32-bit instance in certain cases: | |
5265 | * | |
5266 | * 1) If the register has both 32-bit and 64-bit instances then we | |
5267 | * can count on the 64-bit instance taking care of the | |
5268 | * non-secure bank. | |
5269 | * 2) If ARMv8 is enabled then we can count on a 64-bit version | |
5270 | * taking care of the secure bank. This requires that separate | |
5271 | * 32 and 64-bit definitions are provided. | |
5272 | */ | |
5273 | if ((r->state == ARM_CP_STATE_BOTH && ns) || | |
5274 | (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) { | |
7a0e58fa | 5275 | r2->type |= ARM_CP_ALIAS; |
3f3c82a5 FA |
5276 | } |
5277 | } else if ((secstate != r->secure) && !ns) { | |
5278 | /* The register is not banked so we only want to allow migration of | |
5279 | * the non-secure instance. | |
5280 | */ | |
7a0e58fa | 5281 | r2->type |= ARM_CP_ALIAS; |
58a1d8ce | 5282 | } |
3f3c82a5 FA |
5283 | |
5284 | if (r->state == ARM_CP_STATE_BOTH) { | |
5285 | /* We assume it is a cp15 register if the .cp field is left unset. | |
5286 | */ | |
5287 | if (r2->cp == 0) { | |
5288 | r2->cp = 15; | |
5289 | } | |
5290 | ||
f5a0a5a5 | 5291 | #ifdef HOST_WORDS_BIGENDIAN |
3f3c82a5 FA |
5292 | if (r2->fieldoffset) { |
5293 | r2->fieldoffset += sizeof(uint32_t); | |
5294 | } | |
f5a0a5a5 | 5295 | #endif |
3f3c82a5 | 5296 | } |
f5a0a5a5 PM |
5297 | } |
5298 | if (state == ARM_CP_STATE_AA64) { | |
5299 | /* To allow abbreviation of ARMCPRegInfo | |
5300 | * definitions, we treat cp == 0 as equivalent to | |
5301 | * the value for "standard guest-visible sysreg". | |
58a1d8ce PM |
5302 | * STATE_BOTH definitions are also always "standard |
5303 | * sysreg" in their AArch64 view (the .cp value may | |
5304 | * be non-zero for the benefit of the AArch32 view). | |
f5a0a5a5 | 5305 | */ |
58a1d8ce | 5306 | if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) { |
f5a0a5a5 PM |
5307 | r2->cp = CP_REG_ARM64_SYSREG_CP; |
5308 | } | |
5309 | *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm, | |
5310 | r2->opc0, opc1, opc2); | |
5311 | } else { | |
51a79b03 | 5312 | *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2); |
f5a0a5a5 | 5313 | } |
6e6efd61 PM |
5314 | if (opaque) { |
5315 | r2->opaque = opaque; | |
5316 | } | |
67ed771d PM |
5317 | /* reginfo passed to helpers is correct for the actual access, |
5318 | * and is never ARM_CP_STATE_BOTH: | |
5319 | */ | |
5320 | r2->state = state; | |
6e6efd61 PM |
5321 | /* Make sure reginfo passed to helpers for wildcarded regs |
5322 | * has the correct crm/opc1/opc2 for this reg, not CP_ANY: | |
5323 | */ | |
5324 | r2->crm = crm; | |
5325 | r2->opc1 = opc1; | |
5326 | r2->opc2 = opc2; | |
5327 | /* By convention, for wildcarded registers only the first | |
5328 | * entry is used for migration; the others are marked as | |
7a0e58fa | 5329 | * ALIAS so we don't try to transfer the register |
6e6efd61 | 5330 | * multiple times. Special registers (ie NOP/WFI) are |
7a0e58fa | 5331 | * never migratable and not even raw-accessible. |
6e6efd61 | 5332 | */ |
7a0e58fa PM |
5333 | if ((r->type & ARM_CP_SPECIAL)) { |
5334 | r2->type |= ARM_CP_NO_RAW; | |
5335 | } | |
5336 | if (((r->crm == CP_ANY) && crm != 0) || | |
6e6efd61 PM |
5337 | ((r->opc1 == CP_ANY) && opc1 != 0) || |
5338 | ((r->opc2 == CP_ANY) && opc2 != 0)) { | |
7a0e58fa | 5339 | r2->type |= ARM_CP_ALIAS; |
6e6efd61 PM |
5340 | } |
5341 | ||
375421cc PM |
5342 | /* Check that raw accesses are either forbidden or handled. Note that |
5343 | * we can't assert this earlier because the setup of fieldoffset for | |
5344 | * banked registers has to be done first. | |
5345 | */ | |
5346 | if (!(r2->type & ARM_CP_NO_RAW)) { | |
5347 | assert(!raw_accessors_invalid(r2)); | |
5348 | } | |
5349 | ||
6e6efd61 PM |
5350 | /* Overriding of an existing definition must be explicitly |
5351 | * requested. | |
5352 | */ | |
5353 | if (!(r->type & ARM_CP_OVERRIDE)) { | |
5354 | ARMCPRegInfo *oldreg; | |
5355 | oldreg = g_hash_table_lookup(cpu->cp_regs, key); | |
5356 | if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { | |
5357 | fprintf(stderr, "Register redefined: cp=%d %d bit " | |
5358 | "crn=%d crm=%d opc1=%d opc2=%d, " | |
5359 | "was %s, now %s\n", r2->cp, 32 + 32 * is64, | |
5360 | r2->crn, r2->crm, r2->opc1, r2->opc2, | |
5361 | oldreg->name, r2->name); | |
5362 | g_assert_not_reached(); | |
5363 | } | |
5364 | } | |
5365 | g_hash_table_insert(cpu->cp_regs, key, r2); | |
5366 | } | |
5367 | ||
5368 | ||
4b6a83fb PM |
5369 | void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, |
5370 | const ARMCPRegInfo *r, void *opaque) | |
5371 | { | |
5372 | /* Define implementations of coprocessor registers. | |
5373 | * We store these in a hashtable because typically | |
5374 | * there are less than 150 registers in a space which | |
5375 | * is 16*16*16*8*8 = 262144 in size. | |
5376 | * Wildcarding is supported for the crm, opc1 and opc2 fields. | |
5377 | * If a register is defined twice then the second definition is | |
5378 | * used, so this can be used to define some generic registers and | |
5379 | * then override them with implementation specific variations. | |
5380 | * At least one of the original and the second definition should | |
5381 | * include ARM_CP_OVERRIDE in its type bits -- this is just a guard | |
5382 | * against accidental use. | |
f5a0a5a5 PM |
5383 | * |
5384 | * The state field defines whether the register is to be | |
5385 | * visible in the AArch32 or AArch64 execution state. If the | |
5386 | * state is set to ARM_CP_STATE_BOTH then we synthesise a | |
5387 | * reginfo structure for the AArch32 view, which sees the lower | |
5388 | * 32 bits of the 64 bit register. | |
5389 | * | |
5390 | * Only registers visible in AArch64 may set r->opc0; opc0 cannot | |
5391 | * be wildcarded. AArch64 registers are always considered to be 64 | |
5392 | * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of | |
5393 | * the register, if any. | |
4b6a83fb | 5394 | */ |
f5a0a5a5 | 5395 | int crm, opc1, opc2, state; |
4b6a83fb PM |
5396 | int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; |
5397 | int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; | |
5398 | int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; | |
5399 | int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; | |
5400 | int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; | |
5401 | int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; | |
5402 | /* 64 bit registers have only CRm and Opc1 fields */ | |
5403 | assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); | |
f5a0a5a5 PM |
5404 | /* op0 only exists in the AArch64 encodings */ |
5405 | assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); | |
5406 | /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ | |
5407 | assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); | |
5408 | /* The AArch64 pseudocode CheckSystemAccess() specifies that op1 | |
5409 | * encodes a minimum access level for the register. We roll this | |
5410 | * runtime check into our general permission check code, so check | |
5411 | * here that the reginfo's specified permissions are strict enough | |
5412 | * to encompass the generic architectural permission check. | |
5413 | */ | |
5414 | if (r->state != ARM_CP_STATE_AA32) { | |
5415 | int mask = 0; | |
5416 | switch (r->opc1) { | |
5417 | case 0: case 1: case 2: | |
5418 | /* min_EL EL1 */ | |
5419 | mask = PL1_RW; | |
5420 | break; | |
5421 | case 3: | |
5422 | /* min_EL EL0 */ | |
5423 | mask = PL0_RW; | |
5424 | break; | |
5425 | case 4: | |
5426 | /* min_EL EL2 */ | |
5427 | mask = PL2_RW; | |
5428 | break; | |
5429 | case 5: | |
5430 | /* unallocated encoding, so not possible */ | |
5431 | assert(false); | |
5432 | break; | |
5433 | case 6: | |
5434 | /* min_EL EL3 */ | |
5435 | mask = PL3_RW; | |
5436 | break; | |
5437 | case 7: | |
5438 | /* min_EL EL1, secure mode only (we don't check the latter) */ | |
5439 | mask = PL1_RW; | |
5440 | break; | |
5441 | default: | |
5442 | /* broken reginfo with out-of-range opc1 */ | |
5443 | assert(false); | |
5444 | break; | |
5445 | } | |
5446 | /* assert our permissions are not too lax (stricter is fine) */ | |
5447 | assert((r->access & ~mask) == 0); | |
5448 | } | |
5449 | ||
4b6a83fb PM |
5450 | /* Check that the register definition has enough info to handle |
5451 | * reads and writes if they are permitted. | |
5452 | */ | |
5453 | if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { | |
5454 | if (r->access & PL3_R) { | |
3f3c82a5 FA |
5455 | assert((r->fieldoffset || |
5456 | (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || | |
5457 | r->readfn); | |
4b6a83fb PM |
5458 | } |
5459 | if (r->access & PL3_W) { | |
3f3c82a5 FA |
5460 | assert((r->fieldoffset || |
5461 | (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || | |
5462 | r->writefn); | |
4b6a83fb PM |
5463 | } |
5464 | } | |
5465 | /* Bad type field probably means missing sentinel at end of reg list */ | |
5466 | assert(cptype_valid(r->type)); | |
5467 | for (crm = crmmin; crm <= crmmax; crm++) { | |
5468 | for (opc1 = opc1min; opc1 <= opc1max; opc1++) { | |
5469 | for (opc2 = opc2min; opc2 <= opc2max; opc2++) { | |
f5a0a5a5 PM |
5470 | for (state = ARM_CP_STATE_AA32; |
5471 | state <= ARM_CP_STATE_AA64; state++) { | |
5472 | if (r->state != state && r->state != ARM_CP_STATE_BOTH) { | |
5473 | continue; | |
5474 | } | |
3f3c82a5 FA |
5475 | if (state == ARM_CP_STATE_AA32) { |
5476 | /* Under AArch32 CP registers can be common | |
5477 | * (same for secure and non-secure world) or banked. | |
5478 | */ | |
5479 | switch (r->secure) { | |
5480 | case ARM_CP_SECSTATE_S: | |
5481 | case ARM_CP_SECSTATE_NS: | |
5482 | add_cpreg_to_hashtable(cpu, r, opaque, state, | |
5483 | r->secure, crm, opc1, opc2); | |
5484 | break; | |
5485 | default: | |
5486 | add_cpreg_to_hashtable(cpu, r, opaque, state, | |
5487 | ARM_CP_SECSTATE_S, | |
5488 | crm, opc1, opc2); | |
5489 | add_cpreg_to_hashtable(cpu, r, opaque, state, | |
5490 | ARM_CP_SECSTATE_NS, | |
5491 | crm, opc1, opc2); | |
5492 | break; | |
5493 | } | |
5494 | } else { | |
5495 | /* AArch64 registers get mapped to non-secure instance | |
5496 | * of AArch32 */ | |
5497 | add_cpreg_to_hashtable(cpu, r, opaque, state, | |
5498 | ARM_CP_SECSTATE_NS, | |
5499 | crm, opc1, opc2); | |
5500 | } | |
f5a0a5a5 | 5501 | } |
4b6a83fb PM |
5502 | } |
5503 | } | |
5504 | } | |
5505 | } | |
5506 | ||
5507 | void define_arm_cp_regs_with_opaque(ARMCPU *cpu, | |
5508 | const ARMCPRegInfo *regs, void *opaque) | |
5509 | { | |
5510 | /* Define a whole list of registers */ | |
5511 | const ARMCPRegInfo *r; | |
5512 | for (r = regs; r->type != ARM_CP_SENTINEL; r++) { | |
5513 | define_one_arm_cp_reg_with_opaque(cpu, r, opaque); | |
5514 | } | |
5515 | } | |
5516 | ||
60322b39 | 5517 | const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) |
4b6a83fb | 5518 | { |
60322b39 | 5519 | return g_hash_table_lookup(cpregs, &encoded_cp); |
4b6a83fb PM |
5520 | } |
5521 | ||
c4241c7d PM |
5522 | void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, |
5523 | uint64_t value) | |
4b6a83fb PM |
5524 | { |
5525 | /* Helper coprocessor write function for write-ignore registers */ | |
4b6a83fb PM |
5526 | } |
5527 | ||
c4241c7d | 5528 | uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) |
4b6a83fb PM |
5529 | { |
5530 | /* Helper coprocessor write function for read-as-zero registers */ | |
4b6a83fb PM |
5531 | return 0; |
5532 | } | |
5533 | ||
f5a0a5a5 PM |
5534 | void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) |
5535 | { | |
5536 | /* Helper coprocessor reset function for do-nothing-on-reset registers */ | |
5537 | } | |
5538 | ||
af393ffc | 5539 | static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type) |
37064a8b PM |
5540 | { |
5541 | /* Return true if it is not valid for us to switch to | |
5542 | * this CPU mode (ie all the UNPREDICTABLE cases in | |
5543 | * the ARM ARM CPSRWriteByInstr pseudocode). | |
5544 | */ | |
af393ffc PM |
5545 | |
5546 | /* Changes to or from Hyp via MSR and CPS are illegal. */ | |
5547 | if (write_type == CPSRWriteByInstr && | |
5548 | ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP || | |
5549 | mode == ARM_CPU_MODE_HYP)) { | |
5550 | return 1; | |
5551 | } | |
5552 | ||
37064a8b PM |
5553 | switch (mode) { |
5554 | case ARM_CPU_MODE_USR: | |
10eacda7 | 5555 | return 0; |
37064a8b PM |
5556 | case ARM_CPU_MODE_SYS: |
5557 | case ARM_CPU_MODE_SVC: | |
5558 | case ARM_CPU_MODE_ABT: | |
5559 | case ARM_CPU_MODE_UND: | |
5560 | case ARM_CPU_MODE_IRQ: | |
5561 | case ARM_CPU_MODE_FIQ: | |
52ff951b PM |
5562 | /* Note that we don't implement the IMPDEF NSACR.RFR which in v7 |
5563 | * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.) | |
5564 | */ | |
10eacda7 PM |
5565 | /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR |
5566 | * and CPS are treated as illegal mode changes. | |
5567 | */ | |
5568 | if (write_type == CPSRWriteByInstr && | |
5569 | (env->cp15.hcr_el2 & HCR_TGE) && | |
5570 | (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON && | |
5571 | !arm_is_secure_below_el3(env)) { | |
5572 | return 1; | |
5573 | } | |
37064a8b | 5574 | return 0; |
e6c8fc07 PM |
5575 | case ARM_CPU_MODE_HYP: |
5576 | return !arm_feature(env, ARM_FEATURE_EL2) | |
5577 | || arm_current_el(env) < 2 || arm_is_secure(env); | |
027fc527 | 5578 | case ARM_CPU_MODE_MON: |
58ae2d1f | 5579 | return arm_current_el(env) < 3; |
37064a8b PM |
5580 | default: |
5581 | return 1; | |
5582 | } | |
5583 | } | |
5584 | ||
2f4a40e5 AZ |
5585 | uint32_t cpsr_read(CPUARMState *env) |
5586 | { | |
5587 | int ZF; | |
6fbe23d5 PB |
5588 | ZF = (env->ZF == 0); |
5589 | return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | | |
2f4a40e5 AZ |
5590 | (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) |
5591 | | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) | |
5592 | | ((env->condexec_bits & 0xfc) << 8) | |
af519934 | 5593 | | (env->GE << 16) | (env->daif & CPSR_AIF); |
2f4a40e5 AZ |
5594 | } |
5595 | ||
50866ba5 PM |
5596 | void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask, |
5597 | CPSRWriteType write_type) | |
2f4a40e5 | 5598 | { |
6e8801f9 FA |
5599 | uint32_t changed_daif; |
5600 | ||
2f4a40e5 | 5601 | if (mask & CPSR_NZCV) { |
6fbe23d5 PB |
5602 | env->ZF = (~val) & CPSR_Z; |
5603 | env->NF = val; | |
2f4a40e5 AZ |
5604 | env->CF = (val >> 29) & 1; |
5605 | env->VF = (val << 3) & 0x80000000; | |
5606 | } | |
5607 | if (mask & CPSR_Q) | |
5608 | env->QF = ((val & CPSR_Q) != 0); | |
5609 | if (mask & CPSR_T) | |
5610 | env->thumb = ((val & CPSR_T) != 0); | |
5611 | if (mask & CPSR_IT_0_1) { | |
5612 | env->condexec_bits &= ~3; | |
5613 | env->condexec_bits |= (val >> 25) & 3; | |
5614 | } | |
5615 | if (mask & CPSR_IT_2_7) { | |
5616 | env->condexec_bits &= 3; | |
5617 | env->condexec_bits |= (val >> 8) & 0xfc; | |
5618 | } | |
5619 | if (mask & CPSR_GE) { | |
5620 | env->GE = (val >> 16) & 0xf; | |
5621 | } | |
5622 | ||
6e8801f9 FA |
5623 | /* In a V7 implementation that includes the security extensions but does |
5624 | * not include Virtualization Extensions the SCR.FW and SCR.AW bits control | |
5625 | * whether non-secure software is allowed to change the CPSR_F and CPSR_A | |
5626 | * bits respectively. | |
5627 | * | |
5628 | * In a V8 implementation, it is permitted for privileged software to | |
5629 | * change the CPSR A/F bits regardless of the SCR.AW/FW bits. | |
5630 | */ | |
f8c88bbc | 5631 | if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) && |
6e8801f9 FA |
5632 | arm_feature(env, ARM_FEATURE_EL3) && |
5633 | !arm_feature(env, ARM_FEATURE_EL2) && | |
5634 | !arm_is_secure(env)) { | |
5635 | ||
5636 | changed_daif = (env->daif ^ val) & mask; | |
5637 | ||
5638 | if (changed_daif & CPSR_A) { | |
5639 | /* Check to see if we are allowed to change the masking of async | |
5640 | * abort exceptions from a non-secure state. | |
5641 | */ | |
5642 | if (!(env->cp15.scr_el3 & SCR_AW)) { | |
5643 | qemu_log_mask(LOG_GUEST_ERROR, | |
5644 | "Ignoring attempt to switch CPSR_A flag from " | |
5645 | "non-secure world with SCR.AW bit clear\n"); | |
5646 | mask &= ~CPSR_A; | |
5647 | } | |
5648 | } | |
5649 | ||
5650 | if (changed_daif & CPSR_F) { | |
5651 | /* Check to see if we are allowed to change the masking of FIQ | |
5652 | * exceptions from a non-secure state. | |
5653 | */ | |
5654 | if (!(env->cp15.scr_el3 & SCR_FW)) { | |
5655 | qemu_log_mask(LOG_GUEST_ERROR, | |
5656 | "Ignoring attempt to switch CPSR_F flag from " | |
5657 | "non-secure world with SCR.FW bit clear\n"); | |
5658 | mask &= ~CPSR_F; | |
5659 | } | |
5660 | ||
5661 | /* Check whether non-maskable FIQ (NMFI) support is enabled. | |
5662 | * If this bit is set software is not allowed to mask | |
5663 | * FIQs, but is allowed to set CPSR_F to 0. | |
5664 | */ | |
5665 | if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && | |
5666 | (val & CPSR_F)) { | |
5667 | qemu_log_mask(LOG_GUEST_ERROR, | |
5668 | "Ignoring attempt to enable CPSR_F flag " | |
5669 | "(non-maskable FIQ [NMFI] support enabled)\n"); | |
5670 | mask &= ~CPSR_F; | |
5671 | } | |
5672 | } | |
5673 | } | |
5674 | ||
4cc35614 PM |
5675 | env->daif &= ~(CPSR_AIF & mask); |
5676 | env->daif |= val & CPSR_AIF & mask; | |
5677 | ||
f8c88bbc PM |
5678 | if (write_type != CPSRWriteRaw && |
5679 | ((env->uncached_cpsr ^ val) & mask & CPSR_M)) { | |
8c4f0eb9 PM |
5680 | if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) { |
5681 | /* Note that we can only get here in USR mode if this is a | |
5682 | * gdb stub write; for this case we follow the architectural | |
5683 | * behaviour for guest writes in USR mode of ignoring an attempt | |
5684 | * to switch mode. (Those are caught by translate.c for writes | |
5685 | * triggered by guest instructions.) | |
5686 | */ | |
5687 | mask &= ~CPSR_M; | |
5688 | } else if (bad_mode_switch(env, val & CPSR_M, write_type)) { | |
81907a58 PM |
5689 | /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in |
5690 | * v7, and has defined behaviour in v8: | |
5691 | * + leave CPSR.M untouched | |
5692 | * + allow changes to the other CPSR fields | |
5693 | * + set PSTATE.IL | |
5694 | * For user changes via the GDB stub, we don't set PSTATE.IL, | |
5695 | * as this would be unnecessarily harsh for a user error. | |
37064a8b PM |
5696 | */ |
5697 | mask &= ~CPSR_M; | |
81907a58 PM |
5698 | if (write_type != CPSRWriteByGDBStub && |
5699 | arm_feature(env, ARM_FEATURE_V8)) { | |
5700 | mask |= CPSR_IL; | |
5701 | val |= CPSR_IL; | |
5702 | } | |
37064a8b PM |
5703 | } else { |
5704 | switch_mode(env, val & CPSR_M); | |
5705 | } | |
2f4a40e5 AZ |
5706 | } |
5707 | mask &= ~CACHED_CPSR_BITS; | |
5708 | env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); | |
5709 | } | |
5710 | ||
b26eefb6 PB |
5711 | /* Sign/zero extend */ |
5712 | uint32_t HELPER(sxtb16)(uint32_t x) | |
5713 | { | |
5714 | uint32_t res; | |
5715 | res = (uint16_t)(int8_t)x; | |
5716 | res |= (uint32_t)(int8_t)(x >> 16) << 16; | |
5717 | return res; | |
5718 | } | |
5719 | ||
5720 | uint32_t HELPER(uxtb16)(uint32_t x) | |
5721 | { | |
5722 | uint32_t res; | |
5723 | res = (uint16_t)(uint8_t)x; | |
5724 | res |= (uint32_t)(uint8_t)(x >> 16) << 16; | |
5725 | return res; | |
5726 | } | |
5727 | ||
3670669c PB |
5728 | int32_t HELPER(sdiv)(int32_t num, int32_t den) |
5729 | { | |
5730 | if (den == 0) | |
5731 | return 0; | |
686eeb93 AJ |
5732 | if (num == INT_MIN && den == -1) |
5733 | return INT_MIN; | |
3670669c PB |
5734 | return num / den; |
5735 | } | |
5736 | ||
5737 | uint32_t HELPER(udiv)(uint32_t num, uint32_t den) | |
5738 | { | |
5739 | if (den == 0) | |
5740 | return 0; | |
5741 | return num / den; | |
5742 | } | |
5743 | ||
5744 | uint32_t HELPER(rbit)(uint32_t x) | |
5745 | { | |
42fedbca | 5746 | return revbit32(x); |
3670669c PB |
5747 | } |
5748 | ||
5fafdf24 | 5749 | #if defined(CONFIG_USER_ONLY) |
b5ff1b31 | 5750 | |
9ee6e8bb | 5751 | /* These should probably raise undefined insn exceptions. */ |
0ecb72a5 | 5752 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) |
9ee6e8bb | 5753 | { |
a47dddd7 AF |
5754 | ARMCPU *cpu = arm_env_get_cpu(env); |
5755 | ||
5756 | cpu_abort(CPU(cpu), "v7m_msr %d\n", reg); | |
9ee6e8bb PB |
5757 | } |
5758 | ||
0ecb72a5 | 5759 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) |
9ee6e8bb | 5760 | { |
a47dddd7 AF |
5761 | ARMCPU *cpu = arm_env_get_cpu(env); |
5762 | ||
5763 | cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg); | |
9ee6e8bb PB |
5764 | return 0; |
5765 | } | |
5766 | ||
0ecb72a5 | 5767 | void switch_mode(CPUARMState *env, int mode) |
b5ff1b31 | 5768 | { |
a47dddd7 AF |
5769 | ARMCPU *cpu = arm_env_get_cpu(env); |
5770 | ||
5771 | if (mode != ARM_CPU_MODE_USR) { | |
5772 | cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); | |
5773 | } | |
b5ff1b31 FB |
5774 | } |
5775 | ||
012a906b GB |
5776 | uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, |
5777 | uint32_t cur_el, bool secure) | |
9e729b57 EI |
5778 | { |
5779 | return 1; | |
5780 | } | |
5781 | ||
ce02049d GB |
5782 | void aarch64_sync_64_to_32(CPUARMState *env) |
5783 | { | |
5784 | g_assert_not_reached(); | |
5785 | } | |
5786 | ||
b5ff1b31 FB |
5787 | #else |
5788 | ||
0ecb72a5 | 5789 | void switch_mode(CPUARMState *env, int mode) |
b5ff1b31 FB |
5790 | { |
5791 | int old_mode; | |
5792 | int i; | |
5793 | ||
5794 | old_mode = env->uncached_cpsr & CPSR_M; | |
5795 | if (mode == old_mode) | |
5796 | return; | |
5797 | ||
5798 | if (old_mode == ARM_CPU_MODE_FIQ) { | |
5799 | memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); | |
8637c67f | 5800 | memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); |
b5ff1b31 FB |
5801 | } else if (mode == ARM_CPU_MODE_FIQ) { |
5802 | memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); | |
8637c67f | 5803 | memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); |
b5ff1b31 FB |
5804 | } |
5805 | ||
f5206413 | 5806 | i = bank_number(old_mode); |
b5ff1b31 FB |
5807 | env->banked_r13[i] = env->regs[13]; |
5808 | env->banked_r14[i] = env->regs[14]; | |
5809 | env->banked_spsr[i] = env->spsr; | |
5810 | ||
f5206413 | 5811 | i = bank_number(mode); |
b5ff1b31 FB |
5812 | env->regs[13] = env->banked_r13[i]; |
5813 | env->regs[14] = env->banked_r14[i]; | |
5814 | env->spsr = env->banked_spsr[i]; | |
5815 | } | |
5816 | ||
0eeb17d6 GB |
5817 | /* Physical Interrupt Target EL Lookup Table |
5818 | * | |
5819 | * [ From ARM ARM section G1.13.4 (Table G1-15) ] | |
5820 | * | |
5821 | * The below multi-dimensional table is used for looking up the target | |
5822 | * exception level given numerous condition criteria. Specifically, the | |
5823 | * target EL is based on SCR and HCR routing controls as well as the | |
5824 | * currently executing EL and secure state. | |
5825 | * | |
5826 | * Dimensions: | |
5827 | * target_el_table[2][2][2][2][2][4] | |
5828 | * | | | | | +--- Current EL | |
5829 | * | | | | +------ Non-secure(0)/Secure(1) | |
5830 | * | | | +--------- HCR mask override | |
5831 | * | | +------------ SCR exec state control | |
5832 | * | +--------------- SCR mask override | |
5833 | * +------------------ 32-bit(0)/64-bit(1) EL3 | |
5834 | * | |
5835 | * The table values are as such: | |
5836 | * 0-3 = EL0-EL3 | |
5837 | * -1 = Cannot occur | |
5838 | * | |
5839 | * The ARM ARM target EL table includes entries indicating that an "exception | |
5840 | * is not taken". The two cases where this is applicable are: | |
5841 | * 1) An exception is taken from EL3 but the SCR does not have the exception | |
5842 | * routed to EL3. | |
5843 | * 2) An exception is taken from EL2 but the HCR does not have the exception | |
5844 | * routed to EL2. | |
5845 | * In these two cases, the below table contain a target of EL1. This value is | |
5846 | * returned as it is expected that the consumer of the table data will check | |
5847 | * for "target EL >= current EL" to ensure the exception is not taken. | |
5848 | * | |
5849 | * SCR HCR | |
5850 | * 64 EA AMO From | |
5851 | * BIT IRQ IMO Non-secure Secure | |
5852 | * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 | |
5853 | */ | |
82c39f6a | 5854 | static const int8_t target_el_table[2][2][2][2][2][4] = { |
0eeb17d6 GB |
5855 | {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, |
5856 | {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, | |
5857 | {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, | |
5858 | {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, | |
5859 | {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, | |
5860 | {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, | |
5861 | {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, | |
5862 | {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, | |
5863 | {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, | |
5864 | {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},}, | |
5865 | {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },}, | |
5866 | {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},}, | |
5867 | {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, | |
5868 | {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, | |
5869 | {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, | |
5870 | {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},}, | |
5871 | }; | |
5872 | ||
5873 | /* | |
5874 | * Determine the target EL for physical exceptions | |
5875 | */ | |
012a906b GB |
5876 | uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, |
5877 | uint32_t cur_el, bool secure) | |
0eeb17d6 GB |
5878 | { |
5879 | CPUARMState *env = cs->env_ptr; | |
2cde031f | 5880 | int rw; |
0eeb17d6 GB |
5881 | int scr; |
5882 | int hcr; | |
5883 | int target_el; | |
2cde031f SS |
5884 | /* Is the highest EL AArch64? */ |
5885 | int is64 = arm_feature(env, ARM_FEATURE_AARCH64); | |
5886 | ||
5887 | if (arm_feature(env, ARM_FEATURE_EL3)) { | |
5888 | rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW); | |
5889 | } else { | |
5890 | /* Either EL2 is the highest EL (and so the EL2 register width | |
5891 | * is given by is64); or there is no EL2 or EL3, in which case | |
5892 | * the value of 'rw' does not affect the table lookup anyway. | |
5893 | */ | |
5894 | rw = is64; | |
5895 | } | |
0eeb17d6 GB |
5896 | |
5897 | switch (excp_idx) { | |
5898 | case EXCP_IRQ: | |
5899 | scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); | |
5900 | hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO); | |
5901 | break; | |
5902 | case EXCP_FIQ: | |
5903 | scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); | |
5904 | hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO); | |
5905 | break; | |
5906 | default: | |
5907 | scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); | |
5908 | hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO); | |
5909 | break; | |
5910 | }; | |
5911 | ||
5912 | /* If HCR.TGE is set then HCR is treated as being 1 */ | |
5913 | hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE); | |
5914 | ||
5915 | /* Perform a table-lookup for the target EL given the current state */ | |
5916 | target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; | |
5917 | ||
5918 | assert(target_el > 0); | |
5919 | ||
5920 | return target_el; | |
5921 | } | |
5922 | ||
9ee6e8bb PB |
5923 | static void v7m_push(CPUARMState *env, uint32_t val) |
5924 | { | |
70d74660 AF |
5925 | CPUState *cs = CPU(arm_env_get_cpu(env)); |
5926 | ||
9ee6e8bb | 5927 | env->regs[13] -= 4; |
ab1da857 | 5928 | stl_phys(cs->as, env->regs[13], val); |
9ee6e8bb PB |
5929 | } |
5930 | ||
5931 | static uint32_t v7m_pop(CPUARMState *env) | |
5932 | { | |
70d74660 | 5933 | CPUState *cs = CPU(arm_env_get_cpu(env)); |
9ee6e8bb | 5934 | uint32_t val; |
70d74660 | 5935 | |
fdfba1a2 | 5936 | val = ldl_phys(cs->as, env->regs[13]); |
9ee6e8bb PB |
5937 | env->regs[13] += 4; |
5938 | return val; | |
5939 | } | |
5940 | ||
5941 | /* Switch to V7M main or process stack pointer. */ | |
5942 | static void switch_v7m_sp(CPUARMState *env, int process) | |
5943 | { | |
5944 | uint32_t tmp; | |
5945 | if (env->v7m.current_sp != process) { | |
5946 | tmp = env->v7m.other_sp; | |
5947 | env->v7m.other_sp = env->regs[13]; | |
5948 | env->regs[13] = tmp; | |
5949 | env->v7m.current_sp = process; | |
5950 | } | |
5951 | } | |
5952 | ||
5953 | static void do_v7m_exception_exit(CPUARMState *env) | |
5954 | { | |
5955 | uint32_t type; | |
5956 | uint32_t xpsr; | |
5957 | ||
5958 | type = env->regs[15]; | |
5959 | if (env->v7m.exception != 0) | |
983fe826 | 5960 | armv7m_nvic_complete_irq(env->nvic, env->v7m.exception); |
9ee6e8bb PB |
5961 | |
5962 | /* Switch to the target stack. */ | |
5963 | switch_v7m_sp(env, (type & 4) != 0); | |
5964 | /* Pop registers. */ | |
5965 | env->regs[0] = v7m_pop(env); | |
5966 | env->regs[1] = v7m_pop(env); | |
5967 | env->regs[2] = v7m_pop(env); | |
5968 | env->regs[3] = v7m_pop(env); | |
5969 | env->regs[12] = v7m_pop(env); | |
5970 | env->regs[14] = v7m_pop(env); | |
5971 | env->regs[15] = v7m_pop(env); | |
fcf83ab1 PM |
5972 | if (env->regs[15] & 1) { |
5973 | qemu_log_mask(LOG_GUEST_ERROR, | |
5974 | "M profile return from interrupt with misaligned " | |
5975 | "PC is UNPREDICTABLE\n"); | |
5976 | /* Actual hardware seems to ignore the lsbit, and there are several | |
5977 | * RTOSes out there which incorrectly assume the r15 in the stack | |
5978 | * frame should be a Thumb-style "lsbit indicates ARM/Thumb" value. | |
5979 | */ | |
5980 | env->regs[15] &= ~1U; | |
5981 | } | |
9ee6e8bb PB |
5982 | xpsr = v7m_pop(env); |
5983 | xpsr_write(env, xpsr, 0xfffffdff); | |
5984 | /* Undo stack alignment. */ | |
5985 | if (xpsr & 0x200) | |
5986 | env->regs[13] |= 4; | |
5987 | /* ??? The exception return type specifies Thread/Handler mode. However | |
5988 | this is also implied by the xPSR value. Not sure what to do | |
5989 | if there is a mismatch. */ | |
5990 | /* ??? Likewise for mismatches between the CONTROL register and the stack | |
5991 | pointer. */ | |
5992 | } | |
5993 | ||
27a7ea8a PB |
5994 | static void arm_log_exception(int idx) |
5995 | { | |
5996 | if (qemu_loglevel_mask(CPU_LOG_INT)) { | |
5997 | const char *exc = NULL; | |
5998 | ||
5999 | if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { | |
6000 | exc = excnames[idx]; | |
6001 | } | |
6002 | if (!exc) { | |
6003 | exc = "unknown"; | |
6004 | } | |
6005 | qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc); | |
6006 | } | |
6007 | } | |
6008 | ||
e6f010cc | 6009 | void arm_v7m_cpu_do_interrupt(CPUState *cs) |
9ee6e8bb | 6010 | { |
e6f010cc AF |
6011 | ARMCPU *cpu = ARM_CPU(cs); |
6012 | CPUARMState *env = &cpu->env; | |
9ee6e8bb PB |
6013 | uint32_t xpsr = xpsr_read(env); |
6014 | uint32_t lr; | |
6015 | uint32_t addr; | |
6016 | ||
27103424 | 6017 | arm_log_exception(cs->exception_index); |
3f1beaca | 6018 | |
9ee6e8bb PB |
6019 | lr = 0xfffffff1; |
6020 | if (env->v7m.current_sp) | |
6021 | lr |= 4; | |
6022 | if (env->v7m.exception == 0) | |
6023 | lr |= 8; | |
6024 | ||
6025 | /* For exceptions we just mark as pending on the NVIC, and let that | |
6026 | handle it. */ | |
6027 | /* TODO: Need to escalate if the current priority is higher than the | |
6028 | one we're raising. */ | |
27103424 | 6029 | switch (cs->exception_index) { |
9ee6e8bb | 6030 | case EXCP_UDEF: |
983fe826 | 6031 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE); |
9ee6e8bb PB |
6032 | return; |
6033 | case EXCP_SWI: | |
314e2296 | 6034 | /* The PC already points to the next instruction. */ |
983fe826 | 6035 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC); |
9ee6e8bb PB |
6036 | return; |
6037 | case EXCP_PREFETCH_ABORT: | |
6038 | case EXCP_DATA_ABORT: | |
abf1172f PM |
6039 | /* TODO: if we implemented the MPU registers, this is where we |
6040 | * should set the MMFAR, etc from exception.fsr and exception.vaddress. | |
6041 | */ | |
983fe826 | 6042 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM); |
9ee6e8bb PB |
6043 | return; |
6044 | case EXCP_BKPT: | |
cfe67cef | 6045 | if (semihosting_enabled()) { |
2ad207d4 | 6046 | int nr; |
f9fd40eb | 6047 | nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff; |
2ad207d4 PB |
6048 | if (nr == 0xab) { |
6049 | env->regs[15] += 2; | |
205ace55 CC |
6050 | qemu_log_mask(CPU_LOG_INT, |
6051 | "...handling as semihosting call 0x%x\n", | |
6052 | env->regs[0]); | |
2ad207d4 PB |
6053 | env->regs[0] = do_arm_semihosting(env); |
6054 | return; | |
6055 | } | |
6056 | } | |
983fe826 | 6057 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG); |
9ee6e8bb PB |
6058 | return; |
6059 | case EXCP_IRQ: | |
983fe826 | 6060 | env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic); |
9ee6e8bb PB |
6061 | break; |
6062 | case EXCP_EXCEPTION_EXIT: | |
6063 | do_v7m_exception_exit(env); | |
6064 | return; | |
6065 | default: | |
a47dddd7 | 6066 | cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); |
9ee6e8bb PB |
6067 | return; /* Never happens. Keep compiler happy. */ |
6068 | } | |
6069 | ||
6070 | /* Align stack pointer. */ | |
6071 | /* ??? Should only do this if Configuration Control Register | |
6072 | STACKALIGN bit is set. */ | |
6073 | if (env->regs[13] & 4) { | |
ab19b0ec | 6074 | env->regs[13] -= 4; |
9ee6e8bb PB |
6075 | xpsr |= 0x200; |
6076 | } | |
6c95676b | 6077 | /* Switch to the handler mode. */ |
9ee6e8bb PB |
6078 | v7m_push(env, xpsr); |
6079 | v7m_push(env, env->regs[15]); | |
6080 | v7m_push(env, env->regs[14]); | |
6081 | v7m_push(env, env->regs[12]); | |
6082 | v7m_push(env, env->regs[3]); | |
6083 | v7m_push(env, env->regs[2]); | |
6084 | v7m_push(env, env->regs[1]); | |
6085 | v7m_push(env, env->regs[0]); | |
6086 | switch_v7m_sp(env, 0); | |
c98d174c PM |
6087 | /* Clear IT bits */ |
6088 | env->condexec_bits = 0; | |
9ee6e8bb | 6089 | env->regs[14] = lr; |
fdfba1a2 | 6090 | addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4); |
9ee6e8bb PB |
6091 | env->regs[15] = addr & 0xfffffffe; |
6092 | env->thumb = addr & 1; | |
6093 | } | |
6094 | ||
ce02049d GB |
6095 | /* Function used to synchronize QEMU's AArch64 register set with AArch32 |
6096 | * register set. This is necessary when switching between AArch32 and AArch64 | |
6097 | * execution state. | |
6098 | */ | |
6099 | void aarch64_sync_32_to_64(CPUARMState *env) | |
6100 | { | |
6101 | int i; | |
6102 | uint32_t mode = env->uncached_cpsr & CPSR_M; | |
6103 | ||
6104 | /* We can blanket copy R[0:7] to X[0:7] */ | |
6105 | for (i = 0; i < 8; i++) { | |
6106 | env->xregs[i] = env->regs[i]; | |
6107 | } | |
6108 | ||
6109 | /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12. | |
6110 | * Otherwise, they come from the banked user regs. | |
6111 | */ | |
6112 | if (mode == ARM_CPU_MODE_FIQ) { | |
6113 | for (i = 8; i < 13; i++) { | |
6114 | env->xregs[i] = env->usr_regs[i - 8]; | |
6115 | } | |
6116 | } else { | |
6117 | for (i = 8; i < 13; i++) { | |
6118 | env->xregs[i] = env->regs[i]; | |
6119 | } | |
6120 | } | |
6121 | ||
6122 | /* Registers x13-x23 are the various mode SP and FP registers. Registers | |
6123 | * r13 and r14 are only copied if we are in that mode, otherwise we copy | |
6124 | * from the mode banked register. | |
6125 | */ | |
6126 | if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { | |
6127 | env->xregs[13] = env->regs[13]; | |
6128 | env->xregs[14] = env->regs[14]; | |
6129 | } else { | |
6130 | env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)]; | |
6131 | /* HYP is an exception in that it is copied from r14 */ | |
6132 | if (mode == ARM_CPU_MODE_HYP) { | |
6133 | env->xregs[14] = env->regs[14]; | |
6134 | } else { | |
6135 | env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)]; | |
6136 | } | |
6137 | } | |
6138 | ||
6139 | if (mode == ARM_CPU_MODE_HYP) { | |
6140 | env->xregs[15] = env->regs[13]; | |
6141 | } else { | |
6142 | env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)]; | |
6143 | } | |
6144 | ||
6145 | if (mode == ARM_CPU_MODE_IRQ) { | |
3a9148d0 SS |
6146 | env->xregs[16] = env->regs[14]; |
6147 | env->xregs[17] = env->regs[13]; | |
ce02049d | 6148 | } else { |
3a9148d0 SS |
6149 | env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)]; |
6150 | env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)]; | |
ce02049d GB |
6151 | } |
6152 | ||
6153 | if (mode == ARM_CPU_MODE_SVC) { | |
3a9148d0 SS |
6154 | env->xregs[18] = env->regs[14]; |
6155 | env->xregs[19] = env->regs[13]; | |
ce02049d | 6156 | } else { |
3a9148d0 SS |
6157 | env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)]; |
6158 | env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)]; | |
ce02049d GB |
6159 | } |
6160 | ||
6161 | if (mode == ARM_CPU_MODE_ABT) { | |
3a9148d0 SS |
6162 | env->xregs[20] = env->regs[14]; |
6163 | env->xregs[21] = env->regs[13]; | |
ce02049d | 6164 | } else { |
3a9148d0 SS |
6165 | env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)]; |
6166 | env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)]; | |
ce02049d GB |
6167 | } |
6168 | ||
6169 | if (mode == ARM_CPU_MODE_UND) { | |
3a9148d0 SS |
6170 | env->xregs[22] = env->regs[14]; |
6171 | env->xregs[23] = env->regs[13]; | |
ce02049d | 6172 | } else { |
3a9148d0 SS |
6173 | env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)]; |
6174 | env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)]; | |
ce02049d GB |
6175 | } |
6176 | ||
6177 | /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ | |
6178 | * mode, then we can copy from r8-r14. Otherwise, we copy from the | |
6179 | * FIQ bank for r8-r14. | |
6180 | */ | |
6181 | if (mode == ARM_CPU_MODE_FIQ) { | |
6182 | for (i = 24; i < 31; i++) { | |
6183 | env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */ | |
6184 | } | |
6185 | } else { | |
6186 | for (i = 24; i < 29; i++) { | |
6187 | env->xregs[i] = env->fiq_regs[i - 24]; | |
6188 | } | |
6189 | env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)]; | |
6190 | env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)]; | |
6191 | } | |
6192 | ||
6193 | env->pc = env->regs[15]; | |
6194 | } | |
6195 | ||
6196 | /* Function used to synchronize QEMU's AArch32 register set with AArch64 | |
6197 | * register set. This is necessary when switching between AArch32 and AArch64 | |
6198 | * execution state. | |
6199 | */ | |
6200 | void aarch64_sync_64_to_32(CPUARMState *env) | |
6201 | { | |
6202 | int i; | |
6203 | uint32_t mode = env->uncached_cpsr & CPSR_M; | |
6204 | ||
6205 | /* We can blanket copy X[0:7] to R[0:7] */ | |
6206 | for (i = 0; i < 8; i++) { | |
6207 | env->regs[i] = env->xregs[i]; | |
6208 | } | |
6209 | ||
6210 | /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12. | |
6211 | * Otherwise, we copy x8-x12 into the banked user regs. | |
6212 | */ | |
6213 | if (mode == ARM_CPU_MODE_FIQ) { | |
6214 | for (i = 8; i < 13; i++) { | |
6215 | env->usr_regs[i - 8] = env->xregs[i]; | |
6216 | } | |
6217 | } else { | |
6218 | for (i = 8; i < 13; i++) { | |
6219 | env->regs[i] = env->xregs[i]; | |
6220 | } | |
6221 | } | |
6222 | ||
6223 | /* Registers r13 & r14 depend on the current mode. | |
6224 | * If we are in a given mode, we copy the corresponding x registers to r13 | |
6225 | * and r14. Otherwise, we copy the x register to the banked r13 and r14 | |
6226 | * for the mode. | |
6227 | */ | |
6228 | if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { | |
6229 | env->regs[13] = env->xregs[13]; | |
6230 | env->regs[14] = env->xregs[14]; | |
6231 | } else { | |
6232 | env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13]; | |
6233 | ||
6234 | /* HYP is an exception in that it does not have its own banked r14 but | |
6235 | * shares the USR r14 | |
6236 | */ | |
6237 | if (mode == ARM_CPU_MODE_HYP) { | |
6238 | env->regs[14] = env->xregs[14]; | |
6239 | } else { | |
6240 | env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14]; | |
6241 | } | |
6242 | } | |
6243 | ||
6244 | if (mode == ARM_CPU_MODE_HYP) { | |
6245 | env->regs[13] = env->xregs[15]; | |
6246 | } else { | |
6247 | env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15]; | |
6248 | } | |
6249 | ||
6250 | if (mode == ARM_CPU_MODE_IRQ) { | |
3a9148d0 SS |
6251 | env->regs[14] = env->xregs[16]; |
6252 | env->regs[13] = env->xregs[17]; | |
ce02049d | 6253 | } else { |
3a9148d0 SS |
6254 | env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16]; |
6255 | env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17]; | |
ce02049d GB |
6256 | } |
6257 | ||
6258 | if (mode == ARM_CPU_MODE_SVC) { | |
3a9148d0 SS |
6259 | env->regs[14] = env->xregs[18]; |
6260 | env->regs[13] = env->xregs[19]; | |
ce02049d | 6261 | } else { |
3a9148d0 SS |
6262 | env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18]; |
6263 | env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19]; | |
ce02049d GB |
6264 | } |
6265 | ||
6266 | if (mode == ARM_CPU_MODE_ABT) { | |
3a9148d0 SS |
6267 | env->regs[14] = env->xregs[20]; |
6268 | env->regs[13] = env->xregs[21]; | |
ce02049d | 6269 | } else { |
3a9148d0 SS |
6270 | env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20]; |
6271 | env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21]; | |
ce02049d GB |
6272 | } |
6273 | ||
6274 | if (mode == ARM_CPU_MODE_UND) { | |
3a9148d0 SS |
6275 | env->regs[14] = env->xregs[22]; |
6276 | env->regs[13] = env->xregs[23]; | |
ce02049d | 6277 | } else { |
3a9148d0 SS |
6278 | env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22]; |
6279 | env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23]; | |
ce02049d GB |
6280 | } |
6281 | ||
6282 | /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ | |
6283 | * mode, then we can copy to r8-r14. Otherwise, we copy to the | |
6284 | * FIQ bank for r8-r14. | |
6285 | */ | |
6286 | if (mode == ARM_CPU_MODE_FIQ) { | |
6287 | for (i = 24; i < 31; i++) { | |
6288 | env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */ | |
6289 | } | |
6290 | } else { | |
6291 | for (i = 24; i < 29; i++) { | |
6292 | env->fiq_regs[i - 24] = env->xregs[i]; | |
6293 | } | |
6294 | env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29]; | |
6295 | env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30]; | |
6296 | } | |
6297 | ||
6298 | env->regs[15] = env->pc; | |
6299 | } | |
6300 | ||
966f758c | 6301 | static void arm_cpu_do_interrupt_aarch32(CPUState *cs) |
b5ff1b31 | 6302 | { |
97a8ea5a AF |
6303 | ARMCPU *cpu = ARM_CPU(cs); |
6304 | CPUARMState *env = &cpu->env; | |
b5ff1b31 FB |
6305 | uint32_t addr; |
6306 | uint32_t mask; | |
6307 | int new_mode; | |
6308 | uint32_t offset; | |
16a906fd | 6309 | uint32_t moe; |
b5ff1b31 | 6310 | |
16a906fd PM |
6311 | /* If this is a debug exception we must update the DBGDSCR.MOE bits */ |
6312 | switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) { | |
6313 | case EC_BREAKPOINT: | |
6314 | case EC_BREAKPOINT_SAME_EL: | |
6315 | moe = 1; | |
6316 | break; | |
6317 | case EC_WATCHPOINT: | |
6318 | case EC_WATCHPOINT_SAME_EL: | |
6319 | moe = 10; | |
6320 | break; | |
6321 | case EC_AA32_BKPT: | |
6322 | moe = 3; | |
6323 | break; | |
6324 | case EC_VECTORCATCH: | |
6325 | moe = 5; | |
6326 | break; | |
6327 | default: | |
6328 | moe = 0; | |
6329 | break; | |
6330 | } | |
6331 | ||
6332 | if (moe) { | |
6333 | env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); | |
6334 | } | |
6335 | ||
b5ff1b31 | 6336 | /* TODO: Vectored interrupt controller. */ |
27103424 | 6337 | switch (cs->exception_index) { |
b5ff1b31 FB |
6338 | case EXCP_UDEF: |
6339 | new_mode = ARM_CPU_MODE_UND; | |
6340 | addr = 0x04; | |
6341 | mask = CPSR_I; | |
6342 | if (env->thumb) | |
6343 | offset = 2; | |
6344 | else | |
6345 | offset = 4; | |
6346 | break; | |
6347 | case EXCP_SWI: | |
6348 | new_mode = ARM_CPU_MODE_SVC; | |
6349 | addr = 0x08; | |
6350 | mask = CPSR_I; | |
601d70b9 | 6351 | /* The PC already points to the next instruction. */ |
b5ff1b31 FB |
6352 | offset = 0; |
6353 | break; | |
06c949e6 | 6354 | case EXCP_BKPT: |
abf1172f | 6355 | env->exception.fsr = 2; |
9ee6e8bb PB |
6356 | /* Fall through to prefetch abort. */ |
6357 | case EXCP_PREFETCH_ABORT: | |
88ca1c2d | 6358 | A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); |
b848ce2b | 6359 | A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); |
3f1beaca | 6360 | qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", |
88ca1c2d | 6361 | env->exception.fsr, (uint32_t)env->exception.vaddress); |
b5ff1b31 FB |
6362 | new_mode = ARM_CPU_MODE_ABT; |
6363 | addr = 0x0c; | |
6364 | mask = CPSR_A | CPSR_I; | |
6365 | offset = 4; | |
6366 | break; | |
6367 | case EXCP_DATA_ABORT: | |
4a7e2d73 | 6368 | A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); |
b848ce2b | 6369 | A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); |
3f1beaca | 6370 | qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", |
4a7e2d73 | 6371 | env->exception.fsr, |
6cd8a264 | 6372 | (uint32_t)env->exception.vaddress); |
b5ff1b31 FB |
6373 | new_mode = ARM_CPU_MODE_ABT; |
6374 | addr = 0x10; | |
6375 | mask = CPSR_A | CPSR_I; | |
6376 | offset = 8; | |
6377 | break; | |
6378 | case EXCP_IRQ: | |
6379 | new_mode = ARM_CPU_MODE_IRQ; | |
6380 | addr = 0x18; | |
6381 | /* Disable IRQ and imprecise data aborts. */ | |
6382 | mask = CPSR_A | CPSR_I; | |
6383 | offset = 4; | |
de38d23b FA |
6384 | if (env->cp15.scr_el3 & SCR_IRQ) { |
6385 | /* IRQ routed to monitor mode */ | |
6386 | new_mode = ARM_CPU_MODE_MON; | |
6387 | mask |= CPSR_F; | |
6388 | } | |
b5ff1b31 FB |
6389 | break; |
6390 | case EXCP_FIQ: | |
6391 | new_mode = ARM_CPU_MODE_FIQ; | |
6392 | addr = 0x1c; | |
6393 | /* Disable FIQ, IRQ and imprecise data aborts. */ | |
6394 | mask = CPSR_A | CPSR_I | CPSR_F; | |
de38d23b FA |
6395 | if (env->cp15.scr_el3 & SCR_FIQ) { |
6396 | /* FIQ routed to monitor mode */ | |
6397 | new_mode = ARM_CPU_MODE_MON; | |
6398 | } | |
b5ff1b31 FB |
6399 | offset = 4; |
6400 | break; | |
dbe9d163 FA |
6401 | case EXCP_SMC: |
6402 | new_mode = ARM_CPU_MODE_MON; | |
6403 | addr = 0x08; | |
6404 | mask = CPSR_A | CPSR_I | CPSR_F; | |
6405 | offset = 0; | |
6406 | break; | |
b5ff1b31 | 6407 | default: |
a47dddd7 | 6408 | cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); |
b5ff1b31 FB |
6409 | return; /* Never happens. Keep compiler happy. */ |
6410 | } | |
e89e51a1 FA |
6411 | |
6412 | if (new_mode == ARM_CPU_MODE_MON) { | |
6413 | addr += env->cp15.mvbar; | |
137feaa9 | 6414 | } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { |
e89e51a1 | 6415 | /* High vectors. When enabled, base address cannot be remapped. */ |
b5ff1b31 | 6416 | addr += 0xffff0000; |
8641136c NR |
6417 | } else { |
6418 | /* ARM v7 architectures provide a vector base address register to remap | |
6419 | * the interrupt vector table. | |
e89e51a1 | 6420 | * This register is only followed in non-monitor mode, and is banked. |
8641136c NR |
6421 | * Note: only bits 31:5 are valid. |
6422 | */ | |
fb6c91ba | 6423 | addr += A32_BANKED_CURRENT_REG_GET(env, vbar); |
b5ff1b31 | 6424 | } |
dbe9d163 FA |
6425 | |
6426 | if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { | |
6427 | env->cp15.scr_el3 &= ~SCR_NS; | |
6428 | } | |
6429 | ||
b5ff1b31 | 6430 | switch_mode (env, new_mode); |
662cefb7 PM |
6431 | /* For exceptions taken to AArch32 we must clear the SS bit in both |
6432 | * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. | |
6433 | */ | |
6434 | env->uncached_cpsr &= ~PSTATE_SS; | |
b5ff1b31 | 6435 | env->spsr = cpsr_read(env); |
9ee6e8bb PB |
6436 | /* Clear IT bits. */ |
6437 | env->condexec_bits = 0; | |
30a8cac1 | 6438 | /* Switch to the new mode, and to the correct instruction set. */ |
6d7e6326 | 6439 | env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; |
73462ddd PC |
6440 | /* Set new mode endianness */ |
6441 | env->uncached_cpsr &= ~CPSR_E; | |
6442 | if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) { | |
3823b9db | 6443 | env->uncached_cpsr |= CPSR_E; |
73462ddd | 6444 | } |
4cc35614 | 6445 | env->daif |= mask; |
be5e7a76 DES |
6446 | /* this is a lie, as the was no c1_sys on V4T/V5, but who cares |
6447 | * and we should just guard the thumb mode on V4 */ | |
6448 | if (arm_feature(env, ARM_FEATURE_V4T)) { | |
137feaa9 | 6449 | env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; |
be5e7a76 | 6450 | } |
b5ff1b31 FB |
6451 | env->regs[14] = env->regs[15] + offset; |
6452 | env->regs[15] = addr; | |
b5ff1b31 FB |
6453 | } |
6454 | ||
966f758c PM |
6455 | /* Handle exception entry to a target EL which is using AArch64 */ |
6456 | static void arm_cpu_do_interrupt_aarch64(CPUState *cs) | |
f3a9b694 PM |
6457 | { |
6458 | ARMCPU *cpu = ARM_CPU(cs); | |
6459 | CPUARMState *env = &cpu->env; | |
6460 | unsigned int new_el = env->exception.target_el; | |
6461 | target_ulong addr = env->cp15.vbar_el[new_el]; | |
6462 | unsigned int new_mode = aarch64_pstate_mode(new_el, true); | |
6463 | ||
6464 | if (arm_current_el(env) < new_el) { | |
3d6f7617 PM |
6465 | /* Entry vector offset depends on whether the implemented EL |
6466 | * immediately lower than the target level is using AArch32 or AArch64 | |
6467 | */ | |
6468 | bool is_aa64; | |
6469 | ||
6470 | switch (new_el) { | |
6471 | case 3: | |
6472 | is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0; | |
6473 | break; | |
6474 | case 2: | |
6475 | is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0; | |
6476 | break; | |
6477 | case 1: | |
6478 | is_aa64 = is_a64(env); | |
6479 | break; | |
6480 | default: | |
6481 | g_assert_not_reached(); | |
6482 | } | |
6483 | ||
6484 | if (is_aa64) { | |
f3a9b694 PM |
6485 | addr += 0x400; |
6486 | } else { | |
6487 | addr += 0x600; | |
6488 | } | |
6489 | } else if (pstate_read(env) & PSTATE_SP) { | |
6490 | addr += 0x200; | |
6491 | } | |
6492 | ||
f3a9b694 PM |
6493 | switch (cs->exception_index) { |
6494 | case EXCP_PREFETCH_ABORT: | |
6495 | case EXCP_DATA_ABORT: | |
6496 | env->cp15.far_el[new_el] = env->exception.vaddress; | |
6497 | qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n", | |
6498 | env->cp15.far_el[new_el]); | |
6499 | /* fall through */ | |
6500 | case EXCP_BKPT: | |
6501 | case EXCP_UDEF: | |
6502 | case EXCP_SWI: | |
6503 | case EXCP_HVC: | |
6504 | case EXCP_HYP_TRAP: | |
6505 | case EXCP_SMC: | |
6506 | env->cp15.esr_el[new_el] = env->exception.syndrome; | |
6507 | break; | |
6508 | case EXCP_IRQ: | |
6509 | case EXCP_VIRQ: | |
6510 | addr += 0x80; | |
6511 | break; | |
6512 | case EXCP_FIQ: | |
6513 | case EXCP_VFIQ: | |
6514 | addr += 0x100; | |
6515 | break; | |
6516 | case EXCP_SEMIHOST: | |
6517 | qemu_log_mask(CPU_LOG_INT, | |
6518 | "...handling as semihosting call 0x%" PRIx64 "\n", | |
6519 | env->xregs[0]); | |
6520 | env->xregs[0] = do_arm_semihosting(env); | |
6521 | return; | |
6522 | default: | |
6523 | cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); | |
6524 | } | |
6525 | ||
6526 | if (is_a64(env)) { | |
6527 | env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env); | |
6528 | aarch64_save_sp(env, arm_current_el(env)); | |
6529 | env->elr_el[new_el] = env->pc; | |
6530 | } else { | |
6531 | env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env); | |
f3a9b694 PM |
6532 | env->elr_el[new_el] = env->regs[15]; |
6533 | ||
6534 | aarch64_sync_32_to_64(env); | |
6535 | ||
6536 | env->condexec_bits = 0; | |
6537 | } | |
6538 | qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n", | |
6539 | env->elr_el[new_el]); | |
6540 | ||
6541 | pstate_write(env, PSTATE_DAIF | new_mode); | |
6542 | env->aarch64 = 1; | |
6543 | aarch64_restore_sp(env, new_el); | |
6544 | ||
6545 | env->pc = addr; | |
6546 | ||
6547 | qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n", | |
6548 | new_el, env->pc, pstate_read(env)); | |
966f758c PM |
6549 | } |
6550 | ||
904c04de PM |
6551 | static inline bool check_for_semihosting(CPUState *cs) |
6552 | { | |
6553 | /* Check whether this exception is a semihosting call; if so | |
6554 | * then handle it and return true; otherwise return false. | |
6555 | */ | |
6556 | ARMCPU *cpu = ARM_CPU(cs); | |
6557 | CPUARMState *env = &cpu->env; | |
6558 | ||
6559 | if (is_a64(env)) { | |
6560 | if (cs->exception_index == EXCP_SEMIHOST) { | |
6561 | /* This is always the 64-bit semihosting exception. | |
6562 | * The "is this usermode" and "is semihosting enabled" | |
6563 | * checks have been done at translate time. | |
6564 | */ | |
6565 | qemu_log_mask(CPU_LOG_INT, | |
6566 | "...handling as semihosting call 0x%" PRIx64 "\n", | |
6567 | env->xregs[0]); | |
6568 | env->xregs[0] = do_arm_semihosting(env); | |
6569 | return true; | |
6570 | } | |
6571 | return false; | |
6572 | } else { | |
6573 | uint32_t imm; | |
6574 | ||
6575 | /* Only intercept calls from privileged modes, to provide some | |
6576 | * semblance of security. | |
6577 | */ | |
19a6e31c PM |
6578 | if (cs->exception_index != EXCP_SEMIHOST && |
6579 | (!semihosting_enabled() || | |
6580 | ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) { | |
904c04de PM |
6581 | return false; |
6582 | } | |
6583 | ||
6584 | switch (cs->exception_index) { | |
19a6e31c PM |
6585 | case EXCP_SEMIHOST: |
6586 | /* This is always a semihosting call; the "is this usermode" | |
6587 | * and "is semihosting enabled" checks have been done at | |
6588 | * translate time. | |
6589 | */ | |
6590 | break; | |
904c04de PM |
6591 | case EXCP_SWI: |
6592 | /* Check for semihosting interrupt. */ | |
6593 | if (env->thumb) { | |
f9fd40eb | 6594 | imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env)) |
904c04de PM |
6595 | & 0xff; |
6596 | if (imm == 0xab) { | |
6597 | break; | |
6598 | } | |
6599 | } else { | |
f9fd40eb | 6600 | imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env)) |
904c04de PM |
6601 | & 0xffffff; |
6602 | if (imm == 0x123456) { | |
6603 | break; | |
6604 | } | |
6605 | } | |
6606 | return false; | |
6607 | case EXCP_BKPT: | |
6608 | /* See if this is a semihosting syscall. */ | |
6609 | if (env->thumb) { | |
f9fd40eb | 6610 | imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) |
904c04de PM |
6611 | & 0xff; |
6612 | if (imm == 0xab) { | |
6613 | env->regs[15] += 2; | |
6614 | break; | |
6615 | } | |
6616 | } | |
6617 | return false; | |
6618 | default: | |
6619 | return false; | |
6620 | } | |
6621 | ||
6622 | qemu_log_mask(CPU_LOG_INT, | |
6623 | "...handling as semihosting call 0x%x\n", | |
6624 | env->regs[0]); | |
6625 | env->regs[0] = do_arm_semihosting(env); | |
6626 | return true; | |
6627 | } | |
6628 | } | |
6629 | ||
966f758c PM |
6630 | /* Handle a CPU exception for A and R profile CPUs. |
6631 | * Do any appropriate logging, handle PSCI calls, and then hand off | |
6632 | * to the AArch64-entry or AArch32-entry function depending on the | |
6633 | * target exception level's register width. | |
6634 | */ | |
6635 | void arm_cpu_do_interrupt(CPUState *cs) | |
6636 | { | |
6637 | ARMCPU *cpu = ARM_CPU(cs); | |
6638 | CPUARMState *env = &cpu->env; | |
6639 | unsigned int new_el = env->exception.target_el; | |
6640 | ||
6641 | assert(!IS_M(env)); | |
6642 | ||
6643 | arm_log_exception(cs->exception_index); | |
6644 | qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env), | |
6645 | new_el); | |
6646 | if (qemu_loglevel_mask(CPU_LOG_INT) | |
6647 | && !excp_is_internal(cs->exception_index)) { | |
6648 | qemu_log_mask(CPU_LOG_INT, "...with ESR %x/0x%" PRIx32 "\n", | |
6649 | env->exception.syndrome >> ARM_EL_EC_SHIFT, | |
6650 | env->exception.syndrome); | |
6651 | } | |
6652 | ||
6653 | if (arm_is_psci_call(cpu, cs->exception_index)) { | |
6654 | arm_handle_psci_call(cpu); | |
6655 | qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); | |
6656 | return; | |
6657 | } | |
6658 | ||
904c04de PM |
6659 | /* Semihosting semantics depend on the register width of the |
6660 | * code that caused the exception, not the target exception level, | |
6661 | * so must be handled here. | |
966f758c | 6662 | */ |
904c04de PM |
6663 | if (check_for_semihosting(cs)) { |
6664 | return; | |
6665 | } | |
6666 | ||
6667 | assert(!excp_is_internal(cs->exception_index)); | |
6668 | if (arm_el_is_aa64(env, new_el)) { | |
966f758c PM |
6669 | arm_cpu_do_interrupt_aarch64(cs); |
6670 | } else { | |
6671 | arm_cpu_do_interrupt_aarch32(cs); | |
6672 | } | |
f3a9b694 | 6673 | |
bd7d00fc PM |
6674 | arm_call_el_change_hook(cpu); |
6675 | ||
f3a9b694 PM |
6676 | if (!kvm_enabled()) { |
6677 | cs->interrupt_request |= CPU_INTERRUPT_EXITTB; | |
6678 | } | |
6679 | } | |
0480f69a PM |
6680 | |
6681 | /* Return the exception level which controls this address translation regime */ | |
6682 | static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx) | |
6683 | { | |
6684 | switch (mmu_idx) { | |
6685 | case ARMMMUIdx_S2NS: | |
6686 | case ARMMMUIdx_S1E2: | |
6687 | return 2; | |
6688 | case ARMMMUIdx_S1E3: | |
6689 | return 3; | |
6690 | case ARMMMUIdx_S1SE0: | |
6691 | return arm_el_is_aa64(env, 3) ? 1 : 3; | |
6692 | case ARMMMUIdx_S1SE1: | |
6693 | case ARMMMUIdx_S1NSE0: | |
6694 | case ARMMMUIdx_S1NSE1: | |
6695 | return 1; | |
6696 | default: | |
6697 | g_assert_not_reached(); | |
6698 | } | |
6699 | } | |
6700 | ||
8bf5b6a9 PM |
6701 | /* Return true if this address translation regime is secure */ |
6702 | static inline bool regime_is_secure(CPUARMState *env, ARMMMUIdx mmu_idx) | |
6703 | { | |
6704 | switch (mmu_idx) { | |
6705 | case ARMMMUIdx_S12NSE0: | |
6706 | case ARMMMUIdx_S12NSE1: | |
6707 | case ARMMMUIdx_S1NSE0: | |
6708 | case ARMMMUIdx_S1NSE1: | |
6709 | case ARMMMUIdx_S1E2: | |
6710 | case ARMMMUIdx_S2NS: | |
6711 | return false; | |
6712 | case ARMMMUIdx_S1E3: | |
6713 | case ARMMMUIdx_S1SE0: | |
6714 | case ARMMMUIdx_S1SE1: | |
6715 | return true; | |
6716 | default: | |
6717 | g_assert_not_reached(); | |
6718 | } | |
6719 | } | |
6720 | ||
0480f69a PM |
6721 | /* Return the SCTLR value which controls this address translation regime */ |
6722 | static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx) | |
6723 | { | |
6724 | return env->cp15.sctlr_el[regime_el(env, mmu_idx)]; | |
6725 | } | |
6726 | ||
6727 | /* Return true if the specified stage of address translation is disabled */ | |
6728 | static inline bool regime_translation_disabled(CPUARMState *env, | |
6729 | ARMMMUIdx mmu_idx) | |
6730 | { | |
6731 | if (mmu_idx == ARMMMUIdx_S2NS) { | |
6732 | return (env->cp15.hcr_el2 & HCR_VM) == 0; | |
6733 | } | |
6734 | return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0; | |
6735 | } | |
6736 | ||
73462ddd PC |
6737 | static inline bool regime_translation_big_endian(CPUARMState *env, |
6738 | ARMMMUIdx mmu_idx) | |
6739 | { | |
6740 | return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0; | |
6741 | } | |
6742 | ||
0480f69a PM |
6743 | /* Return the TCR controlling this translation regime */ |
6744 | static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx) | |
6745 | { | |
6746 | if (mmu_idx == ARMMMUIdx_S2NS) { | |
68e9c2fe | 6747 | return &env->cp15.vtcr_el2; |
0480f69a PM |
6748 | } |
6749 | return &env->cp15.tcr_el[regime_el(env, mmu_idx)]; | |
6750 | } | |
6751 | ||
86fb3fa4 TH |
6752 | /* Returns TBI0 value for current regime el */ |
6753 | uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx) | |
6754 | { | |
6755 | TCR *tcr; | |
6756 | uint32_t el; | |
6757 | ||
6758 | /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert | |
6759 | * a stage 1+2 mmu index into the appropriate stage 1 mmu index. | |
6760 | */ | |
6761 | if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { | |
6762 | mmu_idx += ARMMMUIdx_S1NSE0; | |
6763 | } | |
6764 | ||
6765 | tcr = regime_tcr(env, mmu_idx); | |
6766 | el = regime_el(env, mmu_idx); | |
6767 | ||
6768 | if (el > 1) { | |
6769 | return extract64(tcr->raw_tcr, 20, 1); | |
6770 | } else { | |
6771 | return extract64(tcr->raw_tcr, 37, 1); | |
6772 | } | |
6773 | } | |
6774 | ||
6775 | /* Returns TBI1 value for current regime el */ | |
6776 | uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx) | |
6777 | { | |
6778 | TCR *tcr; | |
6779 | uint32_t el; | |
6780 | ||
6781 | /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert | |
6782 | * a stage 1+2 mmu index into the appropriate stage 1 mmu index. | |
6783 | */ | |
6784 | if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { | |
6785 | mmu_idx += ARMMMUIdx_S1NSE0; | |
6786 | } | |
6787 | ||
6788 | tcr = regime_tcr(env, mmu_idx); | |
6789 | el = regime_el(env, mmu_idx); | |
6790 | ||
6791 | if (el > 1) { | |
6792 | return 0; | |
6793 | } else { | |
6794 | return extract64(tcr->raw_tcr, 38, 1); | |
6795 | } | |
6796 | } | |
6797 | ||
aef878be GB |
6798 | /* Return the TTBR associated with this translation regime */ |
6799 | static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx, | |
6800 | int ttbrn) | |
6801 | { | |
6802 | if (mmu_idx == ARMMMUIdx_S2NS) { | |
b698e9cf | 6803 | return env->cp15.vttbr_el2; |
aef878be GB |
6804 | } |
6805 | if (ttbrn == 0) { | |
6806 | return env->cp15.ttbr0_el[regime_el(env, mmu_idx)]; | |
6807 | } else { | |
6808 | return env->cp15.ttbr1_el[regime_el(env, mmu_idx)]; | |
6809 | } | |
6810 | } | |
6811 | ||
0480f69a PM |
6812 | /* Return true if the translation regime is using LPAE format page tables */ |
6813 | static inline bool regime_using_lpae_format(CPUARMState *env, | |
6814 | ARMMMUIdx mmu_idx) | |
6815 | { | |
6816 | int el = regime_el(env, mmu_idx); | |
6817 | if (el == 2 || arm_el_is_aa64(env, el)) { | |
6818 | return true; | |
6819 | } | |
6820 | if (arm_feature(env, ARM_FEATURE_LPAE) | |
6821 | && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) { | |
6822 | return true; | |
6823 | } | |
6824 | return false; | |
6825 | } | |
6826 | ||
deb2db99 AR |
6827 | /* Returns true if the stage 1 translation regime is using LPAE format page |
6828 | * tables. Used when raising alignment exceptions, whose FSR changes depending | |
6829 | * on whether the long or short descriptor format is in use. */ | |
6830 | bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx) | |
30901475 | 6831 | { |
deb2db99 AR |
6832 | if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { |
6833 | mmu_idx += ARMMMUIdx_S1NSE0; | |
6834 | } | |
6835 | ||
30901475 AB |
6836 | return regime_using_lpae_format(env, mmu_idx); |
6837 | } | |
6838 | ||
0480f69a PM |
6839 | static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx) |
6840 | { | |
6841 | switch (mmu_idx) { | |
6842 | case ARMMMUIdx_S1SE0: | |
6843 | case ARMMMUIdx_S1NSE0: | |
6844 | return true; | |
6845 | default: | |
6846 | return false; | |
6847 | case ARMMMUIdx_S12NSE0: | |
6848 | case ARMMMUIdx_S12NSE1: | |
6849 | g_assert_not_reached(); | |
6850 | } | |
6851 | } | |
6852 | ||
0fbf5238 AJ |
6853 | /* Translate section/page access permissions to page |
6854 | * R/W protection flags | |
d76951b6 AJ |
6855 | * |
6856 | * @env: CPUARMState | |
6857 | * @mmu_idx: MMU index indicating required translation regime | |
6858 | * @ap: The 3-bit access permissions (AP[2:0]) | |
6859 | * @domain_prot: The 2-bit domain access permissions | |
0fbf5238 AJ |
6860 | */ |
6861 | static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, | |
6862 | int ap, int domain_prot) | |
6863 | { | |
554b0b09 PM |
6864 | bool is_user = regime_is_user(env, mmu_idx); |
6865 | ||
6866 | if (domain_prot == 3) { | |
6867 | return PAGE_READ | PAGE_WRITE; | |
6868 | } | |
6869 | ||
554b0b09 PM |
6870 | switch (ap) { |
6871 | case 0: | |
6872 | if (arm_feature(env, ARM_FEATURE_V7)) { | |
6873 | return 0; | |
6874 | } | |
554b0b09 PM |
6875 | switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) { |
6876 | case SCTLR_S: | |
6877 | return is_user ? 0 : PAGE_READ; | |
6878 | case SCTLR_R: | |
6879 | return PAGE_READ; | |
6880 | default: | |
6881 | return 0; | |
6882 | } | |
6883 | case 1: | |
6884 | return is_user ? 0 : PAGE_READ | PAGE_WRITE; | |
6885 | case 2: | |
87c3d486 | 6886 | if (is_user) { |
0fbf5238 | 6887 | return PAGE_READ; |
87c3d486 | 6888 | } else { |
554b0b09 | 6889 | return PAGE_READ | PAGE_WRITE; |
87c3d486 | 6890 | } |
554b0b09 PM |
6891 | case 3: |
6892 | return PAGE_READ | PAGE_WRITE; | |
6893 | case 4: /* Reserved. */ | |
6894 | return 0; | |
6895 | case 5: | |
0fbf5238 | 6896 | return is_user ? 0 : PAGE_READ; |
554b0b09 | 6897 | case 6: |
0fbf5238 | 6898 | return PAGE_READ; |
554b0b09 | 6899 | case 7: |
87c3d486 | 6900 | if (!arm_feature(env, ARM_FEATURE_V6K)) { |
554b0b09 | 6901 | return 0; |
87c3d486 | 6902 | } |
0fbf5238 | 6903 | return PAGE_READ; |
554b0b09 | 6904 | default: |
0fbf5238 | 6905 | g_assert_not_reached(); |
554b0b09 | 6906 | } |
b5ff1b31 FB |
6907 | } |
6908 | ||
d76951b6 AJ |
6909 | /* Translate section/page access permissions to page |
6910 | * R/W protection flags. | |
6911 | * | |
d76951b6 | 6912 | * @ap: The 2-bit simple AP (AP[2:1]) |
d8e052b3 | 6913 | * @is_user: TRUE if accessing from PL0 |
d76951b6 | 6914 | */ |
d8e052b3 | 6915 | static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user) |
d76951b6 | 6916 | { |
d76951b6 AJ |
6917 | switch (ap) { |
6918 | case 0: | |
6919 | return is_user ? 0 : PAGE_READ | PAGE_WRITE; | |
6920 | case 1: | |
6921 | return PAGE_READ | PAGE_WRITE; | |
6922 | case 2: | |
6923 | return is_user ? 0 : PAGE_READ; | |
6924 | case 3: | |
6925 | return PAGE_READ; | |
6926 | default: | |
6927 | g_assert_not_reached(); | |
6928 | } | |
6929 | } | |
6930 | ||
d8e052b3 AJ |
6931 | static inline int |
6932 | simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap) | |
6933 | { | |
6934 | return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx)); | |
6935 | } | |
6936 | ||
6ab1a5ee EI |
6937 | /* Translate S2 section/page access permissions to protection flags |
6938 | * | |
6939 | * @env: CPUARMState | |
6940 | * @s2ap: The 2-bit stage2 access permissions (S2AP) | |
6941 | * @xn: XN (execute-never) bit | |
6942 | */ | |
6943 | static int get_S2prot(CPUARMState *env, int s2ap, int xn) | |
6944 | { | |
6945 | int prot = 0; | |
6946 | ||
6947 | if (s2ap & 1) { | |
6948 | prot |= PAGE_READ; | |
6949 | } | |
6950 | if (s2ap & 2) { | |
6951 | prot |= PAGE_WRITE; | |
6952 | } | |
6953 | if (!xn) { | |
dfda6837 SS |
6954 | if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) { |
6955 | prot |= PAGE_EXEC; | |
6956 | } | |
6ab1a5ee EI |
6957 | } |
6958 | return prot; | |
6959 | } | |
6960 | ||
d8e052b3 AJ |
6961 | /* Translate section/page access permissions to protection flags |
6962 | * | |
6963 | * @env: CPUARMState | |
6964 | * @mmu_idx: MMU index indicating required translation regime | |
6965 | * @is_aa64: TRUE if AArch64 | |
6966 | * @ap: The 2-bit simple AP (AP[2:1]) | |
6967 | * @ns: NS (non-secure) bit | |
6968 | * @xn: XN (execute-never) bit | |
6969 | * @pxn: PXN (privileged execute-never) bit | |
6970 | */ | |
6971 | static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64, | |
6972 | int ap, int ns, int xn, int pxn) | |
6973 | { | |
6974 | bool is_user = regime_is_user(env, mmu_idx); | |
6975 | int prot_rw, user_rw; | |
6976 | bool have_wxn; | |
6977 | int wxn = 0; | |
6978 | ||
6979 | assert(mmu_idx != ARMMMUIdx_S2NS); | |
6980 | ||
6981 | user_rw = simple_ap_to_rw_prot_is_user(ap, true); | |
6982 | if (is_user) { | |
6983 | prot_rw = user_rw; | |
6984 | } else { | |
6985 | prot_rw = simple_ap_to_rw_prot_is_user(ap, false); | |
6986 | } | |
6987 | ||
6988 | if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) { | |
6989 | return prot_rw; | |
6990 | } | |
6991 | ||
6992 | /* TODO have_wxn should be replaced with | |
6993 | * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2) | |
6994 | * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE | |
6995 | * compatible processors have EL2, which is required for [U]WXN. | |
6996 | */ | |
6997 | have_wxn = arm_feature(env, ARM_FEATURE_LPAE); | |
6998 | ||
6999 | if (have_wxn) { | |
7000 | wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN; | |
7001 | } | |
7002 | ||
7003 | if (is_aa64) { | |
7004 | switch (regime_el(env, mmu_idx)) { | |
7005 | case 1: | |
7006 | if (!is_user) { | |
7007 | xn = pxn || (user_rw & PAGE_WRITE); | |
7008 | } | |
7009 | break; | |
7010 | case 2: | |
7011 | case 3: | |
7012 | break; | |
7013 | } | |
7014 | } else if (arm_feature(env, ARM_FEATURE_V7)) { | |
7015 | switch (regime_el(env, mmu_idx)) { | |
7016 | case 1: | |
7017 | case 3: | |
7018 | if (is_user) { | |
7019 | xn = xn || !(user_rw & PAGE_READ); | |
7020 | } else { | |
7021 | int uwxn = 0; | |
7022 | if (have_wxn) { | |
7023 | uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN; | |
7024 | } | |
7025 | xn = xn || !(prot_rw & PAGE_READ) || pxn || | |
7026 | (uwxn && (user_rw & PAGE_WRITE)); | |
7027 | } | |
7028 | break; | |
7029 | case 2: | |
7030 | break; | |
7031 | } | |
7032 | } else { | |
7033 | xn = wxn = 0; | |
7034 | } | |
7035 | ||
7036 | if (xn || (wxn && (prot_rw & PAGE_WRITE))) { | |
7037 | return prot_rw; | |
7038 | } | |
7039 | return prot_rw | PAGE_EXEC; | |
7040 | } | |
7041 | ||
0480f69a PM |
7042 | static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx, |
7043 | uint32_t *table, uint32_t address) | |
b2fa1797 | 7044 | { |
0480f69a | 7045 | /* Note that we can only get here for an AArch32 PL0/PL1 lookup */ |
0480f69a | 7046 | TCR *tcr = regime_tcr(env, mmu_idx); |
11f136ee | 7047 | |
11f136ee FA |
7048 | if (address & tcr->mask) { |
7049 | if (tcr->raw_tcr & TTBCR_PD1) { | |
e389be16 FA |
7050 | /* Translation table walk disabled for TTBR1 */ |
7051 | return false; | |
7052 | } | |
aef878be | 7053 | *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000; |
e389be16 | 7054 | } else { |
11f136ee | 7055 | if (tcr->raw_tcr & TTBCR_PD0) { |
e389be16 FA |
7056 | /* Translation table walk disabled for TTBR0 */ |
7057 | return false; | |
7058 | } | |
aef878be | 7059 | *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask; |
e389be16 FA |
7060 | } |
7061 | *table |= (address >> 18) & 0x3ffc; | |
7062 | return true; | |
b2fa1797 PB |
7063 | } |
7064 | ||
37785977 EI |
7065 | /* Translate a S1 pagetable walk through S2 if needed. */ |
7066 | static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx, | |
7067 | hwaddr addr, MemTxAttrs txattrs, | |
7068 | uint32_t *fsr, | |
7069 | ARMMMUFaultInfo *fi) | |
7070 | { | |
7071 | if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) && | |
7072 | !regime_translation_disabled(env, ARMMMUIdx_S2NS)) { | |
7073 | target_ulong s2size; | |
7074 | hwaddr s2pa; | |
7075 | int s2prot; | |
7076 | int ret; | |
7077 | ||
7078 | ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa, | |
7079 | &txattrs, &s2prot, &s2size, fsr, fi); | |
7080 | if (ret) { | |
7081 | fi->s2addr = addr; | |
7082 | fi->stage2 = true; | |
7083 | fi->s1ptw = true; | |
7084 | return ~0; | |
7085 | } | |
7086 | addr = s2pa; | |
7087 | } | |
7088 | return addr; | |
7089 | } | |
7090 | ||
ebca90e4 PM |
7091 | /* All loads done in the course of a page table walk go through here. |
7092 | * TODO: rather than ignoring errors from physical memory reads (which | |
7093 | * are external aborts in ARM terminology) we should propagate this | |
7094 | * error out so that we can turn it into a Data Abort if this walk | |
7095 | * was being done for a CPU load/store or an address translation instruction | |
7096 | * (but not if it was for a debug access). | |
7097 | */ | |
a614e698 EI |
7098 | static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure, |
7099 | ARMMMUIdx mmu_idx, uint32_t *fsr, | |
7100 | ARMMMUFaultInfo *fi) | |
ebca90e4 | 7101 | { |
a614e698 EI |
7102 | ARMCPU *cpu = ARM_CPU(cs); |
7103 | CPUARMState *env = &cpu->env; | |
ebca90e4 | 7104 | MemTxAttrs attrs = {}; |
5ce4ff65 | 7105 | AddressSpace *as; |
ebca90e4 PM |
7106 | |
7107 | attrs.secure = is_secure; | |
5ce4ff65 | 7108 | as = arm_addressspace(cs, attrs); |
a614e698 EI |
7109 | addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi); |
7110 | if (fi->s1ptw) { | |
7111 | return 0; | |
7112 | } | |
73462ddd PC |
7113 | if (regime_translation_big_endian(env, mmu_idx)) { |
7114 | return address_space_ldl_be(as, addr, attrs, NULL); | |
7115 | } else { | |
7116 | return address_space_ldl_le(as, addr, attrs, NULL); | |
7117 | } | |
ebca90e4 PM |
7118 | } |
7119 | ||
37785977 EI |
7120 | static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure, |
7121 | ARMMMUIdx mmu_idx, uint32_t *fsr, | |
7122 | ARMMMUFaultInfo *fi) | |
ebca90e4 | 7123 | { |
37785977 EI |
7124 | ARMCPU *cpu = ARM_CPU(cs); |
7125 | CPUARMState *env = &cpu->env; | |
ebca90e4 | 7126 | MemTxAttrs attrs = {}; |
5ce4ff65 | 7127 | AddressSpace *as; |
ebca90e4 PM |
7128 | |
7129 | attrs.secure = is_secure; | |
5ce4ff65 | 7130 | as = arm_addressspace(cs, attrs); |
37785977 EI |
7131 | addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fsr, fi); |
7132 | if (fi->s1ptw) { | |
7133 | return 0; | |
7134 | } | |
73462ddd PC |
7135 | if (regime_translation_big_endian(env, mmu_idx)) { |
7136 | return address_space_ldq_be(as, addr, attrs, NULL); | |
7137 | } else { | |
7138 | return address_space_ldq_le(as, addr, attrs, NULL); | |
7139 | } | |
ebca90e4 PM |
7140 | } |
7141 | ||
b7cc4e82 PC |
7142 | static bool get_phys_addr_v5(CPUARMState *env, uint32_t address, |
7143 | int access_type, ARMMMUIdx mmu_idx, | |
7144 | hwaddr *phys_ptr, int *prot, | |
e14b5a23 EI |
7145 | target_ulong *page_size, uint32_t *fsr, |
7146 | ARMMMUFaultInfo *fi) | |
b5ff1b31 | 7147 | { |
70d74660 | 7148 | CPUState *cs = CPU(arm_env_get_cpu(env)); |
b5ff1b31 FB |
7149 | int code; |
7150 | uint32_t table; | |
7151 | uint32_t desc; | |
7152 | int type; | |
7153 | int ap; | |
e389be16 | 7154 | int domain = 0; |
dd4ebc2e | 7155 | int domain_prot; |
a8170e5e | 7156 | hwaddr phys_addr; |
0480f69a | 7157 | uint32_t dacr; |
b5ff1b31 | 7158 | |
9ee6e8bb PB |
7159 | /* Pagetable walk. */ |
7160 | /* Lookup l1 descriptor. */ | |
0480f69a | 7161 | if (!get_level1_table_address(env, mmu_idx, &table, address)) { |
e389be16 FA |
7162 | /* Section translation fault if page walk is disabled by PD0 or PD1 */ |
7163 | code = 5; | |
7164 | goto do_fault; | |
7165 | } | |
a614e698 EI |
7166 | desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), |
7167 | mmu_idx, fsr, fi); | |
9ee6e8bb | 7168 | type = (desc & 3); |
dd4ebc2e | 7169 | domain = (desc >> 5) & 0x0f; |
0480f69a PM |
7170 | if (regime_el(env, mmu_idx) == 1) { |
7171 | dacr = env->cp15.dacr_ns; | |
7172 | } else { | |
7173 | dacr = env->cp15.dacr_s; | |
7174 | } | |
7175 | domain_prot = (dacr >> (domain * 2)) & 3; | |
9ee6e8bb | 7176 | if (type == 0) { |
601d70b9 | 7177 | /* Section translation fault. */ |
9ee6e8bb PB |
7178 | code = 5; |
7179 | goto do_fault; | |
7180 | } | |
dd4ebc2e | 7181 | if (domain_prot == 0 || domain_prot == 2) { |
9ee6e8bb PB |
7182 | if (type == 2) |
7183 | code = 9; /* Section domain fault. */ | |
7184 | else | |
7185 | code = 11; /* Page domain fault. */ | |
7186 | goto do_fault; | |
7187 | } | |
7188 | if (type == 2) { | |
7189 | /* 1Mb section. */ | |
7190 | phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); | |
7191 | ap = (desc >> 10) & 3; | |
7192 | code = 13; | |
d4c430a8 | 7193 | *page_size = 1024 * 1024; |
9ee6e8bb PB |
7194 | } else { |
7195 | /* Lookup l2 entry. */ | |
554b0b09 PM |
7196 | if (type == 1) { |
7197 | /* Coarse pagetable. */ | |
7198 | table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); | |
7199 | } else { | |
7200 | /* Fine pagetable. */ | |
7201 | table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); | |
7202 | } | |
a614e698 EI |
7203 | desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), |
7204 | mmu_idx, fsr, fi); | |
9ee6e8bb PB |
7205 | switch (desc & 3) { |
7206 | case 0: /* Page translation fault. */ | |
7207 | code = 7; | |
7208 | goto do_fault; | |
7209 | case 1: /* 64k page. */ | |
7210 | phys_addr = (desc & 0xffff0000) | (address & 0xffff); | |
7211 | ap = (desc >> (4 + ((address >> 13) & 6))) & 3; | |
d4c430a8 | 7212 | *page_size = 0x10000; |
ce819861 | 7213 | break; |
9ee6e8bb PB |
7214 | case 2: /* 4k page. */ |
7215 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); | |
c10f7fc3 | 7216 | ap = (desc >> (4 + ((address >> 9) & 6))) & 3; |
d4c430a8 | 7217 | *page_size = 0x1000; |
ce819861 | 7218 | break; |
fc1891c7 | 7219 | case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */ |
554b0b09 | 7220 | if (type == 1) { |
fc1891c7 PM |
7221 | /* ARMv6/XScale extended small page format */ |
7222 | if (arm_feature(env, ARM_FEATURE_XSCALE) | |
7223 | || arm_feature(env, ARM_FEATURE_V6)) { | |
554b0b09 | 7224 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); |
fc1891c7 | 7225 | *page_size = 0x1000; |
554b0b09 | 7226 | } else { |
fc1891c7 PM |
7227 | /* UNPREDICTABLE in ARMv5; we choose to take a |
7228 | * page translation fault. | |
7229 | */ | |
554b0b09 PM |
7230 | code = 7; |
7231 | goto do_fault; | |
7232 | } | |
7233 | } else { | |
7234 | phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); | |
fc1891c7 | 7235 | *page_size = 0x400; |
554b0b09 | 7236 | } |
9ee6e8bb | 7237 | ap = (desc >> 4) & 3; |
ce819861 PB |
7238 | break; |
7239 | default: | |
9ee6e8bb PB |
7240 | /* Never happens, but compiler isn't smart enough to tell. */ |
7241 | abort(); | |
ce819861 | 7242 | } |
9ee6e8bb PB |
7243 | code = 15; |
7244 | } | |
0fbf5238 AJ |
7245 | *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); |
7246 | *prot |= *prot ? PAGE_EXEC : 0; | |
7247 | if (!(*prot & (1 << access_type))) { | |
9ee6e8bb PB |
7248 | /* Access permission fault. */ |
7249 | goto do_fault; | |
7250 | } | |
7251 | *phys_ptr = phys_addr; | |
b7cc4e82 | 7252 | return false; |
9ee6e8bb | 7253 | do_fault: |
b7cc4e82 PC |
7254 | *fsr = code | (domain << 4); |
7255 | return true; | |
9ee6e8bb PB |
7256 | } |
7257 | ||
b7cc4e82 PC |
7258 | static bool get_phys_addr_v6(CPUARMState *env, uint32_t address, |
7259 | int access_type, ARMMMUIdx mmu_idx, | |
7260 | hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, | |
e14b5a23 EI |
7261 | target_ulong *page_size, uint32_t *fsr, |
7262 | ARMMMUFaultInfo *fi) | |
9ee6e8bb | 7263 | { |
70d74660 | 7264 | CPUState *cs = CPU(arm_env_get_cpu(env)); |
9ee6e8bb PB |
7265 | int code; |
7266 | uint32_t table; | |
7267 | uint32_t desc; | |
7268 | uint32_t xn; | |
de9b05b8 | 7269 | uint32_t pxn = 0; |
9ee6e8bb PB |
7270 | int type; |
7271 | int ap; | |
de9b05b8 | 7272 | int domain = 0; |
dd4ebc2e | 7273 | int domain_prot; |
a8170e5e | 7274 | hwaddr phys_addr; |
0480f69a | 7275 | uint32_t dacr; |
8bf5b6a9 | 7276 | bool ns; |
9ee6e8bb PB |
7277 | |
7278 | /* Pagetable walk. */ | |
7279 | /* Lookup l1 descriptor. */ | |
0480f69a | 7280 | if (!get_level1_table_address(env, mmu_idx, &table, address)) { |
e389be16 FA |
7281 | /* Section translation fault if page walk is disabled by PD0 or PD1 */ |
7282 | code = 5; | |
7283 | goto do_fault; | |
7284 | } | |
a614e698 EI |
7285 | desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), |
7286 | mmu_idx, fsr, fi); | |
9ee6e8bb | 7287 | type = (desc & 3); |
de9b05b8 PM |
7288 | if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) { |
7289 | /* Section translation fault, or attempt to use the encoding | |
7290 | * which is Reserved on implementations without PXN. | |
7291 | */ | |
9ee6e8bb | 7292 | code = 5; |
9ee6e8bb | 7293 | goto do_fault; |
de9b05b8 PM |
7294 | } |
7295 | if ((type == 1) || !(desc & (1 << 18))) { | |
7296 | /* Page or Section. */ | |
dd4ebc2e | 7297 | domain = (desc >> 5) & 0x0f; |
9ee6e8bb | 7298 | } |
0480f69a PM |
7299 | if (regime_el(env, mmu_idx) == 1) { |
7300 | dacr = env->cp15.dacr_ns; | |
7301 | } else { | |
7302 | dacr = env->cp15.dacr_s; | |
7303 | } | |
7304 | domain_prot = (dacr >> (domain * 2)) & 3; | |
dd4ebc2e | 7305 | if (domain_prot == 0 || domain_prot == 2) { |
de9b05b8 | 7306 | if (type != 1) { |
9ee6e8bb | 7307 | code = 9; /* Section domain fault. */ |
de9b05b8 | 7308 | } else { |
9ee6e8bb | 7309 | code = 11; /* Page domain fault. */ |
de9b05b8 | 7310 | } |
9ee6e8bb PB |
7311 | goto do_fault; |
7312 | } | |
de9b05b8 | 7313 | if (type != 1) { |
9ee6e8bb PB |
7314 | if (desc & (1 << 18)) { |
7315 | /* Supersection. */ | |
7316 | phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); | |
4e42a6ca SF |
7317 | phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32; |
7318 | phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36; | |
d4c430a8 | 7319 | *page_size = 0x1000000; |
b5ff1b31 | 7320 | } else { |
9ee6e8bb PB |
7321 | /* Section. */ |
7322 | phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); | |
d4c430a8 | 7323 | *page_size = 0x100000; |
b5ff1b31 | 7324 | } |
9ee6e8bb PB |
7325 | ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); |
7326 | xn = desc & (1 << 4); | |
de9b05b8 | 7327 | pxn = desc & 1; |
9ee6e8bb | 7328 | code = 13; |
8bf5b6a9 | 7329 | ns = extract32(desc, 19, 1); |
9ee6e8bb | 7330 | } else { |
de9b05b8 PM |
7331 | if (arm_feature(env, ARM_FEATURE_PXN)) { |
7332 | pxn = (desc >> 2) & 1; | |
7333 | } | |
8bf5b6a9 | 7334 | ns = extract32(desc, 3, 1); |
9ee6e8bb PB |
7335 | /* Lookup l2 entry. */ |
7336 | table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); | |
a614e698 EI |
7337 | desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), |
7338 | mmu_idx, fsr, fi); | |
9ee6e8bb PB |
7339 | ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); |
7340 | switch (desc & 3) { | |
7341 | case 0: /* Page translation fault. */ | |
7342 | code = 7; | |
b5ff1b31 | 7343 | goto do_fault; |
9ee6e8bb PB |
7344 | case 1: /* 64k page. */ |
7345 | phys_addr = (desc & 0xffff0000) | (address & 0xffff); | |
7346 | xn = desc & (1 << 15); | |
d4c430a8 | 7347 | *page_size = 0x10000; |
9ee6e8bb PB |
7348 | break; |
7349 | case 2: case 3: /* 4k page. */ | |
7350 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); | |
7351 | xn = desc & 1; | |
d4c430a8 | 7352 | *page_size = 0x1000; |
9ee6e8bb PB |
7353 | break; |
7354 | default: | |
7355 | /* Never happens, but compiler isn't smart enough to tell. */ | |
7356 | abort(); | |
b5ff1b31 | 7357 | } |
9ee6e8bb PB |
7358 | code = 15; |
7359 | } | |
dd4ebc2e | 7360 | if (domain_prot == 3) { |
c0034328 JR |
7361 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
7362 | } else { | |
0480f69a | 7363 | if (pxn && !regime_is_user(env, mmu_idx)) { |
de9b05b8 PM |
7364 | xn = 1; |
7365 | } | |
c0034328 JR |
7366 | if (xn && access_type == 2) |
7367 | goto do_fault; | |
9ee6e8bb | 7368 | |
d76951b6 AJ |
7369 | if (arm_feature(env, ARM_FEATURE_V6K) && |
7370 | (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) { | |
7371 | /* The simplified model uses AP[0] as an access control bit. */ | |
7372 | if ((ap & 1) == 0) { | |
7373 | /* Access flag fault. */ | |
7374 | code = (code == 15) ? 6 : 3; | |
7375 | goto do_fault; | |
7376 | } | |
7377 | *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1); | |
7378 | } else { | |
7379 | *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); | |
c0034328 | 7380 | } |
0fbf5238 AJ |
7381 | if (*prot && !xn) { |
7382 | *prot |= PAGE_EXEC; | |
7383 | } | |
7384 | if (!(*prot & (1 << access_type))) { | |
c0034328 JR |
7385 | /* Access permission fault. */ |
7386 | goto do_fault; | |
7387 | } | |
3ad493fc | 7388 | } |
8bf5b6a9 PM |
7389 | if (ns) { |
7390 | /* The NS bit will (as required by the architecture) have no effect if | |
7391 | * the CPU doesn't support TZ or this is a non-secure translation | |
7392 | * regime, because the attribute will already be non-secure. | |
7393 | */ | |
7394 | attrs->secure = false; | |
7395 | } | |
9ee6e8bb | 7396 | *phys_ptr = phys_addr; |
b7cc4e82 | 7397 | return false; |
b5ff1b31 | 7398 | do_fault: |
b7cc4e82 PC |
7399 | *fsr = code | (domain << 4); |
7400 | return true; | |
b5ff1b31 FB |
7401 | } |
7402 | ||
3dde962f PM |
7403 | /* Fault type for long-descriptor MMU fault reporting; this corresponds |
7404 | * to bits [5..2] in the STATUS field in long-format DFSR/IFSR. | |
7405 | */ | |
7406 | typedef enum { | |
7407 | translation_fault = 1, | |
7408 | access_fault = 2, | |
7409 | permission_fault = 3, | |
7410 | } MMUFaultType; | |
7411 | ||
1853d5a9 | 7412 | /* |
a0e966c9 | 7413 | * check_s2_mmu_setup |
1853d5a9 EI |
7414 | * @cpu: ARMCPU |
7415 | * @is_aa64: True if the translation regime is in AArch64 state | |
7416 | * @startlevel: Suggested starting level | |
7417 | * @inputsize: Bitsize of IPAs | |
7418 | * @stride: Page-table stride (See the ARM ARM) | |
7419 | * | |
a0e966c9 EI |
7420 | * Returns true if the suggested S2 translation parameters are OK and |
7421 | * false otherwise. | |
1853d5a9 | 7422 | */ |
a0e966c9 EI |
7423 | static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level, |
7424 | int inputsize, int stride) | |
1853d5a9 | 7425 | { |
98d68ec2 EI |
7426 | const int grainsize = stride + 3; |
7427 | int startsizecheck; | |
7428 | ||
1853d5a9 EI |
7429 | /* Negative levels are never allowed. */ |
7430 | if (level < 0) { | |
7431 | return false; | |
7432 | } | |
7433 | ||
98d68ec2 EI |
7434 | startsizecheck = inputsize - ((3 - level) * stride + grainsize); |
7435 | if (startsizecheck < 1 || startsizecheck > stride + 4) { | |
7436 | return false; | |
7437 | } | |
7438 | ||
1853d5a9 | 7439 | if (is_aa64) { |
3526423e | 7440 | CPUARMState *env = &cpu->env; |
1853d5a9 EI |
7441 | unsigned int pamax = arm_pamax(cpu); |
7442 | ||
7443 | switch (stride) { | |
7444 | case 13: /* 64KB Pages. */ | |
7445 | if (level == 0 || (level == 1 && pamax <= 42)) { | |
7446 | return false; | |
7447 | } | |
7448 | break; | |
7449 | case 11: /* 16KB Pages. */ | |
7450 | if (level == 0 || (level == 1 && pamax <= 40)) { | |
7451 | return false; | |
7452 | } | |
7453 | break; | |
7454 | case 9: /* 4KB Pages. */ | |
7455 | if (level == 0 && pamax <= 42) { | |
7456 | return false; | |
7457 | } | |
7458 | break; | |
7459 | default: | |
7460 | g_assert_not_reached(); | |
7461 | } | |
3526423e EI |
7462 | |
7463 | /* Inputsize checks. */ | |
7464 | if (inputsize > pamax && | |
7465 | (arm_el_is_aa64(env, 1) || inputsize > 40)) { | |
7466 | /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */ | |
7467 | return false; | |
7468 | } | |
1853d5a9 | 7469 | } else { |
1853d5a9 EI |
7470 | /* AArch32 only supports 4KB pages. Assert on that. */ |
7471 | assert(stride == 9); | |
7472 | ||
7473 | if (level == 0) { | |
7474 | return false; | |
7475 | } | |
1853d5a9 EI |
7476 | } |
7477 | return true; | |
7478 | } | |
7479 | ||
b7cc4e82 PC |
7480 | static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, |
7481 | int access_type, ARMMMUIdx mmu_idx, | |
7482 | hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, | |
e14b5a23 EI |
7483 | target_ulong *page_size_ptr, uint32_t *fsr, |
7484 | ARMMMUFaultInfo *fi) | |
3dde962f | 7485 | { |
1853d5a9 EI |
7486 | ARMCPU *cpu = arm_env_get_cpu(env); |
7487 | CPUState *cs = CPU(cpu); | |
3dde962f PM |
7488 | /* Read an LPAE long-descriptor translation table. */ |
7489 | MMUFaultType fault_type = translation_fault; | |
1b4093ea | 7490 | uint32_t level; |
0c5fbf3b | 7491 | uint32_t epd = 0; |
1f4c8c18 | 7492 | int32_t t0sz, t1sz; |
2c8dd318 | 7493 | uint32_t tg; |
3dde962f PM |
7494 | uint64_t ttbr; |
7495 | int ttbr_select; | |
dddb5223 | 7496 | hwaddr descaddr, indexmask, indexmask_grainsize; |
3dde962f PM |
7497 | uint32_t tableattrs; |
7498 | target_ulong page_size; | |
7499 | uint32_t attrs; | |
973a5434 | 7500 | int32_t stride = 9; |
6e99f762 | 7501 | int32_t addrsize; |
4ca6a051 | 7502 | int inputsize; |
2c8dd318 | 7503 | int32_t tbi = 0; |
0480f69a | 7504 | TCR *tcr = regime_tcr(env, mmu_idx); |
d8e052b3 | 7505 | int ap, ns, xn, pxn; |
88e8add8 GB |
7506 | uint32_t el = regime_el(env, mmu_idx); |
7507 | bool ttbr1_valid = true; | |
6109769a | 7508 | uint64_t descaddrmask; |
6e99f762 | 7509 | bool aarch64 = arm_el_is_aa64(env, el); |
0480f69a PM |
7510 | |
7511 | /* TODO: | |
88e8add8 GB |
7512 | * This code does not handle the different format TCR for VTCR_EL2. |
7513 | * This code also does not support shareability levels. | |
7514 | * Attribute and permission bit handling should also be checked when adding | |
7515 | * support for those page table walks. | |
0480f69a | 7516 | */ |
6e99f762 | 7517 | if (aarch64) { |
1b4093ea | 7518 | level = 0; |
6e99f762 | 7519 | addrsize = 64; |
88e8add8 | 7520 | if (el > 1) { |
1edee470 EI |
7521 | if (mmu_idx != ARMMMUIdx_S2NS) { |
7522 | tbi = extract64(tcr->raw_tcr, 20, 1); | |
7523 | } | |
88e8add8 GB |
7524 | } else { |
7525 | if (extract64(address, 55, 1)) { | |
7526 | tbi = extract64(tcr->raw_tcr, 38, 1); | |
7527 | } else { | |
7528 | tbi = extract64(tcr->raw_tcr, 37, 1); | |
7529 | } | |
7530 | } | |
2c8dd318 | 7531 | tbi *= 8; |
88e8add8 GB |
7532 | |
7533 | /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it | |
7534 | * invalid. | |
7535 | */ | |
7536 | if (el > 1) { | |
7537 | ttbr1_valid = false; | |
7538 | } | |
d0a2cbce | 7539 | } else { |
1b4093ea | 7540 | level = 1; |
6e99f762 | 7541 | addrsize = 32; |
d0a2cbce PM |
7542 | /* There is no TTBR1 for EL2 */ |
7543 | if (el == 2) { | |
7544 | ttbr1_valid = false; | |
7545 | } | |
2c8dd318 | 7546 | } |
3dde962f PM |
7547 | |
7548 | /* Determine whether this address is in the region controlled by | |
7549 | * TTBR0 or TTBR1 (or if it is in neither region and should fault). | |
7550 | * This is a Non-secure PL0/1 stage 1 translation, so controlled by | |
7551 | * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32: | |
7552 | */ | |
6e99f762 | 7553 | if (aarch64) { |
4ee38098 EI |
7554 | /* AArch64 translation. */ |
7555 | t0sz = extract32(tcr->raw_tcr, 0, 6); | |
2c8dd318 RH |
7556 | t0sz = MIN(t0sz, 39); |
7557 | t0sz = MAX(t0sz, 16); | |
4ee38098 EI |
7558 | } else if (mmu_idx != ARMMMUIdx_S2NS) { |
7559 | /* AArch32 stage 1 translation. */ | |
7560 | t0sz = extract32(tcr->raw_tcr, 0, 3); | |
7561 | } else { | |
7562 | /* AArch32 stage 2 translation. */ | |
7563 | bool sext = extract32(tcr->raw_tcr, 4, 1); | |
7564 | bool sign = extract32(tcr->raw_tcr, 3, 1); | |
6e99f762 SS |
7565 | /* Address size is 40-bit for a stage 2 translation, |
7566 | * and t0sz can be negative (from -8 to 7), | |
7567 | * so we need to adjust it to use the TTBR selecting logic below. | |
7568 | */ | |
7569 | addrsize = 40; | |
7570 | t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8; | |
4ee38098 EI |
7571 | |
7572 | /* If the sign-extend bit is not the same as t0sz[3], the result | |
7573 | * is unpredictable. Flag this as a guest error. */ | |
7574 | if (sign != sext) { | |
7575 | qemu_log_mask(LOG_GUEST_ERROR, | |
39cba610 | 7576 | "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n"); |
4ee38098 | 7577 | } |
2c8dd318 | 7578 | } |
1f4c8c18 | 7579 | t1sz = extract32(tcr->raw_tcr, 16, 6); |
6e99f762 | 7580 | if (aarch64) { |
2c8dd318 RH |
7581 | t1sz = MIN(t1sz, 39); |
7582 | t1sz = MAX(t1sz, 16); | |
7583 | } | |
6e99f762 | 7584 | if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) { |
3dde962f PM |
7585 | /* there is a ttbr0 region and we are in it (high bits all zero) */ |
7586 | ttbr_select = 0; | |
88e8add8 | 7587 | } else if (ttbr1_valid && t1sz && |
6e99f762 | 7588 | !extract64(~address, addrsize - t1sz, t1sz - tbi)) { |
3dde962f PM |
7589 | /* there is a ttbr1 region and we are in it (high bits all one) */ |
7590 | ttbr_select = 1; | |
7591 | } else if (!t0sz) { | |
7592 | /* ttbr0 region is "everything not in the ttbr1 region" */ | |
7593 | ttbr_select = 0; | |
88e8add8 | 7594 | } else if (!t1sz && ttbr1_valid) { |
3dde962f PM |
7595 | /* ttbr1 region is "everything not in the ttbr0 region" */ |
7596 | ttbr_select = 1; | |
7597 | } else { | |
7598 | /* in the gap between the two regions, this is a Translation fault */ | |
7599 | fault_type = translation_fault; | |
7600 | goto do_fault; | |
7601 | } | |
7602 | ||
7603 | /* Note that QEMU ignores shareability and cacheability attributes, | |
7604 | * so we don't need to do anything with the SH, ORGN, IRGN fields | |
7605 | * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the | |
7606 | * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently | |
7607 | * implement any ASID-like capability so we can ignore it (instead | |
7608 | * we will always flush the TLB any time the ASID is changed). | |
7609 | */ | |
7610 | if (ttbr_select == 0) { | |
aef878be | 7611 | ttbr = regime_ttbr(env, mmu_idx, 0); |
0c5fbf3b EI |
7612 | if (el < 2) { |
7613 | epd = extract32(tcr->raw_tcr, 7, 1); | |
7614 | } | |
6e99f762 | 7615 | inputsize = addrsize - t0sz; |
2c8dd318 | 7616 | |
11f136ee | 7617 | tg = extract32(tcr->raw_tcr, 14, 2); |
2c8dd318 | 7618 | if (tg == 1) { /* 64KB pages */ |
973a5434 | 7619 | stride = 13; |
2c8dd318 RH |
7620 | } |
7621 | if (tg == 2) { /* 16KB pages */ | |
973a5434 | 7622 | stride = 11; |
2c8dd318 | 7623 | } |
3dde962f | 7624 | } else { |
88e8add8 GB |
7625 | /* We should only be here if TTBR1 is valid */ |
7626 | assert(ttbr1_valid); | |
7627 | ||
aef878be | 7628 | ttbr = regime_ttbr(env, mmu_idx, 1); |
11f136ee | 7629 | epd = extract32(tcr->raw_tcr, 23, 1); |
6e99f762 | 7630 | inputsize = addrsize - t1sz; |
2c8dd318 | 7631 | |
11f136ee | 7632 | tg = extract32(tcr->raw_tcr, 30, 2); |
2c8dd318 | 7633 | if (tg == 3) { /* 64KB pages */ |
973a5434 | 7634 | stride = 13; |
2c8dd318 RH |
7635 | } |
7636 | if (tg == 1) { /* 16KB pages */ | |
973a5434 | 7637 | stride = 11; |
2c8dd318 | 7638 | } |
3dde962f PM |
7639 | } |
7640 | ||
0480f69a | 7641 | /* Here we should have set up all the parameters for the translation: |
6e99f762 | 7642 | * inputsize, ttbr, epd, stride, tbi |
0480f69a PM |
7643 | */ |
7644 | ||
3dde962f | 7645 | if (epd) { |
88e8add8 GB |
7646 | /* Translation table walk disabled => Translation fault on TLB miss |
7647 | * Note: This is always 0 on 64-bit EL2 and EL3. | |
7648 | */ | |
3dde962f PM |
7649 | goto do_fault; |
7650 | } | |
7651 | ||
1853d5a9 EI |
7652 | if (mmu_idx != ARMMMUIdx_S2NS) { |
7653 | /* The starting level depends on the virtual address size (which can | |
7654 | * be up to 48 bits) and the translation granule size. It indicates | |
7655 | * the number of strides (stride bits at a time) needed to | |
7656 | * consume the bits of the input address. In the pseudocode this is: | |
7657 | * level = 4 - RoundUp((inputsize - grainsize) / stride) | |
7658 | * where their 'inputsize' is our 'inputsize', 'grainsize' is | |
7659 | * our 'stride + 3' and 'stride' is our 'stride'. | |
7660 | * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying: | |
7661 | * = 4 - (inputsize - stride - 3 + stride - 1) / stride | |
7662 | * = 4 - (inputsize - 4) / stride; | |
7663 | */ | |
7664 | level = 4 - (inputsize - 4) / stride; | |
7665 | } else { | |
7666 | /* For stage 2 translations the starting level is specified by the | |
7667 | * VTCR_EL2.SL0 field (whose interpretation depends on the page size) | |
7668 | */ | |
1b4093ea SS |
7669 | uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2); |
7670 | uint32_t startlevel; | |
1853d5a9 EI |
7671 | bool ok; |
7672 | ||
6e99f762 | 7673 | if (!aarch64 || stride == 9) { |
1853d5a9 | 7674 | /* AArch32 or 4KB pages */ |
1b4093ea | 7675 | startlevel = 2 - sl0; |
1853d5a9 EI |
7676 | } else { |
7677 | /* 16KB or 64KB pages */ | |
1b4093ea | 7678 | startlevel = 3 - sl0; |
1853d5a9 EI |
7679 | } |
7680 | ||
7681 | /* Check that the starting level is valid. */ | |
6e99f762 | 7682 | ok = check_s2_mmu_setup(cpu, aarch64, startlevel, |
1b4093ea | 7683 | inputsize, stride); |
1853d5a9 | 7684 | if (!ok) { |
1853d5a9 EI |
7685 | fault_type = translation_fault; |
7686 | goto do_fault; | |
7687 | } | |
1b4093ea | 7688 | level = startlevel; |
1853d5a9 | 7689 | } |
3dde962f | 7690 | |
dddb5223 SS |
7691 | indexmask_grainsize = (1ULL << (stride + 3)) - 1; |
7692 | indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1; | |
3dde962f PM |
7693 | |
7694 | /* Now we can extract the actual base address from the TTBR */ | |
2c8dd318 | 7695 | descaddr = extract64(ttbr, 0, 48); |
dddb5223 | 7696 | descaddr &= ~indexmask; |
3dde962f | 7697 | |
6109769a | 7698 | /* The address field in the descriptor goes up to bit 39 for ARMv7 |
dddb5223 SS |
7699 | * but up to bit 47 for ARMv8, but we use the descaddrmask |
7700 | * up to bit 39 for AArch32, because we don't need other bits in that case | |
7701 | * to construct next descriptor address (anyway they should be all zeroes). | |
6109769a | 7702 | */ |
6e99f762 | 7703 | descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) & |
dddb5223 | 7704 | ~indexmask_grainsize; |
6109769a | 7705 | |
ebca90e4 PM |
7706 | /* Secure accesses start with the page table in secure memory and |
7707 | * can be downgraded to non-secure at any step. Non-secure accesses | |
7708 | * remain non-secure. We implement this by just ORing in the NSTable/NS | |
7709 | * bits at each step. | |
7710 | */ | |
7711 | tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4); | |
3dde962f PM |
7712 | for (;;) { |
7713 | uint64_t descriptor; | |
ebca90e4 | 7714 | bool nstable; |
3dde962f | 7715 | |
dddb5223 | 7716 | descaddr |= (address >> (stride * (4 - level))) & indexmask; |
2c8dd318 | 7717 | descaddr &= ~7ULL; |
ebca90e4 | 7718 | nstable = extract32(tableattrs, 4, 1); |
37785977 EI |
7719 | descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fsr, fi); |
7720 | if (fi->s1ptw) { | |
7721 | goto do_fault; | |
7722 | } | |
7723 | ||
3dde962f PM |
7724 | if (!(descriptor & 1) || |
7725 | (!(descriptor & 2) && (level == 3))) { | |
7726 | /* Invalid, or the Reserved level 3 encoding */ | |
7727 | goto do_fault; | |
7728 | } | |
6109769a | 7729 | descaddr = descriptor & descaddrmask; |
3dde962f PM |
7730 | |
7731 | if ((descriptor & 2) && (level < 3)) { | |
7732 | /* Table entry. The top five bits are attributes which may | |
7733 | * propagate down through lower levels of the table (and | |
7734 | * which are all arranged so that 0 means "no effect", so | |
7735 | * we can gather them up by ORing in the bits at each level). | |
7736 | */ | |
7737 | tableattrs |= extract64(descriptor, 59, 5); | |
7738 | level++; | |
dddb5223 | 7739 | indexmask = indexmask_grainsize; |
3dde962f PM |
7740 | continue; |
7741 | } | |
7742 | /* Block entry at level 1 or 2, or page entry at level 3. | |
7743 | * These are basically the same thing, although the number | |
7744 | * of bits we pull in from the vaddr varies. | |
7745 | */ | |
973a5434 | 7746 | page_size = (1ULL << ((stride * (4 - level)) + 3)); |
3dde962f | 7747 | descaddr |= (address & (page_size - 1)); |
6ab1a5ee | 7748 | /* Extract attributes from the descriptor */ |
d615efac IC |
7749 | attrs = extract64(descriptor, 2, 10) |
7750 | | (extract64(descriptor, 52, 12) << 10); | |
6ab1a5ee EI |
7751 | |
7752 | if (mmu_idx == ARMMMUIdx_S2NS) { | |
7753 | /* Stage 2 table descriptors do not include any attribute fields */ | |
7754 | break; | |
7755 | } | |
7756 | /* Merge in attributes from table descriptors */ | |
3dde962f PM |
7757 | attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */ |
7758 | attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */ | |
7759 | /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1 | |
7760 | * means "force PL1 access only", which means forcing AP[1] to 0. | |
7761 | */ | |
7762 | if (extract32(tableattrs, 2, 1)) { | |
7763 | attrs &= ~(1 << 4); | |
7764 | } | |
ebca90e4 | 7765 | attrs |= nstable << 3; /* NS */ |
3dde962f PM |
7766 | break; |
7767 | } | |
7768 | /* Here descaddr is the final physical address, and attributes | |
7769 | * are all in attrs. | |
7770 | */ | |
7771 | fault_type = access_fault; | |
7772 | if ((attrs & (1 << 8)) == 0) { | |
7773 | /* Access flag */ | |
7774 | goto do_fault; | |
7775 | } | |
d8e052b3 AJ |
7776 | |
7777 | ap = extract32(attrs, 4, 2); | |
d8e052b3 | 7778 | xn = extract32(attrs, 12, 1); |
d8e052b3 | 7779 | |
6ab1a5ee EI |
7780 | if (mmu_idx == ARMMMUIdx_S2NS) { |
7781 | ns = true; | |
7782 | *prot = get_S2prot(env, ap, xn); | |
7783 | } else { | |
7784 | ns = extract32(attrs, 3, 1); | |
7785 | pxn = extract32(attrs, 11, 1); | |
6e99f762 | 7786 | *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn); |
6ab1a5ee | 7787 | } |
d8e052b3 | 7788 | |
3dde962f | 7789 | fault_type = permission_fault; |
d8e052b3 | 7790 | if (!(*prot & (1 << access_type))) { |
3dde962f PM |
7791 | goto do_fault; |
7792 | } | |
3dde962f | 7793 | |
8bf5b6a9 PM |
7794 | if (ns) { |
7795 | /* The NS bit will (as required by the architecture) have no effect if | |
7796 | * the CPU doesn't support TZ or this is a non-secure translation | |
7797 | * regime, because the attribute will already be non-secure. | |
7798 | */ | |
7799 | txattrs->secure = false; | |
7800 | } | |
3dde962f PM |
7801 | *phys_ptr = descaddr; |
7802 | *page_size_ptr = page_size; | |
b7cc4e82 | 7803 | return false; |
3dde962f PM |
7804 | |
7805 | do_fault: | |
7806 | /* Long-descriptor format IFSR/DFSR value */ | |
b7cc4e82 | 7807 | *fsr = (1 << 9) | (fault_type << 2) | level; |
37785977 EI |
7808 | /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */ |
7809 | fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS); | |
b7cc4e82 | 7810 | return true; |
3dde962f PM |
7811 | } |
7812 | ||
f6bda88f PC |
7813 | static inline void get_phys_addr_pmsav7_default(CPUARMState *env, |
7814 | ARMMMUIdx mmu_idx, | |
7815 | int32_t address, int *prot) | |
7816 | { | |
7817 | *prot = PAGE_READ | PAGE_WRITE; | |
7818 | switch (address) { | |
7819 | case 0xF0000000 ... 0xFFFFFFFF: | |
7820 | if (regime_sctlr(env, mmu_idx) & SCTLR_V) { /* hivecs execing is ok */ | |
7821 | *prot |= PAGE_EXEC; | |
7822 | } | |
7823 | break; | |
7824 | case 0x00000000 ... 0x7FFFFFFF: | |
7825 | *prot |= PAGE_EXEC; | |
7826 | break; | |
7827 | } | |
7828 | ||
7829 | } | |
7830 | ||
7831 | static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address, | |
7832 | int access_type, ARMMMUIdx mmu_idx, | |
7833 | hwaddr *phys_ptr, int *prot, uint32_t *fsr) | |
7834 | { | |
7835 | ARMCPU *cpu = arm_env_get_cpu(env); | |
7836 | int n; | |
7837 | bool is_user = regime_is_user(env, mmu_idx); | |
7838 | ||
7839 | *phys_ptr = address; | |
7840 | *prot = 0; | |
7841 | ||
7842 | if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */ | |
7843 | get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); | |
7844 | } else { /* MPU enabled */ | |
7845 | for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { | |
7846 | /* region search */ | |
7847 | uint32_t base = env->pmsav7.drbar[n]; | |
7848 | uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5); | |
7849 | uint32_t rmask; | |
7850 | bool srdis = false; | |
7851 | ||
7852 | if (!(env->pmsav7.drsr[n] & 0x1)) { | |
7853 | continue; | |
7854 | } | |
7855 | ||
7856 | if (!rsize) { | |
7857 | qemu_log_mask(LOG_GUEST_ERROR, "DRSR.Rsize field can not be 0"); | |
7858 | continue; | |
7859 | } | |
7860 | rsize++; | |
7861 | rmask = (1ull << rsize) - 1; | |
7862 | ||
7863 | if (base & rmask) { | |
7864 | qemu_log_mask(LOG_GUEST_ERROR, "DRBAR %" PRIx32 " misaligned " | |
7865 | "to DRSR region size, mask = %" PRIx32, | |
7866 | base, rmask); | |
7867 | continue; | |
7868 | } | |
7869 | ||
7870 | if (address < base || address > base + rmask) { | |
7871 | continue; | |
7872 | } | |
7873 | ||
7874 | /* Region matched */ | |
7875 | ||
7876 | if (rsize >= 8) { /* no subregions for regions < 256 bytes */ | |
7877 | int i, snd; | |
7878 | uint32_t srdis_mask; | |
7879 | ||
7880 | rsize -= 3; /* sub region size (power of 2) */ | |
7881 | snd = ((address - base) >> rsize) & 0x7; | |
7882 | srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1); | |
7883 | ||
7884 | srdis_mask = srdis ? 0x3 : 0x0; | |
7885 | for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) { | |
7886 | /* This will check in groups of 2, 4 and then 8, whether | |
7887 | * the subregion bits are consistent. rsize is incremented | |
7888 | * back up to give the region size, considering consistent | |
7889 | * adjacent subregions as one region. Stop testing if rsize | |
7890 | * is already big enough for an entire QEMU page. | |
7891 | */ | |
7892 | int snd_rounded = snd & ~(i - 1); | |
7893 | uint32_t srdis_multi = extract32(env->pmsav7.drsr[n], | |
7894 | snd_rounded + 8, i); | |
7895 | if (srdis_mask ^ srdis_multi) { | |
7896 | break; | |
7897 | } | |
7898 | srdis_mask = (srdis_mask << i) | srdis_mask; | |
7899 | rsize++; | |
7900 | } | |
7901 | } | |
7902 | if (rsize < TARGET_PAGE_BITS) { | |
7903 | qemu_log_mask(LOG_UNIMP, "No support for MPU (sub)region" | |
7904 | "alignment of %" PRIu32 " bits. Minimum is %d\n", | |
7905 | rsize, TARGET_PAGE_BITS); | |
7906 | continue; | |
7907 | } | |
7908 | if (srdis) { | |
7909 | continue; | |
7910 | } | |
7911 | break; | |
7912 | } | |
7913 | ||
7914 | if (n == -1) { /* no hits */ | |
7915 | if (cpu->pmsav7_dregion && | |
7916 | (is_user || !(regime_sctlr(env, mmu_idx) & SCTLR_BR))) { | |
7917 | /* background fault */ | |
7918 | *fsr = 0; | |
7919 | return true; | |
7920 | } | |
7921 | get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); | |
7922 | } else { /* a MPU hit! */ | |
7923 | uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3); | |
7924 | ||
7925 | if (is_user) { /* User mode AP bit decoding */ | |
7926 | switch (ap) { | |
7927 | case 0: | |
7928 | case 1: | |
7929 | case 5: | |
7930 | break; /* no access */ | |
7931 | case 3: | |
7932 | *prot |= PAGE_WRITE; | |
7933 | /* fall through */ | |
7934 | case 2: | |
7935 | case 6: | |
7936 | *prot |= PAGE_READ | PAGE_EXEC; | |
7937 | break; | |
7938 | default: | |
7939 | qemu_log_mask(LOG_GUEST_ERROR, | |
7940 | "Bad value for AP bits in DRACR %" | |
7941 | PRIx32 "\n", ap); | |
7942 | } | |
7943 | } else { /* Priv. mode AP bits decoding */ | |
7944 | switch (ap) { | |
7945 | case 0: | |
7946 | break; /* no access */ | |
7947 | case 1: | |
7948 | case 2: | |
7949 | case 3: | |
7950 | *prot |= PAGE_WRITE; | |
7951 | /* fall through */ | |
7952 | case 5: | |
7953 | case 6: | |
7954 | *prot |= PAGE_READ | PAGE_EXEC; | |
7955 | break; | |
7956 | default: | |
7957 | qemu_log_mask(LOG_GUEST_ERROR, | |
7958 | "Bad value for AP bits in DRACR %" | |
7959 | PRIx32 "\n", ap); | |
7960 | } | |
7961 | } | |
7962 | ||
7963 | /* execute never */ | |
7964 | if (env->pmsav7.dracr[n] & (1 << 12)) { | |
7965 | *prot &= ~PAGE_EXEC; | |
7966 | } | |
7967 | } | |
7968 | } | |
7969 | ||
7970 | *fsr = 0x00d; /* Permission fault */ | |
7971 | return !(*prot & (1 << access_type)); | |
7972 | } | |
7973 | ||
13689d43 PC |
7974 | static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address, |
7975 | int access_type, ARMMMUIdx mmu_idx, | |
7976 | hwaddr *phys_ptr, int *prot, uint32_t *fsr) | |
9ee6e8bb PB |
7977 | { |
7978 | int n; | |
7979 | uint32_t mask; | |
7980 | uint32_t base; | |
0480f69a | 7981 | bool is_user = regime_is_user(env, mmu_idx); |
9ee6e8bb PB |
7982 | |
7983 | *phys_ptr = address; | |
7984 | for (n = 7; n >= 0; n--) { | |
554b0b09 | 7985 | base = env->cp15.c6_region[n]; |
87c3d486 | 7986 | if ((base & 1) == 0) { |
554b0b09 | 7987 | continue; |
87c3d486 | 7988 | } |
554b0b09 PM |
7989 | mask = 1 << ((base >> 1) & 0x1f); |
7990 | /* Keep this shift separate from the above to avoid an | |
7991 | (undefined) << 32. */ | |
7992 | mask = (mask << 1) - 1; | |
87c3d486 | 7993 | if (((base ^ address) & ~mask) == 0) { |
554b0b09 | 7994 | break; |
87c3d486 | 7995 | } |
9ee6e8bb | 7996 | } |
87c3d486 | 7997 | if (n < 0) { |
b7cc4e82 PC |
7998 | *fsr = 2; |
7999 | return true; | |
87c3d486 | 8000 | } |
9ee6e8bb PB |
8001 | |
8002 | if (access_type == 2) { | |
7e09797c | 8003 | mask = env->cp15.pmsav5_insn_ap; |
9ee6e8bb | 8004 | } else { |
7e09797c | 8005 | mask = env->cp15.pmsav5_data_ap; |
9ee6e8bb PB |
8006 | } |
8007 | mask = (mask >> (n * 4)) & 0xf; | |
8008 | switch (mask) { | |
8009 | case 0: | |
b7cc4e82 PC |
8010 | *fsr = 1; |
8011 | return true; | |
9ee6e8bb | 8012 | case 1: |
87c3d486 | 8013 | if (is_user) { |
b7cc4e82 PC |
8014 | *fsr = 1; |
8015 | return true; | |
87c3d486 | 8016 | } |
554b0b09 PM |
8017 | *prot = PAGE_READ | PAGE_WRITE; |
8018 | break; | |
9ee6e8bb | 8019 | case 2: |
554b0b09 | 8020 | *prot = PAGE_READ; |
87c3d486 | 8021 | if (!is_user) { |
554b0b09 | 8022 | *prot |= PAGE_WRITE; |
87c3d486 | 8023 | } |
554b0b09 | 8024 | break; |
9ee6e8bb | 8025 | case 3: |
554b0b09 PM |
8026 | *prot = PAGE_READ | PAGE_WRITE; |
8027 | break; | |
9ee6e8bb | 8028 | case 5: |
87c3d486 | 8029 | if (is_user) { |
b7cc4e82 PC |
8030 | *fsr = 1; |
8031 | return true; | |
87c3d486 | 8032 | } |
554b0b09 PM |
8033 | *prot = PAGE_READ; |
8034 | break; | |
9ee6e8bb | 8035 | case 6: |
554b0b09 PM |
8036 | *prot = PAGE_READ; |
8037 | break; | |
9ee6e8bb | 8038 | default: |
554b0b09 | 8039 | /* Bad permission. */ |
b7cc4e82 PC |
8040 | *fsr = 1; |
8041 | return true; | |
9ee6e8bb | 8042 | } |
3ad493fc | 8043 | *prot |= PAGE_EXEC; |
b7cc4e82 | 8044 | return false; |
9ee6e8bb PB |
8045 | } |
8046 | ||
702a9357 PM |
8047 | /* get_phys_addr - get the physical address for this virtual address |
8048 | * | |
8049 | * Find the physical address corresponding to the given virtual address, | |
8050 | * by doing a translation table walk on MMU based systems or using the | |
8051 | * MPU state on MPU based systems. | |
8052 | * | |
b7cc4e82 PC |
8053 | * Returns false if the translation was successful. Otherwise, phys_ptr, attrs, |
8054 | * prot and page_size may not be filled in, and the populated fsr value provides | |
702a9357 PM |
8055 | * information on why the translation aborted, in the format of a |
8056 | * DFSR/IFSR fault register, with the following caveats: | |
8057 | * * we honour the short vs long DFSR format differences. | |
8058 | * * the WnR bit is never set (the caller must do this). | |
f6bda88f | 8059 | * * for PSMAv5 based systems we don't bother to return a full FSR format |
702a9357 PM |
8060 | * value. |
8061 | * | |
8062 | * @env: CPUARMState | |
8063 | * @address: virtual address to get physical address for | |
8064 | * @access_type: 0 for read, 1 for write, 2 for execute | |
d3649702 | 8065 | * @mmu_idx: MMU index indicating required translation regime |
702a9357 | 8066 | * @phys_ptr: set to the physical address corresponding to the virtual address |
8bf5b6a9 | 8067 | * @attrs: set to the memory transaction attributes to use |
702a9357 PM |
8068 | * @prot: set to the permissions for the page containing phys_ptr |
8069 | * @page_size: set to the size of the page containing phys_ptr | |
b7cc4e82 | 8070 | * @fsr: set to the DFSR/IFSR value on failure |
702a9357 | 8071 | */ |
af51f566 EI |
8072 | static bool get_phys_addr(CPUARMState *env, target_ulong address, |
8073 | int access_type, ARMMMUIdx mmu_idx, | |
8074 | hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, | |
e14b5a23 EI |
8075 | target_ulong *page_size, uint32_t *fsr, |
8076 | ARMMMUFaultInfo *fi) | |
9ee6e8bb | 8077 | { |
0480f69a | 8078 | if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { |
9b539263 EI |
8079 | /* Call ourselves recursively to do the stage 1 and then stage 2 |
8080 | * translations. | |
0480f69a | 8081 | */ |
9b539263 EI |
8082 | if (arm_feature(env, ARM_FEATURE_EL2)) { |
8083 | hwaddr ipa; | |
8084 | int s2_prot; | |
8085 | int ret; | |
8086 | ||
8087 | ret = get_phys_addr(env, address, access_type, | |
8088 | mmu_idx + ARMMMUIdx_S1NSE0, &ipa, attrs, | |
8089 | prot, page_size, fsr, fi); | |
8090 | ||
8091 | /* If S1 fails or S2 is disabled, return early. */ | |
8092 | if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) { | |
8093 | *phys_ptr = ipa; | |
8094 | return ret; | |
8095 | } | |
8096 | ||
8097 | /* S1 is done. Now do S2 translation. */ | |
8098 | ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS, | |
8099 | phys_ptr, attrs, &s2_prot, | |
8100 | page_size, fsr, fi); | |
8101 | fi->s2addr = ipa; | |
8102 | /* Combine the S1 and S2 perms. */ | |
8103 | *prot &= s2_prot; | |
8104 | return ret; | |
8105 | } else { | |
8106 | /* | |
8107 | * For non-EL2 CPUs a stage1+stage2 translation is just stage 1. | |
8108 | */ | |
8109 | mmu_idx += ARMMMUIdx_S1NSE0; | |
8110 | } | |
0480f69a | 8111 | } |
d3649702 | 8112 | |
8bf5b6a9 PM |
8113 | /* The page table entries may downgrade secure to non-secure, but |
8114 | * cannot upgrade an non-secure translation regime's attributes | |
8115 | * to secure. | |
8116 | */ | |
8117 | attrs->secure = regime_is_secure(env, mmu_idx); | |
0995bf8c | 8118 | attrs->user = regime_is_user(env, mmu_idx); |
8bf5b6a9 | 8119 | |
0480f69a PM |
8120 | /* Fast Context Switch Extension. This doesn't exist at all in v8. |
8121 | * In v7 and earlier it affects all stage 1 translations. | |
8122 | */ | |
8123 | if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS | |
8124 | && !arm_feature(env, ARM_FEATURE_V8)) { | |
8125 | if (regime_el(env, mmu_idx) == 3) { | |
8126 | address += env->cp15.fcseidr_s; | |
8127 | } else { | |
8128 | address += env->cp15.fcseidr_ns; | |
8129 | } | |
54bf36ed | 8130 | } |
9ee6e8bb | 8131 | |
f6bda88f PC |
8132 | /* pmsav7 has special handling for when MPU is disabled so call it before |
8133 | * the common MMU/MPU disabled check below. | |
8134 | */ | |
8135 | if (arm_feature(env, ARM_FEATURE_MPU) && | |
8136 | arm_feature(env, ARM_FEATURE_V7)) { | |
8137 | *page_size = TARGET_PAGE_SIZE; | |
8138 | return get_phys_addr_pmsav7(env, address, access_type, mmu_idx, | |
8139 | phys_ptr, prot, fsr); | |
8140 | } | |
8141 | ||
0480f69a | 8142 | if (regime_translation_disabled(env, mmu_idx)) { |
9ee6e8bb PB |
8143 | /* MMU/MPU disabled. */ |
8144 | *phys_ptr = address; | |
3ad493fc | 8145 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
d4c430a8 | 8146 | *page_size = TARGET_PAGE_SIZE; |
9ee6e8bb | 8147 | return 0; |
0480f69a PM |
8148 | } |
8149 | ||
8150 | if (arm_feature(env, ARM_FEATURE_MPU)) { | |
f6bda88f | 8151 | /* Pre-v7 MPU */ |
d4c430a8 | 8152 | *page_size = TARGET_PAGE_SIZE; |
13689d43 PC |
8153 | return get_phys_addr_pmsav5(env, address, access_type, mmu_idx, |
8154 | phys_ptr, prot, fsr); | |
0480f69a PM |
8155 | } |
8156 | ||
8157 | if (regime_using_lpae_format(env, mmu_idx)) { | |
8158 | return get_phys_addr_lpae(env, address, access_type, mmu_idx, phys_ptr, | |
e14b5a23 | 8159 | attrs, prot, page_size, fsr, fi); |
0480f69a PM |
8160 | } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) { |
8161 | return get_phys_addr_v6(env, address, access_type, mmu_idx, phys_ptr, | |
e14b5a23 | 8162 | attrs, prot, page_size, fsr, fi); |
9ee6e8bb | 8163 | } else { |
0480f69a | 8164 | return get_phys_addr_v5(env, address, access_type, mmu_idx, phys_ptr, |
e14b5a23 | 8165 | prot, page_size, fsr, fi); |
9ee6e8bb PB |
8166 | } |
8167 | } | |
8168 | ||
8c6084bf | 8169 | /* Walk the page table and (if the mapping exists) add the page |
b7cc4e82 PC |
8170 | * to the TLB. Return false on success, or true on failure. Populate |
8171 | * fsr with ARM DFSR/IFSR fault register format value on failure. | |
8c6084bf | 8172 | */ |
b7cc4e82 | 8173 | bool arm_tlb_fill(CPUState *cs, vaddr address, |
e14b5a23 EI |
8174 | int access_type, int mmu_idx, uint32_t *fsr, |
8175 | ARMMMUFaultInfo *fi) | |
b5ff1b31 | 8176 | { |
7510454e AF |
8177 | ARMCPU *cpu = ARM_CPU(cs); |
8178 | CPUARMState *env = &cpu->env; | |
a8170e5e | 8179 | hwaddr phys_addr; |
d4c430a8 | 8180 | target_ulong page_size; |
b5ff1b31 | 8181 | int prot; |
d3649702 | 8182 | int ret; |
8bf5b6a9 | 8183 | MemTxAttrs attrs = {}; |
b5ff1b31 | 8184 | |
8bf5b6a9 | 8185 | ret = get_phys_addr(env, address, access_type, mmu_idx, &phys_addr, |
e14b5a23 | 8186 | &attrs, &prot, &page_size, fsr, fi); |
b7cc4e82 | 8187 | if (!ret) { |
b5ff1b31 | 8188 | /* Map a single [sub]page. */ |
dcd82c11 AB |
8189 | phys_addr &= TARGET_PAGE_MASK; |
8190 | address &= TARGET_PAGE_MASK; | |
8bf5b6a9 PM |
8191 | tlb_set_page_with_attrs(cs, address, phys_addr, attrs, |
8192 | prot, mmu_idx, page_size); | |
d4c430a8 | 8193 | return 0; |
b5ff1b31 FB |
8194 | } |
8195 | ||
8c6084bf | 8196 | return ret; |
b5ff1b31 FB |
8197 | } |
8198 | ||
0faea0c7 PM |
8199 | hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr, |
8200 | MemTxAttrs *attrs) | |
b5ff1b31 | 8201 | { |
00b941e5 | 8202 | ARMCPU *cpu = ARM_CPU(cs); |
d3649702 | 8203 | CPUARMState *env = &cpu->env; |
a8170e5e | 8204 | hwaddr phys_addr; |
d4c430a8 | 8205 | target_ulong page_size; |
b5ff1b31 | 8206 | int prot; |
b7cc4e82 PC |
8207 | bool ret; |
8208 | uint32_t fsr; | |
e14b5a23 | 8209 | ARMMMUFaultInfo fi = {}; |
b5ff1b31 | 8210 | |
0faea0c7 PM |
8211 | *attrs = (MemTxAttrs) {}; |
8212 | ||
97ed5ccd | 8213 | ret = get_phys_addr(env, addr, 0, cpu_mmu_index(env, false), &phys_addr, |
0faea0c7 | 8214 | attrs, &prot, &page_size, &fsr, &fi); |
b5ff1b31 | 8215 | |
b7cc4e82 | 8216 | if (ret) { |
b5ff1b31 | 8217 | return -1; |
00b941e5 | 8218 | } |
b5ff1b31 FB |
8219 | return phys_addr; |
8220 | } | |
8221 | ||
0ecb72a5 | 8222 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) |
9ee6e8bb | 8223 | { |
a47dddd7 AF |
8224 | ARMCPU *cpu = arm_env_get_cpu(env); |
8225 | ||
9ee6e8bb PB |
8226 | switch (reg) { |
8227 | case 0: /* APSR */ | |
8228 | return xpsr_read(env) & 0xf8000000; | |
8229 | case 1: /* IAPSR */ | |
8230 | return xpsr_read(env) & 0xf80001ff; | |
8231 | case 2: /* EAPSR */ | |
8232 | return xpsr_read(env) & 0xff00fc00; | |
8233 | case 3: /* xPSR */ | |
8234 | return xpsr_read(env) & 0xff00fdff; | |
8235 | case 5: /* IPSR */ | |
8236 | return xpsr_read(env) & 0x000001ff; | |
8237 | case 6: /* EPSR */ | |
8238 | return xpsr_read(env) & 0x0700fc00; | |
8239 | case 7: /* IEPSR */ | |
8240 | return xpsr_read(env) & 0x0700edff; | |
8241 | case 8: /* MSP */ | |
8242 | return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13]; | |
8243 | case 9: /* PSP */ | |
8244 | return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp; | |
8245 | case 16: /* PRIMASK */ | |
4cc35614 | 8246 | return (env->daif & PSTATE_I) != 0; |
82845826 SH |
8247 | case 17: /* BASEPRI */ |
8248 | case 18: /* BASEPRI_MAX */ | |
9ee6e8bb | 8249 | return env->v7m.basepri; |
82845826 | 8250 | case 19: /* FAULTMASK */ |
4cc35614 | 8251 | return (env->daif & PSTATE_F) != 0; |
9ee6e8bb PB |
8252 | case 20: /* CONTROL */ |
8253 | return env->v7m.control; | |
8254 | default: | |
8255 | /* ??? For debugging only. */ | |
a47dddd7 | 8256 | cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg); |
9ee6e8bb PB |
8257 | return 0; |
8258 | } | |
8259 | } | |
8260 | ||
0ecb72a5 | 8261 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) |
9ee6e8bb | 8262 | { |
a47dddd7 AF |
8263 | ARMCPU *cpu = arm_env_get_cpu(env); |
8264 | ||
9ee6e8bb PB |
8265 | switch (reg) { |
8266 | case 0: /* APSR */ | |
8267 | xpsr_write(env, val, 0xf8000000); | |
8268 | break; | |
8269 | case 1: /* IAPSR */ | |
8270 | xpsr_write(env, val, 0xf8000000); | |
8271 | break; | |
8272 | case 2: /* EAPSR */ | |
8273 | xpsr_write(env, val, 0xfe00fc00); | |
8274 | break; | |
8275 | case 3: /* xPSR */ | |
8276 | xpsr_write(env, val, 0xfe00fc00); | |
8277 | break; | |
8278 | case 5: /* IPSR */ | |
8279 | /* IPSR bits are readonly. */ | |
8280 | break; | |
8281 | case 6: /* EPSR */ | |
8282 | xpsr_write(env, val, 0x0600fc00); | |
8283 | break; | |
8284 | case 7: /* IEPSR */ | |
8285 | xpsr_write(env, val, 0x0600fc00); | |
8286 | break; | |
8287 | case 8: /* MSP */ | |
8288 | if (env->v7m.current_sp) | |
8289 | env->v7m.other_sp = val; | |
8290 | else | |
8291 | env->regs[13] = val; | |
8292 | break; | |
8293 | case 9: /* PSP */ | |
8294 | if (env->v7m.current_sp) | |
8295 | env->regs[13] = val; | |
8296 | else | |
8297 | env->v7m.other_sp = val; | |
8298 | break; | |
8299 | case 16: /* PRIMASK */ | |
4cc35614 PM |
8300 | if (val & 1) { |
8301 | env->daif |= PSTATE_I; | |
8302 | } else { | |
8303 | env->daif &= ~PSTATE_I; | |
8304 | } | |
9ee6e8bb | 8305 | break; |
82845826 | 8306 | case 17: /* BASEPRI */ |
9ee6e8bb PB |
8307 | env->v7m.basepri = val & 0xff; |
8308 | break; | |
82845826 | 8309 | case 18: /* BASEPRI_MAX */ |
9ee6e8bb PB |
8310 | val &= 0xff; |
8311 | if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0)) | |
8312 | env->v7m.basepri = val; | |
8313 | break; | |
82845826 | 8314 | case 19: /* FAULTMASK */ |
4cc35614 PM |
8315 | if (val & 1) { |
8316 | env->daif |= PSTATE_F; | |
8317 | } else { | |
8318 | env->daif &= ~PSTATE_F; | |
8319 | } | |
82845826 | 8320 | break; |
9ee6e8bb PB |
8321 | case 20: /* CONTROL */ |
8322 | env->v7m.control = val & 3; | |
8323 | switch_v7m_sp(env, (val & 2) != 0); | |
8324 | break; | |
8325 | default: | |
8326 | /* ??? For debugging only. */ | |
a47dddd7 | 8327 | cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg); |
9ee6e8bb PB |
8328 | return; |
8329 | } | |
8330 | } | |
8331 | ||
b5ff1b31 | 8332 | #endif |
6ddbc6e4 | 8333 | |
aca3f40b PM |
8334 | void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in) |
8335 | { | |
8336 | /* Implement DC ZVA, which zeroes a fixed-length block of memory. | |
8337 | * Note that we do not implement the (architecturally mandated) | |
8338 | * alignment fault for attempts to use this on Device memory | |
8339 | * (which matches the usual QEMU behaviour of not implementing either | |
8340 | * alignment faults or any memory attribute handling). | |
8341 | */ | |
8342 | ||
8343 | ARMCPU *cpu = arm_env_get_cpu(env); | |
8344 | uint64_t blocklen = 4 << cpu->dcz_blocksize; | |
8345 | uint64_t vaddr = vaddr_in & ~(blocklen - 1); | |
8346 | ||
8347 | #ifndef CONFIG_USER_ONLY | |
8348 | { | |
8349 | /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than | |
8350 | * the block size so we might have to do more than one TLB lookup. | |
8351 | * We know that in fact for any v8 CPU the page size is at least 4K | |
8352 | * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only | |
8353 | * 1K as an artefact of legacy v5 subpage support being present in the | |
8354 | * same QEMU executable. | |
8355 | */ | |
8356 | int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE); | |
8357 | void *hostaddr[maxidx]; | |
8358 | int try, i; | |
97ed5ccd | 8359 | unsigned mmu_idx = cpu_mmu_index(env, false); |
3972ef6f | 8360 | TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx); |
aca3f40b PM |
8361 | |
8362 | for (try = 0; try < 2; try++) { | |
8363 | ||
8364 | for (i = 0; i < maxidx; i++) { | |
8365 | hostaddr[i] = tlb_vaddr_to_host(env, | |
8366 | vaddr + TARGET_PAGE_SIZE * i, | |
3972ef6f | 8367 | 1, mmu_idx); |
aca3f40b PM |
8368 | if (!hostaddr[i]) { |
8369 | break; | |
8370 | } | |
8371 | } | |
8372 | if (i == maxidx) { | |
8373 | /* If it's all in the TLB it's fair game for just writing to; | |
8374 | * we know we don't need to update dirty status, etc. | |
8375 | */ | |
8376 | for (i = 0; i < maxidx - 1; i++) { | |
8377 | memset(hostaddr[i], 0, TARGET_PAGE_SIZE); | |
8378 | } | |
8379 | memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE)); | |
8380 | return; | |
8381 | } | |
8382 | /* OK, try a store and see if we can populate the tlb. This | |
8383 | * might cause an exception if the memory isn't writable, | |
8384 | * in which case we will longjmp out of here. We must for | |
8385 | * this purpose use the actual register value passed to us | |
8386 | * so that we get the fault address right. | |
8387 | */ | |
01ecaf43 | 8388 | helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC()); |
aca3f40b PM |
8389 | /* Now we can populate the other TLB entries, if any */ |
8390 | for (i = 0; i < maxidx; i++) { | |
8391 | uint64_t va = vaddr + TARGET_PAGE_SIZE * i; | |
8392 | if (va != (vaddr_in & TARGET_PAGE_MASK)) { | |
01ecaf43 | 8393 | helper_ret_stb_mmu(env, va, 0, oi, GETPC()); |
aca3f40b PM |
8394 | } |
8395 | } | |
8396 | } | |
8397 | ||
8398 | /* Slow path (probably attempt to do this to an I/O device or | |
8399 | * similar, or clearing of a block of code we have translations | |
8400 | * cached for). Just do a series of byte writes as the architecture | |
8401 | * demands. It's not worth trying to use a cpu_physical_memory_map(), | |
8402 | * memset(), unmap() sequence here because: | |
8403 | * + we'd need to account for the blocksize being larger than a page | |
8404 | * + the direct-RAM access case is almost always going to be dealt | |
8405 | * with in the fastpath code above, so there's no speed benefit | |
8406 | * + we would have to deal with the map returning NULL because the | |
8407 | * bounce buffer was in use | |
8408 | */ | |
8409 | for (i = 0; i < blocklen; i++) { | |
01ecaf43 | 8410 | helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC()); |
aca3f40b PM |
8411 | } |
8412 | } | |
8413 | #else | |
8414 | memset(g2h(vaddr), 0, blocklen); | |
8415 | #endif | |
8416 | } | |
8417 | ||
6ddbc6e4 PB |
8418 | /* Note that signed overflow is undefined in C. The following routines are |
8419 | careful to use unsigned types where modulo arithmetic is required. | |
8420 | Failure to do so _will_ break on newer gcc. */ | |
8421 | ||
8422 | /* Signed saturating arithmetic. */ | |
8423 | ||
1654b2d6 | 8424 | /* Perform 16-bit signed saturating addition. */ |
6ddbc6e4 PB |
8425 | static inline uint16_t add16_sat(uint16_t a, uint16_t b) |
8426 | { | |
8427 | uint16_t res; | |
8428 | ||
8429 | res = a + b; | |
8430 | if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { | |
8431 | if (a & 0x8000) | |
8432 | res = 0x8000; | |
8433 | else | |
8434 | res = 0x7fff; | |
8435 | } | |
8436 | return res; | |
8437 | } | |
8438 | ||
1654b2d6 | 8439 | /* Perform 8-bit signed saturating addition. */ |
6ddbc6e4 PB |
8440 | static inline uint8_t add8_sat(uint8_t a, uint8_t b) |
8441 | { | |
8442 | uint8_t res; | |
8443 | ||
8444 | res = a + b; | |
8445 | if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { | |
8446 | if (a & 0x80) | |
8447 | res = 0x80; | |
8448 | else | |
8449 | res = 0x7f; | |
8450 | } | |
8451 | return res; | |
8452 | } | |
8453 | ||
1654b2d6 | 8454 | /* Perform 16-bit signed saturating subtraction. */ |
6ddbc6e4 PB |
8455 | static inline uint16_t sub16_sat(uint16_t a, uint16_t b) |
8456 | { | |
8457 | uint16_t res; | |
8458 | ||
8459 | res = a - b; | |
8460 | if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { | |
8461 | if (a & 0x8000) | |
8462 | res = 0x8000; | |
8463 | else | |
8464 | res = 0x7fff; | |
8465 | } | |
8466 | return res; | |
8467 | } | |
8468 | ||
1654b2d6 | 8469 | /* Perform 8-bit signed saturating subtraction. */ |
6ddbc6e4 PB |
8470 | static inline uint8_t sub8_sat(uint8_t a, uint8_t b) |
8471 | { | |
8472 | uint8_t res; | |
8473 | ||
8474 | res = a - b; | |
8475 | if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { | |
8476 | if (a & 0x80) | |
8477 | res = 0x80; | |
8478 | else | |
8479 | res = 0x7f; | |
8480 | } | |
8481 | return res; | |
8482 | } | |
8483 | ||
8484 | #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); | |
8485 | #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); | |
8486 | #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); | |
8487 | #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); | |
8488 | #define PFX q | |
8489 | ||
8490 | #include "op_addsub.h" | |
8491 | ||
8492 | /* Unsigned saturating arithmetic. */ | |
460a09c1 | 8493 | static inline uint16_t add16_usat(uint16_t a, uint16_t b) |
6ddbc6e4 PB |
8494 | { |
8495 | uint16_t res; | |
8496 | res = a + b; | |
8497 | if (res < a) | |
8498 | res = 0xffff; | |
8499 | return res; | |
8500 | } | |
8501 | ||
460a09c1 | 8502 | static inline uint16_t sub16_usat(uint16_t a, uint16_t b) |
6ddbc6e4 | 8503 | { |
4c4fd3f8 | 8504 | if (a > b) |
6ddbc6e4 PB |
8505 | return a - b; |
8506 | else | |
8507 | return 0; | |
8508 | } | |
8509 | ||
8510 | static inline uint8_t add8_usat(uint8_t a, uint8_t b) | |
8511 | { | |
8512 | uint8_t res; | |
8513 | res = a + b; | |
8514 | if (res < a) | |
8515 | res = 0xff; | |
8516 | return res; | |
8517 | } | |
8518 | ||
8519 | static inline uint8_t sub8_usat(uint8_t a, uint8_t b) | |
8520 | { | |
4c4fd3f8 | 8521 | if (a > b) |
6ddbc6e4 PB |
8522 | return a - b; |
8523 | else | |
8524 | return 0; | |
8525 | } | |
8526 | ||
8527 | #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); | |
8528 | #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); | |
8529 | #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); | |
8530 | #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); | |
8531 | #define PFX uq | |
8532 | ||
8533 | #include "op_addsub.h" | |
8534 | ||
8535 | /* Signed modulo arithmetic. */ | |
8536 | #define SARITH16(a, b, n, op) do { \ | |
8537 | int32_t sum; \ | |
db6e2e65 | 8538 | sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ |
6ddbc6e4 PB |
8539 | RESULT(sum, n, 16); \ |
8540 | if (sum >= 0) \ | |
8541 | ge |= 3 << (n * 2); \ | |
8542 | } while(0) | |
8543 | ||
8544 | #define SARITH8(a, b, n, op) do { \ | |
8545 | int32_t sum; \ | |
db6e2e65 | 8546 | sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ |
6ddbc6e4 PB |
8547 | RESULT(sum, n, 8); \ |
8548 | if (sum >= 0) \ | |
8549 | ge |= 1 << n; \ | |
8550 | } while(0) | |
8551 | ||
8552 | ||
8553 | #define ADD16(a, b, n) SARITH16(a, b, n, +) | |
8554 | #define SUB16(a, b, n) SARITH16(a, b, n, -) | |
8555 | #define ADD8(a, b, n) SARITH8(a, b, n, +) | |
8556 | #define SUB8(a, b, n) SARITH8(a, b, n, -) | |
8557 | #define PFX s | |
8558 | #define ARITH_GE | |
8559 | ||
8560 | #include "op_addsub.h" | |
8561 | ||
8562 | /* Unsigned modulo arithmetic. */ | |
8563 | #define ADD16(a, b, n) do { \ | |
8564 | uint32_t sum; \ | |
8565 | sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ | |
8566 | RESULT(sum, n, 16); \ | |
a87aa10b | 8567 | if ((sum >> 16) == 1) \ |
6ddbc6e4 PB |
8568 | ge |= 3 << (n * 2); \ |
8569 | } while(0) | |
8570 | ||
8571 | #define ADD8(a, b, n) do { \ | |
8572 | uint32_t sum; \ | |
8573 | sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ | |
8574 | RESULT(sum, n, 8); \ | |
a87aa10b AZ |
8575 | if ((sum >> 8) == 1) \ |
8576 | ge |= 1 << n; \ | |
6ddbc6e4 PB |
8577 | } while(0) |
8578 | ||
8579 | #define SUB16(a, b, n) do { \ | |
8580 | uint32_t sum; \ | |
8581 | sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ | |
8582 | RESULT(sum, n, 16); \ | |
8583 | if ((sum >> 16) == 0) \ | |
8584 | ge |= 3 << (n * 2); \ | |
8585 | } while(0) | |
8586 | ||
8587 | #define SUB8(a, b, n) do { \ | |
8588 | uint32_t sum; \ | |
8589 | sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ | |
8590 | RESULT(sum, n, 8); \ | |
8591 | if ((sum >> 8) == 0) \ | |
a87aa10b | 8592 | ge |= 1 << n; \ |
6ddbc6e4 PB |
8593 | } while(0) |
8594 | ||
8595 | #define PFX u | |
8596 | #define ARITH_GE | |
8597 | ||
8598 | #include "op_addsub.h" | |
8599 | ||
8600 | /* Halved signed arithmetic. */ | |
8601 | #define ADD16(a, b, n) \ | |
8602 | RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) | |
8603 | #define SUB16(a, b, n) \ | |
8604 | RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) | |
8605 | #define ADD8(a, b, n) \ | |
8606 | RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) | |
8607 | #define SUB8(a, b, n) \ | |
8608 | RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) | |
8609 | #define PFX sh | |
8610 | ||
8611 | #include "op_addsub.h" | |
8612 | ||
8613 | /* Halved unsigned arithmetic. */ | |
8614 | #define ADD16(a, b, n) \ | |
8615 | RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) | |
8616 | #define SUB16(a, b, n) \ | |
8617 | RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) | |
8618 | #define ADD8(a, b, n) \ | |
8619 | RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) | |
8620 | #define SUB8(a, b, n) \ | |
8621 | RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) | |
8622 | #define PFX uh | |
8623 | ||
8624 | #include "op_addsub.h" | |
8625 | ||
8626 | static inline uint8_t do_usad(uint8_t a, uint8_t b) | |
8627 | { | |
8628 | if (a > b) | |
8629 | return a - b; | |
8630 | else | |
8631 | return b - a; | |
8632 | } | |
8633 | ||
8634 | /* Unsigned sum of absolute byte differences. */ | |
8635 | uint32_t HELPER(usad8)(uint32_t a, uint32_t b) | |
8636 | { | |
8637 | uint32_t sum; | |
8638 | sum = do_usad(a, b); | |
8639 | sum += do_usad(a >> 8, b >> 8); | |
8640 | sum += do_usad(a >> 16, b >>16); | |
8641 | sum += do_usad(a >> 24, b >> 24); | |
8642 | return sum; | |
8643 | } | |
8644 | ||
8645 | /* For ARMv6 SEL instruction. */ | |
8646 | uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) | |
8647 | { | |
8648 | uint32_t mask; | |
8649 | ||
8650 | mask = 0; | |
8651 | if (flags & 1) | |
8652 | mask |= 0xff; | |
8653 | if (flags & 2) | |
8654 | mask |= 0xff00; | |
8655 | if (flags & 4) | |
8656 | mask |= 0xff0000; | |
8657 | if (flags & 8) | |
8658 | mask |= 0xff000000; | |
8659 | return (a & mask) | (b & ~mask); | |
8660 | } | |
8661 | ||
b90372ad PM |
8662 | /* VFP support. We follow the convention used for VFP instructions: |
8663 | Single precision routines have a "s" suffix, double precision a | |
4373f3ce PB |
8664 | "d" suffix. */ |
8665 | ||
8666 | /* Convert host exception flags to vfp form. */ | |
8667 | static inline int vfp_exceptbits_from_host(int host_bits) | |
8668 | { | |
8669 | int target_bits = 0; | |
8670 | ||
8671 | if (host_bits & float_flag_invalid) | |
8672 | target_bits |= 1; | |
8673 | if (host_bits & float_flag_divbyzero) | |
8674 | target_bits |= 2; | |
8675 | if (host_bits & float_flag_overflow) | |
8676 | target_bits |= 4; | |
36802b6b | 8677 | if (host_bits & (float_flag_underflow | float_flag_output_denormal)) |
4373f3ce PB |
8678 | target_bits |= 8; |
8679 | if (host_bits & float_flag_inexact) | |
8680 | target_bits |= 0x10; | |
cecd8504 PM |
8681 | if (host_bits & float_flag_input_denormal) |
8682 | target_bits |= 0x80; | |
4373f3ce PB |
8683 | return target_bits; |
8684 | } | |
8685 | ||
0ecb72a5 | 8686 | uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env) |
4373f3ce PB |
8687 | { |
8688 | int i; | |
8689 | uint32_t fpscr; | |
8690 | ||
8691 | fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff) | |
8692 | | (env->vfp.vec_len << 16) | |
8693 | | (env->vfp.vec_stride << 20); | |
8694 | i = get_float_exception_flags(&env->vfp.fp_status); | |
3a492f3a | 8695 | i |= get_float_exception_flags(&env->vfp.standard_fp_status); |
4373f3ce PB |
8696 | fpscr |= vfp_exceptbits_from_host(i); |
8697 | return fpscr; | |
8698 | } | |
8699 | ||
0ecb72a5 | 8700 | uint32_t vfp_get_fpscr(CPUARMState *env) |
01653295 PM |
8701 | { |
8702 | return HELPER(vfp_get_fpscr)(env); | |
8703 | } | |
8704 | ||
4373f3ce PB |
8705 | /* Convert vfp exception flags to target form. */ |
8706 | static inline int vfp_exceptbits_to_host(int target_bits) | |
8707 | { | |
8708 | int host_bits = 0; | |
8709 | ||
8710 | if (target_bits & 1) | |
8711 | host_bits |= float_flag_invalid; | |
8712 | if (target_bits & 2) | |
8713 | host_bits |= float_flag_divbyzero; | |
8714 | if (target_bits & 4) | |
8715 | host_bits |= float_flag_overflow; | |
8716 | if (target_bits & 8) | |
8717 | host_bits |= float_flag_underflow; | |
8718 | if (target_bits & 0x10) | |
8719 | host_bits |= float_flag_inexact; | |
cecd8504 PM |
8720 | if (target_bits & 0x80) |
8721 | host_bits |= float_flag_input_denormal; | |
4373f3ce PB |
8722 | return host_bits; |
8723 | } | |
8724 | ||
0ecb72a5 | 8725 | void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val) |
4373f3ce PB |
8726 | { |
8727 | int i; | |
8728 | uint32_t changed; | |
8729 | ||
8730 | changed = env->vfp.xregs[ARM_VFP_FPSCR]; | |
8731 | env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff); | |
8732 | env->vfp.vec_len = (val >> 16) & 7; | |
8733 | env->vfp.vec_stride = (val >> 20) & 3; | |
8734 | ||
8735 | changed ^= val; | |
8736 | if (changed & (3 << 22)) { | |
8737 | i = (val >> 22) & 3; | |
8738 | switch (i) { | |
4d3da0f3 | 8739 | case FPROUNDING_TIEEVEN: |
4373f3ce PB |
8740 | i = float_round_nearest_even; |
8741 | break; | |
4d3da0f3 | 8742 | case FPROUNDING_POSINF: |
4373f3ce PB |
8743 | i = float_round_up; |
8744 | break; | |
4d3da0f3 | 8745 | case FPROUNDING_NEGINF: |
4373f3ce PB |
8746 | i = float_round_down; |
8747 | break; | |
4d3da0f3 | 8748 | case FPROUNDING_ZERO: |
4373f3ce PB |
8749 | i = float_round_to_zero; |
8750 | break; | |
8751 | } | |
8752 | set_float_rounding_mode(i, &env->vfp.fp_status); | |
8753 | } | |
cecd8504 | 8754 | if (changed & (1 << 24)) { |
fe76d976 | 8755 | set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status); |
cecd8504 PM |
8756 | set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status); |
8757 | } | |
5c7908ed PB |
8758 | if (changed & (1 << 25)) |
8759 | set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status); | |
4373f3ce | 8760 | |
b12c390b | 8761 | i = vfp_exceptbits_to_host(val); |
4373f3ce | 8762 | set_float_exception_flags(i, &env->vfp.fp_status); |
3a492f3a | 8763 | set_float_exception_flags(0, &env->vfp.standard_fp_status); |
4373f3ce PB |
8764 | } |
8765 | ||
0ecb72a5 | 8766 | void vfp_set_fpscr(CPUARMState *env, uint32_t val) |
01653295 PM |
8767 | { |
8768 | HELPER(vfp_set_fpscr)(env, val); | |
8769 | } | |
8770 | ||
4373f3ce PB |
8771 | #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p)) |
8772 | ||
8773 | #define VFP_BINOP(name) \ | |
ae1857ec | 8774 | float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \ |
4373f3ce | 8775 | { \ |
ae1857ec PM |
8776 | float_status *fpst = fpstp; \ |
8777 | return float32_ ## name(a, b, fpst); \ | |
4373f3ce | 8778 | } \ |
ae1857ec | 8779 | float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \ |
4373f3ce | 8780 | { \ |
ae1857ec PM |
8781 | float_status *fpst = fpstp; \ |
8782 | return float64_ ## name(a, b, fpst); \ | |
4373f3ce PB |
8783 | } |
8784 | VFP_BINOP(add) | |
8785 | VFP_BINOP(sub) | |
8786 | VFP_BINOP(mul) | |
8787 | VFP_BINOP(div) | |
f71a2ae5 PM |
8788 | VFP_BINOP(min) |
8789 | VFP_BINOP(max) | |
8790 | VFP_BINOP(minnum) | |
8791 | VFP_BINOP(maxnum) | |
4373f3ce PB |
8792 | #undef VFP_BINOP |
8793 | ||
8794 | float32 VFP_HELPER(neg, s)(float32 a) | |
8795 | { | |
8796 | return float32_chs(a); | |
8797 | } | |
8798 | ||
8799 | float64 VFP_HELPER(neg, d)(float64 a) | |
8800 | { | |
66230e0d | 8801 | return float64_chs(a); |
4373f3ce PB |
8802 | } |
8803 | ||
8804 | float32 VFP_HELPER(abs, s)(float32 a) | |
8805 | { | |
8806 | return float32_abs(a); | |
8807 | } | |
8808 | ||
8809 | float64 VFP_HELPER(abs, d)(float64 a) | |
8810 | { | |
66230e0d | 8811 | return float64_abs(a); |
4373f3ce PB |
8812 | } |
8813 | ||
0ecb72a5 | 8814 | float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env) |
4373f3ce PB |
8815 | { |
8816 | return float32_sqrt(a, &env->vfp.fp_status); | |
8817 | } | |
8818 | ||
0ecb72a5 | 8819 | float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env) |
4373f3ce PB |
8820 | { |
8821 | return float64_sqrt(a, &env->vfp.fp_status); | |
8822 | } | |
8823 | ||
8824 | /* XXX: check quiet/signaling case */ | |
8825 | #define DO_VFP_cmp(p, type) \ | |
0ecb72a5 | 8826 | void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \ |
4373f3ce PB |
8827 | { \ |
8828 | uint32_t flags; \ | |
8829 | switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \ | |
8830 | case 0: flags = 0x6; break; \ | |
8831 | case -1: flags = 0x8; break; \ | |
8832 | case 1: flags = 0x2; break; \ | |
8833 | default: case 2: flags = 0x3; break; \ | |
8834 | } \ | |
8835 | env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ | |
8836 | | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ | |
8837 | } \ | |
0ecb72a5 | 8838 | void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \ |
4373f3ce PB |
8839 | { \ |
8840 | uint32_t flags; \ | |
8841 | switch(type ## _compare(a, b, &env->vfp.fp_status)) { \ | |
8842 | case 0: flags = 0x6; break; \ | |
8843 | case -1: flags = 0x8; break; \ | |
8844 | case 1: flags = 0x2; break; \ | |
8845 | default: case 2: flags = 0x3; break; \ | |
8846 | } \ | |
8847 | env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ | |
8848 | | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ | |
8849 | } | |
8850 | DO_VFP_cmp(s, float32) | |
8851 | DO_VFP_cmp(d, float64) | |
8852 | #undef DO_VFP_cmp | |
8853 | ||
5500b06c | 8854 | /* Integer to float and float to integer conversions */ |
4373f3ce | 8855 | |
5500b06c PM |
8856 | #define CONV_ITOF(name, fsz, sign) \ |
8857 | float##fsz HELPER(name)(uint32_t x, void *fpstp) \ | |
8858 | { \ | |
8859 | float_status *fpst = fpstp; \ | |
85836979 | 8860 | return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \ |
4373f3ce PB |
8861 | } |
8862 | ||
5500b06c PM |
8863 | #define CONV_FTOI(name, fsz, sign, round) \ |
8864 | uint32_t HELPER(name)(float##fsz x, void *fpstp) \ | |
8865 | { \ | |
8866 | float_status *fpst = fpstp; \ | |
8867 | if (float##fsz##_is_any_nan(x)) { \ | |
8868 | float_raise(float_flag_invalid, fpst); \ | |
8869 | return 0; \ | |
8870 | } \ | |
8871 | return float##fsz##_to_##sign##int32##round(x, fpst); \ | |
4373f3ce PB |
8872 | } |
8873 | ||
5500b06c PM |
8874 | #define FLOAT_CONVS(name, p, fsz, sign) \ |
8875 | CONV_ITOF(vfp_##name##to##p, fsz, sign) \ | |
8876 | CONV_FTOI(vfp_to##name##p, fsz, sign, ) \ | |
8877 | CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero) | |
4373f3ce | 8878 | |
5500b06c PM |
8879 | FLOAT_CONVS(si, s, 32, ) |
8880 | FLOAT_CONVS(si, d, 64, ) | |
8881 | FLOAT_CONVS(ui, s, 32, u) | |
8882 | FLOAT_CONVS(ui, d, 64, u) | |
4373f3ce | 8883 | |
5500b06c PM |
8884 | #undef CONV_ITOF |
8885 | #undef CONV_FTOI | |
8886 | #undef FLOAT_CONVS | |
4373f3ce PB |
8887 | |
8888 | /* floating point conversion */ | |
0ecb72a5 | 8889 | float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env) |
4373f3ce | 8890 | { |
2d627737 PM |
8891 | float64 r = float32_to_float64(x, &env->vfp.fp_status); |
8892 | /* ARM requires that S<->D conversion of any kind of NaN generates | |
8893 | * a quiet NaN by forcing the most significant frac bit to 1. | |
8894 | */ | |
af39bc8c | 8895 | return float64_maybe_silence_nan(r, &env->vfp.fp_status); |
4373f3ce PB |
8896 | } |
8897 | ||
0ecb72a5 | 8898 | float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env) |
4373f3ce | 8899 | { |
2d627737 PM |
8900 | float32 r = float64_to_float32(x, &env->vfp.fp_status); |
8901 | /* ARM requires that S<->D conversion of any kind of NaN generates | |
8902 | * a quiet NaN by forcing the most significant frac bit to 1. | |
8903 | */ | |
af39bc8c | 8904 | return float32_maybe_silence_nan(r, &env->vfp.fp_status); |
4373f3ce PB |
8905 | } |
8906 | ||
8907 | /* VFP3 fixed point conversion. */ | |
16d5b3ca | 8908 | #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ |
8ed697e8 WN |
8909 | float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \ |
8910 | void *fpstp) \ | |
4373f3ce | 8911 | { \ |
5500b06c | 8912 | float_status *fpst = fpstp; \ |
622465e1 | 8913 | float##fsz tmp; \ |
8ed697e8 | 8914 | tmp = itype##_to_##float##fsz(x, fpst); \ |
5500b06c | 8915 | return float##fsz##_scalbn(tmp, -(int)shift, fpst); \ |
16d5b3ca WN |
8916 | } |
8917 | ||
abe66f70 PM |
8918 | /* Notice that we want only input-denormal exception flags from the |
8919 | * scalbn operation: the other possible flags (overflow+inexact if | |
8920 | * we overflow to infinity, output-denormal) aren't correct for the | |
8921 | * complete scale-and-convert operation. | |
8922 | */ | |
16d5b3ca WN |
8923 | #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \ |
8924 | uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \ | |
8925 | uint32_t shift, \ | |
8926 | void *fpstp) \ | |
4373f3ce | 8927 | { \ |
5500b06c | 8928 | float_status *fpst = fpstp; \ |
abe66f70 | 8929 | int old_exc_flags = get_float_exception_flags(fpst); \ |
622465e1 PM |
8930 | float##fsz tmp; \ |
8931 | if (float##fsz##_is_any_nan(x)) { \ | |
5500b06c | 8932 | float_raise(float_flag_invalid, fpst); \ |
622465e1 | 8933 | return 0; \ |
09d9487f | 8934 | } \ |
5500b06c | 8935 | tmp = float##fsz##_scalbn(x, shift, fpst); \ |
abe66f70 PM |
8936 | old_exc_flags |= get_float_exception_flags(fpst) \ |
8937 | & float_flag_input_denormal; \ | |
8938 | set_float_exception_flags(old_exc_flags, fpst); \ | |
16d5b3ca | 8939 | return float##fsz##_to_##itype##round(tmp, fpst); \ |
622465e1 PM |
8940 | } |
8941 | ||
16d5b3ca WN |
8942 | #define VFP_CONV_FIX(name, p, fsz, isz, itype) \ |
8943 | VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ | |
3c6a074a WN |
8944 | VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \ |
8945 | VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ) | |
8946 | ||
8947 | #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \ | |
8948 | VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ | |
8949 | VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ) | |
16d5b3ca | 8950 | |
8ed697e8 WN |
8951 | VFP_CONV_FIX(sh, d, 64, 64, int16) |
8952 | VFP_CONV_FIX(sl, d, 64, 64, int32) | |
3c6a074a | 8953 | VFP_CONV_FIX_A64(sq, d, 64, 64, int64) |
8ed697e8 WN |
8954 | VFP_CONV_FIX(uh, d, 64, 64, uint16) |
8955 | VFP_CONV_FIX(ul, d, 64, 64, uint32) | |
3c6a074a | 8956 | VFP_CONV_FIX_A64(uq, d, 64, 64, uint64) |
8ed697e8 WN |
8957 | VFP_CONV_FIX(sh, s, 32, 32, int16) |
8958 | VFP_CONV_FIX(sl, s, 32, 32, int32) | |
3c6a074a | 8959 | VFP_CONV_FIX_A64(sq, s, 32, 64, int64) |
8ed697e8 WN |
8960 | VFP_CONV_FIX(uh, s, 32, 32, uint16) |
8961 | VFP_CONV_FIX(ul, s, 32, 32, uint32) | |
3c6a074a | 8962 | VFP_CONV_FIX_A64(uq, s, 32, 64, uint64) |
4373f3ce | 8963 | #undef VFP_CONV_FIX |
16d5b3ca WN |
8964 | #undef VFP_CONV_FIX_FLOAT |
8965 | #undef VFP_CONV_FLOAT_FIX_ROUND | |
4373f3ce | 8966 | |
52a1f6a3 AG |
8967 | /* Set the current fp rounding mode and return the old one. |
8968 | * The argument is a softfloat float_round_ value. | |
8969 | */ | |
8970 | uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env) | |
8971 | { | |
8972 | float_status *fp_status = &env->vfp.fp_status; | |
8973 | ||
8974 | uint32_t prev_rmode = get_float_rounding_mode(fp_status); | |
8975 | set_float_rounding_mode(rmode, fp_status); | |
8976 | ||
8977 | return prev_rmode; | |
8978 | } | |
8979 | ||
43630e58 WN |
8980 | /* Set the current fp rounding mode in the standard fp status and return |
8981 | * the old one. This is for NEON instructions that need to change the | |
8982 | * rounding mode but wish to use the standard FPSCR values for everything | |
8983 | * else. Always set the rounding mode back to the correct value after | |
8984 | * modifying it. | |
8985 | * The argument is a softfloat float_round_ value. | |
8986 | */ | |
8987 | uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env) | |
8988 | { | |
8989 | float_status *fp_status = &env->vfp.standard_fp_status; | |
8990 | ||
8991 | uint32_t prev_rmode = get_float_rounding_mode(fp_status); | |
8992 | set_float_rounding_mode(rmode, fp_status); | |
8993 | ||
8994 | return prev_rmode; | |
8995 | } | |
8996 | ||
60011498 | 8997 | /* Half precision conversions. */ |
0ecb72a5 | 8998 | static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s) |
60011498 | 8999 | { |
60011498 | 9000 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; |
fb91678d PM |
9001 | float32 r = float16_to_float32(make_float16(a), ieee, s); |
9002 | if (ieee) { | |
af39bc8c | 9003 | return float32_maybe_silence_nan(r, s); |
fb91678d PM |
9004 | } |
9005 | return r; | |
60011498 PB |
9006 | } |
9007 | ||
0ecb72a5 | 9008 | static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s) |
60011498 | 9009 | { |
60011498 | 9010 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; |
fb91678d PM |
9011 | float16 r = float32_to_float16(a, ieee, s); |
9012 | if (ieee) { | |
af39bc8c | 9013 | r = float16_maybe_silence_nan(r, s); |
fb91678d PM |
9014 | } |
9015 | return float16_val(r); | |
60011498 PB |
9016 | } |
9017 | ||
0ecb72a5 | 9018 | float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env) |
2d981da7 PM |
9019 | { |
9020 | return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status); | |
9021 | } | |
9022 | ||
0ecb72a5 | 9023 | uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env) |
2d981da7 PM |
9024 | { |
9025 | return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status); | |
9026 | } | |
9027 | ||
0ecb72a5 | 9028 | float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env) |
2d981da7 PM |
9029 | { |
9030 | return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status); | |
9031 | } | |
9032 | ||
0ecb72a5 | 9033 | uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env) |
2d981da7 PM |
9034 | { |
9035 | return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status); | |
9036 | } | |
9037 | ||
8900aad2 PM |
9038 | float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env) |
9039 | { | |
9040 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; | |
9041 | float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status); | |
9042 | if (ieee) { | |
af39bc8c | 9043 | return float64_maybe_silence_nan(r, &env->vfp.fp_status); |
8900aad2 PM |
9044 | } |
9045 | return r; | |
9046 | } | |
9047 | ||
9048 | uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env) | |
9049 | { | |
9050 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; | |
9051 | float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status); | |
9052 | if (ieee) { | |
af39bc8c | 9053 | r = float16_maybe_silence_nan(r, &env->vfp.fp_status); |
8900aad2 PM |
9054 | } |
9055 | return float16_val(r); | |
9056 | } | |
9057 | ||
dda3ec49 | 9058 | #define float32_two make_float32(0x40000000) |
6aae3df1 PM |
9059 | #define float32_three make_float32(0x40400000) |
9060 | #define float32_one_point_five make_float32(0x3fc00000) | |
dda3ec49 | 9061 | |
0ecb72a5 | 9062 | float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env) |
4373f3ce | 9063 | { |
dda3ec49 PM |
9064 | float_status *s = &env->vfp.standard_fp_status; |
9065 | if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || | |
9066 | (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { | |
43fe9bdb PM |
9067 | if (!(float32_is_zero(a) || float32_is_zero(b))) { |
9068 | float_raise(float_flag_input_denormal, s); | |
9069 | } | |
dda3ec49 PM |
9070 | return float32_two; |
9071 | } | |
9072 | return float32_sub(float32_two, float32_mul(a, b, s), s); | |
4373f3ce PB |
9073 | } |
9074 | ||
0ecb72a5 | 9075 | float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env) |
4373f3ce | 9076 | { |
71826966 | 9077 | float_status *s = &env->vfp.standard_fp_status; |
9ea62f57 PM |
9078 | float32 product; |
9079 | if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || | |
9080 | (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { | |
43fe9bdb PM |
9081 | if (!(float32_is_zero(a) || float32_is_zero(b))) { |
9082 | float_raise(float_flag_input_denormal, s); | |
9083 | } | |
6aae3df1 | 9084 | return float32_one_point_five; |
9ea62f57 | 9085 | } |
6aae3df1 PM |
9086 | product = float32_mul(a, b, s); |
9087 | return float32_div(float32_sub(float32_three, product, s), float32_two, s); | |
4373f3ce PB |
9088 | } |
9089 | ||
8f8e3aa4 PB |
9090 | /* NEON helpers. */ |
9091 | ||
56bf4fe2 CL |
9092 | /* Constants 256 and 512 are used in some helpers; we avoid relying on |
9093 | * int->float conversions at run-time. */ | |
9094 | #define float64_256 make_float64(0x4070000000000000LL) | |
9095 | #define float64_512 make_float64(0x4080000000000000LL) | |
b6d4443a AB |
9096 | #define float32_maxnorm make_float32(0x7f7fffff) |
9097 | #define float64_maxnorm make_float64(0x7fefffffffffffffLL) | |
56bf4fe2 | 9098 | |
b6d4443a AB |
9099 | /* Reciprocal functions |
9100 | * | |
9101 | * The algorithm that must be used to calculate the estimate | |
9102 | * is specified by the ARM ARM, see FPRecipEstimate() | |
fe0e4872 | 9103 | */ |
b6d4443a AB |
9104 | |
9105 | static float64 recip_estimate(float64 a, float_status *real_fp_status) | |
fe0e4872 | 9106 | { |
1146a817 PM |
9107 | /* These calculations mustn't set any fp exception flags, |
9108 | * so we use a local copy of the fp_status. | |
9109 | */ | |
b6d4443a | 9110 | float_status dummy_status = *real_fp_status; |
1146a817 | 9111 | float_status *s = &dummy_status; |
fe0e4872 CL |
9112 | /* q = (int)(a * 512.0) */ |
9113 | float64 q = float64_mul(float64_512, a, s); | |
9114 | int64_t q_int = float64_to_int64_round_to_zero(q, s); | |
9115 | ||
9116 | /* r = 1.0 / (((double)q + 0.5) / 512.0) */ | |
9117 | q = int64_to_float64(q_int, s); | |
9118 | q = float64_add(q, float64_half, s); | |
9119 | q = float64_div(q, float64_512, s); | |
9120 | q = float64_div(float64_one, q, s); | |
9121 | ||
9122 | /* s = (int)(256.0 * r + 0.5) */ | |
9123 | q = float64_mul(q, float64_256, s); | |
9124 | q = float64_add(q, float64_half, s); | |
9125 | q_int = float64_to_int64_round_to_zero(q, s); | |
9126 | ||
9127 | /* return (double)s / 256.0 */ | |
9128 | return float64_div(int64_to_float64(q_int, s), float64_256, s); | |
9129 | } | |
9130 | ||
b6d4443a AB |
9131 | /* Common wrapper to call recip_estimate */ |
9132 | static float64 call_recip_estimate(float64 num, int off, float_status *fpst) | |
4373f3ce | 9133 | { |
b6d4443a AB |
9134 | uint64_t val64 = float64_val(num); |
9135 | uint64_t frac = extract64(val64, 0, 52); | |
9136 | int64_t exp = extract64(val64, 52, 11); | |
9137 | uint64_t sbit; | |
9138 | float64 scaled, estimate; | |
fe0e4872 | 9139 | |
b6d4443a AB |
9140 | /* Generate the scaled number for the estimate function */ |
9141 | if (exp == 0) { | |
9142 | if (extract64(frac, 51, 1) == 0) { | |
9143 | exp = -1; | |
9144 | frac = extract64(frac, 0, 50) << 2; | |
9145 | } else { | |
9146 | frac = extract64(frac, 0, 51) << 1; | |
9147 | } | |
9148 | } | |
fe0e4872 | 9149 | |
b6d4443a AB |
9150 | /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */ |
9151 | scaled = make_float64((0x3feULL << 52) | |
9152 | | extract64(frac, 44, 8) << 44); | |
9153 | ||
9154 | estimate = recip_estimate(scaled, fpst); | |
9155 | ||
9156 | /* Build new result */ | |
9157 | val64 = float64_val(estimate); | |
9158 | sbit = 0x8000000000000000ULL & val64; | |
9159 | exp = off - exp; | |
9160 | frac = extract64(val64, 0, 52); | |
9161 | ||
9162 | if (exp == 0) { | |
9163 | frac = 1ULL << 51 | extract64(frac, 1, 51); | |
9164 | } else if (exp == -1) { | |
9165 | frac = 1ULL << 50 | extract64(frac, 2, 50); | |
9166 | exp = 0; | |
9167 | } | |
9168 | ||
9169 | return make_float64(sbit | (exp << 52) | frac); | |
9170 | } | |
9171 | ||
9172 | static bool round_to_inf(float_status *fpst, bool sign_bit) | |
9173 | { | |
9174 | switch (fpst->float_rounding_mode) { | |
9175 | case float_round_nearest_even: /* Round to Nearest */ | |
9176 | return true; | |
9177 | case float_round_up: /* Round to +Inf */ | |
9178 | return !sign_bit; | |
9179 | case float_round_down: /* Round to -Inf */ | |
9180 | return sign_bit; | |
9181 | case float_round_to_zero: /* Round to Zero */ | |
9182 | return false; | |
9183 | } | |
9184 | ||
9185 | g_assert_not_reached(); | |
9186 | } | |
9187 | ||
9188 | float32 HELPER(recpe_f32)(float32 input, void *fpstp) | |
9189 | { | |
9190 | float_status *fpst = fpstp; | |
9191 | float32 f32 = float32_squash_input_denormal(input, fpst); | |
9192 | uint32_t f32_val = float32_val(f32); | |
9193 | uint32_t f32_sbit = 0x80000000ULL & f32_val; | |
9194 | int32_t f32_exp = extract32(f32_val, 23, 8); | |
9195 | uint32_t f32_frac = extract32(f32_val, 0, 23); | |
9196 | float64 f64, r64; | |
9197 | uint64_t r64_val; | |
9198 | int64_t r64_exp; | |
9199 | uint64_t r64_frac; | |
9200 | ||
9201 | if (float32_is_any_nan(f32)) { | |
9202 | float32 nan = f32; | |
af39bc8c | 9203 | if (float32_is_signaling_nan(f32, fpst)) { |
b6d4443a | 9204 | float_raise(float_flag_invalid, fpst); |
af39bc8c | 9205 | nan = float32_maybe_silence_nan(f32, fpst); |
fe0e4872 | 9206 | } |
b6d4443a | 9207 | if (fpst->default_nan_mode) { |
af39bc8c | 9208 | nan = float32_default_nan(fpst); |
43fe9bdb | 9209 | } |
b6d4443a AB |
9210 | return nan; |
9211 | } else if (float32_is_infinity(f32)) { | |
9212 | return float32_set_sign(float32_zero, float32_is_neg(f32)); | |
9213 | } else if (float32_is_zero(f32)) { | |
9214 | float_raise(float_flag_divbyzero, fpst); | |
9215 | return float32_set_sign(float32_infinity, float32_is_neg(f32)); | |
9216 | } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) { | |
9217 | /* Abs(value) < 2.0^-128 */ | |
9218 | float_raise(float_flag_overflow | float_flag_inexact, fpst); | |
9219 | if (round_to_inf(fpst, f32_sbit)) { | |
9220 | return float32_set_sign(float32_infinity, float32_is_neg(f32)); | |
9221 | } else { | |
9222 | return float32_set_sign(float32_maxnorm, float32_is_neg(f32)); | |
9223 | } | |
9224 | } else if (f32_exp >= 253 && fpst->flush_to_zero) { | |
9225 | float_raise(float_flag_underflow, fpst); | |
9226 | return float32_set_sign(float32_zero, float32_is_neg(f32)); | |
fe0e4872 CL |
9227 | } |
9228 | ||
fe0e4872 | 9229 | |
b6d4443a AB |
9230 | f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29); |
9231 | r64 = call_recip_estimate(f64, 253, fpst); | |
9232 | r64_val = float64_val(r64); | |
9233 | r64_exp = extract64(r64_val, 52, 11); | |
9234 | r64_frac = extract64(r64_val, 0, 52); | |
9235 | ||
9236 | /* result = sign : result_exp<7:0> : fraction<51:29>; */ | |
9237 | return make_float32(f32_sbit | | |
9238 | (r64_exp & 0xff) << 23 | | |
9239 | extract64(r64_frac, 29, 24)); | |
9240 | } | |
9241 | ||
9242 | float64 HELPER(recpe_f64)(float64 input, void *fpstp) | |
9243 | { | |
9244 | float_status *fpst = fpstp; | |
9245 | float64 f64 = float64_squash_input_denormal(input, fpst); | |
9246 | uint64_t f64_val = float64_val(f64); | |
9247 | uint64_t f64_sbit = 0x8000000000000000ULL & f64_val; | |
9248 | int64_t f64_exp = extract64(f64_val, 52, 11); | |
9249 | float64 r64; | |
9250 | uint64_t r64_val; | |
9251 | int64_t r64_exp; | |
9252 | uint64_t r64_frac; | |
9253 | ||
9254 | /* Deal with any special cases */ | |
9255 | if (float64_is_any_nan(f64)) { | |
9256 | float64 nan = f64; | |
af39bc8c | 9257 | if (float64_is_signaling_nan(f64, fpst)) { |
b6d4443a | 9258 | float_raise(float_flag_invalid, fpst); |
af39bc8c | 9259 | nan = float64_maybe_silence_nan(f64, fpst); |
b6d4443a AB |
9260 | } |
9261 | if (fpst->default_nan_mode) { | |
af39bc8c | 9262 | nan = float64_default_nan(fpst); |
b6d4443a AB |
9263 | } |
9264 | return nan; | |
9265 | } else if (float64_is_infinity(f64)) { | |
9266 | return float64_set_sign(float64_zero, float64_is_neg(f64)); | |
9267 | } else if (float64_is_zero(f64)) { | |
9268 | float_raise(float_flag_divbyzero, fpst); | |
9269 | return float64_set_sign(float64_infinity, float64_is_neg(f64)); | |
9270 | } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) { | |
9271 | /* Abs(value) < 2.0^-1024 */ | |
9272 | float_raise(float_flag_overflow | float_flag_inexact, fpst); | |
9273 | if (round_to_inf(fpst, f64_sbit)) { | |
9274 | return float64_set_sign(float64_infinity, float64_is_neg(f64)); | |
9275 | } else { | |
9276 | return float64_set_sign(float64_maxnorm, float64_is_neg(f64)); | |
9277 | } | |
fc1792e9 | 9278 | } else if (f64_exp >= 2045 && fpst->flush_to_zero) { |
b6d4443a AB |
9279 | float_raise(float_flag_underflow, fpst); |
9280 | return float64_set_sign(float64_zero, float64_is_neg(f64)); | |
9281 | } | |
fe0e4872 | 9282 | |
b6d4443a AB |
9283 | r64 = call_recip_estimate(f64, 2045, fpst); |
9284 | r64_val = float64_val(r64); | |
9285 | r64_exp = extract64(r64_val, 52, 11); | |
9286 | r64_frac = extract64(r64_val, 0, 52); | |
fe0e4872 | 9287 | |
b6d4443a AB |
9288 | /* result = sign : result_exp<10:0> : fraction<51:0> */ |
9289 | return make_float64(f64_sbit | | |
9290 | ((r64_exp & 0x7ff) << 52) | | |
9291 | r64_frac); | |
4373f3ce PB |
9292 | } |
9293 | ||
e07be5d2 CL |
9294 | /* The algorithm that must be used to calculate the estimate |
9295 | * is specified by the ARM ARM. | |
9296 | */ | |
c2fb418e | 9297 | static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status) |
e07be5d2 | 9298 | { |
1146a817 PM |
9299 | /* These calculations mustn't set any fp exception flags, |
9300 | * so we use a local copy of the fp_status. | |
9301 | */ | |
c2fb418e | 9302 | float_status dummy_status = *real_fp_status; |
1146a817 | 9303 | float_status *s = &dummy_status; |
e07be5d2 CL |
9304 | float64 q; |
9305 | int64_t q_int; | |
9306 | ||
9307 | if (float64_lt(a, float64_half, s)) { | |
9308 | /* range 0.25 <= a < 0.5 */ | |
9309 | ||
9310 | /* a in units of 1/512 rounded down */ | |
9311 | /* q0 = (int)(a * 512.0); */ | |
9312 | q = float64_mul(float64_512, a, s); | |
9313 | q_int = float64_to_int64_round_to_zero(q, s); | |
9314 | ||
9315 | /* reciprocal root r */ | |
9316 | /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */ | |
9317 | q = int64_to_float64(q_int, s); | |
9318 | q = float64_add(q, float64_half, s); | |
9319 | q = float64_div(q, float64_512, s); | |
9320 | q = float64_sqrt(q, s); | |
9321 | q = float64_div(float64_one, q, s); | |
9322 | } else { | |
9323 | /* range 0.5 <= a < 1.0 */ | |
9324 | ||
9325 | /* a in units of 1/256 rounded down */ | |
9326 | /* q1 = (int)(a * 256.0); */ | |
9327 | q = float64_mul(float64_256, a, s); | |
9328 | int64_t q_int = float64_to_int64_round_to_zero(q, s); | |
9329 | ||
9330 | /* reciprocal root r */ | |
9331 | /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */ | |
9332 | q = int64_to_float64(q_int, s); | |
9333 | q = float64_add(q, float64_half, s); | |
9334 | q = float64_div(q, float64_256, s); | |
9335 | q = float64_sqrt(q, s); | |
9336 | q = float64_div(float64_one, q, s); | |
9337 | } | |
9338 | /* r in units of 1/256 rounded to nearest */ | |
9339 | /* s = (int)(256.0 * r + 0.5); */ | |
9340 | ||
9341 | q = float64_mul(q, float64_256,s ); | |
9342 | q = float64_add(q, float64_half, s); | |
9343 | q_int = float64_to_int64_round_to_zero(q, s); | |
9344 | ||
9345 | /* return (double)s / 256.0;*/ | |
9346 | return float64_div(int64_to_float64(q_int, s), float64_256, s); | |
9347 | } | |
9348 | ||
c2fb418e | 9349 | float32 HELPER(rsqrte_f32)(float32 input, void *fpstp) |
4373f3ce | 9350 | { |
c2fb418e AB |
9351 | float_status *s = fpstp; |
9352 | float32 f32 = float32_squash_input_denormal(input, s); | |
9353 | uint32_t val = float32_val(f32); | |
9354 | uint32_t f32_sbit = 0x80000000 & val; | |
9355 | int32_t f32_exp = extract32(val, 23, 8); | |
9356 | uint32_t f32_frac = extract32(val, 0, 23); | |
9357 | uint64_t f64_frac; | |
9358 | uint64_t val64; | |
e07be5d2 CL |
9359 | int result_exp; |
9360 | float64 f64; | |
e07be5d2 | 9361 | |
c2fb418e AB |
9362 | if (float32_is_any_nan(f32)) { |
9363 | float32 nan = f32; | |
af39bc8c | 9364 | if (float32_is_signaling_nan(f32, s)) { |
e07be5d2 | 9365 | float_raise(float_flag_invalid, s); |
af39bc8c | 9366 | nan = float32_maybe_silence_nan(f32, s); |
e07be5d2 | 9367 | } |
c2fb418e | 9368 | if (s->default_nan_mode) { |
af39bc8c | 9369 | nan = float32_default_nan(s); |
43fe9bdb | 9370 | } |
c2fb418e AB |
9371 | return nan; |
9372 | } else if (float32_is_zero(f32)) { | |
e07be5d2 | 9373 | float_raise(float_flag_divbyzero, s); |
c2fb418e AB |
9374 | return float32_set_sign(float32_infinity, float32_is_neg(f32)); |
9375 | } else if (float32_is_neg(f32)) { | |
e07be5d2 | 9376 | float_raise(float_flag_invalid, s); |
af39bc8c | 9377 | return float32_default_nan(s); |
c2fb418e | 9378 | } else if (float32_is_infinity(f32)) { |
e07be5d2 CL |
9379 | return float32_zero; |
9380 | } | |
9381 | ||
c2fb418e | 9382 | /* Scale and normalize to a double-precision value between 0.25 and 1.0, |
e07be5d2 | 9383 | * preserving the parity of the exponent. */ |
c2fb418e AB |
9384 | |
9385 | f64_frac = ((uint64_t) f32_frac) << 29; | |
9386 | if (f32_exp == 0) { | |
9387 | while (extract64(f64_frac, 51, 1) == 0) { | |
9388 | f64_frac = f64_frac << 1; | |
9389 | f32_exp = f32_exp-1; | |
9390 | } | |
9391 | f64_frac = extract64(f64_frac, 0, 51) << 1; | |
9392 | } | |
9393 | ||
9394 | if (extract64(f32_exp, 0, 1) == 0) { | |
9395 | f64 = make_float64(((uint64_t) f32_sbit) << 32 | |
e07be5d2 | 9396 | | (0x3feULL << 52) |
c2fb418e | 9397 | | f64_frac); |
e07be5d2 | 9398 | } else { |
c2fb418e | 9399 | f64 = make_float64(((uint64_t) f32_sbit) << 32 |
e07be5d2 | 9400 | | (0x3fdULL << 52) |
c2fb418e | 9401 | | f64_frac); |
e07be5d2 CL |
9402 | } |
9403 | ||
c2fb418e | 9404 | result_exp = (380 - f32_exp) / 2; |
e07be5d2 | 9405 | |
c2fb418e | 9406 | f64 = recip_sqrt_estimate(f64, s); |
e07be5d2 CL |
9407 | |
9408 | val64 = float64_val(f64); | |
9409 | ||
26cc6abf | 9410 | val = ((result_exp & 0xff) << 23) |
e07be5d2 CL |
9411 | | ((val64 >> 29) & 0x7fffff); |
9412 | return make_float32(val); | |
4373f3ce PB |
9413 | } |
9414 | ||
c2fb418e AB |
9415 | float64 HELPER(rsqrte_f64)(float64 input, void *fpstp) |
9416 | { | |
9417 | float_status *s = fpstp; | |
9418 | float64 f64 = float64_squash_input_denormal(input, s); | |
9419 | uint64_t val = float64_val(f64); | |
9420 | uint64_t f64_sbit = 0x8000000000000000ULL & val; | |
9421 | int64_t f64_exp = extract64(val, 52, 11); | |
9422 | uint64_t f64_frac = extract64(val, 0, 52); | |
9423 | int64_t result_exp; | |
9424 | uint64_t result_frac; | |
9425 | ||
9426 | if (float64_is_any_nan(f64)) { | |
9427 | float64 nan = f64; | |
af39bc8c | 9428 | if (float64_is_signaling_nan(f64, s)) { |
c2fb418e | 9429 | float_raise(float_flag_invalid, s); |
af39bc8c | 9430 | nan = float64_maybe_silence_nan(f64, s); |
c2fb418e AB |
9431 | } |
9432 | if (s->default_nan_mode) { | |
af39bc8c | 9433 | nan = float64_default_nan(s); |
c2fb418e AB |
9434 | } |
9435 | return nan; | |
9436 | } else if (float64_is_zero(f64)) { | |
9437 | float_raise(float_flag_divbyzero, s); | |
9438 | return float64_set_sign(float64_infinity, float64_is_neg(f64)); | |
9439 | } else if (float64_is_neg(f64)) { | |
9440 | float_raise(float_flag_invalid, s); | |
af39bc8c | 9441 | return float64_default_nan(s); |
c2fb418e AB |
9442 | } else if (float64_is_infinity(f64)) { |
9443 | return float64_zero; | |
9444 | } | |
9445 | ||
9446 | /* Scale and normalize to a double-precision value between 0.25 and 1.0, | |
9447 | * preserving the parity of the exponent. */ | |
9448 | ||
9449 | if (f64_exp == 0) { | |
9450 | while (extract64(f64_frac, 51, 1) == 0) { | |
9451 | f64_frac = f64_frac << 1; | |
9452 | f64_exp = f64_exp - 1; | |
9453 | } | |
9454 | f64_frac = extract64(f64_frac, 0, 51) << 1; | |
9455 | } | |
9456 | ||
9457 | if (extract64(f64_exp, 0, 1) == 0) { | |
9458 | f64 = make_float64(f64_sbit | |
9459 | | (0x3feULL << 52) | |
9460 | | f64_frac); | |
9461 | } else { | |
9462 | f64 = make_float64(f64_sbit | |
9463 | | (0x3fdULL << 52) | |
9464 | | f64_frac); | |
9465 | } | |
9466 | ||
9467 | result_exp = (3068 - f64_exp) / 2; | |
9468 | ||
9469 | f64 = recip_sqrt_estimate(f64, s); | |
9470 | ||
9471 | result_frac = extract64(float64_val(f64), 0, 52); | |
9472 | ||
9473 | return make_float64(f64_sbit | | |
9474 | ((result_exp & 0x7ff) << 52) | | |
9475 | result_frac); | |
9476 | } | |
9477 | ||
b6d4443a | 9478 | uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp) |
4373f3ce | 9479 | { |
b6d4443a | 9480 | float_status *s = fpstp; |
fe0e4872 CL |
9481 | float64 f64; |
9482 | ||
9483 | if ((a & 0x80000000) == 0) { | |
9484 | return 0xffffffff; | |
9485 | } | |
9486 | ||
9487 | f64 = make_float64((0x3feULL << 52) | |
9488 | | ((int64_t)(a & 0x7fffffff) << 21)); | |
9489 | ||
b6d4443a | 9490 | f64 = recip_estimate(f64, s); |
fe0e4872 CL |
9491 | |
9492 | return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff); | |
4373f3ce PB |
9493 | } |
9494 | ||
c2fb418e | 9495 | uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp) |
4373f3ce | 9496 | { |
c2fb418e | 9497 | float_status *fpst = fpstp; |
e07be5d2 CL |
9498 | float64 f64; |
9499 | ||
9500 | if ((a & 0xc0000000) == 0) { | |
9501 | return 0xffffffff; | |
9502 | } | |
9503 | ||
9504 | if (a & 0x80000000) { | |
9505 | f64 = make_float64((0x3feULL << 52) | |
9506 | | ((uint64_t)(a & 0x7fffffff) << 21)); | |
9507 | } else { /* bits 31-30 == '01' */ | |
9508 | f64 = make_float64((0x3fdULL << 52) | |
9509 | | ((uint64_t)(a & 0x3fffffff) << 22)); | |
9510 | } | |
9511 | ||
c2fb418e | 9512 | f64 = recip_sqrt_estimate(f64, fpst); |
e07be5d2 CL |
9513 | |
9514 | return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff); | |
4373f3ce | 9515 | } |
fe1479c3 | 9516 | |
da97f52c PM |
9517 | /* VFPv4 fused multiply-accumulate */ |
9518 | float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp) | |
9519 | { | |
9520 | float_status *fpst = fpstp; | |
9521 | return float32_muladd(a, b, c, 0, fpst); | |
9522 | } | |
9523 | ||
9524 | float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp) | |
9525 | { | |
9526 | float_status *fpst = fpstp; | |
9527 | return float64_muladd(a, b, c, 0, fpst); | |
9528 | } | |
d9b0848d PM |
9529 | |
9530 | /* ARMv8 round to integral */ | |
9531 | float32 HELPER(rints_exact)(float32 x, void *fp_status) | |
9532 | { | |
9533 | return float32_round_to_int(x, fp_status); | |
9534 | } | |
9535 | ||
9536 | float64 HELPER(rintd_exact)(float64 x, void *fp_status) | |
9537 | { | |
9538 | return float64_round_to_int(x, fp_status); | |
9539 | } | |
9540 | ||
9541 | float32 HELPER(rints)(float32 x, void *fp_status) | |
9542 | { | |
9543 | int old_flags = get_float_exception_flags(fp_status), new_flags; | |
9544 | float32 ret; | |
9545 | ||
9546 | ret = float32_round_to_int(x, fp_status); | |
9547 | ||
9548 | /* Suppress any inexact exceptions the conversion produced */ | |
9549 | if (!(old_flags & float_flag_inexact)) { | |
9550 | new_flags = get_float_exception_flags(fp_status); | |
9551 | set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); | |
9552 | } | |
9553 | ||
9554 | return ret; | |
9555 | } | |
9556 | ||
9557 | float64 HELPER(rintd)(float64 x, void *fp_status) | |
9558 | { | |
9559 | int old_flags = get_float_exception_flags(fp_status), new_flags; | |
9560 | float64 ret; | |
9561 | ||
9562 | ret = float64_round_to_int(x, fp_status); | |
9563 | ||
9564 | new_flags = get_float_exception_flags(fp_status); | |
9565 | ||
9566 | /* Suppress any inexact exceptions the conversion produced */ | |
9567 | if (!(old_flags & float_flag_inexact)) { | |
9568 | new_flags = get_float_exception_flags(fp_status); | |
9569 | set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); | |
9570 | } | |
9571 | ||
9572 | return ret; | |
9573 | } | |
9972da66 WN |
9574 | |
9575 | /* Convert ARM rounding mode to softfloat */ | |
9576 | int arm_rmode_to_sf(int rmode) | |
9577 | { | |
9578 | switch (rmode) { | |
9579 | case FPROUNDING_TIEAWAY: | |
9580 | rmode = float_round_ties_away; | |
9581 | break; | |
9582 | case FPROUNDING_ODD: | |
9583 | /* FIXME: add support for TIEAWAY and ODD */ | |
9584 | qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n", | |
9585 | rmode); | |
9586 | case FPROUNDING_TIEEVEN: | |
9587 | default: | |
9588 | rmode = float_round_nearest_even; | |
9589 | break; | |
9590 | case FPROUNDING_POSINF: | |
9591 | rmode = float_round_up; | |
9592 | break; | |
9593 | case FPROUNDING_NEGINF: | |
9594 | rmode = float_round_down; | |
9595 | break; | |
9596 | case FPROUNDING_ZERO: | |
9597 | rmode = float_round_to_zero; | |
9598 | break; | |
9599 | } | |
9600 | return rmode; | |
9601 | } | |
eb0ecd5a | 9602 | |
aa633469 PM |
9603 | /* CRC helpers. |
9604 | * The upper bytes of val (above the number specified by 'bytes') must have | |
9605 | * been zeroed out by the caller. | |
9606 | */ | |
eb0ecd5a WN |
9607 | uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes) |
9608 | { | |
9609 | uint8_t buf[4]; | |
9610 | ||
aa633469 | 9611 | stl_le_p(buf, val); |
eb0ecd5a WN |
9612 | |
9613 | /* zlib crc32 converts the accumulator and output to one's complement. */ | |
9614 | return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; | |
9615 | } | |
9616 | ||
9617 | uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) | |
9618 | { | |
9619 | uint8_t buf[4]; | |
9620 | ||
aa633469 | 9621 | stl_le_p(buf, val); |
eb0ecd5a WN |
9622 | |
9623 | /* Linux crc32c converts the output to one's complement. */ | |
9624 | return crc32c(acc, buf, bytes) ^ 0xffffffff; | |
9625 | } |