bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 1 | #include "cpu.h" |
Paolo Bonzini | 022c62c | 2012-12-17 18:19:49 +0100 | [diff] [blame] | 2 | #include "exec/gdbstub.h" |
Lluís | 7b59220 | 2011-04-13 18:38:24 +0200 | [diff] [blame] | 3 | #include "helper.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 4 | #include "qemu/host-utils.h" |
Cole Robinson | 78027bb | 2013-09-10 19:09:33 +0100 | [diff] [blame] | 5 | #include "sysemu/arch_init.h" |
Paolo Bonzini | 9c17d61 | 2012-12-17 18:20:04 +0100 | [diff] [blame] | 6 | #include "sysemu/sysemu.h" |
Paolo Bonzini | 1de7afc | 2012-12-17 18:20:00 +0100 | [diff] [blame] | 7 | #include "qemu/bitops.h" |
Peter Maydell | 0b03bdf | 2012-01-25 12:42:29 +0000 | [diff] [blame] | 8 | |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 9 | #ifndef CONFIG_USER_ONLY |
| 10 | static inline int get_phys_addr(CPUARMState *env, uint32_t address, |
| 11 | int access_type, int is_user, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 12 | hwaddr *phys_ptr, int *prot, |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 13 | target_ulong *page_size); |
| 14 | #endif |
| 15 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 16 | static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 17 | { |
| 18 | int nregs; |
| 19 | |
| 20 | /* VFP data registers are always little-endian. */ |
| 21 | nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; |
| 22 | if (reg < nregs) { |
| 23 | stfq_le_p(buf, env->vfp.regs[reg]); |
| 24 | return 8; |
| 25 | } |
| 26 | if (arm_feature(env, ARM_FEATURE_NEON)) { |
| 27 | /* Aliases for Q regs. */ |
| 28 | nregs += 16; |
| 29 | if (reg < nregs) { |
| 30 | stfq_le_p(buf, env->vfp.regs[(reg - 32) * 2]); |
| 31 | stfq_le_p(buf + 8, env->vfp.regs[(reg - 32) * 2 + 1]); |
| 32 | return 16; |
| 33 | } |
| 34 | } |
| 35 | switch (reg - nregs) { |
| 36 | case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4; |
| 37 | case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4; |
| 38 | case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4; |
| 39 | } |
| 40 | return 0; |
| 41 | } |
| 42 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 43 | static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 44 | { |
| 45 | int nregs; |
| 46 | |
| 47 | nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; |
| 48 | if (reg < nregs) { |
| 49 | env->vfp.regs[reg] = ldfq_le_p(buf); |
| 50 | return 8; |
| 51 | } |
| 52 | if (arm_feature(env, ARM_FEATURE_NEON)) { |
| 53 | nregs += 16; |
| 54 | if (reg < nregs) { |
| 55 | env->vfp.regs[(reg - 32) * 2] = ldfq_le_p(buf); |
| 56 | env->vfp.regs[(reg - 32) * 2 + 1] = ldfq_le_p(buf + 8); |
| 57 | return 16; |
| 58 | } |
| 59 | } |
| 60 | switch (reg - nregs) { |
| 61 | case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; |
| 62 | case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4; |
Juha Riihimäki | 71b3c3d | 2009-10-26 11:46:42 +0200 | [diff] [blame] | 63 | case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 64 | } |
| 65 | return 0; |
| 66 | } |
| 67 | |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 68 | static int raw_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 69 | uint64_t *value) |
| 70 | { |
Peter Maydell | 22d9e1a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 71 | if (ri->type & ARM_CP_64BIT) { |
| 72 | *value = CPREG_FIELD64(env, ri); |
| 73 | } else { |
| 74 | *value = CPREG_FIELD32(env, ri); |
| 75 | } |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 76 | return 0; |
| 77 | } |
| 78 | |
| 79 | static int raw_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 80 | uint64_t value) |
| 81 | { |
Peter Maydell | 22d9e1a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 82 | if (ri->type & ARM_CP_64BIT) { |
| 83 | CPREG_FIELD64(env, ri) = value; |
| 84 | } else { |
| 85 | CPREG_FIELD32(env, ri) = value; |
| 86 | } |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 87 | return 0; |
| 88 | } |
| 89 | |
Peter Maydell | 721fae1 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 90 | static bool read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, |
| 91 | uint64_t *v) |
| 92 | { |
| 93 | /* Raw read of a coprocessor register (as needed for migration, etc) |
| 94 | * return true on success, false if the read is impossible for some reason. |
| 95 | */ |
| 96 | if (ri->type & ARM_CP_CONST) { |
| 97 | *v = ri->resetvalue; |
| 98 | } else if (ri->raw_readfn) { |
| 99 | return (ri->raw_readfn(env, ri, v) == 0); |
| 100 | } else if (ri->readfn) { |
| 101 | return (ri->readfn(env, ri, v) == 0); |
| 102 | } else { |
| 103 | if (ri->type & ARM_CP_64BIT) { |
| 104 | *v = CPREG_FIELD64(env, ri); |
| 105 | } else { |
| 106 | *v = CPREG_FIELD32(env, ri); |
| 107 | } |
| 108 | } |
| 109 | return true; |
| 110 | } |
| 111 | |
| 112 | static bool write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, |
| 113 | int64_t v) |
| 114 | { |
| 115 | /* Raw write of a coprocessor register (as needed for migration, etc). |
| 116 | * Return true on success, false if the write is impossible for some reason. |
| 117 | * Note that constant registers are treated as write-ignored; the |
| 118 | * caller should check for success by whether a readback gives the |
| 119 | * value written. |
| 120 | */ |
| 121 | if (ri->type & ARM_CP_CONST) { |
| 122 | return true; |
| 123 | } else if (ri->raw_writefn) { |
| 124 | return (ri->raw_writefn(env, ri, v) == 0); |
| 125 | } else if (ri->writefn) { |
| 126 | return (ri->writefn(env, ri, v) == 0); |
| 127 | } else { |
| 128 | if (ri->type & ARM_CP_64BIT) { |
| 129 | CPREG_FIELD64(env, ri) = v; |
| 130 | } else { |
| 131 | CPREG_FIELD32(env, ri) = v; |
| 132 | } |
| 133 | } |
| 134 | return true; |
| 135 | } |
| 136 | |
| 137 | bool write_cpustate_to_list(ARMCPU *cpu) |
| 138 | { |
| 139 | /* Write the coprocessor state from cpu->env to the (index,value) list. */ |
| 140 | int i; |
| 141 | bool ok = true; |
| 142 | |
| 143 | for (i = 0; i < cpu->cpreg_array_len; i++) { |
| 144 | uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); |
| 145 | const ARMCPRegInfo *ri; |
| 146 | uint64_t v; |
| 147 | ri = get_arm_cp_reginfo(cpu, regidx); |
| 148 | if (!ri) { |
| 149 | ok = false; |
| 150 | continue; |
| 151 | } |
| 152 | if (ri->type & ARM_CP_NO_MIGRATE) { |
| 153 | continue; |
| 154 | } |
| 155 | if (!read_raw_cp_reg(&cpu->env, ri, &v)) { |
| 156 | ok = false; |
| 157 | continue; |
| 158 | } |
| 159 | cpu->cpreg_values[i] = v; |
| 160 | } |
| 161 | return ok; |
| 162 | } |
| 163 | |
| 164 | bool write_list_to_cpustate(ARMCPU *cpu) |
| 165 | { |
| 166 | int i; |
| 167 | bool ok = true; |
| 168 | |
| 169 | for (i = 0; i < cpu->cpreg_array_len; i++) { |
| 170 | uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); |
| 171 | uint64_t v = cpu->cpreg_values[i]; |
| 172 | uint64_t readback; |
| 173 | const ARMCPRegInfo *ri; |
| 174 | |
| 175 | ri = get_arm_cp_reginfo(cpu, regidx); |
| 176 | if (!ri) { |
| 177 | ok = false; |
| 178 | continue; |
| 179 | } |
| 180 | if (ri->type & ARM_CP_NO_MIGRATE) { |
| 181 | continue; |
| 182 | } |
| 183 | /* Write value and confirm it reads back as written |
| 184 | * (to catch read-only registers and partially read-only |
| 185 | * registers where the incoming migration value doesn't match) |
| 186 | */ |
| 187 | if (!write_raw_cp_reg(&cpu->env, ri, v) || |
| 188 | !read_raw_cp_reg(&cpu->env, ri, &readback) || |
| 189 | readback != v) { |
| 190 | ok = false; |
| 191 | } |
| 192 | } |
| 193 | return ok; |
| 194 | } |
| 195 | |
| 196 | static void add_cpreg_to_list(gpointer key, gpointer opaque) |
| 197 | { |
| 198 | ARMCPU *cpu = opaque; |
| 199 | uint64_t regidx; |
| 200 | const ARMCPRegInfo *ri; |
| 201 | |
| 202 | regidx = *(uint32_t *)key; |
| 203 | ri = get_arm_cp_reginfo(cpu, regidx); |
| 204 | |
| 205 | if (!(ri->type & ARM_CP_NO_MIGRATE)) { |
| 206 | cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); |
| 207 | /* The value array need not be initialized at this point */ |
| 208 | cpu->cpreg_array_len++; |
| 209 | } |
| 210 | } |
| 211 | |
| 212 | static void count_cpreg(gpointer key, gpointer opaque) |
| 213 | { |
| 214 | ARMCPU *cpu = opaque; |
| 215 | uint64_t regidx; |
| 216 | const ARMCPRegInfo *ri; |
| 217 | |
| 218 | regidx = *(uint32_t *)key; |
| 219 | ri = get_arm_cp_reginfo(cpu, regidx); |
| 220 | |
| 221 | if (!(ri->type & ARM_CP_NO_MIGRATE)) { |
| 222 | cpu->cpreg_array_len++; |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | static gint cpreg_key_compare(gconstpointer a, gconstpointer b) |
| 227 | { |
| 228 | uint32_t aidx = *(uint32_t *)a; |
| 229 | uint32_t bidx = *(uint32_t *)b; |
| 230 | |
| 231 | return aidx - bidx; |
| 232 | } |
| 233 | |
Peter Maydell | 82a3a11 | 2013-07-01 12:40:19 +0100 | [diff] [blame] | 234 | static void cpreg_make_keylist(gpointer key, gpointer value, gpointer udata) |
| 235 | { |
| 236 | GList **plist = udata; |
| 237 | |
| 238 | *plist = g_list_prepend(*plist, key); |
| 239 | } |
| 240 | |
Peter Maydell | 721fae1 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 241 | void init_cpreg_list(ARMCPU *cpu) |
| 242 | { |
| 243 | /* Initialise the cpreg_tuples[] array based on the cp_regs hash. |
| 244 | * Note that we require cpreg_tuples[] to be sorted by key ID. |
| 245 | */ |
Peter Maydell | 82a3a11 | 2013-07-01 12:40:19 +0100 | [diff] [blame] | 246 | GList *keys = NULL; |
Peter Maydell | 721fae1 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 247 | int arraylen; |
| 248 | |
Peter Maydell | 82a3a11 | 2013-07-01 12:40:19 +0100 | [diff] [blame] | 249 | g_hash_table_foreach(cpu->cp_regs, cpreg_make_keylist, &keys); |
| 250 | |
Peter Maydell | 721fae1 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 251 | keys = g_list_sort(keys, cpreg_key_compare); |
| 252 | |
| 253 | cpu->cpreg_array_len = 0; |
| 254 | |
| 255 | g_list_foreach(keys, count_cpreg, cpu); |
| 256 | |
| 257 | arraylen = cpu->cpreg_array_len; |
| 258 | cpu->cpreg_indexes = g_new(uint64_t, arraylen); |
| 259 | cpu->cpreg_values = g_new(uint64_t, arraylen); |
| 260 | cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); |
| 261 | cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); |
| 262 | cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; |
| 263 | cpu->cpreg_array_len = 0; |
| 264 | |
| 265 | g_list_foreach(keys, add_cpreg_to_list, cpu); |
| 266 | |
| 267 | assert(cpu->cpreg_array_len == arraylen); |
| 268 | |
| 269 | g_list_free(keys); |
| 270 | } |
| 271 | |
Peter Maydell | c983fe6 | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 272 | static int dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
| 273 | { |
| 274 | env->cp15.c3 = value; |
| 275 | tlb_flush(env, 1); /* Flush TLB as domain not tracked in TLB */ |
| 276 | return 0; |
| 277 | } |
| 278 | |
Peter Maydell | 08de207 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 279 | static int fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
| 280 | { |
| 281 | if (env->cp15.c13_fcse != value) { |
| 282 | /* Unlike real hardware the qemu TLB uses virtual addresses, |
| 283 | * not modified virtual addresses, so this causes a TLB flush. |
| 284 | */ |
| 285 | tlb_flush(env, 1); |
| 286 | env->cp15.c13_fcse = value; |
| 287 | } |
| 288 | return 0; |
| 289 | } |
| 290 | static int contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 291 | uint64_t value) |
| 292 | { |
| 293 | if (env->cp15.c13_context != value && !arm_feature(env, ARM_FEATURE_MPU)) { |
| 294 | /* For VMSA (when not using the LPAE long descriptor page table |
| 295 | * format) this register includes the ASID, so do a TLB flush. |
| 296 | * For PMSA it is purely a process ID and no action is needed. |
| 297 | */ |
| 298 | tlb_flush(env, 1); |
| 299 | } |
| 300 | env->cp15.c13_context = value; |
| 301 | return 0; |
| 302 | } |
| 303 | |
Peter Maydell | d929823 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 304 | static int tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 305 | uint64_t value) |
| 306 | { |
| 307 | /* Invalidate all (TLBIALL) */ |
| 308 | tlb_flush(env, 1); |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 313 | uint64_t value) |
| 314 | { |
| 315 | /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ |
| 316 | tlb_flush_page(env, value & TARGET_PAGE_MASK); |
| 317 | return 0; |
| 318 | } |
| 319 | |
| 320 | static int tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 321 | uint64_t value) |
| 322 | { |
| 323 | /* Invalidate by ASID (TLBIASID) */ |
| 324 | tlb_flush(env, value == 0); |
| 325 | return 0; |
| 326 | } |
| 327 | |
| 328 | static int tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 329 | uint64_t value) |
| 330 | { |
| 331 | /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ |
| 332 | tlb_flush_page(env, value & TARGET_PAGE_MASK); |
| 333 | return 0; |
| 334 | } |
| 335 | |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 336 | static const ARMCPRegInfo cp_reginfo[] = { |
| 337 | /* DBGDIDR: just RAZ. In particular this means the "debug architecture |
| 338 | * version" bits will read as a reserved value, which should cause |
| 339 | * Linux to not try to use the debug hardware. |
| 340 | */ |
| 341 | { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 342 | .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
Peter Maydell | c983fe6 | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 343 | /* MMU Domain access control / MPU write buffer control */ |
| 344 | { .name = "DACR", .cp = 15, |
| 345 | .crn = 3, .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, |
| 346 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c3), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 347 | .resetvalue = 0, .writefn = dacr_write, .raw_writefn = raw_write, }, |
Peter Maydell | 08de207 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 348 | { .name = "FCSEIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 349 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 350 | .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, |
Peter Maydell | 08de207 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 351 | { .name = "CONTEXTIDR", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 1, |
| 352 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c13_fcse), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 353 | .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, |
Peter Maydell | 4fdd17d | 2012-06-20 11:57:15 +0000 | [diff] [blame] | 354 | /* ??? This covers not just the impdef TLB lockdown registers but also |
| 355 | * some v7VMSA registers relating to TEX remap, so it is overly broad. |
| 356 | */ |
| 357 | { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = CP_ANY, |
| 358 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, |
Peter Maydell | d929823 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 359 | /* MMU TLB control. Note that the wildcarding means we cover not just |
| 360 | * the unified TLB ops but also the dside/iside/inner-shareable variants. |
| 361 | */ |
| 362 | { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 363 | .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, |
| 364 | .type = ARM_CP_NO_MIGRATE }, |
Peter Maydell | d929823 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 365 | { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 366 | .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, |
| 367 | .type = ARM_CP_NO_MIGRATE }, |
Peter Maydell | d929823 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 368 | { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 369 | .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, |
| 370 | .type = ARM_CP_NO_MIGRATE }, |
Peter Maydell | d929823 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 371 | { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 372 | .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, |
| 373 | .type = ARM_CP_NO_MIGRATE }, |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 374 | /* Cache maintenance ops; some of this space may be overridden later. */ |
| 375 | { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, |
| 376 | .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, |
| 377 | .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 378 | REGINFO_SENTINEL |
| 379 | }; |
| 380 | |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 381 | static const ARMCPRegInfo not_v6_cp_reginfo[] = { |
| 382 | /* Not all pre-v6 cores implemented this WFI, so this is slightly |
| 383 | * over-broad. |
| 384 | */ |
| 385 | { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, |
| 386 | .access = PL1_W, .type = ARM_CP_WFI }, |
| 387 | REGINFO_SENTINEL |
| 388 | }; |
| 389 | |
| 390 | static const ARMCPRegInfo not_v7_cp_reginfo[] = { |
| 391 | /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which |
| 392 | * is UNPREDICTABLE; we choose to NOP as most implementations do). |
| 393 | */ |
| 394 | { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, |
| 395 | .access = PL1_W, .type = ARM_CP_WFI }, |
Peter Maydell | 34f9052 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 396 | /* L1 cache lockdown. Not architectural in v6 and earlier but in practice |
| 397 | * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and |
| 398 | * OMAPCP will override this space. |
| 399 | */ |
| 400 | { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 401 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), |
| 402 | .resetvalue = 0 }, |
| 403 | { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, |
| 404 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), |
| 405 | .resetvalue = 0 }, |
Peter Maydell | 776d4e5 | 2012-06-20 11:57:19 +0000 | [diff] [blame] | 406 | /* v6 doesn't have the cache ID registers but Linux reads them anyway */ |
| 407 | { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 408 | .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, |
| 409 | .resetvalue = 0 }, |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 410 | REGINFO_SENTINEL |
| 411 | }; |
| 412 | |
Peter Maydell | 2771db2 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 413 | static int cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
| 414 | { |
| 415 | if (env->cp15.c1_coproc != value) { |
| 416 | env->cp15.c1_coproc = value; |
| 417 | /* ??? Is this safe when called from within a TB? */ |
| 418 | tb_flush(env); |
| 419 | } |
| 420 | return 0; |
| 421 | } |
| 422 | |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 423 | static const ARMCPRegInfo v6_cp_reginfo[] = { |
| 424 | /* prefetch by MVA in v6, NOP in v7 */ |
| 425 | { .name = "MVA_prefetch", |
| 426 | .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, |
| 427 | .access = PL1_W, .type = ARM_CP_NOP }, |
| 428 | { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, |
| 429 | .access = PL0_W, .type = ARM_CP_NOP }, |
Peter Maydell | 091fd17 | 2012-07-12 10:58:36 +0000 | [diff] [blame] | 430 | { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 431 | .access = PL0_W, .type = ARM_CP_NOP }, |
Peter Maydell | 091fd17 | 2012-07-12 10:58:36 +0000 | [diff] [blame] | 432 | { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 433 | .access = PL0_W, .type = ARM_CP_NOP }, |
Peter Maydell | 06d76f3 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 434 | { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, |
| 435 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_insn), |
| 436 | .resetvalue = 0, }, |
| 437 | /* Watchpoint Fault Address Register : should actually only be present |
| 438 | * for 1136, 1176, 11MPCore. |
| 439 | */ |
| 440 | { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, |
| 441 | .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, |
Peter Maydell | 2771db2 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 442 | { .name = "CPACR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, |
| 443 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_coproc), |
| 444 | .resetvalue = 0, .writefn = cpacr_write }, |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 445 | REGINFO_SENTINEL |
| 446 | }; |
| 447 | |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 448 | |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 449 | static int pmreg_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 450 | uint64_t *value) |
| 451 | { |
| 452 | /* Generic performance monitor register read function for where |
| 453 | * user access may be allowed by PMUSERENR. |
| 454 | */ |
| 455 | if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) { |
| 456 | return EXCP_UDEF; |
| 457 | } |
| 458 | *value = CPREG_FIELD32(env, ri); |
| 459 | return 0; |
| 460 | } |
| 461 | |
| 462 | static int pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 463 | uint64_t value) |
| 464 | { |
| 465 | if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) { |
| 466 | return EXCP_UDEF; |
| 467 | } |
| 468 | /* only the DP, X, D and E bits are writable */ |
| 469 | env->cp15.c9_pmcr &= ~0x39; |
| 470 | env->cp15.c9_pmcr |= (value & 0x39); |
| 471 | return 0; |
| 472 | } |
| 473 | |
| 474 | static int pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 475 | uint64_t value) |
| 476 | { |
| 477 | if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) { |
| 478 | return EXCP_UDEF; |
| 479 | } |
| 480 | value &= (1 << 31); |
| 481 | env->cp15.c9_pmcnten |= value; |
| 482 | return 0; |
| 483 | } |
| 484 | |
| 485 | static int pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 486 | uint64_t value) |
| 487 | { |
| 488 | if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) { |
| 489 | return EXCP_UDEF; |
| 490 | } |
| 491 | value &= (1 << 31); |
| 492 | env->cp15.c9_pmcnten &= ~value; |
| 493 | return 0; |
| 494 | } |
| 495 | |
| 496 | static int pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 497 | uint64_t value) |
| 498 | { |
| 499 | if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) { |
| 500 | return EXCP_UDEF; |
| 501 | } |
| 502 | env->cp15.c9_pmovsr &= ~value; |
| 503 | return 0; |
| 504 | } |
| 505 | |
| 506 | static int pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 507 | uint64_t value) |
| 508 | { |
| 509 | if (arm_current_pl(env) == 0 && !env->cp15.c9_pmuserenr) { |
| 510 | return EXCP_UDEF; |
| 511 | } |
| 512 | env->cp15.c9_pmxevtyper = value & 0xff; |
| 513 | return 0; |
| 514 | } |
| 515 | |
| 516 | static int pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 517 | uint64_t value) |
| 518 | { |
| 519 | env->cp15.c9_pmuserenr = value & 1; |
| 520 | return 0; |
| 521 | } |
| 522 | |
| 523 | static int pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 524 | uint64_t value) |
| 525 | { |
| 526 | /* We have no event counters so only the C bit can be changed */ |
| 527 | value &= (1 << 31); |
| 528 | env->cp15.c9_pminten |= value; |
| 529 | return 0; |
| 530 | } |
| 531 | |
| 532 | static int pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 533 | uint64_t value) |
| 534 | { |
| 535 | value &= (1 << 31); |
| 536 | env->cp15.c9_pminten &= ~value; |
| 537 | return 0; |
| 538 | } |
| 539 | |
Peter Maydell | 776d4e5 | 2012-06-20 11:57:19 +0000 | [diff] [blame] | 540 | static int ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 541 | uint64_t *value) |
| 542 | { |
| 543 | ARMCPU *cpu = arm_env_get_cpu(env); |
| 544 | *value = cpu->ccsidr[env->cp15.c0_cssel]; |
| 545 | return 0; |
| 546 | } |
| 547 | |
| 548 | static int csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 549 | uint64_t value) |
| 550 | { |
| 551 | env->cp15.c0_cssel = value & 0xf; |
| 552 | return 0; |
| 553 | } |
| 554 | |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 555 | static const ARMCPRegInfo v7_cp_reginfo[] = { |
| 556 | /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped |
| 557 | * debug components |
| 558 | */ |
| 559 | { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 560 | .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
Peter Maydell | 091fd17 | 2012-07-12 10:58:36 +0000 | [diff] [blame] | 561 | { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 562 | .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 563 | /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ |
| 564 | { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, |
| 565 | .access = PL1_W, .type = ARM_CP_NOP }, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 566 | /* Performance monitors are implementation defined in v7, |
| 567 | * but with an ARM recommended set of registers, which we |
| 568 | * follow (although we don't actually implement any counters) |
| 569 | * |
| 570 | * Performance registers fall into three categories: |
| 571 | * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) |
| 572 | * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) |
| 573 | * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) |
| 574 | * For the cases controlled by PMUSERENR we must set .access to PL0_RW |
| 575 | * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. |
| 576 | */ |
| 577 | { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, |
| 578 | .access = PL0_RW, .resetvalue = 0, |
| 579 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 580 | .readfn = pmreg_read, .writefn = pmcntenset_write, |
| 581 | .raw_readfn = raw_read, .raw_writefn = raw_write }, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 582 | { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, |
| 583 | .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 584 | .readfn = pmreg_read, .writefn = pmcntenclr_write, |
| 585 | .type = ARM_CP_NO_MIGRATE }, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 586 | { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, |
| 587 | .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 588 | .readfn = pmreg_read, .writefn = pmovsr_write, |
| 589 | .raw_readfn = raw_read, .raw_writefn = raw_write }, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 590 | /* Unimplemented so WI. Strictly speaking write accesses in PL0 should |
| 591 | * respect PMUSERENR. |
| 592 | */ |
| 593 | { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, |
| 594 | .access = PL0_W, .type = ARM_CP_NOP }, |
| 595 | /* Since we don't implement any events, writing to PMSELR is UNPREDICTABLE. |
| 596 | * We choose to RAZ/WI. XXX should respect PMUSERENR. |
| 597 | */ |
| 598 | { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, |
| 599 | .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 600 | /* Unimplemented, RAZ/WI. XXX PMUSERENR */ |
| 601 | { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, |
| 602 | .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 603 | { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, |
| 604 | .access = PL0_RW, |
| 605 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmxevtyper), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 606 | .readfn = pmreg_read, .writefn = pmxevtyper_write, |
| 607 | .raw_readfn = raw_read, .raw_writefn = raw_write }, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 608 | /* Unimplemented, RAZ/WI. XXX PMUSERENR */ |
| 609 | { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, |
| 610 | .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 611 | { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, |
| 612 | .access = PL0_R | PL1_RW, |
| 613 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), |
| 614 | .resetvalue = 0, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 615 | .writefn = pmuserenr_write, .raw_writefn = raw_write }, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 616 | { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, |
| 617 | .access = PL1_RW, |
| 618 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), |
| 619 | .resetvalue = 0, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 620 | .writefn = pmintenset_write, .raw_writefn = raw_write }, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 621 | { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 622 | .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 623 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 624 | .resetvalue = 0, .writefn = pmintenclr_write, }, |
Peter Maydell | 2771db2 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 625 | { .name = "SCR", .cp = 15, .crn = 1, .crm = 1, .opc1 = 0, .opc2 = 0, |
| 626 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_scr), |
| 627 | .resetvalue = 0, }, |
Peter Maydell | 776d4e5 | 2012-06-20 11:57:19 +0000 | [diff] [blame] | 628 | { .name = "CCSIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 629 | .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_MIGRATE }, |
Peter Maydell | 776d4e5 | 2012-06-20 11:57:19 +0000 | [diff] [blame] | 630 | { .name = "CSSELR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, |
| 631 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c0_cssel), |
| 632 | .writefn = csselr_write, .resetvalue = 0 }, |
| 633 | /* Auxiliary ID register: this actually has an IMPDEF value but for now |
| 634 | * just RAZ for all cores: |
| 635 | */ |
| 636 | { .name = "AIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 7, |
| 637 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 638 | REGINFO_SENTINEL |
| 639 | }; |
| 640 | |
Peter Maydell | c326b97 | 2012-06-20 11:57:10 +0000 | [diff] [blame] | 641 | static int teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
| 642 | { |
| 643 | value &= 1; |
| 644 | env->teecr = value; |
| 645 | return 0; |
| 646 | } |
| 647 | |
| 648 | static int teehbr_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 649 | uint64_t *value) |
| 650 | { |
| 651 | /* This is a helper function because the user access rights |
| 652 | * depend on the value of the TEECR. |
| 653 | */ |
| 654 | if (arm_current_pl(env) == 0 && (env->teecr & 1)) { |
| 655 | return EXCP_UDEF; |
| 656 | } |
| 657 | *value = env->teehbr; |
| 658 | return 0; |
| 659 | } |
| 660 | |
| 661 | static int teehbr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 662 | uint64_t value) |
| 663 | { |
| 664 | if (arm_current_pl(env) == 0 && (env->teecr & 1)) { |
| 665 | return EXCP_UDEF; |
| 666 | } |
| 667 | env->teehbr = value; |
| 668 | return 0; |
| 669 | } |
| 670 | |
| 671 | static const ARMCPRegInfo t2ee_cp_reginfo[] = { |
| 672 | { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, |
| 673 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), |
| 674 | .resetvalue = 0, |
| 675 | .writefn = teecr_write }, |
| 676 | { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, |
| 677 | .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 678 | .resetvalue = 0, .raw_readfn = raw_read, .raw_writefn = raw_write, |
Peter Maydell | c326b97 | 2012-06-20 11:57:10 +0000 | [diff] [blame] | 679 | .readfn = teehbr_read, .writefn = teehbr_write }, |
| 680 | REGINFO_SENTINEL |
| 681 | }; |
| 682 | |
Peter Maydell | 4d31c59 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 683 | static const ARMCPRegInfo v6k_cp_reginfo[] = { |
| 684 | { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, |
| 685 | .access = PL0_RW, |
| 686 | .fieldoffset = offsetof(CPUARMState, cp15.c13_tls1), |
| 687 | .resetvalue = 0 }, |
| 688 | { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, |
| 689 | .access = PL0_R|PL1_W, |
| 690 | .fieldoffset = offsetof(CPUARMState, cp15.c13_tls2), |
| 691 | .resetvalue = 0 }, |
| 692 | { .name = "TPIDRPRW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 4, |
| 693 | .access = PL1_RW, |
| 694 | .fieldoffset = offsetof(CPUARMState, cp15.c13_tls3), |
| 695 | .resetvalue = 0 }, |
| 696 | REGINFO_SENTINEL |
| 697 | }; |
| 698 | |
Peter Maydell | 55d284a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 699 | #ifndef CONFIG_USER_ONLY |
| 700 | |
| 701 | static uint64_t gt_get_countervalue(CPUARMState *env) |
| 702 | { |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 703 | return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE; |
Peter Maydell | 55d284a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 704 | } |
| 705 | |
| 706 | static void gt_recalc_timer(ARMCPU *cpu, int timeridx) |
| 707 | { |
| 708 | ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; |
| 709 | |
| 710 | if (gt->ctl & 1) { |
| 711 | /* Timer enabled: calculate and set current ISTATUS, irq, and |
| 712 | * reset timer to when ISTATUS next has to change |
| 713 | */ |
| 714 | uint64_t count = gt_get_countervalue(&cpu->env); |
| 715 | /* Note that this must be unsigned 64 bit arithmetic: */ |
| 716 | int istatus = count >= gt->cval; |
| 717 | uint64_t nexttick; |
| 718 | |
| 719 | gt->ctl = deposit32(gt->ctl, 2, 1, istatus); |
| 720 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], |
| 721 | (istatus && !(gt->ctl & 2))); |
| 722 | if (istatus) { |
| 723 | /* Next transition is when count rolls back over to zero */ |
| 724 | nexttick = UINT64_MAX; |
| 725 | } else { |
| 726 | /* Next transition is when we hit cval */ |
| 727 | nexttick = gt->cval; |
| 728 | } |
| 729 | /* Note that the desired next expiry time might be beyond the |
| 730 | * signed-64-bit range of a QEMUTimer -- in this case we just |
| 731 | * set the timer for as far in the future as possible. When the |
| 732 | * timer expires we will reset the timer for any remaining period. |
| 733 | */ |
| 734 | if (nexttick > INT64_MAX / GTIMER_SCALE) { |
| 735 | nexttick = INT64_MAX / GTIMER_SCALE; |
| 736 | } |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 737 | timer_mod(cpu->gt_timer[timeridx], nexttick); |
Peter Maydell | 55d284a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 738 | } else { |
| 739 | /* Timer disabled: ISTATUS and timer output always clear */ |
| 740 | gt->ctl &= ~4; |
| 741 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0); |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 742 | timer_del(cpu->gt_timer[timeridx]); |
Peter Maydell | 55d284a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 743 | } |
| 744 | } |
| 745 | |
| 746 | static int gt_cntfrq_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 747 | uint64_t *value) |
| 748 | { |
| 749 | /* Not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero */ |
| 750 | if (arm_current_pl(env) == 0 && !extract32(env->cp15.c14_cntkctl, 0, 2)) { |
| 751 | return EXCP_UDEF; |
| 752 | } |
| 753 | *value = env->cp15.c14_cntfrq; |
| 754 | return 0; |
| 755 | } |
| 756 | |
| 757 | static void gt_cnt_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
| 758 | { |
| 759 | ARMCPU *cpu = arm_env_get_cpu(env); |
| 760 | int timeridx = ri->opc1 & 1; |
| 761 | |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 762 | timer_del(cpu->gt_timer[timeridx]); |
Peter Maydell | 55d284a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 763 | } |
| 764 | |
| 765 | static int gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 766 | uint64_t *value) |
| 767 | { |
| 768 | int timeridx = ri->opc1 & 1; |
| 769 | |
| 770 | if (arm_current_pl(env) == 0 && |
| 771 | !extract32(env->cp15.c14_cntkctl, timeridx, 1)) { |
| 772 | return EXCP_UDEF; |
| 773 | } |
| 774 | *value = gt_get_countervalue(env); |
| 775 | return 0; |
| 776 | } |
| 777 | |
| 778 | static int gt_cval_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 779 | uint64_t *value) |
| 780 | { |
| 781 | int timeridx = ri->opc1 & 1; |
| 782 | |
| 783 | if (arm_current_pl(env) == 0 && |
| 784 | !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { |
| 785 | return EXCP_UDEF; |
| 786 | } |
| 787 | *value = env->cp15.c14_timer[timeridx].cval; |
| 788 | return 0; |
| 789 | } |
| 790 | |
| 791 | static int gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 792 | uint64_t value) |
| 793 | { |
| 794 | int timeridx = ri->opc1 & 1; |
| 795 | |
| 796 | env->cp15.c14_timer[timeridx].cval = value; |
| 797 | gt_recalc_timer(arm_env_get_cpu(env), timeridx); |
| 798 | return 0; |
| 799 | } |
| 800 | static int gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 801 | uint64_t *value) |
| 802 | { |
| 803 | int timeridx = ri->crm & 1; |
| 804 | |
| 805 | if (arm_current_pl(env) == 0 && |
| 806 | !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { |
| 807 | return EXCP_UDEF; |
| 808 | } |
| 809 | *value = (uint32_t)(env->cp15.c14_timer[timeridx].cval - |
| 810 | gt_get_countervalue(env)); |
| 811 | return 0; |
| 812 | } |
| 813 | |
| 814 | static int gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 815 | uint64_t value) |
| 816 | { |
| 817 | int timeridx = ri->crm & 1; |
| 818 | |
| 819 | env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) + |
| 820 | + sextract64(value, 0, 32); |
| 821 | gt_recalc_timer(arm_env_get_cpu(env), timeridx); |
| 822 | return 0; |
| 823 | } |
| 824 | |
| 825 | static int gt_ctl_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 826 | uint64_t *value) |
| 827 | { |
| 828 | int timeridx = ri->crm & 1; |
| 829 | |
| 830 | if (arm_current_pl(env) == 0 && |
| 831 | !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { |
| 832 | return EXCP_UDEF; |
| 833 | } |
| 834 | *value = env->cp15.c14_timer[timeridx].ctl; |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static int gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 839 | uint64_t value) |
| 840 | { |
| 841 | ARMCPU *cpu = arm_env_get_cpu(env); |
| 842 | int timeridx = ri->crm & 1; |
| 843 | uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; |
| 844 | |
| 845 | env->cp15.c14_timer[timeridx].ctl = value & 3; |
| 846 | if ((oldval ^ value) & 1) { |
| 847 | /* Enable toggled */ |
| 848 | gt_recalc_timer(cpu, timeridx); |
| 849 | } else if ((oldval & value) & 2) { |
| 850 | /* IMASK toggled: don't need to recalculate, |
| 851 | * just set the interrupt line based on ISTATUS |
| 852 | */ |
| 853 | qemu_set_irq(cpu->gt_timer_outputs[timeridx], |
| 854 | (oldval & 4) && (value & 2)); |
| 855 | } |
| 856 | return 0; |
| 857 | } |
| 858 | |
| 859 | void arm_gt_ptimer_cb(void *opaque) |
| 860 | { |
| 861 | ARMCPU *cpu = opaque; |
| 862 | |
| 863 | gt_recalc_timer(cpu, GTIMER_PHYS); |
| 864 | } |
| 865 | |
| 866 | void arm_gt_vtimer_cb(void *opaque) |
| 867 | { |
| 868 | ARMCPU *cpu = opaque; |
| 869 | |
| 870 | gt_recalc_timer(cpu, GTIMER_VIRT); |
| 871 | } |
| 872 | |
Peter Maydell | 6cc7a3a | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 873 | static const ARMCPRegInfo generic_timer_cp_reginfo[] = { |
Peter Maydell | 55d284a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 874 | /* Note that CNTFRQ is purely reads-as-written for the benefit |
| 875 | * of software; writing it doesn't actually change the timer frequency. |
| 876 | * Our reset value matches the fixed frequency we implement the timer at. |
| 877 | */ |
| 878 | { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 879 | .access = PL1_RW | PL0_R, |
| 880 | .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), |
| 881 | .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE, |
| 882 | .readfn = gt_cntfrq_read, .raw_readfn = raw_read, |
| 883 | }, |
| 884 | /* overall control: mostly access permissions */ |
| 885 | { .name = "CNTKCTL", .cp = 15, .crn = 14, .crm = 1, .opc1 = 0, .opc2 = 0, |
| 886 | .access = PL1_RW, |
| 887 | .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), |
| 888 | .resetvalue = 0, |
| 889 | }, |
| 890 | /* per-timer control */ |
| 891 | { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, |
| 892 | .type = ARM_CP_IO, .access = PL1_RW | PL0_R, |
| 893 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), |
| 894 | .resetvalue = 0, |
| 895 | .readfn = gt_ctl_read, .writefn = gt_ctl_write, |
| 896 | .raw_readfn = raw_read, .raw_writefn = raw_write, |
| 897 | }, |
| 898 | { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, |
| 899 | .type = ARM_CP_IO, .access = PL1_RW | PL0_R, |
| 900 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), |
| 901 | .resetvalue = 0, |
| 902 | .readfn = gt_ctl_read, .writefn = gt_ctl_write, |
| 903 | .raw_readfn = raw_read, .raw_writefn = raw_write, |
| 904 | }, |
| 905 | /* TimerValue views: a 32 bit downcounting view of the underlying state */ |
| 906 | { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, |
| 907 | .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R, |
| 908 | .readfn = gt_tval_read, .writefn = gt_tval_write, |
| 909 | }, |
| 910 | { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, |
| 911 | .type = ARM_CP_NO_MIGRATE | ARM_CP_IO, .access = PL1_RW | PL0_R, |
| 912 | .readfn = gt_tval_read, .writefn = gt_tval_write, |
| 913 | }, |
| 914 | /* The counter itself */ |
| 915 | { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, |
| 916 | .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO, |
| 917 | .readfn = gt_cnt_read, .resetfn = gt_cnt_reset, |
| 918 | }, |
| 919 | { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, |
| 920 | .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_MIGRATE | ARM_CP_IO, |
| 921 | .readfn = gt_cnt_read, .resetfn = gt_cnt_reset, |
| 922 | }, |
| 923 | /* Comparison value, indicating when the timer goes off */ |
| 924 | { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, |
| 925 | .access = PL1_RW | PL0_R, |
| 926 | .type = ARM_CP_64BIT | ARM_CP_IO, |
| 927 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), |
| 928 | .resetvalue = 0, |
| 929 | .readfn = gt_cval_read, .writefn = gt_cval_write, |
| 930 | .raw_readfn = raw_read, .raw_writefn = raw_write, |
| 931 | }, |
| 932 | { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, |
| 933 | .access = PL1_RW | PL0_R, |
| 934 | .type = ARM_CP_64BIT | ARM_CP_IO, |
| 935 | .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), |
| 936 | .resetvalue = 0, |
| 937 | .readfn = gt_cval_read, .writefn = gt_cval_write, |
| 938 | .raw_readfn = raw_read, .raw_writefn = raw_write, |
| 939 | }, |
Peter Maydell | 6cc7a3a | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 940 | REGINFO_SENTINEL |
| 941 | }; |
| 942 | |
Peter Maydell | 55d284a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 943 | #else |
| 944 | /* In user-mode none of the generic timer registers are accessible, |
Alex Bligh | bc72ad6 | 2013-08-21 16:03:08 +0100 | [diff] [blame] | 945 | * and their implementation depends on QEMU_CLOCK_VIRTUAL and qdev gpio outputs, |
Peter Maydell | 55d284a | 2013-08-20 14:54:31 +0100 | [diff] [blame] | 946 | * so instead just don't register any of them. |
| 947 | */ |
| 948 | static const ARMCPRegInfo generic_timer_cp_reginfo[] = { |
| 949 | REGINFO_SENTINEL |
| 950 | }; |
| 951 | |
| 952 | #endif |
| 953 | |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 954 | static int par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
| 955 | { |
Peter Maydell | 891a2fe | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 956 | if (arm_feature(env, ARM_FEATURE_LPAE)) { |
| 957 | env->cp15.c7_par = value; |
| 958 | } else if (arm_feature(env, ARM_FEATURE_V7)) { |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 959 | env->cp15.c7_par = value & 0xfffff6ff; |
| 960 | } else { |
| 961 | env->cp15.c7_par = value & 0xfffff1ff; |
| 962 | } |
| 963 | return 0; |
| 964 | } |
| 965 | |
| 966 | #ifndef CONFIG_USER_ONLY |
| 967 | /* get_phys_addr() isn't present for user-mode-only targets */ |
Peter Maydell | 702a935 | 2012-07-12 10:59:10 +0000 | [diff] [blame] | 968 | |
| 969 | /* Return true if extended addresses are enabled, ie this is an |
| 970 | * LPAE implementation and we are using the long-descriptor translation |
| 971 | * table format because the TTBCR EAE bit is set. |
| 972 | */ |
| 973 | static inline bool extended_addresses_enabled(CPUARMState *env) |
| 974 | { |
| 975 | return arm_feature(env, ARM_FEATURE_LPAE) |
Peter Maydell | 78dbbbe | 2013-09-10 19:09:32 +0100 | [diff] [blame] | 976 | && (env->cp15.c2_control & (1U << 31)); |
Peter Maydell | 702a935 | 2012-07-12 10:59:10 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 979 | static int ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
| 980 | { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 981 | hwaddr phys_addr; |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 982 | target_ulong page_size; |
| 983 | int prot; |
| 984 | int ret, is_user = ri->opc2 & 2; |
| 985 | int access_type = ri->opc2 & 1; |
| 986 | |
| 987 | if (ri->opc2 & 4) { |
| 988 | /* Other states are only available with TrustZone */ |
| 989 | return EXCP_UDEF; |
| 990 | } |
| 991 | ret = get_phys_addr(env, value, access_type, is_user, |
| 992 | &phys_addr, &prot, &page_size); |
Peter Maydell | 702a935 | 2012-07-12 10:59:10 +0000 | [diff] [blame] | 993 | if (extended_addresses_enabled(env)) { |
| 994 | /* ret is a DFSR/IFSR value for the long descriptor |
| 995 | * translation table format, but with WnR always clear. |
| 996 | * Convert it to a 64-bit PAR. |
| 997 | */ |
| 998 | uint64_t par64 = (1 << 11); /* LPAE bit always set */ |
| 999 | if (ret == 0) { |
| 1000 | par64 |= phys_addr & ~0xfffULL; |
| 1001 | /* We don't set the ATTR or SH fields in the PAR. */ |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 1002 | } else { |
Peter Maydell | 702a935 | 2012-07-12 10:59:10 +0000 | [diff] [blame] | 1003 | par64 |= 1; /* F */ |
| 1004 | par64 |= (ret & 0x3f) << 1; /* FS */ |
| 1005 | /* Note that S2WLK and FSTAGE are always zero, because we don't |
| 1006 | * implement virtualization and therefore there can't be a stage 2 |
| 1007 | * fault. |
| 1008 | */ |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 1009 | } |
Peter Maydell | 702a935 | 2012-07-12 10:59:10 +0000 | [diff] [blame] | 1010 | env->cp15.c7_par = par64; |
| 1011 | env->cp15.c7_par_hi = par64 >> 32; |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 1012 | } else { |
Peter Maydell | 702a935 | 2012-07-12 10:59:10 +0000 | [diff] [blame] | 1013 | /* ret is a DFSR/IFSR value for the short descriptor |
| 1014 | * translation table format (with WnR always clear). |
| 1015 | * Convert it to a 32-bit PAR. |
| 1016 | */ |
| 1017 | if (ret == 0) { |
| 1018 | /* We do not set any attribute bits in the PAR */ |
| 1019 | if (page_size == (1 << 24) |
| 1020 | && arm_feature(env, ARM_FEATURE_V7)) { |
| 1021 | env->cp15.c7_par = (phys_addr & 0xff000000) | 1 << 1; |
| 1022 | } else { |
| 1023 | env->cp15.c7_par = phys_addr & 0xfffff000; |
| 1024 | } |
| 1025 | } else { |
| 1026 | env->cp15.c7_par = ((ret & (10 << 1)) >> 5) | |
| 1027 | ((ret & (12 << 1)) >> 6) | |
| 1028 | ((ret & 0xf) << 1) | 1; |
| 1029 | } |
| 1030 | env->cp15.c7_par_hi = 0; |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 1031 | } |
| 1032 | return 0; |
| 1033 | } |
| 1034 | #endif |
| 1035 | |
| 1036 | static const ARMCPRegInfo vapa_cp_reginfo[] = { |
| 1037 | { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, |
| 1038 | .access = PL1_RW, .resetvalue = 0, |
| 1039 | .fieldoffset = offsetof(CPUARMState, cp15.c7_par), |
| 1040 | .writefn = par_write }, |
| 1041 | #ifndef CONFIG_USER_ONLY |
| 1042 | { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1043 | .access = PL1_W, .writefn = ats_write, .type = ARM_CP_NO_MIGRATE }, |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 1044 | #endif |
| 1045 | REGINFO_SENTINEL |
| 1046 | }; |
| 1047 | |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1048 | /* Return basic MPU access permission bits. */ |
| 1049 | static uint32_t simple_mpu_ap_bits(uint32_t val) |
| 1050 | { |
| 1051 | uint32_t ret; |
| 1052 | uint32_t mask; |
| 1053 | int i; |
| 1054 | ret = 0; |
| 1055 | mask = 3; |
| 1056 | for (i = 0; i < 16; i += 2) { |
| 1057 | ret |= (val >> i) & mask; |
| 1058 | mask <<= 2; |
| 1059 | } |
| 1060 | return ret; |
| 1061 | } |
| 1062 | |
| 1063 | /* Pad basic MPU access permission bits to extended format. */ |
| 1064 | static uint32_t extended_mpu_ap_bits(uint32_t val) |
| 1065 | { |
| 1066 | uint32_t ret; |
| 1067 | uint32_t mask; |
| 1068 | int i; |
| 1069 | ret = 0; |
| 1070 | mask = 3; |
| 1071 | for (i = 0; i < 16; i += 2) { |
| 1072 | ret |= (val & mask) << i; |
| 1073 | mask <<= 2; |
| 1074 | } |
| 1075 | return ret; |
| 1076 | } |
| 1077 | |
| 1078 | static int pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1079 | uint64_t value) |
| 1080 | { |
| 1081 | env->cp15.c5_data = extended_mpu_ap_bits(value); |
| 1082 | return 0; |
| 1083 | } |
| 1084 | |
| 1085 | static int pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1086 | uint64_t *value) |
| 1087 | { |
| 1088 | *value = simple_mpu_ap_bits(env->cp15.c5_data); |
| 1089 | return 0; |
| 1090 | } |
| 1091 | |
| 1092 | static int pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1093 | uint64_t value) |
| 1094 | { |
| 1095 | env->cp15.c5_insn = extended_mpu_ap_bits(value); |
| 1096 | return 0; |
| 1097 | } |
| 1098 | |
| 1099 | static int pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1100 | uint64_t *value) |
| 1101 | { |
| 1102 | *value = simple_mpu_ap_bits(env->cp15.c5_insn); |
| 1103 | return 0; |
| 1104 | } |
| 1105 | |
Peter Maydell | 06d76f3 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1106 | static int arm946_prbs_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1107 | uint64_t *value) |
| 1108 | { |
Stefan Weil | 599d64f | 2012-09-04 07:35:57 +0200 | [diff] [blame] | 1109 | if (ri->crm >= 8) { |
Peter Maydell | 06d76f3 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1110 | return EXCP_UDEF; |
| 1111 | } |
| 1112 | *value = env->cp15.c6_region[ri->crm]; |
| 1113 | return 0; |
| 1114 | } |
| 1115 | |
| 1116 | static int arm946_prbs_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1117 | uint64_t value) |
| 1118 | { |
Stefan Weil | 599d64f | 2012-09-04 07:35:57 +0200 | [diff] [blame] | 1119 | if (ri->crm >= 8) { |
Peter Maydell | 06d76f3 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1120 | return EXCP_UDEF; |
| 1121 | } |
| 1122 | env->cp15.c6_region[ri->crm] = value; |
| 1123 | return 0; |
| 1124 | } |
| 1125 | |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1126 | static const ARMCPRegInfo pmsav5_cp_reginfo[] = { |
| 1127 | { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1128 | .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1129 | .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, |
| 1130 | .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, |
| 1131 | { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1132 | .access = PL1_RW, .type = ARM_CP_NO_MIGRATE, |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1133 | .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, |
| 1134 | .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, |
| 1135 | { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, |
| 1136 | .access = PL1_RW, |
| 1137 | .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, }, |
| 1138 | { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, |
| 1139 | .access = PL1_RW, |
| 1140 | .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, }, |
Peter Maydell | ecce5c3 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 1141 | { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 1142 | .access = PL1_RW, |
| 1143 | .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, |
| 1144 | { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, |
| 1145 | .access = PL1_RW, |
| 1146 | .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, |
Peter Maydell | 06d76f3 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1147 | /* Protection region base and size registers */ |
| 1148 | { .name = "946_PRBS", .cp = 15, .crn = 6, .crm = CP_ANY, .opc1 = 0, |
| 1149 | .opc2 = CP_ANY, .access = PL1_RW, |
| 1150 | .readfn = arm946_prbs_read, .writefn = arm946_prbs_write, }, |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1151 | REGINFO_SENTINEL |
| 1152 | }; |
| 1153 | |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1154 | static int vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1155 | uint64_t value) |
Peter Maydell | ecce5c3 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 1156 | { |
Peter Maydell | 2ebcebe | 2013-06-27 16:38:47 +0100 | [diff] [blame] | 1157 | int maskshift = extract32(value, 0, 3); |
| 1158 | |
Peter Maydell | e42c4db | 2012-07-12 10:59:11 +0000 | [diff] [blame] | 1159 | if (arm_feature(env, ARM_FEATURE_LPAE)) { |
| 1160 | value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); |
Peter Maydell | e42c4db | 2012-07-12 10:59:11 +0000 | [diff] [blame] | 1161 | } else { |
| 1162 | value &= 7; |
| 1163 | } |
| 1164 | /* Note that we always calculate c2_mask and c2_base_mask, but |
| 1165 | * they are only used for short-descriptor tables (ie if EAE is 0); |
| 1166 | * for long-descriptor tables the TTBCR fields are used differently |
| 1167 | * and the c2_mask and c2_base_mask values are meaningless. |
| 1168 | */ |
Peter Maydell | ecce5c3 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 1169 | env->cp15.c2_control = value; |
Peter Maydell | 2ebcebe | 2013-06-27 16:38:47 +0100 | [diff] [blame] | 1170 | env->cp15.c2_mask = ~(((uint32_t)0xffffffffu) >> maskshift); |
| 1171 | env->cp15.c2_base_mask = ~((uint32_t)0x3fffu >> maskshift); |
Peter Maydell | ecce5c3 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 1172 | return 0; |
| 1173 | } |
| 1174 | |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1175 | static int vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1176 | uint64_t value) |
| 1177 | { |
| 1178 | if (arm_feature(env, ARM_FEATURE_LPAE)) { |
| 1179 | /* With LPAE the TTBCR could result in a change of ASID |
| 1180 | * via the TTBCR.A1 bit, so do a TLB flush. |
| 1181 | */ |
| 1182 | tlb_flush(env, 1); |
| 1183 | } |
| 1184 | return vmsa_ttbcr_raw_write(env, ri, value); |
| 1185 | } |
| 1186 | |
Peter Maydell | ecce5c3 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 1187 | static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
| 1188 | { |
| 1189 | env->cp15.c2_base_mask = 0xffffc000u; |
| 1190 | env->cp15.c2_control = 0; |
| 1191 | env->cp15.c2_mask = 0; |
| 1192 | } |
| 1193 | |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1194 | static const ARMCPRegInfo vmsa_cp_reginfo[] = { |
| 1195 | { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 1196 | .access = PL1_RW, |
| 1197 | .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, }, |
| 1198 | { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, |
| 1199 | .access = PL1_RW, |
| 1200 | .fieldoffset = offsetof(CPUARMState, cp15.c5_insn), .resetvalue = 0, }, |
Peter Maydell | ecce5c3 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 1201 | { .name = "TTBR0", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 1202 | .access = PL1_RW, |
| 1203 | .fieldoffset = offsetof(CPUARMState, cp15.c2_base0), .resetvalue = 0, }, |
| 1204 | { .name = "TTBR1", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, |
| 1205 | .access = PL1_RW, |
Peter Maydell | 81a60ad | 2012-07-12 10:58:36 +0000 | [diff] [blame] | 1206 | .fieldoffset = offsetof(CPUARMState, cp15.c2_base1), .resetvalue = 0, }, |
Peter Maydell | ecce5c3 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 1207 | { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, |
| 1208 | .access = PL1_RW, .writefn = vmsa_ttbcr_write, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1209 | .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write, |
Peter Maydell | ecce5c3 | 2012-06-20 11:57:14 +0000 | [diff] [blame] | 1210 | .fieldoffset = offsetof(CPUARMState, cp15.c2_control) }, |
Peter Maydell | 06d76f3 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1211 | { .name = "DFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 1212 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c6_data), |
| 1213 | .resetvalue = 0, }, |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1214 | REGINFO_SENTINEL |
| 1215 | }; |
| 1216 | |
Peter Maydell | 1047b9d | 2012-06-20 11:57:15 +0000 | [diff] [blame] | 1217 | static int omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1218 | uint64_t value) |
| 1219 | { |
| 1220 | env->cp15.c15_ticonfig = value & 0xe7; |
| 1221 | /* The OS_TYPE bit in this register changes the reported CPUID! */ |
| 1222 | env->cp15.c0_cpuid = (value & (1 << 5)) ? |
| 1223 | ARM_CPUID_TI915T : ARM_CPUID_TI925T; |
| 1224 | return 0; |
| 1225 | } |
| 1226 | |
| 1227 | static int omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1228 | uint64_t value) |
| 1229 | { |
| 1230 | env->cp15.c15_threadid = value & 0xffff; |
| 1231 | return 0; |
| 1232 | } |
| 1233 | |
| 1234 | static int omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1235 | uint64_t value) |
| 1236 | { |
| 1237 | /* Wait-for-interrupt (deprecated) */ |
Andreas Färber | c3affe5 | 2013-01-18 15:03:43 +0100 | [diff] [blame] | 1238 | cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT); |
Peter Maydell | 1047b9d | 2012-06-20 11:57:15 +0000 | [diff] [blame] | 1239 | return 0; |
| 1240 | } |
| 1241 | |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1242 | static int omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1243 | uint64_t value) |
| 1244 | { |
| 1245 | /* On OMAP there are registers indicating the max/min index of dcache lines |
| 1246 | * containing a dirty line; cache flush operations have to reset these. |
| 1247 | */ |
| 1248 | env->cp15.c15_i_max = 0x000; |
| 1249 | env->cp15.c15_i_min = 0xff0; |
| 1250 | return 0; |
| 1251 | } |
| 1252 | |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1253 | static const ARMCPRegInfo omap_cp_reginfo[] = { |
| 1254 | { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, |
| 1255 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, |
| 1256 | .fieldoffset = offsetof(CPUARMState, cp15.c5_data), .resetvalue = 0, }, |
Peter Maydell | 1047b9d | 2012-06-20 11:57:15 +0000 | [diff] [blame] | 1257 | { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 1258 | .access = PL1_RW, .type = ARM_CP_NOP }, |
| 1259 | { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, |
| 1260 | .access = PL1_RW, |
| 1261 | .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, |
| 1262 | .writefn = omap_ticonfig_write }, |
| 1263 | { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, |
| 1264 | .access = PL1_RW, |
| 1265 | .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, |
| 1266 | { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, |
| 1267 | .access = PL1_RW, .resetvalue = 0xff0, |
| 1268 | .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, |
| 1269 | { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, |
| 1270 | .access = PL1_RW, |
| 1271 | .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, |
| 1272 | .writefn = omap_threadid_write }, |
| 1273 | { .name = "TI925T_STATUS", .cp = 15, .crn = 15, |
| 1274 | .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1275 | .type = ARM_CP_NO_MIGRATE, |
Peter Maydell | 1047b9d | 2012-06-20 11:57:15 +0000 | [diff] [blame] | 1276 | .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, |
| 1277 | /* TODO: Peripheral port remap register: |
| 1278 | * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller |
| 1279 | * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), |
| 1280 | * when MMU is off. |
| 1281 | */ |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1282 | { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1283 | .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, |
| 1284 | .type = ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE, |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1285 | .writefn = omap_cachemaint_write }, |
Peter Maydell | 34f9052 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 1286 | { .name = "C9", .cp = 15, .crn = 9, |
| 1287 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, |
| 1288 | .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, |
Peter Maydell | 1047b9d | 2012-06-20 11:57:15 +0000 | [diff] [blame] | 1289 | REGINFO_SENTINEL |
| 1290 | }; |
| 1291 | |
| 1292 | static int xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1293 | uint64_t value) |
| 1294 | { |
| 1295 | value &= 0x3fff; |
| 1296 | if (env->cp15.c15_cpar != value) { |
| 1297 | /* Changes cp0 to cp13 behavior, so needs a TB flush. */ |
| 1298 | tb_flush(env); |
| 1299 | env->cp15.c15_cpar = value; |
| 1300 | } |
| 1301 | return 0; |
| 1302 | } |
| 1303 | |
| 1304 | static const ARMCPRegInfo xscale_cp_reginfo[] = { |
| 1305 | { .name = "XSCALE_CPAR", |
| 1306 | .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, |
| 1307 | .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, |
| 1308 | .writefn = xscale_cpar_write, }, |
Peter Maydell | 2771db2 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 1309 | { .name = "XSCALE_AUXCR", |
| 1310 | .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, |
| 1311 | .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), |
| 1312 | .resetvalue = 0, }, |
Peter Maydell | 1047b9d | 2012-06-20 11:57:15 +0000 | [diff] [blame] | 1313 | REGINFO_SENTINEL |
| 1314 | }; |
| 1315 | |
| 1316 | static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { |
| 1317 | /* RAZ/WI the whole crn=15 space, when we don't have a more specific |
| 1318 | * implementation of this implementation-defined space. |
| 1319 | * Ideally this should eventually disappear in favour of actually |
| 1320 | * implementing the correct behaviour for all cores. |
| 1321 | */ |
| 1322 | { .name = "C15_IMPDEF", .cp = 15, .crn = 15, |
| 1323 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1324 | .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, |
| 1325 | .resetvalue = 0 }, |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1326 | REGINFO_SENTINEL |
| 1327 | }; |
| 1328 | |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1329 | static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { |
| 1330 | /* Cache status: RAZ because we have no cache so it's always clean */ |
| 1331 | { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1332 | .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, |
| 1333 | .resetvalue = 0 }, |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1334 | REGINFO_SENTINEL |
| 1335 | }; |
| 1336 | |
| 1337 | static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { |
| 1338 | /* We never have a a block transfer operation in progress */ |
| 1339 | { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1340 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, |
| 1341 | .resetvalue = 0 }, |
Peter Maydell | 30b05bb | 2012-06-20 11:57:22 +0000 | [diff] [blame] | 1342 | /* The cache ops themselves: these all NOP for QEMU */ |
| 1343 | { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, |
| 1344 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, |
| 1345 | { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, |
| 1346 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, |
| 1347 | { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, |
| 1348 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, |
| 1349 | { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, |
| 1350 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, |
| 1351 | { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, |
| 1352 | .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, |
| 1353 | { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, |
| 1354 | .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1355 | REGINFO_SENTINEL |
| 1356 | }; |
| 1357 | |
| 1358 | static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { |
| 1359 | /* The cache test-and-clean instructions always return (1 << 30) |
| 1360 | * to indicate that there are no dirty cache lines. |
| 1361 | */ |
| 1362 | { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1363 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, |
| 1364 | .resetvalue = (1 << 30) }, |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1365 | { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1366 | .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_MIGRATE, |
| 1367 | .resetvalue = (1 << 30) }, |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1368 | REGINFO_SENTINEL |
| 1369 | }; |
| 1370 | |
Peter Maydell | 34f9052 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 1371 | static const ARMCPRegInfo strongarm_cp_reginfo[] = { |
| 1372 | /* Ignore ReadBuffer accesses */ |
| 1373 | { .name = "C9_READBUFFER", .cp = 15, .crn = 9, |
| 1374 | .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1375 | .access = PL1_RW, .resetvalue = 0, |
| 1376 | .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_MIGRATE }, |
Peter Maydell | 34f9052 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 1377 | REGINFO_SENTINEL |
| 1378 | }; |
| 1379 | |
Peter Maydell | 81bdde9 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1380 | static int mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1381 | uint64_t *value) |
| 1382 | { |
Andreas Färber | 55e5c28 | 2012-12-17 06:18:02 +0100 | [diff] [blame] | 1383 | CPUState *cs = CPU(arm_env_get_cpu(env)); |
| 1384 | uint32_t mpidr = cs->cpu_index; |
Peter Maydell | 81bdde9 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1385 | /* We don't support setting cluster ID ([8..11]) |
| 1386 | * so these bits always RAZ. |
| 1387 | */ |
| 1388 | if (arm_feature(env, ARM_FEATURE_V7MP)) { |
Peter Maydell | 78dbbbe | 2013-09-10 19:09:32 +0100 | [diff] [blame] | 1389 | mpidr |= (1U << 31); |
Peter Maydell | 81bdde9 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1390 | /* Cores which are uniprocessor (non-coherent) |
| 1391 | * but still implement the MP extensions set |
| 1392 | * bit 30. (For instance, A9UP.) However we do |
| 1393 | * not currently model any of those cores. |
| 1394 | */ |
| 1395 | } |
| 1396 | *value = mpidr; |
| 1397 | return 0; |
| 1398 | } |
| 1399 | |
| 1400 | static const ARMCPRegInfo mpidr_cp_reginfo[] = { |
| 1401 | { .name = "MPIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1402 | .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_MIGRATE }, |
Peter Maydell | 81bdde9 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1403 | REGINFO_SENTINEL |
| 1404 | }; |
| 1405 | |
Peter Maydell | 891a2fe | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 1406 | static int par64_read(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value) |
| 1407 | { |
| 1408 | *value = ((uint64_t)env->cp15.c7_par_hi << 32) | env->cp15.c7_par; |
| 1409 | return 0; |
| 1410 | } |
| 1411 | |
| 1412 | static int par64_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
| 1413 | { |
| 1414 | env->cp15.c7_par_hi = value >> 32; |
| 1415 | env->cp15.c7_par = value; |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
| 1419 | static void par64_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
| 1420 | { |
| 1421 | env->cp15.c7_par_hi = 0; |
| 1422 | env->cp15.c7_par = 0; |
| 1423 | } |
| 1424 | |
| 1425 | static int ttbr064_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1426 | uint64_t *value) |
| 1427 | { |
| 1428 | *value = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0; |
| 1429 | return 0; |
| 1430 | } |
| 1431 | |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1432 | static int ttbr064_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1433 | uint64_t value) |
Peter Maydell | 891a2fe | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 1434 | { |
| 1435 | env->cp15.c2_base0_hi = value >> 32; |
| 1436 | env->cp15.c2_base0 = value; |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1437 | return 0; |
| 1438 | } |
| 1439 | |
| 1440 | static int ttbr064_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1441 | uint64_t value) |
| 1442 | { |
Peter Maydell | 891a2fe | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 1443 | /* Writes to the 64 bit format TTBRs may change the ASID */ |
| 1444 | tlb_flush(env, 1); |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1445 | return ttbr064_raw_write(env, ri, value); |
Peter Maydell | 891a2fe | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 1446 | } |
| 1447 | |
| 1448 | static void ttbr064_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
| 1449 | { |
| 1450 | env->cp15.c2_base0_hi = 0; |
| 1451 | env->cp15.c2_base0 = 0; |
| 1452 | } |
| 1453 | |
| 1454 | static int ttbr164_read(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1455 | uint64_t *value) |
| 1456 | { |
| 1457 | *value = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1; |
| 1458 | return 0; |
| 1459 | } |
| 1460 | |
| 1461 | static int ttbr164_write(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1462 | uint64_t value) |
| 1463 | { |
| 1464 | env->cp15.c2_base1_hi = value >> 32; |
| 1465 | env->cp15.c2_base1 = value; |
| 1466 | return 0; |
| 1467 | } |
| 1468 | |
| 1469 | static void ttbr164_reset(CPUARMState *env, const ARMCPRegInfo *ri) |
| 1470 | { |
| 1471 | env->cp15.c2_base1_hi = 0; |
| 1472 | env->cp15.c2_base1 = 0; |
| 1473 | } |
| 1474 | |
Peter Maydell | 7ac681c | 2012-07-12 10:59:07 +0000 | [diff] [blame] | 1475 | static const ARMCPRegInfo lpae_cp_reginfo[] = { |
Peter Maydell | b90372a | 2012-08-06 17:42:18 +0100 | [diff] [blame] | 1476 | /* NOP AMAIR0/1: the override is because these clash with the rather |
Peter Maydell | 7ac681c | 2012-07-12 10:59:07 +0000 | [diff] [blame] | 1477 | * broadly specified TLB_LOCKDOWN entry in the generic cp_reginfo. |
| 1478 | */ |
| 1479 | { .name = "AMAIR0", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, |
| 1480 | .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE, |
| 1481 | .resetvalue = 0 }, |
| 1482 | { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, |
| 1483 | .access = PL1_RW, .type = ARM_CP_CONST | ARM_CP_OVERRIDE, |
| 1484 | .resetvalue = 0 }, |
Peter Maydell | f9fc619 | 2012-07-12 10:59:08 +0000 | [diff] [blame] | 1485 | /* 64 bit access versions of the (dummy) debug registers */ |
| 1486 | { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0, |
| 1487 | .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, |
| 1488 | { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0, |
| 1489 | .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, |
Peter Maydell | 891a2fe | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 1490 | { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, |
| 1491 | .access = PL1_RW, .type = ARM_CP_64BIT, |
| 1492 | .readfn = par64_read, .writefn = par64_write, .resetfn = par64_reset }, |
| 1493 | { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, |
| 1494 | .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr064_read, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1495 | .writefn = ttbr064_write, .raw_writefn = ttbr064_raw_write, |
| 1496 | .resetfn = ttbr064_reset }, |
Peter Maydell | 891a2fe | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 1497 | { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, |
| 1498 | .access = PL1_RW, .type = ARM_CP_64BIT, .readfn = ttbr164_read, |
| 1499 | .writefn = ttbr164_write, .resetfn = ttbr164_reset }, |
Peter Maydell | 7ac681c | 2012-07-12 10:59:07 +0000 | [diff] [blame] | 1500 | REGINFO_SENTINEL |
| 1501 | }; |
| 1502 | |
Peter Maydell | 2771db2 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 1503 | static int sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) |
| 1504 | { |
| 1505 | env->cp15.c1_sys = value; |
| 1506 | /* ??? Lots of these bits are not implemented. */ |
| 1507 | /* This may enable/disable the MMU, so do a TLB flush. */ |
| 1508 | tlb_flush(env, 1); |
| 1509 | return 0; |
| 1510 | } |
| 1511 | |
Peter Maydell | 2ceb98c | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 1512 | void register_cp_regs_for_features(ARMCPU *cpu) |
| 1513 | { |
| 1514 | /* Register all the coprocessor registers based on feature bits */ |
| 1515 | CPUARMState *env = &cpu->env; |
| 1516 | if (arm_feature(env, ARM_FEATURE_M)) { |
| 1517 | /* M profile has no coprocessor registers */ |
| 1518 | return; |
| 1519 | } |
| 1520 | |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 1521 | define_arm_cp_regs(cpu, cp_reginfo); |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 1522 | if (arm_feature(env, ARM_FEATURE_V6)) { |
Peter Maydell | 8515a09 | 2012-06-20 11:57:19 +0000 | [diff] [blame] | 1523 | /* The ID registers all have impdef reset values */ |
| 1524 | ARMCPRegInfo v6_idregs[] = { |
| 1525 | { .name = "ID_PFR0", .cp = 15, .crn = 0, .crm = 1, |
| 1526 | .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST, |
| 1527 | .resetvalue = cpu->id_pfr0 }, |
| 1528 | { .name = "ID_PFR1", .cp = 15, .crn = 0, .crm = 1, |
| 1529 | .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST, |
| 1530 | .resetvalue = cpu->id_pfr1 }, |
| 1531 | { .name = "ID_DFR0", .cp = 15, .crn = 0, .crm = 1, |
| 1532 | .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST, |
| 1533 | .resetvalue = cpu->id_dfr0 }, |
| 1534 | { .name = "ID_AFR0", .cp = 15, .crn = 0, .crm = 1, |
| 1535 | .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST, |
| 1536 | .resetvalue = cpu->id_afr0 }, |
| 1537 | { .name = "ID_MMFR0", .cp = 15, .crn = 0, .crm = 1, |
| 1538 | .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST, |
| 1539 | .resetvalue = cpu->id_mmfr0 }, |
| 1540 | { .name = "ID_MMFR1", .cp = 15, .crn = 0, .crm = 1, |
| 1541 | .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST, |
| 1542 | .resetvalue = cpu->id_mmfr1 }, |
| 1543 | { .name = "ID_MMFR2", .cp = 15, .crn = 0, .crm = 1, |
| 1544 | .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST, |
| 1545 | .resetvalue = cpu->id_mmfr2 }, |
| 1546 | { .name = "ID_MMFR3", .cp = 15, .crn = 0, .crm = 1, |
| 1547 | .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST, |
| 1548 | .resetvalue = cpu->id_mmfr3 }, |
| 1549 | { .name = "ID_ISAR0", .cp = 15, .crn = 0, .crm = 2, |
| 1550 | .opc1 = 0, .opc2 = 0, .access = PL1_R, .type = ARM_CP_CONST, |
| 1551 | .resetvalue = cpu->id_isar0 }, |
| 1552 | { .name = "ID_ISAR1", .cp = 15, .crn = 0, .crm = 2, |
| 1553 | .opc1 = 0, .opc2 = 1, .access = PL1_R, .type = ARM_CP_CONST, |
| 1554 | .resetvalue = cpu->id_isar1 }, |
| 1555 | { .name = "ID_ISAR2", .cp = 15, .crn = 0, .crm = 2, |
| 1556 | .opc1 = 0, .opc2 = 2, .access = PL1_R, .type = ARM_CP_CONST, |
| 1557 | .resetvalue = cpu->id_isar2 }, |
| 1558 | { .name = "ID_ISAR3", .cp = 15, .crn = 0, .crm = 2, |
| 1559 | .opc1 = 0, .opc2 = 3, .access = PL1_R, .type = ARM_CP_CONST, |
| 1560 | .resetvalue = cpu->id_isar3 }, |
| 1561 | { .name = "ID_ISAR4", .cp = 15, .crn = 0, .crm = 2, |
| 1562 | .opc1 = 0, .opc2 = 4, .access = PL1_R, .type = ARM_CP_CONST, |
| 1563 | .resetvalue = cpu->id_isar4 }, |
| 1564 | { .name = "ID_ISAR5", .cp = 15, .crn = 0, .crm = 2, |
| 1565 | .opc1 = 0, .opc2 = 5, .access = PL1_R, .type = ARM_CP_CONST, |
| 1566 | .resetvalue = cpu->id_isar5 }, |
| 1567 | /* 6..7 are as yet unallocated and must RAZ */ |
| 1568 | { .name = "ID_ISAR6", .cp = 15, .crn = 0, .crm = 2, |
| 1569 | .opc1 = 0, .opc2 = 6, .access = PL1_R, .type = ARM_CP_CONST, |
| 1570 | .resetvalue = 0 }, |
| 1571 | { .name = "ID_ISAR7", .cp = 15, .crn = 0, .crm = 2, |
| 1572 | .opc1 = 0, .opc2 = 7, .access = PL1_R, .type = ARM_CP_CONST, |
| 1573 | .resetvalue = 0 }, |
| 1574 | REGINFO_SENTINEL |
| 1575 | }; |
| 1576 | define_arm_cp_regs(cpu, v6_idregs); |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 1577 | define_arm_cp_regs(cpu, v6_cp_reginfo); |
| 1578 | } else { |
| 1579 | define_arm_cp_regs(cpu, not_v6_cp_reginfo); |
| 1580 | } |
Peter Maydell | 4d31c59 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 1581 | if (arm_feature(env, ARM_FEATURE_V6K)) { |
| 1582 | define_arm_cp_regs(cpu, v6k_cp_reginfo); |
| 1583 | } |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 1584 | if (arm_feature(env, ARM_FEATURE_V7)) { |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 1585 | /* v7 performance monitor control register: same implementor |
| 1586 | * field as main ID register, and we implement no event counters. |
| 1587 | */ |
| 1588 | ARMCPRegInfo pmcr = { |
| 1589 | .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, |
| 1590 | .access = PL0_RW, .resetvalue = cpu->midr & 0xff000000, |
| 1591 | .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1592 | .readfn = pmreg_read, .writefn = pmcr_write, |
| 1593 | .raw_readfn = raw_read, .raw_writefn = raw_write, |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 1594 | }; |
Peter Maydell | 776d4e5 | 2012-06-20 11:57:19 +0000 | [diff] [blame] | 1595 | ARMCPRegInfo clidr = { |
| 1596 | .name = "CLIDR", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, |
| 1597 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr |
| 1598 | }; |
Peter Maydell | 200ac0e | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 1599 | define_one_arm_cp_reg(cpu, &pmcr); |
Peter Maydell | 776d4e5 | 2012-06-20 11:57:19 +0000 | [diff] [blame] | 1600 | define_one_arm_cp_reg(cpu, &clidr); |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 1601 | define_arm_cp_regs(cpu, v7_cp_reginfo); |
Peter Maydell | 7d57f40 | 2012-06-20 11:57:11 +0000 | [diff] [blame] | 1602 | } else { |
| 1603 | define_arm_cp_regs(cpu, not_v7_cp_reginfo); |
Peter Maydell | e9aa6c2 | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 1604 | } |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1605 | if (arm_feature(env, ARM_FEATURE_MPU)) { |
| 1606 | /* These are the MPU registers prior to PMSAv6. Any new |
| 1607 | * PMSA core later than the ARM946 will require that we |
| 1608 | * implement the PMSAv6 or PMSAv7 registers, which are |
| 1609 | * completely different. |
| 1610 | */ |
| 1611 | assert(!arm_feature(env, ARM_FEATURE_V6)); |
| 1612 | define_arm_cp_regs(cpu, pmsav5_cp_reginfo); |
| 1613 | } else { |
| 1614 | define_arm_cp_regs(cpu, vmsa_cp_reginfo); |
| 1615 | } |
Peter Maydell | c326b97 | 2012-06-20 11:57:10 +0000 | [diff] [blame] | 1616 | if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { |
| 1617 | define_arm_cp_regs(cpu, t2ee_cp_reginfo); |
| 1618 | } |
Peter Maydell | 6cc7a3a | 2012-06-20 11:57:12 +0000 | [diff] [blame] | 1619 | if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { |
| 1620 | define_arm_cp_regs(cpu, generic_timer_cp_reginfo); |
| 1621 | } |
Peter Maydell | 4a50160 | 2012-06-20 11:57:16 +0000 | [diff] [blame] | 1622 | if (arm_feature(env, ARM_FEATURE_VAPA)) { |
| 1623 | define_arm_cp_regs(cpu, vapa_cp_reginfo); |
| 1624 | } |
Peter Maydell | c480421 | 2012-06-20 11:57:17 +0000 | [diff] [blame] | 1625 | if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { |
| 1626 | define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); |
| 1627 | } |
| 1628 | if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { |
| 1629 | define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); |
| 1630 | } |
| 1631 | if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { |
| 1632 | define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); |
| 1633 | } |
Peter Maydell | 18032be | 2012-06-20 11:57:13 +0000 | [diff] [blame] | 1634 | if (arm_feature(env, ARM_FEATURE_OMAPCP)) { |
| 1635 | define_arm_cp_regs(cpu, omap_cp_reginfo); |
| 1636 | } |
Peter Maydell | 34f9052 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 1637 | if (arm_feature(env, ARM_FEATURE_STRONGARM)) { |
| 1638 | define_arm_cp_regs(cpu, strongarm_cp_reginfo); |
| 1639 | } |
Peter Maydell | 1047b9d | 2012-06-20 11:57:15 +0000 | [diff] [blame] | 1640 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { |
| 1641 | define_arm_cp_regs(cpu, xscale_cp_reginfo); |
| 1642 | } |
| 1643 | if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { |
| 1644 | define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); |
| 1645 | } |
Peter Maydell | 7ac681c | 2012-07-12 10:59:07 +0000 | [diff] [blame] | 1646 | if (arm_feature(env, ARM_FEATURE_LPAE)) { |
| 1647 | define_arm_cp_regs(cpu, lpae_cp_reginfo); |
| 1648 | } |
Peter Maydell | 7884849 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1649 | /* Slightly awkwardly, the OMAP and StrongARM cores need all of |
| 1650 | * cp15 crn=0 to be writes-ignored, whereas for other cores they should |
| 1651 | * be read-only (ie write causes UNDEF exception). |
| 1652 | */ |
| 1653 | { |
| 1654 | ARMCPRegInfo id_cp_reginfo[] = { |
| 1655 | /* Note that the MIDR isn't a simple constant register because |
| 1656 | * of the TI925 behaviour where writes to another register can |
| 1657 | * cause the MIDR value to change. |
Peter Crosthwaite | 97ce8d6 | 2013-07-10 14:22:21 +1000 | [diff] [blame] | 1658 | * |
| 1659 | * Unimplemented registers in the c15 0 0 0 space default to |
| 1660 | * MIDR. Define MIDR first as this entire space, then CTR, TCMTR |
| 1661 | * and friends override accordingly. |
Peter Maydell | 7884849 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1662 | */ |
| 1663 | { .name = "MIDR", |
Peter Crosthwaite | 97ce8d6 | 2013-07-10 14:22:21 +1000 | [diff] [blame] | 1664 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, |
Peter Maydell | 7884849 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1665 | .access = PL1_R, .resetvalue = cpu->midr, |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1666 | .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, |
Peter Crosthwaite | 97ce8d6 | 2013-07-10 14:22:21 +1000 | [diff] [blame] | 1667 | .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), |
| 1668 | .type = ARM_CP_OVERRIDE }, |
Peter Maydell | 7884849 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1669 | { .name = "CTR", |
| 1670 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, |
| 1671 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, |
| 1672 | { .name = "TCMTR", |
| 1673 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, |
| 1674 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 1675 | { .name = "TLBTR", |
| 1676 | .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, |
| 1677 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 1678 | /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ |
| 1679 | { .name = "DUMMY", |
| 1680 | .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, |
| 1681 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 1682 | { .name = "DUMMY", |
| 1683 | .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, |
| 1684 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 1685 | { .name = "DUMMY", |
| 1686 | .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, |
| 1687 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 1688 | { .name = "DUMMY", |
| 1689 | .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, |
| 1690 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 1691 | { .name = "DUMMY", |
| 1692 | .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, |
| 1693 | .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, |
| 1694 | REGINFO_SENTINEL |
| 1695 | }; |
| 1696 | ARMCPRegInfo crn0_wi_reginfo = { |
| 1697 | .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, |
| 1698 | .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, |
| 1699 | .type = ARM_CP_NOP | ARM_CP_OVERRIDE |
| 1700 | }; |
| 1701 | if (arm_feature(env, ARM_FEATURE_OMAPCP) || |
| 1702 | arm_feature(env, ARM_FEATURE_STRONGARM)) { |
| 1703 | ARMCPRegInfo *r; |
| 1704 | /* Register the blanket "writes ignored" value first to cover the |
Peter Crosthwaite | a703eda | 2013-07-10 14:21:42 +1000 | [diff] [blame] | 1705 | * whole space. Then update the specific ID registers to allow write |
| 1706 | * access, so that they ignore writes rather than causing them to |
| 1707 | * UNDEF. |
Peter Maydell | 7884849 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1708 | */ |
| 1709 | define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); |
| 1710 | for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) { |
| 1711 | r->access = PL1_RW; |
Peter Maydell | 7884849 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1712 | } |
Peter Maydell | 7884849 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1713 | } |
Peter Crosthwaite | a703eda | 2013-07-10 14:21:42 +1000 | [diff] [blame] | 1714 | define_arm_cp_regs(cpu, id_cp_reginfo); |
Peter Maydell | 7884849 | 2012-06-20 11:57:20 +0000 | [diff] [blame] | 1715 | } |
| 1716 | |
Peter Crosthwaite | 97ce8d6 | 2013-07-10 14:22:21 +1000 | [diff] [blame] | 1717 | if (arm_feature(env, ARM_FEATURE_MPIDR)) { |
| 1718 | define_arm_cp_regs(cpu, mpidr_cp_reginfo); |
| 1719 | } |
| 1720 | |
Peter Maydell | 2771db2 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 1721 | if (arm_feature(env, ARM_FEATURE_AUXCR)) { |
| 1722 | ARMCPRegInfo auxcr = { |
| 1723 | .name = "AUXCR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, |
| 1724 | .access = PL1_RW, .type = ARM_CP_CONST, |
| 1725 | .resetvalue = cpu->reset_auxcr |
| 1726 | }; |
| 1727 | define_one_arm_cp_reg(cpu, &auxcr); |
| 1728 | } |
| 1729 | |
| 1730 | /* Generic registers whose values depend on the implementation */ |
| 1731 | { |
| 1732 | ARMCPRegInfo sctlr = { |
| 1733 | .name = "SCTLR", .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, |
| 1734 | .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c1_sys), |
Peter Maydell | d4e6df6 | 2013-06-25 18:16:07 +0100 | [diff] [blame] | 1735 | .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, |
| 1736 | .raw_writefn = raw_write, |
Peter Maydell | 2771db2 | 2012-06-20 11:57:18 +0000 | [diff] [blame] | 1737 | }; |
| 1738 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { |
| 1739 | /* Normally we would always end the TB on an SCTLR write, but Linux |
| 1740 | * arch/arm/mach-pxa/sleep.S expects two instructions following |
| 1741 | * an MMU enable to execute from cache. Imitate this behaviour. |
| 1742 | */ |
| 1743 | sctlr.type |= ARM_CP_SUPPRESS_TB_END; |
| 1744 | } |
| 1745 | define_one_arm_cp_reg(cpu, &sctlr); |
| 1746 | } |
Peter Maydell | 2ceb98c | 2012-06-20 11:57:09 +0000 | [diff] [blame] | 1747 | } |
| 1748 | |
Andreas Färber | 778c3a0 | 2012-04-20 07:39:14 +0000 | [diff] [blame] | 1749 | ARMCPU *cpu_arm_init(const char *cpu_model) |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 1750 | { |
Andreas Färber | dec9c2d | 2012-03-29 04:50:31 +0000 | [diff] [blame] | 1751 | ARMCPU *cpu; |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 1752 | CPUARMState *env; |
Andreas Färber | 5900d6b | 2013-01-21 16:11:43 +0100 | [diff] [blame] | 1753 | ObjectClass *oc; |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 1754 | |
Andreas Färber | 5900d6b | 2013-01-21 16:11:43 +0100 | [diff] [blame] | 1755 | oc = cpu_class_by_name(TYPE_ARM_CPU, cpu_model); |
| 1756 | if (!oc) { |
bellard | aaed909 | 2007-11-10 15:15:54 +0000 | [diff] [blame] | 1757 | return NULL; |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1758 | } |
Andreas Färber | 5900d6b | 2013-01-21 16:11:43 +0100 | [diff] [blame] | 1759 | cpu = ARM_CPU(object_new(object_class_get_name(oc))); |
Andreas Färber | dec9c2d | 2012-03-29 04:50:31 +0000 | [diff] [blame] | 1760 | env = &cpu->env; |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1761 | env->cpu_model_str = cpu_model; |
Andreas Färber | 1496926 | 2013-01-05 10:18:18 +0100 | [diff] [blame] | 1762 | |
| 1763 | /* TODO this should be set centrally, once possible */ |
| 1764 | object_property_set_bool(OBJECT(cpu), true, "realized", NULL); |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1765 | |
Andreas Färber | 1496926 | 2013-01-05 10:18:18 +0100 | [diff] [blame] | 1766 | return cpu; |
| 1767 | } |
| 1768 | |
| 1769 | void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) |
| 1770 | { |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 1771 | CPUState *cs = CPU(cpu); |
Andreas Färber | 1496926 | 2013-01-05 10:18:18 +0100 | [diff] [blame] | 1772 | CPUARMState *env = &cpu->env; |
| 1773 | |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1774 | if (arm_feature(env, ARM_FEATURE_NEON)) { |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 1775 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1776 | 51, "arm-neon.xml", 0); |
| 1777 | } else if (arm_feature(env, ARM_FEATURE_VFP3)) { |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 1778 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1779 | 35, "arm-vfp3.xml", 0); |
| 1780 | } else if (arm_feature(env, ARM_FEATURE_VFP)) { |
Andreas Färber | 22169d4 | 2013-06-28 21:27:39 +0200 | [diff] [blame] | 1781 | gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, |
pbrook | 56aebc8 | 2008-10-11 17:55:29 +0000 | [diff] [blame] | 1782 | 19, "arm-vfp.xml", 0); |
| 1783 | } |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 1784 | } |
| 1785 | |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1786 | /* Sort alphabetically by type name, except for "any". */ |
| 1787 | static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) |
pbrook | 5adb483 | 2007-03-08 03:15:18 +0000 | [diff] [blame] | 1788 | { |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1789 | ObjectClass *class_a = (ObjectClass *)a; |
| 1790 | ObjectClass *class_b = (ObjectClass *)b; |
| 1791 | const char *name_a, *name_b; |
pbrook | 5adb483 | 2007-03-08 03:15:18 +0000 | [diff] [blame] | 1792 | |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1793 | name_a = object_class_get_name(class_a); |
| 1794 | name_b = object_class_get_name(class_b); |
Andreas Färber | 51492fd | 2013-01-27 17:30:10 +0100 | [diff] [blame] | 1795 | if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) { |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1796 | return 1; |
Andreas Färber | 51492fd | 2013-01-27 17:30:10 +0100 | [diff] [blame] | 1797 | } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) { |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1798 | return -1; |
| 1799 | } else { |
| 1800 | return strcmp(name_a, name_b); |
pbrook | 5adb483 | 2007-03-08 03:15:18 +0000 | [diff] [blame] | 1801 | } |
| 1802 | } |
| 1803 | |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1804 | static void arm_cpu_list_entry(gpointer data, gpointer user_data) |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 1805 | { |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1806 | ObjectClass *oc = data; |
Andreas Färber | 92a3136 | 2012-12-16 02:17:02 +0100 | [diff] [blame] | 1807 | CPUListState *s = user_data; |
Andreas Färber | 51492fd | 2013-01-27 17:30:10 +0100 | [diff] [blame] | 1808 | const char *typename; |
| 1809 | char *name; |
pbrook | 3371d27 | 2007-03-08 03:04:12 +0000 | [diff] [blame] | 1810 | |
Andreas Färber | 51492fd | 2013-01-27 17:30:10 +0100 | [diff] [blame] | 1811 | typename = object_class_get_name(oc); |
| 1812 | name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU)); |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1813 | (*s->cpu_fprintf)(s->file, " %s\n", |
Andreas Färber | 51492fd | 2013-01-27 17:30:10 +0100 | [diff] [blame] | 1814 | name); |
| 1815 | g_free(name); |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1816 | } |
| 1817 | |
| 1818 | void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf) |
| 1819 | { |
Andreas Färber | 92a3136 | 2012-12-16 02:17:02 +0100 | [diff] [blame] | 1820 | CPUListState s = { |
Peter Maydell | 777dc78 | 2012-04-20 17:58:31 +0000 | [diff] [blame] | 1821 | .file = f, |
| 1822 | .cpu_fprintf = cpu_fprintf, |
| 1823 | }; |
| 1824 | GSList *list; |
| 1825 | |
| 1826 | list = object_class_get_list(TYPE_ARM_CPU, false); |
| 1827 | list = g_slist_sort(list, arm_cpu_list_compare); |
| 1828 | (*cpu_fprintf)(f, "Available CPUs:\n"); |
| 1829 | g_slist_foreach(list, arm_cpu_list_entry, &s); |
| 1830 | g_slist_free(list); |
pbrook | 40f137e | 2006-02-20 00:33:36 +0000 | [diff] [blame] | 1831 | } |
| 1832 | |
Cole Robinson | 78027bb | 2013-09-10 19:09:33 +0100 | [diff] [blame] | 1833 | static void arm_cpu_add_definition(gpointer data, gpointer user_data) |
| 1834 | { |
| 1835 | ObjectClass *oc = data; |
| 1836 | CpuDefinitionInfoList **cpu_list = user_data; |
| 1837 | CpuDefinitionInfoList *entry; |
| 1838 | CpuDefinitionInfo *info; |
| 1839 | const char *typename; |
| 1840 | |
| 1841 | typename = object_class_get_name(oc); |
| 1842 | info = g_malloc0(sizeof(*info)); |
| 1843 | info->name = g_strndup(typename, |
| 1844 | strlen(typename) - strlen("-" TYPE_ARM_CPU)); |
| 1845 | |
| 1846 | entry = g_malloc0(sizeof(*entry)); |
| 1847 | entry->value = info; |
| 1848 | entry->next = *cpu_list; |
| 1849 | *cpu_list = entry; |
| 1850 | } |
| 1851 | |
| 1852 | CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp) |
| 1853 | { |
| 1854 | CpuDefinitionInfoList *cpu_list = NULL; |
| 1855 | GSList *list; |
| 1856 | |
| 1857 | list = object_class_get_list(TYPE_ARM_CPU, false); |
| 1858 | g_slist_foreach(list, arm_cpu_add_definition, &cpu_list); |
| 1859 | g_slist_free(list); |
| 1860 | |
| 1861 | return cpu_list; |
| 1862 | } |
| 1863 | |
Peter Maydell | 4b6a83f | 2012-06-20 11:57:06 +0000 | [diff] [blame] | 1864 | void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, |
| 1865 | const ARMCPRegInfo *r, void *opaque) |
| 1866 | { |
| 1867 | /* Define implementations of coprocessor registers. |
| 1868 | * We store these in a hashtable because typically |
| 1869 | * there are less than 150 registers in a space which |
| 1870 | * is 16*16*16*8*8 = 262144 in size. |
| 1871 | * Wildcarding is supported for the crm, opc1 and opc2 fields. |
| 1872 | * If a register is defined twice then the second definition is |
| 1873 | * used, so this can be used to define some generic registers and |
| 1874 | * then override them with implementation specific variations. |
| 1875 | * At least one of the original and the second definition should |
| 1876 | * include ARM_CP_OVERRIDE in its type bits -- this is just a guard |
| 1877 | * against accidental use. |
| 1878 | */ |
| 1879 | int crm, opc1, opc2; |
| 1880 | int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; |
| 1881 | int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; |
| 1882 | int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; |
| 1883 | int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; |
| 1884 | int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; |
| 1885 | int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; |
| 1886 | /* 64 bit registers have only CRm and Opc1 fields */ |
| 1887 | assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); |
| 1888 | /* Check that the register definition has enough info to handle |
| 1889 | * reads and writes if they are permitted. |
| 1890 | */ |
| 1891 | if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { |
| 1892 | if (r->access & PL3_R) { |
| 1893 | assert(r->fieldoffset || r->readfn); |
| 1894 | } |
| 1895 | if (r->access & PL3_W) { |
| 1896 | assert(r->fieldoffset || r->writefn); |
| 1897 | } |
| 1898 | } |
| 1899 | /* Bad type field probably means missing sentinel at end of reg list */ |
| 1900 | assert(cptype_valid(r->type)); |
| 1901 | for (crm = crmmin; crm <= crmmax; crm++) { |
| 1902 | for (opc1 = opc1min; opc1 <= opc1max; opc1++) { |
| 1903 | for (opc2 = opc2min; opc2 <= opc2max; opc2++) { |
| 1904 | uint32_t *key = g_new(uint32_t, 1); |
| 1905 | ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); |
| 1906 | int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; |
| 1907 | *key = ENCODE_CP_REG(r->cp, is64, r->crn, crm, opc1, opc2); |
Peter Crosthwaite | 204a9c4 | 2013-07-10 14:22:59 +1000 | [diff] [blame] | 1908 | if (opaque) { |
| 1909 | r2->opaque = opaque; |
| 1910 | } |
Peter Maydell | 4b6a83f | 2012-06-20 11:57:06 +0000 | [diff] [blame] | 1911 | /* Make sure reginfo passed to helpers for wildcarded regs |
| 1912 | * has the correct crm/opc1/opc2 for this reg, not CP_ANY: |
| 1913 | */ |
| 1914 | r2->crm = crm; |
| 1915 | r2->opc1 = opc1; |
| 1916 | r2->opc2 = opc2; |
Peter Maydell | 7023ec7 | 2013-06-25 18:16:06 +0100 | [diff] [blame] | 1917 | /* By convention, for wildcarded registers only the first |
| 1918 | * entry is used for migration; the others are marked as |
| 1919 | * NO_MIGRATE so we don't try to transfer the register |
| 1920 | * multiple times. Special registers (ie NOP/WFI) are |
| 1921 | * never migratable. |
| 1922 | */ |
| 1923 | if ((r->type & ARM_CP_SPECIAL) || |
| 1924 | ((r->crm == CP_ANY) && crm != 0) || |
| 1925 | ((r->opc1 == CP_ANY) && opc1 != 0) || |
| 1926 | ((r->opc2 == CP_ANY) && opc2 != 0)) { |
| 1927 | r2->type |= ARM_CP_NO_MIGRATE; |
| 1928 | } |
| 1929 | |
Peter Maydell | 4b6a83f | 2012-06-20 11:57:06 +0000 | [diff] [blame] | 1930 | /* Overriding of an existing definition must be explicitly |
| 1931 | * requested. |
| 1932 | */ |
| 1933 | if (!(r->type & ARM_CP_OVERRIDE)) { |
| 1934 | ARMCPRegInfo *oldreg; |
| 1935 | oldreg = g_hash_table_lookup(cpu->cp_regs, key); |
| 1936 | if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { |
| 1937 | fprintf(stderr, "Register redefined: cp=%d %d bit " |
| 1938 | "crn=%d crm=%d opc1=%d opc2=%d, " |
| 1939 | "was %s, now %s\n", r2->cp, 32 + 32 * is64, |
| 1940 | r2->crn, r2->crm, r2->opc1, r2->opc2, |
| 1941 | oldreg->name, r2->name); |
Stefan Weil | dfc6f86 | 2013-07-25 18:21:28 +0200 | [diff] [blame] | 1942 | g_assert_not_reached(); |
Peter Maydell | 4b6a83f | 2012-06-20 11:57:06 +0000 | [diff] [blame] | 1943 | } |
| 1944 | } |
| 1945 | g_hash_table_insert(cpu->cp_regs, key, r2); |
| 1946 | } |
| 1947 | } |
| 1948 | } |
| 1949 | } |
| 1950 | |
| 1951 | void define_arm_cp_regs_with_opaque(ARMCPU *cpu, |
| 1952 | const ARMCPRegInfo *regs, void *opaque) |
| 1953 | { |
| 1954 | /* Define a whole list of registers */ |
| 1955 | const ARMCPRegInfo *r; |
| 1956 | for (r = regs; r->type != ARM_CP_SENTINEL; r++) { |
| 1957 | define_one_arm_cp_reg_with_opaque(cpu, r, opaque); |
| 1958 | } |
| 1959 | } |
| 1960 | |
| 1961 | const ARMCPRegInfo *get_arm_cp_reginfo(ARMCPU *cpu, uint32_t encoded_cp) |
| 1962 | { |
| 1963 | return g_hash_table_lookup(cpu->cp_regs, &encoded_cp); |
| 1964 | } |
| 1965 | |
| 1966 | int arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, |
| 1967 | uint64_t value) |
| 1968 | { |
| 1969 | /* Helper coprocessor write function for write-ignore registers */ |
| 1970 | return 0; |
| 1971 | } |
| 1972 | |
| 1973 | int arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t *value) |
| 1974 | { |
| 1975 | /* Helper coprocessor write function for read-as-zero registers */ |
| 1976 | *value = 0; |
| 1977 | return 0; |
| 1978 | } |
| 1979 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 1980 | static int bad_mode_switch(CPUARMState *env, int mode) |
Peter Maydell | 37064a8 | 2012-01-05 15:49:06 +0000 | [diff] [blame] | 1981 | { |
| 1982 | /* Return true if it is not valid for us to switch to |
| 1983 | * this CPU mode (ie all the UNPREDICTABLE cases in |
| 1984 | * the ARM ARM CPSRWriteByInstr pseudocode). |
| 1985 | */ |
| 1986 | switch (mode) { |
| 1987 | case ARM_CPU_MODE_USR: |
| 1988 | case ARM_CPU_MODE_SYS: |
| 1989 | case ARM_CPU_MODE_SVC: |
| 1990 | case ARM_CPU_MODE_ABT: |
| 1991 | case ARM_CPU_MODE_UND: |
| 1992 | case ARM_CPU_MODE_IRQ: |
| 1993 | case ARM_CPU_MODE_FIQ: |
| 1994 | return 0; |
| 1995 | default: |
| 1996 | return 1; |
| 1997 | } |
| 1998 | } |
| 1999 | |
balrog | 2f4a40e | 2007-11-13 01:50:15 +0000 | [diff] [blame] | 2000 | uint32_t cpsr_read(CPUARMState *env) |
| 2001 | { |
| 2002 | int ZF; |
pbrook | 6fbe23d | 2008-04-01 17:19:11 +0000 | [diff] [blame] | 2003 | ZF = (env->ZF == 0); |
| 2004 | return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | |
balrog | 2f4a40e | 2007-11-13 01:50:15 +0000 | [diff] [blame] | 2005 | (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) |
| 2006 | | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) |
| 2007 | | ((env->condexec_bits & 0xfc) << 8) |
| 2008 | | (env->GE << 16); |
| 2009 | } |
| 2010 | |
| 2011 | void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask) |
| 2012 | { |
balrog | 2f4a40e | 2007-11-13 01:50:15 +0000 | [diff] [blame] | 2013 | if (mask & CPSR_NZCV) { |
pbrook | 6fbe23d | 2008-04-01 17:19:11 +0000 | [diff] [blame] | 2014 | env->ZF = (~val) & CPSR_Z; |
| 2015 | env->NF = val; |
balrog | 2f4a40e | 2007-11-13 01:50:15 +0000 | [diff] [blame] | 2016 | env->CF = (val >> 29) & 1; |
| 2017 | env->VF = (val << 3) & 0x80000000; |
| 2018 | } |
| 2019 | if (mask & CPSR_Q) |
| 2020 | env->QF = ((val & CPSR_Q) != 0); |
| 2021 | if (mask & CPSR_T) |
| 2022 | env->thumb = ((val & CPSR_T) != 0); |
| 2023 | if (mask & CPSR_IT_0_1) { |
| 2024 | env->condexec_bits &= ~3; |
| 2025 | env->condexec_bits |= (val >> 25) & 3; |
| 2026 | } |
| 2027 | if (mask & CPSR_IT_2_7) { |
| 2028 | env->condexec_bits &= 3; |
| 2029 | env->condexec_bits |= (val >> 8) & 0xfc; |
| 2030 | } |
| 2031 | if (mask & CPSR_GE) { |
| 2032 | env->GE = (val >> 16) & 0xf; |
| 2033 | } |
| 2034 | |
| 2035 | if ((env->uncached_cpsr ^ val) & mask & CPSR_M) { |
Peter Maydell | 37064a8 | 2012-01-05 15:49:06 +0000 | [diff] [blame] | 2036 | if (bad_mode_switch(env, val & CPSR_M)) { |
| 2037 | /* Attempt to switch to an invalid mode: this is UNPREDICTABLE. |
| 2038 | * We choose to ignore the attempt and leave the CPSR M field |
| 2039 | * untouched. |
| 2040 | */ |
| 2041 | mask &= ~CPSR_M; |
| 2042 | } else { |
| 2043 | switch_mode(env, val & CPSR_M); |
| 2044 | } |
balrog | 2f4a40e | 2007-11-13 01:50:15 +0000 | [diff] [blame] | 2045 | } |
| 2046 | mask &= ~CACHED_CPSR_BITS; |
| 2047 | env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); |
| 2048 | } |
| 2049 | |
pbrook | b26eefb | 2008-03-31 03:44:26 +0000 | [diff] [blame] | 2050 | /* Sign/zero extend */ |
| 2051 | uint32_t HELPER(sxtb16)(uint32_t x) |
| 2052 | { |
| 2053 | uint32_t res; |
| 2054 | res = (uint16_t)(int8_t)x; |
| 2055 | res |= (uint32_t)(int8_t)(x >> 16) << 16; |
| 2056 | return res; |
| 2057 | } |
| 2058 | |
| 2059 | uint32_t HELPER(uxtb16)(uint32_t x) |
| 2060 | { |
| 2061 | uint32_t res; |
| 2062 | res = (uint16_t)(uint8_t)x; |
| 2063 | res |= (uint32_t)(uint8_t)(x >> 16) << 16; |
| 2064 | return res; |
| 2065 | } |
| 2066 | |
pbrook | f51bbbf | 2008-03-31 03:45:13 +0000 | [diff] [blame] | 2067 | uint32_t HELPER(clz)(uint32_t x) |
| 2068 | { |
Aurelien Jarno | 7bbcb0a | 2009-10-15 23:14:52 +0200 | [diff] [blame] | 2069 | return clz32(x); |
pbrook | f51bbbf | 2008-03-31 03:45:13 +0000 | [diff] [blame] | 2070 | } |
| 2071 | |
pbrook | 3670669 | 2008-03-31 03:46:19 +0000 | [diff] [blame] | 2072 | int32_t HELPER(sdiv)(int32_t num, int32_t den) |
| 2073 | { |
| 2074 | if (den == 0) |
| 2075 | return 0; |
Aurelien Jarno | 686eeb9 | 2009-10-15 23:08:46 +0200 | [diff] [blame] | 2076 | if (num == INT_MIN && den == -1) |
| 2077 | return INT_MIN; |
pbrook | 3670669 | 2008-03-31 03:46:19 +0000 | [diff] [blame] | 2078 | return num / den; |
| 2079 | } |
| 2080 | |
| 2081 | uint32_t HELPER(udiv)(uint32_t num, uint32_t den) |
| 2082 | { |
| 2083 | if (den == 0) |
| 2084 | return 0; |
| 2085 | return num / den; |
| 2086 | } |
| 2087 | |
| 2088 | uint32_t HELPER(rbit)(uint32_t x) |
| 2089 | { |
| 2090 | x = ((x & 0xff000000) >> 24) |
| 2091 | | ((x & 0x00ff0000) >> 8) |
| 2092 | | ((x & 0x0000ff00) << 8) |
| 2093 | | ((x & 0x000000ff) << 24); |
| 2094 | x = ((x & 0xf0f0f0f0) >> 4) |
| 2095 | | ((x & 0x0f0f0f0f) << 4); |
| 2096 | x = ((x & 0x88888888) >> 3) |
| 2097 | | ((x & 0x44444444) >> 1) |
| 2098 | | ((x & 0x22222222) << 1) |
| 2099 | | ((x & 0x11111111) << 3); |
| 2100 | return x; |
| 2101 | } |
| 2102 | |
ths | 5fafdf2 | 2007-09-16 21:08:06 +0000 | [diff] [blame] | 2103 | #if defined(CONFIG_USER_ONLY) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2104 | |
Andreas Färber | 97a8ea5 | 2013-02-02 10:57:51 +0100 | [diff] [blame] | 2105 | void arm_cpu_do_interrupt(CPUState *cs) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2106 | { |
Andreas Färber | 97a8ea5 | 2013-02-02 10:57:51 +0100 | [diff] [blame] | 2107 | ARMCPU *cpu = ARM_CPU(cs); |
| 2108 | CPUARMState *env = &cpu->env; |
| 2109 | |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2110 | env->exception_index = -1; |
| 2111 | } |
| 2112 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2113 | int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, int rw, |
Blue Swirl | 97b348e | 2011-08-01 16:12:17 +0000 | [diff] [blame] | 2114 | int mmu_idx) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2115 | { |
| 2116 | if (rw == 2) { |
| 2117 | env->exception_index = EXCP_PREFETCH_ABORT; |
| 2118 | env->cp15.c6_insn = address; |
| 2119 | } else { |
| 2120 | env->exception_index = EXCP_DATA_ABORT; |
| 2121 | env->cp15.c6_data = address; |
| 2122 | } |
| 2123 | return 1; |
| 2124 | } |
| 2125 | |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2126 | /* These should probably raise undefined insn exceptions. */ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2127 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2128 | { |
| 2129 | cpu_abort(env, "v7m_mrs %d\n", reg); |
| 2130 | } |
| 2131 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2132 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2133 | { |
| 2134 | cpu_abort(env, "v7m_mrs %d\n", reg); |
| 2135 | return 0; |
| 2136 | } |
| 2137 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2138 | void switch_mode(CPUARMState *env, int mode) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2139 | { |
| 2140 | if (mode != ARM_CPU_MODE_USR) |
| 2141 | cpu_abort(env, "Tried to switch out of user mode\n"); |
| 2142 | } |
| 2143 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2144 | void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2145 | { |
| 2146 | cpu_abort(env, "banked r13 write\n"); |
| 2147 | } |
| 2148 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2149 | uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2150 | { |
| 2151 | cpu_abort(env, "banked r13 read\n"); |
| 2152 | return 0; |
| 2153 | } |
| 2154 | |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2155 | #else |
| 2156 | |
| 2157 | /* Map CPU modes onto saved register banks. */ |
Christoffer Dall | 494b00c | 2013-03-05 00:34:41 +0000 | [diff] [blame] | 2158 | int bank_number(int mode) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2159 | { |
| 2160 | switch (mode) { |
| 2161 | case ARM_CPU_MODE_USR: |
| 2162 | case ARM_CPU_MODE_SYS: |
| 2163 | return 0; |
| 2164 | case ARM_CPU_MODE_SVC: |
| 2165 | return 1; |
| 2166 | case ARM_CPU_MODE_ABT: |
| 2167 | return 2; |
| 2168 | case ARM_CPU_MODE_UND: |
| 2169 | return 3; |
| 2170 | case ARM_CPU_MODE_IRQ: |
| 2171 | return 4; |
| 2172 | case ARM_CPU_MODE_FIQ: |
| 2173 | return 5; |
| 2174 | } |
Peter Maydell | f520641 | 2013-03-05 00:34:40 +0000 | [diff] [blame] | 2175 | hw_error("bank number requested for bad CPSR mode value 0x%x\n", mode); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2176 | } |
| 2177 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2178 | void switch_mode(CPUARMState *env, int mode) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2179 | { |
| 2180 | int old_mode; |
| 2181 | int i; |
| 2182 | |
| 2183 | old_mode = env->uncached_cpsr & CPSR_M; |
| 2184 | if (mode == old_mode) |
| 2185 | return; |
| 2186 | |
| 2187 | if (old_mode == ARM_CPU_MODE_FIQ) { |
| 2188 | memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); |
pbrook | 8637c67 | 2006-03-14 14:20:32 +0000 | [diff] [blame] | 2189 | memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2190 | } else if (mode == ARM_CPU_MODE_FIQ) { |
| 2191 | memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); |
pbrook | 8637c67 | 2006-03-14 14:20:32 +0000 | [diff] [blame] | 2192 | memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2193 | } |
| 2194 | |
Peter Maydell | f520641 | 2013-03-05 00:34:40 +0000 | [diff] [blame] | 2195 | i = bank_number(old_mode); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2196 | env->banked_r13[i] = env->regs[13]; |
| 2197 | env->banked_r14[i] = env->regs[14]; |
| 2198 | env->banked_spsr[i] = env->spsr; |
| 2199 | |
Peter Maydell | f520641 | 2013-03-05 00:34:40 +0000 | [diff] [blame] | 2200 | i = bank_number(mode); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2201 | env->regs[13] = env->banked_r13[i]; |
| 2202 | env->regs[14] = env->banked_r14[i]; |
| 2203 | env->spsr = env->banked_spsr[i]; |
| 2204 | } |
| 2205 | |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2206 | static void v7m_push(CPUARMState *env, uint32_t val) |
| 2207 | { |
| 2208 | env->regs[13] -= 4; |
| 2209 | stl_phys(env->regs[13], val); |
| 2210 | } |
| 2211 | |
| 2212 | static uint32_t v7m_pop(CPUARMState *env) |
| 2213 | { |
| 2214 | uint32_t val; |
| 2215 | val = ldl_phys(env->regs[13]); |
| 2216 | env->regs[13] += 4; |
| 2217 | return val; |
| 2218 | } |
| 2219 | |
| 2220 | /* Switch to V7M main or process stack pointer. */ |
| 2221 | static void switch_v7m_sp(CPUARMState *env, int process) |
| 2222 | { |
| 2223 | uint32_t tmp; |
| 2224 | if (env->v7m.current_sp != process) { |
| 2225 | tmp = env->v7m.other_sp; |
| 2226 | env->v7m.other_sp = env->regs[13]; |
| 2227 | env->regs[13] = tmp; |
| 2228 | env->v7m.current_sp = process; |
| 2229 | } |
| 2230 | } |
| 2231 | |
| 2232 | static void do_v7m_exception_exit(CPUARMState *env) |
| 2233 | { |
| 2234 | uint32_t type; |
| 2235 | uint32_t xpsr; |
| 2236 | |
| 2237 | type = env->regs[15]; |
| 2238 | if (env->v7m.exception != 0) |
Paul Brook | 983fe82 | 2010-04-05 19:34:51 +0100 | [diff] [blame] | 2239 | armv7m_nvic_complete_irq(env->nvic, env->v7m.exception); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2240 | |
| 2241 | /* Switch to the target stack. */ |
| 2242 | switch_v7m_sp(env, (type & 4) != 0); |
| 2243 | /* Pop registers. */ |
| 2244 | env->regs[0] = v7m_pop(env); |
| 2245 | env->regs[1] = v7m_pop(env); |
| 2246 | env->regs[2] = v7m_pop(env); |
| 2247 | env->regs[3] = v7m_pop(env); |
| 2248 | env->regs[12] = v7m_pop(env); |
| 2249 | env->regs[14] = v7m_pop(env); |
| 2250 | env->regs[15] = v7m_pop(env); |
| 2251 | xpsr = v7m_pop(env); |
| 2252 | xpsr_write(env, xpsr, 0xfffffdff); |
| 2253 | /* Undo stack alignment. */ |
| 2254 | if (xpsr & 0x200) |
| 2255 | env->regs[13] |= 4; |
| 2256 | /* ??? The exception return type specifies Thread/Handler mode. However |
| 2257 | this is also implied by the xPSR value. Not sure what to do |
| 2258 | if there is a mismatch. */ |
| 2259 | /* ??? Likewise for mismatches between the CONTROL register and the stack |
| 2260 | pointer. */ |
| 2261 | } |
| 2262 | |
Peter Maydell | 3f1beac | 2013-08-20 14:54:28 +0100 | [diff] [blame] | 2263 | /* Exception names for debug logging; note that not all of these |
| 2264 | * precisely correspond to architectural exceptions. |
| 2265 | */ |
| 2266 | static const char * const excnames[] = { |
| 2267 | [EXCP_UDEF] = "Undefined Instruction", |
| 2268 | [EXCP_SWI] = "SVC", |
| 2269 | [EXCP_PREFETCH_ABORT] = "Prefetch Abort", |
| 2270 | [EXCP_DATA_ABORT] = "Data Abort", |
| 2271 | [EXCP_IRQ] = "IRQ", |
| 2272 | [EXCP_FIQ] = "FIQ", |
| 2273 | [EXCP_BKPT] = "Breakpoint", |
| 2274 | [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit", |
| 2275 | [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage", |
| 2276 | [EXCP_STREX] = "QEMU intercept of STREX", |
| 2277 | }; |
| 2278 | |
| 2279 | static inline void arm_log_exception(int idx) |
| 2280 | { |
| 2281 | if (qemu_loglevel_mask(CPU_LOG_INT)) { |
| 2282 | const char *exc = NULL; |
| 2283 | |
| 2284 | if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { |
| 2285 | exc = excnames[idx]; |
| 2286 | } |
| 2287 | if (!exc) { |
| 2288 | exc = "unknown"; |
| 2289 | } |
| 2290 | qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc); |
| 2291 | } |
| 2292 | } |
| 2293 | |
Andreas Färber | e6f010c | 2013-02-02 12:33:14 +0100 | [diff] [blame] | 2294 | void arm_v7m_cpu_do_interrupt(CPUState *cs) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2295 | { |
Andreas Färber | e6f010c | 2013-02-02 12:33:14 +0100 | [diff] [blame] | 2296 | ARMCPU *cpu = ARM_CPU(cs); |
| 2297 | CPUARMState *env = &cpu->env; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2298 | uint32_t xpsr = xpsr_read(env); |
| 2299 | uint32_t lr; |
| 2300 | uint32_t addr; |
| 2301 | |
Peter Maydell | 3f1beac | 2013-08-20 14:54:28 +0100 | [diff] [blame] | 2302 | arm_log_exception(env->exception_index); |
| 2303 | |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2304 | lr = 0xfffffff1; |
| 2305 | if (env->v7m.current_sp) |
| 2306 | lr |= 4; |
| 2307 | if (env->v7m.exception == 0) |
| 2308 | lr |= 8; |
| 2309 | |
| 2310 | /* For exceptions we just mark as pending on the NVIC, and let that |
| 2311 | handle it. */ |
| 2312 | /* TODO: Need to escalate if the current priority is higher than the |
| 2313 | one we're raising. */ |
| 2314 | switch (env->exception_index) { |
| 2315 | case EXCP_UDEF: |
Paul Brook | 983fe82 | 2010-04-05 19:34:51 +0100 | [diff] [blame] | 2316 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2317 | return; |
| 2318 | case EXCP_SWI: |
Alex_Rozenman@mentor.com | 314e229 | 2013-01-11 15:21:22 +0000 | [diff] [blame] | 2319 | /* The PC already points to the next instruction. */ |
Paul Brook | 983fe82 | 2010-04-05 19:34:51 +0100 | [diff] [blame] | 2320 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2321 | return; |
| 2322 | case EXCP_PREFETCH_ABORT: |
| 2323 | case EXCP_DATA_ABORT: |
Paul Brook | 983fe82 | 2010-04-05 19:34:51 +0100 | [diff] [blame] | 2324 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2325 | return; |
| 2326 | case EXCP_BKPT: |
pbrook | 2ad207d | 2007-11-24 23:22:11 +0000 | [diff] [blame] | 2327 | if (semihosting_enabled) { |
| 2328 | int nr; |
Blue Swirl | d31dd73 | 2012-09-04 20:25:59 +0000 | [diff] [blame] | 2329 | nr = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff; |
pbrook | 2ad207d | 2007-11-24 23:22:11 +0000 | [diff] [blame] | 2330 | if (nr == 0xab) { |
| 2331 | env->regs[15] += 2; |
| 2332 | env->regs[0] = do_arm_semihosting(env); |
Peter Maydell | 3f1beac | 2013-08-20 14:54:28 +0100 | [diff] [blame] | 2333 | qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n"); |
pbrook | 2ad207d | 2007-11-24 23:22:11 +0000 | [diff] [blame] | 2334 | return; |
| 2335 | } |
| 2336 | } |
Paul Brook | 983fe82 | 2010-04-05 19:34:51 +0100 | [diff] [blame] | 2337 | armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2338 | return; |
| 2339 | case EXCP_IRQ: |
Paul Brook | 983fe82 | 2010-04-05 19:34:51 +0100 | [diff] [blame] | 2340 | env->v7m.exception = armv7m_nvic_acknowledge_irq(env->nvic); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2341 | break; |
| 2342 | case EXCP_EXCEPTION_EXIT: |
| 2343 | do_v7m_exception_exit(env); |
| 2344 | return; |
| 2345 | default: |
| 2346 | cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index); |
| 2347 | return; /* Never happens. Keep compiler happy. */ |
| 2348 | } |
| 2349 | |
| 2350 | /* Align stack pointer. */ |
| 2351 | /* ??? Should only do this if Configuration Control Register |
| 2352 | STACKALIGN bit is set. */ |
| 2353 | if (env->regs[13] & 4) { |
pbrook | ab19b0e | 2008-07-02 16:44:09 +0000 | [diff] [blame] | 2354 | env->regs[13] -= 4; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2355 | xpsr |= 0x200; |
| 2356 | } |
balrog | 6c95676 | 2008-04-13 00:57:49 +0000 | [diff] [blame] | 2357 | /* Switch to the handler mode. */ |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2358 | v7m_push(env, xpsr); |
| 2359 | v7m_push(env, env->regs[15]); |
| 2360 | v7m_push(env, env->regs[14]); |
| 2361 | v7m_push(env, env->regs[12]); |
| 2362 | v7m_push(env, env->regs[3]); |
| 2363 | v7m_push(env, env->regs[2]); |
| 2364 | v7m_push(env, env->regs[1]); |
| 2365 | v7m_push(env, env->regs[0]); |
| 2366 | switch_v7m_sp(env, 0); |
Peter Maydell | c98d174 | 2012-03-14 12:26:10 +0000 | [diff] [blame] | 2367 | /* Clear IT bits */ |
| 2368 | env->condexec_bits = 0; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2369 | env->regs[14] = lr; |
| 2370 | addr = ldl_phys(env->v7m.vecbase + env->v7m.exception * 4); |
| 2371 | env->regs[15] = addr & 0xfffffffe; |
| 2372 | env->thumb = addr & 1; |
| 2373 | } |
| 2374 | |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2375 | /* Handle a CPU exception. */ |
Andreas Färber | 97a8ea5 | 2013-02-02 10:57:51 +0100 | [diff] [blame] | 2376 | void arm_cpu_do_interrupt(CPUState *cs) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2377 | { |
Andreas Färber | 97a8ea5 | 2013-02-02 10:57:51 +0100 | [diff] [blame] | 2378 | ARMCPU *cpu = ARM_CPU(cs); |
| 2379 | CPUARMState *env = &cpu->env; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2380 | uint32_t addr; |
| 2381 | uint32_t mask; |
| 2382 | int new_mode; |
| 2383 | uint32_t offset; |
| 2384 | |
Andreas Färber | e6f010c | 2013-02-02 12:33:14 +0100 | [diff] [blame] | 2385 | assert(!IS_M(env)); |
| 2386 | |
Peter Maydell | 3f1beac | 2013-08-20 14:54:28 +0100 | [diff] [blame] | 2387 | arm_log_exception(env->exception_index); |
| 2388 | |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2389 | /* TODO: Vectored interrupt controller. */ |
| 2390 | switch (env->exception_index) { |
| 2391 | case EXCP_UDEF: |
| 2392 | new_mode = ARM_CPU_MODE_UND; |
| 2393 | addr = 0x04; |
| 2394 | mask = CPSR_I; |
| 2395 | if (env->thumb) |
| 2396 | offset = 2; |
| 2397 | else |
| 2398 | offset = 4; |
| 2399 | break; |
| 2400 | case EXCP_SWI: |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 2401 | if (semihosting_enabled) { |
| 2402 | /* Check for semihosting interrupt. */ |
| 2403 | if (env->thumb) { |
Blue Swirl | d31dd73 | 2012-09-04 20:25:59 +0000 | [diff] [blame] | 2404 | mask = arm_lduw_code(env, env->regs[15] - 2, env->bswap_code) |
| 2405 | & 0xff; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 2406 | } else { |
Blue Swirl | d31dd73 | 2012-09-04 20:25:59 +0000 | [diff] [blame] | 2407 | mask = arm_ldl_code(env, env->regs[15] - 4, env->bswap_code) |
Paul Brook | d8fd295 | 2012-03-30 18:02:50 +0100 | [diff] [blame] | 2408 | & 0xffffff; |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 2409 | } |
| 2410 | /* Only intercept calls from privileged modes, to provide some |
| 2411 | semblance of security. */ |
| 2412 | if (((mask == 0x123456 && !env->thumb) |
| 2413 | || (mask == 0xab && env->thumb)) |
| 2414 | && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) { |
| 2415 | env->regs[0] = do_arm_semihosting(env); |
Peter Maydell | 3f1beac | 2013-08-20 14:54:28 +0100 | [diff] [blame] | 2416 | qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n"); |
pbrook | 8e71621 | 2007-01-20 17:12:09 +0000 | [diff] [blame] | 2417 | return; |
| 2418 | } |
| 2419 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2420 | new_mode = ARM_CPU_MODE_SVC; |
| 2421 | addr = 0x08; |
| 2422 | mask = CPSR_I; |
balrog | 601d70b | 2008-04-20 01:03:45 +0000 | [diff] [blame] | 2423 | /* The PC already points to the next instruction. */ |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2424 | offset = 0; |
| 2425 | break; |
pbrook | 06c949e | 2006-02-04 19:35:26 +0000 | [diff] [blame] | 2426 | case EXCP_BKPT: |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2427 | /* See if this is a semihosting syscall. */ |
pbrook | 2ad207d | 2007-11-24 23:22:11 +0000 | [diff] [blame] | 2428 | if (env->thumb && semihosting_enabled) { |
Blue Swirl | d31dd73 | 2012-09-04 20:25:59 +0000 | [diff] [blame] | 2429 | mask = arm_lduw_code(env, env->regs[15], env->bswap_code) & 0xff; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2430 | if (mask == 0xab |
| 2431 | && (env->uncached_cpsr & CPSR_M) != ARM_CPU_MODE_USR) { |
| 2432 | env->regs[15] += 2; |
| 2433 | env->regs[0] = do_arm_semihosting(env); |
Peter Maydell | 3f1beac | 2013-08-20 14:54:28 +0100 | [diff] [blame] | 2434 | qemu_log_mask(CPU_LOG_INT, "...handled as semihosting call\n"); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2435 | return; |
| 2436 | } |
| 2437 | } |
Alex Zuepke | 81c05da | 2011-06-03 18:42:17 +0200 | [diff] [blame] | 2438 | env->cp15.c5_insn = 2; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2439 | /* Fall through to prefetch abort. */ |
| 2440 | case EXCP_PREFETCH_ABORT: |
Peter Maydell | 3f1beac | 2013-08-20 14:54:28 +0100 | [diff] [blame] | 2441 | qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", |
| 2442 | env->cp15.c5_insn, env->cp15.c6_insn); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2443 | new_mode = ARM_CPU_MODE_ABT; |
| 2444 | addr = 0x0c; |
| 2445 | mask = CPSR_A | CPSR_I; |
| 2446 | offset = 4; |
| 2447 | break; |
| 2448 | case EXCP_DATA_ABORT: |
Peter Maydell | 3f1beac | 2013-08-20 14:54:28 +0100 | [diff] [blame] | 2449 | qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", |
| 2450 | env->cp15.c5_data, env->cp15.c6_data); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2451 | new_mode = ARM_CPU_MODE_ABT; |
| 2452 | addr = 0x10; |
| 2453 | mask = CPSR_A | CPSR_I; |
| 2454 | offset = 8; |
| 2455 | break; |
| 2456 | case EXCP_IRQ: |
| 2457 | new_mode = ARM_CPU_MODE_IRQ; |
| 2458 | addr = 0x18; |
| 2459 | /* Disable IRQ and imprecise data aborts. */ |
| 2460 | mask = CPSR_A | CPSR_I; |
| 2461 | offset = 4; |
| 2462 | break; |
| 2463 | case EXCP_FIQ: |
| 2464 | new_mode = ARM_CPU_MODE_FIQ; |
| 2465 | addr = 0x1c; |
| 2466 | /* Disable FIQ, IRQ and imprecise data aborts. */ |
| 2467 | mask = CPSR_A | CPSR_I | CPSR_F; |
| 2468 | offset = 4; |
| 2469 | break; |
| 2470 | default: |
| 2471 | cpu_abort(env, "Unhandled exception 0x%x\n", env->exception_index); |
| 2472 | return; /* Never happens. Keep compiler happy. */ |
| 2473 | } |
| 2474 | /* High vectors. */ |
| 2475 | if (env->cp15.c1_sys & (1 << 13)) { |
| 2476 | addr += 0xffff0000; |
| 2477 | } |
| 2478 | switch_mode (env, new_mode); |
| 2479 | env->spsr = cpsr_read(env); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2480 | /* Clear IT bits. */ |
| 2481 | env->condexec_bits = 0; |
Rabin Vincent | 30a8cac | 2010-02-15 00:02:36 +0530 | [diff] [blame] | 2482 | /* Switch to the new mode, and to the correct instruction set. */ |
bellard | 6d7e632 | 2005-12-18 16:54:08 +0000 | [diff] [blame] | 2483 | env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2484 | env->uncached_cpsr |= mask; |
Dmitry Eremin-Solenikov | be5e7a7 | 2011-04-04 17:38:44 +0400 | [diff] [blame] | 2485 | /* this is a lie, as the was no c1_sys on V4T/V5, but who cares |
| 2486 | * and we should just guard the thumb mode on V4 */ |
| 2487 | if (arm_feature(env, ARM_FEATURE_V4T)) { |
| 2488 | env->thumb = (env->cp15.c1_sys & (1 << 30)) != 0; |
| 2489 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2490 | env->regs[14] = env->regs[15] + offset; |
| 2491 | env->regs[15] = addr; |
Andreas Färber | 259186a | 2013-01-17 18:51:17 +0100 | [diff] [blame] | 2492 | cs->interrupt_request |= CPU_INTERRUPT_EXITTB; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2493 | } |
| 2494 | |
| 2495 | /* Check section/page access permissions. |
| 2496 | Returns the page protection flags, or zero if the access is not |
| 2497 | permitted. */ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2498 | static inline int check_ap(CPUARMState *env, int ap, int domain_prot, |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2499 | int access_type, int is_user) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2500 | { |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2501 | int prot_ro; |
| 2502 | |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2503 | if (domain_prot == 3) { |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2504 | return PAGE_READ | PAGE_WRITE; |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2505 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2506 | |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2507 | if (access_type == 1) |
| 2508 | prot_ro = 0; |
| 2509 | else |
| 2510 | prot_ro = PAGE_READ; |
| 2511 | |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2512 | switch (ap) { |
| 2513 | case 0: |
pbrook | 7860032 | 2006-09-09 14:36:26 +0000 | [diff] [blame] | 2514 | if (access_type == 1) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2515 | return 0; |
| 2516 | switch ((env->cp15.c1_sys >> 8) & 3) { |
| 2517 | case 1: |
| 2518 | return is_user ? 0 : PAGE_READ; |
| 2519 | case 2: |
| 2520 | return PAGE_READ; |
| 2521 | default: |
| 2522 | return 0; |
| 2523 | } |
| 2524 | case 1: |
| 2525 | return is_user ? 0 : PAGE_READ | PAGE_WRITE; |
| 2526 | case 2: |
| 2527 | if (is_user) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2528 | return prot_ro; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2529 | else |
| 2530 | return PAGE_READ | PAGE_WRITE; |
| 2531 | case 3: |
| 2532 | return PAGE_READ | PAGE_WRITE; |
pbrook | d4934d1 | 2008-12-19 12:39:00 +0000 | [diff] [blame] | 2533 | case 4: /* Reserved. */ |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2534 | return 0; |
| 2535 | case 5: |
| 2536 | return is_user ? 0 : prot_ro; |
| 2537 | case 6: |
| 2538 | return prot_ro; |
pbrook | d4934d1 | 2008-12-19 12:39:00 +0000 | [diff] [blame] | 2539 | case 7: |
Jamie Iles | 0ab06d8 | 2011-06-23 01:12:59 +0000 | [diff] [blame] | 2540 | if (!arm_feature (env, ARM_FEATURE_V6K)) |
pbrook | d4934d1 | 2008-12-19 12:39:00 +0000 | [diff] [blame] | 2541 | return 0; |
| 2542 | return prot_ro; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2543 | default: |
| 2544 | abort(); |
| 2545 | } |
| 2546 | } |
| 2547 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2548 | static uint32_t get_level1_table_address(CPUARMState *env, uint32_t address) |
pbrook | b2fa179 | 2008-10-22 19:22:30 +0000 | [diff] [blame] | 2549 | { |
| 2550 | uint32_t table; |
| 2551 | |
| 2552 | if (address & env->cp15.c2_mask) |
| 2553 | table = env->cp15.c2_base1 & 0xffffc000; |
| 2554 | else |
| 2555 | table = env->cp15.c2_base0 & env->cp15.c2_base_mask; |
| 2556 | |
| 2557 | table |= (address >> 18) & 0x3ffc; |
| 2558 | return table; |
| 2559 | } |
| 2560 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2561 | static int get_phys_addr_v5(CPUARMState *env, uint32_t address, int access_type, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2562 | int is_user, hwaddr *phys_ptr, |
Peter Maydell | 77a71dd | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 2563 | int *prot, target_ulong *page_size) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2564 | { |
| 2565 | int code; |
| 2566 | uint32_t table; |
| 2567 | uint32_t desc; |
| 2568 | int type; |
| 2569 | int ap; |
| 2570 | int domain; |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2571 | int domain_prot; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2572 | hwaddr phys_addr; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 2573 | |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2574 | /* Pagetable walk. */ |
| 2575 | /* Lookup l1 descriptor. */ |
pbrook | b2fa179 | 2008-10-22 19:22:30 +0000 | [diff] [blame] | 2576 | table = get_level1_table_address(env, address); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2577 | desc = ldl_phys(table); |
| 2578 | type = (desc & 3); |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2579 | domain = (desc >> 5) & 0x0f; |
| 2580 | domain_prot = (env->cp15.c3 >> (domain * 2)) & 3; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2581 | if (type == 0) { |
balrog | 601d70b | 2008-04-20 01:03:45 +0000 | [diff] [blame] | 2582 | /* Section translation fault. */ |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2583 | code = 5; |
| 2584 | goto do_fault; |
| 2585 | } |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2586 | if (domain_prot == 0 || domain_prot == 2) { |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2587 | if (type == 2) |
| 2588 | code = 9; /* Section domain fault. */ |
| 2589 | else |
| 2590 | code = 11; /* Page domain fault. */ |
| 2591 | goto do_fault; |
| 2592 | } |
| 2593 | if (type == 2) { |
| 2594 | /* 1Mb section. */ |
| 2595 | phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); |
| 2596 | ap = (desc >> 10) & 3; |
| 2597 | code = 13; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2598 | *page_size = 1024 * 1024; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2599 | } else { |
| 2600 | /* Lookup l2 entry. */ |
| 2601 | if (type == 1) { |
| 2602 | /* Coarse pagetable. */ |
| 2603 | table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); |
| 2604 | } else { |
| 2605 | /* Fine pagetable. */ |
| 2606 | table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); |
| 2607 | } |
| 2608 | desc = ldl_phys(table); |
| 2609 | switch (desc & 3) { |
| 2610 | case 0: /* Page translation fault. */ |
| 2611 | code = 7; |
| 2612 | goto do_fault; |
| 2613 | case 1: /* 64k page. */ |
| 2614 | phys_addr = (desc & 0xffff0000) | (address & 0xffff); |
| 2615 | ap = (desc >> (4 + ((address >> 13) & 6))) & 3; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2616 | *page_size = 0x10000; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2617 | break; |
| 2618 | case 2: /* 4k page. */ |
| 2619 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); |
| 2620 | ap = (desc >> (4 + ((address >> 13) & 6))) & 3; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2621 | *page_size = 0x1000; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2622 | break; |
| 2623 | case 3: /* 1k page. */ |
| 2624 | if (type == 1) { |
| 2625 | if (arm_feature(env, ARM_FEATURE_XSCALE)) { |
| 2626 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); |
| 2627 | } else { |
| 2628 | /* Page translation fault. */ |
| 2629 | code = 7; |
| 2630 | goto do_fault; |
| 2631 | } |
| 2632 | } else { |
| 2633 | phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); |
| 2634 | } |
| 2635 | ap = (desc >> 4) & 3; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2636 | *page_size = 0x400; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2637 | break; |
| 2638 | default: |
| 2639 | /* Never happens, but compiler isn't smart enough to tell. */ |
| 2640 | abort(); |
| 2641 | } |
| 2642 | code = 15; |
| 2643 | } |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2644 | *prot = check_ap(env, ap, domain_prot, access_type, is_user); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2645 | if (!*prot) { |
| 2646 | /* Access permission fault. */ |
| 2647 | goto do_fault; |
| 2648 | } |
Rabin Vincent | 3ad493f | 2010-03-20 02:28:03 +0530 | [diff] [blame] | 2649 | *prot |= PAGE_EXEC; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2650 | *phys_ptr = phys_addr; |
| 2651 | return 0; |
| 2652 | do_fault: |
| 2653 | return code | (domain << 4); |
| 2654 | } |
| 2655 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 2656 | static int get_phys_addr_v6(CPUARMState *env, uint32_t address, int access_type, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2657 | int is_user, hwaddr *phys_ptr, |
Peter Maydell | 77a71dd | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 2658 | int *prot, target_ulong *page_size) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2659 | { |
| 2660 | int code; |
| 2661 | uint32_t table; |
| 2662 | uint32_t desc; |
| 2663 | uint32_t xn; |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2664 | uint32_t pxn = 0; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2665 | int type; |
| 2666 | int ap; |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2667 | int domain = 0; |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2668 | int domain_prot; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2669 | hwaddr phys_addr; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2670 | |
| 2671 | /* Pagetable walk. */ |
| 2672 | /* Lookup l1 descriptor. */ |
pbrook | b2fa179 | 2008-10-22 19:22:30 +0000 | [diff] [blame] | 2673 | table = get_level1_table_address(env, address); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2674 | desc = ldl_phys(table); |
| 2675 | type = (desc & 3); |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2676 | if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) { |
| 2677 | /* Section translation fault, or attempt to use the encoding |
| 2678 | * which is Reserved on implementations without PXN. |
| 2679 | */ |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2680 | code = 5; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2681 | goto do_fault; |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2682 | } |
| 2683 | if ((type == 1) || !(desc & (1 << 18))) { |
| 2684 | /* Page or Section. */ |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2685 | domain = (desc >> 5) & 0x0f; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2686 | } |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2687 | domain_prot = (env->cp15.c3 >> (domain * 2)) & 3; |
| 2688 | if (domain_prot == 0 || domain_prot == 2) { |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2689 | if (type != 1) { |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2690 | code = 9; /* Section domain fault. */ |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2691 | } else { |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2692 | code = 11; /* Page domain fault. */ |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2693 | } |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2694 | goto do_fault; |
| 2695 | } |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2696 | if (type != 1) { |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2697 | if (desc & (1 << 18)) { |
| 2698 | /* Supersection. */ |
| 2699 | phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2700 | *page_size = 0x1000000; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2701 | } else { |
| 2702 | /* Section. */ |
| 2703 | phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2704 | *page_size = 0x100000; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2705 | } |
| 2706 | ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); |
| 2707 | xn = desc & (1 << 4); |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2708 | pxn = desc & 1; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2709 | code = 13; |
| 2710 | } else { |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2711 | if (arm_feature(env, ARM_FEATURE_PXN)) { |
| 2712 | pxn = (desc >> 2) & 1; |
| 2713 | } |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2714 | /* Lookup l2 entry. */ |
| 2715 | table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); |
| 2716 | desc = ldl_phys(table); |
| 2717 | ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); |
| 2718 | switch (desc & 3) { |
| 2719 | case 0: /* Page translation fault. */ |
| 2720 | code = 7; |
| 2721 | goto do_fault; |
| 2722 | case 1: /* 64k page. */ |
| 2723 | phys_addr = (desc & 0xffff0000) | (address & 0xffff); |
| 2724 | xn = desc & (1 << 15); |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2725 | *page_size = 0x10000; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2726 | break; |
| 2727 | case 2: case 3: /* 4k page. */ |
| 2728 | phys_addr = (desc & 0xfffff000) | (address & 0xfff); |
| 2729 | xn = desc & 1; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 2730 | *page_size = 0x1000; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2731 | break; |
| 2732 | default: |
| 2733 | /* Never happens, but compiler isn't smart enough to tell. */ |
| 2734 | abort(); |
| 2735 | } |
| 2736 | code = 15; |
| 2737 | } |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2738 | if (domain_prot == 3) { |
Juha Riihimäki | c003432 | 2010-12-08 13:15:16 +0200 | [diff] [blame] | 2739 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
| 2740 | } else { |
Peter Maydell | de9b05b | 2012-07-12 10:59:05 +0000 | [diff] [blame] | 2741 | if (pxn && !is_user) { |
| 2742 | xn = 1; |
| 2743 | } |
Juha Riihimäki | c003432 | 2010-12-08 13:15:16 +0200 | [diff] [blame] | 2744 | if (xn && access_type == 2) |
| 2745 | goto do_fault; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2746 | |
Juha Riihimäki | c003432 | 2010-12-08 13:15:16 +0200 | [diff] [blame] | 2747 | /* The simplified model uses AP[0] as an access control bit. */ |
| 2748 | if ((env->cp15.c1_sys & (1 << 29)) && (ap & 1) == 0) { |
| 2749 | /* Access flag fault. */ |
| 2750 | code = (code == 15) ? 6 : 3; |
| 2751 | goto do_fault; |
| 2752 | } |
Jean-Christophe DUBOIS | dd4ebc2 | 2011-12-13 18:19:23 +0000 | [diff] [blame] | 2753 | *prot = check_ap(env, ap, domain_prot, access_type, is_user); |
Juha Riihimäki | c003432 | 2010-12-08 13:15:16 +0200 | [diff] [blame] | 2754 | if (!*prot) { |
| 2755 | /* Access permission fault. */ |
| 2756 | goto do_fault; |
| 2757 | } |
| 2758 | if (!xn) { |
| 2759 | *prot |= PAGE_EXEC; |
| 2760 | } |
Rabin Vincent | 3ad493f | 2010-03-20 02:28:03 +0530 | [diff] [blame] | 2761 | } |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2762 | *phys_ptr = phys_addr; |
| 2763 | return 0; |
| 2764 | do_fault: |
| 2765 | return code | (domain << 4); |
| 2766 | } |
| 2767 | |
Peter Maydell | 3dde962 | 2012-07-12 10:59:12 +0000 | [diff] [blame] | 2768 | /* Fault type for long-descriptor MMU fault reporting; this corresponds |
| 2769 | * to bits [5..2] in the STATUS field in long-format DFSR/IFSR. |
| 2770 | */ |
| 2771 | typedef enum { |
| 2772 | translation_fault = 1, |
| 2773 | access_fault = 2, |
| 2774 | permission_fault = 3, |
| 2775 | } MMUFaultType; |
| 2776 | |
| 2777 | static int get_phys_addr_lpae(CPUARMState *env, uint32_t address, |
| 2778 | int access_type, int is_user, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2779 | hwaddr *phys_ptr, int *prot, |
Peter Maydell | 3dde962 | 2012-07-12 10:59:12 +0000 | [diff] [blame] | 2780 | target_ulong *page_size_ptr) |
| 2781 | { |
| 2782 | /* Read an LPAE long-descriptor translation table. */ |
| 2783 | MMUFaultType fault_type = translation_fault; |
| 2784 | uint32_t level = 1; |
| 2785 | uint32_t epd; |
| 2786 | uint32_t tsz; |
| 2787 | uint64_t ttbr; |
| 2788 | int ttbr_select; |
| 2789 | int n; |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2790 | hwaddr descaddr; |
Peter Maydell | 3dde962 | 2012-07-12 10:59:12 +0000 | [diff] [blame] | 2791 | uint32_t tableattrs; |
| 2792 | target_ulong page_size; |
| 2793 | uint32_t attrs; |
| 2794 | |
| 2795 | /* Determine whether this address is in the region controlled by |
| 2796 | * TTBR0 or TTBR1 (or if it is in neither region and should fault). |
| 2797 | * This is a Non-secure PL0/1 stage 1 translation, so controlled by |
| 2798 | * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32: |
| 2799 | */ |
| 2800 | uint32_t t0sz = extract32(env->cp15.c2_control, 0, 3); |
| 2801 | uint32_t t1sz = extract32(env->cp15.c2_control, 16, 3); |
| 2802 | if (t0sz && !extract32(address, 32 - t0sz, t0sz)) { |
| 2803 | /* there is a ttbr0 region and we are in it (high bits all zero) */ |
| 2804 | ttbr_select = 0; |
| 2805 | } else if (t1sz && !extract32(~address, 32 - t1sz, t1sz)) { |
| 2806 | /* there is a ttbr1 region and we are in it (high bits all one) */ |
| 2807 | ttbr_select = 1; |
| 2808 | } else if (!t0sz) { |
| 2809 | /* ttbr0 region is "everything not in the ttbr1 region" */ |
| 2810 | ttbr_select = 0; |
| 2811 | } else if (!t1sz) { |
| 2812 | /* ttbr1 region is "everything not in the ttbr0 region" */ |
| 2813 | ttbr_select = 1; |
| 2814 | } else { |
| 2815 | /* in the gap between the two regions, this is a Translation fault */ |
| 2816 | fault_type = translation_fault; |
| 2817 | goto do_fault; |
| 2818 | } |
| 2819 | |
| 2820 | /* Note that QEMU ignores shareability and cacheability attributes, |
| 2821 | * so we don't need to do anything with the SH, ORGN, IRGN fields |
| 2822 | * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the |
| 2823 | * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently |
| 2824 | * implement any ASID-like capability so we can ignore it (instead |
| 2825 | * we will always flush the TLB any time the ASID is changed). |
| 2826 | */ |
| 2827 | if (ttbr_select == 0) { |
| 2828 | ttbr = ((uint64_t)env->cp15.c2_base0_hi << 32) | env->cp15.c2_base0; |
| 2829 | epd = extract32(env->cp15.c2_control, 7, 1); |
| 2830 | tsz = t0sz; |
| 2831 | } else { |
| 2832 | ttbr = ((uint64_t)env->cp15.c2_base1_hi << 32) | env->cp15.c2_base1; |
| 2833 | epd = extract32(env->cp15.c2_control, 23, 1); |
| 2834 | tsz = t1sz; |
| 2835 | } |
| 2836 | |
| 2837 | if (epd) { |
| 2838 | /* Translation table walk disabled => Translation fault on TLB miss */ |
| 2839 | goto do_fault; |
| 2840 | } |
| 2841 | |
| 2842 | /* If the region is small enough we will skip straight to a 2nd level |
| 2843 | * lookup. This affects the number of bits of the address used in |
| 2844 | * combination with the TTBR to find the first descriptor. ('n' here |
| 2845 | * matches the usage in the ARM ARM sB3.6.6, where bits [39..n] are |
| 2846 | * from the TTBR, [n-1..3] from the vaddr, and [2..0] always zero). |
| 2847 | */ |
| 2848 | if (tsz > 1) { |
| 2849 | level = 2; |
| 2850 | n = 14 - tsz; |
| 2851 | } else { |
| 2852 | n = 5 - tsz; |
| 2853 | } |
| 2854 | |
| 2855 | /* Clear the vaddr bits which aren't part of the within-region address, |
| 2856 | * so that we don't have to special case things when calculating the |
| 2857 | * first descriptor address. |
| 2858 | */ |
| 2859 | address &= (0xffffffffU >> tsz); |
| 2860 | |
| 2861 | /* Now we can extract the actual base address from the TTBR */ |
| 2862 | descaddr = extract64(ttbr, 0, 40); |
| 2863 | descaddr &= ~((1ULL << n) - 1); |
| 2864 | |
| 2865 | tableattrs = 0; |
| 2866 | for (;;) { |
| 2867 | uint64_t descriptor; |
| 2868 | |
| 2869 | descaddr |= ((address >> (9 * (4 - level))) & 0xff8); |
| 2870 | descriptor = ldq_phys(descaddr); |
| 2871 | if (!(descriptor & 1) || |
| 2872 | (!(descriptor & 2) && (level == 3))) { |
| 2873 | /* Invalid, or the Reserved level 3 encoding */ |
| 2874 | goto do_fault; |
| 2875 | } |
| 2876 | descaddr = descriptor & 0xfffffff000ULL; |
| 2877 | |
| 2878 | if ((descriptor & 2) && (level < 3)) { |
| 2879 | /* Table entry. The top five bits are attributes which may |
| 2880 | * propagate down through lower levels of the table (and |
| 2881 | * which are all arranged so that 0 means "no effect", so |
| 2882 | * we can gather them up by ORing in the bits at each level). |
| 2883 | */ |
| 2884 | tableattrs |= extract64(descriptor, 59, 5); |
| 2885 | level++; |
| 2886 | continue; |
| 2887 | } |
| 2888 | /* Block entry at level 1 or 2, or page entry at level 3. |
| 2889 | * These are basically the same thing, although the number |
| 2890 | * of bits we pull in from the vaddr varies. |
| 2891 | */ |
| 2892 | page_size = (1 << (39 - (9 * level))); |
| 2893 | descaddr |= (address & (page_size - 1)); |
| 2894 | /* Extract attributes from the descriptor and merge with table attrs */ |
| 2895 | attrs = extract64(descriptor, 2, 10) |
| 2896 | | (extract64(descriptor, 52, 12) << 10); |
| 2897 | attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */ |
| 2898 | attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */ |
| 2899 | /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1 |
| 2900 | * means "force PL1 access only", which means forcing AP[1] to 0. |
| 2901 | */ |
| 2902 | if (extract32(tableattrs, 2, 1)) { |
| 2903 | attrs &= ~(1 << 4); |
| 2904 | } |
| 2905 | /* Since we're always in the Non-secure state, NSTable is ignored. */ |
| 2906 | break; |
| 2907 | } |
| 2908 | /* Here descaddr is the final physical address, and attributes |
| 2909 | * are all in attrs. |
| 2910 | */ |
| 2911 | fault_type = access_fault; |
| 2912 | if ((attrs & (1 << 8)) == 0) { |
| 2913 | /* Access flag */ |
| 2914 | goto do_fault; |
| 2915 | } |
| 2916 | fault_type = permission_fault; |
| 2917 | if (is_user && !(attrs & (1 << 4))) { |
| 2918 | /* Unprivileged access not enabled */ |
| 2919 | goto do_fault; |
| 2920 | } |
| 2921 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
| 2922 | if (attrs & (1 << 12) || (!is_user && (attrs & (1 << 11)))) { |
| 2923 | /* XN or PXN */ |
| 2924 | if (access_type == 2) { |
| 2925 | goto do_fault; |
| 2926 | } |
| 2927 | *prot &= ~PAGE_EXEC; |
| 2928 | } |
| 2929 | if (attrs & (1 << 5)) { |
| 2930 | /* Write access forbidden */ |
| 2931 | if (access_type == 1) { |
| 2932 | goto do_fault; |
| 2933 | } |
| 2934 | *prot &= ~PAGE_WRITE; |
| 2935 | } |
| 2936 | |
| 2937 | *phys_ptr = descaddr; |
| 2938 | *page_size_ptr = page_size; |
| 2939 | return 0; |
| 2940 | |
| 2941 | do_fault: |
| 2942 | /* Long-descriptor format IFSR/DFSR value */ |
| 2943 | return (1 << 9) | (fault_type << 2) | level; |
| 2944 | } |
| 2945 | |
Peter Maydell | 77a71dd | 2012-07-12 10:59:09 +0000 | [diff] [blame] | 2946 | static int get_phys_addr_mpu(CPUARMState *env, uint32_t address, |
| 2947 | int access_type, int is_user, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 2948 | hwaddr *phys_ptr, int *prot) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 2949 | { |
| 2950 | int n; |
| 2951 | uint32_t mask; |
| 2952 | uint32_t base; |
| 2953 | |
| 2954 | *phys_ptr = address; |
| 2955 | for (n = 7; n >= 0; n--) { |
| 2956 | base = env->cp15.c6_region[n]; |
| 2957 | if ((base & 1) == 0) |
| 2958 | continue; |
| 2959 | mask = 1 << ((base >> 1) & 0x1f); |
| 2960 | /* Keep this shift separate from the above to avoid an |
| 2961 | (undefined) << 32. */ |
| 2962 | mask = (mask << 1) - 1; |
| 2963 | if (((base ^ address) & ~mask) == 0) |
| 2964 | break; |
| 2965 | } |
| 2966 | if (n < 0) |
| 2967 | return 2; |
| 2968 | |
| 2969 | if (access_type == 2) { |
| 2970 | mask = env->cp15.c5_insn; |
| 2971 | } else { |
| 2972 | mask = env->cp15.c5_data; |
| 2973 | } |
| 2974 | mask = (mask >> (n * 4)) & 0xf; |
| 2975 | switch (mask) { |
| 2976 | case 0: |
| 2977 | return 1; |
| 2978 | case 1: |
| 2979 | if (is_user) |
| 2980 | return 1; |
| 2981 | *prot = PAGE_READ | PAGE_WRITE; |
| 2982 | break; |
| 2983 | case 2: |
| 2984 | *prot = PAGE_READ; |
| 2985 | if (!is_user) |
| 2986 | *prot |= PAGE_WRITE; |
| 2987 | break; |
| 2988 | case 3: |
| 2989 | *prot = PAGE_READ | PAGE_WRITE; |
| 2990 | break; |
| 2991 | case 5: |
| 2992 | if (is_user) |
| 2993 | return 1; |
| 2994 | *prot = PAGE_READ; |
| 2995 | break; |
| 2996 | case 6: |
| 2997 | *prot = PAGE_READ; |
| 2998 | break; |
| 2999 | default: |
| 3000 | /* Bad permission. */ |
| 3001 | return 1; |
| 3002 | } |
Rabin Vincent | 3ad493f | 2010-03-20 02:28:03 +0530 | [diff] [blame] | 3003 | *prot |= PAGE_EXEC; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3004 | return 0; |
| 3005 | } |
| 3006 | |
Peter Maydell | 702a935 | 2012-07-12 10:59:10 +0000 | [diff] [blame] | 3007 | /* get_phys_addr - get the physical address for this virtual address |
| 3008 | * |
| 3009 | * Find the physical address corresponding to the given virtual address, |
| 3010 | * by doing a translation table walk on MMU based systems or using the |
| 3011 | * MPU state on MPU based systems. |
| 3012 | * |
| 3013 | * Returns 0 if the translation was successful. Otherwise, phys_ptr, |
| 3014 | * prot and page_size are not filled in, and the return value provides |
| 3015 | * information on why the translation aborted, in the format of a |
| 3016 | * DFSR/IFSR fault register, with the following caveats: |
| 3017 | * * we honour the short vs long DFSR format differences. |
| 3018 | * * the WnR bit is never set (the caller must do this). |
| 3019 | * * for MPU based systems we don't bother to return a full FSR format |
| 3020 | * value. |
| 3021 | * |
| 3022 | * @env: CPUARMState |
| 3023 | * @address: virtual address to get physical address for |
| 3024 | * @access_type: 0 for read, 1 for write, 2 for execute |
| 3025 | * @is_user: 0 for privileged access, 1 for user |
| 3026 | * @phys_ptr: set to the physical address corresponding to the virtual address |
| 3027 | * @prot: set to the permissions for the page containing phys_ptr |
| 3028 | * @page_size: set to the size of the page containing phys_ptr |
| 3029 | */ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3030 | static inline int get_phys_addr(CPUARMState *env, uint32_t address, |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3031 | int access_type, int is_user, |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 3032 | hwaddr *phys_ptr, int *prot, |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3033 | target_ulong *page_size) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3034 | { |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3035 | /* Fast Context Switch Extension. */ |
| 3036 | if (address < 0x02000000) |
| 3037 | address += env->cp15.c13_fcse; |
| 3038 | |
| 3039 | if ((env->cp15.c1_sys & 1) == 0) { |
pbrook | ce81986 | 2007-05-08 02:30:40 +0000 | [diff] [blame] | 3040 | /* MMU/MPU disabled. */ |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3041 | *phys_ptr = address; |
Rabin Vincent | 3ad493f | 2010-03-20 02:28:03 +0530 | [diff] [blame] | 3042 | *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3043 | *page_size = TARGET_PAGE_SIZE; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3044 | return 0; |
pbrook | ce81986 | 2007-05-08 02:30:40 +0000 | [diff] [blame] | 3045 | } else if (arm_feature(env, ARM_FEATURE_MPU)) { |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3046 | *page_size = TARGET_PAGE_SIZE; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3047 | return get_phys_addr_mpu(env, address, access_type, is_user, phys_ptr, |
| 3048 | prot); |
Peter Maydell | 3dde962 | 2012-07-12 10:59:12 +0000 | [diff] [blame] | 3049 | } else if (extended_addresses_enabled(env)) { |
| 3050 | return get_phys_addr_lpae(env, address, access_type, is_user, phys_ptr, |
| 3051 | prot, page_size); |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3052 | } else if (env->cp15.c1_sys & (1 << 23)) { |
| 3053 | return get_phys_addr_v6(env, address, access_type, is_user, phys_ptr, |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3054 | prot, page_size); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3055 | } else { |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3056 | return get_phys_addr_v5(env, address, access_type, is_user, phys_ptr, |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3057 | prot, page_size); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3058 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3059 | } |
| 3060 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3061 | int cpu_arm_handle_mmu_fault (CPUARMState *env, target_ulong address, |
Blue Swirl | 97b348e | 2011-08-01 16:12:17 +0000 | [diff] [blame] | 3062 | int access_type, int mmu_idx) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3063 | { |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 3064 | hwaddr phys_addr; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3065 | target_ulong page_size; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3066 | int prot; |
j_mayer | 6ebbf39 | 2007-10-14 07:07:08 +0000 | [diff] [blame] | 3067 | int ret, is_user; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3068 | |
j_mayer | 6ebbf39 | 2007-10-14 07:07:08 +0000 | [diff] [blame] | 3069 | is_user = mmu_idx == MMU_USER_IDX; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3070 | ret = get_phys_addr(env, address, access_type, is_user, &phys_addr, &prot, |
| 3071 | &page_size); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3072 | if (ret == 0) { |
| 3073 | /* Map a single [sub]page. */ |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 3074 | phys_addr &= ~(hwaddr)0x3ff; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3075 | address &= ~(uint32_t)0x3ff; |
Rabin Vincent | 3ad493f | 2010-03-20 02:28:03 +0530 | [diff] [blame] | 3076 | tlb_set_page (env, address, phys_addr, prot, mmu_idx, page_size); |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3077 | return 0; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3078 | } |
| 3079 | |
| 3080 | if (access_type == 2) { |
| 3081 | env->cp15.c5_insn = ret; |
| 3082 | env->cp15.c6_insn = address; |
| 3083 | env->exception_index = EXCP_PREFETCH_ABORT; |
| 3084 | } else { |
| 3085 | env->cp15.c5_data = ret; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3086 | if (access_type == 1 && arm_feature(env, ARM_FEATURE_V6)) |
| 3087 | env->cp15.c5_data |= (1 << 11); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3088 | env->cp15.c6_data = address; |
| 3089 | env->exception_index = EXCP_DATA_ABORT; |
| 3090 | } |
| 3091 | return 1; |
| 3092 | } |
| 3093 | |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 3094 | hwaddr arm_cpu_get_phys_page_debug(CPUState *cs, vaddr addr) |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3095 | { |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 3096 | ARMCPU *cpu = ARM_CPU(cs); |
Avi Kivity | a8170e5 | 2012-10-23 12:30:10 +0200 | [diff] [blame] | 3097 | hwaddr phys_addr; |
Paul Brook | d4c430a | 2010-03-17 02:14:28 +0000 | [diff] [blame] | 3098 | target_ulong page_size; |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3099 | int prot; |
| 3100 | int ret; |
| 3101 | |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 3102 | ret = get_phys_addr(&cpu->env, addr, 0, 0, &phys_addr, &prot, &page_size); |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3103 | |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 3104 | if (ret != 0) { |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3105 | return -1; |
Andreas Färber | 00b941e | 2013-06-29 18:55:54 +0200 | [diff] [blame] | 3106 | } |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3107 | |
| 3108 | return phys_addr; |
| 3109 | } |
| 3110 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3111 | void HELPER(set_r13_banked)(CPUARMState *env, uint32_t mode, uint32_t val) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3112 | { |
Peter Maydell | 39ea3d4 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3113 | if ((env->uncached_cpsr & CPSR_M) == mode) { |
| 3114 | env->regs[13] = val; |
| 3115 | } else { |
Peter Maydell | f520641 | 2013-03-05 00:34:40 +0000 | [diff] [blame] | 3116 | env->banked_r13[bank_number(mode)] = val; |
Peter Maydell | 39ea3d4 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3117 | } |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3118 | } |
| 3119 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3120 | uint32_t HELPER(get_r13_banked)(CPUARMState *env, uint32_t mode) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3121 | { |
Peter Maydell | 39ea3d4 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3122 | if ((env->uncached_cpsr & CPSR_M) == mode) { |
| 3123 | return env->regs[13]; |
| 3124 | } else { |
Peter Maydell | f520641 | 2013-03-05 00:34:40 +0000 | [diff] [blame] | 3125 | return env->banked_r13[bank_number(mode)]; |
Peter Maydell | 39ea3d4 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3126 | } |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3127 | } |
| 3128 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3129 | uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3130 | { |
| 3131 | switch (reg) { |
| 3132 | case 0: /* APSR */ |
| 3133 | return xpsr_read(env) & 0xf8000000; |
| 3134 | case 1: /* IAPSR */ |
| 3135 | return xpsr_read(env) & 0xf80001ff; |
| 3136 | case 2: /* EAPSR */ |
| 3137 | return xpsr_read(env) & 0xff00fc00; |
| 3138 | case 3: /* xPSR */ |
| 3139 | return xpsr_read(env) & 0xff00fdff; |
| 3140 | case 5: /* IPSR */ |
| 3141 | return xpsr_read(env) & 0x000001ff; |
| 3142 | case 6: /* EPSR */ |
| 3143 | return xpsr_read(env) & 0x0700fc00; |
| 3144 | case 7: /* IEPSR */ |
| 3145 | return xpsr_read(env) & 0x0700edff; |
| 3146 | case 8: /* MSP */ |
| 3147 | return env->v7m.current_sp ? env->v7m.other_sp : env->regs[13]; |
| 3148 | case 9: /* PSP */ |
| 3149 | return env->v7m.current_sp ? env->regs[13] : env->v7m.other_sp; |
| 3150 | case 16: /* PRIMASK */ |
| 3151 | return (env->uncached_cpsr & CPSR_I) != 0; |
Sebastian Huber | 8284582 | 2011-05-29 02:58:41 +0000 | [diff] [blame] | 3152 | case 17: /* BASEPRI */ |
| 3153 | case 18: /* BASEPRI_MAX */ |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3154 | return env->v7m.basepri; |
Sebastian Huber | 8284582 | 2011-05-29 02:58:41 +0000 | [diff] [blame] | 3155 | case 19: /* FAULTMASK */ |
| 3156 | return (env->uncached_cpsr & CPSR_F) != 0; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3157 | case 20: /* CONTROL */ |
| 3158 | return env->v7m.control; |
| 3159 | default: |
| 3160 | /* ??? For debugging only. */ |
| 3161 | cpu_abort(env, "Unimplemented system register read (%d)\n", reg); |
| 3162 | return 0; |
| 3163 | } |
| 3164 | } |
| 3165 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3166 | void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3167 | { |
| 3168 | switch (reg) { |
| 3169 | case 0: /* APSR */ |
| 3170 | xpsr_write(env, val, 0xf8000000); |
| 3171 | break; |
| 3172 | case 1: /* IAPSR */ |
| 3173 | xpsr_write(env, val, 0xf8000000); |
| 3174 | break; |
| 3175 | case 2: /* EAPSR */ |
| 3176 | xpsr_write(env, val, 0xfe00fc00); |
| 3177 | break; |
| 3178 | case 3: /* xPSR */ |
| 3179 | xpsr_write(env, val, 0xfe00fc00); |
| 3180 | break; |
| 3181 | case 5: /* IPSR */ |
| 3182 | /* IPSR bits are readonly. */ |
| 3183 | break; |
| 3184 | case 6: /* EPSR */ |
| 3185 | xpsr_write(env, val, 0x0600fc00); |
| 3186 | break; |
| 3187 | case 7: /* IEPSR */ |
| 3188 | xpsr_write(env, val, 0x0600fc00); |
| 3189 | break; |
| 3190 | case 8: /* MSP */ |
| 3191 | if (env->v7m.current_sp) |
| 3192 | env->v7m.other_sp = val; |
| 3193 | else |
| 3194 | env->regs[13] = val; |
| 3195 | break; |
| 3196 | case 9: /* PSP */ |
| 3197 | if (env->v7m.current_sp) |
| 3198 | env->regs[13] = val; |
| 3199 | else |
| 3200 | env->v7m.other_sp = val; |
| 3201 | break; |
| 3202 | case 16: /* PRIMASK */ |
| 3203 | if (val & 1) |
| 3204 | env->uncached_cpsr |= CPSR_I; |
| 3205 | else |
| 3206 | env->uncached_cpsr &= ~CPSR_I; |
| 3207 | break; |
Sebastian Huber | 8284582 | 2011-05-29 02:58:41 +0000 | [diff] [blame] | 3208 | case 17: /* BASEPRI */ |
| 3209 | env->v7m.basepri = val & 0xff; |
| 3210 | break; |
| 3211 | case 18: /* BASEPRI_MAX */ |
| 3212 | val &= 0xff; |
| 3213 | if (val != 0 && (val < env->v7m.basepri || env->v7m.basepri == 0)) |
| 3214 | env->v7m.basepri = val; |
| 3215 | break; |
| 3216 | case 19: /* FAULTMASK */ |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3217 | if (val & 1) |
| 3218 | env->uncached_cpsr |= CPSR_F; |
| 3219 | else |
| 3220 | env->uncached_cpsr &= ~CPSR_F; |
| 3221 | break; |
pbrook | 9ee6e8b | 2007-11-11 00:04:49 +0000 | [diff] [blame] | 3222 | case 20: /* CONTROL */ |
| 3223 | env->v7m.control = val & 3; |
| 3224 | switch_v7m_sp(env, (val & 2) != 0); |
| 3225 | break; |
| 3226 | default: |
| 3227 | /* ??? For debugging only. */ |
| 3228 | cpu_abort(env, "Unimplemented system register write (%d)\n", reg); |
| 3229 | return; |
| 3230 | } |
| 3231 | } |
| 3232 | |
bellard | b5ff1b3 | 2005-11-26 10:38:39 +0000 | [diff] [blame] | 3233 | #endif |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3234 | |
| 3235 | /* Note that signed overflow is undefined in C. The following routines are |
| 3236 | careful to use unsigned types where modulo arithmetic is required. |
| 3237 | Failure to do so _will_ break on newer gcc. */ |
| 3238 | |
| 3239 | /* Signed saturating arithmetic. */ |
| 3240 | |
aurel32 | 1654b2d | 2008-04-11 04:55:07 +0000 | [diff] [blame] | 3241 | /* Perform 16-bit signed saturating addition. */ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3242 | static inline uint16_t add16_sat(uint16_t a, uint16_t b) |
| 3243 | { |
| 3244 | uint16_t res; |
| 3245 | |
| 3246 | res = a + b; |
| 3247 | if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { |
| 3248 | if (a & 0x8000) |
| 3249 | res = 0x8000; |
| 3250 | else |
| 3251 | res = 0x7fff; |
| 3252 | } |
| 3253 | return res; |
| 3254 | } |
| 3255 | |
aurel32 | 1654b2d | 2008-04-11 04:55:07 +0000 | [diff] [blame] | 3256 | /* Perform 8-bit signed saturating addition. */ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3257 | static inline uint8_t add8_sat(uint8_t a, uint8_t b) |
| 3258 | { |
| 3259 | uint8_t res; |
| 3260 | |
| 3261 | res = a + b; |
| 3262 | if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { |
| 3263 | if (a & 0x80) |
| 3264 | res = 0x80; |
| 3265 | else |
| 3266 | res = 0x7f; |
| 3267 | } |
| 3268 | return res; |
| 3269 | } |
| 3270 | |
aurel32 | 1654b2d | 2008-04-11 04:55:07 +0000 | [diff] [blame] | 3271 | /* Perform 16-bit signed saturating subtraction. */ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3272 | static inline uint16_t sub16_sat(uint16_t a, uint16_t b) |
| 3273 | { |
| 3274 | uint16_t res; |
| 3275 | |
| 3276 | res = a - b; |
| 3277 | if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { |
| 3278 | if (a & 0x8000) |
| 3279 | res = 0x8000; |
| 3280 | else |
| 3281 | res = 0x7fff; |
| 3282 | } |
| 3283 | return res; |
| 3284 | } |
| 3285 | |
aurel32 | 1654b2d | 2008-04-11 04:55:07 +0000 | [diff] [blame] | 3286 | /* Perform 8-bit signed saturating subtraction. */ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3287 | static inline uint8_t sub8_sat(uint8_t a, uint8_t b) |
| 3288 | { |
| 3289 | uint8_t res; |
| 3290 | |
| 3291 | res = a - b; |
| 3292 | if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { |
| 3293 | if (a & 0x80) |
| 3294 | res = 0x80; |
| 3295 | else |
| 3296 | res = 0x7f; |
| 3297 | } |
| 3298 | return res; |
| 3299 | } |
| 3300 | |
| 3301 | #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); |
| 3302 | #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); |
| 3303 | #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); |
| 3304 | #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); |
| 3305 | #define PFX q |
| 3306 | |
| 3307 | #include "op_addsub.h" |
| 3308 | |
| 3309 | /* Unsigned saturating arithmetic. */ |
pbrook | 460a09c | 2008-05-01 12:04:35 +0000 | [diff] [blame] | 3310 | static inline uint16_t add16_usat(uint16_t a, uint16_t b) |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3311 | { |
| 3312 | uint16_t res; |
| 3313 | res = a + b; |
| 3314 | if (res < a) |
| 3315 | res = 0xffff; |
| 3316 | return res; |
| 3317 | } |
| 3318 | |
pbrook | 460a09c | 2008-05-01 12:04:35 +0000 | [diff] [blame] | 3319 | static inline uint16_t sub16_usat(uint16_t a, uint16_t b) |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3320 | { |
Chih-Min Chao | 4c4fd3f | 2010-06-28 23:54:06 +0800 | [diff] [blame] | 3321 | if (a > b) |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3322 | return a - b; |
| 3323 | else |
| 3324 | return 0; |
| 3325 | } |
| 3326 | |
| 3327 | static inline uint8_t add8_usat(uint8_t a, uint8_t b) |
| 3328 | { |
| 3329 | uint8_t res; |
| 3330 | res = a + b; |
| 3331 | if (res < a) |
| 3332 | res = 0xff; |
| 3333 | return res; |
| 3334 | } |
| 3335 | |
| 3336 | static inline uint8_t sub8_usat(uint8_t a, uint8_t b) |
| 3337 | { |
Chih-Min Chao | 4c4fd3f | 2010-06-28 23:54:06 +0800 | [diff] [blame] | 3338 | if (a > b) |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3339 | return a - b; |
| 3340 | else |
| 3341 | return 0; |
| 3342 | } |
| 3343 | |
| 3344 | #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); |
| 3345 | #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); |
| 3346 | #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); |
| 3347 | #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); |
| 3348 | #define PFX uq |
| 3349 | |
| 3350 | #include "op_addsub.h" |
| 3351 | |
| 3352 | /* Signed modulo arithmetic. */ |
| 3353 | #define SARITH16(a, b, n, op) do { \ |
| 3354 | int32_t sum; \ |
Peter Maydell | db6e2e6 | 2011-03-10 18:51:49 +0000 | [diff] [blame] | 3355 | sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3356 | RESULT(sum, n, 16); \ |
| 3357 | if (sum >= 0) \ |
| 3358 | ge |= 3 << (n * 2); \ |
| 3359 | } while(0) |
| 3360 | |
| 3361 | #define SARITH8(a, b, n, op) do { \ |
| 3362 | int32_t sum; \ |
Peter Maydell | db6e2e6 | 2011-03-10 18:51:49 +0000 | [diff] [blame] | 3363 | sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3364 | RESULT(sum, n, 8); \ |
| 3365 | if (sum >= 0) \ |
| 3366 | ge |= 1 << n; \ |
| 3367 | } while(0) |
| 3368 | |
| 3369 | |
| 3370 | #define ADD16(a, b, n) SARITH16(a, b, n, +) |
| 3371 | #define SUB16(a, b, n) SARITH16(a, b, n, -) |
| 3372 | #define ADD8(a, b, n) SARITH8(a, b, n, +) |
| 3373 | #define SUB8(a, b, n) SARITH8(a, b, n, -) |
| 3374 | #define PFX s |
| 3375 | #define ARITH_GE |
| 3376 | |
| 3377 | #include "op_addsub.h" |
| 3378 | |
| 3379 | /* Unsigned modulo arithmetic. */ |
| 3380 | #define ADD16(a, b, n) do { \ |
| 3381 | uint32_t sum; \ |
| 3382 | sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ |
| 3383 | RESULT(sum, n, 16); \ |
balrog | a87aa10 | 2008-07-19 10:46:13 +0000 | [diff] [blame] | 3384 | if ((sum >> 16) == 1) \ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3385 | ge |= 3 << (n * 2); \ |
| 3386 | } while(0) |
| 3387 | |
| 3388 | #define ADD8(a, b, n) do { \ |
| 3389 | uint32_t sum; \ |
| 3390 | sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ |
| 3391 | RESULT(sum, n, 8); \ |
balrog | a87aa10 | 2008-07-19 10:46:13 +0000 | [diff] [blame] | 3392 | if ((sum >> 8) == 1) \ |
| 3393 | ge |= 1 << n; \ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3394 | } while(0) |
| 3395 | |
| 3396 | #define SUB16(a, b, n) do { \ |
| 3397 | uint32_t sum; \ |
| 3398 | sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ |
| 3399 | RESULT(sum, n, 16); \ |
| 3400 | if ((sum >> 16) == 0) \ |
| 3401 | ge |= 3 << (n * 2); \ |
| 3402 | } while(0) |
| 3403 | |
| 3404 | #define SUB8(a, b, n) do { \ |
| 3405 | uint32_t sum; \ |
| 3406 | sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ |
| 3407 | RESULT(sum, n, 8); \ |
| 3408 | if ((sum >> 8) == 0) \ |
balrog | a87aa10 | 2008-07-19 10:46:13 +0000 | [diff] [blame] | 3409 | ge |= 1 << n; \ |
pbrook | 6ddbc6e | 2008-03-31 03:46:33 +0000 | [diff] [blame] | 3410 | } while(0) |
| 3411 | |
| 3412 | #define PFX u |
| 3413 | #define ARITH_GE |
| 3414 | |
| 3415 | #include "op_addsub.h" |
| 3416 | |
| 3417 | /* Halved signed arithmetic. */ |
| 3418 | #define ADD16(a, b, n) \ |
| 3419 | RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) |
| 3420 | #define SUB16(a, b, n) \ |
| 3421 | RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) |
| 3422 | #define ADD8(a, b, n) \ |
| 3423 | RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) |
| 3424 | #define SUB8(a, b, n) \ |
| 3425 | RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) |
| 3426 | #define PFX sh |
| 3427 | |
| 3428 | #include "op_addsub.h" |
| 3429 | |
| 3430 | /* Halved unsigned arithmetic. */ |
| 3431 | #define ADD16(a, b, n) \ |
| 3432 | RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) |
| 3433 | #define SUB16(a, b, n) \ |
| 3434 | RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) |
| 3435 | #define ADD8(a, b, n) \ |
| 3436 | RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) |
| 3437 | #define SUB8(a, b, n) \ |
| 3438 | RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) |
| 3439 | #define PFX uh |
| 3440 | |
| 3441 | #include "op_addsub.h" |
| 3442 | |
| 3443 | static inline uint8_t do_usad(uint8_t a, uint8_t b) |
| 3444 | { |
| 3445 | if (a > b) |
| 3446 | return a - b; |
| 3447 | else |
| 3448 | return b - a; |
| 3449 | } |
| 3450 | |
| 3451 | /* Unsigned sum of absolute byte differences. */ |
| 3452 | uint32_t HELPER(usad8)(uint32_t a, uint32_t b) |
| 3453 | { |
| 3454 | uint32_t sum; |
| 3455 | sum = do_usad(a, b); |
| 3456 | sum += do_usad(a >> 8, b >> 8); |
| 3457 | sum += do_usad(a >> 16, b >>16); |
| 3458 | sum += do_usad(a >> 24, b >> 24); |
| 3459 | return sum; |
| 3460 | } |
| 3461 | |
| 3462 | /* For ARMv6 SEL instruction. */ |
| 3463 | uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) |
| 3464 | { |
| 3465 | uint32_t mask; |
| 3466 | |
| 3467 | mask = 0; |
| 3468 | if (flags & 1) |
| 3469 | mask |= 0xff; |
| 3470 | if (flags & 2) |
| 3471 | mask |= 0xff00; |
| 3472 | if (flags & 4) |
| 3473 | mask |= 0xff0000; |
| 3474 | if (flags & 8) |
| 3475 | mask |= 0xff000000; |
| 3476 | return (a & mask) | (b & ~mask); |
| 3477 | } |
| 3478 | |
Peter Maydell | b90372a | 2012-08-06 17:42:18 +0100 | [diff] [blame] | 3479 | /* VFP support. We follow the convention used for VFP instructions: |
| 3480 | Single precision routines have a "s" suffix, double precision a |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3481 | "d" suffix. */ |
| 3482 | |
| 3483 | /* Convert host exception flags to vfp form. */ |
| 3484 | static inline int vfp_exceptbits_from_host(int host_bits) |
| 3485 | { |
| 3486 | int target_bits = 0; |
| 3487 | |
| 3488 | if (host_bits & float_flag_invalid) |
| 3489 | target_bits |= 1; |
| 3490 | if (host_bits & float_flag_divbyzero) |
| 3491 | target_bits |= 2; |
| 3492 | if (host_bits & float_flag_overflow) |
| 3493 | target_bits |= 4; |
Peter Maydell | 36802b6 | 2011-05-19 14:46:18 +0100 | [diff] [blame] | 3494 | if (host_bits & (float_flag_underflow | float_flag_output_denormal)) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3495 | target_bits |= 8; |
| 3496 | if (host_bits & float_flag_inexact) |
| 3497 | target_bits |= 0x10; |
Peter Maydell | cecd850 | 2011-01-06 19:37:55 +0000 | [diff] [blame] | 3498 | if (host_bits & float_flag_input_denormal) |
| 3499 | target_bits |= 0x80; |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3500 | return target_bits; |
| 3501 | } |
| 3502 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3503 | uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3504 | { |
| 3505 | int i; |
| 3506 | uint32_t fpscr; |
| 3507 | |
| 3508 | fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff) |
| 3509 | | (env->vfp.vec_len << 16) |
| 3510 | | (env->vfp.vec_stride << 20); |
| 3511 | i = get_float_exception_flags(&env->vfp.fp_status); |
Peter Maydell | 3a492f3 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3512 | i |= get_float_exception_flags(&env->vfp.standard_fp_status); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3513 | fpscr |= vfp_exceptbits_from_host(i); |
| 3514 | return fpscr; |
| 3515 | } |
| 3516 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3517 | uint32_t vfp_get_fpscr(CPUARMState *env) |
Peter Maydell | 0165329 | 2010-11-24 15:20:04 +0000 | [diff] [blame] | 3518 | { |
| 3519 | return HELPER(vfp_get_fpscr)(env); |
| 3520 | } |
| 3521 | |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3522 | /* Convert vfp exception flags to target form. */ |
| 3523 | static inline int vfp_exceptbits_to_host(int target_bits) |
| 3524 | { |
| 3525 | int host_bits = 0; |
| 3526 | |
| 3527 | if (target_bits & 1) |
| 3528 | host_bits |= float_flag_invalid; |
| 3529 | if (target_bits & 2) |
| 3530 | host_bits |= float_flag_divbyzero; |
| 3531 | if (target_bits & 4) |
| 3532 | host_bits |= float_flag_overflow; |
| 3533 | if (target_bits & 8) |
| 3534 | host_bits |= float_flag_underflow; |
| 3535 | if (target_bits & 0x10) |
| 3536 | host_bits |= float_flag_inexact; |
Peter Maydell | cecd850 | 2011-01-06 19:37:55 +0000 | [diff] [blame] | 3537 | if (target_bits & 0x80) |
| 3538 | host_bits |= float_flag_input_denormal; |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3539 | return host_bits; |
| 3540 | } |
| 3541 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3542 | void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3543 | { |
| 3544 | int i; |
| 3545 | uint32_t changed; |
| 3546 | |
| 3547 | changed = env->vfp.xregs[ARM_VFP_FPSCR]; |
| 3548 | env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff); |
| 3549 | env->vfp.vec_len = (val >> 16) & 7; |
| 3550 | env->vfp.vec_stride = (val >> 20) & 3; |
| 3551 | |
| 3552 | changed ^= val; |
| 3553 | if (changed & (3 << 22)) { |
| 3554 | i = (val >> 22) & 3; |
| 3555 | switch (i) { |
| 3556 | case 0: |
| 3557 | i = float_round_nearest_even; |
| 3558 | break; |
| 3559 | case 1: |
| 3560 | i = float_round_up; |
| 3561 | break; |
| 3562 | case 2: |
| 3563 | i = float_round_down; |
| 3564 | break; |
| 3565 | case 3: |
| 3566 | i = float_round_to_zero; |
| 3567 | break; |
| 3568 | } |
| 3569 | set_float_rounding_mode(i, &env->vfp.fp_status); |
| 3570 | } |
Peter Maydell | cecd850 | 2011-01-06 19:37:55 +0000 | [diff] [blame] | 3571 | if (changed & (1 << 24)) { |
pbrook | fe76d97 | 2008-12-19 14:33:59 +0000 | [diff] [blame] | 3572 | set_flush_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status); |
Peter Maydell | cecd850 | 2011-01-06 19:37:55 +0000 | [diff] [blame] | 3573 | set_flush_inputs_to_zero((val & (1 << 24)) != 0, &env->vfp.fp_status); |
| 3574 | } |
pbrook | 5c7908e | 2008-12-19 13:53:37 +0000 | [diff] [blame] | 3575 | if (changed & (1 << 25)) |
| 3576 | set_default_nan_mode((val & (1 << 25)) != 0, &env->vfp.fp_status); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3577 | |
Peter Maydell | b12c390 | 2011-01-06 19:37:54 +0000 | [diff] [blame] | 3578 | i = vfp_exceptbits_to_host(val); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3579 | set_float_exception_flags(i, &env->vfp.fp_status); |
Peter Maydell | 3a492f3 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3580 | set_float_exception_flags(0, &env->vfp.standard_fp_status); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3581 | } |
| 3582 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3583 | void vfp_set_fpscr(CPUARMState *env, uint32_t val) |
Peter Maydell | 0165329 | 2010-11-24 15:20:04 +0000 | [diff] [blame] | 3584 | { |
| 3585 | HELPER(vfp_set_fpscr)(env, val); |
| 3586 | } |
| 3587 | |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3588 | #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p)) |
| 3589 | |
| 3590 | #define VFP_BINOP(name) \ |
Peter Maydell | ae1857e | 2011-05-25 14:51:48 +0000 | [diff] [blame] | 3591 | float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3592 | { \ |
Peter Maydell | ae1857e | 2011-05-25 14:51:48 +0000 | [diff] [blame] | 3593 | float_status *fpst = fpstp; \ |
| 3594 | return float32_ ## name(a, b, fpst); \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3595 | } \ |
Peter Maydell | ae1857e | 2011-05-25 14:51:48 +0000 | [diff] [blame] | 3596 | float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3597 | { \ |
Peter Maydell | ae1857e | 2011-05-25 14:51:48 +0000 | [diff] [blame] | 3598 | float_status *fpst = fpstp; \ |
| 3599 | return float64_ ## name(a, b, fpst); \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3600 | } |
| 3601 | VFP_BINOP(add) |
| 3602 | VFP_BINOP(sub) |
| 3603 | VFP_BINOP(mul) |
| 3604 | VFP_BINOP(div) |
| 3605 | #undef VFP_BINOP |
| 3606 | |
| 3607 | float32 VFP_HELPER(neg, s)(float32 a) |
| 3608 | { |
| 3609 | return float32_chs(a); |
| 3610 | } |
| 3611 | |
| 3612 | float64 VFP_HELPER(neg, d)(float64 a) |
| 3613 | { |
balrog | 66230e0 | 2008-04-20 00:58:01 +0000 | [diff] [blame] | 3614 | return float64_chs(a); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3615 | } |
| 3616 | |
| 3617 | float32 VFP_HELPER(abs, s)(float32 a) |
| 3618 | { |
| 3619 | return float32_abs(a); |
| 3620 | } |
| 3621 | |
| 3622 | float64 VFP_HELPER(abs, d)(float64 a) |
| 3623 | { |
balrog | 66230e0 | 2008-04-20 00:58:01 +0000 | [diff] [blame] | 3624 | return float64_abs(a); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3625 | } |
| 3626 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3627 | float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3628 | { |
| 3629 | return float32_sqrt(a, &env->vfp.fp_status); |
| 3630 | } |
| 3631 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3632 | float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3633 | { |
| 3634 | return float64_sqrt(a, &env->vfp.fp_status); |
| 3635 | } |
| 3636 | |
| 3637 | /* XXX: check quiet/signaling case */ |
| 3638 | #define DO_VFP_cmp(p, type) \ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3639 | void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3640 | { \ |
| 3641 | uint32_t flags; \ |
| 3642 | switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \ |
| 3643 | case 0: flags = 0x6; break; \ |
| 3644 | case -1: flags = 0x8; break; \ |
| 3645 | case 1: flags = 0x2; break; \ |
| 3646 | default: case 2: flags = 0x3; break; \ |
| 3647 | } \ |
| 3648 | env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ |
| 3649 | | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ |
| 3650 | } \ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3651 | void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3652 | { \ |
| 3653 | uint32_t flags; \ |
| 3654 | switch(type ## _compare(a, b, &env->vfp.fp_status)) { \ |
| 3655 | case 0: flags = 0x6; break; \ |
| 3656 | case -1: flags = 0x8; break; \ |
| 3657 | case 1: flags = 0x2; break; \ |
| 3658 | default: case 2: flags = 0x3; break; \ |
| 3659 | } \ |
| 3660 | env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ |
| 3661 | | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ |
| 3662 | } |
| 3663 | DO_VFP_cmp(s, float32) |
| 3664 | DO_VFP_cmp(d, float64) |
| 3665 | #undef DO_VFP_cmp |
| 3666 | |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3667 | /* Integer to float and float to integer conversions */ |
| 3668 | |
| 3669 | #define CONV_ITOF(name, fsz, sign) \ |
| 3670 | float##fsz HELPER(name)(uint32_t x, void *fpstp) \ |
| 3671 | { \ |
| 3672 | float_status *fpst = fpstp; \ |
Peter Maydell | 8583697 | 2012-01-25 11:49:46 +0000 | [diff] [blame] | 3673 | return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3674 | } |
| 3675 | |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3676 | #define CONV_FTOI(name, fsz, sign, round) \ |
| 3677 | uint32_t HELPER(name)(float##fsz x, void *fpstp) \ |
| 3678 | { \ |
| 3679 | float_status *fpst = fpstp; \ |
| 3680 | if (float##fsz##_is_any_nan(x)) { \ |
| 3681 | float_raise(float_flag_invalid, fpst); \ |
| 3682 | return 0; \ |
| 3683 | } \ |
| 3684 | return float##fsz##_to_##sign##int32##round(x, fpst); \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3685 | } |
| 3686 | |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3687 | #define FLOAT_CONVS(name, p, fsz, sign) \ |
| 3688 | CONV_ITOF(vfp_##name##to##p, fsz, sign) \ |
| 3689 | CONV_FTOI(vfp_to##name##p, fsz, sign, ) \ |
| 3690 | CONV_FTOI(vfp_to##name##z##p, fsz, sign, _round_to_zero) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3691 | |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3692 | FLOAT_CONVS(si, s, 32, ) |
| 3693 | FLOAT_CONVS(si, d, 64, ) |
| 3694 | FLOAT_CONVS(ui, s, 32, u) |
| 3695 | FLOAT_CONVS(ui, d, 64, u) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3696 | |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3697 | #undef CONV_ITOF |
| 3698 | #undef CONV_FTOI |
| 3699 | #undef FLOAT_CONVS |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3700 | |
| 3701 | /* floating point conversion */ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3702 | float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3703 | { |
Peter Maydell | 2d62773 | 2010-12-07 15:37:34 +0000 | [diff] [blame] | 3704 | float64 r = float32_to_float64(x, &env->vfp.fp_status); |
| 3705 | /* ARM requires that S<->D conversion of any kind of NaN generates |
| 3706 | * a quiet NaN by forcing the most significant frac bit to 1. |
| 3707 | */ |
| 3708 | return float64_maybe_silence_nan(r); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3709 | } |
| 3710 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3711 | float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3712 | { |
Peter Maydell | 2d62773 | 2010-12-07 15:37:34 +0000 | [diff] [blame] | 3713 | float32 r = float64_to_float32(x, &env->vfp.fp_status); |
| 3714 | /* ARM requires that S<->D conversion of any kind of NaN generates |
| 3715 | * a quiet NaN by forcing the most significant frac bit to 1. |
| 3716 | */ |
| 3717 | return float32_maybe_silence_nan(r); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3718 | } |
| 3719 | |
| 3720 | /* VFP3 fixed point conversion. */ |
Peter Maydell | 622465e | 2011-03-14 07:23:11 +0000 | [diff] [blame] | 3721 | #define VFP_CONV_FIX(name, p, fsz, itype, sign) \ |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3722 | float##fsz HELPER(vfp_##name##to##p)(uint##fsz##_t x, uint32_t shift, \ |
| 3723 | void *fpstp) \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3724 | { \ |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3725 | float_status *fpst = fpstp; \ |
Peter Maydell | 622465e | 2011-03-14 07:23:11 +0000 | [diff] [blame] | 3726 | float##fsz tmp; \ |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3727 | tmp = sign##int32_to_##float##fsz((itype##_t)x, fpst); \ |
| 3728 | return float##fsz##_scalbn(tmp, -(int)shift, fpst); \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3729 | } \ |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3730 | uint##fsz##_t HELPER(vfp_to##name##p)(float##fsz x, uint32_t shift, \ |
| 3731 | void *fpstp) \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3732 | { \ |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3733 | float_status *fpst = fpstp; \ |
Peter Maydell | 622465e | 2011-03-14 07:23:11 +0000 | [diff] [blame] | 3734 | float##fsz tmp; \ |
| 3735 | if (float##fsz##_is_any_nan(x)) { \ |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3736 | float_raise(float_flag_invalid, fpst); \ |
Peter Maydell | 622465e | 2011-03-14 07:23:11 +0000 | [diff] [blame] | 3737 | return 0; \ |
Peter Maydell | 09d9487 | 2010-12-07 15:37:34 +0000 | [diff] [blame] | 3738 | } \ |
Peter Maydell | 5500b06 | 2011-05-19 14:46:19 +0100 | [diff] [blame] | 3739 | tmp = float##fsz##_scalbn(x, shift, fpst); \ |
| 3740 | return float##fsz##_to_##itype##_round_to_zero(tmp, fpst); \ |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3741 | } |
| 3742 | |
Peter Maydell | 622465e | 2011-03-14 07:23:11 +0000 | [diff] [blame] | 3743 | VFP_CONV_FIX(sh, d, 64, int16, ) |
| 3744 | VFP_CONV_FIX(sl, d, 64, int32, ) |
| 3745 | VFP_CONV_FIX(uh, d, 64, uint16, u) |
| 3746 | VFP_CONV_FIX(ul, d, 64, uint32, u) |
| 3747 | VFP_CONV_FIX(sh, s, 32, int16, ) |
| 3748 | VFP_CONV_FIX(sl, s, 32, int32, ) |
| 3749 | VFP_CONV_FIX(uh, s, 32, uint16, u) |
| 3750 | VFP_CONV_FIX(ul, s, 32, uint32, u) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3751 | #undef VFP_CONV_FIX |
| 3752 | |
Paul Brook | 6001149 | 2009-11-19 16:45:20 +0000 | [diff] [blame] | 3753 | /* Half precision conversions. */ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3754 | static float32 do_fcvt_f16_to_f32(uint32_t a, CPUARMState *env, float_status *s) |
Paul Brook | 6001149 | 2009-11-19 16:45:20 +0000 | [diff] [blame] | 3755 | { |
Paul Brook | 6001149 | 2009-11-19 16:45:20 +0000 | [diff] [blame] | 3756 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; |
Peter Maydell | fb91678 | 2011-02-10 11:29:00 +0000 | [diff] [blame] | 3757 | float32 r = float16_to_float32(make_float16(a), ieee, s); |
| 3758 | if (ieee) { |
| 3759 | return float32_maybe_silence_nan(r); |
| 3760 | } |
| 3761 | return r; |
Paul Brook | 6001149 | 2009-11-19 16:45:20 +0000 | [diff] [blame] | 3762 | } |
| 3763 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3764 | static uint32_t do_fcvt_f32_to_f16(float32 a, CPUARMState *env, float_status *s) |
Paul Brook | 6001149 | 2009-11-19 16:45:20 +0000 | [diff] [blame] | 3765 | { |
Paul Brook | 6001149 | 2009-11-19 16:45:20 +0000 | [diff] [blame] | 3766 | int ieee = (env->vfp.xregs[ARM_VFP_FPSCR] & (1 << 26)) == 0; |
Peter Maydell | fb91678 | 2011-02-10 11:29:00 +0000 | [diff] [blame] | 3767 | float16 r = float32_to_float16(a, ieee, s); |
| 3768 | if (ieee) { |
| 3769 | r = float16_maybe_silence_nan(r); |
| 3770 | } |
| 3771 | return float16_val(r); |
Paul Brook | 6001149 | 2009-11-19 16:45:20 +0000 | [diff] [blame] | 3772 | } |
| 3773 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3774 | float32 HELPER(neon_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env) |
Peter Maydell | 2d981da | 2011-02-10 11:29:01 +0000 | [diff] [blame] | 3775 | { |
| 3776 | return do_fcvt_f16_to_f32(a, env, &env->vfp.standard_fp_status); |
| 3777 | } |
| 3778 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3779 | uint32_t HELPER(neon_fcvt_f32_to_f16)(float32 a, CPUARMState *env) |
Peter Maydell | 2d981da | 2011-02-10 11:29:01 +0000 | [diff] [blame] | 3780 | { |
| 3781 | return do_fcvt_f32_to_f16(a, env, &env->vfp.standard_fp_status); |
| 3782 | } |
| 3783 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3784 | float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, CPUARMState *env) |
Peter Maydell | 2d981da | 2011-02-10 11:29:01 +0000 | [diff] [blame] | 3785 | { |
| 3786 | return do_fcvt_f16_to_f32(a, env, &env->vfp.fp_status); |
| 3787 | } |
| 3788 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3789 | uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, CPUARMState *env) |
Peter Maydell | 2d981da | 2011-02-10 11:29:01 +0000 | [diff] [blame] | 3790 | { |
| 3791 | return do_fcvt_f32_to_f16(a, env, &env->vfp.fp_status); |
| 3792 | } |
| 3793 | |
Peter Maydell | dda3ec4 | 2011-03-14 15:37:12 +0000 | [diff] [blame] | 3794 | #define float32_two make_float32(0x40000000) |
Peter Maydell | 6aae3df | 2011-03-14 15:37:13 +0000 | [diff] [blame] | 3795 | #define float32_three make_float32(0x40400000) |
| 3796 | #define float32_one_point_five make_float32(0x3fc00000) |
Peter Maydell | dda3ec4 | 2011-03-14 15:37:12 +0000 | [diff] [blame] | 3797 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3798 | float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3799 | { |
Peter Maydell | dda3ec4 | 2011-03-14 15:37:12 +0000 | [diff] [blame] | 3800 | float_status *s = &env->vfp.standard_fp_status; |
| 3801 | if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || |
| 3802 | (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { |
Peter Maydell | 43fe9bd | 2011-05-19 14:46:15 +0100 | [diff] [blame] | 3803 | if (!(float32_is_zero(a) || float32_is_zero(b))) { |
| 3804 | float_raise(float_flag_input_denormal, s); |
| 3805 | } |
Peter Maydell | dda3ec4 | 2011-03-14 15:37:12 +0000 | [diff] [blame] | 3806 | return float32_two; |
| 3807 | } |
| 3808 | return float32_sub(float32_two, float32_mul(a, b, s), s); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3809 | } |
| 3810 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3811 | float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3812 | { |
Peter Maydell | 7182696 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3813 | float_status *s = &env->vfp.standard_fp_status; |
Peter Maydell | 9ea62f5 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3814 | float32 product; |
| 3815 | if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || |
| 3816 | (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { |
Peter Maydell | 43fe9bd | 2011-05-19 14:46:15 +0100 | [diff] [blame] | 3817 | if (!(float32_is_zero(a) || float32_is_zero(b))) { |
| 3818 | float_raise(float_flag_input_denormal, s); |
| 3819 | } |
Peter Maydell | 6aae3df | 2011-03-14 15:37:13 +0000 | [diff] [blame] | 3820 | return float32_one_point_five; |
Peter Maydell | 9ea62f5 | 2011-01-14 20:39:18 +0100 | [diff] [blame] | 3821 | } |
Peter Maydell | 6aae3df | 2011-03-14 15:37:13 +0000 | [diff] [blame] | 3822 | product = float32_mul(a, b, s); |
| 3823 | return float32_div(float32_sub(float32_three, product, s), float32_two, s); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3824 | } |
| 3825 | |
pbrook | 8f8e3aa | 2008-03-31 03:48:01 +0000 | [diff] [blame] | 3826 | /* NEON helpers. */ |
| 3827 | |
Christophe Lyon | 56bf4fe | 2011-02-21 17:38:46 +0100 | [diff] [blame] | 3828 | /* Constants 256 and 512 are used in some helpers; we avoid relying on |
| 3829 | * int->float conversions at run-time. */ |
| 3830 | #define float64_256 make_float64(0x4070000000000000LL) |
| 3831 | #define float64_512 make_float64(0x4080000000000000LL) |
| 3832 | |
Christophe Lyon | fe0e487 | 2011-02-21 17:38:47 +0100 | [diff] [blame] | 3833 | /* The algorithm that must be used to calculate the estimate |
| 3834 | * is specified by the ARM ARM. |
| 3835 | */ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3836 | static float64 recip_estimate(float64 a, CPUARMState *env) |
Christophe Lyon | fe0e487 | 2011-02-21 17:38:47 +0100 | [diff] [blame] | 3837 | { |
Peter Maydell | 1146a81 | 2011-05-19 14:46:14 +0100 | [diff] [blame] | 3838 | /* These calculations mustn't set any fp exception flags, |
| 3839 | * so we use a local copy of the fp_status. |
| 3840 | */ |
| 3841 | float_status dummy_status = env->vfp.standard_fp_status; |
| 3842 | float_status *s = &dummy_status; |
Christophe Lyon | fe0e487 | 2011-02-21 17:38:47 +0100 | [diff] [blame] | 3843 | /* q = (int)(a * 512.0) */ |
| 3844 | float64 q = float64_mul(float64_512, a, s); |
| 3845 | int64_t q_int = float64_to_int64_round_to_zero(q, s); |
| 3846 | |
| 3847 | /* r = 1.0 / (((double)q + 0.5) / 512.0) */ |
| 3848 | q = int64_to_float64(q_int, s); |
| 3849 | q = float64_add(q, float64_half, s); |
| 3850 | q = float64_div(q, float64_512, s); |
| 3851 | q = float64_div(float64_one, q, s); |
| 3852 | |
| 3853 | /* s = (int)(256.0 * r + 0.5) */ |
| 3854 | q = float64_mul(q, float64_256, s); |
| 3855 | q = float64_add(q, float64_half, s); |
| 3856 | q_int = float64_to_int64_round_to_zero(q, s); |
| 3857 | |
| 3858 | /* return (double)s / 256.0 */ |
| 3859 | return float64_div(int64_to_float64(q_int, s), float64_256, s); |
| 3860 | } |
| 3861 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3862 | float32 HELPER(recpe_f32)(float32 a, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3863 | { |
Christophe Lyon | fe0e487 | 2011-02-21 17:38:47 +0100 | [diff] [blame] | 3864 | float_status *s = &env->vfp.standard_fp_status; |
| 3865 | float64 f64; |
| 3866 | uint32_t val32 = float32_val(a); |
| 3867 | |
| 3868 | int result_exp; |
| 3869 | int a_exp = (val32 & 0x7f800000) >> 23; |
| 3870 | int sign = val32 & 0x80000000; |
| 3871 | |
| 3872 | if (float32_is_any_nan(a)) { |
| 3873 | if (float32_is_signaling_nan(a)) { |
| 3874 | float_raise(float_flag_invalid, s); |
| 3875 | } |
| 3876 | return float32_default_nan; |
| 3877 | } else if (float32_is_infinity(a)) { |
| 3878 | return float32_set_sign(float32_zero, float32_is_neg(a)); |
| 3879 | } else if (float32_is_zero_or_denormal(a)) { |
Peter Maydell | 43fe9bd | 2011-05-19 14:46:15 +0100 | [diff] [blame] | 3880 | if (!float32_is_zero(a)) { |
| 3881 | float_raise(float_flag_input_denormal, s); |
| 3882 | } |
Christophe Lyon | fe0e487 | 2011-02-21 17:38:47 +0100 | [diff] [blame] | 3883 | float_raise(float_flag_divbyzero, s); |
| 3884 | return float32_set_sign(float32_infinity, float32_is_neg(a)); |
| 3885 | } else if (a_exp >= 253) { |
| 3886 | float_raise(float_flag_underflow, s); |
| 3887 | return float32_set_sign(float32_zero, float32_is_neg(a)); |
| 3888 | } |
| 3889 | |
| 3890 | f64 = make_float64((0x3feULL << 52) |
| 3891 | | ((int64_t)(val32 & 0x7fffff) << 29)); |
| 3892 | |
| 3893 | result_exp = 253 - a_exp; |
| 3894 | |
| 3895 | f64 = recip_estimate(f64, env); |
| 3896 | |
| 3897 | val32 = sign |
| 3898 | | ((result_exp & 0xff) << 23) |
| 3899 | | ((float64_val(f64) >> 29) & 0x7fffff); |
| 3900 | return make_float32(val32); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3901 | } |
| 3902 | |
Christophe Lyon | e07be5d | 2011-02-21 17:38:48 +0100 | [diff] [blame] | 3903 | /* The algorithm that must be used to calculate the estimate |
| 3904 | * is specified by the ARM ARM. |
| 3905 | */ |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3906 | static float64 recip_sqrt_estimate(float64 a, CPUARMState *env) |
Christophe Lyon | e07be5d | 2011-02-21 17:38:48 +0100 | [diff] [blame] | 3907 | { |
Peter Maydell | 1146a81 | 2011-05-19 14:46:14 +0100 | [diff] [blame] | 3908 | /* These calculations mustn't set any fp exception flags, |
| 3909 | * so we use a local copy of the fp_status. |
| 3910 | */ |
| 3911 | float_status dummy_status = env->vfp.standard_fp_status; |
| 3912 | float_status *s = &dummy_status; |
Christophe Lyon | e07be5d | 2011-02-21 17:38:48 +0100 | [diff] [blame] | 3913 | float64 q; |
| 3914 | int64_t q_int; |
| 3915 | |
| 3916 | if (float64_lt(a, float64_half, s)) { |
| 3917 | /* range 0.25 <= a < 0.5 */ |
| 3918 | |
| 3919 | /* a in units of 1/512 rounded down */ |
| 3920 | /* q0 = (int)(a * 512.0); */ |
| 3921 | q = float64_mul(float64_512, a, s); |
| 3922 | q_int = float64_to_int64_round_to_zero(q, s); |
| 3923 | |
| 3924 | /* reciprocal root r */ |
| 3925 | /* r = 1.0 / sqrt(((double)q0 + 0.5) / 512.0); */ |
| 3926 | q = int64_to_float64(q_int, s); |
| 3927 | q = float64_add(q, float64_half, s); |
| 3928 | q = float64_div(q, float64_512, s); |
| 3929 | q = float64_sqrt(q, s); |
| 3930 | q = float64_div(float64_one, q, s); |
| 3931 | } else { |
| 3932 | /* range 0.5 <= a < 1.0 */ |
| 3933 | |
| 3934 | /* a in units of 1/256 rounded down */ |
| 3935 | /* q1 = (int)(a * 256.0); */ |
| 3936 | q = float64_mul(float64_256, a, s); |
| 3937 | int64_t q_int = float64_to_int64_round_to_zero(q, s); |
| 3938 | |
| 3939 | /* reciprocal root r */ |
| 3940 | /* r = 1.0 /sqrt(((double)q1 + 0.5) / 256); */ |
| 3941 | q = int64_to_float64(q_int, s); |
| 3942 | q = float64_add(q, float64_half, s); |
| 3943 | q = float64_div(q, float64_256, s); |
| 3944 | q = float64_sqrt(q, s); |
| 3945 | q = float64_div(float64_one, q, s); |
| 3946 | } |
| 3947 | /* r in units of 1/256 rounded to nearest */ |
| 3948 | /* s = (int)(256.0 * r + 0.5); */ |
| 3949 | |
| 3950 | q = float64_mul(q, float64_256,s ); |
| 3951 | q = float64_add(q, float64_half, s); |
| 3952 | q_int = float64_to_int64_round_to_zero(q, s); |
| 3953 | |
| 3954 | /* return (double)s / 256.0;*/ |
| 3955 | return float64_div(int64_to_float64(q_int, s), float64_256, s); |
| 3956 | } |
| 3957 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 3958 | float32 HELPER(rsqrte_f32)(float32 a, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 3959 | { |
Christophe Lyon | e07be5d | 2011-02-21 17:38:48 +0100 | [diff] [blame] | 3960 | float_status *s = &env->vfp.standard_fp_status; |
| 3961 | int result_exp; |
| 3962 | float64 f64; |
| 3963 | uint32_t val; |
| 3964 | uint64_t val64; |
| 3965 | |
| 3966 | val = float32_val(a); |
| 3967 | |
| 3968 | if (float32_is_any_nan(a)) { |
| 3969 | if (float32_is_signaling_nan(a)) { |
| 3970 | float_raise(float_flag_invalid, s); |
| 3971 | } |
| 3972 | return float32_default_nan; |
| 3973 | } else if (float32_is_zero_or_denormal(a)) { |
Peter Maydell | 43fe9bd | 2011-05-19 14:46:15 +0100 | [diff] [blame] | 3974 | if (!float32_is_zero(a)) { |
| 3975 | float_raise(float_flag_input_denormal, s); |
| 3976 | } |
Christophe Lyon | e07be5d | 2011-02-21 17:38:48 +0100 | [diff] [blame] | 3977 | float_raise(float_flag_divbyzero, s); |
| 3978 | return float32_set_sign(float32_infinity, float32_is_neg(a)); |
| 3979 | } else if (float32_is_neg(a)) { |
| 3980 | float_raise(float_flag_invalid, s); |
| 3981 | return float32_default_nan; |
| 3982 | } else if (float32_is_infinity(a)) { |
| 3983 | return float32_zero; |
| 3984 | } |
| 3985 | |
| 3986 | /* Normalize to a double-precision value between 0.25 and 1.0, |
| 3987 | * preserving the parity of the exponent. */ |
| 3988 | if ((val & 0x800000) == 0) { |
| 3989 | f64 = make_float64(((uint64_t)(val & 0x80000000) << 32) |
| 3990 | | (0x3feULL << 52) |
| 3991 | | ((uint64_t)(val & 0x7fffff) << 29)); |
| 3992 | } else { |
| 3993 | f64 = make_float64(((uint64_t)(val & 0x80000000) << 32) |
| 3994 | | (0x3fdULL << 52) |
| 3995 | | ((uint64_t)(val & 0x7fffff) << 29)); |
| 3996 | } |
| 3997 | |
| 3998 | result_exp = (380 - ((val & 0x7f800000) >> 23)) / 2; |
| 3999 | |
| 4000 | f64 = recip_sqrt_estimate(f64, env); |
| 4001 | |
| 4002 | val64 = float64_val(f64); |
| 4003 | |
Christophe LYON | 26cc6ab | 2011-10-19 16:14:05 +0000 | [diff] [blame] | 4004 | val = ((result_exp & 0xff) << 23) |
Christophe Lyon | e07be5d | 2011-02-21 17:38:48 +0100 | [diff] [blame] | 4005 | | ((val64 >> 29) & 0x7fffff); |
| 4006 | return make_float32(val); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 4007 | } |
| 4008 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 4009 | uint32_t HELPER(recpe_u32)(uint32_t a, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 4010 | { |
Christophe Lyon | fe0e487 | 2011-02-21 17:38:47 +0100 | [diff] [blame] | 4011 | float64 f64; |
| 4012 | |
| 4013 | if ((a & 0x80000000) == 0) { |
| 4014 | return 0xffffffff; |
| 4015 | } |
| 4016 | |
| 4017 | f64 = make_float64((0x3feULL << 52) |
| 4018 | | ((int64_t)(a & 0x7fffffff) << 21)); |
| 4019 | |
| 4020 | f64 = recip_estimate (f64, env); |
| 4021 | |
| 4022 | return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 4023 | } |
| 4024 | |
Andreas Färber | 0ecb72a | 2012-03-14 01:38:21 +0100 | [diff] [blame] | 4025 | uint32_t HELPER(rsqrte_u32)(uint32_t a, CPUARMState *env) |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 4026 | { |
Christophe Lyon | e07be5d | 2011-02-21 17:38:48 +0100 | [diff] [blame] | 4027 | float64 f64; |
| 4028 | |
| 4029 | if ((a & 0xc0000000) == 0) { |
| 4030 | return 0xffffffff; |
| 4031 | } |
| 4032 | |
| 4033 | if (a & 0x80000000) { |
| 4034 | f64 = make_float64((0x3feULL << 52) |
| 4035 | | ((uint64_t)(a & 0x7fffffff) << 21)); |
| 4036 | } else { /* bits 31-30 == '01' */ |
| 4037 | f64 = make_float64((0x3fdULL << 52) |
| 4038 | | ((uint64_t)(a & 0x3fffffff) << 22)); |
| 4039 | } |
| 4040 | |
| 4041 | f64 = recip_sqrt_estimate(f64, env); |
| 4042 | |
| 4043 | return 0x80000000 | ((float64_val(f64) >> 21) & 0x7fffffff); |
pbrook | 4373f3c | 2008-03-31 03:47:19 +0000 | [diff] [blame] | 4044 | } |
pbrook | fe1479c | 2008-12-19 13:18:36 +0000 | [diff] [blame] | 4045 | |
Peter Maydell | da97f52 | 2011-10-19 16:14:07 +0000 | [diff] [blame] | 4046 | /* VFPv4 fused multiply-accumulate */ |
| 4047 | float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp) |
| 4048 | { |
| 4049 | float_status *fpst = fpstp; |
| 4050 | return float32_muladd(a, b, c, 0, fpst); |
| 4051 | } |
| 4052 | |
| 4053 | float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp) |
| 4054 | { |
| 4055 | float_status *fpst = fpstp; |
| 4056 | return float64_muladd(a, b, c, 0, fpst); |
| 4057 | } |