]>
Commit | Line | Data |
---|---|---|
1 | #include "cpu.h" | |
2 | #include "internals.h" | |
3 | #include "exec/gdbstub.h" | |
4 | #include "exec/helper-proto.h" | |
5 | #include "qemu/host-utils.h" | |
6 | #include "sysemu/arch_init.h" | |
7 | #include "sysemu/sysemu.h" | |
8 | #include "qemu/bitops.h" | |
9 | #include "qemu/crc32c.h" | |
10 | #include "exec/cpu_ldst.h" | |
11 | #include "arm_ldst.h" | |
12 | #include <zlib.h> /* For crc32 */ | |
13 | ||
14 | #ifndef CONFIG_USER_ONLY | |
15 | static inline int get_phys_addr(CPUARMState *env, target_ulong address, | |
16 | int access_type, int is_user, | |
17 | hwaddr *phys_ptr, int *prot, | |
18 | target_ulong *page_size); | |
19 | ||
20 | /* Definitions for the PMCCNTR and PMCR registers */ | |
21 | #define PMCRD 0x8 | |
22 | #define PMCRC 0x4 | |
23 | #define PMCRE 0x1 | |
24 | #endif | |
25 | ||
26 | static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) | |
27 | { | |
28 | int nregs; | |
29 | ||
30 | /* VFP data registers are always little-endian. */ | |
31 | nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; | |
32 | if (reg < nregs) { | |
33 | stfq_le_p(buf, env->vfp.regs[reg]); | |
34 | return 8; | |
35 | } | |
36 | if (arm_feature(env, ARM_FEATURE_NEON)) { | |
37 | /* Aliases for Q regs. */ | |
38 | nregs += 16; | |
39 | if (reg < nregs) { | |
40 | stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]); | |
41 | stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]); | |
42 | return 16; | |
43 | } | |
44 | } | |
45 | switch (reg - nregs) { | |
46 | case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4; | |
47 | case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4; | |
48 | case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4; | |
49 | } | |
50 | return 0; | |
51 | } | |
52 | ||
53 | static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) | |
54 | { | |
55 | int nregs; | |
56 | ||
57 | nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; | |
58 | if (reg < nregs) { | |
59 | env->vfp.regs[reg] = ldfq_le_p(buf); | |
60 | return 8; | |
61 | } | |
62 | if (arm_feature(env, ARM_FEATURE_NEON)) { | |
63 | nregs += 16; | |
64 | if (reg < nregs) { | |
65 | env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf); | |
66 | env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8); | |
67 | return 16; | |
68 | } | |
69 | } | |
70 | switch (reg - nregs) { | |
71 | case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; | |
72 | case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4; | |
73 | case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; | |
74 | } | |
75 | return 0; | |
76 | } | |
77 | ||
78 | static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) | |
79 | { | |
80 | switch (reg) { | |
81 | case 0 ... 31: | |
82 | /* 128 bit FP register */ | |
83 | stfq_le_p(buf, env->vfp.regs[reg * 2]); | |
84 | stfq_le_p(buf + 8, env->vfp.regs[reg * 2 + 1]); | |
85 | return 16; | |
86 | case 32: | |
87 | /* FPSR */ | |
88 | stl_p(buf, vfp_get_fpsr(env)); | |
89 | return 4; | |
90 | case 33: | |
91 | /* FPCR */ | |
92 | stl_p(buf, vfp_get_fpcr(env)); | |
93 | return 4; | |
94 | default: | |
95 | return 0; | |
96 | } | |
97 | } | |
98 | ||
99 | static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) | |
100 | { | |
101 | switch (reg) { | |
102 | case 0 ... 31: | |
103 | /* 128 bit FP register */ | |
104 | env->vfp.regs[reg * 2] = ldfq_le_p(buf); | |
105 | env->vfp.regs[reg * 2 + 1] = ldfq_le_p(buf + 8); | |
106 | return 16; | |
107 | case 32: | |
108 | /* FPSR */ | |
109 | vfp_set_fpsr(env, ldl_p(buf)); | |
110 | return 4; | |
111 | case 33: | |
112 | /* FPCR */ | |
113 | vfp_set_fpcr(env, ldl_p(buf)); | |
114 | return 4; | |
115 | default: | |
116 | return 0; | |
117 | } | |
118 | } | |
119 | ||
120 | static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
121 | { | |
122 | if (cpreg_field_is_64bit(ri)) { | |
123 | return CPREG_FIELD64(env, ri); | |
124 | } else { | |
125 | return CPREG_FIELD32(env, ri); | |
126 | } | |
127 | } | |
128 | ||
129 | static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
130 | uint64_t value) | |
131 | { | |
132 | if (cpreg_field_is_64bit(ri)) { | |
133 | CPREG_FIELD64(env, ri) = value; | |
134 | } else { | |
135 | CPREG_FIELD32(env, ri) = value; | |
136 | } | |
137 | } | |
138 | ||
139 | static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) | |
140 | { | |
141 | return (char *)env + ri->fieldoffset; | |
142 | } | |
143 | ||
144 | static uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) | |
145 | { | |
146 | /* Raw read of a coprocessor register (as needed for migration, etc). */ | |
147 | if (ri->type & ARM_CP_CONST) { | |
148 | return ri->resetvalue; | |
149 | } else if (ri->raw_readfn) { | |
150 | return ri->raw_readfn(env, ri); | |
151 | } else if (ri->readfn) { | |
152 | return ri->readfn(env, ri); | |
153 | } else { | |
154 | return raw_read(env, ri); | |
155 | } | |
156 | } | |
157 | ||
158 | static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, | |
159 | uint64_t v) | |
160 | { | |
161 | /* Raw write of a coprocessor register (as needed for migration, etc). | |
162 | * Note that constant registers are treated as write-ignored; the | |
163 | * caller should check for success by whether a readback gives the | |
164 | * value written. | |
165 | */ | |
166 | if (ri->type & ARM_CP_CONST) { | |
167 | return; | |
168 | } else if (ri->raw_writefn) { | |
169 | ri->raw_writefn(env, ri, v); | |
170 | } else if (ri->writefn) { | |
171 | ri->writefn(env, ri, v); | |
172 | } else { | |
173 | raw_write(env, ri, v); | |
174 | } | |
175 | } | |
176 | ||
177 | bool write_cpustate_to_list(ARMCPU *cpu) | |
178 | { | |
179 | /* Write the coprocessor state from cpu->env to the (index,value) list. */ | |
180 | int i; | |
181 | bool ok = true; | |
182 | ||
183 | for (i = 0; i < cpu->cpreg_array_len; i++) { | |
184 | uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); | |
185 | const ARMCPRegInfo *ri; | |
186 | ||
187 | ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); | |
188 | if (!ri) { | |
189 | ok = false; | |
190 | continue; | |
191 | } | |
192 | if (ri->type & ARM_CP_NO_MIGRATE) { | |
193 | continue; | |
194 | } | |
195 | cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri); | |
196 | } | |
197 | return ok; | |
198 | } | |
199 | ||
200 | bool write_list_to_cpustate(ARMCPU *cpu) | |
201 | { | |
202 | int i; | |
203 | bool ok = true; | |
204 | ||
205 | for (i = 0; i < cpu->cpreg_array_len; i++) { | |
206 | uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); | |
207 | uint64_t v = cpu->cpreg_values[i]; | |
208 | const ARMCPRegInfo *ri; | |
209 | ||
210 | ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); | |
211 | if (!ri) { | |
212 | ok = false; | |
213 | continue; | |
214 | } | |
215 | if (ri->type & ARM_CP_NO_MIGRATE) { | |
216 | continue; | |
217 | } | |
218 | /* Write value and confirm it reads back as written | |
219 | * (to catch read-only registers and partially read-only | |
220 | * registers where the incoming migration value doesn't match) | |
221 | */ | |
222 | write_raw_cp_reg(&cpu->env, ri, v); | |
223 | if (read_raw_cp_reg(&cpu->env, ri) != v) { | |
224 | ok = false; | |
225 | } | |
226 | } | |
227 | return ok; | |
228 | } | |
229 | ||
230 | static void add_cpreg_to_list(gpointer key, gpointer opaque) | |
231 | { | |
232 | ARMCPU *cpu = opaque; | |
233 | uint64_t regidx; | |
234 | const ARMCPRegInfo *ri; | |
235 | ||
236 | regidx = *(uint32_t *)key; | |
237 | ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); | |
238 | ||
239 | if (!(ri->type & ARM_CP_NO_MIGRATE)) { | |
240 | cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); | |
241 | /* The value array need not be initialized at this point */ | |
242 | cpu->cpreg_array_len++; | |
243 | } | |
244 | } | |
245 | ||
246 | static void count_cpreg(gpointer key, gpointer opaque) | |
247 | { | |
248 | ARMCPU *cpu = opaque; | |
249 | uint64_t regidx; | |
250 | const ARMCPRegInfo *ri; | |
251 | ||
252 | regidx = *(uint32_t *)key; | |
253 | ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); | |
254 | ||
255 | if (!(ri->type & ARM_CP_NO_MIGRATE)) { | |
256 | cpu->cpreg_array_len++; | |
257 | } | |
258 | } | |
259 | ||
260 | static gint cpreg_key_compare(gconstpointer a, gconstpointer b) | |
261 | { | |
262 | uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a); | |
263 | uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b); | |
264 | ||
265 | if (aidx > bidx) { | |
266 | return 1; | |
267 | } | |
268 | if (aidx < bidx) { | |
269 | return -1; | |
270 | } | |
271 | return 0; | |
272 | } | |
273 | ||
274 | static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata) | |
275 | { | |
276 | GList **plist = udata; | |
277 | ||
278 | *plist = g_list_prepend(*plist, key); | |
279 | } | |
280 | ||
281 | void init_cpreg_list(ARMCPU *cpu) | |
282 | { | |
283 | /* Initialise the cpreg_tuples[] array based on the cp_regs hash. | |
284 | * Note that we require cpreg_tuples[] to be sorted by key ID. | |
285 | */ | |
286 | GList *keys = NULL; | |
287 | int arraylen; | |
288 | ||
289 | g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys); | |
290 | ||
291 | keys = g_list_sort(keys, cpreg_key_compare); | |
292 | ||
293 | cpu->cpreg_array_len = 0; | |
294 | ||
295 | g_list_foreach(keys, count_cpreg, cpu); | |
296 | ||
297 | arraylen = cpu->cpreg_array_len; | |
298 | cpu->cpreg_indexes = g_new(uint64_t, arraylen); | |
299 | cpu->cpreg_values = g_new(uint64_t, arraylen); | |
300 | cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); | |
301 | cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); | |
302 | cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; | |
303 | cpu->cpreg_array_len = 0; | |
304 | ||
305 | g_list_foreach(keys, add_cpreg_to_list, cpu); | |
306 | ||
307 | assert(cpu->cpreg_array_len == arraylen); | |
308 | ||
309 | g_list_free(keys); | |
310 | } | |
311 | ||
312 | static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) | |
313 | { | |
314 | ARMCPU *cpu = arm_env_get_cpu(env); | |
315 | ||
316 | raw_write(env, ri, value); | |
317 | tlb_flush(CPU(cpu), 1); /* Flush TLB as domain not tracked in TLB */ | |
318 | } | |
319 | ||
320 | static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) | |
321 | { | |
322 | ARMCPU *cpu = arm_env_get_cpu(env); | |
323 | ||
324 | if (raw_read(env, ri) != value) { | |
325 | /* Unlike real hardware the qemu TLB uses virtual addresses, | |
326 | * not modified virtual addresses, so this causes a TLB flush. | |
327 | */ | |
328 | tlb_flush(CPU(cpu), 1); | |
329 | raw_write(env, ri, value); | |
330 | } | |
331 | } | |
332 | ||
333 | static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
334 | uint64_t value) | |
335 | { | |
336 | ARMCPU *cpu = arm_env_get_cpu(env); | |
337 | ||
338 | if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_MPU) | |
339 | && !extended_addresses_enabled(env)) { | |
340 | /* For VMSA (when not using the LPAE long descriptor page table | |
341 | * format) this register includes the ASID, so do a TLB flush. | |
342 | * For PMSA it is purely a process ID and no action is needed. | |
343 | */ | |
344 | tlb_flush(CPU(cpu), 1); | |
345 | } | |
346 | raw_write(env, ri, value); | |
347 | } | |
348 | ||
349 | static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
350 | uint64_t value) | |
351 | { | |
352 | /* Invalidate all (TLBIALL) */ | |
353 | ARMCPU *cpu = arm_env_get_cpu(env); | |
354 | ||
355 | tlb_flush(CPU(cpu), 1); | |
356 | } | |
357 | ||
358 | static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
359 | uint64_t value) | |
360 | { | |
361 | /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ | |
362 | ARMCPU *cpu = arm_env_get_cpu(env); | |
363 | ||
364 | tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); | |
365 | } | |
366 | ||
367 | static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
368 | uint64_t value) | |
369 | { | |
370 | /* Invalidate by ASID (TLBIASID) */ | |
371 | ARMCPU *cpu = arm_env_get_cpu(env); | |
372 | ||
373 | tlb_flush(CPU(cpu), value == 0); | |
374 | } | |
375 | ||
376 | static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
377 | uint64_t value) | |
378 | { | |
379 | /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ | |
380 | ARMCPU *cpu = arm_env_get_cpu(env); | |
381 | ||
382 | tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); | |
383 | } | |
384 | ||
385 | /* IS variants of TLB operations must affect all cores */ | |
386 | static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
387 | uint64_t value) | |
388 | { | |
389 | CPUState *other_cs; | |
390 | ||
391 | CPU_FOREACH(other_cs) { | |
392 | tlb_flush(other_cs, 1); | |
393 | } | |
394 | } | |
395 | ||
396 | static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
397 | uint64_t value) | |
398 | { | |
399 | CPUState *other_cs; | |
400 | ||
401 | CPU_FOREACH(other_cs) { | |
402 | tlb_flush(other_cs, value == 0); | |
403 | } | |
404 | } | |
405 | ||
406 | static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
407 | uint64_t value) | |
408 | { | |
409 | CPUState *other_cs; | |
410 | ||
411 | CPU_FOREACH(other_cs) { | |
412 | tlb_flush_page(other_cs, value & TARGET_PAGE_MASK); | |
413 | } | |
414 | } | |
415 | ||
416 | static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
417 | uint64_t value) | |
418 | { | |
419 | CPUState *other_cs; | |
420 | ||
421 | CPU_FOREACH(other_cs) { | |
422 | tlb_flush_page(other_cs, value & TARGET_PAGE_MASK); | |
423 | } | |
424 | } | |
425 | ||
426 | static const ARMCPRegInfo cp_reginfo[] = { | |
427 | { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0, | |
428 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse), | |
429 | .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, | |
430 | { .name = "CONTEXTIDR", .state = ARM_CP_STATE_BOTH, | |
431 | .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, | |
432 | .access = PL1_RW, | |
433 | .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el1), | |
434 | .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, | |
435 | REGINFO_SENTINEL | |
436 | }; | |
437 | ||
438 | static const ARMCPRegInfo not_v8_cp_reginfo[] = { | |
439 | /* NB: Some of these registers exist in v8 but with more precise | |
440 | * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). | |
441 | */ | |
442 | /* MMU Domain access control / MPU write buffer control */ | |
443 | { .name = "DACR", | |
444 | .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, | |
445 | .access = PL1_RW, .resetvalue = 0, | |
446 | .writefn = dacr_write, .raw_writefn = raw_write, | |
447 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), | |
448 | offsetoflow32(CPUARMState, cp15.dacr_ns) } }, | |
449 | /* ??? This covers not just the impdef TLB lockdown registers but also | |
450 | * some v7VMSA registers relating to TEX remap, so it is overly broad. | |
451 | */ | |
452 | { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY, | |
453 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, | |
454 | /* Cache maintenance ops; some of this space may be overridden later. */ | |
455 | { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, | |
456 | .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, | |
457 | .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, | |
458 | REGINFO_SENTINEL | |
459 | }; | |
460 | ||
461 | static const ARMCPRegInfo not_v6_cp_reginfo[] = { | |
462 | /* Not all pre-v6 cores implemented this WFI, so this is slightly | |
463 | * over-broad. | |
464 | */ | |
465 | { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, | |
466 | .access = PL1_W, .type = ARM_CP_WFI }, | |
467 | REGINFO_SENTINEL | |
468 | }; | |
469 | ||
470 | static const ARMCPRegInfo not_v7_cp_reginfo[] = { | |
471 | /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which | |
472 | * is UNPREDICTABLE; we choose to NOP as most implementations do). | |
473 | */ | |
474 | { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, | |
475 | .access = PL1_W, .type = ARM_CP_WFI }, | |
476 | /* L1 cache lockdown. Not architectural in v6 and earlier but in practice | |
477 | * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and | |
478 | * OMAPCP will override this space. | |
479 | */ | |
480 | { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, | |
481 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), | |
482 | .resetvalue = 0 }, | |
483 | { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, | |
484 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), | |
485 | .resetvalue = 0 }, | |
486 | /* v6 doesn't have the cache ID registers but Linux reads them anyway */ | |
487 | { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, | |
488 | .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, | |
489 | .resetvalue = 0 }, | |
490 | /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; | |
491 | * implementing it as RAZ means the "debug architecture version" bits | |
492 | * will read as a reserved value, which should cause Linux to not try | |
493 | * to use the debug hardware. | |
494 | */ | |
495 | { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, | |
496 | .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
497 | /* MMU TLB control. Note that the wildcarding means we cover not just | |
498 | * the unified TLB ops but also the dside/iside/inner-shareable variants. | |
499 | */ | |
500 | { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, | |
501 | .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, | |
502 | .type = ARM_CP_NO_MIGRATE }, | |
503 | { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, | |
504 | .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, | |
505 | .type = ARM_CP_NO_MIGRATE }, | |
506 | { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, | |
507 | .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, | |
508 | .type = ARM_CP_NO_MIGRATE }, | |
509 | { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, | |
510 | .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, | |
511 | .type = ARM_CP_NO_MIGRATE }, | |
512 | REGINFO_SENTINEL | |
513 | }; | |
514 | ||
515 | static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
516 | uint64_t value) | |
517 | { | |
518 | uint32_t mask = 0; | |
519 | ||
520 | /* In ARMv8 most bits of CPACR_EL1 are RES0. */ | |
521 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
522 | /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. | |
523 | * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. | |
524 | * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. | |
525 | */ | |
526 | if (arm_feature(env, ARM_FEATURE_VFP)) { | |
527 | /* VFP coprocessor: cp10 & cp11 [23:20] */ | |
528 | mask |= (1 << 31) | (1 << 30) | (0xf << 20); | |
529 | ||
530 | if (!arm_feature(env, ARM_FEATURE_NEON)) { | |
531 | /* ASEDIS [31] bit is RAO/WI */ | |
532 | value |= (1 << 31); | |
533 | } | |
534 | ||
535 | /* VFPv3 and upwards with NEON implement 32 double precision | |
536 | * registers (D0-D31). | |
537 | */ | |
538 | if (!arm_feature(env, ARM_FEATURE_NEON) || | |
539 | !arm_feature(env, ARM_FEATURE_VFP3)) { | |
540 | /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ | |
541 | value |= (1 << 30); | |
542 | } | |
543 | } | |
544 | value &= mask; | |
545 | } | |
546 | env->cp15.c1_coproc = value; | |
547 | } | |
548 | ||
549 | static const ARMCPRegInfo v6_cp_reginfo[] = { | |
550 | /* prefetch by MVA in v6, NOP in v7 */ | |
551 | { .name = "MVA_prefetch", | |
552 | .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, | |
553 | .access = PL1_W, .type = ARM_CP_NOP }, | |
554 | { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, | |
555 | .access = PL0_W, .type = ARM_CP_NOP }, | |
556 | { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, | |
557 | .access = PL0_W, .type = ARM_CP_NOP }, | |
558 | { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, | |
559 | .access = PL0_W, .type = ARM_CP_NOP }, | |
560 | { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, | |
561 | .access = PL1_RW, | |
562 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), | |
563 | offsetof(CPUARMState, cp15.ifar_ns) }, | |
564 | .resetvalue = 0, }, | |
565 | /* Watchpoint Fault Address Register : should actually only be present | |
566 | * for 1136, 1176, 11MPCore. | |
567 | */ | |
568 | { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, | |
569 | .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, | |
570 | { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, | |
571 | .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, | |
572 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc), | |
573 | .resetvalue = 0, .writefn = cpacr_write }, | |
574 | REGINFO_SENTINEL | |
575 | }; | |
576 | ||
577 | static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
578 | { | |
579 | /* Performance monitor registers user accessibility is controlled | |
580 | * by PMUSERENR. | |
581 | */ | |
582 | if (arm_current_el(env) == 0 && !env->cp15.c9_pmuserenr) { | |
583 | return CP_ACCESS_TRAP; | |
584 | } | |
585 | return CP_ACCESS_OK; | |
586 | } | |
587 | ||
588 | #ifndef CONFIG_USER_ONLY | |
589 | ||
590 | static inline bool arm_ccnt_enabled(CPUARMState *env) | |
591 | { | |
592 | /* This does not support checking PMCCFILTR_EL0 register */ | |
593 | ||
594 | if (!(env->cp15.c9_pmcr & PMCRE)) { | |
595 | return false; | |
596 | } | |
597 | ||
598 | return true; | |
599 | } | |
600 | ||
601 | void pmccntr_sync(CPUARMState *env) | |
602 | { | |
603 | uint64_t temp_ticks; | |
604 | ||
605 | temp_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL), | |
606 | get_ticks_per_sec(), 1000000); | |
607 | ||
608 | if (env->cp15.c9_pmcr & PMCRD) { | |
609 | /* Increment once every 64 processor clock cycles */ | |
610 | temp_ticks /= 64; | |
611 | } | |
612 | ||
613 | if (arm_ccnt_enabled(env)) { | |
614 | env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt; | |
615 | } | |
616 | } | |
617 | ||
618 | static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
619 | uint64_t value) | |
620 | { | |
621 | pmccntr_sync(env); | |
622 | ||
623 | if (value & PMCRC) { | |
624 | /* The counter has been reset */ | |
625 | env->cp15.c15_ccnt = 0; | |
626 | } | |
627 | ||
628 | /* only the DP, X, D and E bits are writable */ | |
629 | env->cp15.c9_pmcr &= ~0x39; | |
630 | env->cp15.c9_pmcr |= (value & 0x39); | |
631 | ||
632 | pmccntr_sync(env); | |
633 | } | |
634 | ||
635 | static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
636 | { | |
637 | uint64_t total_ticks; | |
638 | ||
639 | if (!arm_ccnt_enabled(env)) { | |
640 | /* Counter is disabled, do not change value */ | |
641 | return env->cp15.c15_ccnt; | |
642 | } | |
643 | ||
644 | total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL), | |
645 | get_ticks_per_sec(), 1000000); | |
646 | ||
647 | if (env->cp15.c9_pmcr & PMCRD) { | |
648 | /* Increment once every 64 processor clock cycles */ | |
649 | total_ticks /= 64; | |
650 | } | |
651 | return total_ticks - env->cp15.c15_ccnt; | |
652 | } | |
653 | ||
654 | static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
655 | uint64_t value) | |
656 | { | |
657 | uint64_t total_ticks; | |
658 | ||
659 | if (!arm_ccnt_enabled(env)) { | |
660 | /* Counter is disabled, set the absolute value */ | |
661 | env->cp15.c15_ccnt = value; | |
662 | return; | |
663 | } | |
664 | ||
665 | total_ticks = muldiv64(qemu_clock_get_us(QEMU_CLOCK_VIRTUAL), | |
666 | get_ticks_per_sec(), 1000000); | |
667 | ||
668 | if (env->cp15.c9_pmcr & PMCRD) { | |
669 | /* Increment once every 64 processor clock cycles */ | |
670 | total_ticks /= 64; | |
671 | } | |
672 | env->cp15.c15_ccnt = total_ticks - value; | |
673 | } | |
674 | ||
675 | static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, | |
676 | uint64_t value) | |
677 | { | |
678 | uint64_t cur_val = pmccntr_read(env, NULL); | |
679 | ||
680 | pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); | |
681 | } | |
682 | ||
683 | #else /* CONFIG_USER_ONLY */ | |
684 | ||
685 | void pmccntr_sync(CPUARMState *env) | |
686 | { | |
687 | } | |
688 | ||
689 | #endif | |
690 | ||
691 | static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
692 | uint64_t value) | |
693 | { | |
694 | pmccntr_sync(env); | |
695 | env->cp15.pmccfiltr_el0 = value & 0x7E000000; | |
696 | pmccntr_sync(env); | |
697 | } | |
698 | ||
699 | static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
700 | uint64_t value) | |
701 | { | |
702 | value &= (1 << 31); | |
703 | env->cp15.c9_pmcnten |= value; | |
704 | } | |
705 | ||
706 | static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
707 | uint64_t value) | |
708 | { | |
709 | value &= (1 << 31); | |
710 | env->cp15.c9_pmcnten &= ~value; | |
711 | } | |
712 | ||
713 | static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
714 | uint64_t value) | |
715 | { | |
716 | env->cp15.c9_pmovsr &= ~value; | |
717 | } | |
718 | ||
719 | static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
720 | uint64_t value) | |
721 | { | |
722 | env->cp15.c9_pmxevtyper = value & 0xff; | |
723 | } | |
724 | ||
725 | static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
726 | uint64_t value) | |
727 | { | |
728 | env->cp15.c9_pmuserenr = value & 1; | |
729 | } | |
730 | ||
731 | static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
732 | uint64_t value) | |
733 | { | |
734 | /* We have no event counters so only the C bit can be changed */ | |
735 | value &= (1 << 31); | |
736 | env->cp15.c9_pminten |= value; | |
737 | } | |
738 | ||
739 | static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
740 | uint64_t value) | |
741 | { | |
742 | value &= (1 << 31); | |
743 | env->cp15.c9_pminten &= ~value; | |
744 | } | |
745 | ||
746 | static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
747 | uint64_t value) | |
748 | { | |
749 | /* Note that even though the AArch64 view of this register has bits | |
750 | * [10:0] all RES0 we can only mask the bottom 5, to comply with the | |
751 | * architectural requirements for bits which are RES0 only in some | |
752 | * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 | |
753 | * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) | |
754 | */ | |
755 | raw_write(env, ri, value & ~0x1FULL); | |
756 | } | |
757 | ||
758 | static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) | |
759 | { | |
760 | /* We only mask off bits that are RES0 both for AArch64 and AArch32. | |
761 | * For bits that vary between AArch32/64, code needs to check the | |
762 | * current execution mode before directly using the feature bit. | |
763 | */ | |
764 | uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK; | |
765 | ||
766 | if (!arm_feature(env, ARM_FEATURE_EL2)) { | |
767 | valid_mask &= ~SCR_HCE; | |
768 | ||
769 | /* On ARMv7, SMD (or SCD as it is called in v7) is only | |
770 | * supported if EL2 exists. The bit is UNK/SBZP when | |
771 | * EL2 is unavailable. In QEMU ARMv7, we force it to always zero | |
772 | * when EL2 is unavailable. | |
773 | */ | |
774 | if (arm_feature(env, ARM_FEATURE_V7)) { | |
775 | valid_mask &= ~SCR_SMD; | |
776 | } | |
777 | } | |
778 | ||
779 | /* Clear all-context RES0 bits. */ | |
780 | value &= valid_mask; | |
781 | raw_write(env, ri, value); | |
782 | } | |
783 | ||
784 | static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
785 | { | |
786 | ARMCPU *cpu = arm_env_get_cpu(env); | |
787 | ||
788 | /* Acquire the CSSELR index from the bank corresponding to the CCSIDR | |
789 | * bank | |
790 | */ | |
791 | uint32_t index = A32_BANKED_REG_GET(env, csselr, | |
792 | ri->secure & ARM_CP_SECSTATE_S); | |
793 | ||
794 | return cpu->ccsidr[index]; | |
795 | } | |
796 | ||
797 | static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
798 | uint64_t value) | |
799 | { | |
800 | raw_write(env, ri, value & 0xf); | |
801 | } | |
802 | ||
803 | static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
804 | { | |
805 | CPUState *cs = ENV_GET_CPU(env); | |
806 | uint64_t ret = 0; | |
807 | ||
808 | if (cs->interrupt_request & CPU_INTERRUPT_HARD) { | |
809 | ret |= CPSR_I; | |
810 | } | |
811 | if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { | |
812 | ret |= CPSR_F; | |
813 | } | |
814 | /* External aborts are not possible in QEMU so A bit is always clear */ | |
815 | return ret; | |
816 | } | |
817 | ||
818 | static const ARMCPRegInfo v7_cp_reginfo[] = { | |
819 | /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ | |
820 | { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, | |
821 | .access = PL1_W, .type = ARM_CP_NOP }, | |
822 | /* Performance monitors are implementation defined in v7, | |
823 | * but with an ARM recommended set of registers, which we | |
824 | * follow (although we don't actually implement any counters) | |
825 | * | |
826 | * Performance registers fall into three categories: | |
827 | * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) | |
828 | * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) | |
829 | * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) | |
830 | * For the cases controlled by PMUSERENR we must set .access to PL0_RW | |
831 | * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. | |
832 | */ | |
833 | { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, | |
834 | .access = PL0_RW, .type = ARM_CP_NO_MIGRATE, | |
835 | .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), | |
836 | .writefn = pmcntenset_write, | |
837 | .accessfn = pmreg_access, | |
838 | .raw_writefn = raw_write }, | |
839 | { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, | |
840 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, | |
841 | .access = PL0_RW, .accessfn = pmreg_access, | |
842 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, | |
843 | .writefn = pmcntenset_write, .raw_writefn = raw_write }, | |
844 | { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, | |
845 | .access = PL0_RW, | |
846 | .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), | |
847 | .accessfn = pmreg_access, | |
848 | .writefn = pmcntenclr_write, | |
849 | .type = ARM_CP_NO_MIGRATE }, | |
850 | { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, | |
851 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, | |
852 | .access = PL0_RW, .accessfn = pmreg_access, | |
853 | .type = ARM_CP_NO_MIGRATE, | |
854 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), | |
855 | .writefn = pmcntenclr_write }, | |
856 | { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, | |
857 | .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), | |
858 | .accessfn = pmreg_access, | |
859 | .writefn = pmovsr_write, | |
860 | .raw_writefn = raw_write }, | |
861 | /* Unimplemented so WI. */ | |
862 | { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, | |
863 | .access = PL0_W, .accessfn = pmreg_access, .type = ARM_CP_NOP }, | |
864 | /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE. | |
865 | * We choose to RAZ/WI. | |
866 | */ | |
867 | { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, | |
868 | .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0, | |
869 | .accessfn = pmreg_access }, | |
870 | #ifndef CONFIG_USER_ONLY | |
871 | { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, | |
872 | .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_IO, | |
873 | .readfn = pmccntr_read, .writefn = pmccntr_write32, | |
874 | .accessfn = pmreg_access }, | |
875 | { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, | |
876 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, | |
877 | .access = PL0_RW, .accessfn = pmreg_access, | |
878 | .type = ARM_CP_IO, | |
879 | .readfn = pmccntr_read, .writefn = pmccntr_write, }, | |
880 | #endif | |
881 | { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, | |
882 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, | |
883 | .writefn = pmccfiltr_write, | |
884 | .access = PL0_RW, .accessfn = pmreg_access, | |
885 | .type = ARM_CP_IO, | |
886 | .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), | |
887 | .resetvalue = 0, }, | |
888 | { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, | |
889 | .access = PL0_RW, | |
890 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper), | |
891 | .accessfn = pmreg_access, .writefn = pmxevtyper_write, | |
892 | .raw_writefn = raw_write }, | |
893 | /* Unimplemented, RAZ/WI. */ | |
894 | { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, | |
895 | .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0, | |
896 | .accessfn = pmreg_access }, | |
897 | { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, | |
898 | .access = PL0_R | PL1_RW, | |
899 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), | |
900 | .resetvalue = 0, | |
901 | .writefn = pmuserenr_write, .raw_writefn = raw_write }, | |
902 | { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, | |
903 | .access = PL1_RW, | |
904 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), | |
905 | .resetvalue = 0, | |
906 | .writefn = pmintenset_write, .raw_writefn = raw_write }, | |
907 | { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, | |
908 | .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, | |
909 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), | |
910 | .resetvalue = 0, .writefn = pmintenclr_write, }, | |
911 | { .name = "VBAR", .state = ARM_CP_STATE_BOTH, | |
912 | .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, | |
913 | .access = PL1_RW, .writefn = vbar_write, | |
914 | .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[1]), | |
915 | .resetvalue = 0 }, | |
916 | { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, | |
917 | .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, | |
918 | .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE }, | |
919 | { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, | |
920 | .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, | |
921 | .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0, | |
922 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), | |
923 | offsetof(CPUARMState, cp15.csselr_ns) } }, | |
924 | /* Auxiliary ID register: this actually has an IMPDEF value but for now | |
925 | * just RAZ for all cores: | |
926 | */ | |
927 | { .name = "AIDR", .state = ARM_CP_STATE_BOTH, | |
928 | .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, | |
929 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
930 | /* Auxiliary fault status registers: these also are IMPDEF, and we | |
931 | * choose to RAZ/WI for all cores. | |
932 | */ | |
933 | { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, | |
934 | .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, | |
935 | .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
936 | { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, | |
937 | .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, | |
938 | .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
939 | /* MAIR can just read-as-written because we don't implement caches | |
940 | * and so don't need to care about memory attributes. | |
941 | */ | |
942 | { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, | |
943 | .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, | |
944 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el1), | |
945 | .resetvalue = 0 }, | |
946 | /* For non-long-descriptor page tables these are PRRR and NMRR; | |
947 | * regardless they still act as reads-as-written for QEMU. | |
948 | * The override is necessary because of the overly-broad TLB_LOCKDOWN | |
949 | * definition. | |
950 | */ | |
951 | { .name = "MAIR0", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE, | |
952 | .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW, | |
953 | .fieldoffset = offsetoflow32(CPUARMState, cp15.mair_el1), | |
954 | .resetfn = arm_cp_reset_ignore }, | |
955 | { .name = "MAIR1", .state = ARM_CP_STATE_AA32, .type = ARM_CP_OVERRIDE, | |
956 | .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW, | |
957 | .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el1), | |
958 | .resetfn = arm_cp_reset_ignore }, | |
959 | { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, | |
960 | .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, | |
961 | .type = ARM_CP_NO_MIGRATE, .access = PL1_R, .readfn = isr_read }, | |
962 | /* 32 bit ITLB invalidates */ | |
963 | { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0, | |
964 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write }, | |
965 | { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, | |
966 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write }, | |
967 | { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2, | |
968 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write }, | |
969 | /* 32 bit DTLB invalidates */ | |
970 | { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0, | |
971 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write }, | |
972 | { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, | |
973 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write }, | |
974 | { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2, | |
975 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write }, | |
976 | /* 32 bit TLB invalidates */ | |
977 | { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, | |
978 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_write }, | |
979 | { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, | |
980 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write }, | |
981 | { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, | |
982 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiasid_write }, | |
983 | { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, | |
984 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write }, | |
985 | REGINFO_SENTINEL | |
986 | }; | |
987 | ||
988 | static const ARMCPRegInfo v7mp_cp_reginfo[] = { | |
989 | /* 32 bit TLB invalidates, Inner Shareable */ | |
990 | { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, | |
991 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbiall_is_write }, | |
992 | { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, | |
993 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_is_write }, | |
994 | { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, | |
995 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, | |
996 | .writefn = tlbiasid_is_write }, | |
997 | { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, | |
998 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, | |
999 | .writefn = tlbimvaa_is_write }, | |
1000 | REGINFO_SENTINEL | |
1001 | }; | |
1002 | ||
1003 | static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1004 | uint64_t value) | |
1005 | { | |
1006 | value &= 1; | |
1007 | env->teecr = value; | |
1008 | } | |
1009 | ||
1010 | static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
1011 | { | |
1012 | if (arm_current_el(env) == 0 && (env->teecr & 1)) { | |
1013 | return CP_ACCESS_TRAP; | |
1014 | } | |
1015 | return CP_ACCESS_OK; | |
1016 | } | |
1017 | ||
1018 | static const ARMCPRegInfo t2ee_cp_reginfo[] = { | |
1019 | { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, | |
1020 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), | |
1021 | .resetvalue = 0, | |
1022 | .writefn = teecr_write }, | |
1023 | { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, | |
1024 | .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), | |
1025 | .accessfn = teehbr_access, .resetvalue = 0 }, | |
1026 | REGINFO_SENTINEL | |
1027 | }; | |
1028 | ||
1029 | static const ARMCPRegInfo v6k_cp_reginfo[] = { | |
1030 | { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, | |
1031 | .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, | |
1032 | .access = PL0_RW, | |
1033 | .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el0), .resetvalue = 0 }, | |
1034 | { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, | |
1035 | .access = PL0_RW, | |
1036 | .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidr_el0), | |
1037 | .resetfn = arm_cp_reset_ignore }, | |
1038 | { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, | |
1039 | .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, | |
1040 | .access = PL0_R|PL1_W, | |
1041 | .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el0), .resetvalue = 0 }, | |
1042 | { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, | |
1043 | .access = PL0_R|PL1_W, | |
1044 | .fieldoffset = offsetoflow32(CPUARMState, cp15.tpidrro_el0), | |
1045 | .resetfn = arm_cp_reset_ignore }, | |
1046 | { .name = "TPIDR_EL1", .state = ARM_CP_STATE_BOTH, | |
1047 | .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, | |
1048 | .access = PL1_RW, | |
1049 | .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el1), .resetvalue = 0 }, | |
1050 | REGINFO_SENTINEL | |
1051 | }; | |
1052 | ||
1053 | #ifndef CONFIG_USER_ONLY | |
1054 | ||
1055 | static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
1056 | { | |
1057 | /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */ | |
1058 | if (arm_current_el(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) { | |
1059 | return CP_ACCESS_TRAP; | |
1060 | } | |
1061 | return CP_ACCESS_OK; | |
1062 | } | |
1063 | ||
1064 | static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx) | |
1065 | { | |
1066 | /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */ | |
1067 | if (arm_current_el(env) == 0 && | |
1068 | !extract32(env->cp15.c14_cntkctl, timeridx, 1)) { | |
1069 | return CP_ACCESS_TRAP; | |
1070 | } | |
1071 | return CP_ACCESS_OK; | |
1072 | } | |
1073 | ||
1074 | static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx) | |
1075 | { | |
1076 | /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if | |
1077 | * EL0[PV]TEN is zero. | |
1078 | */ | |
1079 | if (arm_current_el(env) == 0 && | |
1080 | !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { | |
1081 | return CP_ACCESS_TRAP; | |
1082 | } | |
1083 | return CP_ACCESS_OK; | |
1084 | } | |
1085 | ||
1086 | static CPAccessResult gt_pct_access(CPUARMState *env, | |
1087 | const ARMCPRegInfo *ri) | |
1088 | { | |
1089 | return gt_counter_access(env, GTIMER_PHYS); | |
1090 | } | |
1091 | ||
1092 | static CPAccessResult gt_vct_access(CPUARMState *env, | |
1093 | const ARMCPRegInfo *ri) | |
1094 | { | |
1095 | return gt_counter_access(env, GTIMER_VIRT); | |
1096 | } | |
1097 | ||
1098 | static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
1099 | { | |
1100 | return gt_timer_access(env, GTIMER_PHYS); | |
1101 | } | |
1102 | ||
1103 | static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
1104 | { | |
1105 | return gt_timer_access(env, GTIMER_VIRT); | |
1106 | } | |
1107 | ||
1108 | static uint64_t gt_get_countervalue(CPUARMState *env) | |
1109 | { | |
1110 | return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE; | |
1111 | } | |
1112 | ||
1113 | static void gt_recalc_timer(ARMCPU *cpu, int timeridx) | |
1114 | { | |
1115 | ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; | |
1116 | ||
1117 | if (gt->ctl & 1) { | |
1118 | /* Timer enabled: calculate and set current ISTATUS, irq, and | |
1119 | * reset timer to when ISTATUS next has to change | |
1120 | */ | |
1121 | uint64_t count = gt_get_countervalue(&cpu->env); | |
1122 | /* Note that this must be unsigned 64 bit arithmetic: */ | |
1123 | int istatus = count >= gt->cval; | |
1124 | uint64_t nexttick; | |
1125 | ||
1126 | gt->ctl = deposit32(gt->ctl, 2, 1, istatus); | |
1127 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], | |
1128 | (istatus && !(gt->ctl & 2))); | |
1129 | if (istatus) { | |
1130 | /* Next transition is when count rolls back over to zero */ | |
1131 | nexttick = UINT64_MAX; | |
1132 | } else { | |
1133 | /* Next transition is when we hit cval */ | |
1134 | nexttick = gt->cval; | |
1135 | } | |
1136 | /* Note that the desired next expiry time might be beyond the | |
1137 | * signed-64-bit range of a QEMUTimer -- in this case we just | |
1138 | * set the timer for as far in the future as possible. When the | |
1139 | * timer expires we will reset the timer for any remaining period. | |
1140 | */ | |
1141 | if (nexttick > INT64_MAX / GTIMER_SCALE) { | |
1142 | nexttick = INT64_MAX / GTIMER_SCALE; | |
1143 | } | |
1144 | timer_mod(cpu->gt_timer[timeridx], nexttick); | |
1145 | } else { | |
1146 | /* Timer disabled: ISTATUS and timer output always clear */ | |
1147 | gt->ctl &= ~4; | |
1148 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0); | |
1149 | timer_del(cpu->gt_timer[timeridx]); | |
1150 | } | |
1151 | } | |
1152 | ||
1153 | static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri) | |
1154 | { | |
1155 | ARMCPU *cpu = arm_env_get_cpu(env); | |
1156 | int timeridx = ri->opc1 & 1; | |
1157 | ||
1158 | timer_del(cpu->gt_timer[timeridx]); | |
1159 | } | |
1160 | ||
1161 | static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1162 | { | |
1163 | return gt_get_countervalue(env); | |
1164 | } | |
1165 | ||
1166 | static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1167 | uint64_t value) | |
1168 | { | |
1169 | int timeridx = ri->opc1 & 1; | |
1170 | ||
1171 | env->cp15.c14_timer[timeridx].cval = value; | |
1172 | gt_recalc_timer(arm_env_get_cpu(env), timeridx); | |
1173 | } | |
1174 | ||
1175 | static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1176 | { | |
1177 | int timeridx = ri->crm & 1; | |
1178 | ||
1179 | return (uint32_t)(env->cp15.c14_timer[timeridx].cval - | |
1180 | gt_get_countervalue(env)); | |
1181 | } | |
1182 | ||
1183 | static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1184 | uint64_t value) | |
1185 | { | |
1186 | int timeridx = ri->crm & 1; | |
1187 | ||
1188 | env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) + | |
1189 | + sextract64(value, 0, 32); | |
1190 | gt_recalc_timer(arm_env_get_cpu(env), timeridx); | |
1191 | } | |
1192 | ||
1193 | static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1194 | uint64_t value) | |
1195 | { | |
1196 | ARMCPU *cpu = arm_env_get_cpu(env); | |
1197 | int timeridx = ri->crm & 1; | |
1198 | uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; | |
1199 | ||
1200 | env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); | |
1201 | if ((oldval ^ value) & 1) { | |
1202 | /* Enable toggled */ | |
1203 | gt_recalc_timer(cpu, timeridx); | |
1204 | } else if ((oldval ^ value) & 2) { | |
1205 | /* IMASK toggled: don't need to recalculate, | |
1206 | * just set the interrupt line based on ISTATUS | |
1207 | */ | |
1208 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], | |
1209 | (oldval & 4) && !(value & 2)); | |
1210 | } | |
1211 | } | |
1212 | ||
1213 | void arm_gt_ptimer_cb(void *opaque) | |
1214 | { | |
1215 | ARMCPU *cpu = opaque; | |
1216 | ||
1217 | gt_recalc_timer(cpu, GTIMER_PHYS); | |
1218 | } | |
1219 | ||
1220 | void arm_gt_vtimer_cb(void *opaque) | |
1221 | { | |
1222 | ARMCPU *cpu = opaque; | |
1223 | ||
1224 | gt_recalc_timer(cpu, GTIMER_VIRT); | |
1225 | } | |
1226 | ||
1227 | static const ARMCPRegInfo generic_timer_cp_reginfo[] = { | |
1228 | /* Note that CNTFRQ is purely reads-as-written for the benefit | |
1229 | * of software; writing it doesn't actually change the timer frequency. | |
1230 | * Our reset value matches the fixed frequency we implement the timer at. | |
1231 | */ | |
1232 | { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, | |
1233 | .type = ARM_CP_NO_MIGRATE, | |
1234 | .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, | |
1235 | .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), | |
1236 | .resetfn = arm_cp_reset_ignore, | |
1237 | }, | |
1238 | { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, | |
1239 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, | |
1240 | .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, | |
1241 | .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), | |
1242 | .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE, | |
1243 | }, | |
1244 | /* overall control: mostly access permissions */ | |
1245 | { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, | |
1246 | .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, | |
1247 | .access = PL1_RW, | |
1248 | .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), | |
1249 | .resetvalue = 0, | |
1250 | }, | |
1251 | /* per-timer control */ | |
1252 | { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, | |
1253 | .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R, | |
1254 | .accessfn = gt_ptimer_access, | |
1255 | .fieldoffset = offsetoflow32(CPUARMState, | |
1256 | cp15.c14_timer[GTIMER_PHYS].ctl), | |
1257 | .resetfn = arm_cp_reset_ignore, | |
1258 | .writefn = gt_ctl_write, .raw_writefn = raw_write, | |
1259 | }, | |
1260 | { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, | |
1261 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, | |
1262 | .type = ARM_CP_IO, .access = PL1_RW | PL0_R, | |
1263 | .accessfn = gt_ptimer_access, | |
1264 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), | |
1265 | .resetvalue = 0, | |
1266 | .writefn = gt_ctl_write, .raw_writefn = raw_write, | |
1267 | }, | |
1268 | { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, | |
1269 | .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, .access = PL1_RW | PL0_R, | |
1270 | .accessfn = gt_vtimer_access, | |
1271 | .fieldoffset = offsetoflow32(CPUARMState, | |
1272 | cp15.c14_timer[GTIMER_VIRT].ctl), | |
1273 | .resetfn = arm_cp_reset_ignore, | |
1274 | .writefn = gt_ctl_write, .raw_writefn = raw_write, | |
1275 | }, | |
1276 | { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, | |
1277 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, | |
1278 | .type = ARM_CP_IO, .access = PL1_RW | PL0_R, | |
1279 | .accessfn = gt_vtimer_access, | |
1280 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), | |
1281 | .resetvalue = 0, | |
1282 | .writefn = gt_ctl_write, .raw_writefn = raw_write, | |
1283 | }, | |
1284 | /* TimerValue views: a 32 bit downcounting view of the underlying state */ | |
1285 | { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, | |
1286 | .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R, | |
1287 | .accessfn = gt_ptimer_access, | |
1288 | .readfn = gt_tval_read, .writefn = gt_tval_write, | |
1289 | }, | |
1290 | { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, | |
1291 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, | |
1292 | .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R, | |
1293 | .readfn = gt_tval_read, .writefn = gt_tval_write, | |
1294 | }, | |
1295 | { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, | |
1296 | .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R, | |
1297 | .accessfn = gt_vtimer_access, | |
1298 | .readfn = gt_tval_read, .writefn = gt_tval_write, | |
1299 | }, | |
1300 | { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, | |
1301 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, | |
1302 | .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R, | |
1303 | .readfn = gt_tval_read, .writefn = gt_tval_write, | |
1304 | }, | |
1305 | /* The counter itself */ | |
1306 | { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, | |
1307 | .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO, | |
1308 | .accessfn = gt_pct_access, | |
1309 | .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, | |
1310 | }, | |
1311 | { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, | |
1312 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, | |
1313 | .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, | |
1314 | .accessfn = gt_pct_access, | |
1315 | .readfn = gt_cnt_read, .resetfn = gt_cnt_reset, | |
1316 | }, | |
1317 | { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, | |
1318 | .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO, | |
1319 | .accessfn = gt_vct_access, | |
1320 | .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, | |
1321 | }, | |
1322 | { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, | |
1323 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, | |
1324 | .access = PL0_R, .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, | |
1325 | .accessfn = gt_vct_access, | |
1326 | .readfn = gt_cnt_read, .resetfn = gt_cnt_reset, | |
1327 | }, | |
1328 | /* Comparison value, indicating when the timer goes off */ | |
1329 | { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, | |
1330 | .access = PL1_RW | PL0_R, | |
1331 | .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE, | |
1332 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), | |
1333 | .accessfn = gt_ptimer_access, .resetfn = arm_cp_reset_ignore, | |
1334 | .writefn = gt_cval_write, .raw_writefn = raw_write, | |
1335 | }, | |
1336 | { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, | |
1337 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, | |
1338 | .access = PL1_RW | PL0_R, | |
1339 | .type = ARM_CP_IO, | |
1340 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), | |
1341 | .resetvalue = 0, .accessfn = gt_vtimer_access, | |
1342 | .writefn = gt_cval_write, .raw_writefn = raw_write, | |
1343 | }, | |
1344 | { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, | |
1345 | .access = PL1_RW | PL0_R, | |
1346 | .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_NO_MIGRATE, | |
1347 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), | |
1348 | .accessfn = gt_vtimer_access, .resetfn = arm_cp_reset_ignore, | |
1349 | .writefn = gt_cval_write, .raw_writefn = raw_write, | |
1350 | }, | |
1351 | { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, | |
1352 | .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, | |
1353 | .access = PL1_RW | PL0_R, | |
1354 | .type = ARM_CP_IO, | |
1355 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), | |
1356 | .resetvalue = 0, .accessfn = gt_vtimer_access, | |
1357 | .writefn = gt_cval_write, .raw_writefn = raw_write, | |
1358 | }, | |
1359 | REGINFO_SENTINEL | |
1360 | }; | |
1361 | ||
1362 | #else | |
1363 | /* In user-mode none of the generic timer registers are accessible, | |
1364 | * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs, | |
1365 | * so instead just don't register any of them. | |
1366 | */ | |
1367 | static const ARMCPRegInfo generic_timer_cp_reginfo[] = { | |
1368 | REGINFO_SENTINEL | |
1369 | }; | |
1370 | ||
1371 | #endif | |
1372 | ||
1373 | static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) | |
1374 | { | |
1375 | if (arm_feature(env, ARM_FEATURE_LPAE)) { | |
1376 | raw_write(env, ri, value); | |
1377 | } else if (arm_feature(env, ARM_FEATURE_V7)) { | |
1378 | raw_write(env, ri, value & 0xfffff6ff); | |
1379 | } else { | |
1380 | raw_write(env, ri, value & 0xfffff1ff); | |
1381 | } | |
1382 | } | |
1383 | ||
1384 | #ifndef CONFIG_USER_ONLY | |
1385 | /* get_phys_addr() isn't present for user-mode-only targets */ | |
1386 | ||
1387 | static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
1388 | { | |
1389 | if (ri->opc2 & 4) { | |
1390 | /* Other states are only available with TrustZone; in | |
1391 | * a non-TZ implementation these registers don't exist | |
1392 | * at all, which is an Uncategorized trap. This underdecoding | |
1393 | * is safe because the reginfo is NO_MIGRATE. | |
1394 | */ | |
1395 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
1396 | } | |
1397 | return CP_ACCESS_OK; | |
1398 | } | |
1399 | ||
1400 | static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) | |
1401 | { | |
1402 | hwaddr phys_addr; | |
1403 | target_ulong page_size; | |
1404 | int prot; | |
1405 | int ret, is_user = ri->opc2 & 2; | |
1406 | int access_type = ri->opc2 & 1; | |
1407 | uint64_t par64; | |
1408 | ||
1409 | ret = get_phys_addr(env, value, access_type, is_user, | |
1410 | &phys_addr, &prot, &page_size); | |
1411 | if (extended_addresses_enabled(env)) { | |
1412 | /* ret is a DFSR/IFSR value for the long descriptor | |
1413 | * translation table format, but with WnR always clear. | |
1414 | * Convert it to a 64-bit PAR. | |
1415 | */ | |
1416 | par64 = (1 << 11); /* LPAE bit always set */ | |
1417 | if (ret == 0) { | |
1418 | par64 |= phys_addr & ~0xfffULL; | |
1419 | /* We don't set the ATTR or SH fields in the PAR. */ | |
1420 | } else { | |
1421 | par64 |= 1; /* F */ | |
1422 | par64 |= (ret & 0x3f) << 1; /* FS */ | |
1423 | /* Note that S2WLK and FSTAGE are always zero, because we don't | |
1424 | * implement virtualization and therefore there can't be a stage 2 | |
1425 | * fault. | |
1426 | */ | |
1427 | } | |
1428 | } else { | |
1429 | /* ret is a DFSR/IFSR value for the short descriptor | |
1430 | * translation table format (with WnR always clear). | |
1431 | * Convert it to a 32-bit PAR. | |
1432 | */ | |
1433 | if (ret == 0) { | |
1434 | /* We do not set any attribute bits in the PAR */ | |
1435 | if (page_size == (1 << 24) | |
1436 | && arm_feature(env, ARM_FEATURE_V7)) { | |
1437 | par64 = (phys_addr & 0xff000000) | (1 << 1); | |
1438 | } else { | |
1439 | par64 = phys_addr & 0xfffff000; | |
1440 | } | |
1441 | } else { | |
1442 | par64 = ((ret & (1 << 10)) >> 5) | ((ret & (1 << 12)) >> 6) | | |
1443 | ((ret & 0xf) << 1) | 1; | |
1444 | } | |
1445 | } | |
1446 | ||
1447 | A32_BANKED_CURRENT_REG_SET(env, par, par64); | |
1448 | } | |
1449 | #endif | |
1450 | ||
1451 | static const ARMCPRegInfo vapa_cp_reginfo[] = { | |
1452 | { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, | |
1453 | .access = PL1_RW, .resetvalue = 0, | |
1454 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), | |
1455 | offsetoflow32(CPUARMState, cp15.par_ns) }, | |
1456 | .writefn = par_write }, | |
1457 | #ifndef CONFIG_USER_ONLY | |
1458 | { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, | |
1459 | .access = PL1_W, .accessfn = ats_access, | |
1460 | .writefn = ats_write, .type = ARM_CP_NO_MIGRATE }, | |
1461 | #endif | |
1462 | REGINFO_SENTINEL | |
1463 | }; | |
1464 | ||
1465 | /* Return basic MPU access permission bits. */ | |
1466 | static uint32_t simple_mpu_ap_bits(uint32_t val) | |
1467 | { | |
1468 | uint32_t ret; | |
1469 | uint32_t mask; | |
1470 | int i; | |
1471 | ret = 0; | |
1472 | mask = 3; | |
1473 | for (i = 0; i < 16; i += 2) { | |
1474 | ret |= (val >> i) & mask; | |
1475 | mask <<= 2; | |
1476 | } | |
1477 | return ret; | |
1478 | } | |
1479 | ||
1480 | /* Pad basic MPU access permission bits to extended format. */ | |
1481 | static uint32_t extended_mpu_ap_bits(uint32_t val) | |
1482 | { | |
1483 | uint32_t ret; | |
1484 | uint32_t mask; | |
1485 | int i; | |
1486 | ret = 0; | |
1487 | mask = 3; | |
1488 | for (i = 0; i < 16; i += 2) { | |
1489 | ret |= (val & mask) << i; | |
1490 | mask <<= 2; | |
1491 | } | |
1492 | return ret; | |
1493 | } | |
1494 | ||
1495 | static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1496 | uint64_t value) | |
1497 | { | |
1498 | env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); | |
1499 | } | |
1500 | ||
1501 | static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1502 | { | |
1503 | return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); | |
1504 | } | |
1505 | ||
1506 | static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1507 | uint64_t value) | |
1508 | { | |
1509 | env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); | |
1510 | } | |
1511 | ||
1512 | static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1513 | { | |
1514 | return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); | |
1515 | } | |
1516 | ||
1517 | static const ARMCPRegInfo pmsav5_cp_reginfo[] = { | |
1518 | { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, | |
1519 | .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, | |
1520 | .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), | |
1521 | .resetvalue = 0, | |
1522 | .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, | |
1523 | { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, | |
1524 | .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, | |
1525 | .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), | |
1526 | .resetvalue = 0, | |
1527 | .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, | |
1528 | { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, | |
1529 | .access = PL1_RW, | |
1530 | .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), | |
1531 | .resetvalue = 0, }, | |
1532 | { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, | |
1533 | .access = PL1_RW, | |
1534 | .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), | |
1535 | .resetvalue = 0, }, | |
1536 | { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, | |
1537 | .access = PL1_RW, | |
1538 | .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, | |
1539 | { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, | |
1540 | .access = PL1_RW, | |
1541 | .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, | |
1542 | /* Protection region base and size registers */ | |
1543 | { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, | |
1544 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
1545 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, | |
1546 | { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, | |
1547 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
1548 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, | |
1549 | { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, | |
1550 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
1551 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, | |
1552 | { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, | |
1553 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
1554 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, | |
1555 | { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, | |
1556 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
1557 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, | |
1558 | { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, | |
1559 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
1560 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, | |
1561 | { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, | |
1562 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
1563 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, | |
1564 | { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, | |
1565 | .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, | |
1566 | .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, | |
1567 | REGINFO_SENTINEL | |
1568 | }; | |
1569 | ||
1570 | static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1571 | uint64_t value) | |
1572 | { | |
1573 | TCR *tcr = raw_ptr(env, ri); | |
1574 | int maskshift = extract32(value, 0, 3); | |
1575 | ||
1576 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
1577 | if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { | |
1578 | /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when | |
1579 | * using Long-desciptor translation table format */ | |
1580 | value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); | |
1581 | } else if (arm_feature(env, ARM_FEATURE_EL3)) { | |
1582 | /* In an implementation that includes the Security Extensions | |
1583 | * TTBCR has additional fields PD0 [4] and PD1 [5] for | |
1584 | * Short-descriptor translation table format. | |
1585 | */ | |
1586 | value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; | |
1587 | } else { | |
1588 | value &= TTBCR_N; | |
1589 | } | |
1590 | } | |
1591 | ||
1592 | /* Update the masks corresponding to the the TCR bank being written | |
1593 | * Note that we always calculate mask and base_mask, but | |
1594 | * they are only used for short-descriptor tables (ie if EAE is 0); | |
1595 | * for long-descriptor tables the TCR fields are used differently | |
1596 | * and the mask and base_mask values are meaningless. | |
1597 | */ | |
1598 | tcr->raw_tcr = value; | |
1599 | tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift); | |
1600 | tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift); | |
1601 | } | |
1602 | ||
1603 | static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1604 | uint64_t value) | |
1605 | { | |
1606 | ARMCPU *cpu = arm_env_get_cpu(env); | |
1607 | ||
1608 | if (arm_feature(env, ARM_FEATURE_LPAE)) { | |
1609 | /* With LPAE the TTBCR could result in a change of ASID | |
1610 | * via the TTBCR.A1 bit, so do a TLB flush. | |
1611 | */ | |
1612 | tlb_flush(CPU(cpu), 1); | |
1613 | } | |
1614 | vmsa_ttbcr_raw_write(env, ri, value); | |
1615 | } | |
1616 | ||
1617 | static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) | |
1618 | { | |
1619 | TCR *tcr = raw_ptr(env, ri); | |
1620 | ||
1621 | /* Reset both the TCR as well as the masks corresponding to the bank of | |
1622 | * the TCR being reset. | |
1623 | */ | |
1624 | tcr->raw_tcr = 0; | |
1625 | tcr->mask = 0; | |
1626 | tcr->base_mask = 0xffffc000u; | |
1627 | } | |
1628 | ||
1629 | static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1630 | uint64_t value) | |
1631 | { | |
1632 | ARMCPU *cpu = arm_env_get_cpu(env); | |
1633 | TCR *tcr = raw_ptr(env, ri); | |
1634 | ||
1635 | /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ | |
1636 | tlb_flush(CPU(cpu), 1); | |
1637 | tcr->raw_tcr = value; | |
1638 | } | |
1639 | ||
1640 | static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1641 | uint64_t value) | |
1642 | { | |
1643 | /* 64 bit accesses to the TTBRs can change the ASID and so we | |
1644 | * must flush the TLB. | |
1645 | */ | |
1646 | if (cpreg_field_is_64bit(ri)) { | |
1647 | ARMCPU *cpu = arm_env_get_cpu(env); | |
1648 | ||
1649 | tlb_flush(CPU(cpu), 1); | |
1650 | } | |
1651 | raw_write(env, ri, value); | |
1652 | } | |
1653 | ||
1654 | static const ARMCPRegInfo vmsa_cp_reginfo[] = { | |
1655 | { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, | |
1656 | .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, | |
1657 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), | |
1658 | offsetoflow32(CPUARMState, cp15.dfsr_ns) }, | |
1659 | .resetfn = arm_cp_reset_ignore, }, | |
1660 | { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, | |
1661 | .access = PL1_RW, .resetvalue = 0, | |
1662 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), | |
1663 | offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, | |
1664 | { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, | |
1665 | .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, | |
1666 | .access = PL1_RW, | |
1667 | .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, | |
1668 | { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, | |
1669 | .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, | |
1670 | .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, | |
1671 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), | |
1672 | offsetof(CPUARMState, cp15.ttbr0_ns) } }, | |
1673 | { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, | |
1674 | .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, | |
1675 | .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, | |
1676 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), | |
1677 | offsetof(CPUARMState, cp15.ttbr1_ns) } }, | |
1678 | { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, | |
1679 | .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, | |
1680 | .access = PL1_RW, .writefn = vmsa_tcr_el1_write, | |
1681 | .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write, | |
1682 | .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, | |
1683 | { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, | |
1684 | .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, .writefn = vmsa_ttbcr_write, | |
1685 | .resetfn = arm_cp_reset_ignore, .raw_writefn = vmsa_ttbcr_raw_write, | |
1686 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), | |
1687 | offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, | |
1688 | { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, | |
1689 | .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, | |
1690 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), | |
1691 | .resetvalue = 0, }, | |
1692 | { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, | |
1693 | .access = PL1_RW, .resetvalue = 0, | |
1694 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), | |
1695 | offsetof(CPUARMState, cp15.dfar_ns) } }, | |
1696 | REGINFO_SENTINEL | |
1697 | }; | |
1698 | ||
1699 | static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1700 | uint64_t value) | |
1701 | { | |
1702 | env->cp15.c15_ticonfig = value & 0xe7; | |
1703 | /* The OS_TYPE bit in this register changes the reported CPUID! */ | |
1704 | env->cp15.c0_cpuid = (value & (1 << 5)) ? | |
1705 | ARM_CPUID_TI915T : ARM_CPUID_TI925T; | |
1706 | } | |
1707 | ||
1708 | static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1709 | uint64_t value) | |
1710 | { | |
1711 | env->cp15.c15_threadid = value & 0xffff; | |
1712 | } | |
1713 | ||
1714 | static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1715 | uint64_t value) | |
1716 | { | |
1717 | /* Wait-for-interrupt (deprecated) */ | |
1718 | cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT); | |
1719 | } | |
1720 | ||
1721 | static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1722 | uint64_t value) | |
1723 | { | |
1724 | /* On OMAP there are registers indicating the max/min index of dcache lines | |
1725 | * containing a dirty line; cache flush operations have to reset these. | |
1726 | */ | |
1727 | env->cp15.c15_i_max = 0x000; | |
1728 | env->cp15.c15_i_min = 0xff0; | |
1729 | } | |
1730 | ||
1731 | static const ARMCPRegInfo omap_cp_reginfo[] = { | |
1732 | { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, | |
1733 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, | |
1734 | .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), | |
1735 | .resetvalue = 0, }, | |
1736 | { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, | |
1737 | .access = PL1_RW, .type = ARM_CP_NOP }, | |
1738 | { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, | |
1739 | .access = PL1_RW, | |
1740 | .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, | |
1741 | .writefn = omap_ticonfig_write }, | |
1742 | { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, | |
1743 | .access = PL1_RW, | |
1744 | .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, | |
1745 | { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, | |
1746 | .access = PL1_RW, .resetvalue = 0xff0, | |
1747 | .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, | |
1748 | { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, | |
1749 | .access = PL1_RW, | |
1750 | .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, | |
1751 | .writefn = omap_threadid_write }, | |
1752 | { .name = "TI925T_STATUS", .cp = 15, .crn = 15, | |
1753 | .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, | |
1754 | .type = ARM_CP_NO_MIGRATE, | |
1755 | .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, | |
1756 | /* TODO: Peripheral port remap register: | |
1757 | * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller | |
1758 | * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), | |
1759 | * when MMU is off. | |
1760 | */ | |
1761 | { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, | |
1762 | .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, | |
1763 | .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE, | |
1764 | .writefn = omap_cachemaint_write }, | |
1765 | { .name = "C9", .cp = 15, .crn = 9, | |
1766 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, | |
1767 | .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, | |
1768 | REGINFO_SENTINEL | |
1769 | }; | |
1770 | ||
1771 | static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1772 | uint64_t value) | |
1773 | { | |
1774 | env->cp15.c15_cpar = value & 0x3fff; | |
1775 | } | |
1776 | ||
1777 | static const ARMCPRegInfo xscale_cp_reginfo[] = { | |
1778 | { .name = "XSCALE_CPAR", | |
1779 | .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, | |
1780 | .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, | |
1781 | .writefn = xscale_cpar_write, }, | |
1782 | { .name = "XSCALE_AUXCR", | |
1783 | .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, | |
1784 | .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), | |
1785 | .resetvalue = 0, }, | |
1786 | /* XScale specific cache-lockdown: since we have no cache we NOP these | |
1787 | * and hope the guest does not really rely on cache behaviour. | |
1788 | */ | |
1789 | { .name = "XSCALE_LOCK_ICACHE_LINE", | |
1790 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, | |
1791 | .access = PL1_W, .type = ARM_CP_NOP }, | |
1792 | { .name = "XSCALE_UNLOCK_ICACHE", | |
1793 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, | |
1794 | .access = PL1_W, .type = ARM_CP_NOP }, | |
1795 | { .name = "XSCALE_DCACHE_LOCK", | |
1796 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, | |
1797 | .access = PL1_RW, .type = ARM_CP_NOP }, | |
1798 | { .name = "XSCALE_UNLOCK_DCACHE", | |
1799 | .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, | |
1800 | .access = PL1_W, .type = ARM_CP_NOP }, | |
1801 | REGINFO_SENTINEL | |
1802 | }; | |
1803 | ||
1804 | static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { | |
1805 | /* RAZ/WI the whole crn=15 space, when we don't have a more specific | |
1806 | * implementation of this implementation-defined space. | |
1807 | * Ideally this should eventually disappear in favour of actually | |
1808 | * implementing the correct behaviour for all cores. | |
1809 | */ | |
1810 | { .name = "C15_IMPDEF", .cp = 15, .crn = 15, | |
1811 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, | |
1812 | .access = PL1_RW, | |
1813 | .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE | ARM_CP_OVERRIDE, | |
1814 | .resetvalue = 0 }, | |
1815 | REGINFO_SENTINEL | |
1816 | }; | |
1817 | ||
1818 | static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { | |
1819 | /* Cache status: RAZ because we have no cache so it's always clean */ | |
1820 | { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, | |
1821 | .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, | |
1822 | .resetvalue = 0 }, | |
1823 | REGINFO_SENTINEL | |
1824 | }; | |
1825 | ||
1826 | static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { | |
1827 | /* We never have a a block transfer operation in progress */ | |
1828 | { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, | |
1829 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, | |
1830 | .resetvalue = 0 }, | |
1831 | /* The cache ops themselves: these all NOP for QEMU */ | |
1832 | { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, | |
1833 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
1834 | { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, | |
1835 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
1836 | { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, | |
1837 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
1838 | { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, | |
1839 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
1840 | { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, | |
1841 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
1842 | { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, | |
1843 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, | |
1844 | REGINFO_SENTINEL | |
1845 | }; | |
1846 | ||
1847 | static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { | |
1848 | /* The cache test-and-clean instructions always return (1 << 30) | |
1849 | * to indicate that there are no dirty cache lines. | |
1850 | */ | |
1851 | { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, | |
1852 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, | |
1853 | .resetvalue = (1 << 30) }, | |
1854 | { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, | |
1855 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, | |
1856 | .resetvalue = (1 << 30) }, | |
1857 | REGINFO_SENTINEL | |
1858 | }; | |
1859 | ||
1860 | static const ARMCPRegInfo strongarm_cp_reginfo[] = { | |
1861 | /* Ignore ReadBuffer accesses */ | |
1862 | { .name = "C9_READBUFFER", .cp = 15, .crn = 9, | |
1863 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, | |
1864 | .access = PL1_RW, .resetvalue = 0, | |
1865 | .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE }, | |
1866 | REGINFO_SENTINEL | |
1867 | }; | |
1868 | ||
1869 | static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1870 | { | |
1871 | CPUState *cs = CPU(arm_env_get_cpu(env)); | |
1872 | uint32_t mpidr = cs->cpu_index; | |
1873 | /* We don't support setting cluster ID ([8..11]) (known as Aff1 | |
1874 | * in later ARM ARM versions), or any of the higher affinity level fields, | |
1875 | * so these bits always RAZ. | |
1876 | */ | |
1877 | if (arm_feature(env, ARM_FEATURE_V7MP)) { | |
1878 | mpidr |= (1U << 31); | |
1879 | /* Cores which are uniprocessor (non-coherent) | |
1880 | * but still implement the MP extensions set | |
1881 | * bit 30. (For instance, A9UP.) However we do | |
1882 | * not currently model any of those cores. | |
1883 | */ | |
1884 | } | |
1885 | return mpidr; | |
1886 | } | |
1887 | ||
1888 | static const ARMCPRegInfo mpidr_cp_reginfo[] = { | |
1889 | { .name = "MPIDR", .state = ARM_CP_STATE_BOTH, | |
1890 | .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, | |
1891 | .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE }, | |
1892 | REGINFO_SENTINEL | |
1893 | }; | |
1894 | ||
1895 | static const ARMCPRegInfo lpae_cp_reginfo[] = { | |
1896 | /* NOP AMAIR0/1: the override is because these clash with the rather | |
1897 | * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo. | |
1898 | */ | |
1899 | { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, | |
1900 | .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, | |
1901 | .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE, | |
1902 | .resetvalue = 0 }, | |
1903 | /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ | |
1904 | { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, | |
1905 | .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE, | |
1906 | .resetvalue = 0 }, | |
1907 | { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, | |
1908 | .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, | |
1909 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), | |
1910 | offsetof(CPUARMState, cp15.par_ns)} }, | |
1911 | { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, | |
1912 | .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE, | |
1913 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), | |
1914 | offsetof(CPUARMState, cp15.ttbr0_ns) }, | |
1915 | .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore }, | |
1916 | { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, | |
1917 | .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE, | |
1918 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), | |
1919 | offsetof(CPUARMState, cp15.ttbr1_ns) }, | |
1920 | .writefn = vmsa_ttbr_write, .resetfn = arm_cp_reset_ignore }, | |
1921 | REGINFO_SENTINEL | |
1922 | }; | |
1923 | ||
1924 | static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1925 | { | |
1926 | return vfp_get_fpcr(env); | |
1927 | } | |
1928 | ||
1929 | static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1930 | uint64_t value) | |
1931 | { | |
1932 | vfp_set_fpcr(env, value); | |
1933 | } | |
1934 | ||
1935 | static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
1936 | { | |
1937 | return vfp_get_fpsr(env); | |
1938 | } | |
1939 | ||
1940 | static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1941 | uint64_t value) | |
1942 | { | |
1943 | vfp_set_fpsr(env, value); | |
1944 | } | |
1945 | ||
1946 | static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
1947 | { | |
1948 | if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) { | |
1949 | return CP_ACCESS_TRAP; | |
1950 | } | |
1951 | return CP_ACCESS_OK; | |
1952 | } | |
1953 | ||
1954 | static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1955 | uint64_t value) | |
1956 | { | |
1957 | env->daif = value & PSTATE_DAIF; | |
1958 | } | |
1959 | ||
1960 | static CPAccessResult aa64_cacheop_access(CPUARMState *env, | |
1961 | const ARMCPRegInfo *ri) | |
1962 | { | |
1963 | /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless | |
1964 | * SCTLR_EL1.UCI is set. | |
1965 | */ | |
1966 | if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) { | |
1967 | return CP_ACCESS_TRAP; | |
1968 | } | |
1969 | return CP_ACCESS_OK; | |
1970 | } | |
1971 | ||
1972 | /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions | |
1973 | * Page D4-1736 (DDI0487A.b) | |
1974 | */ | |
1975 | ||
1976 | static void tlbi_aa64_va_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1977 | uint64_t value) | |
1978 | { | |
1979 | /* Invalidate by VA (AArch64 version) */ | |
1980 | ARMCPU *cpu = arm_env_get_cpu(env); | |
1981 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
1982 | ||
1983 | tlb_flush_page(CPU(cpu), pageaddr); | |
1984 | } | |
1985 | ||
1986 | static void tlbi_aa64_vaa_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1987 | uint64_t value) | |
1988 | { | |
1989 | /* Invalidate by VA, all ASIDs (AArch64 version) */ | |
1990 | ARMCPU *cpu = arm_env_get_cpu(env); | |
1991 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
1992 | ||
1993 | tlb_flush_page(CPU(cpu), pageaddr); | |
1994 | } | |
1995 | ||
1996 | static void tlbi_aa64_asid_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
1997 | uint64_t value) | |
1998 | { | |
1999 | /* Invalidate by ASID (AArch64 version) */ | |
2000 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2001 | int asid = extract64(value, 48, 16); | |
2002 | tlb_flush(CPU(cpu), asid == 0); | |
2003 | } | |
2004 | ||
2005 | static void tlbi_aa64_va_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2006 | uint64_t value) | |
2007 | { | |
2008 | CPUState *other_cs; | |
2009 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
2010 | ||
2011 | CPU_FOREACH(other_cs) { | |
2012 | tlb_flush_page(other_cs, pageaddr); | |
2013 | } | |
2014 | } | |
2015 | ||
2016 | static void tlbi_aa64_vaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2017 | uint64_t value) | |
2018 | { | |
2019 | CPUState *other_cs; | |
2020 | uint64_t pageaddr = sextract64(value << 12, 0, 56); | |
2021 | ||
2022 | CPU_FOREACH(other_cs) { | |
2023 | tlb_flush_page(other_cs, pageaddr); | |
2024 | } | |
2025 | } | |
2026 | ||
2027 | static void tlbi_aa64_asid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2028 | uint64_t value) | |
2029 | { | |
2030 | CPUState *other_cs; | |
2031 | int asid = extract64(value, 48, 16); | |
2032 | ||
2033 | CPU_FOREACH(other_cs) { | |
2034 | tlb_flush(other_cs, asid == 0); | |
2035 | } | |
2036 | } | |
2037 | ||
2038 | static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
2039 | { | |
2040 | /* We don't implement EL2, so the only control on DC ZVA is the | |
2041 | * bit in the SCTLR which can prohibit access for EL0. | |
2042 | */ | |
2043 | if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) { | |
2044 | return CP_ACCESS_TRAP; | |
2045 | } | |
2046 | return CP_ACCESS_OK; | |
2047 | } | |
2048 | ||
2049 | static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
2050 | { | |
2051 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2052 | int dzp_bit = 1 << 4; | |
2053 | ||
2054 | /* DZP indicates whether DC ZVA access is allowed */ | |
2055 | if (aa64_zva_access(env, NULL) == CP_ACCESS_OK) { | |
2056 | dzp_bit = 0; | |
2057 | } | |
2058 | return cpu->dcz_blocksize | dzp_bit; | |
2059 | } | |
2060 | ||
2061 | static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
2062 | { | |
2063 | if (!(env->pstate & PSTATE_SP)) { | |
2064 | /* Access to SP_EL0 is undefined if it's being used as | |
2065 | * the stack pointer. | |
2066 | */ | |
2067 | return CP_ACCESS_TRAP_UNCATEGORIZED; | |
2068 | } | |
2069 | return CP_ACCESS_OK; | |
2070 | } | |
2071 | ||
2072 | static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) | |
2073 | { | |
2074 | return env->pstate & PSTATE_SP; | |
2075 | } | |
2076 | ||
2077 | static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) | |
2078 | { | |
2079 | update_spsel(env, val); | |
2080 | } | |
2081 | ||
2082 | static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2083 | uint64_t value) | |
2084 | { | |
2085 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2086 | ||
2087 | if (raw_read(env, ri) == value) { | |
2088 | /* Skip the TLB flush if nothing actually changed; Linux likes | |
2089 | * to do a lot of pointless SCTLR writes. | |
2090 | */ | |
2091 | return; | |
2092 | } | |
2093 | ||
2094 | raw_write(env, ri, value); | |
2095 | /* ??? Lots of these bits are not implemented. */ | |
2096 | /* This may enable/disable the MMU, so do a TLB flush. */ | |
2097 | tlb_flush(CPU(cpu), 1); | |
2098 | } | |
2099 | ||
2100 | static const ARMCPRegInfo v8_cp_reginfo[] = { | |
2101 | /* Minimal set of EL0-visible registers. This will need to be expanded | |
2102 | * significantly for system emulation of AArch64 CPUs. | |
2103 | */ | |
2104 | { .name = "NZCV", .state = ARM_CP_STATE_AA64, | |
2105 | .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, | |
2106 | .access = PL0_RW, .type = ARM_CP_NZCV }, | |
2107 | { .name = "DAIF", .state = ARM_CP_STATE_AA64, | |
2108 | .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, | |
2109 | .type = ARM_CP_NO_MIGRATE, | |
2110 | .access = PL0_RW, .accessfn = aa64_daif_access, | |
2111 | .fieldoffset = offsetof(CPUARMState, daif), | |
2112 | .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, | |
2113 | { .name = "FPCR", .state = ARM_CP_STATE_AA64, | |
2114 | .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, | |
2115 | .access = PL0_RW, .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, | |
2116 | { .name = "FPSR", .state = ARM_CP_STATE_AA64, | |
2117 | .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, | |
2118 | .access = PL0_RW, .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, | |
2119 | { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, | |
2120 | .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, | |
2121 | .access = PL0_R, .type = ARM_CP_NO_MIGRATE, | |
2122 | .readfn = aa64_dczid_read }, | |
2123 | { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, | |
2124 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, | |
2125 | .access = PL0_W, .type = ARM_CP_DC_ZVA, | |
2126 | #ifndef CONFIG_USER_ONLY | |
2127 | /* Avoid overhead of an access check that always passes in user-mode */ | |
2128 | .accessfn = aa64_zva_access, | |
2129 | #endif | |
2130 | }, | |
2131 | { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, | |
2132 | .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, | |
2133 | .access = PL1_R, .type = ARM_CP_CURRENTEL }, | |
2134 | /* Cache ops: all NOPs since we don't emulate caches */ | |
2135 | { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, | |
2136 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, | |
2137 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2138 | { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, | |
2139 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, | |
2140 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2141 | { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, | |
2142 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, | |
2143 | .access = PL0_W, .type = ARM_CP_NOP, | |
2144 | .accessfn = aa64_cacheop_access }, | |
2145 | { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, | |
2146 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, | |
2147 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2148 | { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, | |
2149 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, | |
2150 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2151 | { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, | |
2152 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, | |
2153 | .access = PL0_W, .type = ARM_CP_NOP, | |
2154 | .accessfn = aa64_cacheop_access }, | |
2155 | { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, | |
2156 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, | |
2157 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2158 | { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, | |
2159 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, | |
2160 | .access = PL0_W, .type = ARM_CP_NOP, | |
2161 | .accessfn = aa64_cacheop_access }, | |
2162 | { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, | |
2163 | .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, | |
2164 | .access = PL0_W, .type = ARM_CP_NOP, | |
2165 | .accessfn = aa64_cacheop_access }, | |
2166 | { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, | |
2167 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, | |
2168 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2169 | /* TLBI operations */ | |
2170 | { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64, | |
2171 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, | |
2172 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2173 | .writefn = tlbiall_is_write }, | |
2174 | { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64, | |
2175 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, | |
2176 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2177 | .writefn = tlbi_aa64_va_is_write }, | |
2178 | { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64, | |
2179 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, | |
2180 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2181 | .writefn = tlbi_aa64_asid_is_write }, | |
2182 | { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64, | |
2183 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, | |
2184 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2185 | .writefn = tlbi_aa64_vaa_is_write }, | |
2186 | { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64, | |
2187 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, | |
2188 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2189 | .writefn = tlbi_aa64_va_is_write }, | |
2190 | { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64, | |
2191 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, | |
2192 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2193 | .writefn = tlbi_aa64_vaa_is_write }, | |
2194 | { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64, | |
2195 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, | |
2196 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2197 | .writefn = tlbiall_write }, | |
2198 | { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64, | |
2199 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, | |
2200 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2201 | .writefn = tlbi_aa64_va_write }, | |
2202 | { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64, | |
2203 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, | |
2204 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2205 | .writefn = tlbi_aa64_asid_write }, | |
2206 | { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64, | |
2207 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, | |
2208 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2209 | .writefn = tlbi_aa64_vaa_write }, | |
2210 | { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64, | |
2211 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, | |
2212 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2213 | .writefn = tlbi_aa64_va_write }, | |
2214 | { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64, | |
2215 | .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, | |
2216 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, | |
2217 | .writefn = tlbi_aa64_vaa_write }, | |
2218 | #ifndef CONFIG_USER_ONLY | |
2219 | /* 64 bit address translation operations */ | |
2220 | { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, | |
2221 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, | |
2222 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write }, | |
2223 | { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, | |
2224 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, | |
2225 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write }, | |
2226 | { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, | |
2227 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, | |
2228 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write }, | |
2229 | { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, | |
2230 | .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, | |
2231 | .access = PL1_W, .type = ARM_CP_NO_MIGRATE, .writefn = ats_write }, | |
2232 | #endif | |
2233 | /* TLB invalidate last level of translation table walk */ | |
2234 | { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, | |
2235 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_is_write }, | |
2236 | { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, | |
2237 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, | |
2238 | .writefn = tlbimvaa_is_write }, | |
2239 | { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, | |
2240 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimva_write }, | |
2241 | { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, | |
2242 | .type = ARM_CP_NO_MIGRATE, .access = PL1_W, .writefn = tlbimvaa_write }, | |
2243 | /* 32 bit cache operations */ | |
2244 | { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, | |
2245 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2246 | { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, | |
2247 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2248 | { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, | |
2249 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2250 | { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, | |
2251 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2252 | { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, | |
2253 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2254 | { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, | |
2255 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2256 | { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, | |
2257 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2258 | { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, | |
2259 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2260 | { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, | |
2261 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2262 | { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, | |
2263 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2264 | { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, | |
2265 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2266 | { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, | |
2267 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2268 | { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, | |
2269 | .type = ARM_CP_NOP, .access = PL1_W }, | |
2270 | /* MMU Domain access control / MPU write buffer control */ | |
2271 | { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, | |
2272 | .access = PL1_RW, .resetvalue = 0, | |
2273 | .writefn = dacr_write, .raw_writefn = raw_write, | |
2274 | .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), | |
2275 | offsetoflow32(CPUARMState, cp15.dacr_ns) } }, | |
2276 | { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, | |
2277 | .type = ARM_CP_NO_MIGRATE, | |
2278 | .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, | |
2279 | .access = PL1_RW, | |
2280 | .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, | |
2281 | { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, | |
2282 | .type = ARM_CP_NO_MIGRATE, | |
2283 | .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, | |
2284 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[0]) }, | |
2285 | /* We rely on the access checks not allowing the guest to write to the | |
2286 | * state field when SPSel indicates that it's being used as the stack | |
2287 | * pointer. | |
2288 | */ | |
2289 | { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, | |
2290 | .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, | |
2291 | .access = PL1_RW, .accessfn = sp_el0_access, | |
2292 | .type = ARM_CP_NO_MIGRATE, | |
2293 | .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, | |
2294 | { .name = "SPSel", .state = ARM_CP_STATE_AA64, | |
2295 | .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, | |
2296 | .type = ARM_CP_NO_MIGRATE, | |
2297 | .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, | |
2298 | REGINFO_SENTINEL | |
2299 | }; | |
2300 | ||
2301 | /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */ | |
2302 | static const ARMCPRegInfo v8_el3_no_el2_cp_reginfo[] = { | |
2303 | { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64, | |
2304 | .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, | |
2305 | .access = PL2_RW, | |
2306 | .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, | |
2307 | { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, | |
2308 | .type = ARM_CP_NO_MIGRATE, | |
2309 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, | |
2310 | .access = PL2_RW, | |
2311 | .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, | |
2312 | REGINFO_SENTINEL | |
2313 | }; | |
2314 | ||
2315 | static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) | |
2316 | { | |
2317 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2318 | uint64_t valid_mask = HCR_MASK; | |
2319 | ||
2320 | if (arm_feature(env, ARM_FEATURE_EL3)) { | |
2321 | valid_mask &= ~HCR_HCD; | |
2322 | } else { | |
2323 | valid_mask &= ~HCR_TSC; | |
2324 | } | |
2325 | ||
2326 | /* Clear RES0 bits. */ | |
2327 | value &= valid_mask; | |
2328 | ||
2329 | /* These bits change the MMU setup: | |
2330 | * HCR_VM enables stage 2 translation | |
2331 | * HCR_PTW forbids certain page-table setups | |
2332 | * HCR_DC Disables stage1 and enables stage2 translation | |
2333 | */ | |
2334 | if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) { | |
2335 | tlb_flush(CPU(cpu), 1); | |
2336 | } | |
2337 | raw_write(env, ri, value); | |
2338 | } | |
2339 | ||
2340 | static const ARMCPRegInfo v8_el2_cp_reginfo[] = { | |
2341 | { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, | |
2342 | .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, | |
2343 | .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), | |
2344 | .writefn = hcr_write }, | |
2345 | { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, | |
2346 | .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, | |
2347 | .access = PL2_RW, .resetvalue = 0, | |
2348 | .writefn = dacr_write, .raw_writefn = raw_write, | |
2349 | .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, | |
2350 | { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, | |
2351 | .type = ARM_CP_NO_MIGRATE, | |
2352 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, | |
2353 | .access = PL2_RW, | |
2354 | .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, | |
2355 | { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64, | |
2356 | .type = ARM_CP_NO_MIGRATE, | |
2357 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, | |
2358 | .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, | |
2359 | { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, | |
2360 | .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, | |
2361 | .access = PL2_RW, .resetvalue = 0, | |
2362 | .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, | |
2363 | { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64, | |
2364 | .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, | |
2365 | .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, | |
2366 | { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, | |
2367 | .type = ARM_CP_NO_MIGRATE, | |
2368 | .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, | |
2369 | .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[6]) }, | |
2370 | { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64, | |
2371 | .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, | |
2372 | .access = PL2_RW, .writefn = vbar_write, | |
2373 | .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), | |
2374 | .resetvalue = 0 }, | |
2375 | REGINFO_SENTINEL | |
2376 | }; | |
2377 | ||
2378 | static const ARMCPRegInfo v8_el3_cp_reginfo[] = { | |
2379 | { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, | |
2380 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, | |
2381 | .access = PL3_RW, .raw_writefn = raw_write, .writefn = sctlr_write, | |
2382 | .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]) }, | |
2383 | { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, | |
2384 | .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, | |
2385 | .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, | |
2386 | .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, | |
2387 | { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, | |
2388 | .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, | |
2389 | .access = PL3_RW, .writefn = vmsa_tcr_el1_write, | |
2390 | .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write, | |
2391 | .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, | |
2392 | { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, | |
2393 | .type = ARM_CP_NO_MIGRATE, | |
2394 | .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, | |
2395 | .access = PL3_RW, | |
2396 | .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, | |
2397 | { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, | |
2398 | .type = ARM_CP_NO_MIGRATE, | |
2399 | .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, | |
2400 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, | |
2401 | { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, | |
2402 | .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, | |
2403 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, | |
2404 | { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, | |
2405 | .type = ARM_CP_NO_MIGRATE, | |
2406 | .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, | |
2407 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, banked_spsr[7]) }, | |
2408 | { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, | |
2409 | .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, | |
2410 | .access = PL3_RW, .writefn = vbar_write, | |
2411 | .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), | |
2412 | .resetvalue = 0 }, | |
2413 | REGINFO_SENTINEL | |
2414 | }; | |
2415 | ||
2416 | static const ARMCPRegInfo el3_cp_reginfo[] = { | |
2417 | { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, | |
2418 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, | |
2419 | .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), | |
2420 | .resetvalue = 0, .writefn = scr_write }, | |
2421 | { .name = "SCR", .type = ARM_CP_NO_MIGRATE, | |
2422 | .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, | |
2423 | .access = PL3_RW, .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), | |
2424 | .resetfn = arm_cp_reset_ignore, .writefn = scr_write }, | |
2425 | { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, | |
2426 | .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, | |
2427 | .access = PL3_RW, .resetvalue = 0, | |
2428 | .fieldoffset = offsetof(CPUARMState, cp15.sder) }, | |
2429 | { .name = "SDER", | |
2430 | .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, | |
2431 | .access = PL3_RW, .resetvalue = 0, | |
2432 | .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, | |
2433 | /* TODO: Implement NSACR trapping of secure EL1 accesses to EL3 */ | |
2434 | { .name = "NSACR", .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, | |
2435 | .access = PL3_W | PL1_R, .resetvalue = 0, | |
2436 | .fieldoffset = offsetof(CPUARMState, cp15.nsacr) }, | |
2437 | { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, | |
2438 | .access = PL3_RW, .writefn = vbar_write, .resetvalue = 0, | |
2439 | .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, | |
2440 | REGINFO_SENTINEL | |
2441 | }; | |
2442 | ||
2443 | static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri) | |
2444 | { | |
2445 | /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64, | |
2446 | * but the AArch32 CTR has its own reginfo struct) | |
2447 | */ | |
2448 | if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) { | |
2449 | return CP_ACCESS_TRAP; | |
2450 | } | |
2451 | return CP_ACCESS_OK; | |
2452 | } | |
2453 | ||
2454 | static const ARMCPRegInfo debug_cp_reginfo[] = { | |
2455 | /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped | |
2456 | * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1; | |
2457 | * unlike DBGDRAR it is never accessible from EL0. | |
2458 | * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64 | |
2459 | * accessor. | |
2460 | */ | |
2461 | { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, | |
2462 | .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
2463 | { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64, | |
2464 | .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, | |
2465 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
2466 | { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, | |
2467 | .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
2468 | /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */ | |
2469 | { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH, | |
2470 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, | |
2471 | .access = PL1_RW, | |
2472 | .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), | |
2473 | .resetvalue = 0 }, | |
2474 | /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1. | |
2475 | * We don't implement the configurable EL0 access. | |
2476 | */ | |
2477 | { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH, | |
2478 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, | |
2479 | .type = ARM_CP_NO_MIGRATE, | |
2480 | .access = PL1_R, | |
2481 | .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), | |
2482 | .resetfn = arm_cp_reset_ignore }, | |
2483 | /* We define a dummy WI OSLAR_EL1, because Linux writes to it. */ | |
2484 | { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH, | |
2485 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4, | |
2486 | .access = PL1_W, .type = ARM_CP_NOP }, | |
2487 | /* Dummy OSDLR_EL1: 32-bit Linux will read this */ | |
2488 | { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH, | |
2489 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4, | |
2490 | .access = PL1_RW, .type = ARM_CP_NOP }, | |
2491 | /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't | |
2492 | * implement vector catch debug events yet. | |
2493 | */ | |
2494 | { .name = "DBGVCR", | |
2495 | .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, | |
2496 | .access = PL1_RW, .type = ARM_CP_NOP }, | |
2497 | REGINFO_SENTINEL | |
2498 | }; | |
2499 | ||
2500 | static const ARMCPRegInfo debug_lpae_cp_reginfo[] = { | |
2501 | /* 64 bit access versions of the (dummy) debug registers */ | |
2502 | { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0, | |
2503 | .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, | |
2504 | { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0, | |
2505 | .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, | |
2506 | REGINFO_SENTINEL | |
2507 | }; | |
2508 | ||
2509 | void hw_watchpoint_update(ARMCPU *cpu, int n) | |
2510 | { | |
2511 | CPUARMState *env = &cpu->env; | |
2512 | vaddr len = 0; | |
2513 | vaddr wvr = env->cp15.dbgwvr[n]; | |
2514 | uint64_t wcr = env->cp15.dbgwcr[n]; | |
2515 | int mask; | |
2516 | int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; | |
2517 | ||
2518 | if (env->cpu_watchpoint[n]) { | |
2519 | cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]); | |
2520 | env->cpu_watchpoint[n] = NULL; | |
2521 | } | |
2522 | ||
2523 | if (!extract64(wcr, 0, 1)) { | |
2524 | /* E bit clear : watchpoint disabled */ | |
2525 | return; | |
2526 | } | |
2527 | ||
2528 | switch (extract64(wcr, 3, 2)) { | |
2529 | case 0: | |
2530 | /* LSC 00 is reserved and must behave as if the wp is disabled */ | |
2531 | return; | |
2532 | case 1: | |
2533 | flags |= BP_MEM_READ; | |
2534 | break; | |
2535 | case 2: | |
2536 | flags |= BP_MEM_WRITE; | |
2537 | break; | |
2538 | case 3: | |
2539 | flags |= BP_MEM_ACCESS; | |
2540 | break; | |
2541 | } | |
2542 | ||
2543 | /* Attempts to use both MASK and BAS fields simultaneously are | |
2544 | * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case, | |
2545 | * thus generating a watchpoint for every byte in the masked region. | |
2546 | */ | |
2547 | mask = extract64(wcr, 24, 4); | |
2548 | if (mask == 1 || mask == 2) { | |
2549 | /* Reserved values of MASK; we must act as if the mask value was | |
2550 | * some non-reserved value, or as if the watchpoint were disabled. | |
2551 | * We choose the latter. | |
2552 | */ | |
2553 | return; | |
2554 | } else if (mask) { | |
2555 | /* Watchpoint covers an aligned area up to 2GB in size */ | |
2556 | len = 1ULL << mask; | |
2557 | /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE | |
2558 | * whether the watchpoint fires when the unmasked bits match; we opt | |
2559 | * to generate the exceptions. | |
2560 | */ | |
2561 | wvr &= ~(len - 1); | |
2562 | } else { | |
2563 | /* Watchpoint covers bytes defined by the byte address select bits */ | |
2564 | int bas = extract64(wcr, 5, 8); | |
2565 | int basstart; | |
2566 | ||
2567 | if (bas == 0) { | |
2568 | /* This must act as if the watchpoint is disabled */ | |
2569 | return; | |
2570 | } | |
2571 | ||
2572 | if (extract64(wvr, 2, 1)) { | |
2573 | /* Deprecated case of an only 4-aligned address. BAS[7:4] are | |
2574 | * ignored, and BAS[3:0] define which bytes to watch. | |
2575 | */ | |
2576 | bas &= 0xf; | |
2577 | } | |
2578 | /* The BAS bits are supposed to be programmed to indicate a contiguous | |
2579 | * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether | |
2580 | * we fire for each byte in the word/doubleword addressed by the WVR. | |
2581 | * We choose to ignore any non-zero bits after the first range of 1s. | |
2582 | */ | |
2583 | basstart = ctz32(bas); | |
2584 | len = cto32(bas >> basstart); | |
2585 | wvr += basstart; | |
2586 | } | |
2587 | ||
2588 | cpu_watchpoint_insert(CPU(cpu), wvr, len, flags, | |
2589 | &env->cpu_watchpoint[n]); | |
2590 | } | |
2591 | ||
2592 | void hw_watchpoint_update_all(ARMCPU *cpu) | |
2593 | { | |
2594 | int i; | |
2595 | CPUARMState *env = &cpu->env; | |
2596 | ||
2597 | /* Completely clear out existing QEMU watchpoints and our array, to | |
2598 | * avoid possible stale entries following migration load. | |
2599 | */ | |
2600 | cpu_watchpoint_remove_all(CPU(cpu), BP_CPU); | |
2601 | memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint)); | |
2602 | ||
2603 | for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) { | |
2604 | hw_watchpoint_update(cpu, i); | |
2605 | } | |
2606 | } | |
2607 | ||
2608 | static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2609 | uint64_t value) | |
2610 | { | |
2611 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2612 | int i = ri->crm; | |
2613 | ||
2614 | /* Bits [63:49] are hardwired to the value of bit [48]; that is, the | |
2615 | * register reads and behaves as if values written are sign extended. | |
2616 | * Bits [1:0] are RES0. | |
2617 | */ | |
2618 | value = sextract64(value, 0, 49) & ~3ULL; | |
2619 | ||
2620 | raw_write(env, ri, value); | |
2621 | hw_watchpoint_update(cpu, i); | |
2622 | } | |
2623 | ||
2624 | static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2625 | uint64_t value) | |
2626 | { | |
2627 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2628 | int i = ri->crm; | |
2629 | ||
2630 | raw_write(env, ri, value); | |
2631 | hw_watchpoint_update(cpu, i); | |
2632 | } | |
2633 | ||
2634 | void hw_breakpoint_update(ARMCPU *cpu, int n) | |
2635 | { | |
2636 | CPUARMState *env = &cpu->env; | |
2637 | uint64_t bvr = env->cp15.dbgbvr[n]; | |
2638 | uint64_t bcr = env->cp15.dbgbcr[n]; | |
2639 | vaddr addr; | |
2640 | int bt; | |
2641 | int flags = BP_CPU; | |
2642 | ||
2643 | if (env->cpu_breakpoint[n]) { | |
2644 | cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]); | |
2645 | env->cpu_breakpoint[n] = NULL; | |
2646 | } | |
2647 | ||
2648 | if (!extract64(bcr, 0, 1)) { | |
2649 | /* E bit clear : watchpoint disabled */ | |
2650 | return; | |
2651 | } | |
2652 | ||
2653 | bt = extract64(bcr, 20, 4); | |
2654 | ||
2655 | switch (bt) { | |
2656 | case 4: /* unlinked address mismatch (reserved if AArch64) */ | |
2657 | case 5: /* linked address mismatch (reserved if AArch64) */ | |
2658 | qemu_log_mask(LOG_UNIMP, | |
2659 | "arm: address mismatch breakpoint types not implemented"); | |
2660 | return; | |
2661 | case 0: /* unlinked address match */ | |
2662 | case 1: /* linked address match */ | |
2663 | { | |
2664 | /* Bits [63:49] are hardwired to the value of bit [48]; that is, | |
2665 | * we behave as if the register was sign extended. Bits [1:0] are | |
2666 | * RES0. The BAS field is used to allow setting breakpoints on 16 | |
2667 | * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether | |
2668 | * a bp will fire if the addresses covered by the bp and the addresses | |
2669 | * covered by the insn overlap but the insn doesn't start at the | |
2670 | * start of the bp address range. We choose to require the insn and | |
2671 | * the bp to have the same address. The constraints on writing to | |
2672 | * BAS enforced in dbgbcr_write mean we have only four cases: | |
2673 | * 0b0000 => no breakpoint | |
2674 | * 0b0011 => breakpoint on addr | |
2675 | * 0b1100 => breakpoint on addr + 2 | |
2676 | * 0b1111 => breakpoint on addr | |
2677 | * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c). | |
2678 | */ | |
2679 | int bas = extract64(bcr, 5, 4); | |
2680 | addr = sextract64(bvr, 0, 49) & ~3ULL; | |
2681 | if (bas == 0) { | |
2682 | return; | |
2683 | } | |
2684 | if (bas == 0xc) { | |
2685 | addr += 2; | |
2686 | } | |
2687 | break; | |
2688 | } | |
2689 | case 2: /* unlinked context ID match */ | |
2690 | case 8: /* unlinked VMID match (reserved if no EL2) */ | |
2691 | case 10: /* unlinked context ID and VMID match (reserved if no EL2) */ | |
2692 | qemu_log_mask(LOG_UNIMP, | |
2693 | "arm: unlinked context breakpoint types not implemented"); | |
2694 | return; | |
2695 | case 9: /* linked VMID match (reserved if no EL2) */ | |
2696 | case 11: /* linked context ID and VMID match (reserved if no EL2) */ | |
2697 | case 3: /* linked context ID match */ | |
2698 | default: | |
2699 | /* We must generate no events for Linked context matches (unless | |
2700 | * they are linked to by some other bp/wp, which is handled in | |
2701 | * updates for the linking bp/wp). We choose to also generate no events | |
2702 | * for reserved values. | |
2703 | */ | |
2704 | return; | |
2705 | } | |
2706 | ||
2707 | cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]); | |
2708 | } | |
2709 | ||
2710 | void hw_breakpoint_update_all(ARMCPU *cpu) | |
2711 | { | |
2712 | int i; | |
2713 | CPUARMState *env = &cpu->env; | |
2714 | ||
2715 | /* Completely clear out existing QEMU breakpoints and our array, to | |
2716 | * avoid possible stale entries following migration load. | |
2717 | */ | |
2718 | cpu_breakpoint_remove_all(CPU(cpu), BP_CPU); | |
2719 | memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint)); | |
2720 | ||
2721 | for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) { | |
2722 | hw_breakpoint_update(cpu, i); | |
2723 | } | |
2724 | } | |
2725 | ||
2726 | static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2727 | uint64_t value) | |
2728 | { | |
2729 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2730 | int i = ri->crm; | |
2731 | ||
2732 | raw_write(env, ri, value); | |
2733 | hw_breakpoint_update(cpu, i); | |
2734 | } | |
2735 | ||
2736 | static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, | |
2737 | uint64_t value) | |
2738 | { | |
2739 | ARMCPU *cpu = arm_env_get_cpu(env); | |
2740 | int i = ri->crm; | |
2741 | ||
2742 | /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only | |
2743 | * copy of BAS[0]. | |
2744 | */ | |
2745 | value = deposit64(value, 6, 1, extract64(value, 5, 1)); | |
2746 | value = deposit64(value, 8, 1, extract64(value, 7, 1)); | |
2747 | ||
2748 | raw_write(env, ri, value); | |
2749 | hw_breakpoint_update(cpu, i); | |
2750 | } | |
2751 | ||
2752 | static void define_debug_regs(ARMCPU *cpu) | |
2753 | { | |
2754 | /* Define v7 and v8 architectural debug registers. | |
2755 | * These are just dummy implementations for now. | |
2756 | */ | |
2757 | int i; | |
2758 | int wrps, brps, ctx_cmps; | |
2759 | ARMCPRegInfo dbgdidr = { | |
2760 | .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, | |
2761 | .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr, | |
2762 | }; | |
2763 | ||
2764 | /* Note that all these register fields hold "number of Xs minus 1". */ | |
2765 | brps = extract32(cpu->dbgdidr, 24, 4); | |
2766 | wrps = extract32(cpu->dbgdidr, 28, 4); | |
2767 | ctx_cmps = extract32(cpu->dbgdidr, 20, 4); | |
2768 | ||
2769 | assert(ctx_cmps <= brps); | |
2770 | ||
2771 | /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties | |
2772 | * of the debug registers such as number of breakpoints; | |
2773 | * check that if they both exist then they agree. | |
2774 | */ | |
2775 | if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { | |
2776 | assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps); | |
2777 | assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps); | |
2778 | assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps); | |
2779 | } | |
2780 | ||
2781 | define_one_arm_cp_reg(cpu, &dbgdidr); | |
2782 | define_arm_cp_regs(cpu, debug_cp_reginfo); | |
2783 | ||
2784 | if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) { | |
2785 | define_arm_cp_regs(cpu, debug_lpae_cp_reginfo); | |
2786 | } | |
2787 | ||
2788 | for (i = 0; i < brps + 1; i++) { | |
2789 | ARMCPRegInfo dbgregs[] = { | |
2790 | { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH, | |
2791 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4, | |
2792 | .access = PL1_RW, | |
2793 | .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]), | |
2794 | .writefn = dbgbvr_write, .raw_writefn = raw_write | |
2795 | }, | |
2796 | { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH, | |
2797 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5, | |
2798 | .access = PL1_RW, | |
2799 | .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]), | |
2800 | .writefn = dbgbcr_write, .raw_writefn = raw_write | |
2801 | }, | |
2802 | REGINFO_SENTINEL | |
2803 | }; | |
2804 | define_arm_cp_regs(cpu, dbgregs); | |
2805 | } | |
2806 | ||
2807 | for (i = 0; i < wrps + 1; i++) { | |
2808 | ARMCPRegInfo dbgregs[] = { | |
2809 | { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH, | |
2810 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6, | |
2811 | .access = PL1_RW, | |
2812 | .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]), | |
2813 | .writefn = dbgwvr_write, .raw_writefn = raw_write | |
2814 | }, | |
2815 | { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH, | |
2816 | .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7, | |
2817 | .access = PL1_RW, | |
2818 | .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]), | |
2819 | .writefn = dbgwcr_write, .raw_writefn = raw_write | |
2820 | }, | |
2821 | REGINFO_SENTINEL | |
2822 | }; | |
2823 | define_arm_cp_regs(cpu, dbgregs); | |
2824 | } | |
2825 | } | |
2826 | ||
2827 | void register_cp_regs_for_features(ARMCPU *cpu) | |
2828 | { | |
2829 | /* Register all the coprocessor registers based on feature bits */ | |
2830 | CPUARMState *env = &cpu->env; | |
2831 | if (arm_feature(env, ARM_FEATURE_M)) { | |
2832 | /* M profile has no coprocessor registers */ | |
2833 | return; | |
2834 | } | |
2835 | ||
2836 | define_arm_cp_regs(cpu, cp_reginfo); | |
2837 | if (!arm_feature(env, ARM_FEATURE_V8)) { | |
2838 | /* Must go early as it is full of wildcards that may be | |
2839 | * overridden by later definitions. | |
2840 | */ | |
2841 | define_arm_cp_regs(cpu, not_v8_cp_reginfo); | |
2842 | } | |
2843 | ||
2844 | if (arm_feature(env, ARM_FEATURE_V6)) { | |
2845 | /* The ID registers all have impdef reset values */ | |
2846 | ARMCPRegInfo v6_idregs[] = { | |
2847 | { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, | |
2848 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, | |
2849 | .access = PL1_R, .type = ARM_CP_CONST, | |
2850 | .resetvalue = cpu->id_pfr0 }, | |
2851 | { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, | |
2852 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, | |
2853 | .access = PL1_R, .type = ARM_CP_CONST, | |
2854 | .resetvalue = cpu->id_pfr1 }, | |
2855 | { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, | |
2856 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, | |
2857 | .access = PL1_R, .type = ARM_CP_CONST, | |
2858 | .resetvalue = cpu->id_dfr0 }, | |
2859 | { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, | |
2860 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, | |
2861 | .access = PL1_R, .type = ARM_CP_CONST, | |
2862 | .resetvalue = cpu->id_afr0 }, | |
2863 | { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, | |
2864 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, | |
2865 | .access = PL1_R, .type = ARM_CP_CONST, | |
2866 | .resetvalue = cpu->id_mmfr0 }, | |
2867 | { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, | |
2868 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, | |
2869 | .access = PL1_R, .type = ARM_CP_CONST, | |
2870 | .resetvalue = cpu->id_mmfr1 }, | |
2871 | { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, | |
2872 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, | |
2873 | .access = PL1_R, .type = ARM_CP_CONST, | |
2874 | .resetvalue = cpu->id_mmfr2 }, | |
2875 | { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, | |
2876 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, | |
2877 | .access = PL1_R, .type = ARM_CP_CONST, | |
2878 | .resetvalue = cpu->id_mmfr3 }, | |
2879 | { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, | |
2880 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, | |
2881 | .access = PL1_R, .type = ARM_CP_CONST, | |
2882 | .resetvalue = cpu->id_isar0 }, | |
2883 | { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, | |
2884 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, | |
2885 | .access = PL1_R, .type = ARM_CP_CONST, | |
2886 | .resetvalue = cpu->id_isar1 }, | |
2887 | { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, | |
2888 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, | |
2889 | .access = PL1_R, .type = ARM_CP_CONST, | |
2890 | .resetvalue = cpu->id_isar2 }, | |
2891 | { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, | |
2892 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, | |
2893 | .access = PL1_R, .type = ARM_CP_CONST, | |
2894 | .resetvalue = cpu->id_isar3 }, | |
2895 | { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, | |
2896 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, | |
2897 | .access = PL1_R, .type = ARM_CP_CONST, | |
2898 | .resetvalue = cpu->id_isar4 }, | |
2899 | { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, | |
2900 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, | |
2901 | .access = PL1_R, .type = ARM_CP_CONST, | |
2902 | .resetvalue = cpu->id_isar5 }, | |
2903 | /* 6..7 are as yet unallocated and must RAZ */ | |
2904 | { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2, | |
2905 | .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST, | |
2906 | .resetvalue = 0 }, | |
2907 | { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2, | |
2908 | .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST, | |
2909 | .resetvalue = 0 }, | |
2910 | REGINFO_SENTINEL | |
2911 | }; | |
2912 | define_arm_cp_regs(cpu, v6_idregs); | |
2913 | define_arm_cp_regs(cpu, v6_cp_reginfo); | |
2914 | } else { | |
2915 | define_arm_cp_regs(cpu, not_v6_cp_reginfo); | |
2916 | } | |
2917 | if (arm_feature(env, ARM_FEATURE_V6K)) { | |
2918 | define_arm_cp_regs(cpu, v6k_cp_reginfo); | |
2919 | } | |
2920 | if (arm_feature(env, ARM_FEATURE_V7MP)) { | |
2921 | define_arm_cp_regs(cpu, v7mp_cp_reginfo); | |
2922 | } | |
2923 | if (arm_feature(env, ARM_FEATURE_V7)) { | |
2924 | /* v7 performance monitor control register: same implementor | |
2925 | * field as main ID register, and we implement only the cycle | |
2926 | * count register. | |
2927 | */ | |
2928 | #ifndef CONFIG_USER_ONLY | |
2929 | ARMCPRegInfo pmcr = { | |
2930 | .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, | |
2931 | .access = PL0_RW, | |
2932 | .type = ARM_CP_IO | ARM_CP_NO_MIGRATE, | |
2933 | .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), | |
2934 | .accessfn = pmreg_access, .writefn = pmcr_write, | |
2935 | .raw_writefn = raw_write, | |
2936 | }; | |
2937 | ARMCPRegInfo pmcr64 = { | |
2938 | .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, | |
2939 | .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, | |
2940 | .access = PL0_RW, .accessfn = pmreg_access, | |
2941 | .type = ARM_CP_IO, | |
2942 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), | |
2943 | .resetvalue = cpu->midr & 0xff000000, | |
2944 | .writefn = pmcr_write, .raw_writefn = raw_write, | |
2945 | }; | |
2946 | define_one_arm_cp_reg(cpu, &pmcr); | |
2947 | define_one_arm_cp_reg(cpu, &pmcr64); | |
2948 | #endif | |
2949 | ARMCPRegInfo clidr = { | |
2950 | .name = "CLIDR", .state = ARM_CP_STATE_BOTH, | |
2951 | .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, | |
2952 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr | |
2953 | }; | |
2954 | define_one_arm_cp_reg(cpu, &clidr); | |
2955 | define_arm_cp_regs(cpu, v7_cp_reginfo); | |
2956 | define_debug_regs(cpu); | |
2957 | } else { | |
2958 | define_arm_cp_regs(cpu, not_v7_cp_reginfo); | |
2959 | } | |
2960 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
2961 | /* AArch64 ID registers, which all have impdef reset values */ | |
2962 | ARMCPRegInfo v8_idregs[] = { | |
2963 | { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, | |
2964 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, | |
2965 | .access = PL1_R, .type = ARM_CP_CONST, | |
2966 | .resetvalue = cpu->id_aa64pfr0 }, | |
2967 | { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, | |
2968 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, | |
2969 | .access = PL1_R, .type = ARM_CP_CONST, | |
2970 | .resetvalue = cpu->id_aa64pfr1}, | |
2971 | { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, | |
2972 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, | |
2973 | .access = PL1_R, .type = ARM_CP_CONST, | |
2974 | /* We mask out the PMUVer field, because we don't currently | |
2975 | * implement the PMU. Not advertising it prevents the guest | |
2976 | * from trying to use it and getting UNDEFs on registers we | |
2977 | * don't implement. | |
2978 | */ | |
2979 | .resetvalue = cpu->id_aa64dfr0 & ~0xf00 }, | |
2980 | { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, | |
2981 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, | |
2982 | .access = PL1_R, .type = ARM_CP_CONST, | |
2983 | .resetvalue = cpu->id_aa64dfr1 }, | |
2984 | { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, | |
2985 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, | |
2986 | .access = PL1_R, .type = ARM_CP_CONST, | |
2987 | .resetvalue = cpu->id_aa64afr0 }, | |
2988 | { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, | |
2989 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, | |
2990 | .access = PL1_R, .type = ARM_CP_CONST, | |
2991 | .resetvalue = cpu->id_aa64afr1 }, | |
2992 | { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, | |
2993 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, | |
2994 | .access = PL1_R, .type = ARM_CP_CONST, | |
2995 | .resetvalue = cpu->id_aa64isar0 }, | |
2996 | { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, | |
2997 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, | |
2998 | .access = PL1_R, .type = ARM_CP_CONST, | |
2999 | .resetvalue = cpu->id_aa64isar1 }, | |
3000 | { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, | |
3001 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, | |
3002 | .access = PL1_R, .type = ARM_CP_CONST, | |
3003 | .resetvalue = cpu->id_aa64mmfr0 }, | |
3004 | { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, | |
3005 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, | |
3006 | .access = PL1_R, .type = ARM_CP_CONST, | |
3007 | .resetvalue = cpu->id_aa64mmfr1 }, | |
3008 | { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, | |
3009 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, | |
3010 | .access = PL1_R, .type = ARM_CP_CONST, | |
3011 | .resetvalue = cpu->mvfr0 }, | |
3012 | { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, | |
3013 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, | |
3014 | .access = PL1_R, .type = ARM_CP_CONST, | |
3015 | .resetvalue = cpu->mvfr1 }, | |
3016 | { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, | |
3017 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, | |
3018 | .access = PL1_R, .type = ARM_CP_CONST, | |
3019 | .resetvalue = cpu->mvfr2 }, | |
3020 | REGINFO_SENTINEL | |
3021 | }; | |
3022 | ARMCPRegInfo rvbar = { | |
3023 | .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64, | |
3024 | .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2, | |
3025 | .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar | |
3026 | }; | |
3027 | define_one_arm_cp_reg(cpu, &rvbar); | |
3028 | define_arm_cp_regs(cpu, v8_idregs); | |
3029 | define_arm_cp_regs(cpu, v8_cp_reginfo); | |
3030 | } | |
3031 | if (arm_feature(env, ARM_FEATURE_EL2)) { | |
3032 | define_arm_cp_regs(cpu, v8_el2_cp_reginfo); | |
3033 | } else { | |
3034 | /* If EL2 is missing but higher ELs are enabled, we need to | |
3035 | * register the no_el2 reginfos. | |
3036 | */ | |
3037 | if (arm_feature(env, ARM_FEATURE_EL3)) { | |
3038 | define_arm_cp_regs(cpu, v8_el3_no_el2_cp_reginfo); | |
3039 | } | |
3040 | } | |
3041 | if (arm_feature(env, ARM_FEATURE_EL3)) { | |
3042 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
3043 | define_arm_cp_regs(cpu, v8_el3_cp_reginfo); | |
3044 | } | |
3045 | define_arm_cp_regs(cpu, el3_cp_reginfo); | |
3046 | } | |
3047 | if (arm_feature(env, ARM_FEATURE_MPU)) { | |
3048 | /* These are the MPU registers prior to PMSAv6. Any new | |
3049 | * PMSA core later than the ARM946 will require that we | |
3050 | * implement the PMSAv6 or PMSAv7 registers, which are | |
3051 | * completely different. | |
3052 | */ | |
3053 | assert(!arm_feature(env, ARM_FEATURE_V6)); | |
3054 | define_arm_cp_regs(cpu, pmsav5_cp_reginfo); | |
3055 | } else { | |
3056 | define_arm_cp_regs(cpu, vmsa_cp_reginfo); | |
3057 | } | |
3058 | if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { | |
3059 | define_arm_cp_regs(cpu, t2ee_cp_reginfo); | |
3060 | } | |
3061 | if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { | |
3062 | define_arm_cp_regs(cpu, generic_timer_cp_reginfo); | |
3063 | } | |
3064 | if (arm_feature(env, ARM_FEATURE_VAPA)) { | |
3065 | define_arm_cp_regs(cpu, vapa_cp_reginfo); | |
3066 | } | |
3067 | if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { | |
3068 | define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); | |
3069 | } | |
3070 | if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { | |
3071 | define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); | |
3072 | } | |
3073 | if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { | |
3074 | define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); | |
3075 | } | |
3076 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) { | |
3077 | define_arm_cp_regs(cpu, omap_cp_reginfo); | |
3078 | } | |
3079 | if (arm_feature(env, ARM_FEATURE_STRONGARM)) { | |
3080 | define_arm_cp_regs(cpu, strongarm_cp_reginfo); | |
3081 | } | |
3082 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { | |
3083 | define_arm_cp_regs(cpu, xscale_cp_reginfo); | |
3084 | } | |
3085 | if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { | |
3086 | define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); | |
3087 | } | |
3088 | if (arm_feature(env, ARM_FEATURE_LPAE)) { | |
3089 | define_arm_cp_regs(cpu, lpae_cp_reginfo); | |
3090 | } | |
3091 | /* Slightly awkwardly, the OMAP and StrongARM cores need all of | |
3092 | * cp15 crn=0 to be writes-ignored, whereas for other cores they should | |
3093 | * be read-only (ie write causes UNDEF exception). | |
3094 | */ | |
3095 | { | |
3096 | ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { | |
3097 | /* Pre-v8 MIDR space. | |
3098 | * Note that the MIDR isn't a simple constant register because | |
3099 | * of the TI925 behaviour where writes to another register can | |
3100 | * cause the MIDR value to change. | |
3101 | * | |
3102 | * Unimplemented registers in the c15 0 0 0 space default to | |
3103 | * MIDR. Define MIDR first as this entire space, then CTR, TCMTR | |
3104 | * and friends override accordingly. | |
3105 | */ | |
3106 | { .name = "MIDR", | |
3107 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, | |
3108 | .access = PL1_R, .resetvalue = cpu->midr, | |
3109 | .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, | |
3110 | .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), | |
3111 | .type = ARM_CP_OVERRIDE }, | |
3112 | /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ | |
3113 | { .name = "DUMMY", | |
3114 | .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, | |
3115 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3116 | { .name = "DUMMY", | |
3117 | .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, | |
3118 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3119 | { .name = "DUMMY", | |
3120 | .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, | |
3121 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3122 | { .name = "DUMMY", | |
3123 | .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, | |
3124 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3125 | { .name = "DUMMY", | |
3126 | .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, | |
3127 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3128 | REGINFO_SENTINEL | |
3129 | }; | |
3130 | ARMCPRegInfo id_v8_midr_cp_reginfo[] = { | |
3131 | /* v8 MIDR -- the wildcard isn't necessary, and nor is the | |
3132 | * variable-MIDR TI925 behaviour. Instead we have a single | |
3133 | * (strictly speaking IMPDEF) alias of the MIDR, REVIDR. | |
3134 | */ | |
3135 | { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, | |
3136 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, | |
3137 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr }, | |
3138 | { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, | |
3139 | .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, | |
3140 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->midr }, | |
3141 | REGINFO_SENTINEL | |
3142 | }; | |
3143 | ARMCPRegInfo id_cp_reginfo[] = { | |
3144 | /* These are common to v8 and pre-v8 */ | |
3145 | { .name = "CTR", | |
3146 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, | |
3147 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, | |
3148 | { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, | |
3149 | .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, | |
3150 | .access = PL0_R, .accessfn = ctr_el0_access, | |
3151 | .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, | |
3152 | /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ | |
3153 | { .name = "TCMTR", | |
3154 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, | |
3155 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3156 | { .name = "TLBTR", | |
3157 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, | |
3158 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, | |
3159 | REGINFO_SENTINEL | |
3160 | }; | |
3161 | ARMCPRegInfo crn0_wi_reginfo = { | |
3162 | .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, | |
3163 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, | |
3164 | .type = ARM_CP_NOP | ARM_CP_OVERRIDE | |
3165 | }; | |
3166 | if (arm_feature(env, ARM_FEATURE_OMAPCP) || | |
3167 | arm_feature(env, ARM_FEATURE_STRONGARM)) { | |
3168 | ARMCPRegInfo *r; | |
3169 | /* Register the blanket "writes ignored" value first to cover the | |
3170 | * whole space. Then update the specific ID registers to allow write | |
3171 | * access, so that they ignore writes rather than causing them to | |
3172 | * UNDEF. | |
3173 | */ | |
3174 | define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); | |
3175 | for (r = id_pre_v8_midr_cp_reginfo; | |
3176 | r->type != ARM_CP_SENTINEL; r++) { | |
3177 | r->access = PL1_RW; | |
3178 | } | |
3179 | for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) { | |
3180 | r->access = PL1_RW; | |
3181 | } | |
3182 | } | |
3183 | if (arm_feature(env, ARM_FEATURE_V8)) { | |
3184 | define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); | |
3185 | } else { | |
3186 | define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); | |
3187 | } | |
3188 | define_arm_cp_regs(cpu, id_cp_reginfo); | |
3189 | } | |
3190 | ||
3191 | if (arm_feature(env, ARM_FEATURE_MPIDR)) { | |
3192 | define_arm_cp_regs(cpu, mpidr_cp_reginfo); | |
3193 | } | |
3194 | ||
3195 | if (arm_feature(env, ARM_FEATURE_AUXCR)) { | |
3196 | ARMCPRegInfo auxcr = { | |
3197 | .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, | |
3198 | .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, | |
3199 | .access = PL1_RW, .type = ARM_CP_CONST, | |
3200 | .resetvalue = cpu->reset_auxcr | |
3201 | }; | |
3202 | define_one_arm_cp_reg(cpu, &auxcr); | |
3203 | } | |
3204 | ||
3205 | if (arm_feature(env, ARM_FEATURE_CBAR)) { | |
3206 | if (arm_feature(env, ARM_FEATURE_AARCH64)) { | |
3207 | /* 32 bit view is [31:18] 0...0 [43:32]. */ | |
3208 | uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) | |
3209 | | extract64(cpu->reset_cbar, 32, 12); | |
3210 | ARMCPRegInfo cbar_reginfo[] = { | |
3211 | { .name = "CBAR", | |
3212 | .type = ARM_CP_CONST, | |
3213 | .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, | |
3214 | .access = PL1_R, .resetvalue = cpu->reset_cbar }, | |
3215 | { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, | |
3216 | .type = ARM_CP_CONST, | |
3217 | .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, | |
3218 | .access = PL1_R, .resetvalue = cbar32 }, | |
3219 | REGINFO_SENTINEL | |
3220 | }; | |
3221 | /* We don't implement a r/w 64 bit CBAR currently */ | |
3222 | assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); | |
3223 | define_arm_cp_regs(cpu, cbar_reginfo); | |
3224 | } else { | |
3225 | ARMCPRegInfo cbar = { | |
3226 | .name = "CBAR", | |
3227 | .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, | |
3228 | .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar, | |
3229 | .fieldoffset = offsetof(CPUARMState, | |
3230 | cp15.c15_config_base_address) | |
3231 | }; | |
3232 | if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { | |
3233 | cbar.access = PL1_R; | |
3234 | cbar.fieldoffset = 0; | |
3235 | cbar.type = ARM_CP_CONST; | |
3236 | } | |
3237 | define_one_arm_cp_reg(cpu, &cbar); | |
3238 | } | |
3239 | } | |
3240 | ||
3241 | /* Generic registers whose values depend on the implementation */ | |
3242 | { | |
3243 | ARMCPRegInfo sctlr = { | |
3244 | .name = "SCTLR", .state = ARM_CP_STATE_BOTH, | |
3245 | .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, | |
3246 | .access = PL1_RW, | |
3247 | .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), | |
3248 | offsetof(CPUARMState, cp15.sctlr_ns) }, | |
3249 | .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, | |
3250 | .raw_writefn = raw_write, | |
3251 | }; | |
3252 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { | |
3253 | /* Normally we would always end the TB on an SCTLR write, but Linux | |
3254 | * arch/arm/mach-pxa/sleep.S expects two instructions following | |
3255 | * an MMU enable to execute from cache. Imitate this behaviour. | |
3256 | */ | |
3257 | sctlr.type |= ARM_CP_SUPPRESS_TB_END; | |
3258 | } | |
3259 | define_one_arm_cp_reg(cpu, &sctlr); | |
3260 | } | |
3261 | } | |
3262 | ||
3263 | ARMCPU *cpu_arm_init(const char *cpu_model) | |
3264 | { | |
3265 | return ARM_CPU(cpu_generic_init(TYPE_ARM_CPU, cpu_model)); | |
3266 | } | |
3267 | ||
3268 | void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) | |
3269 | { | |
3270 | CPUState *cs = CPU(cpu); | |
3271 | CPUARMState *env = &cpu->env; | |
3272 | ||
3273 | if (arm_feature(env, ARM_FEATURE_AARCH64)) { | |
3274 | gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg, | |
3275 | aarch64_fpu_gdb_set_reg, | |
3276 | 34, "aarch64-fpu.xml", 0); | |
3277 | } else if (arm_feature(env, ARM_FEATURE_NEON)) { | |
3278 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, | |
3279 | 51, "arm-neon.xml", 0); | |
3280 | } else if (arm_feature(env, ARM_FEATURE_VFP3)) { | |
3281 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, | |
3282 | 35, "arm-vfp3.xml", 0); | |
3283 | } else if (arm_feature(env, ARM_FEATURE_VFP)) { | |
3284 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, | |
3285 | 19, "arm-vfp.xml", 0); | |
3286 | } | |
3287 | } | |
3288 | ||
3289 | /* Sort alphabetically by type name, except for "any". */ | |
3290 | static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) | |
3291 | { | |
3292 | ObjectClass *class_a = (ObjectClass *)a; | |
3293 | ObjectClass *class_b = (ObjectClass *)b; | |
3294 | const char *name_a, *name_b; | |
3295 | ||
3296 | name_a = object_class_get_name(class_a); | |
3297 | name_b = object_class_get_name(class_b); | |
3298 | if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) { | |
3299 | return 1; | |
3300 | } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) { | |
3301 | return -1; | |
3302 | } else { | |
3303 | return strcmp(name_a, name_b); | |
3304 | } | |
3305 | } | |
3306 | ||
3307 | static void arm_cpu_list_entry(gpointer data, gpointer user_data) | |
3308 | { | |
3309 | ObjectClass *oc = data; | |
3310 | CPUListState *s = user_data; | |
3311 | const char *typename; | |
3312 | char *name; | |
3313 | ||
3314 | typename = object_class_get_name(oc); | |
3315 | name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU)); | |
3316 | (*s->cpu_fprintf)(s->file, " %s\n", | |
3317 | name); | |
3318 | g_free(name); | |
3319 | } | |
3320 | ||
3321 | void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf) | |
3322 | { | |
3323 | CPUListState s = { | |
3324 | .file = f, | |
3325 | .cpu_fprintf = cpu_fprintf, | |
3326 | }; | |
3327 | GSList *list; | |
3328 | ||
3329 | list = object_class_get_list(TYPE_ARM_CPU, false); | |
3330 | list = g_slist_sort(list, arm_cpu_list_compare); | |
3331 | (*cpu_fprintf)(f, "Available CPUs:\n"); | |
3332 | g_slist_foreach(list, arm_cpu_list_entry, &s); | |
3333 | g_slist_free(list); | |
3334 | #ifdef CONFIG_KVM | |
3335 | /* The 'host' CPU type is dynamically registered only if KVM is | |
3336 | * enabled, so we have to special-case it here: | |
3337 | */ | |
3338 | (*cpu_fprintf)(f, " host (only available in KVM mode)\n"); | |
3339 | #endif | |
3340 | } | |
3341 | ||
3342 | static void arm_cpu_add_definition(gpointer data, gpointer user_data) | |
3343 | { | |
3344 | ObjectClass *oc = data; | |
3345 | CpuDefinitionInfoList **cpu_list = user_data; | |
3346 | CpuDefinitionInfoList *entry; | |
3347 | CpuDefinitionInfo *info; | |
3348 | const char *typename; | |
3349 | ||
3350 | typename = object_class_get_name(oc); | |
3351 | info = g_malloc0(sizeof(*info)); | |
3352 | info->name = g_strndup(typename, | |
3353 | strlen(typename) - strlen("-" TYPE_ARM_CPU)); | |
3354 | ||
3355 | entry = g_malloc0(sizeof(*entry)); | |
3356 | entry->value = info; | |
3357 | entry->next = *cpu_list; | |
3358 | *cpu_list = entry; | |
3359 | } | |
3360 | ||
3361 | CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp) | |
3362 | { | |
3363 | CpuDefinitionInfoList *cpu_list = NULL; | |
3364 | GSList *list; | |
3365 | ||
3366 | list = object_class_get_list(TYPE_ARM_CPU, false); | |
3367 | g_slist_foreach(list, arm_cpu_add_definition, &cpu_list); | |
3368 | g_slist_free(list); | |
3369 | ||
3370 | return cpu_list; | |
3371 | } | |
3372 | ||
3373 | static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, | |
3374 | void *opaque, int state, int secstate, | |
3375 | int crm, int opc1, int opc2) | |
3376 | { | |
3377 | /* Private utility function for define_one_arm_cp_reg_with_opaque(): | |
3378 | * add a single reginfo struct to the hash table. | |
3379 | */ | |
3380 | uint32_t *key = g_new(uint32_t, 1); | |
3381 | ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); | |
3382 | int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; | |
3383 | int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0; | |
3384 | ||
3385 | /* Reset the secure state to the specific incoming state. This is | |
3386 | * necessary as the register may have been defined with both states. | |
3387 | */ | |
3388 | r2->secure = secstate; | |
3389 | ||
3390 | if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { | |
3391 | /* Register is banked (using both entries in array). | |
3392 | * Overwriting fieldoffset as the array is only used to define | |
3393 | * banked registers but later only fieldoffset is used. | |
3394 | */ | |
3395 | r2->fieldoffset = r->bank_fieldoffsets[ns]; | |
3396 | } | |
3397 | ||
3398 | if (state == ARM_CP_STATE_AA32) { | |
3399 | if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { | |
3400 | /* If the register is banked then we don't need to migrate or | |
3401 | * reset the 32-bit instance in certain cases: | |
3402 | * | |
3403 | * 1) If the register has both 32-bit and 64-bit instances then we | |
3404 | * can count on the 64-bit instance taking care of the | |
3405 | * non-secure bank. | |
3406 | * 2) If ARMv8 is enabled then we can count on a 64-bit version | |
3407 | * taking care of the secure bank. This requires that separate | |
3408 | * 32 and 64-bit definitions are provided. | |
3409 | */ | |
3410 | if ((r->state == ARM_CP_STATE_BOTH && ns) || | |
3411 | (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) { | |
3412 | r2->type |= ARM_CP_NO_MIGRATE; | |
3413 | r2->resetfn = arm_cp_reset_ignore; | |
3414 | } | |
3415 | } else if ((secstate != r->secure) && !ns) { | |
3416 | /* The register is not banked so we only want to allow migration of | |
3417 | * the non-secure instance. | |
3418 | */ | |
3419 | r2->type |= ARM_CP_NO_MIGRATE; | |
3420 | r2->resetfn = arm_cp_reset_ignore; | |
3421 | } | |
3422 | ||
3423 | if (r->state == ARM_CP_STATE_BOTH) { | |
3424 | /* We assume it is a cp15 register if the .cp field is left unset. | |
3425 | */ | |
3426 | if (r2->cp == 0) { | |
3427 | r2->cp = 15; | |
3428 | } | |
3429 | ||
3430 | #ifdef HOST_WORDS_BIGENDIAN | |
3431 | if (r2->fieldoffset) { | |
3432 | r2->fieldoffset += sizeof(uint32_t); | |
3433 | } | |
3434 | #endif | |
3435 | } | |
3436 | } | |
3437 | if (state == ARM_CP_STATE_AA64) { | |
3438 | /* To allow abbreviation of ARMCPRegInfo | |
3439 | * definitions, we treat cp == 0 as equivalent to | |
3440 | * the value for "standard guest-visible sysreg". | |
3441 | * STATE_BOTH definitions are also always "standard | |
3442 | * sysreg" in their AArch64 view (the .cp value may | |
3443 | * be non-zero for the benefit of the AArch32 view). | |
3444 | */ | |
3445 | if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) { | |
3446 | r2->cp = CP_REG_ARM64_SYSREG_CP; | |
3447 | } | |
3448 | *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm, | |
3449 | r2->opc0, opc1, opc2); | |
3450 | } else { | |
3451 | *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2); | |
3452 | } | |
3453 | if (opaque) { | |
3454 | r2->opaque = opaque; | |
3455 | } | |
3456 | /* reginfo passed to helpers is correct for the actual access, | |
3457 | * and is never ARM_CP_STATE_BOTH: | |
3458 | */ | |
3459 | r2->state = state; | |
3460 | /* Make sure reginfo passed to helpers for wildcarded regs | |
3461 | * has the correct crm/opc1/opc2 for this reg, not CP_ANY: | |
3462 | */ | |
3463 | r2->crm = crm; | |
3464 | r2->opc1 = opc1; | |
3465 | r2->opc2 = opc2; | |
3466 | /* By convention, for wildcarded registers only the first | |
3467 | * entry is used for migration; the others are marked as | |
3468 | * NO_MIGRATE so we don't try to transfer the register | |
3469 | * multiple times. Special registers (ie NOP/WFI) are | |
3470 | * never migratable. | |
3471 | */ | |
3472 | if ((r->type & ARM_CP_SPECIAL) || | |
3473 | ((r->crm == CP_ANY) && crm != 0) || | |
3474 | ((r->opc1 == CP_ANY) && opc1 != 0) || | |
3475 | ((r->opc2 == CP_ANY) && opc2 != 0)) { | |
3476 | r2->type |= ARM_CP_NO_MIGRATE; | |
3477 | } | |
3478 | ||
3479 | /* Overriding of an existing definition must be explicitly | |
3480 | * requested. | |
3481 | */ | |
3482 | if (!(r->type & ARM_CP_OVERRIDE)) { | |
3483 | ARMCPRegInfo *oldreg; | |
3484 | oldreg = g_hash_table_lookup(cpu->cp_regs, key); | |
3485 | if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { | |
3486 | fprintf(stderr, "Register redefined: cp=%d %d bit " | |
3487 | "crn=%d crm=%d opc1=%d opc2=%d, " | |
3488 | "was %s, now %s\n", r2->cp, 32 + 32 * is64, | |
3489 | r2->crn, r2->crm, r2->opc1, r2->opc2, | |
3490 | oldreg->name, r2->name); | |
3491 | g_assert_not_reached(); | |
3492 | } | |
3493 | } | |
3494 | g_hash_table_insert(cpu->cp_regs, key, r2); | |
3495 | } | |
3496 | ||
3497 | ||
3498 | void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, | |
3499 | const ARMCPRegInfo *r, void *opaque) | |
3500 | { | |
3501 | /* Define implementations of coprocessor registers. | |
3502 | * We store these in a hashtable because typically | |
3503 | * there are less than 150 registers in a space which | |
3504 | * is 16*16*16*8*8 = 262144 in size. | |
3505 | * Wildcarding is supported for the crm, opc1 and opc2 fields. | |
3506 | * If a register is defined twice then the second definition is | |
3507 | * used, so this can be used to define some generic registers and | |
3508 | * then override them with implementation specific variations. | |
3509 | * At least one of the original and the second definition should | |
3510 | * include ARM_CP_OVERRIDE in its type bits -- this is just a guard | |
3511 | * against accidental use. | |
3512 | * | |
3513 | * The state field defines whether the register is to be | |
3514 | * visible in the AArch32 or AArch64 execution state. If the | |
3515 | * state is set to ARM_CP_STATE_BOTH then we synthesise a | |
3516 | * reginfo structure for the AArch32 view, which sees the lower | |
3517 | * 32 bits of the 64 bit register. | |
3518 | * | |
3519 | * Only registers visible in AArch64 may set r->opc0; opc0 cannot | |
3520 | * be wildcarded. AArch64 registers are always considered to be 64 | |
3521 | * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of | |
3522 | * the register, if any. | |
3523 | */ | |
3524 | int crm, opc1, opc2, state; | |
3525 | int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; | |
3526 | int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; | |
3527 | int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; | |
3528 | int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; | |
3529 | int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; | |
3530 | int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; | |
3531 | /* 64 bit registers have only CRm and Opc1 fields */ | |
3532 | assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); | |
3533 | /* op0 only exists in the AArch64 encodings */ | |
3534 | assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); | |
3535 | /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ | |
3536 | assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); | |
3537 | /* The AArch64 pseudocode CheckSystemAccess() specifies that op1 | |
3538 | * encodes a minimum access level for the register. We roll this | |
3539 | * runtime check into our general permission check code, so check | |
3540 | * here that the reginfo's specified permissions are strict enough | |
3541 | * to encompass the generic architectural permission check. | |
3542 | */ | |
3543 | if (r->state != ARM_CP_STATE_AA32) { | |
3544 | int mask = 0; | |
3545 | switch (r->opc1) { | |
3546 | case 0: case 1: case 2: | |
3547 | /* min_EL EL1 */ | |
3548 | mask = PL1_RW; | |
3549 | break; | |
3550 | case 3: | |
3551 | /* min_EL EL0 */ | |
3552 | mask = PL0_RW; | |
3553 | break; | |
3554 | case 4: | |
3555 | /* min_EL EL2 */ | |
3556 | mask = PL2_RW; | |
3557 | break; | |
3558 | case 5: | |
3559 | /* unallocated encoding, so not possible */ | |
3560 | assert(false); | |
3561 | break; | |
3562 | case 6: | |
3563 | /* min_EL EL3 */ | |
3564 | mask = PL3_RW; | |
3565 | break; | |
3566 | case 7: | |
3567 | /* min_EL EL1, secure mode only (we don't check the latter) */ | |
3568 | mask = PL1_RW; | |
3569 | break; | |
3570 | default: | |
3571 | /* broken reginfo with out-of-range opc1 */ | |
3572 | assert(false); | |
3573 | break; | |
3574 | } | |
3575 | /* assert our permissions are not too lax (stricter is fine) */ | |
3576 | assert((r->access & ~mask) == 0); | |
3577 | } | |
3578 | ||
3579 | /* Check that the register definition has enough info to handle | |
3580 | * reads and writes if they are permitted. | |
3581 | */ | |
3582 | if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { | |
3583 | if (r->access & PL3_R) { | |
3584 | assert((r->fieldoffset || | |
3585 | (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || | |
3586 | r->readfn); | |
3587 | } | |
3588 | if (r->access & PL3_W) { | |
3589 | assert((r->fieldoffset || | |
3590 | (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || | |
3591 | r->writefn); | |
3592 | } | |
3593 | } | |
3594 | /* Bad type field probably means missing sentinel at end of reg list */ | |
3595 | assert(cptype_valid(r->type)); | |
3596 | for (crm = crmmin; crm <= crmmax; crm++) { | |
3597 | for (opc1 = opc1min; opc1 <= opc1max; opc1++) { | |
3598 | for (opc2 = opc2min; opc2 <= opc2max; opc2++) { | |
3599 | for (state = ARM_CP_STATE_AA32; | |
3600 | state <= ARM_CP_STATE_AA64; state++) { | |
3601 | if (r->state != state && r->state != ARM_CP_STATE_BOTH) { | |
3602 | continue; | |
3603 | } | |
3604 | if (state == ARM_CP_STATE_AA32) { | |
3605 | /* Under AArch32 CP registers can be common | |
3606 | * (same for secure and non-secure world) or banked. | |
3607 | */ | |
3608 | switch (r->secure) { | |
3609 | case ARM_CP_SECSTATE_S: | |
3610 | case ARM_CP_SECSTATE_NS: | |
3611 | add_cpreg_to_hashtable(cpu, r, opaque, state, | |
3612 | r->secure, crm, opc1, opc2); | |
3613 | break; | |
3614 | default: | |
3615 | add_cpreg_to_hashtable(cpu, r, opaque, state, | |
3616 | ARM_CP_SECSTATE_S, | |
3617 | crm, opc1, opc2); | |
3618 | add_cpreg_to_hashtable(cpu, r, opaque, state, | |
3619 | ARM_CP_SECSTATE_NS, | |
3620 | crm, opc1, opc2); | |
3621 | break; | |
3622 | } | |
3623 | } else { | |
3624 | /* AArch64 registers get mapped to non-secure instance | |
3625 | * of AArch32 */ | |
3626 | add_cpreg_to_hashtable(cpu, r, opaque, state, | |
3627 | ARM_CP_SECSTATE_NS, | |
3628 | crm, opc1, opc2); | |
3629 | } | |
3630 | } | |
3631 | } | |
3632 | } | |
3633 | } | |
3634 | } | |
3635 | ||
3636 | void define_arm_cp_regs_with_opaque(ARMCPU *cpu, | |
3637 | const ARMCPRegInfo *regs, void *opaque) | |
3638 | { | |
3639 | /* Define a whole list of registers */ | |
3640 | const ARMCPRegInfo *r; | |
3641 | for (r = regs; r->type != ARM_CP_SENTINEL; r++) { | |
3642 | define_one_arm_cp_reg_with_opaque(cpu, r, opaque); | |
3643 | } | |
3644 | } | |
3645 | ||
3646 | const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) | |
3647 | { | |
3648 | return g_hash_table_lookup(cpregs, &encoded_cp); | |
3649 | } | |
3650 | ||
3651 | void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, | |
3652 | uint64_t value) | |
3653 | { | |
3654 | /* Helper coprocessor write function for write-ignore registers */ | |
3655 | } | |
3656 | ||
3657 | uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) | |
3658 | { | |
3659 | /* Helper coprocessor write function for read-as-zero registers */ | |
3660 | return 0; | |
3661 | } | |
3662 | ||
3663 | void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) | |
3664 | { | |
3665 | /* Helper coprocessor reset function for do-nothing-on-reset registers */ | |
3666 | } | |
3667 | ||
3668 | static int bad_mode_switch(CPUARMState *env, int mode) | |
3669 | { | |
3670 | /* Return true if it is not valid for us to switch to | |
3671 | * this CPU mode (ie all the UNPREDICTABLE cases in | |
3672 | * the ARM ARM CPSRWriteByInstr pseudocode). | |
3673 | */ | |
3674 | switch (mode) { | |
3675 | case ARM_CPU_MODE_USR: | |
3676 | case ARM_CPU_MODE_SYS: | |
3677 | case ARM_CPU_MODE_SVC: | |
3678 | case ARM_CPU_MODE_ABT: | |
3679 | case ARM_CPU_MODE_UND: | |
3680 | case ARM_CPU_MODE_IRQ: | |
3681 | case ARM_CPU_MODE_FIQ: | |
3682 | return 0; | |
3683 | case ARM_CPU_MODE_MON: | |
3684 | return !arm_is_secure(env); | |
3685 | default: | |
3686 | return 1; | |
3687 | } | |
3688 | } | |
3689 | ||
3690 | uint32_t cpsr_read(CPUARMState *env) | |
3691 | { | |
3692 | int ZF; | |
3693 | ZF = (env->ZF == 0); | |
3694 | return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | | |
3695 | (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) | |
3696 | | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) | |
3697 | | ((env->condexec_bits & 0xfc) << 8) | |
3698 | | (env->GE << 16) | (env->daif & CPSR_AIF); | |
3699 | } | |
3700 | ||
3701 | void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask) | |
3702 | { | |
3703 | uint32_t changed_daif; | |
3704 | ||
3705 | if (mask & CPSR_NZCV) { | |
3706 | env->ZF = (~val) & CPSR_Z; | |
3707 | env->NF = val; | |
3708 | env->CF = (val >> 29) & 1; | |
3709 | env->VF = (val << 3) & 0x80000000; | |
3710 | } | |
3711 | if (mask & CPSR_Q) | |
3712 | env->QF = ((val & CPSR_Q) != 0); | |
3713 | if (mask & CPSR_T) | |
3714 | env->thumb = ((val & CPSR_T) != 0); | |
3715 | if (mask & CPSR_IT_0_1) { | |
3716 | env->condexec_bits &= ~3; | |
3717 | env->condexec_bits |= (val >> 25) & 3; | |
3718 | } | |
3719 | if (mask & CPSR_IT_2_7) { | |
3720 | env->condexec_bits &= 3; | |
3721 | env->condexec_bits |= (val >> 8) & 0xfc; | |
3722 | } | |
3723 | if (mask & CPSR_GE) { | |
3724 | env->GE = (val >> 16) & 0xf; | |
3725 | } | |
3726 | ||
3727 | /* In a V7 implementation that includes the security extensions but does | |
3728 | * not include Virtualization Extensions the SCR.FW and SCR.AW bits control | |
3729 | * whether non-secure software is allowed to change the CPSR_F and CPSR_A | |
3730 | * bits respectively. | |
3731 | * | |
3732 | * In a V8 implementation, it is permitted for privileged software to | |
3733 | * change the CPSR A/F bits regardless of the SCR.AW/FW bits. | |
3734 | */ | |
3735 | if (!arm_feature(env, ARM_FEATURE_V8) && | |
3736 | arm_feature(env, ARM_FEATURE_EL3) && | |
3737 | !arm_feature(env, ARM_FEATURE_EL2) && | |
3738 | !arm_is_secure(env)) { | |
3739 | ||
3740 | changed_daif = (env->daif ^ val) & mask; | |
3741 | ||
3742 | if (changed_daif & CPSR_A) { | |
3743 | /* Check to see if we are allowed to change the masking of async | |
3744 | * abort exceptions from a non-secure state. | |
3745 | */ | |
3746 | if (!(env->cp15.scr_el3 & SCR_AW)) { | |
3747 | qemu_log_mask(LOG_GUEST_ERROR, | |
3748 | "Ignoring attempt to switch CPSR_A flag from " | |
3749 | "non-secure world with SCR.AW bit clear\n"); | |
3750 | mask &= ~CPSR_A; | |
3751 | } | |
3752 | } | |
3753 | ||
3754 | if (changed_daif & CPSR_F) { | |
3755 | /* Check to see if we are allowed to change the masking of FIQ | |
3756 | * exceptions from a non-secure state. | |
3757 | */ | |
3758 | if (!(env->cp15.scr_el3 & SCR_FW)) { | |
3759 | qemu_log_mask(LOG_GUEST_ERROR, | |
3760 | "Ignoring attempt to switch CPSR_F flag from " | |
3761 | "non-secure world with SCR.FW bit clear\n"); | |
3762 | mask &= ~CPSR_F; | |
3763 | } | |
3764 | ||
3765 | /* Check whether non-maskable FIQ (NMFI) support is enabled. | |
3766 | * If this bit is set software is not allowed to mask | |
3767 | * FIQs, but is allowed to set CPSR_F to 0. | |
3768 | */ | |
3769 | if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && | |
3770 | (val & CPSR_F)) { | |
3771 | qemu_log_mask(LOG_GUEST_ERROR, | |
3772 | "Ignoring attempt to enable CPSR_F flag " | |
3773 | "(non-maskable FIQ [NMFI] support enabled)\n"); | |
3774 | mask &= ~CPSR_F; | |
3775 | } | |
3776 | } | |
3777 | } | |
3778 | ||
3779 | env->daif &= ~(CPSR_AIF & mask); | |
3780 | env->daif |= val & CPSR_AIF & mask; | |
3781 | ||
3782 | if ((env->uncached_cpsr ^ val) & mask & CPSR_M) { | |
3783 | if (bad_mode_switch(env, val & CPSR_M)) { | |
3784 | /* Attempt to switch to an invalid mode: this is UNPREDICTABLE. | |
3785 | * We choose to ignore the attempt and leave the CPSR M field | |
3786 | * untouched. | |
3787 | */ | |
3788 | mask &= ~CPSR_M; | |
3789 | } else { | |
3790 | switch_mode(env, val & CPSR_M); | |
3791 | } | |
3792 | } | |
3793 | mask &= ~CACHED_CPSR_BITS; | |
3794 | env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); | |
3795 | } | |
3796 | ||
3797 | /* Sign/zero extend */ | |
3798 | uint32_t HELPER(sxtb16)(uint32_t x) | |
3799 | { | |
3800 | uint32_t res; | |
3801 | res = (uint16_t)(int8_t)x; | |
3802 | res |= (uint32_t)(int8_t)(x >> 16) << 16; | |
3803 | return res; | |
3804 | } | |
3805 | ||
3806 | uint32_t HELPER(uxtb16)(uint32_t x) | |
3807 | { | |
3808 | uint32_t res; | |
3809 | res = (uint16_t)(uint8_t)x; | |
3810 | res |= (uint32_t)(uint8_t)(x >> 16) << 16; | |
3811 | return res; | |
3812 | } | |
3813 | ||
3814 | uint32_t HELPER(clz)(uint32_t x) | |
3815 | { | |
3816 | return clz32(x); | |
3817 | } | |
3818 | ||
3819 | int32_t HELPER(sdiv)(int32_t num, int32_t den) | |
3820 | { | |
3821 | if (den == 0) | |
3822 | return 0; | |
3823 | if (num == INT_MIN && den == -1) | |
3824 | return INT_MIN; | |
3825 | return num / den; | |
3826 | } | |
3827 | ||
3828 | uint32_t HELPER(udiv)(uint32_t num, uint32_t den) | |
3829 | { | |
3830 | if (den == 0) | |
3831 | return 0; | |
3832 | return num / den; | |
3833 | } | |
3834 | ||
3835 | uint32_t HELPER(rbit)(uint32_t x) | |
3836 | { | |
3837 | x = ((x & 0xff000000) >> 24) | |
3838 | | ((x & 0x00ff0000) >> 8) | |
3839 | | ((x & 0x0000ff00) << 8) | |
3840 | | ((x & 0x000000ff) << 24); | |
3841 | x = ((x & 0xf0f0f0f0) >> 4) | |
3842 | | ((x & 0x0f0f0f0f) << 4); | |
3843 | x = ((x & 0x88888888) >> 3) | |
3844 | | ((x & 0x44444444) >> 1) | |
3845 | | ((x & 0x22222222) << 1) | |
3846 | | ((x & 0x11111111) << 3); | |
3847 | return x; | |
3848 | } | |
3849 | ||
3850 | #if defined(CONFIG_USER_ONLY) | |
3851 | ||
3852 | int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, int rw, | |
3853 | int mmu_idx) | |
3854 | { | |
3855 | ARMCPU *cpu = ARM_CPU(cs); | |
3856 | CPUARMState *env = &cpu->env; | |
3857 | ||
3858 | env->exception.vaddress = address; | |
3859 | if (rw == 2) { | |
3860 | cs->exception_index = EXCP_PREFETCH_ABORT; | |
3861 | } else { | |
3862 | cs->exception_index = EXCP_DATA_ABORT; | |
3863 | } | |
3864 | return 1; | |
3865 | } | |
3866 | ||
3867 | /* These should probably raise undefined insn exceptions. */ | |
3868 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) | |
3869 | { | |
3870 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3871 | ||
3872 | cpu_abort(CPU(cpu), "v7m_msr %d\n", reg); | |
3873 | } | |
3874 | ||
3875 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) | |
3876 | { | |
3877 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3878 | ||
3879 | cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg); | |
3880 | return 0; | |
3881 | } | |
3882 | ||
3883 | void switch_mode(CPUARMState *env, int mode) | |
3884 | { | |
3885 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3886 | ||
3887 | if (mode != ARM_CPU_MODE_USR) { | |
3888 | cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); | |
3889 | } | |
3890 | } | |
3891 | ||
3892 | void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val) | |
3893 | { | |
3894 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3895 | ||
3896 | cpu_abort(CPU(cpu), "banked r13 write\n"); | |
3897 | } | |
3898 | ||
3899 | uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode) | |
3900 | { | |
3901 | ARMCPU *cpu = arm_env_get_cpu(env); | |
3902 | ||
3903 | cpu_abort(CPU(cpu), "banked r13 read\n"); | |
3904 | return 0; | |
3905 | } | |
3906 | ||
3907 | unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx) | |
3908 | { | |
3909 | return 1; | |
3910 | } | |
3911 | ||
3912 | #else | |
3913 | ||
3914 | /* Map CPU modes onto saved register banks. */ | |
3915 | int bank_number(int mode) | |
3916 | { | |
3917 | switch (mode) { | |
3918 | case ARM_CPU_MODE_USR: | |
3919 | case ARM_CPU_MODE_SYS: | |
3920 | return 0; | |
3921 | case ARM_CPU_MODE_SVC: | |
3922 | return 1; | |
3923 | case ARM_CPU_MODE_ABT: | |
3924 | return 2; | |
3925 | case ARM_CPU_MODE_UND: | |
3926 | return 3; | |
3927 | case ARM_CPU_MODE_IRQ: | |
3928 | return 4; | |
3929 | case ARM_CPU_MODE_FIQ: | |
3930 | return 5; | |
3931 | case ARM_CPU_MODE_HYP: | |
3932 | return 6; | |
3933 | case ARM_CPU_MODE_MON: | |
3934 | return 7; | |
3935 | } | |
3936 | hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode); | |
3937 | } | |
3938 | ||
3939 | void switch_mode(CPUARMState *env, int mode) | |
3940 | { | |
3941 | int old_mode; | |
3942 | int i; | |
3943 | ||
3944 | old_mode = env->uncached_cpsr & CPSR_M; | |
3945 | if (mode == old_mode) | |
3946 | return; | |
3947 | ||
3948 | if (old_mode == ARM_CPU_MODE_FIQ) { | |
3949 | memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); | |
3950 | memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); | |
3951 | } else if (mode == ARM_CPU_MODE_FIQ) { | |
3952 | memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); | |
3953 | memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); | |
3954 | } | |
3955 | ||
3956 | i = bank_number(old_mode); | |
3957 | env->banked_r13[i] = env->regs[13]; | |
3958 | env->banked_r14[i] = env->regs[14]; | |
3959 | env->banked_spsr[i] = env->spsr; | |
3960 | ||
3961 | i = bank_number(mode); | |
3962 | env->regs[13] = env->banked_r13[i]; | |
3963 | env->regs[14] = env->banked_r14[i]; | |
3964 | env->spsr = env->banked_spsr[i]; | |
3965 | } | |
3966 | ||
3967 | /* Physical Interrupt Target EL Lookup Table | |
3968 | * | |
3969 | * [ From ARM ARM section G1.13.4 (Table G1-15) ] | |
3970 | * | |
3971 | * The below multi-dimensional table is used for looking up the target | |
3972 | * exception level given numerous condition criteria. Specifically, the | |
3973 | * target EL is based on SCR and HCR routing controls as well as the | |
3974 | * currently executing EL and secure state. | |
3975 | * | |
3976 | * Dimensions: | |
3977 | * target_el_table[2][2][2][2][2][4] | |
3978 | * | | | | | +--- Current EL | |
3979 | * | | | | +------ Non-secure(0)/Secure(1) | |
3980 | * | | | +--------- HCR mask override | |
3981 | * | | +------------ SCR exec state control | |
3982 | * | +--------------- SCR mask override | |
3983 | * +------------------ 32-bit(0)/64-bit(1) EL3 | |
3984 | * | |
3985 | * The table values are as such: | |
3986 | * 0-3 = EL0-EL3 | |
3987 | * -1 = Cannot occur | |
3988 | * | |
3989 | * The ARM ARM target EL table includes entries indicating that an "exception | |
3990 | * is not taken". The two cases where this is applicable are: | |
3991 | * 1) An exception is taken from EL3 but the SCR does not have the exception | |
3992 | * routed to EL3. | |
3993 | * 2) An exception is taken from EL2 but the HCR does not have the exception | |
3994 | * routed to EL2. | |
3995 | * In these two cases, the below table contain a target of EL1. This value is | |
3996 | * returned as it is expected that the consumer of the table data will check | |
3997 | * for "target EL >= current EL" to ensure the exception is not taken. | |
3998 | * | |
3999 | * SCR HCR | |
4000 | * 64 EA AMO From | |
4001 | * BIT IRQ IMO Non-secure Secure | |
4002 | * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 | |
4003 | */ | |
4004 | const int8_t target_el_table[2][2][2][2][2][4] = { | |
4005 | {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, | |
4006 | {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, | |
4007 | {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, | |
4008 | {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, | |
4009 | {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, | |
4010 | {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, | |
4011 | {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, | |
4012 | {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, | |
4013 | {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, | |
4014 | {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},}, | |
4015 | {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },}, | |
4016 | {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},}, | |
4017 | {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, | |
4018 | {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, | |
4019 | {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, | |
4020 | {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},}, | |
4021 | }; | |
4022 | ||
4023 | /* | |
4024 | * Determine the target EL for physical exceptions | |
4025 | */ | |
4026 | static inline uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, | |
4027 | uint32_t cur_el, bool secure) | |
4028 | { | |
4029 | CPUARMState *env = cs->env_ptr; | |
4030 | int rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW); | |
4031 | int scr; | |
4032 | int hcr; | |
4033 | int target_el; | |
4034 | int is64 = arm_el_is_aa64(env, 3); | |
4035 | ||
4036 | switch (excp_idx) { | |
4037 | case EXCP_IRQ: | |
4038 | scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); | |
4039 | hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO); | |
4040 | break; | |
4041 | case EXCP_FIQ: | |
4042 | scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); | |
4043 | hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO); | |
4044 | break; | |
4045 | default: | |
4046 | scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); | |
4047 | hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO); | |
4048 | break; | |
4049 | }; | |
4050 | ||
4051 | /* If HCR.TGE is set then HCR is treated as being 1 */ | |
4052 | hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE); | |
4053 | ||
4054 | /* Perform a table-lookup for the target EL given the current state */ | |
4055 | target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; | |
4056 | ||
4057 | assert(target_el > 0); | |
4058 | ||
4059 | return target_el; | |
4060 | } | |
4061 | ||
4062 | /* | |
4063 | * Determine the target EL for a given exception type. | |
4064 | */ | |
4065 | unsigned int arm_excp_target_el(CPUState *cs, unsigned int excp_idx) | |
4066 | { | |
4067 | ARMCPU *cpu = ARM_CPU(cs); | |
4068 | CPUARMState *env = &cpu->env; | |
4069 | unsigned int cur_el = arm_current_el(env); | |
4070 | unsigned int target_el; | |
4071 | bool secure = arm_is_secure(env); | |
4072 | ||
4073 | switch (excp_idx) { | |
4074 | case EXCP_HVC: | |
4075 | case EXCP_HYP_TRAP: | |
4076 | target_el = 2; | |
4077 | break; | |
4078 | case EXCP_SMC: | |
4079 | target_el = 3; | |
4080 | break; | |
4081 | case EXCP_FIQ: | |
4082 | case EXCP_IRQ: | |
4083 | target_el = arm_phys_excp_target_el(cs, excp_idx, cur_el, secure); | |
4084 | break; | |
4085 | case EXCP_VIRQ: | |
4086 | case EXCP_VFIQ: | |
4087 | target_el = 1; | |
4088 | break; | |
4089 | default: | |
4090 | target_el = MAX(cur_el, 1); | |
4091 | break; | |
4092 | } | |
4093 | return target_el; | |
4094 | } | |
4095 | ||
4096 | static void v7m_push(CPUARMState *env, uint32_t val) | |
4097 | { | |
4098 | CPUState *cs = CPU(arm_env_get_cpu(env)); | |
4099 | ||
4100 | env->regs[13] -= 4; | |
4101 | stl_phys(cs->as, env->regs[13], val); | |
4102 | } | |
4103 | ||
4104 | static uint32_t v7m_pop(CPUARMState *env) | |
4105 | { | |
4106 | CPUState *cs = CPU(arm_env_get_cpu(env)); | |
4107 | uint32_t val; | |
4108 | ||
4109 | val = ldl_phys(cs->as, env->regs[13]); | |
4110 | env->regs[13] += 4; | |
4111 | return val; | |
4112 | } | |
4113 | ||
4114 | /* Switch to V7M main or process stack pointer. */ | |
4115 | static void switch_v7m_sp(CPUARMState *env, int process) | |
4116 | { | |
4117 | uint32_t tmp; | |
4118 | if (env->v7m.current_sp != process) { | |
4119 | tmp = env->v7m.other_sp; | |
4120 | env->v7m.other_sp = env->regs[13]; | |
4121 | env->regs[13] = tmp; | |
4122 | env->v7m.current_sp = process; | |
4123 | } | |
4124 | } | |
4125 | ||
4126 | static void do_v7m_exception_exit(CPUARMState *env) | |
4127 | { | |
4128 | uint32_t type; | |
4129 | uint32_t xpsr; | |
4130 | ||
4131 | type = env->regs[15]; | |
4132 | if (env->v7m.exception != 0) | |
4133 | armv7m_nvic_complete_irq(env->nvic, env->v7m.exception); | |
4134 | ||
4135 | /* Switch to the target stack. */ | |
4136 | switch_v7m_sp(env, (type & 4) != 0); | |
4137 | /* Pop registers. */ | |
4138 | env->regs[0] = v7m_pop(env); | |
4139 | env->regs[1] = v7m_pop(env); | |
4140 | env->regs[2] = v7m_pop(env); | |
4141 | env->regs[3] = v7m_pop(env); | |
4142 | env->regs[12] = v7m_pop(env); | |
4143 | env->regs[14] = v7m_pop(env); | |
4144 | env->regs[15] = v7m_pop(env); | |
4145 | xpsr = v7m_pop(env); | |
4146 | xpsr_write(env, xpsr, 0xfffffdff); | |
4147 | /* Undo stack alignment. */ | |
4148 | if (xpsr & 0x200) | |
4149 | env->regs[13] |= 4; | |
4150 | /* ??? The exception return type specifies Thread/Handler mode. However | |
4151 | this is also implied by the xPSR value. Not sure what to do | |
4152 | if there is a mismatch. */ | |
4153 | /* ??? Likewise for mismatches between the CONTROL register and the stack | |
4154 | pointer. */ | |
4155 | } | |
4156 | ||
4157 | void arm_v7m_cpu_do_interrupt(CPUState *cs) | |
4158 | { | |
4159 | ARMCPU *cpu = ARM_CPU(cs); | |
4160 | CPUARMState *env = &cpu->env; | |
4161 | uint32_t xpsr = xpsr_read(env); | |
4162 | uint32_t lr; | |
4163 | uint32_t addr; | |
4164 | ||
4165 | arm_log_exception(cs->exception_index); | |
4166 | ||
4167 | lr = 0xfffffff1; | |
4168 | if (env->v7m.current_sp) | |
4169 | lr |= 4; | |
4170 | if (env->v7m.exception == 0) | |
4171 | lr |= 8; | |
4172 | ||
4173 | /* For exceptions we just mark as pending on the NVIC, and let that | |
4174 | handle it. */ | |
4175 | /* TODO: Need to escalate if the current priority is higher than the | |
4176 | one we're raising. */ | |
4177 | switch (cs->exception_index) { | |
4178 | case EXCP_UDEF: | |
4179 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE); | |
4180 | return; | |
4181 | case EXCP_SWI: | |
4182 | /* The PC already points to the next instruction. */ | |
4183 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC); | |
4184 | return; | |
4185 | case EXCP_PREFETCH_ABORT: | |
4186 | case EXCP_DATA_ABORT: | |
4187 | /* TODO: if we implemented the MPU registers, this is where we | |
4188 | * should set the MMFAR, etc from exception.fsr and exception.vaddress. | |
4189 | */ | |
4190 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM); | |
4191 | return; | |
4192 | case EXCP_BKPT: | |
4193 | if (semihosting_enabled) { | |
4194 | int nr; | |
4195 | nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff; | |
4196 | if (nr == 0xab) { | |
4197 | env->regs[15] += 2; | |
4198 | env->regs[0] = do_arm_semihosting(env); | |
4199 | qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n"); | |
4200 | return; | |
4201 | } | |
4202 | } | |
4203 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG); | |
4204 | return; | |
4205 | case EXCP_IRQ: | |
4206 | env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic); | |
4207 | break; | |
4208 | case EXCP_EXCEPTION_EXIT: | |
4209 | do_v7m_exception_exit(env); | |
4210 | return; | |
4211 | default: | |
4212 | cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); | |
4213 | return; /* Never happens. Keep compiler happy. */ | |
4214 | } | |
4215 | ||
4216 | /* Align stack pointer. */ | |
4217 | /* ??? Should only do this if Configuration Control Register | |
4218 | STACKALIGN bit is set. */ | |
4219 | if (env->regs[13] & 4) { | |
4220 | env->regs[13] -= 4; | |
4221 | xpsr |= 0x200; | |
4222 | } | |
4223 | /* Switch to the handler mode. */ | |
4224 | v7m_push(env, xpsr); | |
4225 | v7m_push(env, env->regs[15]); | |
4226 | v7m_push(env, env->regs[14]); | |
4227 | v7m_push(env, env->regs[12]); | |
4228 | v7m_push(env, env->regs[3]); | |
4229 | v7m_push(env, env->regs[2]); | |
4230 | v7m_push(env, env->regs[1]); | |
4231 | v7m_push(env, env->regs[0]); | |
4232 | switch_v7m_sp(env, 0); | |
4233 | /* Clear IT bits */ | |
4234 | env->condexec_bits = 0; | |
4235 | env->regs[14] = lr; | |
4236 | addr = ldl_phys(cs->as, env->v7m.vecbase + env->v7m.exception * 4); | |
4237 | env->regs[15] = addr & 0xfffffffe; | |
4238 | env->thumb = addr & 1; | |
4239 | } | |
4240 | ||
4241 | /* Handle a CPU exception. */ | |
4242 | void arm_cpu_do_interrupt(CPUState *cs) | |
4243 | { | |
4244 | ARMCPU *cpu = ARM_CPU(cs); | |
4245 | CPUARMState *env = &cpu->env; | |
4246 | uint32_t addr; | |
4247 | uint32_t mask; | |
4248 | int new_mode; | |
4249 | uint32_t offset; | |
4250 | uint32_t moe; | |
4251 | ||
4252 | assert(!IS_M(env)); | |
4253 | ||
4254 | arm_log_exception(cs->exception_index); | |
4255 | ||
4256 | if (arm_is_psci_call(cpu, cs->exception_index)) { | |
4257 | arm_handle_psci_call(cpu); | |
4258 | qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); | |
4259 | return; | |
4260 | } | |
4261 | ||
4262 | /* If this is a debug exception we must update the DBGDSCR.MOE bits */ | |
4263 | switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) { | |
4264 | case EC_BREAKPOINT: | |
4265 | case EC_BREAKPOINT_SAME_EL: | |
4266 | moe = 1; | |
4267 | break; | |
4268 | case EC_WATCHPOINT: | |
4269 | case EC_WATCHPOINT_SAME_EL: | |
4270 | moe = 10; | |
4271 | break; | |
4272 | case EC_AA32_BKPT: | |
4273 | moe = 3; | |
4274 | break; | |
4275 | case EC_VECTORCATCH: | |
4276 | moe = 5; | |
4277 | break; | |
4278 | default: | |
4279 | moe = 0; | |
4280 | break; | |
4281 | } | |
4282 | ||
4283 | if (moe) { | |
4284 | env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); | |
4285 | } | |
4286 | ||
4287 | /* TODO: Vectored interrupt controller. */ | |
4288 | switch (cs->exception_index) { | |
4289 | case EXCP_UDEF: | |
4290 | new_mode = ARM_CPU_MODE_UND; | |
4291 | addr = 0x04; | |
4292 | mask = CPSR_I; | |
4293 | if (env->thumb) | |
4294 | offset = 2; | |
4295 | else | |
4296 | offset = 4; | |
4297 | break; | |
4298 | case EXCP_SWI: | |
4299 | if (semihosting_enabled) { | |
4300 | /* Check for semihosting interrupt. */ | |
4301 | if (env->thumb) { | |
4302 | mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code) | |
4303 | & 0xff; | |
4304 | } else { | |
4305 | mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code) | |
4306 | & 0xffffff; | |
4307 | } | |
4308 | /* Only intercept calls from privileged modes, to provide some | |
4309 | semblance of security. */ | |
4310 | if (((mask == 0x123456 && !env->thumb) | |
4311 | || (mask == 0xab && env->thumb)) | |
4312 | && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) { | |
4313 | env->regs[0] = do_arm_semihosting(env); | |
4314 | qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n"); | |
4315 | return; | |
4316 | } | |
4317 | } | |
4318 | new_mode = ARM_CPU_MODE_SVC; | |
4319 | addr = 0x08; | |
4320 | mask = CPSR_I; | |
4321 | /* The PC already points to the next instruction. */ | |
4322 | offset = 0; | |
4323 | break; | |
4324 | case EXCP_BKPT: | |
4325 | /* See if this is a semihosting syscall. */ | |
4326 | if (env->thumb && semihosting_enabled) { | |
4327 | mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff; | |
4328 | if (mask == 0xab | |
4329 | && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) { | |
4330 | env->regs[15] += 2; | |
4331 | env->regs[0] = do_arm_semihosting(env); | |
4332 | qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n"); | |
4333 | return; | |
4334 | } | |
4335 | } | |
4336 | env->exception.fsr = 2; | |
4337 | /* Fall through to prefetch abort. */ | |
4338 | case EXCP_PREFETCH_ABORT: | |
4339 | A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); | |
4340 | A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); | |
4341 | qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", | |
4342 | env->exception.fsr, (uint32_t)env->exception.vaddress); | |
4343 | new_mode = ARM_CPU_MODE_ABT; | |
4344 | addr = 0x0c; | |
4345 | mask = CPSR_A | CPSR_I; | |
4346 | offset = 4; | |
4347 | break; | |
4348 | case EXCP_DATA_ABORT: | |
4349 | A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); | |
4350 | A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); | |
4351 | qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", | |
4352 | env->exception.fsr, | |
4353 | (uint32_t)env->exception.vaddress); | |
4354 | new_mode = ARM_CPU_MODE_ABT; | |
4355 | addr = 0x10; | |
4356 | mask = CPSR_A | CPSR_I; | |
4357 | offset = 8; | |
4358 | break; | |
4359 | case EXCP_IRQ: | |
4360 | new_mode = ARM_CPU_MODE_IRQ; | |
4361 | addr = 0x18; | |
4362 | /* Disable IRQ and imprecise data aborts. */ | |
4363 | mask = CPSR_A | CPSR_I; | |
4364 | offset = 4; | |
4365 | if (env->cp15.scr_el3 & SCR_IRQ) { | |
4366 | /* IRQ routed to monitor mode */ | |
4367 | new_mode = ARM_CPU_MODE_MON; | |
4368 | mask |= CPSR_F; | |
4369 | } | |
4370 | break; | |
4371 | case EXCP_FIQ: | |
4372 | new_mode = ARM_CPU_MODE_FIQ; | |
4373 | addr = 0x1c; | |
4374 | /* Disable FIQ, IRQ and imprecise data aborts. */ | |
4375 | mask = CPSR_A | CPSR_I | CPSR_F; | |
4376 | if (env->cp15.scr_el3 & SCR_FIQ) { | |
4377 | /* FIQ routed to monitor mode */ | |
4378 | new_mode = ARM_CPU_MODE_MON; | |
4379 | } | |
4380 | offset = 4; | |
4381 | break; | |
4382 | case EXCP_SMC: | |
4383 | new_mode = ARM_CPU_MODE_MON; | |
4384 | addr = 0x08; | |
4385 | mask = CPSR_A | CPSR_I | CPSR_F; | |
4386 | offset = 0; | |
4387 | break; | |
4388 | default: | |
4389 | cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); | |
4390 | return; /* Never happens. Keep compiler happy. */ | |
4391 | } | |
4392 | ||
4393 | if (new_mode == ARM_CPU_MODE_MON) { | |
4394 | addr += env->cp15.mvbar; | |
4395 | } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { | |
4396 | /* High vectors. When enabled, base address cannot be remapped. */ | |
4397 | addr += 0xffff0000; | |
4398 | } else { | |
4399 | /* ARM v7 architectures provide a vector base address register to remap | |
4400 | * the interrupt vector table. | |
4401 | * This register is only followed in non-monitor mode, and is banked. | |
4402 | * Note: only bits 31:5 are valid. | |
4403 | */ | |
4404 | addr += env->cp15.vbar_el[1]; | |
4405 | } | |
4406 | ||
4407 | if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { | |
4408 | env->cp15.scr_el3 &= ~SCR_NS; | |
4409 | } | |
4410 | ||
4411 | switch_mode (env, new_mode); | |
4412 | /* For exceptions taken to AArch32 we must clear the SS bit in both | |
4413 | * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. | |
4414 | */ | |
4415 | env->uncached_cpsr &= ~PSTATE_SS; | |
4416 | env->spsr = cpsr_read(env); | |
4417 | /* Clear IT bits. */ | |
4418 | env->condexec_bits = 0; | |
4419 | /* Switch to the new mode, and to the correct instruction set. */ | |
4420 | env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; | |
4421 | env->daif |= mask; | |
4422 | /* this is a lie, as the was no c1_sys on V4T/V5, but who cares | |
4423 | * and we should just guard the thumb mode on V4 */ | |
4424 | if (arm_feature(env, ARM_FEATURE_V4T)) { | |
4425 | env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; | |
4426 | } | |
4427 | env->regs[14] = env->regs[15] + offset; | |
4428 | env->regs[15] = addr; | |
4429 | cs->interrupt_request |= CPU_INTERRUPT_EXITTB; | |
4430 | } | |
4431 | ||
4432 | /* Check section/page access permissions. | |
4433 | Returns the page protection flags, or zero if the access is not | |
4434 | permitted. */ | |
4435 | static inline int check_ap(CPUARMState *env, int ap, int domain_prot, | |
4436 | int access_type, int is_user) | |
4437 | { | |
4438 | int prot_ro; | |
4439 | ||
4440 | if (domain_prot == 3) { | |
4441 | return PAGE_READ | PAGE_WRITE; | |
4442 | } | |
4443 | ||
4444 | if (access_type == 1) | |
4445 | prot_ro = 0; | |
4446 | else | |
4447 | prot_ro = PAGE_READ; | |
4448 | ||
4449 | switch (ap) { | |
4450 | case 0: | |
4451 | if (arm_feature(env, ARM_FEATURE_V7)) { | |
4452 | return 0; | |
4453 | } | |
4454 | if (access_type == 1) | |
4455 | return 0; | |
4456 | switch (A32_BANKED_CURRENT_REG_GET(env, sctlr) & (SCTLR_S | SCTLR_R)) { | |
4457 | case SCTLR_S: | |
4458 | return is_user ? 0 : PAGE_READ; | |
4459 | case SCTLR_R: | |
4460 | return PAGE_READ; | |
4461 | default: | |
4462 | return 0; | |
4463 | } | |
4464 | case 1: | |
4465 | return is_user ? 0 : PAGE_READ | PAGE_WRITE; | |
4466 | case 2: | |
4467 | if (is_user) | |
4468 | return prot_ro; | |
4469 | else | |
4470 | return PAGE_READ | PAGE_WRITE; | |
4471 | case 3: | |
4472 | return PAGE_READ | PAGE_WRITE; | |
4473 | case 4: /* Reserved. */ | |
4474 | return 0; | |
4475 | case 5: | |
4476 | return is_user ? 0 : prot_ro; | |
4477 | case 6: | |
4478 | return prot_ro; | |
4479 | case 7: | |
4480 | if (!arm_feature (env, ARM_FEATURE_V6K)) | |
4481 | return 0; | |
4482 | return prot_ro; | |
4483 | default: | |
4484 | abort(); | |
4485 | } | |
4486 | } | |
4487 | ||
4488 | static bool get_level1_table_address(CPUARMState *env, uint32_t *table, | |
4489 | uint32_t address) | |
4490 | { | |
4491 | /* Get the TCR bank based on our security state */ | |
4492 | TCR *tcr = &env->cp15.tcr_el[arm_is_secure(env) ? 3 : 1]; | |
4493 | ||
4494 | /* We only get here if EL1 is running in AArch32. If EL3 is running in | |
4495 | * AArch32 there is a secure and non-secure instance of the translation | |
4496 | * table registers. | |
4497 | */ | |
4498 | if (address & tcr->mask) { | |
4499 | if (tcr->raw_tcr & TTBCR_PD1) { | |
4500 | /* Translation table walk disabled for TTBR1 */ | |
4501 | return false; | |
4502 | } | |
4503 | *table = A32_BANKED_CURRENT_REG_GET(env, ttbr1) & 0xffffc000; | |
4504 | } else { | |
4505 | if (tcr->raw_tcr & TTBCR_PD0) { | |
4506 | /* Translation table walk disabled for TTBR0 */ | |
4507 | return false; | |
4508 | } | |
4509 | *table = A32_BANKED_CURRENT_REG_GET(env, ttbr0) & tcr->base_mask; | |
4510 | } | |
4511 | *table |= (address >> 18) & 0x3ffc; | |
4512 | return true; | |
4513 | } | |
4514 | ||
4515 | static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type, | |
4516 | int is_user, hwaddr *phys_ptr, | |
4517 | int *prot, target_ulong *page_size) | |
4518 | { | |
4519 | CPUState *cs = CPU(arm_env_get_cpu(env)); | |
4520 | int code; | |
4521 | uint32_t table; | |
4522 | uint32_t desc; | |
4523 | int type; | |
4524 | int ap; | |
4525 | int domain = 0; | |
4526 | int domain_prot; | |
4527 | hwaddr phys_addr; | |
4528 | ||
4529 | /* Pagetable walk. */ | |
4530 | /* Lookup l1 descriptor. */ | |
4531 | if (!get_level1_table_address(env, &table, address)) { | |
4532 | /* Section translation fault if page walk is disabled by PD0 or PD1 */ | |
4533 | code = 5; | |
4534 | goto do_fault; | |
4535 | } | |
4536 | desc = ldl_phys(cs->as, table); | |
4537 | type = (desc & 3); | |
4538 | domain = (desc >> 5) & 0x0f; | |
4539 | domain_prot = (A32_BANKED_CURRENT_REG_GET(env, dacr) >> (domain * 2)) & 3; | |
4540 | if (type == 0) { | |
4541 | /* Section translation fault. */ | |
4542 | code = 5; | |
4543 | goto do_fault; | |
4544 | } | |
4545 | if (domain_prot == 0 || domain_prot == 2) { | |
4546 | if (type == 2) | |
4547 | code = 9; /* Section domain fault. */ | |
4548 | else | |
4549 | code = 11; /* Page domain fault. */ | |
4550 | goto do_fault; | |
4551 | } | |
4552 | if (type == 2) { | |
4553 | /* 1Mb section. */ | |
4554 | phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); | |
4555 | ap = (desc >> 10) & 3; | |
4556 | code = 13; | |
4557 | *page_size = 1024 * 1024; | |
4558 | } else { | |
4559 | /* Lookup l2 entry. */ | |
4560 | if (type == 1) { | |
4561 | /* Coarse pagetable. */ | |
4562 | table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); | |
4563 | } else { | |
4564 | /* Fine pagetable. */ | |
4565 | table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); | |
4566 | } | |
4567 | desc = ldl_phys(cs->as, table); | |
4568 | switch (desc & 3) { | |
4569 | case 0: /* Page translation fault. */ | |
4570 | code = 7; | |
4571 | goto do_fault; | |
4572 | case 1: /* 64k page. */ | |
4573 | phys_addr = (desc & 0xffff0000) | (address & 0xffff); | |
4574 | ap = (desc >> (4 + ((address >> 13) & 6))) & 3; | |
4575 | *page_size = 0x10000; | |
4576 | break; | |
4577 | case 2: /* 4k page. */ | |
4578 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); | |
4579 | ap = (desc >> (4 + ((address >> 9) & 6))) & 3; | |
4580 | *page_size = 0x1000; | |
4581 | break; | |
4582 | case 3: /* 1k page. */ | |
4583 | if (type == 1) { | |
4584 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { | |
4585 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); | |
4586 | } else { | |
4587 | /* Page translation fault. */ | |
4588 | code = 7; | |
4589 | goto do_fault; | |
4590 | } | |
4591 | } else { | |
4592 | phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); | |
4593 | } | |
4594 | ap = (desc >> 4) & 3; | |
4595 | *page_size = 0x400; | |
4596 | break; | |
4597 | default: | |
4598 | /* Never happens, but compiler isn't smart enough to tell. */ | |
4599 | abort(); | |
4600 | } | |
4601 | code = 15; | |
4602 | } | |
4603 | *prot = check_ap(env, ap, domain_prot, access_type, is_user); | |
4604 | if (!*prot) { | |
4605 | /* Access permission fault. */ | |
4606 | goto do_fault; | |
4607 | } | |
4608 | *prot |= PAGE_EXEC; | |
4609 | *phys_ptr = phys_addr; | |
4610 | return 0; | |
4611 | do_fault: | |
4612 | return code | (domain << 4); | |
4613 | } | |
4614 | ||
4615 | static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type, | |
4616 | int is_user, hwaddr *phys_ptr, | |
4617 | int *prot, target_ulong *page_size) | |
4618 | { | |
4619 | CPUState *cs = CPU(arm_env_get_cpu(env)); | |
4620 | int code; | |
4621 | uint32_t table; | |
4622 | uint32_t desc; | |
4623 | uint32_t xn; | |
4624 | uint32_t pxn = 0; | |
4625 | int type; | |
4626 | int ap; | |
4627 | int domain = 0; | |
4628 | int domain_prot; | |
4629 | hwaddr phys_addr; | |
4630 | ||
4631 | /* Pagetable walk. */ | |
4632 | /* Lookup l1 descriptor. */ | |
4633 | if (!get_level1_table_address(env, &table, address)) { | |
4634 | /* Section translation fault if page walk is disabled by PD0 or PD1 */ | |
4635 | code = 5; | |
4636 | goto do_fault; | |
4637 | } | |
4638 | desc = ldl_phys(cs->as, table); | |
4639 | type = (desc & 3); | |
4640 | if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) { | |
4641 | /* Section translation fault, or attempt to use the encoding | |
4642 | * which is Reserved on implementations without PXN. | |
4643 | */ | |
4644 | code = 5; | |
4645 | goto do_fault; | |
4646 | } | |
4647 | if ((type == 1) || !(desc & (1 << 18))) { | |
4648 | /* Page or Section. */ | |
4649 | domain = (desc >> 5) & 0x0f; | |
4650 | } | |
4651 | domain_prot = (A32_BANKED_CURRENT_REG_GET(env, dacr) >> (domain * 2)) & 3; | |
4652 | if (domain_prot == 0 || domain_prot == 2) { | |
4653 | if (type != 1) { | |
4654 | code = 9; /* Section domain fault. */ | |
4655 | } else { | |
4656 | code = 11; /* Page domain fault. */ | |
4657 | } | |
4658 | goto do_fault; | |
4659 | } | |
4660 | if (type != 1) { | |
4661 | if (desc & (1 << 18)) { | |
4662 | /* Supersection. */ | |
4663 | phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); | |
4664 | *page_size = 0x1000000; | |
4665 | } else { | |
4666 | /* Section. */ | |
4667 | phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); | |
4668 | *page_size = 0x100000; | |
4669 | } | |
4670 | ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); | |
4671 | xn = desc & (1 << 4); | |
4672 | pxn = desc & 1; | |
4673 | code = 13; | |
4674 | } else { | |
4675 | if (arm_feature(env, ARM_FEATURE_PXN)) { | |
4676 | pxn = (desc >> 2) & 1; | |
4677 | } | |
4678 | /* Lookup l2 entry. */ | |
4679 | table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); | |
4680 | desc = ldl_phys(cs->as, table); | |
4681 | ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); | |
4682 | switch (desc & 3) { | |
4683 | case 0: /* Page translation fault. */ | |
4684 | code = 7; | |
4685 | goto do_fault; | |
4686 | case 1: /* 64k page. */ | |
4687 | phys_addr = (desc & 0xffff0000) | (address & 0xffff); | |
4688 | xn = desc & (1 << 15); | |
4689 | *page_size = 0x10000; | |
4690 | break; | |
4691 | case 2: case 3: /* 4k page. */ | |
4692 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); | |
4693 | xn = desc & 1; | |
4694 | *page_size = 0x1000; | |
4695 | break; | |
4696 | default: | |
4697 | /* Never happens, but compiler isn't smart enough to tell. */ | |
4698 | abort(); | |
4699 | } | |
4700 | code = 15; | |
4701 | } | |
4702 | if (domain_prot == 3) { | |
4703 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
4704 | } else { | |
4705 | if (pxn && !is_user) { | |
4706 | xn = 1; | |
4707 | } | |
4708 | if (xn && access_type == 2) | |
4709 | goto do_fault; | |
4710 | ||
4711 | /* The simplified model uses AP[0] as an access control bit. */ | |
4712 | if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_AFE) | |
4713 | && (ap & 1) == 0) { | |
4714 | /* Access flag fault. */ | |
4715 | code = (code == 15) ? 6 : 3; | |
4716 | goto do_fault; | |
4717 | } | |
4718 | *prot = check_ap(env, ap, domain_prot, access_type, is_user); | |
4719 | if (!*prot) { | |
4720 | /* Access permission fault. */ | |
4721 | goto do_fault; | |
4722 | } | |
4723 | if (!xn) { | |
4724 | *prot |= PAGE_EXEC; | |
4725 | } | |
4726 | } | |
4727 | *phys_ptr = phys_addr; | |
4728 | return 0; | |
4729 | do_fault: | |
4730 | return code | (domain << 4); | |
4731 | } | |
4732 | ||
4733 | /* Fault type for long-descriptor MMU fault reporting; this corresponds | |
4734 | * to bits [5..2] in the STATUS field in long-format DFSR/IFSR. | |
4735 | */ | |
4736 | typedef enum { | |
4737 | translation_fault = 1, | |
4738 | access_fault = 2, | |
4739 | permission_fault = 3, | |
4740 | } MMUFaultType; | |
4741 | ||
4742 | static int get_phys_addr_lpae(CPUARMState *env, target_ulong address, | |
4743 | int access_type, int is_user, | |
4744 | hwaddr *phys_ptr, int *prot, | |
4745 | target_ulong *page_size_ptr) | |
4746 | { | |
4747 | CPUState *cs = CPU(arm_env_get_cpu(env)); | |
4748 | /* Read an LPAE long-descriptor translation table. */ | |
4749 | MMUFaultType fault_type = translation_fault; | |
4750 | uint32_t level = 1; | |
4751 | uint32_t epd; | |
4752 | int32_t tsz; | |
4753 | uint32_t tg; | |
4754 | uint64_t ttbr; | |
4755 | int ttbr_select; | |
4756 | hwaddr descaddr, descmask; | |
4757 | uint32_t tableattrs; | |
4758 | target_ulong page_size; | |
4759 | uint32_t attrs; | |
4760 | int32_t granule_sz = 9; | |
4761 | int32_t va_size = 32; | |
4762 | int32_t tbi = 0; | |
4763 | TCR *tcr = &env->cp15.tcr_el[arm_is_secure(env) ? 3 : 1]; | |
4764 | ||
4765 | if (arm_el_is_aa64(env, 1)) { | |
4766 | va_size = 64; | |
4767 | if (extract64(address, 55, 1)) | |
4768 | tbi = extract64(tcr->raw_tcr, 38, 1); | |
4769 | else | |
4770 | tbi = extract64(tcr->raw_tcr, 37, 1); | |
4771 | tbi *= 8; | |
4772 | } | |
4773 | ||
4774 | /* Determine whether this address is in the region controlled by | |
4775 | * TTBR0 or TTBR1 (or if it is in neither region and should fault). | |
4776 | * This is a Non-secure PL0/1 stage 1 translation, so controlled by | |
4777 | * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32: | |
4778 | */ | |
4779 | uint32_t t0sz = extract32(tcr->raw_tcr, 0, 6); | |
4780 | if (arm_el_is_aa64(env, 1)) { | |
4781 | t0sz = MIN(t0sz, 39); | |
4782 | t0sz = MAX(t0sz, 16); | |
4783 | } | |
4784 | uint32_t t1sz = extract32(tcr->raw_tcr, 16, 6); | |
4785 | if (arm_el_is_aa64(env, 1)) { | |
4786 | t1sz = MIN(t1sz, 39); | |
4787 | t1sz = MAX(t1sz, 16); | |
4788 | } | |
4789 | if (t0sz && !extract64(address, va_size - t0sz, t0sz - tbi)) { | |
4790 | /* there is a ttbr0 region and we are in it (high bits all zero) */ | |
4791 | ttbr_select = 0; | |
4792 | } else if (t1sz && !extract64(~address, va_size - t1sz, t1sz - tbi)) { | |
4793 | /* there is a ttbr1 region and we are in it (high bits all one) */ | |
4794 | ttbr_select = 1; | |
4795 | } else if (!t0sz) { | |
4796 | /* ttbr0 region is "everything not in the ttbr1 region" */ | |
4797 | ttbr_select = 0; | |
4798 | } else if (!t1sz) { | |
4799 | /* ttbr1 region is "everything not in the ttbr0 region" */ | |
4800 | ttbr_select = 1; | |
4801 | } else { | |
4802 | /* in the gap between the two regions, this is a Translation fault */ | |
4803 | fault_type = translation_fault; | |
4804 | goto do_fault; | |
4805 | } | |
4806 | ||
4807 | /* Note that QEMU ignores shareability and cacheability attributes, | |
4808 | * so we don't need to do anything with the SH, ORGN, IRGN fields | |
4809 | * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the | |
4810 | * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently | |
4811 | * implement any ASID-like capability so we can ignore it (instead | |
4812 | * we will always flush the TLB any time the ASID is changed). | |
4813 | */ | |
4814 | if (ttbr_select == 0) { | |
4815 | ttbr = A32_BANKED_CURRENT_REG_GET(env, ttbr0); | |
4816 | epd = extract32(tcr->raw_tcr, 7, 1); | |
4817 | tsz = t0sz; | |
4818 | ||
4819 | tg = extract32(tcr->raw_tcr, 14, 2); | |
4820 | if (tg == 1) { /* 64KB pages */ | |
4821 | granule_sz = 13; | |
4822 | } | |
4823 | if (tg == 2) { /* 16KB pages */ | |
4824 | granule_sz = 11; | |
4825 | } | |
4826 | } else { | |
4827 | ttbr = A32_BANKED_CURRENT_REG_GET(env, ttbr1); | |
4828 | epd = extract32(tcr->raw_tcr, 23, 1); | |
4829 | tsz = t1sz; | |
4830 | ||
4831 | tg = extract32(tcr->raw_tcr, 30, 2); | |
4832 | if (tg == 3) { /* 64KB pages */ | |
4833 | granule_sz = 13; | |
4834 | } | |
4835 | if (tg == 1) { /* 16KB pages */ | |
4836 | granule_sz = 11; | |
4837 | } | |
4838 | } | |
4839 | ||
4840 | if (epd) { | |
4841 | /* Translation table walk disabled => Translation fault on TLB miss */ | |
4842 | goto do_fault; | |
4843 | } | |
4844 | ||
4845 | /* The starting level depends on the virtual address size (which can be | |
4846 | * up to 48 bits) and the translation granule size. It indicates the number | |
4847 | * of strides (granule_sz bits at a time) needed to consume the bits | |
4848 | * of the input address. In the pseudocode this is: | |
4849 | * level = 4 - RoundUp((inputsize - grainsize) / stride) | |
4850 | * where their 'inputsize' is our 'va_size - tsz', 'grainsize' is | |
4851 | * our 'granule_sz + 3' and 'stride' is our 'granule_sz'. | |
4852 | * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying: | |
4853 | * = 4 - (va_size - tsz - granule_sz - 3 + granule_sz - 1) / granule_sz | |
4854 | * = 4 - (va_size - tsz - 4) / granule_sz; | |
4855 | */ | |
4856 | level = 4 - (va_size - tsz - 4) / granule_sz; | |
4857 | ||
4858 | /* Clear the vaddr bits which aren't part of the within-region address, | |
4859 | * so that we don't have to special case things when calculating the | |
4860 | * first descriptor address. | |
4861 | */ | |
4862 | if (tsz) { | |
4863 | address &= (1ULL << (va_size - tsz)) - 1; | |
4864 | } | |
4865 | ||
4866 | descmask = (1ULL << (granule_sz + 3)) - 1; | |
4867 | ||
4868 | /* Now we can extract the actual base address from the TTBR */ | |
4869 | descaddr = extract64(ttbr, 0, 48); | |
4870 | descaddr &= ~((1ULL << (va_size - tsz - (granule_sz * (4 - level)))) - 1); | |
4871 | ||
4872 | tableattrs = 0; | |
4873 | for (;;) { | |
4874 | uint64_t descriptor; | |
4875 | ||
4876 | descaddr |= (address >> (granule_sz * (4 - level))) & descmask; | |
4877 | descaddr &= ~7ULL; | |
4878 | descriptor = ldq_phys(cs->as, descaddr); | |
4879 | if (!(descriptor & 1) || | |
4880 | (!(descriptor & 2) && (level == 3))) { | |
4881 | /* Invalid, or the Reserved level 3 encoding */ | |
4882 | goto do_fault; | |
4883 | } | |
4884 | descaddr = descriptor & 0xfffffff000ULL; | |
4885 | ||
4886 | if ((descriptor & 2) && (level < 3)) { | |
4887 | /* Table entry. The top five bits are attributes which may | |
4888 | * propagate down through lower levels of the table (and | |
4889 | * which are all arranged so that 0 means "no effect", so | |
4890 | * we can gather them up by ORing in the bits at each level). | |
4891 | */ | |
4892 | tableattrs |= extract64(descriptor, 59, 5); | |
4893 | level++; | |
4894 | continue; | |
4895 | } | |
4896 | /* Block entry at level 1 or 2, or page entry at level 3. | |
4897 | * These are basically the same thing, although the number | |
4898 | * of bits we pull in from the vaddr varies. | |
4899 | */ | |
4900 | page_size = (1ULL << ((granule_sz * (4 - level)) + 3)); | |
4901 | descaddr |= (address & (page_size - 1)); | |
4902 | /* Extract attributes from the descriptor and merge with table attrs */ | |
4903 | attrs = extract64(descriptor, 2, 10) | |
4904 | | (extract64(descriptor, 52, 12) << 10); | |
4905 | attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */ | |
4906 | attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */ | |
4907 | /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1 | |
4908 | * means "force PL1 access only", which means forcing AP[1] to 0. | |
4909 | */ | |
4910 | if (extract32(tableattrs, 2, 1)) { | |
4911 | attrs &= ~(1 << 4); | |
4912 | } | |
4913 | /* Since we're always in the Non-secure state, NSTable is ignored. */ | |
4914 | break; | |
4915 | } | |
4916 | /* Here descaddr is the final physical address, and attributes | |
4917 | * are all in attrs. | |
4918 | */ | |
4919 | fault_type = access_fault; | |
4920 | if ((attrs & (1 << 8)) == 0) { | |
4921 | /* Access flag */ | |
4922 | goto do_fault; | |
4923 | } | |
4924 | fault_type = permission_fault; | |
4925 | if (is_user && !(attrs & (1 << 4))) { | |
4926 | /* Unprivileged access not enabled */ | |
4927 | goto do_fault; | |
4928 | } | |
4929 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
4930 | if ((arm_feature(env, ARM_FEATURE_V8) && is_user && (attrs & (1 << 12))) || | |
4931 | (!arm_feature(env, ARM_FEATURE_V8) && (attrs & (1 << 12))) || | |
4932 | (!is_user && (attrs & (1 << 11)))) { | |
4933 | /* XN/UXN or PXN. Since we only implement EL0/EL1 we unconditionally | |
4934 | * treat XN/UXN as UXN for v8. | |
4935 | */ | |
4936 | if (access_type == 2) { | |
4937 | goto do_fault; | |
4938 | } | |
4939 | *prot &= ~PAGE_EXEC; | |
4940 | } | |
4941 | if (attrs & (1 << 5)) { | |
4942 | /* Write access forbidden */ | |
4943 | if (access_type == 1) { | |
4944 | goto do_fault; | |
4945 | } | |
4946 | *prot &= ~PAGE_WRITE; | |
4947 | } | |
4948 | ||
4949 | *phys_ptr = descaddr; | |
4950 | *page_size_ptr = page_size; | |
4951 | return 0; | |
4952 | ||
4953 | do_fault: | |
4954 | /* Long-descriptor format IFSR/DFSR value */ | |
4955 | return (1 << 9) | (fault_type << 2) | level; | |
4956 | } | |
4957 | ||
4958 | static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, | |
4959 | int access_type, int is_user, | |
4960 | hwaddr *phys_ptr, int *prot) | |
4961 | { | |
4962 | int n; | |
4963 | uint32_t mask; | |
4964 | uint32_t base; | |
4965 | ||
4966 | *phys_ptr = address; | |
4967 | for (n = 7; n >= 0; n--) { | |
4968 | base = env->cp15.c6_region[n]; | |
4969 | if ((base & 1) == 0) | |
4970 | continue; | |
4971 | mask = 1 << ((base >> 1) & 0x1f); | |
4972 | /* Keep this shift separate from the above to avoid an | |
4973 | (undefined) << 32. */ | |
4974 | mask = (mask << 1) - 1; | |
4975 | if (((base ^ address) & ~mask) == 0) | |
4976 | break; | |
4977 | } | |
4978 | if (n < 0) | |
4979 | return 2; | |
4980 | ||
4981 | if (access_type == 2) { | |
4982 | mask = env->cp15.pmsav5_insn_ap; | |
4983 | } else { | |
4984 | mask = env->cp15.pmsav5_data_ap; | |
4985 | } | |
4986 | mask = (mask >> (n * 4)) & 0xf; | |
4987 | switch (mask) { | |
4988 | case 0: | |
4989 | return 1; | |
4990 | case 1: | |
4991 | if (is_user) | |
4992 | return 1; | |
4993 | *prot = PAGE_READ | PAGE_WRITE; | |
4994 | break; | |
4995 | case 2: | |
4996 | *prot = PAGE_READ; | |
4997 | if (!is_user) | |
4998 | *prot |= PAGE_WRITE; | |
4999 | break; | |
5000 | case 3: | |
5001 | *prot = PAGE_READ | PAGE_WRITE; | |
5002 | break; | |
5003 | case 5: | |
5004 | if (is_user) | |
5005 | return 1; | |
5006 | *prot = PAGE_READ; | |
5007 | break; | |
5008 | case 6: | |
5009 | *prot = PAGE_READ; | |
5010 | break; | |
5011 | default: | |
5012 | /* Bad permission. */ | |
5013 | return 1; | |
5014 | } | |
5015 | *prot |= PAGE_EXEC; | |
5016 | return 0; | |
5017 | } | |
5018 | ||
5019 | /* get_phys_addr - get the physical address for this virtual address | |
5020 | * | |
5021 | * Find the physical address corresponding to the given virtual address, | |
5022 | * by doing a translation table walk on MMU based systems or using the | |
5023 | * MPU state on MPU based systems. | |
5024 | * | |
5025 | * Returns 0 if the translation was successful. Otherwise, phys_ptr, | |
5026 | * prot and page_size are not filled in, and the return value provides | |
5027 | * information on why the translation aborted, in the format of a | |
5028 | * DFSR/IFSR fault register, with the following caveats: | |
5029 | * * we honour the short vs long DFSR format differences. | |
5030 | * * the WnR bit is never set (the caller must do this). | |
5031 | * * for MPU based systems we don't bother to return a full FSR format | |
5032 | * value. | |
5033 | * | |
5034 | * @env: CPUARMState | |
5035 | * @address: virtual address to get physical address for | |
5036 | * @access_type: 0 for read, 1 for write, 2 for execute | |
5037 | * @is_user: 0 for privileged access, 1 for user | |
5038 | * @phys_ptr: set to the physical address corresponding to the virtual address | |
5039 | * @prot: set to the permissions for the page containing phys_ptr | |
5040 | * @page_size: set to the size of the page containing phys_ptr | |
5041 | */ | |
5042 | static inline int get_phys_addr(CPUARMState *env, target_ulong address, | |
5043 | int access_type, int is_user, | |
5044 | hwaddr *phys_ptr, int *prot, | |
5045 | target_ulong *page_size) | |
5046 | { | |
5047 | /* This is not entirely correct as get_phys_addr() can also be called | |
5048 | * from ats_write() for an address translation of a specific regime. | |
5049 | */ | |
5050 | uint32_t sctlr = A32_BANKED_CURRENT_REG_GET(env, sctlr); | |
5051 | ||
5052 | /* Fast Context Switch Extension. */ | |
5053 | if (address < 0x02000000) | |
5054 | address += env->cp15.c13_fcse; | |
5055 | ||
5056 | if ((sctlr & SCTLR_M) == 0) { | |
5057 | /* MMU/MPU disabled. */ | |
5058 | *phys_ptr = address; | |
5059 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; | |
5060 | *page_size = TARGET_PAGE_SIZE; | |
5061 | return 0; | |
5062 | } else if (arm_feature(env, ARM_FEATURE_MPU)) { | |
5063 | *page_size = TARGET_PAGE_SIZE; | |
5064 | return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr, | |
5065 | prot); | |
5066 | } else if (extended_addresses_enabled(env)) { | |
5067 | return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr, | |
5068 | prot, page_size); | |
5069 | } else if (sctlr & SCTLR_XP) { | |
5070 | return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr, | |
5071 | prot, page_size); | |
5072 | } else { | |
5073 | return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr, | |
5074 | prot, page_size); | |
5075 | } | |
5076 | } | |
5077 | ||
5078 | int arm_cpu_handle_mmu_fault(CPUState *cs, vaddr address, | |
5079 | int access_type, int mmu_idx) | |
5080 | { | |
5081 | ARMCPU *cpu = ARM_CPU(cs); | |
5082 | CPUARMState *env = &cpu->env; | |
5083 | hwaddr phys_addr; | |
5084 | target_ulong page_size; | |
5085 | int prot; | |
5086 | int ret, is_user; | |
5087 | uint32_t syn; | |
5088 | bool same_el = (arm_current_el(env) != 0); | |
5089 | ||
5090 | is_user = mmu_idx == MMU_USER_IDX; | |
5091 | ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot, | |
5092 | &page_size); | |
5093 | if (ret == 0) { | |
5094 | /* Map a single [sub]page. */ | |
5095 | phys_addr &= TARGET_PAGE_MASK; | |
5096 | address &= TARGET_PAGE_MASK; | |
5097 | tlb_set_page(cs, address, phys_addr, prot, mmu_idx, page_size); | |
5098 | return 0; | |
5099 | } | |
5100 | ||
5101 | /* AArch64 syndrome does not have an LPAE bit */ | |
5102 | syn = ret & ~(1 << 9); | |
5103 | ||
5104 | /* For insn and data aborts we assume there is no instruction syndrome | |
5105 | * information; this is always true for exceptions reported to EL1. | |
5106 | */ | |
5107 | if (access_type == 2) { | |
5108 | syn = syn_insn_abort(same_el, 0, 0, syn); | |
5109 | cs->exception_index = EXCP_PREFETCH_ABORT; | |
5110 | } else { | |
5111 | syn = syn_data_abort(same_el, 0, 0, 0, access_type == 1, syn); | |
5112 | if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6)) { | |
5113 | ret |= (1 << 11); | |
5114 | } | |
5115 | cs->exception_index = EXCP_DATA_ABORT; | |
5116 | } | |
5117 | ||
5118 | env->exception.syndrome = syn; | |
5119 | env->exception.vaddress = address; | |
5120 | env->exception.fsr = ret; | |
5121 | return 1; | |
5122 | } | |
5123 | ||
5124 | hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) | |
5125 | { | |
5126 | ARMCPU *cpu = ARM_CPU(cs); | |
5127 | hwaddr phys_addr; | |
5128 | target_ulong page_size; | |
5129 | int prot; | |
5130 | int ret; | |
5131 | ||
5132 | ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size); | |
5133 | ||
5134 | if (ret != 0) { | |
5135 | return -1; | |
5136 | } | |
5137 | ||
5138 | return phys_addr; | |
5139 | } | |
5140 | ||
5141 | void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val) | |
5142 | { | |
5143 | if ((env->uncached_cpsr & CPSR_M) == mode) { | |
5144 | env->regs[13] = val; | |
5145 | } else { | |
5146 | env->banked_r13[bank_number(mode)] = val; | |
5147 | } | |
5148 | } | |
5149 | ||
5150 | uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode) | |
5151 | { | |
5152 | if ((env->uncached_cpsr & CPSR_M) == mode) { | |
5153 | return env->regs[13]; | |
5154 | } else { | |
5155 | return env->banked_r13[bank_number(mode)]; | |
5156 | } | |
5157 | } | |
5158 | ||
5159 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) | |
5160 | { | |
5161 | ARMCPU *cpu = arm_env_get_cpu(env); | |
5162 | ||
5163 | switch (reg) { | |
5164 | case 0: /* APSR */ | |
5165 | return xpsr_read(env) & 0xf8000000; | |
5166 | case 1: /* IAPSR */ | |
5167 | return xpsr_read(env) & 0xf80001ff; | |
5168 | case 2: /* EAPSR */ | |
5169 | return xpsr_read(env) & 0xff00fc00; | |
5170 | case 3: /* xPSR */ | |
5171 | return xpsr_read(env) & 0xff00fdff; | |
5172 | case 5: /* IPSR */ | |
5173 | return xpsr_read(env) & 0x000001ff; | |
5174 | case 6: /* EPSR */ | |
5175 | return xpsr_read(env) & 0x0700fc00; | |
5176 | case 7: /* IEPSR */ | |
5177 | return xpsr_read(env) & 0x0700edff; | |
5178 | case 8: /* MSP */ | |
5179 | return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13]; | |
5180 | case 9: /* PSP */ | |
5181 | return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp; | |
5182 | case 16: /* PRIMASK */ | |
5183 | return (env->daif & PSTATE_I) != 0; | |
5184 | case 17: /* BASEPRI */ | |
5185 | case 18: /* BASEPRI_MAX */ | |
5186 | return env->v7m.basepri; | |
5187 | case 19: /* FAULTMASK */ | |
5188 | return (env->daif & PSTATE_F) != 0; | |
5189 | case 20: /* CONTROL */ | |
5190 | return env->v7m.control; | |
5191 | default: | |
5192 | /* ??? For debugging only. */ | |
5193 | cpu_abort(CPU(cpu), "Unimplemented system register read (%d)\n", reg); | |
5194 | return 0; | |
5195 | } | |
5196 | } | |
5197 | ||
5198 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) | |
5199 | { | |
5200 | ARMCPU *cpu = arm_env_get_cpu(env); | |
5201 | ||
5202 | switch (reg) { | |
5203 | case 0: /* APSR */ | |
5204 | xpsr_write(env, val, 0xf8000000); | |
5205 | break; | |
5206 | case 1: /* IAPSR */ | |
5207 | xpsr_write(env, val, 0xf8000000); | |
5208 | break; | |
5209 | case 2: /* EAPSR */ | |
5210 | xpsr_write(env, val, 0xfe00fc00); | |
5211 | break; | |
5212 | case 3: /* xPSR */ | |
5213 | xpsr_write(env, val, 0xfe00fc00); | |
5214 | break; | |
5215 | case 5: /* IPSR */ | |
5216 | /* IPSR bits are readonly. */ | |
5217 | break; | |
5218 | case 6: /* EPSR */ | |
5219 | xpsr_write(env, val, 0x0600fc00); | |
5220 | break; | |
5221 | case 7: /* IEPSR */ | |
5222 | xpsr_write(env, val, 0x0600fc00); | |
5223 | break; | |
5224 | case 8: /* MSP */ | |
5225 | if (env->v7m.current_sp) | |
5226 | env->v7m.other_sp = val; | |
5227 | else | |
5228 | env->regs[13] = val; | |
5229 | break; | |
5230 | case 9: /* PSP */ | |
5231 | if (env->v7m.current_sp) | |
5232 | env->regs[13] = val; | |
5233 | else | |
5234 | env->v7m.other_sp = val; | |
5235 | break; | |
5236 | case 16: /* PRIMASK */ | |
5237 | if (val & 1) { | |
5238 | env->daif |= PSTATE_I; | |
5239 | } else { | |
5240 | env->daif &= ~PSTATE_I; | |
5241 | } | |
5242 | break; | |
5243 | case 17: /* BASEPRI */ | |
5244 | env->v7m.basepri = val & 0xff; | |
5245 | break; | |
5246 | case 18: /* BASEPRI_MAX */ | |
5247 | val &= 0xff; | |
5248 | if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0)) | |
5249 | env->v7m.basepri = val; | |
5250 | break; | |
5251 | case 19: /* FAULTMASK */ | |
5252 | if (val & 1) { | |
5253 | env->daif |= PSTATE_F; | |
5254 | } else { | |
5255 | env->daif &= ~PSTATE_F; | |
5256 | } | |
5257 | break; | |
5258 | case 20: /* CONTROL */ | |
5259 | env->v7m.control = val & 3; | |
5260 | switch_v7m_sp(env, (val & 2) != 0); | |
5261 | break; | |
5262 | default: | |
5263 | /* ??? For debugging only. */ | |
5264 | cpu_abort(CPU(cpu), "Unimplemented system register write (%d)\n", reg); | |
5265 | return; | |
5266 | } | |
5267 | } | |
5268 | ||
5269 | #endif | |
5270 | ||
5271 | void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in) | |
5272 | { | |
5273 | /* Implement DC ZVA, which zeroes a fixed-length block of memory. | |
5274 | * Note that we do not implement the (architecturally mandated) | |
5275 | * alignment fault for attempts to use this on Device memory | |
5276 | * (which matches the usual QEMU behaviour of not implementing either | |
5277 | * alignment faults or any memory attribute handling). | |
5278 | */ | |
5279 | ||
5280 | ARMCPU *cpu = arm_env_get_cpu(env); | |
5281 | uint64_t blocklen = 4 << cpu->dcz_blocksize; | |
5282 | uint64_t vaddr = vaddr_in & ~(blocklen - 1); | |
5283 | ||
5284 | #ifndef CONFIG_USER_ONLY | |
5285 | { | |
5286 | /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than | |
5287 | * the block size so we might have to do more than one TLB lookup. | |
5288 | * We know that in fact for any v8 CPU the page size is at least 4K | |
5289 | * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only | |
5290 | * 1K as an artefact of legacy v5 subpage support being present in the | |
5291 | * same QEMU executable. | |
5292 | */ | |
5293 | int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE); | |
5294 | void *hostaddr[maxidx]; | |
5295 | int try, i; | |
5296 | ||
5297 | for (try = 0; try < 2; try++) { | |
5298 | ||
5299 | for (i = 0; i < maxidx; i++) { | |
5300 | hostaddr[i] = tlb_vaddr_to_host(env, | |
5301 | vaddr + TARGET_PAGE_SIZE * i, | |
5302 | 1, cpu_mmu_index(env)); | |
5303 | if (!hostaddr[i]) { | |
5304 | break; | |
5305 | } | |
5306 | } | |
5307 | if (i == maxidx) { | |
5308 | /* If it's all in the TLB it's fair game for just writing to; | |
5309 | * we know we don't need to update dirty status, etc. | |
5310 | */ | |
5311 | for (i = 0; i < maxidx - 1; i++) { | |
5312 | memset(hostaddr[i], 0, TARGET_PAGE_SIZE); | |
5313 | } | |
5314 | memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE)); | |
5315 | return; | |
5316 | } | |
5317 | /* OK, try a store and see if we can populate the tlb. This | |
5318 | * might cause an exception if the memory isn't writable, | |
5319 | * in which case we will longjmp out of here. We must for | |
5320 | * this purpose use the actual register value passed to us | |
5321 | * so that we get the fault address right. | |
5322 | */ | |
5323 | helper_ret_stb_mmu(env, vaddr_in, 0, cpu_mmu_index(env), GETRA()); | |
5324 | /* Now we can populate the other TLB entries, if any */ | |
5325 | for (i = 0; i < maxidx; i++) { | |
5326 | uint64_t va = vaddr + TARGET_PAGE_SIZE * i; | |
5327 | if (va != (vaddr_in & TARGET_PAGE_MASK)) { | |
5328 | helper_ret_stb_mmu(env, va, 0, cpu_mmu_index(env), GETRA()); | |
5329 | } | |
5330 | } | |
5331 | } | |
5332 | ||
5333 | /* Slow path (probably attempt to do this to an I/O device or | |
5334 | * similar, or clearing of a block of code we have translations | |
5335 | * cached for). Just do a series of byte writes as the architecture | |
5336 | * demands. It's not worth trying to use a cpu_physical_memory_map(), | |
5337 | * memset(), unmap() sequence here because: | |
5338 | * + we'd need to account for the blocksize being larger than a page | |
5339 | * + the direct-RAM access case is almost always going to be dealt | |
5340 | * with in the fastpath code above, so there's no speed benefit | |
5341 | * + we would have to deal with the map returning NULL because the | |
5342 | * bounce buffer was in use | |
5343 | */ | |
5344 | for (i = 0; i < blocklen; i++) { | |
5345 | helper_ret_stb_mmu(env, vaddr + i, 0, cpu_mmu_index(env), GETRA()); | |
5346 | } | |
5347 | } | |
5348 | #else | |
5349 | memset(g2h(vaddr), 0, blocklen); | |
5350 | #endif | |
5351 | } | |
5352 | ||
5353 | /* Note that signed overflow is undefined in C. The following routines are | |
5354 | careful to use unsigned types where modulo arithmetic is required. | |
5355 | Failure to do so _will_ break on newer gcc. */ | |
5356 | ||
5357 | /* Signed saturating arithmetic. */ | |
5358 | ||
5359 | /* Perform 16-bit signed saturating addition. */ | |
5360 | static inline uint16_t add16_sat(uint16_t a, uint16_t b) | |
5361 | { | |
5362 | uint16_t res; | |
5363 | ||
5364 | res = a + b; | |
5365 | if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { | |
5366 | if (a & 0x8000) | |
5367 | res = 0x8000; | |
5368 | else | |
5369 | res = 0x7fff; | |
5370 | } | |
5371 | return res; | |
5372 | } | |
5373 | ||
5374 | /* Perform 8-bit signed saturating addition. */ | |
5375 | static inline uint8_t add8_sat(uint8_t a, uint8_t b) | |
5376 | { | |
5377 | uint8_t res; | |
5378 | ||
5379 | res = a + b; | |
5380 | if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { | |
5381 | if (a & 0x80) | |
5382 | res = 0x80; | |
5383 | else | |
5384 | res = 0x7f; | |
5385 | } | |
5386 | return res; | |
5387 | } | |
5388 | ||
5389 | /* Perform 16-bit signed saturating subtraction. */ | |
5390 | static inline uint16_t sub16_sat(uint16_t a, uint16_t b) | |
5391 | { | |
5392 | uint16_t res; | |
5393 | ||
5394 | res = a - b; | |
5395 | if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { | |
5396 | if (a & 0x8000) | |
5397 | res = 0x8000; | |
5398 | else | |
5399 | res = 0x7fff; | |
5400 | } | |
5401 | return res; | |
5402 | } | |
5403 | ||
5404 | /* Perform 8-bit signed saturating subtraction. */ | |
5405 | static inline uint8_t sub8_sat(uint8_t a, uint8_t b) | |
5406 | { | |
5407 | uint8_t res; | |
5408 | ||
5409 | res = a - b; | |
5410 | if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { | |
5411 | if (a & 0x80) | |
5412 | res = 0x80; | |
5413 | else | |
5414 | res = 0x7f; | |
5415 | } | |
5416 | return res; | |
5417 | } | |
5418 | ||
5419 | #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); | |
5420 | #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); | |
5421 | #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); | |
5422 | #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); | |
5423 | #define PFX q | |
5424 | ||
5425 | #include "op_addsub.h" | |
5426 | ||
5427 | /* Unsigned saturating arithmetic. */ | |
5428 | static inline uint16_t add16_usat(uint16_t a, uint16_t b) | |
5429 | { | |
5430 | uint16_t res; | |
5431 | res = a + b; | |
5432 | if (res < a) | |
5433 | res = 0xffff; | |
5434 | return res; | |
5435 | } | |
5436 | ||
5437 | static inline uint16_t sub16_usat(uint16_t a, uint16_t b) | |
5438 | { | |
5439 | if (a > b) | |
5440 | return a - b; | |
5441 | else | |
5442 | return 0; | |
5443 | } | |
5444 | ||
5445 | static inline uint8_t add8_usat(uint8_t a, uint8_t b) | |
5446 | { | |
5447 | uint8_t res; | |
5448 | res = a + b; | |
5449 | if (res < a) | |
5450 | res = 0xff; | |
5451 | return res; | |
5452 | } | |
5453 | ||
5454 | static inline uint8_t sub8_usat(uint8_t a, uint8_t b) | |
5455 | { | |
5456 | if (a > b) | |
5457 | return a - b; | |
5458 | else | |
5459 | return 0; | |
5460 | } | |
5461 | ||
5462 | #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); | |
5463 | #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); | |
5464 | #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); | |
5465 | #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); | |
5466 | #define PFX uq | |
5467 | ||
5468 | #include "op_addsub.h" | |
5469 | ||
5470 | /* Signed modulo arithmetic. */ | |
5471 | #define SARITH16(a, b, n, op) do { \ | |
5472 | int32_t sum; \ | |
5473 | sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ | |
5474 | RESULT(sum, n, 16); \ | |
5475 | if (sum >= 0) \ | |
5476 | ge |= 3 << (n * 2); \ | |
5477 | } while(0) | |
5478 | ||
5479 | #define SARITH8(a, b, n, op) do { \ | |
5480 | int32_t sum; \ | |
5481 | sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ | |
5482 | RESULT(sum, n, 8); \ | |
5483 | if (sum >= 0) \ | |
5484 | ge |= 1 << n; \ | |
5485 | } while(0) | |
5486 | ||
5487 | ||
5488 | #define ADD16(a, b, n) SARITH16(a, b, n, +) | |
5489 | #define SUB16(a, b, n) SARITH16(a, b, n, -) | |
5490 | #define ADD8(a, b, n) SARITH8(a, b, n, +) | |
5491 | #define SUB8(a, b, n) SARITH8(a, b, n, -) | |
5492 | #define PFX s | |
5493 | #define ARITH_GE | |
5494 | ||
5495 | #include "op_addsub.h" | |
5496 | ||
5497 | /* Unsigned modulo arithmetic. */ | |
5498 | #define ADD16(a, b, n) do { \ | |
5499 | uint32_t sum; \ | |
5500 | sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ | |
5501 | RESULT(sum, n, 16); \ | |
5502 | if ((sum >> 16) == 1) \ | |
5503 | ge |= 3 << (n * 2); \ | |
5504 | } while(0) | |
5505 | ||
5506 | #define ADD8(a, b, n) do { \ | |
5507 | uint32_t sum; \ | |
5508 | sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ | |
5509 | RESULT(sum, n, 8); \ | |
5510 | if ((sum >> 8) == 1) \ | |
5511 | ge |= 1 << n; \ | |
5512 | } while(0) | |
5513 | ||
5514 | #define SUB16(a, b, n) do { \ | |
5515 | uint32_t sum; \ | |
5516 | sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ | |
5517 | RESULT(sum, n, 16); \ | |
5518 | if ((sum >> 16) == 0) \ | |
5519 | ge |= 3 << (n * 2); \ | |
5520 | } while(0) | |
5521 | ||
5522 | #define SUB8(a, b, n) do { \ | |
5523 | uint32_t sum; \ | |
5524 | sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ | |
5525 | RESULT(sum, n, 8); \ | |
5526 | if ((sum >> 8) == 0) \ | |
5527 | ge |= 1 << n; \ | |
5528 | } while(0) | |
5529 | ||
5530 | #define PFX u | |
5531 | #define ARITH_GE | |
5532 | ||
5533 | #include "op_addsub.h" | |
5534 | ||
5535 | /* Halved signed arithmetic. */ | |
5536 | #define ADD16(a, b, n) \ | |
5537 | RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) | |
5538 | #define SUB16(a, b, n) \ | |
5539 | RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) | |
5540 | #define ADD8(a, b, n) \ | |
5541 | RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) | |
5542 | #define SUB8(a, b, n) \ | |
5543 | RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) | |
5544 | #define PFX sh | |
5545 | ||
5546 | #include "op_addsub.h" | |
5547 | ||
5548 | /* Halved unsigned arithmetic. */ | |
5549 | #define ADD16(a, b, n) \ | |
5550 | RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) | |
5551 | #define SUB16(a, b, n) \ | |
5552 | RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) | |
5553 | #define ADD8(a, b, n) \ | |
5554 | RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) | |
5555 | #define SUB8(a, b, n) \ | |
5556 | RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) | |
5557 | #define PFX uh | |
5558 | ||
5559 | #include "op_addsub.h" | |
5560 | ||
5561 | static inline uint8_t do_usad(uint8_t a, uint8_t b) | |
5562 | { | |
5563 | if (a > b) | |
5564 | return a - b; | |
5565 | else | |
5566 | return b - a; | |
5567 | } | |
5568 | ||
5569 | /* Unsigned sum of absolute byte differences. */ | |
5570 | uint32_t HELPER(usad8)(uint32_t a, uint32_t b) | |
5571 | { | |
5572 | uint32_t sum; | |
5573 | sum = do_usad(a, b); | |
5574 | sum += do_usad(a >> 8, b >> 8); | |
5575 | sum += do_usad(a >> 16, b >>16); | |
5576 | sum += do_usad(a >> 24, b >> 24); | |
5577 | return sum; | |
5578 | } | |
5579 | ||
5580 | /* For ARMv6 SEL instruction. */ | |
5581 | uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) | |
5582 | { | |
5583 | uint32_t mask; | |
5584 | ||
5585 | mask = 0; | |
5586 | if (flags & 1) | |
5587 | mask |= 0xff; | |
5588 | if (flags & 2) | |
5589 | mask |= 0xff00; | |
5590 | if (flags & 4) | |
5591 | mask |= 0xff0000; | |
5592 | if (flags & 8) | |
5593 | mask |= 0xff000000; | |
5594 | return (a & mask) | (b & ~mask); | |
5595 | } | |
5596 | ||
5597 | /* VFP support. We follow the convention used for VFP instructions: | |
5598 | Single precision routines have a "s" suffix, double precision a | |
5599 | "d" suffix. */ | |
5600 | ||
5601 | /* Convert host exception flags to vfp form. */ | |
5602 | static inline int vfp_exceptbits_from_host(int host_bits) | |
5603 | { | |
5604 | int target_bits = 0; | |
5605 | ||
5606 | if (host_bits & float_flag_invalid) | |
5607 | target_bits |= 1; | |
5608 | if (host_bits & float_flag_divbyzero) | |
5609 | target_bits |= 2; | |
5610 | if (host_bits & float_flag_overflow) | |
5611 | target_bits |= 4; | |
5612 | if (host_bits & (float_flag_underflow | float_flag_output_denormal)) | |
5613 | target_bits |= 8; | |
5614 | if (host_bits & float_flag_inexact) | |
5615 | target_bits |= 0x10; | |
5616 | if (host_bits & float_flag_input_denormal) | |
5617 | target_bits |= 0x80; | |
5618 | return target_bits; | |
5619 | } | |
5620 | ||
5621 | uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env) | |
5622 | { | |
5623 | int i; | |
5624 | uint32_t fpscr; | |
5625 | ||
5626 | fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff) | |
5627 | | (env->vfp.vec_len << 16) | |
5628 | | (env->vfp.vec_stride << 20); | |
5629 | i = get_float_exception_flags(&env->vfp.fp_status); | |
5630 | i |= get_float_exception_flags(&env->vfp.standard_fp_status); | |
5631 | fpscr |= vfp_exceptbits_from_host(i); | |
5632 | return fpscr; | |
5633 | } | |
5634 | ||
5635 | uint32_t vfp_get_fpscr(CPUARMState *env) | |
5636 | { | |
5637 | return HELPER(vfp_get_fpscr)(env); | |
5638 | } | |
5639 | ||
5640 | /* Convert vfp exception flags to target form. */ | |
5641 | static inline int vfp_exceptbits_to_host(int target_bits) | |
5642 | { | |
5643 | int host_bits = 0; | |
5644 | ||
5645 | if (target_bits & 1) | |
5646 | host_bits |= float_flag_invalid; | |
5647 | if (target_bits & 2) | |
5648 | host_bits |= float_flag_divbyzero; | |
5649 | if (target_bits & 4) | |
5650 | host_bits |= float_flag_overflow; | |
5651 | if (target_bits & 8) | |
5652 | host_bits |= float_flag_underflow; | |
5653 | if (target_bits & 0x10) | |
5654 | host_bits |= float_flag_inexact; | |
5655 | if (target_bits & 0x80) | |
5656 | host_bits |= float_flag_input_denormal; | |
5657 | return host_bits; | |
5658 | } | |
5659 | ||
5660 | void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val) | |
5661 | { | |
5662 | int i; | |
5663 | uint32_t changed; | |
5664 | ||
5665 | changed = env->vfp.xregs[ARM_VFP_FPSCR]; | |
5666 | env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff); | |
5667 | env->vfp.vec_len = (val >> 16) & 7; | |
5668 | env->vfp.vec_stride = (val >> 20) & 3; | |
5669 | ||
5670 | changed ^= val; | |
5671 | if (changed & (3 << 22)) { | |
5672 | i = (val >> 22) & 3; | |
5673 | switch (i) { | |
5674 | case FPROUNDING_TIEEVEN: | |
5675 | i = float_round_nearest_even; | |
5676 | break; | |
5677 | case FPROUNDING_POSINF: | |
5678 | i = float_round_up; | |
5679 | break; | |
5680 | case FPROUNDING_NEGINF: | |
5681 | i = float_round_down; | |
5682 | break; | |
5683 | case FPROUNDING_ZERO: | |
5684 | i = float_round_to_zero; | |
5685 | break; | |
5686 | } | |
5687 | set_float_rounding_mode(i, &env->vfp.fp_status); | |
5688 | } | |
5689 | if (changed & (1 << 24)) { | |
5690 | set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status); | |
5691 | set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status); | |
5692 | } | |
5693 | if (changed & (1 << 25)) | |
5694 | set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status); | |
5695 | ||
5696 | i = vfp_exceptbits_to_host(val); | |
5697 | set_float_exception_flags(i, &env->vfp.fp_status); | |
5698 | set_float_exception_flags(0, &env->vfp.standard_fp_status); | |
5699 | } | |
5700 | ||
5701 | void vfp_set_fpscr(CPUARMState *env, uint32_t val) | |
5702 | { | |
5703 | HELPER(vfp_set_fpscr)(env, val); | |
5704 | } | |
5705 | ||
5706 | #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p)) | |
5707 | ||
5708 | #define VFP_BINOP(name) \ | |
5709 | float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \ | |
5710 | { \ | |
5711 | float_status *fpst = fpstp; \ | |
5712 | return float32_ ## name(a, b, fpst); \ | |
5713 | } \ | |
5714 | float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \ | |
5715 | { \ | |
5716 | float_status *fpst = fpstp; \ | |
5717 | return float64_ ## name(a, b, fpst); \ | |
5718 | } | |
5719 | VFP_BINOP(add) | |
5720 | VFP_BINOP(sub) | |
5721 | VFP_BINOP(mul) | |
5722 | VFP_BINOP(div) | |
5723 | VFP_BINOP(min) | |
5724 | VFP_BINOP(max) | |
5725 | VFP_BINOP(minnum) | |
5726 | VFP_BINOP(maxnum) | |
5727 | #undef VFP_BINOP | |
5728 | ||
5729 | float32 VFP_HELPER(neg, s)(float32 a) | |
5730 | { | |
5731 | return float32_chs(a); | |
5732 | } | |
5733 | ||
5734 | float64 VFP_HELPER(neg, d)(float64 a) | |
5735 | { | |
5736 | return float64_chs(a); | |
5737 | } | |
5738 | ||
5739 | float32 VFP_HELPER(abs, s)(float32 a) | |
5740 | { | |
5741 | return float32_abs(a); | |
5742 | } | |
5743 | ||
5744 | float64 VFP_HELPER(abs, d)(float64 a) | |
5745 | { | |
5746 | return float64_abs(a); | |
5747 | } | |
5748 | ||
5749 | float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env) | |
5750 | { | |
5751 | return float32_sqrt(a, &env->vfp.fp_status); | |
5752 | } | |
5753 | ||
5754 | float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env) | |
5755 | { | |
5756 | return float64_sqrt(a, &env->vfp.fp_status); | |
5757 | } | |
5758 | ||
5759 | /* XXX: check quiet/signaling case */ | |
5760 | #define DO_VFP_cmp(p, type) \ | |
5761 | void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \ | |
5762 | { \ | |
5763 | uint32_t flags; \ | |
5764 | switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \ | |
5765 | case 0: flags = 0x6; break; \ | |
5766 | case -1: flags = 0x8; break; \ | |
5767 | case 1: flags = 0x2; break; \ | |
5768 | default: case 2: flags = 0x3; break; \ | |
5769 | } \ | |
5770 | env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ | |
5771 | | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ | |
5772 | } \ | |
5773 | void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \ | |
5774 | { \ | |
5775 | uint32_t flags; \ | |
5776 | switch(type ## _compare(a, b, &env->vfp.fp_status)) { \ | |
5777 | case 0: flags = 0x6; break; \ | |
5778 | case -1: flags = 0x8; break; \ | |
5779 | case 1: flags = 0x2; break; \ | |
5780 | default: case 2: flags = 0x3; break; \ | |
5781 | } \ | |
5782 | env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ | |
5783 | | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ | |
5784 | } | |
5785 | DO_VFP_cmp(s, float32) | |
5786 | DO_VFP_cmp(d, float64) | |
5787 | #undef DO_VFP_cmp | |
5788 | ||
5789 | /* Integer to float and float to integer conversions */ | |
5790 | ||
5791 | #define CONV_ITOF(name, fsz, sign) \ | |
5792 | float##fsz HELPER(name)(uint32_t x, void *fpstp) \ | |
5793 | { \ | |
5794 | float_status *fpst = fpstp; \ | |
5795 | return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \ | |
5796 | } | |
5797 | ||
5798 | #define CONV_FTOI(name, fsz, sign, round) \ | |
5799 | uint32_t HELPER(name)(float##fsz x, void *fpstp) \ | |
5800 | { \ | |
5801 | float_status *fpst = fpstp; \ | |
5802 | if (float##fsz##_is_any_nan(x)) { \ | |
5803 | float_raise(float_flag_invalid, fpst); \ | |
5804 | return 0; \ | |
5805 | } \ | |
5806 | return float##fsz##_to_##sign##int32##round(x, fpst); \ | |
5807 | } | |
5808 | ||
5809 | #define FLOAT_CONVS(name, p, fsz, sign) \ | |
5810 | CONV_ITOF(vfp_##name##to##p, fsz, sign) \ | |
5811 | CONV_FTOI(vfp_to##name##p, fsz, sign, ) \ | |
5812 | CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero) | |
5813 | ||
5814 | FLOAT_CONVS(si, s, 32, ) | |
5815 | FLOAT_CONVS(si, d, 64, ) | |
5816 | FLOAT_CONVS(ui, s, 32, u) | |
5817 | FLOAT_CONVS(ui, d, 64, u) | |
5818 | ||
5819 | #undef CONV_ITOF | |
5820 | #undef CONV_FTOI | |
5821 | #undef FLOAT_CONVS | |
5822 | ||
5823 | /* floating point conversion */ | |
5824 | float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env) | |
5825 | { | |
5826 | float64 r = float32_to_float64(x, &env->vfp.fp_status); | |
5827 | /* ARM requires that S<->D conversion of any kind of NaN generates | |
5828 | * a quiet NaN by forcing the most significant frac bit to 1. | |
5829 | */ | |
5830 | return float64_maybe_silence_nan(r); | |
5831 | } | |
5832 | ||
5833 | float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env) | |
5834 | { | |
5835 | float32 r = float64_to_float32(x, &env->vfp.fp_status); | |
5836 | /* ARM requires that S<->D conversion of any kind of NaN generates | |
5837 | * a quiet NaN by forcing the most significant frac bit to 1. | |
5838 | */ | |
5839 | return float32_maybe_silence_nan(r); | |
5840 | } | |
5841 | ||
5842 | /* VFP3 fixed point conversion. */ | |
5843 | #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ | |
5844 | float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \ | |
5845 | void *fpstp) \ | |
5846 | { \ | |
5847 | float_status *fpst = fpstp; \ | |
5848 | float##fsz tmp; \ | |
5849 | tmp = itype##_to_##float##fsz(x, fpst); \ | |
5850 | return float##fsz##_scalbn(tmp, -(int)shift, fpst); \ | |
5851 | } | |
5852 | ||
5853 | /* Notice that we want only input-denormal exception flags from the | |
5854 | * scalbn operation: the other possible flags (overflow+inexact if | |
5855 | * we overflow to infinity, output-denormal) aren't correct for the | |
5856 | * complete scale-and-convert operation. | |
5857 | */ | |
5858 | #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \ | |
5859 | uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \ | |
5860 | uint32_t shift, \ | |
5861 | void *fpstp) \ | |
5862 | { \ | |
5863 | float_status *fpst = fpstp; \ | |
5864 | int old_exc_flags = get_float_exception_flags(fpst); \ | |
5865 | float##fsz tmp; \ | |
5866 | if (float##fsz##_is_any_nan(x)) { \ | |
5867 | float_raise(float_flag_invalid, fpst); \ | |
5868 | return 0; \ | |
5869 | } \ | |
5870 | tmp = float##fsz##_scalbn(x, shift, fpst); \ | |
5871 | old_exc_flags |= get_float_exception_flags(fpst) \ | |
5872 | & float_flag_input_denormal; \ | |
5873 | set_float_exception_flags(old_exc_flags, fpst); \ | |
5874 | return float##fsz##_to_##itype##round(tmp, fpst); \ | |
5875 | } | |
5876 | ||
5877 | #define VFP_CONV_FIX(name, p, fsz, isz, itype) \ | |
5878 | VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ | |
5879 | VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \ | |
5880 | VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ) | |
5881 | ||
5882 | #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \ | |
5883 | VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ | |
5884 | VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ) | |
5885 | ||
5886 | VFP_CONV_FIX(sh, d, 64, 64, int16) | |
5887 | VFP_CONV_FIX(sl, d, 64, 64, int32) | |
5888 | VFP_CONV_FIX_A64(sq, d, 64, 64, int64) | |
5889 | VFP_CONV_FIX(uh, d, 64, 64, uint16) | |
5890 | VFP_CONV_FIX(ul, d, 64, 64, uint32) | |
5891 | VFP_CONV_FIX_A64(uq, d, 64, 64, uint64) | |
5892 | VFP_CONV_FIX(sh, s, 32, 32, int16) | |
5893 | VFP_CONV_FIX(sl, s, 32, 32, int32) | |
5894 | VFP_CONV_FIX_A64(sq, s, 32, 64, int64) | |
5895 | VFP_CONV_FIX(uh, s, 32, 32, uint16) | |
5896 | VFP_CONV_FIX(ul, s, 32, 32, uint32) | |
5897 | VFP_CONV_FIX_A64(uq, s, 32, 64, uint64) | |
5898 | #undef VFP_CONV_FIX | |
5899 | #undef VFP_CONV_FIX_FLOAT | |
5900 | #undef VFP_CONV_FLOAT_FIX_ROUND | |
5901 | ||
5902 | /* Set the current fp rounding mode and return the old one. | |
5903 | * The argument is a softfloat float_round_ value. | |
5904 | */ | |
5905 | uint32_t HELPER(set_rmode)(uint32_t rmode, CPUARMState *env) | |
5906 | { | |
5907 | float_status *fp_status = &env->vfp.fp_status; | |
5908 | ||
5909 | uint32_t prev_rmode = get_float_rounding_mode(fp_status); | |
5910 | set_float_rounding_mode(rmode, fp_status); | |
5911 | ||
5912 | return prev_rmode; | |
5913 | } | |
5914 | ||
5915 | /* Set the current fp rounding mode in the standard fp status and return | |
5916 | * the old one. This is for NEON instructions that need to change the | |
5917 | * rounding mode but wish to use the standard FPSCR values for everything | |
5918 | * else. Always set the rounding mode back to the correct value after | |
5919 | * modifying it. | |
5920 | * The argument is a softfloat float_round_ value. | |
5921 | */ | |
5922 | uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env) | |
5923 | { | |
5924 | float_status *fp_status = &env->vfp.standard_fp_status; | |
5925 | ||
5926 | uint32_t prev_rmode = get_float_rounding_mode(fp_status); | |
5927 | set_float_rounding_mode(rmode, fp_status); | |
5928 | ||
5929 | return prev_rmode; | |
5930 | } | |
5931 | ||
5932 | /* Half precision conversions. */ | |
5933 | static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s) | |
5934 | { | |
5935 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; | |
5936 | float32 r = float16_to_float32(make_float16(a), ieee, s); | |
5937 | if (ieee) { | |
5938 | return float32_maybe_silence_nan(r); | |
5939 | } | |
5940 | return r; | |
5941 | } | |
5942 | ||
5943 | static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s) | |
5944 | { | |
5945 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; | |
5946 | float16 r = float32_to_float16(a, ieee, s); | |
5947 | if (ieee) { | |
5948 | r = float16_maybe_silence_nan(r); | |
5949 | } | |
5950 | return float16_val(r); | |
5951 | } | |
5952 | ||
5953 | float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env) | |
5954 | { | |
5955 | return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status); | |
5956 | } | |
5957 | ||
5958 | uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env) | |
5959 | { | |
5960 | return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status); | |
5961 | } | |
5962 | ||
5963 | float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env) | |
5964 | { | |
5965 | return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status); | |
5966 | } | |
5967 | ||
5968 | uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env) | |
5969 | { | |
5970 | return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status); | |
5971 | } | |
5972 | ||
5973 | float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, CPUARMState *env) | |
5974 | { | |
5975 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; | |
5976 | float64 r = float16_to_float64(make_float16(a), ieee, &env->vfp.fp_status); | |
5977 | if (ieee) { | |
5978 | return float64_maybe_silence_nan(r); | |
5979 | } | |
5980 | return r; | |
5981 | } | |
5982 | ||
5983 | uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, CPUARMState *env) | |
5984 | { | |
5985 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; | |
5986 | float16 r = float64_to_float16(a, ieee, &env->vfp.fp_status); | |
5987 | if (ieee) { | |
5988 | r = float16_maybe_silence_nan(r); | |
5989 | } | |
5990 | return float16_val(r); | |
5991 | } | |
5992 | ||
5993 | #define float32_two make_float32(0x40000000) | |
5994 | #define float32_three make_float32(0x40400000) | |
5995 | #define float32_one_point_five make_float32(0x3fc00000) | |
5996 | ||
5997 | float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env) | |
5998 | { | |
5999 | float_status *s = &env->vfp.standard_fp_status; | |
6000 | if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || | |
6001 | (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { | |
6002 | if (!(float32_is_zero(a) || float32_is_zero(b))) { | |
6003 | float_raise(float_flag_input_denormal, s); | |
6004 | } | |
6005 | return float32_two; | |
6006 | } | |
6007 | return float32_sub(float32_two, float32_mul(a, b, s), s); | |
6008 | } | |
6009 | ||
6010 | float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env) | |
6011 | { | |
6012 | float_status *s = &env->vfp.standard_fp_status; | |
6013 | float32 product; | |
6014 | if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || | |
6015 | (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { | |
6016 | if (!(float32_is_zero(a) || float32_is_zero(b))) { | |
6017 | float_raise(float_flag_input_denormal, s); | |
6018 | } | |
6019 | return float32_one_point_five; | |
6020 | } | |
6021 | product = float32_mul(a, b, s); | |
6022 | return float32_div(float32_sub(float32_three, product, s), float32_two, s); | |
6023 | } | |
6024 | ||
6025 | /* NEON helpers. */ | |
6026 | ||
6027 | /* Constants 256 and 512 are used in some helpers; we avoid relying on | |
6028 | * int->float conversions at run-time. */ | |
6029 | #define float64_256 make_float64(0x4070000000000000LL) | |
6030 | #define float64_512 make_float64(0x4080000000000000LL) | |
6031 | #define float32_maxnorm make_float32(0x7f7fffff) | |
6032 | #define float64_maxnorm make_float64(0x7fefffffffffffffLL) | |
6033 | ||
6034 | /* Reciprocal functions | |
6035 | * | |
6036 | * The algorithm that must be used to calculate the estimate | |
6037 | * is specified by the ARM ARM, see FPRecipEstimate() | |
6038 | */ | |
6039 | ||
6040 | static float64 recip_estimate(float64 a, float_status *real_fp_status) | |
6041 | { | |
6042 | /* These calculations mustn't set any fp exception flags, | |
6043 | * so we use a local copy of the fp_status. | |
6044 | */ | |
6045 | float_status dummy_status = *real_fp_status; | |
6046 | float_status *s = &dummy_status; | |
6047 | /* q = (int)(a * 512.0) */ | |
6048 | float64 q = float64_mul(float64_512, a, s); | |
6049 | int64_t q_int = float64_to_int64_round_to_zero(q, s); | |
6050 | ||
6051 | /* r = 1.0 / (((double)q + 0.5) / 512.0) */ | |
6052 | q = int64_to_float64(q_int, s); | |
6053 | q = float64_add(q, float64_half, s); | |
6054 | q = float64_div(q, float64_512, s); | |
6055 | q = float64_div(float64_one, q, s); | |
6056 | ||
6057 | /* s = (int)(256.0 * r + 0.5) */ | |
6058 | q = float64_mul(q, float64_256, s); | |
6059 | q = float64_add(q, float64_half, s); | |
6060 | q_int = float64_to_int64_round_to_zero(q, s); | |
6061 | ||
6062 | /* return (double)s / 256.0 */ | |
6063 | return float64_div(int64_to_float64(q_int, s), float64_256, s); | |
6064 | } | |
6065 | ||
6066 | /* Common wrapper to call recip_estimate */ | |
6067 | static float64 call_recip_estimate(float64 num, int off, float_status *fpst) | |
6068 | { | |
6069 | uint64_t val64 = float64_val(num); | |
6070 | uint64_t frac = extract64(val64, 0, 52); | |
6071 | int64_t exp = extract64(val64, 52, 11); | |
6072 | uint64_t sbit; | |
6073 | float64 scaled, estimate; | |
6074 | ||
6075 | /* Generate the scaled number for the estimate function */ | |
6076 | if (exp == 0) { | |
6077 | if (extract64(frac, 51, 1) == 0) { | |
6078 | exp = -1; | |
6079 | frac = extract64(frac, 0, 50) << 2; | |
6080 | } else { | |
6081 | frac = extract64(frac, 0, 51) << 1; | |
6082 | } | |
6083 | } | |
6084 | ||
6085 | /* scaled = '0' : '01111111110' : fraction<51:44> : Zeros(44); */ | |
6086 | scaled = make_float64((0x3feULL << 52) | |
6087 | | extract64(frac, 44, 8) << 44); | |
6088 | ||
6089 | estimate = recip_estimate(scaled, fpst); | |
6090 | ||
6091 | /* Build new result */ | |
6092 | val64 = float64_val(estimate); | |
6093 | sbit = 0x8000000000000000ULL & val64; | |
6094 | exp = off - exp; | |
6095 | frac = extract64(val64, 0, 52); | |
6096 | ||
6097 | if (exp == 0) { | |
6098 | frac = 1ULL << 51 | extract64(frac, 1, 51); | |
6099 | } else if (exp == -1) { | |
6100 | frac = 1ULL << 50 | extract64(frac, 2, 50); | |
6101 | exp = 0; | |
6102 | } | |
6103 | ||
6104 | return make_float64(sbit | (exp << 52) | frac); | |
6105 | } | |
6106 | ||
6107 | static bool round_to_inf(float_status *fpst, bool sign_bit) | |
6108 | { | |
6109 | switch (fpst->float_rounding_mode) { | |
6110 | case float_round_nearest_even: /* Round to Nearest */ | |
6111 | return true; | |
6112 | case float_round_up: /* Round to +Inf */ | |
6113 | return !sign_bit; | |
6114 | case float_round_down: /* Round to -Inf */ | |
6115 | return sign_bit; | |
6116 | case float_round_to_zero: /* Round to Zero */ | |
6117 | return false; | |
6118 | } | |
6119 | ||
6120 | g_assert_not_reached(); | |
6121 | } | |
6122 | ||
6123 | float32 HELPER(recpe_f32)(float32 input, void *fpstp) | |
6124 | { | |
6125 | float_status *fpst = fpstp; | |
6126 | float32 f32 = float32_squash_input_denormal(input, fpst); | |
6127 | uint32_t f32_val = float32_val(f32); | |
6128 | uint32_t f32_sbit = 0x80000000ULL & f32_val; | |
6129 | int32_t f32_exp = extract32(f32_val, 23, 8); | |
6130 | uint32_t f32_frac = extract32(f32_val, 0, 23); | |
6131 | float64 f64, r64; | |
6132 | uint64_t r64_val; | |
6133 | int64_t r64_exp; | |
6134 | uint64_t r64_frac; | |
6135 | ||
6136 | if (float32_is_any_nan(f32)) { | |
6137 | float32 nan = f32; | |
6138 | if (float32_is_signaling_nan(f32)) { | |
6139 | float_raise(float_flag_invalid, fpst); | |
6140 | nan = float32_maybe_silence_nan(f32); | |
6141 | } | |
6142 | if (fpst->default_nan_mode) { | |
6143 | nan = float32_default_nan; | |
6144 | } | |
6145 | return nan; | |
6146 | } else if (float32_is_infinity(f32)) { | |
6147 | return float32_set_sign(float32_zero, float32_is_neg(f32)); | |
6148 | } else if (float32_is_zero(f32)) { | |
6149 | float_raise(float_flag_divbyzero, fpst); | |
6150 | return float32_set_sign(float32_infinity, float32_is_neg(f32)); | |
6151 | } else if ((f32_val & ~(1ULL << 31)) < (1ULL << 21)) { | |
6152 | /* Abs(value) < 2.0^-128 */ | |
6153 | float_raise(float_flag_overflow | float_flag_inexact, fpst); | |
6154 | if (round_to_inf(fpst, f32_sbit)) { | |
6155 | return float32_set_sign(float32_infinity, float32_is_neg(f32)); | |
6156 | } else { | |
6157 | return float32_set_sign(float32_maxnorm, float32_is_neg(f32)); | |
6158 | } | |
6159 | } else if (f32_exp >= 253 && fpst->flush_to_zero) { | |
6160 | float_raise(float_flag_underflow, fpst); | |
6161 | return float32_set_sign(float32_zero, float32_is_neg(f32)); | |
6162 | } | |
6163 | ||
6164 | ||
6165 | f64 = make_float64(((int64_t)(f32_exp) << 52) | (int64_t)(f32_frac) << 29); | |
6166 | r64 = call_recip_estimate(f64, 253, fpst); | |
6167 | r64_val = float64_val(r64); | |
6168 | r64_exp = extract64(r64_val, 52, 11); | |
6169 | r64_frac = extract64(r64_val, 0, 52); | |
6170 | ||
6171 | /* result = sign : result_exp<7:0> : fraction<51:29>; */ | |
6172 | return make_float32(f32_sbit | | |
6173 | (r64_exp & 0xff) << 23 | | |
6174 | extract64(r64_frac, 29, 24)); | |
6175 | } | |
6176 | ||
6177 | float64 HELPER(recpe_f64)(float64 input, void *fpstp) | |
6178 | { | |
6179 | float_status *fpst = fpstp; | |
6180 | float64 f64 = float64_squash_input_denormal(input, fpst); | |
6181 | uint64_t f64_val = float64_val(f64); | |
6182 | uint64_t f64_sbit = 0x8000000000000000ULL & f64_val; | |
6183 | int64_t f64_exp = extract64(f64_val, 52, 11); | |
6184 | float64 r64; | |
6185 | uint64_t r64_val; | |
6186 | int64_t r64_exp; | |
6187 | uint64_t r64_frac; | |
6188 | ||
6189 | /* Deal with any special cases */ | |
6190 | if (float64_is_any_nan(f64)) { | |
6191 | float64 nan = f64; | |
6192 | if (float64_is_signaling_nan(f64)) { | |
6193 | float_raise(float_flag_invalid, fpst); | |
6194 | nan = float64_maybe_silence_nan(f64); | |
6195 | } | |
6196 | if (fpst->default_nan_mode) { | |
6197 | nan = float64_default_nan; | |
6198 | } | |
6199 | return nan; | |
6200 | } else if (float64_is_infinity(f64)) { | |
6201 | return float64_set_sign(float64_zero, float64_is_neg(f64)); | |
6202 | } else if (float64_is_zero(f64)) { | |
6203 | float_raise(float_flag_divbyzero, fpst); | |
6204 | return float64_set_sign(float64_infinity, float64_is_neg(f64)); | |
6205 | } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) { | |
6206 | /* Abs(value) < 2.0^-1024 */ | |
6207 | float_raise(float_flag_overflow | float_flag_inexact, fpst); | |
6208 | if (round_to_inf(fpst, f64_sbit)) { | |
6209 | return float64_set_sign(float64_infinity, float64_is_neg(f64)); | |
6210 | } else { | |
6211 | return float64_set_sign(float64_maxnorm, float64_is_neg(f64)); | |
6212 | } | |
6213 | } else if (f64_exp >= 1023 && fpst->flush_to_zero) { | |
6214 | float_raise(float_flag_underflow, fpst); | |
6215 | return float64_set_sign(float64_zero, float64_is_neg(f64)); | |
6216 | } | |
6217 | ||
6218 | r64 = call_recip_estimate(f64, 2045, fpst); | |
6219 | r64_val = float64_val(r64); | |
6220 | r64_exp = extract64(r64_val, 52, 11); | |
6221 | r64_frac = extract64(r64_val, 0, 52); | |
6222 | ||
6223 | /* result = sign : result_exp<10:0> : fraction<51:0> */ | |
6224 | return make_float64(f64_sbit | | |
6225 | ((r64_exp & 0x7ff) << 52) | | |
6226 | r64_frac); | |
6227 | } | |
6228 | ||
6229 | /* The algorithm that must be used to calculate the estimate | |
6230 | * is specified by the ARM ARM. | |
6231 | */ | |
6232 | static float64 recip_sqrt_estimate(float64 a, float_status *real_fp_status) | |
6233 | { | |
6234 | /* These calculations mustn't set any fp exception flags, | |
6235 | * so we use a local copy of the fp_status. | |
6236 | */ | |
6237 | float_status dummy_status = *real_fp_status; | |
6238 | float_status *s = &dummy_status; | |
6239 | float64 q; | |
6240 | int64_t q_int; | |
6241 | ||
6242 | if (float64_lt(a, float64_half, s)) { | |
6243 | /* range 0.25 <= a < 0.5 */ | |
6244 | ||
6245 | /* a in units of 1/512 rounded down */ | |
6246 | /* q0 = (int)(a * 512.0); */ | |
6247 | q = float64_mul(float64_512, a, s); | |
6248 | q_int = float64_to_int64_round_to_zero(q, s); | |
6249 | ||
6250 | /* reciprocal root r */ | |
6251 | /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */ | |
6252 | q = int64_to_float64(q_int, s); | |
6253 | q = float64_add(q, float64_half, s); | |
6254 | q = float64_div(q, float64_512, s); | |
6255 | q = float64_sqrt(q, s); | |
6256 | q = float64_div(float64_one, q, s); | |
6257 | } else { | |
6258 | /* range 0.5 <= a < 1.0 */ | |
6259 | ||
6260 | /* a in units of 1/256 rounded down */ | |
6261 | /* q1 = (int)(a * 256.0); */ | |
6262 | q = float64_mul(float64_256, a, s); | |
6263 | int64_t q_int = float64_to_int64_round_to_zero(q, s); | |
6264 | ||
6265 | /* reciprocal root r */ | |
6266 | /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */ | |
6267 | q = int64_to_float64(q_int, s); | |
6268 | q = float64_add(q, float64_half, s); | |
6269 | q = float64_div(q, float64_256, s); | |
6270 | q = float64_sqrt(q, s); | |
6271 | q = float64_div(float64_one, q, s); | |
6272 | } | |
6273 | /* r in units of 1/256 rounded to nearest */ | |
6274 | /* s = (int)(256.0 * r + 0.5); */ | |
6275 | ||
6276 | q = float64_mul(q, float64_256,s ); | |
6277 | q = float64_add(q, float64_half, s); | |
6278 | q_int = float64_to_int64_round_to_zero(q, s); | |
6279 | ||
6280 | /* return (double)s / 256.0;*/ | |
6281 | return float64_div(int64_to_float64(q_int, s), float64_256, s); | |
6282 | } | |
6283 | ||
6284 | float32 HELPER(rsqrte_f32)(float32 input, void *fpstp) | |
6285 | { | |
6286 | float_status *s = fpstp; | |
6287 | float32 f32 = float32_squash_input_denormal(input, s); | |
6288 | uint32_t val = float32_val(f32); | |
6289 | uint32_t f32_sbit = 0x80000000 & val; | |
6290 | int32_t f32_exp = extract32(val, 23, 8); | |
6291 | uint32_t f32_frac = extract32(val, 0, 23); | |
6292 | uint64_t f64_frac; | |
6293 | uint64_t val64; | |
6294 | int result_exp; | |
6295 | float64 f64; | |
6296 | ||
6297 | if (float32_is_any_nan(f32)) { | |
6298 | float32 nan = f32; | |
6299 | if (float32_is_signaling_nan(f32)) { | |
6300 | float_raise(float_flag_invalid, s); | |
6301 | nan = float32_maybe_silence_nan(f32); | |
6302 | } | |
6303 | if (s->default_nan_mode) { | |
6304 | nan = float32_default_nan; | |
6305 | } | |
6306 | return nan; | |
6307 | } else if (float32_is_zero(f32)) { | |
6308 | float_raise(float_flag_divbyzero, s); | |
6309 | return float32_set_sign(float32_infinity, float32_is_neg(f32)); | |
6310 | } else if (float32_is_neg(f32)) { | |
6311 | float_raise(float_flag_invalid, s); | |
6312 | return float32_default_nan; | |
6313 | } else if (float32_is_infinity(f32)) { | |
6314 | return float32_zero; | |
6315 | } | |
6316 | ||
6317 | /* Scale and normalize to a double-precision value between 0.25 and 1.0, | |
6318 | * preserving the parity of the exponent. */ | |
6319 | ||
6320 | f64_frac = ((uint64_t) f32_frac) << 29; | |
6321 | if (f32_exp == 0) { | |
6322 | while (extract64(f64_frac, 51, 1) == 0) { | |
6323 | f64_frac = f64_frac << 1; | |
6324 | f32_exp = f32_exp-1; | |
6325 | } | |
6326 | f64_frac = extract64(f64_frac, 0, 51) << 1; | |
6327 | } | |
6328 | ||
6329 | if (extract64(f32_exp, 0, 1) == 0) { | |
6330 | f64 = make_float64(((uint64_t) f32_sbit) << 32 | |
6331 | | (0x3feULL << 52) | |
6332 | | f64_frac); | |
6333 | } else { | |
6334 | f64 = make_float64(((uint64_t) f32_sbit) << 32 | |
6335 | | (0x3fdULL << 52) | |
6336 | | f64_frac); | |
6337 | } | |
6338 | ||
6339 | result_exp = (380 - f32_exp) / 2; | |
6340 | ||
6341 | f64 = recip_sqrt_estimate(f64, s); | |
6342 | ||
6343 | val64 = float64_val(f64); | |
6344 | ||
6345 | val = ((result_exp & 0xff) << 23) | |
6346 | | ((val64 >> 29) & 0x7fffff); | |
6347 | return make_float32(val); | |
6348 | } | |
6349 | ||
6350 | float64 HELPER(rsqrte_f64)(float64 input, void *fpstp) | |
6351 | { | |
6352 | float_status *s = fpstp; | |
6353 | float64 f64 = float64_squash_input_denormal(input, s); | |
6354 | uint64_t val = float64_val(f64); | |
6355 | uint64_t f64_sbit = 0x8000000000000000ULL & val; | |
6356 | int64_t f64_exp = extract64(val, 52, 11); | |
6357 | uint64_t f64_frac = extract64(val, 0, 52); | |
6358 | int64_t result_exp; | |
6359 | uint64_t result_frac; | |
6360 | ||
6361 | if (float64_is_any_nan(f64)) { | |
6362 | float64 nan = f64; | |
6363 | if (float64_is_signaling_nan(f64)) { | |
6364 | float_raise(float_flag_invalid, s); | |
6365 | nan = float64_maybe_silence_nan(f64); | |
6366 | } | |
6367 | if (s->default_nan_mode) { | |
6368 | nan = float64_default_nan; | |
6369 | } | |
6370 | return nan; | |
6371 | } else if (float64_is_zero(f64)) { | |
6372 | float_raise(float_flag_divbyzero, s); | |
6373 | return float64_set_sign(float64_infinity, float64_is_neg(f64)); | |
6374 | } else if (float64_is_neg(f64)) { | |
6375 | float_raise(float_flag_invalid, s); | |
6376 | return float64_default_nan; | |
6377 | } else if (float64_is_infinity(f64)) { | |
6378 | return float64_zero; | |
6379 | } | |
6380 | ||
6381 | /* Scale and normalize to a double-precision value between 0.25 and 1.0, | |
6382 | * preserving the parity of the exponent. */ | |
6383 | ||
6384 | if (f64_exp == 0) { | |
6385 | while (extract64(f64_frac, 51, 1) == 0) { | |
6386 | f64_frac = f64_frac << 1; | |
6387 | f64_exp = f64_exp - 1; | |
6388 | } | |
6389 | f64_frac = extract64(f64_frac, 0, 51) << 1; | |
6390 | } | |
6391 | ||
6392 | if (extract64(f64_exp, 0, 1) == 0) { | |
6393 | f64 = make_float64(f64_sbit | |
6394 | | (0x3feULL << 52) | |
6395 | | f64_frac); | |
6396 | } else { | |
6397 | f64 = make_float64(f64_sbit | |
6398 | | (0x3fdULL << 52) | |
6399 | | f64_frac); | |
6400 | } | |
6401 | ||
6402 | result_exp = (3068 - f64_exp) / 2; | |
6403 | ||
6404 | f64 = recip_sqrt_estimate(f64, s); | |
6405 | ||
6406 | result_frac = extract64(float64_val(f64), 0, 52); | |
6407 | ||
6408 | return make_float64(f64_sbit | | |
6409 | ((result_exp & 0x7ff) << 52) | | |
6410 | result_frac); | |
6411 | } | |
6412 | ||
6413 | uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp) | |
6414 | { | |
6415 | float_status *s = fpstp; | |
6416 | float64 f64; | |
6417 | ||
6418 | if ((a & 0x80000000) == 0) { | |
6419 | return 0xffffffff; | |
6420 | } | |
6421 | ||
6422 | f64 = make_float64((0x3feULL << 52) | |
6423 | | ((int64_t)(a & 0x7fffffff) << 21)); | |
6424 | ||
6425 | f64 = recip_estimate(f64, s); | |
6426 | ||
6427 | return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff); | |
6428 | } | |
6429 | ||
6430 | uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp) | |
6431 | { | |
6432 | float_status *fpst = fpstp; | |
6433 | float64 f64; | |
6434 | ||
6435 | if ((a & 0xc0000000) == 0) { | |
6436 | return 0xffffffff; | |
6437 | } | |
6438 | ||
6439 | if (a & 0x80000000) { | |
6440 | f64 = make_float64((0x3feULL << 52) | |
6441 | | ((uint64_t)(a & 0x7fffffff) << 21)); | |
6442 | } else { /* bits 31-30 == '01' */ | |
6443 | f64 = make_float64((0x3fdULL << 52) | |
6444 | | ((uint64_t)(a & 0x3fffffff) << 22)); | |
6445 | } | |
6446 | ||
6447 | f64 = recip_sqrt_estimate(f64, fpst); | |
6448 | ||
6449 | return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff); | |
6450 | } | |
6451 | ||
6452 | /* VFPv4 fused multiply-accumulate */ | |
6453 | float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp) | |
6454 | { | |
6455 | float_status *fpst = fpstp; | |
6456 | return float32_muladd(a, b, c, 0, fpst); | |
6457 | } | |
6458 | ||
6459 | float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp) | |
6460 | { | |
6461 | float_status *fpst = fpstp; | |
6462 | return float64_muladd(a, b, c, 0, fpst); | |
6463 | } | |
6464 | ||
6465 | /* ARMv8 round to integral */ | |
6466 | float32 HELPER(rints_exact)(float32 x, void *fp_status) | |
6467 | { | |
6468 | return float32_round_to_int(x, fp_status); | |
6469 | } | |
6470 | ||
6471 | float64 HELPER(rintd_exact)(float64 x, void *fp_status) | |
6472 | { | |
6473 | return float64_round_to_int(x, fp_status); | |
6474 | } | |
6475 | ||
6476 | float32 HELPER(rints)(float32 x, void *fp_status) | |
6477 | { | |
6478 | int old_flags = get_float_exception_flags(fp_status), new_flags; | |
6479 | float32 ret; | |
6480 | ||
6481 | ret = float32_round_to_int(x, fp_status); | |
6482 | ||
6483 | /* Suppress any inexact exceptions the conversion produced */ | |
6484 | if (!(old_flags & float_flag_inexact)) { | |
6485 | new_flags = get_float_exception_flags(fp_status); | |
6486 | set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); | |
6487 | } | |
6488 | ||
6489 | return ret; | |
6490 | } | |
6491 | ||
6492 | float64 HELPER(rintd)(float64 x, void *fp_status) | |
6493 | { | |
6494 | int old_flags = get_float_exception_flags(fp_status), new_flags; | |
6495 | float64 ret; | |
6496 | ||
6497 | ret = float64_round_to_int(x, fp_status); | |
6498 | ||
6499 | new_flags = get_float_exception_flags(fp_status); | |
6500 | ||
6501 | /* Suppress any inexact exceptions the conversion produced */ | |
6502 | if (!(old_flags & float_flag_inexact)) { | |
6503 | new_flags = get_float_exception_flags(fp_status); | |
6504 | set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); | |
6505 | } | |
6506 | ||
6507 | return ret; | |
6508 | } | |
6509 | ||
6510 | /* Convert ARM rounding mode to softfloat */ | |
6511 | int arm_rmode_to_sf(int rmode) | |
6512 | { | |
6513 | switch (rmode) { | |
6514 | case FPROUNDING_TIEAWAY: | |
6515 | rmode = float_round_ties_away; | |
6516 | break; | |
6517 | case FPROUNDING_ODD: | |
6518 | /* FIXME: add support for TIEAWAY and ODD */ | |
6519 | qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n", | |
6520 | rmode); | |
6521 | case FPROUNDING_TIEEVEN: | |
6522 | default: | |
6523 | rmode = float_round_nearest_even; | |
6524 | break; | |
6525 | case FPROUNDING_POSINF: | |
6526 | rmode = float_round_up; | |
6527 | break; | |
6528 | case FPROUNDING_NEGINF: | |
6529 | rmode = float_round_down; | |
6530 | break; | |
6531 | case FPROUNDING_ZERO: | |
6532 | rmode = float_round_to_zero; | |
6533 | break; | |
6534 | } | |
6535 | return rmode; | |
6536 | } | |
6537 | ||
6538 | /* CRC helpers. | |
6539 | * The upper bytes of val (above the number specified by 'bytes') must have | |
6540 | * been zeroed out by the caller. | |
6541 | */ | |
6542 | uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes) | |
6543 | { | |
6544 | uint8_t buf[4]; | |
6545 | ||
6546 | stl_le_p(buf, val); | |
6547 | ||
6548 | /* zlib crc32 converts the accumulator and output to one's complement. */ | |
6549 | return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; | |
6550 | } | |
6551 | ||
6552 | uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) | |
6553 | { | |
6554 | uint8_t buf[4]; | |
6555 | ||
6556 | stl_le_p(buf, val); | |
6557 | ||
6558 | /* Linux crc32c converts the output to one's complement. */ | |
6559 | return crc32c(acc, buf, bytes) ^ 0xffffffff; | |
6560 | } |