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 void switch_mode(CPUARMState *env, int mode); 60 61 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) 62 { 63 int nregs; 64 65 /* VFP data registers are always little-endian. */ 66 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; 67 if (reg < nregs) { 68 stq_le_p(buf, *aa32_vfp_dreg(env, reg)); 69 return 8; 70 } 71 if (arm_feature(env, ARM_FEATURE_NEON)) { 72 /* Aliases for Q regs. */ 73 nregs += 16; 74 if (reg < nregs) { 75 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 76 stq_le_p(buf, q[0]); 77 stq_le_p(buf + 8, q[1]); 78 return 16; 79 } 80 } 81 switch (reg - nregs) { 82 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4; 83 case 1: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSCR]); return 4; 84 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4; 85 } 86 return 0; 87 } 88 89 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 90 { 91 int nregs; 92 93 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; 94 if (reg < nregs) { 95 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf); 96 return 8; 97 } 98 if (arm_feature(env, ARM_FEATURE_NEON)) { 99 nregs += 16; 100 if (reg < nregs) { 101 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 102 q[0] = ldq_le_p(buf); 103 q[1] = ldq_le_p(buf + 8); 104 return 16; 105 } 106 } 107 switch (reg - nregs) { 108 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; 109 case 1: env->vfp.xregs[ARM_VFP_FPSCR] = ldl_p(buf); return 4; 110 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; 111 } 112 return 0; 113 } 114 115 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) 116 { 117 switch (reg) { 118 case 0 ... 31: 119 /* 128 bit FP register */ 120 { 121 uint64_t *q = aa64_vfp_qreg(env, reg); 122 stq_le_p(buf, q[0]); 123 stq_le_p(buf + 8, q[1]); 124 return 16; 125 } 126 case 32: 127 /* FPSR */ 128 stl_p(buf, vfp_get_fpsr(env)); 129 return 4; 130 case 33: 131 /* FPCR */ 132 stl_p(buf, vfp_get_fpcr(env)); 133 return 4; 134 default: 135 return 0; 136 } 137 } 138 139 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 140 { 141 switch (reg) { 142 case 0 ... 31: 143 /* 128 bit FP register */ 144 { 145 uint64_t *q = aa64_vfp_qreg(env, reg); 146 q[0] = ldq_le_p(buf); 147 q[1] = ldq_le_p(buf + 8); 148 return 16; 149 } 150 case 32: 151 /* FPSR */ 152 vfp_set_fpsr(env, ldl_p(buf)); 153 return 4; 154 case 33: 155 /* FPCR */ 156 vfp_set_fpcr(env, ldl_p(buf)); 157 return 4; 158 default: 159 return 0; 160 } 161 } 162 163 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) 164 { 165 assert(ri->fieldoffset); 166 if (cpreg_field_is_64bit(ri)) { 167 return CPREG_FIELD64(env, ri); 168 } else { 169 return CPREG_FIELD32(env, ri); 170 } 171 } 172 173 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 174 uint64_t value) 175 { 176 assert(ri->fieldoffset); 177 if (cpreg_field_is_64bit(ri)) { 178 CPREG_FIELD64(env, ri) = value; 179 } else { 180 CPREG_FIELD32(env, ri) = value; 181 } 182 } 183 184 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) 185 { 186 return (char *)env + ri->fieldoffset; 187 } 188 189 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) 190 { 191 /* Raw read of a coprocessor register (as needed for migration, etc). */ 192 if (ri->type & ARM_CP_CONST) { 193 return ri->resetvalue; 194 } else if (ri->raw_readfn) { 195 return ri->raw_readfn(env, ri); 196 } else if (ri->readfn) { 197 return ri->readfn(env, ri); 198 } else { 199 return raw_read(env, ri); 200 } 201 } 202 203 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, 204 uint64_t v) 205 { 206 /* Raw write of a coprocessor register (as needed for migration, etc). 207 * Note that constant registers are treated as write-ignored; the 208 * caller should check for success by whether a readback gives the 209 * value written. 210 */ 211 if (ri->type & ARM_CP_CONST) { 212 return; 213 } else if (ri->raw_writefn) { 214 ri->raw_writefn(env, ri, v); 215 } else if (ri->writefn) { 216 ri->writefn(env, ri, v); 217 } else { 218 raw_write(env, ri, v); 219 } 220 } 221 222 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg) 223 { 224 ARMCPU *cpu = arm_env_get_cpu(env); 225 const ARMCPRegInfo *ri; 226 uint32_t key; 227 228 key = cpu->dyn_xml.cpregs_keys[reg]; 229 ri = get_arm_cp_reginfo(cpu->cp_regs, key); 230 if (ri) { 231 if (cpreg_field_is_64bit(ri)) { 232 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri)); 233 } else { 234 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri)); 235 } 236 } 237 return 0; 238 } 239 240 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg) 241 { 242 return 0; 243 } 244 245 static bool raw_accessors_invalid(const ARMCPRegInfo *ri) 246 { 247 /* Return true if the regdef would cause an assertion if you called 248 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a 249 * program bug for it not to have the NO_RAW flag). 250 * NB that returning false here doesn't necessarily mean that calling 251 * read/write_raw_cp_reg() is safe, because we can't distinguish "has 252 * read/write access functions which are safe for raw use" from "has 253 * read/write access functions which have side effects but has forgotten 254 * to provide raw access functions". 255 * The tests here line up with the conditions in read/write_raw_cp_reg() 256 * and assertions in raw_read()/raw_write(). 257 */ 258 if ((ri->type & ARM_CP_CONST) || 259 ri->fieldoffset || 260 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { 261 return false; 262 } 263 return true; 264 } 265 266 bool write_cpustate_to_list(ARMCPU *cpu) 267 { 268 /* Write the coprocessor state from cpu->env to the (index,value) list. */ 269 int i; 270 bool ok = true; 271 272 for (i = 0; i < cpu->cpreg_array_len; i++) { 273 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 274 const ARMCPRegInfo *ri; 275 276 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 277 if (!ri) { 278 ok = false; 279 continue; 280 } 281 if (ri->type & ARM_CP_NO_RAW) { 282 continue; 283 } 284 cpu->cpreg_values[i] = read_raw_cp_reg(&cpu->env, ri); 285 } 286 return ok; 287 } 288 289 bool write_list_to_cpustate(ARMCPU *cpu) 290 { 291 int i; 292 bool ok = true; 293 294 for (i = 0; i < cpu->cpreg_array_len; i++) { 295 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 296 uint64_t v = cpu->cpreg_values[i]; 297 const ARMCPRegInfo *ri; 298 299 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 300 if (!ri) { 301 ok = false; 302 continue; 303 } 304 if (ri->type & ARM_CP_NO_RAW) { 305 continue; 306 } 307 /* Write value and confirm it reads back as written 308 * (to catch read-only registers and partially read-only 309 * registers where the incoming migration value doesn't match) 310 */ 311 write_raw_cp_reg(&cpu->env, ri, v); 312 if (read_raw_cp_reg(&cpu->env, ri) != v) { 313 ok = false; 314 } 315 } 316 return ok; 317 } 318 319 static void add_cpreg_to_list(gpointer key, gpointer opaque) 320 { 321 ARMCPU *cpu = opaque; 322 uint64_t regidx; 323 const ARMCPRegInfo *ri; 324 325 regidx = *(uint32_t *)key; 326 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 327 328 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 329 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); 330 /* The value array need not be initialized at this point */ 331 cpu->cpreg_array_len++; 332 } 333 } 334 335 static void count_cpreg(gpointer key, gpointer opaque) 336 { 337 ARMCPU *cpu = opaque; 338 uint64_t regidx; 339 const ARMCPRegInfo *ri; 340 341 regidx = *(uint32_t *)key; 342 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 343 344 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 345 cpu->cpreg_array_len++; 346 } 347 } 348 349 static gint cpreg_key_compare(gconstpointer a, gconstpointer b) 350 { 351 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a); 352 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b); 353 354 if (aidx > bidx) { 355 return 1; 356 } 357 if (aidx < bidx) { 358 return -1; 359 } 360 return 0; 361 } 362 363 void init_cpreg_list(ARMCPU *cpu) 364 { 365 /* Initialise the cpreg_tuples[] array based on the cp_regs hash. 366 * Note that we require cpreg_tuples[] to be sorted by key ID. 367 */ 368 GList *keys; 369 int arraylen; 370 371 keys = g_hash_table_get_keys(cpu->cp_regs); 372 keys = g_list_sort(keys, cpreg_key_compare); 373 374 cpu->cpreg_array_len = 0; 375 376 g_list_foreach(keys, count_cpreg, cpu); 377 378 arraylen = cpu->cpreg_array_len; 379 cpu->cpreg_indexes = g_new(uint64_t, arraylen); 380 cpu->cpreg_values = g_new(uint64_t, arraylen); 381 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); 382 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); 383 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; 384 cpu->cpreg_array_len = 0; 385 386 g_list_foreach(keys, add_cpreg_to_list, cpu); 387 388 assert(cpu->cpreg_array_len == arraylen); 389 390 g_list_free(keys); 391 } 392 393 /* 394 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but 395 * they are accessible when EL3 is using AArch64 regardless of EL3.NS. 396 * 397 * access_el3_aa32ns: Used to check AArch32 register views. 398 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views. 399 */ 400 static CPAccessResult access_el3_aa32ns(CPUARMState *env, 401 const ARMCPRegInfo *ri, 402 bool isread) 403 { 404 bool secure = arm_is_secure_below_el3(env); 405 406 assert(!arm_el_is_aa64(env, 3)); 407 if (secure) { 408 return CP_ACCESS_TRAP_UNCATEGORIZED; 409 } 410 return CP_ACCESS_OK; 411 } 412 413 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env, 414 const ARMCPRegInfo *ri, 415 bool isread) 416 { 417 if (!arm_el_is_aa64(env, 3)) { 418 return access_el3_aa32ns(env, ri, isread); 419 } 420 return CP_ACCESS_OK; 421 } 422 423 /* Some secure-only AArch32 registers trap to EL3 if used from 424 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts). 425 * Note that an access from Secure EL1 can only happen if EL3 is AArch64. 426 * We assume that the .access field is set to PL1_RW. 427 */ 428 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env, 429 const ARMCPRegInfo *ri, 430 bool isread) 431 { 432 if (arm_current_el(env) == 3) { 433 return CP_ACCESS_OK; 434 } 435 if (arm_is_secure_below_el3(env)) { 436 return CP_ACCESS_TRAP_EL3; 437 } 438 /* This will be EL1 NS and EL2 NS, which just UNDEF */ 439 return CP_ACCESS_TRAP_UNCATEGORIZED; 440 } 441 442 /* Check for traps to "powerdown debug" registers, which are controlled 443 * by MDCR.TDOSA 444 */ 445 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri, 446 bool isread) 447 { 448 int el = arm_current_el(env); 449 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) || 450 (env->cp15.mdcr_el2 & MDCR_TDE) || 451 (env->cp15.hcr_el2 & HCR_TGE); 452 453 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) { 454 return CP_ACCESS_TRAP_EL2; 455 } 456 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) { 457 return CP_ACCESS_TRAP_EL3; 458 } 459 return CP_ACCESS_OK; 460 } 461 462 /* Check for traps to "debug ROM" registers, which are controlled 463 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3. 464 */ 465 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri, 466 bool isread) 467 { 468 int el = arm_current_el(env); 469 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) || 470 (env->cp15.mdcr_el2 & MDCR_TDE) || 471 (env->cp15.hcr_el2 & HCR_TGE); 472 473 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) { 474 return CP_ACCESS_TRAP_EL2; 475 } 476 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 477 return CP_ACCESS_TRAP_EL3; 478 } 479 return CP_ACCESS_OK; 480 } 481 482 /* Check for traps to general debug registers, which are controlled 483 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3. 484 */ 485 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri, 486 bool isread) 487 { 488 int el = arm_current_el(env); 489 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) || 490 (env->cp15.mdcr_el2 & MDCR_TDE) || 491 (env->cp15.hcr_el2 & HCR_TGE); 492 493 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) { 494 return CP_ACCESS_TRAP_EL2; 495 } 496 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 497 return CP_ACCESS_TRAP_EL3; 498 } 499 return CP_ACCESS_OK; 500 } 501 502 /* Check for traps to performance monitor registers, which are controlled 503 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3. 504 */ 505 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri, 506 bool isread) 507 { 508 int el = arm_current_el(env); 509 510 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 511 && !arm_is_secure_below_el3(env)) { 512 return CP_ACCESS_TRAP_EL2; 513 } 514 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 515 return CP_ACCESS_TRAP_EL3; 516 } 517 return CP_ACCESS_OK; 518 } 519 520 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 521 { 522 ARMCPU *cpu = arm_env_get_cpu(env); 523 524 raw_write(env, ri, value); 525 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */ 526 } 527 528 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 529 { 530 ARMCPU *cpu = arm_env_get_cpu(env); 531 532 if (raw_read(env, ri) != value) { 533 /* Unlike real hardware the qemu TLB uses virtual addresses, 534 * not modified virtual addresses, so this causes a TLB flush. 535 */ 536 tlb_flush(CPU(cpu)); 537 raw_write(env, ri, value); 538 } 539 } 540 541 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, 542 uint64_t value) 543 { 544 ARMCPU *cpu = arm_env_get_cpu(env); 545 546 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA) 547 && !extended_addresses_enabled(env)) { 548 /* For VMSA (when not using the LPAE long descriptor page table 549 * format) this register includes the ASID, so do a TLB flush. 550 * For PMSA it is purely a process ID and no action is needed. 551 */ 552 tlb_flush(CPU(cpu)); 553 } 554 raw_write(env, ri, value); 555 } 556 557 /* IS variants of TLB operations must affect all cores */ 558 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 559 uint64_t value) 560 { 561 CPUState *cs = ENV_GET_CPU(env); 562 563 tlb_flush_all_cpus_synced(cs); 564 } 565 566 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 567 uint64_t value) 568 { 569 CPUState *cs = ENV_GET_CPU(env); 570 571 tlb_flush_all_cpus_synced(cs); 572 } 573 574 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 575 uint64_t value) 576 { 577 CPUState *cs = ENV_GET_CPU(env); 578 579 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 580 } 581 582 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 583 uint64_t value) 584 { 585 CPUState *cs = ENV_GET_CPU(env); 586 587 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 588 } 589 590 /* 591 * Non-IS variants of TLB operations are upgraded to 592 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to 593 * force broadcast of these operations. 594 */ 595 static bool tlb_force_broadcast(CPUARMState *env) 596 { 597 return (env->cp15.hcr_el2 & HCR_FB) && 598 arm_current_el(env) == 1 && arm_is_secure_below_el3(env); 599 } 600 601 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, 602 uint64_t value) 603 { 604 /* Invalidate all (TLBIALL) */ 605 ARMCPU *cpu = arm_env_get_cpu(env); 606 607 if (tlb_force_broadcast(env)) { 608 tlbiall_is_write(env, NULL, value); 609 return; 610 } 611 612 tlb_flush(CPU(cpu)); 613 } 614 615 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, 616 uint64_t value) 617 { 618 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ 619 ARMCPU *cpu = arm_env_get_cpu(env); 620 621 if (tlb_force_broadcast(env)) { 622 tlbimva_is_write(env, NULL, value); 623 return; 624 } 625 626 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); 627 } 628 629 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, 630 uint64_t value) 631 { 632 /* Invalidate by ASID (TLBIASID) */ 633 ARMCPU *cpu = arm_env_get_cpu(env); 634 635 if (tlb_force_broadcast(env)) { 636 tlbiasid_is_write(env, NULL, value); 637 return; 638 } 639 640 tlb_flush(CPU(cpu)); 641 } 642 643 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, 644 uint64_t value) 645 { 646 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ 647 ARMCPU *cpu = arm_env_get_cpu(env); 648 649 if (tlb_force_broadcast(env)) { 650 tlbimvaa_is_write(env, NULL, value); 651 return; 652 } 653 654 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); 655 } 656 657 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri, 658 uint64_t value) 659 { 660 CPUState *cs = ENV_GET_CPU(env); 661 662 tlb_flush_by_mmuidx(cs, 663 ARMMMUIdxBit_S12NSE1 | 664 ARMMMUIdxBit_S12NSE0 | 665 ARMMMUIdxBit_S2NS); 666 } 667 668 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 669 uint64_t value) 670 { 671 CPUState *cs = ENV_GET_CPU(env); 672 673 tlb_flush_by_mmuidx_all_cpus_synced(cs, 674 ARMMMUIdxBit_S12NSE1 | 675 ARMMMUIdxBit_S12NSE0 | 676 ARMMMUIdxBit_S2NS); 677 } 678 679 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri, 680 uint64_t value) 681 { 682 /* Invalidate by IPA. This has to invalidate any structures that 683 * contain only stage 2 translation information, but does not need 684 * to apply to structures that contain combined stage 1 and stage 2 685 * translation information. 686 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. 687 */ 688 CPUState *cs = ENV_GET_CPU(env); 689 uint64_t pageaddr; 690 691 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 692 return; 693 } 694 695 pageaddr = sextract64(value << 12, 0, 40); 696 697 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS); 698 } 699 700 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 701 uint64_t value) 702 { 703 CPUState *cs = ENV_GET_CPU(env); 704 uint64_t pageaddr; 705 706 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 707 return; 708 } 709 710 pageaddr = sextract64(value << 12, 0, 40); 711 712 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 713 ARMMMUIdxBit_S2NS); 714 } 715 716 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 717 uint64_t value) 718 { 719 CPUState *cs = ENV_GET_CPU(env); 720 721 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2); 722 } 723 724 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 725 uint64_t value) 726 { 727 CPUState *cs = ENV_GET_CPU(env); 728 729 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2); 730 } 731 732 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 733 uint64_t value) 734 { 735 CPUState *cs = ENV_GET_CPU(env); 736 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 737 738 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2); 739 } 740 741 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 742 uint64_t value) 743 { 744 CPUState *cs = ENV_GET_CPU(env); 745 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 746 747 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 748 ARMMMUIdxBit_S1E2); 749 } 750 751 static const ARMCPRegInfo cp_reginfo[] = { 752 /* Define the secure and non-secure FCSE identifier CP registers 753 * separately because there is no secure bank in V8 (no _EL3). This allows 754 * the secure register to be properly reset and migrated. There is also no 755 * v8 EL1 version of the register so the non-secure instance stands alone. 756 */ 757 { .name = "FCSEIDR", 758 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 759 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 760 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns), 761 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 762 { .name = "FCSEIDR_S", 763 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 764 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 765 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s), 766 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 767 /* Define the secure and non-secure context identifier CP registers 768 * separately because there is no secure bank in V8 (no _EL3). This allows 769 * the secure register to be properly reset and migrated. In the 770 * non-secure case, the 32-bit register will have reset and migration 771 * disabled during registration as it is handled by the 64-bit instance. 772 */ 773 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH, 774 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 775 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 776 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]), 777 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 778 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32, 779 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 780 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 781 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s), 782 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 783 REGINFO_SENTINEL 784 }; 785 786 static const ARMCPRegInfo not_v8_cp_reginfo[] = { 787 /* NB: Some of these registers exist in v8 but with more precise 788 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). 789 */ 790 /* MMU Domain access control / MPU write buffer control */ 791 { .name = "DACR", 792 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, 793 .access = PL1_RW, .resetvalue = 0, 794 .writefn = dacr_write, .raw_writefn = raw_write, 795 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 796 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 797 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs. 798 * For v6 and v5, these mappings are overly broad. 799 */ 800 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0, 801 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 802 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1, 803 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 804 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4, 805 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 806 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8, 807 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 808 /* Cache maintenance ops; some of this space may be overridden later. */ 809 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 810 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 811 .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, 812 REGINFO_SENTINEL 813 }; 814 815 static const ARMCPRegInfo not_v6_cp_reginfo[] = { 816 /* Not all pre-v6 cores implemented this WFI, so this is slightly 817 * over-broad. 818 */ 819 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, 820 .access = PL1_W, .type = ARM_CP_WFI }, 821 REGINFO_SENTINEL 822 }; 823 824 static const ARMCPRegInfo not_v7_cp_reginfo[] = { 825 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which 826 * is UNPREDICTABLE; we choose to NOP as most implementations do). 827 */ 828 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 829 .access = PL1_W, .type = ARM_CP_WFI }, 830 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice 831 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and 832 * OMAPCP will override this space. 833 */ 834 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, 835 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), 836 .resetvalue = 0 }, 837 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, 838 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), 839 .resetvalue = 0 }, 840 /* v6 doesn't have the cache ID registers but Linux reads them anyway */ 841 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, 842 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 843 .resetvalue = 0 }, 844 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; 845 * implementing it as RAZ means the "debug architecture version" bits 846 * will read as a reserved value, which should cause Linux to not try 847 * to use the debug hardware. 848 */ 849 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 850 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 851 /* MMU TLB control. Note that the wildcarding means we cover not just 852 * the unified TLB ops but also the dside/iside/inner-shareable variants. 853 */ 854 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, 855 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, 856 .type = ARM_CP_NO_RAW }, 857 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, 858 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, 859 .type = ARM_CP_NO_RAW }, 860 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, 861 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, 862 .type = ARM_CP_NO_RAW }, 863 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, 864 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, 865 .type = ARM_CP_NO_RAW }, 866 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2, 867 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP }, 868 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2, 869 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP }, 870 REGINFO_SENTINEL 871 }; 872 873 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, 874 uint64_t value) 875 { 876 uint32_t mask = 0; 877 878 /* In ARMv8 most bits of CPACR_EL1 are RES0. */ 879 if (!arm_feature(env, ARM_FEATURE_V8)) { 880 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. 881 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. 882 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. 883 */ 884 if (arm_feature(env, ARM_FEATURE_VFP)) { 885 /* VFP coprocessor: cp10 & cp11 [23:20] */ 886 mask |= (1 << 31) | (1 << 30) | (0xf << 20); 887 888 if (!arm_feature(env, ARM_FEATURE_NEON)) { 889 /* ASEDIS [31] bit is RAO/WI */ 890 value |= (1 << 31); 891 } 892 893 /* VFPv3 and upwards with NEON implement 32 double precision 894 * registers (D0-D31). 895 */ 896 if (!arm_feature(env, ARM_FEATURE_NEON) || 897 !arm_feature(env, ARM_FEATURE_VFP3)) { 898 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ 899 value |= (1 << 30); 900 } 901 } 902 value &= mask; 903 } 904 env->cp15.cpacr_el1 = value; 905 } 906 907 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 908 { 909 /* Call cpacr_write() so that we reset with the correct RAO bits set 910 * for our CPU features. 911 */ 912 cpacr_write(env, ri, 0); 913 } 914 915 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 916 bool isread) 917 { 918 if (arm_feature(env, ARM_FEATURE_V8)) { 919 /* Check if CPACR accesses are to be trapped to EL2 */ 920 if (arm_current_el(env) == 1 && 921 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) { 922 return CP_ACCESS_TRAP_EL2; 923 /* Check if CPACR accesses are to be trapped to EL3 */ 924 } else if (arm_current_el(env) < 3 && 925 (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 926 return CP_ACCESS_TRAP_EL3; 927 } 928 } 929 930 return CP_ACCESS_OK; 931 } 932 933 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri, 934 bool isread) 935 { 936 /* Check if CPTR accesses are set to trap to EL3 */ 937 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 938 return CP_ACCESS_TRAP_EL3; 939 } 940 941 return CP_ACCESS_OK; 942 } 943 944 static const ARMCPRegInfo v6_cp_reginfo[] = { 945 /* prefetch by MVA in v6, NOP in v7 */ 946 { .name = "MVA_prefetch", 947 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, 948 .access = PL1_W, .type = ARM_CP_NOP }, 949 /* We need to break the TB after ISB to execute self-modifying code 950 * correctly and also to take any pending interrupts immediately. 951 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag. 952 */ 953 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, 954 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore }, 955 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, 956 .access = PL0_W, .type = ARM_CP_NOP }, 957 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, 958 .access = PL0_W, .type = ARM_CP_NOP }, 959 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, 960 .access = PL1_RW, 961 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), 962 offsetof(CPUARMState, cp15.ifar_ns) }, 963 .resetvalue = 0, }, 964 /* Watchpoint Fault Address Register : should actually only be present 965 * for 1136, 1176, 11MPCore. 966 */ 967 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, 968 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, 969 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, 970 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access, 971 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1), 972 .resetfn = cpacr_reset, .writefn = cpacr_write }, 973 REGINFO_SENTINEL 974 }; 975 976 /* Definitions for the PMU registers */ 977 #define PMCRN_MASK 0xf800 978 #define PMCRN_SHIFT 11 979 #define PMCRD 0x8 980 #define PMCRC 0x4 981 #define PMCRE 0x1 982 983 static inline uint32_t pmu_num_counters(CPUARMState *env) 984 { 985 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT; 986 } 987 988 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */ 989 static inline uint64_t pmu_counter_mask(CPUARMState *env) 990 { 991 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1); 992 } 993 994 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri, 995 bool isread) 996 { 997 /* Performance monitor registers user accessibility is controlled 998 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable 999 * trapping to EL2 or EL3 for other accesses. 1000 */ 1001 int el = arm_current_el(env); 1002 1003 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) { 1004 return CP_ACCESS_TRAP; 1005 } 1006 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 1007 && !arm_is_secure_below_el3(env)) { 1008 return CP_ACCESS_TRAP_EL2; 1009 } 1010 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 1011 return CP_ACCESS_TRAP_EL3; 1012 } 1013 1014 return CP_ACCESS_OK; 1015 } 1016 1017 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env, 1018 const ARMCPRegInfo *ri, 1019 bool isread) 1020 { 1021 /* ER: event counter read trap control */ 1022 if (arm_feature(env, ARM_FEATURE_V8) 1023 && arm_current_el(env) == 0 1024 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0 1025 && isread) { 1026 return CP_ACCESS_OK; 1027 } 1028 1029 return pmreg_access(env, ri, isread); 1030 } 1031 1032 static CPAccessResult pmreg_access_swinc(CPUARMState *env, 1033 const ARMCPRegInfo *ri, 1034 bool isread) 1035 { 1036 /* SW: software increment write trap control */ 1037 if (arm_feature(env, ARM_FEATURE_V8) 1038 && arm_current_el(env) == 0 1039 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0 1040 && !isread) { 1041 return CP_ACCESS_OK; 1042 } 1043 1044 return pmreg_access(env, ri, isread); 1045 } 1046 1047 #ifndef CONFIG_USER_ONLY 1048 1049 static CPAccessResult pmreg_access_selr(CPUARMState *env, 1050 const ARMCPRegInfo *ri, 1051 bool isread) 1052 { 1053 /* ER: event counter read trap control */ 1054 if (arm_feature(env, ARM_FEATURE_V8) 1055 && arm_current_el(env) == 0 1056 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) { 1057 return CP_ACCESS_OK; 1058 } 1059 1060 return pmreg_access(env, ri, isread); 1061 } 1062 1063 static CPAccessResult pmreg_access_ccntr(CPUARMState *env, 1064 const ARMCPRegInfo *ri, 1065 bool isread) 1066 { 1067 /* CR: cycle counter read trap control */ 1068 if (arm_feature(env, ARM_FEATURE_V8) 1069 && arm_current_el(env) == 0 1070 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0 1071 && isread) { 1072 return CP_ACCESS_OK; 1073 } 1074 1075 return pmreg_access(env, ri, isread); 1076 } 1077 1078 static inline bool arm_ccnt_enabled(CPUARMState *env) 1079 { 1080 /* This does not support checking PMCCFILTR_EL0 register */ 1081 1082 if (!(env->cp15.c9_pmcr & PMCRE) || !(env->cp15.c9_pmcnten & (1 << 31))) { 1083 return false; 1084 } 1085 1086 return true; 1087 } 1088 1089 void pmccntr_sync(CPUARMState *env) 1090 { 1091 uint64_t temp_ticks; 1092 1093 temp_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 temp_ticks /= 64; 1099 } 1100 1101 if (arm_ccnt_enabled(env)) { 1102 env->cp15.c15_ccnt = temp_ticks - env->cp15.c15_ccnt; 1103 } 1104 } 1105 1106 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1107 uint64_t value) 1108 { 1109 pmccntr_sync(env); 1110 1111 if (value & PMCRC) { 1112 /* The counter has been reset */ 1113 env->cp15.c15_ccnt = 0; 1114 } 1115 1116 /* only the DP, X, D and E bits are writable */ 1117 env->cp15.c9_pmcr &= ~0x39; 1118 env->cp15.c9_pmcr |= (value & 0x39); 1119 1120 pmccntr_sync(env); 1121 } 1122 1123 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1124 { 1125 uint64_t total_ticks; 1126 1127 if (!arm_ccnt_enabled(env)) { 1128 /* Counter is disabled, do not change value */ 1129 return env->cp15.c15_ccnt; 1130 } 1131 1132 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1133 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 1134 1135 if (env->cp15.c9_pmcr & PMCRD) { 1136 /* Increment once every 64 processor clock cycles */ 1137 total_ticks /= 64; 1138 } 1139 return total_ticks - env->cp15.c15_ccnt; 1140 } 1141 1142 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1143 uint64_t value) 1144 { 1145 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and 1146 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the 1147 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are 1148 * accessed. 1149 */ 1150 env->cp15.c9_pmselr = value & 0x1f; 1151 } 1152 1153 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1154 uint64_t value) 1155 { 1156 uint64_t total_ticks; 1157 1158 if (!arm_ccnt_enabled(env)) { 1159 /* Counter is disabled, set the absolute value */ 1160 env->cp15.c15_ccnt = value; 1161 return; 1162 } 1163 1164 total_ticks = muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1165 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 1166 1167 if (env->cp15.c9_pmcr & PMCRD) { 1168 /* Increment once every 64 processor clock cycles */ 1169 total_ticks /= 64; 1170 } 1171 env->cp15.c15_ccnt = total_ticks - value; 1172 } 1173 1174 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, 1175 uint64_t value) 1176 { 1177 uint64_t cur_val = pmccntr_read(env, NULL); 1178 1179 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); 1180 } 1181 1182 #else /* CONFIG_USER_ONLY */ 1183 1184 void pmccntr_sync(CPUARMState *env) 1185 { 1186 } 1187 1188 #endif 1189 1190 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1191 uint64_t value) 1192 { 1193 pmccntr_sync(env); 1194 env->cp15.pmccfiltr_el0 = value & 0xfc000000; 1195 pmccntr_sync(env); 1196 } 1197 1198 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1199 uint64_t value) 1200 { 1201 value &= pmu_counter_mask(env); 1202 env->cp15.c9_pmcnten |= value; 1203 } 1204 1205 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1206 uint64_t value) 1207 { 1208 value &= pmu_counter_mask(env); 1209 env->cp15.c9_pmcnten &= ~value; 1210 } 1211 1212 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1213 uint64_t value) 1214 { 1215 value &= pmu_counter_mask(env); 1216 env->cp15.c9_pmovsr &= ~value; 1217 } 1218 1219 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1220 uint64_t value) 1221 { 1222 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when 1223 * PMSELR value is equal to or greater than the number of implemented 1224 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI. 1225 */ 1226 if (env->cp15.c9_pmselr == 0x1f) { 1227 pmccfiltr_write(env, ri, value); 1228 } 1229 } 1230 1231 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri) 1232 { 1233 /* We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER 1234 * are CONSTRAINED UNPREDICTABLE. See comments in pmxevtyper_write(). 1235 */ 1236 if (env->cp15.c9_pmselr == 0x1f) { 1237 return env->cp15.pmccfiltr_el0; 1238 } else { 1239 return 0; 1240 } 1241 } 1242 1243 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1244 uint64_t value) 1245 { 1246 if (arm_feature(env, ARM_FEATURE_V8)) { 1247 env->cp15.c9_pmuserenr = value & 0xf; 1248 } else { 1249 env->cp15.c9_pmuserenr = value & 1; 1250 } 1251 } 1252 1253 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1254 uint64_t value) 1255 { 1256 /* We have no event counters so only the C bit can be changed */ 1257 value &= pmu_counter_mask(env); 1258 env->cp15.c9_pminten |= value; 1259 } 1260 1261 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1262 uint64_t value) 1263 { 1264 value &= pmu_counter_mask(env); 1265 env->cp15.c9_pminten &= ~value; 1266 } 1267 1268 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 1269 uint64_t value) 1270 { 1271 /* Note that even though the AArch64 view of this register has bits 1272 * [10:0] all RES0 we can only mask the bottom 5, to comply with the 1273 * architectural requirements for bits which are RES0 only in some 1274 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 1275 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) 1276 */ 1277 raw_write(env, ri, value & ~0x1FULL); 1278 } 1279 1280 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 1281 { 1282 /* We only mask off bits that are RES0 both for AArch64 and AArch32. 1283 * For bits that vary between AArch32/64, code needs to check the 1284 * current execution mode before directly using the feature bit. 1285 */ 1286 uint32_t valid_mask = SCR_AARCH64_MASK | SCR_AARCH32_MASK; 1287 1288 if (!arm_feature(env, ARM_FEATURE_EL2)) { 1289 valid_mask &= ~SCR_HCE; 1290 1291 /* On ARMv7, SMD (or SCD as it is called in v7) is only 1292 * supported if EL2 exists. The bit is UNK/SBZP when 1293 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero 1294 * when EL2 is unavailable. 1295 * On ARMv8, this bit is always available. 1296 */ 1297 if (arm_feature(env, ARM_FEATURE_V7) && 1298 !arm_feature(env, ARM_FEATURE_V8)) { 1299 valid_mask &= ~SCR_SMD; 1300 } 1301 } 1302 1303 /* Clear all-context RES0 bits. */ 1304 value &= valid_mask; 1305 raw_write(env, ri, value); 1306 } 1307 1308 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1309 { 1310 ARMCPU *cpu = arm_env_get_cpu(env); 1311 1312 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR 1313 * bank 1314 */ 1315 uint32_t index = A32_BANKED_REG_GET(env, csselr, 1316 ri->secure & ARM_CP_SECSTATE_S); 1317 1318 return cpu->ccsidr[index]; 1319 } 1320 1321 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1322 uint64_t value) 1323 { 1324 raw_write(env, ri, value & 0xf); 1325 } 1326 1327 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1328 { 1329 CPUState *cs = ENV_GET_CPU(env); 1330 uint64_t ret = 0; 1331 1332 if (arm_hcr_el2_imo(env)) { 1333 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) { 1334 ret |= CPSR_I; 1335 } 1336 } else { 1337 if (cs->interrupt_request & CPU_INTERRUPT_HARD) { 1338 ret |= CPSR_I; 1339 } 1340 } 1341 1342 if (arm_hcr_el2_fmo(env)) { 1343 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) { 1344 ret |= CPSR_F; 1345 } 1346 } else { 1347 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { 1348 ret |= CPSR_F; 1349 } 1350 } 1351 1352 /* External aborts are not possible in QEMU so A bit is always clear */ 1353 return ret; 1354 } 1355 1356 static const ARMCPRegInfo v7_cp_reginfo[] = { 1357 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ 1358 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 1359 .access = PL1_W, .type = ARM_CP_NOP }, 1360 /* Performance monitors are implementation defined in v7, 1361 * but with an ARM recommended set of registers, which we 1362 * follow (although we don't actually implement any counters) 1363 * 1364 * Performance registers fall into three categories: 1365 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) 1366 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) 1367 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) 1368 * For the cases controlled by PMUSERENR we must set .access to PL0_RW 1369 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. 1370 */ 1371 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, 1372 .access = PL0_RW, .type = ARM_CP_ALIAS, 1373 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1374 .writefn = pmcntenset_write, 1375 .accessfn = pmreg_access, 1376 .raw_writefn = raw_write }, 1377 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, 1378 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, 1379 .access = PL0_RW, .accessfn = pmreg_access, 1380 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, 1381 .writefn = pmcntenset_write, .raw_writefn = raw_write }, 1382 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, 1383 .access = PL0_RW, 1384 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1385 .accessfn = pmreg_access, 1386 .writefn = pmcntenclr_write, 1387 .type = ARM_CP_ALIAS }, 1388 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, 1389 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, 1390 .access = PL0_RW, .accessfn = pmreg_access, 1391 .type = ARM_CP_ALIAS, 1392 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), 1393 .writefn = pmcntenclr_write }, 1394 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, 1395 .access = PL0_RW, 1396 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 1397 .accessfn = pmreg_access, 1398 .writefn = pmovsr_write, 1399 .raw_writefn = raw_write }, 1400 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64, 1401 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3, 1402 .access = PL0_RW, .accessfn = pmreg_access, 1403 .type = ARM_CP_ALIAS, 1404 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 1405 .writefn = pmovsr_write, 1406 .raw_writefn = raw_write }, 1407 /* Unimplemented so WI. */ 1408 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, 1409 .access = PL0_W, .accessfn = pmreg_access_swinc, .type = ARM_CP_NOP }, 1410 #ifndef CONFIG_USER_ONLY 1411 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, 1412 .access = PL0_RW, .type = ARM_CP_ALIAS, 1413 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr), 1414 .accessfn = pmreg_access_selr, .writefn = pmselr_write, 1415 .raw_writefn = raw_write}, 1416 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64, 1417 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5, 1418 .access = PL0_RW, .accessfn = pmreg_access_selr, 1419 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr), 1420 .writefn = pmselr_write, .raw_writefn = raw_write, }, 1421 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, 1422 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO, 1423 .readfn = pmccntr_read, .writefn = pmccntr_write32, 1424 .accessfn = pmreg_access_ccntr }, 1425 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, 1426 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, 1427 .access = PL0_RW, .accessfn = pmreg_access_ccntr, 1428 .type = ARM_CP_IO, 1429 .readfn = pmccntr_read, .writefn = pmccntr_write, }, 1430 #endif 1431 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, 1432 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, 1433 .writefn = pmccfiltr_write, 1434 .access = PL0_RW, .accessfn = pmreg_access, 1435 .type = ARM_CP_IO, 1436 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), 1437 .resetvalue = 0, }, 1438 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, 1439 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access, 1440 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 1441 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64, 1442 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1, 1443 .access = PL0_RW, .type = ARM_CP_NO_RAW, .accessfn = pmreg_access, 1444 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 1445 /* Unimplemented, RAZ/WI. */ 1446 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, 1447 .access = PL0_RW, .type = ARM_CP_CONST, .resetvalue = 0, 1448 .accessfn = pmreg_access_xevcntr }, 1449 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, 1450 .access = PL0_R | PL1_RW, .accessfn = access_tpm, 1451 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr), 1452 .resetvalue = 0, 1453 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 1454 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64, 1455 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0, 1456 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 1457 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), 1458 .resetvalue = 0, 1459 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 1460 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, 1461 .access = PL1_RW, .accessfn = access_tpm, 1462 .type = ARM_CP_ALIAS | ARM_CP_IO, 1463 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten), 1464 .resetvalue = 0, 1465 .writefn = pmintenset_write, .raw_writefn = raw_write }, 1466 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64, 1467 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1, 1468 .access = PL1_RW, .accessfn = access_tpm, 1469 .type = ARM_CP_IO, 1470 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 1471 .writefn = pmintenset_write, .raw_writefn = raw_write, 1472 .resetvalue = 0x0 }, 1473 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, 1474 .access = PL1_RW, .accessfn = access_tpm, 1475 .type = ARM_CP_ALIAS | ARM_CP_IO, 1476 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 1477 .writefn = pmintenclr_write, }, 1478 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64, 1479 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2, 1480 .access = PL1_RW, .accessfn = access_tpm, 1481 .type = ARM_CP_ALIAS | ARM_CP_IO, 1482 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 1483 .writefn = pmintenclr_write }, 1484 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, 1485 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, 1486 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW }, 1487 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, 1488 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, 1489 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0, 1490 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), 1491 offsetof(CPUARMState, cp15.csselr_ns) } }, 1492 /* Auxiliary ID register: this actually has an IMPDEF value but for now 1493 * just RAZ for all cores: 1494 */ 1495 { .name = "AIDR", .state = ARM_CP_STATE_BOTH, 1496 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, 1497 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 1498 /* Auxiliary fault status registers: these also are IMPDEF, and we 1499 * choose to RAZ/WI for all cores. 1500 */ 1501 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, 1502 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, 1503 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 1504 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, 1505 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, 1506 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 1507 /* MAIR can just read-as-written because we don't implement caches 1508 * and so don't need to care about memory attributes. 1509 */ 1510 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, 1511 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 1512 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]), 1513 .resetvalue = 0 }, 1514 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64, 1515 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0, 1516 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]), 1517 .resetvalue = 0 }, 1518 /* For non-long-descriptor page tables these are PRRR and NMRR; 1519 * regardless they still act as reads-as-written for QEMU. 1520 */ 1521 /* MAIR0/1 are defined separately from their 64-bit counterpart which 1522 * allows them to assign the correct fieldoffset based on the endianness 1523 * handled in the field definitions. 1524 */ 1525 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, 1526 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW, 1527 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s), 1528 offsetof(CPUARMState, cp15.mair0_ns) }, 1529 .resetfn = arm_cp_reset_ignore }, 1530 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, 1531 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW, 1532 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s), 1533 offsetof(CPUARMState, cp15.mair1_ns) }, 1534 .resetfn = arm_cp_reset_ignore }, 1535 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, 1536 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, 1537 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read }, 1538 /* 32 bit ITLB invalidates */ 1539 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0, 1540 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 1541 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, 1542 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 1543 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2, 1544 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 1545 /* 32 bit DTLB invalidates */ 1546 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0, 1547 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 1548 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, 1549 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 1550 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2, 1551 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 1552 /* 32 bit TLB invalidates */ 1553 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 1554 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 1555 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 1556 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 1557 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 1558 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 1559 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 1560 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, 1561 REGINFO_SENTINEL 1562 }; 1563 1564 static const ARMCPRegInfo v7mp_cp_reginfo[] = { 1565 /* 32 bit TLB invalidates, Inner Shareable */ 1566 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 1567 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write }, 1568 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 1569 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, 1570 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 1571 .type = ARM_CP_NO_RAW, .access = PL1_W, 1572 .writefn = tlbiasid_is_write }, 1573 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 1574 .type = ARM_CP_NO_RAW, .access = PL1_W, 1575 .writefn = tlbimvaa_is_write }, 1576 REGINFO_SENTINEL 1577 }; 1578 1579 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1580 uint64_t value) 1581 { 1582 value &= 1; 1583 env->teecr = value; 1584 } 1585 1586 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri, 1587 bool isread) 1588 { 1589 if (arm_current_el(env) == 0 && (env->teecr & 1)) { 1590 return CP_ACCESS_TRAP; 1591 } 1592 return CP_ACCESS_OK; 1593 } 1594 1595 static const ARMCPRegInfo t2ee_cp_reginfo[] = { 1596 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, 1597 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), 1598 .resetvalue = 0, 1599 .writefn = teecr_write }, 1600 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, 1601 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), 1602 .accessfn = teehbr_access, .resetvalue = 0 }, 1603 REGINFO_SENTINEL 1604 }; 1605 1606 static const ARMCPRegInfo v6k_cp_reginfo[] = { 1607 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, 1608 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, 1609 .access = PL0_RW, 1610 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 }, 1611 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, 1612 .access = PL0_RW, 1613 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s), 1614 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, 1615 .resetfn = arm_cp_reset_ignore }, 1616 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, 1617 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, 1618 .access = PL0_R|PL1_W, 1619 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]), 1620 .resetvalue = 0}, 1621 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, 1622 .access = PL0_R|PL1_W, 1623 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s), 1624 offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, 1625 .resetfn = arm_cp_reset_ignore }, 1626 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64, 1627 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, 1628 .access = PL1_RW, 1629 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 }, 1630 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4, 1631 .access = PL1_RW, 1632 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s), 1633 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) }, 1634 .resetvalue = 0 }, 1635 REGINFO_SENTINEL 1636 }; 1637 1638 #ifndef CONFIG_USER_ONLY 1639 1640 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, 1641 bool isread) 1642 { 1643 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero. 1644 * Writable only at the highest implemented exception level. 1645 */ 1646 int el = arm_current_el(env); 1647 1648 switch (el) { 1649 case 0: 1650 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) { 1651 return CP_ACCESS_TRAP; 1652 } 1653 break; 1654 case 1: 1655 if (!isread && ri->state == ARM_CP_STATE_AA32 && 1656 arm_is_secure_below_el3(env)) { 1657 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */ 1658 return CP_ACCESS_TRAP_UNCATEGORIZED; 1659 } 1660 break; 1661 case 2: 1662 case 3: 1663 break; 1664 } 1665 1666 if (!isread && el < arm_highest_el(env)) { 1667 return CP_ACCESS_TRAP_UNCATEGORIZED; 1668 } 1669 1670 return CP_ACCESS_OK; 1671 } 1672 1673 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx, 1674 bool isread) 1675 { 1676 unsigned int cur_el = arm_current_el(env); 1677 bool secure = arm_is_secure(env); 1678 1679 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */ 1680 if (cur_el == 0 && 1681 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) { 1682 return CP_ACCESS_TRAP; 1683 } 1684 1685 if (arm_feature(env, ARM_FEATURE_EL2) && 1686 timeridx == GTIMER_PHYS && !secure && cur_el < 2 && 1687 !extract32(env->cp15.cnthctl_el2, 0, 1)) { 1688 return CP_ACCESS_TRAP_EL2; 1689 } 1690 return CP_ACCESS_OK; 1691 } 1692 1693 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx, 1694 bool isread) 1695 { 1696 unsigned int cur_el = arm_current_el(env); 1697 bool secure = arm_is_secure(env); 1698 1699 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if 1700 * EL0[PV]TEN is zero. 1701 */ 1702 if (cur_el == 0 && 1703 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { 1704 return CP_ACCESS_TRAP; 1705 } 1706 1707 if (arm_feature(env, ARM_FEATURE_EL2) && 1708 timeridx == GTIMER_PHYS && !secure && cur_el < 2 && 1709 !extract32(env->cp15.cnthctl_el2, 1, 1)) { 1710 return CP_ACCESS_TRAP_EL2; 1711 } 1712 return CP_ACCESS_OK; 1713 } 1714 1715 static CPAccessResult gt_pct_access(CPUARMState *env, 1716 const ARMCPRegInfo *ri, 1717 bool isread) 1718 { 1719 return gt_counter_access(env, GTIMER_PHYS, isread); 1720 } 1721 1722 static CPAccessResult gt_vct_access(CPUARMState *env, 1723 const ARMCPRegInfo *ri, 1724 bool isread) 1725 { 1726 return gt_counter_access(env, GTIMER_VIRT, isread); 1727 } 1728 1729 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 1730 bool isread) 1731 { 1732 return gt_timer_access(env, GTIMER_PHYS, isread); 1733 } 1734 1735 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 1736 bool isread) 1737 { 1738 return gt_timer_access(env, GTIMER_VIRT, isread); 1739 } 1740 1741 static CPAccessResult gt_stimer_access(CPUARMState *env, 1742 const ARMCPRegInfo *ri, 1743 bool isread) 1744 { 1745 /* The AArch64 register view of the secure physical timer is 1746 * always accessible from EL3, and configurably accessible from 1747 * Secure EL1. 1748 */ 1749 switch (arm_current_el(env)) { 1750 case 1: 1751 if (!arm_is_secure(env)) { 1752 return CP_ACCESS_TRAP; 1753 } 1754 if (!(env->cp15.scr_el3 & SCR_ST)) { 1755 return CP_ACCESS_TRAP_EL3; 1756 } 1757 return CP_ACCESS_OK; 1758 case 0: 1759 case 2: 1760 return CP_ACCESS_TRAP; 1761 case 3: 1762 return CP_ACCESS_OK; 1763 default: 1764 g_assert_not_reached(); 1765 } 1766 } 1767 1768 static uint64_t gt_get_countervalue(CPUARMState *env) 1769 { 1770 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE; 1771 } 1772 1773 static void gt_recalc_timer(ARMCPU *cpu, int timeridx) 1774 { 1775 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; 1776 1777 if (gt->ctl & 1) { 1778 /* Timer enabled: calculate and set current ISTATUS, irq, and 1779 * reset timer to when ISTATUS next has to change 1780 */ 1781 uint64_t offset = timeridx == GTIMER_VIRT ? 1782 cpu->env.cp15.cntvoff_el2 : 0; 1783 uint64_t count = gt_get_countervalue(&cpu->env); 1784 /* Note that this must be unsigned 64 bit arithmetic: */ 1785 int istatus = count - offset >= gt->cval; 1786 uint64_t nexttick; 1787 int irqstate; 1788 1789 gt->ctl = deposit32(gt->ctl, 2, 1, istatus); 1790 1791 irqstate = (istatus && !(gt->ctl & 2)); 1792 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 1793 1794 if (istatus) { 1795 /* Next transition is when count rolls back over to zero */ 1796 nexttick = UINT64_MAX; 1797 } else { 1798 /* Next transition is when we hit cval */ 1799 nexttick = gt->cval + offset; 1800 } 1801 /* Note that the desired next expiry time might be beyond the 1802 * signed-64-bit range of a QEMUTimer -- in this case we just 1803 * set the timer for as far in the future as possible. When the 1804 * timer expires we will reset the timer for any remaining period. 1805 */ 1806 if (nexttick > INT64_MAX / GTIMER_SCALE) { 1807 nexttick = INT64_MAX / GTIMER_SCALE; 1808 } 1809 timer_mod(cpu->gt_timer[timeridx], nexttick); 1810 trace_arm_gt_recalc(timeridx, irqstate, nexttick); 1811 } else { 1812 /* Timer disabled: ISTATUS and timer output always clear */ 1813 gt->ctl &= ~4; 1814 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0); 1815 timer_del(cpu->gt_timer[timeridx]); 1816 trace_arm_gt_recalc_disabled(timeridx); 1817 } 1818 } 1819 1820 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri, 1821 int timeridx) 1822 { 1823 ARMCPU *cpu = arm_env_get_cpu(env); 1824 1825 timer_del(cpu->gt_timer[timeridx]); 1826 } 1827 1828 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 1829 { 1830 return gt_get_countervalue(env); 1831 } 1832 1833 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 1834 { 1835 return gt_get_countervalue(env) - env->cp15.cntvoff_el2; 1836 } 1837 1838 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1839 int timeridx, 1840 uint64_t value) 1841 { 1842 trace_arm_gt_cval_write(timeridx, value); 1843 env->cp15.c14_timer[timeridx].cval = value; 1844 gt_recalc_timer(arm_env_get_cpu(env), timeridx); 1845 } 1846 1847 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, 1848 int timeridx) 1849 { 1850 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; 1851 1852 return (uint32_t)(env->cp15.c14_timer[timeridx].cval - 1853 (gt_get_countervalue(env) - offset)); 1854 } 1855 1856 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1857 int timeridx, 1858 uint64_t value) 1859 { 1860 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; 1861 1862 trace_arm_gt_tval_write(timeridx, value); 1863 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset + 1864 sextract64(value, 0, 32); 1865 gt_recalc_timer(arm_env_get_cpu(env), timeridx); 1866 } 1867 1868 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1869 int timeridx, 1870 uint64_t value) 1871 { 1872 ARMCPU *cpu = arm_env_get_cpu(env); 1873 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; 1874 1875 trace_arm_gt_ctl_write(timeridx, value); 1876 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); 1877 if ((oldval ^ value) & 1) { 1878 /* Enable toggled */ 1879 gt_recalc_timer(cpu, timeridx); 1880 } else if ((oldval ^ value) & 2) { 1881 /* IMASK toggled: don't need to recalculate, 1882 * just set the interrupt line based on ISTATUS 1883 */ 1884 int irqstate = (oldval & 4) && !(value & 2); 1885 1886 trace_arm_gt_imask_toggle(timeridx, irqstate); 1887 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 1888 } 1889 } 1890 1891 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1892 { 1893 gt_timer_reset(env, ri, GTIMER_PHYS); 1894 } 1895 1896 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1897 uint64_t value) 1898 { 1899 gt_cval_write(env, ri, GTIMER_PHYS, value); 1900 } 1901 1902 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 1903 { 1904 return gt_tval_read(env, ri, GTIMER_PHYS); 1905 } 1906 1907 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1908 uint64_t value) 1909 { 1910 gt_tval_write(env, ri, GTIMER_PHYS, value); 1911 } 1912 1913 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1914 uint64_t value) 1915 { 1916 gt_ctl_write(env, ri, GTIMER_PHYS, value); 1917 } 1918 1919 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1920 { 1921 gt_timer_reset(env, ri, GTIMER_VIRT); 1922 } 1923 1924 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1925 uint64_t value) 1926 { 1927 gt_cval_write(env, ri, GTIMER_VIRT, value); 1928 } 1929 1930 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 1931 { 1932 return gt_tval_read(env, ri, GTIMER_VIRT); 1933 } 1934 1935 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1936 uint64_t value) 1937 { 1938 gt_tval_write(env, ri, GTIMER_VIRT, value); 1939 } 1940 1941 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1942 uint64_t value) 1943 { 1944 gt_ctl_write(env, ri, GTIMER_VIRT, value); 1945 } 1946 1947 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri, 1948 uint64_t value) 1949 { 1950 ARMCPU *cpu = arm_env_get_cpu(env); 1951 1952 trace_arm_gt_cntvoff_write(value); 1953 raw_write(env, ri, value); 1954 gt_recalc_timer(cpu, GTIMER_VIRT); 1955 } 1956 1957 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1958 { 1959 gt_timer_reset(env, ri, GTIMER_HYP); 1960 } 1961 1962 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1963 uint64_t value) 1964 { 1965 gt_cval_write(env, ri, GTIMER_HYP, value); 1966 } 1967 1968 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 1969 { 1970 return gt_tval_read(env, ri, GTIMER_HYP); 1971 } 1972 1973 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1974 uint64_t value) 1975 { 1976 gt_tval_write(env, ri, GTIMER_HYP, value); 1977 } 1978 1979 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 1980 uint64_t value) 1981 { 1982 gt_ctl_write(env, ri, GTIMER_HYP, value); 1983 } 1984 1985 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1986 { 1987 gt_timer_reset(env, ri, GTIMER_SEC); 1988 } 1989 1990 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 1991 uint64_t value) 1992 { 1993 gt_cval_write(env, ri, GTIMER_SEC, value); 1994 } 1995 1996 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 1997 { 1998 return gt_tval_read(env, ri, GTIMER_SEC); 1999 } 2000 2001 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2002 uint64_t value) 2003 { 2004 gt_tval_write(env, ri, GTIMER_SEC, value); 2005 } 2006 2007 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2008 uint64_t value) 2009 { 2010 gt_ctl_write(env, ri, GTIMER_SEC, value); 2011 } 2012 2013 void arm_gt_ptimer_cb(void *opaque) 2014 { 2015 ARMCPU *cpu = opaque; 2016 2017 gt_recalc_timer(cpu, GTIMER_PHYS); 2018 } 2019 2020 void arm_gt_vtimer_cb(void *opaque) 2021 { 2022 ARMCPU *cpu = opaque; 2023 2024 gt_recalc_timer(cpu, GTIMER_VIRT); 2025 } 2026 2027 void arm_gt_htimer_cb(void *opaque) 2028 { 2029 ARMCPU *cpu = opaque; 2030 2031 gt_recalc_timer(cpu, GTIMER_HYP); 2032 } 2033 2034 void arm_gt_stimer_cb(void *opaque) 2035 { 2036 ARMCPU *cpu = opaque; 2037 2038 gt_recalc_timer(cpu, GTIMER_SEC); 2039 } 2040 2041 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 2042 /* Note that CNTFRQ is purely reads-as-written for the benefit 2043 * of software; writing it doesn't actually change the timer frequency. 2044 * Our reset value matches the fixed frequency we implement the timer at. 2045 */ 2046 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, 2047 .type = ARM_CP_ALIAS, 2048 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 2049 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), 2050 }, 2051 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 2052 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 2053 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 2054 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 2055 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE, 2056 }, 2057 /* overall control: mostly access permissions */ 2058 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, 2059 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, 2060 .access = PL1_RW, 2061 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), 2062 .resetvalue = 0, 2063 }, 2064 /* per-timer control */ 2065 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 2066 .secure = ARM_CP_SECSTATE_NS, 2067 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, 2068 .accessfn = gt_ptimer_access, 2069 .fieldoffset = offsetoflow32(CPUARMState, 2070 cp15.c14_timer[GTIMER_PHYS].ctl), 2071 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, 2072 }, 2073 { .name = "CNTP_CTL_S", 2074 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 2075 .secure = ARM_CP_SECSTATE_S, 2076 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, 2077 .accessfn = gt_ptimer_access, 2078 .fieldoffset = offsetoflow32(CPUARMState, 2079 cp15.c14_timer[GTIMER_SEC].ctl), 2080 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 2081 }, 2082 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, 2083 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, 2084 .type = ARM_CP_IO, .access = PL1_RW | PL0_R, 2085 .accessfn = gt_ptimer_access, 2086 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 2087 .resetvalue = 0, 2088 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, 2089 }, 2090 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, 2091 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL1_RW | PL0_R, 2092 .accessfn = gt_vtimer_access, 2093 .fieldoffset = offsetoflow32(CPUARMState, 2094 cp15.c14_timer[GTIMER_VIRT].ctl), 2095 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, 2096 }, 2097 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, 2098 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, 2099 .type = ARM_CP_IO, .access = PL1_RW | PL0_R, 2100 .accessfn = gt_vtimer_access, 2101 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 2102 .resetvalue = 0, 2103 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, 2104 }, 2105 /* TimerValue views: a 32 bit downcounting view of the underlying state */ 2106 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 2107 .secure = ARM_CP_SECSTATE_NS, 2108 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2109 .accessfn = gt_ptimer_access, 2110 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, 2111 }, 2112 { .name = "CNTP_TVAL_S", 2113 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 2114 .secure = ARM_CP_SECSTATE_S, 2115 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2116 .accessfn = gt_ptimer_access, 2117 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write, 2118 }, 2119 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, 2120 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, 2121 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2122 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset, 2123 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, 2124 }, 2125 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, 2126 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2127 .accessfn = gt_vtimer_access, 2128 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, 2129 }, 2130 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, 2131 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, 2132 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW | PL0_R, 2133 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset, 2134 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, 2135 }, 2136 /* The counter itself */ 2137 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, 2138 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 2139 .accessfn = gt_pct_access, 2140 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, 2141 }, 2142 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, 2143 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, 2144 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2145 .accessfn = gt_pct_access, .readfn = gt_cnt_read, 2146 }, 2147 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, 2148 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 2149 .accessfn = gt_vct_access, 2150 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, 2151 }, 2152 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 2153 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 2154 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2155 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, 2156 }, 2157 /* Comparison value, indicating when the timer goes off */ 2158 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, 2159 .secure = ARM_CP_SECSTATE_NS, 2160 .access = PL1_RW | PL0_R, 2161 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2162 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 2163 .accessfn = gt_ptimer_access, 2164 .writefn = gt_phys_cval_write, .raw_writefn = raw_write, 2165 }, 2166 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2, 2167 .secure = ARM_CP_SECSTATE_S, 2168 .access = PL1_RW | PL0_R, 2169 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2170 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 2171 .accessfn = gt_ptimer_access, 2172 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 2173 }, 2174 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, 2175 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, 2176 .access = PL1_RW | PL0_R, 2177 .type = ARM_CP_IO, 2178 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 2179 .resetvalue = 0, .accessfn = gt_ptimer_access, 2180 .writefn = gt_phys_cval_write, .raw_writefn = raw_write, 2181 }, 2182 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, 2183 .access = PL1_RW | PL0_R, 2184 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2185 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 2186 .accessfn = gt_vtimer_access, 2187 .writefn = gt_virt_cval_write, .raw_writefn = raw_write, 2188 }, 2189 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, 2190 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, 2191 .access = PL1_RW | PL0_R, 2192 .type = ARM_CP_IO, 2193 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 2194 .resetvalue = 0, .accessfn = gt_vtimer_access, 2195 .writefn = gt_virt_cval_write, .raw_writefn = raw_write, 2196 }, 2197 /* Secure timer -- this is actually restricted to only EL3 2198 * and configurably Secure-EL1 via the accessfn. 2199 */ 2200 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64, 2201 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0, 2202 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW, 2203 .accessfn = gt_stimer_access, 2204 .readfn = gt_sec_tval_read, 2205 .writefn = gt_sec_tval_write, 2206 .resetfn = gt_sec_timer_reset, 2207 }, 2208 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64, 2209 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1, 2210 .type = ARM_CP_IO, .access = PL1_RW, 2211 .accessfn = gt_stimer_access, 2212 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl), 2213 .resetvalue = 0, 2214 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 2215 }, 2216 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64, 2217 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2, 2218 .type = ARM_CP_IO, .access = PL1_RW, 2219 .accessfn = gt_stimer_access, 2220 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 2221 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 2222 }, 2223 REGINFO_SENTINEL 2224 }; 2225 2226 #else 2227 2228 /* In user-mode most of the generic timer registers are inaccessible 2229 * however modern kernels (4.12+) allow access to cntvct_el0 2230 */ 2231 2232 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2233 { 2234 /* Currently we have no support for QEMUTimer in linux-user so we 2235 * can't call gt_get_countervalue(env), instead we directly 2236 * call the lower level functions. 2237 */ 2238 return cpu_get_clock() / GTIMER_SCALE; 2239 } 2240 2241 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 2242 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 2243 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 2244 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */, 2245 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 2246 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE, 2247 }, 2248 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 2249 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 2250 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2251 .readfn = gt_virt_cnt_read, 2252 }, 2253 REGINFO_SENTINEL 2254 }; 2255 2256 #endif 2257 2258 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 2259 { 2260 if (arm_feature(env, ARM_FEATURE_LPAE)) { 2261 raw_write(env, ri, value); 2262 } else if (arm_feature(env, ARM_FEATURE_V7)) { 2263 raw_write(env, ri, value & 0xfffff6ff); 2264 } else { 2265 raw_write(env, ri, value & 0xfffff1ff); 2266 } 2267 } 2268 2269 #ifndef CONFIG_USER_ONLY 2270 /* get_phys_addr() isn't present for user-mode-only targets */ 2271 2272 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri, 2273 bool isread) 2274 { 2275 if (ri->opc2 & 4) { 2276 /* The ATS12NSO* operations must trap to EL3 if executed in 2277 * Secure EL1 (which can only happen if EL3 is AArch64). 2278 * They are simply UNDEF if executed from NS EL1. 2279 * They function normally from EL2 or EL3. 2280 */ 2281 if (arm_current_el(env) == 1) { 2282 if (arm_is_secure_below_el3(env)) { 2283 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3; 2284 } 2285 return CP_ACCESS_TRAP_UNCATEGORIZED; 2286 } 2287 } 2288 return CP_ACCESS_OK; 2289 } 2290 2291 static uint64_t do_ats_write(CPUARMState *env, uint64_t value, 2292 MMUAccessType access_type, ARMMMUIdx mmu_idx) 2293 { 2294 hwaddr phys_addr; 2295 target_ulong page_size; 2296 int prot; 2297 bool ret; 2298 uint64_t par64; 2299 bool format64 = false; 2300 MemTxAttrs attrs = {}; 2301 ARMMMUFaultInfo fi = {}; 2302 ARMCacheAttrs cacheattrs = {}; 2303 2304 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs, 2305 &prot, &page_size, &fi, &cacheattrs); 2306 2307 if (is_a64(env)) { 2308 format64 = true; 2309 } else if (arm_feature(env, ARM_FEATURE_LPAE)) { 2310 /* 2311 * ATS1Cxx: 2312 * * TTBCR.EAE determines whether the result is returned using the 2313 * 32-bit or the 64-bit PAR format 2314 * * Instructions executed in Hyp mode always use the 64bit format 2315 * 2316 * ATS1S2NSOxx uses the 64bit format if any of the following is true: 2317 * * The Non-secure TTBCR.EAE bit is set to 1 2318 * * The implementation includes EL2, and the value of HCR.VM is 1 2319 * 2320 * (Note that HCR.DC makes HCR.VM behave as if it is 1.) 2321 * 2322 * ATS1Hx always uses the 64bit format (not supported yet). 2323 */ 2324 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx); 2325 2326 if (arm_feature(env, ARM_FEATURE_EL2)) { 2327 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 2328 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC); 2329 } else { 2330 format64 |= arm_current_el(env) == 2; 2331 } 2332 } 2333 } 2334 2335 if (format64) { 2336 /* Create a 64-bit PAR */ 2337 par64 = (1 << 11); /* LPAE bit always set */ 2338 if (!ret) { 2339 par64 |= phys_addr & ~0xfffULL; 2340 if (!attrs.secure) { 2341 par64 |= (1 << 9); /* NS */ 2342 } 2343 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */ 2344 par64 |= cacheattrs.shareability << 7; /* SH */ 2345 } else { 2346 uint32_t fsr = arm_fi_to_lfsc(&fi); 2347 2348 par64 |= 1; /* F */ 2349 par64 |= (fsr & 0x3f) << 1; /* FS */ 2350 /* Note that S2WLK and FSTAGE are always zero, because we don't 2351 * implement virtualization and therefore there can't be a stage 2 2352 * fault. 2353 */ 2354 } 2355 } else { 2356 /* fsr is a DFSR/IFSR value for the short descriptor 2357 * translation table format (with WnR always clear). 2358 * Convert it to a 32-bit PAR. 2359 */ 2360 if (!ret) { 2361 /* We do not set any attribute bits in the PAR */ 2362 if (page_size == (1 << 24) 2363 && arm_feature(env, ARM_FEATURE_V7)) { 2364 par64 = (phys_addr & 0xff000000) | (1 << 1); 2365 } else { 2366 par64 = phys_addr & 0xfffff000; 2367 } 2368 if (!attrs.secure) { 2369 par64 |= (1 << 9); /* NS */ 2370 } 2371 } else { 2372 uint32_t fsr = arm_fi_to_sfsc(&fi); 2373 2374 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) | 2375 ((fsr & 0xf) << 1) | 1; 2376 } 2377 } 2378 return par64; 2379 } 2380 2381 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 2382 { 2383 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 2384 uint64_t par64; 2385 ARMMMUIdx mmu_idx; 2386 int el = arm_current_el(env); 2387 bool secure = arm_is_secure_below_el3(env); 2388 2389 switch (ri->opc2 & 6) { 2390 case 0: 2391 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */ 2392 switch (el) { 2393 case 3: 2394 mmu_idx = ARMMMUIdx_S1E3; 2395 break; 2396 case 2: 2397 mmu_idx = ARMMMUIdx_S1NSE1; 2398 break; 2399 case 1: 2400 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; 2401 break; 2402 default: 2403 g_assert_not_reached(); 2404 } 2405 break; 2406 case 2: 2407 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */ 2408 switch (el) { 2409 case 3: 2410 mmu_idx = ARMMMUIdx_S1SE0; 2411 break; 2412 case 2: 2413 mmu_idx = ARMMMUIdx_S1NSE0; 2414 break; 2415 case 1: 2416 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; 2417 break; 2418 default: 2419 g_assert_not_reached(); 2420 } 2421 break; 2422 case 4: 2423 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */ 2424 mmu_idx = ARMMMUIdx_S12NSE1; 2425 break; 2426 case 6: 2427 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */ 2428 mmu_idx = ARMMMUIdx_S12NSE0; 2429 break; 2430 default: 2431 g_assert_not_reached(); 2432 } 2433 2434 par64 = do_ats_write(env, value, access_type, mmu_idx); 2435 2436 A32_BANKED_CURRENT_REG_SET(env, par, par64); 2437 } 2438 2439 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri, 2440 uint64_t value) 2441 { 2442 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 2443 uint64_t par64; 2444 2445 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S2NS); 2446 2447 A32_BANKED_CURRENT_REG_SET(env, par, par64); 2448 } 2449 2450 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri, 2451 bool isread) 2452 { 2453 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) { 2454 return CP_ACCESS_TRAP; 2455 } 2456 return CP_ACCESS_OK; 2457 } 2458 2459 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri, 2460 uint64_t value) 2461 { 2462 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 2463 ARMMMUIdx mmu_idx; 2464 int secure = arm_is_secure_below_el3(env); 2465 2466 switch (ri->opc2 & 6) { 2467 case 0: 2468 switch (ri->opc1) { 2469 case 0: /* AT S1E1R, AT S1E1W */ 2470 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; 2471 break; 2472 case 4: /* AT S1E2R, AT S1E2W */ 2473 mmu_idx = ARMMMUIdx_S1E2; 2474 break; 2475 case 6: /* AT S1E3R, AT S1E3W */ 2476 mmu_idx = ARMMMUIdx_S1E3; 2477 break; 2478 default: 2479 g_assert_not_reached(); 2480 } 2481 break; 2482 case 2: /* AT S1E0R, AT S1E0W */ 2483 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; 2484 break; 2485 case 4: /* AT S12E1R, AT S12E1W */ 2486 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1; 2487 break; 2488 case 6: /* AT S12E0R, AT S12E0W */ 2489 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0; 2490 break; 2491 default: 2492 g_assert_not_reached(); 2493 } 2494 2495 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx); 2496 } 2497 #endif 2498 2499 static const ARMCPRegInfo vapa_cp_reginfo[] = { 2500 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, 2501 .access = PL1_RW, .resetvalue = 0, 2502 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), 2503 offsetoflow32(CPUARMState, cp15.par_ns) }, 2504 .writefn = par_write }, 2505 #ifndef CONFIG_USER_ONLY 2506 /* This underdecoding is safe because the reginfo is NO_RAW. */ 2507 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, 2508 .access = PL1_W, .accessfn = ats_access, 2509 .writefn = ats_write, .type = ARM_CP_NO_RAW }, 2510 #endif 2511 REGINFO_SENTINEL 2512 }; 2513 2514 /* Return basic MPU access permission bits. */ 2515 static uint32_t simple_mpu_ap_bits(uint32_t val) 2516 { 2517 uint32_t ret; 2518 uint32_t mask; 2519 int i; 2520 ret = 0; 2521 mask = 3; 2522 for (i = 0; i < 16; i += 2) { 2523 ret |= (val >> i) & mask; 2524 mask <<= 2; 2525 } 2526 return ret; 2527 } 2528 2529 /* Pad basic MPU access permission bits to extended format. */ 2530 static uint32_t extended_mpu_ap_bits(uint32_t val) 2531 { 2532 uint32_t ret; 2533 uint32_t mask; 2534 int i; 2535 ret = 0; 2536 mask = 3; 2537 for (i = 0; i < 16; i += 2) { 2538 ret |= (val & mask) << i; 2539 mask <<= 2; 2540 } 2541 return ret; 2542 } 2543 2544 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 2545 uint64_t value) 2546 { 2547 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); 2548 } 2549 2550 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 2551 { 2552 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); 2553 } 2554 2555 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 2556 uint64_t value) 2557 { 2558 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); 2559 } 2560 2561 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 2562 { 2563 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); 2564 } 2565 2566 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri) 2567 { 2568 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 2569 2570 if (!u32p) { 2571 return 0; 2572 } 2573 2574 u32p += env->pmsav7.rnr[M_REG_NS]; 2575 return *u32p; 2576 } 2577 2578 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri, 2579 uint64_t value) 2580 { 2581 ARMCPU *cpu = arm_env_get_cpu(env); 2582 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 2583 2584 if (!u32p) { 2585 return; 2586 } 2587 2588 u32p += env->pmsav7.rnr[M_REG_NS]; 2589 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 2590 *u32p = value; 2591 } 2592 2593 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2594 uint64_t value) 2595 { 2596 ARMCPU *cpu = arm_env_get_cpu(env); 2597 uint32_t nrgs = cpu->pmsav7_dregion; 2598 2599 if (value >= nrgs) { 2600 qemu_log_mask(LOG_GUEST_ERROR, 2601 "PMSAv7 RGNR write >= # supported regions, %" PRIu32 2602 " > %" PRIu32 "\n", (uint32_t)value, nrgs); 2603 return; 2604 } 2605 2606 raw_write(env, ri, value); 2607 } 2608 2609 static const ARMCPRegInfo pmsav7_cp_reginfo[] = { 2610 /* Reset for all these registers is handled in arm_cpu_reset(), 2611 * because the PMSAv7 is also used by M-profile CPUs, which do 2612 * not register cpregs but still need the state to be reset. 2613 */ 2614 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0, 2615 .access = PL1_RW, .type = ARM_CP_NO_RAW, 2616 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar), 2617 .readfn = pmsav7_read, .writefn = pmsav7_write, 2618 .resetfn = arm_cp_reset_ignore }, 2619 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2, 2620 .access = PL1_RW, .type = ARM_CP_NO_RAW, 2621 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr), 2622 .readfn = pmsav7_read, .writefn = pmsav7_write, 2623 .resetfn = arm_cp_reset_ignore }, 2624 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4, 2625 .access = PL1_RW, .type = ARM_CP_NO_RAW, 2626 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr), 2627 .readfn = pmsav7_read, .writefn = pmsav7_write, 2628 .resetfn = arm_cp_reset_ignore }, 2629 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0, 2630 .access = PL1_RW, 2631 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]), 2632 .writefn = pmsav7_rgnr_write, 2633 .resetfn = arm_cp_reset_ignore }, 2634 REGINFO_SENTINEL 2635 }; 2636 2637 static const ARMCPRegInfo pmsav5_cp_reginfo[] = { 2638 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 2639 .access = PL1_RW, .type = ARM_CP_ALIAS, 2640 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 2641 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, 2642 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 2643 .access = PL1_RW, .type = ARM_CP_ALIAS, 2644 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 2645 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, 2646 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, 2647 .access = PL1_RW, 2648 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 2649 .resetvalue = 0, }, 2650 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, 2651 .access = PL1_RW, 2652 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 2653 .resetvalue = 0, }, 2654 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 2655 .access = PL1_RW, 2656 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, 2657 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, 2658 .access = PL1_RW, 2659 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, 2660 /* Protection region base and size registers */ 2661 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, 2662 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2663 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, 2664 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, 2665 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2666 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, 2667 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, 2668 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2669 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, 2670 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, 2671 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2672 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, 2673 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, 2674 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2675 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, 2676 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, 2677 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2678 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, 2679 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, 2680 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2681 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, 2682 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, 2683 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 2684 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, 2685 REGINFO_SENTINEL 2686 }; 2687 2688 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 2689 uint64_t value) 2690 { 2691 TCR *tcr = raw_ptr(env, ri); 2692 int maskshift = extract32(value, 0, 3); 2693 2694 if (!arm_feature(env, ARM_FEATURE_V8)) { 2695 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { 2696 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when 2697 * using Long-desciptor translation table format */ 2698 value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); 2699 } else if (arm_feature(env, ARM_FEATURE_EL3)) { 2700 /* In an implementation that includes the Security Extensions 2701 * TTBCR has additional fields PD0 [4] and PD1 [5] for 2702 * Short-descriptor translation table format. 2703 */ 2704 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; 2705 } else { 2706 value &= TTBCR_N; 2707 } 2708 } 2709 2710 /* Update the masks corresponding to the TCR bank being written 2711 * Note that we always calculate mask and base_mask, but 2712 * they are only used for short-descriptor tables (ie if EAE is 0); 2713 * for long-descriptor tables the TCR fields are used differently 2714 * and the mask and base_mask values are meaningless. 2715 */ 2716 tcr->raw_tcr = value; 2717 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift); 2718 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift); 2719 } 2720 2721 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2722 uint64_t value) 2723 { 2724 ARMCPU *cpu = arm_env_get_cpu(env); 2725 2726 if (arm_feature(env, ARM_FEATURE_LPAE)) { 2727 /* With LPAE the TTBCR could result in a change of ASID 2728 * via the TTBCR.A1 bit, so do a TLB flush. 2729 */ 2730 tlb_flush(CPU(cpu)); 2731 } 2732 vmsa_ttbcr_raw_write(env, ri, value); 2733 } 2734 2735 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2736 { 2737 TCR *tcr = raw_ptr(env, ri); 2738 2739 /* Reset both the TCR as well as the masks corresponding to the bank of 2740 * the TCR being reset. 2741 */ 2742 tcr->raw_tcr = 0; 2743 tcr->mask = 0; 2744 tcr->base_mask = 0xffffc000u; 2745 } 2746 2747 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, 2748 uint64_t value) 2749 { 2750 ARMCPU *cpu = arm_env_get_cpu(env); 2751 TCR *tcr = raw_ptr(env, ri); 2752 2753 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ 2754 tlb_flush(CPU(cpu)); 2755 tcr->raw_tcr = value; 2756 } 2757 2758 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2759 uint64_t value) 2760 { 2761 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */ 2762 if (cpreg_field_is_64bit(ri) && 2763 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) { 2764 ARMCPU *cpu = arm_env_get_cpu(env); 2765 tlb_flush(CPU(cpu)); 2766 } 2767 raw_write(env, ri, value); 2768 } 2769 2770 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2771 uint64_t value) 2772 { 2773 ARMCPU *cpu = arm_env_get_cpu(env); 2774 CPUState *cs = CPU(cpu); 2775 2776 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */ 2777 if (raw_read(env, ri) != value) { 2778 tlb_flush_by_mmuidx(cs, 2779 ARMMMUIdxBit_S12NSE1 | 2780 ARMMMUIdxBit_S12NSE0 | 2781 ARMMMUIdxBit_S2NS); 2782 raw_write(env, ri, value); 2783 } 2784 } 2785 2786 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = { 2787 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 2788 .access = PL1_RW, .type = ARM_CP_ALIAS, 2789 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), 2790 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, }, 2791 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 2792 .access = PL1_RW, .resetvalue = 0, 2793 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), 2794 offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, 2795 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, 2796 .access = PL1_RW, .resetvalue = 0, 2797 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), 2798 offsetof(CPUARMState, cp15.dfar_ns) } }, 2799 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, 2800 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, 2801 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), 2802 .resetvalue = 0, }, 2803 REGINFO_SENTINEL 2804 }; 2805 2806 static const ARMCPRegInfo vmsa_cp_reginfo[] = { 2807 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, 2808 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, 2809 .access = PL1_RW, 2810 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, 2811 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, 2812 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, 2813 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 2814 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 2815 offsetof(CPUARMState, cp15.ttbr0_ns) } }, 2816 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, 2817 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, 2818 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 2819 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 2820 offsetof(CPUARMState, cp15.ttbr1_ns) } }, 2821 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, 2822 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 2823 .access = PL1_RW, .writefn = vmsa_tcr_el1_write, 2824 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write, 2825 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, 2826 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 2827 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write, 2828 .raw_writefn = vmsa_ttbcr_raw_write, 2829 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), 2830 offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, 2831 REGINFO_SENTINEL 2832 }; 2833 2834 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, 2835 uint64_t value) 2836 { 2837 env->cp15.c15_ticonfig = value & 0xe7; 2838 /* The OS_TYPE bit in this register changes the reported CPUID! */ 2839 env->cp15.c0_cpuid = (value & (1 << 5)) ? 2840 ARM_CPUID_TI915T : ARM_CPUID_TI925T; 2841 } 2842 2843 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, 2844 uint64_t value) 2845 { 2846 env->cp15.c15_threadid = value & 0xffff; 2847 } 2848 2849 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, 2850 uint64_t value) 2851 { 2852 /* Wait-for-interrupt (deprecated) */ 2853 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT); 2854 } 2855 2856 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, 2857 uint64_t value) 2858 { 2859 /* On OMAP there are registers indicating the max/min index of dcache lines 2860 * containing a dirty line; cache flush operations have to reset these. 2861 */ 2862 env->cp15.c15_i_max = 0x000; 2863 env->cp15.c15_i_min = 0xff0; 2864 } 2865 2866 static const ARMCPRegInfo omap_cp_reginfo[] = { 2867 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, 2868 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, 2869 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), 2870 .resetvalue = 0, }, 2871 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, 2872 .access = PL1_RW, .type = ARM_CP_NOP }, 2873 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, 2874 .access = PL1_RW, 2875 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, 2876 .writefn = omap_ticonfig_write }, 2877 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, 2878 .access = PL1_RW, 2879 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, 2880 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, 2881 .access = PL1_RW, .resetvalue = 0xff0, 2882 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, 2883 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, 2884 .access = PL1_RW, 2885 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, 2886 .writefn = omap_threadid_write }, 2887 { .name = "TI925T_STATUS", .cp = 15, .crn = 15, 2888 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 2889 .type = ARM_CP_NO_RAW, 2890 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, 2891 /* TODO: Peripheral port remap register: 2892 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller 2893 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), 2894 * when MMU is off. 2895 */ 2896 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 2897 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 2898 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW, 2899 .writefn = omap_cachemaint_write }, 2900 { .name = "C9", .cp = 15, .crn = 9, 2901 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, 2902 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, 2903 REGINFO_SENTINEL 2904 }; 2905 2906 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, 2907 uint64_t value) 2908 { 2909 env->cp15.c15_cpar = value & 0x3fff; 2910 } 2911 2912 static const ARMCPRegInfo xscale_cp_reginfo[] = { 2913 { .name = "XSCALE_CPAR", 2914 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 2915 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, 2916 .writefn = xscale_cpar_write, }, 2917 { .name = "XSCALE_AUXCR", 2918 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, 2919 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), 2920 .resetvalue = 0, }, 2921 /* XScale specific cache-lockdown: since we have no cache we NOP these 2922 * and hope the guest does not really rely on cache behaviour. 2923 */ 2924 { .name = "XSCALE_LOCK_ICACHE_LINE", 2925 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, 2926 .access = PL1_W, .type = ARM_CP_NOP }, 2927 { .name = "XSCALE_UNLOCK_ICACHE", 2928 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, 2929 .access = PL1_W, .type = ARM_CP_NOP }, 2930 { .name = "XSCALE_DCACHE_LOCK", 2931 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, 2932 .access = PL1_RW, .type = ARM_CP_NOP }, 2933 { .name = "XSCALE_UNLOCK_DCACHE", 2934 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, 2935 .access = PL1_W, .type = ARM_CP_NOP }, 2936 REGINFO_SENTINEL 2937 }; 2938 2939 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { 2940 /* RAZ/WI the whole crn=15 space, when we don't have a more specific 2941 * implementation of this implementation-defined space. 2942 * Ideally this should eventually disappear in favour of actually 2943 * implementing the correct behaviour for all cores. 2944 */ 2945 { .name = "C15_IMPDEF", .cp = 15, .crn = 15, 2946 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 2947 .access = PL1_RW, 2948 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE, 2949 .resetvalue = 0 }, 2950 REGINFO_SENTINEL 2951 }; 2952 2953 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { 2954 /* Cache status: RAZ because we have no cache so it's always clean */ 2955 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, 2956 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 2957 .resetvalue = 0 }, 2958 REGINFO_SENTINEL 2959 }; 2960 2961 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { 2962 /* We never have a a block transfer operation in progress */ 2963 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, 2964 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 2965 .resetvalue = 0 }, 2966 /* The cache ops themselves: these all NOP for QEMU */ 2967 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, 2968 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2969 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, 2970 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2971 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, 2972 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2973 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, 2974 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2975 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, 2976 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2977 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, 2978 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 2979 REGINFO_SENTINEL 2980 }; 2981 2982 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { 2983 /* The cache test-and-clean instructions always return (1 << 30) 2984 * to indicate that there are no dirty cache lines. 2985 */ 2986 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, 2987 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 2988 .resetvalue = (1 << 30) }, 2989 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, 2990 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 2991 .resetvalue = (1 << 30) }, 2992 REGINFO_SENTINEL 2993 }; 2994 2995 static const ARMCPRegInfo strongarm_cp_reginfo[] = { 2996 /* Ignore ReadBuffer accesses */ 2997 { .name = "C9_READBUFFER", .cp = 15, .crn = 9, 2998 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 2999 .access = PL1_RW, .resetvalue = 0, 3000 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW }, 3001 REGINFO_SENTINEL 3002 }; 3003 3004 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3005 { 3006 ARMCPU *cpu = arm_env_get_cpu(env); 3007 unsigned int cur_el = arm_current_el(env); 3008 bool secure = arm_is_secure(env); 3009 3010 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 3011 return env->cp15.vpidr_el2; 3012 } 3013 return raw_read(env, ri); 3014 } 3015 3016 static uint64_t mpidr_read_val(CPUARMState *env) 3017 { 3018 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env)); 3019 uint64_t mpidr = cpu->mp_affinity; 3020 3021 if (arm_feature(env, ARM_FEATURE_V7MP)) { 3022 mpidr |= (1U << 31); 3023 /* Cores which are uniprocessor (non-coherent) 3024 * but still implement the MP extensions set 3025 * bit 30. (For instance, Cortex-R5). 3026 */ 3027 if (cpu->mp_is_up) { 3028 mpidr |= (1u << 30); 3029 } 3030 } 3031 return mpidr; 3032 } 3033 3034 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3035 { 3036 unsigned int cur_el = arm_current_el(env); 3037 bool secure = arm_is_secure(env); 3038 3039 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 3040 return env->cp15.vmpidr_el2; 3041 } 3042 return mpidr_read_val(env); 3043 } 3044 3045 static const ARMCPRegInfo mpidr_cp_reginfo[] = { 3046 { .name = "MPIDR", .state = ARM_CP_STATE_BOTH, 3047 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, 3048 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW }, 3049 REGINFO_SENTINEL 3050 }; 3051 3052 static const ARMCPRegInfo lpae_cp_reginfo[] = { 3053 /* NOP AMAIR0/1 */ 3054 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, 3055 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, 3056 .access = PL1_RW, .type = ARM_CP_CONST, 3057 .resetvalue = 0 }, 3058 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ 3059 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, 3060 .access = PL1_RW, .type = ARM_CP_CONST, 3061 .resetvalue = 0 }, 3062 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, 3063 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, 3064 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), 3065 offsetof(CPUARMState, cp15.par_ns)} }, 3066 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, 3067 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3068 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 3069 offsetof(CPUARMState, cp15.ttbr0_ns) }, 3070 .writefn = vmsa_ttbr_write, }, 3071 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, 3072 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3073 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 3074 offsetof(CPUARMState, cp15.ttbr1_ns) }, 3075 .writefn = vmsa_ttbr_write, }, 3076 REGINFO_SENTINEL 3077 }; 3078 3079 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3080 { 3081 return vfp_get_fpcr(env); 3082 } 3083 3084 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3085 uint64_t value) 3086 { 3087 vfp_set_fpcr(env, value); 3088 } 3089 3090 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3091 { 3092 return vfp_get_fpsr(env); 3093 } 3094 3095 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3096 uint64_t value) 3097 { 3098 vfp_set_fpsr(env, value); 3099 } 3100 3101 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri, 3102 bool isread) 3103 { 3104 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) { 3105 return CP_ACCESS_TRAP; 3106 } 3107 return CP_ACCESS_OK; 3108 } 3109 3110 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, 3111 uint64_t value) 3112 { 3113 env->daif = value & PSTATE_DAIF; 3114 } 3115 3116 static CPAccessResult aa64_cacheop_access(CPUARMState *env, 3117 const ARMCPRegInfo *ri, 3118 bool isread) 3119 { 3120 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless 3121 * SCTLR_EL1.UCI is set. 3122 */ 3123 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) { 3124 return CP_ACCESS_TRAP; 3125 } 3126 return CP_ACCESS_OK; 3127 } 3128 3129 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions 3130 * Page D4-1736 (DDI0487A.b) 3131 */ 3132 3133 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3134 uint64_t value) 3135 { 3136 CPUState *cs = ENV_GET_CPU(env); 3137 bool sec = arm_is_secure_below_el3(env); 3138 3139 if (sec) { 3140 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3141 ARMMMUIdxBit_S1SE1 | 3142 ARMMMUIdxBit_S1SE0); 3143 } else { 3144 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3145 ARMMMUIdxBit_S12NSE1 | 3146 ARMMMUIdxBit_S12NSE0); 3147 } 3148 } 3149 3150 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3151 uint64_t value) 3152 { 3153 CPUState *cs = ENV_GET_CPU(env); 3154 3155 if (tlb_force_broadcast(env)) { 3156 tlbi_aa64_vmalle1_write(env, NULL, value); 3157 return; 3158 } 3159 3160 if (arm_is_secure_below_el3(env)) { 3161 tlb_flush_by_mmuidx(cs, 3162 ARMMMUIdxBit_S1SE1 | 3163 ARMMMUIdxBit_S1SE0); 3164 } else { 3165 tlb_flush_by_mmuidx(cs, 3166 ARMMMUIdxBit_S12NSE1 | 3167 ARMMMUIdxBit_S12NSE0); 3168 } 3169 } 3170 3171 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3172 uint64_t value) 3173 { 3174 /* Note that the 'ALL' scope must invalidate both stage 1 and 3175 * stage 2 translations, whereas most other scopes only invalidate 3176 * stage 1 translations. 3177 */ 3178 ARMCPU *cpu = arm_env_get_cpu(env); 3179 CPUState *cs = CPU(cpu); 3180 3181 if (arm_is_secure_below_el3(env)) { 3182 tlb_flush_by_mmuidx(cs, 3183 ARMMMUIdxBit_S1SE1 | 3184 ARMMMUIdxBit_S1SE0); 3185 } else { 3186 if (arm_feature(env, ARM_FEATURE_EL2)) { 3187 tlb_flush_by_mmuidx(cs, 3188 ARMMMUIdxBit_S12NSE1 | 3189 ARMMMUIdxBit_S12NSE0 | 3190 ARMMMUIdxBit_S2NS); 3191 } else { 3192 tlb_flush_by_mmuidx(cs, 3193 ARMMMUIdxBit_S12NSE1 | 3194 ARMMMUIdxBit_S12NSE0); 3195 } 3196 } 3197 } 3198 3199 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3200 uint64_t value) 3201 { 3202 ARMCPU *cpu = arm_env_get_cpu(env); 3203 CPUState *cs = CPU(cpu); 3204 3205 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2); 3206 } 3207 3208 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri, 3209 uint64_t value) 3210 { 3211 ARMCPU *cpu = arm_env_get_cpu(env); 3212 CPUState *cs = CPU(cpu); 3213 3214 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3); 3215 } 3216 3217 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3218 uint64_t value) 3219 { 3220 /* Note that the 'ALL' scope must invalidate both stage 1 and 3221 * stage 2 translations, whereas most other scopes only invalidate 3222 * stage 1 translations. 3223 */ 3224 CPUState *cs = ENV_GET_CPU(env); 3225 bool sec = arm_is_secure_below_el3(env); 3226 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2); 3227 3228 if (sec) { 3229 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3230 ARMMMUIdxBit_S1SE1 | 3231 ARMMMUIdxBit_S1SE0); 3232 } else if (has_el2) { 3233 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3234 ARMMMUIdxBit_S12NSE1 | 3235 ARMMMUIdxBit_S12NSE0 | 3236 ARMMMUIdxBit_S2NS); 3237 } else { 3238 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3239 ARMMMUIdxBit_S12NSE1 | 3240 ARMMMUIdxBit_S12NSE0); 3241 } 3242 } 3243 3244 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3245 uint64_t value) 3246 { 3247 CPUState *cs = ENV_GET_CPU(env); 3248 3249 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2); 3250 } 3251 3252 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3253 uint64_t value) 3254 { 3255 CPUState *cs = ENV_GET_CPU(env); 3256 3257 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3); 3258 } 3259 3260 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3261 uint64_t value) 3262 { 3263 /* Invalidate by VA, EL2 3264 * Currently handles both VAE2 and VALE2, since we don't support 3265 * flush-last-level-only. 3266 */ 3267 ARMCPU *cpu = arm_env_get_cpu(env); 3268 CPUState *cs = CPU(cpu); 3269 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3270 3271 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2); 3272 } 3273 3274 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri, 3275 uint64_t value) 3276 { 3277 /* Invalidate by VA, EL3 3278 * Currently handles both VAE3 and VALE3, since we don't support 3279 * flush-last-level-only. 3280 */ 3281 ARMCPU *cpu = arm_env_get_cpu(env); 3282 CPUState *cs = CPU(cpu); 3283 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3284 3285 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3); 3286 } 3287 3288 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3289 uint64_t value) 3290 { 3291 ARMCPU *cpu = arm_env_get_cpu(env); 3292 CPUState *cs = CPU(cpu); 3293 bool sec = arm_is_secure_below_el3(env); 3294 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3295 3296 if (sec) { 3297 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3298 ARMMMUIdxBit_S1SE1 | 3299 ARMMMUIdxBit_S1SE0); 3300 } else { 3301 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3302 ARMMMUIdxBit_S12NSE1 | 3303 ARMMMUIdxBit_S12NSE0); 3304 } 3305 } 3306 3307 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3308 uint64_t value) 3309 { 3310 /* Invalidate by VA, EL1&0 (AArch64 version). 3311 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1, 3312 * since we don't support flush-for-specific-ASID-only or 3313 * flush-last-level-only. 3314 */ 3315 ARMCPU *cpu = arm_env_get_cpu(env); 3316 CPUState *cs = CPU(cpu); 3317 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3318 3319 if (tlb_force_broadcast(env)) { 3320 tlbi_aa64_vae1is_write(env, NULL, value); 3321 return; 3322 } 3323 3324 if (arm_is_secure_below_el3(env)) { 3325 tlb_flush_page_by_mmuidx(cs, pageaddr, 3326 ARMMMUIdxBit_S1SE1 | 3327 ARMMMUIdxBit_S1SE0); 3328 } else { 3329 tlb_flush_page_by_mmuidx(cs, pageaddr, 3330 ARMMMUIdxBit_S12NSE1 | 3331 ARMMMUIdxBit_S12NSE0); 3332 } 3333 } 3334 3335 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3336 uint64_t value) 3337 { 3338 CPUState *cs = ENV_GET_CPU(env); 3339 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3340 3341 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3342 ARMMMUIdxBit_S1E2); 3343 } 3344 3345 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3346 uint64_t value) 3347 { 3348 CPUState *cs = ENV_GET_CPU(env); 3349 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3350 3351 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3352 ARMMMUIdxBit_S1E3); 3353 } 3354 3355 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3356 uint64_t value) 3357 { 3358 /* Invalidate by IPA. This has to invalidate any structures that 3359 * contain only stage 2 translation information, but does not need 3360 * to apply to structures that contain combined stage 1 and stage 2 3361 * translation information. 3362 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. 3363 */ 3364 ARMCPU *cpu = arm_env_get_cpu(env); 3365 CPUState *cs = CPU(cpu); 3366 uint64_t pageaddr; 3367 3368 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 3369 return; 3370 } 3371 3372 pageaddr = sextract64(value << 12, 0, 48); 3373 3374 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS); 3375 } 3376 3377 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3378 uint64_t value) 3379 { 3380 CPUState *cs = ENV_GET_CPU(env); 3381 uint64_t pageaddr; 3382 3383 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 3384 return; 3385 } 3386 3387 pageaddr = sextract64(value << 12, 0, 48); 3388 3389 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3390 ARMMMUIdxBit_S2NS); 3391 } 3392 3393 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri, 3394 bool isread) 3395 { 3396 /* We don't implement EL2, so the only control on DC ZVA is the 3397 * bit in the SCTLR which can prohibit access for EL0. 3398 */ 3399 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) { 3400 return CP_ACCESS_TRAP; 3401 } 3402 return CP_ACCESS_OK; 3403 } 3404 3405 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) 3406 { 3407 ARMCPU *cpu = arm_env_get_cpu(env); 3408 int dzp_bit = 1 << 4; 3409 3410 /* DZP indicates whether DC ZVA access is allowed */ 3411 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) { 3412 dzp_bit = 0; 3413 } 3414 return cpu->dcz_blocksize | dzp_bit; 3415 } 3416 3417 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 3418 bool isread) 3419 { 3420 if (!(env->pstate & PSTATE_SP)) { 3421 /* Access to SP_EL0 is undefined if it's being used as 3422 * the stack pointer. 3423 */ 3424 return CP_ACCESS_TRAP_UNCATEGORIZED; 3425 } 3426 return CP_ACCESS_OK; 3427 } 3428 3429 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) 3430 { 3431 return env->pstate & PSTATE_SP; 3432 } 3433 3434 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 3435 { 3436 update_spsel(env, val); 3437 } 3438 3439 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3440 uint64_t value) 3441 { 3442 ARMCPU *cpu = arm_env_get_cpu(env); 3443 3444 if (raw_read(env, ri) == value) { 3445 /* Skip the TLB flush if nothing actually changed; Linux likes 3446 * to do a lot of pointless SCTLR writes. 3447 */ 3448 return; 3449 } 3450 3451 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) { 3452 /* M bit is RAZ/WI for PMSA with no MPU implemented */ 3453 value &= ~SCTLR_M; 3454 } 3455 3456 raw_write(env, ri, value); 3457 /* ??? Lots of these bits are not implemented. */ 3458 /* This may enable/disable the MMU, so do a TLB flush. */ 3459 tlb_flush(CPU(cpu)); 3460 } 3461 3462 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri, 3463 bool isread) 3464 { 3465 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) { 3466 return CP_ACCESS_TRAP_FP_EL2; 3467 } 3468 if (env->cp15.cptr_el[3] & CPTR_TFP) { 3469 return CP_ACCESS_TRAP_FP_EL3; 3470 } 3471 return CP_ACCESS_OK; 3472 } 3473 3474 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3475 uint64_t value) 3476 { 3477 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK; 3478 } 3479 3480 static const ARMCPRegInfo v8_cp_reginfo[] = { 3481 /* Minimal set of EL0-visible registers. This will need to be expanded 3482 * significantly for system emulation of AArch64 CPUs. 3483 */ 3484 { .name = "NZCV", .state = ARM_CP_STATE_AA64, 3485 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, 3486 .access = PL0_RW, .type = ARM_CP_NZCV }, 3487 { .name = "DAIF", .state = ARM_CP_STATE_AA64, 3488 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, 3489 .type = ARM_CP_NO_RAW, 3490 .access = PL0_RW, .accessfn = aa64_daif_access, 3491 .fieldoffset = offsetof(CPUARMState, daif), 3492 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, 3493 { .name = "FPCR", .state = ARM_CP_STATE_AA64, 3494 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, 3495 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 3496 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, 3497 { .name = "FPSR", .state = ARM_CP_STATE_AA64, 3498 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, 3499 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 3500 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, 3501 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, 3502 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, 3503 .access = PL0_R, .type = ARM_CP_NO_RAW, 3504 .readfn = aa64_dczid_read }, 3505 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, 3506 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, 3507 .access = PL0_W, .type = ARM_CP_DC_ZVA, 3508 #ifndef CONFIG_USER_ONLY 3509 /* Avoid overhead of an access check that always passes in user-mode */ 3510 .accessfn = aa64_zva_access, 3511 #endif 3512 }, 3513 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, 3514 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, 3515 .access = PL1_R, .type = ARM_CP_CURRENTEL }, 3516 /* Cache ops: all NOPs since we don't emulate caches */ 3517 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, 3518 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 3519 .access = PL1_W, .type = ARM_CP_NOP }, 3520 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, 3521 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 3522 .access = PL1_W, .type = ARM_CP_NOP }, 3523 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, 3524 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, 3525 .access = PL0_W, .type = ARM_CP_NOP, 3526 .accessfn = aa64_cacheop_access }, 3527 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, 3528 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 3529 .access = PL1_W, .type = ARM_CP_NOP }, 3530 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, 3531 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 3532 .access = PL1_W, .type = ARM_CP_NOP }, 3533 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, 3534 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, 3535 .access = PL0_W, .type = ARM_CP_NOP, 3536 .accessfn = aa64_cacheop_access }, 3537 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, 3538 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 3539 .access = PL1_W, .type = ARM_CP_NOP }, 3540 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, 3541 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, 3542 .access = PL0_W, .type = ARM_CP_NOP, 3543 .accessfn = aa64_cacheop_access }, 3544 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, 3545 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, 3546 .access = PL0_W, .type = ARM_CP_NOP, 3547 .accessfn = aa64_cacheop_access }, 3548 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, 3549 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 3550 .access = PL1_W, .type = ARM_CP_NOP }, 3551 /* TLBI operations */ 3552 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64, 3553 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 3554 .access = PL1_W, .type = ARM_CP_NO_RAW, 3555 .writefn = tlbi_aa64_vmalle1is_write }, 3556 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64, 3557 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 3558 .access = PL1_W, .type = ARM_CP_NO_RAW, 3559 .writefn = tlbi_aa64_vae1is_write }, 3560 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64, 3561 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 3562 .access = PL1_W, .type = ARM_CP_NO_RAW, 3563 .writefn = tlbi_aa64_vmalle1is_write }, 3564 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64, 3565 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 3566 .access = PL1_W, .type = ARM_CP_NO_RAW, 3567 .writefn = tlbi_aa64_vae1is_write }, 3568 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64, 3569 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 3570 .access = PL1_W, .type = ARM_CP_NO_RAW, 3571 .writefn = tlbi_aa64_vae1is_write }, 3572 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64, 3573 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 3574 .access = PL1_W, .type = ARM_CP_NO_RAW, 3575 .writefn = tlbi_aa64_vae1is_write }, 3576 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64, 3577 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 3578 .access = PL1_W, .type = ARM_CP_NO_RAW, 3579 .writefn = tlbi_aa64_vmalle1_write }, 3580 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64, 3581 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 3582 .access = PL1_W, .type = ARM_CP_NO_RAW, 3583 .writefn = tlbi_aa64_vae1_write }, 3584 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64, 3585 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 3586 .access = PL1_W, .type = ARM_CP_NO_RAW, 3587 .writefn = tlbi_aa64_vmalle1_write }, 3588 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64, 3589 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 3590 .access = PL1_W, .type = ARM_CP_NO_RAW, 3591 .writefn = tlbi_aa64_vae1_write }, 3592 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64, 3593 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 3594 .access = PL1_W, .type = ARM_CP_NO_RAW, 3595 .writefn = tlbi_aa64_vae1_write }, 3596 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64, 3597 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 3598 .access = PL1_W, .type = ARM_CP_NO_RAW, 3599 .writefn = tlbi_aa64_vae1_write }, 3600 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64, 3601 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 3602 .access = PL2_W, .type = ARM_CP_NO_RAW, 3603 .writefn = tlbi_aa64_ipas2e1is_write }, 3604 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64, 3605 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 3606 .access = PL2_W, .type = ARM_CP_NO_RAW, 3607 .writefn = tlbi_aa64_ipas2e1is_write }, 3608 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64, 3609 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 3610 .access = PL2_W, .type = ARM_CP_NO_RAW, 3611 .writefn = tlbi_aa64_alle1is_write }, 3612 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64, 3613 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6, 3614 .access = PL2_W, .type = ARM_CP_NO_RAW, 3615 .writefn = tlbi_aa64_alle1is_write }, 3616 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64, 3617 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 3618 .access = PL2_W, .type = ARM_CP_NO_RAW, 3619 .writefn = tlbi_aa64_ipas2e1_write }, 3620 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64, 3621 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 3622 .access = PL2_W, .type = ARM_CP_NO_RAW, 3623 .writefn = tlbi_aa64_ipas2e1_write }, 3624 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64, 3625 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 3626 .access = PL2_W, .type = ARM_CP_NO_RAW, 3627 .writefn = tlbi_aa64_alle1_write }, 3628 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64, 3629 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6, 3630 .access = PL2_W, .type = ARM_CP_NO_RAW, 3631 .writefn = tlbi_aa64_alle1is_write }, 3632 #ifndef CONFIG_USER_ONLY 3633 /* 64 bit address translation operations */ 3634 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, 3635 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, 3636 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3637 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, 3638 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, 3639 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3640 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, 3641 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, 3642 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3643 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, 3644 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, 3645 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3646 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64, 3647 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4, 3648 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3649 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64, 3650 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5, 3651 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3652 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64, 3653 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6, 3654 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3655 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64, 3656 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7, 3657 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3658 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */ 3659 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64, 3660 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0, 3661 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3662 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64, 3663 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1, 3664 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 3665 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64, 3666 .type = ARM_CP_ALIAS, 3667 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0, 3668 .access = PL1_RW, .resetvalue = 0, 3669 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]), 3670 .writefn = par_write }, 3671 #endif 3672 /* TLB invalidate last level of translation table walk */ 3673 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 3674 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, 3675 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 3676 .type = ARM_CP_NO_RAW, .access = PL1_W, 3677 .writefn = tlbimvaa_is_write }, 3678 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 3679 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 3680 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 3681 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, 3682 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 3683 .type = ARM_CP_NO_RAW, .access = PL2_W, 3684 .writefn = tlbimva_hyp_write }, 3685 { .name = "TLBIMVALHIS", 3686 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 3687 .type = ARM_CP_NO_RAW, .access = PL2_W, 3688 .writefn = tlbimva_hyp_is_write }, 3689 { .name = "TLBIIPAS2", 3690 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 3691 .type = ARM_CP_NO_RAW, .access = PL2_W, 3692 .writefn = tlbiipas2_write }, 3693 { .name = "TLBIIPAS2IS", 3694 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 3695 .type = ARM_CP_NO_RAW, .access = PL2_W, 3696 .writefn = tlbiipas2_is_write }, 3697 { .name = "TLBIIPAS2L", 3698 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 3699 .type = ARM_CP_NO_RAW, .access = PL2_W, 3700 .writefn = tlbiipas2_write }, 3701 { .name = "TLBIIPAS2LIS", 3702 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 3703 .type = ARM_CP_NO_RAW, .access = PL2_W, 3704 .writefn = tlbiipas2_is_write }, 3705 /* 32 bit cache operations */ 3706 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 3707 .type = ARM_CP_NOP, .access = PL1_W }, 3708 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, 3709 .type = ARM_CP_NOP, .access = PL1_W }, 3710 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 3711 .type = ARM_CP_NOP, .access = PL1_W }, 3712 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, 3713 .type = ARM_CP_NOP, .access = PL1_W }, 3714 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, 3715 .type = ARM_CP_NOP, .access = PL1_W }, 3716 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, 3717 .type = ARM_CP_NOP, .access = PL1_W }, 3718 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 3719 .type = ARM_CP_NOP, .access = PL1_W }, 3720 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 3721 .type = ARM_CP_NOP, .access = PL1_W }, 3722 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, 3723 .type = ARM_CP_NOP, .access = PL1_W }, 3724 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 3725 .type = ARM_CP_NOP, .access = PL1_W }, 3726 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, 3727 .type = ARM_CP_NOP, .access = PL1_W }, 3728 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, 3729 .type = ARM_CP_NOP, .access = PL1_W }, 3730 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 3731 .type = ARM_CP_NOP, .access = PL1_W }, 3732 /* MMU Domain access control / MPU write buffer control */ 3733 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, 3734 .access = PL1_RW, .resetvalue = 0, 3735 .writefn = dacr_write, .raw_writefn = raw_write, 3736 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 3737 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 3738 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, 3739 .type = ARM_CP_ALIAS, 3740 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, 3741 .access = PL1_RW, 3742 .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, 3743 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, 3744 .type = ARM_CP_ALIAS, 3745 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, 3746 .access = PL1_RW, 3747 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) }, 3748 /* We rely on the access checks not allowing the guest to write to the 3749 * state field when SPSel indicates that it's being used as the stack 3750 * pointer. 3751 */ 3752 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, 3753 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, 3754 .access = PL1_RW, .accessfn = sp_el0_access, 3755 .type = ARM_CP_ALIAS, 3756 .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, 3757 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64, 3758 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0, 3759 .access = PL2_RW, .type = ARM_CP_ALIAS, 3760 .fieldoffset = offsetof(CPUARMState, sp_el[1]) }, 3761 { .name = "SPSel", .state = ARM_CP_STATE_AA64, 3762 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, 3763 .type = ARM_CP_NO_RAW, 3764 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, 3765 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64, 3766 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0, 3767 .type = ARM_CP_ALIAS, 3768 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]), 3769 .access = PL2_RW, .accessfn = fpexc32_access }, 3770 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, 3771 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, 3772 .access = PL2_RW, .resetvalue = 0, 3773 .writefn = dacr_write, .raw_writefn = raw_write, 3774 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, 3775 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, 3776 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, 3777 .access = PL2_RW, .resetvalue = 0, 3778 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, 3779 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64, 3780 .type = ARM_CP_ALIAS, 3781 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0, 3782 .access = PL2_RW, 3783 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) }, 3784 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64, 3785 .type = ARM_CP_ALIAS, 3786 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1, 3787 .access = PL2_RW, 3788 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) }, 3789 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64, 3790 .type = ARM_CP_ALIAS, 3791 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2, 3792 .access = PL2_RW, 3793 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) }, 3794 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64, 3795 .type = ARM_CP_ALIAS, 3796 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3, 3797 .access = PL2_RW, 3798 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) }, 3799 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64, 3800 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1, 3801 .resetvalue = 0, 3802 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) }, 3803 { .name = "SDCR", .type = ARM_CP_ALIAS, 3804 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1, 3805 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 3806 .writefn = sdcr_write, 3807 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) }, 3808 REGINFO_SENTINEL 3809 }; 3810 3811 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */ 3812 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = { 3813 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 3814 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 3815 .access = PL2_RW, 3816 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, 3817 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH, 3818 .type = ARM_CP_NO_RAW, 3819 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 3820 .access = PL2_RW, 3821 .type = ARM_CP_CONST, .resetvalue = 0 }, 3822 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 3823 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 3824 .access = PL2_RW, 3825 .type = ARM_CP_CONST, .resetvalue = 0 }, 3826 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 3827 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 3828 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3829 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 3830 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 3831 .access = PL2_RW, .type = ARM_CP_CONST, 3832 .resetvalue = 0 }, 3833 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 3834 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 3835 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3836 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 3837 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 3838 .access = PL2_RW, .type = ARM_CP_CONST, 3839 .resetvalue = 0 }, 3840 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 3841 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 3842 .access = PL2_RW, .type = ARM_CP_CONST, 3843 .resetvalue = 0 }, 3844 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 3845 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 3846 .access = PL2_RW, .type = ARM_CP_CONST, 3847 .resetvalue = 0 }, 3848 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 3849 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 3850 .access = PL2_RW, .type = ARM_CP_CONST, 3851 .resetvalue = 0 }, 3852 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 3853 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 3854 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3855 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH, 3856 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 3857 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 3858 .type = ARM_CP_CONST, .resetvalue = 0 }, 3859 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 3860 .cp = 15, .opc1 = 6, .crm = 2, 3861 .access = PL2_RW, .accessfn = access_el3_aa32ns, 3862 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 3863 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 3864 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 3865 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3866 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 3867 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 3868 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3869 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 3870 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 3871 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3872 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 3873 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 3874 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3875 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 3876 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 3877 .resetvalue = 0 }, 3878 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 3879 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 3880 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3881 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 3882 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 3883 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3884 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 3885 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 3886 .resetvalue = 0 }, 3887 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 3888 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 3889 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3890 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 3891 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 3892 .resetvalue = 0 }, 3893 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 3894 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 3895 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3896 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 3897 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 3898 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3899 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 3900 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 3901 .access = PL2_RW, .accessfn = access_tda, 3902 .type = ARM_CP_CONST, .resetvalue = 0 }, 3903 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH, 3904 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 3905 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 3906 .type = ARM_CP_CONST, .resetvalue = 0 }, 3907 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 3908 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 3909 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3910 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 3911 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 3912 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 3913 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 3914 .type = ARM_CP_CONST, 3915 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 3916 .access = PL2_RW, .resetvalue = 0 }, 3917 REGINFO_SENTINEL 3918 }; 3919 3920 /* Ditto, but for registers which exist in ARMv8 but not v7 */ 3921 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = { 3922 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 3923 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 3924 .access = PL2_RW, 3925 .type = ARM_CP_CONST, .resetvalue = 0 }, 3926 REGINFO_SENTINEL 3927 }; 3928 3929 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3930 { 3931 ARMCPU *cpu = arm_env_get_cpu(env); 3932 CPUState *cs = ENV_GET_CPU(env); 3933 uint64_t valid_mask = HCR_MASK; 3934 3935 if (arm_feature(env, ARM_FEATURE_EL3)) { 3936 valid_mask &= ~HCR_HCD; 3937 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) { 3938 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented. 3939 * However, if we're using the SMC PSCI conduit then QEMU is 3940 * effectively acting like EL3 firmware and so the guest at 3941 * EL2 should retain the ability to prevent EL1 from being 3942 * able to make SMC calls into the ersatz firmware, so in 3943 * that case HCR.TSC should be read/write. 3944 */ 3945 valid_mask &= ~HCR_TSC; 3946 } 3947 3948 /* Clear RES0 bits. */ 3949 value &= valid_mask; 3950 3951 /* 3952 * VI and VF are kept in cs->interrupt_request. Modifying that 3953 * requires that we have the iothread lock, which is done by 3954 * marking the reginfo structs as ARM_CP_IO. 3955 * Note that if a write to HCR pends a VIRQ or VFIQ it is never 3956 * possible for it to be taken immediately, because VIRQ and 3957 * VFIQ are masked unless running at EL0 or EL1, and HCR 3958 * can only be written at EL2. 3959 */ 3960 g_assert(qemu_mutex_iothread_locked()); 3961 if (value & HCR_VI) { 3962 cs->interrupt_request |= CPU_INTERRUPT_VIRQ; 3963 } else { 3964 cs->interrupt_request &= ~CPU_INTERRUPT_VIRQ; 3965 } 3966 if (value & HCR_VF) { 3967 cs->interrupt_request |= CPU_INTERRUPT_VFIQ; 3968 } else { 3969 cs->interrupt_request &= ~CPU_INTERRUPT_VFIQ; 3970 } 3971 value &= ~(HCR_VI | HCR_VF); 3972 3973 /* These bits change the MMU setup: 3974 * HCR_VM enables stage 2 translation 3975 * HCR_PTW forbids certain page-table setups 3976 * HCR_DC Disables stage1 and enables stage2 translation 3977 */ 3978 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) { 3979 tlb_flush(CPU(cpu)); 3980 } 3981 env->cp15.hcr_el2 = value; 3982 } 3983 3984 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri, 3985 uint64_t value) 3986 { 3987 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */ 3988 value = deposit64(env->cp15.hcr_el2, 32, 32, value); 3989 hcr_write(env, NULL, value); 3990 } 3991 3992 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri, 3993 uint64_t value) 3994 { 3995 /* Handle HCR write, i.e. write to low half of HCR_EL2 */ 3996 value = deposit64(env->cp15.hcr_el2, 0, 32, value); 3997 hcr_write(env, NULL, value); 3998 } 3999 4000 static uint64_t hcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4001 { 4002 /* The VI and VF bits live in cs->interrupt_request */ 4003 uint64_t ret = env->cp15.hcr_el2 & ~(HCR_VI | HCR_VF); 4004 CPUState *cs = ENV_GET_CPU(env); 4005 4006 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) { 4007 ret |= HCR_VI; 4008 } 4009 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) { 4010 ret |= HCR_VF; 4011 } 4012 return ret; 4013 } 4014 4015 static const ARMCPRegInfo el2_cp_reginfo[] = { 4016 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, 4017 .type = ARM_CP_IO, 4018 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 4019 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 4020 .writefn = hcr_write, .readfn = hcr_read }, 4021 { .name = "HCR", .state = ARM_CP_STATE_AA32, 4022 .type = ARM_CP_ALIAS | ARM_CP_IO, 4023 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 4024 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 4025 .writefn = hcr_writelow, .readfn = hcr_read }, 4026 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, 4027 .type = ARM_CP_ALIAS, 4028 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, 4029 .access = PL2_RW, 4030 .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, 4031 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 4032 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 4033 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, 4034 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 4035 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 4036 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, 4037 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 4038 .type = ARM_CP_ALIAS, 4039 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 4040 .access = PL2_RW, 4041 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) }, 4042 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, 4043 .type = ARM_CP_ALIAS, 4044 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, 4045 .access = PL2_RW, 4046 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) }, 4047 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 4048 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 4049 .access = PL2_RW, .writefn = vbar_write, 4050 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), 4051 .resetvalue = 0 }, 4052 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64, 4053 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0, 4054 .access = PL3_RW, .type = ARM_CP_ALIAS, 4055 .fieldoffset = offsetof(CPUARMState, sp_el[2]) }, 4056 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 4057 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 4058 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0, 4059 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) }, 4060 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 4061 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 4062 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]), 4063 .resetvalue = 0 }, 4064 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 4065 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 4066 .access = PL2_RW, .type = ARM_CP_ALIAS, 4067 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) }, 4068 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 4069 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 4070 .access = PL2_RW, .type = ARM_CP_CONST, 4071 .resetvalue = 0 }, 4072 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */ 4073 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 4074 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 4075 .access = PL2_RW, .type = ARM_CP_CONST, 4076 .resetvalue = 0 }, 4077 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 4078 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 4079 .access = PL2_RW, .type = ARM_CP_CONST, 4080 .resetvalue = 0 }, 4081 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 4082 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 4083 .access = PL2_RW, .type = ARM_CP_CONST, 4084 .resetvalue = 0 }, 4085 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 4086 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 4087 .access = PL2_RW, 4088 /* no .writefn needed as this can't cause an ASID change; 4089 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 4090 */ 4091 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) }, 4092 { .name = "VTCR", .state = ARM_CP_STATE_AA32, 4093 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 4094 .type = ARM_CP_ALIAS, 4095 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4096 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 4097 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64, 4098 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 4099 .access = PL2_RW, 4100 /* no .writefn needed as this can't cause an ASID change; 4101 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 4102 */ 4103 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 4104 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 4105 .cp = 15, .opc1 = 6, .crm = 2, 4106 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4107 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4108 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2), 4109 .writefn = vttbr_write }, 4110 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 4111 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 4112 .access = PL2_RW, .writefn = vttbr_write, 4113 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) }, 4114 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 4115 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 4116 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write, 4117 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) }, 4118 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 4119 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 4120 .access = PL2_RW, .resetvalue = 0, 4121 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) }, 4122 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 4123 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 4124 .access = PL2_RW, .resetvalue = 0, 4125 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 4126 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 4127 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4128 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 4129 { .name = "TLBIALLNSNH", 4130 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 4131 .type = ARM_CP_NO_RAW, .access = PL2_W, 4132 .writefn = tlbiall_nsnh_write }, 4133 { .name = "TLBIALLNSNHIS", 4134 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 4135 .type = ARM_CP_NO_RAW, .access = PL2_W, 4136 .writefn = tlbiall_nsnh_is_write }, 4137 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 4138 .type = ARM_CP_NO_RAW, .access = PL2_W, 4139 .writefn = tlbiall_hyp_write }, 4140 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 4141 .type = ARM_CP_NO_RAW, .access = PL2_W, 4142 .writefn = tlbiall_hyp_is_write }, 4143 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 4144 .type = ARM_CP_NO_RAW, .access = PL2_W, 4145 .writefn = tlbimva_hyp_write }, 4146 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 4147 .type = ARM_CP_NO_RAW, .access = PL2_W, 4148 .writefn = tlbimva_hyp_is_write }, 4149 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64, 4150 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 4151 .type = ARM_CP_NO_RAW, .access = PL2_W, 4152 .writefn = tlbi_aa64_alle2_write }, 4153 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64, 4154 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 4155 .type = ARM_CP_NO_RAW, .access = PL2_W, 4156 .writefn = tlbi_aa64_vae2_write }, 4157 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64, 4158 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 4159 .access = PL2_W, .type = ARM_CP_NO_RAW, 4160 .writefn = tlbi_aa64_vae2_write }, 4161 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64, 4162 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 4163 .access = PL2_W, .type = ARM_CP_NO_RAW, 4164 .writefn = tlbi_aa64_alle2is_write }, 4165 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64, 4166 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 4167 .type = ARM_CP_NO_RAW, .access = PL2_W, 4168 .writefn = tlbi_aa64_vae2is_write }, 4169 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64, 4170 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 4171 .access = PL2_W, .type = ARM_CP_NO_RAW, 4172 .writefn = tlbi_aa64_vae2is_write }, 4173 #ifndef CONFIG_USER_ONLY 4174 /* Unlike the other EL2-related AT operations, these must 4175 * UNDEF from EL3 if EL2 is not implemented, which is why we 4176 * define them here rather than with the rest of the AT ops. 4177 */ 4178 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64, 4179 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 4180 .access = PL2_W, .accessfn = at_s1e2_access, 4181 .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4182 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64, 4183 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 4184 .access = PL2_W, .accessfn = at_s1e2_access, 4185 .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4186 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE 4187 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3 4188 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose 4189 * to behave as if SCR.NS was 1. 4190 */ 4191 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 4192 .access = PL2_W, 4193 .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, 4194 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 4195 .access = PL2_W, 4196 .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, 4197 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 4198 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 4199 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the 4200 * reset values as IMPDEF. We choose to reset to 3 to comply with 4201 * both ARMv7 and ARMv8. 4202 */ 4203 .access = PL2_RW, .resetvalue = 3, 4204 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) }, 4205 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 4206 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 4207 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, 4208 .writefn = gt_cntvoff_write, 4209 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 4210 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 4211 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO, 4212 .writefn = gt_cntvoff_write, 4213 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 4214 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 4215 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 4216 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 4217 .type = ARM_CP_IO, .access = PL2_RW, 4218 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 4219 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 4220 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 4221 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO, 4222 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 4223 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 4224 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 4225 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 4226 .resetfn = gt_hyp_timer_reset, 4227 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write }, 4228 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 4229 .type = ARM_CP_IO, 4230 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 4231 .access = PL2_RW, 4232 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl), 4233 .resetvalue = 0, 4234 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write }, 4235 #endif 4236 /* The only field of MDCR_EL2 that has a defined architectural reset value 4237 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we 4238 * don't impelment any PMU event counters, so using zero as a reset 4239 * value for MDCR_EL2 is okay 4240 */ 4241 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 4242 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 4243 .access = PL2_RW, .resetvalue = 0, 4244 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), }, 4245 { .name = "HPFAR", .state = ARM_CP_STATE_AA32, 4246 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4247 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4248 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 4249 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64, 4250 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4251 .access = PL2_RW, 4252 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 4253 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 4254 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 4255 .access = PL2_RW, 4256 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) }, 4257 REGINFO_SENTINEL 4258 }; 4259 4260 static const ARMCPRegInfo el2_v8_cp_reginfo[] = { 4261 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 4262 .type = ARM_CP_ALIAS | ARM_CP_IO, 4263 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 4264 .access = PL2_RW, 4265 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2), 4266 .writefn = hcr_writehigh }, 4267 REGINFO_SENTINEL 4268 }; 4269 4270 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 4271 bool isread) 4272 { 4273 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2. 4274 * At Secure EL1 it traps to EL3. 4275 */ 4276 if (arm_current_el(env) == 3) { 4277 return CP_ACCESS_OK; 4278 } 4279 if (arm_is_secure_below_el3(env)) { 4280 return CP_ACCESS_TRAP_EL3; 4281 } 4282 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */ 4283 if (isread) { 4284 return CP_ACCESS_OK; 4285 } 4286 return CP_ACCESS_TRAP_UNCATEGORIZED; 4287 } 4288 4289 static const ARMCPRegInfo el3_cp_reginfo[] = { 4290 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, 4291 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, 4292 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), 4293 .resetvalue = 0, .writefn = scr_write }, 4294 { .name = "SCR", .type = ARM_CP_ALIAS, 4295 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, 4296 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 4297 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), 4298 .writefn = scr_write }, 4299 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, 4300 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, 4301 .access = PL3_RW, .resetvalue = 0, 4302 .fieldoffset = offsetof(CPUARMState, cp15.sder) }, 4303 { .name = "SDER", 4304 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, 4305 .access = PL3_RW, .resetvalue = 0, 4306 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, 4307 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 4308 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 4309 .writefn = vbar_write, .resetvalue = 0, 4310 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, 4311 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, 4312 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, 4313 .access = PL3_RW, .resetvalue = 0, 4314 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, 4315 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, 4316 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, 4317 .access = PL3_RW, 4318 /* no .writefn needed as this can't cause an ASID change; 4319 * we must provide a .raw_writefn and .resetfn because we handle 4320 * reset and migration for the AArch32 TTBCR(S), which might be 4321 * using mask and base_mask. 4322 */ 4323 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write, 4324 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, 4325 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, 4326 .type = ARM_CP_ALIAS, 4327 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, 4328 .access = PL3_RW, 4329 .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, 4330 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, 4331 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, 4332 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, 4333 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, 4334 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, 4335 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, 4336 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, 4337 .type = ARM_CP_ALIAS, 4338 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, 4339 .access = PL3_RW, 4340 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) }, 4341 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, 4342 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, 4343 .access = PL3_RW, .writefn = vbar_write, 4344 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), 4345 .resetvalue = 0 }, 4346 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64, 4347 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2, 4348 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0, 4349 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) }, 4350 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64, 4351 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2, 4352 .access = PL3_RW, .resetvalue = 0, 4353 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) }, 4354 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64, 4355 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0, 4356 .access = PL3_RW, .type = ARM_CP_CONST, 4357 .resetvalue = 0 }, 4358 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH, 4359 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0, 4360 .access = PL3_RW, .type = ARM_CP_CONST, 4361 .resetvalue = 0 }, 4362 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH, 4363 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1, 4364 .access = PL3_RW, .type = ARM_CP_CONST, 4365 .resetvalue = 0 }, 4366 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64, 4367 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0, 4368 .access = PL3_W, .type = ARM_CP_NO_RAW, 4369 .writefn = tlbi_aa64_alle3is_write }, 4370 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64, 4371 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1, 4372 .access = PL3_W, .type = ARM_CP_NO_RAW, 4373 .writefn = tlbi_aa64_vae3is_write }, 4374 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64, 4375 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5, 4376 .access = PL3_W, .type = ARM_CP_NO_RAW, 4377 .writefn = tlbi_aa64_vae3is_write }, 4378 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64, 4379 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0, 4380 .access = PL3_W, .type = ARM_CP_NO_RAW, 4381 .writefn = tlbi_aa64_alle3_write }, 4382 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64, 4383 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1, 4384 .access = PL3_W, .type = ARM_CP_NO_RAW, 4385 .writefn = tlbi_aa64_vae3_write }, 4386 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64, 4387 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5, 4388 .access = PL3_W, .type = ARM_CP_NO_RAW, 4389 .writefn = tlbi_aa64_vae3_write }, 4390 REGINFO_SENTINEL 4391 }; 4392 4393 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 4394 bool isread) 4395 { 4396 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64, 4397 * but the AArch32 CTR has its own reginfo struct) 4398 */ 4399 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) { 4400 return CP_ACCESS_TRAP; 4401 } 4402 return CP_ACCESS_OK; 4403 } 4404 4405 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri, 4406 uint64_t value) 4407 { 4408 /* Writes to OSLAR_EL1 may update the OS lock status, which can be 4409 * read via a bit in OSLSR_EL1. 4410 */ 4411 int oslock; 4412 4413 if (ri->state == ARM_CP_STATE_AA32) { 4414 oslock = (value == 0xC5ACCE55); 4415 } else { 4416 oslock = value & 1; 4417 } 4418 4419 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock); 4420 } 4421 4422 static const ARMCPRegInfo debug_cp_reginfo[] = { 4423 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped 4424 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1; 4425 * unlike DBGDRAR it is never accessible from EL0. 4426 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64 4427 * accessor. 4428 */ 4429 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, 4430 .access = PL0_R, .accessfn = access_tdra, 4431 .type = ARM_CP_CONST, .resetvalue = 0 }, 4432 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64, 4433 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 4434 .access = PL1_R, .accessfn = access_tdra, 4435 .type = ARM_CP_CONST, .resetvalue = 0 }, 4436 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 4437 .access = PL0_R, .accessfn = access_tdra, 4438 .type = ARM_CP_CONST, .resetvalue = 0 }, 4439 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */ 4440 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH, 4441 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 4442 .access = PL1_RW, .accessfn = access_tda, 4443 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), 4444 .resetvalue = 0 }, 4445 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1. 4446 * We don't implement the configurable EL0 access. 4447 */ 4448 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH, 4449 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 4450 .type = ARM_CP_ALIAS, 4451 .access = PL1_R, .accessfn = access_tda, 4452 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), }, 4453 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH, 4454 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4, 4455 .access = PL1_W, .type = ARM_CP_NO_RAW, 4456 .accessfn = access_tdosa, 4457 .writefn = oslar_write }, 4458 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH, 4459 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4, 4460 .access = PL1_R, .resetvalue = 10, 4461 .accessfn = access_tdosa, 4462 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) }, 4463 /* Dummy OSDLR_EL1: 32-bit Linux will read this */ 4464 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH, 4465 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4, 4466 .access = PL1_RW, .accessfn = access_tdosa, 4467 .type = ARM_CP_NOP }, 4468 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't 4469 * implement vector catch debug events yet. 4470 */ 4471 { .name = "DBGVCR", 4472 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 4473 .access = PL1_RW, .accessfn = access_tda, 4474 .type = ARM_CP_NOP }, 4475 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor 4476 * to save and restore a 32-bit guest's DBGVCR) 4477 */ 4478 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64, 4479 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0, 4480 .access = PL2_RW, .accessfn = access_tda, 4481 .type = ARM_CP_NOP }, 4482 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications 4483 * Channel but Linux may try to access this register. The 32-bit 4484 * alias is DBGDCCINT. 4485 */ 4486 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH, 4487 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 4488 .access = PL1_RW, .accessfn = access_tda, 4489 .type = ARM_CP_NOP }, 4490 REGINFO_SENTINEL 4491 }; 4492 4493 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = { 4494 /* 64 bit access versions of the (dummy) debug registers */ 4495 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0, 4496 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 4497 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0, 4498 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 4499 REGINFO_SENTINEL 4500 }; 4501 4502 /* Return the exception level to which exceptions should be taken 4503 * via SVEAccessTrap. If an exception should be routed through 4504 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should 4505 * take care of raising that exception. 4506 * C.f. the ARM pseudocode function CheckSVEEnabled. 4507 */ 4508 int sve_exception_el(CPUARMState *env, int el) 4509 { 4510 #ifndef CONFIG_USER_ONLY 4511 if (el <= 1) { 4512 bool disabled = false; 4513 4514 /* The CPACR.ZEN controls traps to EL1: 4515 * 0, 2 : trap EL0 and EL1 accesses 4516 * 1 : trap only EL0 accesses 4517 * 3 : trap no accesses 4518 */ 4519 if (!extract32(env->cp15.cpacr_el1, 16, 1)) { 4520 disabled = true; 4521 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) { 4522 disabled = el == 0; 4523 } 4524 if (disabled) { 4525 /* route_to_el2 */ 4526 return (arm_feature(env, ARM_FEATURE_EL2) 4527 && !arm_is_secure(env) 4528 && (env->cp15.hcr_el2 & HCR_TGE) ? 2 : 1); 4529 } 4530 4531 /* Check CPACR.FPEN. */ 4532 if (!extract32(env->cp15.cpacr_el1, 20, 1)) { 4533 disabled = true; 4534 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) { 4535 disabled = el == 0; 4536 } 4537 if (disabled) { 4538 return 0; 4539 } 4540 } 4541 4542 /* CPTR_EL2. Since TZ and TFP are positive, 4543 * they will be zero when EL2 is not present. 4544 */ 4545 if (el <= 2 && !arm_is_secure_below_el3(env)) { 4546 if (env->cp15.cptr_el[2] & CPTR_TZ) { 4547 return 2; 4548 } 4549 if (env->cp15.cptr_el[2] & CPTR_TFP) { 4550 return 0; 4551 } 4552 } 4553 4554 /* CPTR_EL3. Since EZ is negative we must check for EL3. */ 4555 if (arm_feature(env, ARM_FEATURE_EL3) 4556 && !(env->cp15.cptr_el[3] & CPTR_EZ)) { 4557 return 3; 4558 } 4559 #endif 4560 return 0; 4561 } 4562 4563 /* 4564 * Given that SVE is enabled, return the vector length for EL. 4565 */ 4566 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el) 4567 { 4568 ARMCPU *cpu = arm_env_get_cpu(env); 4569 uint32_t zcr_len = cpu->sve_max_vq - 1; 4570 4571 if (el <= 1) { 4572 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]); 4573 } 4574 if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) { 4575 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]); 4576 } 4577 if (el < 3 && arm_feature(env, ARM_FEATURE_EL3)) { 4578 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]); 4579 } 4580 return zcr_len; 4581 } 4582 4583 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4584 uint64_t value) 4585 { 4586 int cur_el = arm_current_el(env); 4587 int old_len = sve_zcr_len_for_el(env, cur_el); 4588 int new_len; 4589 4590 /* Bits other than [3:0] are RAZ/WI. */ 4591 raw_write(env, ri, value & 0xf); 4592 4593 /* 4594 * Because we arrived here, we know both FP and SVE are enabled; 4595 * otherwise we would have trapped access to the ZCR_ELn register. 4596 */ 4597 new_len = sve_zcr_len_for_el(env, cur_el); 4598 if (new_len < old_len) { 4599 aarch64_sve_narrow_vq(env, new_len + 1); 4600 } 4601 } 4602 4603 static const ARMCPRegInfo zcr_el1_reginfo = { 4604 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64, 4605 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0, 4606 .access = PL1_RW, .type = ARM_CP_SVE, 4607 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]), 4608 .writefn = zcr_write, .raw_writefn = raw_write 4609 }; 4610 4611 static const ARMCPRegInfo zcr_el2_reginfo = { 4612 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 4613 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 4614 .access = PL2_RW, .type = ARM_CP_SVE, 4615 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]), 4616 .writefn = zcr_write, .raw_writefn = raw_write 4617 }; 4618 4619 static const ARMCPRegInfo zcr_no_el2_reginfo = { 4620 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 4621 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 4622 .access = PL2_RW, .type = ARM_CP_SVE, 4623 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore 4624 }; 4625 4626 static const ARMCPRegInfo zcr_el3_reginfo = { 4627 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64, 4628 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0, 4629 .access = PL3_RW, .type = ARM_CP_SVE, 4630 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]), 4631 .writefn = zcr_write, .raw_writefn = raw_write 4632 }; 4633 4634 void hw_watchpoint_update(ARMCPU *cpu, int n) 4635 { 4636 CPUARMState *env = &cpu->env; 4637 vaddr len = 0; 4638 vaddr wvr = env->cp15.dbgwvr[n]; 4639 uint64_t wcr = env->cp15.dbgwcr[n]; 4640 int mask; 4641 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 4642 4643 if (env->cpu_watchpoint[n]) { 4644 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]); 4645 env->cpu_watchpoint[n] = NULL; 4646 } 4647 4648 if (!extract64(wcr, 0, 1)) { 4649 /* E bit clear : watchpoint disabled */ 4650 return; 4651 } 4652 4653 switch (extract64(wcr, 3, 2)) { 4654 case 0: 4655 /* LSC 00 is reserved and must behave as if the wp is disabled */ 4656 return; 4657 case 1: 4658 flags |= BP_MEM_READ; 4659 break; 4660 case 2: 4661 flags |= BP_MEM_WRITE; 4662 break; 4663 case 3: 4664 flags |= BP_MEM_ACCESS; 4665 break; 4666 } 4667 4668 /* Attempts to use both MASK and BAS fields simultaneously are 4669 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case, 4670 * thus generating a watchpoint for every byte in the masked region. 4671 */ 4672 mask = extract64(wcr, 24, 4); 4673 if (mask == 1 || mask == 2) { 4674 /* Reserved values of MASK; we must act as if the mask value was 4675 * some non-reserved value, or as if the watchpoint were disabled. 4676 * We choose the latter. 4677 */ 4678 return; 4679 } else if (mask) { 4680 /* Watchpoint covers an aligned area up to 2GB in size */ 4681 len = 1ULL << mask; 4682 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE 4683 * whether the watchpoint fires when the unmasked bits match; we opt 4684 * to generate the exceptions. 4685 */ 4686 wvr &= ~(len - 1); 4687 } else { 4688 /* Watchpoint covers bytes defined by the byte address select bits */ 4689 int bas = extract64(wcr, 5, 8); 4690 int basstart; 4691 4692 if (bas == 0) { 4693 /* This must act as if the watchpoint is disabled */ 4694 return; 4695 } 4696 4697 if (extract64(wvr, 2, 1)) { 4698 /* Deprecated case of an only 4-aligned address. BAS[7:4] are 4699 * ignored, and BAS[3:0] define which bytes to watch. 4700 */ 4701 bas &= 0xf; 4702 } 4703 /* The BAS bits are supposed to be programmed to indicate a contiguous 4704 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether 4705 * we fire for each byte in the word/doubleword addressed by the WVR. 4706 * We choose to ignore any non-zero bits after the first range of 1s. 4707 */ 4708 basstart = ctz32(bas); 4709 len = cto32(bas >> basstart); 4710 wvr += basstart; 4711 } 4712 4713 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags, 4714 &env->cpu_watchpoint[n]); 4715 } 4716 4717 void hw_watchpoint_update_all(ARMCPU *cpu) 4718 { 4719 int i; 4720 CPUARMState *env = &cpu->env; 4721 4722 /* Completely clear out existing QEMU watchpoints and our array, to 4723 * avoid possible stale entries following migration load. 4724 */ 4725 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU); 4726 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint)); 4727 4728 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) { 4729 hw_watchpoint_update(cpu, i); 4730 } 4731 } 4732 4733 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4734 uint64_t value) 4735 { 4736 ARMCPU *cpu = arm_env_get_cpu(env); 4737 int i = ri->crm; 4738 4739 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the 4740 * register reads and behaves as if values written are sign extended. 4741 * Bits [1:0] are RES0. 4742 */ 4743 value = sextract64(value, 0, 49) & ~3ULL; 4744 4745 raw_write(env, ri, value); 4746 hw_watchpoint_update(cpu, i); 4747 } 4748 4749 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4750 uint64_t value) 4751 { 4752 ARMCPU *cpu = arm_env_get_cpu(env); 4753 int i = ri->crm; 4754 4755 raw_write(env, ri, value); 4756 hw_watchpoint_update(cpu, i); 4757 } 4758 4759 void hw_breakpoint_update(ARMCPU *cpu, int n) 4760 { 4761 CPUARMState *env = &cpu->env; 4762 uint64_t bvr = env->cp15.dbgbvr[n]; 4763 uint64_t bcr = env->cp15.dbgbcr[n]; 4764 vaddr addr; 4765 int bt; 4766 int flags = BP_CPU; 4767 4768 if (env->cpu_breakpoint[n]) { 4769 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]); 4770 env->cpu_breakpoint[n] = NULL; 4771 } 4772 4773 if (!extract64(bcr, 0, 1)) { 4774 /* E bit clear : watchpoint disabled */ 4775 return; 4776 } 4777 4778 bt = extract64(bcr, 20, 4); 4779 4780 switch (bt) { 4781 case 4: /* unlinked address mismatch (reserved if AArch64) */ 4782 case 5: /* linked address mismatch (reserved if AArch64) */ 4783 qemu_log_mask(LOG_UNIMP, 4784 "arm: address mismatch breakpoint types not implemented\n"); 4785 return; 4786 case 0: /* unlinked address match */ 4787 case 1: /* linked address match */ 4788 { 4789 /* Bits [63:49] are hardwired to the value of bit [48]; that is, 4790 * we behave as if the register was sign extended. Bits [1:0] are 4791 * RES0. The BAS field is used to allow setting breakpoints on 16 4792 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether 4793 * a bp will fire if the addresses covered by the bp and the addresses 4794 * covered by the insn overlap but the insn doesn't start at the 4795 * start of the bp address range. We choose to require the insn and 4796 * the bp to have the same address. The constraints on writing to 4797 * BAS enforced in dbgbcr_write mean we have only four cases: 4798 * 0b0000 => no breakpoint 4799 * 0b0011 => breakpoint on addr 4800 * 0b1100 => breakpoint on addr + 2 4801 * 0b1111 => breakpoint on addr 4802 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c). 4803 */ 4804 int bas = extract64(bcr, 5, 4); 4805 addr = sextract64(bvr, 0, 49) & ~3ULL; 4806 if (bas == 0) { 4807 return; 4808 } 4809 if (bas == 0xc) { 4810 addr += 2; 4811 } 4812 break; 4813 } 4814 case 2: /* unlinked context ID match */ 4815 case 8: /* unlinked VMID match (reserved if no EL2) */ 4816 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */ 4817 qemu_log_mask(LOG_UNIMP, 4818 "arm: unlinked context breakpoint types not implemented\n"); 4819 return; 4820 case 9: /* linked VMID match (reserved if no EL2) */ 4821 case 11: /* linked context ID and VMID match (reserved if no EL2) */ 4822 case 3: /* linked context ID match */ 4823 default: 4824 /* We must generate no events for Linked context matches (unless 4825 * they are linked to by some other bp/wp, which is handled in 4826 * updates for the linking bp/wp). We choose to also generate no events 4827 * for reserved values. 4828 */ 4829 return; 4830 } 4831 4832 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]); 4833 } 4834 4835 void hw_breakpoint_update_all(ARMCPU *cpu) 4836 { 4837 int i; 4838 CPUARMState *env = &cpu->env; 4839 4840 /* Completely clear out existing QEMU breakpoints and our array, to 4841 * avoid possible stale entries following migration load. 4842 */ 4843 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU); 4844 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint)); 4845 4846 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) { 4847 hw_breakpoint_update(cpu, i); 4848 } 4849 } 4850 4851 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4852 uint64_t value) 4853 { 4854 ARMCPU *cpu = arm_env_get_cpu(env); 4855 int i = ri->crm; 4856 4857 raw_write(env, ri, value); 4858 hw_breakpoint_update(cpu, i); 4859 } 4860 4861 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4862 uint64_t value) 4863 { 4864 ARMCPU *cpu = arm_env_get_cpu(env); 4865 int i = ri->crm; 4866 4867 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only 4868 * copy of BAS[0]. 4869 */ 4870 value = deposit64(value, 6, 1, extract64(value, 5, 1)); 4871 value = deposit64(value, 8, 1, extract64(value, 7, 1)); 4872 4873 raw_write(env, ri, value); 4874 hw_breakpoint_update(cpu, i); 4875 } 4876 4877 static void define_debug_regs(ARMCPU *cpu) 4878 { 4879 /* Define v7 and v8 architectural debug registers. 4880 * These are just dummy implementations for now. 4881 */ 4882 int i; 4883 int wrps, brps, ctx_cmps; 4884 ARMCPRegInfo dbgdidr = { 4885 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 4886 .access = PL0_R, .accessfn = access_tda, 4887 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr, 4888 }; 4889 4890 /* Note that all these register fields hold "number of Xs minus 1". */ 4891 brps = extract32(cpu->dbgdidr, 24, 4); 4892 wrps = extract32(cpu->dbgdidr, 28, 4); 4893 ctx_cmps = extract32(cpu->dbgdidr, 20, 4); 4894 4895 assert(ctx_cmps <= brps); 4896 4897 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties 4898 * of the debug registers such as number of breakpoints; 4899 * check that if they both exist then they agree. 4900 */ 4901 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 4902 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps); 4903 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps); 4904 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps); 4905 } 4906 4907 define_one_arm_cp_reg(cpu, &dbgdidr); 4908 define_arm_cp_regs(cpu, debug_cp_reginfo); 4909 4910 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) { 4911 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo); 4912 } 4913 4914 for (i = 0; i < brps + 1; i++) { 4915 ARMCPRegInfo dbgregs[] = { 4916 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH, 4917 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4, 4918 .access = PL1_RW, .accessfn = access_tda, 4919 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]), 4920 .writefn = dbgbvr_write, .raw_writefn = raw_write 4921 }, 4922 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH, 4923 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5, 4924 .access = PL1_RW, .accessfn = access_tda, 4925 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]), 4926 .writefn = dbgbcr_write, .raw_writefn = raw_write 4927 }, 4928 REGINFO_SENTINEL 4929 }; 4930 define_arm_cp_regs(cpu, dbgregs); 4931 } 4932 4933 for (i = 0; i < wrps + 1; i++) { 4934 ARMCPRegInfo dbgregs[] = { 4935 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH, 4936 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6, 4937 .access = PL1_RW, .accessfn = access_tda, 4938 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]), 4939 .writefn = dbgwvr_write, .raw_writefn = raw_write 4940 }, 4941 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH, 4942 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7, 4943 .access = PL1_RW, .accessfn = access_tda, 4944 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]), 4945 .writefn = dbgwcr_write, .raw_writefn = raw_write 4946 }, 4947 REGINFO_SENTINEL 4948 }; 4949 define_arm_cp_regs(cpu, dbgregs); 4950 } 4951 } 4952 4953 /* We don't know until after realize whether there's a GICv3 4954 * attached, and that is what registers the gicv3 sysregs. 4955 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1 4956 * at runtime. 4957 */ 4958 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri) 4959 { 4960 ARMCPU *cpu = arm_env_get_cpu(env); 4961 uint64_t pfr1 = cpu->id_pfr1; 4962 4963 if (env->gicv3state) { 4964 pfr1 |= 1 << 28; 4965 } 4966 return pfr1; 4967 } 4968 4969 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri) 4970 { 4971 ARMCPU *cpu = arm_env_get_cpu(env); 4972 uint64_t pfr0 = cpu->isar.id_aa64pfr0; 4973 4974 if (env->gicv3state) { 4975 pfr0 |= 1 << 24; 4976 } 4977 return pfr0; 4978 } 4979 4980 void register_cp_regs_for_features(ARMCPU *cpu) 4981 { 4982 /* Register all the coprocessor registers based on feature bits */ 4983 CPUARMState *env = &cpu->env; 4984 if (arm_feature(env, ARM_FEATURE_M)) { 4985 /* M profile has no coprocessor registers */ 4986 return; 4987 } 4988 4989 define_arm_cp_regs(cpu, cp_reginfo); 4990 if (!arm_feature(env, ARM_FEATURE_V8)) { 4991 /* Must go early as it is full of wildcards that may be 4992 * overridden by later definitions. 4993 */ 4994 define_arm_cp_regs(cpu, not_v8_cp_reginfo); 4995 } 4996 4997 if (arm_feature(env, ARM_FEATURE_V6)) { 4998 /* The ID registers all have impdef reset values */ 4999 ARMCPRegInfo v6_idregs[] = { 5000 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, 5001 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 5002 .access = PL1_R, .type = ARM_CP_CONST, 5003 .resetvalue = cpu->id_pfr0 }, 5004 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know 5005 * the value of the GIC field until after we define these regs. 5006 */ 5007 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, 5008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, 5009 .access = PL1_R, .type = ARM_CP_NO_RAW, 5010 .readfn = id_pfr1_read, 5011 .writefn = arm_cp_write_ignore }, 5012 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, 5013 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, 5014 .access = PL1_R, .type = ARM_CP_CONST, 5015 .resetvalue = cpu->id_dfr0 }, 5016 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, 5017 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, 5018 .access = PL1_R, .type = ARM_CP_CONST, 5019 .resetvalue = cpu->id_afr0 }, 5020 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, 5021 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, 5022 .access = PL1_R, .type = ARM_CP_CONST, 5023 .resetvalue = cpu->id_mmfr0 }, 5024 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, 5025 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, 5026 .access = PL1_R, .type = ARM_CP_CONST, 5027 .resetvalue = cpu->id_mmfr1 }, 5028 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, 5029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, 5030 .access = PL1_R, .type = ARM_CP_CONST, 5031 .resetvalue = cpu->id_mmfr2 }, 5032 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, 5033 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, 5034 .access = PL1_R, .type = ARM_CP_CONST, 5035 .resetvalue = cpu->id_mmfr3 }, 5036 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, 5037 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 5038 .access = PL1_R, .type = ARM_CP_CONST, 5039 .resetvalue = cpu->isar.id_isar0 }, 5040 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, 5041 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, 5042 .access = PL1_R, .type = ARM_CP_CONST, 5043 .resetvalue = cpu->isar.id_isar1 }, 5044 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, 5045 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 5046 .access = PL1_R, .type = ARM_CP_CONST, 5047 .resetvalue = cpu->isar.id_isar2 }, 5048 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, 5049 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, 5050 .access = PL1_R, .type = ARM_CP_CONST, 5051 .resetvalue = cpu->isar.id_isar3 }, 5052 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, 5053 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, 5054 .access = PL1_R, .type = ARM_CP_CONST, 5055 .resetvalue = cpu->isar.id_isar4 }, 5056 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, 5057 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, 5058 .access = PL1_R, .type = ARM_CP_CONST, 5059 .resetvalue = cpu->isar.id_isar5 }, 5060 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH, 5061 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6, 5062 .access = PL1_R, .type = ARM_CP_CONST, 5063 .resetvalue = cpu->id_mmfr4 }, 5064 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH, 5065 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7, 5066 .access = PL1_R, .type = ARM_CP_CONST, 5067 .resetvalue = cpu->isar.id_isar6 }, 5068 REGINFO_SENTINEL 5069 }; 5070 define_arm_cp_regs(cpu, v6_idregs); 5071 define_arm_cp_regs(cpu, v6_cp_reginfo); 5072 } else { 5073 define_arm_cp_regs(cpu, not_v6_cp_reginfo); 5074 } 5075 if (arm_feature(env, ARM_FEATURE_V6K)) { 5076 define_arm_cp_regs(cpu, v6k_cp_reginfo); 5077 } 5078 if (arm_feature(env, ARM_FEATURE_V7MP) && 5079 !arm_feature(env, ARM_FEATURE_PMSA)) { 5080 define_arm_cp_regs(cpu, v7mp_cp_reginfo); 5081 } 5082 if (arm_feature(env, ARM_FEATURE_V7)) { 5083 /* v7 performance monitor control register: same implementor 5084 * field as main ID register, and we implement only the cycle 5085 * count register. 5086 */ 5087 #ifndef CONFIG_USER_ONLY 5088 ARMCPRegInfo pmcr = { 5089 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, 5090 .access = PL0_RW, 5091 .type = ARM_CP_IO | ARM_CP_ALIAS, 5092 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), 5093 .accessfn = pmreg_access, .writefn = pmcr_write, 5094 .raw_writefn = raw_write, 5095 }; 5096 ARMCPRegInfo pmcr64 = { 5097 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, 5098 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, 5099 .access = PL0_RW, .accessfn = pmreg_access, 5100 .type = ARM_CP_IO, 5101 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), 5102 .resetvalue = cpu->midr & 0xff000000, 5103 .writefn = pmcr_write, .raw_writefn = raw_write, 5104 }; 5105 define_one_arm_cp_reg(cpu, &pmcr); 5106 define_one_arm_cp_reg(cpu, &pmcr64); 5107 #endif 5108 ARMCPRegInfo clidr = { 5109 .name = "CLIDR", .state = ARM_CP_STATE_BOTH, 5110 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, 5111 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr 5112 }; 5113 define_one_arm_cp_reg(cpu, &clidr); 5114 define_arm_cp_regs(cpu, v7_cp_reginfo); 5115 define_debug_regs(cpu); 5116 } else { 5117 define_arm_cp_regs(cpu, not_v7_cp_reginfo); 5118 } 5119 if (arm_feature(env, ARM_FEATURE_V8)) { 5120 /* AArch64 ID registers, which all have impdef reset values. 5121 * Note that within the ID register ranges the unused slots 5122 * must all RAZ, not UNDEF; future architecture versions may 5123 * define new registers here. 5124 */ 5125 ARMCPRegInfo v8_idregs[] = { 5126 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't 5127 * know the right value for the GIC field until after we 5128 * define these regs. 5129 */ 5130 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, 5131 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, 5132 .access = PL1_R, .type = ARM_CP_NO_RAW, 5133 .readfn = id_aa64pfr0_read, 5134 .writefn = arm_cp_write_ignore }, 5135 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, 5136 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, 5137 .access = PL1_R, .type = ARM_CP_CONST, 5138 .resetvalue = cpu->isar.id_aa64pfr1}, 5139 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5140 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2, 5141 .access = PL1_R, .type = ARM_CP_CONST, 5142 .resetvalue = 0 }, 5143 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5144 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3, 5145 .access = PL1_R, .type = ARM_CP_CONST, 5146 .resetvalue = 0 }, 5147 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64, 5148 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4, 5149 .access = PL1_R, .type = ARM_CP_CONST, 5150 /* At present, only SVEver == 0 is defined anyway. */ 5151 .resetvalue = 0 }, 5152 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5153 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5, 5154 .access = PL1_R, .type = ARM_CP_CONST, 5155 .resetvalue = 0 }, 5156 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5157 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6, 5158 .access = PL1_R, .type = ARM_CP_CONST, 5159 .resetvalue = 0 }, 5160 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5161 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7, 5162 .access = PL1_R, .type = ARM_CP_CONST, 5163 .resetvalue = 0 }, 5164 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, 5165 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, 5166 .access = PL1_R, .type = ARM_CP_CONST, 5167 .resetvalue = cpu->id_aa64dfr0 }, 5168 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, 5169 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, 5170 .access = PL1_R, .type = ARM_CP_CONST, 5171 .resetvalue = cpu->id_aa64dfr1 }, 5172 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5173 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2, 5174 .access = PL1_R, .type = ARM_CP_CONST, 5175 .resetvalue = 0 }, 5176 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5177 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3, 5178 .access = PL1_R, .type = ARM_CP_CONST, 5179 .resetvalue = 0 }, 5180 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, 5181 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, 5182 .access = PL1_R, .type = ARM_CP_CONST, 5183 .resetvalue = cpu->id_aa64afr0 }, 5184 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, 5185 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, 5186 .access = PL1_R, .type = ARM_CP_CONST, 5187 .resetvalue = cpu->id_aa64afr1 }, 5188 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5189 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6, 5190 .access = PL1_R, .type = ARM_CP_CONST, 5191 .resetvalue = 0 }, 5192 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5193 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7, 5194 .access = PL1_R, .type = ARM_CP_CONST, 5195 .resetvalue = 0 }, 5196 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, 5197 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, 5198 .access = PL1_R, .type = ARM_CP_CONST, 5199 .resetvalue = cpu->isar.id_aa64isar0 }, 5200 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, 5201 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, 5202 .access = PL1_R, .type = ARM_CP_CONST, 5203 .resetvalue = cpu->isar.id_aa64isar1 }, 5204 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5205 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2, 5206 .access = PL1_R, .type = ARM_CP_CONST, 5207 .resetvalue = 0 }, 5208 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5209 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3, 5210 .access = PL1_R, .type = ARM_CP_CONST, 5211 .resetvalue = 0 }, 5212 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5213 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4, 5214 .access = PL1_R, .type = ARM_CP_CONST, 5215 .resetvalue = 0 }, 5216 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5217 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5, 5218 .access = PL1_R, .type = ARM_CP_CONST, 5219 .resetvalue = 0 }, 5220 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5221 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6, 5222 .access = PL1_R, .type = ARM_CP_CONST, 5223 .resetvalue = 0 }, 5224 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5225 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7, 5226 .access = PL1_R, .type = ARM_CP_CONST, 5227 .resetvalue = 0 }, 5228 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, 5229 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 5230 .access = PL1_R, .type = ARM_CP_CONST, 5231 .resetvalue = cpu->id_aa64mmfr0 }, 5232 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, 5233 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, 5234 .access = PL1_R, .type = ARM_CP_CONST, 5235 .resetvalue = cpu->id_aa64mmfr1 }, 5236 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5237 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2, 5238 .access = PL1_R, .type = ARM_CP_CONST, 5239 .resetvalue = 0 }, 5240 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5241 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3, 5242 .access = PL1_R, .type = ARM_CP_CONST, 5243 .resetvalue = 0 }, 5244 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5245 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4, 5246 .access = PL1_R, .type = ARM_CP_CONST, 5247 .resetvalue = 0 }, 5248 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5249 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5, 5250 .access = PL1_R, .type = ARM_CP_CONST, 5251 .resetvalue = 0 }, 5252 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5253 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6, 5254 .access = PL1_R, .type = ARM_CP_CONST, 5255 .resetvalue = 0 }, 5256 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5257 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7, 5258 .access = PL1_R, .type = ARM_CP_CONST, 5259 .resetvalue = 0 }, 5260 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, 5261 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 5262 .access = PL1_R, .type = ARM_CP_CONST, 5263 .resetvalue = cpu->isar.mvfr0 }, 5264 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, 5265 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 5266 .access = PL1_R, .type = ARM_CP_CONST, 5267 .resetvalue = cpu->isar.mvfr1 }, 5268 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, 5269 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 5270 .access = PL1_R, .type = ARM_CP_CONST, 5271 .resetvalue = cpu->isar.mvfr2 }, 5272 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5273 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3, 5274 .access = PL1_R, .type = ARM_CP_CONST, 5275 .resetvalue = 0 }, 5276 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5277 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4, 5278 .access = PL1_R, .type = ARM_CP_CONST, 5279 .resetvalue = 0 }, 5280 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5281 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5, 5282 .access = PL1_R, .type = ARM_CP_CONST, 5283 .resetvalue = 0 }, 5284 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5285 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6, 5286 .access = PL1_R, .type = ARM_CP_CONST, 5287 .resetvalue = 0 }, 5288 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 5289 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7, 5290 .access = PL1_R, .type = ARM_CP_CONST, 5291 .resetvalue = 0 }, 5292 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32, 5293 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6, 5294 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5295 .resetvalue = cpu->pmceid0 }, 5296 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64, 5297 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6, 5298 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5299 .resetvalue = cpu->pmceid0 }, 5300 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32, 5301 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7, 5302 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5303 .resetvalue = cpu->pmceid1 }, 5304 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64, 5305 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7, 5306 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5307 .resetvalue = cpu->pmceid1 }, 5308 REGINFO_SENTINEL 5309 }; 5310 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */ 5311 if (!arm_feature(env, ARM_FEATURE_EL3) && 5312 !arm_feature(env, ARM_FEATURE_EL2)) { 5313 ARMCPRegInfo rvbar = { 5314 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64, 5315 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 5316 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar 5317 }; 5318 define_one_arm_cp_reg(cpu, &rvbar); 5319 } 5320 define_arm_cp_regs(cpu, v8_idregs); 5321 define_arm_cp_regs(cpu, v8_cp_reginfo); 5322 } 5323 if (arm_feature(env, ARM_FEATURE_EL2)) { 5324 uint64_t vmpidr_def = mpidr_read_val(env); 5325 ARMCPRegInfo vpidr_regs[] = { 5326 { .name = "VPIDR", .state = ARM_CP_STATE_AA32, 5327 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 5328 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5329 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS, 5330 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) }, 5331 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64, 5332 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 5333 .access = PL2_RW, .resetvalue = cpu->midr, 5334 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 5335 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32, 5336 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 5337 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5338 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS, 5339 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) }, 5340 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64, 5341 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 5342 .access = PL2_RW, 5343 .resetvalue = vmpidr_def, 5344 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, 5345 REGINFO_SENTINEL 5346 }; 5347 define_arm_cp_regs(cpu, vpidr_regs); 5348 define_arm_cp_regs(cpu, el2_cp_reginfo); 5349 if (arm_feature(env, ARM_FEATURE_V8)) { 5350 define_arm_cp_regs(cpu, el2_v8_cp_reginfo); 5351 } 5352 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */ 5353 if (!arm_feature(env, ARM_FEATURE_EL3)) { 5354 ARMCPRegInfo rvbar = { 5355 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64, 5356 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1, 5357 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar 5358 }; 5359 define_one_arm_cp_reg(cpu, &rvbar); 5360 } 5361 } else { 5362 /* If EL2 is missing but higher ELs are enabled, we need to 5363 * register the no_el2 reginfos. 5364 */ 5365 if (arm_feature(env, ARM_FEATURE_EL3)) { 5366 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value 5367 * of MIDR_EL1 and MPIDR_EL1. 5368 */ 5369 ARMCPRegInfo vpidr_regs[] = { 5370 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH, 5371 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 5372 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 5373 .type = ARM_CP_CONST, .resetvalue = cpu->midr, 5374 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 5375 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH, 5376 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 5377 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 5378 .type = ARM_CP_NO_RAW, 5379 .writefn = arm_cp_write_ignore, .readfn = mpidr_read }, 5380 REGINFO_SENTINEL 5381 }; 5382 define_arm_cp_regs(cpu, vpidr_regs); 5383 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo); 5384 if (arm_feature(env, ARM_FEATURE_V8)) { 5385 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo); 5386 } 5387 } 5388 } 5389 if (arm_feature(env, ARM_FEATURE_EL3)) { 5390 define_arm_cp_regs(cpu, el3_cp_reginfo); 5391 ARMCPRegInfo el3_regs[] = { 5392 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64, 5393 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1, 5394 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar }, 5395 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, 5396 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, 5397 .access = PL3_RW, 5398 .raw_writefn = raw_write, .writefn = sctlr_write, 5399 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]), 5400 .resetvalue = cpu->reset_sctlr }, 5401 REGINFO_SENTINEL 5402 }; 5403 5404 define_arm_cp_regs(cpu, el3_regs); 5405 } 5406 /* The behaviour of NSACR is sufficiently various that we don't 5407 * try to describe it in a single reginfo: 5408 * if EL3 is 64 bit, then trap to EL3 from S EL1, 5409 * reads as constant 0xc00 from NS EL1 and NS EL2 5410 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2 5411 * if v7 without EL3, register doesn't exist 5412 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2 5413 */ 5414 if (arm_feature(env, ARM_FEATURE_EL3)) { 5415 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5416 ARMCPRegInfo nsacr = { 5417 .name = "NSACR", .type = ARM_CP_CONST, 5418 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 5419 .access = PL1_RW, .accessfn = nsacr_access, 5420 .resetvalue = 0xc00 5421 }; 5422 define_one_arm_cp_reg(cpu, &nsacr); 5423 } else { 5424 ARMCPRegInfo nsacr = { 5425 .name = "NSACR", 5426 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 5427 .access = PL3_RW | PL1_R, 5428 .resetvalue = 0, 5429 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) 5430 }; 5431 define_one_arm_cp_reg(cpu, &nsacr); 5432 } 5433 } else { 5434 if (arm_feature(env, ARM_FEATURE_V8)) { 5435 ARMCPRegInfo nsacr = { 5436 .name = "NSACR", .type = ARM_CP_CONST, 5437 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 5438 .access = PL1_R, 5439 .resetvalue = 0xc00 5440 }; 5441 define_one_arm_cp_reg(cpu, &nsacr); 5442 } 5443 } 5444 5445 if (arm_feature(env, ARM_FEATURE_PMSA)) { 5446 if (arm_feature(env, ARM_FEATURE_V6)) { 5447 /* PMSAv6 not implemented */ 5448 assert(arm_feature(env, ARM_FEATURE_V7)); 5449 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 5450 define_arm_cp_regs(cpu, pmsav7_cp_reginfo); 5451 } else { 5452 define_arm_cp_regs(cpu, pmsav5_cp_reginfo); 5453 } 5454 } else { 5455 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 5456 define_arm_cp_regs(cpu, vmsa_cp_reginfo); 5457 } 5458 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { 5459 define_arm_cp_regs(cpu, t2ee_cp_reginfo); 5460 } 5461 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 5462 define_arm_cp_regs(cpu, generic_timer_cp_reginfo); 5463 } 5464 if (arm_feature(env, ARM_FEATURE_VAPA)) { 5465 define_arm_cp_regs(cpu, vapa_cp_reginfo); 5466 } 5467 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { 5468 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); 5469 } 5470 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { 5471 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); 5472 } 5473 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { 5474 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); 5475 } 5476 if (arm_feature(env, ARM_FEATURE_OMAPCP)) { 5477 define_arm_cp_regs(cpu, omap_cp_reginfo); 5478 } 5479 if (arm_feature(env, ARM_FEATURE_STRONGARM)) { 5480 define_arm_cp_regs(cpu, strongarm_cp_reginfo); 5481 } 5482 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 5483 define_arm_cp_regs(cpu, xscale_cp_reginfo); 5484 } 5485 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { 5486 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); 5487 } 5488 if (arm_feature(env, ARM_FEATURE_LPAE)) { 5489 define_arm_cp_regs(cpu, lpae_cp_reginfo); 5490 } 5491 /* Slightly awkwardly, the OMAP and StrongARM cores need all of 5492 * cp15 crn=0 to be writes-ignored, whereas for other cores they should 5493 * be read-only (ie write causes UNDEF exception). 5494 */ 5495 { 5496 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { 5497 /* Pre-v8 MIDR space. 5498 * Note that the MIDR isn't a simple constant register because 5499 * of the TI925 behaviour where writes to another register can 5500 * cause the MIDR value to change. 5501 * 5502 * Unimplemented registers in the c15 0 0 0 space default to 5503 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR 5504 * and friends override accordingly. 5505 */ 5506 { .name = "MIDR", 5507 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, 5508 .access = PL1_R, .resetvalue = cpu->midr, 5509 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, 5510 .readfn = midr_read, 5511 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 5512 .type = ARM_CP_OVERRIDE }, 5513 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ 5514 { .name = "DUMMY", 5515 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, 5516 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5517 { .name = "DUMMY", 5518 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, 5519 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5520 { .name = "DUMMY", 5521 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, 5522 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5523 { .name = "DUMMY", 5524 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, 5525 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5526 { .name = "DUMMY", 5527 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, 5528 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5529 REGINFO_SENTINEL 5530 }; 5531 ARMCPRegInfo id_v8_midr_cp_reginfo[] = { 5532 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, 5533 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, 5534 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr, 5535 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 5536 .readfn = midr_read }, 5537 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */ 5538 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 5539 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 5540 .access = PL1_R, .resetvalue = cpu->midr }, 5541 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 5542 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7, 5543 .access = PL1_R, .resetvalue = cpu->midr }, 5544 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, 5545 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, 5546 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr }, 5547 REGINFO_SENTINEL 5548 }; 5549 ARMCPRegInfo id_cp_reginfo[] = { 5550 /* These are common to v8 and pre-v8 */ 5551 { .name = "CTR", 5552 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, 5553 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 5554 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, 5555 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, 5556 .access = PL0_R, .accessfn = ctr_el0_access, 5557 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 5558 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ 5559 { .name = "TCMTR", 5560 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, 5561 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 5562 REGINFO_SENTINEL 5563 }; 5564 /* TLBTR is specific to VMSA */ 5565 ARMCPRegInfo id_tlbtr_reginfo = { 5566 .name = "TLBTR", 5567 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, 5568 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0, 5569 }; 5570 /* MPUIR is specific to PMSA V6+ */ 5571 ARMCPRegInfo id_mpuir_reginfo = { 5572 .name = "MPUIR", 5573 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 5574 .access = PL1_R, .type = ARM_CP_CONST, 5575 .resetvalue = cpu->pmsav7_dregion << 8 5576 }; 5577 ARMCPRegInfo crn0_wi_reginfo = { 5578 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, 5579 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, 5580 .type = ARM_CP_NOP | ARM_CP_OVERRIDE 5581 }; 5582 if (arm_feature(env, ARM_FEATURE_OMAPCP) || 5583 arm_feature(env, ARM_FEATURE_STRONGARM)) { 5584 ARMCPRegInfo *r; 5585 /* Register the blanket "writes ignored" value first to cover the 5586 * whole space. Then update the specific ID registers to allow write 5587 * access, so that they ignore writes rather than causing them to 5588 * UNDEF. 5589 */ 5590 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); 5591 for (r = id_pre_v8_midr_cp_reginfo; 5592 r->type != ARM_CP_SENTINEL; r++) { 5593 r->access = PL1_RW; 5594 } 5595 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) { 5596 r->access = PL1_RW; 5597 } 5598 id_mpuir_reginfo.access = PL1_RW; 5599 id_tlbtr_reginfo.access = PL1_RW; 5600 } 5601 if (arm_feature(env, ARM_FEATURE_V8)) { 5602 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); 5603 } else { 5604 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); 5605 } 5606 define_arm_cp_regs(cpu, id_cp_reginfo); 5607 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 5608 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo); 5609 } else if (arm_feature(env, ARM_FEATURE_V7)) { 5610 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 5611 } 5612 } 5613 5614 if (arm_feature(env, ARM_FEATURE_MPIDR)) { 5615 define_arm_cp_regs(cpu, mpidr_cp_reginfo); 5616 } 5617 5618 if (arm_feature(env, ARM_FEATURE_AUXCR)) { 5619 ARMCPRegInfo auxcr_reginfo[] = { 5620 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, 5621 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, 5622 .access = PL1_RW, .type = ARM_CP_CONST, 5623 .resetvalue = cpu->reset_auxcr }, 5624 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH, 5625 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1, 5626 .access = PL2_RW, .type = ARM_CP_CONST, 5627 .resetvalue = 0 }, 5628 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64, 5629 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1, 5630 .access = PL3_RW, .type = ARM_CP_CONST, 5631 .resetvalue = 0 }, 5632 REGINFO_SENTINEL 5633 }; 5634 define_arm_cp_regs(cpu, auxcr_reginfo); 5635 if (arm_feature(env, ARM_FEATURE_V8)) { 5636 /* HACTLR2 maps to ACTLR_EL2[63:32] and is not in ARMv7 */ 5637 ARMCPRegInfo hactlr2_reginfo = { 5638 .name = "HACTLR2", .state = ARM_CP_STATE_AA32, 5639 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3, 5640 .access = PL2_RW, .type = ARM_CP_CONST, 5641 .resetvalue = 0 5642 }; 5643 define_one_arm_cp_reg(cpu, &hactlr2_reginfo); 5644 } 5645 } 5646 5647 if (arm_feature(env, ARM_FEATURE_CBAR)) { 5648 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5649 /* 32 bit view is [31:18] 0...0 [43:32]. */ 5650 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) 5651 | extract64(cpu->reset_cbar, 32, 12); 5652 ARMCPRegInfo cbar_reginfo[] = { 5653 { .name = "CBAR", 5654 .type = ARM_CP_CONST, 5655 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 5656 .access = PL1_R, .resetvalue = cpu->reset_cbar }, 5657 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, 5658 .type = ARM_CP_CONST, 5659 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, 5660 .access = PL1_R, .resetvalue = cbar32 }, 5661 REGINFO_SENTINEL 5662 }; 5663 /* We don't implement a r/w 64 bit CBAR currently */ 5664 assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); 5665 define_arm_cp_regs(cpu, cbar_reginfo); 5666 } else { 5667 ARMCPRegInfo cbar = { 5668 .name = "CBAR", 5669 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 5670 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar, 5671 .fieldoffset = offsetof(CPUARMState, 5672 cp15.c15_config_base_address) 5673 }; 5674 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 5675 cbar.access = PL1_R; 5676 cbar.fieldoffset = 0; 5677 cbar.type = ARM_CP_CONST; 5678 } 5679 define_one_arm_cp_reg(cpu, &cbar); 5680 } 5681 } 5682 5683 if (arm_feature(env, ARM_FEATURE_VBAR)) { 5684 ARMCPRegInfo vbar_cp_reginfo[] = { 5685 { .name = "VBAR", .state = ARM_CP_STATE_BOTH, 5686 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, 5687 .access = PL1_RW, .writefn = vbar_write, 5688 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s), 5689 offsetof(CPUARMState, cp15.vbar_ns) }, 5690 .resetvalue = 0 }, 5691 REGINFO_SENTINEL 5692 }; 5693 define_arm_cp_regs(cpu, vbar_cp_reginfo); 5694 } 5695 5696 /* Generic registers whose values depend on the implementation */ 5697 { 5698 ARMCPRegInfo sctlr = { 5699 .name = "SCTLR", .state = ARM_CP_STATE_BOTH, 5700 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 5701 .access = PL1_RW, 5702 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), 5703 offsetof(CPUARMState, cp15.sctlr_ns) }, 5704 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, 5705 .raw_writefn = raw_write, 5706 }; 5707 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 5708 /* Normally we would always end the TB on an SCTLR write, but Linux 5709 * arch/arm/mach-pxa/sleep.S expects two instructions following 5710 * an MMU enable to execute from cache. Imitate this behaviour. 5711 */ 5712 sctlr.type |= ARM_CP_SUPPRESS_TB_END; 5713 } 5714 define_one_arm_cp_reg(cpu, &sctlr); 5715 } 5716 5717 if (cpu_isar_feature(aa64_sve, cpu)) { 5718 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo); 5719 if (arm_feature(env, ARM_FEATURE_EL2)) { 5720 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo); 5721 } else { 5722 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo); 5723 } 5724 if (arm_feature(env, ARM_FEATURE_EL3)) { 5725 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo); 5726 } 5727 } 5728 } 5729 5730 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) 5731 { 5732 CPUState *cs = CPU(cpu); 5733 CPUARMState *env = &cpu->env; 5734 5735 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5736 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg, 5737 aarch64_fpu_gdb_set_reg, 5738 34, "aarch64-fpu.xml", 0); 5739 } else if (arm_feature(env, ARM_FEATURE_NEON)) { 5740 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 5741 51, "arm-neon.xml", 0); 5742 } else if (arm_feature(env, ARM_FEATURE_VFP3)) { 5743 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 5744 35, "arm-vfp3.xml", 0); 5745 } else if (arm_feature(env, ARM_FEATURE_VFP)) { 5746 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 5747 19, "arm-vfp.xml", 0); 5748 } 5749 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg, 5750 arm_gen_dynamic_xml(cs), 5751 "system-registers.xml", 0); 5752 } 5753 5754 /* Sort alphabetically by type name, except for "any". */ 5755 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) 5756 { 5757 ObjectClass *class_a = (ObjectClass *)a; 5758 ObjectClass *class_b = (ObjectClass *)b; 5759 const char *name_a, *name_b; 5760 5761 name_a = object_class_get_name(class_a); 5762 name_b = object_class_get_name(class_b); 5763 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) { 5764 return 1; 5765 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) { 5766 return -1; 5767 } else { 5768 return strcmp(name_a, name_b); 5769 } 5770 } 5771 5772 static void arm_cpu_list_entry(gpointer data, gpointer user_data) 5773 { 5774 ObjectClass *oc = data; 5775 CPUListState *s = user_data; 5776 const char *typename; 5777 char *name; 5778 5779 typename = object_class_get_name(oc); 5780 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU)); 5781 (*s->cpu_fprintf)(s->file, " %s\n", 5782 name); 5783 g_free(name); 5784 } 5785 5786 void arm_cpu_list(FILE *f, fprintf_function cpu_fprintf) 5787 { 5788 CPUListState s = { 5789 .file = f, 5790 .cpu_fprintf = cpu_fprintf, 5791 }; 5792 GSList *list; 5793 5794 list = object_class_get_list(TYPE_ARM_CPU, false); 5795 list = g_slist_sort(list, arm_cpu_list_compare); 5796 (*cpu_fprintf)(f, "Available CPUs:\n"); 5797 g_slist_foreach(list, arm_cpu_list_entry, &s); 5798 g_slist_free(list); 5799 } 5800 5801 static void arm_cpu_add_definition(gpointer data, gpointer user_data) 5802 { 5803 ObjectClass *oc = data; 5804 CpuDefinitionInfoList **cpu_list = user_data; 5805 CpuDefinitionInfoList *entry; 5806 CpuDefinitionInfo *info; 5807 const char *typename; 5808 5809 typename = object_class_get_name(oc); 5810 info = g_malloc0(sizeof(*info)); 5811 info->name = g_strndup(typename, 5812 strlen(typename) - strlen("-" TYPE_ARM_CPU)); 5813 info->q_typename = g_strdup(typename); 5814 5815 entry = g_malloc0(sizeof(*entry)); 5816 entry->value = info; 5817 entry->next = *cpu_list; 5818 *cpu_list = entry; 5819 } 5820 5821 CpuDefinitionInfoList *arch_query_cpu_definitions(Error **errp) 5822 { 5823 CpuDefinitionInfoList *cpu_list = NULL; 5824 GSList *list; 5825 5826 list = object_class_get_list(TYPE_ARM_CPU, false); 5827 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list); 5828 g_slist_free(list); 5829 5830 return cpu_list; 5831 } 5832 5833 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, 5834 void *opaque, int state, int secstate, 5835 int crm, int opc1, int opc2, 5836 const char *name) 5837 { 5838 /* Private utility function for define_one_arm_cp_reg_with_opaque(): 5839 * add a single reginfo struct to the hash table. 5840 */ 5841 uint32_t *key = g_new(uint32_t, 1); 5842 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); 5843 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; 5844 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0; 5845 5846 r2->name = g_strdup(name); 5847 /* Reset the secure state to the specific incoming state. This is 5848 * necessary as the register may have been defined with both states. 5849 */ 5850 r2->secure = secstate; 5851 5852 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 5853 /* Register is banked (using both entries in array). 5854 * Overwriting fieldoffset as the array is only used to define 5855 * banked registers but later only fieldoffset is used. 5856 */ 5857 r2->fieldoffset = r->bank_fieldoffsets[ns]; 5858 } 5859 5860 if (state == ARM_CP_STATE_AA32) { 5861 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 5862 /* If the register is banked then we don't need to migrate or 5863 * reset the 32-bit instance in certain cases: 5864 * 5865 * 1) If the register has both 32-bit and 64-bit instances then we 5866 * can count on the 64-bit instance taking care of the 5867 * non-secure bank. 5868 * 2) If ARMv8 is enabled then we can count on a 64-bit version 5869 * taking care of the secure bank. This requires that separate 5870 * 32 and 64-bit definitions are provided. 5871 */ 5872 if ((r->state == ARM_CP_STATE_BOTH && ns) || 5873 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) { 5874 r2->type |= ARM_CP_ALIAS; 5875 } 5876 } else if ((secstate != r->secure) && !ns) { 5877 /* The register is not banked so we only want to allow migration of 5878 * the non-secure instance. 5879 */ 5880 r2->type |= ARM_CP_ALIAS; 5881 } 5882 5883 if (r->state == ARM_CP_STATE_BOTH) { 5884 /* We assume it is a cp15 register if the .cp field is left unset. 5885 */ 5886 if (r2->cp == 0) { 5887 r2->cp = 15; 5888 } 5889 5890 #ifdef HOST_WORDS_BIGENDIAN 5891 if (r2->fieldoffset) { 5892 r2->fieldoffset += sizeof(uint32_t); 5893 } 5894 #endif 5895 } 5896 } 5897 if (state == ARM_CP_STATE_AA64) { 5898 /* To allow abbreviation of ARMCPRegInfo 5899 * definitions, we treat cp == 0 as equivalent to 5900 * the value for "standard guest-visible sysreg". 5901 * STATE_BOTH definitions are also always "standard 5902 * sysreg" in their AArch64 view (the .cp value may 5903 * be non-zero for the benefit of the AArch32 view). 5904 */ 5905 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) { 5906 r2->cp = CP_REG_ARM64_SYSREG_CP; 5907 } 5908 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm, 5909 r2->opc0, opc1, opc2); 5910 } else { 5911 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2); 5912 } 5913 if (opaque) { 5914 r2->opaque = opaque; 5915 } 5916 /* reginfo passed to helpers is correct for the actual access, 5917 * and is never ARM_CP_STATE_BOTH: 5918 */ 5919 r2->state = state; 5920 /* Make sure reginfo passed to helpers for wildcarded regs 5921 * has the correct crm/opc1/opc2 for this reg, not CP_ANY: 5922 */ 5923 r2->crm = crm; 5924 r2->opc1 = opc1; 5925 r2->opc2 = opc2; 5926 /* By convention, for wildcarded registers only the first 5927 * entry is used for migration; the others are marked as 5928 * ALIAS so we don't try to transfer the register 5929 * multiple times. Special registers (ie NOP/WFI) are 5930 * never migratable and not even raw-accessible. 5931 */ 5932 if ((r->type & ARM_CP_SPECIAL)) { 5933 r2->type |= ARM_CP_NO_RAW; 5934 } 5935 if (((r->crm == CP_ANY) && crm != 0) || 5936 ((r->opc1 == CP_ANY) && opc1 != 0) || 5937 ((r->opc2 == CP_ANY) && opc2 != 0)) { 5938 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB; 5939 } 5940 5941 /* Check that raw accesses are either forbidden or handled. Note that 5942 * we can't assert this earlier because the setup of fieldoffset for 5943 * banked registers has to be done first. 5944 */ 5945 if (!(r2->type & ARM_CP_NO_RAW)) { 5946 assert(!raw_accessors_invalid(r2)); 5947 } 5948 5949 /* Overriding of an existing definition must be explicitly 5950 * requested. 5951 */ 5952 if (!(r->type & ARM_CP_OVERRIDE)) { 5953 ARMCPRegInfo *oldreg; 5954 oldreg = g_hash_table_lookup(cpu->cp_regs, key); 5955 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { 5956 fprintf(stderr, "Register redefined: cp=%d %d bit " 5957 "crn=%d crm=%d opc1=%d opc2=%d, " 5958 "was %s, now %s\n", r2->cp, 32 + 32 * is64, 5959 r2->crn, r2->crm, r2->opc1, r2->opc2, 5960 oldreg->name, r2->name); 5961 g_assert_not_reached(); 5962 } 5963 } 5964 g_hash_table_insert(cpu->cp_regs, key, r2); 5965 } 5966 5967 5968 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, 5969 const ARMCPRegInfo *r, void *opaque) 5970 { 5971 /* Define implementations of coprocessor registers. 5972 * We store these in a hashtable because typically 5973 * there are less than 150 registers in a space which 5974 * is 16*16*16*8*8 = 262144 in size. 5975 * Wildcarding is supported for the crm, opc1 and opc2 fields. 5976 * If a register is defined twice then the second definition is 5977 * used, so this can be used to define some generic registers and 5978 * then override them with implementation specific variations. 5979 * At least one of the original and the second definition should 5980 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard 5981 * against accidental use. 5982 * 5983 * The state field defines whether the register is to be 5984 * visible in the AArch32 or AArch64 execution state. If the 5985 * state is set to ARM_CP_STATE_BOTH then we synthesise a 5986 * reginfo structure for the AArch32 view, which sees the lower 5987 * 32 bits of the 64 bit register. 5988 * 5989 * Only registers visible in AArch64 may set r->opc0; opc0 cannot 5990 * be wildcarded. AArch64 registers are always considered to be 64 5991 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of 5992 * the register, if any. 5993 */ 5994 int crm, opc1, opc2, state; 5995 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; 5996 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; 5997 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; 5998 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; 5999 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; 6000 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; 6001 /* 64 bit registers have only CRm and Opc1 fields */ 6002 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); 6003 /* op0 only exists in the AArch64 encodings */ 6004 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); 6005 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ 6006 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); 6007 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1 6008 * encodes a minimum access level for the register. We roll this 6009 * runtime check into our general permission check code, so check 6010 * here that the reginfo's specified permissions are strict enough 6011 * to encompass the generic architectural permission check. 6012 */ 6013 if (r->state != ARM_CP_STATE_AA32) { 6014 int mask = 0; 6015 switch (r->opc1) { 6016 case 0: case 1: case 2: 6017 /* min_EL EL1 */ 6018 mask = PL1_RW; 6019 break; 6020 case 3: 6021 /* min_EL EL0 */ 6022 mask = PL0_RW; 6023 break; 6024 case 4: 6025 /* min_EL EL2 */ 6026 mask = PL2_RW; 6027 break; 6028 case 5: 6029 /* unallocated encoding, so not possible */ 6030 assert(false); 6031 break; 6032 case 6: 6033 /* min_EL EL3 */ 6034 mask = PL3_RW; 6035 break; 6036 case 7: 6037 /* min_EL EL1, secure mode only (we don't check the latter) */ 6038 mask = PL1_RW; 6039 break; 6040 default: 6041 /* broken reginfo with out-of-range opc1 */ 6042 assert(false); 6043 break; 6044 } 6045 /* assert our permissions are not too lax (stricter is fine) */ 6046 assert((r->access & ~mask) == 0); 6047 } 6048 6049 /* Check that the register definition has enough info to handle 6050 * reads and writes if they are permitted. 6051 */ 6052 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { 6053 if (r->access & PL3_R) { 6054 assert((r->fieldoffset || 6055 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 6056 r->readfn); 6057 } 6058 if (r->access & PL3_W) { 6059 assert((r->fieldoffset || 6060 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 6061 r->writefn); 6062 } 6063 } 6064 /* Bad type field probably means missing sentinel at end of reg list */ 6065 assert(cptype_valid(r->type)); 6066 for (crm = crmmin; crm <= crmmax; crm++) { 6067 for (opc1 = opc1min; opc1 <= opc1max; opc1++) { 6068 for (opc2 = opc2min; opc2 <= opc2max; opc2++) { 6069 for (state = ARM_CP_STATE_AA32; 6070 state <= ARM_CP_STATE_AA64; state++) { 6071 if (r->state != state && r->state != ARM_CP_STATE_BOTH) { 6072 continue; 6073 } 6074 if (state == ARM_CP_STATE_AA32) { 6075 /* Under AArch32 CP registers can be common 6076 * (same for secure and non-secure world) or banked. 6077 */ 6078 char *name; 6079 6080 switch (r->secure) { 6081 case ARM_CP_SECSTATE_S: 6082 case ARM_CP_SECSTATE_NS: 6083 add_cpreg_to_hashtable(cpu, r, opaque, state, 6084 r->secure, crm, opc1, opc2, 6085 r->name); 6086 break; 6087 default: 6088 name = g_strdup_printf("%s_S", r->name); 6089 add_cpreg_to_hashtable(cpu, r, opaque, state, 6090 ARM_CP_SECSTATE_S, 6091 crm, opc1, opc2, name); 6092 g_free(name); 6093 add_cpreg_to_hashtable(cpu, r, opaque, state, 6094 ARM_CP_SECSTATE_NS, 6095 crm, opc1, opc2, r->name); 6096 break; 6097 } 6098 } else { 6099 /* AArch64 registers get mapped to non-secure instance 6100 * of AArch32 */ 6101 add_cpreg_to_hashtable(cpu, r, opaque, state, 6102 ARM_CP_SECSTATE_NS, 6103 crm, opc1, opc2, r->name); 6104 } 6105 } 6106 } 6107 } 6108 } 6109 } 6110 6111 void define_arm_cp_regs_with_opaque(ARMCPU *cpu, 6112 const ARMCPRegInfo *regs, void *opaque) 6113 { 6114 /* Define a whole list of registers */ 6115 const ARMCPRegInfo *r; 6116 for (r = regs; r->type != ARM_CP_SENTINEL; r++) { 6117 define_one_arm_cp_reg_with_opaque(cpu, r, opaque); 6118 } 6119 } 6120 6121 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) 6122 { 6123 return g_hash_table_lookup(cpregs, &encoded_cp); 6124 } 6125 6126 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, 6127 uint64_t value) 6128 { 6129 /* Helper coprocessor write function for write-ignore registers */ 6130 } 6131 6132 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) 6133 { 6134 /* Helper coprocessor write function for read-as-zero registers */ 6135 return 0; 6136 } 6137 6138 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) 6139 { 6140 /* Helper coprocessor reset function for do-nothing-on-reset registers */ 6141 } 6142 6143 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type) 6144 { 6145 /* Return true if it is not valid for us to switch to 6146 * this CPU mode (ie all the UNPREDICTABLE cases in 6147 * the ARM ARM CPSRWriteByInstr pseudocode). 6148 */ 6149 6150 /* Changes to or from Hyp via MSR and CPS are illegal. */ 6151 if (write_type == CPSRWriteByInstr && 6152 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP || 6153 mode == ARM_CPU_MODE_HYP)) { 6154 return 1; 6155 } 6156 6157 switch (mode) { 6158 case ARM_CPU_MODE_USR: 6159 return 0; 6160 case ARM_CPU_MODE_SYS: 6161 case ARM_CPU_MODE_SVC: 6162 case ARM_CPU_MODE_ABT: 6163 case ARM_CPU_MODE_UND: 6164 case ARM_CPU_MODE_IRQ: 6165 case ARM_CPU_MODE_FIQ: 6166 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7 6167 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.) 6168 */ 6169 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR 6170 * and CPS are treated as illegal mode changes. 6171 */ 6172 if (write_type == CPSRWriteByInstr && 6173 (env->cp15.hcr_el2 & HCR_TGE) && 6174 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON && 6175 !arm_is_secure_below_el3(env)) { 6176 return 1; 6177 } 6178 return 0; 6179 case ARM_CPU_MODE_HYP: 6180 return !arm_feature(env, ARM_FEATURE_EL2) 6181 || arm_current_el(env) < 2 || arm_is_secure(env); 6182 case ARM_CPU_MODE_MON: 6183 return arm_current_el(env) < 3; 6184 default: 6185 return 1; 6186 } 6187 } 6188 6189 uint32_t cpsr_read(CPUARMState *env) 6190 { 6191 int ZF; 6192 ZF = (env->ZF == 0); 6193 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | 6194 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) 6195 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) 6196 | ((env->condexec_bits & 0xfc) << 8) 6197 | (env->GE << 16) | (env->daif & CPSR_AIF); 6198 } 6199 6200 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask, 6201 CPSRWriteType write_type) 6202 { 6203 uint32_t changed_daif; 6204 6205 if (mask & CPSR_NZCV) { 6206 env->ZF = (~val) & CPSR_Z; 6207 env->NF = val; 6208 env->CF = (val >> 29) & 1; 6209 env->VF = (val << 3) & 0x80000000; 6210 } 6211 if (mask & CPSR_Q) 6212 env->QF = ((val & CPSR_Q) != 0); 6213 if (mask & CPSR_T) 6214 env->thumb = ((val & CPSR_T) != 0); 6215 if (mask & CPSR_IT_0_1) { 6216 env->condexec_bits &= ~3; 6217 env->condexec_bits |= (val >> 25) & 3; 6218 } 6219 if (mask & CPSR_IT_2_7) { 6220 env->condexec_bits &= 3; 6221 env->condexec_bits |= (val >> 8) & 0xfc; 6222 } 6223 if (mask & CPSR_GE) { 6224 env->GE = (val >> 16) & 0xf; 6225 } 6226 6227 /* In a V7 implementation that includes the security extensions but does 6228 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control 6229 * whether non-secure software is allowed to change the CPSR_F and CPSR_A 6230 * bits respectively. 6231 * 6232 * In a V8 implementation, it is permitted for privileged software to 6233 * change the CPSR A/F bits regardless of the SCR.AW/FW bits. 6234 */ 6235 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) && 6236 arm_feature(env, ARM_FEATURE_EL3) && 6237 !arm_feature(env, ARM_FEATURE_EL2) && 6238 !arm_is_secure(env)) { 6239 6240 changed_daif = (env->daif ^ val) & mask; 6241 6242 if (changed_daif & CPSR_A) { 6243 /* Check to see if we are allowed to change the masking of async 6244 * abort exceptions from a non-secure state. 6245 */ 6246 if (!(env->cp15.scr_el3 & SCR_AW)) { 6247 qemu_log_mask(LOG_GUEST_ERROR, 6248 "Ignoring attempt to switch CPSR_A flag from " 6249 "non-secure world with SCR.AW bit clear\n"); 6250 mask &= ~CPSR_A; 6251 } 6252 } 6253 6254 if (changed_daif & CPSR_F) { 6255 /* Check to see if we are allowed to change the masking of FIQ 6256 * exceptions from a non-secure state. 6257 */ 6258 if (!(env->cp15.scr_el3 & SCR_FW)) { 6259 qemu_log_mask(LOG_GUEST_ERROR, 6260 "Ignoring attempt to switch CPSR_F flag from " 6261 "non-secure world with SCR.FW bit clear\n"); 6262 mask &= ~CPSR_F; 6263 } 6264 6265 /* Check whether non-maskable FIQ (NMFI) support is enabled. 6266 * If this bit is set software is not allowed to mask 6267 * FIQs, but is allowed to set CPSR_F to 0. 6268 */ 6269 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && 6270 (val & CPSR_F)) { 6271 qemu_log_mask(LOG_GUEST_ERROR, 6272 "Ignoring attempt to enable CPSR_F flag " 6273 "(non-maskable FIQ [NMFI] support enabled)\n"); 6274 mask &= ~CPSR_F; 6275 } 6276 } 6277 } 6278 6279 env->daif &= ~(CPSR_AIF & mask); 6280 env->daif |= val & CPSR_AIF & mask; 6281 6282 if (write_type != CPSRWriteRaw && 6283 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) { 6284 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) { 6285 /* Note that we can only get here in USR mode if this is a 6286 * gdb stub write; for this case we follow the architectural 6287 * behaviour for guest writes in USR mode of ignoring an attempt 6288 * to switch mode. (Those are caught by translate.c for writes 6289 * triggered by guest instructions.) 6290 */ 6291 mask &= ~CPSR_M; 6292 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) { 6293 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in 6294 * v7, and has defined behaviour in v8: 6295 * + leave CPSR.M untouched 6296 * + allow changes to the other CPSR fields 6297 * + set PSTATE.IL 6298 * For user changes via the GDB stub, we don't set PSTATE.IL, 6299 * as this would be unnecessarily harsh for a user error. 6300 */ 6301 mask &= ~CPSR_M; 6302 if (write_type != CPSRWriteByGDBStub && 6303 arm_feature(env, ARM_FEATURE_V8)) { 6304 mask |= CPSR_IL; 6305 val |= CPSR_IL; 6306 } 6307 qemu_log_mask(LOG_GUEST_ERROR, 6308 "Illegal AArch32 mode switch attempt from %s to %s\n", 6309 aarch32_mode_name(env->uncached_cpsr), 6310 aarch32_mode_name(val)); 6311 } else { 6312 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n", 6313 write_type == CPSRWriteExceptionReturn ? 6314 "Exception return from AArch32" : 6315 "AArch32 mode switch from", 6316 aarch32_mode_name(env->uncached_cpsr), 6317 aarch32_mode_name(val), env->regs[15]); 6318 switch_mode(env, val & CPSR_M); 6319 } 6320 } 6321 mask &= ~CACHED_CPSR_BITS; 6322 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); 6323 } 6324 6325 /* Sign/zero extend */ 6326 uint32_t HELPER(sxtb16)(uint32_t x) 6327 { 6328 uint32_t res; 6329 res = (uint16_t)(int8_t)x; 6330 res |= (uint32_t)(int8_t)(x >> 16) << 16; 6331 return res; 6332 } 6333 6334 uint32_t HELPER(uxtb16)(uint32_t x) 6335 { 6336 uint32_t res; 6337 res = (uint16_t)(uint8_t)x; 6338 res |= (uint32_t)(uint8_t)(x >> 16) << 16; 6339 return res; 6340 } 6341 6342 int32_t HELPER(sdiv)(int32_t num, int32_t den) 6343 { 6344 if (den == 0) 6345 return 0; 6346 if (num == INT_MIN && den == -1) 6347 return INT_MIN; 6348 return num / den; 6349 } 6350 6351 uint32_t HELPER(udiv)(uint32_t num, uint32_t den) 6352 { 6353 if (den == 0) 6354 return 0; 6355 return num / den; 6356 } 6357 6358 uint32_t HELPER(rbit)(uint32_t x) 6359 { 6360 return revbit32(x); 6361 } 6362 6363 #if defined(CONFIG_USER_ONLY) 6364 6365 /* These should probably raise undefined insn exceptions. */ 6366 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) 6367 { 6368 ARMCPU *cpu = arm_env_get_cpu(env); 6369 6370 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg); 6371 } 6372 6373 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) 6374 { 6375 ARMCPU *cpu = arm_env_get_cpu(env); 6376 6377 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg); 6378 return 0; 6379 } 6380 6381 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) 6382 { 6383 /* translate.c should never generate calls here in user-only mode */ 6384 g_assert_not_reached(); 6385 } 6386 6387 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) 6388 { 6389 /* translate.c should never generate calls here in user-only mode */ 6390 g_assert_not_reached(); 6391 } 6392 6393 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) 6394 { 6395 /* The TT instructions can be used by unprivileged code, but in 6396 * user-only emulation we don't have the MPU. 6397 * Luckily since we know we are NonSecure unprivileged (and that in 6398 * turn means that the A flag wasn't specified), all the bits in the 6399 * register must be zero: 6400 * IREGION: 0 because IRVALID is 0 6401 * IRVALID: 0 because NS 6402 * S: 0 because NS 6403 * NSRW: 0 because NS 6404 * NSR: 0 because NS 6405 * RW: 0 because unpriv and A flag not set 6406 * R: 0 because unpriv and A flag not set 6407 * SRVALID: 0 because NS 6408 * MRVALID: 0 because unpriv and A flag not set 6409 * SREGION: 0 becaus SRVALID is 0 6410 * MREGION: 0 because MRVALID is 0 6411 */ 6412 return 0; 6413 } 6414 6415 static void switch_mode(CPUARMState *env, int mode) 6416 { 6417 ARMCPU *cpu = arm_env_get_cpu(env); 6418 6419 if (mode != ARM_CPU_MODE_USR) { 6420 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); 6421 } 6422 } 6423 6424 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 6425 uint32_t cur_el, bool secure) 6426 { 6427 return 1; 6428 } 6429 6430 void aarch64_sync_64_to_32(CPUARMState *env) 6431 { 6432 g_assert_not_reached(); 6433 } 6434 6435 #else 6436 6437 static void switch_mode(CPUARMState *env, int mode) 6438 { 6439 int old_mode; 6440 int i; 6441 6442 old_mode = env->uncached_cpsr & CPSR_M; 6443 if (mode == old_mode) 6444 return; 6445 6446 if (old_mode == ARM_CPU_MODE_FIQ) { 6447 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); 6448 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); 6449 } else if (mode == ARM_CPU_MODE_FIQ) { 6450 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); 6451 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); 6452 } 6453 6454 i = bank_number(old_mode); 6455 env->banked_r13[i] = env->regs[13]; 6456 env->banked_r14[i] = env->regs[14]; 6457 env->banked_spsr[i] = env->spsr; 6458 6459 i = bank_number(mode); 6460 env->regs[13] = env->banked_r13[i]; 6461 env->regs[14] = env->banked_r14[i]; 6462 env->spsr = env->banked_spsr[i]; 6463 } 6464 6465 /* Physical Interrupt Target EL Lookup Table 6466 * 6467 * [ From ARM ARM section G1.13.4 (Table G1-15) ] 6468 * 6469 * The below multi-dimensional table is used for looking up the target 6470 * exception level given numerous condition criteria. Specifically, the 6471 * target EL is based on SCR and HCR routing controls as well as the 6472 * currently executing EL and secure state. 6473 * 6474 * Dimensions: 6475 * target_el_table[2][2][2][2][2][4] 6476 * | | | | | +--- Current EL 6477 * | | | | +------ Non-secure(0)/Secure(1) 6478 * | | | +--------- HCR mask override 6479 * | | +------------ SCR exec state control 6480 * | +--------------- SCR mask override 6481 * +------------------ 32-bit(0)/64-bit(1) EL3 6482 * 6483 * The table values are as such: 6484 * 0-3 = EL0-EL3 6485 * -1 = Cannot occur 6486 * 6487 * The ARM ARM target EL table includes entries indicating that an "exception 6488 * is not taken". The two cases where this is applicable are: 6489 * 1) An exception is taken from EL3 but the SCR does not have the exception 6490 * routed to EL3. 6491 * 2) An exception is taken from EL2 but the HCR does not have the exception 6492 * routed to EL2. 6493 * In these two cases, the below table contain a target of EL1. This value is 6494 * returned as it is expected that the consumer of the table data will check 6495 * for "target EL >= current EL" to ensure the exception is not taken. 6496 * 6497 * SCR HCR 6498 * 64 EA AMO From 6499 * BIT IRQ IMO Non-secure Secure 6500 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 6501 */ 6502 static const int8_t target_el_table[2][2][2][2][2][4] = { 6503 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 6504 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, 6505 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 6506 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, 6507 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 6508 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, 6509 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 6510 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, 6511 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, 6512 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},}, 6513 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },}, 6514 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},}, 6515 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 6516 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, 6517 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 6518 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},}, 6519 }; 6520 6521 /* 6522 * Determine the target EL for physical exceptions 6523 */ 6524 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 6525 uint32_t cur_el, bool secure) 6526 { 6527 CPUARMState *env = cs->env_ptr; 6528 int rw; 6529 int scr; 6530 int hcr; 6531 int target_el; 6532 /* Is the highest EL AArch64? */ 6533 int is64 = arm_feature(env, ARM_FEATURE_AARCH64); 6534 6535 if (arm_feature(env, ARM_FEATURE_EL3)) { 6536 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW); 6537 } else { 6538 /* Either EL2 is the highest EL (and so the EL2 register width 6539 * is given by is64); or there is no EL2 or EL3, in which case 6540 * the value of 'rw' does not affect the table lookup anyway. 6541 */ 6542 rw = is64; 6543 } 6544 6545 switch (excp_idx) { 6546 case EXCP_IRQ: 6547 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); 6548 hcr = arm_hcr_el2_imo(env); 6549 break; 6550 case EXCP_FIQ: 6551 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); 6552 hcr = arm_hcr_el2_fmo(env); 6553 break; 6554 default: 6555 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); 6556 hcr = arm_hcr_el2_amo(env); 6557 break; 6558 }; 6559 6560 /* If HCR.TGE is set then HCR is treated as being 1 */ 6561 hcr |= ((env->cp15.hcr_el2 & HCR_TGE) == HCR_TGE); 6562 6563 /* Perform a table-lookup for the target EL given the current state */ 6564 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; 6565 6566 assert(target_el > 0); 6567 6568 return target_el; 6569 } 6570 6571 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value, 6572 ARMMMUIdx mmu_idx, bool ignfault) 6573 { 6574 CPUState *cs = CPU(cpu); 6575 CPUARMState *env = &cpu->env; 6576 MemTxAttrs attrs = {}; 6577 MemTxResult txres; 6578 target_ulong page_size; 6579 hwaddr physaddr; 6580 int prot; 6581 ARMMMUFaultInfo fi = {}; 6582 bool secure = mmu_idx & ARM_MMU_IDX_M_S; 6583 int exc; 6584 bool exc_secure; 6585 6586 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr, 6587 &attrs, &prot, &page_size, &fi, NULL)) { 6588 /* MPU/SAU lookup failed */ 6589 if (fi.type == ARMFault_QEMU_SFault) { 6590 qemu_log_mask(CPU_LOG_INT, 6591 "...SecureFault with SFSR.AUVIOL during stacking\n"); 6592 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK; 6593 env->v7m.sfar = addr; 6594 exc = ARMV7M_EXCP_SECURE; 6595 exc_secure = false; 6596 } else { 6597 qemu_log_mask(CPU_LOG_INT, "...MemManageFault with CFSR.MSTKERR\n"); 6598 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK; 6599 exc = ARMV7M_EXCP_MEM; 6600 exc_secure = secure; 6601 } 6602 goto pend_fault; 6603 } 6604 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value, 6605 attrs, &txres); 6606 if (txres != MEMTX_OK) { 6607 /* BusFault trying to write the data */ 6608 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n"); 6609 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK; 6610 exc = ARMV7M_EXCP_BUS; 6611 exc_secure = false; 6612 goto pend_fault; 6613 } 6614 return true; 6615 6616 pend_fault: 6617 /* By pending the exception at this point we are making 6618 * the IMPDEF choice "overridden exceptions pended" (see the 6619 * MergeExcInfo() pseudocode). The other choice would be to not 6620 * pend them now and then make a choice about which to throw away 6621 * later if we have two derived exceptions. 6622 * The only case when we must not pend the exception but instead 6623 * throw it away is if we are doing the push of the callee registers 6624 * and we've already generated a derived exception. Even in this 6625 * case we will still update the fault status registers. 6626 */ 6627 if (!ignfault) { 6628 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure); 6629 } 6630 return false; 6631 } 6632 6633 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr, 6634 ARMMMUIdx mmu_idx) 6635 { 6636 CPUState *cs = CPU(cpu); 6637 CPUARMState *env = &cpu->env; 6638 MemTxAttrs attrs = {}; 6639 MemTxResult txres; 6640 target_ulong page_size; 6641 hwaddr physaddr; 6642 int prot; 6643 ARMMMUFaultInfo fi = {}; 6644 bool secure = mmu_idx & ARM_MMU_IDX_M_S; 6645 int exc; 6646 bool exc_secure; 6647 uint32_t value; 6648 6649 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr, 6650 &attrs, &prot, &page_size, &fi, NULL)) { 6651 /* MPU/SAU lookup failed */ 6652 if (fi.type == ARMFault_QEMU_SFault) { 6653 qemu_log_mask(CPU_LOG_INT, 6654 "...SecureFault with SFSR.AUVIOL during unstack\n"); 6655 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK; 6656 env->v7m.sfar = addr; 6657 exc = ARMV7M_EXCP_SECURE; 6658 exc_secure = false; 6659 } else { 6660 qemu_log_mask(CPU_LOG_INT, 6661 "...MemManageFault with CFSR.MUNSTKERR\n"); 6662 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK; 6663 exc = ARMV7M_EXCP_MEM; 6664 exc_secure = secure; 6665 } 6666 goto pend_fault; 6667 } 6668 6669 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr, 6670 attrs, &txres); 6671 if (txres != MEMTX_OK) { 6672 /* BusFault trying to read the data */ 6673 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n"); 6674 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK; 6675 exc = ARMV7M_EXCP_BUS; 6676 exc_secure = false; 6677 goto pend_fault; 6678 } 6679 6680 *dest = value; 6681 return true; 6682 6683 pend_fault: 6684 /* By pending the exception at this point we are making 6685 * the IMPDEF choice "overridden exceptions pended" (see the 6686 * MergeExcInfo() pseudocode). The other choice would be to not 6687 * pend them now and then make a choice about which to throw away 6688 * later if we have two derived exceptions. 6689 */ 6690 armv7m_nvic_set_pending(env->nvic, exc, exc_secure); 6691 return false; 6692 } 6693 6694 /* Write to v7M CONTROL.SPSEL bit for the specified security bank. 6695 * This may change the current stack pointer between Main and Process 6696 * stack pointers if it is done for the CONTROL register for the current 6697 * security state. 6698 */ 6699 static void write_v7m_control_spsel_for_secstate(CPUARMState *env, 6700 bool new_spsel, 6701 bool secstate) 6702 { 6703 bool old_is_psp = v7m_using_psp(env); 6704 6705 env->v7m.control[secstate] = 6706 deposit32(env->v7m.control[secstate], 6707 R_V7M_CONTROL_SPSEL_SHIFT, 6708 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel); 6709 6710 if (secstate == env->v7m.secure) { 6711 bool new_is_psp = v7m_using_psp(env); 6712 uint32_t tmp; 6713 6714 if (old_is_psp != new_is_psp) { 6715 tmp = env->v7m.other_sp; 6716 env->v7m.other_sp = env->regs[13]; 6717 env->regs[13] = tmp; 6718 } 6719 } 6720 } 6721 6722 /* Write to v7M CONTROL.SPSEL bit. This may change the current 6723 * stack pointer between Main and Process stack pointers. 6724 */ 6725 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel) 6726 { 6727 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure); 6728 } 6729 6730 void write_v7m_exception(CPUARMState *env, uint32_t new_exc) 6731 { 6732 /* Write a new value to v7m.exception, thus transitioning into or out 6733 * of Handler mode; this may result in a change of active stack pointer. 6734 */ 6735 bool new_is_psp, old_is_psp = v7m_using_psp(env); 6736 uint32_t tmp; 6737 6738 env->v7m.exception = new_exc; 6739 6740 new_is_psp = v7m_using_psp(env); 6741 6742 if (old_is_psp != new_is_psp) { 6743 tmp = env->v7m.other_sp; 6744 env->v7m.other_sp = env->regs[13]; 6745 env->regs[13] = tmp; 6746 } 6747 } 6748 6749 /* Switch M profile security state between NS and S */ 6750 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate) 6751 { 6752 uint32_t new_ss_msp, new_ss_psp; 6753 6754 if (env->v7m.secure == new_secstate) { 6755 return; 6756 } 6757 6758 /* All the banked state is accessed by looking at env->v7m.secure 6759 * except for the stack pointer; rearrange the SP appropriately. 6760 */ 6761 new_ss_msp = env->v7m.other_ss_msp; 6762 new_ss_psp = env->v7m.other_ss_psp; 6763 6764 if (v7m_using_psp(env)) { 6765 env->v7m.other_ss_psp = env->regs[13]; 6766 env->v7m.other_ss_msp = env->v7m.other_sp; 6767 } else { 6768 env->v7m.other_ss_msp = env->regs[13]; 6769 env->v7m.other_ss_psp = env->v7m.other_sp; 6770 } 6771 6772 env->v7m.secure = new_secstate; 6773 6774 if (v7m_using_psp(env)) { 6775 env->regs[13] = new_ss_psp; 6776 env->v7m.other_sp = new_ss_msp; 6777 } else { 6778 env->regs[13] = new_ss_msp; 6779 env->v7m.other_sp = new_ss_psp; 6780 } 6781 } 6782 6783 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) 6784 { 6785 /* Handle v7M BXNS: 6786 * - if the return value is a magic value, do exception return (like BX) 6787 * - otherwise bit 0 of the return value is the target security state 6788 */ 6789 uint32_t min_magic; 6790 6791 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 6792 /* Covers FNC_RETURN and EXC_RETURN magic */ 6793 min_magic = FNC_RETURN_MIN_MAGIC; 6794 } else { 6795 /* EXC_RETURN magic only */ 6796 min_magic = EXC_RETURN_MIN_MAGIC; 6797 } 6798 6799 if (dest >= min_magic) { 6800 /* This is an exception return magic value; put it where 6801 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT. 6802 * Note that if we ever add gen_ss_advance() singlestep support to 6803 * M profile this should count as an "instruction execution complete" 6804 * event (compare gen_bx_excret_final_code()). 6805 */ 6806 env->regs[15] = dest & ~1; 6807 env->thumb = dest & 1; 6808 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT); 6809 /* notreached */ 6810 } 6811 6812 /* translate.c should have made BXNS UNDEF unless we're secure */ 6813 assert(env->v7m.secure); 6814 6815 switch_v7m_security_state(env, dest & 1); 6816 env->thumb = 1; 6817 env->regs[15] = dest & ~1; 6818 } 6819 6820 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) 6821 { 6822 /* Handle v7M BLXNS: 6823 * - bit 0 of the destination address is the target security state 6824 */ 6825 6826 /* At this point regs[15] is the address just after the BLXNS */ 6827 uint32_t nextinst = env->regs[15] | 1; 6828 uint32_t sp = env->regs[13] - 8; 6829 uint32_t saved_psr; 6830 6831 /* translate.c will have made BLXNS UNDEF unless we're secure */ 6832 assert(env->v7m.secure); 6833 6834 if (dest & 1) { 6835 /* target is Secure, so this is just a normal BLX, 6836 * except that the low bit doesn't indicate Thumb/not. 6837 */ 6838 env->regs[14] = nextinst; 6839 env->thumb = 1; 6840 env->regs[15] = dest & ~1; 6841 return; 6842 } 6843 6844 /* Target is non-secure: first push a stack frame */ 6845 if (!QEMU_IS_ALIGNED(sp, 8)) { 6846 qemu_log_mask(LOG_GUEST_ERROR, 6847 "BLXNS with misaligned SP is UNPREDICTABLE\n"); 6848 } 6849 6850 if (sp < v7m_sp_limit(env)) { 6851 raise_exception(env, EXCP_STKOF, 0, 1); 6852 } 6853 6854 saved_psr = env->v7m.exception; 6855 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) { 6856 saved_psr |= XPSR_SFPA; 6857 } 6858 6859 /* Note that these stores can throw exceptions on MPU faults */ 6860 cpu_stl_data(env, sp, nextinst); 6861 cpu_stl_data(env, sp + 4, saved_psr); 6862 6863 env->regs[13] = sp; 6864 env->regs[14] = 0xfeffffff; 6865 if (arm_v7m_is_handler_mode(env)) { 6866 /* Write a dummy value to IPSR, to avoid leaking the current secure 6867 * exception number to non-secure code. This is guaranteed not 6868 * to cause write_v7m_exception() to actually change stacks. 6869 */ 6870 write_v7m_exception(env, 1); 6871 } 6872 switch_v7m_security_state(env, 0); 6873 env->thumb = 1; 6874 env->regs[15] = dest; 6875 } 6876 6877 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode, 6878 bool spsel) 6879 { 6880 /* Return a pointer to the location where we currently store the 6881 * stack pointer for the requested security state and thread mode. 6882 * This pointer will become invalid if the CPU state is updated 6883 * such that the stack pointers are switched around (eg changing 6884 * the SPSEL control bit). 6885 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode(). 6886 * Unlike that pseudocode, we require the caller to pass us in the 6887 * SPSEL control bit value; this is because we also use this 6888 * function in handling of pushing of the callee-saves registers 6889 * part of the v8M stack frame (pseudocode PushCalleeStack()), 6890 * and in the tailchain codepath the SPSEL bit comes from the exception 6891 * return magic LR value from the previous exception. The pseudocode 6892 * opencodes the stack-selection in PushCalleeStack(), but we prefer 6893 * to make this utility function generic enough to do the job. 6894 */ 6895 bool want_psp = threadmode && spsel; 6896 6897 if (secure == env->v7m.secure) { 6898 if (want_psp == v7m_using_psp(env)) { 6899 return &env->regs[13]; 6900 } else { 6901 return &env->v7m.other_sp; 6902 } 6903 } else { 6904 if (want_psp) { 6905 return &env->v7m.other_ss_psp; 6906 } else { 6907 return &env->v7m.other_ss_msp; 6908 } 6909 } 6910 } 6911 6912 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure, 6913 uint32_t *pvec) 6914 { 6915 CPUState *cs = CPU(cpu); 6916 CPUARMState *env = &cpu->env; 6917 MemTxResult result; 6918 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4; 6919 uint32_t vector_entry; 6920 MemTxAttrs attrs = {}; 6921 ARMMMUIdx mmu_idx; 6922 bool exc_secure; 6923 6924 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true); 6925 6926 /* We don't do a get_phys_addr() here because the rules for vector 6927 * loads are special: they always use the default memory map, and 6928 * the default memory map permits reads from all addresses. 6929 * Since there's no easy way to pass through to pmsav8_mpu_lookup() 6930 * that we want this special case which would always say "yes", 6931 * we just do the SAU lookup here followed by a direct physical load. 6932 */ 6933 attrs.secure = targets_secure; 6934 attrs.user = false; 6935 6936 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 6937 V8M_SAttributes sattrs = {}; 6938 6939 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); 6940 if (sattrs.ns) { 6941 attrs.secure = false; 6942 } else if (!targets_secure) { 6943 /* NS access to S memory */ 6944 goto load_fail; 6945 } 6946 } 6947 6948 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr, 6949 attrs, &result); 6950 if (result != MEMTX_OK) { 6951 goto load_fail; 6952 } 6953 *pvec = vector_entry; 6954 return true; 6955 6956 load_fail: 6957 /* All vector table fetch fails are reported as HardFault, with 6958 * HFSR.VECTTBL and .FORCED set. (FORCED is set because 6959 * technically the underlying exception is a MemManage or BusFault 6960 * that is escalated to HardFault.) This is a terminal exception, 6961 * so we will either take the HardFault immediately or else enter 6962 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()). 6963 */ 6964 exc_secure = targets_secure || 6965 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK); 6966 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK; 6967 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure); 6968 return false; 6969 } 6970 6971 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain, 6972 bool ignore_faults) 6973 { 6974 /* For v8M, push the callee-saves register part of the stack frame. 6975 * Compare the v8M pseudocode PushCalleeStack(). 6976 * In the tailchaining case this may not be the current stack. 6977 */ 6978 CPUARMState *env = &cpu->env; 6979 uint32_t *frame_sp_p; 6980 uint32_t frameptr; 6981 ARMMMUIdx mmu_idx; 6982 bool stacked_ok; 6983 uint32_t limit; 6984 bool want_psp; 6985 6986 if (dotailchain) { 6987 bool mode = lr & R_V7M_EXCRET_MODE_MASK; 6988 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) || 6989 !mode; 6990 6991 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv); 6992 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode, 6993 lr & R_V7M_EXCRET_SPSEL_MASK); 6994 want_psp = mode && (lr & R_V7M_EXCRET_SPSEL_MASK); 6995 if (want_psp) { 6996 limit = env->v7m.psplim[M_REG_S]; 6997 } else { 6998 limit = env->v7m.msplim[M_REG_S]; 6999 } 7000 } else { 7001 mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false)); 7002 frame_sp_p = &env->regs[13]; 7003 limit = v7m_sp_limit(env); 7004 } 7005 7006 frameptr = *frame_sp_p - 0x28; 7007 if (frameptr < limit) { 7008 /* 7009 * Stack limit failure: set SP to the limit value, and generate 7010 * STKOF UsageFault. Stack pushes below the limit must not be 7011 * performed. It is IMPDEF whether pushes above the limit are 7012 * performed; we choose not to. 7013 */ 7014 qemu_log_mask(CPU_LOG_INT, 7015 "...STKOF during callee-saves register stacking\n"); 7016 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 7017 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 7018 env->v7m.secure); 7019 *frame_sp_p = limit; 7020 return true; 7021 } 7022 7023 /* Write as much of the stack frame as we can. A write failure may 7024 * cause us to pend a derived exception. 7025 */ 7026 stacked_ok = 7027 v7m_stack_write(cpu, frameptr, 0xfefa125b, mmu_idx, ignore_faults) && 7028 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx, 7029 ignore_faults) && 7030 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx, 7031 ignore_faults) && 7032 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx, 7033 ignore_faults) && 7034 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx, 7035 ignore_faults) && 7036 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx, 7037 ignore_faults) && 7038 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx, 7039 ignore_faults) && 7040 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx, 7041 ignore_faults) && 7042 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx, 7043 ignore_faults); 7044 7045 /* Update SP regardless of whether any of the stack accesses failed. */ 7046 *frame_sp_p = frameptr; 7047 7048 return !stacked_ok; 7049 } 7050 7051 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain, 7052 bool ignore_stackfaults) 7053 { 7054 /* Do the "take the exception" parts of exception entry, 7055 * but not the pushing of state to the stack. This is 7056 * similar to the pseudocode ExceptionTaken() function. 7057 */ 7058 CPUARMState *env = &cpu->env; 7059 uint32_t addr; 7060 bool targets_secure; 7061 int exc; 7062 bool push_failed = false; 7063 7064 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure); 7065 qemu_log_mask(CPU_LOG_INT, "...taking pending %s exception %d\n", 7066 targets_secure ? "secure" : "nonsecure", exc); 7067 7068 if (arm_feature(env, ARM_FEATURE_V8)) { 7069 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 7070 (lr & R_V7M_EXCRET_S_MASK)) { 7071 /* The background code (the owner of the registers in the 7072 * exception frame) is Secure. This means it may either already 7073 * have or now needs to push callee-saves registers. 7074 */ 7075 if (targets_secure) { 7076 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) { 7077 /* We took an exception from Secure to NonSecure 7078 * (which means the callee-saved registers got stacked) 7079 * and are now tailchaining to a Secure exception. 7080 * Clear DCRS so eventual return from this Secure 7081 * exception unstacks the callee-saved registers. 7082 */ 7083 lr &= ~R_V7M_EXCRET_DCRS_MASK; 7084 } 7085 } else { 7086 /* We're going to a non-secure exception; push the 7087 * callee-saves registers to the stack now, if they're 7088 * not already saved. 7089 */ 7090 if (lr & R_V7M_EXCRET_DCRS_MASK && 7091 !(dotailchain && !(lr & R_V7M_EXCRET_ES_MASK))) { 7092 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain, 7093 ignore_stackfaults); 7094 } 7095 lr |= R_V7M_EXCRET_DCRS_MASK; 7096 } 7097 } 7098 7099 lr &= ~R_V7M_EXCRET_ES_MASK; 7100 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7101 lr |= R_V7M_EXCRET_ES_MASK; 7102 } 7103 lr &= ~R_V7M_EXCRET_SPSEL_MASK; 7104 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) { 7105 lr |= R_V7M_EXCRET_SPSEL_MASK; 7106 } 7107 7108 /* Clear registers if necessary to prevent non-secure exception 7109 * code being able to see register values from secure code. 7110 * Where register values become architecturally UNKNOWN we leave 7111 * them with their previous values. 7112 */ 7113 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7114 if (!targets_secure) { 7115 /* Always clear the caller-saved registers (they have been 7116 * pushed to the stack earlier in v7m_push_stack()). 7117 * Clear callee-saved registers if the background code is 7118 * Secure (in which case these regs were saved in 7119 * v7m_push_callee_stack()). 7120 */ 7121 int i; 7122 7123 for (i = 0; i < 13; i++) { 7124 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */ 7125 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) { 7126 env->regs[i] = 0; 7127 } 7128 } 7129 /* Clear EAPSR */ 7130 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT); 7131 } 7132 } 7133 } 7134 7135 if (push_failed && !ignore_stackfaults) { 7136 /* Derived exception on callee-saves register stacking: 7137 * we might now want to take a different exception which 7138 * targets a different security state, so try again from the top. 7139 */ 7140 qemu_log_mask(CPU_LOG_INT, 7141 "...derived exception on callee-saves register stacking"); 7142 v7m_exception_taken(cpu, lr, true, true); 7143 return; 7144 } 7145 7146 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) { 7147 /* Vector load failed: derived exception */ 7148 qemu_log_mask(CPU_LOG_INT, "...derived exception on vector table load"); 7149 v7m_exception_taken(cpu, lr, true, true); 7150 return; 7151 } 7152 7153 /* Now we've done everything that might cause a derived exception 7154 * we can go ahead and activate whichever exception we're going to 7155 * take (which might now be the derived exception). 7156 */ 7157 armv7m_nvic_acknowledge_irq(env->nvic); 7158 7159 /* Switch to target security state -- must do this before writing SPSEL */ 7160 switch_v7m_security_state(env, targets_secure); 7161 write_v7m_control_spsel(env, 0); 7162 arm_clear_exclusive(env); 7163 /* Clear IT bits */ 7164 env->condexec_bits = 0; 7165 env->regs[14] = lr; 7166 env->regs[15] = addr & 0xfffffffe; 7167 env->thumb = addr & 1; 7168 } 7169 7170 static bool v7m_push_stack(ARMCPU *cpu) 7171 { 7172 /* Do the "set up stack frame" part of exception entry, 7173 * similar to pseudocode PushStack(). 7174 * Return true if we generate a derived exception (and so 7175 * should ignore further stack faults trying to process 7176 * that derived exception.) 7177 */ 7178 bool stacked_ok; 7179 CPUARMState *env = &cpu->env; 7180 uint32_t xpsr = xpsr_read(env); 7181 uint32_t frameptr = env->regs[13]; 7182 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false)); 7183 7184 /* Align stack pointer if the guest wants that */ 7185 if ((frameptr & 4) && 7186 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) { 7187 frameptr -= 4; 7188 xpsr |= XPSR_SPREALIGN; 7189 } 7190 7191 frameptr -= 0x20; 7192 7193 if (arm_feature(env, ARM_FEATURE_V8)) { 7194 uint32_t limit = v7m_sp_limit(env); 7195 7196 if (frameptr < limit) { 7197 /* 7198 * Stack limit failure: set SP to the limit value, and generate 7199 * STKOF UsageFault. Stack pushes below the limit must not be 7200 * performed. It is IMPDEF whether pushes above the limit are 7201 * performed; we choose not to. 7202 */ 7203 qemu_log_mask(CPU_LOG_INT, 7204 "...STKOF during stacking\n"); 7205 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 7206 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 7207 env->v7m.secure); 7208 env->regs[13] = limit; 7209 return true; 7210 } 7211 } 7212 7213 /* Write as much of the stack frame as we can. If we fail a stack 7214 * write this will result in a derived exception being pended 7215 * (which may be taken in preference to the one we started with 7216 * if it has higher priority). 7217 */ 7218 stacked_ok = 7219 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, false) && 7220 v7m_stack_write(cpu, frameptr + 4, env->regs[1], mmu_idx, false) && 7221 v7m_stack_write(cpu, frameptr + 8, env->regs[2], mmu_idx, false) && 7222 v7m_stack_write(cpu, frameptr + 12, env->regs[3], mmu_idx, false) && 7223 v7m_stack_write(cpu, frameptr + 16, env->regs[12], mmu_idx, false) && 7224 v7m_stack_write(cpu, frameptr + 20, env->regs[14], mmu_idx, false) && 7225 v7m_stack_write(cpu, frameptr + 24, env->regs[15], mmu_idx, false) && 7226 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, false); 7227 7228 /* Update SP regardless of whether any of the stack accesses failed. */ 7229 env->regs[13] = frameptr; 7230 7231 return !stacked_ok; 7232 } 7233 7234 static void do_v7m_exception_exit(ARMCPU *cpu) 7235 { 7236 CPUARMState *env = &cpu->env; 7237 uint32_t excret; 7238 uint32_t xpsr; 7239 bool ufault = false; 7240 bool sfault = false; 7241 bool return_to_sp_process; 7242 bool return_to_handler; 7243 bool rettobase = false; 7244 bool exc_secure = false; 7245 bool return_to_secure; 7246 7247 /* If we're not in Handler mode then jumps to magic exception-exit 7248 * addresses don't have magic behaviour. However for the v8M 7249 * security extensions the magic secure-function-return has to 7250 * work in thread mode too, so to avoid doing an extra check in 7251 * the generated code we allow exception-exit magic to also cause the 7252 * internal exception and bring us here in thread mode. Correct code 7253 * will never try to do this (the following insn fetch will always 7254 * fault) so we the overhead of having taken an unnecessary exception 7255 * doesn't matter. 7256 */ 7257 if (!arm_v7m_is_handler_mode(env)) { 7258 return; 7259 } 7260 7261 /* In the spec pseudocode ExceptionReturn() is called directly 7262 * from BXWritePC() and gets the full target PC value including 7263 * bit zero. In QEMU's implementation we treat it as a normal 7264 * jump-to-register (which is then caught later on), and so split 7265 * the target value up between env->regs[15] and env->thumb in 7266 * gen_bx(). Reconstitute it. 7267 */ 7268 excret = env->regs[15]; 7269 if (env->thumb) { 7270 excret |= 1; 7271 } 7272 7273 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32 7274 " previous exception %d\n", 7275 excret, env->v7m.exception); 7276 7277 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) { 7278 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception " 7279 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n", 7280 excret); 7281 } 7282 7283 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7284 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before 7285 * we pick which FAULTMASK to clear. 7286 */ 7287 if (!env->v7m.secure && 7288 ((excret & R_V7M_EXCRET_ES_MASK) || 7289 !(excret & R_V7M_EXCRET_DCRS_MASK))) { 7290 sfault = 1; 7291 /* For all other purposes, treat ES as 0 (R_HXSR) */ 7292 excret &= ~R_V7M_EXCRET_ES_MASK; 7293 } 7294 exc_secure = excret & R_V7M_EXCRET_ES_MASK; 7295 } 7296 7297 if (env->v7m.exception != ARMV7M_EXCP_NMI) { 7298 /* Auto-clear FAULTMASK on return from other than NMI. 7299 * If the security extension is implemented then this only 7300 * happens if the raw execution priority is >= 0; the 7301 * value of the ES bit in the exception return value indicates 7302 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.) 7303 */ 7304 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7305 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) { 7306 env->v7m.faultmask[exc_secure] = 0; 7307 } 7308 } else { 7309 env->v7m.faultmask[M_REG_NS] = 0; 7310 } 7311 } 7312 7313 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception, 7314 exc_secure)) { 7315 case -1: 7316 /* attempt to exit an exception that isn't active */ 7317 ufault = true; 7318 break; 7319 case 0: 7320 /* still an irq active now */ 7321 break; 7322 case 1: 7323 /* we returned to base exception level, no nesting. 7324 * (In the pseudocode this is written using "NestedActivation != 1" 7325 * where we have 'rettobase == false'.) 7326 */ 7327 rettobase = true; 7328 break; 7329 default: 7330 g_assert_not_reached(); 7331 } 7332 7333 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK); 7334 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK; 7335 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) && 7336 (excret & R_V7M_EXCRET_S_MASK); 7337 7338 if (arm_feature(env, ARM_FEATURE_V8)) { 7339 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7340 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP); 7341 * we choose to take the UsageFault. 7342 */ 7343 if ((excret & R_V7M_EXCRET_S_MASK) || 7344 (excret & R_V7M_EXCRET_ES_MASK) || 7345 !(excret & R_V7M_EXCRET_DCRS_MASK)) { 7346 ufault = true; 7347 } 7348 } 7349 if (excret & R_V7M_EXCRET_RES0_MASK) { 7350 ufault = true; 7351 } 7352 } else { 7353 /* For v7M we only recognize certain combinations of the low bits */ 7354 switch (excret & 0xf) { 7355 case 1: /* Return to Handler */ 7356 break; 7357 case 13: /* Return to Thread using Process stack */ 7358 case 9: /* Return to Thread using Main stack */ 7359 /* We only need to check NONBASETHRDENA for v7M, because in 7360 * v8M this bit does not exist (it is RES1). 7361 */ 7362 if (!rettobase && 7363 !(env->v7m.ccr[env->v7m.secure] & 7364 R_V7M_CCR_NONBASETHRDENA_MASK)) { 7365 ufault = true; 7366 } 7367 break; 7368 default: 7369 ufault = true; 7370 } 7371 } 7372 7373 /* 7374 * Set CONTROL.SPSEL from excret.SPSEL. Since we're still in 7375 * Handler mode (and will be until we write the new XPSR.Interrupt 7376 * field) this does not switch around the current stack pointer. 7377 * We must do this before we do any kind of tailchaining, including 7378 * for the derived exceptions on integrity check failures, or we will 7379 * give the guest an incorrect EXCRET.SPSEL value on exception entry. 7380 */ 7381 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure); 7382 7383 if (sfault) { 7384 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK; 7385 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7386 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 7387 "stackframe: failed EXC_RETURN.ES validity check\n"); 7388 v7m_exception_taken(cpu, excret, true, false); 7389 return; 7390 } 7391 7392 if (ufault) { 7393 /* Bad exception return: instead of popping the exception 7394 * stack, directly take a usage fault on the current stack. 7395 */ 7396 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 7397 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7398 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " 7399 "stackframe: failed exception return integrity check\n"); 7400 v7m_exception_taken(cpu, excret, true, false); 7401 return; 7402 } 7403 7404 /* 7405 * Tailchaining: if there is currently a pending exception that 7406 * is high enough priority to preempt execution at the level we're 7407 * about to return to, then just directly take that exception now, 7408 * avoiding an unstack-and-then-stack. Note that now we have 7409 * deactivated the previous exception by calling armv7m_nvic_complete_irq() 7410 * our current execution priority is already the execution priority we are 7411 * returning to -- none of the state we would unstack or set based on 7412 * the EXCRET value affects it. 7413 */ 7414 if (armv7m_nvic_can_take_pending_exception(env->nvic)) { 7415 qemu_log_mask(CPU_LOG_INT, "...tailchaining to pending exception\n"); 7416 v7m_exception_taken(cpu, excret, true, false); 7417 return; 7418 } 7419 7420 switch_v7m_security_state(env, return_to_secure); 7421 7422 { 7423 /* The stack pointer we should be reading the exception frame from 7424 * depends on bits in the magic exception return type value (and 7425 * for v8M isn't necessarily the stack pointer we will eventually 7426 * end up resuming execution with). Get a pointer to the location 7427 * in the CPU state struct where the SP we need is currently being 7428 * stored; we will use and modify it in place. 7429 * We use this limited C variable scope so we don't accidentally 7430 * use 'frame_sp_p' after we do something that makes it invalid. 7431 */ 7432 uint32_t *frame_sp_p = get_v7m_sp_ptr(env, 7433 return_to_secure, 7434 !return_to_handler, 7435 return_to_sp_process); 7436 uint32_t frameptr = *frame_sp_p; 7437 bool pop_ok = true; 7438 ARMMMUIdx mmu_idx; 7439 bool return_to_priv = return_to_handler || 7440 !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK); 7441 7442 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure, 7443 return_to_priv); 7444 7445 if (!QEMU_IS_ALIGNED(frameptr, 8) && 7446 arm_feature(env, ARM_FEATURE_V8)) { 7447 qemu_log_mask(LOG_GUEST_ERROR, 7448 "M profile exception return with non-8-aligned SP " 7449 "for destination state is UNPREDICTABLE\n"); 7450 } 7451 7452 /* Do we need to pop callee-saved registers? */ 7453 if (return_to_secure && 7454 ((excret & R_V7M_EXCRET_ES_MASK) == 0 || 7455 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) { 7456 uint32_t expected_sig = 0xfefa125b; 7457 uint32_t actual_sig; 7458 7459 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx); 7460 7461 if (pop_ok && expected_sig != actual_sig) { 7462 /* Take a SecureFault on the current stack */ 7463 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK; 7464 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7465 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 7466 "stackframe: failed exception return integrity " 7467 "signature check\n"); 7468 v7m_exception_taken(cpu, excret, true, false); 7469 return; 7470 } 7471 7472 pop_ok = pop_ok && 7473 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) && 7474 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) && 7475 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) && 7476 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) && 7477 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) && 7478 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) && 7479 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) && 7480 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx); 7481 7482 frameptr += 0x28; 7483 } 7484 7485 /* Pop registers */ 7486 pop_ok = pop_ok && 7487 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) && 7488 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) && 7489 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) && 7490 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) && 7491 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) && 7492 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) && 7493 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) && 7494 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx); 7495 7496 if (!pop_ok) { 7497 /* v7m_stack_read() pended a fault, so take it (as a tail 7498 * chained exception on the same stack frame) 7499 */ 7500 qemu_log_mask(CPU_LOG_INT, "...derived exception on unstacking\n"); 7501 v7m_exception_taken(cpu, excret, true, false); 7502 return; 7503 } 7504 7505 /* Returning from an exception with a PC with bit 0 set is defined 7506 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified 7507 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore 7508 * the lsbit, and there are several RTOSes out there which incorrectly 7509 * assume the r15 in the stack frame should be a Thumb-style "lsbit 7510 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but 7511 * complain about the badly behaved guest. 7512 */ 7513 if (env->regs[15] & 1) { 7514 env->regs[15] &= ~1U; 7515 if (!arm_feature(env, ARM_FEATURE_V8)) { 7516 qemu_log_mask(LOG_GUEST_ERROR, 7517 "M profile return from interrupt with misaligned " 7518 "PC is UNPREDICTABLE on v7M\n"); 7519 } 7520 } 7521 7522 if (arm_feature(env, ARM_FEATURE_V8)) { 7523 /* For v8M we have to check whether the xPSR exception field 7524 * matches the EXCRET value for return to handler/thread 7525 * before we commit to changing the SP and xPSR. 7526 */ 7527 bool will_be_handler = (xpsr & XPSR_EXCP) != 0; 7528 if (return_to_handler != will_be_handler) { 7529 /* Take an INVPC UsageFault on the current stack. 7530 * By this point we will have switched to the security state 7531 * for the background state, so this UsageFault will target 7532 * that state. 7533 */ 7534 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 7535 env->v7m.secure); 7536 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 7537 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " 7538 "stackframe: failed exception return integrity " 7539 "check\n"); 7540 v7m_exception_taken(cpu, excret, true, false); 7541 return; 7542 } 7543 } 7544 7545 /* Commit to consuming the stack frame */ 7546 frameptr += 0x20; 7547 /* Undo stack alignment (the SPREALIGN bit indicates that the original 7548 * pre-exception SP was not 8-aligned and we added a padding word to 7549 * align it, so we undo this by ORing in the bit that increases it 7550 * from the current 8-aligned value to the 8-unaligned value. (Adding 4 7551 * would work too but a logical OR is how the pseudocode specifies it.) 7552 */ 7553 if (xpsr & XPSR_SPREALIGN) { 7554 frameptr |= 4; 7555 } 7556 *frame_sp_p = frameptr; 7557 } 7558 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */ 7559 xpsr_write(env, xpsr, ~XPSR_SPREALIGN); 7560 7561 /* The restored xPSR exception field will be zero if we're 7562 * resuming in Thread mode. If that doesn't match what the 7563 * exception return excret specified then this is a UsageFault. 7564 * v7M requires we make this check here; v8M did it earlier. 7565 */ 7566 if (return_to_handler != arm_v7m_is_handler_mode(env)) { 7567 /* Take an INVPC UsageFault by pushing the stack again; 7568 * we know we're v7M so this is never a Secure UsageFault. 7569 */ 7570 bool ignore_stackfaults; 7571 7572 assert(!arm_feature(env, ARM_FEATURE_V8)); 7573 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false); 7574 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 7575 ignore_stackfaults = v7m_push_stack(cpu); 7576 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: " 7577 "failed exception return integrity check\n"); 7578 v7m_exception_taken(cpu, excret, false, ignore_stackfaults); 7579 return; 7580 } 7581 7582 /* Otherwise, we have a successful exception exit. */ 7583 arm_clear_exclusive(env); 7584 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n"); 7585 } 7586 7587 static bool do_v7m_function_return(ARMCPU *cpu) 7588 { 7589 /* v8M security extensions magic function return. 7590 * We may either: 7591 * (1) throw an exception (longjump) 7592 * (2) return true if we successfully handled the function return 7593 * (3) return false if we failed a consistency check and have 7594 * pended a UsageFault that needs to be taken now 7595 * 7596 * At this point the magic return value is split between env->regs[15] 7597 * and env->thumb. We don't bother to reconstitute it because we don't 7598 * need it (all values are handled the same way). 7599 */ 7600 CPUARMState *env = &cpu->env; 7601 uint32_t newpc, newpsr, newpsr_exc; 7602 7603 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n"); 7604 7605 { 7606 bool threadmode, spsel; 7607 TCGMemOpIdx oi; 7608 ARMMMUIdx mmu_idx; 7609 uint32_t *frame_sp_p; 7610 uint32_t frameptr; 7611 7612 /* Pull the return address and IPSR from the Secure stack */ 7613 threadmode = !arm_v7m_is_handler_mode(env); 7614 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK; 7615 7616 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel); 7617 frameptr = *frame_sp_p; 7618 7619 /* These loads may throw an exception (for MPU faults). We want to 7620 * do them as secure, so work out what MMU index that is. 7621 */ 7622 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); 7623 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx)); 7624 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0); 7625 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0); 7626 7627 /* Consistency checks on new IPSR */ 7628 newpsr_exc = newpsr & XPSR_EXCP; 7629 if (!((env->v7m.exception == 0 && newpsr_exc == 0) || 7630 (env->v7m.exception == 1 && newpsr_exc != 0))) { 7631 /* Pend the fault and tell our caller to take it */ 7632 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 7633 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 7634 env->v7m.secure); 7635 qemu_log_mask(CPU_LOG_INT, 7636 "...taking INVPC UsageFault: " 7637 "IPSR consistency check failed\n"); 7638 return false; 7639 } 7640 7641 *frame_sp_p = frameptr + 8; 7642 } 7643 7644 /* This invalidates frame_sp_p */ 7645 switch_v7m_security_state(env, true); 7646 env->v7m.exception = newpsr_exc; 7647 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 7648 if (newpsr & XPSR_SFPA) { 7649 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK; 7650 } 7651 xpsr_write(env, 0, XPSR_IT); 7652 env->thumb = newpc & 1; 7653 env->regs[15] = newpc & ~1; 7654 7655 qemu_log_mask(CPU_LOG_INT, "...function return successful\n"); 7656 return true; 7657 } 7658 7659 static void arm_log_exception(int idx) 7660 { 7661 if (qemu_loglevel_mask(CPU_LOG_INT)) { 7662 const char *exc = NULL; 7663 static const char * const excnames[] = { 7664 [EXCP_UDEF] = "Undefined Instruction", 7665 [EXCP_SWI] = "SVC", 7666 [EXCP_PREFETCH_ABORT] = "Prefetch Abort", 7667 [EXCP_DATA_ABORT] = "Data Abort", 7668 [EXCP_IRQ] = "IRQ", 7669 [EXCP_FIQ] = "FIQ", 7670 [EXCP_BKPT] = "Breakpoint", 7671 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit", 7672 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage", 7673 [EXCP_HVC] = "Hypervisor Call", 7674 [EXCP_HYP_TRAP] = "Hypervisor Trap", 7675 [EXCP_SMC] = "Secure Monitor Call", 7676 [EXCP_VIRQ] = "Virtual IRQ", 7677 [EXCP_VFIQ] = "Virtual FIQ", 7678 [EXCP_SEMIHOST] = "Semihosting call", 7679 [EXCP_NOCP] = "v7M NOCP UsageFault", 7680 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault", 7681 [EXCP_STKOF] = "v8M STKOF UsageFault", 7682 }; 7683 7684 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { 7685 exc = excnames[idx]; 7686 } 7687 if (!exc) { 7688 exc = "unknown"; 7689 } 7690 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc); 7691 } 7692 } 7693 7694 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx, 7695 uint32_t addr, uint16_t *insn) 7696 { 7697 /* Load a 16-bit portion of a v7M instruction, returning true on success, 7698 * or false on failure (in which case we will have pended the appropriate 7699 * exception). 7700 * We need to do the instruction fetch's MPU and SAU checks 7701 * like this because there is no MMU index that would allow 7702 * doing the load with a single function call. Instead we must 7703 * first check that the security attributes permit the load 7704 * and that they don't mismatch on the two halves of the instruction, 7705 * and then we do the load as a secure load (ie using the security 7706 * attributes of the address, not the CPU, as architecturally required). 7707 */ 7708 CPUState *cs = CPU(cpu); 7709 CPUARMState *env = &cpu->env; 7710 V8M_SAttributes sattrs = {}; 7711 MemTxAttrs attrs = {}; 7712 ARMMMUFaultInfo fi = {}; 7713 MemTxResult txres; 7714 target_ulong page_size; 7715 hwaddr physaddr; 7716 int prot; 7717 7718 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs); 7719 if (!sattrs.nsc || sattrs.ns) { 7720 /* This must be the second half of the insn, and it straddles a 7721 * region boundary with the second half not being S&NSC. 7722 */ 7723 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 7724 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7725 qemu_log_mask(CPU_LOG_INT, 7726 "...really SecureFault with SFSR.INVEP\n"); 7727 return false; 7728 } 7729 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx, 7730 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) { 7731 /* the MPU lookup failed */ 7732 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; 7733 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure); 7734 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n"); 7735 return false; 7736 } 7737 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr, 7738 attrs, &txres); 7739 if (txres != MEMTX_OK) { 7740 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; 7741 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); 7742 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n"); 7743 return false; 7744 } 7745 return true; 7746 } 7747 7748 static bool v7m_handle_execute_nsc(ARMCPU *cpu) 7749 { 7750 /* Check whether this attempt to execute code in a Secure & NS-Callable 7751 * memory region is for an SG instruction; if so, then emulate the 7752 * effect of the SG instruction and return true. Otherwise pend 7753 * the correct kind of exception and return false. 7754 */ 7755 CPUARMState *env = &cpu->env; 7756 ARMMMUIdx mmu_idx; 7757 uint16_t insn; 7758 7759 /* We should never get here unless get_phys_addr_pmsav8() caused 7760 * an exception for NS executing in S&NSC memory. 7761 */ 7762 assert(!env->v7m.secure); 7763 assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); 7764 7765 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */ 7766 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); 7767 7768 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) { 7769 return false; 7770 } 7771 7772 if (!env->thumb) { 7773 goto gen_invep; 7774 } 7775 7776 if (insn != 0xe97f) { 7777 /* Not an SG instruction first half (we choose the IMPDEF 7778 * early-SG-check option). 7779 */ 7780 goto gen_invep; 7781 } 7782 7783 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) { 7784 return false; 7785 } 7786 7787 if (insn != 0xe97f) { 7788 /* Not an SG instruction second half (yes, both halves of the SG 7789 * insn have the same hex value) 7790 */ 7791 goto gen_invep; 7792 } 7793 7794 /* OK, we have confirmed that we really have an SG instruction. 7795 * We know we're NS in S memory so don't need to repeat those checks. 7796 */ 7797 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32 7798 ", executing it\n", env->regs[15]); 7799 env->regs[14] &= ~1; 7800 switch_v7m_security_state(env, true); 7801 xpsr_write(env, 0, XPSR_IT); 7802 env->regs[15] += 4; 7803 return true; 7804 7805 gen_invep: 7806 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 7807 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7808 qemu_log_mask(CPU_LOG_INT, 7809 "...really SecureFault with SFSR.INVEP\n"); 7810 return false; 7811 } 7812 7813 void arm_v7m_cpu_do_interrupt(CPUState *cs) 7814 { 7815 ARMCPU *cpu = ARM_CPU(cs); 7816 CPUARMState *env = &cpu->env; 7817 uint32_t lr; 7818 bool ignore_stackfaults; 7819 7820 arm_log_exception(cs->exception_index); 7821 7822 /* For exceptions we just mark as pending on the NVIC, and let that 7823 handle it. */ 7824 switch (cs->exception_index) { 7825 case EXCP_UDEF: 7826 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7827 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK; 7828 break; 7829 case EXCP_NOCP: 7830 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7831 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK; 7832 break; 7833 case EXCP_INVSTATE: 7834 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7835 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK; 7836 break; 7837 case EXCP_STKOF: 7838 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 7839 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 7840 break; 7841 case EXCP_SWI: 7842 /* The PC already points to the next instruction. */ 7843 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure); 7844 break; 7845 case EXCP_PREFETCH_ABORT: 7846 case EXCP_DATA_ABORT: 7847 /* Note that for M profile we don't have a guest facing FSR, but 7848 * the env->exception.fsr will be populated by the code that 7849 * raises the fault, in the A profile short-descriptor format. 7850 */ 7851 switch (env->exception.fsr & 0xf) { 7852 case M_FAKE_FSR_NSC_EXEC: 7853 /* Exception generated when we try to execute code at an address 7854 * which is marked as Secure & Non-Secure Callable and the CPU 7855 * is in the Non-Secure state. The only instruction which can 7856 * be executed like this is SG (and that only if both halves of 7857 * the SG instruction have the same security attributes.) 7858 * Everything else must generate an INVEP SecureFault, so we 7859 * emulate the SG instruction here. 7860 */ 7861 if (v7m_handle_execute_nsc(cpu)) { 7862 return; 7863 } 7864 break; 7865 case M_FAKE_FSR_SFAULT: 7866 /* Various flavours of SecureFault for attempts to execute or 7867 * access data in the wrong security state. 7868 */ 7869 switch (cs->exception_index) { 7870 case EXCP_PREFETCH_ABORT: 7871 if (env->v7m.secure) { 7872 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK; 7873 qemu_log_mask(CPU_LOG_INT, 7874 "...really SecureFault with SFSR.INVTRAN\n"); 7875 } else { 7876 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 7877 qemu_log_mask(CPU_LOG_INT, 7878 "...really SecureFault with SFSR.INVEP\n"); 7879 } 7880 break; 7881 case EXCP_DATA_ABORT: 7882 /* This must be an NS access to S memory */ 7883 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK; 7884 qemu_log_mask(CPU_LOG_INT, 7885 "...really SecureFault with SFSR.AUVIOL\n"); 7886 break; 7887 } 7888 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 7889 break; 7890 case 0x8: /* External Abort */ 7891 switch (cs->exception_index) { 7892 case EXCP_PREFETCH_ABORT: 7893 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; 7894 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n"); 7895 break; 7896 case EXCP_DATA_ABORT: 7897 env->v7m.cfsr[M_REG_NS] |= 7898 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK); 7899 env->v7m.bfar = env->exception.vaddress; 7900 qemu_log_mask(CPU_LOG_INT, 7901 "...with CFSR.PRECISERR and BFAR 0x%x\n", 7902 env->v7m.bfar); 7903 break; 7904 } 7905 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); 7906 break; 7907 default: 7908 /* All other FSR values are either MPU faults or "can't happen 7909 * for M profile" cases. 7910 */ 7911 switch (cs->exception_index) { 7912 case EXCP_PREFETCH_ABORT: 7913 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; 7914 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n"); 7915 break; 7916 case EXCP_DATA_ABORT: 7917 env->v7m.cfsr[env->v7m.secure] |= 7918 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK); 7919 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress; 7920 qemu_log_mask(CPU_LOG_INT, 7921 "...with CFSR.DACCVIOL and MMFAR 0x%x\n", 7922 env->v7m.mmfar[env->v7m.secure]); 7923 break; 7924 } 7925 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, 7926 env->v7m.secure); 7927 break; 7928 } 7929 break; 7930 case EXCP_BKPT: 7931 if (semihosting_enabled()) { 7932 int nr; 7933 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff; 7934 if (nr == 0xab) { 7935 env->regs[15] += 2; 7936 qemu_log_mask(CPU_LOG_INT, 7937 "...handling as semihosting call 0x%x\n", 7938 env->regs[0]); 7939 env->regs[0] = do_arm_semihosting(env); 7940 return; 7941 } 7942 } 7943 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false); 7944 break; 7945 case EXCP_IRQ: 7946 break; 7947 case EXCP_EXCEPTION_EXIT: 7948 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) { 7949 /* Must be v8M security extension function return */ 7950 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC); 7951 assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); 7952 if (do_v7m_function_return(cpu)) { 7953 return; 7954 } 7955 } else { 7956 do_v7m_exception_exit(cpu); 7957 return; 7958 } 7959 break; 7960 default: 7961 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 7962 return; /* Never happens. Keep compiler happy. */ 7963 } 7964 7965 if (arm_feature(env, ARM_FEATURE_V8)) { 7966 lr = R_V7M_EXCRET_RES1_MASK | 7967 R_V7M_EXCRET_DCRS_MASK | 7968 R_V7M_EXCRET_FTYPE_MASK; 7969 /* The S bit indicates whether we should return to Secure 7970 * or NonSecure (ie our current state). 7971 * The ES bit indicates whether we're taking this exception 7972 * to Secure or NonSecure (ie our target state). We set it 7973 * later, in v7m_exception_taken(). 7974 * The SPSEL bit is also set in v7m_exception_taken() for v8M. 7975 * This corresponds to the ARM ARM pseudocode for v8M setting 7976 * some LR bits in PushStack() and some in ExceptionTaken(); 7977 * the distinction matters for the tailchain cases where we 7978 * can take an exception without pushing the stack. 7979 */ 7980 if (env->v7m.secure) { 7981 lr |= R_V7M_EXCRET_S_MASK; 7982 } 7983 } else { 7984 lr = R_V7M_EXCRET_RES1_MASK | 7985 R_V7M_EXCRET_S_MASK | 7986 R_V7M_EXCRET_DCRS_MASK | 7987 R_V7M_EXCRET_FTYPE_MASK | 7988 R_V7M_EXCRET_ES_MASK; 7989 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) { 7990 lr |= R_V7M_EXCRET_SPSEL_MASK; 7991 } 7992 } 7993 if (!arm_v7m_is_handler_mode(env)) { 7994 lr |= R_V7M_EXCRET_MODE_MASK; 7995 } 7996 7997 ignore_stackfaults = v7m_push_stack(cpu); 7998 v7m_exception_taken(cpu, lr, false, ignore_stackfaults); 7999 } 8000 8001 /* Function used to synchronize QEMU's AArch64 register set with AArch32 8002 * register set. This is necessary when switching between AArch32 and AArch64 8003 * execution state. 8004 */ 8005 void aarch64_sync_32_to_64(CPUARMState *env) 8006 { 8007 int i; 8008 uint32_t mode = env->uncached_cpsr & CPSR_M; 8009 8010 /* We can blanket copy R[0:7] to X[0:7] */ 8011 for (i = 0; i < 8; i++) { 8012 env->xregs[i] = env->regs[i]; 8013 } 8014 8015 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12. 8016 * Otherwise, they come from the banked user regs. 8017 */ 8018 if (mode == ARM_CPU_MODE_FIQ) { 8019 for (i = 8; i < 13; i++) { 8020 env->xregs[i] = env->usr_regs[i - 8]; 8021 } 8022 } else { 8023 for (i = 8; i < 13; i++) { 8024 env->xregs[i] = env->regs[i]; 8025 } 8026 } 8027 8028 /* Registers x13-x23 are the various mode SP and FP registers. Registers 8029 * r13 and r14 are only copied if we are in that mode, otherwise we copy 8030 * from the mode banked register. 8031 */ 8032 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 8033 env->xregs[13] = env->regs[13]; 8034 env->xregs[14] = env->regs[14]; 8035 } else { 8036 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)]; 8037 /* HYP is an exception in that it is copied from r14 */ 8038 if (mode == ARM_CPU_MODE_HYP) { 8039 env->xregs[14] = env->regs[14]; 8040 } else { 8041 env->xregs[14] = env->banked_r14[bank_number(ARM_CPU_MODE_USR)]; 8042 } 8043 } 8044 8045 if (mode == ARM_CPU_MODE_HYP) { 8046 env->xregs[15] = env->regs[13]; 8047 } else { 8048 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)]; 8049 } 8050 8051 if (mode == ARM_CPU_MODE_IRQ) { 8052 env->xregs[16] = env->regs[14]; 8053 env->xregs[17] = env->regs[13]; 8054 } else { 8055 env->xregs[16] = env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)]; 8056 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)]; 8057 } 8058 8059 if (mode == ARM_CPU_MODE_SVC) { 8060 env->xregs[18] = env->regs[14]; 8061 env->xregs[19] = env->regs[13]; 8062 } else { 8063 env->xregs[18] = env->banked_r14[bank_number(ARM_CPU_MODE_SVC)]; 8064 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)]; 8065 } 8066 8067 if (mode == ARM_CPU_MODE_ABT) { 8068 env->xregs[20] = env->regs[14]; 8069 env->xregs[21] = env->regs[13]; 8070 } else { 8071 env->xregs[20] = env->banked_r14[bank_number(ARM_CPU_MODE_ABT)]; 8072 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)]; 8073 } 8074 8075 if (mode == ARM_CPU_MODE_UND) { 8076 env->xregs[22] = env->regs[14]; 8077 env->xregs[23] = env->regs[13]; 8078 } else { 8079 env->xregs[22] = env->banked_r14[bank_number(ARM_CPU_MODE_UND)]; 8080 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)]; 8081 } 8082 8083 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 8084 * mode, then we can copy from r8-r14. Otherwise, we copy from the 8085 * FIQ bank for r8-r14. 8086 */ 8087 if (mode == ARM_CPU_MODE_FIQ) { 8088 for (i = 24; i < 31; i++) { 8089 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */ 8090 } 8091 } else { 8092 for (i = 24; i < 29; i++) { 8093 env->xregs[i] = env->fiq_regs[i - 24]; 8094 } 8095 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)]; 8096 env->xregs[30] = env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)]; 8097 } 8098 8099 env->pc = env->regs[15]; 8100 } 8101 8102 /* Function used to synchronize QEMU's AArch32 register set with AArch64 8103 * register set. This is necessary when switching between AArch32 and AArch64 8104 * execution state. 8105 */ 8106 void aarch64_sync_64_to_32(CPUARMState *env) 8107 { 8108 int i; 8109 uint32_t mode = env->uncached_cpsr & CPSR_M; 8110 8111 /* We can blanket copy X[0:7] to R[0:7] */ 8112 for (i = 0; i < 8; i++) { 8113 env->regs[i] = env->xregs[i]; 8114 } 8115 8116 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12. 8117 * Otherwise, we copy x8-x12 into the banked user regs. 8118 */ 8119 if (mode == ARM_CPU_MODE_FIQ) { 8120 for (i = 8; i < 13; i++) { 8121 env->usr_regs[i - 8] = env->xregs[i]; 8122 } 8123 } else { 8124 for (i = 8; i < 13; i++) { 8125 env->regs[i] = env->xregs[i]; 8126 } 8127 } 8128 8129 /* Registers r13 & r14 depend on the current mode. 8130 * If we are in a given mode, we copy the corresponding x registers to r13 8131 * and r14. Otherwise, we copy the x register to the banked r13 and r14 8132 * for the mode. 8133 */ 8134 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 8135 env->regs[13] = env->xregs[13]; 8136 env->regs[14] = env->xregs[14]; 8137 } else { 8138 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13]; 8139 8140 /* HYP is an exception in that it does not have its own banked r14 but 8141 * shares the USR r14 8142 */ 8143 if (mode == ARM_CPU_MODE_HYP) { 8144 env->regs[14] = env->xregs[14]; 8145 } else { 8146 env->banked_r14[bank_number(ARM_CPU_MODE_USR)] = env->xregs[14]; 8147 } 8148 } 8149 8150 if (mode == ARM_CPU_MODE_HYP) { 8151 env->regs[13] = env->xregs[15]; 8152 } else { 8153 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15]; 8154 } 8155 8156 if (mode == ARM_CPU_MODE_IRQ) { 8157 env->regs[14] = env->xregs[16]; 8158 env->regs[13] = env->xregs[17]; 8159 } else { 8160 env->banked_r14[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16]; 8161 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17]; 8162 } 8163 8164 if (mode == ARM_CPU_MODE_SVC) { 8165 env->regs[14] = env->xregs[18]; 8166 env->regs[13] = env->xregs[19]; 8167 } else { 8168 env->banked_r14[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18]; 8169 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19]; 8170 } 8171 8172 if (mode == ARM_CPU_MODE_ABT) { 8173 env->regs[14] = env->xregs[20]; 8174 env->regs[13] = env->xregs[21]; 8175 } else { 8176 env->banked_r14[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20]; 8177 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21]; 8178 } 8179 8180 if (mode == ARM_CPU_MODE_UND) { 8181 env->regs[14] = env->xregs[22]; 8182 env->regs[13] = env->xregs[23]; 8183 } else { 8184 env->banked_r14[bank_number(ARM_CPU_MODE_UND)] = env->xregs[22]; 8185 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23]; 8186 } 8187 8188 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 8189 * mode, then we can copy to r8-r14. Otherwise, we copy to the 8190 * FIQ bank for r8-r14. 8191 */ 8192 if (mode == ARM_CPU_MODE_FIQ) { 8193 for (i = 24; i < 31; i++) { 8194 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */ 8195 } 8196 } else { 8197 for (i = 24; i < 29; i++) { 8198 env->fiq_regs[i - 24] = env->xregs[i]; 8199 } 8200 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29]; 8201 env->banked_r14[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30]; 8202 } 8203 8204 env->regs[15] = env->pc; 8205 } 8206 8207 static void take_aarch32_exception(CPUARMState *env, int new_mode, 8208 uint32_t mask, uint32_t offset, 8209 uint32_t newpc) 8210 { 8211 /* Change the CPU state so as to actually take the exception. */ 8212 switch_mode(env, new_mode); 8213 /* 8214 * For exceptions taken to AArch32 we must clear the SS bit in both 8215 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. 8216 */ 8217 env->uncached_cpsr &= ~PSTATE_SS; 8218 env->spsr = cpsr_read(env); 8219 /* Clear IT bits. */ 8220 env->condexec_bits = 0; 8221 /* Switch to the new mode, and to the correct instruction set. */ 8222 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; 8223 /* Set new mode endianness */ 8224 env->uncached_cpsr &= ~CPSR_E; 8225 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) { 8226 env->uncached_cpsr |= CPSR_E; 8227 } 8228 /* J and IL must always be cleared for exception entry */ 8229 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J); 8230 env->daif |= mask; 8231 8232 if (new_mode == ARM_CPU_MODE_HYP) { 8233 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0; 8234 env->elr_el[2] = env->regs[15]; 8235 } else { 8236 /* 8237 * this is a lie, as there was no c1_sys on V4T/V5, but who cares 8238 * and we should just guard the thumb mode on V4 8239 */ 8240 if (arm_feature(env, ARM_FEATURE_V4T)) { 8241 env->thumb = 8242 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; 8243 } 8244 env->regs[14] = env->regs[15] + offset; 8245 } 8246 env->regs[15] = newpc; 8247 } 8248 8249 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs) 8250 { 8251 /* 8252 * Handle exception entry to Hyp mode; this is sufficiently 8253 * different to entry to other AArch32 modes that we handle it 8254 * separately here. 8255 * 8256 * The vector table entry used is always the 0x14 Hyp mode entry point, 8257 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp. 8258 * The offset applied to the preferred return address is always zero 8259 * (see DDI0487C.a section G1.12.3). 8260 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values. 8261 */ 8262 uint32_t addr, mask; 8263 ARMCPU *cpu = ARM_CPU(cs); 8264 CPUARMState *env = &cpu->env; 8265 8266 switch (cs->exception_index) { 8267 case EXCP_UDEF: 8268 addr = 0x04; 8269 break; 8270 case EXCP_SWI: 8271 addr = 0x14; 8272 break; 8273 case EXCP_BKPT: 8274 /* Fall through to prefetch abort. */ 8275 case EXCP_PREFETCH_ABORT: 8276 env->cp15.ifar_s = env->exception.vaddress; 8277 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n", 8278 (uint32_t)env->exception.vaddress); 8279 addr = 0x0c; 8280 break; 8281 case EXCP_DATA_ABORT: 8282 env->cp15.dfar_s = env->exception.vaddress; 8283 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n", 8284 (uint32_t)env->exception.vaddress); 8285 addr = 0x10; 8286 break; 8287 case EXCP_IRQ: 8288 addr = 0x18; 8289 break; 8290 case EXCP_FIQ: 8291 addr = 0x1c; 8292 break; 8293 case EXCP_HVC: 8294 addr = 0x08; 8295 break; 8296 case EXCP_HYP_TRAP: 8297 addr = 0x14; 8298 default: 8299 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 8300 } 8301 8302 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) { 8303 if (!arm_feature(env, ARM_FEATURE_V8)) { 8304 /* 8305 * QEMU syndrome values are v8-style. v7 has the IL bit 8306 * UNK/SBZP for "field not valid" cases, where v8 uses RES1. 8307 * If this is a v7 CPU, squash the IL bit in those cases. 8308 */ 8309 if (cs->exception_index == EXCP_PREFETCH_ABORT || 8310 (cs->exception_index == EXCP_DATA_ABORT && 8311 !(env->exception.syndrome & ARM_EL_ISV)) || 8312 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) { 8313 env->exception.syndrome &= ~ARM_EL_IL; 8314 } 8315 } 8316 env->cp15.esr_el[2] = env->exception.syndrome; 8317 } 8318 8319 if (arm_current_el(env) != 2 && addr < 0x14) { 8320 addr = 0x14; 8321 } 8322 8323 mask = 0; 8324 if (!(env->cp15.scr_el3 & SCR_EA)) { 8325 mask |= CPSR_A; 8326 } 8327 if (!(env->cp15.scr_el3 & SCR_IRQ)) { 8328 mask |= CPSR_I; 8329 } 8330 if (!(env->cp15.scr_el3 & SCR_FIQ)) { 8331 mask |= CPSR_F; 8332 } 8333 8334 addr += env->cp15.hvbar; 8335 8336 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr); 8337 } 8338 8339 static void arm_cpu_do_interrupt_aarch32(CPUState *cs) 8340 { 8341 ARMCPU *cpu = ARM_CPU(cs); 8342 CPUARMState *env = &cpu->env; 8343 uint32_t addr; 8344 uint32_t mask; 8345 int new_mode; 8346 uint32_t offset; 8347 uint32_t moe; 8348 8349 /* If this is a debug exception we must update the DBGDSCR.MOE bits */ 8350 switch (syn_get_ec(env->exception.syndrome)) { 8351 case EC_BREAKPOINT: 8352 case EC_BREAKPOINT_SAME_EL: 8353 moe = 1; 8354 break; 8355 case EC_WATCHPOINT: 8356 case EC_WATCHPOINT_SAME_EL: 8357 moe = 10; 8358 break; 8359 case EC_AA32_BKPT: 8360 moe = 3; 8361 break; 8362 case EC_VECTORCATCH: 8363 moe = 5; 8364 break; 8365 default: 8366 moe = 0; 8367 break; 8368 } 8369 8370 if (moe) { 8371 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); 8372 } 8373 8374 if (env->exception.target_el == 2) { 8375 arm_cpu_do_interrupt_aarch32_hyp(cs); 8376 return; 8377 } 8378 8379 /* TODO: Vectored interrupt controller. */ 8380 switch (cs->exception_index) { 8381 case EXCP_UDEF: 8382 new_mode = ARM_CPU_MODE_UND; 8383 addr = 0x04; 8384 mask = CPSR_I; 8385 if (env->thumb) 8386 offset = 2; 8387 else 8388 offset = 4; 8389 break; 8390 case EXCP_SWI: 8391 new_mode = ARM_CPU_MODE_SVC; 8392 addr = 0x08; 8393 mask = CPSR_I; 8394 /* The PC already points to the next instruction. */ 8395 offset = 0; 8396 break; 8397 case EXCP_BKPT: 8398 /* Fall through to prefetch abort. */ 8399 case EXCP_PREFETCH_ABORT: 8400 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); 8401 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); 8402 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", 8403 env->exception.fsr, (uint32_t)env->exception.vaddress); 8404 new_mode = ARM_CPU_MODE_ABT; 8405 addr = 0x0c; 8406 mask = CPSR_A | CPSR_I; 8407 offset = 4; 8408 break; 8409 case EXCP_DATA_ABORT: 8410 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 8411 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); 8412 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", 8413 env->exception.fsr, 8414 (uint32_t)env->exception.vaddress); 8415 new_mode = ARM_CPU_MODE_ABT; 8416 addr = 0x10; 8417 mask = CPSR_A | CPSR_I; 8418 offset = 8; 8419 break; 8420 case EXCP_IRQ: 8421 new_mode = ARM_CPU_MODE_IRQ; 8422 addr = 0x18; 8423 /* Disable IRQ and imprecise data aborts. */ 8424 mask = CPSR_A | CPSR_I; 8425 offset = 4; 8426 if (env->cp15.scr_el3 & SCR_IRQ) { 8427 /* IRQ routed to monitor mode */ 8428 new_mode = ARM_CPU_MODE_MON; 8429 mask |= CPSR_F; 8430 } 8431 break; 8432 case EXCP_FIQ: 8433 new_mode = ARM_CPU_MODE_FIQ; 8434 addr = 0x1c; 8435 /* Disable FIQ, IRQ and imprecise data aborts. */ 8436 mask = CPSR_A | CPSR_I | CPSR_F; 8437 if (env->cp15.scr_el3 & SCR_FIQ) { 8438 /* FIQ routed to monitor mode */ 8439 new_mode = ARM_CPU_MODE_MON; 8440 } 8441 offset = 4; 8442 break; 8443 case EXCP_VIRQ: 8444 new_mode = ARM_CPU_MODE_IRQ; 8445 addr = 0x18; 8446 /* Disable IRQ and imprecise data aborts. */ 8447 mask = CPSR_A | CPSR_I; 8448 offset = 4; 8449 break; 8450 case EXCP_VFIQ: 8451 new_mode = ARM_CPU_MODE_FIQ; 8452 addr = 0x1c; 8453 /* Disable FIQ, IRQ and imprecise data aborts. */ 8454 mask = CPSR_A | CPSR_I | CPSR_F; 8455 offset = 4; 8456 break; 8457 case EXCP_SMC: 8458 new_mode = ARM_CPU_MODE_MON; 8459 addr = 0x08; 8460 mask = CPSR_A | CPSR_I | CPSR_F; 8461 offset = 0; 8462 break; 8463 default: 8464 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 8465 return; /* Never happens. Keep compiler happy. */ 8466 } 8467 8468 if (new_mode == ARM_CPU_MODE_MON) { 8469 addr += env->cp15.mvbar; 8470 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 8471 /* High vectors. When enabled, base address cannot be remapped. */ 8472 addr += 0xffff0000; 8473 } else { 8474 /* ARM v7 architectures provide a vector base address register to remap 8475 * the interrupt vector table. 8476 * This register is only followed in non-monitor mode, and is banked. 8477 * Note: only bits 31:5 are valid. 8478 */ 8479 addr += A32_BANKED_CURRENT_REG_GET(env, vbar); 8480 } 8481 8482 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 8483 env->cp15.scr_el3 &= ~SCR_NS; 8484 } 8485 8486 take_aarch32_exception(env, new_mode, mask, offset, addr); 8487 } 8488 8489 /* Handle exception entry to a target EL which is using AArch64 */ 8490 static void arm_cpu_do_interrupt_aarch64(CPUState *cs) 8491 { 8492 ARMCPU *cpu = ARM_CPU(cs); 8493 CPUARMState *env = &cpu->env; 8494 unsigned int new_el = env->exception.target_el; 8495 target_ulong addr = env->cp15.vbar_el[new_el]; 8496 unsigned int new_mode = aarch64_pstate_mode(new_el, true); 8497 unsigned int cur_el = arm_current_el(env); 8498 8499 /* 8500 * Note that new_el can never be 0. If cur_el is 0, then 8501 * el0_a64 is is_a64(), else el0_a64 is ignored. 8502 */ 8503 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env)); 8504 8505 if (cur_el < new_el) { 8506 /* Entry vector offset depends on whether the implemented EL 8507 * immediately lower than the target level is using AArch32 or AArch64 8508 */ 8509 bool is_aa64; 8510 8511 switch (new_el) { 8512 case 3: 8513 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0; 8514 break; 8515 case 2: 8516 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0; 8517 break; 8518 case 1: 8519 is_aa64 = is_a64(env); 8520 break; 8521 default: 8522 g_assert_not_reached(); 8523 } 8524 8525 if (is_aa64) { 8526 addr += 0x400; 8527 } else { 8528 addr += 0x600; 8529 } 8530 } else if (pstate_read(env) & PSTATE_SP) { 8531 addr += 0x200; 8532 } 8533 8534 switch (cs->exception_index) { 8535 case EXCP_PREFETCH_ABORT: 8536 case EXCP_DATA_ABORT: 8537 env->cp15.far_el[new_el] = env->exception.vaddress; 8538 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n", 8539 env->cp15.far_el[new_el]); 8540 /* fall through */ 8541 case EXCP_BKPT: 8542 case EXCP_UDEF: 8543 case EXCP_SWI: 8544 case EXCP_HVC: 8545 case EXCP_HYP_TRAP: 8546 case EXCP_SMC: 8547 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) { 8548 /* 8549 * QEMU internal FP/SIMD syndromes from AArch32 include the 8550 * TA and coproc fields which are only exposed if the exception 8551 * is taken to AArch32 Hyp mode. Mask them out to get a valid 8552 * AArch64 format syndrome. 8553 */ 8554 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20); 8555 } 8556 env->cp15.esr_el[new_el] = env->exception.syndrome; 8557 break; 8558 case EXCP_IRQ: 8559 case EXCP_VIRQ: 8560 addr += 0x80; 8561 break; 8562 case EXCP_FIQ: 8563 case EXCP_VFIQ: 8564 addr += 0x100; 8565 break; 8566 case EXCP_SEMIHOST: 8567 qemu_log_mask(CPU_LOG_INT, 8568 "...handling as semihosting call 0x%" PRIx64 "\n", 8569 env->xregs[0]); 8570 env->xregs[0] = do_arm_semihosting(env); 8571 return; 8572 default: 8573 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 8574 } 8575 8576 if (is_a64(env)) { 8577 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env); 8578 aarch64_save_sp(env, arm_current_el(env)); 8579 env->elr_el[new_el] = env->pc; 8580 } else { 8581 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env); 8582 env->elr_el[new_el] = env->regs[15]; 8583 8584 aarch64_sync_32_to_64(env); 8585 8586 env->condexec_bits = 0; 8587 } 8588 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n", 8589 env->elr_el[new_el]); 8590 8591 pstate_write(env, PSTATE_DAIF | new_mode); 8592 env->aarch64 = 1; 8593 aarch64_restore_sp(env, new_el); 8594 8595 env->pc = addr; 8596 8597 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n", 8598 new_el, env->pc, pstate_read(env)); 8599 } 8600 8601 static inline bool check_for_semihosting(CPUState *cs) 8602 { 8603 /* Check whether this exception is a semihosting call; if so 8604 * then handle it and return true; otherwise return false. 8605 */ 8606 ARMCPU *cpu = ARM_CPU(cs); 8607 CPUARMState *env = &cpu->env; 8608 8609 if (is_a64(env)) { 8610 if (cs->exception_index == EXCP_SEMIHOST) { 8611 /* This is always the 64-bit semihosting exception. 8612 * The "is this usermode" and "is semihosting enabled" 8613 * checks have been done at translate time. 8614 */ 8615 qemu_log_mask(CPU_LOG_INT, 8616 "...handling as semihosting call 0x%" PRIx64 "\n", 8617 env->xregs[0]); 8618 env->xregs[0] = do_arm_semihosting(env); 8619 return true; 8620 } 8621 return false; 8622 } else { 8623 uint32_t imm; 8624 8625 /* Only intercept calls from privileged modes, to provide some 8626 * semblance of security. 8627 */ 8628 if (cs->exception_index != EXCP_SEMIHOST && 8629 (!semihosting_enabled() || 8630 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) { 8631 return false; 8632 } 8633 8634 switch (cs->exception_index) { 8635 case EXCP_SEMIHOST: 8636 /* This is always a semihosting call; the "is this usermode" 8637 * and "is semihosting enabled" checks have been done at 8638 * translate time. 8639 */ 8640 break; 8641 case EXCP_SWI: 8642 /* Check for semihosting interrupt. */ 8643 if (env->thumb) { 8644 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env)) 8645 & 0xff; 8646 if (imm == 0xab) { 8647 break; 8648 } 8649 } else { 8650 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env)) 8651 & 0xffffff; 8652 if (imm == 0x123456) { 8653 break; 8654 } 8655 } 8656 return false; 8657 case EXCP_BKPT: 8658 /* See if this is a semihosting syscall. */ 8659 if (env->thumb) { 8660 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) 8661 & 0xff; 8662 if (imm == 0xab) { 8663 env->regs[15] += 2; 8664 break; 8665 } 8666 } 8667 return false; 8668 default: 8669 return false; 8670 } 8671 8672 qemu_log_mask(CPU_LOG_INT, 8673 "...handling as semihosting call 0x%x\n", 8674 env->regs[0]); 8675 env->regs[0] = do_arm_semihosting(env); 8676 return true; 8677 } 8678 } 8679 8680 /* Handle a CPU exception for A and R profile CPUs. 8681 * Do any appropriate logging, handle PSCI calls, and then hand off 8682 * to the AArch64-entry or AArch32-entry function depending on the 8683 * target exception level's register width. 8684 */ 8685 void arm_cpu_do_interrupt(CPUState *cs) 8686 { 8687 ARMCPU *cpu = ARM_CPU(cs); 8688 CPUARMState *env = &cpu->env; 8689 unsigned int new_el = env->exception.target_el; 8690 8691 assert(!arm_feature(env, ARM_FEATURE_M)); 8692 8693 arm_log_exception(cs->exception_index); 8694 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env), 8695 new_el); 8696 if (qemu_loglevel_mask(CPU_LOG_INT) 8697 && !excp_is_internal(cs->exception_index)) { 8698 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n", 8699 syn_get_ec(env->exception.syndrome), 8700 env->exception.syndrome); 8701 } 8702 8703 if (arm_is_psci_call(cpu, cs->exception_index)) { 8704 arm_handle_psci_call(cpu); 8705 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); 8706 return; 8707 } 8708 8709 /* Semihosting semantics depend on the register width of the 8710 * code that caused the exception, not the target exception level, 8711 * so must be handled here. 8712 */ 8713 if (check_for_semihosting(cs)) { 8714 return; 8715 } 8716 8717 /* Hooks may change global state so BQL should be held, also the 8718 * BQL needs to be held for any modification of 8719 * cs->interrupt_request. 8720 */ 8721 g_assert(qemu_mutex_iothread_locked()); 8722 8723 arm_call_pre_el_change_hook(cpu); 8724 8725 assert(!excp_is_internal(cs->exception_index)); 8726 if (arm_el_is_aa64(env, new_el)) { 8727 arm_cpu_do_interrupt_aarch64(cs); 8728 } else { 8729 arm_cpu_do_interrupt_aarch32(cs); 8730 } 8731 8732 arm_call_el_change_hook(cpu); 8733 8734 if (!kvm_enabled()) { 8735 cs->interrupt_request |= CPU_INTERRUPT_EXITTB; 8736 } 8737 } 8738 8739 /* Return the exception level which controls this address translation regime */ 8740 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx) 8741 { 8742 switch (mmu_idx) { 8743 case ARMMMUIdx_S2NS: 8744 case ARMMMUIdx_S1E2: 8745 return 2; 8746 case ARMMMUIdx_S1E3: 8747 return 3; 8748 case ARMMMUIdx_S1SE0: 8749 return arm_el_is_aa64(env, 3) ? 1 : 3; 8750 case ARMMMUIdx_S1SE1: 8751 case ARMMMUIdx_S1NSE0: 8752 case ARMMMUIdx_S1NSE1: 8753 case ARMMMUIdx_MPrivNegPri: 8754 case ARMMMUIdx_MUserNegPri: 8755 case ARMMMUIdx_MPriv: 8756 case ARMMMUIdx_MUser: 8757 case ARMMMUIdx_MSPrivNegPri: 8758 case ARMMMUIdx_MSUserNegPri: 8759 case ARMMMUIdx_MSPriv: 8760 case ARMMMUIdx_MSUser: 8761 return 1; 8762 default: 8763 g_assert_not_reached(); 8764 } 8765 } 8766 8767 /* Return the SCTLR value which controls this address translation regime */ 8768 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx) 8769 { 8770 return env->cp15.sctlr_el[regime_el(env, mmu_idx)]; 8771 } 8772 8773 /* Return true if the specified stage of address translation is disabled */ 8774 static inline bool regime_translation_disabled(CPUARMState *env, 8775 ARMMMUIdx mmu_idx) 8776 { 8777 if (arm_feature(env, ARM_FEATURE_M)) { 8778 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] & 8779 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) { 8780 case R_V7M_MPU_CTRL_ENABLE_MASK: 8781 /* Enabled, but not for HardFault and NMI */ 8782 return mmu_idx & ARM_MMU_IDX_M_NEGPRI; 8783 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK: 8784 /* Enabled for all cases */ 8785 return false; 8786 case 0: 8787 default: 8788 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but 8789 * we warned about that in armv7m_nvic.c when the guest set it. 8790 */ 8791 return true; 8792 } 8793 } 8794 8795 if (mmu_idx == ARMMMUIdx_S2NS) { 8796 /* HCR.DC means HCR.VM behaves as 1 */ 8797 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0; 8798 } 8799 8800 if (env->cp15.hcr_el2 & HCR_TGE) { 8801 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */ 8802 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) { 8803 return true; 8804 } 8805 } 8806 8807 if ((env->cp15.hcr_el2 & HCR_DC) && 8808 (mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1)) { 8809 /* HCR.DC means SCTLR_EL1.M behaves as 0 */ 8810 return true; 8811 } 8812 8813 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0; 8814 } 8815 8816 static inline bool regime_translation_big_endian(CPUARMState *env, 8817 ARMMMUIdx mmu_idx) 8818 { 8819 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0; 8820 } 8821 8822 /* Return the TCR controlling this translation regime */ 8823 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx) 8824 { 8825 if (mmu_idx == ARMMMUIdx_S2NS) { 8826 return &env->cp15.vtcr_el2; 8827 } 8828 return &env->cp15.tcr_el[regime_el(env, mmu_idx)]; 8829 } 8830 8831 /* Convert a possible stage1+2 MMU index into the appropriate 8832 * stage 1 MMU index 8833 */ 8834 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx) 8835 { 8836 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 8837 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0); 8838 } 8839 return mmu_idx; 8840 } 8841 8842 /* Returns TBI0 value for current regime el */ 8843 uint32_t arm_regime_tbi0(CPUARMState *env, ARMMMUIdx mmu_idx) 8844 { 8845 TCR *tcr; 8846 uint32_t el; 8847 8848 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert 8849 * a stage 1+2 mmu index into the appropriate stage 1 mmu index. 8850 */ 8851 mmu_idx = stage_1_mmu_idx(mmu_idx); 8852 8853 tcr = regime_tcr(env, mmu_idx); 8854 el = regime_el(env, mmu_idx); 8855 8856 if (el > 1) { 8857 return extract64(tcr->raw_tcr, 20, 1); 8858 } else { 8859 return extract64(tcr->raw_tcr, 37, 1); 8860 } 8861 } 8862 8863 /* Returns TBI1 value for current regime el */ 8864 uint32_t arm_regime_tbi1(CPUARMState *env, ARMMMUIdx mmu_idx) 8865 { 8866 TCR *tcr; 8867 uint32_t el; 8868 8869 /* For EL0 and EL1, TBI is controlled by stage 1's TCR, so convert 8870 * a stage 1+2 mmu index into the appropriate stage 1 mmu index. 8871 */ 8872 mmu_idx = stage_1_mmu_idx(mmu_idx); 8873 8874 tcr = regime_tcr(env, mmu_idx); 8875 el = regime_el(env, mmu_idx); 8876 8877 if (el > 1) { 8878 return 0; 8879 } else { 8880 return extract64(tcr->raw_tcr, 38, 1); 8881 } 8882 } 8883 8884 /* Return the TTBR associated with this translation regime */ 8885 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx, 8886 int ttbrn) 8887 { 8888 if (mmu_idx == ARMMMUIdx_S2NS) { 8889 return env->cp15.vttbr_el2; 8890 } 8891 if (ttbrn == 0) { 8892 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)]; 8893 } else { 8894 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)]; 8895 } 8896 } 8897 8898 /* Return true if the translation regime is using LPAE format page tables */ 8899 static inline bool regime_using_lpae_format(CPUARMState *env, 8900 ARMMMUIdx mmu_idx) 8901 { 8902 int el = regime_el(env, mmu_idx); 8903 if (el == 2 || arm_el_is_aa64(env, el)) { 8904 return true; 8905 } 8906 if (arm_feature(env, ARM_FEATURE_LPAE) 8907 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) { 8908 return true; 8909 } 8910 return false; 8911 } 8912 8913 /* Returns true if the stage 1 translation regime is using LPAE format page 8914 * tables. Used when raising alignment exceptions, whose FSR changes depending 8915 * on whether the long or short descriptor format is in use. */ 8916 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx) 8917 { 8918 mmu_idx = stage_1_mmu_idx(mmu_idx); 8919 8920 return regime_using_lpae_format(env, mmu_idx); 8921 } 8922 8923 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx) 8924 { 8925 switch (mmu_idx) { 8926 case ARMMMUIdx_S1SE0: 8927 case ARMMMUIdx_S1NSE0: 8928 case ARMMMUIdx_MUser: 8929 case ARMMMUIdx_MSUser: 8930 case ARMMMUIdx_MUserNegPri: 8931 case ARMMMUIdx_MSUserNegPri: 8932 return true; 8933 default: 8934 return false; 8935 case ARMMMUIdx_S12NSE0: 8936 case ARMMMUIdx_S12NSE1: 8937 g_assert_not_reached(); 8938 } 8939 } 8940 8941 /* Translate section/page access permissions to page 8942 * R/W protection flags 8943 * 8944 * @env: CPUARMState 8945 * @mmu_idx: MMU index indicating required translation regime 8946 * @ap: The 3-bit access permissions (AP[2:0]) 8947 * @domain_prot: The 2-bit domain access permissions 8948 */ 8949 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, 8950 int ap, int domain_prot) 8951 { 8952 bool is_user = regime_is_user(env, mmu_idx); 8953 8954 if (domain_prot == 3) { 8955 return PAGE_READ | PAGE_WRITE; 8956 } 8957 8958 switch (ap) { 8959 case 0: 8960 if (arm_feature(env, ARM_FEATURE_V7)) { 8961 return 0; 8962 } 8963 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) { 8964 case SCTLR_S: 8965 return is_user ? 0 : PAGE_READ; 8966 case SCTLR_R: 8967 return PAGE_READ; 8968 default: 8969 return 0; 8970 } 8971 case 1: 8972 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 8973 case 2: 8974 if (is_user) { 8975 return PAGE_READ; 8976 } else { 8977 return PAGE_READ | PAGE_WRITE; 8978 } 8979 case 3: 8980 return PAGE_READ | PAGE_WRITE; 8981 case 4: /* Reserved. */ 8982 return 0; 8983 case 5: 8984 return is_user ? 0 : PAGE_READ; 8985 case 6: 8986 return PAGE_READ; 8987 case 7: 8988 if (!arm_feature(env, ARM_FEATURE_V6K)) { 8989 return 0; 8990 } 8991 return PAGE_READ; 8992 default: 8993 g_assert_not_reached(); 8994 } 8995 } 8996 8997 /* Translate section/page access permissions to page 8998 * R/W protection flags. 8999 * 9000 * @ap: The 2-bit simple AP (AP[2:1]) 9001 * @is_user: TRUE if accessing from PL0 9002 */ 9003 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user) 9004 { 9005 switch (ap) { 9006 case 0: 9007 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 9008 case 1: 9009 return PAGE_READ | PAGE_WRITE; 9010 case 2: 9011 return is_user ? 0 : PAGE_READ; 9012 case 3: 9013 return PAGE_READ; 9014 default: 9015 g_assert_not_reached(); 9016 } 9017 } 9018 9019 static inline int 9020 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap) 9021 { 9022 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx)); 9023 } 9024 9025 /* Translate S2 section/page access permissions to protection flags 9026 * 9027 * @env: CPUARMState 9028 * @s2ap: The 2-bit stage2 access permissions (S2AP) 9029 * @xn: XN (execute-never) bit 9030 */ 9031 static int get_S2prot(CPUARMState *env, int s2ap, int xn) 9032 { 9033 int prot = 0; 9034 9035 if (s2ap & 1) { 9036 prot |= PAGE_READ; 9037 } 9038 if (s2ap & 2) { 9039 prot |= PAGE_WRITE; 9040 } 9041 if (!xn) { 9042 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) { 9043 prot |= PAGE_EXEC; 9044 } 9045 } 9046 return prot; 9047 } 9048 9049 /* Translate section/page access permissions to protection flags 9050 * 9051 * @env: CPUARMState 9052 * @mmu_idx: MMU index indicating required translation regime 9053 * @is_aa64: TRUE if AArch64 9054 * @ap: The 2-bit simple AP (AP[2:1]) 9055 * @ns: NS (non-secure) bit 9056 * @xn: XN (execute-never) bit 9057 * @pxn: PXN (privileged execute-never) bit 9058 */ 9059 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64, 9060 int ap, int ns, int xn, int pxn) 9061 { 9062 bool is_user = regime_is_user(env, mmu_idx); 9063 int prot_rw, user_rw; 9064 bool have_wxn; 9065 int wxn = 0; 9066 9067 assert(mmu_idx != ARMMMUIdx_S2NS); 9068 9069 user_rw = simple_ap_to_rw_prot_is_user(ap, true); 9070 if (is_user) { 9071 prot_rw = user_rw; 9072 } else { 9073 prot_rw = simple_ap_to_rw_prot_is_user(ap, false); 9074 } 9075 9076 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) { 9077 return prot_rw; 9078 } 9079 9080 /* TODO have_wxn should be replaced with 9081 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2) 9082 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE 9083 * compatible processors have EL2, which is required for [U]WXN. 9084 */ 9085 have_wxn = arm_feature(env, ARM_FEATURE_LPAE); 9086 9087 if (have_wxn) { 9088 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN; 9089 } 9090 9091 if (is_aa64) { 9092 switch (regime_el(env, mmu_idx)) { 9093 case 1: 9094 if (!is_user) { 9095 xn = pxn || (user_rw & PAGE_WRITE); 9096 } 9097 break; 9098 case 2: 9099 case 3: 9100 break; 9101 } 9102 } else if (arm_feature(env, ARM_FEATURE_V7)) { 9103 switch (regime_el(env, mmu_idx)) { 9104 case 1: 9105 case 3: 9106 if (is_user) { 9107 xn = xn || !(user_rw & PAGE_READ); 9108 } else { 9109 int uwxn = 0; 9110 if (have_wxn) { 9111 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN; 9112 } 9113 xn = xn || !(prot_rw & PAGE_READ) || pxn || 9114 (uwxn && (user_rw & PAGE_WRITE)); 9115 } 9116 break; 9117 case 2: 9118 break; 9119 } 9120 } else { 9121 xn = wxn = 0; 9122 } 9123 9124 if (xn || (wxn && (prot_rw & PAGE_WRITE))) { 9125 return prot_rw; 9126 } 9127 return prot_rw | PAGE_EXEC; 9128 } 9129 9130 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx, 9131 uint32_t *table, uint32_t address) 9132 { 9133 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */ 9134 TCR *tcr = regime_tcr(env, mmu_idx); 9135 9136 if (address & tcr->mask) { 9137 if (tcr->raw_tcr & TTBCR_PD1) { 9138 /* Translation table walk disabled for TTBR1 */ 9139 return false; 9140 } 9141 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000; 9142 } else { 9143 if (tcr->raw_tcr & TTBCR_PD0) { 9144 /* Translation table walk disabled for TTBR0 */ 9145 return false; 9146 } 9147 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask; 9148 } 9149 *table |= (address >> 18) & 0x3ffc; 9150 return true; 9151 } 9152 9153 /* Translate a S1 pagetable walk through S2 if needed. */ 9154 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx, 9155 hwaddr addr, MemTxAttrs txattrs, 9156 ARMMMUFaultInfo *fi) 9157 { 9158 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) && 9159 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) { 9160 target_ulong s2size; 9161 hwaddr s2pa; 9162 int s2prot; 9163 int ret; 9164 ARMCacheAttrs cacheattrs = {}; 9165 ARMCacheAttrs *pcacheattrs = NULL; 9166 9167 if (env->cp15.hcr_el2 & HCR_PTW) { 9168 /* 9169 * PTW means we must fault if this S1 walk touches S2 Device 9170 * memory; otherwise we don't care about the attributes and can 9171 * save the S2 translation the effort of computing them. 9172 */ 9173 pcacheattrs = &cacheattrs; 9174 } 9175 9176 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa, 9177 &txattrs, &s2prot, &s2size, fi, pcacheattrs); 9178 if (ret) { 9179 assert(fi->type != ARMFault_None); 9180 fi->s2addr = addr; 9181 fi->stage2 = true; 9182 fi->s1ptw = true; 9183 return ~0; 9184 } 9185 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) { 9186 /* Access was to Device memory: generate Permission fault */ 9187 fi->type = ARMFault_Permission; 9188 fi->s2addr = addr; 9189 fi->stage2 = true; 9190 fi->s1ptw = true; 9191 return ~0; 9192 } 9193 addr = s2pa; 9194 } 9195 return addr; 9196 } 9197 9198 /* All loads done in the course of a page table walk go through here. */ 9199 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure, 9200 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 9201 { 9202 ARMCPU *cpu = ARM_CPU(cs); 9203 CPUARMState *env = &cpu->env; 9204 MemTxAttrs attrs = {}; 9205 MemTxResult result = MEMTX_OK; 9206 AddressSpace *as; 9207 uint32_t data; 9208 9209 attrs.secure = is_secure; 9210 as = arm_addressspace(cs, attrs); 9211 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 9212 if (fi->s1ptw) { 9213 return 0; 9214 } 9215 if (regime_translation_big_endian(env, mmu_idx)) { 9216 data = address_space_ldl_be(as, addr, attrs, &result); 9217 } else { 9218 data = address_space_ldl_le(as, addr, attrs, &result); 9219 } 9220 if (result == MEMTX_OK) { 9221 return data; 9222 } 9223 fi->type = ARMFault_SyncExternalOnWalk; 9224 fi->ea = arm_extabort_type(result); 9225 return 0; 9226 } 9227 9228 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure, 9229 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 9230 { 9231 ARMCPU *cpu = ARM_CPU(cs); 9232 CPUARMState *env = &cpu->env; 9233 MemTxAttrs attrs = {}; 9234 MemTxResult result = MEMTX_OK; 9235 AddressSpace *as; 9236 uint64_t data; 9237 9238 attrs.secure = is_secure; 9239 as = arm_addressspace(cs, attrs); 9240 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 9241 if (fi->s1ptw) { 9242 return 0; 9243 } 9244 if (regime_translation_big_endian(env, mmu_idx)) { 9245 data = address_space_ldq_be(as, addr, attrs, &result); 9246 } else { 9247 data = address_space_ldq_le(as, addr, attrs, &result); 9248 } 9249 if (result == MEMTX_OK) { 9250 return data; 9251 } 9252 fi->type = ARMFault_SyncExternalOnWalk; 9253 fi->ea = arm_extabort_type(result); 9254 return 0; 9255 } 9256 9257 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address, 9258 MMUAccessType access_type, ARMMMUIdx mmu_idx, 9259 hwaddr *phys_ptr, int *prot, 9260 target_ulong *page_size, 9261 ARMMMUFaultInfo *fi) 9262 { 9263 CPUState *cs = CPU(arm_env_get_cpu(env)); 9264 int level = 1; 9265 uint32_t table; 9266 uint32_t desc; 9267 int type; 9268 int ap; 9269 int domain = 0; 9270 int domain_prot; 9271 hwaddr phys_addr; 9272 uint32_t dacr; 9273 9274 /* Pagetable walk. */ 9275 /* Lookup l1 descriptor. */ 9276 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 9277 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 9278 fi->type = ARMFault_Translation; 9279 goto do_fault; 9280 } 9281 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 9282 mmu_idx, fi); 9283 if (fi->type != ARMFault_None) { 9284 goto do_fault; 9285 } 9286 type = (desc & 3); 9287 domain = (desc >> 5) & 0x0f; 9288 if (regime_el(env, mmu_idx) == 1) { 9289 dacr = env->cp15.dacr_ns; 9290 } else { 9291 dacr = env->cp15.dacr_s; 9292 } 9293 domain_prot = (dacr >> (domain * 2)) & 3; 9294 if (type == 0) { 9295 /* Section translation fault. */ 9296 fi->type = ARMFault_Translation; 9297 goto do_fault; 9298 } 9299 if (type != 2) { 9300 level = 2; 9301 } 9302 if (domain_prot == 0 || domain_prot == 2) { 9303 fi->type = ARMFault_Domain; 9304 goto do_fault; 9305 } 9306 if (type == 2) { 9307 /* 1Mb section. */ 9308 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 9309 ap = (desc >> 10) & 3; 9310 *page_size = 1024 * 1024; 9311 } else { 9312 /* Lookup l2 entry. */ 9313 if (type == 1) { 9314 /* Coarse pagetable. */ 9315 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 9316 } else { 9317 /* Fine pagetable. */ 9318 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); 9319 } 9320 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 9321 mmu_idx, fi); 9322 if (fi->type != ARMFault_None) { 9323 goto do_fault; 9324 } 9325 switch (desc & 3) { 9326 case 0: /* Page translation fault. */ 9327 fi->type = ARMFault_Translation; 9328 goto do_fault; 9329 case 1: /* 64k page. */ 9330 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 9331 ap = (desc >> (4 + ((address >> 13) & 6))) & 3; 9332 *page_size = 0x10000; 9333 break; 9334 case 2: /* 4k page. */ 9335 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 9336 ap = (desc >> (4 + ((address >> 9) & 6))) & 3; 9337 *page_size = 0x1000; 9338 break; 9339 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */ 9340 if (type == 1) { 9341 /* ARMv6/XScale extended small page format */ 9342 if (arm_feature(env, ARM_FEATURE_XSCALE) 9343 || arm_feature(env, ARM_FEATURE_V6)) { 9344 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 9345 *page_size = 0x1000; 9346 } else { 9347 /* UNPREDICTABLE in ARMv5; we choose to take a 9348 * page translation fault. 9349 */ 9350 fi->type = ARMFault_Translation; 9351 goto do_fault; 9352 } 9353 } else { 9354 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); 9355 *page_size = 0x400; 9356 } 9357 ap = (desc >> 4) & 3; 9358 break; 9359 default: 9360 /* Never happens, but compiler isn't smart enough to tell. */ 9361 abort(); 9362 } 9363 } 9364 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 9365 *prot |= *prot ? PAGE_EXEC : 0; 9366 if (!(*prot & (1 << access_type))) { 9367 /* Access permission fault. */ 9368 fi->type = ARMFault_Permission; 9369 goto do_fault; 9370 } 9371 *phys_ptr = phys_addr; 9372 return false; 9373 do_fault: 9374 fi->domain = domain; 9375 fi->level = level; 9376 return true; 9377 } 9378 9379 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address, 9380 MMUAccessType access_type, ARMMMUIdx mmu_idx, 9381 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 9382 target_ulong *page_size, ARMMMUFaultInfo *fi) 9383 { 9384 CPUState *cs = CPU(arm_env_get_cpu(env)); 9385 int level = 1; 9386 uint32_t table; 9387 uint32_t desc; 9388 uint32_t xn; 9389 uint32_t pxn = 0; 9390 int type; 9391 int ap; 9392 int domain = 0; 9393 int domain_prot; 9394 hwaddr phys_addr; 9395 uint32_t dacr; 9396 bool ns; 9397 9398 /* Pagetable walk. */ 9399 /* Lookup l1 descriptor. */ 9400 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 9401 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 9402 fi->type = ARMFault_Translation; 9403 goto do_fault; 9404 } 9405 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 9406 mmu_idx, fi); 9407 if (fi->type != ARMFault_None) { 9408 goto do_fault; 9409 } 9410 type = (desc & 3); 9411 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) { 9412 /* Section translation fault, or attempt to use the encoding 9413 * which is Reserved on implementations without PXN. 9414 */ 9415 fi->type = ARMFault_Translation; 9416 goto do_fault; 9417 } 9418 if ((type == 1) || !(desc & (1 << 18))) { 9419 /* Page or Section. */ 9420 domain = (desc >> 5) & 0x0f; 9421 } 9422 if (regime_el(env, mmu_idx) == 1) { 9423 dacr = env->cp15.dacr_ns; 9424 } else { 9425 dacr = env->cp15.dacr_s; 9426 } 9427 if (type == 1) { 9428 level = 2; 9429 } 9430 domain_prot = (dacr >> (domain * 2)) & 3; 9431 if (domain_prot == 0 || domain_prot == 2) { 9432 /* Section or Page domain fault */ 9433 fi->type = ARMFault_Domain; 9434 goto do_fault; 9435 } 9436 if (type != 1) { 9437 if (desc & (1 << 18)) { 9438 /* Supersection. */ 9439 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); 9440 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32; 9441 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36; 9442 *page_size = 0x1000000; 9443 } else { 9444 /* Section. */ 9445 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 9446 *page_size = 0x100000; 9447 } 9448 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); 9449 xn = desc & (1 << 4); 9450 pxn = desc & 1; 9451 ns = extract32(desc, 19, 1); 9452 } else { 9453 if (arm_feature(env, ARM_FEATURE_PXN)) { 9454 pxn = (desc >> 2) & 1; 9455 } 9456 ns = extract32(desc, 3, 1); 9457 /* Lookup l2 entry. */ 9458 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 9459 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 9460 mmu_idx, fi); 9461 if (fi->type != ARMFault_None) { 9462 goto do_fault; 9463 } 9464 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); 9465 switch (desc & 3) { 9466 case 0: /* Page translation fault. */ 9467 fi->type = ARMFault_Translation; 9468 goto do_fault; 9469 case 1: /* 64k page. */ 9470 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 9471 xn = desc & (1 << 15); 9472 *page_size = 0x10000; 9473 break; 9474 case 2: case 3: /* 4k page. */ 9475 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 9476 xn = desc & 1; 9477 *page_size = 0x1000; 9478 break; 9479 default: 9480 /* Never happens, but compiler isn't smart enough to tell. */ 9481 abort(); 9482 } 9483 } 9484 if (domain_prot == 3) { 9485 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 9486 } else { 9487 if (pxn && !regime_is_user(env, mmu_idx)) { 9488 xn = 1; 9489 } 9490 if (xn && access_type == MMU_INST_FETCH) { 9491 fi->type = ARMFault_Permission; 9492 goto do_fault; 9493 } 9494 9495 if (arm_feature(env, ARM_FEATURE_V6K) && 9496 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) { 9497 /* The simplified model uses AP[0] as an access control bit. */ 9498 if ((ap & 1) == 0) { 9499 /* Access flag fault. */ 9500 fi->type = ARMFault_AccessFlag; 9501 goto do_fault; 9502 } 9503 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1); 9504 } else { 9505 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 9506 } 9507 if (*prot && !xn) { 9508 *prot |= PAGE_EXEC; 9509 } 9510 if (!(*prot & (1 << access_type))) { 9511 /* Access permission fault. */ 9512 fi->type = ARMFault_Permission; 9513 goto do_fault; 9514 } 9515 } 9516 if (ns) { 9517 /* The NS bit will (as required by the architecture) have no effect if 9518 * the CPU doesn't support TZ or this is a non-secure translation 9519 * regime, because the attribute will already be non-secure. 9520 */ 9521 attrs->secure = false; 9522 } 9523 *phys_ptr = phys_addr; 9524 return false; 9525 do_fault: 9526 fi->domain = domain; 9527 fi->level = level; 9528 return true; 9529 } 9530 9531 /* 9532 * check_s2_mmu_setup 9533 * @cpu: ARMCPU 9534 * @is_aa64: True if the translation regime is in AArch64 state 9535 * @startlevel: Suggested starting level 9536 * @inputsize: Bitsize of IPAs 9537 * @stride: Page-table stride (See the ARM ARM) 9538 * 9539 * Returns true if the suggested S2 translation parameters are OK and 9540 * false otherwise. 9541 */ 9542 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level, 9543 int inputsize, int stride) 9544 { 9545 const int grainsize = stride + 3; 9546 int startsizecheck; 9547 9548 /* Negative levels are never allowed. */ 9549 if (level < 0) { 9550 return false; 9551 } 9552 9553 startsizecheck = inputsize - ((3 - level) * stride + grainsize); 9554 if (startsizecheck < 1 || startsizecheck > stride + 4) { 9555 return false; 9556 } 9557 9558 if (is_aa64) { 9559 CPUARMState *env = &cpu->env; 9560 unsigned int pamax = arm_pamax(cpu); 9561 9562 switch (stride) { 9563 case 13: /* 64KB Pages. */ 9564 if (level == 0 || (level == 1 && pamax <= 42)) { 9565 return false; 9566 } 9567 break; 9568 case 11: /* 16KB Pages. */ 9569 if (level == 0 || (level == 1 && pamax <= 40)) { 9570 return false; 9571 } 9572 break; 9573 case 9: /* 4KB Pages. */ 9574 if (level == 0 && pamax <= 42) { 9575 return false; 9576 } 9577 break; 9578 default: 9579 g_assert_not_reached(); 9580 } 9581 9582 /* Inputsize checks. */ 9583 if (inputsize > pamax && 9584 (arm_el_is_aa64(env, 1) || inputsize > 40)) { 9585 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */ 9586 return false; 9587 } 9588 } else { 9589 /* AArch32 only supports 4KB pages. Assert on that. */ 9590 assert(stride == 9); 9591 9592 if (level == 0) { 9593 return false; 9594 } 9595 } 9596 return true; 9597 } 9598 9599 /* Translate from the 4-bit stage 2 representation of 9600 * memory attributes (without cache-allocation hints) to 9601 * the 8-bit representation of the stage 1 MAIR registers 9602 * (which includes allocation hints). 9603 * 9604 * ref: shared/translation/attrs/S2AttrDecode() 9605 * .../S2ConvertAttrsHints() 9606 */ 9607 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs) 9608 { 9609 uint8_t hiattr = extract32(s2attrs, 2, 2); 9610 uint8_t loattr = extract32(s2attrs, 0, 2); 9611 uint8_t hihint = 0, lohint = 0; 9612 9613 if (hiattr != 0) { /* normal memory */ 9614 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */ 9615 hiattr = loattr = 1; /* non-cacheable */ 9616 } else { 9617 if (hiattr != 1) { /* Write-through or write-back */ 9618 hihint = 3; /* RW allocate */ 9619 } 9620 if (loattr != 1) { /* Write-through or write-back */ 9621 lohint = 3; /* RW allocate */ 9622 } 9623 } 9624 } 9625 9626 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint; 9627 } 9628 9629 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, 9630 MMUAccessType access_type, ARMMMUIdx mmu_idx, 9631 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 9632 target_ulong *page_size_ptr, 9633 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 9634 { 9635 ARMCPU *cpu = arm_env_get_cpu(env); 9636 CPUState *cs = CPU(cpu); 9637 /* Read an LPAE long-descriptor translation table. */ 9638 ARMFaultType fault_type = ARMFault_Translation; 9639 uint32_t level; 9640 uint32_t epd = 0; 9641 int32_t t0sz, t1sz; 9642 uint32_t tg; 9643 uint64_t ttbr; 9644 int ttbr_select; 9645 hwaddr descaddr, indexmask, indexmask_grainsize; 9646 uint32_t tableattrs; 9647 target_ulong page_size; 9648 uint32_t attrs; 9649 int32_t stride = 9; 9650 int32_t addrsize; 9651 int inputsize; 9652 int32_t tbi = 0; 9653 TCR *tcr = regime_tcr(env, mmu_idx); 9654 int ap, ns, xn, pxn; 9655 uint32_t el = regime_el(env, mmu_idx); 9656 bool ttbr1_valid = true; 9657 uint64_t descaddrmask; 9658 bool aarch64 = arm_el_is_aa64(env, el); 9659 9660 /* TODO: 9661 * This code does not handle the different format TCR for VTCR_EL2. 9662 * This code also does not support shareability levels. 9663 * Attribute and permission bit handling should also be checked when adding 9664 * support for those page table walks. 9665 */ 9666 if (aarch64) { 9667 level = 0; 9668 addrsize = 64; 9669 if (el > 1) { 9670 if (mmu_idx != ARMMMUIdx_S2NS) { 9671 tbi = extract64(tcr->raw_tcr, 20, 1); 9672 } 9673 } else { 9674 if (extract64(address, 55, 1)) { 9675 tbi = extract64(tcr->raw_tcr, 38, 1); 9676 } else { 9677 tbi = extract64(tcr->raw_tcr, 37, 1); 9678 } 9679 } 9680 tbi *= 8; 9681 9682 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it 9683 * invalid. 9684 */ 9685 if (el > 1) { 9686 ttbr1_valid = false; 9687 } 9688 } else { 9689 level = 1; 9690 addrsize = 32; 9691 /* There is no TTBR1 for EL2 */ 9692 if (el == 2) { 9693 ttbr1_valid = false; 9694 } 9695 } 9696 9697 /* Determine whether this address is in the region controlled by 9698 * TTBR0 or TTBR1 (or if it is in neither region and should fault). 9699 * This is a Non-secure PL0/1 stage 1 translation, so controlled by 9700 * TTBCR/TTBR0/TTBR1 in accordance with ARM ARM DDI0406C table B-32: 9701 */ 9702 if (aarch64) { 9703 /* AArch64 translation. */ 9704 t0sz = extract32(tcr->raw_tcr, 0, 6); 9705 t0sz = MIN(t0sz, 39); 9706 t0sz = MAX(t0sz, 16); 9707 } else if (mmu_idx != ARMMMUIdx_S2NS) { 9708 /* AArch32 stage 1 translation. */ 9709 t0sz = extract32(tcr->raw_tcr, 0, 3); 9710 } else { 9711 /* AArch32 stage 2 translation. */ 9712 bool sext = extract32(tcr->raw_tcr, 4, 1); 9713 bool sign = extract32(tcr->raw_tcr, 3, 1); 9714 /* Address size is 40-bit for a stage 2 translation, 9715 * and t0sz can be negative (from -8 to 7), 9716 * so we need to adjust it to use the TTBR selecting logic below. 9717 */ 9718 addrsize = 40; 9719 t0sz = sextract32(tcr->raw_tcr, 0, 4) + 8; 9720 9721 /* If the sign-extend bit is not the same as t0sz[3], the result 9722 * is unpredictable. Flag this as a guest error. */ 9723 if (sign != sext) { 9724 qemu_log_mask(LOG_GUEST_ERROR, 9725 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n"); 9726 } 9727 } 9728 t1sz = extract32(tcr->raw_tcr, 16, 6); 9729 if (aarch64) { 9730 t1sz = MIN(t1sz, 39); 9731 t1sz = MAX(t1sz, 16); 9732 } 9733 if (t0sz && !extract64(address, addrsize - t0sz, t0sz - tbi)) { 9734 /* there is a ttbr0 region and we are in it (high bits all zero) */ 9735 ttbr_select = 0; 9736 } else if (ttbr1_valid && t1sz && 9737 !extract64(~address, addrsize - t1sz, t1sz - tbi)) { 9738 /* there is a ttbr1 region and we are in it (high bits all one) */ 9739 ttbr_select = 1; 9740 } else if (!t0sz) { 9741 /* ttbr0 region is "everything not in the ttbr1 region" */ 9742 ttbr_select = 0; 9743 } else if (!t1sz && ttbr1_valid) { 9744 /* ttbr1 region is "everything not in the ttbr0 region" */ 9745 ttbr_select = 1; 9746 } else { 9747 /* in the gap between the two regions, this is a Translation fault */ 9748 fault_type = ARMFault_Translation; 9749 goto do_fault; 9750 } 9751 9752 /* Note that QEMU ignores shareability and cacheability attributes, 9753 * so we don't need to do anything with the SH, ORGN, IRGN fields 9754 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the 9755 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently 9756 * implement any ASID-like capability so we can ignore it (instead 9757 * we will always flush the TLB any time the ASID is changed). 9758 */ 9759 if (ttbr_select == 0) { 9760 ttbr = regime_ttbr(env, mmu_idx, 0); 9761 if (el < 2) { 9762 epd = extract32(tcr->raw_tcr, 7, 1); 9763 } 9764 inputsize = addrsize - t0sz; 9765 9766 tg = extract32(tcr->raw_tcr, 14, 2); 9767 if (tg == 1) { /* 64KB pages */ 9768 stride = 13; 9769 } 9770 if (tg == 2) { /* 16KB pages */ 9771 stride = 11; 9772 } 9773 } else { 9774 /* We should only be here if TTBR1 is valid */ 9775 assert(ttbr1_valid); 9776 9777 ttbr = regime_ttbr(env, mmu_idx, 1); 9778 epd = extract32(tcr->raw_tcr, 23, 1); 9779 inputsize = addrsize - t1sz; 9780 9781 tg = extract32(tcr->raw_tcr, 30, 2); 9782 if (tg == 3) { /* 64KB pages */ 9783 stride = 13; 9784 } 9785 if (tg == 1) { /* 16KB pages */ 9786 stride = 11; 9787 } 9788 } 9789 9790 /* Here we should have set up all the parameters for the translation: 9791 * inputsize, ttbr, epd, stride, tbi 9792 */ 9793 9794 if (epd) { 9795 /* Translation table walk disabled => Translation fault on TLB miss 9796 * Note: This is always 0 on 64-bit EL2 and EL3. 9797 */ 9798 goto do_fault; 9799 } 9800 9801 if (mmu_idx != ARMMMUIdx_S2NS) { 9802 /* The starting level depends on the virtual address size (which can 9803 * be up to 48 bits) and the translation granule size. It indicates 9804 * the number of strides (stride bits at a time) needed to 9805 * consume the bits of the input address. In the pseudocode this is: 9806 * level = 4 - RoundUp((inputsize - grainsize) / stride) 9807 * where their 'inputsize' is our 'inputsize', 'grainsize' is 9808 * our 'stride + 3' and 'stride' is our 'stride'. 9809 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying: 9810 * = 4 - (inputsize - stride - 3 + stride - 1) / stride 9811 * = 4 - (inputsize - 4) / stride; 9812 */ 9813 level = 4 - (inputsize - 4) / stride; 9814 } else { 9815 /* For stage 2 translations the starting level is specified by the 9816 * VTCR_EL2.SL0 field (whose interpretation depends on the page size) 9817 */ 9818 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2); 9819 uint32_t startlevel; 9820 bool ok; 9821 9822 if (!aarch64 || stride == 9) { 9823 /* AArch32 or 4KB pages */ 9824 startlevel = 2 - sl0; 9825 } else { 9826 /* 16KB or 64KB pages */ 9827 startlevel = 3 - sl0; 9828 } 9829 9830 /* Check that the starting level is valid. */ 9831 ok = check_s2_mmu_setup(cpu, aarch64, startlevel, 9832 inputsize, stride); 9833 if (!ok) { 9834 fault_type = ARMFault_Translation; 9835 goto do_fault; 9836 } 9837 level = startlevel; 9838 } 9839 9840 indexmask_grainsize = (1ULL << (stride + 3)) - 1; 9841 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1; 9842 9843 /* Now we can extract the actual base address from the TTBR */ 9844 descaddr = extract64(ttbr, 0, 48); 9845 descaddr &= ~indexmask; 9846 9847 /* The address field in the descriptor goes up to bit 39 for ARMv7 9848 * but up to bit 47 for ARMv8, but we use the descaddrmask 9849 * up to bit 39 for AArch32, because we don't need other bits in that case 9850 * to construct next descriptor address (anyway they should be all zeroes). 9851 */ 9852 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) & 9853 ~indexmask_grainsize; 9854 9855 /* Secure accesses start with the page table in secure memory and 9856 * can be downgraded to non-secure at any step. Non-secure accesses 9857 * remain non-secure. We implement this by just ORing in the NSTable/NS 9858 * bits at each step. 9859 */ 9860 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4); 9861 for (;;) { 9862 uint64_t descriptor; 9863 bool nstable; 9864 9865 descaddr |= (address >> (stride * (4 - level))) & indexmask; 9866 descaddr &= ~7ULL; 9867 nstable = extract32(tableattrs, 4, 1); 9868 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi); 9869 if (fi->type != ARMFault_None) { 9870 goto do_fault; 9871 } 9872 9873 if (!(descriptor & 1) || 9874 (!(descriptor & 2) && (level == 3))) { 9875 /* Invalid, or the Reserved level 3 encoding */ 9876 goto do_fault; 9877 } 9878 descaddr = descriptor & descaddrmask; 9879 9880 if ((descriptor & 2) && (level < 3)) { 9881 /* Table entry. The top five bits are attributes which may 9882 * propagate down through lower levels of the table (and 9883 * which are all arranged so that 0 means "no effect", so 9884 * we can gather them up by ORing in the bits at each level). 9885 */ 9886 tableattrs |= extract64(descriptor, 59, 5); 9887 level++; 9888 indexmask = indexmask_grainsize; 9889 continue; 9890 } 9891 /* Block entry at level 1 or 2, or page entry at level 3. 9892 * These are basically the same thing, although the number 9893 * of bits we pull in from the vaddr varies. 9894 */ 9895 page_size = (1ULL << ((stride * (4 - level)) + 3)); 9896 descaddr |= (address & (page_size - 1)); 9897 /* Extract attributes from the descriptor */ 9898 attrs = extract64(descriptor, 2, 10) 9899 | (extract64(descriptor, 52, 12) << 10); 9900 9901 if (mmu_idx == ARMMMUIdx_S2NS) { 9902 /* Stage 2 table descriptors do not include any attribute fields */ 9903 break; 9904 } 9905 /* Merge in attributes from table descriptors */ 9906 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */ 9907 attrs |= extract32(tableattrs, 3, 1) << 5; /* APTable[1] => AP[2] */ 9908 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1 9909 * means "force PL1 access only", which means forcing AP[1] to 0. 9910 */ 9911 if (extract32(tableattrs, 2, 1)) { 9912 attrs &= ~(1 << 4); 9913 } 9914 attrs |= nstable << 3; /* NS */ 9915 break; 9916 } 9917 /* Here descaddr is the final physical address, and attributes 9918 * are all in attrs. 9919 */ 9920 fault_type = ARMFault_AccessFlag; 9921 if ((attrs & (1 << 8)) == 0) { 9922 /* Access flag */ 9923 goto do_fault; 9924 } 9925 9926 ap = extract32(attrs, 4, 2); 9927 xn = extract32(attrs, 12, 1); 9928 9929 if (mmu_idx == ARMMMUIdx_S2NS) { 9930 ns = true; 9931 *prot = get_S2prot(env, ap, xn); 9932 } else { 9933 ns = extract32(attrs, 3, 1); 9934 pxn = extract32(attrs, 11, 1); 9935 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn); 9936 } 9937 9938 fault_type = ARMFault_Permission; 9939 if (!(*prot & (1 << access_type))) { 9940 goto do_fault; 9941 } 9942 9943 if (ns) { 9944 /* The NS bit will (as required by the architecture) have no effect if 9945 * the CPU doesn't support TZ or this is a non-secure translation 9946 * regime, because the attribute will already be non-secure. 9947 */ 9948 txattrs->secure = false; 9949 } 9950 9951 if (cacheattrs != NULL) { 9952 if (mmu_idx == ARMMMUIdx_S2NS) { 9953 cacheattrs->attrs = convert_stage2_attrs(env, 9954 extract32(attrs, 0, 4)); 9955 } else { 9956 /* Index into MAIR registers for cache attributes */ 9957 uint8_t attrindx = extract32(attrs, 0, 3); 9958 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)]; 9959 assert(attrindx <= 7); 9960 cacheattrs->attrs = extract64(mair, attrindx * 8, 8); 9961 } 9962 cacheattrs->shareability = extract32(attrs, 6, 2); 9963 } 9964 9965 *phys_ptr = descaddr; 9966 *page_size_ptr = page_size; 9967 return false; 9968 9969 do_fault: 9970 fi->type = fault_type; 9971 fi->level = level; 9972 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */ 9973 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS); 9974 return true; 9975 } 9976 9977 static inline void get_phys_addr_pmsav7_default(CPUARMState *env, 9978 ARMMMUIdx mmu_idx, 9979 int32_t address, int *prot) 9980 { 9981 if (!arm_feature(env, ARM_FEATURE_M)) { 9982 *prot = PAGE_READ | PAGE_WRITE; 9983 switch (address) { 9984 case 0xF0000000 ... 0xFFFFFFFF: 9985 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { 9986 /* hivecs execing is ok */ 9987 *prot |= PAGE_EXEC; 9988 } 9989 break; 9990 case 0x00000000 ... 0x7FFFFFFF: 9991 *prot |= PAGE_EXEC; 9992 break; 9993 } 9994 } else { 9995 /* Default system address map for M profile cores. 9996 * The architecture specifies which regions are execute-never; 9997 * at the MPU level no other checks are defined. 9998 */ 9999 switch (address) { 10000 case 0x00000000 ... 0x1fffffff: /* ROM */ 10001 case 0x20000000 ... 0x3fffffff: /* SRAM */ 10002 case 0x60000000 ... 0x7fffffff: /* RAM */ 10003 case 0x80000000 ... 0x9fffffff: /* RAM */ 10004 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 10005 break; 10006 case 0x40000000 ... 0x5fffffff: /* Peripheral */ 10007 case 0xa0000000 ... 0xbfffffff: /* Device */ 10008 case 0xc0000000 ... 0xdfffffff: /* Device */ 10009 case 0xe0000000 ... 0xffffffff: /* System */ 10010 *prot = PAGE_READ | PAGE_WRITE; 10011 break; 10012 default: 10013 g_assert_not_reached(); 10014 } 10015 } 10016 } 10017 10018 static bool pmsav7_use_background_region(ARMCPU *cpu, 10019 ARMMMUIdx mmu_idx, bool is_user) 10020 { 10021 /* Return true if we should use the default memory map as a 10022 * "background" region if there are no hits against any MPU regions. 10023 */ 10024 CPUARMState *env = &cpu->env; 10025 10026 if (is_user) { 10027 return false; 10028 } 10029 10030 if (arm_feature(env, ARM_FEATURE_M)) { 10031 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] 10032 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK; 10033 } else { 10034 return regime_sctlr(env, mmu_idx) & SCTLR_BR; 10035 } 10036 } 10037 10038 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address) 10039 { 10040 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */ 10041 return arm_feature(env, ARM_FEATURE_M) && 10042 extract32(address, 20, 12) == 0xe00; 10043 } 10044 10045 static inline bool m_is_system_region(CPUARMState *env, uint32_t address) 10046 { 10047 /* True if address is in the M profile system region 10048 * 0xe0000000 - 0xffffffff 10049 */ 10050 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7; 10051 } 10052 10053 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address, 10054 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10055 hwaddr *phys_ptr, int *prot, 10056 target_ulong *page_size, 10057 ARMMMUFaultInfo *fi) 10058 { 10059 ARMCPU *cpu = arm_env_get_cpu(env); 10060 int n; 10061 bool is_user = regime_is_user(env, mmu_idx); 10062 10063 *phys_ptr = address; 10064 *page_size = TARGET_PAGE_SIZE; 10065 *prot = 0; 10066 10067 if (regime_translation_disabled(env, mmu_idx) || 10068 m_is_ppb_region(env, address)) { 10069 /* MPU disabled or M profile PPB access: use default memory map. 10070 * The other case which uses the default memory map in the 10071 * v7M ARM ARM pseudocode is exception vector reads from the vector 10072 * table. In QEMU those accesses are done in arm_v7m_load_vector(), 10073 * which always does a direct read using address_space_ldl(), rather 10074 * than going via this function, so we don't need to check that here. 10075 */ 10076 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 10077 } else { /* MPU enabled */ 10078 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 10079 /* region search */ 10080 uint32_t base = env->pmsav7.drbar[n]; 10081 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5); 10082 uint32_t rmask; 10083 bool srdis = false; 10084 10085 if (!(env->pmsav7.drsr[n] & 0x1)) { 10086 continue; 10087 } 10088 10089 if (!rsize) { 10090 qemu_log_mask(LOG_GUEST_ERROR, 10091 "DRSR[%d]: Rsize field cannot be 0\n", n); 10092 continue; 10093 } 10094 rsize++; 10095 rmask = (1ull << rsize) - 1; 10096 10097 if (base & rmask) { 10098 qemu_log_mask(LOG_GUEST_ERROR, 10099 "DRBAR[%d]: 0x%" PRIx32 " misaligned " 10100 "to DRSR region size, mask = 0x%" PRIx32 "\n", 10101 n, base, rmask); 10102 continue; 10103 } 10104 10105 if (address < base || address > base + rmask) { 10106 /* 10107 * Address not in this region. We must check whether the 10108 * region covers addresses in the same page as our address. 10109 * In that case we must not report a size that covers the 10110 * whole page for a subsequent hit against a different MPU 10111 * region or the background region, because it would result in 10112 * incorrect TLB hits for subsequent accesses to addresses that 10113 * are in this MPU region. 10114 */ 10115 if (ranges_overlap(base, rmask, 10116 address & TARGET_PAGE_MASK, 10117 TARGET_PAGE_SIZE)) { 10118 *page_size = 1; 10119 } 10120 continue; 10121 } 10122 10123 /* Region matched */ 10124 10125 if (rsize >= 8) { /* no subregions for regions < 256 bytes */ 10126 int i, snd; 10127 uint32_t srdis_mask; 10128 10129 rsize -= 3; /* sub region size (power of 2) */ 10130 snd = ((address - base) >> rsize) & 0x7; 10131 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1); 10132 10133 srdis_mask = srdis ? 0x3 : 0x0; 10134 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) { 10135 /* This will check in groups of 2, 4 and then 8, whether 10136 * the subregion bits are consistent. rsize is incremented 10137 * back up to give the region size, considering consistent 10138 * adjacent subregions as one region. Stop testing if rsize 10139 * is already big enough for an entire QEMU page. 10140 */ 10141 int snd_rounded = snd & ~(i - 1); 10142 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n], 10143 snd_rounded + 8, i); 10144 if (srdis_mask ^ srdis_multi) { 10145 break; 10146 } 10147 srdis_mask = (srdis_mask << i) | srdis_mask; 10148 rsize++; 10149 } 10150 } 10151 if (srdis) { 10152 continue; 10153 } 10154 if (rsize < TARGET_PAGE_BITS) { 10155 *page_size = 1 << rsize; 10156 } 10157 break; 10158 } 10159 10160 if (n == -1) { /* no hits */ 10161 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 10162 /* background fault */ 10163 fi->type = ARMFault_Background; 10164 return true; 10165 } 10166 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 10167 } else { /* a MPU hit! */ 10168 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3); 10169 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1); 10170 10171 if (m_is_system_region(env, address)) { 10172 /* System space is always execute never */ 10173 xn = 1; 10174 } 10175 10176 if (is_user) { /* User mode AP bit decoding */ 10177 switch (ap) { 10178 case 0: 10179 case 1: 10180 case 5: 10181 break; /* no access */ 10182 case 3: 10183 *prot |= PAGE_WRITE; 10184 /* fall through */ 10185 case 2: 10186 case 6: 10187 *prot |= PAGE_READ | PAGE_EXEC; 10188 break; 10189 case 7: 10190 /* for v7M, same as 6; for R profile a reserved value */ 10191 if (arm_feature(env, ARM_FEATURE_M)) { 10192 *prot |= PAGE_READ | PAGE_EXEC; 10193 break; 10194 } 10195 /* fall through */ 10196 default: 10197 qemu_log_mask(LOG_GUEST_ERROR, 10198 "DRACR[%d]: Bad value for AP bits: 0x%" 10199 PRIx32 "\n", n, ap); 10200 } 10201 } else { /* Priv. mode AP bits decoding */ 10202 switch (ap) { 10203 case 0: 10204 break; /* no access */ 10205 case 1: 10206 case 2: 10207 case 3: 10208 *prot |= PAGE_WRITE; 10209 /* fall through */ 10210 case 5: 10211 case 6: 10212 *prot |= PAGE_READ | PAGE_EXEC; 10213 break; 10214 case 7: 10215 /* for v7M, same as 6; for R profile a reserved value */ 10216 if (arm_feature(env, ARM_FEATURE_M)) { 10217 *prot |= PAGE_READ | PAGE_EXEC; 10218 break; 10219 } 10220 /* fall through */ 10221 default: 10222 qemu_log_mask(LOG_GUEST_ERROR, 10223 "DRACR[%d]: Bad value for AP bits: 0x%" 10224 PRIx32 "\n", n, ap); 10225 } 10226 } 10227 10228 /* execute never */ 10229 if (xn) { 10230 *prot &= ~PAGE_EXEC; 10231 } 10232 } 10233 } 10234 10235 fi->type = ARMFault_Permission; 10236 fi->level = 1; 10237 return !(*prot & (1 << access_type)); 10238 } 10239 10240 static bool v8m_is_sau_exempt(CPUARMState *env, 10241 uint32_t address, MMUAccessType access_type) 10242 { 10243 /* The architecture specifies that certain address ranges are 10244 * exempt from v8M SAU/IDAU checks. 10245 */ 10246 return 10247 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) || 10248 (address >= 0xe0000000 && address <= 0xe0002fff) || 10249 (address >= 0xe000e000 && address <= 0xe000efff) || 10250 (address >= 0xe002e000 && address <= 0xe002efff) || 10251 (address >= 0xe0040000 && address <= 0xe0041fff) || 10252 (address >= 0xe00ff000 && address <= 0xe00fffff); 10253 } 10254 10255 static void v8m_security_lookup(CPUARMState *env, uint32_t address, 10256 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10257 V8M_SAttributes *sattrs) 10258 { 10259 /* Look up the security attributes for this address. Compare the 10260 * pseudocode SecurityCheck() function. 10261 * We assume the caller has zero-initialized *sattrs. 10262 */ 10263 ARMCPU *cpu = arm_env_get_cpu(env); 10264 int r; 10265 bool idau_exempt = false, idau_ns = true, idau_nsc = true; 10266 int idau_region = IREGION_NOTVALID; 10267 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 10268 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 10269 10270 if (cpu->idau) { 10271 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau); 10272 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau); 10273 10274 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns, 10275 &idau_nsc); 10276 } 10277 10278 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) { 10279 /* 0xf0000000..0xffffffff is always S for insn fetches */ 10280 return; 10281 } 10282 10283 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) { 10284 sattrs->ns = !regime_is_secure(env, mmu_idx); 10285 return; 10286 } 10287 10288 if (idau_region != IREGION_NOTVALID) { 10289 sattrs->irvalid = true; 10290 sattrs->iregion = idau_region; 10291 } 10292 10293 switch (env->sau.ctrl & 3) { 10294 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */ 10295 break; 10296 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */ 10297 sattrs->ns = true; 10298 break; 10299 default: /* SAU.ENABLE == 1 */ 10300 for (r = 0; r < cpu->sau_sregion; r++) { 10301 if (env->sau.rlar[r] & 1) { 10302 uint32_t base = env->sau.rbar[r] & ~0x1f; 10303 uint32_t limit = env->sau.rlar[r] | 0x1f; 10304 10305 if (base <= address && limit >= address) { 10306 if (base > addr_page_base || limit < addr_page_limit) { 10307 sattrs->subpage = true; 10308 } 10309 if (sattrs->srvalid) { 10310 /* If we hit in more than one region then we must report 10311 * as Secure, not NS-Callable, with no valid region 10312 * number info. 10313 */ 10314 sattrs->ns = false; 10315 sattrs->nsc = false; 10316 sattrs->sregion = 0; 10317 sattrs->srvalid = false; 10318 break; 10319 } else { 10320 if (env->sau.rlar[r] & 2) { 10321 sattrs->nsc = true; 10322 } else { 10323 sattrs->ns = true; 10324 } 10325 sattrs->srvalid = true; 10326 sattrs->sregion = r; 10327 } 10328 } else { 10329 /* 10330 * Address not in this region. We must check whether the 10331 * region covers addresses in the same page as our address. 10332 * In that case we must not report a size that covers the 10333 * whole page for a subsequent hit against a different MPU 10334 * region or the background region, because it would result 10335 * in incorrect TLB hits for subsequent accesses to 10336 * addresses that are in this MPU region. 10337 */ 10338 if (limit >= base && 10339 ranges_overlap(base, limit - base + 1, 10340 addr_page_base, 10341 TARGET_PAGE_SIZE)) { 10342 sattrs->subpage = true; 10343 } 10344 } 10345 } 10346 } 10347 10348 /* The IDAU will override the SAU lookup results if it specifies 10349 * higher security than the SAU does. 10350 */ 10351 if (!idau_ns) { 10352 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) { 10353 sattrs->ns = false; 10354 sattrs->nsc = idau_nsc; 10355 } 10356 } 10357 break; 10358 } 10359 } 10360 10361 static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address, 10362 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10363 hwaddr *phys_ptr, MemTxAttrs *txattrs, 10364 int *prot, bool *is_subpage, 10365 ARMMMUFaultInfo *fi, uint32_t *mregion) 10366 { 10367 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check 10368 * that a full phys-to-virt translation does). 10369 * mregion is (if not NULL) set to the region number which matched, 10370 * or -1 if no region number is returned (MPU off, address did not 10371 * hit a region, address hit in multiple regions). 10372 * We set is_subpage to true if the region hit doesn't cover the 10373 * entire TARGET_PAGE the address is within. 10374 */ 10375 ARMCPU *cpu = arm_env_get_cpu(env); 10376 bool is_user = regime_is_user(env, mmu_idx); 10377 uint32_t secure = regime_is_secure(env, mmu_idx); 10378 int n; 10379 int matchregion = -1; 10380 bool hit = false; 10381 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 10382 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 10383 10384 *is_subpage = false; 10385 *phys_ptr = address; 10386 *prot = 0; 10387 if (mregion) { 10388 *mregion = -1; 10389 } 10390 10391 /* Unlike the ARM ARM pseudocode, we don't need to check whether this 10392 * was an exception vector read from the vector table (which is always 10393 * done using the default system address map), because those accesses 10394 * are done in arm_v7m_load_vector(), which always does a direct 10395 * read using address_space_ldl(), rather than going via this function. 10396 */ 10397 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */ 10398 hit = true; 10399 } else if (m_is_ppb_region(env, address)) { 10400 hit = true; 10401 } else if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 10402 hit = true; 10403 } else { 10404 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 10405 /* region search */ 10406 /* Note that the base address is bits [31:5] from the register 10407 * with bits [4:0] all zeroes, but the limit address is bits 10408 * [31:5] from the register with bits [4:0] all ones. 10409 */ 10410 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f; 10411 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f; 10412 10413 if (!(env->pmsav8.rlar[secure][n] & 0x1)) { 10414 /* Region disabled */ 10415 continue; 10416 } 10417 10418 if (address < base || address > limit) { 10419 /* 10420 * Address not in this region. We must check whether the 10421 * region covers addresses in the same page as our address. 10422 * In that case we must not report a size that covers the 10423 * whole page for a subsequent hit against a different MPU 10424 * region or the background region, because it would result in 10425 * incorrect TLB hits for subsequent accesses to addresses that 10426 * are in this MPU region. 10427 */ 10428 if (limit >= base && 10429 ranges_overlap(base, limit - base + 1, 10430 addr_page_base, 10431 TARGET_PAGE_SIZE)) { 10432 *is_subpage = true; 10433 } 10434 continue; 10435 } 10436 10437 if (base > addr_page_base || limit < addr_page_limit) { 10438 *is_subpage = true; 10439 } 10440 10441 if (hit) { 10442 /* Multiple regions match -- always a failure (unlike 10443 * PMSAv7 where highest-numbered-region wins) 10444 */ 10445 fi->type = ARMFault_Permission; 10446 fi->level = 1; 10447 return true; 10448 } 10449 10450 matchregion = n; 10451 hit = true; 10452 } 10453 } 10454 10455 if (!hit) { 10456 /* background fault */ 10457 fi->type = ARMFault_Background; 10458 return true; 10459 } 10460 10461 if (matchregion == -1) { 10462 /* hit using the background region */ 10463 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 10464 } else { 10465 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2); 10466 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1); 10467 10468 if (m_is_system_region(env, address)) { 10469 /* System space is always execute never */ 10470 xn = 1; 10471 } 10472 10473 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap); 10474 if (*prot && !xn) { 10475 *prot |= PAGE_EXEC; 10476 } 10477 /* We don't need to look the attribute up in the MAIR0/MAIR1 10478 * registers because that only tells us about cacheability. 10479 */ 10480 if (mregion) { 10481 *mregion = matchregion; 10482 } 10483 } 10484 10485 fi->type = ARMFault_Permission; 10486 fi->level = 1; 10487 return !(*prot & (1 << access_type)); 10488 } 10489 10490 10491 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address, 10492 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10493 hwaddr *phys_ptr, MemTxAttrs *txattrs, 10494 int *prot, target_ulong *page_size, 10495 ARMMMUFaultInfo *fi) 10496 { 10497 uint32_t secure = regime_is_secure(env, mmu_idx); 10498 V8M_SAttributes sattrs = {}; 10499 bool ret; 10500 bool mpu_is_subpage; 10501 10502 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 10503 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs); 10504 if (access_type == MMU_INST_FETCH) { 10505 /* Instruction fetches always use the MMU bank and the 10506 * transaction attribute determined by the fetch address, 10507 * regardless of CPU state. This is painful for QEMU 10508 * to handle, because it would mean we need to encode 10509 * into the mmu_idx not just the (user, negpri) information 10510 * for the current security state but also that for the 10511 * other security state, which would balloon the number 10512 * of mmu_idx values needed alarmingly. 10513 * Fortunately we can avoid this because it's not actually 10514 * possible to arbitrarily execute code from memory with 10515 * the wrong security attribute: it will always generate 10516 * an exception of some kind or another, apart from the 10517 * special case of an NS CPU executing an SG instruction 10518 * in S&NSC memory. So we always just fail the translation 10519 * here and sort things out in the exception handler 10520 * (including possibly emulating an SG instruction). 10521 */ 10522 if (sattrs.ns != !secure) { 10523 if (sattrs.nsc) { 10524 fi->type = ARMFault_QEMU_NSCExec; 10525 } else { 10526 fi->type = ARMFault_QEMU_SFault; 10527 } 10528 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 10529 *phys_ptr = address; 10530 *prot = 0; 10531 return true; 10532 } 10533 } else { 10534 /* For data accesses we always use the MMU bank indicated 10535 * by the current CPU state, but the security attributes 10536 * might downgrade a secure access to nonsecure. 10537 */ 10538 if (sattrs.ns) { 10539 txattrs->secure = false; 10540 } else if (!secure) { 10541 /* NS access to S memory must fault. 10542 * Architecturally we should first check whether the 10543 * MPU information for this address indicates that we 10544 * are doing an unaligned access to Device memory, which 10545 * should generate a UsageFault instead. QEMU does not 10546 * currently check for that kind of unaligned access though. 10547 * If we added it we would need to do so as a special case 10548 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt(). 10549 */ 10550 fi->type = ARMFault_QEMU_SFault; 10551 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 10552 *phys_ptr = address; 10553 *prot = 0; 10554 return true; 10555 } 10556 } 10557 } 10558 10559 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr, 10560 txattrs, prot, &mpu_is_subpage, fi, NULL); 10561 /* 10562 * TODO: this is a temporary hack to ignore the fact that the SAU region 10563 * is smaller than a page if this is an executable region. We never 10564 * supported small MPU regions, but we did (accidentally) allow small 10565 * SAU regions, and if we now made small SAU regions not be executable 10566 * then this would break previously working guest code. We can't 10567 * remove this until/unless we implement support for execution from 10568 * small regions. 10569 */ 10570 if (*prot & PAGE_EXEC) { 10571 sattrs.subpage = false; 10572 } 10573 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE; 10574 return ret; 10575 } 10576 10577 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address, 10578 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10579 hwaddr *phys_ptr, int *prot, 10580 ARMMMUFaultInfo *fi) 10581 { 10582 int n; 10583 uint32_t mask; 10584 uint32_t base; 10585 bool is_user = regime_is_user(env, mmu_idx); 10586 10587 if (regime_translation_disabled(env, mmu_idx)) { 10588 /* MPU disabled. */ 10589 *phys_ptr = address; 10590 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 10591 return false; 10592 } 10593 10594 *phys_ptr = address; 10595 for (n = 7; n >= 0; n--) { 10596 base = env->cp15.c6_region[n]; 10597 if ((base & 1) == 0) { 10598 continue; 10599 } 10600 mask = 1 << ((base >> 1) & 0x1f); 10601 /* Keep this shift separate from the above to avoid an 10602 (undefined) << 32. */ 10603 mask = (mask << 1) - 1; 10604 if (((base ^ address) & ~mask) == 0) { 10605 break; 10606 } 10607 } 10608 if (n < 0) { 10609 fi->type = ARMFault_Background; 10610 return true; 10611 } 10612 10613 if (access_type == MMU_INST_FETCH) { 10614 mask = env->cp15.pmsav5_insn_ap; 10615 } else { 10616 mask = env->cp15.pmsav5_data_ap; 10617 } 10618 mask = (mask >> (n * 4)) & 0xf; 10619 switch (mask) { 10620 case 0: 10621 fi->type = ARMFault_Permission; 10622 fi->level = 1; 10623 return true; 10624 case 1: 10625 if (is_user) { 10626 fi->type = ARMFault_Permission; 10627 fi->level = 1; 10628 return true; 10629 } 10630 *prot = PAGE_READ | PAGE_WRITE; 10631 break; 10632 case 2: 10633 *prot = PAGE_READ; 10634 if (!is_user) { 10635 *prot |= PAGE_WRITE; 10636 } 10637 break; 10638 case 3: 10639 *prot = PAGE_READ | PAGE_WRITE; 10640 break; 10641 case 5: 10642 if (is_user) { 10643 fi->type = ARMFault_Permission; 10644 fi->level = 1; 10645 return true; 10646 } 10647 *prot = PAGE_READ; 10648 break; 10649 case 6: 10650 *prot = PAGE_READ; 10651 break; 10652 default: 10653 /* Bad permission. */ 10654 fi->type = ARMFault_Permission; 10655 fi->level = 1; 10656 return true; 10657 } 10658 *prot |= PAGE_EXEC; 10659 return false; 10660 } 10661 10662 /* Combine either inner or outer cacheability attributes for normal 10663 * memory, according to table D4-42 and pseudocode procedure 10664 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM). 10665 * 10666 * NB: only stage 1 includes allocation hints (RW bits), leading to 10667 * some asymmetry. 10668 */ 10669 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2) 10670 { 10671 if (s1 == 4 || s2 == 4) { 10672 /* non-cacheable has precedence */ 10673 return 4; 10674 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) { 10675 /* stage 1 write-through takes precedence */ 10676 return s1; 10677 } else if (extract32(s2, 2, 2) == 2) { 10678 /* stage 2 write-through takes precedence, but the allocation hint 10679 * is still taken from stage 1 10680 */ 10681 return (2 << 2) | extract32(s1, 0, 2); 10682 } else { /* write-back */ 10683 return s1; 10684 } 10685 } 10686 10687 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4 10688 * and CombineS1S2Desc() 10689 * 10690 * @s1: Attributes from stage 1 walk 10691 * @s2: Attributes from stage 2 walk 10692 */ 10693 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2) 10694 { 10695 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4); 10696 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4); 10697 ARMCacheAttrs ret; 10698 10699 /* Combine shareability attributes (table D4-43) */ 10700 if (s1.shareability == 2 || s2.shareability == 2) { 10701 /* if either are outer-shareable, the result is outer-shareable */ 10702 ret.shareability = 2; 10703 } else if (s1.shareability == 3 || s2.shareability == 3) { 10704 /* if either are inner-shareable, the result is inner-shareable */ 10705 ret.shareability = 3; 10706 } else { 10707 /* both non-shareable */ 10708 ret.shareability = 0; 10709 } 10710 10711 /* Combine memory type and cacheability attributes */ 10712 if (s1hi == 0 || s2hi == 0) { 10713 /* Device has precedence over normal */ 10714 if (s1lo == 0 || s2lo == 0) { 10715 /* nGnRnE has precedence over anything */ 10716 ret.attrs = 0; 10717 } else if (s1lo == 4 || s2lo == 4) { 10718 /* non-Reordering has precedence over Reordering */ 10719 ret.attrs = 4; /* nGnRE */ 10720 } else if (s1lo == 8 || s2lo == 8) { 10721 /* non-Gathering has precedence over Gathering */ 10722 ret.attrs = 8; /* nGRE */ 10723 } else { 10724 ret.attrs = 0xc; /* GRE */ 10725 } 10726 10727 /* Any location for which the resultant memory type is any 10728 * type of Device memory is always treated as Outer Shareable. 10729 */ 10730 ret.shareability = 2; 10731 } else { /* Normal memory */ 10732 /* Outer/inner cacheability combine independently */ 10733 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4 10734 | combine_cacheattr_nibble(s1lo, s2lo); 10735 10736 if (ret.attrs == 0x44) { 10737 /* Any location for which the resultant memory type is Normal 10738 * Inner Non-cacheable, Outer Non-cacheable is always treated 10739 * as Outer Shareable. 10740 */ 10741 ret.shareability = 2; 10742 } 10743 } 10744 10745 return ret; 10746 } 10747 10748 10749 /* get_phys_addr - get the physical address for this virtual address 10750 * 10751 * Find the physical address corresponding to the given virtual address, 10752 * by doing a translation table walk on MMU based systems or using the 10753 * MPU state on MPU based systems. 10754 * 10755 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs, 10756 * prot and page_size may not be filled in, and the populated fsr value provides 10757 * information on why the translation aborted, in the format of a 10758 * DFSR/IFSR fault register, with the following caveats: 10759 * * we honour the short vs long DFSR format differences. 10760 * * the WnR bit is never set (the caller must do this). 10761 * * for PSMAv5 based systems we don't bother to return a full FSR format 10762 * value. 10763 * 10764 * @env: CPUARMState 10765 * @address: virtual address to get physical address for 10766 * @access_type: 0 for read, 1 for write, 2 for execute 10767 * @mmu_idx: MMU index indicating required translation regime 10768 * @phys_ptr: set to the physical address corresponding to the virtual address 10769 * @attrs: set to the memory transaction attributes to use 10770 * @prot: set to the permissions for the page containing phys_ptr 10771 * @page_size: set to the size of the page containing phys_ptr 10772 * @fi: set to fault info if the translation fails 10773 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes 10774 */ 10775 static bool get_phys_addr(CPUARMState *env, target_ulong address, 10776 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10777 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 10778 target_ulong *page_size, 10779 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 10780 { 10781 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 10782 /* Call ourselves recursively to do the stage 1 and then stage 2 10783 * translations. 10784 */ 10785 if (arm_feature(env, ARM_FEATURE_EL2)) { 10786 hwaddr ipa; 10787 int s2_prot; 10788 int ret; 10789 ARMCacheAttrs cacheattrs2 = {}; 10790 10791 ret = get_phys_addr(env, address, access_type, 10792 stage_1_mmu_idx(mmu_idx), &ipa, attrs, 10793 prot, page_size, fi, cacheattrs); 10794 10795 /* If S1 fails or S2 is disabled, return early. */ 10796 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) { 10797 *phys_ptr = ipa; 10798 return ret; 10799 } 10800 10801 /* S1 is done. Now do S2 translation. */ 10802 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS, 10803 phys_ptr, attrs, &s2_prot, 10804 page_size, fi, 10805 cacheattrs != NULL ? &cacheattrs2 : NULL); 10806 fi->s2addr = ipa; 10807 /* Combine the S1 and S2 perms. */ 10808 *prot &= s2_prot; 10809 10810 /* Combine the S1 and S2 cache attributes, if needed */ 10811 if (!ret && cacheattrs != NULL) { 10812 if (env->cp15.hcr_el2 & HCR_DC) { 10813 /* 10814 * HCR.DC forces the first stage attributes to 10815 * Normal Non-Shareable, 10816 * Inner Write-Back Read-Allocate Write-Allocate, 10817 * Outer Write-Back Read-Allocate Write-Allocate. 10818 */ 10819 cacheattrs->attrs = 0xff; 10820 cacheattrs->shareability = 0; 10821 } 10822 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2); 10823 } 10824 10825 return ret; 10826 } else { 10827 /* 10828 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1. 10829 */ 10830 mmu_idx = stage_1_mmu_idx(mmu_idx); 10831 } 10832 } 10833 10834 /* The page table entries may downgrade secure to non-secure, but 10835 * cannot upgrade an non-secure translation regime's attributes 10836 * to secure. 10837 */ 10838 attrs->secure = regime_is_secure(env, mmu_idx); 10839 attrs->user = regime_is_user(env, mmu_idx); 10840 10841 /* Fast Context Switch Extension. This doesn't exist at all in v8. 10842 * In v7 and earlier it affects all stage 1 translations. 10843 */ 10844 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS 10845 && !arm_feature(env, ARM_FEATURE_V8)) { 10846 if (regime_el(env, mmu_idx) == 3) { 10847 address += env->cp15.fcseidr_s; 10848 } else { 10849 address += env->cp15.fcseidr_ns; 10850 } 10851 } 10852 10853 if (arm_feature(env, ARM_FEATURE_PMSA)) { 10854 bool ret; 10855 *page_size = TARGET_PAGE_SIZE; 10856 10857 if (arm_feature(env, ARM_FEATURE_V8)) { 10858 /* PMSAv8 */ 10859 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx, 10860 phys_ptr, attrs, prot, page_size, fi); 10861 } else if (arm_feature(env, ARM_FEATURE_V7)) { 10862 /* PMSAv7 */ 10863 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx, 10864 phys_ptr, prot, page_size, fi); 10865 } else { 10866 /* Pre-v7 MPU */ 10867 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx, 10868 phys_ptr, prot, fi); 10869 } 10870 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32 10871 " mmu_idx %u -> %s (prot %c%c%c)\n", 10872 access_type == MMU_DATA_LOAD ? "reading" : 10873 (access_type == MMU_DATA_STORE ? "writing" : "execute"), 10874 (uint32_t)address, mmu_idx, 10875 ret ? "Miss" : "Hit", 10876 *prot & PAGE_READ ? 'r' : '-', 10877 *prot & PAGE_WRITE ? 'w' : '-', 10878 *prot & PAGE_EXEC ? 'x' : '-'); 10879 10880 return ret; 10881 } 10882 10883 /* Definitely a real MMU, not an MPU */ 10884 10885 if (regime_translation_disabled(env, mmu_idx)) { 10886 /* MMU disabled. */ 10887 *phys_ptr = address; 10888 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 10889 *page_size = TARGET_PAGE_SIZE; 10890 return 0; 10891 } 10892 10893 if (regime_using_lpae_format(env, mmu_idx)) { 10894 return get_phys_addr_lpae(env, address, access_type, mmu_idx, 10895 phys_ptr, attrs, prot, page_size, 10896 fi, cacheattrs); 10897 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) { 10898 return get_phys_addr_v6(env, address, access_type, mmu_idx, 10899 phys_ptr, attrs, prot, page_size, fi); 10900 } else { 10901 return get_phys_addr_v5(env, address, access_type, mmu_idx, 10902 phys_ptr, prot, page_size, fi); 10903 } 10904 } 10905 10906 /* Walk the page table and (if the mapping exists) add the page 10907 * to the TLB. Return false on success, or true on failure. Populate 10908 * fsr with ARM DFSR/IFSR fault register format value on failure. 10909 */ 10910 bool arm_tlb_fill(CPUState *cs, vaddr address, 10911 MMUAccessType access_type, int mmu_idx, 10912 ARMMMUFaultInfo *fi) 10913 { 10914 ARMCPU *cpu = ARM_CPU(cs); 10915 CPUARMState *env = &cpu->env; 10916 hwaddr phys_addr; 10917 target_ulong page_size; 10918 int prot; 10919 int ret; 10920 MemTxAttrs attrs = {}; 10921 10922 ret = get_phys_addr(env, address, access_type, 10923 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr, 10924 &attrs, &prot, &page_size, fi, NULL); 10925 if (!ret) { 10926 /* 10927 * Map a single [sub]page. Regions smaller than our declared 10928 * target page size are handled specially, so for those we 10929 * pass in the exact addresses. 10930 */ 10931 if (page_size >= TARGET_PAGE_SIZE) { 10932 phys_addr &= TARGET_PAGE_MASK; 10933 address &= TARGET_PAGE_MASK; 10934 } 10935 tlb_set_page_with_attrs(cs, address, phys_addr, attrs, 10936 prot, mmu_idx, page_size); 10937 return 0; 10938 } 10939 10940 return ret; 10941 } 10942 10943 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr, 10944 MemTxAttrs *attrs) 10945 { 10946 ARMCPU *cpu = ARM_CPU(cs); 10947 CPUARMState *env = &cpu->env; 10948 hwaddr phys_addr; 10949 target_ulong page_size; 10950 int prot; 10951 bool ret; 10952 ARMMMUFaultInfo fi = {}; 10953 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false)); 10954 10955 *attrs = (MemTxAttrs) {}; 10956 10957 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr, 10958 attrs, &prot, &page_size, &fi, NULL); 10959 10960 if (ret) { 10961 return -1; 10962 } 10963 return phys_addr; 10964 } 10965 10966 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) 10967 { 10968 uint32_t mask; 10969 unsigned el = arm_current_el(env); 10970 10971 /* First handle registers which unprivileged can read */ 10972 10973 switch (reg) { 10974 case 0 ... 7: /* xPSR sub-fields */ 10975 mask = 0; 10976 if ((reg & 1) && el) { 10977 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */ 10978 } 10979 if (!(reg & 4)) { 10980 mask |= XPSR_NZCV | XPSR_Q; /* APSR */ 10981 } 10982 /* EPSR reads as zero */ 10983 return xpsr_read(env) & mask; 10984 break; 10985 case 20: /* CONTROL */ 10986 return env->v7m.control[env->v7m.secure]; 10987 case 0x94: /* CONTROL_NS */ 10988 /* We have to handle this here because unprivileged Secure code 10989 * can read the NS CONTROL register. 10990 */ 10991 if (!env->v7m.secure) { 10992 return 0; 10993 } 10994 return env->v7m.control[M_REG_NS]; 10995 } 10996 10997 if (el == 0) { 10998 return 0; /* unprivileged reads others as zero */ 10999 } 11000 11001 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 11002 switch (reg) { 11003 case 0x88: /* MSP_NS */ 11004 if (!env->v7m.secure) { 11005 return 0; 11006 } 11007 return env->v7m.other_ss_msp; 11008 case 0x89: /* PSP_NS */ 11009 if (!env->v7m.secure) { 11010 return 0; 11011 } 11012 return env->v7m.other_ss_psp; 11013 case 0x8a: /* MSPLIM_NS */ 11014 if (!env->v7m.secure) { 11015 return 0; 11016 } 11017 return env->v7m.msplim[M_REG_NS]; 11018 case 0x8b: /* PSPLIM_NS */ 11019 if (!env->v7m.secure) { 11020 return 0; 11021 } 11022 return env->v7m.psplim[M_REG_NS]; 11023 case 0x90: /* PRIMASK_NS */ 11024 if (!env->v7m.secure) { 11025 return 0; 11026 } 11027 return env->v7m.primask[M_REG_NS]; 11028 case 0x91: /* BASEPRI_NS */ 11029 if (!env->v7m.secure) { 11030 return 0; 11031 } 11032 return env->v7m.basepri[M_REG_NS]; 11033 case 0x93: /* FAULTMASK_NS */ 11034 if (!env->v7m.secure) { 11035 return 0; 11036 } 11037 return env->v7m.faultmask[M_REG_NS]; 11038 case 0x98: /* SP_NS */ 11039 { 11040 /* This gives the non-secure SP selected based on whether we're 11041 * currently in handler mode or not, using the NS CONTROL.SPSEL. 11042 */ 11043 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; 11044 11045 if (!env->v7m.secure) { 11046 return 0; 11047 } 11048 if (!arm_v7m_is_handler_mode(env) && spsel) { 11049 return env->v7m.other_ss_psp; 11050 } else { 11051 return env->v7m.other_ss_msp; 11052 } 11053 } 11054 default: 11055 break; 11056 } 11057 } 11058 11059 switch (reg) { 11060 case 8: /* MSP */ 11061 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13]; 11062 case 9: /* PSP */ 11063 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp; 11064 case 10: /* MSPLIM */ 11065 if (!arm_feature(env, ARM_FEATURE_V8)) { 11066 goto bad_reg; 11067 } 11068 return env->v7m.msplim[env->v7m.secure]; 11069 case 11: /* PSPLIM */ 11070 if (!arm_feature(env, ARM_FEATURE_V8)) { 11071 goto bad_reg; 11072 } 11073 return env->v7m.psplim[env->v7m.secure]; 11074 case 16: /* PRIMASK */ 11075 return env->v7m.primask[env->v7m.secure]; 11076 case 17: /* BASEPRI */ 11077 case 18: /* BASEPRI_MAX */ 11078 return env->v7m.basepri[env->v7m.secure]; 11079 case 19: /* FAULTMASK */ 11080 return env->v7m.faultmask[env->v7m.secure]; 11081 default: 11082 bad_reg: 11083 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special" 11084 " register %d\n", reg); 11085 return 0; 11086 } 11087 } 11088 11089 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val) 11090 { 11091 /* We're passed bits [11..0] of the instruction; extract 11092 * SYSm and the mask bits. 11093 * Invalid combinations of SYSm and mask are UNPREDICTABLE; 11094 * we choose to treat them as if the mask bits were valid. 11095 * NB that the pseudocode 'mask' variable is bits [11..10], 11096 * whereas ours is [11..8]. 11097 */ 11098 uint32_t mask = extract32(maskreg, 8, 4); 11099 uint32_t reg = extract32(maskreg, 0, 8); 11100 11101 if (arm_current_el(env) == 0 && reg > 7) { 11102 /* only xPSR sub-fields may be written by unprivileged */ 11103 return; 11104 } 11105 11106 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 11107 switch (reg) { 11108 case 0x88: /* MSP_NS */ 11109 if (!env->v7m.secure) { 11110 return; 11111 } 11112 env->v7m.other_ss_msp = val; 11113 return; 11114 case 0x89: /* PSP_NS */ 11115 if (!env->v7m.secure) { 11116 return; 11117 } 11118 env->v7m.other_ss_psp = val; 11119 return; 11120 case 0x8a: /* MSPLIM_NS */ 11121 if (!env->v7m.secure) { 11122 return; 11123 } 11124 env->v7m.msplim[M_REG_NS] = val & ~7; 11125 return; 11126 case 0x8b: /* PSPLIM_NS */ 11127 if (!env->v7m.secure) { 11128 return; 11129 } 11130 env->v7m.psplim[M_REG_NS] = val & ~7; 11131 return; 11132 case 0x90: /* PRIMASK_NS */ 11133 if (!env->v7m.secure) { 11134 return; 11135 } 11136 env->v7m.primask[M_REG_NS] = val & 1; 11137 return; 11138 case 0x91: /* BASEPRI_NS */ 11139 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { 11140 return; 11141 } 11142 env->v7m.basepri[M_REG_NS] = val & 0xff; 11143 return; 11144 case 0x93: /* FAULTMASK_NS */ 11145 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { 11146 return; 11147 } 11148 env->v7m.faultmask[M_REG_NS] = val & 1; 11149 return; 11150 case 0x94: /* CONTROL_NS */ 11151 if (!env->v7m.secure) { 11152 return; 11153 } 11154 write_v7m_control_spsel_for_secstate(env, 11155 val & R_V7M_CONTROL_SPSEL_MASK, 11156 M_REG_NS); 11157 if (arm_feature(env, ARM_FEATURE_M_MAIN)) { 11158 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK; 11159 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK; 11160 } 11161 return; 11162 case 0x98: /* SP_NS */ 11163 { 11164 /* This gives the non-secure SP selected based on whether we're 11165 * currently in handler mode or not, using the NS CONTROL.SPSEL. 11166 */ 11167 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; 11168 bool is_psp = !arm_v7m_is_handler_mode(env) && spsel; 11169 uint32_t limit; 11170 11171 if (!env->v7m.secure) { 11172 return; 11173 } 11174 11175 limit = is_psp ? env->v7m.psplim[false] : env->v7m.msplim[false]; 11176 11177 if (val < limit) { 11178 CPUState *cs = CPU(arm_env_get_cpu(env)); 11179 11180 cpu_restore_state(cs, GETPC(), true); 11181 raise_exception(env, EXCP_STKOF, 0, 1); 11182 } 11183 11184 if (is_psp) { 11185 env->v7m.other_ss_psp = val; 11186 } else { 11187 env->v7m.other_ss_msp = val; 11188 } 11189 return; 11190 } 11191 default: 11192 break; 11193 } 11194 } 11195 11196 switch (reg) { 11197 case 0 ... 7: /* xPSR sub-fields */ 11198 /* only APSR is actually writable */ 11199 if (!(reg & 4)) { 11200 uint32_t apsrmask = 0; 11201 11202 if (mask & 8) { 11203 apsrmask |= XPSR_NZCV | XPSR_Q; 11204 } 11205 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) { 11206 apsrmask |= XPSR_GE; 11207 } 11208 xpsr_write(env, val, apsrmask); 11209 } 11210 break; 11211 case 8: /* MSP */ 11212 if (v7m_using_psp(env)) { 11213 env->v7m.other_sp = val; 11214 } else { 11215 env->regs[13] = val; 11216 } 11217 break; 11218 case 9: /* PSP */ 11219 if (v7m_using_psp(env)) { 11220 env->regs[13] = val; 11221 } else { 11222 env->v7m.other_sp = val; 11223 } 11224 break; 11225 case 10: /* MSPLIM */ 11226 if (!arm_feature(env, ARM_FEATURE_V8)) { 11227 goto bad_reg; 11228 } 11229 env->v7m.msplim[env->v7m.secure] = val & ~7; 11230 break; 11231 case 11: /* PSPLIM */ 11232 if (!arm_feature(env, ARM_FEATURE_V8)) { 11233 goto bad_reg; 11234 } 11235 env->v7m.psplim[env->v7m.secure] = val & ~7; 11236 break; 11237 case 16: /* PRIMASK */ 11238 env->v7m.primask[env->v7m.secure] = val & 1; 11239 break; 11240 case 17: /* BASEPRI */ 11241 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 11242 goto bad_reg; 11243 } 11244 env->v7m.basepri[env->v7m.secure] = val & 0xff; 11245 break; 11246 case 18: /* BASEPRI_MAX */ 11247 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 11248 goto bad_reg; 11249 } 11250 val &= 0xff; 11251 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure] 11252 || env->v7m.basepri[env->v7m.secure] == 0)) { 11253 env->v7m.basepri[env->v7m.secure] = val; 11254 } 11255 break; 11256 case 19: /* FAULTMASK */ 11257 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 11258 goto bad_reg; 11259 } 11260 env->v7m.faultmask[env->v7m.secure] = val & 1; 11261 break; 11262 case 20: /* CONTROL */ 11263 /* Writing to the SPSEL bit only has an effect if we are in 11264 * thread mode; other bits can be updated by any privileged code. 11265 * write_v7m_control_spsel() deals with updating the SPSEL bit in 11266 * env->v7m.control, so we only need update the others. 11267 * For v7M, we must just ignore explicit writes to SPSEL in handler 11268 * mode; for v8M the write is permitted but will have no effect. 11269 */ 11270 if (arm_feature(env, ARM_FEATURE_V8) || 11271 !arm_v7m_is_handler_mode(env)) { 11272 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0); 11273 } 11274 if (arm_feature(env, ARM_FEATURE_M_MAIN)) { 11275 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK; 11276 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK; 11277 } 11278 break; 11279 default: 11280 bad_reg: 11281 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special" 11282 " register %d\n", reg); 11283 return; 11284 } 11285 } 11286 11287 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) 11288 { 11289 /* Implement the TT instruction. op is bits [7:6] of the insn. */ 11290 bool forceunpriv = op & 1; 11291 bool alt = op & 2; 11292 V8M_SAttributes sattrs = {}; 11293 uint32_t tt_resp; 11294 bool r, rw, nsr, nsrw, mrvalid; 11295 int prot; 11296 ARMMMUFaultInfo fi = {}; 11297 MemTxAttrs attrs = {}; 11298 hwaddr phys_addr; 11299 ARMMMUIdx mmu_idx; 11300 uint32_t mregion; 11301 bool targetpriv; 11302 bool targetsec = env->v7m.secure; 11303 bool is_subpage; 11304 11305 /* Work out what the security state and privilege level we're 11306 * interested in is... 11307 */ 11308 if (alt) { 11309 targetsec = !targetsec; 11310 } 11311 11312 if (forceunpriv) { 11313 targetpriv = false; 11314 } else { 11315 targetpriv = arm_v7m_is_handler_mode(env) || 11316 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK); 11317 } 11318 11319 /* ...and then figure out which MMU index this is */ 11320 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv); 11321 11322 /* We know that the MPU and SAU don't care about the access type 11323 * for our purposes beyond that we don't want to claim to be 11324 * an insn fetch, so we arbitrarily call this a read. 11325 */ 11326 11327 /* MPU region info only available for privileged or if 11328 * inspecting the other MPU state. 11329 */ 11330 if (arm_current_el(env) != 0 || alt) { 11331 /* We can ignore the return value as prot is always set */ 11332 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, 11333 &phys_addr, &attrs, &prot, &is_subpage, 11334 &fi, &mregion); 11335 if (mregion == -1) { 11336 mrvalid = false; 11337 mregion = 0; 11338 } else { 11339 mrvalid = true; 11340 } 11341 r = prot & PAGE_READ; 11342 rw = prot & PAGE_WRITE; 11343 } else { 11344 r = false; 11345 rw = false; 11346 mrvalid = false; 11347 mregion = 0; 11348 } 11349 11350 if (env->v7m.secure) { 11351 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); 11352 nsr = sattrs.ns && r; 11353 nsrw = sattrs.ns && rw; 11354 } else { 11355 sattrs.ns = true; 11356 nsr = false; 11357 nsrw = false; 11358 } 11359 11360 tt_resp = (sattrs.iregion << 24) | 11361 (sattrs.irvalid << 23) | 11362 ((!sattrs.ns) << 22) | 11363 (nsrw << 21) | 11364 (nsr << 20) | 11365 (rw << 19) | 11366 (r << 18) | 11367 (sattrs.srvalid << 17) | 11368 (mrvalid << 16) | 11369 (sattrs.sregion << 8) | 11370 mregion; 11371 11372 return tt_resp; 11373 } 11374 11375 #endif 11376 11377 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in) 11378 { 11379 /* Implement DC ZVA, which zeroes a fixed-length block of memory. 11380 * Note that we do not implement the (architecturally mandated) 11381 * alignment fault for attempts to use this on Device memory 11382 * (which matches the usual QEMU behaviour of not implementing either 11383 * alignment faults or any memory attribute handling). 11384 */ 11385 11386 ARMCPU *cpu = arm_env_get_cpu(env); 11387 uint64_t blocklen = 4 << cpu->dcz_blocksize; 11388 uint64_t vaddr = vaddr_in & ~(blocklen - 1); 11389 11390 #ifndef CONFIG_USER_ONLY 11391 { 11392 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than 11393 * the block size so we might have to do more than one TLB lookup. 11394 * We know that in fact for any v8 CPU the page size is at least 4K 11395 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only 11396 * 1K as an artefact of legacy v5 subpage support being present in the 11397 * same QEMU executable. 11398 */ 11399 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE); 11400 void *hostaddr[maxidx]; 11401 int try, i; 11402 unsigned mmu_idx = cpu_mmu_index(env, false); 11403 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx); 11404 11405 for (try = 0; try < 2; try++) { 11406 11407 for (i = 0; i < maxidx; i++) { 11408 hostaddr[i] = tlb_vaddr_to_host(env, 11409 vaddr + TARGET_PAGE_SIZE * i, 11410 1, mmu_idx); 11411 if (!hostaddr[i]) { 11412 break; 11413 } 11414 } 11415 if (i == maxidx) { 11416 /* If it's all in the TLB it's fair game for just writing to; 11417 * we know we don't need to update dirty status, etc. 11418 */ 11419 for (i = 0; i < maxidx - 1; i++) { 11420 memset(hostaddr[i], 0, TARGET_PAGE_SIZE); 11421 } 11422 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE)); 11423 return; 11424 } 11425 /* OK, try a store and see if we can populate the tlb. This 11426 * might cause an exception if the memory isn't writable, 11427 * in which case we will longjmp out of here. We must for 11428 * this purpose use the actual register value passed to us 11429 * so that we get the fault address right. 11430 */ 11431 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC()); 11432 /* Now we can populate the other TLB entries, if any */ 11433 for (i = 0; i < maxidx; i++) { 11434 uint64_t va = vaddr + TARGET_PAGE_SIZE * i; 11435 if (va != (vaddr_in & TARGET_PAGE_MASK)) { 11436 helper_ret_stb_mmu(env, va, 0, oi, GETPC()); 11437 } 11438 } 11439 } 11440 11441 /* Slow path (probably attempt to do this to an I/O device or 11442 * similar, or clearing of a block of code we have translations 11443 * cached for). Just do a series of byte writes as the architecture 11444 * demands. It's not worth trying to use a cpu_physical_memory_map(), 11445 * memset(), unmap() sequence here because: 11446 * + we'd need to account for the blocksize being larger than a page 11447 * + the direct-RAM access case is almost always going to be dealt 11448 * with in the fastpath code above, so there's no speed benefit 11449 * + we would have to deal with the map returning NULL because the 11450 * bounce buffer was in use 11451 */ 11452 for (i = 0; i < blocklen; i++) { 11453 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC()); 11454 } 11455 } 11456 #else 11457 memset(g2h(vaddr), 0, blocklen); 11458 #endif 11459 } 11460 11461 /* Note that signed overflow is undefined in C. The following routines are 11462 careful to use unsigned types where modulo arithmetic is required. 11463 Failure to do so _will_ break on newer gcc. */ 11464 11465 /* Signed saturating arithmetic. */ 11466 11467 /* Perform 16-bit signed saturating addition. */ 11468 static inline uint16_t add16_sat(uint16_t a, uint16_t b) 11469 { 11470 uint16_t res; 11471 11472 res = a + b; 11473 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { 11474 if (a & 0x8000) 11475 res = 0x8000; 11476 else 11477 res = 0x7fff; 11478 } 11479 return res; 11480 } 11481 11482 /* Perform 8-bit signed saturating addition. */ 11483 static inline uint8_t add8_sat(uint8_t a, uint8_t b) 11484 { 11485 uint8_t res; 11486 11487 res = a + b; 11488 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { 11489 if (a & 0x80) 11490 res = 0x80; 11491 else 11492 res = 0x7f; 11493 } 11494 return res; 11495 } 11496 11497 /* Perform 16-bit signed saturating subtraction. */ 11498 static inline uint16_t sub16_sat(uint16_t a, uint16_t b) 11499 { 11500 uint16_t res; 11501 11502 res = a - b; 11503 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { 11504 if (a & 0x8000) 11505 res = 0x8000; 11506 else 11507 res = 0x7fff; 11508 } 11509 return res; 11510 } 11511 11512 /* Perform 8-bit signed saturating subtraction. */ 11513 static inline uint8_t sub8_sat(uint8_t a, uint8_t b) 11514 { 11515 uint8_t res; 11516 11517 res = a - b; 11518 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { 11519 if (a & 0x80) 11520 res = 0x80; 11521 else 11522 res = 0x7f; 11523 } 11524 return res; 11525 } 11526 11527 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); 11528 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); 11529 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); 11530 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); 11531 #define PFX q 11532 11533 #include "op_addsub.h" 11534 11535 /* Unsigned saturating arithmetic. */ 11536 static inline uint16_t add16_usat(uint16_t a, uint16_t b) 11537 { 11538 uint16_t res; 11539 res = a + b; 11540 if (res < a) 11541 res = 0xffff; 11542 return res; 11543 } 11544 11545 static inline uint16_t sub16_usat(uint16_t a, uint16_t b) 11546 { 11547 if (a > b) 11548 return a - b; 11549 else 11550 return 0; 11551 } 11552 11553 static inline uint8_t add8_usat(uint8_t a, uint8_t b) 11554 { 11555 uint8_t res; 11556 res = a + b; 11557 if (res < a) 11558 res = 0xff; 11559 return res; 11560 } 11561 11562 static inline uint8_t sub8_usat(uint8_t a, uint8_t b) 11563 { 11564 if (a > b) 11565 return a - b; 11566 else 11567 return 0; 11568 } 11569 11570 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); 11571 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); 11572 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); 11573 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); 11574 #define PFX uq 11575 11576 #include "op_addsub.h" 11577 11578 /* Signed modulo arithmetic. */ 11579 #define SARITH16(a, b, n, op) do { \ 11580 int32_t sum; \ 11581 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ 11582 RESULT(sum, n, 16); \ 11583 if (sum >= 0) \ 11584 ge |= 3 << (n * 2); \ 11585 } while(0) 11586 11587 #define SARITH8(a, b, n, op) do { \ 11588 int32_t sum; \ 11589 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ 11590 RESULT(sum, n, 8); \ 11591 if (sum >= 0) \ 11592 ge |= 1 << n; \ 11593 } while(0) 11594 11595 11596 #define ADD16(a, b, n) SARITH16(a, b, n, +) 11597 #define SUB16(a, b, n) SARITH16(a, b, n, -) 11598 #define ADD8(a, b, n) SARITH8(a, b, n, +) 11599 #define SUB8(a, b, n) SARITH8(a, b, n, -) 11600 #define PFX s 11601 #define ARITH_GE 11602 11603 #include "op_addsub.h" 11604 11605 /* Unsigned modulo arithmetic. */ 11606 #define ADD16(a, b, n) do { \ 11607 uint32_t sum; \ 11608 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ 11609 RESULT(sum, n, 16); \ 11610 if ((sum >> 16) == 1) \ 11611 ge |= 3 << (n * 2); \ 11612 } while(0) 11613 11614 #define ADD8(a, b, n) do { \ 11615 uint32_t sum; \ 11616 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ 11617 RESULT(sum, n, 8); \ 11618 if ((sum >> 8) == 1) \ 11619 ge |= 1 << n; \ 11620 } while(0) 11621 11622 #define SUB16(a, b, n) do { \ 11623 uint32_t sum; \ 11624 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ 11625 RESULT(sum, n, 16); \ 11626 if ((sum >> 16) == 0) \ 11627 ge |= 3 << (n * 2); \ 11628 } while(0) 11629 11630 #define SUB8(a, b, n) do { \ 11631 uint32_t sum; \ 11632 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ 11633 RESULT(sum, n, 8); \ 11634 if ((sum >> 8) == 0) \ 11635 ge |= 1 << n; \ 11636 } while(0) 11637 11638 #define PFX u 11639 #define ARITH_GE 11640 11641 #include "op_addsub.h" 11642 11643 /* Halved signed arithmetic. */ 11644 #define ADD16(a, b, n) \ 11645 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) 11646 #define SUB16(a, b, n) \ 11647 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) 11648 #define ADD8(a, b, n) \ 11649 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) 11650 #define SUB8(a, b, n) \ 11651 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) 11652 #define PFX sh 11653 11654 #include "op_addsub.h" 11655 11656 /* Halved unsigned arithmetic. */ 11657 #define ADD16(a, b, n) \ 11658 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) 11659 #define SUB16(a, b, n) \ 11660 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) 11661 #define ADD8(a, b, n) \ 11662 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) 11663 #define SUB8(a, b, n) \ 11664 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) 11665 #define PFX uh 11666 11667 #include "op_addsub.h" 11668 11669 static inline uint8_t do_usad(uint8_t a, uint8_t b) 11670 { 11671 if (a > b) 11672 return a - b; 11673 else 11674 return b - a; 11675 } 11676 11677 /* Unsigned sum of absolute byte differences. */ 11678 uint32_t HELPER(usad8)(uint32_t a, uint32_t b) 11679 { 11680 uint32_t sum; 11681 sum = do_usad(a, b); 11682 sum += do_usad(a >> 8, b >> 8); 11683 sum += do_usad(a >> 16, b >>16); 11684 sum += do_usad(a >> 24, b >> 24); 11685 return sum; 11686 } 11687 11688 /* For ARMv6 SEL instruction. */ 11689 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) 11690 { 11691 uint32_t mask; 11692 11693 mask = 0; 11694 if (flags & 1) 11695 mask |= 0xff; 11696 if (flags & 2) 11697 mask |= 0xff00; 11698 if (flags & 4) 11699 mask |= 0xff0000; 11700 if (flags & 8) 11701 mask |= 0xff000000; 11702 return (a & mask) | (b & ~mask); 11703 } 11704 11705 /* VFP support. We follow the convention used for VFP instructions: 11706 Single precision routines have a "s" suffix, double precision a 11707 "d" suffix. */ 11708 11709 /* Convert host exception flags to vfp form. */ 11710 static inline int vfp_exceptbits_from_host(int host_bits) 11711 { 11712 int target_bits = 0; 11713 11714 if (host_bits & float_flag_invalid) 11715 target_bits |= 1; 11716 if (host_bits & float_flag_divbyzero) 11717 target_bits |= 2; 11718 if (host_bits & float_flag_overflow) 11719 target_bits |= 4; 11720 if (host_bits & (float_flag_underflow | float_flag_output_denormal)) 11721 target_bits |= 8; 11722 if (host_bits & float_flag_inexact) 11723 target_bits |= 0x10; 11724 if (host_bits & float_flag_input_denormal) 11725 target_bits |= 0x80; 11726 return target_bits; 11727 } 11728 11729 uint32_t HELPER(vfp_get_fpscr)(CPUARMState *env) 11730 { 11731 int i; 11732 uint32_t fpscr; 11733 11734 fpscr = (env->vfp.xregs[ARM_VFP_FPSCR] & 0xffc8ffff) 11735 | (env->vfp.vec_len << 16) 11736 | (env->vfp.vec_stride << 20); 11737 11738 i = get_float_exception_flags(&env->vfp.fp_status); 11739 i |= get_float_exception_flags(&env->vfp.standard_fp_status); 11740 /* FZ16 does not generate an input denormal exception. */ 11741 i |= (get_float_exception_flags(&env->vfp.fp_status_f16) 11742 & ~float_flag_input_denormal); 11743 11744 fpscr |= vfp_exceptbits_from_host(i); 11745 return fpscr; 11746 } 11747 11748 uint32_t vfp_get_fpscr(CPUARMState *env) 11749 { 11750 return HELPER(vfp_get_fpscr)(env); 11751 } 11752 11753 /* Convert vfp exception flags to target form. */ 11754 static inline int vfp_exceptbits_to_host(int target_bits) 11755 { 11756 int host_bits = 0; 11757 11758 if (target_bits & 1) 11759 host_bits |= float_flag_invalid; 11760 if (target_bits & 2) 11761 host_bits |= float_flag_divbyzero; 11762 if (target_bits & 4) 11763 host_bits |= float_flag_overflow; 11764 if (target_bits & 8) 11765 host_bits |= float_flag_underflow; 11766 if (target_bits & 0x10) 11767 host_bits |= float_flag_inexact; 11768 if (target_bits & 0x80) 11769 host_bits |= float_flag_input_denormal; 11770 return host_bits; 11771 } 11772 11773 void HELPER(vfp_set_fpscr)(CPUARMState *env, uint32_t val) 11774 { 11775 int i; 11776 uint32_t changed; 11777 11778 /* When ARMv8.2-FP16 is not supported, FZ16 is RES0. */ 11779 if (!cpu_isar_feature(aa64_fp16, arm_env_get_cpu(env))) { 11780 val &= ~FPCR_FZ16; 11781 } 11782 11783 changed = env->vfp.xregs[ARM_VFP_FPSCR]; 11784 env->vfp.xregs[ARM_VFP_FPSCR] = (val & 0xffc8ffff); 11785 env->vfp.vec_len = (val >> 16) & 7; 11786 env->vfp.vec_stride = (val >> 20) & 3; 11787 11788 changed ^= val; 11789 if (changed & (3 << 22)) { 11790 i = (val >> 22) & 3; 11791 switch (i) { 11792 case FPROUNDING_TIEEVEN: 11793 i = float_round_nearest_even; 11794 break; 11795 case FPROUNDING_POSINF: 11796 i = float_round_up; 11797 break; 11798 case FPROUNDING_NEGINF: 11799 i = float_round_down; 11800 break; 11801 case FPROUNDING_ZERO: 11802 i = float_round_to_zero; 11803 break; 11804 } 11805 set_float_rounding_mode(i, &env->vfp.fp_status); 11806 set_float_rounding_mode(i, &env->vfp.fp_status_f16); 11807 } 11808 if (changed & FPCR_FZ16) { 11809 bool ftz_enabled = val & FPCR_FZ16; 11810 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status_f16); 11811 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status_f16); 11812 } 11813 if (changed & FPCR_FZ) { 11814 bool ftz_enabled = val & FPCR_FZ; 11815 set_flush_to_zero(ftz_enabled, &env->vfp.fp_status); 11816 set_flush_inputs_to_zero(ftz_enabled, &env->vfp.fp_status); 11817 } 11818 if (changed & FPCR_DN) { 11819 bool dnan_enabled = val & FPCR_DN; 11820 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status); 11821 set_default_nan_mode(dnan_enabled, &env->vfp.fp_status_f16); 11822 } 11823 11824 /* The exception flags are ORed together when we read fpscr so we 11825 * only need to preserve the current state in one of our 11826 * float_status values. 11827 */ 11828 i = vfp_exceptbits_to_host(val); 11829 set_float_exception_flags(i, &env->vfp.fp_status); 11830 set_float_exception_flags(0, &env->vfp.fp_status_f16); 11831 set_float_exception_flags(0, &env->vfp.standard_fp_status); 11832 } 11833 11834 void vfp_set_fpscr(CPUARMState *env, uint32_t val) 11835 { 11836 HELPER(vfp_set_fpscr)(env, val); 11837 } 11838 11839 #define VFP_HELPER(name, p) HELPER(glue(glue(vfp_,name),p)) 11840 11841 #define VFP_BINOP(name) \ 11842 float32 VFP_HELPER(name, s)(float32 a, float32 b, void *fpstp) \ 11843 { \ 11844 float_status *fpst = fpstp; \ 11845 return float32_ ## name(a, b, fpst); \ 11846 } \ 11847 float64 VFP_HELPER(name, d)(float64 a, float64 b, void *fpstp) \ 11848 { \ 11849 float_status *fpst = fpstp; \ 11850 return float64_ ## name(a, b, fpst); \ 11851 } 11852 VFP_BINOP(add) 11853 VFP_BINOP(sub) 11854 VFP_BINOP(mul) 11855 VFP_BINOP(div) 11856 VFP_BINOP(min) 11857 VFP_BINOP(max) 11858 VFP_BINOP(minnum) 11859 VFP_BINOP(maxnum) 11860 #undef VFP_BINOP 11861 11862 float32 VFP_HELPER(neg, s)(float32 a) 11863 { 11864 return float32_chs(a); 11865 } 11866 11867 float64 VFP_HELPER(neg, d)(float64 a) 11868 { 11869 return float64_chs(a); 11870 } 11871 11872 float32 VFP_HELPER(abs, s)(float32 a) 11873 { 11874 return float32_abs(a); 11875 } 11876 11877 float64 VFP_HELPER(abs, d)(float64 a) 11878 { 11879 return float64_abs(a); 11880 } 11881 11882 float32 VFP_HELPER(sqrt, s)(float32 a, CPUARMState *env) 11883 { 11884 return float32_sqrt(a, &env->vfp.fp_status); 11885 } 11886 11887 float64 VFP_HELPER(sqrt, d)(float64 a, CPUARMState *env) 11888 { 11889 return float64_sqrt(a, &env->vfp.fp_status); 11890 } 11891 11892 /* XXX: check quiet/signaling case */ 11893 #define DO_VFP_cmp(p, type) \ 11894 void VFP_HELPER(cmp, p)(type a, type b, CPUARMState *env) \ 11895 { \ 11896 uint32_t flags; \ 11897 switch(type ## _compare_quiet(a, b, &env->vfp.fp_status)) { \ 11898 case 0: flags = 0x6; break; \ 11899 case -1: flags = 0x8; break; \ 11900 case 1: flags = 0x2; break; \ 11901 default: case 2: flags = 0x3; break; \ 11902 } \ 11903 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ 11904 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ 11905 } \ 11906 void VFP_HELPER(cmpe, p)(type a, type b, CPUARMState *env) \ 11907 { \ 11908 uint32_t flags; \ 11909 switch(type ## _compare(a, b, &env->vfp.fp_status)) { \ 11910 case 0: flags = 0x6; break; \ 11911 case -1: flags = 0x8; break; \ 11912 case 1: flags = 0x2; break; \ 11913 default: case 2: flags = 0x3; break; \ 11914 } \ 11915 env->vfp.xregs[ARM_VFP_FPSCR] = (flags << 28) \ 11916 | (env->vfp.xregs[ARM_VFP_FPSCR] & 0x0fffffff); \ 11917 } 11918 DO_VFP_cmp(s, float32) 11919 DO_VFP_cmp(d, float64) 11920 #undef DO_VFP_cmp 11921 11922 /* Integer to float and float to integer conversions */ 11923 11924 #define CONV_ITOF(name, ftype, fsz, sign) \ 11925 ftype HELPER(name)(uint32_t x, void *fpstp) \ 11926 { \ 11927 float_status *fpst = fpstp; \ 11928 return sign##int32_to_##float##fsz((sign##int32_t)x, fpst); \ 11929 } 11930 11931 #define CONV_FTOI(name, ftype, fsz, sign, round) \ 11932 sign##int32_t HELPER(name)(ftype x, void *fpstp) \ 11933 { \ 11934 float_status *fpst = fpstp; \ 11935 if (float##fsz##_is_any_nan(x)) { \ 11936 float_raise(float_flag_invalid, fpst); \ 11937 return 0; \ 11938 } \ 11939 return float##fsz##_to_##sign##int32##round(x, fpst); \ 11940 } 11941 11942 #define FLOAT_CONVS(name, p, ftype, fsz, sign) \ 11943 CONV_ITOF(vfp_##name##to##p, ftype, fsz, sign) \ 11944 CONV_FTOI(vfp_to##name##p, ftype, fsz, sign, ) \ 11945 CONV_FTOI(vfp_to##name##z##p, ftype, fsz, sign, _round_to_zero) 11946 11947 FLOAT_CONVS(si, h, uint32_t, 16, ) 11948 FLOAT_CONVS(si, s, float32, 32, ) 11949 FLOAT_CONVS(si, d, float64, 64, ) 11950 FLOAT_CONVS(ui, h, uint32_t, 16, u) 11951 FLOAT_CONVS(ui, s, float32, 32, u) 11952 FLOAT_CONVS(ui, d, float64, 64, u) 11953 11954 #undef CONV_ITOF 11955 #undef CONV_FTOI 11956 #undef FLOAT_CONVS 11957 11958 /* floating point conversion */ 11959 float64 VFP_HELPER(fcvtd, s)(float32 x, CPUARMState *env) 11960 { 11961 return float32_to_float64(x, &env->vfp.fp_status); 11962 } 11963 11964 float32 VFP_HELPER(fcvts, d)(float64 x, CPUARMState *env) 11965 { 11966 return float64_to_float32(x, &env->vfp.fp_status); 11967 } 11968 11969 /* VFP3 fixed point conversion. */ 11970 #define VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ 11971 float##fsz HELPER(vfp_##name##to##p)(uint##isz##_t x, uint32_t shift, \ 11972 void *fpstp) \ 11973 { return itype##_to_##float##fsz##_scalbn(x, -shift, fpstp); } 11974 11975 #define VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, ROUND, suff) \ 11976 uint##isz##_t HELPER(vfp_to##name##p##suff)(float##fsz x, uint32_t shift, \ 11977 void *fpst) \ 11978 { \ 11979 if (unlikely(float##fsz##_is_any_nan(x))) { \ 11980 float_raise(float_flag_invalid, fpst); \ 11981 return 0; \ 11982 } \ 11983 return float##fsz##_to_##itype##_scalbn(x, ROUND, shift, fpst); \ 11984 } 11985 11986 #define VFP_CONV_FIX(name, p, fsz, isz, itype) \ 11987 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ 11988 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \ 11989 float_round_to_zero, _round_to_zero) \ 11990 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \ 11991 get_float_rounding_mode(fpst), ) 11992 11993 #define VFP_CONV_FIX_A64(name, p, fsz, isz, itype) \ 11994 VFP_CONV_FIX_FLOAT(name, p, fsz, isz, itype) \ 11995 VFP_CONV_FLOAT_FIX_ROUND(name, p, fsz, isz, itype, \ 11996 get_float_rounding_mode(fpst), ) 11997 11998 VFP_CONV_FIX(sh, d, 64, 64, int16) 11999 VFP_CONV_FIX(sl, d, 64, 64, int32) 12000 VFP_CONV_FIX_A64(sq, d, 64, 64, int64) 12001 VFP_CONV_FIX(uh, d, 64, 64, uint16) 12002 VFP_CONV_FIX(ul, d, 64, 64, uint32) 12003 VFP_CONV_FIX_A64(uq, d, 64, 64, uint64) 12004 VFP_CONV_FIX(sh, s, 32, 32, int16) 12005 VFP_CONV_FIX(sl, s, 32, 32, int32) 12006 VFP_CONV_FIX_A64(sq, s, 32, 64, int64) 12007 VFP_CONV_FIX(uh, s, 32, 32, uint16) 12008 VFP_CONV_FIX(ul, s, 32, 32, uint32) 12009 VFP_CONV_FIX_A64(uq, s, 32, 64, uint64) 12010 12011 #undef VFP_CONV_FIX 12012 #undef VFP_CONV_FIX_FLOAT 12013 #undef VFP_CONV_FLOAT_FIX_ROUND 12014 #undef VFP_CONV_FIX_A64 12015 12016 uint32_t HELPER(vfp_sltoh)(uint32_t x, uint32_t shift, void *fpst) 12017 { 12018 return int32_to_float16_scalbn(x, -shift, fpst); 12019 } 12020 12021 uint32_t HELPER(vfp_ultoh)(uint32_t x, uint32_t shift, void *fpst) 12022 { 12023 return uint32_to_float16_scalbn(x, -shift, fpst); 12024 } 12025 12026 uint32_t HELPER(vfp_sqtoh)(uint64_t x, uint32_t shift, void *fpst) 12027 { 12028 return int64_to_float16_scalbn(x, -shift, fpst); 12029 } 12030 12031 uint32_t HELPER(vfp_uqtoh)(uint64_t x, uint32_t shift, void *fpst) 12032 { 12033 return uint64_to_float16_scalbn(x, -shift, fpst); 12034 } 12035 12036 uint32_t HELPER(vfp_toshh)(uint32_t x, uint32_t shift, void *fpst) 12037 { 12038 if (unlikely(float16_is_any_nan(x))) { 12039 float_raise(float_flag_invalid, fpst); 12040 return 0; 12041 } 12042 return float16_to_int16_scalbn(x, get_float_rounding_mode(fpst), 12043 shift, fpst); 12044 } 12045 12046 uint32_t HELPER(vfp_touhh)(uint32_t x, uint32_t shift, void *fpst) 12047 { 12048 if (unlikely(float16_is_any_nan(x))) { 12049 float_raise(float_flag_invalid, fpst); 12050 return 0; 12051 } 12052 return float16_to_uint16_scalbn(x, get_float_rounding_mode(fpst), 12053 shift, fpst); 12054 } 12055 12056 uint32_t HELPER(vfp_toslh)(uint32_t x, uint32_t shift, void *fpst) 12057 { 12058 if (unlikely(float16_is_any_nan(x))) { 12059 float_raise(float_flag_invalid, fpst); 12060 return 0; 12061 } 12062 return float16_to_int32_scalbn(x, get_float_rounding_mode(fpst), 12063 shift, fpst); 12064 } 12065 12066 uint32_t HELPER(vfp_toulh)(uint32_t x, uint32_t shift, void *fpst) 12067 { 12068 if (unlikely(float16_is_any_nan(x))) { 12069 float_raise(float_flag_invalid, fpst); 12070 return 0; 12071 } 12072 return float16_to_uint32_scalbn(x, get_float_rounding_mode(fpst), 12073 shift, fpst); 12074 } 12075 12076 uint64_t HELPER(vfp_tosqh)(uint32_t x, uint32_t shift, void *fpst) 12077 { 12078 if (unlikely(float16_is_any_nan(x))) { 12079 float_raise(float_flag_invalid, fpst); 12080 return 0; 12081 } 12082 return float16_to_int64_scalbn(x, get_float_rounding_mode(fpst), 12083 shift, fpst); 12084 } 12085 12086 uint64_t HELPER(vfp_touqh)(uint32_t x, uint32_t shift, void *fpst) 12087 { 12088 if (unlikely(float16_is_any_nan(x))) { 12089 float_raise(float_flag_invalid, fpst); 12090 return 0; 12091 } 12092 return float16_to_uint64_scalbn(x, get_float_rounding_mode(fpst), 12093 shift, fpst); 12094 } 12095 12096 /* Set the current fp rounding mode and return the old one. 12097 * The argument is a softfloat float_round_ value. 12098 */ 12099 uint32_t HELPER(set_rmode)(uint32_t rmode, void *fpstp) 12100 { 12101 float_status *fp_status = fpstp; 12102 12103 uint32_t prev_rmode = get_float_rounding_mode(fp_status); 12104 set_float_rounding_mode(rmode, fp_status); 12105 12106 return prev_rmode; 12107 } 12108 12109 /* Set the current fp rounding mode in the standard fp status and return 12110 * the old one. This is for NEON instructions that need to change the 12111 * rounding mode but wish to use the standard FPSCR values for everything 12112 * else. Always set the rounding mode back to the correct value after 12113 * modifying it. 12114 * The argument is a softfloat float_round_ value. 12115 */ 12116 uint32_t HELPER(set_neon_rmode)(uint32_t rmode, CPUARMState *env) 12117 { 12118 float_status *fp_status = &env->vfp.standard_fp_status; 12119 12120 uint32_t prev_rmode = get_float_rounding_mode(fp_status); 12121 set_float_rounding_mode(rmode, fp_status); 12122 12123 return prev_rmode; 12124 } 12125 12126 /* Half precision conversions. */ 12127 float32 HELPER(vfp_fcvt_f16_to_f32)(uint32_t a, void *fpstp, uint32_t ahp_mode) 12128 { 12129 /* Squash FZ16 to 0 for the duration of conversion. In this case, 12130 * it would affect flushing input denormals. 12131 */ 12132 float_status *fpst = fpstp; 12133 flag save = get_flush_inputs_to_zero(fpst); 12134 set_flush_inputs_to_zero(false, fpst); 12135 float32 r = float16_to_float32(a, !ahp_mode, fpst); 12136 set_flush_inputs_to_zero(save, fpst); 12137 return r; 12138 } 12139 12140 uint32_t HELPER(vfp_fcvt_f32_to_f16)(float32 a, void *fpstp, uint32_t ahp_mode) 12141 { 12142 /* Squash FZ16 to 0 for the duration of conversion. In this case, 12143 * it would affect flushing output denormals. 12144 */ 12145 float_status *fpst = fpstp; 12146 flag save = get_flush_to_zero(fpst); 12147 set_flush_to_zero(false, fpst); 12148 float16 r = float32_to_float16(a, !ahp_mode, fpst); 12149 set_flush_to_zero(save, fpst); 12150 return r; 12151 } 12152 12153 float64 HELPER(vfp_fcvt_f16_to_f64)(uint32_t a, void *fpstp, uint32_t ahp_mode) 12154 { 12155 /* Squash FZ16 to 0 for the duration of conversion. In this case, 12156 * it would affect flushing input denormals. 12157 */ 12158 float_status *fpst = fpstp; 12159 flag save = get_flush_inputs_to_zero(fpst); 12160 set_flush_inputs_to_zero(false, fpst); 12161 float64 r = float16_to_float64(a, !ahp_mode, fpst); 12162 set_flush_inputs_to_zero(save, fpst); 12163 return r; 12164 } 12165 12166 uint32_t HELPER(vfp_fcvt_f64_to_f16)(float64 a, void *fpstp, uint32_t ahp_mode) 12167 { 12168 /* Squash FZ16 to 0 for the duration of conversion. In this case, 12169 * it would affect flushing output denormals. 12170 */ 12171 float_status *fpst = fpstp; 12172 flag save = get_flush_to_zero(fpst); 12173 set_flush_to_zero(false, fpst); 12174 float16 r = float64_to_float16(a, !ahp_mode, fpst); 12175 set_flush_to_zero(save, fpst); 12176 return r; 12177 } 12178 12179 #define float32_two make_float32(0x40000000) 12180 #define float32_three make_float32(0x40400000) 12181 #define float32_one_point_five make_float32(0x3fc00000) 12182 12183 float32 HELPER(recps_f32)(float32 a, float32 b, CPUARMState *env) 12184 { 12185 float_status *s = &env->vfp.standard_fp_status; 12186 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || 12187 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { 12188 if (!(float32_is_zero(a) || float32_is_zero(b))) { 12189 float_raise(float_flag_input_denormal, s); 12190 } 12191 return float32_two; 12192 } 12193 return float32_sub(float32_two, float32_mul(a, b, s), s); 12194 } 12195 12196 float32 HELPER(rsqrts_f32)(float32 a, float32 b, CPUARMState *env) 12197 { 12198 float_status *s = &env->vfp.standard_fp_status; 12199 float32 product; 12200 if ((float32_is_infinity(a) && float32_is_zero_or_denormal(b)) || 12201 (float32_is_infinity(b) && float32_is_zero_or_denormal(a))) { 12202 if (!(float32_is_zero(a) || float32_is_zero(b))) { 12203 float_raise(float_flag_input_denormal, s); 12204 } 12205 return float32_one_point_five; 12206 } 12207 product = float32_mul(a, b, s); 12208 return float32_div(float32_sub(float32_three, product, s), float32_two, s); 12209 } 12210 12211 /* NEON helpers. */ 12212 12213 /* Constants 256 and 512 are used in some helpers; we avoid relying on 12214 * int->float conversions at run-time. */ 12215 #define float64_256 make_float64(0x4070000000000000LL) 12216 #define float64_512 make_float64(0x4080000000000000LL) 12217 #define float16_maxnorm make_float16(0x7bff) 12218 #define float32_maxnorm make_float32(0x7f7fffff) 12219 #define float64_maxnorm make_float64(0x7fefffffffffffffLL) 12220 12221 /* Reciprocal functions 12222 * 12223 * The algorithm that must be used to calculate the estimate 12224 * is specified by the ARM ARM, see FPRecipEstimate()/RecipEstimate 12225 */ 12226 12227 /* See RecipEstimate() 12228 * 12229 * input is a 9 bit fixed point number 12230 * input range 256 .. 511 for a number from 0.5 <= x < 1.0. 12231 * result range 256 .. 511 for a number from 1.0 to 511/256. 12232 */ 12233 12234 static int recip_estimate(int input) 12235 { 12236 int a, b, r; 12237 assert(256 <= input && input < 512); 12238 a = (input * 2) + 1; 12239 b = (1 << 19) / a; 12240 r = (b + 1) >> 1; 12241 assert(256 <= r && r < 512); 12242 return r; 12243 } 12244 12245 /* 12246 * Common wrapper to call recip_estimate 12247 * 12248 * The parameters are exponent and 64 bit fraction (without implicit 12249 * bit) where the binary point is nominally at bit 52. Returns a 12250 * float64 which can then be rounded to the appropriate size by the 12251 * callee. 12252 */ 12253 12254 static uint64_t call_recip_estimate(int *exp, int exp_off, uint64_t frac) 12255 { 12256 uint32_t scaled, estimate; 12257 uint64_t result_frac; 12258 int result_exp; 12259 12260 /* Handle sub-normals */ 12261 if (*exp == 0) { 12262 if (extract64(frac, 51, 1) == 0) { 12263 *exp = -1; 12264 frac <<= 2; 12265 } else { 12266 frac <<= 1; 12267 } 12268 } 12269 12270 /* scaled = UInt('1':fraction<51:44>) */ 12271 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8)); 12272 estimate = recip_estimate(scaled); 12273 12274 result_exp = exp_off - *exp; 12275 result_frac = deposit64(0, 44, 8, estimate); 12276 if (result_exp == 0) { 12277 result_frac = deposit64(result_frac >> 1, 51, 1, 1); 12278 } else if (result_exp == -1) { 12279 result_frac = deposit64(result_frac >> 2, 50, 2, 1); 12280 result_exp = 0; 12281 } 12282 12283 *exp = result_exp; 12284 12285 return result_frac; 12286 } 12287 12288 static bool round_to_inf(float_status *fpst, bool sign_bit) 12289 { 12290 switch (fpst->float_rounding_mode) { 12291 case float_round_nearest_even: /* Round to Nearest */ 12292 return true; 12293 case float_round_up: /* Round to +Inf */ 12294 return !sign_bit; 12295 case float_round_down: /* Round to -Inf */ 12296 return sign_bit; 12297 case float_round_to_zero: /* Round to Zero */ 12298 return false; 12299 } 12300 12301 g_assert_not_reached(); 12302 } 12303 12304 uint32_t HELPER(recpe_f16)(uint32_t input, void *fpstp) 12305 { 12306 float_status *fpst = fpstp; 12307 float16 f16 = float16_squash_input_denormal(input, fpst); 12308 uint32_t f16_val = float16_val(f16); 12309 uint32_t f16_sign = float16_is_neg(f16); 12310 int f16_exp = extract32(f16_val, 10, 5); 12311 uint32_t f16_frac = extract32(f16_val, 0, 10); 12312 uint64_t f64_frac; 12313 12314 if (float16_is_any_nan(f16)) { 12315 float16 nan = f16; 12316 if (float16_is_signaling_nan(f16, fpst)) { 12317 float_raise(float_flag_invalid, fpst); 12318 nan = float16_silence_nan(f16, fpst); 12319 } 12320 if (fpst->default_nan_mode) { 12321 nan = float16_default_nan(fpst); 12322 } 12323 return nan; 12324 } else if (float16_is_infinity(f16)) { 12325 return float16_set_sign(float16_zero, float16_is_neg(f16)); 12326 } else if (float16_is_zero(f16)) { 12327 float_raise(float_flag_divbyzero, fpst); 12328 return float16_set_sign(float16_infinity, float16_is_neg(f16)); 12329 } else if (float16_abs(f16) < (1 << 8)) { 12330 /* Abs(value) < 2.0^-16 */ 12331 float_raise(float_flag_overflow | float_flag_inexact, fpst); 12332 if (round_to_inf(fpst, f16_sign)) { 12333 return float16_set_sign(float16_infinity, f16_sign); 12334 } else { 12335 return float16_set_sign(float16_maxnorm, f16_sign); 12336 } 12337 } else if (f16_exp >= 29 && fpst->flush_to_zero) { 12338 float_raise(float_flag_underflow, fpst); 12339 return float16_set_sign(float16_zero, float16_is_neg(f16)); 12340 } 12341 12342 f64_frac = call_recip_estimate(&f16_exp, 29, 12343 ((uint64_t) f16_frac) << (52 - 10)); 12344 12345 /* result = sign : result_exp<4:0> : fraction<51:42> */ 12346 f16_val = deposit32(0, 15, 1, f16_sign); 12347 f16_val = deposit32(f16_val, 10, 5, f16_exp); 12348 f16_val = deposit32(f16_val, 0, 10, extract64(f64_frac, 52 - 10, 10)); 12349 return make_float16(f16_val); 12350 } 12351 12352 float32 HELPER(recpe_f32)(float32 input, void *fpstp) 12353 { 12354 float_status *fpst = fpstp; 12355 float32 f32 = float32_squash_input_denormal(input, fpst); 12356 uint32_t f32_val = float32_val(f32); 12357 bool f32_sign = float32_is_neg(f32); 12358 int f32_exp = extract32(f32_val, 23, 8); 12359 uint32_t f32_frac = extract32(f32_val, 0, 23); 12360 uint64_t f64_frac; 12361 12362 if (float32_is_any_nan(f32)) { 12363 float32 nan = f32; 12364 if (float32_is_signaling_nan(f32, fpst)) { 12365 float_raise(float_flag_invalid, fpst); 12366 nan = float32_silence_nan(f32, fpst); 12367 } 12368 if (fpst->default_nan_mode) { 12369 nan = float32_default_nan(fpst); 12370 } 12371 return nan; 12372 } else if (float32_is_infinity(f32)) { 12373 return float32_set_sign(float32_zero, float32_is_neg(f32)); 12374 } else if (float32_is_zero(f32)) { 12375 float_raise(float_flag_divbyzero, fpst); 12376 return float32_set_sign(float32_infinity, float32_is_neg(f32)); 12377 } else if (float32_abs(f32) < (1ULL << 21)) { 12378 /* Abs(value) < 2.0^-128 */ 12379 float_raise(float_flag_overflow | float_flag_inexact, fpst); 12380 if (round_to_inf(fpst, f32_sign)) { 12381 return float32_set_sign(float32_infinity, f32_sign); 12382 } else { 12383 return float32_set_sign(float32_maxnorm, f32_sign); 12384 } 12385 } else if (f32_exp >= 253 && fpst->flush_to_zero) { 12386 float_raise(float_flag_underflow, fpst); 12387 return float32_set_sign(float32_zero, float32_is_neg(f32)); 12388 } 12389 12390 f64_frac = call_recip_estimate(&f32_exp, 253, 12391 ((uint64_t) f32_frac) << (52 - 23)); 12392 12393 /* result = sign : result_exp<7:0> : fraction<51:29> */ 12394 f32_val = deposit32(0, 31, 1, f32_sign); 12395 f32_val = deposit32(f32_val, 23, 8, f32_exp); 12396 f32_val = deposit32(f32_val, 0, 23, extract64(f64_frac, 52 - 23, 23)); 12397 return make_float32(f32_val); 12398 } 12399 12400 float64 HELPER(recpe_f64)(float64 input, void *fpstp) 12401 { 12402 float_status *fpst = fpstp; 12403 float64 f64 = float64_squash_input_denormal(input, fpst); 12404 uint64_t f64_val = float64_val(f64); 12405 bool f64_sign = float64_is_neg(f64); 12406 int f64_exp = extract64(f64_val, 52, 11); 12407 uint64_t f64_frac = extract64(f64_val, 0, 52); 12408 12409 /* Deal with any special cases */ 12410 if (float64_is_any_nan(f64)) { 12411 float64 nan = f64; 12412 if (float64_is_signaling_nan(f64, fpst)) { 12413 float_raise(float_flag_invalid, fpst); 12414 nan = float64_silence_nan(f64, fpst); 12415 } 12416 if (fpst->default_nan_mode) { 12417 nan = float64_default_nan(fpst); 12418 } 12419 return nan; 12420 } else if (float64_is_infinity(f64)) { 12421 return float64_set_sign(float64_zero, float64_is_neg(f64)); 12422 } else if (float64_is_zero(f64)) { 12423 float_raise(float_flag_divbyzero, fpst); 12424 return float64_set_sign(float64_infinity, float64_is_neg(f64)); 12425 } else if ((f64_val & ~(1ULL << 63)) < (1ULL << 50)) { 12426 /* Abs(value) < 2.0^-1024 */ 12427 float_raise(float_flag_overflow | float_flag_inexact, fpst); 12428 if (round_to_inf(fpst, f64_sign)) { 12429 return float64_set_sign(float64_infinity, f64_sign); 12430 } else { 12431 return float64_set_sign(float64_maxnorm, f64_sign); 12432 } 12433 } else if (f64_exp >= 2045 && fpst->flush_to_zero) { 12434 float_raise(float_flag_underflow, fpst); 12435 return float64_set_sign(float64_zero, float64_is_neg(f64)); 12436 } 12437 12438 f64_frac = call_recip_estimate(&f64_exp, 2045, f64_frac); 12439 12440 /* result = sign : result_exp<10:0> : fraction<51:0>; */ 12441 f64_val = deposit64(0, 63, 1, f64_sign); 12442 f64_val = deposit64(f64_val, 52, 11, f64_exp); 12443 f64_val = deposit64(f64_val, 0, 52, f64_frac); 12444 return make_float64(f64_val); 12445 } 12446 12447 /* The algorithm that must be used to calculate the estimate 12448 * is specified by the ARM ARM. 12449 */ 12450 12451 static int do_recip_sqrt_estimate(int a) 12452 { 12453 int b, estimate; 12454 12455 assert(128 <= a && a < 512); 12456 if (a < 256) { 12457 a = a * 2 + 1; 12458 } else { 12459 a = (a >> 1) << 1; 12460 a = (a + 1) * 2; 12461 } 12462 b = 512; 12463 while (a * (b + 1) * (b + 1) < (1 << 28)) { 12464 b += 1; 12465 } 12466 estimate = (b + 1) / 2; 12467 assert(256 <= estimate && estimate < 512); 12468 12469 return estimate; 12470 } 12471 12472 12473 static uint64_t recip_sqrt_estimate(int *exp , int exp_off, uint64_t frac) 12474 { 12475 int estimate; 12476 uint32_t scaled; 12477 12478 if (*exp == 0) { 12479 while (extract64(frac, 51, 1) == 0) { 12480 frac = frac << 1; 12481 *exp -= 1; 12482 } 12483 frac = extract64(frac, 0, 51) << 1; 12484 } 12485 12486 if (*exp & 1) { 12487 /* scaled = UInt('01':fraction<51:45>) */ 12488 scaled = deposit32(1 << 7, 0, 7, extract64(frac, 45, 7)); 12489 } else { 12490 /* scaled = UInt('1':fraction<51:44>) */ 12491 scaled = deposit32(1 << 8, 0, 8, extract64(frac, 44, 8)); 12492 } 12493 estimate = do_recip_sqrt_estimate(scaled); 12494 12495 *exp = (exp_off - *exp) / 2; 12496 return extract64(estimate, 0, 8) << 44; 12497 } 12498 12499 uint32_t HELPER(rsqrte_f16)(uint32_t input, void *fpstp) 12500 { 12501 float_status *s = fpstp; 12502 float16 f16 = float16_squash_input_denormal(input, s); 12503 uint16_t val = float16_val(f16); 12504 bool f16_sign = float16_is_neg(f16); 12505 int f16_exp = extract32(val, 10, 5); 12506 uint16_t f16_frac = extract32(val, 0, 10); 12507 uint64_t f64_frac; 12508 12509 if (float16_is_any_nan(f16)) { 12510 float16 nan = f16; 12511 if (float16_is_signaling_nan(f16, s)) { 12512 float_raise(float_flag_invalid, s); 12513 nan = float16_silence_nan(f16, s); 12514 } 12515 if (s->default_nan_mode) { 12516 nan = float16_default_nan(s); 12517 } 12518 return nan; 12519 } else if (float16_is_zero(f16)) { 12520 float_raise(float_flag_divbyzero, s); 12521 return float16_set_sign(float16_infinity, f16_sign); 12522 } else if (f16_sign) { 12523 float_raise(float_flag_invalid, s); 12524 return float16_default_nan(s); 12525 } else if (float16_is_infinity(f16)) { 12526 return float16_zero; 12527 } 12528 12529 /* Scale and normalize to a double-precision value between 0.25 and 1.0, 12530 * preserving the parity of the exponent. */ 12531 12532 f64_frac = ((uint64_t) f16_frac) << (52 - 10); 12533 12534 f64_frac = recip_sqrt_estimate(&f16_exp, 44, f64_frac); 12535 12536 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(2) */ 12537 val = deposit32(0, 15, 1, f16_sign); 12538 val = deposit32(val, 10, 5, f16_exp); 12539 val = deposit32(val, 2, 8, extract64(f64_frac, 52 - 8, 8)); 12540 return make_float16(val); 12541 } 12542 12543 float32 HELPER(rsqrte_f32)(float32 input, void *fpstp) 12544 { 12545 float_status *s = fpstp; 12546 float32 f32 = float32_squash_input_denormal(input, s); 12547 uint32_t val = float32_val(f32); 12548 uint32_t f32_sign = float32_is_neg(f32); 12549 int f32_exp = extract32(val, 23, 8); 12550 uint32_t f32_frac = extract32(val, 0, 23); 12551 uint64_t f64_frac; 12552 12553 if (float32_is_any_nan(f32)) { 12554 float32 nan = f32; 12555 if (float32_is_signaling_nan(f32, s)) { 12556 float_raise(float_flag_invalid, s); 12557 nan = float32_silence_nan(f32, s); 12558 } 12559 if (s->default_nan_mode) { 12560 nan = float32_default_nan(s); 12561 } 12562 return nan; 12563 } else if (float32_is_zero(f32)) { 12564 float_raise(float_flag_divbyzero, s); 12565 return float32_set_sign(float32_infinity, float32_is_neg(f32)); 12566 } else if (float32_is_neg(f32)) { 12567 float_raise(float_flag_invalid, s); 12568 return float32_default_nan(s); 12569 } else if (float32_is_infinity(f32)) { 12570 return float32_zero; 12571 } 12572 12573 /* Scale and normalize to a double-precision value between 0.25 and 1.0, 12574 * preserving the parity of the exponent. */ 12575 12576 f64_frac = ((uint64_t) f32_frac) << 29; 12577 12578 f64_frac = recip_sqrt_estimate(&f32_exp, 380, f64_frac); 12579 12580 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(15) */ 12581 val = deposit32(0, 31, 1, f32_sign); 12582 val = deposit32(val, 23, 8, f32_exp); 12583 val = deposit32(val, 15, 8, extract64(f64_frac, 52 - 8, 8)); 12584 return make_float32(val); 12585 } 12586 12587 float64 HELPER(rsqrte_f64)(float64 input, void *fpstp) 12588 { 12589 float_status *s = fpstp; 12590 float64 f64 = float64_squash_input_denormal(input, s); 12591 uint64_t val = float64_val(f64); 12592 bool f64_sign = float64_is_neg(f64); 12593 int f64_exp = extract64(val, 52, 11); 12594 uint64_t f64_frac = extract64(val, 0, 52); 12595 12596 if (float64_is_any_nan(f64)) { 12597 float64 nan = f64; 12598 if (float64_is_signaling_nan(f64, s)) { 12599 float_raise(float_flag_invalid, s); 12600 nan = float64_silence_nan(f64, s); 12601 } 12602 if (s->default_nan_mode) { 12603 nan = float64_default_nan(s); 12604 } 12605 return nan; 12606 } else if (float64_is_zero(f64)) { 12607 float_raise(float_flag_divbyzero, s); 12608 return float64_set_sign(float64_infinity, float64_is_neg(f64)); 12609 } else if (float64_is_neg(f64)) { 12610 float_raise(float_flag_invalid, s); 12611 return float64_default_nan(s); 12612 } else if (float64_is_infinity(f64)) { 12613 return float64_zero; 12614 } 12615 12616 f64_frac = recip_sqrt_estimate(&f64_exp, 3068, f64_frac); 12617 12618 /* result = sign : result_exp<4:0> : estimate<7:0> : Zeros(44) */ 12619 val = deposit64(0, 61, 1, f64_sign); 12620 val = deposit64(val, 52, 11, f64_exp); 12621 val = deposit64(val, 44, 8, extract64(f64_frac, 52 - 8, 8)); 12622 return make_float64(val); 12623 } 12624 12625 uint32_t HELPER(recpe_u32)(uint32_t a, void *fpstp) 12626 { 12627 /* float_status *s = fpstp; */ 12628 int input, estimate; 12629 12630 if ((a & 0x80000000) == 0) { 12631 return 0xffffffff; 12632 } 12633 12634 input = extract32(a, 23, 9); 12635 estimate = recip_estimate(input); 12636 12637 return deposit32(0, (32 - 9), 9, estimate); 12638 } 12639 12640 uint32_t HELPER(rsqrte_u32)(uint32_t a, void *fpstp) 12641 { 12642 int estimate; 12643 12644 if ((a & 0xc0000000) == 0) { 12645 return 0xffffffff; 12646 } 12647 12648 estimate = do_recip_sqrt_estimate(extract32(a, 23, 9)); 12649 12650 return deposit32(0, 23, 9, estimate); 12651 } 12652 12653 /* VFPv4 fused multiply-accumulate */ 12654 float32 VFP_HELPER(muladd, s)(float32 a, float32 b, float32 c, void *fpstp) 12655 { 12656 float_status *fpst = fpstp; 12657 return float32_muladd(a, b, c, 0, fpst); 12658 } 12659 12660 float64 VFP_HELPER(muladd, d)(float64 a, float64 b, float64 c, void *fpstp) 12661 { 12662 float_status *fpst = fpstp; 12663 return float64_muladd(a, b, c, 0, fpst); 12664 } 12665 12666 /* ARMv8 round to integral */ 12667 float32 HELPER(rints_exact)(float32 x, void *fp_status) 12668 { 12669 return float32_round_to_int(x, fp_status); 12670 } 12671 12672 float64 HELPER(rintd_exact)(float64 x, void *fp_status) 12673 { 12674 return float64_round_to_int(x, fp_status); 12675 } 12676 12677 float32 HELPER(rints)(float32 x, void *fp_status) 12678 { 12679 int old_flags = get_float_exception_flags(fp_status), new_flags; 12680 float32 ret; 12681 12682 ret = float32_round_to_int(x, fp_status); 12683 12684 /* Suppress any inexact exceptions the conversion produced */ 12685 if (!(old_flags & float_flag_inexact)) { 12686 new_flags = get_float_exception_flags(fp_status); 12687 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); 12688 } 12689 12690 return ret; 12691 } 12692 12693 float64 HELPER(rintd)(float64 x, void *fp_status) 12694 { 12695 int old_flags = get_float_exception_flags(fp_status), new_flags; 12696 float64 ret; 12697 12698 ret = float64_round_to_int(x, fp_status); 12699 12700 new_flags = get_float_exception_flags(fp_status); 12701 12702 /* Suppress any inexact exceptions the conversion produced */ 12703 if (!(old_flags & float_flag_inexact)) { 12704 new_flags = get_float_exception_flags(fp_status); 12705 set_float_exception_flags(new_flags & ~float_flag_inexact, fp_status); 12706 } 12707 12708 return ret; 12709 } 12710 12711 /* Convert ARM rounding mode to softfloat */ 12712 int arm_rmode_to_sf(int rmode) 12713 { 12714 switch (rmode) { 12715 case FPROUNDING_TIEAWAY: 12716 rmode = float_round_ties_away; 12717 break; 12718 case FPROUNDING_ODD: 12719 /* FIXME: add support for TIEAWAY and ODD */ 12720 qemu_log_mask(LOG_UNIMP, "arm: unimplemented rounding mode: %d\n", 12721 rmode); 12722 /* fall through for now */ 12723 case FPROUNDING_TIEEVEN: 12724 default: 12725 rmode = float_round_nearest_even; 12726 break; 12727 case FPROUNDING_POSINF: 12728 rmode = float_round_up; 12729 break; 12730 case FPROUNDING_NEGINF: 12731 rmode = float_round_down; 12732 break; 12733 case FPROUNDING_ZERO: 12734 rmode = float_round_to_zero; 12735 break; 12736 } 12737 return rmode; 12738 } 12739 12740 /* CRC helpers. 12741 * The upper bytes of val (above the number specified by 'bytes') must have 12742 * been zeroed out by the caller. 12743 */ 12744 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes) 12745 { 12746 uint8_t buf[4]; 12747 12748 stl_le_p(buf, val); 12749 12750 /* zlib crc32 converts the accumulator and output to one's complement. */ 12751 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; 12752 } 12753 12754 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) 12755 { 12756 uint8_t buf[4]; 12757 12758 stl_le_p(buf, val); 12759 12760 /* Linux crc32c converts the output to one's complement. */ 12761 return crc32c(acc, buf, bytes) ^ 0xffffffff; 12762 } 12763 12764 /* Return the exception level to which FP-disabled exceptions should 12765 * be taken, or 0 if FP is enabled. 12766 */ 12767 int fp_exception_el(CPUARMState *env, int cur_el) 12768 { 12769 #ifndef CONFIG_USER_ONLY 12770 int fpen; 12771 12772 /* CPACR and the CPTR registers don't exist before v6, so FP is 12773 * always accessible 12774 */ 12775 if (!arm_feature(env, ARM_FEATURE_V6)) { 12776 return 0; 12777 } 12778 12779 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit: 12780 * 0, 2 : trap EL0 and EL1/PL1 accesses 12781 * 1 : trap only EL0 accesses 12782 * 3 : trap no accesses 12783 */ 12784 fpen = extract32(env->cp15.cpacr_el1, 20, 2); 12785 switch (fpen) { 12786 case 0: 12787 case 2: 12788 if (cur_el == 0 || cur_el == 1) { 12789 /* Trap to PL1, which might be EL1 or EL3 */ 12790 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) { 12791 return 3; 12792 } 12793 return 1; 12794 } 12795 if (cur_el == 3 && !is_a64(env)) { 12796 /* Secure PL1 running at EL3 */ 12797 return 3; 12798 } 12799 break; 12800 case 1: 12801 if (cur_el == 0) { 12802 return 1; 12803 } 12804 break; 12805 case 3: 12806 break; 12807 } 12808 12809 /* For the CPTR registers we don't need to guard with an ARM_FEATURE 12810 * check because zero bits in the registers mean "don't trap". 12811 */ 12812 12813 /* CPTR_EL2 : present in v7VE or v8 */ 12814 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1) 12815 && !arm_is_secure_below_el3(env)) { 12816 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */ 12817 return 2; 12818 } 12819 12820 /* CPTR_EL3 : present in v8 */ 12821 if (extract32(env->cp15.cptr_el[3], 10, 1)) { 12822 /* Trap all FP ops to EL3 */ 12823 return 3; 12824 } 12825 #endif 12826 return 0; 12827 } 12828 12829 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc, 12830 target_ulong *cs_base, uint32_t *pflags) 12831 { 12832 ARMMMUIdx mmu_idx = core_to_arm_mmu_idx(env, cpu_mmu_index(env, false)); 12833 int current_el = arm_current_el(env); 12834 int fp_el = fp_exception_el(env, current_el); 12835 uint32_t flags; 12836 12837 if (is_a64(env)) { 12838 ARMCPU *cpu = arm_env_get_cpu(env); 12839 12840 *pc = env->pc; 12841 flags = ARM_TBFLAG_AARCH64_STATE_MASK; 12842 /* Get control bits for tagged addresses */ 12843 flags |= (arm_regime_tbi0(env, mmu_idx) << ARM_TBFLAG_TBI0_SHIFT); 12844 flags |= (arm_regime_tbi1(env, mmu_idx) << ARM_TBFLAG_TBI1_SHIFT); 12845 12846 if (cpu_isar_feature(aa64_sve, cpu)) { 12847 int sve_el = sve_exception_el(env, current_el); 12848 uint32_t zcr_len; 12849 12850 /* If SVE is disabled, but FP is enabled, 12851 * then the effective len is 0. 12852 */ 12853 if (sve_el != 0 && fp_el == 0) { 12854 zcr_len = 0; 12855 } else { 12856 zcr_len = sve_zcr_len_for_el(env, current_el); 12857 } 12858 flags |= sve_el << ARM_TBFLAG_SVEEXC_EL_SHIFT; 12859 flags |= zcr_len << ARM_TBFLAG_ZCR_LEN_SHIFT; 12860 } 12861 } else { 12862 *pc = env->regs[15]; 12863 flags = (env->thumb << ARM_TBFLAG_THUMB_SHIFT) 12864 | (env->vfp.vec_len << ARM_TBFLAG_VECLEN_SHIFT) 12865 | (env->vfp.vec_stride << ARM_TBFLAG_VECSTRIDE_SHIFT) 12866 | (env->condexec_bits << ARM_TBFLAG_CONDEXEC_SHIFT) 12867 | (arm_sctlr_b(env) << ARM_TBFLAG_SCTLR_B_SHIFT); 12868 if (!(access_secure_reg(env))) { 12869 flags |= ARM_TBFLAG_NS_MASK; 12870 } 12871 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30) 12872 || arm_el_is_aa64(env, 1)) { 12873 flags |= ARM_TBFLAG_VFPEN_MASK; 12874 } 12875 flags |= (extract32(env->cp15.c15_cpar, 0, 2) 12876 << ARM_TBFLAG_XSCALE_CPAR_SHIFT); 12877 } 12878 12879 flags |= (arm_to_core_mmu_idx(mmu_idx) << ARM_TBFLAG_MMUIDX_SHIFT); 12880 12881 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine 12882 * states defined in the ARM ARM for software singlestep: 12883 * SS_ACTIVE PSTATE.SS State 12884 * 0 x Inactive (the TB flag for SS is always 0) 12885 * 1 0 Active-pending 12886 * 1 1 Active-not-pending 12887 */ 12888 if (arm_singlestep_active(env)) { 12889 flags |= ARM_TBFLAG_SS_ACTIVE_MASK; 12890 if (is_a64(env)) { 12891 if (env->pstate & PSTATE_SS) { 12892 flags |= ARM_TBFLAG_PSTATE_SS_MASK; 12893 } 12894 } else { 12895 if (env->uncached_cpsr & PSTATE_SS) { 12896 flags |= ARM_TBFLAG_PSTATE_SS_MASK; 12897 } 12898 } 12899 } 12900 if (arm_cpu_data_is_big_endian(env)) { 12901 flags |= ARM_TBFLAG_BE_DATA_MASK; 12902 } 12903 flags |= fp_el << ARM_TBFLAG_FPEXC_EL_SHIFT; 12904 12905 if (arm_v7m_is_handler_mode(env)) { 12906 flags |= ARM_TBFLAG_HANDLER_MASK; 12907 } 12908 12909 /* v8M always applies stack limit checks unless CCR.STKOFHFNMIGN is 12910 * suppressing them because the requested execution priority is less than 0. 12911 */ 12912 if (arm_feature(env, ARM_FEATURE_V8) && 12913 arm_feature(env, ARM_FEATURE_M) && 12914 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) && 12915 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) { 12916 flags |= ARM_TBFLAG_STACKCHECK_MASK; 12917 } 12918 12919 *pflags = flags; 12920 *cs_base = 0; 12921 } 12922 12923 #ifdef TARGET_AARCH64 12924 /* 12925 * The manual says that when SVE is enabled and VQ is widened the 12926 * implementation is allowed to zero the previously inaccessible 12927 * portion of the registers. The corollary to that is that when 12928 * SVE is enabled and VQ is narrowed we are also allowed to zero 12929 * the now inaccessible portion of the registers. 12930 * 12931 * The intent of this is that no predicate bit beyond VQ is ever set. 12932 * Which means that some operations on predicate registers themselves 12933 * may operate on full uint64_t or even unrolled across the maximum 12934 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally 12935 * may well be cheaper than conditionals to restrict the operation 12936 * to the relevant portion of a uint16_t[16]. 12937 */ 12938 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq) 12939 { 12940 int i, j; 12941 uint64_t pmask; 12942 12943 assert(vq >= 1 && vq <= ARM_MAX_VQ); 12944 assert(vq <= arm_env_get_cpu(env)->sve_max_vq); 12945 12946 /* Zap the high bits of the zregs. */ 12947 for (i = 0; i < 32; i++) { 12948 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq)); 12949 } 12950 12951 /* Zap the high bits of the pregs and ffr. */ 12952 pmask = 0; 12953 if (vq & 3) { 12954 pmask = ~(-1ULL << (16 * (vq & 3))); 12955 } 12956 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) { 12957 for (i = 0; i < 17; ++i) { 12958 env->vfp.pregs[i].p[j] &= pmask; 12959 } 12960 pmask = 0; 12961 } 12962 } 12963 12964 /* 12965 * Notice a change in SVE vector size when changing EL. 12966 */ 12967 void aarch64_sve_change_el(CPUARMState *env, int old_el, 12968 int new_el, bool el0_a64) 12969 { 12970 ARMCPU *cpu = arm_env_get_cpu(env); 12971 int old_len, new_len; 12972 bool old_a64, new_a64; 12973 12974 /* Nothing to do if no SVE. */ 12975 if (!cpu_isar_feature(aa64_sve, cpu)) { 12976 return; 12977 } 12978 12979 /* Nothing to do if FP is disabled in either EL. */ 12980 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) { 12981 return; 12982 } 12983 12984 /* 12985 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped 12986 * at ELx, or not available because the EL is in AArch32 state, then 12987 * for all purposes other than a direct read, the ZCR_ELx.LEN field 12988 * has an effective value of 0". 12989 * 12990 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0). 12991 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition 12992 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that 12993 * we already have the correct register contents when encountering the 12994 * vq0->vq0 transition between EL0->EL1. 12995 */ 12996 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64; 12997 old_len = (old_a64 && !sve_exception_el(env, old_el) 12998 ? sve_zcr_len_for_el(env, old_el) : 0); 12999 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64; 13000 new_len = (new_a64 && !sve_exception_el(env, new_el) 13001 ? sve_zcr_len_for_el(env, new_el) : 0); 13002 13003 /* When changing vector length, clear inaccessible state. */ 13004 if (new_len < old_len) { 13005 aarch64_sve_narrow_vq(env, new_len + 1); 13006 } 13007 } 13008 #endif 13009