1 #include "qemu/osdep.h" 2 #include "target/arm/idau.h" 3 #include "trace.h" 4 #include "cpu.h" 5 #include "internals.h" 6 #include "exec/gdbstub.h" 7 #include "exec/helper-proto.h" 8 #include "qemu/host-utils.h" 9 #include "sysemu/arch_init.h" 10 #include "sysemu/sysemu.h" 11 #include "qemu/bitops.h" 12 #include "qemu/crc32c.h" 13 #include "exec/exec-all.h" 14 #include "exec/cpu_ldst.h" 15 #include "arm_ldst.h" 16 #include <zlib.h> /* For crc32 */ 17 #include "exec/semihost.h" 18 #include "sysemu/kvm.h" 19 #include "fpu/softfloat.h" 20 #include "qemu/range.h" 21 22 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */ 23 24 #ifndef CONFIG_USER_ONLY 25 /* Cacheability and shareability attributes for a memory access */ 26 typedef struct ARMCacheAttrs { 27 unsigned int attrs:8; /* as in the MAIR register encoding */ 28 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */ 29 } ARMCacheAttrs; 30 31 static bool get_phys_addr(CPUARMState *env, target_ulong address, 32 MMUAccessType access_type, ARMMMUIdx mmu_idx, 33 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 34 target_ulong *page_size, 35 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs); 36 37 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, 38 MMUAccessType access_type, ARMMMUIdx mmu_idx, 39 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 40 target_ulong *page_size_ptr, 41 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs); 42 43 /* Security attributes for an address, as returned by v8m_security_lookup. */ 44 typedef struct V8M_SAttributes { 45 bool subpage; /* true if these attrs don't cover the whole TARGET_PAGE */ 46 bool ns; 47 bool nsc; 48 uint8_t sregion; 49 bool srvalid; 50 uint8_t iregion; 51 bool irvalid; 52 } V8M_SAttributes; 53 54 static void v8m_security_lookup(CPUARMState *env, uint32_t address, 55 MMUAccessType access_type, ARMMMUIdx mmu_idx, 56 V8M_SAttributes *sattrs); 57 #endif 58 59 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) 60 { 61 int nregs; 62 63 /* VFP data registers are always little-endian. */ 64 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; 65 if (reg < nregs) { 66 stq_le_p(buf, *aa32_vfp_dreg(env, reg)); 67 return 8; 68 } 69 if (arm_feature(env, ARM_FEATURE_NEON)) { 70 /* Aliases for Q regs. */ 71 nregs += 16; 72 if (reg < nregs) { 73 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 74 stq_le_p(buf, q[0]); 75 stq_le_p(buf + 8, q[1]); 76 return 16; 77 } 78 } 79 switch (reg - nregs) { 80 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4; 81 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4; 82 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4; 83 } 84 return 0; 85 } 86 87 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 88 { 89 int nregs; 90 91 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; 92 if (reg < nregs) { 93 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf); 94 return 8; 95 } 96 if (arm_feature(env, ARM_FEATURE_NEON)) { 97 nregs += 16; 98 if (reg < nregs) { 99 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 100 q[0] = ldq_le_p(buf); 101 q[1] = ldq_le_p(buf + 8); 102 return 16; 103 } 104 } 105 switch (reg - nregs) { 106 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; 107 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4; 108 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; 109 } 110 return 0; 111 } 112 113 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) 114 { 115 switch (reg) { 116 case 0 ... 31: 117 /* 128 bit FP register */ 118 { 119 uint64_t *q = aa64_vfp_qreg(env, reg); 120 stq_le_p(buf, q[0]); 121 stq_le_p(buf + 8, q[1]); 122 return 16; 123 } 124 case 32: 125 /* FPSR */ 126 stl_p(buf, vfp_get_fpsr(env)); 127 return 4; 128 case 33: 129 /* FPCR */ 130 stl_p(buf, vfp_get_fpcr(env)); 131 return 4; 132 default: 133 return 0; 134 } 135 } 136 137 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 138 { 139 switch (reg) { 140 case 0 ... 31: 141 /* 128 bit FP register */ 142 { 143 uint64_t *q = aa64_vfp_qreg(env, reg); 144 q[0] = ldq_le_p(buf); 145 q[1] = ldq_le_p(buf + 8); 146 return 16; 147 } 148 case 32: 149 /* FPSR */ 150 vfp_set_fpsr(env, ldl_p(buf)); 151 return 4; 152 case 33: 153 /* FPCR */ 154 vfp_set_fpcr(env, ldl_p(buf)); 155 return 4; 156 default: 157 return 0; 158 } 159 } 160 161 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) 162 { 163 assert(ri->fieldoffset); 164 if (cpreg_field_is_64bit(ri)) { 165 return CPREG_FIELD64(env, ri); 166 } else { 167 return CPREG_FIELD32(env, ri); 168 } 169 } 170 171 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 172 uint64_t value) 173 { 174 assert(ri->fieldoffset); 175 if (cpreg_field_is_64bit(ri)) { 176 CPREG_FIELD64(env, ri) = value; 177 } else { 178 CPREG_FIELD32(env, ri) = value; 179 } 180 } 181 182 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) 183 { 184 return (char *)env + ri->fieldoffset; 185 } 186 187 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) 188 { 189 /* Raw read of a coprocessor register (as needed for migration, etc). */ 190 if (ri->type & ARM_CP_CONST) { 191 return ri->resetvalue; 192 } else if (ri->raw_readfn) { 193 return ri->raw_readfn(env, ri); 194 } else if (ri->readfn) { 195 return ri->readfn(env, ri); 196 } else { 197 return raw_read(env, ri); 198 } 199 } 200 201 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, 202 uint64_t v) 203 { 204 /* Raw write of a coprocessor register (as needed for migration, etc). 205 * Note that constant registers are treated as write-ignored; the 206 * caller should check for success by whether a readback gives the 207 * value written. 208 */ 209 if (ri->type & ARM_CP_CONST) { 210 return; 211 } else if (ri->raw_writefn) { 212 ri->raw_writefn(env, ri, v); 213 } else if (ri->writefn) { 214 ri->writefn(env, ri, v); 215 } else { 216 raw_write(env, ri, v); 217 } 218 } 219 220 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg) 221 { 222 ARMCPU *cpu = arm_env_get_cpu(env); 223 const ARMCPRegInfo *ri; 224 uint32_t key; 225 226 key = cpu->dyn_xml.cpregs_keys[reg]; 227 ri = get_arm_cp_reginfo(cpu->cp_regs, key); 228 if (ri) { 229 if (cpreg_field_is_64bit(ri)) { 230 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri)); 231 } else { 232 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri)); 233 } 234 } 235 return 0; 236 } 237 238 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg) 239 { 240 return 0; 241 } 242 243 static bool raw_accessors_invalid(const ARMCPRegInfo *ri) 244 { 245 /* Return true if the regdef would cause an assertion if you called 246 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a 247 * program bug for it not to have the NO_RAW flag). 248 * NB that returning false here doesn't necessarily mean that calling 249 * read/write_raw_cp_reg() is safe, because we can't distinguish "has 250 * read/write access functions which are safe for raw use" from "has 251 * read/write access functions which have side effects but has forgotten 252 * to provide raw access functions". 253 * The tests here line up with the conditions in read/write_raw_cp_reg() 254 * and assertions in raw_read()/raw_write(). 255 */ 256 if ((ri->type & ARM_CP_CONST) || 257 ri->fieldoffset || 258 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { 259 return false; 260 } 261 return true; 262 } 263 264 bool write_cpustate_to_list(ARMCPU *cpu) 265 { 266 /* Write the coprocessor state from cpu->env to the (index,value) list. */ 267 int i; 268 bool ok = true; 269 270 for (i = 0; i < cpu->cpreg_array_len; i++) { 271 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 272 const ARMCPRegInfo *ri; 273 274 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 275 if (!ri) { 276 ok = false; 277 continue; 278 } 279 if (ri->type & ARM_CP_NO_RAW) { 280 continue; 281 } 282 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri); 283 } 284 return ok; 285 } 286 287 bool write_list_to_cpustate(ARMCPU *cpu) 288 { 289 int i; 290 bool ok = true; 291 292 for (i = 0; i < cpu->cpreg_array_len; i++) { 293 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 294 uint64_t v = cpu->cpreg_values[i]; 295 const ARMCPRegInfo *ri; 296 297 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 298 if (!ri) { 299 ok = false; 300 continue; 301 } 302 if (ri->type & ARM_CP_NO_RAW) { 303 continue; 304 } 305 /* Write value and confirm it reads back as written 306 * (to catch read-only registers and partially read-only 307 * registers where the incoming migration value doesn't match) 308 */ 309 write_raw_cp_reg(&cpu->env, ri, v); 310 if (read_raw_cp_reg(&cpu->env, ri) != v) { 311 ok = false; 312 } 313 } 314 return ok; 315 } 316 317 static void add_cpreg_to_list(gpointer key, gpointer opaque) 318 { 319 ARMCPU *cpu = opaque; 320 uint64_t regidx; 321 const ARMCPRegInfo *ri; 322 323 regidx = *(uint32_t *)key; 324 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 325 326 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 327 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); 328 /* The value array need not be initialized at this point */ 329 cpu->cpreg_array_len++; 330 } 331 } 332 333 static void count_cpreg(gpointer key, gpointer opaque) 334 { 335 ARMCPU *cpu = opaque; 336 uint64_t regidx; 337 const ARMCPRegInfo *ri; 338 339 regidx = *(uint32_t *)key; 340 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 341 342 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 343 cpu->cpreg_array_len++; 344 } 345 } 346 347 static gint cpreg_key_compare(gconstpointer a, gconstpointer b) 348 { 349 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a); 350 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b); 351 352 if (aidx > bidx) { 353 return 1; 354 } 355 if (aidx < bidx) { 356 return -1; 357 } 358 return 0; 359 } 360 361 void init_cpreg_list(ARMCPU *cpu) 362 { 363 /* Initialise the cpreg_tuples[] array based on the cp_regs hash. 364 * Note that we require cpreg_tuples[] to be sorted by key ID. 365 */ 366 GList *keys; 367 int arraylen; 368 369 keys = g_hash_table_get_keys(cpu->cp_regs); 370 keys = g_list_sort(keys, cpreg_key_compare); 371 372 cpu->cpreg_array_len = 0; 373 374 g_list_foreach(keys, count_cpreg, cpu); 375 376 arraylen = cpu->cpreg_array_len; 377 cpu->cpreg_indexes = g_new(uint64_t, arraylen); 378 cpu->cpreg_values = g_new(uint64_t, arraylen); 379 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); 380 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); 381 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; 382 cpu->cpreg_array_len = 0; 383 384 g_list_foreach(keys, add_cpreg_to_list, cpu); 385 386 assert(cpu->cpreg_array_len == arraylen); 387 388 g_list_free(keys); 389 } 390 391 /* 392 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but 393 * they are accessible when EL3 is using AArch64 regardless of EL3.NS. 394 * 395 * access_el3_aa32ns: Used to check AArch32 register views. 396 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views. 397 */ 398 static CPAccessResult access_el3_aa32ns(CPUARMState *env, 399 const ARMCPRegInfo *ri, 400 bool isread) 401 { 402 bool secure = arm_is_secure_below_el3(env); 403 404 assert(!arm_el_is_aa64(env, 3)); 405 if (secure) { 406 return CP_ACCESS_TRAP_UNCATEGORIZED; 407 } 408 return CP_ACCESS_OK; 409 } 410 411 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env, 412 const ARMCPRegInfo *ri, 413 bool isread) 414 { 415 if (!arm_el_is_aa64(env, 3)) { 416 return access_el3_aa32ns(env, ri, isread); 417 } 418 return CP_ACCESS_OK; 419 } 420 421 /* Some secure-only AArch32 registers trap to EL3 if used from 422 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts). 423 * Note that an access from Secure EL1 can only happen if EL3 is AArch64. 424 * We assume that the .access field is set to PL1_RW. 425 */ 426 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env, 427 const ARMCPRegInfo *ri, 428 bool isread) 429 { 430 if (arm_current_el(env) == 3) { 431 return CP_ACCESS_OK; 432 } 433 if (arm_is_secure_below_el3(env)) { 434 return CP_ACCESS_TRAP_EL3; 435 } 436 /* This will be EL1 NS and EL2 NS, which just UNDEF */ 437 return CP_ACCESS_TRAP_UNCATEGORIZED; 438 } 439 440 /* Check for traps to "powerdown debug" registers, which are controlled 441 * by MDCR.TDOSA 442 */ 443 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri, 444 bool isread) 445 { 446 int el = arm_current_el(env); 447 448 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDOSA) 449 && !arm_is_secure_below_el3(env)) { 450 return CP_ACCESS_TRAP_EL2; 451 } 452 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) { 453 return CP_ACCESS_TRAP_EL3; 454 } 455 return CP_ACCESS_OK; 456 } 457 458 /* Check for traps to "debug ROM" registers, which are controlled 459 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3. 460 */ 461 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri, 462 bool isread) 463 { 464 int el = arm_current_el(env); 465 466 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDRA) 467 && !arm_is_secure_below_el3(env)) { 468 return CP_ACCESS_TRAP_EL2; 469 } 470 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 471 return CP_ACCESS_TRAP_EL3; 472 } 473 return CP_ACCESS_OK; 474 } 475 476 /* Check for traps to general debug registers, which are controlled 477 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3. 478 */ 479 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri, 480 bool isread) 481 { 482 int el = arm_current_el(env); 483 484 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TDA) 485 && !arm_is_secure_below_el3(env)) { 486 return CP_ACCESS_TRAP_EL2; 487 } 488 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 489 return CP_ACCESS_TRAP_EL3; 490 } 491 return CP_ACCESS_OK; 492 } 493 494 /* Check for traps to performance monitor registers, which are controlled 495 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3. 496 */ 497 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri, 498 bool isread) 499 { 500 int el = arm_current_el(env); 501 502 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 503 && !arm_is_secure_below_el3(env)) { 504 return CP_ACCESS_TRAP_EL2; 505 } 506 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 507 return CP_ACCESS_TRAP_EL3; 508 } 509 return CP_ACCESS_OK; 510 } 511 512 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 513 { 514 ARMCPU *cpu = arm_env_get_cpu(env); 515 516 raw_write(env, ri, value); 517 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */ 518 } 519 520 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 521 { 522 ARMCPU *cpu = arm_env_get_cpu(env); 523 524 if (raw_read(env, ri) != value) { 525 /* Unlike real hardware the qemu TLB uses virtual addresses, 526 * not modified virtual addresses, so this causes a TLB flush. 527 */ 528 tlb_flush(CPU(cpu)); 529 raw_write(env, ri, value); 530 } 531 } 532 533 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, 534 uint64_t value) 535 { 536 ARMCPU *cpu = arm_env_get_cpu(env); 537 538 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA) 539 && !extended_addresses_enabled(env)) { 540 /* For VMSA (when not using the LPAE long descriptor page table 541 * format) this register includes the ASID, so do a TLB flush. 542 * For PMSA it is purely a process ID and no action is needed. 543 */ 544 tlb_flush(CPU(cpu)); 545 } 546 raw_write(env, ri, value); 547 } 548 549 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, 550 uint64_t value) 551 { 552 /* Invalidate all (TLBIALL) */ 553 ARMCPU *cpu = arm_env_get_cpu(env); 554 555 tlb_flush(CPU(cpu)); 556 } 557 558 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, 559 uint64_t value) 560 { 561 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ 562 ARMCPU *cpu = arm_env_get_cpu(env); 563 564 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); 565 } 566 567 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, 568 uint64_t value) 569 { 570 /* Invalidate by ASID (TLBIASID) */ 571 ARMCPU *cpu = arm_env_get_cpu(env); 572 573 tlb_flush(CPU(cpu)); 574 } 575 576 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, 577 uint64_t value) 578 { 579 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ 580 ARMCPU *cpu = arm_env_get_cpu(env); 581 582 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); 583 } 584 585 /* IS variants of TLB operations must affect all cores */ 586 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 587 uint64_t value) 588 { 589 CPUState *cs = ENV_GET_CPU(env); 590 591 tlb_flush_all_cpus_synced(cs); 592 } 593 594 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 595 uint64_t value) 596 { 597 CPUState *cs = ENV_GET_CPU(env); 598 599 tlb_flush_all_cpus_synced(cs); 600 } 601 602 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 603 uint64_t value) 604 { 605 CPUState *cs = ENV_GET_CPU(env); 606 607 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 608 } 609 610 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 611 uint64_t value) 612 { 613 CPUState *cs = ENV_GET_CPU(env); 614 615 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 616 } 617 618 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri, 619 uint64_t value) 620 { 621 CPUState *cs = ENV_GET_CPU(env); 622 623 tlb_flush_by_mmuidx(cs, 624 ARMMMUIdxBit_S12NSE1 | 625 ARMMMUIdxBit_S12NSE0 | 626 ARMMMUIdxBit_S2NS); 627 } 628 629 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 630 uint64_t value) 631 { 632 CPUState *cs = ENV_GET_CPU(env); 633 634 tlb_flush_by_mmuidx_all_cpus_synced(cs, 635 ARMMMUIdxBit_S12NSE1 | 636 ARMMMUIdxBit_S12NSE0 | 637 ARMMMUIdxBit_S2NS); 638 } 639 640 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri, 641 uint64_t value) 642 { 643 /* Invalidate by IPA. This has to invalidate any structures that 644 * contain only stage 2 translation information, but does not need 645 * to apply to structures that contain combined stage 1 and stage 2 646 * translation information. 647 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. 648 */ 649 CPUState *cs = ENV_GET_CPU(env); 650 uint64_t pageaddr; 651 652 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 653 return; 654 } 655 656 pageaddr = sextract64(value << 12, 0, 40); 657 658 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS); 659 } 660 661 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 662 uint64_t value) 663 { 664 CPUState *cs = ENV_GET_CPU(env); 665 uint64_t pageaddr; 666 667 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 668 return; 669 } 670 671 pageaddr = sextract64(value << 12, 0, 40); 672 673 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 674 ARMMMUIdxBit_S2NS); 675 } 676 677 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 678 uint64_t value) 679 { 680 CPUState *cs = ENV_GET_CPU(env); 681 682 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2); 683 } 684 685 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 686 uint64_t value) 687 { 688 CPUState *cs = ENV_GET_CPU(env); 689 690 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2); 691 } 692 693 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 694 uint64_t value) 695 { 696 CPUState *cs = ENV_GET_CPU(env); 697 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 698 699 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2); 700 } 701 702 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 703 uint64_t value) 704 { 705 CPUState *cs = ENV_GET_CPU(env); 706 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 707 708 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 709 ARMMMUIdxBit_S1E2); 710 } 711 712 static const ARMCPRegInfo cp_reginfo[] = { 713 /* Define the secure and non-secure FCSE identifier CP registers 714 * separately because there is no secure bank in V8 (no _EL3). This allows 715 * the secure register to be properly reset and migrated. There is also no 716 * v8 EL1 version of the register so the non-secure instance stands alone. 717 */ 718 { .name = "FCSEIDR", 719 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 720 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 721 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns), 722 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 723 { .name = "FCSEIDR_S", 724 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 725 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 726 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s), 727 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 728 /* Define the secure and non-secure context identifier CP registers 729 * separately because there is no secure bank in V8 (no _EL3). This allows 730 * the secure register to be properly reset and migrated. In the 731 * non-secure case, the 32-bit register will have reset and migration 732 * disabled during registration as it is handled by the 64-bit instance. 733 */ 734 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH, 735 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 736 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 737 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]), 738 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 739 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32, 740 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 741 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 742 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s), 743 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 744 REGINFO_SENTINEL 745 }; 746 747 static const ARMCPRegInfo not_v8_cp_reginfo[] = { 748 /* NB: Some of these registers exist in v8 but with more precise 749 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). 750 */ 751 /* MMU Domain access control / MPU write buffer control */ 752 { .name = "DACR", 753 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, 754 .access = PL1_RW, .resetvalue = 0, 755 .writefn = dacr_write, .raw_writefn = raw_write, 756 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 757 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 758 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs. 759 * For v6 and v5, these mappings are overly broad. 760 */ 761 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0, 762 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 763 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1, 764 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 765 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4, 766 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 767 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8, 768 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 769 /* Cache maintenance ops; some of this space may be overridden later. */ 770 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 771 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 772 .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, 773 REGINFO_SENTINEL 774 }; 775 776 static const ARMCPRegInfo not_v6_cp_reginfo[] = { 777 /* Not all pre-v6 cores implemented this WFI, so this is slightly 778 * over-broad. 779 */ 780 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, 781 .access = PL1_W, .type = ARM_CP_WFI }, 782 REGINFO_SENTINEL 783 }; 784 785 static const ARMCPRegInfo not_v7_cp_reginfo[] = { 786 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which 787 * is UNPREDICTABLE; we choose to NOP as most implementations do). 788 */ 789 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 790 .access = PL1_W, .type = ARM_CP_WFI }, 791 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice 792 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and 793 * OMAPCP will override this space. 794 */ 795 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, 796 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), 797 .resetvalue = 0 }, 798 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, 799 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), 800 .resetvalue = 0 }, 801 /* v6 doesn't have the cache ID registers but Linux reads them anyway */ 802 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, 803 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 804 .resetvalue = 0 }, 805 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; 806 * implementing it as RAZ means the "debug architecture version" bits 807 * will read as a reserved value, which should cause Linux to not try 808 * to use the debug hardware. 809 */ 810 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 811 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 812 /* MMU TLB control. Note that the wildcarding means we cover not just 813 * the unified TLB ops but also the dside/iside/inner-shareable variants. 814 */ 815 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, 816 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, 817 .type = ARM_CP_NO_RAW }, 818 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, 819 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, 820 .type = ARM_CP_NO_RAW }, 821 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, 822 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, 823 .type = ARM_CP_NO_RAW }, 824 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, 825 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, 826 .type = ARM_CP_NO_RAW }, 827 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2, 828 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP }, 829 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2, 830 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP }, 831 REGINFO_SENTINEL 832 }; 833 834 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, 835 uint64_t value) 836 { 837 uint32_t mask = 0; 838 839 /* In ARMv8 most bits of CPACR_EL1 are RES0. */ 840 if (!arm_feature(env, ARM_FEATURE_V8)) { 841 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. 842 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. 843 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. 844 */ 845 if (arm_feature(env, ARM_FEATURE_VFP)) { 846 /* VFP coprocessor: cp10 & cp11 [23:20] */ 847 mask |= (1 << 31) | (1 << 30) | (0xf << 20); 848 849 if (!arm_feature(env, ARM_FEATURE_NEON)) { 850 /* ASEDIS [31] bit is RAO/WI */ 851 value |= (1 << 31); 852 } 853 854 /* VFPv3 and upwards with NEON implement 32 double precision 855 * registers (D0-D31). 856 */ 857 if (!arm_feature(env, ARM_FEATURE_NEON) || 858 !arm_feature(env, ARM_FEATURE_VFP3)) { 859 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ 860 value |= (1 << 30); 861 } 862 } 863 value &= mask; 864 } 865 env->cp15.cpacr_el1 = value; 866 } 867 868 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 869 { 870 /* Call cpacr_write() so that we reset with the correct RAO bits set 871 * for our CPU features. 872 */ 873 cpacr_write(env, ri, 0); 874 } 875 876 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 877 bool isread) 878 { 879 if (arm_feature(env, ARM_FEATURE_V8)) { 880 /* Check if CPACR accesses are to be trapped to EL2 */ 881 if (arm_current_el(env) == 1 && 882 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) { 883 return CP_ACCESS_TRAP_EL2; 884 /* Check if CPACR accesses are to be trapped to EL3 */ 885 } else if (arm_current_el(env) < 3 && 886 (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 887 return CP_ACCESS_TRAP_EL3; 888 } 889 } 890 891 return CP_ACCESS_OK; 892 } 893 894 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri, 895 bool isread) 896 { 897 /* Check if CPTR accesses are set to trap to EL3 */ 898 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 899 return CP_ACCESS_TRAP_EL3; 900 } 901 902 return CP_ACCESS_OK; 903 } 904 905 static const ARMCPRegInfo v6_cp_reginfo[] = { 906 /* prefetch by MVA in v6, NOP in v7 */ 907 { .name = "MVA_prefetch", 908 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, 909 .access = PL1_W, .type = ARM_CP_NOP }, 910 /* We need to break the TB after ISB to execute self-modifying code 911 * correctly and also to take any pending interrupts immediately. 912 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag. 913 */ 914 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, 915 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore }, 916 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, 917 .access = PL0_W, .type = ARM_CP_NOP }, 918 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, 919 .access = PL0_W, .type = ARM_CP_NOP }, 920 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, 921 .access = PL1_RW, 922 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), 923 offsetof(CPUARMState, cp15.ifar_ns) }, 924 .resetvalue = 0, }, 925 /* Watchpoint Fault Address Register : should actually only be present 926 * for 1136, 1176, 11MPCore. 927 */ 928 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, 929 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, 930 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, 931 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access, 932 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1), 933 .resetfn = cpacr_reset, .writefn = cpacr_write }, 934 REGINFO_SENTINEL 935 }; 936 937 /* Definitions for the PMU registers */ 938 #define PMCRN_MASK 0xf800 939 #define PMCRN_SHIFT 11 940 #define PMCRD 0x8 941 #define PMCRC 0x4 942 #define PMCRE 0x1 943 944 static inline uint32_t pmu_num_counters(CPUARMState *env) 945 { 946 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT; 947 } 948 949 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */ 950 static inline uint64_t pmu_counter_mask(CPUARMState *env) 951 { 952 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1); 953 } 954 955 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri, 956 bool isread) 957 { 958 /* Performance monitor registers user accessibility is controlled 959 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable 960 * trapping to EL2 or EL3 for other accesses. 961 */ 962 int el = arm_current_el(env); 963 964 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) { 965 return CP_ACCESS_TRAP; 966 } 967 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 968 && !arm_is_secure_below_el3(env)) { 969 return CP_ACCESS_TRAP_EL2; 970 } 971 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 972 return CP_ACCESS_TRAP_EL3; 973 } 974 975 return CP_ACCESS_OK; 976 } 977 978 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env, 979 const ARMCPRegInfo *ri, 980 bool isread) 981 { 982 /* ER: event counter read trap control */ 983 if (arm_feature(env, ARM_FEATURE_V8) 984 && arm_current_el(env) == 0 985 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0 986 && isread) { 987 return CP_ACCESS_OK; 988 } 989 990 return pmreg_access(env, ri, isread); 991 } 992 993 static CPAccessResult pmreg_access_swinc(CPUARMState *env, 994 const ARMCPRegInfo *ri, 995 bool isread) 996 { 997 /* SW: software increment write trap control */ 998 if (arm_feature(env, ARM_FEATURE_V8) 999 && arm_current_el(env) == 0 1000 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0 1001 && !isread) { 1002 return CP_ACCESS_OK; 1003 } 1004 1005 return pmreg_access(env, ri, isread); 1006 } 1007 1008 #ifndef CONFIG_USER_ONLY 1009 1010 static CPAccessResult pmreg_access_selr(CPUARMState *env, 1011 const ARMCPRegInfo *ri, 1012 bool isread) 1013 { 1014 /* ER: event counter read trap control */ 1015 if (arm_feature(env, ARM_FEATURE_V8) 1016 && arm_current_el(env) == 0 1017 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) { 1018 return CP_ACCESS_OK; 1019 } 1020 1021 return pmreg_access(env, ri, isread); 1022 } 1023 1024 static CPAccessResult pmreg_access_ccntr(CPUARMState *env, 1025 const ARMCPRegInfo *ri, 1026 bool isread) 1027 { 1028 /* CR: cycle counter read trap control */ 1029 if (arm_feature(env, ARM_FEATURE_V8) 1030 && arm_current_el(env) == 0 1031 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0 1032 && isread) { 1033 return CP_ACCESS_OK; 1034 } 1035 1036 return pmreg_access(env, ri, isread); 1037 } 1038 1039 static inline bool arm_ccnt_enabled(CPUARMState *env) 1040 { 1041 /* This does not support checking PMCCFILTR_EL0 register */ 1042 1043 if (!(env->cp15.c9_pmcr & PMCRE) || !(env->cp15.c9_pmcnten & (1 << 31))) { 1044 return false; 1045 } 1046 1047 return true; 1048 } 1049 1050 void pmccntr_sync(CPUARMState *env) 1051 { 1052 uint64_t temp_ticks; 1053 1054 temp_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1055 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 1056 1057 if (env->cp15.c9_pmcr & PMCRD) { 1058 /* Increment once every 64 processor clock cycles */ 1059 temp_ticks /= 64; 1060 } 1061 1062 if (arm_ccnt_enabled(env)) { 1063 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt; 1064 } 1065 } 1066 1067 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1068 uint64_t value) 1069 { 1070 pmccntr_sync(env); 1071 1072 if (value & PMCRC) { 1073 /* The counter has been reset */ 1074 env->cp15.c15_ccnt = 0; 1075 } 1076 1077 /* only the DP, X, D and E bits are writable */ 1078 env->cp15.c9_pmcr &= ~0x39; 1079 env->cp15.c9_pmcr |= (value & 0x39); 1080 1081 pmccntr_sync(env); 1082 } 1083 1084 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1085 { 1086 uint64_t total_ticks; 1087 1088 if (!arm_ccnt_enabled(env)) { 1089 /* Counter is disabled, do not change value */ 1090 return env->cp15.c15_ccnt; 1091 } 1092 1093 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1094 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 1095 1096 if (env->cp15.c9_pmcr & PMCRD) { 1097 /* Increment once every 64 processor clock cycles */ 1098 total_ticks /= 64; 1099 } 1100 return total_ticks - env->cp15.c15_ccnt; 1101 } 1102 1103 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1104 uint64_t value) 1105 { 1106 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and 1107 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the 1108 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are 1109 * accessed. 1110 */ 1111 env->cp15.c9_pmselr = value & 0x1f; 1112 } 1113 1114 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1115 uint64_t value) 1116 { 1117 uint64_t total_ticks; 1118 1119 if (!arm_ccnt_enabled(env)) { 1120 /* Counter is disabled, set the absolute value */ 1121 env->cp15.c15_ccnt = value; 1122 return; 1123 } 1124 1125 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1126 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 1127 1128 if (env->cp15.c9_pmcr & PMCRD) { 1129 /* Increment once every 64 processor clock cycles */ 1130 total_ticks /= 64; 1131 } 1132 env->cp15.c15_ccnt = total_ticks - value; 1133 } 1134 1135 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, 1136 uint64_t value) 1137 { 1138 uint64_t cur_val = pmccntr_read(env, NULL); 1139 1140 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); 1141 } 1142 1143 #else /* CONFIG_USER_ONLY */ 1144 1145 void pmccntr_sync(CPUARMState *env) 1146 { 1147 } 1148 1149 #endif 1150 1151 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1152 uint64_t value) 1153 { 1154 pmccntr_sync(env); 1155 env->cp15.pmccfiltr_el0 = value & 0xfc000000; 1156 pmccntr_sync(env); 1157 } 1158 1159 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1160 uint64_t value) 1161 { 1162 value &= pmu_counter_mask(env); 1163 env->cp15.c9_pmcnten |= value; 1164 } 1165 1166 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1167 uint64_t value) 1168 { 1169 value &= pmu_counter_mask(env); 1170 env->cp15.c9_pmcnten &= ~value; 1171 } 1172 1173 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1174 uint64_t value) 1175 { 1176 env->cp15.c9_pmovsr &= ~value; 1177 } 1178 1179 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1180 uint64_t value) 1181 { 1182 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when 1183 * PMSELR value is equal to or greater than the number of implemented 1184 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI. 1185 */ 1186 if (env->cp15.c9_pmselr == 0x1f) { 1187 pmccfiltr_write(env, ri, value); 1188 } 1189 } 1190 1191 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri) 1192 { 1193 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER 1194 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write(). 1195 */ 1196 if (env->cp15.c9_pmselr == 0x1f) { 1197 return env->cp15.pmccfiltr_el0; 1198 } else { 1199 return 0; 1200 } 1201 } 1202 1203 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1204 uint64_t value) 1205 { 1206 if (arm_feature(env, ARM_FEATURE_V8)) { 1207 env->cp15.c9_pmuserenr = value & 0xf; 1208 } else { 1209 env->cp15.c9_pmuserenr = value & 1; 1210 } 1211 } 1212 1213 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1214 uint64_t value) 1215 { 1216 /* We have no event counters so only the C bit can be changed */ 1217 value &= pmu_counter_mask(env); 1218 env->cp15.c9_pminten |= value; 1219 } 1220 1221 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1222 uint64_t value) 1223 { 1224 value &= pmu_counter_mask(env); 1225 env->cp15.c9_pminten &= ~value; 1226 } 1227 1228 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 1229 uint64_t value) 1230 { 1231 /* Note that even though the AArch64 view of this register has bits 1232 * [10:0] all RES0 we can only mask the bottom 5, to comply with the 1233 * architectural requirements for bits which are RES0 only in some 1234 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 1235 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) 1236 */ 1237 raw_write(env, ri, value & ~0x1FULL); 1238 } 1239 1240 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 1241 { 1242 /* We only mask off bits that are RES0 both for AArch64 and AArch32. 1243 * For bits that vary between AArch32/64, code needs to check the 1244 * current execution mode before directly using the feature bit. 1245 */ 1246 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK; 1247 1248 if (!arm_feature(env, ARM_FEATURE_EL2)) { 1249 valid_mask &= ~SCR_HCE; 1250 1251 /* On ARMv7, SMD (or SCD as it is called in v7) is only 1252 * supported if EL2 exists. The bit is UNK/SBZP when 1253 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero 1254 * when EL2 is unavailable. 1255 * On ARMv8, this bit is always available. 1256 */ 1257 if (arm_feature(env, ARM_FEATURE_V7) && 1258 !arm_feature(env, ARM_FEATURE_V8)) { 1259 valid_mask &= ~SCR_SMD; 1260 } 1261 } 1262 1263 /* Clear all-context RES0 bits. */ 1264 value &= valid_mask; 1265 raw_write(env, ri, value); 1266 } 1267 1268 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1269 { 1270 ARMCPU *cpu = arm_env_get_cpu(env); 1271 1272 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR 1273 * bank 1274 */ 1275 uint32_t index = A32_BANKED_REG_GET(env, csselr, 1276 ri->secure & ARM_CP_SECSTATE_S); 1277 1278 return cpu->ccsidr[index]; 1279 } 1280 1281 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1282 uint64_t value) 1283 { 1284 raw_write(env, ri, value & 0xf); 1285 } 1286 1287 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1288 { 1289 CPUState *cs = ENV_GET_CPU(env); 1290 uint64_t ret = 0; 1291 1292 if (cs->interrupt_request & CPU_INTERRUPT_HARD) { 1293 ret |= CPSR_I; 1294 } 1295 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { 1296 ret |= CPSR_F; 1297 } 1298 /* External aborts are not possible in QEMU so A bit is always clear */ 1299 return ret; 1300 } 1301 1302 static const ARMCPRegInfo v7_cp_reginfo[] = { 1303 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ 1304 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 1305 .access = PL1_W, .type = ARM_CP_NOP }, 1306 /* Performance monitors are implementation defined in v7, 1307 * but with an ARM recommended set of registers, which we 1308 * follow (although we don't actually implement any counters) 1309 * 1310 * Performance registers fall into three categories: 1311 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) 1312 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) 1313 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) 1314 * For the cases controlled by PMUSERENR we must set .access to PL0_RW 1315 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. 1316 */ 1317 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, 1318 .access = PL0_RW, .type = ARM_CP_ALIAS, 1319 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1320 .writefn = pmcntenset_write, 1321 .accessfn = pmreg_access, 1322 .raw_writefn = raw_write }, 1323 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, 1324 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, 1325 .access = PL0_RW, .accessfn = pmreg_access, 1326 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, 1327 .writefn = pmcntenset_write, .raw_writefn = raw_write }, 1328 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, 1329 .access = PL0_RW, 1330 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1331 .accessfn = pmreg_access, 1332 .writefn = pmcntenclr_write, 1333 .type = ARM_CP_ALIAS }, 1334 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, 1335 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, 1336 .access = PL0_RW, .accessfn = pmreg_access, 1337 .type = ARM_CP_ALIAS, 1338 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), 1339 .writefn = pmcntenclr_write }, 1340 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, 1341 .access = PL0_RW, 1342 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 1343 .accessfn = pmreg_access, 1344 .writefn = pmovsr_write, 1345 .raw_writefn = raw_write }, 1346 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64, 1347 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3, 1348 .access = PL0_RW, .accessfn = pmreg_access, 1349 .type = ARM_CP_ALIAS, 1350 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 1351 .writefn = pmovsr_write, 1352 .raw_writefn = raw_write }, 1353 /* Unimplemented so WI. */ 1354 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, 1355 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP }, 1356 #ifndef CONFIG_USER_ONLY 1357 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, 1358 .access = PL0_RW, .type = ARM_CP_ALIAS, 1359 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr), 1360 .accessfn = pmreg_access_selr, .writefn = pmselr_write, 1361 .raw_writefn = raw_write}, 1362 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64, 1363 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5, 1364 .access = PL0_RW, .accessfn = pmreg_access_selr, 1365 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr), 1366 .writefn = pmselr_write, .raw_writefn = raw_write, }, 1367 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, 1368 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO, 1369 .readfn = pmccntr_read, .writefn = pmccntr_write32, 1370 .accessfn = pmreg_access_ccntr }, 1371 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, 1372 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, 1373 .access = PL0_RW, .accessfn = pmreg_access_ccntr, 1374 .type = ARM_CP_IO, 1375 .readfn = pmccntr_read, .writefn = pmccntr_write, }, 1376 #endif 1377 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, 1378 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, 1379 .writefn = pmccfiltr_write, 1380 .access = PL0_RW, .accessfn = pmreg_access, 1381 .type = ARM_CP_IO, 1382 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), 1383 .resetvalue = 0, }, 1384 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, 1385 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access, 1386 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 1387 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64, 1388 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1, 1389 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access, 1390 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 1391 /* Unimplemented, RAZ/WI. */ 1392 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, 1393 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0, 1394 .accessfn = pmreg_access_xevcntr }, 1395 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, 1396 .access = PL0_R | PL1_RW, .accessfn = access_tpm, 1397 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr), 1398 .resetvalue = 0, 1399 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 1400 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64, 1401 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0, 1402 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 1403 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), 1404 .resetvalue = 0, 1405 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 1406 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, 1407 .access = PL1_RW, .accessfn = access_tpm, 1408 .type = ARM_CP_ALIAS | ARM_CP_IO, 1409 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten), 1410 .resetvalue = 0, 1411 .writefn = pmintenset_write, .raw_writefn = raw_write }, 1412 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64, 1413 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1, 1414 .access = PL1_RW, .accessfn = access_tpm, 1415 .type = ARM_CP_IO, 1416 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 1417 .writefn = pmintenset_write, .raw_writefn = raw_write, 1418 .resetvalue = 0x0 }, 1419 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, 1420 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 1421 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 1422 .writefn = pmintenclr_write, }, 1423 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64, 1424 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2, 1425 .access = PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 1426 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 1427 .writefn = pmintenclr_write }, 1428 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, 1429 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, 1430 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW }, 1431 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, 1432 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, 1433 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0, 1434 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), 1435 offsetof(CPUARMState, cp15.csselr_ns) } }, 1436 /* Auxiliary ID register: this actually has an IMPDEF value but for now 1437 * just RAZ for all cores: 1438 */ 1439 { .name = "AIDR", .state = ARM_CP_STATE_BOTH, 1440 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, 1441 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 1442 /* Auxiliary fault status registers: these also are IMPDEF, and we 1443 * choose to RAZ/WI for all cores. 1444 */ 1445 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, 1446 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, 1447 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 1448 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, 1449 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, 1450 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 1451 /* MAIR can just read-as-written because we don't implement caches 1452 * and so don't need to care about memory attributes. 1453 */ 1454 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, 1455 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 1456 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]), 1457 .resetvalue = 0 }, 1458 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64, 1459 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0, 1460 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]), 1461 .resetvalue = 0 }, 1462 /* For non-long-descriptor page tables these are PRRR and NMRR; 1463 * regardless they still act as reads-as-written for QEMU. 1464 */ 1465 /* MAIR0/1 are defined separately from their 64-bit counterpart which 1466 * allows them to assign the correct fieldoffset based on the endianness 1467 * handled in the field definitions. 1468 */ 1469 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, 1470 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW, 1471 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s), 1472 offsetof(CPUARMState, cp15.mair0_ns) }, 1473 .resetfn = arm_cp_reset_ignore }, 1474 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, 1475 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW, 1476 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s), 1477 offsetof(CPUARMState, cp15.mair1_ns) }, 1478 .resetfn = arm_cp_reset_ignore }, 1479 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, 1480 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, 1481 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read }, 1482 /* 32 bit ITLB invalidates */ 1483 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0, 1484 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 1485 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, 1486 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 1487 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2, 1488 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 1489 /* 32 bit DTLB invalidates */ 1490 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0, 1491 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 1492 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, 1493 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 1494 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2, 1495 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 1496 /* 32 bit TLB invalidates */ 1497 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 1498 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 1499 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 1500 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 1501 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 1502 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 1503 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 1504 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, 1505 REGINFO_SENTINEL 1506 }; 1507 1508 static const ARMCPRegInfo v7mp_cp_reginfo[] = { 1509 /* 32 bit TLB invalidates, Inner Shareable */ 1510 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 1511 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write }, 1512 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 1513 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, 1514 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 1515 .type = ARM_CP_NO_RAW, .access = PL1_W, 1516 .writefn = tlbiasid_is_write }, 1517 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 1518 .type = ARM_CP_NO_RAW, .access = PL1_W, 1519 .writefn = tlbimvaa_is_write }, 1520 REGINFO_SENTINEL 1521 }; 1522 1523 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1524 uint64_t value) 1525 { 1526 value &= 1; 1527 env->teecr = value; 1528 } 1529 1530 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri, 1531 bool isread) 1532 { 1533 if (arm_current_el(env) == 0 && (env->teecr & 1)) { 1534 return CP_ACCESS_TRAP; 1535 } 1536 return CP_ACCESS_OK; 1537 } 1538 1539 static const ARMCPRegInfo t2ee_cp_reginfo[] = { 1540 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, 1541 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), 1542 .resetvalue = 0, 1543 .writefn = teecr_write }, 1544 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, 1545 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), 1546 .accessfn = teehbr_access, .resetvalue = 0 }, 1547 REGINFO_SENTINEL 1548 }; 1549 1550 static const ARMCPRegInfo v6k_cp_reginfo[] = { 1551 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, 1552 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, 1553 .access = PL0_RW, 1554 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 }, 1555 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, 1556 .access = PL0_RW, 1557 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s), 1558 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, 1559 .resetfn = arm_cp_reset_ignore }, 1560 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, 1561 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, 1562 .access = PL0_R|PL1_W, 1563 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]), 1564 .resetvalue = 0}, 1565 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, 1566 .access = PL0_R|PL1_W, 1567 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s), 1568 offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, 1569 .resetfn = arm_cp_reset_ignore }, 1570 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64, 1571 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, 1572 .access = PL1_RW, 1573 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 }, 1574 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4, 1575 .access = PL1_RW, 1576 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s), 1577 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) }, 1578 .resetvalue = 0 }, 1579 REGINFO_SENTINEL 1580 }; 1581 1582 #ifndef CONFIG_USER_ONLY 1583 1584 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, 1585 bool isread) 1586 { 1587 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero. 1588 * Writable only at the highest implemented exception level. 1589 */ 1590 int el = arm_current_el(env); 1591 1592 switch (el) { 1593 case 0: 1594 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) { 1595 return CP_ACCESS_TRAP; 1596 } 1597 break; 1598 case 1: 1599 if (!isread && ri->state == ARM_CP_STATE_AA32 && 1600 arm_is_secure_below_el3(env)) { 1601 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */ 1602 return CP_ACCESS_TRAP_UNCATEGORIZED; 1603 } 1604 break; 1605 case 2: 1606 case 3: 1607 break; 1608 } 1609 1610 if (!isread && el < arm_highest_el(env)) { 1611 return CP_ACCESS_TRAP_UNCATEGORIZED; 1612 } 1613 1614 return CP_ACCESS_OK; 1615 } 1616 1617 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx, 1618 bool isread) 1619 { 1620 unsigned int cur_el = arm_current_el(env); 1621 bool secure = arm_is_secure(env); 1622 1623 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */ 1624 if (cur_el == 0 && 1625 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) { 1626 return CP_ACCESS_TRAP; 1627 } 1628 1629 if (arm_feature(env, ARM_FEATURE_EL2) && 1630 timeridx == GTIMER_PHYS && !secure && cur_el < 2 && 1631 !extract32(env->cp15.cnthctl_el2, 0, 1)) { 1632 return CP_ACCESS_TRAP_EL2; 1633 } 1634 return CP_ACCESS_OK; 1635 } 1636 1637 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx, 1638 bool isread) 1639 { 1640 unsigned int cur_el = arm_current_el(env); 1641 bool secure = arm_is_secure(env); 1642 1643 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if 1644 * EL0[PV]TEN is zero. 1645 */ 1646 if (cur_el == 0 && 1647 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { 1648 return CP_ACCESS_TRAP; 1649 } 1650 1651 if (arm_feature(env, ARM_FEATURE_EL2) && 1652 timeridx == GTIMER_PHYS && !secure && cur_el < 2 && 1653 !extract32(env->cp15.cnthctl_el2, 1, 1)) { 1654 return CP_ACCESS_TRAP_EL2; 1655 } 1656 return CP_ACCESS_OK; 1657 } 1658 1659 static CPAccessResult gt_pct_access(CPUARMState *env, 1660 const ARMCPRegInfo *ri, 1661 bool isread) 1662 { 1663 return gt_counter_access(env, GTIMER_PHYS, isread); 1664 } 1665 1666 static CPAccessResult gt_vct_access(CPUARMState *env, 1667 const ARMCPRegInfo *ri, 1668 bool isread) 1669 { 1670 return gt_counter_access(env, GTIMER_VIRT, isread); 1671 } 1672 1673 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 1674 bool isread) 1675 { 1676 return gt_timer_access(env, GTIMER_PHYS, isread); 1677 } 1678 1679 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 1680 bool isread) 1681 { 1682 return gt_timer_access(env, GTIMER_VIRT, isread); 1683 } 1684 1685 static CPAccessResult gt_stimer_access(CPUARMState *env, 1686 const ARMCPRegInfo *ri, 1687 bool isread) 1688 { 1689 /* The AArch64 register view of the secure physical timer is 1690 * always accessible from EL3, and configurably accessible from 1691 * Secure EL1. 1692 */ 1693 switch (arm_current_el(env)) { 1694 case 1: 1695 if (!arm_is_secure(env)) { 1696 return CP_ACCESS_TRAP; 1697 } 1698 if (!(env->cp15.scr_el3 & SCR_ST)) { 1699 return CP_ACCESS_TRAP_EL3; 1700 } 1701 return CP_ACCESS_OK; 1702 case 0: 1703 case 2: 1704 return CP_ACCESS_TRAP; 1705 case 3: 1706 return CP_ACCESS_OK; 1707 default: 1708 g_assert_not_reached(); 1709 } 1710 } 1711 1712 static uint64_t gt_get_countervalue(CPUARMState *env) 1713 { 1714 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE; 1715 } 1716 1717 static void gt_recalc_timer(ARMCPU *cpu, int timeridx) 1718 { 1719 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; 1720 1721 if (gt->ctl & 1) { 1722 /* Timer enabled: calculate and set current ISTATUS, irq, and 1723 * reset timer to when ISTATUS next has to change 1724 */ 1725 uint64_t offset = timeridx == GTIMER_VIRT ? 1726 cpu->env.cp15.cntvoff_el2 : 0; 1727 uint64_t count = gt_get_countervalue(&cpu->env); 1728 /* Note that this must be unsigned 64 bit arithmetic: */ 1729 int istatus = count - offset >= gt->cval; 1730 uint64_t nexttick; 1731 int irqstate; 1732 1733 gt->ctl = deposit32(gt->ctl, 2, 1, istatus); 1734 1735 irqstate = (istatus && !(gt->ctl & 2)); 1736 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 1737 1738 if (istatus) { 1739 /* Next transition is when count rolls back over to zero */ 1740 nexttick = UINT64_MAX; 1741 } else { 1742 /* Next transition is when we hit cval */ 1743 nexttick = gt->cval + offset; 1744 } 1745 /* Note that the desired next expiry time might be beyond the 1746 * signed-64-bit range of a QEMUTimer -- in this case we just 1747 * set the timer for as far in the future as possible. When the 1748 * timer expires we will reset the timer for any remaining period. 1749 */ 1750 if (nexttick > INT64_MAX / GTIMER_SCALE) { 1751 nexttick = INT64_MAX / GTIMER_SCALE; 1752 } 1753 timer_mod(cpu->gt_timer[timeridx], nexttick); 1754 trace_arm_gt_recalc(timeridx, irqstate, nexttick); 1755 } else { 1756 /* Timer disabled: ISTATUS and timer output always clear */ 1757 gt->ctl &= ~4; 1758 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0); 1759 timer_del(cpu->gt_timer[timeridx]); 1760 trace_arm_gt_recalc_disabled(timeridx); 1761 } 1762 } 1763 1764 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri, 1765 int timeridx) 1766 { 1767 ARMCPU *cpu = arm_env_get_cpu(env); 1768 1769 timer_del(cpu->gt_timer[timeridx]); 1770 } 1771 1772 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 1773 { 1774 return gt_get_countervalue(env); 1775 } 1776 1777 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 1778 { 1779 return gt_get_countervalue(env) - env->cp15.cntvoff_el2; 1780 } 1781 1782 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1783 int timeridx, 1784 uint64_t value) 1785 { 1786 trace_arm_gt_cval_write(timeridx, value); 1787 env->cp15.c14_timer[timeridx].cval = value; 1788 gt_recalc_timer(arm_env_get_cpu(env), timeridx); 1789 } 1790 1791 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, 1792 int timeridx) 1793 { 1794 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; 1795 1796 return (uint32_t)(env->cp15.c14_timer[timeridx].cval - 1797 (gt_get_countervalue(env) - offset)); 1798 } 1799 1800 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1801 int timeridx, 1802 uint64_t value) 1803 { 1804 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; 1805 1806 trace_arm_gt_tval_write(timeridx, value); 1807 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset + 1808 sextract64(value, 0, 32); 1809 gt_recalc_timer(arm_env_get_cpu(env), timeridx); 1810 } 1811 1812 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1813 int timeridx, 1814 uint64_t value) 1815 { 1816 ARMCPU *cpu = arm_env_get_cpu(env); 1817 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; 1818 1819 trace_arm_gt_ctl_write(timeridx, value); 1820 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); 1821 if ((oldval ^ value) & 1) { 1822 /* Enable toggled */ 1823 gt_recalc_timer(cpu, timeridx); 1824 } else if ((oldval ^ value) & 2) { 1825 /* IMASK toggled: don't need to recalculate, 1826 * just set the interrupt line based on ISTATUS 1827 */ 1828 int irqstate = (oldval & 4) && !(value & 2); 1829 1830 trace_arm_gt_imask_toggle(timeridx, irqstate); 1831 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 1832 } 1833 } 1834 1835 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1836 { 1837 gt_timer_reset(env, ri, GTIMER_PHYS); 1838 } 1839 1840 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1841 uint64_t value) 1842 { 1843 gt_cval_write(env, ri, GTIMER_PHYS, value); 1844 } 1845 1846 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 1847 { 1848 return gt_tval_read(env, ri, GTIMER_PHYS); 1849 } 1850 1851 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1852 uint64_t value) 1853 { 1854 gt_tval_write(env, ri, GTIMER_PHYS, value); 1855 } 1856 1857 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1858 uint64_t value) 1859 { 1860 gt_ctl_write(env, ri, GTIMER_PHYS, value); 1861 } 1862 1863 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1864 { 1865 gt_timer_reset(env, ri, GTIMER_VIRT); 1866 } 1867 1868 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1869 uint64_t value) 1870 { 1871 gt_cval_write(env, ri, GTIMER_VIRT, value); 1872 } 1873 1874 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 1875 { 1876 return gt_tval_read(env, ri, GTIMER_VIRT); 1877 } 1878 1879 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1880 uint64_t value) 1881 { 1882 gt_tval_write(env, ri, GTIMER_VIRT, value); 1883 } 1884 1885 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1886 uint64_t value) 1887 { 1888 gt_ctl_write(env, ri, GTIMER_VIRT, value); 1889 } 1890 1891 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri, 1892 uint64_t value) 1893 { 1894 ARMCPU *cpu = arm_env_get_cpu(env); 1895 1896 trace_arm_gt_cntvoff_write(value); 1897 raw_write(env, ri, value); 1898 gt_recalc_timer(cpu, GTIMER_VIRT); 1899 } 1900 1901 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1902 { 1903 gt_timer_reset(env, ri, GTIMER_HYP); 1904 } 1905 1906 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1907 uint64_t value) 1908 { 1909 gt_cval_write(env, ri, GTIMER_HYP, value); 1910 } 1911 1912 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 1913 { 1914 return gt_tval_read(env, ri, GTIMER_HYP); 1915 } 1916 1917 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1918 uint64_t value) 1919 { 1920 gt_tval_write(env, ri, GTIMER_HYP, value); 1921 } 1922 1923 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1924 uint64_t value) 1925 { 1926 gt_ctl_write(env, ri, GTIMER_HYP, value); 1927 } 1928 1929 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1930 { 1931 gt_timer_reset(env, ri, GTIMER_SEC); 1932 } 1933 1934 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1935 uint64_t value) 1936 { 1937 gt_cval_write(env, ri, GTIMER_SEC, value); 1938 } 1939 1940 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 1941 { 1942 return gt_tval_read(env, ri, GTIMER_SEC); 1943 } 1944 1945 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1946 uint64_t value) 1947 { 1948 gt_tval_write(env, ri, GTIMER_SEC, value); 1949 } 1950 1951 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1952 uint64_t value) 1953 { 1954 gt_ctl_write(env, ri, GTIMER_SEC, value); 1955 } 1956 1957 void arm_gt_ptimer_cb(void *opaque) 1958 { 1959 ARMCPU *cpu = opaque; 1960 1961 gt_recalc_timer(cpu, GTIMER_PHYS); 1962 } 1963 1964 void arm_gt_vtimer_cb(void *opaque) 1965 { 1966 ARMCPU *cpu = opaque; 1967 1968 gt_recalc_timer(cpu, GTIMER_VIRT); 1969 } 1970 1971 void arm_gt_htimer_cb(void *opaque) 1972 { 1973 ARMCPU *cpu = opaque; 1974 1975 gt_recalc_timer(cpu, GTIMER_HYP); 1976 } 1977 1978 void arm_gt_stimer_cb(void *opaque) 1979 { 1980 ARMCPU *cpu = opaque; 1981 1982 gt_recalc_timer(cpu, GTIMER_SEC); 1983 } 1984 1985 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 1986 /* Note that CNTFRQ is purely reads-as-written for the benefit 1987 * of software; writing it doesn't actually change the timer frequency. 1988 * Our reset value matches the fixed frequency we implement the timer at. 1989 */ 1990 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, 1991 .type = ARM_CP_ALIAS, 1992 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 1993 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), 1994 }, 1995 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 1996 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 1997 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 1998 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 1999 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE, 2000 }, 2001 /* overall control: mostly access permissions */ 2002 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, 2003 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, 2004 .access = PL1_RW, 2005 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), 2006 .resetvalue = 0, 2007 }, 2008 /* per-timer control */ 2009 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 2010 .secure = ARM_CP_SECSTATE_NS, 2011 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, 2012 .accessfn = gt_ptimer_access, 2013 .fieldoffset = offsetoflow32(CPUARMState, 2014 cp15.c14_timer[GTIMER_PHYS].ctl), 2015 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, 2016 }, 2017 { .name = "CNTP_CTL_S", 2018 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 2019 .secure = ARM_CP_SECSTATE_S, 2020 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, 2021 .accessfn = gt_ptimer_access, 2022 .fieldoffset = offsetoflow32(CPUARMState, 2023 cp15.c14_timer[GTIMER_SEC].ctl), 2024 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 2025 }, 2026 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, 2027 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, 2028 .type = ARM_CP_IO, .access = PL1_RW | PL0_R, 2029 .accessfn = gt_ptimer_access, 2030 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 2031 .resetvalue = 0, 2032 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, 2033 }, 2034 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, 2035 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, 2036 .accessfn = gt_vtimer_access, 2037 .fieldoffset = offsetoflow32(CPUARMState, 2038 cp15.c14_timer[GTIMER_VIRT].ctl), 2039 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, 2040 }, 2041 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, 2042 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, 2043 .type = ARM_CP_IO, .access = PL1_RW | PL0_R, 2044 .accessfn = gt_vtimer_access, 2045 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 2046 .resetvalue = 0, 2047 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, 2048 }, 2049 /* TimerValue views: a 32 bit downcounting view of the underlying state */ 2050 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 2051 .secure = ARM_CP_SECSTATE_NS, 2052 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2053 .accessfn = gt_ptimer_access, 2054 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, 2055 }, 2056 { .name = "CNTP_TVAL_S", 2057 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 2058 .secure = ARM_CP_SECSTATE_S, 2059 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2060 .accessfn = gt_ptimer_access, 2061 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write, 2062 }, 2063 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, 2064 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, 2065 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2066 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset, 2067 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, 2068 }, 2069 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, 2070 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2071 .accessfn = gt_vtimer_access, 2072 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, 2073 }, 2074 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, 2075 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, 2076 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2077 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset, 2078 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, 2079 }, 2080 /* The counter itself */ 2081 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, 2082 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 2083 .accessfn = gt_pct_access, 2084 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, 2085 }, 2086 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, 2087 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, 2088 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2089 .accessfn = gt_pct_access, .readfn = gt_cnt_read, 2090 }, 2091 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, 2092 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 2093 .accessfn = gt_vct_access, 2094 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, 2095 }, 2096 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 2097 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 2098 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2099 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, 2100 }, 2101 /* Comparison value, indicating when the timer goes off */ 2102 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, 2103 .secure = ARM_CP_SECSTATE_NS, 2104 .access = PL1_RW | PL0_R, 2105 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2106 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 2107 .accessfn = gt_ptimer_access, 2108 .writefn = gt_phys_cval_write, .raw_writefn = raw_write, 2109 }, 2110 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2, 2111 .secure = ARM_CP_SECSTATE_S, 2112 .access = PL1_RW | PL0_R, 2113 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2114 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 2115 .accessfn = gt_ptimer_access, 2116 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 2117 }, 2118 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, 2119 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, 2120 .access = PL1_RW | PL0_R, 2121 .type = ARM_CP_IO, 2122 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 2123 .resetvalue = 0, .accessfn = gt_ptimer_access, 2124 .writefn = gt_phys_cval_write, .raw_writefn = raw_write, 2125 }, 2126 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, 2127 .access = PL1_RW | PL0_R, 2128 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2129 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 2130 .accessfn = gt_vtimer_access, 2131 .writefn = gt_virt_cval_write, .raw_writefn = raw_write, 2132 }, 2133 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, 2134 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, 2135 .access = PL1_RW | PL0_R, 2136 .type = ARM_CP_IO, 2137 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 2138 .resetvalue = 0, .accessfn = gt_vtimer_access, 2139 .writefn = gt_virt_cval_write, .raw_writefn = raw_write, 2140 }, 2141 /* Secure timer -- this is actually restricted to only EL3 2142 * and configurably Secure-EL1 via the accessfn. 2143 */ 2144 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64, 2145 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0, 2146 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW, 2147 .accessfn = gt_stimer_access, 2148 .readfn = gt_sec_tval_read, 2149 .writefn = gt_sec_tval_write, 2150 .resetfn = gt_sec_timer_reset, 2151 }, 2152 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64, 2153 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1, 2154 .type = ARM_CP_IO, .access = PL1_RW, 2155 .accessfn = gt_stimer_access, 2156 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl), 2157 .resetvalue = 0, 2158 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 2159 }, 2160 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64, 2161 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2, 2162 .type = ARM_CP_IO, .access = PL1_RW, 2163 .accessfn = gt_stimer_access, 2164 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 2165 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 2166 }, 2167 REGINFO_SENTINEL 2168 }; 2169 2170 #else 2171 2172 /* In user-mode most of the generic timer registers are inaccessible 2173 * however modern kernels (4.12+) allow access to cntvct_el0 2174 */ 2175 2176 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2177 { 2178 /* Currently we have no support for QEMUTimer in linux-user so we 2179 * can't call gt_get_countervalue(env), instead we directly 2180 * call the lower level functions. 2181 */ 2182 return cpu_get_clock() / GTIMER_SCALE; 2183 } 2184 2185 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 2186 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 2187 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 2188 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */, 2189 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 2190 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE, 2191 }, 2192 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 2193 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 2194 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2195 .readfn = gt_virt_cnt_read, 2196 }, 2197 REGINFO_SENTINEL 2198 }; 2199 2200 #endif 2201 2202 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 2203 { 2204 if (arm_feature(env, ARM_FEATURE_LPAE)) { 2205 raw_write(env, ri, value); 2206 } else if (arm_feature(env, ARM_FEATURE_V7)) { 2207 raw_write(env, ri, value & 0xfffff6ff); 2208 } else { 2209 raw_write(env, ri, value & 0xfffff1ff); 2210 } 2211 } 2212 2213 #ifndef CONFIG_USER_ONLY 2214 /* get_phys_addr() isn't present for user-mode-only targets */ 2215 2216 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri, 2217 bool isread) 2218 { 2219 if (ri->opc2 & 4) { 2220 /* The ATS12NSO* operations must trap to EL3 if executed in 2221 * Secure EL1 (which can only happen if EL3 is AArch64). 2222 * They are simply UNDEF if executed from NS EL1. 2223 * They function normally from EL2 or EL3. 2224 */ 2225 if (arm_current_el(env) == 1) { 2226 if (arm_is_secure_below_el3(env)) { 2227 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3; 2228 } 2229 return CP_ACCESS_TRAP_UNCATEGORIZED; 2230 } 2231 } 2232 return CP_ACCESS_OK; 2233 } 2234 2235 static uint64_t do_ats_write(CPUARMState *env, uint64_t value, 2236 MMUAccessType access_type, ARMMMUIdx mmu_idx) 2237 { 2238 hwaddr phys_addr; 2239 target_ulong page_size; 2240 int prot; 2241 bool ret; 2242 uint64_t par64; 2243 bool format64 = false; 2244 MemTxAttrs attrs = {}; 2245 ARMMMUFaultInfo fi = {}; 2246 ARMCacheAttrs cacheattrs = {}; 2247 2248 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs, 2249 &prot, &page_size, &fi, &cacheattrs); 2250 2251 if (is_a64(env)) { 2252 format64 = true; 2253 } else if (arm_feature(env, ARM_FEATURE_LPAE)) { 2254 /* 2255 * ATS1Cxx: 2256 * * TTBCR.EAE determines whether the result is returned using the 2257 * 32-bit or the 64-bit PAR format 2258 * * Instructions executed in Hyp mode always use the 64bit format 2259 * 2260 * ATS1S2NSOxx uses the 64bit format if any of the following is true: 2261 * * The Non-secure TTBCR.EAE bit is set to 1 2262 * * The implementation includes EL2, and the value of HCR.VM is 1 2263 * 2264 * ATS1Hx always uses the 64bit format (not supported yet). 2265 */ 2266 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx); 2267 2268 if (arm_feature(env, ARM_FEATURE_EL2)) { 2269 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 2270 format64 |= env->cp15.hcr_el2 & HCR_VM; 2271 } else { 2272 format64 |= arm_current_el(env) == 2; 2273 } 2274 } 2275 } 2276 2277 if (format64) { 2278 /* Create a 64-bit PAR */ 2279 par64 = (1 << 11); /* LPAE bit always set */ 2280 if (!ret) { 2281 par64 |= phys_addr & ~0xfffULL; 2282 if (!attrs.secure) { 2283 par64 |= (1 << 9); /* NS */ 2284 } 2285 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */ 2286 par64 |= cacheattrs.shareability << 7; /* SH */ 2287 } else { 2288 uint32_t fsr = arm_fi_to_lfsc(&fi); 2289 2290 par64 |= 1; /* F */ 2291 par64 |= (fsr & 0x3f) << 1; /* FS */ 2292 /* Note that S2WLK and FSTAGE are always zero, because we don't 2293 * implement virtualization and therefore there can't be a stage 2 2294 * fault. 2295 */ 2296 } 2297 } else { 2298 /* fsr is a DFSR/IFSR value for the short descriptor 2299 * translation table format (with WnR always clear). 2300 * Convert it to a 32-bit PAR. 2301 */ 2302 if (!ret) { 2303 /* We do not set any attribute bits in the PAR */ 2304 if (page_size == (1 << 24) 2305 && arm_feature(env, ARM_FEATURE_V7)) { 2306 par64 = (phys_addr & 0xff000000) | (1 << 1); 2307 } else { 2308 par64 = phys_addr & 0xfffff000; 2309 } 2310 if (!attrs.secure) { 2311 par64 |= (1 << 9); /* NS */ 2312 } 2313 } else { 2314 uint32_t fsr = arm_fi_to_sfsc(&fi); 2315 2316 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) | 2317 ((fsr & 0xf) << 1) | 1; 2318 } 2319 } 2320 return par64; 2321 } 2322 2323 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 2324 { 2325 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 2326 uint64_t par64; 2327 ARMMMUIdx mmu_idx; 2328 int el = arm_current_el(env); 2329 bool secure = arm_is_secure_below_el3(env); 2330 2331 switch (ri->opc2 & 6) { 2332 case 0: 2333 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */ 2334 switch (el) { 2335 case 3: 2336 mmu_idx = ARMMMUIdx_S1E3; 2337 break; 2338 case 2: 2339 mmu_idx = ARMMMUIdx_S1NSE1; 2340 break; 2341 case 1: 2342 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; 2343 break; 2344 default: 2345 g_assert_not_reached(); 2346 } 2347 break; 2348 case 2: 2349 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */ 2350 switch (el) { 2351 case 3: 2352 mmu_idx = ARMMMUIdx_S1SE0; 2353 break; 2354 case 2: 2355 mmu_idx = ARMMMUIdx_S1NSE0; 2356 break; 2357 case 1: 2358 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; 2359 break; 2360 default: 2361 g_assert_not_reached(); 2362 } 2363 break; 2364 case 4: 2365 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */ 2366 mmu_idx = ARMMMUIdx_S12NSE1; 2367 break; 2368 case 6: 2369 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */ 2370 mmu_idx = ARMMMUIdx_S12NSE0; 2371 break; 2372 default: 2373 g_assert_not_reached(); 2374 } 2375 2376 par64 = do_ats_write(env, value, access_type, mmu_idx); 2377 2378 A32_BANKED_CURRENT_REG_SET(env, par, par64); 2379 } 2380 2381 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri, 2382 uint64_t value) 2383 { 2384 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 2385 uint64_t par64; 2386 2387 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS); 2388 2389 A32_BANKED_CURRENT_REG_SET(env, par, par64); 2390 } 2391 2392 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri, 2393 bool isread) 2394 { 2395 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) { 2396 return CP_ACCESS_TRAP; 2397 } 2398 return CP_ACCESS_OK; 2399 } 2400 2401 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri, 2402 uint64_t value) 2403 { 2404 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 2405 ARMMMUIdx mmu_idx; 2406 int secure = arm_is_secure_below_el3(env); 2407 2408 switch (ri->opc2 & 6) { 2409 case 0: 2410 switch (ri->opc1) { 2411 case 0: /* AT S1E1R, AT S1E1W */ 2412 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; 2413 break; 2414 case 4: /* AT S1E2R, AT S1E2W */ 2415 mmu_idx = ARMMMUIdx_S1E2; 2416 break; 2417 case 6: /* AT S1E3R, AT S1E3W */ 2418 mmu_idx = ARMMMUIdx_S1E3; 2419 break; 2420 default: 2421 g_assert_not_reached(); 2422 } 2423 break; 2424 case 2: /* AT S1E0R, AT S1E0W */ 2425 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; 2426 break; 2427 case 4: /* AT S12E1R, AT S12E1W */ 2428 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1; 2429 break; 2430 case 6: /* AT S12E0R, AT S12E0W */ 2431 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0; 2432 break; 2433 default: 2434 g_assert_not_reached(); 2435 } 2436 2437 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx); 2438 } 2439 #endif 2440 2441 static const ARMCPRegInfo vapa_cp_reginfo[] = { 2442 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, 2443 .access = PL1_RW, .resetvalue = 0, 2444 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), 2445 offsetoflow32(CPUARMState, cp15.par_ns) }, 2446 .writefn = par_write }, 2447 #ifndef CONFIG_USER_ONLY 2448 /* This underdecoding is safe because the reginfo is NO_RAW. */ 2449 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, 2450 .access = PL1_W, .accessfn = ats_access, 2451 .writefn = ats_write, .type = ARM_CP_NO_RAW }, 2452 #endif 2453 REGINFO_SENTINEL 2454 }; 2455 2456 /* Return basic MPU access permission bits. */ 2457 static uint32_t simple_mpu_ap_bits(uint32_t val) 2458 { 2459 uint32_t ret; 2460 uint32_t mask; 2461 int i; 2462 ret = 0; 2463 mask = 3; 2464 for (i = 0; i < 16; i += 2) { 2465 ret |= (val >> i) & mask; 2466 mask <<= 2; 2467 } 2468 return ret; 2469 } 2470 2471 /* Pad basic MPU access permission bits to extended format. */ 2472 static uint32_t extended_mpu_ap_bits(uint32_t val) 2473 { 2474 uint32_t ret; 2475 uint32_t mask; 2476 int i; 2477 ret = 0; 2478 mask = 3; 2479 for (i = 0; i < 16; i += 2) { 2480 ret |= (val & mask) << i; 2481 mask <<= 2; 2482 } 2483 return ret; 2484 } 2485 2486 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 2487 uint64_t value) 2488 { 2489 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); 2490 } 2491 2492 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 2493 { 2494 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); 2495 } 2496 2497 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 2498 uint64_t value) 2499 { 2500 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); 2501 } 2502 2503 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 2504 { 2505 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); 2506 } 2507 2508 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri) 2509 { 2510 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 2511 2512 if (!u32p) { 2513 return 0; 2514 } 2515 2516 u32p += env->pmsav7.rnr[M_REG_NS]; 2517 return *u32p; 2518 } 2519 2520 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri, 2521 uint64_t value) 2522 { 2523 ARMCPU *cpu = arm_env_get_cpu(env); 2524 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 2525 2526 if (!u32p) { 2527 return; 2528 } 2529 2530 u32p += env->pmsav7.rnr[M_REG_NS]; 2531 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 2532 *u32p = value; 2533 } 2534 2535 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2536 uint64_t value) 2537 { 2538 ARMCPU *cpu = arm_env_get_cpu(env); 2539 uint32_t nrgs = cpu->pmsav7_dregion; 2540 2541 if (value >= nrgs) { 2542 qemu_log_mask(LOG_GUEST_ERROR, 2543 "PMSAv7 RGNR write >= # supported regions, %" PRIu32 2544 " > %" PRIu32 "\n", (uint32_t)value, nrgs); 2545 return; 2546 } 2547 2548 raw_write(env, ri, value); 2549 } 2550 2551 static const ARMCPRegInfo pmsav7_cp_reginfo[] = { 2552 /* Reset for all these registers is handled in arm_cpu_reset(), 2553 * because the PMSAv7 is also used by M-profile CPUs, which do 2554 * not register cpregs but still need the state to be reset. 2555 */ 2556 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0, 2557 .access = PL1_RW, .type = ARM_CP_NO_RAW, 2558 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar), 2559 .readfn = pmsav7_read, .writefn = pmsav7_write, 2560 .resetfn = arm_cp_reset_ignore }, 2561 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2, 2562 .access = PL1_RW, .type = ARM_CP_NO_RAW, 2563 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr), 2564 .readfn = pmsav7_read, .writefn = pmsav7_write, 2565 .resetfn = arm_cp_reset_ignore }, 2566 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4, 2567 .access = PL1_RW, .type = ARM_CP_NO_RAW, 2568 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr), 2569 .readfn = pmsav7_read, .writefn = pmsav7_write, 2570 .resetfn = arm_cp_reset_ignore }, 2571 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0, 2572 .access = PL1_RW, 2573 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]), 2574 .writefn = pmsav7_rgnr_write, 2575 .resetfn = arm_cp_reset_ignore }, 2576 REGINFO_SENTINEL 2577 }; 2578 2579 static const ARMCPRegInfo pmsav5_cp_reginfo[] = { 2580 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 2581 .access = PL1_RW, .type = ARM_CP_ALIAS, 2582 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 2583 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, 2584 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 2585 .access = PL1_RW, .type = ARM_CP_ALIAS, 2586 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 2587 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, 2588 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, 2589 .access = PL1_RW, 2590 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 2591 .resetvalue = 0, }, 2592 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, 2593 .access = PL1_RW, 2594 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 2595 .resetvalue = 0, }, 2596 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 2597 .access = PL1_RW, 2598 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, 2599 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, 2600 .access = PL1_RW, 2601 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, 2602 /* Protection region base and size registers */ 2603 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, 2604 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2605 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, 2606 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, 2607 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2608 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, 2609 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, 2610 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2611 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, 2612 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, 2613 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2614 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, 2615 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, 2616 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2617 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, 2618 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, 2619 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2620 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, 2621 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, 2622 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2623 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, 2624 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, 2625 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2626 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, 2627 REGINFO_SENTINEL 2628 }; 2629 2630 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 2631 uint64_t value) 2632 { 2633 TCR *tcr = raw_ptr(env, ri); 2634 int maskshift = extract32(value, 0, 3); 2635 2636 if (!arm_feature(env, ARM_FEATURE_V8)) { 2637 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { 2638 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when 2639 * using Long-desciptor translation table format */ 2640 value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); 2641 } else if (arm_feature(env, ARM_FEATURE_EL3)) { 2642 /* In an implementation that includes the Security Extensions 2643 * TTBCR has additional fields PD0 [4] and PD1 [5] for 2644 * Short-descriptor translation table format. 2645 */ 2646 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; 2647 } else { 2648 value &= TTBCR_N; 2649 } 2650 } 2651 2652 /* Update the masks corresponding to the TCR bank being written 2653 * Note that we always calculate mask and base_mask, but 2654 * they are only used for short-descriptor tables (ie if EAE is 0); 2655 * for long-descriptor tables the TCR fields are used differently 2656 * and the mask and base_mask values are meaningless. 2657 */ 2658 tcr->raw_tcr = value; 2659 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift); 2660 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift); 2661 } 2662 2663 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2664 uint64_t value) 2665 { 2666 ARMCPU *cpu = arm_env_get_cpu(env); 2667 2668 if (arm_feature(env, ARM_FEATURE_LPAE)) { 2669 /* With LPAE the TTBCR could result in a change of ASID 2670 * via the TTBCR.A1 bit, so do a TLB flush. 2671 */ 2672 tlb_flush(CPU(cpu)); 2673 } 2674 vmsa_ttbcr_raw_write(env, ri, value); 2675 } 2676 2677 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2678 { 2679 TCR *tcr = raw_ptr(env, ri); 2680 2681 /* Reset both the TCR as well as the masks corresponding to the bank of 2682 * the TCR being reset. 2683 */ 2684 tcr->raw_tcr = 0; 2685 tcr->mask = 0; 2686 tcr->base_mask = 0xffffc000u; 2687 } 2688 2689 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, 2690 uint64_t value) 2691 { 2692 ARMCPU *cpu = arm_env_get_cpu(env); 2693 TCR *tcr = raw_ptr(env, ri); 2694 2695 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ 2696 tlb_flush(CPU(cpu)); 2697 tcr->raw_tcr = value; 2698 } 2699 2700 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2701 uint64_t value) 2702 { 2703 /* 64 bit accesses to the TTBRs can change the ASID and so we 2704 * must flush the TLB. 2705 */ 2706 if (cpreg_field_is_64bit(ri)) { 2707 ARMCPU *cpu = arm_env_get_cpu(env); 2708 2709 tlb_flush(CPU(cpu)); 2710 } 2711 raw_write(env, ri, value); 2712 } 2713 2714 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2715 uint64_t value) 2716 { 2717 ARMCPU *cpu = arm_env_get_cpu(env); 2718 CPUState *cs = CPU(cpu); 2719 2720 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */ 2721 if (raw_read(env, ri) != value) { 2722 tlb_flush_by_mmuidx(cs, 2723 ARMMMUIdxBit_S12NSE1 | 2724 ARMMMUIdxBit_S12NSE0 | 2725 ARMMMUIdxBit_S2NS); 2726 raw_write(env, ri, value); 2727 } 2728 } 2729 2730 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = { 2731 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 2732 .access = PL1_RW, .type = ARM_CP_ALIAS, 2733 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), 2734 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, }, 2735 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 2736 .access = PL1_RW, .resetvalue = 0, 2737 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), 2738 offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, 2739 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, 2740 .access = PL1_RW, .resetvalue = 0, 2741 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), 2742 offsetof(CPUARMState, cp15.dfar_ns) } }, 2743 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, 2744 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, 2745 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), 2746 .resetvalue = 0, }, 2747 REGINFO_SENTINEL 2748 }; 2749 2750 static const ARMCPRegInfo vmsa_cp_reginfo[] = { 2751 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, 2752 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, 2753 .access = PL1_RW, 2754 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, 2755 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, 2756 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, 2757 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 2758 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 2759 offsetof(CPUARMState, cp15.ttbr0_ns) } }, 2760 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, 2761 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, 2762 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 2763 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 2764 offsetof(CPUARMState, cp15.ttbr1_ns) } }, 2765 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, 2766 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 2767 .access = PL1_RW, .writefn = vmsa_tcr_el1_write, 2768 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write, 2769 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, 2770 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 2771 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write, 2772 .raw_writefn = vmsa_ttbcr_raw_write, 2773 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), 2774 offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, 2775 REGINFO_SENTINEL 2776 }; 2777 2778 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, 2779 uint64_t value) 2780 { 2781 env->cp15.c15_ticonfig = value & 0xe7; 2782 /* The OS_TYPE bit in this register changes the reported CPUID! */ 2783 env->cp15.c0_cpuid = (value & (1 << 5)) ? 2784 ARM_CPUID_TI915T : ARM_CPUID_TI925T; 2785 } 2786 2787 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, 2788 uint64_t value) 2789 { 2790 env->cp15.c15_threadid = value & 0xffff; 2791 } 2792 2793 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, 2794 uint64_t value) 2795 { 2796 /* Wait-for-interrupt (deprecated) */ 2797 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT); 2798 } 2799 2800 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, 2801 uint64_t value) 2802 { 2803 /* On OMAP there are registers indicating the max/min index of dcache lines 2804 * containing a dirty line; cache flush operations have to reset these. 2805 */ 2806 env->cp15.c15_i_max = 0x000; 2807 env->cp15.c15_i_min = 0xff0; 2808 } 2809 2810 static const ARMCPRegInfo omap_cp_reginfo[] = { 2811 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, 2812 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, 2813 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), 2814 .resetvalue = 0, }, 2815 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, 2816 .access = PL1_RW, .type = ARM_CP_NOP }, 2817 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, 2818 .access = PL1_RW, 2819 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, 2820 .writefn = omap_ticonfig_write }, 2821 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, 2822 .access = PL1_RW, 2823 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, 2824 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, 2825 .access = PL1_RW, .resetvalue = 0xff0, 2826 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, 2827 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, 2828 .access = PL1_RW, 2829 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, 2830 .writefn = omap_threadid_write }, 2831 { .name = "TI925T_STATUS", .cp = 15, .crn = 15, 2832 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 2833 .type = ARM_CP_NO_RAW, 2834 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, 2835 /* TODO: Peripheral port remap register: 2836 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller 2837 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), 2838 * when MMU is off. 2839 */ 2840 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 2841 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 2842 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW, 2843 .writefn = omap_cachemaint_write }, 2844 { .name = "C9", .cp = 15, .crn = 9, 2845 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, 2846 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, 2847 REGINFO_SENTINEL 2848 }; 2849 2850 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, 2851 uint64_t value) 2852 { 2853 env->cp15.c15_cpar = value & 0x3fff; 2854 } 2855 2856 static const ARMCPRegInfo xscale_cp_reginfo[] = { 2857 { .name = "XSCALE_CPAR", 2858 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 2859 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, 2860 .writefn = xscale_cpar_write, }, 2861 { .name = "XSCALE_AUXCR", 2862 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, 2863 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), 2864 .resetvalue = 0, }, 2865 /* XScale specific cache-lockdown: since we have no cache we NOP these 2866 * and hope the guest does not really rely on cache behaviour. 2867 */ 2868 { .name = "XSCALE_LOCK_ICACHE_LINE", 2869 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, 2870 .access = PL1_W, .type = ARM_CP_NOP }, 2871 { .name = "XSCALE_UNLOCK_ICACHE", 2872 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, 2873 .access = PL1_W, .type = ARM_CP_NOP }, 2874 { .name = "XSCALE_DCACHE_LOCK", 2875 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, 2876 .access = PL1_RW, .type = ARM_CP_NOP }, 2877 { .name = "XSCALE_UNLOCK_DCACHE", 2878 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, 2879 .access = PL1_W, .type = ARM_CP_NOP }, 2880 REGINFO_SENTINEL 2881 }; 2882 2883 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { 2884 /* RAZ/WI the whole crn=15 space, when we don't have a more specific 2885 * implementation of this implementation-defined space. 2886 * Ideally this should eventually disappear in favour of actually 2887 * implementing the correct behaviour for all cores. 2888 */ 2889 { .name = "C15_IMPDEF", .cp = 15, .crn = 15, 2890 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 2891 .access = PL1_RW, 2892 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE, 2893 .resetvalue = 0 }, 2894 REGINFO_SENTINEL 2895 }; 2896 2897 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { 2898 /* Cache status: RAZ because we have no cache so it's always clean */ 2899 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, 2900 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 2901 .resetvalue = 0 }, 2902 REGINFO_SENTINEL 2903 }; 2904 2905 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { 2906 /* We never have a a block transfer operation in progress */ 2907 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, 2908 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 2909 .resetvalue = 0 }, 2910 /* The cache ops themselves: these all NOP for QEMU */ 2911 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, 2912 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2913 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, 2914 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2915 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, 2916 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2917 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, 2918 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2919 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, 2920 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2921 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, 2922 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2923 REGINFO_SENTINEL 2924 }; 2925 2926 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { 2927 /* The cache test-and-clean instructions always return (1 << 30) 2928 * to indicate that there are no dirty cache lines. 2929 */ 2930 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, 2931 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 2932 .resetvalue = (1 << 30) }, 2933 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, 2934 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 2935 .resetvalue = (1 << 30) }, 2936 REGINFO_SENTINEL 2937 }; 2938 2939 static const ARMCPRegInfo strongarm_cp_reginfo[] = { 2940 /* Ignore ReadBuffer accesses */ 2941 { .name = "C9_READBUFFER", .cp = 15, .crn = 9, 2942 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 2943 .access = PL1_RW, .resetvalue = 0, 2944 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW }, 2945 REGINFO_SENTINEL 2946 }; 2947 2948 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2949 { 2950 ARMCPU *cpu = arm_env_get_cpu(env); 2951 unsigned int cur_el = arm_current_el(env); 2952 bool secure = arm_is_secure(env); 2953 2954 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 2955 return env->cp15.vpidr_el2; 2956 } 2957 return raw_read(env, ri); 2958 } 2959 2960 static uint64_t mpidr_read_val(CPUARMState *env) 2961 { 2962 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env)); 2963 uint64_t mpidr = cpu->mp_affinity; 2964 2965 if (arm_feature(env, ARM_FEATURE_V7MP)) { 2966 mpidr |= (1U << 31); 2967 /* Cores which are uniprocessor (non-coherent) 2968 * but still implement the MP extensions set 2969 * bit 30. (For instance, Cortex-R5). 2970 */ 2971 if (cpu->mp_is_up) { 2972 mpidr |= (1u << 30); 2973 } 2974 } 2975 return mpidr; 2976 } 2977 2978 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2979 { 2980 unsigned int cur_el = arm_current_el(env); 2981 bool secure = arm_is_secure(env); 2982 2983 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 2984 return env->cp15.vmpidr_el2; 2985 } 2986 return mpidr_read_val(env); 2987 } 2988 2989 static const ARMCPRegInfo mpidr_cp_reginfo[] = { 2990 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH, 2991 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, 2992 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW }, 2993 REGINFO_SENTINEL 2994 }; 2995 2996 static const ARMCPRegInfo lpae_cp_reginfo[] = { 2997 /* NOP AMAIR0/1 */ 2998 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, 2999 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, 3000 .access = PL1_RW, .type = ARM_CP_CONST, 3001 .resetvalue = 0 }, 3002 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ 3003 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, 3004 .access = PL1_RW, .type = ARM_CP_CONST, 3005 .resetvalue = 0 }, 3006 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, 3007 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, 3008 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), 3009 offsetof(CPUARMState, cp15.par_ns)} }, 3010 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, 3011 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3012 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 3013 offsetof(CPUARMState, cp15.ttbr0_ns) }, 3014 .writefn = vmsa_ttbr_write, }, 3015 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, 3016 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3017 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 3018 offsetof(CPUARMState, cp15.ttbr1_ns) }, 3019 .writefn = vmsa_ttbr_write, }, 3020 REGINFO_SENTINEL 3021 }; 3022 3023 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3024 { 3025 return vfp_get_fpcr(env); 3026 } 3027 3028 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3029 uint64_t value) 3030 { 3031 vfp_set_fpcr(env, value); 3032 } 3033 3034 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3035 { 3036 return vfp_get_fpsr(env); 3037 } 3038 3039 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3040 uint64_t value) 3041 { 3042 vfp_set_fpsr(env, value); 3043 } 3044 3045 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri, 3046 bool isread) 3047 { 3048 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) { 3049 return CP_ACCESS_TRAP; 3050 } 3051 return CP_ACCESS_OK; 3052 } 3053 3054 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, 3055 uint64_t value) 3056 { 3057 env->daif = value & PSTATE_DAIF; 3058 } 3059 3060 static CPAccessResult aa64_cacheop_access(CPUARMState *env, 3061 const ARMCPRegInfo *ri, 3062 bool isread) 3063 { 3064 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless 3065 * SCTLR_EL1.UCI is set. 3066 */ 3067 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) { 3068 return CP_ACCESS_TRAP; 3069 } 3070 return CP_ACCESS_OK; 3071 } 3072 3073 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions 3074 * Page D4-1736 (DDI0487A.b) 3075 */ 3076 3077 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3078 uint64_t value) 3079 { 3080 CPUState *cs = ENV_GET_CPU(env); 3081 3082 if (arm_is_secure_below_el3(env)) { 3083 tlb_flush_by_mmuidx(cs, 3084 ARMMMUIdxBit_S1SE1 | 3085 ARMMMUIdxBit_S1SE0); 3086 } else { 3087 tlb_flush_by_mmuidx(cs, 3088 ARMMMUIdxBit_S12NSE1 | 3089 ARMMMUIdxBit_S12NSE0); 3090 } 3091 } 3092 3093 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3094 uint64_t value) 3095 { 3096 CPUState *cs = ENV_GET_CPU(env); 3097 bool sec = arm_is_secure_below_el3(env); 3098 3099 if (sec) { 3100 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3101 ARMMMUIdxBit_S1SE1 | 3102 ARMMMUIdxBit_S1SE0); 3103 } else { 3104 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3105 ARMMMUIdxBit_S12NSE1 | 3106 ARMMMUIdxBit_S12NSE0); 3107 } 3108 } 3109 3110 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3111 uint64_t value) 3112 { 3113 /* Note that the 'ALL' scope must invalidate both stage 1 and 3114 * stage 2 translations, whereas most other scopes only invalidate 3115 * stage 1 translations. 3116 */ 3117 ARMCPU *cpu = arm_env_get_cpu(env); 3118 CPUState *cs = CPU(cpu); 3119 3120 if (arm_is_secure_below_el3(env)) { 3121 tlb_flush_by_mmuidx(cs, 3122 ARMMMUIdxBit_S1SE1 | 3123 ARMMMUIdxBit_S1SE0); 3124 } else { 3125 if (arm_feature(env, ARM_FEATURE_EL2)) { 3126 tlb_flush_by_mmuidx(cs, 3127 ARMMMUIdxBit_S12NSE1 | 3128 ARMMMUIdxBit_S12NSE0 | 3129 ARMMMUIdxBit_S2NS); 3130 } else { 3131 tlb_flush_by_mmuidx(cs, 3132 ARMMMUIdxBit_S12NSE1 | 3133 ARMMMUIdxBit_S12NSE0); 3134 } 3135 } 3136 } 3137 3138 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3139 uint64_t value) 3140 { 3141 ARMCPU *cpu = arm_env_get_cpu(env); 3142 CPUState *cs = CPU(cpu); 3143 3144 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2); 3145 } 3146 3147 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri, 3148 uint64_t value) 3149 { 3150 ARMCPU *cpu = arm_env_get_cpu(env); 3151 CPUState *cs = CPU(cpu); 3152 3153 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3); 3154 } 3155 3156 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3157 uint64_t value) 3158 { 3159 /* Note that the 'ALL' scope must invalidate both stage 1 and 3160 * stage 2 translations, whereas most other scopes only invalidate 3161 * stage 1 translations. 3162 */ 3163 CPUState *cs = ENV_GET_CPU(env); 3164 bool sec = arm_is_secure_below_el3(env); 3165 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2); 3166 3167 if (sec) { 3168 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3169 ARMMMUIdxBit_S1SE1 | 3170 ARMMMUIdxBit_S1SE0); 3171 } else if (has_el2) { 3172 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3173 ARMMMUIdxBit_S12NSE1 | 3174 ARMMMUIdxBit_S12NSE0 | 3175 ARMMMUIdxBit_S2NS); 3176 } else { 3177 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3178 ARMMMUIdxBit_S12NSE1 | 3179 ARMMMUIdxBit_S12NSE0); 3180 } 3181 } 3182 3183 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3184 uint64_t value) 3185 { 3186 CPUState *cs = ENV_GET_CPU(env); 3187 3188 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2); 3189 } 3190 3191 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3192 uint64_t value) 3193 { 3194 CPUState *cs = ENV_GET_CPU(env); 3195 3196 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3); 3197 } 3198 3199 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3200 uint64_t value) 3201 { 3202 /* Invalidate by VA, EL1&0 (AArch64 version). 3203 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1, 3204 * since we don't support flush-for-specific-ASID-only or 3205 * flush-last-level-only. 3206 */ 3207 ARMCPU *cpu = arm_env_get_cpu(env); 3208 CPUState *cs = CPU(cpu); 3209 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3210 3211 if (arm_is_secure_below_el3(env)) { 3212 tlb_flush_page_by_mmuidx(cs, pageaddr, 3213 ARMMMUIdxBit_S1SE1 | 3214 ARMMMUIdxBit_S1SE0); 3215 } else { 3216 tlb_flush_page_by_mmuidx(cs, pageaddr, 3217 ARMMMUIdxBit_S12NSE1 | 3218 ARMMMUIdxBit_S12NSE0); 3219 } 3220 } 3221 3222 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3223 uint64_t value) 3224 { 3225 /* Invalidate by VA, EL2 3226 * Currently handles both VAE2 and VALE2, since we don't support 3227 * flush-last-level-only. 3228 */ 3229 ARMCPU *cpu = arm_env_get_cpu(env); 3230 CPUState *cs = CPU(cpu); 3231 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3232 3233 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2); 3234 } 3235 3236 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri, 3237 uint64_t value) 3238 { 3239 /* Invalidate by VA, EL3 3240 * Currently handles both VAE3 and VALE3, since we don't support 3241 * flush-last-level-only. 3242 */ 3243 ARMCPU *cpu = arm_env_get_cpu(env); 3244 CPUState *cs = CPU(cpu); 3245 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3246 3247 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3); 3248 } 3249 3250 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3251 uint64_t value) 3252 { 3253 ARMCPU *cpu = arm_env_get_cpu(env); 3254 CPUState *cs = CPU(cpu); 3255 bool sec = arm_is_secure_below_el3(env); 3256 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3257 3258 if (sec) { 3259 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3260 ARMMMUIdxBit_S1SE1 | 3261 ARMMMUIdxBit_S1SE0); 3262 } else { 3263 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3264 ARMMMUIdxBit_S12NSE1 | 3265 ARMMMUIdxBit_S12NSE0); 3266 } 3267 } 3268 3269 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3270 uint64_t value) 3271 { 3272 CPUState *cs = ENV_GET_CPU(env); 3273 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3274 3275 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3276 ARMMMUIdxBit_S1E2); 3277 } 3278 3279 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3280 uint64_t value) 3281 { 3282 CPUState *cs = ENV_GET_CPU(env); 3283 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3284 3285 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3286 ARMMMUIdxBit_S1E3); 3287 } 3288 3289 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3290 uint64_t value) 3291 { 3292 /* Invalidate by IPA. This has to invalidate any structures that 3293 * contain only stage 2 translation information, but does not need 3294 * to apply to structures that contain combined stage 1 and stage 2 3295 * translation information. 3296 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. 3297 */ 3298 ARMCPU *cpu = arm_env_get_cpu(env); 3299 CPUState *cs = CPU(cpu); 3300 uint64_t pageaddr; 3301 3302 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 3303 return; 3304 } 3305 3306 pageaddr = sextract64(value << 12, 0, 48); 3307 3308 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS); 3309 } 3310 3311 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3312 uint64_t value) 3313 { 3314 CPUState *cs = ENV_GET_CPU(env); 3315 uint64_t pageaddr; 3316 3317 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 3318 return; 3319 } 3320 3321 pageaddr = sextract64(value << 12, 0, 48); 3322 3323 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3324 ARMMMUIdxBit_S2NS); 3325 } 3326 3327 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri, 3328 bool isread) 3329 { 3330 /* We don't implement EL2, so the only control on DC ZVA is the 3331 * bit in the SCTLR which can prohibit access for EL0. 3332 */ 3333 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) { 3334 return CP_ACCESS_TRAP; 3335 } 3336 return CP_ACCESS_OK; 3337 } 3338 3339 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) 3340 { 3341 ARMCPU *cpu = arm_env_get_cpu(env); 3342 int dzp_bit = 1 << 4; 3343 3344 /* DZP indicates whether DC ZVA access is allowed */ 3345 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) { 3346 dzp_bit = 0; 3347 } 3348 return cpu->dcz_blocksize | dzp_bit; 3349 } 3350 3351 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 3352 bool isread) 3353 { 3354 if (!(env->pstate & PSTATE_SP)) { 3355 /* Access to SP_EL0 is undefined if it's being used as 3356 * the stack pointer. 3357 */ 3358 return CP_ACCESS_TRAP_UNCATEGORIZED; 3359 } 3360 return CP_ACCESS_OK; 3361 } 3362 3363 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) 3364 { 3365 return env->pstate & PSTATE_SP; 3366 } 3367 3368 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 3369 { 3370 update_spsel(env, val); 3371 } 3372 3373 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3374 uint64_t value) 3375 { 3376 ARMCPU *cpu = arm_env_get_cpu(env); 3377 3378 if (raw_read(env, ri) == value) { 3379 /* Skip the TLB flush if nothing actually changed; Linux likes 3380 * to do a lot of pointless SCTLR writes. 3381 */ 3382 return; 3383 } 3384 3385 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) { 3386 /* M bit is RAZ/WI for PMSA with no MPU implemented */ 3387 value &= ~SCTLR_M; 3388 } 3389 3390 raw_write(env, ri, value); 3391 /* ??? Lots of these bits are not implemented. */ 3392 /* This may enable/disable the MMU, so do a TLB flush. */ 3393 tlb_flush(CPU(cpu)); 3394 } 3395 3396 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri, 3397 bool isread) 3398 { 3399 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) { 3400 return CP_ACCESS_TRAP_FP_EL2; 3401 } 3402 if (env->cp15.cptr_el[3] & CPTR_TFP) { 3403 return CP_ACCESS_TRAP_FP_EL3; 3404 } 3405 return CP_ACCESS_OK; 3406 } 3407 3408 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3409 uint64_t value) 3410 { 3411 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK; 3412 } 3413 3414 static const ARMCPRegInfo v8_cp_reginfo[] = { 3415 /* Minimal set of EL0-visible registers. This will need to be expanded 3416 * significantly for system emulation of AArch64 CPUs. 3417 */ 3418 { .name = "NZCV", .state = ARM_CP_STATE_AA64, 3419 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, 3420 .access = PL0_RW, .type = ARM_CP_NZCV }, 3421 { .name = "DAIF", .state = ARM_CP_STATE_AA64, 3422 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, 3423 .type = ARM_CP_NO_RAW, 3424 .access = PL0_RW, .accessfn = aa64_daif_access, 3425 .fieldoffset = offsetof(CPUARMState, daif), 3426 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, 3427 { .name = "FPCR", .state = ARM_CP_STATE_AA64, 3428 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, 3429 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 3430 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, 3431 { .name = "FPSR", .state = ARM_CP_STATE_AA64, 3432 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, 3433 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 3434 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, 3435 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, 3436 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, 3437 .access = PL0_R, .type = ARM_CP_NO_RAW, 3438 .readfn = aa64_dczid_read }, 3439 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, 3440 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, 3441 .access = PL0_W, .type = ARM_CP_DC_ZVA, 3442 #ifndef CONFIG_USER_ONLY 3443 /* Avoid overhead of an access check that always passes in user-mode */ 3444 .accessfn = aa64_zva_access, 3445 #endif 3446 }, 3447 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, 3448 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, 3449 .access = PL1_R, .type = ARM_CP_CURRENTEL }, 3450 /* Cache ops: all NOPs since we don't emulate caches */ 3451 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, 3452 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 3453 .access = PL1_W, .type = ARM_CP_NOP }, 3454 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, 3455 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 3456 .access = PL1_W, .type = ARM_CP_NOP }, 3457 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, 3458 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, 3459 .access = PL0_W, .type = ARM_CP_NOP, 3460 .accessfn = aa64_cacheop_access }, 3461 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, 3462 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 3463 .access = PL1_W, .type = ARM_CP_NOP }, 3464 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, 3465 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 3466 .access = PL1_W, .type = ARM_CP_NOP }, 3467 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, 3468 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, 3469 .access = PL0_W, .type = ARM_CP_NOP, 3470 .accessfn = aa64_cacheop_access }, 3471 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, 3472 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 3473 .access = PL1_W, .type = ARM_CP_NOP }, 3474 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, 3475 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, 3476 .access = PL0_W, .type = ARM_CP_NOP, 3477 .accessfn = aa64_cacheop_access }, 3478 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, 3479 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, 3480 .access = PL0_W, .type = ARM_CP_NOP, 3481 .accessfn = aa64_cacheop_access }, 3482 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, 3483 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 3484 .access = PL1_W, .type = ARM_CP_NOP }, 3485 /* TLBI operations */ 3486 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64, 3487 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 3488 .access = PL1_W, .type = ARM_CP_NO_RAW, 3489 .writefn = tlbi_aa64_vmalle1is_write }, 3490 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64, 3491 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 3492 .access = PL1_W, .type = ARM_CP_NO_RAW, 3493 .writefn = tlbi_aa64_vae1is_write }, 3494 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64, 3495 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 3496 .access = PL1_W, .type = ARM_CP_NO_RAW, 3497 .writefn = tlbi_aa64_vmalle1is_write }, 3498 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64, 3499 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 3500 .access = PL1_W, .type = ARM_CP_NO_RAW, 3501 .writefn = tlbi_aa64_vae1is_write }, 3502 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64, 3503 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 3504 .access = PL1_W, .type = ARM_CP_NO_RAW, 3505 .writefn = tlbi_aa64_vae1is_write }, 3506 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64, 3507 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 3508 .access = PL1_W, .type = ARM_CP_NO_RAW, 3509 .writefn = tlbi_aa64_vae1is_write }, 3510 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64, 3511 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 3512 .access = PL1_W, .type = ARM_CP_NO_RAW, 3513 .writefn = tlbi_aa64_vmalle1_write }, 3514 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64, 3515 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 3516 .access = PL1_W, .type = ARM_CP_NO_RAW, 3517 .writefn = tlbi_aa64_vae1_write }, 3518 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64, 3519 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 3520 .access = PL1_W, .type = ARM_CP_NO_RAW, 3521 .writefn = tlbi_aa64_vmalle1_write }, 3522 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64, 3523 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 3524 .access = PL1_W, .type = ARM_CP_NO_RAW, 3525 .writefn = tlbi_aa64_vae1_write }, 3526 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64, 3527 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 3528 .access = PL1_W, .type = ARM_CP_NO_RAW, 3529 .writefn = tlbi_aa64_vae1_write }, 3530 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64, 3531 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 3532 .access = PL1_W, .type = ARM_CP_NO_RAW, 3533 .writefn = tlbi_aa64_vae1_write }, 3534 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64, 3535 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 3536 .access = PL2_W, .type = ARM_CP_NO_RAW, 3537 .writefn = tlbi_aa64_ipas2e1is_write }, 3538 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64, 3539 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 3540 .access = PL2_W, .type = ARM_CP_NO_RAW, 3541 .writefn = tlbi_aa64_ipas2e1is_write }, 3542 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64, 3543 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 3544 .access = PL2_W, .type = ARM_CP_NO_RAW, 3545 .writefn = tlbi_aa64_alle1is_write }, 3546 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64, 3547 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6, 3548 .access = PL2_W, .type = ARM_CP_NO_RAW, 3549 .writefn = tlbi_aa64_alle1is_write }, 3550 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64, 3551 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 3552 .access = PL2_W, .type = ARM_CP_NO_RAW, 3553 .writefn = tlbi_aa64_ipas2e1_write }, 3554 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64, 3555 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 3556 .access = PL2_W, .type = ARM_CP_NO_RAW, 3557 .writefn = tlbi_aa64_ipas2e1_write }, 3558 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64, 3559 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 3560 .access = PL2_W, .type = ARM_CP_NO_RAW, 3561 .writefn = tlbi_aa64_alle1_write }, 3562 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64, 3563 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6, 3564 .access = PL2_W, .type = ARM_CP_NO_RAW, 3565 .writefn = tlbi_aa64_alle1is_write }, 3566 #ifndef CONFIG_USER_ONLY 3567 /* 64 bit address translation operations */ 3568 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, 3569 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, 3570 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3571 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, 3572 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, 3573 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3574 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, 3575 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, 3576 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3577 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, 3578 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, 3579 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3580 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64, 3581 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4, 3582 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3583 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64, 3584 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5, 3585 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3586 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64, 3587 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6, 3588 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3589 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64, 3590 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7, 3591 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3592 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */ 3593 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64, 3594 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0, 3595 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3596 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64, 3597 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1, 3598 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3599 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64, 3600 .type = ARM_CP_ALIAS, 3601 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0, 3602 .access = PL1_RW, .resetvalue = 0, 3603 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]), 3604 .writefn = par_write }, 3605 #endif 3606 /* TLB invalidate last level of translation table walk */ 3607 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 3608 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, 3609 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 3610 .type = ARM_CP_NO_RAW, .access = PL1_W, 3611 .writefn = tlbimvaa_is_write }, 3612 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 3613 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 3614 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 3615 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, 3616 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 3617 .type = ARM_CP_NO_RAW, .access = PL2_W, 3618 .writefn = tlbimva_hyp_write }, 3619 { .name = "TLBIMVALHIS", 3620 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 3621 .type = ARM_CP_NO_RAW, .access = PL2_W, 3622 .writefn = tlbimva_hyp_is_write }, 3623 { .name = "TLBIIPAS2", 3624 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 3625 .type = ARM_CP_NO_RAW, .access = PL2_W, 3626 .writefn = tlbiipas2_write }, 3627 { .name = "TLBIIPAS2IS", 3628 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 3629 .type = ARM_CP_NO_RAW, .access = PL2_W, 3630 .writefn = tlbiipas2_is_write }, 3631 { .name = "TLBIIPAS2L", 3632 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 3633 .type = ARM_CP_NO_RAW, .access = PL2_W, 3634 .writefn = tlbiipas2_write }, 3635 { .name = "TLBIIPAS2LIS", 3636 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 3637 .type = ARM_CP_NO_RAW, .access = PL2_W, 3638 .writefn = tlbiipas2_is_write }, 3639 /* 32 bit cache operations */ 3640 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 3641 .type = ARM_CP_NOP, .access = PL1_W }, 3642 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, 3643 .type = ARM_CP_NOP, .access = PL1_W }, 3644 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 3645 .type = ARM_CP_NOP, .access = PL1_W }, 3646 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, 3647 .type = ARM_CP_NOP, .access = PL1_W }, 3648 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, 3649 .type = ARM_CP_NOP, .access = PL1_W }, 3650 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, 3651 .type = ARM_CP_NOP, .access = PL1_W }, 3652 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 3653 .type = ARM_CP_NOP, .access = PL1_W }, 3654 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 3655 .type = ARM_CP_NOP, .access = PL1_W }, 3656 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, 3657 .type = ARM_CP_NOP, .access = PL1_W }, 3658 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 3659 .type = ARM_CP_NOP, .access = PL1_W }, 3660 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, 3661 .type = ARM_CP_NOP, .access = PL1_W }, 3662 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, 3663 .type = ARM_CP_NOP, .access = PL1_W }, 3664 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 3665 .type = ARM_CP_NOP, .access = PL1_W }, 3666 /* MMU Domain access control / MPU write buffer control */ 3667 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, 3668 .access = PL1_RW, .resetvalue = 0, 3669 .writefn = dacr_write, .raw_writefn = raw_write, 3670 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 3671 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 3672 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, 3673 .type = ARM_CP_ALIAS, 3674 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, 3675 .access = PL1_RW, 3676 .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, 3677 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, 3678 .type = ARM_CP_ALIAS, 3679 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, 3680 .access = PL1_RW, 3681 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) }, 3682 /* We rely on the access checks not allowing the guest to write to the 3683 * state field when SPSel indicates that it's being used as the stack 3684 * pointer. 3685 */ 3686 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, 3687 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, 3688 .access = PL1_RW, .accessfn = sp_el0_access, 3689 .type = ARM_CP_ALIAS, 3690 .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, 3691 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64, 3692 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0, 3693 .access = PL2_RW, .type = ARM_CP_ALIAS, 3694 .fieldoffset = offsetof(CPUARMState, sp_el[1]) }, 3695 { .name = "SPSel", .state = ARM_CP_STATE_AA64, 3696 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, 3697 .type = ARM_CP_NO_RAW, 3698 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, 3699 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64, 3700 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0, 3701 .type = ARM_CP_ALIAS, 3702 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]), 3703 .access = PL2_RW, .accessfn = fpexc32_access }, 3704 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, 3705 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, 3706 .access = PL2_RW, .resetvalue = 0, 3707 .writefn = dacr_write, .raw_writefn = raw_write, 3708 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, 3709 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, 3710 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, 3711 .access = PL2_RW, .resetvalue = 0, 3712 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, 3713 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64, 3714 .type = ARM_CP_ALIAS, 3715 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0, 3716 .access = PL2_RW, 3717 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) }, 3718 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64, 3719 .type = ARM_CP_ALIAS, 3720 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1, 3721 .access = PL2_RW, 3722 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) }, 3723 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64, 3724 .type = ARM_CP_ALIAS, 3725 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2, 3726 .access = PL2_RW, 3727 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) }, 3728 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64, 3729 .type = ARM_CP_ALIAS, 3730 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3, 3731 .access = PL2_RW, 3732 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) }, 3733 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64, 3734 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1, 3735 .resetvalue = 0, 3736 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) }, 3737 { .name = "SDCR", .type = ARM_CP_ALIAS, 3738 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1, 3739 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 3740 .writefn = sdcr_write, 3741 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) }, 3742 REGINFO_SENTINEL 3743 }; 3744 3745 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */ 3746 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = { 3747 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64, 3748 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 3749 .access = PL2_RW, 3750 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, 3751 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, 3752 .type = ARM_CP_NO_RAW, 3753 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 3754 .access = PL2_RW, 3755 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, 3756 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 3757 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 3758 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3759 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 3760 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 3761 .access = PL2_RW, .type = ARM_CP_CONST, 3762 .resetvalue = 0 }, 3763 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 3764 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 3765 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3766 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 3767 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 3768 .access = PL2_RW, .type = ARM_CP_CONST, 3769 .resetvalue = 0 }, 3770 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 3771 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 3772 .access = PL2_RW, .type = ARM_CP_CONST, 3773 .resetvalue = 0 }, 3774 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 3775 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 3776 .access = PL2_RW, .type = ARM_CP_CONST, 3777 .resetvalue = 0 }, 3778 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 3779 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 3780 .access = PL2_RW, .type = ARM_CP_CONST, 3781 .resetvalue = 0 }, 3782 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 3783 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 3784 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3785 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH, 3786 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 3787 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 3788 .type = ARM_CP_CONST, .resetvalue = 0 }, 3789 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 3790 .cp = 15, .opc1 = 6, .crm = 2, 3791 .access = PL2_RW, .accessfn = access_el3_aa32ns, 3792 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 3793 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 3794 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 3795 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3796 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 3797 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 3798 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3799 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 3800 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 3801 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3802 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 3803 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 3804 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3805 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 3806 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 3807 .resetvalue = 0 }, 3808 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 3809 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 3810 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3811 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 3812 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 3813 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3814 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 3815 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 3816 .resetvalue = 0 }, 3817 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 3818 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 3819 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3820 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 3821 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 3822 .resetvalue = 0 }, 3823 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 3824 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 3825 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3826 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 3827 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 3828 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3829 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 3830 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 3831 .access = PL2_RW, .accessfn = access_tda, 3832 .type = ARM_CP_CONST, .resetvalue = 0 }, 3833 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH, 3834 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 3835 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 3836 .type = ARM_CP_CONST, .resetvalue = 0 }, 3837 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 3838 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 3839 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3840 REGINFO_SENTINEL 3841 }; 3842 3843 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3844 { 3845 ARMCPU *cpu = arm_env_get_cpu(env); 3846 uint64_t valid_mask = HCR_MASK; 3847 3848 if (arm_feature(env, ARM_FEATURE_EL3)) { 3849 valid_mask &= ~HCR_HCD; 3850 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) { 3851 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented. 3852 * However, if we're using the SMC PSCI conduit then QEMU is 3853 * effectively acting like EL3 firmware and so the guest at 3854 * EL2 should retain the ability to prevent EL1 from being 3855 * able to make SMC calls into the ersatz firmware, so in 3856 * that case HCR.TSC should be read/write. 3857 */ 3858 valid_mask &= ~HCR_TSC; 3859 } 3860 3861 /* Clear RES0 bits. */ 3862 value &= valid_mask; 3863 3864 /* These bits change the MMU setup: 3865 * HCR_VM enables stage 2 translation 3866 * HCR_PTW forbids certain page-table setups 3867 * HCR_DC Disables stage1 and enables stage2 translation 3868 */ 3869 if ((raw_read(env, ri) ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) { 3870 tlb_flush(CPU(cpu)); 3871 } 3872 raw_write(env, ri, value); 3873 } 3874 3875 static const ARMCPRegInfo el2_cp_reginfo[] = { 3876 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, 3877 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 3878 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 3879 .writefn = hcr_write }, 3880 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, 3881 .type = ARM_CP_ALIAS, 3882 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, 3883 .access = PL2_RW, 3884 .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, 3885 { .name = "ESR_EL2", .state = ARM_CP_STATE_AA64, 3886 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 3887 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, 3888 { .name = "FAR_EL2", .state = ARM_CP_STATE_AA64, 3889 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 3890 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, 3891 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, 3892 .type = ARM_CP_ALIAS, 3893 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, 3894 .access = PL2_RW, 3895 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) }, 3896 { .name = "VBAR_EL2", .state = ARM_CP_STATE_AA64, 3897 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 3898 .access = PL2_RW, .writefn = vbar_write, 3899 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), 3900 .resetvalue = 0 }, 3901 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64, 3902 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0, 3903 .access = PL3_RW, .type = ARM_CP_ALIAS, 3904 .fieldoffset = offsetof(CPUARMState, sp_el[2]) }, 3905 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 3906 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 3907 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0, 3908 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) }, 3909 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 3910 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 3911 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]), 3912 .resetvalue = 0 }, 3913 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 3914 .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 3915 .access = PL2_RW, .type = ARM_CP_ALIAS, 3916 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) }, 3917 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 3918 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 3919 .access = PL2_RW, .type = ARM_CP_CONST, 3920 .resetvalue = 0 }, 3921 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */ 3922 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 3923 .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 3924 .access = PL2_RW, .type = ARM_CP_CONST, 3925 .resetvalue = 0 }, 3926 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 3927 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 3928 .access = PL2_RW, .type = ARM_CP_CONST, 3929 .resetvalue = 0 }, 3930 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 3931 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 3932 .access = PL2_RW, .type = ARM_CP_CONST, 3933 .resetvalue = 0 }, 3934 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 3935 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 3936 .access = PL2_RW, 3937 /* no .writefn needed as this can't cause an ASID change; 3938 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 3939 */ 3940 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) }, 3941 { .name = "VTCR", .state = ARM_CP_STATE_AA32, 3942 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 3943 .type = ARM_CP_ALIAS, 3944 .access = PL2_RW, .accessfn = access_el3_aa32ns, 3945 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 3946 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64, 3947 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 3948 .access = PL2_RW, 3949 /* no .writefn needed as this can't cause an ASID change; 3950 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 3951 */ 3952 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 3953 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 3954 .cp = 15, .opc1 = 6, .crm = 2, 3955 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3956 .access = PL2_RW, .accessfn = access_el3_aa32ns, 3957 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2), 3958 .writefn = vttbr_write }, 3959 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 3960 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 3961 .access = PL2_RW, .writefn = vttbr_write, 3962 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) }, 3963 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 3964 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 3965 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write, 3966 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) }, 3967 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 3968 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 3969 .access = PL2_RW, .resetvalue = 0, 3970 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) }, 3971 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 3972 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 3973 .access = PL2_RW, .resetvalue = 0, 3974 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 3975 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 3976 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3977 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 3978 { .name = "TLBIALLNSNH", 3979 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 3980 .type = ARM_CP_NO_RAW, .access = PL2_W, 3981 .writefn = tlbiall_nsnh_write }, 3982 { .name = "TLBIALLNSNHIS", 3983 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 3984 .type = ARM_CP_NO_RAW, .access = PL2_W, 3985 .writefn = tlbiall_nsnh_is_write }, 3986 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 3987 .type = ARM_CP_NO_RAW, .access = PL2_W, 3988 .writefn = tlbiall_hyp_write }, 3989 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 3990 .type = ARM_CP_NO_RAW, .access = PL2_W, 3991 .writefn = tlbiall_hyp_is_write }, 3992 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 3993 .type = ARM_CP_NO_RAW, .access = PL2_W, 3994 .writefn = tlbimva_hyp_write }, 3995 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 3996 .type = ARM_CP_NO_RAW, .access = PL2_W, 3997 .writefn = tlbimva_hyp_is_write }, 3998 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64, 3999 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 4000 .type = ARM_CP_NO_RAW, .access = PL2_W, 4001 .writefn = tlbi_aa64_alle2_write }, 4002 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64, 4003 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 4004 .type = ARM_CP_NO_RAW, .access = PL2_W, 4005 .writefn = tlbi_aa64_vae2_write }, 4006 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64, 4007 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 4008 .access = PL2_W, .type = ARM_CP_NO_RAW, 4009 .writefn = tlbi_aa64_vae2_write }, 4010 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64, 4011 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 4012 .access = PL2_W, .type = ARM_CP_NO_RAW, 4013 .writefn = tlbi_aa64_alle2is_write }, 4014 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64, 4015 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 4016 .type = ARM_CP_NO_RAW, .access = PL2_W, 4017 .writefn = tlbi_aa64_vae2is_write }, 4018 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64, 4019 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 4020 .access = PL2_W, .type = ARM_CP_NO_RAW, 4021 .writefn = tlbi_aa64_vae2is_write }, 4022 #ifndef CONFIG_USER_ONLY 4023 /* Unlike the other EL2-related AT operations, these must 4024 * UNDEF from EL3 if EL2 is not implemented, which is why we 4025 * define them here rather than with the rest of the AT ops. 4026 */ 4027 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64, 4028 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 4029 .access = PL2_W, .accessfn = at_s1e2_access, 4030 .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4031 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64, 4032 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 4033 .access = PL2_W, .accessfn = at_s1e2_access, 4034 .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4035 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE 4036 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3 4037 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose 4038 * to behave as if SCR.NS was 1. 4039 */ 4040 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 4041 .access = PL2_W, 4042 .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, 4043 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 4044 .access = PL2_W, 4045 .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, 4046 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 4047 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 4048 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the 4049 * reset values as IMPDEF. We choose to reset to 3 to comply with 4050 * both ARMv7 and ARMv8. 4051 */ 4052 .access = PL2_RW, .resetvalue = 3, 4053 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) }, 4054 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 4055 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 4056 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, 4057 .writefn = gt_cntvoff_write, 4058 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 4059 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 4060 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO, 4061 .writefn = gt_cntvoff_write, 4062 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 4063 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 4064 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 4065 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 4066 .type = ARM_CP_IO, .access = PL2_RW, 4067 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 4068 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 4069 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 4070 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO, 4071 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 4072 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 4073 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 4074 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 4075 .resetfn = gt_hyp_timer_reset, 4076 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write }, 4077 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 4078 .type = ARM_CP_IO, 4079 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 4080 .access = PL2_RW, 4081 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl), 4082 .resetvalue = 0, 4083 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write }, 4084 #endif 4085 /* The only field of MDCR_EL2 that has a defined architectural reset value 4086 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we 4087 * don't impelment any PMU event counters, so using zero as a reset 4088 * value for MDCR_EL2 is okay 4089 */ 4090 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 4091 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 4092 .access = PL2_RW, .resetvalue = 0, 4093 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), }, 4094 { .name = "HPFAR", .state = ARM_CP_STATE_AA32, 4095 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4096 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4097 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 4098 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64, 4099 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4100 .access = PL2_RW, 4101 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 4102 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 4103 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 4104 .access = PL2_RW, 4105 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) }, 4106 REGINFO_SENTINEL 4107 }; 4108 4109 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 4110 bool isread) 4111 { 4112 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2. 4113 * At Secure EL1 it traps to EL3. 4114 */ 4115 if (arm_current_el(env) == 3) { 4116 return CP_ACCESS_OK; 4117 } 4118 if (arm_is_secure_below_el3(env)) { 4119 return CP_ACCESS_TRAP_EL3; 4120 } 4121 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */ 4122 if (isread) { 4123 return CP_ACCESS_OK; 4124 } 4125 return CP_ACCESS_TRAP_UNCATEGORIZED; 4126 } 4127 4128 static const ARMCPRegInfo el3_cp_reginfo[] = { 4129 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, 4130 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, 4131 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), 4132 .resetvalue = 0, .writefn = scr_write }, 4133 { .name = "SCR", .type = ARM_CP_ALIAS, 4134 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, 4135 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 4136 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), 4137 .writefn = scr_write }, 4138 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, 4139 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, 4140 .access = PL3_RW, .resetvalue = 0, 4141 .fieldoffset = offsetof(CPUARMState, cp15.sder) }, 4142 { .name = "SDER", 4143 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, 4144 .access = PL3_RW, .resetvalue = 0, 4145 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, 4146 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 4147 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 4148 .writefn = vbar_write, .resetvalue = 0, 4149 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, 4150 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, 4151 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, 4152 .access = PL3_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 4153 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, 4154 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, 4155 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, 4156 .access = PL3_RW, 4157 /* no .writefn needed as this can't cause an ASID change; 4158 * we must provide a .raw_writefn and .resetfn because we handle 4159 * reset and migration for the AArch32 TTBCR(S), which might be 4160 * using mask and base_mask. 4161 */ 4162 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write, 4163 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, 4164 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, 4165 .type = ARM_CP_ALIAS, 4166 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, 4167 .access = PL3_RW, 4168 .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, 4169 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, 4170 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, 4171 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, 4172 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, 4173 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, 4174 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, 4175 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, 4176 .type = ARM_CP_ALIAS, 4177 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, 4178 .access = PL3_RW, 4179 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) }, 4180 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, 4181 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, 4182 .access = PL3_RW, .writefn = vbar_write, 4183 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), 4184 .resetvalue = 0 }, 4185 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64, 4186 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2, 4187 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0, 4188 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) }, 4189 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64, 4190 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2, 4191 .access = PL3_RW, .resetvalue = 0, 4192 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) }, 4193 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64, 4194 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0, 4195 .access = PL3_RW, .type = ARM_CP_CONST, 4196 .resetvalue = 0 }, 4197 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH, 4198 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0, 4199 .access = PL3_RW, .type = ARM_CP_CONST, 4200 .resetvalue = 0 }, 4201 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH, 4202 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1, 4203 .access = PL3_RW, .type = ARM_CP_CONST, 4204 .resetvalue = 0 }, 4205 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64, 4206 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0, 4207 .access = PL3_W, .type = ARM_CP_NO_RAW, 4208 .writefn = tlbi_aa64_alle3is_write }, 4209 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64, 4210 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1, 4211 .access = PL3_W, .type = ARM_CP_NO_RAW, 4212 .writefn = tlbi_aa64_vae3is_write }, 4213 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64, 4214 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5, 4215 .access = PL3_W, .type = ARM_CP_NO_RAW, 4216 .writefn = tlbi_aa64_vae3is_write }, 4217 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64, 4218 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0, 4219 .access = PL3_W, .type = ARM_CP_NO_RAW, 4220 .writefn = tlbi_aa64_alle3_write }, 4221 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64, 4222 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1, 4223 .access = PL3_W, .type = ARM_CP_NO_RAW, 4224 .writefn = tlbi_aa64_vae3_write }, 4225 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64, 4226 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5, 4227 .access = PL3_W, .type = ARM_CP_NO_RAW, 4228 .writefn = tlbi_aa64_vae3_write }, 4229 REGINFO_SENTINEL 4230 }; 4231 4232 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 4233 bool isread) 4234 { 4235 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64, 4236 * but the AArch32 CTR has its own reginfo struct) 4237 */ 4238 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) { 4239 return CP_ACCESS_TRAP; 4240 } 4241 return CP_ACCESS_OK; 4242 } 4243 4244 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri, 4245 uint64_t value) 4246 { 4247 /* Writes to OSLAR_EL1 may update the OS lock status, which can be 4248 * read via a bit in OSLSR_EL1. 4249 */ 4250 int oslock; 4251 4252 if (ri->state == ARM_CP_STATE_AA32) { 4253 oslock = (value == 0xC5ACCE55); 4254 } else { 4255 oslock = value & 1; 4256 } 4257 4258 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock); 4259 } 4260 4261 static const ARMCPRegInfo debug_cp_reginfo[] = { 4262 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped 4263 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1; 4264 * unlike DBGDRAR it is never accessible from EL0. 4265 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64 4266 * accessor. 4267 */ 4268 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, 4269 .access = PL0_R, .accessfn = access_tdra, 4270 .type = ARM_CP_CONST, .resetvalue = 0 }, 4271 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64, 4272 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 4273 .access = PL1_R, .accessfn = access_tdra, 4274 .type = ARM_CP_CONST, .resetvalue = 0 }, 4275 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 4276 .access = PL0_R, .accessfn = access_tdra, 4277 .type = ARM_CP_CONST, .resetvalue = 0 }, 4278 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */ 4279 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH, 4280 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 4281 .access = PL1_RW, .accessfn = access_tda, 4282 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), 4283 .resetvalue = 0 }, 4284 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1. 4285 * We don't implement the configurable EL0 access. 4286 */ 4287 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH, 4288 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 4289 .type = ARM_CP_ALIAS, 4290 .access = PL1_R, .accessfn = access_tda, 4291 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), }, 4292 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH, 4293 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4, 4294 .access = PL1_W, .type = ARM_CP_NO_RAW, 4295 .accessfn = access_tdosa, 4296 .writefn = oslar_write }, 4297 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH, 4298 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4, 4299 .access = PL1_R, .resetvalue = 10, 4300 .accessfn = access_tdosa, 4301 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) }, 4302 /* Dummy OSDLR_EL1: 32-bit Linux will read this */ 4303 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH, 4304 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4, 4305 .access = PL1_RW, .accessfn = access_tdosa, 4306 .type = ARM_CP_NOP }, 4307 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't 4308 * implement vector catch debug events yet. 4309 */ 4310 { .name = "DBGVCR", 4311 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 4312 .access = PL1_RW, .accessfn = access_tda, 4313 .type = ARM_CP_NOP }, 4314 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor 4315 * to save and restore a 32-bit guest's DBGVCR) 4316 */ 4317 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64, 4318 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0, 4319 .access = PL2_RW, .accessfn = access_tda, 4320 .type = ARM_CP_NOP }, 4321 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications 4322 * Channel but Linux may try to access this register. The 32-bit 4323 * alias is DBGDCCINT. 4324 */ 4325 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH, 4326 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 4327 .access = PL1_RW, .accessfn = access_tda, 4328 .type = ARM_CP_NOP }, 4329 REGINFO_SENTINEL 4330 }; 4331 4332 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = { 4333 /* 64 bit access versions of the (dummy) debug registers */ 4334 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0, 4335 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 4336 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0, 4337 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 4338 REGINFO_SENTINEL 4339 }; 4340 4341 /* Return the exception level to which SVE-disabled exceptions should 4342 * be taken, or 0 if SVE is enabled. 4343 */ 4344 static int sve_exception_el(CPUARMState *env) 4345 { 4346 #ifndef CONFIG_USER_ONLY 4347 unsigned current_el = arm_current_el(env); 4348 4349 /* The CPACR.ZEN controls traps to EL1: 4350 * 0, 2 : trap EL0 and EL1 accesses 4351 * 1 : trap only EL0 accesses 4352 * 3 : trap no accesses 4353 */ 4354 switch (extract32(env->cp15.cpacr_el1, 16, 2)) { 4355 default: 4356 if (current_el <= 1) { 4357 /* Trap to PL1, which might be EL1 or EL3 */ 4358 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) { 4359 return 3; 4360 } 4361 return 1; 4362 } 4363 break; 4364 case 1: 4365 if (current_el == 0) { 4366 return 1; 4367 } 4368 break; 4369 case 3: 4370 break; 4371 } 4372 4373 /* Similarly for CPACR.FPEN, after having checked ZEN. */ 4374 switch (extract32(env->cp15.cpacr_el1, 20, 2)) { 4375 default: 4376 if (current_el <= 1) { 4377 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) { 4378 return 3; 4379 } 4380 return 1; 4381 } 4382 break; 4383 case 1: 4384 if (current_el == 0) { 4385 return 1; 4386 } 4387 break; 4388 case 3: 4389 break; 4390 } 4391 4392 /* CPTR_EL2. Check both TZ and TFP. */ 4393 if (current_el <= 2 4394 && (env->cp15.cptr_el[2] & (CPTR_TFP | CPTR_TZ)) 4395 && !arm_is_secure_below_el3(env)) { 4396 return 2; 4397 } 4398 4399 /* CPTR_EL3. Check both EZ and TFP. */ 4400 if (!(env->cp15.cptr_el[3] & CPTR_EZ) 4401 || (env->cp15.cptr_el[3] & CPTR_TFP)) { 4402 return 3; 4403 } 4404 #endif 4405 return 0; 4406 } 4407 4408 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4409 uint64_t value) 4410 { 4411 /* Bits other than [3:0] are RAZ/WI. */ 4412 raw_write(env, ri, value & 0xf); 4413 } 4414 4415 static const ARMCPRegInfo zcr_el1_reginfo = { 4416 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64, 4417 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0, 4418 .access = PL1_RW, .type = ARM_CP_SVE, 4419 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]), 4420 .writefn = zcr_write, .raw_writefn = raw_write 4421 }; 4422 4423 static const ARMCPRegInfo zcr_el2_reginfo = { 4424 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 4425 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 4426 .access = PL2_RW, .type = ARM_CP_SVE, 4427 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]), 4428 .writefn = zcr_write, .raw_writefn = raw_write 4429 }; 4430 4431 static const ARMCPRegInfo zcr_no_el2_reginfo = { 4432 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 4433 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 4434 .access = PL2_RW, .type = ARM_CP_SVE, 4435 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore 4436 }; 4437 4438 static const ARMCPRegInfo zcr_el3_reginfo = { 4439 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64, 4440 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0, 4441 .access = PL3_RW, .type = ARM_CP_SVE, 4442 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]), 4443 .writefn = zcr_write, .raw_writefn = raw_write 4444 }; 4445 4446 void hw_watchpoint_update(ARMCPU *cpu, int n) 4447 { 4448 CPUARMState *env = &cpu->env; 4449 vaddr len = 0; 4450 vaddr wvr = env->cp15.dbgwvr[n]; 4451 uint64_t wcr = env->cp15.dbgwcr[n]; 4452 int mask; 4453 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 4454 4455 if (env->cpu_watchpoint[n]) { 4456 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]); 4457 env->cpu_watchpoint[n] = NULL; 4458 } 4459 4460 if (!extract64(wcr, 0, 1)) { 4461 /* E bit clear : watchpoint disabled */ 4462 return; 4463 } 4464 4465 switch (extract64(wcr, 3, 2)) { 4466 case 0: 4467 /* LSC 00 is reserved and must behave as if the wp is disabled */ 4468 return; 4469 case 1: 4470 flags |= BP_MEM_READ; 4471 break; 4472 case 2: 4473 flags |= BP_MEM_WRITE; 4474 break; 4475 case 3: 4476 flags |= BP_MEM_ACCESS; 4477 break; 4478 } 4479 4480 /* Attempts to use both MASK and BAS fields simultaneously are 4481 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case, 4482 * thus generating a watchpoint for every byte in the masked region. 4483 */ 4484 mask = extract64(wcr, 24, 4); 4485 if (mask == 1 || mask == 2) { 4486 /* Reserved values of MASK; we must act as if the mask value was 4487 * some non-reserved value, or as if the watchpoint were disabled. 4488 * We choose the latter. 4489 */ 4490 return; 4491 } else if (mask) { 4492 /* Watchpoint covers an aligned area up to 2GB in size */ 4493 len = 1ULL << mask; 4494 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE 4495 * whether the watchpoint fires when the unmasked bits match; we opt 4496 * to generate the exceptions. 4497 */ 4498 wvr &= ~(len - 1); 4499 } else { 4500 /* Watchpoint covers bytes defined by the byte address select bits */ 4501 int bas = extract64(wcr, 5, 8); 4502 int basstart; 4503 4504 if (bas == 0) { 4505 /* This must act as if the watchpoint is disabled */ 4506 return; 4507 } 4508 4509 if (extract64(wvr, 2, 1)) { 4510 /* Deprecated case of an only 4-aligned address. BAS[7:4] are 4511 * ignored, and BAS[3:0] define which bytes to watch. 4512 */ 4513 bas &= 0xf; 4514 } 4515 /* The BAS bits are supposed to be programmed to indicate a contiguous 4516 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether 4517 * we fire for each byte in the word/doubleword addressed by the WVR. 4518 * We choose to ignore any non-zero bits after the first range of 1s. 4519 */ 4520 basstart = ctz32(bas); 4521 len = cto32(bas >> basstart); 4522 wvr += basstart; 4523 } 4524 4525 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags, 4526 &env->cpu_watchpoint[n]); 4527 } 4528 4529 void hw_watchpoint_update_all(ARMCPU *cpu) 4530 { 4531 int i; 4532 CPUARMState *env = &cpu->env; 4533 4534 /* Completely clear out existing QEMU watchpoints and our array, to 4535 * avoid possible stale entries following migration load. 4536 */ 4537 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU); 4538 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint)); 4539 4540 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) { 4541 hw_watchpoint_update(cpu, i); 4542 } 4543 } 4544 4545 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4546 uint64_t value) 4547 { 4548 ARMCPU *cpu = arm_env_get_cpu(env); 4549 int i = ri->crm; 4550 4551 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the 4552 * register reads and behaves as if values written are sign extended. 4553 * Bits [1:0] are RES0. 4554 */ 4555 value = sextract64(value, 0, 49) & ~3ULL; 4556 4557 raw_write(env, ri, value); 4558 hw_watchpoint_update(cpu, i); 4559 } 4560 4561 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4562 uint64_t value) 4563 { 4564 ARMCPU *cpu = arm_env_get_cpu(env); 4565 int i = ri->crm; 4566 4567 raw_write(env, ri, value); 4568 hw_watchpoint_update(cpu, i); 4569 } 4570 4571 void hw_breakpoint_update(ARMCPU *cpu, int n) 4572 { 4573 CPUARMState *env = &cpu->env; 4574 uint64_t bvr = env->cp15.dbgbvr[n]; 4575 uint64_t bcr = env->cp15.dbgbcr[n]; 4576 vaddr addr; 4577 int bt; 4578 int flags = BP_CPU; 4579 4580 if (env->cpu_breakpoint[n]) { 4581 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]); 4582 env->cpu_breakpoint[n] = NULL; 4583 } 4584 4585 if (!extract64(bcr, 0, 1)) { 4586 /* E bit clear : watchpoint disabled */ 4587 return; 4588 } 4589 4590 bt = extract64(bcr, 20, 4); 4591 4592 switch (bt) { 4593 case 4: /* unlinked address mismatch (reserved if AArch64) */ 4594 case 5: /* linked address mismatch (reserved if AArch64) */ 4595 qemu_log_mask(LOG_UNIMP, 4596 "arm: address mismatch breakpoint types not implemented\n"); 4597 return; 4598 case 0: /* unlinked address match */ 4599 case 1: /* linked address match */ 4600 { 4601 /* Bits [63:49] are hardwired to the value of bit [48]; that is, 4602 * we behave as if the register was sign extended. Bits [1:0] are 4603 * RES0. The BAS field is used to allow setting breakpoints on 16 4604 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether 4605 * a bp will fire if the addresses covered by the bp and the addresses 4606 * covered by the insn overlap but the insn doesn't start at the 4607 * start of the bp address range. We choose to require the insn and 4608 * the bp to have the same address. The constraints on writing to 4609 * BAS enforced in dbgbcr_write mean we have only four cases: 4610 * 0b0000 => no breakpoint 4611 * 0b0011 => breakpoint on addr 4612 * 0b1100 => breakpoint on addr + 2 4613 * 0b1111 => breakpoint on addr 4614 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c). 4615 */ 4616 int bas = extract64(bcr, 5, 4); 4617 addr = sextract64(bvr, 0, 49) & ~3ULL; 4618 if (bas == 0) { 4619 return; 4620 } 4621 if (bas == 0xc) { 4622 addr += 2; 4623 } 4624 break; 4625 } 4626 case 2: /* unlinked context ID match */ 4627 case 8: /* unlinked VMID match (reserved if no EL2) */ 4628 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */ 4629 qemu_log_mask(LOG_UNIMP, 4630 "arm: unlinked context breakpoint types not implemented\n"); 4631 return; 4632 case 9: /* linked VMID match (reserved if no EL2) */ 4633 case 11: /* linked context ID and VMID match (reserved if no EL2) */ 4634 case 3: /* linked context ID match */ 4635 default: 4636 /* We must generate no events for Linked context matches (unless 4637 * they are linked to by some other bp/wp, which is handled in 4638 * updates for the linking bp/wp). We choose to also generate no events 4639 * for reserved values. 4640 */ 4641 return; 4642 } 4643 4644 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]); 4645 } 4646 4647 void hw_breakpoint_update_all(ARMCPU *cpu) 4648 { 4649 int i; 4650 CPUARMState *env = &cpu->env; 4651 4652 /* Completely clear out existing QEMU breakpoints and our array, to 4653 * avoid possible stale entries following migration load. 4654 */ 4655 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU); 4656 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint)); 4657 4658 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) { 4659 hw_breakpoint_update(cpu, i); 4660 } 4661 } 4662 4663 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4664 uint64_t value) 4665 { 4666 ARMCPU *cpu = arm_env_get_cpu(env); 4667 int i = ri->crm; 4668 4669 raw_write(env, ri, value); 4670 hw_breakpoint_update(cpu, i); 4671 } 4672 4673 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4674 uint64_t value) 4675 { 4676 ARMCPU *cpu = arm_env_get_cpu(env); 4677 int i = ri->crm; 4678 4679 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only 4680 * copy of BAS[0]. 4681 */ 4682 value = deposit64(value, 6, 1, extract64(value, 5, 1)); 4683 value = deposit64(value, 8, 1, extract64(value, 7, 1)); 4684 4685 raw_write(env, ri, value); 4686 hw_breakpoint_update(cpu, i); 4687 } 4688 4689 static void define_debug_regs(ARMCPU *cpu) 4690 { 4691 /* Define v7 and v8 architectural debug registers. 4692 * These are just dummy implementations for now. 4693 */ 4694 int i; 4695 int wrps, brps, ctx_cmps; 4696 ARMCPRegInfo dbgdidr = { 4697 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 4698 .access = PL0_R, .accessfn = access_tda, 4699 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr, 4700 }; 4701 4702 /* Note that all these register fields hold "number of Xs minus 1". */ 4703 brps = extract32(cpu->dbgdidr, 24, 4); 4704 wrps = extract32(cpu->dbgdidr, 28, 4); 4705 ctx_cmps = extract32(cpu->dbgdidr, 20, 4); 4706 4707 assert(ctx_cmps <= brps); 4708 4709 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties 4710 * of the debug registers such as number of breakpoints; 4711 * check that if they both exist then they agree. 4712 */ 4713 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 4714 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps); 4715 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps); 4716 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps); 4717 } 4718 4719 define_one_arm_cp_reg(cpu, &dbgdidr); 4720 define_arm_cp_regs(cpu, debug_cp_reginfo); 4721 4722 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) { 4723 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo); 4724 } 4725 4726 for (i = 0; i < brps + 1; i++) { 4727 ARMCPRegInfo dbgregs[] = { 4728 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH, 4729 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4, 4730 .access = PL1_RW, .accessfn = access_tda, 4731 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]), 4732 .writefn = dbgbvr_write, .raw_writefn = raw_write 4733 }, 4734 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH, 4735 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5, 4736 .access = PL1_RW, .accessfn = access_tda, 4737 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]), 4738 .writefn = dbgbcr_write, .raw_writefn = raw_write 4739 }, 4740 REGINFO_SENTINEL 4741 }; 4742 define_arm_cp_regs(cpu, dbgregs); 4743 } 4744 4745 for (i = 0; i < wrps + 1; i++) { 4746 ARMCPRegInfo dbgregs[] = { 4747 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH, 4748 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6, 4749 .access = PL1_RW, .accessfn = access_tda, 4750 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]), 4751 .writefn = dbgwvr_write, .raw_writefn = raw_write 4752 }, 4753 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH, 4754 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7, 4755 .access = PL1_RW, .accessfn = access_tda, 4756 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]), 4757 .writefn = dbgwcr_write, .raw_writefn = raw_write 4758 }, 4759 REGINFO_SENTINEL 4760 }; 4761 define_arm_cp_regs(cpu, dbgregs); 4762 } 4763 } 4764 4765 /* We don't know until after realize whether there's a GICv3 4766 * attached, and that is what registers the gicv3 sysregs. 4767 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1 4768 * at runtime. 4769 */ 4770 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri) 4771 { 4772 ARMCPU *cpu = arm_env_get_cpu(env); 4773 uint64_t pfr1 = cpu->id_pfr1; 4774 4775 if (env->gicv3state) { 4776 pfr1 |= 1 << 28; 4777 } 4778 return pfr1; 4779 } 4780 4781 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri) 4782 { 4783 ARMCPU *cpu = arm_env_get_cpu(env); 4784 uint64_t pfr0 = cpu->id_aa64pfr0; 4785 4786 if (env->gicv3state) { 4787 pfr0 |= 1 << 24; 4788 } 4789 return pfr0; 4790 } 4791 4792 void register_cp_regs_for_features(ARMCPU *cpu) 4793 { 4794 /* Register all the coprocessor registers based on feature bits */ 4795 CPUARMState *env = &cpu->env; 4796 if (arm_feature(env, ARM_FEATURE_M)) { 4797 /* M profile has no coprocessor registers */ 4798 return; 4799 } 4800 4801 define_arm_cp_regs(cpu, cp_reginfo); 4802 if (!arm_feature(env, ARM_FEATURE_V8)) { 4803 /* Must go early as it is full of wildcards that may be 4804 * overridden by later definitions. 4805 */ 4806 define_arm_cp_regs(cpu, not_v8_cp_reginfo); 4807 } 4808 4809 if (arm_feature(env, ARM_FEATURE_V6)) { 4810 /* The ID registers all have impdef reset values */ 4811 ARMCPRegInfo v6_idregs[] = { 4812 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, 4813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 4814 .access = PL1_R, .type = ARM_CP_CONST, 4815 .resetvalue = cpu->id_pfr0 }, 4816 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know 4817 * the value of the GIC field until after we define these regs. 4818 */ 4819 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, 4820 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, 4821 .access = PL1_R, .type = ARM_CP_NO_RAW, 4822 .readfn = id_pfr1_read, 4823 .writefn = arm_cp_write_ignore }, 4824 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, 4825 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, 4826 .access = PL1_R, .type = ARM_CP_CONST, 4827 .resetvalue = cpu->id_dfr0 }, 4828 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, 4829 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, 4830 .access = PL1_R, .type = ARM_CP_CONST, 4831 .resetvalue = cpu->id_afr0 }, 4832 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, 4833 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, 4834 .access = PL1_R, .type = ARM_CP_CONST, 4835 .resetvalue = cpu->id_mmfr0 }, 4836 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, 4837 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, 4838 .access = PL1_R, .type = ARM_CP_CONST, 4839 .resetvalue = cpu->id_mmfr1 }, 4840 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, 4841 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, 4842 .access = PL1_R, .type = ARM_CP_CONST, 4843 .resetvalue = cpu->id_mmfr2 }, 4844 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, 4845 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, 4846 .access = PL1_R, .type = ARM_CP_CONST, 4847 .resetvalue = cpu->id_mmfr3 }, 4848 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, 4849 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 4850 .access = PL1_R, .type = ARM_CP_CONST, 4851 .resetvalue = cpu->id_isar0 }, 4852 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, 4853 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, 4854 .access = PL1_R, .type = ARM_CP_CONST, 4855 .resetvalue = cpu->id_isar1 }, 4856 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, 4857 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 4858 .access = PL1_R, .type = ARM_CP_CONST, 4859 .resetvalue = cpu->id_isar2 }, 4860 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, 4861 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, 4862 .access = PL1_R, .type = ARM_CP_CONST, 4863 .resetvalue = cpu->id_isar3 }, 4864 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, 4865 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, 4866 .access = PL1_R, .type = ARM_CP_CONST, 4867 .resetvalue = cpu->id_isar4 }, 4868 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, 4869 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, 4870 .access = PL1_R, .type = ARM_CP_CONST, 4871 .resetvalue = cpu->id_isar5 }, 4872 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH, 4873 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6, 4874 .access = PL1_R, .type = ARM_CP_CONST, 4875 .resetvalue = cpu->id_mmfr4 }, 4876 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH, 4877 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7, 4878 .access = PL1_R, .type = ARM_CP_CONST, 4879 .resetvalue = cpu->id_isar6 }, 4880 REGINFO_SENTINEL 4881 }; 4882 define_arm_cp_regs(cpu, v6_idregs); 4883 define_arm_cp_regs(cpu, v6_cp_reginfo); 4884 } else { 4885 define_arm_cp_regs(cpu, not_v6_cp_reginfo); 4886 } 4887 if (arm_feature(env, ARM_FEATURE_V6K)) { 4888 define_arm_cp_regs(cpu, v6k_cp_reginfo); 4889 } 4890 if (arm_feature(env, ARM_FEATURE_V7MP) && 4891 !arm_feature(env, ARM_FEATURE_PMSA)) { 4892 define_arm_cp_regs(cpu, v7mp_cp_reginfo); 4893 } 4894 if (arm_feature(env, ARM_FEATURE_V7)) { 4895 /* v7 performance monitor control register: same implementor 4896 * field as main ID register, and we implement only the cycle 4897 * count register. 4898 */ 4899 #ifndef CONFIG_USER_ONLY 4900 ARMCPRegInfo pmcr = { 4901 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, 4902 .access = PL0_RW, 4903 .type = ARM_CP_IO | ARM_CP_ALIAS, 4904 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), 4905 .accessfn = pmreg_access, .writefn = pmcr_write, 4906 .raw_writefn = raw_write, 4907 }; 4908 ARMCPRegInfo pmcr64 = { 4909 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, 4910 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, 4911 .access = PL0_RW, .accessfn = pmreg_access, 4912 .type = ARM_CP_IO, 4913 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), 4914 .resetvalue = cpu->midr & 0xff000000, 4915 .writefn = pmcr_write, .raw_writefn = raw_write, 4916 }; 4917 define_one_arm_cp_reg(cpu, &pmcr); 4918 define_one_arm_cp_reg(cpu, &pmcr64); 4919 #endif 4920 ARMCPRegInfo clidr = { 4921 .name = "CLIDR", .state = ARM_CP_STATE_BOTH, 4922 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, 4923 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr 4924 }; 4925 define_one_arm_cp_reg(cpu, &clidr); 4926 define_arm_cp_regs(cpu, v7_cp_reginfo); 4927 define_debug_regs(cpu); 4928 } else { 4929 define_arm_cp_regs(cpu, not_v7_cp_reginfo); 4930 } 4931 if (arm_feature(env, ARM_FEATURE_V8)) { 4932 /* AArch64 ID registers, which all have impdef reset values. 4933 * Note that within the ID register ranges the unused slots 4934 * must all RAZ, not UNDEF; future architecture versions may 4935 * define new registers here. 4936 */ 4937 ARMCPRegInfo v8_idregs[] = { 4938 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't 4939 * know the right value for the GIC field until after we 4940 * define these regs. 4941 */ 4942 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, 4943 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, 4944 .access = PL1_R, .type = ARM_CP_NO_RAW, 4945 .readfn = id_aa64pfr0_read, 4946 .writefn = arm_cp_write_ignore }, 4947 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, 4948 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, 4949 .access = PL1_R, .type = ARM_CP_CONST, 4950 .resetvalue = cpu->id_aa64pfr1}, 4951 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 4952 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2, 4953 .access = PL1_R, .type = ARM_CP_CONST, 4954 .resetvalue = 0 }, 4955 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 4956 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3, 4957 .access = PL1_R, .type = ARM_CP_CONST, 4958 .resetvalue = 0 }, 4959 { .name = "ID_AA64PFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 4960 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4, 4961 .access = PL1_R, .type = ARM_CP_CONST, 4962 .resetvalue = 0 }, 4963 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 4964 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5, 4965 .access = PL1_R, .type = ARM_CP_CONST, 4966 .resetvalue = 0 }, 4967 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 4968 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6, 4969 .access = PL1_R, .type = ARM_CP_CONST, 4970 .resetvalue = 0 }, 4971 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 4972 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7, 4973 .access = PL1_R, .type = ARM_CP_CONST, 4974 .resetvalue = 0 }, 4975 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, 4976 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, 4977 .access = PL1_R, .type = ARM_CP_CONST, 4978 .resetvalue = cpu->id_aa64dfr0 }, 4979 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, 4980 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, 4981 .access = PL1_R, .type = ARM_CP_CONST, 4982 .resetvalue = cpu->id_aa64dfr1 }, 4983 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 4984 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2, 4985 .access = PL1_R, .type = ARM_CP_CONST, 4986 .resetvalue = 0 }, 4987 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 4988 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3, 4989 .access = PL1_R, .type = ARM_CP_CONST, 4990 .resetvalue = 0 }, 4991 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, 4992 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, 4993 .access = PL1_R, .type = ARM_CP_CONST, 4994 .resetvalue = cpu->id_aa64afr0 }, 4995 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, 4996 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, 4997 .access = PL1_R, .type = ARM_CP_CONST, 4998 .resetvalue = cpu->id_aa64afr1 }, 4999 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5000 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6, 5001 .access = PL1_R, .type = ARM_CP_CONST, 5002 .resetvalue = 0 }, 5003 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7, 5005 .access = PL1_R, .type = ARM_CP_CONST, 5006 .resetvalue = 0 }, 5007 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, 5008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, 5009 .access = PL1_R, .type = ARM_CP_CONST, 5010 .resetvalue = cpu->id_aa64isar0 }, 5011 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, 5012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, 5013 .access = PL1_R, .type = ARM_CP_CONST, 5014 .resetvalue = cpu->id_aa64isar1 }, 5015 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5016 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2, 5017 .access = PL1_R, .type = ARM_CP_CONST, 5018 .resetvalue = 0 }, 5019 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5020 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3, 5021 .access = PL1_R, .type = ARM_CP_CONST, 5022 .resetvalue = 0 }, 5023 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5024 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4, 5025 .access = PL1_R, .type = ARM_CP_CONST, 5026 .resetvalue = 0 }, 5027 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5028 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5, 5029 .access = PL1_R, .type = ARM_CP_CONST, 5030 .resetvalue = 0 }, 5031 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5032 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6, 5033 .access = PL1_R, .type = ARM_CP_CONST, 5034 .resetvalue = 0 }, 5035 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5036 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7, 5037 .access = PL1_R, .type = ARM_CP_CONST, 5038 .resetvalue = 0 }, 5039 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, 5040 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 5041 .access = PL1_R, .type = ARM_CP_CONST, 5042 .resetvalue = cpu->id_aa64mmfr0 }, 5043 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, 5044 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, 5045 .access = PL1_R, .type = ARM_CP_CONST, 5046 .resetvalue = cpu->id_aa64mmfr1 }, 5047 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5048 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2, 5049 .access = PL1_R, .type = ARM_CP_CONST, 5050 .resetvalue = 0 }, 5051 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5052 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3, 5053 .access = PL1_R, .type = ARM_CP_CONST, 5054 .resetvalue = 0 }, 5055 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5056 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4, 5057 .access = PL1_R, .type = ARM_CP_CONST, 5058 .resetvalue = 0 }, 5059 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5060 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5, 5061 .access = PL1_R, .type = ARM_CP_CONST, 5062 .resetvalue = 0 }, 5063 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5064 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6, 5065 .access = PL1_R, .type = ARM_CP_CONST, 5066 .resetvalue = 0 }, 5067 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5068 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7, 5069 .access = PL1_R, .type = ARM_CP_CONST, 5070 .resetvalue = 0 }, 5071 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, 5072 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 5073 .access = PL1_R, .type = ARM_CP_CONST, 5074 .resetvalue = cpu->mvfr0 }, 5075 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, 5076 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 5077 .access = PL1_R, .type = ARM_CP_CONST, 5078 .resetvalue = cpu->mvfr1 }, 5079 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, 5080 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 5081 .access = PL1_R, .type = ARM_CP_CONST, 5082 .resetvalue = cpu->mvfr2 }, 5083 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5084 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3, 5085 .access = PL1_R, .type = ARM_CP_CONST, 5086 .resetvalue = 0 }, 5087 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5088 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4, 5089 .access = PL1_R, .type = ARM_CP_CONST, 5090 .resetvalue = 0 }, 5091 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5092 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5, 5093 .access = PL1_R, .type = ARM_CP_CONST, 5094 .resetvalue = 0 }, 5095 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5096 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6, 5097 .access = PL1_R, .type = ARM_CP_CONST, 5098 .resetvalue = 0 }, 5099 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5100 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7, 5101 .access = PL1_R, .type = ARM_CP_CONST, 5102 .resetvalue = 0 }, 5103 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32, 5104 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6, 5105 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5106 .resetvalue = cpu->pmceid0 }, 5107 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64, 5108 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6, 5109 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5110 .resetvalue = cpu->pmceid0 }, 5111 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32, 5112 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7, 5113 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5114 .resetvalue = cpu->pmceid1 }, 5115 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64, 5116 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7, 5117 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5118 .resetvalue = cpu->pmceid1 }, 5119 REGINFO_SENTINEL 5120 }; 5121 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */ 5122 if (!arm_feature(env, ARM_FEATURE_EL3) && 5123 !arm_feature(env, ARM_FEATURE_EL2)) { 5124 ARMCPRegInfo rvbar = { 5125 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64, 5126 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 5127 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar 5128 }; 5129 define_one_arm_cp_reg(cpu, &rvbar); 5130 } 5131 define_arm_cp_regs(cpu, v8_idregs); 5132 define_arm_cp_regs(cpu, v8_cp_reginfo); 5133 } 5134 if (arm_feature(env, ARM_FEATURE_EL2)) { 5135 uint64_t vmpidr_def = mpidr_read_val(env); 5136 ARMCPRegInfo vpidr_regs[] = { 5137 { .name = "VPIDR", .state = ARM_CP_STATE_AA32, 5138 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 5139 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5140 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS, 5141 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) }, 5142 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64, 5143 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 5144 .access = PL2_RW, .resetvalue = cpu->midr, 5145 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 5146 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32, 5147 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 5148 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5149 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS, 5150 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) }, 5151 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64, 5152 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 5153 .access = PL2_RW, 5154 .resetvalue = vmpidr_def, 5155 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, 5156 REGINFO_SENTINEL 5157 }; 5158 define_arm_cp_regs(cpu, vpidr_regs); 5159 define_arm_cp_regs(cpu, el2_cp_reginfo); 5160 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */ 5161 if (!arm_feature(env, ARM_FEATURE_EL3)) { 5162 ARMCPRegInfo rvbar = { 5163 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64, 5164 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1, 5165 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar 5166 }; 5167 define_one_arm_cp_reg(cpu, &rvbar); 5168 } 5169 } else { 5170 /* If EL2 is missing but higher ELs are enabled, we need to 5171 * register the no_el2 reginfos. 5172 */ 5173 if (arm_feature(env, ARM_FEATURE_EL3)) { 5174 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value 5175 * of MIDR_EL1 and MPIDR_EL1. 5176 */ 5177 ARMCPRegInfo vpidr_regs[] = { 5178 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH, 5179 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 5180 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 5181 .type = ARM_CP_CONST, .resetvalue = cpu->midr, 5182 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 5183 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH, 5184 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 5185 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 5186 .type = ARM_CP_NO_RAW, 5187 .writefn = arm_cp_write_ignore, .readfn = mpidr_read }, 5188 REGINFO_SENTINEL 5189 }; 5190 define_arm_cp_regs(cpu, vpidr_regs); 5191 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo); 5192 } 5193 } 5194 if (arm_feature(env, ARM_FEATURE_EL3)) { 5195 define_arm_cp_regs(cpu, el3_cp_reginfo); 5196 ARMCPRegInfo el3_regs[] = { 5197 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64, 5198 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1, 5199 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar }, 5200 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, 5201 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, 5202 .access = PL3_RW, 5203 .raw_writefn = raw_write, .writefn = sctlr_write, 5204 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]), 5205 .resetvalue = cpu->reset_sctlr }, 5206 REGINFO_SENTINEL 5207 }; 5208 5209 define_arm_cp_regs(cpu, el3_regs); 5210 } 5211 /* The behaviour of NSACR is sufficiently various that we don't 5212 * try to describe it in a single reginfo: 5213 * if EL3 is 64 bit, then trap to EL3 from S EL1, 5214 * reads as constant 0xc00 from NS EL1 and NS EL2 5215 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2 5216 * if v7 without EL3, register doesn't exist 5217 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2 5218 */ 5219 if (arm_feature(env, ARM_FEATURE_EL3)) { 5220 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5221 ARMCPRegInfo nsacr = { 5222 .name = "NSACR", .type = ARM_CP_CONST, 5223 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 5224 .access = PL1_RW, .accessfn = nsacr_access, 5225 .resetvalue = 0xc00 5226 }; 5227 define_one_arm_cp_reg(cpu, &nsacr); 5228 } else { 5229 ARMCPRegInfo nsacr = { 5230 .name = "NSACR", 5231 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 5232 .access = PL3_RW | PL1_R, 5233 .resetvalue = 0, 5234 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) 5235 }; 5236 define_one_arm_cp_reg(cpu, &nsacr); 5237 } 5238 } else { 5239 if (arm_feature(env, ARM_FEATURE_V8)) { 5240 ARMCPRegInfo nsacr = { 5241 .name = "NSACR", .type = ARM_CP_CONST, 5242 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 5243 .access = PL1_R, 5244 .resetvalue = 0xc00 5245 }; 5246 define_one_arm_cp_reg(cpu, &nsacr); 5247 } 5248 } 5249 5250 if (arm_feature(env, ARM_FEATURE_PMSA)) { 5251 if (arm_feature(env, ARM_FEATURE_V6)) { 5252 /* PMSAv6 not implemented */ 5253 assert(arm_feature(env, ARM_FEATURE_V7)); 5254 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 5255 define_arm_cp_regs(cpu, pmsav7_cp_reginfo); 5256 } else { 5257 define_arm_cp_regs(cpu, pmsav5_cp_reginfo); 5258 } 5259 } else { 5260 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 5261 define_arm_cp_regs(cpu, vmsa_cp_reginfo); 5262 } 5263 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { 5264 define_arm_cp_regs(cpu, t2ee_cp_reginfo); 5265 } 5266 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 5267 define_arm_cp_regs(cpu, generic_timer_cp_reginfo); 5268 } 5269 if (arm_feature(env, ARM_FEATURE_VAPA)) { 5270 define_arm_cp_regs(cpu, vapa_cp_reginfo); 5271 } 5272 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { 5273 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); 5274 } 5275 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { 5276 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); 5277 } 5278 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { 5279 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); 5280 } 5281 if (arm_feature(env, ARM_FEATURE_OMAPCP)) { 5282 define_arm_cp_regs(cpu, omap_cp_reginfo); 5283 } 5284 if (arm_feature(env, ARM_FEATURE_STRONGARM)) { 5285 define_arm_cp_regs(cpu, strongarm_cp_reginfo); 5286 } 5287 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 5288 define_arm_cp_regs(cpu, xscale_cp_reginfo); 5289 } 5290 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { 5291 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); 5292 } 5293 if (arm_feature(env, ARM_FEATURE_LPAE)) { 5294 define_arm_cp_regs(cpu, lpae_cp_reginfo); 5295 } 5296 /* Slightly awkwardly, the OMAP and StrongARM cores need all of 5297 * cp15 crn=0 to be writes-ignored, whereas for other cores they should 5298 * be read-only (ie write causes UNDEF exception). 5299 */ 5300 { 5301 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { 5302 /* Pre-v8 MIDR space. 5303 * Note that the MIDR isn't a simple constant register because 5304 * of the TI925 behaviour where writes to another register can 5305 * cause the MIDR value to change. 5306 * 5307 * Unimplemented registers in the c15 0 0 0 space default to 5308 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR 5309 * and friends override accordingly. 5310 */ 5311 { .name = "MIDR", 5312 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, 5313 .access = PL1_R, .resetvalue = cpu->midr, 5314 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, 5315 .readfn = midr_read, 5316 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 5317 .type = ARM_CP_OVERRIDE }, 5318 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ 5319 { .name = "DUMMY", 5320 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, 5321 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5322 { .name = "DUMMY", 5323 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, 5324 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5325 { .name = "DUMMY", 5326 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, 5327 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5328 { .name = "DUMMY", 5329 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, 5330 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5331 { .name = "DUMMY", 5332 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, 5333 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5334 REGINFO_SENTINEL 5335 }; 5336 ARMCPRegInfo id_v8_midr_cp_reginfo[] = { 5337 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, 5338 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, 5339 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr, 5340 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 5341 .readfn = midr_read }, 5342 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */ 5343 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 5344 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 5345 .access = PL1_R, .resetvalue = cpu->midr }, 5346 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 5347 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7, 5348 .access = PL1_R, .resetvalue = cpu->midr }, 5349 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, 5350 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, 5351 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr }, 5352 REGINFO_SENTINEL 5353 }; 5354 ARMCPRegInfo id_cp_reginfo[] = { 5355 /* These are common to v8 and pre-v8 */ 5356 { .name = "CTR", 5357 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, 5358 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 5359 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, 5360 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, 5361 .access = PL0_R, .accessfn = ctr_el0_access, 5362 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 5363 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ 5364 { .name = "TCMTR", 5365 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, 5366 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5367 REGINFO_SENTINEL 5368 }; 5369 /* TLBTR is specific to VMSA */ 5370 ARMCPRegInfo id_tlbtr_reginfo = { 5371 .name = "TLBTR", 5372 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, 5373 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0, 5374 }; 5375 /* MPUIR is specific to PMSA V6+ */ 5376 ARMCPRegInfo id_mpuir_reginfo = { 5377 .name = "MPUIR", 5378 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 5379 .access = PL1_R, .type = ARM_CP_CONST, 5380 .resetvalue = cpu->pmsav7_dregion << 8 5381 }; 5382 ARMCPRegInfo crn0_wi_reginfo = { 5383 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, 5384 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, 5385 .type = ARM_CP_NOP | ARM_CP_OVERRIDE 5386 }; 5387 if (arm_feature(env, ARM_FEATURE_OMAPCP) || 5388 arm_feature(env, ARM_FEATURE_STRONGARM)) { 5389 ARMCPRegInfo *r; 5390 /* Register the blanket "writes ignored" value first to cover the 5391 * whole space. Then update the specific ID registers to allow write 5392 * access, so that they ignore writes rather than causing them to 5393 * UNDEF. 5394 */ 5395 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); 5396 for (r = id_pre_v8_midr_cp_reginfo; 5397 r->type != ARM_CP_SENTINEL; r++) { 5398 r->access = PL1_RW; 5399 } 5400 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) { 5401 r->access = PL1_RW; 5402 } 5403 id_mpuir_reginfo.access = PL1_RW; 5404 id_tlbtr_reginfo.access = PL1_RW; 5405 } 5406 if (arm_feature(env, ARM_FEATURE_V8)) { 5407 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); 5408 } else { 5409 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); 5410 } 5411 define_arm_cp_regs(cpu, id_cp_reginfo); 5412 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 5413 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo); 5414 } else if (arm_feature(env, ARM_FEATURE_V7)) { 5415 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 5416 } 5417 } 5418 5419 if (arm_feature(env, ARM_FEATURE_MPIDR)) { 5420 define_arm_cp_regs(cpu, mpidr_cp_reginfo); 5421 } 5422 5423 if (arm_feature(env, ARM_FEATURE_AUXCR)) { 5424 ARMCPRegInfo auxcr_reginfo[] = { 5425 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, 5426 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, 5427 .access = PL1_RW, .type = ARM_CP_CONST, 5428 .resetvalue = cpu->reset_auxcr }, 5429 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH, 5430 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1, 5431 .access = PL2_RW, .type = ARM_CP_CONST, 5432 .resetvalue = 0 }, 5433 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64, 5434 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1, 5435 .access = PL3_RW, .type = ARM_CP_CONST, 5436 .resetvalue = 0 }, 5437 REGINFO_SENTINEL 5438 }; 5439 define_arm_cp_regs(cpu, auxcr_reginfo); 5440 } 5441 5442 if (arm_feature(env, ARM_FEATURE_CBAR)) { 5443 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5444 /* 32 bit view is [31:18] 0...0 [43:32]. */ 5445 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) 5446 | extract64(cpu->reset_cbar, 32, 12); 5447 ARMCPRegInfo cbar_reginfo[] = { 5448 { .name = "CBAR", 5449 .type = ARM_CP_CONST, 5450 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 5451 .access = PL1_R, .resetvalue = cpu->reset_cbar }, 5452 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, 5453 .type = ARM_CP_CONST, 5454 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, 5455 .access = PL1_R, .resetvalue = cbar32 }, 5456 REGINFO_SENTINEL 5457 }; 5458 /* We don't implement a r/w 64 bit CBAR currently */ 5459 assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); 5460 define_arm_cp_regs(cpu, cbar_reginfo); 5461 } else { 5462 ARMCPRegInfo cbar = { 5463 .name = "CBAR", 5464 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 5465 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar, 5466 .fieldoffset = offsetof(CPUARMState, 5467 cp15.c15_config_base_address) 5468 }; 5469 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 5470 cbar.access = PL1_R; 5471 cbar.fieldoffset = 0; 5472 cbar.type = ARM_CP_CONST; 5473 } 5474 define_one_arm_cp_reg(cpu, &cbar); 5475 } 5476 } 5477 5478 if (arm_feature(env, ARM_FEATURE_VBAR)) { 5479 ARMCPRegInfo vbar_cp_reginfo[] = { 5480 { .name = "VBAR", .state = ARM_CP_STATE_BOTH, 5481 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, 5482 .access = PL1_RW, .writefn = vbar_write, 5483 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s), 5484 offsetof(CPUARMState, cp15.vbar_ns) }, 5485 .resetvalue = 0 }, 5486 REGINFO_SENTINEL 5487 }; 5488 define_arm_cp_regs(cpu, vbar_cp_reginfo); 5489 } 5490 5491 /* Generic registers whose values depend on the implementation */ 5492 { 5493 ARMCPRegInfo sctlr = { 5494 .name = "SCTLR", .state = ARM_CP_STATE_BOTH, 5495 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 5496 .access = PL1_RW, 5497 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), 5498 offsetof(CPUARMState, cp15.sctlr_ns) }, 5499 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, 5500 .raw_writefn = raw_write, 5501 }; 5502 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 5503 /* Normally we would always end the TB on an SCTLR write, but Linux 5504 * arch/arm/mach-pxa/sleep.S expects two instructions following 5505 * an MMU enable to execute from cache. Imitate this behaviour. 5506 */ 5507 sctlr.type |= ARM_CP_SUPPRESS_TB_END; 5508 } 5509 define_one_arm_cp_reg(cpu, &sctlr); 5510 } 5511 5512 if (arm_feature(env, ARM_FEATURE_SVE)) { 5513 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo); 5514 if (arm_feature(env, ARM_FEATURE_EL2)) { 5515 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo); 5516 } else { 5517 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo); 5518 } 5519 if (arm_feature(env, ARM_FEATURE_EL3)) { 5520 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo); 5521 } 5522 } 5523 } 5524 5525 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) 5526 { 5527 CPUState *cs = CPU(cpu); 5528 CPUARMState *env = &cpu->env; 5529 5530 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5531 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg, 5532 aarch64_fpu_gdb_set_reg, 5533 34, "aarch64-fpu.xml", 0); 5534 } else if (arm_feature(env, ARM_FEATURE_NEON)) { 5535 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 5536 51, "arm-neon.xml", 0); 5537 } else if (arm_feature(env, ARM_FEATURE_VFP3)) { 5538 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 5539 35, "arm-vfp3.xml", 0); 5540 } else if (arm_feature(env, ARM_FEATURE_VFP)) { 5541 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 5542 19, "arm-vfp.xml", 0); 5543 } 5544 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg, 5545 arm_gen_dynamic_xml(cs), 5546 "system-registers.xml", 0); 5547 } 5548 5549 /* Sort alphabetically by type name, except for "any". */ 5550 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) 5551 { 5552 ObjectClass *class_a = (ObjectClass *)a; 5553 ObjectClass *class_b = (ObjectClass *)b; 5554 const char *name_a, *name_b; 5555 5556 name_a = object_class_get_name(class_a); 5557 name_b = object_class_get_name(class_b); 5558 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) { 5559 return 1; 5560 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) { 5561 return -1; 5562 } else { 5563 return strcmp(name_a, name_b); 5564 } 5565 } 5566 5567 static void arm_cpu_list_entry(gpointer data, gpointer user_data) 5568 { 5569 ObjectClass *oc = data; 5570 CPUListState *s = user_data; 5571 const char *typename; 5572 char *name; 5573 5574 typename = object_class_get_name(oc); 5575 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU)); 5576 (*s->cpu_fprintf)(s->file, " %s\n", 5577 name); 5578 g_free(name); 5579 } 5580 5581 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf) 5582 { 5583 CPUListState s = { 5584 .file = f, 5585 .cpu_fprintf = cpu_fprintf, 5586 }; 5587 GSList *list; 5588 5589 list = object_class_get_list(TYPE_ARM_CPU, false); 5590 list = g_slist_sort(list, arm_cpu_list_compare); 5591 (*cpu_fprintf)(f, "Available CPUs:\n"); 5592 g_slist_foreach(list, arm_cpu_list_entry, &s); 5593 g_slist_free(list); 5594 } 5595 5596 static void arm_cpu_add_definition(gpointer data, gpointer user_data) 5597 { 5598 ObjectClass *oc = data; 5599 CpuDefinitionInfoList **cpu_list = user_data; 5600 CpuDefinitionInfoList *entry; 5601 CpuDefinitionInfo *info; 5602 const char *typename; 5603 5604 typename = object_class_get_name(oc); 5605 info = g_malloc0(sizeof(*info)); 5606 info->name = g_strndup(typename, 5607 strlen(typename) - strlen("-" TYPE_ARM_CPU)); 5608 info->q_typename = g_strdup(typename); 5609 5610 entry = g_malloc0(sizeof(*entry)); 5611 entry->value = info; 5612 entry->next = *cpu_list; 5613 *cpu_list = entry; 5614 } 5615 5616 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp) 5617 { 5618 CpuDefinitionInfoList *cpu_list = NULL; 5619 GSList *list; 5620 5621 list = object_class_get_list(TYPE_ARM_CPU, false); 5622 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list); 5623 g_slist_free(list); 5624 5625 return cpu_list; 5626 } 5627 5628 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, 5629 void *opaque, int state, int secstate, 5630 int crm, int opc1, int opc2, 5631 const char *name) 5632 { 5633 /* Private utility function for define_one_arm_cp_reg_with_opaque(): 5634 * add a single reginfo struct to the hash table. 5635 */ 5636 uint32_t *key = g_new(uint32_t, 1); 5637 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); 5638 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; 5639 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0; 5640 5641 r2->name = g_strdup(name); 5642 /* Reset the secure state to the specific incoming state. This is 5643 * necessary as the register may have been defined with both states. 5644 */ 5645 r2->secure = secstate; 5646 5647 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 5648 /* Register is banked (using both entries in array). 5649 * Overwriting fieldoffset as the array is only used to define 5650 * banked registers but later only fieldoffset is used. 5651 */ 5652 r2->fieldoffset = r->bank_fieldoffsets[ns]; 5653 } 5654 5655 if (state == ARM_CP_STATE_AA32) { 5656 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 5657 /* If the register is banked then we don't need to migrate or 5658 * reset the 32-bit instance in certain cases: 5659 * 5660 * 1) If the register has both 32-bit and 64-bit instances then we 5661 * can count on the 64-bit instance taking care of the 5662 * non-secure bank. 5663 * 2) If ARMv8 is enabled then we can count on a 64-bit version 5664 * taking care of the secure bank. This requires that separate 5665 * 32 and 64-bit definitions are provided. 5666 */ 5667 if ((r->state == ARM_CP_STATE_BOTH && ns) || 5668 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) { 5669 r2->type |= ARM_CP_ALIAS; 5670 } 5671 } else if ((secstate != r->secure) && !ns) { 5672 /* The register is not banked so we only want to allow migration of 5673 * the non-secure instance. 5674 */ 5675 r2->type |= ARM_CP_ALIAS; 5676 } 5677 5678 if (r->state == ARM_CP_STATE_BOTH) { 5679 /* We assume it is a cp15 register if the .cp field is left unset. 5680 */ 5681 if (r2->cp == 0) { 5682 r2->cp = 15; 5683 } 5684 5685 #ifdef HOST_WORDS_BIGENDIAN 5686 if (r2->fieldoffset) { 5687 r2->fieldoffset += sizeof(uint32_t); 5688 } 5689 #endif 5690 } 5691 } 5692 if (state == ARM_CP_STATE_AA64) { 5693 /* To allow abbreviation of ARMCPRegInfo 5694 * definitions, we treat cp == 0 as equivalent to 5695 * the value for "standard guest-visible sysreg". 5696 * STATE_BOTH definitions are also always "standard 5697 * sysreg" in their AArch64 view (the .cp value may 5698 * be non-zero for the benefit of the AArch32 view). 5699 */ 5700 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) { 5701 r2->cp = CP_REG_ARM64_SYSREG_CP; 5702 } 5703 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm, 5704 r2->opc0, opc1, opc2); 5705 } else { 5706 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2); 5707 } 5708 if (opaque) { 5709 r2->opaque = opaque; 5710 } 5711 /* reginfo passed to helpers is correct for the actual access, 5712 * and is never ARM_CP_STATE_BOTH: 5713 */ 5714 r2->state = state; 5715 /* Make sure reginfo passed to helpers for wildcarded regs 5716 * has the correct crm/opc1/opc2 for this reg, not CP_ANY: 5717 */ 5718 r2->crm = crm; 5719 r2->opc1 = opc1; 5720 r2->opc2 = opc2; 5721 /* By convention, for wildcarded registers only the first 5722 * entry is used for migration; the others are marked as 5723 * ALIAS so we don't try to transfer the register 5724 * multiple times. Special registers (ie NOP/WFI) are 5725 * never migratable and not even raw-accessible. 5726 */ 5727 if ((r->type & ARM_CP_SPECIAL)) { 5728 r2->type |= ARM_CP_NO_RAW; 5729 } 5730 if (((r->crm == CP_ANY) && crm != 0) || 5731 ((r->opc1 == CP_ANY) && opc1 != 0) || 5732 ((r->opc2 == CP_ANY) && opc2 != 0)) { 5733 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB; 5734 } 5735 5736 /* Check that raw accesses are either forbidden or handled. Note that 5737 * we can't assert this earlier because the setup of fieldoffset for 5738 * banked registers has to be done first. 5739 */ 5740 if (!(r2->type & ARM_CP_NO_RAW)) { 5741 assert(!raw_accessors_invalid(r2)); 5742 } 5743 5744 /* Overriding of an existing definition must be explicitly 5745 * requested. 5746 */ 5747 if (!(r->type & ARM_CP_OVERRIDE)) { 5748 ARMCPRegInfo *oldreg; 5749 oldreg = g_hash_table_lookup(cpu->cp_regs, key); 5750 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { 5751 fprintf(stderr, "Register redefined: cp=%d %d bit " 5752 "crn=%d crm=%d opc1=%d opc2=%d, " 5753 "was %s, now %s\n", r2->cp, 32 + 32 * is64, 5754 r2->crn, r2->crm, r2->opc1, r2->opc2, 5755 oldreg->name, r2->name); 5756 g_assert_not_reached(); 5757 } 5758 } 5759 g_hash_table_insert(cpu->cp_regs, key, r2); 5760 } 5761 5762 5763 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, 5764 const ARMCPRegInfo *r, void *opaque) 5765 { 5766 /* Define implementations of coprocessor registers. 5767 * We store these in a hashtable because typically 5768 * there are less than 150 registers in a space which 5769 * is 16*16*16*8*8 = 262144 in size. 5770 * Wildcarding is supported for the crm, opc1 and opc2 fields. 5771 * If a register is defined twice then the second definition is 5772 * used, so this can be used to define some generic registers and 5773 * then override them with implementation specific variations. 5774 * At least one of the original and the second definition should 5775 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard 5776 * against accidental use. 5777 * 5778 * The state field defines whether the register is to be 5779 * visible in the AArch32 or AArch64 execution state. If the 5780 * state is set to ARM_CP_STATE_BOTH then we synthesise a 5781 * reginfo structure for the AArch32 view, which sees the lower 5782 * 32 bits of the 64 bit register. 5783 * 5784 * Only registers visible in AArch64 may set r->opc0; opc0 cannot 5785 * be wildcarded. AArch64 registers are always considered to be 64 5786 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of 5787 * the register, if any. 5788 */ 5789 int crm, opc1, opc2, state; 5790 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; 5791 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; 5792 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; 5793 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; 5794 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; 5795 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; 5796 /* 64 bit registers have only CRm and Opc1 fields */ 5797 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); 5798 /* op0 only exists in the AArch64 encodings */ 5799 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); 5800 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ 5801 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); 5802 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1 5803 * encodes a minimum access level for the register. We roll this 5804 * runtime check into our general permission check code, so check 5805 * here that the reginfo's specified permissions are strict enough 5806 * to encompass the generic architectural permission check. 5807 */ 5808 if (r->state != ARM_CP_STATE_AA32) { 5809 int mask = 0; 5810 switch (r->opc1) { 5811 case 0: case 1: case 2: 5812 /* min_EL EL1 */ 5813 mask = PL1_RW; 5814 break; 5815 case 3: 5816 /* min_EL EL0 */ 5817 mask = PL0_RW; 5818 break; 5819 case 4: 5820 /* min_EL EL2 */ 5821 mask = PL2_RW; 5822 break; 5823 case 5: 5824 /* unallocated encoding, so not possible */ 5825 assert(false); 5826 break; 5827 case 6: 5828 /* min_EL EL3 */ 5829 mask = PL3_RW; 5830 break; 5831 case 7: 5832 /* min_EL EL1, secure mode only (we don't check the latter) */ 5833 mask = PL1_RW; 5834 break; 5835 default: 5836 /* broken reginfo with out-of-range opc1 */ 5837 assert(false); 5838 break; 5839 } 5840 /* assert our permissions are not too lax (stricter is fine) */ 5841 assert((r->access & ~mask) == 0); 5842 } 5843 5844 /* Check that the register definition has enough info to handle 5845 * reads and writes if they are permitted. 5846 */ 5847 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { 5848 if (r->access & PL3_R) { 5849 assert((r->fieldoffset || 5850 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 5851 r->readfn); 5852 } 5853 if (r->access & PL3_W) { 5854 assert((r->fieldoffset || 5855 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 5856 r->writefn); 5857 } 5858 } 5859 /* Bad type field probably means missing sentinel at end of reg list */ 5860 assert(cptype_valid(r->type)); 5861 for (crm = crmmin; crm <= crmmax; crm++) { 5862 for (opc1 = opc1min; opc1 <= opc1max; opc1++) { 5863 for (opc2 = opc2min; opc2 <= opc2max; opc2++) { 5864 for (state = ARM_CP_STATE_AA32; 5865 state <= ARM_CP_STATE_AA64; state++) { 5866 if (r->state != state && r->state != ARM_CP_STATE_BOTH) { 5867 continue; 5868 } 5869 if (state == ARM_CP_STATE_AA32) { 5870 /* Under AArch32 CP registers can be common 5871 * (same for secure and non-secure world) or banked. 5872 */ 5873 char *name; 5874 5875 switch (r->secure) { 5876 case ARM_CP_SECSTATE_S: 5877 case ARM_CP_SECSTATE_NS: 5878 add_cpreg_to_hashtable(cpu, r, opaque, state, 5879 r->secure, crm, opc1, opc2, 5880 r->name); 5881 break; 5882 default: 5883 name = g_strdup_printf("%s_S", r->name); 5884 add_cpreg_to_hashtable(cpu, r, opaque, state, 5885 ARM_CP_SECSTATE_S, 5886 crm, opc1, opc2, name); 5887 g_free(name); 5888 add_cpreg_to_hashtable(cpu, r, opaque, state, 5889 ARM_CP_SECSTATE_NS, 5890 crm, opc1, opc2, r->name); 5891 break; 5892 } 5893 } else { 5894 /* AArch64 registers get mapped to non-secure instance 5895 * of AArch32 */ 5896 add_cpreg_to_hashtable(cpu, r, opaque, state, 5897 ARM_CP_SECSTATE_NS, 5898 crm, opc1, opc2, r->name); 5899 } 5900 } 5901 } 5902 } 5903 } 5904 } 5905 5906 void define_arm_cp_regs_with_opaque(ARMCPU *cpu, 5907 const ARMCPRegInfo *regs, void *opaque) 5908 { 5909 /* Define a whole list of registers */ 5910 const ARMCPRegInfo *r; 5911 for (r = regs; r->type != ARM_CP_SENTINEL; r++) { 5912 define_one_arm_cp_reg_with_opaque(cpu, r, opaque); 5913 } 5914 } 5915 5916 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) 5917 { 5918 return g_hash_table_lookup(cpregs, &encoded_cp); 5919 } 5920 5921 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, 5922 uint64_t value) 5923 { 5924 /* Helper coprocessor write function for write-ignore registers */ 5925 } 5926 5927 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) 5928 { 5929 /* Helper coprocessor write function for read-as-zero registers */ 5930 return 0; 5931 } 5932 5933 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) 5934 { 5935 /* Helper coprocessor reset function for do-nothing-on-reset registers */ 5936 } 5937 5938 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type) 5939 { 5940 /* Return true if it is not valid for us to switch to 5941 * this CPU mode (ie all the UNPREDICTABLE cases in 5942 * the ARM ARM CPSRWriteByInstr pseudocode). 5943 */ 5944 5945 /* Changes to or from Hyp via MSR and CPS are illegal. */ 5946 if (write_type == CPSRWriteByInstr && 5947 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP || 5948 mode == ARM_CPU_MODE_HYP)) { 5949 return 1; 5950 } 5951 5952 switch (mode) { 5953 case ARM_CPU_MODE_USR: 5954 return 0; 5955 case ARM_CPU_MODE_SYS: 5956 case ARM_CPU_MODE_SVC: 5957 case ARM_CPU_MODE_ABT: 5958 case ARM_CPU_MODE_UND: 5959 case ARM_CPU_MODE_IRQ: 5960 case ARM_CPU_MODE_FIQ: 5961 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7 5962 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.) 5963 */ 5964 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR 5965 * and CPS are treated as illegal mode changes. 5966 */ 5967 if (write_type == CPSRWriteByInstr && 5968 (env->cp15.hcr_el2 & HCR_TGE) && 5969 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON && 5970 !arm_is_secure_below_el3(env)) { 5971 return 1; 5972 } 5973 return 0; 5974 case ARM_CPU_MODE_HYP: 5975 return !arm_feature(env, ARM_FEATURE_EL2) 5976 || arm_current_el(env) < 2 || arm_is_secure(env); 5977 case ARM_CPU_MODE_MON: 5978 return arm_current_el(env) < 3; 5979 default: 5980 return 1; 5981 } 5982 } 5983 5984 uint32_t cpsr_read(CPUARMState *env) 5985 { 5986 int ZF; 5987 ZF = (env->ZF == 0); 5988 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | 5989 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) 5990 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) 5991 | ((env->condexec_bits & 0xfc) << 8) 5992 | (env->GE << 16) | (env->daif & CPSR_AIF); 5993 } 5994 5995 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask, 5996 CPSRWriteType write_type) 5997 { 5998 uint32_t changed_daif; 5999 6000 if (mask & CPSR_NZCV) { 6001 env->ZF = (~val) & CPSR_Z; 6002 env->NF = val; 6003 env->CF = (val >> 29) & 1; 6004 env->VF = (val << 3) & 0x80000000; 6005 } 6006 if (mask & CPSR_Q) 6007 env->QF = ((val & CPSR_Q) != 0); 6008 if (mask & CPSR_T) 6009 env->thumb = ((val & CPSR_T) != 0); 6010 if (mask & CPSR_IT_0_1) { 6011 env->condexec_bits &= ~3; 6012 env->condexec_bits |= (val >> 25) & 3; 6013 } 6014 if (mask & CPSR_IT_2_7) { 6015 env->condexec_bits &= 3; 6016 env->condexec_bits |= (val >> 8) & 0xfc; 6017 } 6018 if (mask & CPSR_GE) { 6019 env->GE = (val >> 16) & 0xf; 6020 } 6021 6022 /* In a V7 implementation that includes the security extensions but does 6023 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control 6024 * whether non-secure software is allowed to change the CPSR_F and CPSR_A 6025 * bits respectively. 6026 * 6027 * In a V8 implementation, it is permitted for privileged software to 6028 * change the CPSR A/F bits regardless of the SCR.AW/FW bits. 6029 */ 6030 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) && 6031 arm_feature(env, ARM_FEATURE_EL3) && 6032 !arm_feature(env, ARM_FEATURE_EL2) && 6033 !arm_is_secure(env)) { 6034 6035 changed_daif = (env->daif ^ val) & mask; 6036 6037 if (changed_daif & CPSR_A) { 6038 /* Check to see if we are allowed to change the masking of async 6039 * abort exceptions from a non-secure state. 6040 */ 6041 if (!(env->cp15.scr_el3 & SCR_AW)) { 6042 qemu_log_mask(LOG_GUEST_ERROR, 6043 "Ignoring attempt to switch CPSR_A flag from " 6044 "non-secure world with SCR.AW bit clear\n"); 6045 mask &= ~CPSR_A; 6046 } 6047 } 6048 6049 if (changed_daif & CPSR_F) { 6050 /* Check to see if we are allowed to change the masking of FIQ 6051 * exceptions from a non-secure state. 6052 */ 6053 if (!(env->cp15.scr_el3 & SCR_FW)) { 6054 qemu_log_mask(LOG_GUEST_ERROR, 6055 "Ignoring attempt to switch CPSR_F flag from " 6056 "non-secure world with SCR.FW bit clear\n"); 6057 mask &= ~CPSR_F; 6058 } 6059 6060 /* Check whether non-maskable FIQ (NMFI) support is enabled. 6061 * If this bit is set software is not allowed to mask 6062 * FIQs, but is allowed to set CPSR_F to 0. 6063 */ 6064 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && 6065 (val & CPSR_F)) { 6066 qemu_log_mask(LOG_GUEST_ERROR, 6067 "Ignoring attempt to enable CPSR_F flag " 6068 "(non-maskable FIQ [NMFI] support enabled)\n"); 6069 mask &= ~CPSR_F; 6070 } 6071 } 6072 } 6073 6074 env->daif &= ~(CPSR_AIF & mask); 6075 env->daif |= val & CPSR_AIF & mask; 6076 6077 if (write_type != CPSRWriteRaw && 6078 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) { 6079 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) { 6080 /* Note that we can only get here in USR mode if this is a 6081 * gdb stub write; for this case we follow the architectural 6082 * behaviour for guest writes in USR mode of ignoring an attempt 6083 * to switch mode. (Those are caught by translate.c for writes 6084 * triggered by guest instructions.) 6085 */ 6086 mask &= ~CPSR_M; 6087 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) { 6088 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in 6089 * v7, and has defined behaviour in v8: 6090 * + leave CPSR.M untouched 6091 * + allow changes to the other CPSR fields 6092 * + set PSTATE.IL 6093 * For user changes via the GDB stub, we don't set PSTATE.IL, 6094 * as this would be unnecessarily harsh for a user error. 6095 */ 6096 mask &= ~CPSR_M; 6097 if (write_type != CPSRWriteByGDBStub && 6098 arm_feature(env, ARM_FEATURE_V8)) { 6099 mask |= CPSR_IL; 6100 val |= CPSR_IL; 6101 } 6102 } else { 6103 switch_mode(env, val & CPSR_M); 6104 } 6105 } 6106 mask &= ~CACHED_CPSR_BITS; 6107 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); 6108 } 6109 6110 /* Sign/zero extend */ 6111 uint32_t HELPER(sxtb16)(uint32_t x) 6112 { 6113 uint32_t res; 6114 res = (uint16_t)(int8_t)x; 6115 res |= (uint32_t)(int8_t)(x >> 16) << 16; 6116 return res; 6117 } 6118 6119 uint32_t HELPER(uxtb16)(uint32_t x) 6120 { 6121 uint32_t res; 6122 res = (uint16_t)(uint8_t)x; 6123 res |= (uint32_t)(uint8_t)(x >> 16) << 16; 6124 return res; 6125 } 6126 6127 int32_t HELPER(sdiv)(int32_t num, int32_t den) 6128 { 6129 if (den == 0) 6130 return 0; 6131 if (num == INT_MIN && den == -1) 6132 return INT_MIN; 6133 return num / den; 6134 } 6135 6136 uint32_t HELPER(udiv)(uint32_t num, uint32_t den) 6137 { 6138 if (den == 0) 6139 return 0; 6140 return num / den; 6141 } 6142 6143 uint32_t HELPER(rbit)(uint32_t x) 6144 { 6145 return revbit32(x); 6146 } 6147 6148 #if defined(CONFIG_USER_ONLY) 6149 6150 /* These should probably raise undefined insn exceptions. */ 6151 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) 6152 { 6153 ARMCPU *cpu = arm_env_get_cpu(env); 6154 6155 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg); 6156 } 6157 6158 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) 6159 { 6160 ARMCPU *cpu = arm_env_get_cpu(env); 6161 6162 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg); 6163 return 0; 6164 } 6165 6166 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) 6167 { 6168 /* translate.c should never generate calls here in user-only mode */ 6169 g_assert_not_reached(); 6170 } 6171 6172 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) 6173 { 6174 /* translate.c should never generate calls here in user-only mode */ 6175 g_assert_not_reached(); 6176 } 6177 6178 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) 6179 { 6180 /* The TT instructions can be used by unprivileged code, but in 6181 * user-only emulation we don't have the MPU. 6182 * Luckily since we know we are NonSecure unprivileged (and that in 6183 * turn means that the A flag wasn't specified), all the bits in the 6184 * register must be zero: 6185 * IREGION: 0 because IRVALID is 0 6186 * IRVALID: 0 because NS 6187 * S: 0 because NS 6188 * NSRW: 0 because NS 6189 * NSR: 0 because NS 6190 * RW: 0 because unpriv and A flag not set 6191 * R: 0 because unpriv and A flag not set 6192 * SRVALID: 0 because NS 6193 * MRVALID: 0 because unpriv and A flag not set 6194 * SREGION: 0 becaus SRVALID is 0 6195 * MREGION: 0 because MRVALID is 0 6196 */ 6197 return 0; 6198 } 6199 6200 void switch_mode(CPUARMState *env, int mode) 6201 { 6202 ARMCPU *cpu = arm_env_get_cpu(env); 6203 6204 if (mode != ARM_CPU_MODE_USR) { 6205 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); 6206 } 6207 } 6208 6209 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 6210 uint32_t cur_el, bool secure) 6211 { 6212 return 1; 6213 } 6214 6215 void aarch64_sync_64_to_32(CPUARMState *env) 6216 { 6217 g_assert_not_reached(); 6218 } 6219 6220 #else 6221 6222 void switch_mode(CPUARMState *env, int mode) 6223 { 6224 int old_mode; 6225 int i; 6226 6227 old_mode = env->uncached_cpsr & CPSR_M; 6228 if (mode == old_mode) 6229 return; 6230 6231 if (old_mode == ARM_CPU_MODE_FIQ) { 6232 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); 6233 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); 6234 } else if (mode == ARM_CPU_MODE_FIQ) { 6235 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); 6236 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); 6237 } 6238 6239 i = bank_number(old_mode); 6240 env->banked_r13[i] = env->regs[13]; 6241 env->banked_r14[i] = env->regs[14]; 6242 env->banked_spsr[i] = env->spsr; 6243 6244 i = bank_number(mode); 6245 env->regs[13] = env->banked_r13[i]; 6246 env->regs[14] = env->banked_r14[i]; 6247 env->spsr = env->banked_spsr[i]; 6248 } 6249 6250 /* Physical Interrupt Target EL Lookup Table 6251 * 6252 * [ From ARM ARM section G1.13.4 (Table G1-15) ] 6253 * 6254 * The below multi-dimensional table is used for looking up the target 6255 * exception level given numerous condition criteria. Specifically, the 6256 * target EL is based on SCR and HCR routing controls as well as the 6257 * currently executing EL and secure state. 6258 * 6259 * Dimensions: 6260 * target_el_table[2][2][2][2][2][4] 6261 * | | | | | +--- Current EL 6262 * | | | | +------ Non-secure(0)/Secure(1) 6263 * | | | +--------- HCR mask override 6264 * | | +------------ SCR exec state control 6265 * | +--------------- SCR mask override 6266 * +------------------ 32-bit(0)/64-bit(1) EL3 6267 * 6268 * The table values are as such: 6269 * 0-3 = EL0-EL3 6270 * -1 = Cannot occur 6271 * 6272 * The ARM ARM target EL table includes entries indicating that an "exception 6273 * is not taken". The two cases where this is applicable are: 6274 * 1) An exception is taken from EL3 but the SCR does not have the exception 6275 * routed to EL3. 6276 * 2) An exception is taken from EL2 but the HCR does not have the exception 6277 * routed to EL2. 6278 * In these two cases, the below table contain a target of EL1. This value is 6279 * returned as it is expected that the consumer of the table data will check 6280 * for "target EL >= current EL" to ensure the exception is not taken. 6281 * 6282 * SCR HCR 6283 * 64 EA AMO From 6284 * BIT IRQ IMO Non-secure Secure 6285 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 6286 */ 6287 static const int8_t target_el_table[2][2][2][2][2][4] = { 6288 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 6289 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, 6290 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 6291 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, 6292 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 6293 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, 6294 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 6295 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, 6296 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, 6297 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},}, 6298 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },}, 6299 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},}, 6300 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 6301 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, 6302 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 6303 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},}, 6304 }; 6305 6306 /* 6307 * Determine the target EL for physical exceptions 6308 */ 6309 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 6310 uint32_t cur_el, bool secure) 6311 { 6312 CPUARMState *env = cs->env_ptr; 6313 int rw; 6314 int scr; 6315 int hcr; 6316 int target_el; 6317 /* Is the highest EL AArch64? */ 6318 int is64 = arm_feature(env, ARM_FEATURE_AARCH64); 6319 6320 if (arm_feature(env, ARM_FEATURE_EL3)) { 6321 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW); 6322 } else { 6323 /* Either EL2 is the highest EL (and so the EL2 register width 6324 * is given by is64); or there is no EL2 or EL3, in which case 6325 * the value of 'rw' does not affect the table lookup anyway. 6326 */ 6327 rw = is64; 6328 } 6329 6330 switch (excp_idx) { 6331 case EXCP_IRQ: 6332 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); 6333 hcr = ((env->cp15.hcr_el2 & HCR_IMO) == HCR_IMO); 6334 break; 6335 case EXCP_FIQ: 6336 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); 6337 hcr = ((env->cp15.hcr_el2 & HCR_FMO) == HCR_FMO); 6338 break; 6339 default: 6340 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); 6341 hcr = ((env->cp15.hcr_el2 & HCR_AMO) == HCR_AMO); 6342 break; 6343 }; 6344 6345 /* If HCR.TGE is set then HCR is treated as being 1 */ 6346 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE); 6347 6348 /* Perform a table-lookup for the target EL given the current state */ 6349 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; 6350 6351 assert(target_el > 0); 6352 6353 return target_el; 6354 } 6355 6356 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value, 6357 ARMMMUIdx mmu_idx, bool ignfault) 6358 { 6359 CPUState *cs = CPU(cpu); 6360 CPUARMState *env = &cpu->env; 6361 MemTxAttrs attrs = {}; 6362 MemTxResult txres; 6363 target_ulong page_size; 6364 hwaddr physaddr; 6365 int prot; 6366 ARMMMUFaultInfo fi; 6367 bool secure = mmu_idx & ARM_MMU_IDX_M_S; 6368 int exc; 6369 bool exc_secure; 6370 6371 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr, 6372 &attrs, &prot, &page_size, &fi, NULL)) { 6373 /* MPU/SAU lookup failed */ 6374 if (fi.type == ARMFault_QEMU_SFault) { 6375 qemu_log_mask(CPU_LOG_INT, 6376 "...SecureFault with SFSR.AUVIOL during stacking\n"); 6377 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK; 6378 env->v7m.sfar = addr; 6379 exc = ARMV7M_EXCP_SECURE; 6380 exc_secure = false; 6381 } else { 6382 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n"); 6383 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK; 6384 exc = ARMV7M_EXCP_MEM; 6385 exc_secure = secure; 6386 } 6387 goto pend_fault; 6388 } 6389 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value, 6390 attrs, &txres); 6391 if (txres != MEMTX_OK) { 6392 /* BusFault trying to write the data */ 6393 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n"); 6394 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK; 6395 exc = ARMV7M_EXCP_BUS; 6396 exc_secure = false; 6397 goto pend_fault; 6398 } 6399 return true; 6400 6401 pend_fault: 6402 /* By pending the exception at this point we are making 6403 * the IMPDEF choice "overridden exceptions pended" (see the 6404 * MergeExcInfo() pseudocode). The other choice would be to not 6405 * pend them now and then make a choice about which to throw away 6406 * later if we have two derived exceptions. 6407 * The only case when we must not pend the exception but instead 6408 * throw it away is if we are doing the push of the callee registers 6409 * and we've already generated a derived exception. Even in this 6410 * case we will still update the fault status registers. 6411 */ 6412 if (!ignfault) { 6413 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure); 6414 } 6415 return false; 6416 } 6417 6418 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr, 6419 ARMMMUIdx mmu_idx) 6420 { 6421 CPUState *cs = CPU(cpu); 6422 CPUARMState *env = &cpu->env; 6423 MemTxAttrs attrs = {}; 6424 MemTxResult txres; 6425 target_ulong page_size; 6426 hwaddr physaddr; 6427 int prot; 6428 ARMMMUFaultInfo fi; 6429 bool secure = mmu_idx & ARM_MMU_IDX_M_S; 6430 int exc; 6431 bool exc_secure; 6432 uint32_t value; 6433 6434 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr, 6435 &attrs, &prot, &page_size, &fi, NULL)) { 6436 /* MPU/SAU lookup failed */ 6437 if (fi.type == ARMFault_QEMU_SFault) { 6438 qemu_log_mask(CPU_LOG_INT, 6439 "...SecureFault with SFSR.AUVIOL during unstack\n"); 6440 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK; 6441 env->v7m.sfar = addr; 6442 exc = ARMV7M_EXCP_SECURE; 6443 exc_secure = false; 6444 } else { 6445 qemu_log_mask(CPU_LOG_INT, 6446 "...MemManageFault with CFSR.MUNSTKERR\n"); 6447 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK; 6448 exc = ARMV7M_EXCP_MEM; 6449 exc_secure = secure; 6450 } 6451 goto pend_fault; 6452 } 6453 6454 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr, 6455 attrs, &txres); 6456 if (txres != MEMTX_OK) { 6457 /* BusFault trying to read the data */ 6458 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n"); 6459 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK; 6460 exc = ARMV7M_EXCP_BUS; 6461 exc_secure = false; 6462 goto pend_fault; 6463 } 6464 6465 *dest = value; 6466 return true; 6467 6468 pend_fault: 6469 /* By pending the exception at this point we are making 6470 * the IMPDEF choice "overridden exceptions pended" (see the 6471 * MergeExcInfo() pseudocode). The other choice would be to not 6472 * pend them now and then make a choice about which to throw away 6473 * later if we have two derived exceptions. 6474 */ 6475 armv7m_nvic_set_pending(env->nvic, exc, exc_secure); 6476 return false; 6477 } 6478 6479 /* Return true if we're using the process stack pointer (not the MSP) */ 6480 static bool v7m_using_psp(CPUARMState *env) 6481 { 6482 /* Handler mode always uses the main stack; for thread mode 6483 * the CONTROL.SPSEL bit determines the answer. 6484 * Note that in v7M it is not possible to be in Handler mode with 6485 * CONTROL.SPSEL non-zero, but in v8M it is, so we must check both. 6486 */ 6487 return !arm_v7m_is_handler_mode(env) && 6488 env->v7m.control[env->v7m.secure] & R_V7M_CONTROL_SPSEL_MASK; 6489 } 6490 6491 /* Write to v7M CONTROL.SPSEL bit for the specified security bank. 6492 * This may change the current stack pointer between Main and Process 6493 * stack pointers if it is done for the CONTROL register for the current 6494 * security state. 6495 */ 6496 static void write_v7m_control_spsel_for_secstate(CPUARMState *env, 6497 bool new_spsel, 6498 bool secstate) 6499 { 6500 bool old_is_psp = v7m_using_psp(env); 6501 6502 env->v7m.control[secstate] = 6503 deposit32(env->v7m.control[secstate], 6504 R_V7M_CONTROL_SPSEL_SHIFT, 6505 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel); 6506 6507 if (secstate == env->v7m.secure) { 6508 bool new_is_psp = v7m_using_psp(env); 6509 uint32_t tmp; 6510 6511 if (old_is_psp != new_is_psp) { 6512 tmp = env->v7m.other_sp; 6513 env->v7m.other_sp = env->regs[13]; 6514 env->regs[13] = tmp; 6515 } 6516 } 6517 } 6518 6519 /* Write to v7M CONTROL.SPSEL bit. This may change the current 6520 * stack pointer between Main and Process stack pointers. 6521 */ 6522 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel) 6523 { 6524 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure); 6525 } 6526 6527 void write_v7m_exception(CPUARMState *env, uint32_t new_exc) 6528 { 6529 /* Write a new value to v7m.exception, thus transitioning into or out 6530 * of Handler mode; this may result in a change of active stack pointer. 6531 */ 6532 bool new_is_psp, old_is_psp = v7m_using_psp(env); 6533 uint32_t tmp; 6534 6535 env->v7m.exception = new_exc; 6536 6537 new_is_psp = v7m_using_psp(env); 6538 6539 if (old_is_psp != new_is_psp) { 6540 tmp = env->v7m.other_sp; 6541 env->v7m.other_sp = env->regs[13]; 6542 env->regs[13] = tmp; 6543 } 6544 } 6545 6546 /* Switch M profile security state between NS and S */ 6547 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate) 6548 { 6549 uint32_t new_ss_msp, new_ss_psp; 6550 6551 if (env->v7m.secure == new_secstate) { 6552 return; 6553 } 6554 6555 /* All the banked state is accessed by looking at env->v7m.secure 6556 * except for the stack pointer; rearrange the SP appropriately. 6557 */ 6558 new_ss_msp = env->v7m.other_ss_msp; 6559 new_ss_psp = env->v7m.other_ss_psp; 6560 6561 if (v7m_using_psp(env)) { 6562 env->v7m.other_ss_psp = env->regs[13]; 6563 env->v7m.other_ss_msp = env->v7m.other_sp; 6564 } else { 6565 env->v7m.other_ss_msp = env->regs[13]; 6566 env->v7m.other_ss_psp = env->v7m.other_sp; 6567 } 6568 6569 env->v7m.secure = new_secstate; 6570 6571 if (v7m_using_psp(env)) { 6572 env->regs[13] = new_ss_psp; 6573 env->v7m.other_sp = new_ss_msp; 6574 } else { 6575 env->regs[13] = new_ss_msp; 6576 env->v7m.other_sp = new_ss_psp; 6577 } 6578 } 6579 6580 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) 6581 { 6582 /* Handle v7M BXNS: 6583 * - if the return value is a magic value, do exception return (like BX) 6584 * - otherwise bit 0 of the return value is the target security state 6585 */ 6586 uint32_t min_magic; 6587 6588 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 6589 /* Covers FNC_RETURN and EXC_RETURN magic */ 6590 min_magic = FNC_RETURN_MIN_MAGIC; 6591 } else { 6592 /* EXC_RETURN magic only */ 6593 min_magic = EXC_RETURN_MIN_MAGIC; 6594 } 6595 6596 if (dest >= min_magic) { 6597 /* This is an exception return magic value; put it where 6598 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT. 6599 * Note that if we ever add gen_ss_advance() singlestep support to 6600 * M profile this should count as an "instruction execution complete" 6601 * event (compare gen_bx_excret_final_code()). 6602 */ 6603 env->regs[15] = dest & ~1; 6604 env->thumb = dest & 1; 6605 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT); 6606 /* notreached */ 6607 } 6608 6609 /* translate.c should have made BXNS UNDEF unless we're secure */ 6610 assert(env->v7m.secure); 6611 6612 switch_v7m_security_state(env, dest & 1); 6613 env->thumb = 1; 6614 env->regs[15] = dest & ~1; 6615 } 6616 6617 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) 6618 { 6619 /* Handle v7M BLXNS: 6620 * - bit 0 of the destination address is the target security state 6621 */ 6622 6623 /* At this point regs[15] is the address just after the BLXNS */ 6624 uint32_t nextinst = env->regs[15] | 1; 6625 uint32_t sp = env->regs[13] - 8; 6626 uint32_t saved_psr; 6627 6628 /* translate.c will have made BLXNS UNDEF unless we're secure */ 6629 assert(env->v7m.secure); 6630 6631 if (dest & 1) { 6632 /* target is Secure, so this is just a normal BLX, 6633 * except that the low bit doesn't indicate Thumb/not. 6634 */ 6635 env->regs[14] = nextinst; 6636 env->thumb = 1; 6637 env->regs[15] = dest & ~1; 6638 return; 6639 } 6640 6641 /* Target is non-secure: first push a stack frame */ 6642 if (!QEMU_IS_ALIGNED(sp, 8)) { 6643 qemu_log_mask(LOG_GUEST_ERROR, 6644 "BLXNS with misaligned SP is UNPREDICTABLE\n"); 6645 } 6646 6647 saved_psr = env->v7m.exception; 6648 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) { 6649 saved_psr |= XPSR_SFPA; 6650 } 6651 6652 /* Note that these stores can throw exceptions on MPU faults */ 6653 cpu_stl_data(env, sp, nextinst); 6654 cpu_stl_data(env, sp + 4, saved_psr); 6655 6656 env->regs[13] = sp; 6657 env->regs[14] = 0xfeffffff; 6658 if (arm_v7m_is_handler_mode(env)) { 6659 /* Write a dummy value to IPSR, to avoid leaking the current secure 6660 * exception number to non-secure code. This is guaranteed not 6661 * to cause write_v7m_exception() to actually change stacks. 6662 */ 6663 write_v7m_exception(env, 1); 6664 } 6665 switch_v7m_security_state(env, 0); 6666 env->thumb = 1; 6667 env->regs[15] = dest; 6668 } 6669 6670 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode, 6671 bool spsel) 6672 { 6673 /* Return a pointer to the location where we currently store the 6674 * stack pointer for the requested security state and thread mode. 6675 * This pointer will become invalid if the CPU state is updated 6676 * such that the stack pointers are switched around (eg changing 6677 * the SPSEL control bit). 6678 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode(). 6679 * Unlike that pseudocode, we require the caller to pass us in the 6680 * SPSEL control bit value; this is because we also use this 6681 * function in handling of pushing of the callee-saves registers 6682 * part of the v8M stack frame (pseudocode PushCalleeStack()), 6683 * and in the tailchain codepath the SPSEL bit comes from the exception 6684 * return magic LR value from the previous exception. The pseudocode 6685 * opencodes the stack-selection in PushCalleeStack(), but we prefer 6686 * to make this utility function generic enough to do the job. 6687 */ 6688 bool want_psp = threadmode && spsel; 6689 6690 if (secure == env->v7m.secure) { 6691 if (want_psp == v7m_using_psp(env)) { 6692 return &env->regs[13]; 6693 } else { 6694 return &env->v7m.other_sp; 6695 } 6696 } else { 6697 if (want_psp) { 6698 return &env->v7m.other_ss_psp; 6699 } else { 6700 return &env->v7m.other_ss_msp; 6701 } 6702 } 6703 } 6704 6705 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure, 6706 uint32_t *pvec) 6707 { 6708 CPUState *cs = CPU(cpu); 6709 CPUARMState *env = &cpu->env; 6710 MemTxResult result; 6711 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4; 6712 uint32_t vector_entry; 6713 MemTxAttrs attrs = {}; 6714 ARMMMUIdx mmu_idx; 6715 bool exc_secure; 6716 6717 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true); 6718 6719 /* We don't do a get_phys_addr() here because the rules for vector 6720 * loads are special: they always use the default memory map, and 6721 * the default memory map permits reads from all addresses. 6722 * Since there's no easy way to pass through to pmsav8_mpu_lookup() 6723 * that we want this special case which would always say "yes", 6724 * we just do the SAU lookup here followed by a direct physical load. 6725 */ 6726 attrs.secure = targets_secure; 6727 attrs.user = false; 6728 6729 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 6730 V8M_SAttributes sattrs = {}; 6731 6732 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); 6733 if (sattrs.ns) { 6734 attrs.secure = false; 6735 } else if (!targets_secure) { 6736 /* NS access to S memory */ 6737 goto load_fail; 6738 } 6739 } 6740 6741 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr, 6742 attrs, &result); 6743 if (result != MEMTX_OK) { 6744 goto load_fail; 6745 } 6746 *pvec = vector_entry; 6747 return true; 6748 6749 load_fail: 6750 /* All vector table fetch fails are reported as HardFault, with 6751 * HFSR.VECTTBL and .FORCED set. (FORCED is set because 6752 * technically the underlying exception is a MemManage or BusFault 6753 * that is escalated to HardFault.) This is a terminal exception, 6754 * so we will either take the HardFault immediately or else enter 6755 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()). 6756 */ 6757 exc_secure = targets_secure || 6758 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK); 6759 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK; 6760 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure); 6761 return false; 6762 } 6763 6764 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain, 6765 bool ignore_faults) 6766 { 6767 /* For v8M, push the callee-saves register part of the stack frame. 6768 * Compare the v8M pseudocode PushCalleeStack(). 6769 * In the tailchaining case this may not be the current stack. 6770 */ 6771 CPUARMState *env = &cpu->env; 6772 uint32_t *frame_sp_p; 6773 uint32_t frameptr; 6774 ARMMMUIdx mmu_idx; 6775 bool stacked_ok; 6776 6777 if (dotailchain) { 6778 bool mode = lr & R_V7M_EXCRET_MODE_MASK; 6779 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) || 6780 !mode; 6781 6782 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv); 6783 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode, 6784 lr & R_V7M_EXCRET_SPSEL_MASK); 6785 } else { 6786 mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false)); 6787 frame_sp_p = &env->regs[13]; 6788 } 6789 6790 frameptr = *frame_sp_p - 0x28; 6791 6792 /* Write as much of the stack frame as we can. A write failure may 6793 * cause us to pend a derived exception. 6794 */ 6795 stacked_ok = 6796 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) && 6797 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx, 6798 ignore_faults) && 6799 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx, 6800 ignore_faults) && 6801 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx, 6802 ignore_faults) && 6803 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx, 6804 ignore_faults) && 6805 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx, 6806 ignore_faults) && 6807 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx, 6808 ignore_faults) && 6809 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx, 6810 ignore_faults) && 6811 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx, 6812 ignore_faults); 6813 6814 /* Update SP regardless of whether any of the stack accesses failed. 6815 * When we implement v8M stack limit checking then this attempt to 6816 * update SP might also fail and result in a derived exception. 6817 */ 6818 *frame_sp_p = frameptr; 6819 6820 return !stacked_ok; 6821 } 6822 6823 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain, 6824 bool ignore_stackfaults) 6825 { 6826 /* Do the "take the exception" parts of exception entry, 6827 * but not the pushing of state to the stack. This is 6828 * similar to the pseudocode ExceptionTaken() function. 6829 */ 6830 CPUARMState *env = &cpu->env; 6831 uint32_t addr; 6832 bool targets_secure; 6833 int exc; 6834 bool push_failed = false; 6835 6836 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure); 6837 6838 if (arm_feature(env, ARM_FEATURE_V8)) { 6839 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 6840 (lr & R_V7M_EXCRET_S_MASK)) { 6841 /* The background code (the owner of the registers in the 6842 * exception frame) is Secure. This means it may either already 6843 * have or now needs to push callee-saves registers. 6844 */ 6845 if (targets_secure) { 6846 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) { 6847 /* We took an exception from Secure to NonSecure 6848 * (which means the callee-saved registers got stacked) 6849 * and are now tailchaining to a Secure exception. 6850 * Clear DCRS so eventual return from this Secure 6851 * exception unstacks the callee-saved registers. 6852 */ 6853 lr &= ~R_V7M_EXCRET_DCRS_MASK; 6854 } 6855 } else { 6856 /* We're going to a non-secure exception; push the 6857 * callee-saves registers to the stack now, if they're 6858 * not already saved. 6859 */ 6860 if (lr & R_V7M_EXCRET_DCRS_MASK && 6861 !(dotailchain && (lr & R_V7M_EXCRET_ES_MASK))) { 6862 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain, 6863 ignore_stackfaults); 6864 } 6865 lr |= R_V7M_EXCRET_DCRS_MASK; 6866 } 6867 } 6868 6869 lr &= ~R_V7M_EXCRET_ES_MASK; 6870 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) { 6871 lr |= R_V7M_EXCRET_ES_MASK; 6872 } 6873 lr &= ~R_V7M_EXCRET_SPSEL_MASK; 6874 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) { 6875 lr |= R_V7M_EXCRET_SPSEL_MASK; 6876 } 6877 6878 /* Clear registers if necessary to prevent non-secure exception 6879 * code being able to see register values from secure code. 6880 * Where register values become architecturally UNKNOWN we leave 6881 * them with their previous values. 6882 */ 6883 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 6884 if (!targets_secure) { 6885 /* Always clear the caller-saved registers (they have been 6886 * pushed to the stack earlier in v7m_push_stack()). 6887 * Clear callee-saved registers if the background code is 6888 * Secure (in which case these regs were saved in 6889 * v7m_push_callee_stack()). 6890 */ 6891 int i; 6892 6893 for (i = 0; i < 13; i++) { 6894 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */ 6895 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) { 6896 env->regs[i] = 0; 6897 } 6898 } 6899 /* Clear EAPSR */ 6900 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT); 6901 } 6902 } 6903 } 6904 6905 if (push_failed && !ignore_stackfaults) { 6906 /* Derived exception on callee-saves register stacking: 6907 * we might now want to take a different exception which 6908 * targets a different security state, so try again from the top. 6909 */ 6910 v7m_exception_taken(cpu, lr, true, true); 6911 return; 6912 } 6913 6914 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) { 6915 /* Vector load failed: derived exception */ 6916 v7m_exception_taken(cpu, lr, true, true); 6917 return; 6918 } 6919 6920 /* Now we've done everything that might cause a derived exception 6921 * we can go ahead and activate whichever exception we're going to 6922 * take (which might now be the derived exception). 6923 */ 6924 armv7m_nvic_acknowledge_irq(env->nvic); 6925 6926 /* Switch to target security state -- must do this before writing SPSEL */ 6927 switch_v7m_security_state(env, targets_secure); 6928 write_v7m_control_spsel(env, 0); 6929 arm_clear_exclusive(env); 6930 /* Clear IT bits */ 6931 env->condexec_bits = 0; 6932 env->regs[14] = lr; 6933 env->regs[15] = addr & 0xfffffffe; 6934 env->thumb = addr & 1; 6935 } 6936 6937 static bool v7m_push_stack(ARMCPU *cpu) 6938 { 6939 /* Do the "set up stack frame" part of exception entry, 6940 * similar to pseudocode PushStack(). 6941 * Return true if we generate a derived exception (and so 6942 * should ignore further stack faults trying to process 6943 * that derived exception.) 6944 */ 6945 bool stacked_ok; 6946 CPUARMState *env = &cpu->env; 6947 uint32_t xpsr = xpsr_read(env); 6948 uint32_t frameptr = env->regs[13]; 6949 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false)); 6950 6951 /* Align stack pointer if the guest wants that */ 6952 if ((frameptr & 4) && 6953 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) { 6954 frameptr -= 4; 6955 xpsr |= XPSR_SPREALIGN; 6956 } 6957 6958 frameptr -= 0x20; 6959 6960 /* Write as much of the stack frame as we can. If we fail a stack 6961 * write this will result in a derived exception being pended 6962 * (which may be taken in preference to the one we started with 6963 * if it has higher priority). 6964 */ 6965 stacked_ok = 6966 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) && 6967 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) && 6968 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) && 6969 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) && 6970 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) && 6971 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) && 6972 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) && 6973 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false); 6974 6975 /* Update SP regardless of whether any of the stack accesses failed. 6976 * When we implement v8M stack limit checking then this attempt to 6977 * update SP might also fail and result in a derived exception. 6978 */ 6979 env->regs[13] = frameptr; 6980 6981 return !stacked_ok; 6982 } 6983 6984 static void do_v7m_exception_exit(ARMCPU *cpu) 6985 { 6986 CPUARMState *env = &cpu->env; 6987 uint32_t excret; 6988 uint32_t xpsr; 6989 bool ufault = false; 6990 bool sfault = false; 6991 bool return_to_sp_process; 6992 bool return_to_handler; 6993 bool rettobase = false; 6994 bool exc_secure = false; 6995 bool return_to_secure; 6996 6997 /* If we're not in Handler mode then jumps to magic exception-exit 6998 * addresses don't have magic behaviour. However for the v8M 6999 * security extensions the magic secure-function-return has to 7000 * work in thread mode too, so to avoid doing an extra check in 7001 * the generated code we allow exception-exit magic to also cause the 7002 * internal exception and bring us here in thread mode. Correct code 7003 * will never try to do this (the following insn fetch will always 7004 * fault) so we the overhead of having taken an unnecessary exception 7005 * doesn't matter. 7006 */ 7007 if (!arm_v7m_is_handler_mode(env)) { 7008 return; 7009 } 7010 7011 /* In the spec pseudocode ExceptionReturn() is called directly 7012 * from BXWritePC() and gets the full target PC value including 7013 * bit zero. In QEMU's implementation we treat it as a normal 7014 * jump-to-register (which is then caught later on), and so split 7015 * the target value up between env->regs[15] and env->thumb in 7016 * gen_bx(). Reconstitute it. 7017 */ 7018 excret = env->regs[15]; 7019 if (env->thumb) { 7020 excret |= 1; 7021 } 7022 7023 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32 7024 " previous exception %d\n", 7025 excret, env->v7m.exception); 7026 7027 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) { 7028 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception " 7029 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n", 7030 excret); 7031 } 7032 7033 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7034 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before 7035 * we pick which FAULTMASK to clear. 7036 */ 7037 if (!env->v7m.secure && 7038 ((excret & R_V7M_EXCRET_ES_MASK) || 7039 !(excret & R_V7M_EXCRET_DCRS_MASK))) { 7040 sfault = 1; 7041 /* For all other purposes, treat ES as 0 (R_HXSR) */ 7042 excret &= ~R_V7M_EXCRET_ES_MASK; 7043 } 7044 } 7045 7046 if (env->v7m.exception != ARMV7M_EXCP_NMI) { 7047 /* Auto-clear FAULTMASK on return from other than NMI. 7048 * If the security extension is implemented then this only 7049 * happens if the raw execution priority is >= 0; the 7050 * value of the ES bit in the exception return value indicates 7051 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.) 7052 */ 7053 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7054 exc_secure = excret & R_V7M_EXCRET_ES_MASK; 7055 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) { 7056 env->v7m.faultmask[exc_secure] = 0; 7057 } 7058 } else { 7059 env->v7m.faultmask[M_REG_NS] = 0; 7060 } 7061 } 7062 7063 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception, 7064 exc_secure)) { 7065 case -1: 7066 /* attempt to exit an exception that isn't active */ 7067 ufault = true; 7068 break; 7069 case 0: 7070 /* still an irq active now */ 7071 break; 7072 case 1: 7073 /* we returned to base exception level, no nesting. 7074 * (In the pseudocode this is written using "NestedActivation != 1" 7075 * where we have 'rettobase == false'.) 7076 */ 7077 rettobase = true; 7078 break; 7079 default: 7080 g_assert_not_reached(); 7081 } 7082 7083 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK); 7084 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK; 7085 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) && 7086 (excret & R_V7M_EXCRET_S_MASK); 7087 7088 if (arm_feature(env, ARM_FEATURE_V8)) { 7089 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7090 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP); 7091 * we choose to take the UsageFault. 7092 */ 7093 if ((excret & R_V7M_EXCRET_S_MASK) || 7094 (excret & R_V7M_EXCRET_ES_MASK) || 7095 !(excret & R_V7M_EXCRET_DCRS_MASK)) { 7096 ufault = true; 7097 } 7098 } 7099 if (excret & R_V7M_EXCRET_RES0_MASK) { 7100 ufault = true; 7101 } 7102 } else { 7103 /* For v7M we only recognize certain combinations of the low bits */ 7104 switch (excret & 0xf) { 7105 case 1: /* Return to Handler */ 7106 break; 7107 case 13: /* Return to Thread using Process stack */ 7108 case 9: /* Return to Thread using Main stack */ 7109 /* We only need to check NONBASETHRDENA for v7M, because in 7110 * v8M this bit does not exist (it is RES1). 7111 */ 7112 if (!rettobase && 7113 !(env->v7m.ccr[env->v7m.secure] & 7114 R_V7M_CCR_NONBASETHRDENA_MASK)) { 7115 ufault = true; 7116 } 7117 break; 7118 default: 7119 ufault = true; 7120 } 7121 } 7122 7123 if (sfault) { 7124 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK; 7125 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7126 v7m_exception_taken(cpu, excret, true, false); 7127 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 7128 "stackframe: failed EXC_RETURN.ES validity check\n"); 7129 return; 7130 } 7131 7132 if (ufault) { 7133 /* Bad exception return: instead of popping the exception 7134 * stack, directly take a usage fault on the current stack. 7135 */ 7136 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 7137 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7138 v7m_exception_taken(cpu, excret, true, false); 7139 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " 7140 "stackframe: failed exception return integrity check\n"); 7141 return; 7142 } 7143 7144 /* Set CONTROL.SPSEL from excret.SPSEL. Since we're still in 7145 * Handler mode (and will be until we write the new XPSR.Interrupt 7146 * field) this does not switch around the current stack pointer. 7147 */ 7148 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure); 7149 7150 switch_v7m_security_state(env, return_to_secure); 7151 7152 { 7153 /* The stack pointer we should be reading the exception frame from 7154 * depends on bits in the magic exception return type value (and 7155 * for v8M isn't necessarily the stack pointer we will eventually 7156 * end up resuming execution with). Get a pointer to the location 7157 * in the CPU state struct where the SP we need is currently being 7158 * stored; we will use and modify it in place. 7159 * We use this limited C variable scope so we don't accidentally 7160 * use 'frame_sp_p' after we do something that makes it invalid. 7161 */ 7162 uint32_t *frame_sp_p = get_v7m_sp_ptr(env, 7163 return_to_secure, 7164 !return_to_handler, 7165 return_to_sp_process); 7166 uint32_t frameptr = *frame_sp_p; 7167 bool pop_ok = true; 7168 ARMMMUIdx mmu_idx; 7169 bool return_to_priv = return_to_handler || 7170 !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK); 7171 7172 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure, 7173 return_to_priv); 7174 7175 if (!QEMU_IS_ALIGNED(frameptr, 8) && 7176 arm_feature(env, ARM_FEATURE_V8)) { 7177 qemu_log_mask(LOG_GUEST_ERROR, 7178 "M profile exception return with non-8-aligned SP " 7179 "for destination state is UNPREDICTABLE\n"); 7180 } 7181 7182 /* Do we need to pop callee-saved registers? */ 7183 if (return_to_secure && 7184 ((excret & R_V7M_EXCRET_ES_MASK) == 0 || 7185 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) { 7186 uint32_t expected_sig = 0xfefa125b; 7187 uint32_t actual_sig; 7188 7189 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx); 7190 7191 if (pop_ok && expected_sig != actual_sig) { 7192 /* Take a SecureFault on the current stack */ 7193 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK; 7194 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7195 v7m_exception_taken(cpu, excret, true, false); 7196 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 7197 "stackframe: failed exception return integrity " 7198 "signature check\n"); 7199 return; 7200 } 7201 7202 pop_ok = pop_ok && 7203 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) && 7204 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) && 7205 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) && 7206 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) && 7207 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) && 7208 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) && 7209 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) && 7210 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) && 7211 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx); 7212 7213 frameptr += 0x28; 7214 } 7215 7216 /* Pop registers */ 7217 pop_ok = pop_ok && 7218 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) && 7219 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) && 7220 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) && 7221 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) && 7222 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) && 7223 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) && 7224 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) && 7225 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx); 7226 7227 if (!pop_ok) { 7228 /* v7m_stack_read() pended a fault, so take it (as a tail 7229 * chained exception on the same stack frame) 7230 */ 7231 v7m_exception_taken(cpu, excret, true, false); 7232 return; 7233 } 7234 7235 /* Returning from an exception with a PC with bit 0 set is defined 7236 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified 7237 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore 7238 * the lsbit, and there are several RTOSes out there which incorrectly 7239 * assume the r15 in the stack frame should be a Thumb-style "lsbit 7240 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but 7241 * complain about the badly behaved guest. 7242 */ 7243 if (env->regs[15] & 1) { 7244 env->regs[15] &= ~1U; 7245 if (!arm_feature(env, ARM_FEATURE_V8)) { 7246 qemu_log_mask(LOG_GUEST_ERROR, 7247 "M profile return from interrupt with misaligned " 7248 "PC is UNPREDICTABLE on v7M\n"); 7249 } 7250 } 7251 7252 if (arm_feature(env, ARM_FEATURE_V8)) { 7253 /* For v8M we have to check whether the xPSR exception field 7254 * matches the EXCRET value for return to handler/thread 7255 * before we commit to changing the SP and xPSR. 7256 */ 7257 bool will_be_handler = (xpsr & XPSR_EXCP) != 0; 7258 if (return_to_handler != will_be_handler) { 7259 /* Take an INVPC UsageFault on the current stack. 7260 * By this point we will have switched to the security state 7261 * for the background state, so this UsageFault will target 7262 * that state. 7263 */ 7264 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 7265 env->v7m.secure); 7266 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 7267 v7m_exception_taken(cpu, excret, true, false); 7268 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " 7269 "stackframe: failed exception return integrity " 7270 "check\n"); 7271 return; 7272 } 7273 } 7274 7275 /* Commit to consuming the stack frame */ 7276 frameptr += 0x20; 7277 /* Undo stack alignment (the SPREALIGN bit indicates that the original 7278 * pre-exception SP was not 8-aligned and we added a padding word to 7279 * align it, so we undo this by ORing in the bit that increases it 7280 * from the current 8-aligned value to the 8-unaligned value. (Adding 4 7281 * would work too but a logical OR is how the pseudocode specifies it.) 7282 */ 7283 if (xpsr & XPSR_SPREALIGN) { 7284 frameptr |= 4; 7285 } 7286 *frame_sp_p = frameptr; 7287 } 7288 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */ 7289 xpsr_write(env, xpsr, ~XPSR_SPREALIGN); 7290 7291 /* The restored xPSR exception field will be zero if we're 7292 * resuming in Thread mode. If that doesn't match what the 7293 * exception return excret specified then this is a UsageFault. 7294 * v7M requires we make this check here; v8M did it earlier. 7295 */ 7296 if (return_to_handler != arm_v7m_is_handler_mode(env)) { 7297 /* Take an INVPC UsageFault by pushing the stack again; 7298 * we know we're v7M so this is never a Secure UsageFault. 7299 */ 7300 bool ignore_stackfaults; 7301 7302 assert(!arm_feature(env, ARM_FEATURE_V8)); 7303 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false); 7304 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 7305 ignore_stackfaults = v7m_push_stack(cpu); 7306 v7m_exception_taken(cpu, excret, false, ignore_stackfaults); 7307 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: " 7308 "failed exception return integrity check\n"); 7309 return; 7310 } 7311 7312 /* Otherwise, we have a successful exception exit. */ 7313 arm_clear_exclusive(env); 7314 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n"); 7315 } 7316 7317 static bool do_v7m_function_return(ARMCPU *cpu) 7318 { 7319 /* v8M security extensions magic function return. 7320 * We may either: 7321 * (1) throw an exception (longjump) 7322 * (2) return true if we successfully handled the function return 7323 * (3) return false if we failed a consistency check and have 7324 * pended a UsageFault that needs to be taken now 7325 * 7326 * At this point the magic return value is split between env->regs[15] 7327 * and env->thumb. We don't bother to reconstitute it because we don't 7328 * need it (all values are handled the same way). 7329 */ 7330 CPUARMState *env = &cpu->env; 7331 uint32_t newpc, newpsr, newpsr_exc; 7332 7333 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n"); 7334 7335 { 7336 bool threadmode, spsel; 7337 TCGMemOpIdx oi; 7338 ARMMMUIdx mmu_idx; 7339 uint32_t *frame_sp_p; 7340 uint32_t frameptr; 7341 7342 /* Pull the return address and IPSR from the Secure stack */ 7343 threadmode = !arm_v7m_is_handler_mode(env); 7344 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK; 7345 7346 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel); 7347 frameptr = *frame_sp_p; 7348 7349 /* These loads may throw an exception (for MPU faults). We want to 7350 * do them as secure, so work out what MMU index that is. 7351 */ 7352 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); 7353 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx)); 7354 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0); 7355 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0); 7356 7357 /* Consistency checks on new IPSR */ 7358 newpsr_exc = newpsr & XPSR_EXCP; 7359 if (!((env->v7m.exception == 0 && newpsr_exc == 0) || 7360 (env->v7m.exception == 1 && newpsr_exc != 0))) { 7361 /* Pend the fault and tell our caller to take it */ 7362 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 7363 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 7364 env->v7m.secure); 7365 qemu_log_mask(CPU_LOG_INT, 7366 "...taking INVPC UsageFault: " 7367 "IPSR consistency check failed\n"); 7368 return false; 7369 } 7370 7371 *frame_sp_p = frameptr + 8; 7372 } 7373 7374 /* This invalidates frame_sp_p */ 7375 switch_v7m_security_state(env, true); 7376 env->v7m.exception = newpsr_exc; 7377 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 7378 if (newpsr & XPSR_SFPA) { 7379 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK; 7380 } 7381 xpsr_write(env, 0, XPSR_IT); 7382 env->thumb = newpc & 1; 7383 env->regs[15] = newpc & ~1; 7384 7385 qemu_log_mask(CPU_LOG_INT, "...function return successful\n"); 7386 return true; 7387 } 7388 7389 static void arm_log_exception(int idx) 7390 { 7391 if (qemu_loglevel_mask(CPU_LOG_INT)) { 7392 const char *exc = NULL; 7393 static const char * const excnames[] = { 7394 [EXCP_UDEF] = "Undefined Instruction", 7395 [EXCP_SWI] = "SVC", 7396 [EXCP_PREFETCH_ABORT] = "Prefetch Abort", 7397 [EXCP_DATA_ABORT] = "Data Abort", 7398 [EXCP_IRQ] = "IRQ", 7399 [EXCP_FIQ] = "FIQ", 7400 [EXCP_BKPT] = "Breakpoint", 7401 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit", 7402 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage", 7403 [EXCP_HVC] = "Hypervisor Call", 7404 [EXCP_HYP_TRAP] = "Hypervisor Trap", 7405 [EXCP_SMC] = "Secure Monitor Call", 7406 [EXCP_VIRQ] = "Virtual IRQ", 7407 [EXCP_VFIQ] = "Virtual FIQ", 7408 [EXCP_SEMIHOST] = "Semihosting call", 7409 [EXCP_NOCP] = "v7M NOCP UsageFault", 7410 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault", 7411 }; 7412 7413 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { 7414 exc = excnames[idx]; 7415 } 7416 if (!exc) { 7417 exc = "unknown"; 7418 } 7419 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc); 7420 } 7421 } 7422 7423 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx, 7424 uint32_t addr, uint16_t *insn) 7425 { 7426 /* Load a 16-bit portion of a v7M instruction, returning true on success, 7427 * or false on failure (in which case we will have pended the appropriate 7428 * exception). 7429 * We need to do the instruction fetch's MPU and SAU checks 7430 * like this because there is no MMU index that would allow 7431 * doing the load with a single function call. Instead we must 7432 * first check that the security attributes permit the load 7433 * and that they don't mismatch on the two halves of the instruction, 7434 * and then we do the load as a secure load (ie using the security 7435 * attributes of the address, not the CPU, as architecturally required). 7436 */ 7437 CPUState *cs = CPU(cpu); 7438 CPUARMState *env = &cpu->env; 7439 V8M_SAttributes sattrs = {}; 7440 MemTxAttrs attrs = {}; 7441 ARMMMUFaultInfo fi = {}; 7442 MemTxResult txres; 7443 target_ulong page_size; 7444 hwaddr physaddr; 7445 int prot; 7446 7447 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs); 7448 if (!sattrs.nsc || sattrs.ns) { 7449 /* This must be the second half of the insn, and it straddles a 7450 * region boundary with the second half not being S&NSC. 7451 */ 7452 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 7453 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7454 qemu_log_mask(CPU_LOG_INT, 7455 "...really SecureFault with SFSR.INVEP\n"); 7456 return false; 7457 } 7458 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx, 7459 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) { 7460 /* the MPU lookup failed */ 7461 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; 7462 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure); 7463 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n"); 7464 return false; 7465 } 7466 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr, 7467 attrs, &txres); 7468 if (txres != MEMTX_OK) { 7469 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; 7470 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); 7471 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n"); 7472 return false; 7473 } 7474 return true; 7475 } 7476 7477 static bool v7m_handle_execute_nsc(ARMCPU *cpu) 7478 { 7479 /* Check whether this attempt to execute code in a Secure & NS-Callable 7480 * memory region is for an SG instruction; if so, then emulate the 7481 * effect of the SG instruction and return true. Otherwise pend 7482 * the correct kind of exception and return false. 7483 */ 7484 CPUARMState *env = &cpu->env; 7485 ARMMMUIdx mmu_idx; 7486 uint16_t insn; 7487 7488 /* We should never get here unless get_phys_addr_pmsav8() caused 7489 * an exception for NS executing in S&NSC memory. 7490 */ 7491 assert(!env->v7m.secure); 7492 assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); 7493 7494 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */ 7495 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); 7496 7497 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) { 7498 return false; 7499 } 7500 7501 if (!env->thumb) { 7502 goto gen_invep; 7503 } 7504 7505 if (insn != 0xe97f) { 7506 /* Not an SG instruction first half (we choose the IMPDEF 7507 * early-SG-check option). 7508 */ 7509 goto gen_invep; 7510 } 7511 7512 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) { 7513 return false; 7514 } 7515 7516 if (insn != 0xe97f) { 7517 /* Not an SG instruction second half (yes, both halves of the SG 7518 * insn have the same hex value) 7519 */ 7520 goto gen_invep; 7521 } 7522 7523 /* OK, we have confirmed that we really have an SG instruction. 7524 * We know we're NS in S memory so don't need to repeat those checks. 7525 */ 7526 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32 7527 ", executing it\n", env->regs[15]); 7528 env->regs[14] &= ~1; 7529 switch_v7m_security_state(env, true); 7530 xpsr_write(env, 0, XPSR_IT); 7531 env->regs[15] += 4; 7532 return true; 7533 7534 gen_invep: 7535 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 7536 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7537 qemu_log_mask(CPU_LOG_INT, 7538 "...really SecureFault with SFSR.INVEP\n"); 7539 return false; 7540 } 7541 7542 void arm_v7m_cpu_do_interrupt(CPUState *cs) 7543 { 7544 ARMCPU *cpu = ARM_CPU(cs); 7545 CPUARMState *env = &cpu->env; 7546 uint32_t lr; 7547 bool ignore_stackfaults; 7548 7549 arm_log_exception(cs->exception_index); 7550 7551 /* For exceptions we just mark as pending on the NVIC, and let that 7552 handle it. */ 7553 switch (cs->exception_index) { 7554 case EXCP_UDEF: 7555 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7556 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK; 7557 break; 7558 case EXCP_NOCP: 7559 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7560 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK; 7561 break; 7562 case EXCP_INVSTATE: 7563 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7564 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK; 7565 break; 7566 case EXCP_SWI: 7567 /* The PC already points to the next instruction. */ 7568 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure); 7569 break; 7570 case EXCP_PREFETCH_ABORT: 7571 case EXCP_DATA_ABORT: 7572 /* Note that for M profile we don't have a guest facing FSR, but 7573 * the env->exception.fsr will be populated by the code that 7574 * raises the fault, in the A profile short-descriptor format. 7575 */ 7576 switch (env->exception.fsr & 0xf) { 7577 case M_FAKE_FSR_NSC_EXEC: 7578 /* Exception generated when we try to execute code at an address 7579 * which is marked as Secure & Non-Secure Callable and the CPU 7580 * is in the Non-Secure state. The only instruction which can 7581 * be executed like this is SG (and that only if both halves of 7582 * the SG instruction have the same security attributes.) 7583 * Everything else must generate an INVEP SecureFault, so we 7584 * emulate the SG instruction here. 7585 */ 7586 if (v7m_handle_execute_nsc(cpu)) { 7587 return; 7588 } 7589 break; 7590 case M_FAKE_FSR_SFAULT: 7591 /* Various flavours of SecureFault for attempts to execute or 7592 * access data in the wrong security state. 7593 */ 7594 switch (cs->exception_index) { 7595 case EXCP_PREFETCH_ABORT: 7596 if (env->v7m.secure) { 7597 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK; 7598 qemu_log_mask(CPU_LOG_INT, 7599 "...really SecureFault with SFSR.INVTRAN\n"); 7600 } else { 7601 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 7602 qemu_log_mask(CPU_LOG_INT, 7603 "...really SecureFault with SFSR.INVEP\n"); 7604 } 7605 break; 7606 case EXCP_DATA_ABORT: 7607 /* This must be an NS access to S memory */ 7608 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK; 7609 qemu_log_mask(CPU_LOG_INT, 7610 "...really SecureFault with SFSR.AUVIOL\n"); 7611 break; 7612 } 7613 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7614 break; 7615 case 0x8: /* External Abort */ 7616 switch (cs->exception_index) { 7617 case EXCP_PREFETCH_ABORT: 7618 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; 7619 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n"); 7620 break; 7621 case EXCP_DATA_ABORT: 7622 env->v7m.cfsr[M_REG_NS] |= 7623 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK); 7624 env->v7m.bfar = env->exception.vaddress; 7625 qemu_log_mask(CPU_LOG_INT, 7626 "...with CFSR.PRECISERR and BFAR 0x%x\n", 7627 env->v7m.bfar); 7628 break; 7629 } 7630 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); 7631 break; 7632 default: 7633 /* All other FSR values are either MPU faults or "can't happen 7634 * for M profile" cases. 7635 */ 7636 switch (cs->exception_index) { 7637 case EXCP_PREFETCH_ABORT: 7638 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; 7639 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n"); 7640 break; 7641 case EXCP_DATA_ABORT: 7642 env->v7m.cfsr[env->v7m.secure] |= 7643 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK); 7644 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress; 7645 qemu_log_mask(CPU_LOG_INT, 7646 "...with CFSR.DACCVIOL and MMFAR 0x%x\n", 7647 env->v7m.mmfar[env->v7m.secure]); 7648 break; 7649 } 7650 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, 7651 env->v7m.secure); 7652 break; 7653 } 7654 break; 7655 case EXCP_BKPT: 7656 if (semihosting_enabled()) { 7657 int nr; 7658 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff; 7659 if (nr == 0xab) { 7660 env->regs[15] += 2; 7661 qemu_log_mask(CPU_LOG_INT, 7662 "...handling as semihosting call 0x%x\n", 7663 env->regs[0]); 7664 env->regs[0] = do_arm_semihosting(env); 7665 return; 7666 } 7667 } 7668 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false); 7669 break; 7670 case EXCP_IRQ: 7671 break; 7672 case EXCP_EXCEPTION_EXIT: 7673 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) { 7674 /* Must be v8M security extension function return */ 7675 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC); 7676 assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); 7677 if (do_v7m_function_return(cpu)) { 7678 return; 7679 } 7680 } else { 7681 do_v7m_exception_exit(cpu); 7682 return; 7683 } 7684 break; 7685 default: 7686 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 7687 return; /* Never happens. Keep compiler happy. */ 7688 } 7689 7690 if (arm_feature(env, ARM_FEATURE_V8)) { 7691 lr = R_V7M_EXCRET_RES1_MASK | 7692 R_V7M_EXCRET_DCRS_MASK | 7693 R_V7M_EXCRET_FTYPE_MASK; 7694 /* The S bit indicates whether we should return to Secure 7695 * or NonSecure (ie our current state). 7696 * The ES bit indicates whether we're taking this exception 7697 * to Secure or NonSecure (ie our target state). We set it 7698 * later, in v7m_exception_taken(). 7699 * The SPSEL bit is also set in v7m_exception_taken() for v8M. 7700 * This corresponds to the ARM ARM pseudocode for v8M setting 7701 * some LR bits in PushStack() and some in ExceptionTaken(); 7702 * the distinction matters for the tailchain cases where we 7703 * can take an exception without pushing the stack. 7704 */ 7705 if (env->v7m.secure) { 7706 lr |= R_V7M_EXCRET_S_MASK; 7707 } 7708 } else { 7709 lr = R_V7M_EXCRET_RES1_MASK | 7710 R_V7M_EXCRET_S_MASK | 7711 R_V7M_EXCRET_DCRS_MASK | 7712 R_V7M_EXCRET_FTYPE_MASK | 7713 R_V7M_EXCRET_ES_MASK; 7714 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) { 7715 lr |= R_V7M_EXCRET_SPSEL_MASK; 7716 } 7717 } 7718 if (!arm_v7m_is_handler_mode(env)) { 7719 lr |= R_V7M_EXCRET_MODE_MASK; 7720 } 7721 7722 ignore_stackfaults = v7m_push_stack(cpu); 7723 v7m_exception_taken(cpu, lr, false, ignore_stackfaults); 7724 qemu_log_mask(CPU_LOG_INT, "... as %d\n", env->v7m.exception); 7725 } 7726 7727 /* Function used to synchronize QEMU's AArch64 register set with AArch32 7728 * register set. This is necessary when switching between AArch32 and AArch64 7729 * execution state. 7730 */ 7731 void aarch64_sync_32_to_64(CPUARMState *env) 7732 { 7733 int i; 7734 uint32_t mode = env->uncached_cpsr & CPSR_M; 7735 7736 /* We can blanket copy R[0:7] to X[0:7] */ 7737 for (i = 0; i < 8; i++) { 7738 env->xregs[i] = env->regs[i]; 7739 } 7740 7741 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12. 7742 * Otherwise, they come from the banked user regs. 7743 */ 7744 if (mode == ARM_CPU_MODE_FIQ) { 7745 for (i = 8; i < 13; i++) { 7746 env->xregs[i] = env->usr_regs[i - 8]; 7747 } 7748 } else { 7749 for (i = 8; i < 13; i++) { 7750 env->xregs[i] = env->regs[i]; 7751 } 7752 } 7753 7754 /* Registers x13-x23 are the various mode SP and FP registers. Registers 7755 * r13 and r14 are only copied if we are in that mode, otherwise we copy 7756 * from the mode banked register. 7757 */ 7758 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 7759 env->xregs[13] = env->regs[13]; 7760 env->xregs[14] = env->regs[14]; 7761 } else { 7762 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)]; 7763 /* HYP is an exception in that it is copied from r14 */ 7764 if (mode == ARM_CPU_MODE_HYP) { 7765 env->xregs[14] = env->regs[14]; 7766 } else { 7767 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)]; 7768 } 7769 } 7770 7771 if (mode == ARM_CPU_MODE_HYP) { 7772 env->xregs[15] = env->regs[13]; 7773 } else { 7774 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)]; 7775 } 7776 7777 if (mode == ARM_CPU_MODE_IRQ) { 7778 env->xregs[16] = env->regs[14]; 7779 env->xregs[17] = env->regs[13]; 7780 } else { 7781 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)]; 7782 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)]; 7783 } 7784 7785 if (mode == ARM_CPU_MODE_SVC) { 7786 env->xregs[18] = env->regs[14]; 7787 env->xregs[19] = env->regs[13]; 7788 } else { 7789 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)]; 7790 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)]; 7791 } 7792 7793 if (mode == ARM_CPU_MODE_ABT) { 7794 env->xregs[20] = env->regs[14]; 7795 env->xregs[21] = env->regs[13]; 7796 } else { 7797 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)]; 7798 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)]; 7799 } 7800 7801 if (mode == ARM_CPU_MODE_UND) { 7802 env->xregs[22] = env->regs[14]; 7803 env->xregs[23] = env->regs[13]; 7804 } else { 7805 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)]; 7806 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)]; 7807 } 7808 7809 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 7810 * mode, then we can copy from r8-r14. Otherwise, we copy from the 7811 * FIQ bank for r8-r14. 7812 */ 7813 if (mode == ARM_CPU_MODE_FIQ) { 7814 for (i = 24; i < 31; i++) { 7815 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */ 7816 } 7817 } else { 7818 for (i = 24; i < 29; i++) { 7819 env->xregs[i] = env->fiq_regs[i - 24]; 7820 } 7821 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)]; 7822 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)]; 7823 } 7824 7825 env->pc = env->regs[15]; 7826 } 7827 7828 /* Function used to synchronize QEMU's AArch32 register set with AArch64 7829 * register set. This is necessary when switching between AArch32 and AArch64 7830 * execution state. 7831 */ 7832 void aarch64_sync_64_to_32(CPUARMState *env) 7833 { 7834 int i; 7835 uint32_t mode = env->uncached_cpsr & CPSR_M; 7836 7837 /* We can blanket copy X[0:7] to R[0:7] */ 7838 for (i = 0; i < 8; i++) { 7839 env->regs[i] = env->xregs[i]; 7840 } 7841 7842 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12. 7843 * Otherwise, we copy x8-x12 into the banked user regs. 7844 */ 7845 if (mode == ARM_CPU_MODE_FIQ) { 7846 for (i = 8; i < 13; i++) { 7847 env->usr_regs[i - 8] = env->xregs[i]; 7848 } 7849 } else { 7850 for (i = 8; i < 13; i++) { 7851 env->regs[i] = env->xregs[i]; 7852 } 7853 } 7854 7855 /* Registers r13 & r14 depend on the current mode. 7856 * If we are in a given mode, we copy the corresponding x registers to r13 7857 * and r14. Otherwise, we copy the x register to the banked r13 and r14 7858 * for the mode. 7859 */ 7860 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 7861 env->regs[13] = env->xregs[13]; 7862 env->regs[14] = env->xregs[14]; 7863 } else { 7864 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13]; 7865 7866 /* HYP is an exception in that it does not have its own banked r14 but 7867 * shares the USR r14 7868 */ 7869 if (mode == ARM_CPU_MODE_HYP) { 7870 env->regs[14] = env->xregs[14]; 7871 } else { 7872 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14]; 7873 } 7874 } 7875 7876 if (mode == ARM_CPU_MODE_HYP) { 7877 env->regs[13] = env->xregs[15]; 7878 } else { 7879 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15]; 7880 } 7881 7882 if (mode == ARM_CPU_MODE_IRQ) { 7883 env->regs[14] = env->xregs[16]; 7884 env->regs[13] = env->xregs[17]; 7885 } else { 7886 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16]; 7887 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17]; 7888 } 7889 7890 if (mode == ARM_CPU_MODE_SVC) { 7891 env->regs[14] = env->xregs[18]; 7892 env->regs[13] = env->xregs[19]; 7893 } else { 7894 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18]; 7895 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19]; 7896 } 7897 7898 if (mode == ARM_CPU_MODE_ABT) { 7899 env->regs[14] = env->xregs[20]; 7900 env->regs[13] = env->xregs[21]; 7901 } else { 7902 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20]; 7903 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21]; 7904 } 7905 7906 if (mode == ARM_CPU_MODE_UND) { 7907 env->regs[14] = env->xregs[22]; 7908 env->regs[13] = env->xregs[23]; 7909 } else { 7910 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22]; 7911 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23]; 7912 } 7913 7914 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 7915 * mode, then we can copy to r8-r14. Otherwise, we copy to the 7916 * FIQ bank for r8-r14. 7917 */ 7918 if (mode == ARM_CPU_MODE_FIQ) { 7919 for (i = 24; i < 31; i++) { 7920 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */ 7921 } 7922 } else { 7923 for (i = 24; i < 29; i++) { 7924 env->fiq_regs[i - 24] = env->xregs[i]; 7925 } 7926 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29]; 7927 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30]; 7928 } 7929 7930 env->regs[15] = env->pc; 7931 } 7932 7933 static void arm_cpu_do_interrupt_aarch32(CPUState *cs) 7934 { 7935 ARMCPU *cpu = ARM_CPU(cs); 7936 CPUARMState *env = &cpu->env; 7937 uint32_t addr; 7938 uint32_t mask; 7939 int new_mode; 7940 uint32_t offset; 7941 uint32_t moe; 7942 7943 /* If this is a debug exception we must update the DBGDSCR.MOE bits */ 7944 switch (env->exception.syndrome >> ARM_EL_EC_SHIFT) { 7945 case EC_BREAKPOINT: 7946 case EC_BREAKPOINT_SAME_EL: 7947 moe = 1; 7948 break; 7949 case EC_WATCHPOINT: 7950 case EC_WATCHPOINT_SAME_EL: 7951 moe = 10; 7952 break; 7953 case EC_AA32_BKPT: 7954 moe = 3; 7955 break; 7956 case EC_VECTORCATCH: 7957 moe = 5; 7958 break; 7959 default: 7960 moe = 0; 7961 break; 7962 } 7963 7964 if (moe) { 7965 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); 7966 } 7967 7968 /* TODO: Vectored interrupt controller. */ 7969 switch (cs->exception_index) { 7970 case EXCP_UDEF: 7971 new_mode = ARM_CPU_MODE_UND; 7972 addr = 0x04; 7973 mask = CPSR_I; 7974 if (env->thumb) 7975 offset = 2; 7976 else 7977 offset = 4; 7978 break; 7979 case EXCP_SWI: 7980 new_mode = ARM_CPU_MODE_SVC; 7981 addr = 0x08; 7982 mask = CPSR_I; 7983 /* The PC already points to the next instruction. */ 7984 offset = 0; 7985 break; 7986 case EXCP_BKPT: 7987 /* Fall through to prefetch abort. */ 7988 case EXCP_PREFETCH_ABORT: 7989 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); 7990 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); 7991 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", 7992 env->exception.fsr, (uint32_t)env->exception.vaddress); 7993 new_mode = ARM_CPU_MODE_ABT; 7994 addr = 0x0c; 7995 mask = CPSR_A | CPSR_I; 7996 offset = 4; 7997 break; 7998 case EXCP_DATA_ABORT: 7999 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 8000 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); 8001 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", 8002 env->exception.fsr, 8003 (uint32_t)env->exception.vaddress); 8004 new_mode = ARM_CPU_MODE_ABT; 8005 addr = 0x10; 8006 mask = CPSR_A | CPSR_I; 8007 offset = 8; 8008 break; 8009 case EXCP_IRQ: 8010 new_mode = ARM_CPU_MODE_IRQ; 8011 addr = 0x18; 8012 /* Disable IRQ and imprecise data aborts. */ 8013 mask = CPSR_A | CPSR_I; 8014 offset = 4; 8015 if (env->cp15.scr_el3 & SCR_IRQ) { 8016 /* IRQ routed to monitor mode */ 8017 new_mode = ARM_CPU_MODE_MON; 8018 mask |= CPSR_F; 8019 } 8020 break; 8021 case EXCP_FIQ: 8022 new_mode = ARM_CPU_MODE_FIQ; 8023 addr = 0x1c; 8024 /* Disable FIQ, IRQ and imprecise data aborts. */ 8025 mask = CPSR_A | CPSR_I | CPSR_F; 8026 if (env->cp15.scr_el3 & SCR_FIQ) { 8027 /* FIQ routed to monitor mode */ 8028 new_mode = ARM_CPU_MODE_MON; 8029 } 8030 offset = 4; 8031 break; 8032 case EXCP_VIRQ: 8033 new_mode = ARM_CPU_MODE_IRQ; 8034 addr = 0x18; 8035 /* Disable IRQ and imprecise data aborts. */ 8036 mask = CPSR_A | CPSR_I; 8037 offset = 4; 8038 break; 8039 case EXCP_VFIQ: 8040 new_mode = ARM_CPU_MODE_FIQ; 8041 addr = 0x1c; 8042 /* Disable FIQ, IRQ and imprecise data aborts. */ 8043 mask = CPSR_A | CPSR_I | CPSR_F; 8044 offset = 4; 8045 break; 8046 case EXCP_SMC: 8047 new_mode = ARM_CPU_MODE_MON; 8048 addr = 0x08; 8049 mask = CPSR_A | CPSR_I | CPSR_F; 8050 offset = 0; 8051 break; 8052 default: 8053 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 8054 return; /* Never happens. Keep compiler happy. */ 8055 } 8056 8057 if (new_mode == ARM_CPU_MODE_MON) { 8058 addr += env->cp15.mvbar; 8059 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 8060 /* High vectors. When enabled, base address cannot be remapped. */ 8061 addr += 0xffff0000; 8062 } else { 8063 /* ARM v7 architectures provide a vector base address register to remap 8064 * the interrupt vector table. 8065 * This register is only followed in non-monitor mode, and is banked. 8066 * Note: only bits 31:5 are valid. 8067 */ 8068 addr += A32_BANKED_CURRENT_REG_GET(env, vbar); 8069 } 8070 8071 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 8072 env->cp15.scr_el3 &= ~SCR_NS; 8073 } 8074 8075 switch_mode (env, new_mode); 8076 /* For exceptions taken to AArch32 we must clear the SS bit in both 8077 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. 8078 */ 8079 env->uncached_cpsr &= ~PSTATE_SS; 8080 env->spsr = cpsr_read(env); 8081 /* Clear IT bits. */ 8082 env->condexec_bits = 0; 8083 /* Switch to the new mode, and to the correct instruction set. */ 8084 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; 8085 /* Set new mode endianness */ 8086 env->uncached_cpsr &= ~CPSR_E; 8087 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) { 8088 env->uncached_cpsr |= CPSR_E; 8089 } 8090 env->daif |= mask; 8091 /* this is a lie, as the was no c1_sys on V4T/V5, but who cares 8092 * and we should just guard the thumb mode on V4 */ 8093 if (arm_feature(env, ARM_FEATURE_V4T)) { 8094 env->thumb = (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; 8095 } 8096 env->regs[14] = env->regs[15] + offset; 8097 env->regs[15] = addr; 8098 } 8099 8100 /* Handle exception entry to a target EL which is using AArch64 */ 8101 static void arm_cpu_do_interrupt_aarch64(CPUState *cs) 8102 { 8103 ARMCPU *cpu = ARM_CPU(cs); 8104 CPUARMState *env = &cpu->env; 8105 unsigned int new_el = env->exception.target_el; 8106 target_ulong addr = env->cp15.vbar_el[new_el]; 8107 unsigned int new_mode = aarch64_pstate_mode(new_el, true); 8108 8109 if (arm_current_el(env) < new_el) { 8110 /* Entry vector offset depends on whether the implemented EL 8111 * immediately lower than the target level is using AArch32 or AArch64 8112 */ 8113 bool is_aa64; 8114 8115 switch (new_el) { 8116 case 3: 8117 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0; 8118 break; 8119 case 2: 8120 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0; 8121 break; 8122 case 1: 8123 is_aa64 = is_a64(env); 8124 break; 8125 default: 8126 g_assert_not_reached(); 8127 } 8128 8129 if (is_aa64) { 8130 addr += 0x400; 8131 } else { 8132 addr += 0x600; 8133 } 8134 } else if (pstate_read(env) & PSTATE_SP) { 8135 addr += 0x200; 8136 } 8137 8138 switch (cs->exception_index) { 8139 case EXCP_PREFETCH_ABORT: 8140 case EXCP_DATA_ABORT: 8141 env->cp15.far_el[new_el] = env->exception.vaddress; 8142 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n", 8143 env->cp15.far_el[new_el]); 8144 /* fall through */ 8145 case EXCP_BKPT: 8146 case EXCP_UDEF: 8147 case EXCP_SWI: 8148 case EXCP_HVC: 8149 case EXCP_HYP_TRAP: 8150 case EXCP_SMC: 8151 env->cp15.esr_el[new_el] = env->exception.syndrome; 8152 break; 8153 case EXCP_IRQ: 8154 case EXCP_VIRQ: 8155 addr += 0x80; 8156 break; 8157 case EXCP_FIQ: 8158 case EXCP_VFIQ: 8159 addr += 0x100; 8160 break; 8161 case EXCP_SEMIHOST: 8162 qemu_log_mask(CPU_LOG_INT, 8163 "...handling as semihosting call 0x%" PRIx64 "\n", 8164 env->xregs[0]); 8165 env->xregs[0] = do_arm_semihosting(env); 8166 return; 8167 default: 8168 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 8169 } 8170 8171 if (is_a64(env)) { 8172 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env); 8173 aarch64_save_sp(env, arm_current_el(env)); 8174 env->elr_el[new_el] = env->pc; 8175 } else { 8176 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env); 8177 env->elr_el[new_el] = env->regs[15]; 8178 8179 aarch64_sync_32_to_64(env); 8180 8181 env->condexec_bits = 0; 8182 } 8183 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n", 8184 env->elr_el[new_el]); 8185 8186 pstate_write(env, PSTATE_DAIF | new_mode); 8187 env->aarch64 = 1; 8188 aarch64_restore_sp(env, new_el); 8189 8190 env->pc = addr; 8191 8192 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n", 8193 new_el, env->pc, pstate_read(env)); 8194 } 8195 8196 static inline bool check_for_semihosting(CPUState *cs) 8197 { 8198 /* Check whether this exception is a semihosting call; if so 8199 * then handle it and return true; otherwise return false. 8200 */ 8201 ARMCPU *cpu = ARM_CPU(cs); 8202 CPUARMState *env = &cpu->env; 8203 8204 if (is_a64(env)) { 8205 if (cs->exception_index == EXCP_SEMIHOST) { 8206 /* This is always the 64-bit semihosting exception. 8207 * The "is this usermode" and "is semihosting enabled" 8208 * checks have been done at translate time. 8209 */ 8210 qemu_log_mask(CPU_LOG_INT, 8211 "...handling as semihosting call 0x%" PRIx64 "\n", 8212 env->xregs[0]); 8213 env->xregs[0] = do_arm_semihosting(env); 8214 return true; 8215 } 8216 return false; 8217 } else { 8218 uint32_t imm; 8219 8220 /* Only intercept calls from privileged modes, to provide some 8221 * semblance of security. 8222 */ 8223 if (cs->exception_index != EXCP_SEMIHOST && 8224 (!semihosting_enabled() || 8225 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) { 8226 return false; 8227 } 8228 8229 switch (cs->exception_index) { 8230 case EXCP_SEMIHOST: 8231 /* This is always a semihosting call; the "is this usermode" 8232 * and "is semihosting enabled" checks have been done at 8233 * translate time. 8234 */ 8235 break; 8236 case EXCP_SWI: 8237 /* Check for semihosting interrupt. */ 8238 if (env->thumb) { 8239 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env)) 8240 & 0xff; 8241 if (imm == 0xab) { 8242 break; 8243 } 8244 } else { 8245 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env)) 8246 & 0xffffff; 8247 if (imm == 0x123456) { 8248 break; 8249 } 8250 } 8251 return false; 8252 case EXCP_BKPT: 8253 /* See if this is a semihosting syscall. */ 8254 if (env->thumb) { 8255 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) 8256 & 0xff; 8257 if (imm == 0xab) { 8258 env->regs[15] += 2; 8259 break; 8260 } 8261 } 8262 return false; 8263 default: 8264 return false; 8265 } 8266 8267 qemu_log_mask(CPU_LOG_INT, 8268 "...handling as semihosting call 0x%x\n", 8269 env->regs[0]); 8270 env->regs[0] = do_arm_semihosting(env); 8271 return true; 8272 } 8273 } 8274 8275 /* Handle a CPU exception for A and R profile CPUs. 8276 * Do any appropriate logging, handle PSCI calls, and then hand off 8277 * to the AArch64-entry or AArch32-entry function depending on the 8278 * target exception level's register width. 8279 */ 8280 void arm_cpu_do_interrupt(CPUState *cs) 8281 { 8282 ARMCPU *cpu = ARM_CPU(cs); 8283 CPUARMState *env = &cpu->env; 8284 unsigned int new_el = env->exception.target_el; 8285 8286 assert(!arm_feature(env, ARM_FEATURE_M)); 8287 8288 arm_log_exception(cs->exception_index); 8289 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env), 8290 new_el); 8291 if (qemu_loglevel_mask(CPU_LOG_INT) 8292 && !excp_is_internal(cs->exception_index)) { 8293 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n", 8294 env->exception.syndrome >> ARM_EL_EC_SHIFT, 8295 env->exception.syndrome); 8296 } 8297 8298 if (arm_is_psci_call(cpu, cs->exception_index)) { 8299 arm_handle_psci_call(cpu); 8300 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); 8301 return; 8302 } 8303 8304 /* Semihosting semantics depend on the register width of the 8305 * code that caused the exception, not the target exception level, 8306 * so must be handled here. 8307 */ 8308 if (check_for_semihosting(cs)) { 8309 return; 8310 } 8311 8312 /* Hooks may change global state so BQL should be held, also the 8313 * BQL needs to be held for any modification of 8314 * cs->interrupt_request. 8315 */ 8316 g_assert(qemu_mutex_iothread_locked()); 8317 8318 arm_call_pre_el_change_hook(cpu); 8319 8320 assert(!excp_is_internal(cs->exception_index)); 8321 if (arm_el_is_aa64(env, new_el)) { 8322 arm_cpu_do_interrupt_aarch64(cs); 8323 } else { 8324 arm_cpu_do_interrupt_aarch32(cs); 8325 } 8326 8327 arm_call_el_change_hook(cpu); 8328 8329 if (!kvm_enabled()) { 8330 cs->interrupt_request |= CPU_INTERRUPT_EXITTB; 8331 } 8332 } 8333 8334 /* Return the exception level which controls this address translation regime */ 8335 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx) 8336 { 8337 switch (mmu_idx) { 8338 case ARMMMUIdx_S2NS: 8339 case ARMMMUIdx_S1E2: 8340 return 2; 8341 case ARMMMUIdx_S1E3: 8342 return 3; 8343 case ARMMMUIdx_S1SE0: 8344 return arm_el_is_aa64(env, 3) ? 1 : 3; 8345 case ARMMMUIdx_S1SE1: 8346 case ARMMMUIdx_S1NSE0: 8347 case ARMMMUIdx_S1NSE1: 8348 case ARMMMUIdx_MPrivNegPri: 8349 case ARMMMUIdx_MUserNegPri: 8350 case ARMMMUIdx_MPriv: 8351 case ARMMMUIdx_MUser: 8352 case ARMMMUIdx_MSPrivNegPri: 8353 case ARMMMUIdx_MSUserNegPri: 8354 case ARMMMUIdx_MSPriv: 8355 case ARMMMUIdx_MSUser: 8356 return 1; 8357 default: 8358 g_assert_not_reached(); 8359 } 8360 } 8361 8362 /* Return the SCTLR value which controls this address translation regime */ 8363 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx) 8364 { 8365 return env->cp15.sctlr_el[regime_el(env, mmu_idx)]; 8366 } 8367 8368 /* Return true if the specified stage of address translation is disabled */ 8369 static inline bool regime_translation_disabled(CPUARMState *env, 8370 ARMMMUIdx mmu_idx) 8371 { 8372 if (arm_feature(env, ARM_FEATURE_M)) { 8373 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] & 8374 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) { 8375 case R_V7M_MPU_CTRL_ENABLE_MASK: 8376 /* Enabled, but not for HardFault and NMI */ 8377 return mmu_idx & ARM_MMU_IDX_M_NEGPRI; 8378 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK: 8379 /* Enabled for all cases */ 8380 return false; 8381 case 0: 8382 default: 8383 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but 8384 * we warned about that in armv7m_nvic.c when the guest set it. 8385 */ 8386 return true; 8387 } 8388 } 8389 8390 if (mmu_idx == ARMMMUIdx_S2NS) { 8391 return (env->cp15.hcr_el2 & HCR_VM) == 0; 8392 } 8393 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0; 8394 } 8395 8396 static inline bool regime_translation_big_endian(CPUARMState *env, 8397 ARMMMUIdx mmu_idx) 8398 { 8399 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0; 8400 } 8401 8402 /* Return the TCR controlling this translation regime */ 8403 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx) 8404 { 8405 if (mmu_idx == ARMMMUIdx_S2NS) { 8406 return &env->cp15.vtcr_el2; 8407 } 8408 return &env->cp15.tcr_el[regime_el(env, mmu_idx)]; 8409 } 8410 8411 /* Convert a possible stage1+2 MMU index into the appropriate 8412 * stage 1 MMU index 8413 */ 8414 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx) 8415 { 8416 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 8417 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0); 8418 } 8419 return mmu_idx; 8420 } 8421 8422 /* Returns TBI0 value for current regime el */ 8423 uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx) 8424 { 8425 TCR *tcr; 8426 uint32_t el; 8427 8428 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert 8429 * a stage 1+2 mmu index into the appropriate stage 1 mmu index. 8430 */ 8431 mmu_idx = stage_1_mmu_idx(mmu_idx); 8432 8433 tcr = regime_tcr(env, mmu_idx); 8434 el = regime_el(env, mmu_idx); 8435 8436 if (el > 1) { 8437 return extract64(tcr->raw_tcr, 20, 1); 8438 } else { 8439 return extract64(tcr->raw_tcr, 37, 1); 8440 } 8441 } 8442 8443 /* Returns TBI1 value for current regime el */ 8444 uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx) 8445 { 8446 TCR *tcr; 8447 uint32_t el; 8448 8449 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert 8450 * a stage 1+2 mmu index into the appropriate stage 1 mmu index. 8451 */ 8452 mmu_idx = stage_1_mmu_idx(mmu_idx); 8453 8454 tcr = regime_tcr(env, mmu_idx); 8455 el = regime_el(env, mmu_idx); 8456 8457 if (el > 1) { 8458 return 0; 8459 } else { 8460 return extract64(tcr->raw_tcr, 38, 1); 8461 } 8462 } 8463 8464 /* Return the TTBR associated with this translation regime */ 8465 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx, 8466 int ttbrn) 8467 { 8468 if (mmu_idx == ARMMMUIdx_S2NS) { 8469 return env->cp15.vttbr_el2; 8470 } 8471 if (ttbrn == 0) { 8472 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)]; 8473 } else { 8474 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)]; 8475 } 8476 } 8477 8478 /* Return true if the translation regime is using LPAE format page tables */ 8479 static inline bool regime_using_lpae_format(CPUARMState *env, 8480 ARMMMUIdx mmu_idx) 8481 { 8482 int el = regime_el(env, mmu_idx); 8483 if (el == 2 || arm_el_is_aa64(env, el)) { 8484 return true; 8485 } 8486 if (arm_feature(env, ARM_FEATURE_LPAE) 8487 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) { 8488 return true; 8489 } 8490 return false; 8491 } 8492 8493 /* Returns true if the stage 1 translation regime is using LPAE format page 8494 * tables. Used when raising alignment exceptions, whose FSR changes depending 8495 * on whether the long or short descriptor format is in use. */ 8496 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx) 8497 { 8498 mmu_idx = stage_1_mmu_idx(mmu_idx); 8499 8500 return regime_using_lpae_format(env, mmu_idx); 8501 } 8502 8503 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx) 8504 { 8505 switch (mmu_idx) { 8506 case ARMMMUIdx_S1SE0: 8507 case ARMMMUIdx_S1NSE0: 8508 case ARMMMUIdx_MUser: 8509 case ARMMMUIdx_MSUser: 8510 case ARMMMUIdx_MUserNegPri: 8511 case ARMMMUIdx_MSUserNegPri: 8512 return true; 8513 default: 8514 return false; 8515 case ARMMMUIdx_S12NSE0: 8516 case ARMMMUIdx_S12NSE1: 8517 g_assert_not_reached(); 8518 } 8519 } 8520 8521 /* Translate section/page access permissions to page 8522 * R/W protection flags 8523 * 8524 * @env: CPUARMState 8525 * @mmu_idx: MMU index indicating required translation regime 8526 * @ap: The 3-bit access permissions (AP[2:0]) 8527 * @domain_prot: The 2-bit domain access permissions 8528 */ 8529 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, 8530 int ap, int domain_prot) 8531 { 8532 bool is_user = regime_is_user(env, mmu_idx); 8533 8534 if (domain_prot == 3) { 8535 return PAGE_READ | PAGE_WRITE; 8536 } 8537 8538 switch (ap) { 8539 case 0: 8540 if (arm_feature(env, ARM_FEATURE_V7)) { 8541 return 0; 8542 } 8543 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) { 8544 case SCTLR_S: 8545 return is_user ? 0 : PAGE_READ; 8546 case SCTLR_R: 8547 return PAGE_READ; 8548 default: 8549 return 0; 8550 } 8551 case 1: 8552 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 8553 case 2: 8554 if (is_user) { 8555 return PAGE_READ; 8556 } else { 8557 return PAGE_READ | PAGE_WRITE; 8558 } 8559 case 3: 8560 return PAGE_READ | PAGE_WRITE; 8561 case 4: /* Reserved. */ 8562 return 0; 8563 case 5: 8564 return is_user ? 0 : PAGE_READ; 8565 case 6: 8566 return PAGE_READ; 8567 case 7: 8568 if (!arm_feature(env, ARM_FEATURE_V6K)) { 8569 return 0; 8570 } 8571 return PAGE_READ; 8572 default: 8573 g_assert_not_reached(); 8574 } 8575 } 8576 8577 /* Translate section/page access permissions to page 8578 * R/W protection flags. 8579 * 8580 * @ap: The 2-bit simple AP (AP[2:1]) 8581 * @is_user: TRUE if accessing from PL0 8582 */ 8583 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user) 8584 { 8585 switch (ap) { 8586 case 0: 8587 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 8588 case 1: 8589 return PAGE_READ | PAGE_WRITE; 8590 case 2: 8591 return is_user ? 0 : PAGE_READ; 8592 case 3: 8593 return PAGE_READ; 8594 default: 8595 g_assert_not_reached(); 8596 } 8597 } 8598 8599 static inline int 8600 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap) 8601 { 8602 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx)); 8603 } 8604 8605 /* Translate S2 section/page access permissions to protection flags 8606 * 8607 * @env: CPUARMState 8608 * @s2ap: The 2-bit stage2 access permissions (S2AP) 8609 * @xn: XN (execute-never) bit 8610 */ 8611 static int get_S2prot(CPUARMState *env, int s2ap, int xn) 8612 { 8613 int prot = 0; 8614 8615 if (s2ap & 1) { 8616 prot |= PAGE_READ; 8617 } 8618 if (s2ap & 2) { 8619 prot |= PAGE_WRITE; 8620 } 8621 if (!xn) { 8622 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) { 8623 prot |= PAGE_EXEC; 8624 } 8625 } 8626 return prot; 8627 } 8628 8629 /* Translate section/page access permissions to protection flags 8630 * 8631 * @env: CPUARMState 8632 * @mmu_idx: MMU index indicating required translation regime 8633 * @is_aa64: TRUE if AArch64 8634 * @ap: The 2-bit simple AP (AP[2:1]) 8635 * @ns: NS (non-secure) bit 8636 * @xn: XN (execute-never) bit 8637 * @pxn: PXN (privileged execute-never) bit 8638 */ 8639 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64, 8640 int ap, int ns, int xn, int pxn) 8641 { 8642 bool is_user = regime_is_user(env, mmu_idx); 8643 int prot_rw, user_rw; 8644 bool have_wxn; 8645 int wxn = 0; 8646 8647 assert(mmu_idx != ARMMMUIdx_S2NS); 8648 8649 user_rw = simple_ap_to_rw_prot_is_user(ap, true); 8650 if (is_user) { 8651 prot_rw = user_rw; 8652 } else { 8653 prot_rw = simple_ap_to_rw_prot_is_user(ap, false); 8654 } 8655 8656 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) { 8657 return prot_rw; 8658 } 8659 8660 /* TODO have_wxn should be replaced with 8661 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2) 8662 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE 8663 * compatible processors have EL2, which is required for [U]WXN. 8664 */ 8665 have_wxn = arm_feature(env, ARM_FEATURE_LPAE); 8666 8667 if (have_wxn) { 8668 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN; 8669 } 8670 8671 if (is_aa64) { 8672 switch (regime_el(env, mmu_idx)) { 8673 case 1: 8674 if (!is_user) { 8675 xn = pxn || (user_rw & PAGE_WRITE); 8676 } 8677 break; 8678 case 2: 8679 case 3: 8680 break; 8681 } 8682 } else if (arm_feature(env, ARM_FEATURE_V7)) { 8683 switch (regime_el(env, mmu_idx)) { 8684 case 1: 8685 case 3: 8686 if (is_user) { 8687 xn = xn || !(user_rw & PAGE_READ); 8688 } else { 8689 int uwxn = 0; 8690 if (have_wxn) { 8691 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN; 8692 } 8693 xn = xn || !(prot_rw & PAGE_READ) || pxn || 8694 (uwxn && (user_rw & PAGE_WRITE)); 8695 } 8696 break; 8697 case 2: 8698 break; 8699 } 8700 } else { 8701 xn = wxn = 0; 8702 } 8703 8704 if (xn || (wxn && (prot_rw & PAGE_WRITE))) { 8705 return prot_rw; 8706 } 8707 return prot_rw | PAGE_EXEC; 8708 } 8709 8710 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx, 8711 uint32_t *table, uint32_t address) 8712 { 8713 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */ 8714 TCR *tcr = regime_tcr(env, mmu_idx); 8715 8716 if (address & tcr->mask) { 8717 if (tcr->raw_tcr & TTBCR_PD1) { 8718 /* Translation table walk disabled for TTBR1 */ 8719 return false; 8720 } 8721 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000; 8722 } else { 8723 if (tcr->raw_tcr & TTBCR_PD0) { 8724 /* Translation table walk disabled for TTBR0 */ 8725 return false; 8726 } 8727 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask; 8728 } 8729 *table |= (address >> 18) & 0x3ffc; 8730 return true; 8731 } 8732 8733 /* Translate a S1 pagetable walk through S2 if needed. */ 8734 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx, 8735 hwaddr addr, MemTxAttrs txattrs, 8736 ARMMMUFaultInfo *fi) 8737 { 8738 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) && 8739 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) { 8740 target_ulong s2size; 8741 hwaddr s2pa; 8742 int s2prot; 8743 int ret; 8744 8745 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa, 8746 &txattrs, &s2prot, &s2size, fi, NULL); 8747 if (ret) { 8748 assert(fi->type != ARMFault_None); 8749 fi->s2addr = addr; 8750 fi->stage2 = true; 8751 fi->s1ptw = true; 8752 return ~0; 8753 } 8754 addr = s2pa; 8755 } 8756 return addr; 8757 } 8758 8759 /* All loads done in the course of a page table walk go through here. */ 8760 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure, 8761 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 8762 { 8763 ARMCPU *cpu = ARM_CPU(cs); 8764 CPUARMState *env = &cpu->env; 8765 MemTxAttrs attrs = {}; 8766 MemTxResult result = MEMTX_OK; 8767 AddressSpace *as; 8768 uint32_t data; 8769 8770 attrs.secure = is_secure; 8771 as = arm_addressspace(cs, attrs); 8772 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 8773 if (fi->s1ptw) { 8774 return 0; 8775 } 8776 if (regime_translation_big_endian(env, mmu_idx)) { 8777 data = address_space_ldl_be(as, addr, attrs, &result); 8778 } else { 8779 data = address_space_ldl_le(as, addr, attrs, &result); 8780 } 8781 if (result == MEMTX_OK) { 8782 return data; 8783 } 8784 fi->type = ARMFault_SyncExternalOnWalk; 8785 fi->ea = arm_extabort_type(result); 8786 return 0; 8787 } 8788 8789 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure, 8790 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 8791 { 8792 ARMCPU *cpu = ARM_CPU(cs); 8793 CPUARMState *env = &cpu->env; 8794 MemTxAttrs attrs = {}; 8795 MemTxResult result = MEMTX_OK; 8796 AddressSpace *as; 8797 uint64_t data; 8798 8799 attrs.secure = is_secure; 8800 as = arm_addressspace(cs, attrs); 8801 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 8802 if (fi->s1ptw) { 8803 return 0; 8804 } 8805 if (regime_translation_big_endian(env, mmu_idx)) { 8806 data = address_space_ldq_be(as, addr, attrs, &result); 8807 } else { 8808 data = address_space_ldq_le(as, addr, attrs, &result); 8809 } 8810 if (result == MEMTX_OK) { 8811 return data; 8812 } 8813 fi->type = ARMFault_SyncExternalOnWalk; 8814 fi->ea = arm_extabort_type(result); 8815 return 0; 8816 } 8817 8818 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address, 8819 MMUAccessType access_type, ARMMMUIdx mmu_idx, 8820 hwaddr *phys_ptr, int *prot, 8821 target_ulong *page_size, 8822 ARMMMUFaultInfo *fi) 8823 { 8824 CPUState *cs = CPU(arm_env_get_cpu(env)); 8825 int level = 1; 8826 uint32_t table; 8827 uint32_t desc; 8828 int type; 8829 int ap; 8830 int domain = 0; 8831 int domain_prot; 8832 hwaddr phys_addr; 8833 uint32_t dacr; 8834 8835 /* Pagetable walk. */ 8836 /* Lookup l1 descriptor. */ 8837 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 8838 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 8839 fi->type = ARMFault_Translation; 8840 goto do_fault; 8841 } 8842 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 8843 mmu_idx, fi); 8844 if (fi->type != ARMFault_None) { 8845 goto do_fault; 8846 } 8847 type = (desc & 3); 8848 domain = (desc >> 5) & 0x0f; 8849 if (regime_el(env, mmu_idx) == 1) { 8850 dacr = env->cp15.dacr_ns; 8851 } else { 8852 dacr = env->cp15.dacr_s; 8853 } 8854 domain_prot = (dacr >> (domain * 2)) & 3; 8855 if (type == 0) { 8856 /* Section translation fault. */ 8857 fi->type = ARMFault_Translation; 8858 goto do_fault; 8859 } 8860 if (type != 2) { 8861 level = 2; 8862 } 8863 if (domain_prot == 0 || domain_prot == 2) { 8864 fi->type = ARMFault_Domain; 8865 goto do_fault; 8866 } 8867 if (type == 2) { 8868 /* 1Mb section. */ 8869 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 8870 ap = (desc >> 10) & 3; 8871 *page_size = 1024 * 1024; 8872 } else { 8873 /* Lookup l2 entry. */ 8874 if (type == 1) { 8875 /* Coarse pagetable. */ 8876 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 8877 } else { 8878 /* Fine pagetable. */ 8879 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); 8880 } 8881 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 8882 mmu_idx, fi); 8883 if (fi->type != ARMFault_None) { 8884 goto do_fault; 8885 } 8886 switch (desc & 3) { 8887 case 0: /* Page translation fault. */ 8888 fi->type = ARMFault_Translation; 8889 goto do_fault; 8890 case 1: /* 64k page. */ 8891 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 8892 ap = (desc >> (4 + ((address >> 13) & 6))) & 3; 8893 *page_size = 0x10000; 8894 break; 8895 case 2: /* 4k page. */ 8896 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 8897 ap = (desc >> (4 + ((address >> 9) & 6))) & 3; 8898 *page_size = 0x1000; 8899 break; 8900 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */ 8901 if (type == 1) { 8902 /* ARMv6/XScale extended small page format */ 8903 if (arm_feature(env, ARM_FEATURE_XSCALE) 8904 || arm_feature(env, ARM_FEATURE_V6)) { 8905 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 8906 *page_size = 0x1000; 8907 } else { 8908 /* UNPREDICTABLE in ARMv5; we choose to take a 8909 * page translation fault. 8910 */ 8911 fi->type = ARMFault_Translation; 8912 goto do_fault; 8913 } 8914 } else { 8915 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); 8916 *page_size = 0x400; 8917 } 8918 ap = (desc >> 4) & 3; 8919 break; 8920 default: 8921 /* Never happens, but compiler isn't smart enough to tell. */ 8922 abort(); 8923 } 8924 } 8925 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 8926 *prot |= *prot ? PAGE_EXEC : 0; 8927 if (!(*prot & (1 << access_type))) { 8928 /* Access permission fault. */ 8929 fi->type = ARMFault_Permission; 8930 goto do_fault; 8931 } 8932 *phys_ptr = phys_addr; 8933 return false; 8934 do_fault: 8935 fi->domain = domain; 8936 fi->level = level; 8937 return true; 8938 } 8939 8940 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address, 8941 MMUAccessType access_type, ARMMMUIdx mmu_idx, 8942 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 8943 target_ulong *page_size, ARMMMUFaultInfo *fi) 8944 { 8945 CPUState *cs = CPU(arm_env_get_cpu(env)); 8946 int level = 1; 8947 uint32_t table; 8948 uint32_t desc; 8949 uint32_t xn; 8950 uint32_t pxn = 0; 8951 int type; 8952 int ap; 8953 int domain = 0; 8954 int domain_prot; 8955 hwaddr phys_addr; 8956 uint32_t dacr; 8957 bool ns; 8958 8959 /* Pagetable walk. */ 8960 /* Lookup l1 descriptor. */ 8961 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 8962 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 8963 fi->type = ARMFault_Translation; 8964 goto do_fault; 8965 } 8966 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 8967 mmu_idx, fi); 8968 if (fi->type != ARMFault_None) { 8969 goto do_fault; 8970 } 8971 type = (desc & 3); 8972 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) { 8973 /* Section translation fault, or attempt to use the encoding 8974 * which is Reserved on implementations without PXN. 8975 */ 8976 fi->type = ARMFault_Translation; 8977 goto do_fault; 8978 } 8979 if ((type == 1) || !(desc & (1 << 18))) { 8980 /* Page or Section. */ 8981 domain = (desc >> 5) & 0x0f; 8982 } 8983 if (regime_el(env, mmu_idx) == 1) { 8984 dacr = env->cp15.dacr_ns; 8985 } else { 8986 dacr = env->cp15.dacr_s; 8987 } 8988 if (type == 1) { 8989 level = 2; 8990 } 8991 domain_prot = (dacr >> (domain * 2)) & 3; 8992 if (domain_prot == 0 || domain_prot == 2) { 8993 /* Section or Page domain fault */ 8994 fi->type = ARMFault_Domain; 8995 goto do_fault; 8996 } 8997 if (type != 1) { 8998 if (desc & (1 << 18)) { 8999 /* Supersection. */ 9000 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); 9001 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32; 9002 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36; 9003 *page_size = 0x1000000; 9004 } else { 9005 /* Section. */ 9006 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 9007 *page_size = 0x100000; 9008 } 9009 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); 9010 xn = desc & (1 << 4); 9011 pxn = desc & 1; 9012 ns = extract32(desc, 19, 1); 9013 } else { 9014 if (arm_feature(env, ARM_FEATURE_PXN)) { 9015 pxn = (desc >> 2) & 1; 9016 } 9017 ns = extract32(desc, 3, 1); 9018 /* Lookup l2 entry. */ 9019 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 9020 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 9021 mmu_idx, fi); 9022 if (fi->type != ARMFault_None) { 9023 goto do_fault; 9024 } 9025 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); 9026 switch (desc & 3) { 9027 case 0: /* Page translation fault. */ 9028 fi->type = ARMFault_Translation; 9029 goto do_fault; 9030 case 1: /* 64k page. */ 9031 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 9032 xn = desc & (1 << 15); 9033 *page_size = 0x10000; 9034 break; 9035 case 2: case 3: /* 4k page. */ 9036 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 9037 xn = desc & 1; 9038 *page_size = 0x1000; 9039 break; 9040 default: 9041 /* Never happens, but compiler isn't smart enough to tell. */ 9042 abort(); 9043 } 9044 } 9045 if (domain_prot == 3) { 9046 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 9047 } else { 9048 if (pxn && !regime_is_user(env, mmu_idx)) { 9049 xn = 1; 9050 } 9051 if (xn && access_type == MMU_INST_FETCH) { 9052 fi->type = ARMFault_Permission; 9053 goto do_fault; 9054 } 9055 9056 if (arm_feature(env, ARM_FEATURE_V6K) && 9057 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) { 9058 /* The simplified model uses AP[0] as an access control bit. */ 9059 if ((ap & 1) == 0) { 9060 /* Access flag fault. */ 9061 fi->type = ARMFault_AccessFlag; 9062 goto do_fault; 9063 } 9064 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1); 9065 } else { 9066 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 9067 } 9068 if (*prot && !xn) { 9069 *prot |= PAGE_EXEC; 9070 } 9071 if (!(*prot & (1 << access_type))) { 9072 /* Access permission fault. */ 9073 fi->type = ARMFault_Permission; 9074 goto do_fault; 9075 } 9076 } 9077 if (ns) { 9078 /* The NS bit will (as required by the architecture) have no effect if 9079 * the CPU doesn't support TZ or this is a non-secure translation 9080 * regime, because the attribute will already be non-secure. 9081 */ 9082 attrs->secure = false; 9083 } 9084 *phys_ptr = phys_addr; 9085 return false; 9086 do_fault: 9087 fi->domain = domain; 9088 fi->level = level; 9089 return true; 9090 } 9091 9092 /* 9093 * check_s2_mmu_setup 9094 * @cpu: ARMCPU 9095 * @is_aa64: True if the translation regime is in AArch64 state 9096 * @startlevel: Suggested starting level 9097 * @inputsize: Bitsize of IPAs 9098 * @stride: Page-table stride (See the ARM ARM) 9099 * 9100 * Returns true if the suggested S2 translation parameters are OK and 9101 * false otherwise. 9102 */ 9103 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level, 9104 int inputsize, int stride) 9105 { 9106 const int grainsize = stride + 3; 9107 int startsizecheck; 9108 9109 /* Negative levels are never allowed. */ 9110 if (level < 0) { 9111 return false; 9112 } 9113 9114 startsizecheck = inputsize - ((3 - level) * stride + grainsize); 9115 if (startsizecheck < 1 || startsizecheck > stride + 4) { 9116 return false; 9117 } 9118 9119 if (is_aa64) { 9120 CPUARMState *env = &cpu->env; 9121 unsigned int pamax = arm_pamax(cpu); 9122 9123 switch (stride) { 9124 case 13: /* 64KB Pages. */ 9125 if (level == 0 || (level == 1 && pamax <= 42)) { 9126 return false; 9127 } 9128 break; 9129 case 11: /* 16KB Pages. */ 9130 if (level == 0 || (level == 1 && pamax <= 40)) { 9131 return false; 9132 } 9133 break; 9134 case 9: /* 4KB Pages. */ 9135 if (level == 0 && pamax <= 42) { 9136 return false; 9137 } 9138 break; 9139 default: 9140 g_assert_not_reached(); 9141 } 9142 9143 /* Inputsize checks. */ 9144 if (inputsize > pamax && 9145 (arm_el_is_aa64(env, 1) || inputsize > 40)) { 9146 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */ 9147 return false; 9148 } 9149 } else { 9150 /* AArch32 only supports 4KB pages. Assert on that. */ 9151 assert(stride == 9); 9152 9153 if (level == 0) { 9154 return false; 9155 } 9156 } 9157 return true; 9158 } 9159 9160 /* Translate from the 4-bit stage 2 representation of 9161 * memory attributes (without cache-allocation hints) to 9162 * the 8-bit representation of the stage 1 MAIR registers 9163 * (which includes allocation hints). 9164 * 9165 * ref: shared/translation/attrs/S2AttrDecode() 9166 * .../S2ConvertAttrsHints() 9167 */ 9168 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs) 9169 { 9170 uint8_t hiattr = extract32(s2attrs, 2, 2); 9171 uint8_t loattr = extract32(s2attrs, 0, 2); 9172 uint8_t hihint = 0, lohint = 0; 9173 9174 if (hiattr != 0) { /* normal memory */ 9175 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */ 9176 hiattr = loattr = 1; /* non-cacheable */ 9177 } else { 9178 if (hiattr != 1) { /* Write-through or write-back */ 9179 hihint = 3; /* RW allocate */ 9180 } 9181 if (loattr != 1) { /* Write-through or write-back */ 9182 lohint = 3; /* RW allocate */ 9183 } 9184 } 9185 } 9186 9187 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint; 9188 } 9189 9190 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, 9191 MMUAccessType access_type, ARMMMUIdx mmu_idx, 9192 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 9193 target_ulong *page_size_ptr, 9194 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 9195 { 9196 ARMCPU *cpu = arm_env_get_cpu(env); 9197 CPUState *cs = CPU(cpu); 9198 /* Read an LPAE long-descriptor translation table. */ 9199 ARMFaultType fault_type = ARMFault_Translation; 9200 uint32_t level; 9201 uint32_t epd = 0; 9202 int32_t t0sz, t1sz; 9203 uint32_t tg; 9204 uint64_t ttbr; 9205 int ttbr_select; 9206 hwaddr descaddr, indexmask, indexmask_grainsize; 9207 uint32_t tableattrs; 9208 target_ulong page_size; 9209 uint32_t attrs; 9210 int32_t stride = 9; 9211 int32_t addrsize; 9212 int inputsize; 9213 int32_t tbi = 0; 9214 TCR *tcr = regime_tcr(env, mmu_idx); 9215 int ap, ns, xn, pxn; 9216 uint32_t el = regime_el(env, mmu_idx); 9217 bool ttbr1_valid = true; 9218 uint64_t descaddrmask; 9219 bool aarch64 = arm_el_is_aa64(env, el); 9220 9221 /* TODO: 9222 * This code does not handle the different format TCR for VTCR_EL2. 9223 * This code also does not support shareability levels. 9224 * Attribute and permission bit handling should also be checked when adding 9225 * support for those page table walks. 9226 */ 9227 if (aarch64) { 9228 level = 0; 9229 addrsize = 64; 9230 if (el > 1) { 9231 if (mmu_idx != ARMMMUIdx_S2NS) { 9232 tbi = extract64(tcr->raw_tcr, 20, 1); 9233 } 9234 } else { 9235 if (extract64(address, 55, 1)) { 9236 tbi = extract64(tcr->raw_tcr, 38, 1); 9237 } else { 9238 tbi = extract64(tcr->raw_tcr, 37, 1); 9239 } 9240 } 9241 tbi *= 8; 9242 9243 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it 9244 * invalid. 9245 */ 9246 if (el > 1) { 9247 ttbr1_valid = false; 9248 } 9249 } else { 9250 level = 1; 9251 addrsize = 32; 9252 /* There is no TTBR1 for EL2 */ 9253 if (el == 2) { 9254 ttbr1_valid = false; 9255 } 9256 } 9257 9258 /* Determine whether this address is in the region controlled by 9259 * TTBR0 or TTBR1 (or if it is in neither region and should fault). 9260 * This is a Non-secure PL0/1 stage 1 translation, so controlled by 9261 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32: 9262 */ 9263 if (aarch64) { 9264 /* AArch64 translation. */ 9265 t0sz = extract32(tcr->raw_tcr, 0, 6); 9266 t0sz = MIN(t0sz, 39); 9267 t0sz = MAX(t0sz, 16); 9268 } else if (mmu_idx != ARMMMUIdx_S2NS) { 9269 /* AArch32 stage 1 translation. */ 9270 t0sz = extract32(tcr->raw_tcr, 0, 3); 9271 } else { 9272 /* AArch32 stage 2 translation. */ 9273 bool sext = extract32(tcr->raw_tcr, 4, 1); 9274 bool sign = extract32(tcr->raw_tcr, 3, 1); 9275 /* Address size is 40-bit for a stage 2 translation, 9276 * and t0sz can be negative (from -8 to 7), 9277 * so we need to adjust it to use the TTBR selecting logic below. 9278 */ 9279 addrsize = 40; 9280 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8; 9281 9282 /* If the sign-extend bit is not the same as t0sz[3], the result 9283 * is unpredictable. Flag this as a guest error. */ 9284 if (sign != sext) { 9285 qemu_log_mask(LOG_GUEST_ERROR, 9286 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n"); 9287 } 9288 } 9289 t1sz = extract32(tcr->raw_tcr, 16, 6); 9290 if (aarch64) { 9291 t1sz = MIN(t1sz, 39); 9292 t1sz = MAX(t1sz, 16); 9293 } 9294 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) { 9295 /* there is a ttbr0 region and we are in it (high bits all zero) */ 9296 ttbr_select = 0; 9297 } else if (ttbr1_valid && t1sz && 9298 !extract64(~address, addrsize - t1sz, t1sz - tbi)) { 9299 /* there is a ttbr1 region and we are in it (high bits all one) */ 9300 ttbr_select = 1; 9301 } else if (!t0sz) { 9302 /* ttbr0 region is "everything not in the ttbr1 region" */ 9303 ttbr_select = 0; 9304 } else if (!t1sz && ttbr1_valid) { 9305 /* ttbr1 region is "everything not in the ttbr0 region" */ 9306 ttbr_select = 1; 9307 } else { 9308 /* in the gap between the two regions, this is a Translation fault */ 9309 fault_type = ARMFault_Translation; 9310 goto do_fault; 9311 } 9312 9313 /* Note that QEMU ignores shareability and cacheability attributes, 9314 * so we don't need to do anything with the SH, ORGN, IRGN fields 9315 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the 9316 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently 9317 * implement any ASID-like capability so we can ignore it (instead 9318 * we will always flush the TLB any time the ASID is changed). 9319 */ 9320 if (ttbr_select == 0) { 9321 ttbr = regime_ttbr(env, mmu_idx, 0); 9322 if (el < 2) { 9323 epd = extract32(tcr->raw_tcr, 7, 1); 9324 } 9325 inputsize = addrsize - t0sz; 9326 9327 tg = extract32(tcr->raw_tcr, 14, 2); 9328 if (tg == 1) { /* 64KB pages */ 9329 stride = 13; 9330 } 9331 if (tg == 2) { /* 16KB pages */ 9332 stride = 11; 9333 } 9334 } else { 9335 /* We should only be here if TTBR1 is valid */ 9336 assert(ttbr1_valid); 9337 9338 ttbr = regime_ttbr(env, mmu_idx, 1); 9339 epd = extract32(tcr->raw_tcr, 23, 1); 9340 inputsize = addrsize - t1sz; 9341 9342 tg = extract32(tcr->raw_tcr, 30, 2); 9343 if (tg == 3) { /* 64KB pages */ 9344 stride = 13; 9345 } 9346 if (tg == 1) { /* 16KB pages */ 9347 stride = 11; 9348 } 9349 } 9350 9351 /* Here we should have set up all the parameters for the translation: 9352 * inputsize, ttbr, epd, stride, tbi 9353 */ 9354 9355 if (epd) { 9356 /* Translation table walk disabled => Translation fault on TLB miss 9357 * Note: This is always 0 on 64-bit EL2 and EL3. 9358 */ 9359 goto do_fault; 9360 } 9361 9362 if (mmu_idx != ARMMMUIdx_S2NS) { 9363 /* The starting level depends on the virtual address size (which can 9364 * be up to 48 bits) and the translation granule size. It indicates 9365 * the number of strides (stride bits at a time) needed to 9366 * consume the bits of the input address. In the pseudocode this is: 9367 * level = 4 - RoundUp((inputsize - grainsize) / stride) 9368 * where their 'inputsize' is our 'inputsize', 'grainsize' is 9369 * our 'stride + 3' and 'stride' is our 'stride'. 9370 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying: 9371 * = 4 - (inputsize - stride - 3 + stride - 1) / stride 9372 * = 4 - (inputsize - 4) / stride; 9373 */ 9374 level = 4 - (inputsize - 4) / stride; 9375 } else { 9376 /* For stage 2 translations the starting level is specified by the 9377 * VTCR_EL2.SL0 field (whose interpretation depends on the page size) 9378 */ 9379 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2); 9380 uint32_t startlevel; 9381 bool ok; 9382 9383 if (!aarch64 || stride == 9) { 9384 /* AArch32 or 4KB pages */ 9385 startlevel = 2 - sl0; 9386 } else { 9387 /* 16KB or 64KB pages */ 9388 startlevel = 3 - sl0; 9389 } 9390 9391 /* Check that the starting level is valid. */ 9392 ok = check_s2_mmu_setup(cpu, aarch64, startlevel, 9393 inputsize, stride); 9394 if (!ok) { 9395 fault_type = ARMFault_Translation; 9396 goto do_fault; 9397 } 9398 level = startlevel; 9399 } 9400 9401 indexmask_grainsize = (1ULL << (stride + 3)) - 1; 9402 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1; 9403 9404 /* Now we can extract the actual base address from the TTBR */ 9405 descaddr = extract64(ttbr, 0, 48); 9406 descaddr &= ~indexmask; 9407 9408 /* The address field in the descriptor goes up to bit 39 for ARMv7 9409 * but up to bit 47 for ARMv8, but we use the descaddrmask 9410 * up to bit 39 for AArch32, because we don't need other bits in that case 9411 * to construct next descriptor address (anyway they should be all zeroes). 9412 */ 9413 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) & 9414 ~indexmask_grainsize; 9415 9416 /* Secure accesses start with the page table in secure memory and 9417 * can be downgraded to non-secure at any step. Non-secure accesses 9418 * remain non-secure. We implement this by just ORing in the NSTable/NS 9419 * bits at each step. 9420 */ 9421 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4); 9422 for (;;) { 9423 uint64_t descriptor; 9424 bool nstable; 9425 9426 descaddr |= (address >> (stride * (4 - level))) & indexmask; 9427 descaddr &= ~7ULL; 9428 nstable = extract32(tableattrs, 4, 1); 9429 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi); 9430 if (fi->type != ARMFault_None) { 9431 goto do_fault; 9432 } 9433 9434 if (!(descriptor & 1) || 9435 (!(descriptor & 2) && (level == 3))) { 9436 /* Invalid, or the Reserved level 3 encoding */ 9437 goto do_fault; 9438 } 9439 descaddr = descriptor & descaddrmask; 9440 9441 if ((descriptor & 2) && (level < 3)) { 9442 /* Table entry. The top five bits are attributes which may 9443 * propagate down through lower levels of the table (and 9444 * which are all arranged so that 0 means "no effect", so 9445 * we can gather them up by ORing in the bits at each level). 9446 */ 9447 tableattrs |= extract64(descriptor, 59, 5); 9448 level++; 9449 indexmask = indexmask_grainsize; 9450 continue; 9451 } 9452 /* Block entry at level 1 or 2, or page entry at level 3. 9453 * These are basically the same thing, although the number 9454 * of bits we pull in from the vaddr varies. 9455 */ 9456 page_size = (1ULL << ((stride * (4 - level)) + 3)); 9457 descaddr |= (address & (page_size - 1)); 9458 /* Extract attributes from the descriptor */ 9459 attrs = extract64(descriptor, 2, 10) 9460 | (extract64(descriptor, 52, 12) << 10); 9461 9462 if (mmu_idx == ARMMMUIdx_S2NS) { 9463 /* Stage 2 table descriptors do not include any attribute fields */ 9464 break; 9465 } 9466 /* Merge in attributes from table descriptors */ 9467 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */ 9468 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */ 9469 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1 9470 * means "force PL1 access only", which means forcing AP[1] to 0. 9471 */ 9472 if (extract32(tableattrs, 2, 1)) { 9473 attrs &= ~(1 << 4); 9474 } 9475 attrs |= nstable << 3; /* NS */ 9476 break; 9477 } 9478 /* Here descaddr is the final physical address, and attributes 9479 * are all in attrs. 9480 */ 9481 fault_type = ARMFault_AccessFlag; 9482 if ((attrs & (1 << 8)) == 0) { 9483 /* Access flag */ 9484 goto do_fault; 9485 } 9486 9487 ap = extract32(attrs, 4, 2); 9488 xn = extract32(attrs, 12, 1); 9489 9490 if (mmu_idx == ARMMMUIdx_S2NS) { 9491 ns = true; 9492 *prot = get_S2prot(env, ap, xn); 9493 } else { 9494 ns = extract32(attrs, 3, 1); 9495 pxn = extract32(attrs, 11, 1); 9496 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn); 9497 } 9498 9499 fault_type = ARMFault_Permission; 9500 if (!(*prot & (1 << access_type))) { 9501 goto do_fault; 9502 } 9503 9504 if (ns) { 9505 /* The NS bit will (as required by the architecture) have no effect if 9506 * the CPU doesn't support TZ or this is a non-secure translation 9507 * regime, because the attribute will already be non-secure. 9508 */ 9509 txattrs->secure = false; 9510 } 9511 9512 if (cacheattrs != NULL) { 9513 if (mmu_idx == ARMMMUIdx_S2NS) { 9514 cacheattrs->attrs = convert_stage2_attrs(env, 9515 extract32(attrs, 0, 4)); 9516 } else { 9517 /* Index into MAIR registers for cache attributes */ 9518 uint8_t attrindx = extract32(attrs, 0, 3); 9519 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)]; 9520 assert(attrindx <= 7); 9521 cacheattrs->attrs = extract64(mair, attrindx * 8, 8); 9522 } 9523 cacheattrs->shareability = extract32(attrs, 6, 2); 9524 } 9525 9526 *phys_ptr = descaddr; 9527 *page_size_ptr = page_size; 9528 return false; 9529 9530 do_fault: 9531 fi->type = fault_type; 9532 fi->level = level; 9533 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */ 9534 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS); 9535 return true; 9536 } 9537 9538 static inline void get_phys_addr_pmsav7_default(CPUARMState *env, 9539 ARMMMUIdx mmu_idx, 9540 int32_t address, int *prot) 9541 { 9542 if (!arm_feature(env, ARM_FEATURE_M)) { 9543 *prot = PAGE_READ | PAGE_WRITE; 9544 switch (address) { 9545 case 0xF0000000 ... 0xFFFFFFFF: 9546 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { 9547 /* hivecs execing is ok */ 9548 *prot |= PAGE_EXEC; 9549 } 9550 break; 9551 case 0x00000000 ... 0x7FFFFFFF: 9552 *prot |= PAGE_EXEC; 9553 break; 9554 } 9555 } else { 9556 /* Default system address map for M profile cores. 9557 * The architecture specifies which regions are execute-never; 9558 * at the MPU level no other checks are defined. 9559 */ 9560 switch (address) { 9561 case 0x00000000 ... 0x1fffffff: /* ROM */ 9562 case 0x20000000 ... 0x3fffffff: /* SRAM */ 9563 case 0x60000000 ... 0x7fffffff: /* RAM */ 9564 case 0x80000000 ... 0x9fffffff: /* RAM */ 9565 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 9566 break; 9567 case 0x40000000 ... 0x5fffffff: /* Peripheral */ 9568 case 0xa0000000 ... 0xbfffffff: /* Device */ 9569 case 0xc0000000 ... 0xdfffffff: /* Device */ 9570 case 0xe0000000 ... 0xffffffff: /* System */ 9571 *prot = PAGE_READ | PAGE_WRITE; 9572 break; 9573 default: 9574 g_assert_not_reached(); 9575 } 9576 } 9577 } 9578 9579 static bool pmsav7_use_background_region(ARMCPU *cpu, 9580 ARMMMUIdx mmu_idx, bool is_user) 9581 { 9582 /* Return true if we should use the default memory map as a 9583 * "background" region if there are no hits against any MPU regions. 9584 */ 9585 CPUARMState *env = &cpu->env; 9586 9587 if (is_user) { 9588 return false; 9589 } 9590 9591 if (arm_feature(env, ARM_FEATURE_M)) { 9592 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] 9593 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK; 9594 } else { 9595 return regime_sctlr(env, mmu_idx) & SCTLR_BR; 9596 } 9597 } 9598 9599 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address) 9600 { 9601 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */ 9602 return arm_feature(env, ARM_FEATURE_M) && 9603 extract32(address, 20, 12) == 0xe00; 9604 } 9605 9606 static inline bool m_is_system_region(CPUARMState *env, uint32_t address) 9607 { 9608 /* True if address is in the M profile system region 9609 * 0xe0000000 - 0xffffffff 9610 */ 9611 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7; 9612 } 9613 9614 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address, 9615 MMUAccessType access_type, ARMMMUIdx mmu_idx, 9616 hwaddr *phys_ptr, int *prot, 9617 target_ulong *page_size, 9618 ARMMMUFaultInfo *fi) 9619 { 9620 ARMCPU *cpu = arm_env_get_cpu(env); 9621 int n; 9622 bool is_user = regime_is_user(env, mmu_idx); 9623 9624 *phys_ptr = address; 9625 *page_size = TARGET_PAGE_SIZE; 9626 *prot = 0; 9627 9628 if (regime_translation_disabled(env, mmu_idx) || 9629 m_is_ppb_region(env, address)) { 9630 /* MPU disabled or M profile PPB access: use default memory map. 9631 * The other case which uses the default memory map in the 9632 * v7M ARM ARM pseudocode is exception vector reads from the vector 9633 * table. In QEMU those accesses are done in arm_v7m_load_vector(), 9634 * which always does a direct read using address_space_ldl(), rather 9635 * than going via this function, so we don't need to check that here. 9636 */ 9637 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 9638 } else { /* MPU enabled */ 9639 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 9640 /* region search */ 9641 uint32_t base = env->pmsav7.drbar[n]; 9642 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5); 9643 uint32_t rmask; 9644 bool srdis = false; 9645 9646 if (!(env->pmsav7.drsr[n] & 0x1)) { 9647 continue; 9648 } 9649 9650 if (!rsize) { 9651 qemu_log_mask(LOG_GUEST_ERROR, 9652 "DRSR[%d]: Rsize field cannot be 0\n", n); 9653 continue; 9654 } 9655 rsize++; 9656 rmask = (1ull << rsize) - 1; 9657 9658 if (base & rmask) { 9659 qemu_log_mask(LOG_GUEST_ERROR, 9660 "DRBAR[%d]: 0x%" PRIx32 " misaligned " 9661 "to DRSR region size, mask = 0x%" PRIx32 "\n", 9662 n, base, rmask); 9663 continue; 9664 } 9665 9666 if (address < base || address > base + rmask) { 9667 /* 9668 * Address not in this region. We must check whether the 9669 * region covers addresses in the same page as our address. 9670 * In that case we must not report a size that covers the 9671 * whole page for a subsequent hit against a different MPU 9672 * region or the background region, because it would result in 9673 * incorrect TLB hits for subsequent accesses to addresses that 9674 * are in this MPU region. 9675 */ 9676 if (ranges_overlap(base, rmask, 9677 address & TARGET_PAGE_MASK, 9678 TARGET_PAGE_SIZE)) { 9679 *page_size = 1; 9680 } 9681 continue; 9682 } 9683 9684 /* Region matched */ 9685 9686 if (rsize >= 8) { /* no subregions for regions < 256 bytes */ 9687 int i, snd; 9688 uint32_t srdis_mask; 9689 9690 rsize -= 3; /* sub region size (power of 2) */ 9691 snd = ((address - base) >> rsize) & 0x7; 9692 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1); 9693 9694 srdis_mask = srdis ? 0x3 : 0x0; 9695 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) { 9696 /* This will check in groups of 2, 4 and then 8, whether 9697 * the subregion bits are consistent. rsize is incremented 9698 * back up to give the region size, considering consistent 9699 * adjacent subregions as one region. Stop testing if rsize 9700 * is already big enough for an entire QEMU page. 9701 */ 9702 int snd_rounded = snd & ~(i - 1); 9703 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n], 9704 snd_rounded + 8, i); 9705 if (srdis_mask ^ srdis_multi) { 9706 break; 9707 } 9708 srdis_mask = (srdis_mask << i) | srdis_mask; 9709 rsize++; 9710 } 9711 } 9712 if (srdis) { 9713 continue; 9714 } 9715 if (rsize < TARGET_PAGE_BITS) { 9716 *page_size = 1 << rsize; 9717 } 9718 break; 9719 } 9720 9721 if (n == -1) { /* no hits */ 9722 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 9723 /* background fault */ 9724 fi->type = ARMFault_Background; 9725 return true; 9726 } 9727 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 9728 } else { /* a MPU hit! */ 9729 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3); 9730 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1); 9731 9732 if (m_is_system_region(env, address)) { 9733 /* System space is always execute never */ 9734 xn = 1; 9735 } 9736 9737 if (is_user) { /* User mode AP bit decoding */ 9738 switch (ap) { 9739 case 0: 9740 case 1: 9741 case 5: 9742 break; /* no access */ 9743 case 3: 9744 *prot |= PAGE_WRITE; 9745 /* fall through */ 9746 case 2: 9747 case 6: 9748 *prot |= PAGE_READ | PAGE_EXEC; 9749 break; 9750 case 7: 9751 /* for v7M, same as 6; for R profile a reserved value */ 9752 if (arm_feature(env, ARM_FEATURE_M)) { 9753 *prot |= PAGE_READ | PAGE_EXEC; 9754 break; 9755 } 9756 /* fall through */ 9757 default: 9758 qemu_log_mask(LOG_GUEST_ERROR, 9759 "DRACR[%d]: Bad value for AP bits: 0x%" 9760 PRIx32 "\n", n, ap); 9761 } 9762 } else { /* Priv. mode AP bits decoding */ 9763 switch (ap) { 9764 case 0: 9765 break; /* no access */ 9766 case 1: 9767 case 2: 9768 case 3: 9769 *prot |= PAGE_WRITE; 9770 /* fall through */ 9771 case 5: 9772 case 6: 9773 *prot |= PAGE_READ | PAGE_EXEC; 9774 break; 9775 case 7: 9776 /* for v7M, same as 6; for R profile a reserved value */ 9777 if (arm_feature(env, ARM_FEATURE_M)) { 9778 *prot |= PAGE_READ | PAGE_EXEC; 9779 break; 9780 } 9781 /* fall through */ 9782 default: 9783 qemu_log_mask(LOG_GUEST_ERROR, 9784 "DRACR[%d]: Bad value for AP bits: 0x%" 9785 PRIx32 "\n", n, ap); 9786 } 9787 } 9788 9789 /* execute never */ 9790 if (xn) { 9791 *prot &= ~PAGE_EXEC; 9792 } 9793 } 9794 } 9795 9796 fi->type = ARMFault_Permission; 9797 fi->level = 1; 9798 return !(*prot & (1 << access_type)); 9799 } 9800 9801 static bool v8m_is_sau_exempt(CPUARMState *env, 9802 uint32_t address, MMUAccessType access_type) 9803 { 9804 /* The architecture specifies that certain address ranges are 9805 * exempt from v8M SAU/IDAU checks. 9806 */ 9807 return 9808 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) || 9809 (address >= 0xe0000000 && address <= 0xe0002fff) || 9810 (address >= 0xe000e000 && address <= 0xe000efff) || 9811 (address >= 0xe002e000 && address <= 0xe002efff) || 9812 (address >= 0xe0040000 && address <= 0xe0041fff) || 9813 (address >= 0xe00ff000 && address <= 0xe00fffff); 9814 } 9815 9816 static void v8m_security_lookup(CPUARMState *env, uint32_t address, 9817 MMUAccessType access_type, ARMMMUIdx mmu_idx, 9818 V8M_SAttributes *sattrs) 9819 { 9820 /* Look up the security attributes for this address. Compare the 9821 * pseudocode SecurityCheck() function. 9822 * We assume the caller has zero-initialized *sattrs. 9823 */ 9824 ARMCPU *cpu = arm_env_get_cpu(env); 9825 int r; 9826 bool idau_exempt = false, idau_ns = true, idau_nsc = true; 9827 int idau_region = IREGION_NOTVALID; 9828 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 9829 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 9830 9831 if (cpu->idau) { 9832 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau); 9833 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau); 9834 9835 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns, 9836 &idau_nsc); 9837 } 9838 9839 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) { 9840 /* 0xf0000000..0xffffffff is always S for insn fetches */ 9841 return; 9842 } 9843 9844 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) { 9845 sattrs->ns = !regime_is_secure(env, mmu_idx); 9846 return; 9847 } 9848 9849 if (idau_region != IREGION_NOTVALID) { 9850 sattrs->irvalid = true; 9851 sattrs->iregion = idau_region; 9852 } 9853 9854 switch (env->sau.ctrl & 3) { 9855 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */ 9856 break; 9857 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */ 9858 sattrs->ns = true; 9859 break; 9860 default: /* SAU.ENABLE == 1 */ 9861 for (r = 0; r < cpu->sau_sregion; r++) { 9862 if (env->sau.rlar[r] & 1) { 9863 uint32_t base = env->sau.rbar[r] & ~0x1f; 9864 uint32_t limit = env->sau.rlar[r] | 0x1f; 9865 9866 if (base <= address && limit >= address) { 9867 if (base > addr_page_base || limit < addr_page_limit) { 9868 sattrs->subpage = true; 9869 } 9870 if (sattrs->srvalid) { 9871 /* If we hit in more than one region then we must report 9872 * as Secure, not NS-Callable, with no valid region 9873 * number info. 9874 */ 9875 sattrs->ns = false; 9876 sattrs->nsc = false; 9877 sattrs->sregion = 0; 9878 sattrs->srvalid = false; 9879 break; 9880 } else { 9881 if (env->sau.rlar[r] & 2) { 9882 sattrs->nsc = true; 9883 } else { 9884 sattrs->ns = true; 9885 } 9886 sattrs->srvalid = true; 9887 sattrs->sregion = r; 9888 } 9889 } else { 9890 /* 9891 * Address not in this region. We must check whether the 9892 * region covers addresses in the same page as our address. 9893 * In that case we must not report a size that covers the 9894 * whole page for a subsequent hit against a different MPU 9895 * region or the background region, because it would result 9896 * in incorrect TLB hits for subsequent accesses to 9897 * addresses that are in this MPU region. 9898 */ 9899 if (limit >= base && 9900 ranges_overlap(base, limit - base + 1, 9901 addr_page_base, 9902 TARGET_PAGE_SIZE)) { 9903 sattrs->subpage = true; 9904 } 9905 } 9906 } 9907 } 9908 9909 /* The IDAU will override the SAU lookup results if it specifies 9910 * higher security than the SAU does. 9911 */ 9912 if (!idau_ns) { 9913 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) { 9914 sattrs->ns = false; 9915 sattrs->nsc = idau_nsc; 9916 } 9917 } 9918 break; 9919 } 9920 } 9921 9922 static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address, 9923 MMUAccessType access_type, ARMMMUIdx mmu_idx, 9924 hwaddr *phys_ptr, MemTxAttrs *txattrs, 9925 int *prot, bool *is_subpage, 9926 ARMMMUFaultInfo *fi, uint32_t *mregion) 9927 { 9928 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check 9929 * that a full phys-to-virt translation does). 9930 * mregion is (if not NULL) set to the region number which matched, 9931 * or -1 if no region number is returned (MPU off, address did not 9932 * hit a region, address hit in multiple regions). 9933 * We set is_subpage to true if the region hit doesn't cover the 9934 * entire TARGET_PAGE the address is within. 9935 */ 9936 ARMCPU *cpu = arm_env_get_cpu(env); 9937 bool is_user = regime_is_user(env, mmu_idx); 9938 uint32_t secure = regime_is_secure(env, mmu_idx); 9939 int n; 9940 int matchregion = -1; 9941 bool hit = false; 9942 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 9943 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 9944 9945 *is_subpage = false; 9946 *phys_ptr = address; 9947 *prot = 0; 9948 if (mregion) { 9949 *mregion = -1; 9950 } 9951 9952 /* Unlike the ARM ARM pseudocode, we don't need to check whether this 9953 * was an exception vector read from the vector table (which is always 9954 * done using the default system address map), because those accesses 9955 * are done in arm_v7m_load_vector(), which always does a direct 9956 * read using address_space_ldl(), rather than going via this function. 9957 */ 9958 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */ 9959 hit = true; 9960 } else if (m_is_ppb_region(env, address)) { 9961 hit = true; 9962 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 9963 hit = true; 9964 } else { 9965 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 9966 /* region search */ 9967 /* Note that the base address is bits [31:5] from the register 9968 * with bits [4:0] all zeroes, but the limit address is bits 9969 * [31:5] from the register with bits [4:0] all ones. 9970 */ 9971 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f; 9972 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f; 9973 9974 if (!(env->pmsav8.rlar[secure][n] & 0x1)) { 9975 /* Region disabled */ 9976 continue; 9977 } 9978 9979 if (address < base || address > limit) { 9980 /* 9981 * Address not in this region. We must check whether the 9982 * region covers addresses in the same page as our address. 9983 * In that case we must not report a size that covers the 9984 * whole page for a subsequent hit against a different MPU 9985 * region or the background region, because it would result in 9986 * incorrect TLB hits for subsequent accesses to addresses that 9987 * are in this MPU region. 9988 */ 9989 if (limit >= base && 9990 ranges_overlap(base, limit - base + 1, 9991 addr_page_base, 9992 TARGET_PAGE_SIZE)) { 9993 *is_subpage = true; 9994 } 9995 continue; 9996 } 9997 9998 if (base > addr_page_base || limit < addr_page_limit) { 9999 *is_subpage = true; 10000 } 10001 10002 if (hit) { 10003 /* Multiple regions match -- always a failure (unlike 10004 * PMSAv7 where highest-numbered-region wins) 10005 */ 10006 fi->type = ARMFault_Permission; 10007 fi->level = 1; 10008 return true; 10009 } 10010 10011 matchregion = n; 10012 hit = true; 10013 } 10014 } 10015 10016 if (!hit) { 10017 /* background fault */ 10018 fi->type = ARMFault_Background; 10019 return true; 10020 } 10021 10022 if (matchregion == -1) { 10023 /* hit using the background region */ 10024 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 10025 } else { 10026 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2); 10027 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1); 10028 10029 if (m_is_system_region(env, address)) { 10030 /* System space is always execute never */ 10031 xn = 1; 10032 } 10033 10034 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap); 10035 if (*prot && !xn) { 10036 *prot |= PAGE_EXEC; 10037 } 10038 /* We don't need to look the attribute up in the MAIR0/MAIR1 10039 * registers because that only tells us about cacheability. 10040 */ 10041 if (mregion) { 10042 *mregion = matchregion; 10043 } 10044 } 10045 10046 fi->type = ARMFault_Permission; 10047 fi->level = 1; 10048 return !(*prot & (1 << access_type)); 10049 } 10050 10051 10052 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address, 10053 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10054 hwaddr *phys_ptr, MemTxAttrs *txattrs, 10055 int *prot, target_ulong *page_size, 10056 ARMMMUFaultInfo *fi) 10057 { 10058 uint32_t secure = regime_is_secure(env, mmu_idx); 10059 V8M_SAttributes sattrs = {}; 10060 bool ret; 10061 bool mpu_is_subpage; 10062 10063 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 10064 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs); 10065 if (access_type == MMU_INST_FETCH) { 10066 /* Instruction fetches always use the MMU bank and the 10067 * transaction attribute determined by the fetch address, 10068 * regardless of CPU state. This is painful for QEMU 10069 * to handle, because it would mean we need to encode 10070 * into the mmu_idx not just the (user, negpri) information 10071 * for the current security state but also that for the 10072 * other security state, which would balloon the number 10073 * of mmu_idx values needed alarmingly. 10074 * Fortunately we can avoid this because it's not actually 10075 * possible to arbitrarily execute code from memory with 10076 * the wrong security attribute: it will always generate 10077 * an exception of some kind or another, apart from the 10078 * special case of an NS CPU executing an SG instruction 10079 * in S&NSC memory. So we always just fail the translation 10080 * here and sort things out in the exception handler 10081 * (including possibly emulating an SG instruction). 10082 */ 10083 if (sattrs.ns != !secure) { 10084 if (sattrs.nsc) { 10085 fi->type = ARMFault_QEMU_NSCExec; 10086 } else { 10087 fi->type = ARMFault_QEMU_SFault; 10088 } 10089 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 10090 *phys_ptr = address; 10091 *prot = 0; 10092 return true; 10093 } 10094 } else { 10095 /* For data accesses we always use the MMU bank indicated 10096 * by the current CPU state, but the security attributes 10097 * might downgrade a secure access to nonsecure. 10098 */ 10099 if (sattrs.ns) { 10100 txattrs->secure = false; 10101 } else if (!secure) { 10102 /* NS access to S memory must fault. 10103 * Architecturally we should first check whether the 10104 * MPU information for this address indicates that we 10105 * are doing an unaligned access to Device memory, which 10106 * should generate a UsageFault instead. QEMU does not 10107 * currently check for that kind of unaligned access though. 10108 * If we added it we would need to do so as a special case 10109 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt(). 10110 */ 10111 fi->type = ARMFault_QEMU_SFault; 10112 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 10113 *phys_ptr = address; 10114 *prot = 0; 10115 return true; 10116 } 10117 } 10118 } 10119 10120 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr, 10121 txattrs, prot, &mpu_is_subpage, fi, NULL); 10122 /* 10123 * TODO: this is a temporary hack to ignore the fact that the SAU region 10124 * is smaller than a page if this is an executable region. We never 10125 * supported small MPU regions, but we did (accidentally) allow small 10126 * SAU regions, and if we now made small SAU regions not be executable 10127 * then this would break previously working guest code. We can't 10128 * remove this until/unless we implement support for execution from 10129 * small regions. 10130 */ 10131 if (*prot & PAGE_EXEC) { 10132 sattrs.subpage = false; 10133 } 10134 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE; 10135 return ret; 10136 } 10137 10138 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address, 10139 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10140 hwaddr *phys_ptr, int *prot, 10141 ARMMMUFaultInfo *fi) 10142 { 10143 int n; 10144 uint32_t mask; 10145 uint32_t base; 10146 bool is_user = regime_is_user(env, mmu_idx); 10147 10148 if (regime_translation_disabled(env, mmu_idx)) { 10149 /* MPU disabled. */ 10150 *phys_ptr = address; 10151 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 10152 return false; 10153 } 10154 10155 *phys_ptr = address; 10156 for (n = 7; n >= 0; n--) { 10157 base = env->cp15.c6_region[n]; 10158 if ((base & 1) == 0) { 10159 continue; 10160 } 10161 mask = 1 << ((base >> 1) & 0x1f); 10162 /* Keep this shift separate from the above to avoid an 10163 (undefined) << 32. */ 10164 mask = (mask << 1) - 1; 10165 if (((base ^ address) & ~mask) == 0) { 10166 break; 10167 } 10168 } 10169 if (n < 0) { 10170 fi->type = ARMFault_Background; 10171 return true; 10172 } 10173 10174 if (access_type == MMU_INST_FETCH) { 10175 mask = env->cp15.pmsav5_insn_ap; 10176 } else { 10177 mask = env->cp15.pmsav5_data_ap; 10178 } 10179 mask = (mask >> (n * 4)) & 0xf; 10180 switch (mask) { 10181 case 0: 10182 fi->type = ARMFault_Permission; 10183 fi->level = 1; 10184 return true; 10185 case 1: 10186 if (is_user) { 10187 fi->type = ARMFault_Permission; 10188 fi->level = 1; 10189 return true; 10190 } 10191 *prot = PAGE_READ | PAGE_WRITE; 10192 break; 10193 case 2: 10194 *prot = PAGE_READ; 10195 if (!is_user) { 10196 *prot |= PAGE_WRITE; 10197 } 10198 break; 10199 case 3: 10200 *prot = PAGE_READ | PAGE_WRITE; 10201 break; 10202 case 5: 10203 if (is_user) { 10204 fi->type = ARMFault_Permission; 10205 fi->level = 1; 10206 return true; 10207 } 10208 *prot = PAGE_READ; 10209 break; 10210 case 6: 10211 *prot = PAGE_READ; 10212 break; 10213 default: 10214 /* Bad permission. */ 10215 fi->type = ARMFault_Permission; 10216 fi->level = 1; 10217 return true; 10218 } 10219 *prot |= PAGE_EXEC; 10220 return false; 10221 } 10222 10223 /* Combine either inner or outer cacheability attributes for normal 10224 * memory, according to table D4-42 and pseudocode procedure 10225 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM). 10226 * 10227 * NB: only stage 1 includes allocation hints (RW bits), leading to 10228 * some asymmetry. 10229 */ 10230 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2) 10231 { 10232 if (s1 == 4 || s2 == 4) { 10233 /* non-cacheable has precedence */ 10234 return 4; 10235 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) { 10236 /* stage 1 write-through takes precedence */ 10237 return s1; 10238 } else if (extract32(s2, 2, 2) == 2) { 10239 /* stage 2 write-through takes precedence, but the allocation hint 10240 * is still taken from stage 1 10241 */ 10242 return (2 << 2) | extract32(s1, 0, 2); 10243 } else { /* write-back */ 10244 return s1; 10245 } 10246 } 10247 10248 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4 10249 * and CombineS1S2Desc() 10250 * 10251 * @s1: Attributes from stage 1 walk 10252 * @s2: Attributes from stage 2 walk 10253 */ 10254 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2) 10255 { 10256 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4); 10257 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4); 10258 ARMCacheAttrs ret; 10259 10260 /* Combine shareability attributes (table D4-43) */ 10261 if (s1.shareability == 2 || s2.shareability == 2) { 10262 /* if either are outer-shareable, the result is outer-shareable */ 10263 ret.shareability = 2; 10264 } else if (s1.shareability == 3 || s2.shareability == 3) { 10265 /* if either are inner-shareable, the result is inner-shareable */ 10266 ret.shareability = 3; 10267 } else { 10268 /* both non-shareable */ 10269 ret.shareability = 0; 10270 } 10271 10272 /* Combine memory type and cacheability attributes */ 10273 if (s1hi == 0 || s2hi == 0) { 10274 /* Device has precedence over normal */ 10275 if (s1lo == 0 || s2lo == 0) { 10276 /* nGnRnE has precedence over anything */ 10277 ret.attrs = 0; 10278 } else if (s1lo == 4 || s2lo == 4) { 10279 /* non-Reordering has precedence over Reordering */ 10280 ret.attrs = 4; /* nGnRE */ 10281 } else if (s1lo == 8 || s2lo == 8) { 10282 /* non-Gathering has precedence over Gathering */ 10283 ret.attrs = 8; /* nGRE */ 10284 } else { 10285 ret.attrs = 0xc; /* GRE */ 10286 } 10287 10288 /* Any location for which the resultant memory type is any 10289 * type of Device memory is always treated as Outer Shareable. 10290 */ 10291 ret.shareability = 2; 10292 } else { /* Normal memory */ 10293 /* Outer/inner cacheability combine independently */ 10294 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4 10295 | combine_cacheattr_nibble(s1lo, s2lo); 10296 10297 if (ret.attrs == 0x44) { 10298 /* Any location for which the resultant memory type is Normal 10299 * Inner Non-cacheable, Outer Non-cacheable is always treated 10300 * as Outer Shareable. 10301 */ 10302 ret.shareability = 2; 10303 } 10304 } 10305 10306 return ret; 10307 } 10308 10309 10310 /* get_phys_addr - get the physical address for this virtual address 10311 * 10312 * Find the physical address corresponding to the given virtual address, 10313 * by doing a translation table walk on MMU based systems or using the 10314 * MPU state on MPU based systems. 10315 * 10316 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs, 10317 * prot and page_size may not be filled in, and the populated fsr value provides 10318 * information on why the translation aborted, in the format of a 10319 * DFSR/IFSR fault register, with the following caveats: 10320 * * we honour the short vs long DFSR format differences. 10321 * * the WnR bit is never set (the caller must do this). 10322 * * for PSMAv5 based systems we don't bother to return a full FSR format 10323 * value. 10324 * 10325 * @env: CPUARMState 10326 * @address: virtual address to get physical address for 10327 * @access_type: 0 for read, 1 for write, 2 for execute 10328 * @mmu_idx: MMU index indicating required translation regime 10329 * @phys_ptr: set to the physical address corresponding to the virtual address 10330 * @attrs: set to the memory transaction attributes to use 10331 * @prot: set to the permissions for the page containing phys_ptr 10332 * @page_size: set to the size of the page containing phys_ptr 10333 * @fi: set to fault info if the translation fails 10334 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes 10335 */ 10336 static bool get_phys_addr(CPUARMState *env, target_ulong address, 10337 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10338 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 10339 target_ulong *page_size, 10340 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 10341 { 10342 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 10343 /* Call ourselves recursively to do the stage 1 and then stage 2 10344 * translations. 10345 */ 10346 if (arm_feature(env, ARM_FEATURE_EL2)) { 10347 hwaddr ipa; 10348 int s2_prot; 10349 int ret; 10350 ARMCacheAttrs cacheattrs2 = {}; 10351 10352 ret = get_phys_addr(env, address, access_type, 10353 stage_1_mmu_idx(mmu_idx), &ipa, attrs, 10354 prot, page_size, fi, cacheattrs); 10355 10356 /* If S1 fails or S2 is disabled, return early. */ 10357 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) { 10358 *phys_ptr = ipa; 10359 return ret; 10360 } 10361 10362 /* S1 is done. Now do S2 translation. */ 10363 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS, 10364 phys_ptr, attrs, &s2_prot, 10365 page_size, fi, 10366 cacheattrs != NULL ? &cacheattrs2 : NULL); 10367 fi->s2addr = ipa; 10368 /* Combine the S1 and S2 perms. */ 10369 *prot &= s2_prot; 10370 10371 /* Combine the S1 and S2 cache attributes, if needed */ 10372 if (!ret && cacheattrs != NULL) { 10373 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2); 10374 } 10375 10376 return ret; 10377 } else { 10378 /* 10379 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1. 10380 */ 10381 mmu_idx = stage_1_mmu_idx(mmu_idx); 10382 } 10383 } 10384 10385 /* The page table entries may downgrade secure to non-secure, but 10386 * cannot upgrade an non-secure translation regime's attributes 10387 * to secure. 10388 */ 10389 attrs->secure = regime_is_secure(env, mmu_idx); 10390 attrs->user = regime_is_user(env, mmu_idx); 10391 10392 /* Fast Context Switch Extension. This doesn't exist at all in v8. 10393 * In v7 and earlier it affects all stage 1 translations. 10394 */ 10395 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS 10396 && !arm_feature(env, ARM_FEATURE_V8)) { 10397 if (regime_el(env, mmu_idx) == 3) { 10398 address += env->cp15.fcseidr_s; 10399 } else { 10400 address += env->cp15.fcseidr_ns; 10401 } 10402 } 10403 10404 if (arm_feature(env, ARM_FEATURE_PMSA)) { 10405 bool ret; 10406 *page_size = TARGET_PAGE_SIZE; 10407 10408 if (arm_feature(env, ARM_FEATURE_V8)) { 10409 /* PMSAv8 */ 10410 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx, 10411 phys_ptr, attrs, prot, page_size, fi); 10412 } else if (arm_feature(env, ARM_FEATURE_V7)) { 10413 /* PMSAv7 */ 10414 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx, 10415 phys_ptr, prot, page_size, fi); 10416 } else { 10417 /* Pre-v7 MPU */ 10418 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx, 10419 phys_ptr, prot, fi); 10420 } 10421 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32 10422 " mmu_idx %u -> %s (prot %c%c%c)\n", 10423 access_type == MMU_DATA_LOAD ? "reading" : 10424 (access_type == MMU_DATA_STORE ? "writing" : "execute"), 10425 (uint32_t)address, mmu_idx, 10426 ret ? "Miss" : "Hit", 10427 *prot & PAGE_READ ? 'r' : '-', 10428 *prot & PAGE_WRITE ? 'w' : '-', 10429 *prot & PAGE_EXEC ? 'x' : '-'); 10430 10431 return ret; 10432 } 10433 10434 /* Definitely a real MMU, not an MPU */ 10435 10436 if (regime_translation_disabled(env, mmu_idx)) { 10437 /* MMU disabled. */ 10438 *phys_ptr = address; 10439 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 10440 *page_size = TARGET_PAGE_SIZE; 10441 return 0; 10442 } 10443 10444 if (regime_using_lpae_format(env, mmu_idx)) { 10445 return get_phys_addr_lpae(env, address, access_type, mmu_idx, 10446 phys_ptr, attrs, prot, page_size, 10447 fi, cacheattrs); 10448 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) { 10449 return get_phys_addr_v6(env, address, access_type, mmu_idx, 10450 phys_ptr, attrs, prot, page_size, fi); 10451 } else { 10452 return get_phys_addr_v5(env, address, access_type, mmu_idx, 10453 phys_ptr, prot, page_size, fi); 10454 } 10455 } 10456 10457 /* Walk the page table and (if the mapping exists) add the page 10458 * to the TLB. Return false on success, or true on failure. Populate 10459 * fsr with ARM DFSR/IFSR fault register format value on failure. 10460 */ 10461 bool arm_tlb_fill(CPUState *cs, vaddr address, 10462 MMUAccessType access_type, int mmu_idx, 10463 ARMMMUFaultInfo *fi) 10464 { 10465 ARMCPU *cpu = ARM_CPU(cs); 10466 CPUARMState *env = &cpu->env; 10467 hwaddr phys_addr; 10468 target_ulong page_size; 10469 int prot; 10470 int ret; 10471 MemTxAttrs attrs = {}; 10472 10473 ret = get_phys_addr(env, address, access_type, 10474 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr, 10475 &attrs, &prot, &page_size, fi, NULL); 10476 if (!ret) { 10477 /* 10478 * Map a single [sub]page. Regions smaller than our declared 10479 * target page size are handled specially, so for those we 10480 * pass in the exact addresses. 10481 */ 10482 if (page_size >= TARGET_PAGE_SIZE) { 10483 phys_addr &= TARGET_PAGE_MASK; 10484 address &= TARGET_PAGE_MASK; 10485 } 10486 tlb_set_page_with_attrs(cs, address, phys_addr, attrs, 10487 prot, mmu_idx, page_size); 10488 return 0; 10489 } 10490 10491 return ret; 10492 } 10493 10494 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr, 10495 MemTxAttrs *attrs) 10496 { 10497 ARMCPU *cpu = ARM_CPU(cs); 10498 CPUARMState *env = &cpu->env; 10499 hwaddr phys_addr; 10500 target_ulong page_size; 10501 int prot; 10502 bool ret; 10503 ARMMMUFaultInfo fi = {}; 10504 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false)); 10505 10506 *attrs = (MemTxAttrs) {}; 10507 10508 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr, 10509 attrs, &prot, &page_size, &fi, NULL); 10510 10511 if (ret) { 10512 return -1; 10513 } 10514 return phys_addr; 10515 } 10516 10517 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) 10518 { 10519 uint32_t mask; 10520 unsigned el = arm_current_el(env); 10521 10522 /* First handle registers which unprivileged can read */ 10523 10524 switch (reg) { 10525 case 0 ... 7: /* xPSR sub-fields */ 10526 mask = 0; 10527 if ((reg & 1) && el) { 10528 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */ 10529 } 10530 if (!(reg & 4)) { 10531 mask |= XPSR_NZCV | XPSR_Q; /* APSR */ 10532 } 10533 /* EPSR reads as zero */ 10534 return xpsr_read(env) & mask; 10535 break; 10536 case 20: /* CONTROL */ 10537 return env->v7m.control[env->v7m.secure]; 10538 case 0x94: /* CONTROL_NS */ 10539 /* We have to handle this here because unprivileged Secure code 10540 * can read the NS CONTROL register. 10541 */ 10542 if (!env->v7m.secure) { 10543 return 0; 10544 } 10545 return env->v7m.control[M_REG_NS]; 10546 } 10547 10548 if (el == 0) { 10549 return 0; /* unprivileged reads others as zero */ 10550 } 10551 10552 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 10553 switch (reg) { 10554 case 0x88: /* MSP_NS */ 10555 if (!env->v7m.secure) { 10556 return 0; 10557 } 10558 return env->v7m.other_ss_msp; 10559 case 0x89: /* PSP_NS */ 10560 if (!env->v7m.secure) { 10561 return 0; 10562 } 10563 return env->v7m.other_ss_psp; 10564 case 0x8a: /* MSPLIM_NS */ 10565 if (!env->v7m.secure) { 10566 return 0; 10567 } 10568 return env->v7m.msplim[M_REG_NS]; 10569 case 0x8b: /* PSPLIM_NS */ 10570 if (!env->v7m.secure) { 10571 return 0; 10572 } 10573 return env->v7m.psplim[M_REG_NS]; 10574 case 0x90: /* PRIMASK_NS */ 10575 if (!env->v7m.secure) { 10576 return 0; 10577 } 10578 return env->v7m.primask[M_REG_NS]; 10579 case 0x91: /* BASEPRI_NS */ 10580 if (!env->v7m.secure) { 10581 return 0; 10582 } 10583 return env->v7m.basepri[M_REG_NS]; 10584 case 0x93: /* FAULTMASK_NS */ 10585 if (!env->v7m.secure) { 10586 return 0; 10587 } 10588 return env->v7m.faultmask[M_REG_NS]; 10589 case 0x98: /* SP_NS */ 10590 { 10591 /* This gives the non-secure SP selected based on whether we're 10592 * currently in handler mode or not, using the NS CONTROL.SPSEL. 10593 */ 10594 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; 10595 10596 if (!env->v7m.secure) { 10597 return 0; 10598 } 10599 if (!arm_v7m_is_handler_mode(env) && spsel) { 10600 return env->v7m.other_ss_psp; 10601 } else { 10602 return env->v7m.other_ss_msp; 10603 } 10604 } 10605 default: 10606 break; 10607 } 10608 } 10609 10610 switch (reg) { 10611 case 8: /* MSP */ 10612 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13]; 10613 case 9: /* PSP */ 10614 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp; 10615 case 10: /* MSPLIM */ 10616 if (!arm_feature(env, ARM_FEATURE_V8)) { 10617 goto bad_reg; 10618 } 10619 return env->v7m.msplim[env->v7m.secure]; 10620 case 11: /* PSPLIM */ 10621 if (!arm_feature(env, ARM_FEATURE_V8)) { 10622 goto bad_reg; 10623 } 10624 return env->v7m.psplim[env->v7m.secure]; 10625 case 16: /* PRIMASK */ 10626 return env->v7m.primask[env->v7m.secure]; 10627 case 17: /* BASEPRI */ 10628 case 18: /* BASEPRI_MAX */ 10629 return env->v7m.basepri[env->v7m.secure]; 10630 case 19: /* FAULTMASK */ 10631 return env->v7m.faultmask[env->v7m.secure]; 10632 default: 10633 bad_reg: 10634 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special" 10635 " register %d\n", reg); 10636 return 0; 10637 } 10638 } 10639 10640 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val) 10641 { 10642 /* We're passed bits [11..0] of the instruction; extract 10643 * SYSm and the mask bits. 10644 * Invalid combinations of SYSm and mask are UNPREDICTABLE; 10645 * we choose to treat them as if the mask bits were valid. 10646 * NB that the pseudocode 'mask' variable is bits [11..10], 10647 * whereas ours is [11..8]. 10648 */ 10649 uint32_t mask = extract32(maskreg, 8, 4); 10650 uint32_t reg = extract32(maskreg, 0, 8); 10651 10652 if (arm_current_el(env) == 0 && reg > 7) { 10653 /* only xPSR sub-fields may be written by unprivileged */ 10654 return; 10655 } 10656 10657 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 10658 switch (reg) { 10659 case 0x88: /* MSP_NS */ 10660 if (!env->v7m.secure) { 10661 return; 10662 } 10663 env->v7m.other_ss_msp = val; 10664 return; 10665 case 0x89: /* PSP_NS */ 10666 if (!env->v7m.secure) { 10667 return; 10668 } 10669 env->v7m.other_ss_psp = val; 10670 return; 10671 case 0x8a: /* MSPLIM_NS */ 10672 if (!env->v7m.secure) { 10673 return; 10674 } 10675 env->v7m.msplim[M_REG_NS] = val & ~7; 10676 return; 10677 case 0x8b: /* PSPLIM_NS */ 10678 if (!env->v7m.secure) { 10679 return; 10680 } 10681 env->v7m.psplim[M_REG_NS] = val & ~7; 10682 return; 10683 case 0x90: /* PRIMASK_NS */ 10684 if (!env->v7m.secure) { 10685 return; 10686 } 10687 env->v7m.primask[M_REG_NS] = val & 1; 10688 return; 10689 case 0x91: /* BASEPRI_NS */ 10690 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { 10691 return; 10692 } 10693 env->v7m.basepri[M_REG_NS] = val & 0xff; 10694 return; 10695 case 0x93: /* FAULTMASK_NS */ 10696 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { 10697 return; 10698 } 10699 env->v7m.faultmask[M_REG_NS] = val & 1; 10700 return; 10701 case 0x94: /* CONTROL_NS */ 10702 if (!env->v7m.secure) { 10703 return; 10704 } 10705 write_v7m_control_spsel_for_secstate(env, 10706 val & R_V7M_CONTROL_SPSEL_MASK, 10707 M_REG_NS); 10708 if (arm_feature(env, ARM_FEATURE_M_MAIN)) { 10709 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK; 10710 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK; 10711 } 10712 return; 10713 case 0x98: /* SP_NS */ 10714 { 10715 /* This gives the non-secure SP selected based on whether we're 10716 * currently in handler mode or not, using the NS CONTROL.SPSEL. 10717 */ 10718 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; 10719 10720 if (!env->v7m.secure) { 10721 return; 10722 } 10723 if (!arm_v7m_is_handler_mode(env) && spsel) { 10724 env->v7m.other_ss_psp = val; 10725 } else { 10726 env->v7m.other_ss_msp = val; 10727 } 10728 return; 10729 } 10730 default: 10731 break; 10732 } 10733 } 10734 10735 switch (reg) { 10736 case 0 ... 7: /* xPSR sub-fields */ 10737 /* only APSR is actually writable */ 10738 if (!(reg & 4)) { 10739 uint32_t apsrmask = 0; 10740 10741 if (mask & 8) { 10742 apsrmask |= XPSR_NZCV | XPSR_Q; 10743 } 10744 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) { 10745 apsrmask |= XPSR_GE; 10746 } 10747 xpsr_write(env, val, apsrmask); 10748 } 10749 break; 10750 case 8: /* MSP */ 10751 if (v7m_using_psp(env)) { 10752 env->v7m.other_sp = val; 10753 } else { 10754 env->regs[13] = val; 10755 } 10756 break; 10757 case 9: /* PSP */ 10758 if (v7m_using_psp(env)) { 10759 env->regs[13] = val; 10760 } else { 10761 env->v7m.other_sp = val; 10762 } 10763 break; 10764 case 10: /* MSPLIM */ 10765 if (!arm_feature(env, ARM_FEATURE_V8)) { 10766 goto bad_reg; 10767 } 10768 env->v7m.msplim[env->v7m.secure] = val & ~7; 10769 break; 10770 case 11: /* PSPLIM */ 10771 if (!arm_feature(env, ARM_FEATURE_V8)) { 10772 goto bad_reg; 10773 } 10774 env->v7m.psplim[env->v7m.secure] = val & ~7; 10775 break; 10776 case 16: /* PRIMASK */ 10777 env->v7m.primask[env->v7m.secure] = val & 1; 10778 break; 10779 case 17: /* BASEPRI */ 10780 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 10781 goto bad_reg; 10782 } 10783 env->v7m.basepri[env->v7m.secure] = val & 0xff; 10784 break; 10785 case 18: /* BASEPRI_MAX */ 10786 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 10787 goto bad_reg; 10788 } 10789 val &= 0xff; 10790 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure] 10791 || env->v7m.basepri[env->v7m.secure] == 0)) { 10792 env->v7m.basepri[env->v7m.secure] = val; 10793 } 10794 break; 10795 case 19: /* FAULTMASK */ 10796 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 10797 goto bad_reg; 10798 } 10799 env->v7m.faultmask[env->v7m.secure] = val & 1; 10800 break; 10801 case 20: /* CONTROL */ 10802 /* Writing to the SPSEL bit only has an effect if we are in 10803 * thread mode; other bits can be updated by any privileged code. 10804 * write_v7m_control_spsel() deals with updating the SPSEL bit in 10805 * env->v7m.control, so we only need update the others. 10806 * For v7M, we must just ignore explicit writes to SPSEL in handler 10807 * mode; for v8M the write is permitted but will have no effect. 10808 */ 10809 if (arm_feature(env, ARM_FEATURE_V8) || 10810 !arm_v7m_is_handler_mode(env)) { 10811 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0); 10812 } 10813 if (arm_feature(env, ARM_FEATURE_M_MAIN)) { 10814 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK; 10815 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK; 10816 } 10817 break; 10818 default: 10819 bad_reg: 10820 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special" 10821 " register %d\n", reg); 10822 return; 10823 } 10824 } 10825 10826 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) 10827 { 10828 /* Implement the TT instruction. op is bits [7:6] of the insn. */ 10829 bool forceunpriv = op & 1; 10830 bool alt = op & 2; 10831 V8M_SAttributes sattrs = {}; 10832 uint32_t tt_resp; 10833 bool r, rw, nsr, nsrw, mrvalid; 10834 int prot; 10835 ARMMMUFaultInfo fi = {}; 10836 MemTxAttrs attrs = {}; 10837 hwaddr phys_addr; 10838 ARMMMUIdx mmu_idx; 10839 uint32_t mregion; 10840 bool targetpriv; 10841 bool targetsec = env->v7m.secure; 10842 bool is_subpage; 10843 10844 /* Work out what the security state and privilege level we're 10845 * interested in is... 10846 */ 10847 if (alt) { 10848 targetsec = !targetsec; 10849 } 10850 10851 if (forceunpriv) { 10852 targetpriv = false; 10853 } else { 10854 targetpriv = arm_v7m_is_handler_mode(env) || 10855 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK); 10856 } 10857 10858 /* ...and then figure out which MMU index this is */ 10859 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv); 10860 10861 /* We know that the MPU and SAU don't care about the access type 10862 * for our purposes beyond that we don't want to claim to be 10863 * an insn fetch, so we arbitrarily call this a read. 10864 */ 10865 10866 /* MPU region info only available for privileged or if 10867 * inspecting the other MPU state. 10868 */ 10869 if (arm_current_el(env) != 0 || alt) { 10870 /* We can ignore the return value as prot is always set */ 10871 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, 10872 &phys_addr, &attrs, &prot, &is_subpage, 10873 &fi, &mregion); 10874 if (mregion == -1) { 10875 mrvalid = false; 10876 mregion = 0; 10877 } else { 10878 mrvalid = true; 10879 } 10880 r = prot & PAGE_READ; 10881 rw = prot & PAGE_WRITE; 10882 } else { 10883 r = false; 10884 rw = false; 10885 mrvalid = false; 10886 mregion = 0; 10887 } 10888 10889 if (env->v7m.secure) { 10890 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); 10891 nsr = sattrs.ns && r; 10892 nsrw = sattrs.ns && rw; 10893 } else { 10894 sattrs.ns = true; 10895 nsr = false; 10896 nsrw = false; 10897 } 10898 10899 tt_resp = (sattrs.iregion << 24) | 10900 (sattrs.irvalid << 23) | 10901 ((!sattrs.ns) << 22) | 10902 (nsrw << 21) | 10903 (nsr << 20) | 10904 (rw << 19) | 10905 (r << 18) | 10906 (sattrs.srvalid << 17) | 10907 (mrvalid << 16) | 10908 (sattrs.sregion << 8) | 10909 mregion; 10910 10911 return tt_resp; 10912 } 10913 10914 #endif 10915 10916 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in) 10917 { 10918 /* Implement DC ZVA, which zeroes a fixed-length block of memory. 10919 * Note that we do not implement the (architecturally mandated) 10920 * alignment fault for attempts to use this on Device memory 10921 * (which matches the usual QEMU behaviour of not implementing either 10922 * alignment faults or any memory attribute handling). 10923 */ 10924 10925 ARMCPU *cpu = arm_env_get_cpu(env); 10926 uint64_t blocklen = 4 << cpu->dcz_blocksize; 10927 uint64_t vaddr = vaddr_in & ~(blocklen - 1); 10928 10929 #ifndef CONFIG_USER_ONLY 10930 { 10931 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than 10932 * the block size so we might have to do more than one TLB lookup. 10933 * We know that in fact for any v8 CPU the page size is at least 4K 10934 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only 10935 * 1K as an artefact of legacy v5 subpage support being present in the 10936 * same QEMU executable. 10937 */ 10938 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE); 10939 void *hostaddr[maxidx]; 10940 int try, i; 10941 unsigned mmu_idx = cpu_mmu_index(env, false); 10942 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx); 10943 10944 for (try = 0; try < 2; try++) { 10945 10946 for (i = 0; i < maxidx; i++) { 10947 hostaddr[i] = tlb_vaddr_to_host(env, 10948 vaddr + TARGET_PAGE_SIZE * i, 10949 1, mmu_idx); 10950 if (!hostaddr[i]) { 10951 break; 10952 } 10953 } 10954 if (i == maxidx) { 10955 /* If it's all in the TLB it's fair game for just writing to; 10956 * we know we don't need to update dirty status, etc. 10957 */ 10958 for (i = 0; i < maxidx - 1; i++) { 10959 memset(hostaddr[i], 0, TARGET_PAGE_SIZE); 10960 } 10961 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE)); 10962 return; 10963 } 10964 /* OK, try a store and see if we can populate the tlb. This 10965 * might cause an exception if the memory isn't writable, 10966 * in which case we will longjmp out of here. We must for 10967 * this purpose use the actual register value passed to us 10968 * so that we get the fault address right. 10969 */ 10970 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC()); 10971 /* Now we can populate the other TLB entries, if any */ 10972 for (i = 0; i < maxidx; i++) { 10973 uint64_t va = vaddr + TARGET_PAGE_SIZE * i; 10974 if (va != (vaddr_in & TARGET_PAGE_MASK)) { 10975 helper_ret_stb_mmu(env, va, 0, oi, GETPC()); 10976 } 10977 } 10978 } 10979 10980 /* Slow path (probably attempt to do this to an I/O device or 10981 * similar, or clearing of a block of code we have translations 10982 * cached for). Just do a series of byte writes as the architecture 10983 * demands. It's not worth trying to use a cpu_physical_memory_map(), 10984 * memset(), unmap() sequence here because: 10985 * + we'd need to account for the blocksize being larger than a page 10986 * + the direct-RAM access case is almost always going to be dealt 10987 * with in the fastpath code above, so there's no speed benefit 10988 * + we would have to deal with the map returning NULL because the 10989 * bounce buffer was in use 10990 */ 10991 for (i = 0; i < blocklen; i++) { 10992 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC()); 10993 } 10994 } 10995 #else 10996 memset(g2h(vaddr), 0, blocklen); 10997 #endif 10998 } 10999 11000 /* Note that signed overflow is undefined in C. The following routines are 11001 careful to use unsigned types where modulo arithmetic is required. 11002 Failure to do so _will_ break on newer gcc. */ 11003 11004 /* Signed saturating arithmetic. */ 11005 11006 /* Perform 16-bit signed saturating addition. */ 11007 static inline uint16_t add16_sat(uint16_t a, uint16_t b) 11008 { 11009 uint16_t res; 11010 11011 res = a + b; 11012 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { 11013 if (a & 0x8000) 11014 res = 0x8000; 11015 else 11016 res = 0x7fff; 11017 } 11018 return res; 11019 } 11020 11021 /* Perform 8-bit signed saturating addition. */ 11022 static inline uint8_t add8_sat(uint8_t a, uint8_t b) 11023 { 11024 uint8_t res; 11025 11026 res = a + b; 11027 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { 11028 if (a & 0x80) 11029 res = 0x80; 11030 else 11031 res = 0x7f; 11032 } 11033 return res; 11034 } 11035 11036 /* Perform 16-bit signed saturating subtraction. */ 11037 static inline uint16_t sub16_sat(uint16_t a, uint16_t b) 11038 { 11039 uint16_t res; 11040 11041 res = a - b; 11042 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { 11043 if (a & 0x8000) 11044 res = 0x8000; 11045 else 11046 res = 0x7fff; 11047 } 11048 return res; 11049 } 11050 11051 /* Perform 8-bit signed saturating subtraction. */ 11052 static inline uint8_t sub8_sat(uint8_t a, uint8_t b) 11053 { 11054 uint8_t res; 11055 11056 res = a - b; 11057 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { 11058 if (a & 0x80) 11059 res = 0x80; 11060 else 11061 res = 0x7f; 11062 } 11063 return res; 11064 } 11065 11066 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); 11067 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); 11068 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); 11069 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); 11070 #define PFX q 11071 11072 #include "op_addsub.h" 11073 11074 /* Unsigned saturating arithmetic. */ 11075 static inline uint16_t add16_usat(uint16_t a, uint16_t b) 11076 { 11077 uint16_t res; 11078 res = a + b; 11079 if (res < a) 11080 res = 0xffff; 11081 return res; 11082 } 11083 11084 static inline uint16_t sub16_usat(uint16_t a, uint16_t b) 11085 { 11086 if (a > b) 11087 return a - b; 11088 else 11089 return 0; 11090 } 11091 11092 static inline uint8_t add8_usat(uint8_t a, uint8_t b) 11093 { 11094 uint8_t res; 11095 res = a + b; 11096 if (res < a) 11097 res = 0xff; 11098 return res; 11099 } 11100 11101 static inline uint8_t sub8_usat(uint8_t a, uint8_t b) 11102 { 11103 if (a > b) 11104 return a - b; 11105 else 11106 return 0; 11107 } 11108 11109 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); 11110 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); 11111 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); 11112 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); 11113 #define PFX uq 11114 11115 #include "op_addsub.h" 11116 11117 /* Signed modulo arithmetic. */ 11118 #define SARITH16(a, b, n, op) do { \ 11119 int32_t sum; \ 11120 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ 11121 RESULT(sum, n, 16); \ 11122 if (sum >= 0) \ 11123 ge |= 3 << (n * 2); \ 11124 } while(0) 11125 11126 #define SARITH8(a, b, n, op) do { \ 11127 int32_t sum; \ 11128 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ 11129 RESULT(sum, n, 8); \ 11130 if (sum >= 0) \ 11131 ge |= 1 << n; \ 11132 } while(0) 11133 11134 11135 #define ADD16(a, b, n) SARITH16(a, b, n, +) 11136 #define SUB16(a, b, n) SARITH16(a, b, n, -) 11137 #define ADD8(a, b, n) SARITH8(a, b, n, +) 11138 #define SUB8(a, b, n) SARITH8(a, b, n, -) 11139 #define PFX s 11140 #define ARITH_GE 11141 11142 #include "op_addsub.h" 11143 11144 /* Unsigned modulo arithmetic. */ 11145 #define ADD16(a, b, n) do { \ 11146 uint32_t sum; \ 11147 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ 11148 RESULT(sum, n, 16); \ 11149 if ((sum >> 16) == 1) \ 11150 ge |= 3 << (n * 2); \ 11151 } while(0) 11152 11153 #define ADD8(a, b, n) do { \ 11154 uint32_t sum; \ 11155 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ 11156 RESULT(sum, n, 8); \ 11157 if ((sum >> 8) == 1) \ 11158 ge |= 1 << n; \ 11159 } while(0) 11160 11161 #define SUB16(a, b, n) do { \ 11162 uint32_t sum; \ 11163 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ 11164 RESULT(sum, n, 16); \ 11165 if ((sum >> 16) == 0) \ 11166 ge |= 3 << (n * 2); \ 11167 } while(0) 11168 11169 #define SUB8(a, b, n) do { \ 11170 uint32_t sum; \ 11171 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ 11172 RESULT(sum, n, 8); \ 11173 if ((sum >> 8) == 0) \ 11174 ge |= 1 << n; \ 11175 } while(0) 11176 11177 #define PFX u 11178 #define ARITH_GE 11179 11180 #include "op_addsub.h" 11181 11182 /* Halved signed arithmetic. */ 11183 #define ADD16(a, b, n) \ 11184 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) 11185 #define SUB16(a, b, n) \ 11186 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) 11187 #define ADD8(a, b, n) \ 11188 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) 11189 #define SUB8(a, b, n) \ 11190 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) 11191 #define PFX sh 11192 11193 #include "op_addsub.h" 11194 11195 /* Halved unsigned arithmetic. */ 11196 #define ADD16(a, b, n) \ 11197 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) 11198 #define SUB16(a, b, n) \ 11199 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) 11200 #define ADD8(a, b, n) \ 11201 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) 11202 #define SUB8(a, b, n) \ 11203 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) 11204 #define PFX uh 11205 11206 #include "op_addsub.h" 11207 11208 static inline uint8_t do_usad(uint8_t a, uint8_t b) 11209 { 11210 if (a > b) 11211 return a - b; 11212 else 11213 return b - a; 11214 } 11215 11216 /* Unsigned sum of absolute byte differences. */ 11217 uint32_t HELPER(usad8)(uint32_t a, uint32_t b) 11218 { 11219 uint32_t sum; 11220 sum = do_usad(a, b); 11221 sum += do_usad(a >> 8, b >> 8); 11222 sum += do_usad(a >> 16, b >>16); 11223 sum += do_usad(a >> 24, b >> 24); 11224 return sum; 11225 } 11226 11227 /* For ARMv6 SEL instruction. */ 11228 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) 11229 { 11230 uint32_t mask; 11231 11232 mask = 0; 11233 if (flags & 1) 11234 mask |= 0xff; 11235 if (flags & 2) 11236 mask |= 0xff00; 11237 if (flags & 4) 11238 mask |= 0xff0000; 11239 if (flags & 8) 11240 mask |= 0xff000000; 11241 return (a & mask) | (b & ~mask); 11242 } 11243 11244 /* VFP support. We follow the convention used for VFP instructions: 11245 Single precision routines have a "s" suffix, double precision a 11246 "d" suffix. */ 11247 11248 /* Convert host exception flags to vfp form. */ 11249 static inline int vfp_exceptbits_from_host(int host_bits) 11250 { 11251 int target_bits = 0; 11252 11253 if (host_bits & float_flag_invalid) 11254 target_bits |= 1; 11255 if (host_bits & float_flag_divbyzero) 11256 target_bits |= 2; 11257 if (host_bits & float_flag_overflow) 11258 target_bits |= 4; 11259 if (host_bits & (float_flag_underflow | float_flag_output_denormal)) 11260 target_bits |= 8; 11261 if (host_bits & float_flag_inexact) 11262 target_bits |= 0x10; 11263 if (host_bits & float_flag_input_denormal) 11264 target_bits |= 0x80; 11265 return target_bits; 11266 } 11267 11268 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env) 11269 { 11270 int i; 11271 uint32_t fpscr; 11272 11273 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff) 11274 | (env->vfp.vec_len << 16) 11275 | (env->vfp.vec_stride << 20); 11276 i = get_float_exception_flags(&env->vfp.fp_status); 11277 i |= get_float_exception_flags(&env->vfp.standard_fp_status); 11278 i |= get_float_exception_flags(&env->vfp.fp_status_f16); 11279 fpscr |= vfp_exceptbits_from_host(i); 11280 return fpscr; 11281 } 11282 11283 uint32_t vfp_get_fpscr(CPUARMState *env) 11284 { 11285 return HELPER(vfp_get_fpscr)(env); 11286 } 11287 11288 /* Convert vfp exception flags to target form. */ 11289 static inline int vfp_exceptbits_to_host(int target_bits) 11290 { 11291 int host_bits = 0; 11292 11293 if (target_bits & 1) 11294 host_bits |= float_flag_invalid; 11295 if (target_bits & 2) 11296 host_bits |= float_flag_divbyzero; 11297 if (target_bits & 4) 11298 host_bits |= float_flag_overflow; 11299 if (target_bits & 8) 11300 host_bits |= float_flag_underflow; 11301 if (target_bits & 0x10) 11302 host_bits |= float_flag_inexact; 11303 if (target_bits & 0x80) 11304 host_bits |= float_flag_input_denormal; 11305 return host_bits; 11306 } 11307 11308 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val) 11309 { 11310 int i; 11311 uint32_t changed; 11312 11313 changed = env->vfp.xregs[ARM_VFP_FPSCR]; 11314 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff); 11315 env->vfp.vec_len = (val >> 16) & 7; 11316 env->vfp.vec_stride = (val >> 20) & 3; 11317 11318 changed ^= val; 11319 if (changed & (3 << 22)) { 11320 i = (val >> 22) & 3; 11321 switch (i) { 11322 case FPROUNDING_TIEEVEN: 11323 i = float_round_nearest_even; 11324 break; 11325 case FPROUNDING_POSINF: 11326 i = float_round_up; 11327 break; 11328 case FPROUNDING_NEGINF: 11329 i = float_round_down; 11330 break; 11331 case FPROUNDING_ZERO: 11332 i = float_round_to_zero; 11333 break; 11334 } 11335 set_float_rounding_mode(i, &env->vfp.fp_status); 11336 set_float_rounding_mode(i, &env->vfp.fp_status_f16); 11337 } 11338 if (changed & FPCR_FZ16) { 11339 bool ftz_enabled = val & FPCR_FZ16; 11340 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16); 11341 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16); 11342 } 11343 if (changed & FPCR_FZ) { 11344 bool ftz_enabled = val & FPCR_FZ; 11345 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status); 11346 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status); 11347 } 11348 if (changed & FPCR_DN) { 11349 bool dnan_enabled = val & FPCR_DN; 11350 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status); 11351 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16); 11352 } 11353 11354 /* The exception flags are ORed together when we read fpscr so we 11355 * only need to preserve the current state in one of our 11356 * float_status values. 11357 */ 11358 i = vfp_exceptbits_to_host(val); 11359 set_float_exception_flags(i, &env->vfp.fp_status); 11360 set_float_exception_flags(0, &env->vfp.fp_status_f16); 11361 set_float_exception_flags(0, &env->vfp.standard_fp_status); 11362 } 11363 11364 void vfp_set_fpscr(CPUARMState *env, uint32_t val) 11365 { 11366 HELPER(vfp_set_fpscr)(env, val); 11367 } 11368 11369 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p)) 11370 11371 #define VFP_BINOP(name) \ 11372 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \ 11373 { \ 11374 float_status *fpst = fpstp; \ 11375 return float32_ ## name(a, b, fpst); \ 11376 } \ 11377 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \ 11378 { \ 11379 float_status *fpst = fpstp; \ 11380 return float64_ ## name(a, b, fpst); \ 11381 } 11382 VFP_BINOP(add) 11383 VFP_BINOP(sub) 11384 VFP_BINOP(mul) 11385 VFP_BINOP(div) 11386 VFP_BINOP(min) 11387 VFP_BINOP(max) 11388 VFP_BINOP(minnum) 11389 VFP_BINOP(maxnum) 11390 #undef VFP_BINOP 11391 11392 float32 VFP_HELPER(neg, s)(float32 a) 11393 { 11394 return float32_chs(a); 11395 } 11396 11397 float64 VFP_HELPER(neg, d)(float64 a) 11398 { 11399 return float64_chs(a); 11400 } 11401 11402 float32 VFP_HELPER(abs, s)(float32 a) 11403 { 11404 return float32_abs(a); 11405 } 11406 11407 float64 VFP_HELPER(abs, d)(float64 a) 11408 { 11409 return float64_abs(a); 11410 } 11411 11412 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env) 11413 { 11414 return float32_sqrt(a, &env->vfp.fp_status); 11415 } 11416 11417 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env) 11418 { 11419 return float64_sqrt(a, &env->vfp.fp_status); 11420 } 11421 11422 /* XXX: check quiet/signaling case */ 11423 #define DO_VFP_cmp(p, type) \ 11424 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \ 11425 { \ 11426 uint32_t flags; \ 11427 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \ 11428 case 0: flags = 0x6; break; \ 11429 case -1: flags = 0x8; break; \ 11430 case 1: flags = 0x2; break; \ 11431 default: case 2: flags = 0x3; break; \ 11432 } \ 11433 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ 11434 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ 11435 } \ 11436 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \ 11437 { \ 11438 uint32_t flags; \ 11439 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \ 11440 case 0: flags = 0x6; break; \ 11441 case -1: flags = 0x8; break; \ 11442 case 1: flags = 0x2; break; \ 11443 default: case 2: flags = 0x3; break; \ 11444 } \ 11445 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ 11446 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ 11447 } 11448 DO_VFP_cmp(s, float32) 11449 DO_VFP_cmp(d, float64) 11450 #undef DO_VFP_cmp 11451 11452 /* Integer to float and float to integer conversions */ 11453 11454 #define CONV_ITOF(name, ftype, fsz, sign) \ 11455 ftype HELPER(name)(uint32_t x, void *fpstp) \ 11456 { \ 11457 float_status *fpst = fpstp; \ 11458 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \ 11459 } 11460 11461 #define CONV_FTOI(name, ftype, fsz, sign, round) \ 11462 sign##int32_t HELPER(name)(ftype x, void *fpstp) \ 11463 { \ 11464 float_status *fpst = fpstp; \ 11465 if (float##fsz##_is_any_nan(x)) { \ 11466 float_raise(float_flag_invalid, fpst); \ 11467 return 0; \ 11468 } \ 11469 return float##fsz##_to_##sign##int32##round(x, fpst); \ 11470 } 11471 11472 #define FLOAT_CONVS(name, p, ftype, fsz, sign) \ 11473 CONV_ITOF(vfp_##name##to##p, ftype, fsz, sign) \ 11474 CONV_FTOI(vfp_to##name##p, ftype, fsz, sign, ) \ 11475 CONV_FTOI(vfp_to##name##z##p, ftype, fsz, sign, _round_to_zero) 11476 11477 FLOAT_CONVS(si, h, uint32_t, 16, ) 11478 FLOAT_CONVS(si, s, float32, 32, ) 11479 FLOAT_CONVS(si, d, float64, 64, ) 11480 FLOAT_CONVS(ui, h, uint32_t, 16, u) 11481 FLOAT_CONVS(ui, s, float32, 32, u) 11482 FLOAT_CONVS(ui, d, float64, 64, u) 11483 11484 #undef CONV_ITOF 11485 #undef CONV_FTOI 11486 #undef FLOAT_CONVS 11487 11488 /* floating point conversion */ 11489 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env) 11490 { 11491 return float32_to_float64(x, &env->vfp.fp_status); 11492 } 11493 11494 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env) 11495 { 11496 return float64_to_float32(x, &env->vfp.fp_status); 11497 } 11498 11499 /* VFP3 fixed point conversion. */ 11500 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ 11501 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \ 11502 void *fpstp) \ 11503 { \ 11504 float_status *fpst = fpstp; \ 11505 float##fsz tmp; \ 11506 tmp = itype##_to_##float##fsz(x, fpst); \ 11507 return float##fsz##_scalbn(tmp, -(int)shift, fpst); \ 11508 } 11509 11510 /* Notice that we want only input-denormal exception flags from the 11511 * scalbn operation: the other possible flags (overflow+inexact if 11512 * we overflow to infinity, output-denormal) aren't correct for the 11513 * complete scale-and-convert operation. 11514 */ 11515 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, round) \ 11516 uint##isz##_t HELPER(vfp_to##name##p##round)(float##fsz x, \ 11517 uint32_t shift, \ 11518 void *fpstp) \ 11519 { \ 11520 float_status *fpst = fpstp; \ 11521 int old_exc_flags = get_float_exception_flags(fpst); \ 11522 float##fsz tmp; \ 11523 if (float##fsz##_is_any_nan(x)) { \ 11524 float_raise(float_flag_invalid, fpst); \ 11525 return 0; \ 11526 } \ 11527 tmp = float##fsz##_scalbn(x, shift, fpst); \ 11528 old_exc_flags |= get_float_exception_flags(fpst) \ 11529 & float_flag_input_denormal; \ 11530 set_float_exception_flags(old_exc_flags, fpst); \ 11531 return float##fsz##_to_##itype##round(tmp, fpst); \ 11532 } 11533 11534 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \ 11535 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ 11536 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, _round_to_zero) \ 11537 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ) 11538 11539 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \ 11540 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ 11541 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ) 11542 11543 VFP_CONV_FIX(sh, d, 64, 64, int16) 11544 VFP_CONV_FIX(sl, d, 64, 64, int32) 11545 VFP_CONV_FIX_A64(sq, d, 64, 64, int64) 11546 VFP_CONV_FIX(uh, d, 64, 64, uint16) 11547 VFP_CONV_FIX(ul, d, 64, 64, uint32) 11548 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64) 11549 VFP_CONV_FIX(sh, s, 32, 32, int16) 11550 VFP_CONV_FIX(sl, s, 32, 32, int32) 11551 VFP_CONV_FIX_A64(sq, s, 32, 64, int64) 11552 VFP_CONV_FIX(uh, s, 32, 32, uint16) 11553 VFP_CONV_FIX(ul, s, 32, 32, uint32) 11554 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64) 11555 11556 #undef VFP_CONV_FIX 11557 #undef VFP_CONV_FIX_FLOAT 11558 #undef VFP_CONV_FLOAT_FIX_ROUND 11559 #undef VFP_CONV_FIX_A64 11560 11561 /* Conversion to/from f16 can overflow to infinity before/after scaling. 11562 * Therefore we convert to f64, scale, and then convert f64 to f16; or 11563 * vice versa for conversion to integer. 11564 * 11565 * For 16- and 32-bit integers, the conversion to f64 never rounds. 11566 * For 64-bit integers, any integer that would cause rounding will also 11567 * overflow to f16 infinity, so there is no double rounding problem. 11568 */ 11569 11570 static float16 do_postscale_fp16(float64 f, int shift, float_status *fpst) 11571 { 11572 return float64_to_float16(float64_scalbn(f, -shift, fpst), true, fpst); 11573 } 11574 11575 uint32_t HELPER(vfp_sltoh)(uint32_t x, uint32_t shift, void *fpst) 11576 { 11577 return do_postscale_fp16(int32_to_float64(x, fpst), shift, fpst); 11578 } 11579 11580 uint32_t HELPER(vfp_ultoh)(uint32_t x, uint32_t shift, void *fpst) 11581 { 11582 return do_postscale_fp16(uint32_to_float64(x, fpst), shift, fpst); 11583 } 11584 11585 uint32_t HELPER(vfp_sqtoh)(uint64_t x, uint32_t shift, void *fpst) 11586 { 11587 return do_postscale_fp16(int64_to_float64(x, fpst), shift, fpst); 11588 } 11589 11590 uint32_t HELPER(vfp_uqtoh)(uint64_t x, uint32_t shift, void *fpst) 11591 { 11592 return do_postscale_fp16(uint64_to_float64(x, fpst), shift, fpst); 11593 } 11594 11595 static float64 do_prescale_fp16(float16 f, int shift, float_status *fpst) 11596 { 11597 if (unlikely(float16_is_any_nan(f))) { 11598 float_raise(float_flag_invalid, fpst); 11599 return 0; 11600 } else { 11601 int old_exc_flags = get_float_exception_flags(fpst); 11602 float64 ret; 11603 11604 ret = float16_to_float64(f, true, fpst); 11605 ret = float64_scalbn(ret, shift, fpst); 11606 old_exc_flags |= get_float_exception_flags(fpst) 11607 & float_flag_input_denormal; 11608 set_float_exception_flags(old_exc_flags, fpst); 11609 11610 return ret; 11611 } 11612 } 11613 11614 uint32_t HELPER(vfp_toshh)(uint32_t x, uint32_t shift, void *fpst) 11615 { 11616 return float64_to_int16(do_prescale_fp16(x, shift, fpst), fpst); 11617 } 11618 11619 uint32_t HELPER(vfp_touhh)(uint32_t x, uint32_t shift, void *fpst) 11620 { 11621 return float64_to_uint16(do_prescale_fp16(x, shift, fpst), fpst); 11622 } 11623 11624 uint32_t HELPER(vfp_toslh)(uint32_t x, uint32_t shift, void *fpst) 11625 { 11626 return float64_to_int32(do_prescale_fp16(x, shift, fpst), fpst); 11627 } 11628 11629 uint32_t HELPER(vfp_toulh)(uint32_t x, uint32_t shift, void *fpst) 11630 { 11631 return float64_to_uint32(do_prescale_fp16(x, shift, fpst), fpst); 11632 } 11633 11634 uint64_t HELPER(vfp_tosqh)(uint32_t x, uint32_t shift, void *fpst) 11635 { 11636 return float64_to_int64(do_prescale_fp16(x, shift, fpst), fpst); 11637 } 11638 11639 uint64_t HELPER(vfp_touqh)(uint32_t x, uint32_t shift, void *fpst) 11640 { 11641 return float64_to_uint64(do_prescale_fp16(x, shift, fpst), fpst); 11642 } 11643 11644 /* Set the current fp rounding mode and return the old one. 11645 * The argument is a softfloat float_round_ value. 11646 */ 11647 uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp) 11648 { 11649 float_status *fp_status = fpstp; 11650 11651 uint32_t prev_rmode = get_float_rounding_mode(fp_status); 11652 set_float_rounding_mode(rmode, fp_status); 11653 11654 return prev_rmode; 11655 } 11656 11657 /* Set the current fp rounding mode in the standard fp status and return 11658 * the old one. This is for NEON instructions that need to change the 11659 * rounding mode but wish to use the standard FPSCR values for everything 11660 * else. Always set the rounding mode back to the correct value after 11661 * modifying it. 11662 * The argument is a softfloat float_round_ value. 11663 */ 11664 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env) 11665 { 11666 float_status *fp_status = &env->vfp.standard_fp_status; 11667 11668 uint32_t prev_rmode = get_float_rounding_mode(fp_status); 11669 set_float_rounding_mode(rmode, fp_status); 11670 11671 return prev_rmode; 11672 } 11673 11674 /* Half precision conversions. */ 11675 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, void *fpstp, uint32_t ahp_mode) 11676 { 11677 /* Squash FZ16 to 0 for the duration of conversion. In this case, 11678 * it would affect flushing input denormals. 11679 */ 11680 float_status *fpst = fpstp; 11681 flag save = get_flush_inputs_to_zero(fpst); 11682 set_flush_inputs_to_zero(false, fpst); 11683 float32 r = float16_to_float32(a, !ahp_mode, fpst); 11684 set_flush_inputs_to_zero(save, fpst); 11685 return r; 11686 } 11687 11688 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, void *fpstp, uint32_t ahp_mode) 11689 { 11690 /* Squash FZ16 to 0 for the duration of conversion. In this case, 11691 * it would affect flushing output denormals. 11692 */ 11693 float_status *fpst = fpstp; 11694 flag save = get_flush_to_zero(fpst); 11695 set_flush_to_zero(false, fpst); 11696 float16 r = float32_to_float16(a, !ahp_mode, fpst); 11697 set_flush_to_zero(save, fpst); 11698 return r; 11699 } 11700 11701 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, void *fpstp, uint32_t ahp_mode) 11702 { 11703 /* Squash FZ16 to 0 for the duration of conversion. In this case, 11704 * it would affect flushing input denormals. 11705 */ 11706 float_status *fpst = fpstp; 11707 flag save = get_flush_inputs_to_zero(fpst); 11708 set_flush_inputs_to_zero(false, fpst); 11709 float64 r = float16_to_float64(a, !ahp_mode, fpst); 11710 set_flush_inputs_to_zero(save, fpst); 11711 return r; 11712 } 11713 11714 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, void *fpstp, uint32_t ahp_mode) 11715 { 11716 /* Squash FZ16 to 0 for the duration of conversion. In this case, 11717 * it would affect flushing output denormals. 11718 */ 11719 float_status *fpst = fpstp; 11720 flag save = get_flush_to_zero(fpst); 11721 set_flush_to_zero(false, fpst); 11722 float16 r = float64_to_float16(a, !ahp_mode, fpst); 11723 set_flush_to_zero(save, fpst); 11724 return r; 11725 } 11726 11727 #define float32_two make_float32(0x40000000) 11728 #define float32_three make_float32(0x40400000) 11729 #define float32_one_point_five make_float32(0x3fc00000) 11730 11731 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env) 11732 { 11733 float_status *s = &env->vfp.standard_fp_status; 11734 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || 11735 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { 11736 if (!(float32_is_zero(a) || float32_is_zero(b))) { 11737 float_raise(float_flag_input_denormal, s); 11738 } 11739 return float32_two; 11740 } 11741 return float32_sub(float32_two, float32_mul(a, b, s), s); 11742 } 11743 11744 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env) 11745 { 11746 float_status *s = &env->vfp.standard_fp_status; 11747 float32 product; 11748 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || 11749 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { 11750 if (!(float32_is_zero(a) || float32_is_zero(b))) { 11751 float_raise(float_flag_input_denormal, s); 11752 } 11753 return float32_one_point_five; 11754 } 11755 product = float32_mul(a, b, s); 11756 return float32_div(float32_sub(float32_three, product, s), float32_two, s); 11757 } 11758 11759 /* NEON helpers. */ 11760 11761 /* Constants 256 and 512 are used in some helpers; we avoid relying on 11762 * int->float conversions at run-time. */ 11763 #define float64_256 make_float64(0x4070000000000000LL) 11764 #define float64_512 make_float64(0x4080000000000000LL) 11765 #define float16_maxnorm make_float16(0x7bff) 11766 #define float32_maxnorm make_float32(0x7f7fffff) 11767 #define float64_maxnorm make_float64(0x7fefffffffffffffLL) 11768 11769 /* Reciprocal functions 11770 * 11771 * The algorithm that must be used to calculate the estimate 11772 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate 11773 */ 11774 11775 /* See RecipEstimate() 11776 * 11777 * input is a 9 bit fixed point number 11778 * input range 256 .. 511 for a number from 0.5 <= x < 1.0. 11779 * result range 256 .. 511 for a number from 1.0 to 511/256. 11780 */ 11781 11782 static int recip_estimate(int input) 11783 { 11784 int a, b, r; 11785 assert(256 <= input && input < 512); 11786 a = (input * 2) + 1; 11787 b = (1 << 19) / a; 11788 r = (b + 1) >> 1; 11789 assert(256 <= r && r < 512); 11790 return r; 11791 } 11792 11793 /* 11794 * Common wrapper to call recip_estimate 11795 * 11796 * The parameters are exponent and 64 bit fraction (without implicit 11797 * bit) where the binary point is nominally at bit 52. Returns a 11798 * float64 which can then be rounded to the appropriate size by the 11799 * callee. 11800 */ 11801 11802 static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac) 11803 { 11804 uint32_t scaled, estimate; 11805 uint64_t result_frac; 11806 int result_exp; 11807 11808 /* Handle sub-normals */ 11809 if (*exp == 0) { 11810 if (extract64(frac, 51, 1) == 0) { 11811 *exp = -1; 11812 frac <<= 2; 11813 } else { 11814 frac <<= 1; 11815 } 11816 } 11817 11818 /* scaled = UInt('1':fraction<51:44>) */ 11819 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8)); 11820 estimate = recip_estimate(scaled); 11821 11822 result_exp = exp_off - *exp; 11823 result_frac = deposit64(0, 44, 8, estimate); 11824 if (result_exp == 0) { 11825 result_frac = deposit64(result_frac >> 1, 51, 1, 1); 11826 } else if (result_exp == -1) { 11827 result_frac = deposit64(result_frac >> 2, 50, 2, 1); 11828 result_exp = 0; 11829 } 11830 11831 *exp = result_exp; 11832 11833 return result_frac; 11834 } 11835 11836 static bool round_to_inf(float_status *fpst, bool sign_bit) 11837 { 11838 switch (fpst->float_rounding_mode) { 11839 case float_round_nearest_even: /* Round to Nearest */ 11840 return true; 11841 case float_round_up: /* Round to +Inf */ 11842 return !sign_bit; 11843 case float_round_down: /* Round to -Inf */ 11844 return sign_bit; 11845 case float_round_to_zero: /* Round to Zero */ 11846 return false; 11847 } 11848 11849 g_assert_not_reached(); 11850 } 11851 11852 uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp) 11853 { 11854 float_status *fpst = fpstp; 11855 float16 f16 = float16_squash_input_denormal(input, fpst); 11856 uint32_t f16_val = float16_val(f16); 11857 uint32_t f16_sign = float16_is_neg(f16); 11858 int f16_exp = extract32(f16_val, 10, 5); 11859 uint32_t f16_frac = extract32(f16_val, 0, 10); 11860 uint64_t f64_frac; 11861 11862 if (float16_is_any_nan(f16)) { 11863 float16 nan = f16; 11864 if (float16_is_signaling_nan(f16, fpst)) { 11865 float_raise(float_flag_invalid, fpst); 11866 nan = float16_silence_nan(f16, fpst); 11867 } 11868 if (fpst->default_nan_mode) { 11869 nan = float16_default_nan(fpst); 11870 } 11871 return nan; 11872 } else if (float16_is_infinity(f16)) { 11873 return float16_set_sign(float16_zero, float16_is_neg(f16)); 11874 } else if (float16_is_zero(f16)) { 11875 float_raise(float_flag_divbyzero, fpst); 11876 return float16_set_sign(float16_infinity, float16_is_neg(f16)); 11877 } else if (float16_abs(f16) < (1 << 8)) { 11878 /* Abs(value) < 2.0^-16 */ 11879 float_raise(float_flag_overflow | float_flag_inexact, fpst); 11880 if (round_to_inf(fpst, f16_sign)) { 11881 return float16_set_sign(float16_infinity, f16_sign); 11882 } else { 11883 return float16_set_sign(float16_maxnorm, f16_sign); 11884 } 11885 } else if (f16_exp >= 29 && fpst->flush_to_zero) { 11886 float_raise(float_flag_underflow, fpst); 11887 return float16_set_sign(float16_zero, float16_is_neg(f16)); 11888 } 11889 11890 f64_frac = call_recip_estimate(&f16_exp, 29, 11891 ((uint64_t) f16_frac) << (52 - 10)); 11892 11893 /* result = sign : result_exp<4:0> : fraction<51:42> */ 11894 f16_val = deposit32(0, 15, 1, f16_sign); 11895 f16_val = deposit32(f16_val, 10, 5, f16_exp); 11896 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10)); 11897 return make_float16(f16_val); 11898 } 11899 11900 float32 HELPER(recpe_f32)(float32 input, void *fpstp) 11901 { 11902 float_status *fpst = fpstp; 11903 float32 f32 = float32_squash_input_denormal(input, fpst); 11904 uint32_t f32_val = float32_val(f32); 11905 bool f32_sign = float32_is_neg(f32); 11906 int f32_exp = extract32(f32_val, 23, 8); 11907 uint32_t f32_frac = extract32(f32_val, 0, 23); 11908 uint64_t f64_frac; 11909 11910 if (float32_is_any_nan(f32)) { 11911 float32 nan = f32; 11912 if (float32_is_signaling_nan(f32, fpst)) { 11913 float_raise(float_flag_invalid, fpst); 11914 nan = float32_silence_nan(f32, fpst); 11915 } 11916 if (fpst->default_nan_mode) { 11917 nan = float32_default_nan(fpst); 11918 } 11919 return nan; 11920 } else if (float32_is_infinity(f32)) { 11921 return float32_set_sign(float32_zero, float32_is_neg(f32)); 11922 } else if (float32_is_zero(f32)) { 11923 float_raise(float_flag_divbyzero, fpst); 11924 return float32_set_sign(float32_infinity, float32_is_neg(f32)); 11925 } else if (float32_abs(f32) < (1ULL << 21)) { 11926 /* Abs(value) < 2.0^-128 */ 11927 float_raise(float_flag_overflow | float_flag_inexact, fpst); 11928 if (round_to_inf(fpst, f32_sign)) { 11929 return float32_set_sign(float32_infinity, f32_sign); 11930 } else { 11931 return float32_set_sign(float32_maxnorm, f32_sign); 11932 } 11933 } else if (f32_exp >= 253 && fpst->flush_to_zero) { 11934 float_raise(float_flag_underflow, fpst); 11935 return float32_set_sign(float32_zero, float32_is_neg(f32)); 11936 } 11937 11938 f64_frac = call_recip_estimate(&f32_exp, 253, 11939 ((uint64_t) f32_frac) << (52 - 23)); 11940 11941 /* result = sign : result_exp<7:0> : fraction<51:29> */ 11942 f32_val = deposit32(0, 31, 1, f32_sign); 11943 f32_val = deposit32(f32_val, 23, 8, f32_exp); 11944 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23)); 11945 return make_float32(f32_val); 11946 } 11947 11948 float64 HELPER(recpe_f64)(float64 input, void *fpstp) 11949 { 11950 float_status *fpst = fpstp; 11951 float64 f64 = float64_squash_input_denormal(input, fpst); 11952 uint64_t f64_val = float64_val(f64); 11953 bool f64_sign = float64_is_neg(f64); 11954 int f64_exp = extract64(f64_val, 52, 11); 11955 uint64_t f64_frac = extract64(f64_val, 0, 52); 11956 11957 /* Deal with any special cases */ 11958 if (float64_is_any_nan(f64)) { 11959 float64 nan = f64; 11960 if (float64_is_signaling_nan(f64, fpst)) { 11961 float_raise(float_flag_invalid, fpst); 11962 nan = float64_silence_nan(f64, fpst); 11963 } 11964 if (fpst->default_nan_mode) { 11965 nan = float64_default_nan(fpst); 11966 } 11967 return nan; 11968 } else if (float64_is_infinity(f64)) { 11969 return float64_set_sign(float64_zero, float64_is_neg(f64)); 11970 } else if (float64_is_zero(f64)) { 11971 float_raise(float_flag_divbyzero, fpst); 11972 return float64_set_sign(float64_infinity, float64_is_neg(f64)); 11973 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) { 11974 /* Abs(value) < 2.0^-1024 */ 11975 float_raise(float_flag_overflow | float_flag_inexact, fpst); 11976 if (round_to_inf(fpst, f64_sign)) { 11977 return float64_set_sign(float64_infinity, f64_sign); 11978 } else { 11979 return float64_set_sign(float64_maxnorm, f64_sign); 11980 } 11981 } else if (f64_exp >= 2045 && fpst->flush_to_zero) { 11982 float_raise(float_flag_underflow, fpst); 11983 return float64_set_sign(float64_zero, float64_is_neg(f64)); 11984 } 11985 11986 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac); 11987 11988 /* result = sign : result_exp<10:0> : fraction<51:0>; */ 11989 f64_val = deposit64(0, 63, 1, f64_sign); 11990 f64_val = deposit64(f64_val, 52, 11, f64_exp); 11991 f64_val = deposit64(f64_val, 0, 52, f64_frac); 11992 return make_float64(f64_val); 11993 } 11994 11995 /* The algorithm that must be used to calculate the estimate 11996 * is specified by the ARM ARM. 11997 */ 11998 11999 static int do_recip_sqrt_estimate(int a) 12000 { 12001 int b, estimate; 12002 12003 assert(128 <= a && a < 512); 12004 if (a < 256) { 12005 a = a * 2 + 1; 12006 } else { 12007 a = (a >> 1) << 1; 12008 a = (a + 1) * 2; 12009 } 12010 b = 512; 12011 while (a * (b + 1) * (b + 1) < (1 << 28)) { 12012 b += 1; 12013 } 12014 estimate = (b + 1) / 2; 12015 assert(256 <= estimate && estimate < 512); 12016 12017 return estimate; 12018 } 12019 12020 12021 static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac) 12022 { 12023 int estimate; 12024 uint32_t scaled; 12025 12026 if (*exp == 0) { 12027 while (extract64(frac, 51, 1) == 0) { 12028 frac = frac << 1; 12029 *exp -= 1; 12030 } 12031 frac = extract64(frac, 0, 51) << 1; 12032 } 12033 12034 if (*exp & 1) { 12035 /* scaled = UInt('01':fraction<51:45>) */ 12036 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7)); 12037 } else { 12038 /* scaled = UInt('1':fraction<51:44>) */ 12039 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8)); 12040 } 12041 estimate = do_recip_sqrt_estimate(scaled); 12042 12043 *exp = (exp_off - *exp) / 2; 12044 return extract64(estimate, 0, 8) << 44; 12045 } 12046 12047 uint32_t HELPER(rsqrte_f16)(uint32_t input, void *fpstp) 12048 { 12049 float_status *s = fpstp; 12050 float16 f16 = float16_squash_input_denormal(input, s); 12051 uint16_t val = float16_val(f16); 12052 bool f16_sign = float16_is_neg(f16); 12053 int f16_exp = extract32(val, 10, 5); 12054 uint16_t f16_frac = extract32(val, 0, 10); 12055 uint64_t f64_frac; 12056 12057 if (float16_is_any_nan(f16)) { 12058 float16 nan = f16; 12059 if (float16_is_signaling_nan(f16, s)) { 12060 float_raise(float_flag_invalid, s); 12061 nan = float16_silence_nan(f16, s); 12062 } 12063 if (s->default_nan_mode) { 12064 nan = float16_default_nan(s); 12065 } 12066 return nan; 12067 } else if (float16_is_zero(f16)) { 12068 float_raise(float_flag_divbyzero, s); 12069 return float16_set_sign(float16_infinity, f16_sign); 12070 } else if (f16_sign) { 12071 float_raise(float_flag_invalid, s); 12072 return float16_default_nan(s); 12073 } else if (float16_is_infinity(f16)) { 12074 return float16_zero; 12075 } 12076 12077 /* Scale and normalize to a double-precision value between 0.25 and 1.0, 12078 * preserving the parity of the exponent. */ 12079 12080 f64_frac = ((uint64_t) f16_frac) << (52 - 10); 12081 12082 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac); 12083 12084 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */ 12085 val = deposit32(0, 15, 1, f16_sign); 12086 val = deposit32(val, 10, 5, f16_exp); 12087 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8)); 12088 return make_float16(val); 12089 } 12090 12091 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp) 12092 { 12093 float_status *s = fpstp; 12094 float32 f32 = float32_squash_input_denormal(input, s); 12095 uint32_t val = float32_val(f32); 12096 uint32_t f32_sign = float32_is_neg(f32); 12097 int f32_exp = extract32(val, 23, 8); 12098 uint32_t f32_frac = extract32(val, 0, 23); 12099 uint64_t f64_frac; 12100 12101 if (float32_is_any_nan(f32)) { 12102 float32 nan = f32; 12103 if (float32_is_signaling_nan(f32, s)) { 12104 float_raise(float_flag_invalid, s); 12105 nan = float32_silence_nan(f32, s); 12106 } 12107 if (s->default_nan_mode) { 12108 nan = float32_default_nan(s); 12109 } 12110 return nan; 12111 } else if (float32_is_zero(f32)) { 12112 float_raise(float_flag_divbyzero, s); 12113 return float32_set_sign(float32_infinity, float32_is_neg(f32)); 12114 } else if (float32_is_neg(f32)) { 12115 float_raise(float_flag_invalid, s); 12116 return float32_default_nan(s); 12117 } else if (float32_is_infinity(f32)) { 12118 return float32_zero; 12119 } 12120 12121 /* Scale and normalize to a double-precision value between 0.25 and 1.0, 12122 * preserving the parity of the exponent. */ 12123 12124 f64_frac = ((uint64_t) f32_frac) << 29; 12125 12126 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac); 12127 12128 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */ 12129 val = deposit32(0, 31, 1, f32_sign); 12130 val = deposit32(val, 23, 8, f32_exp); 12131 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8)); 12132 return make_float32(val); 12133 } 12134 12135 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp) 12136 { 12137 float_status *s = fpstp; 12138 float64 f64 = float64_squash_input_denormal(input, s); 12139 uint64_t val = float64_val(f64); 12140 bool f64_sign = float64_is_neg(f64); 12141 int f64_exp = extract64(val, 52, 11); 12142 uint64_t f64_frac = extract64(val, 0, 52); 12143 12144 if (float64_is_any_nan(f64)) { 12145 float64 nan = f64; 12146 if (float64_is_signaling_nan(f64, s)) { 12147 float_raise(float_flag_invalid, s); 12148 nan = float64_silence_nan(f64, s); 12149 } 12150 if (s->default_nan_mode) { 12151 nan = float64_default_nan(s); 12152 } 12153 return nan; 12154 } else if (float64_is_zero(f64)) { 12155 float_raise(float_flag_divbyzero, s); 12156 return float64_set_sign(float64_infinity, float64_is_neg(f64)); 12157 } else if (float64_is_neg(f64)) { 12158 float_raise(float_flag_invalid, s); 12159 return float64_default_nan(s); 12160 } else if (float64_is_infinity(f64)) { 12161 return float64_zero; 12162 } 12163 12164 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac); 12165 12166 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */ 12167 val = deposit64(0, 61, 1, f64_sign); 12168 val = deposit64(val, 52, 11, f64_exp); 12169 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8)); 12170 return make_float64(val); 12171 } 12172 12173 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp) 12174 { 12175 /* float_status *s = fpstp; */ 12176 int input, estimate; 12177 12178 if ((a & 0x80000000) == 0) { 12179 return 0xffffffff; 12180 } 12181 12182 input = extract32(a, 23, 9); 12183 estimate = recip_estimate(input); 12184 12185 return deposit32(0, (32 - 9), 9, estimate); 12186 } 12187 12188 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp) 12189 { 12190 int estimate; 12191 12192 if ((a & 0xc0000000) == 0) { 12193 return 0xffffffff; 12194 } 12195 12196 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9)); 12197 12198 return deposit32(0, 23, 9, estimate); 12199 } 12200 12201 /* VFPv4 fused multiply-accumulate */ 12202 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp) 12203 { 12204 float_status *fpst = fpstp; 12205 return float32_muladd(a, b, c, 0, fpst); 12206 } 12207 12208 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp) 12209 { 12210 float_status *fpst = fpstp; 12211 return float64_muladd(a, b, c, 0, fpst); 12212 } 12213 12214 /* ARMv8 round to integral */ 12215 float32 HELPER(rints_exact)(float32 x, void *fp_status) 12216 { 12217 return float32_round_to_int(x, fp_status); 12218 } 12219 12220 float64 HELPER(rintd_exact)(float64 x, void *fp_status) 12221 { 12222 return float64_round_to_int(x, fp_status); 12223 } 12224 12225 float32 HELPER(rints)(float32 x, void *fp_status) 12226 { 12227 int old_flags = get_float_exception_flags(fp_status), new_flags; 12228 float32 ret; 12229 12230 ret = float32_round_to_int(x, fp_status); 12231 12232 /* Suppress any inexact exceptions the conversion produced */ 12233 if (!(old_flags & float_flag_inexact)) { 12234 new_flags = get_float_exception_flags(fp_status); 12235 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); 12236 } 12237 12238 return ret; 12239 } 12240 12241 float64 HELPER(rintd)(float64 x, void *fp_status) 12242 { 12243 int old_flags = get_float_exception_flags(fp_status), new_flags; 12244 float64 ret; 12245 12246 ret = float64_round_to_int(x, fp_status); 12247 12248 new_flags = get_float_exception_flags(fp_status); 12249 12250 /* Suppress any inexact exceptions the conversion produced */ 12251 if (!(old_flags & float_flag_inexact)) { 12252 new_flags = get_float_exception_flags(fp_status); 12253 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); 12254 } 12255 12256 return ret; 12257 } 12258 12259 /* Convert ARM rounding mode to softfloat */ 12260 int arm_rmode_to_sf(int rmode) 12261 { 12262 switch (rmode) { 12263 case FPROUNDING_TIEAWAY: 12264 rmode = float_round_ties_away; 12265 break; 12266 case FPROUNDING_ODD: 12267 /* FIXME: add support for TIEAWAY and ODD */ 12268 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n", 12269 rmode); 12270 case FPROUNDING_TIEEVEN: 12271 default: 12272 rmode = float_round_nearest_even; 12273 break; 12274 case FPROUNDING_POSINF: 12275 rmode = float_round_up; 12276 break; 12277 case FPROUNDING_NEGINF: 12278 rmode = float_round_down; 12279 break; 12280 case FPROUNDING_ZERO: 12281 rmode = float_round_to_zero; 12282 break; 12283 } 12284 return rmode; 12285 } 12286 12287 /* CRC helpers. 12288 * The upper bytes of val (above the number specified by 'bytes') must have 12289 * been zeroed out by the caller. 12290 */ 12291 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes) 12292 { 12293 uint8_t buf[4]; 12294 12295 stl_le_p(buf, val); 12296 12297 /* zlib crc32 converts the accumulator and output to one's complement. */ 12298 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; 12299 } 12300 12301 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) 12302 { 12303 uint8_t buf[4]; 12304 12305 stl_le_p(buf, val); 12306 12307 /* Linux crc32c converts the output to one's complement. */ 12308 return crc32c(acc, buf, bytes) ^ 0xffffffff; 12309 } 12310 12311 /* Return the exception level to which FP-disabled exceptions should 12312 * be taken, or 0 if FP is enabled. 12313 */ 12314 static inline int fp_exception_el(CPUARMState *env) 12315 { 12316 #ifndef CONFIG_USER_ONLY 12317 int fpen; 12318 int cur_el = arm_current_el(env); 12319 12320 /* CPACR and the CPTR registers don't exist before v6, so FP is 12321 * always accessible 12322 */ 12323 if (!arm_feature(env, ARM_FEATURE_V6)) { 12324 return 0; 12325 } 12326 12327 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit: 12328 * 0, 2 : trap EL0 and EL1/PL1 accesses 12329 * 1 : trap only EL0 accesses 12330 * 3 : trap no accesses 12331 */ 12332 fpen = extract32(env->cp15.cpacr_el1, 20, 2); 12333 switch (fpen) { 12334 case 0: 12335 case 2: 12336 if (cur_el == 0 || cur_el == 1) { 12337 /* Trap to PL1, which might be EL1 or EL3 */ 12338 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) { 12339 return 3; 12340 } 12341 return 1; 12342 } 12343 if (cur_el == 3 && !is_a64(env)) { 12344 /* Secure PL1 running at EL3 */ 12345 return 3; 12346 } 12347 break; 12348 case 1: 12349 if (cur_el == 0) { 12350 return 1; 12351 } 12352 break; 12353 case 3: 12354 break; 12355 } 12356 12357 /* For the CPTR registers we don't need to guard with an ARM_FEATURE 12358 * check because zero bits in the registers mean "don't trap". 12359 */ 12360 12361 /* CPTR_EL2 : present in v7VE or v8 */ 12362 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1) 12363 && !arm_is_secure_below_el3(env)) { 12364 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */ 12365 return 2; 12366 } 12367 12368 /* CPTR_EL3 : present in v8 */ 12369 if (extract32(env->cp15.cptr_el[3], 10, 1)) { 12370 /* Trap all FP ops to EL3 */ 12371 return 3; 12372 } 12373 #endif 12374 return 0; 12375 } 12376 12377 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc, 12378 target_ulong *cs_base, uint32_t *pflags) 12379 { 12380 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false)); 12381 int fp_el = fp_exception_el(env); 12382 uint32_t flags; 12383 12384 if (is_a64(env)) { 12385 int sve_el = sve_exception_el(env); 12386 uint32_t zcr_len; 12387 12388 *pc = env->pc; 12389 flags = ARM_TBFLAG_AARCH64_STATE_MASK; 12390 /* Get control bits for tagged addresses */ 12391 flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT); 12392 flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT); 12393 flags |= sve_el << ARM_TBFLAG_SVEEXC_EL_SHIFT; 12394 12395 /* If SVE is disabled, but FP is enabled, 12396 then the effective len is 0. */ 12397 if (sve_el != 0 && fp_el == 0) { 12398 zcr_len = 0; 12399 } else { 12400 int current_el = arm_current_el(env); 12401 12402 zcr_len = env->vfp.zcr_el[current_el <= 1 ? 1 : current_el]; 12403 zcr_len &= 0xf; 12404 if (current_el < 2 && arm_feature(env, ARM_FEATURE_EL2)) { 12405 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]); 12406 } 12407 if (current_el < 3 && arm_feature(env, ARM_FEATURE_EL3)) { 12408 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]); 12409 } 12410 } 12411 flags |= zcr_len << ARM_TBFLAG_ZCR_LEN_SHIFT; 12412 } else { 12413 *pc = env->regs[15]; 12414 flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT) 12415 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT) 12416 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT) 12417 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT) 12418 | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT); 12419 if (!(access_secure_reg(env))) { 12420 flags |= ARM_TBFLAG_NS_MASK; 12421 } 12422 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30) 12423 || arm_el_is_aa64(env, 1)) { 12424 flags |= ARM_TBFLAG_VFPEN_MASK; 12425 } 12426 flags |= (extract32(env->cp15.c15_cpar, 0, 2) 12427 << ARM_TBFLAG_XSCALE_CPAR_SHIFT); 12428 } 12429 12430 flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT); 12431 12432 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine 12433 * states defined in the ARM ARM for software singlestep: 12434 * SS_ACTIVE PSTATE.SS State 12435 * 0 x Inactive (the TB flag for SS is always 0) 12436 * 1 0 Active-pending 12437 * 1 1 Active-not-pending 12438 */ 12439 if (arm_singlestep_active(env)) { 12440 flags |= ARM_TBFLAG_SS_ACTIVE_MASK; 12441 if (is_a64(env)) { 12442 if (env->pstate & PSTATE_SS) { 12443 flags |= ARM_TBFLAG_PSTATE_SS_MASK; 12444 } 12445 } else { 12446 if (env->uncached_cpsr & PSTATE_SS) { 12447 flags |= ARM_TBFLAG_PSTATE_SS_MASK; 12448 } 12449 } 12450 } 12451 if (arm_cpu_data_is_big_endian(env)) { 12452 flags |= ARM_TBFLAG_BE_DATA_MASK; 12453 } 12454 flags |= fp_el << ARM_TBFLAG_FPEXC_EL_SHIFT; 12455 12456 if (arm_v7m_is_handler_mode(env)) { 12457 flags |= ARM_TBFLAG_HANDLER_MASK; 12458 } 12459 12460 *pflags = flags; 12461 *cs_base = 0; 12462 } 12463