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 "qemu/qemu-print.h" 14 #include "exec/exec-all.h" 15 #include "exec/cpu_ldst.h" 16 #include "arm_ldst.h" 17 #include <zlib.h> /* For crc32 */ 18 #include "exec/semihost.h" 19 #include "sysemu/cpus.h" 20 #include "sysemu/kvm.h" 21 #include "fpu/softfloat.h" 22 #include "qemu/range.h" 23 #include "qapi/qapi-commands-target.h" 24 25 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */ 26 27 #ifndef CONFIG_USER_ONLY 28 /* Cacheability and shareability attributes for a memory access */ 29 typedef struct ARMCacheAttrs { 30 unsigned int attrs:8; /* as in the MAIR register encoding */ 31 unsigned int shareability:2; /* as in the SH field of the VMSAv8-64 PTEs */ 32 } ARMCacheAttrs; 33 34 static bool get_phys_addr(CPUARMState *env, target_ulong address, 35 MMUAccessType access_type, ARMMMUIdx mmu_idx, 36 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 37 target_ulong *page_size, 38 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs); 39 40 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, 41 MMUAccessType access_type, ARMMMUIdx mmu_idx, 42 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 43 target_ulong *page_size_ptr, 44 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs); 45 46 /* Security attributes for an address, as returned by v8m_security_lookup. */ 47 typedef struct V8M_SAttributes { 48 bool subpage; /* true if these attrs don't cover the whole TARGET_PAGE */ 49 bool ns; 50 bool nsc; 51 uint8_t sregion; 52 bool srvalid; 53 uint8_t iregion; 54 bool irvalid; 55 } V8M_SAttributes; 56 57 static void v8m_security_lookup(CPUARMState *env, uint32_t address, 58 MMUAccessType access_type, ARMMMUIdx mmu_idx, 59 V8M_SAttributes *sattrs); 60 #endif 61 62 static void switch_mode(CPUARMState *env, int mode); 63 64 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) 65 { 66 int nregs; 67 68 /* VFP data registers are always little-endian. */ 69 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; 70 if (reg < nregs) { 71 stq_le_p(buf, *aa32_vfp_dreg(env, reg)); 72 return 8; 73 } 74 if (arm_feature(env, ARM_FEATURE_NEON)) { 75 /* Aliases for Q regs. */ 76 nregs += 16; 77 if (reg < nregs) { 78 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 79 stq_le_p(buf, q[0]); 80 stq_le_p(buf + 8, q[1]); 81 return 16; 82 } 83 } 84 switch (reg - nregs) { 85 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4; 86 case 1: stl_p(buf, vfp_get_fpscr(env)); return 4; 87 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4; 88 } 89 return 0; 90 } 91 92 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 93 { 94 int nregs; 95 96 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; 97 if (reg < nregs) { 98 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf); 99 return 8; 100 } 101 if (arm_feature(env, ARM_FEATURE_NEON)) { 102 nregs += 16; 103 if (reg < nregs) { 104 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 105 q[0] = ldq_le_p(buf); 106 q[1] = ldq_le_p(buf + 8); 107 return 16; 108 } 109 } 110 switch (reg - nregs) { 111 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; 112 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4; 113 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; 114 } 115 return 0; 116 } 117 118 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) 119 { 120 switch (reg) { 121 case 0 ... 31: 122 /* 128 bit FP register */ 123 { 124 uint64_t *q = aa64_vfp_qreg(env, reg); 125 stq_le_p(buf, q[0]); 126 stq_le_p(buf + 8, q[1]); 127 return 16; 128 } 129 case 32: 130 /* FPSR */ 131 stl_p(buf, vfp_get_fpsr(env)); 132 return 4; 133 case 33: 134 /* FPCR */ 135 stl_p(buf, vfp_get_fpcr(env)); 136 return 4; 137 default: 138 return 0; 139 } 140 } 141 142 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 143 { 144 switch (reg) { 145 case 0 ... 31: 146 /* 128 bit FP register */ 147 { 148 uint64_t *q = aa64_vfp_qreg(env, reg); 149 q[0] = ldq_le_p(buf); 150 q[1] = ldq_le_p(buf + 8); 151 return 16; 152 } 153 case 32: 154 /* FPSR */ 155 vfp_set_fpsr(env, ldl_p(buf)); 156 return 4; 157 case 33: 158 /* FPCR */ 159 vfp_set_fpcr(env, ldl_p(buf)); 160 return 4; 161 default: 162 return 0; 163 } 164 } 165 166 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) 167 { 168 assert(ri->fieldoffset); 169 if (cpreg_field_is_64bit(ri)) { 170 return CPREG_FIELD64(env, ri); 171 } else { 172 return CPREG_FIELD32(env, ri); 173 } 174 } 175 176 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 177 uint64_t value) 178 { 179 assert(ri->fieldoffset); 180 if (cpreg_field_is_64bit(ri)) { 181 CPREG_FIELD64(env, ri) = value; 182 } else { 183 CPREG_FIELD32(env, ri) = value; 184 } 185 } 186 187 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) 188 { 189 return (char *)env + ri->fieldoffset; 190 } 191 192 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) 193 { 194 /* Raw read of a coprocessor register (as needed for migration, etc). */ 195 if (ri->type & ARM_CP_CONST) { 196 return ri->resetvalue; 197 } else if (ri->raw_readfn) { 198 return ri->raw_readfn(env, ri); 199 } else if (ri->readfn) { 200 return ri->readfn(env, ri); 201 } else { 202 return raw_read(env, ri); 203 } 204 } 205 206 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, 207 uint64_t v) 208 { 209 /* Raw write of a coprocessor register (as needed for migration, etc). 210 * Note that constant registers are treated as write-ignored; the 211 * caller should check for success by whether a readback gives the 212 * value written. 213 */ 214 if (ri->type & ARM_CP_CONST) { 215 return; 216 } else if (ri->raw_writefn) { 217 ri->raw_writefn(env, ri, v); 218 } else if (ri->writefn) { 219 ri->writefn(env, ri, v); 220 } else { 221 raw_write(env, ri, v); 222 } 223 } 224 225 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg) 226 { 227 ARMCPU *cpu = arm_env_get_cpu(env); 228 const ARMCPRegInfo *ri; 229 uint32_t key; 230 231 key = cpu->dyn_xml.cpregs_keys[reg]; 232 ri = get_arm_cp_reginfo(cpu->cp_regs, key); 233 if (ri) { 234 if (cpreg_field_is_64bit(ri)) { 235 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri)); 236 } else { 237 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri)); 238 } 239 } 240 return 0; 241 } 242 243 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg) 244 { 245 return 0; 246 } 247 248 static bool raw_accessors_invalid(const ARMCPRegInfo *ri) 249 { 250 /* Return true if the regdef would cause an assertion if you called 251 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a 252 * program bug for it not to have the NO_RAW flag). 253 * NB that returning false here doesn't necessarily mean that calling 254 * read/write_raw_cp_reg() is safe, because we can't distinguish "has 255 * read/write access functions which are safe for raw use" from "has 256 * read/write access functions which have side effects but has forgotten 257 * to provide raw access functions". 258 * The tests here line up with the conditions in read/write_raw_cp_reg() 259 * and assertions in raw_read()/raw_write(). 260 */ 261 if ((ri->type & ARM_CP_CONST) || 262 ri->fieldoffset || 263 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { 264 return false; 265 } 266 return true; 267 } 268 269 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync) 270 { 271 /* Write the coprocessor state from cpu->env to the (index,value) list. */ 272 int i; 273 bool ok = true; 274 275 for (i = 0; i < cpu->cpreg_array_len; i++) { 276 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 277 const ARMCPRegInfo *ri; 278 uint64_t newval; 279 280 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 281 if (!ri) { 282 ok = false; 283 continue; 284 } 285 if (ri->type & ARM_CP_NO_RAW) { 286 continue; 287 } 288 289 newval = read_raw_cp_reg(&cpu->env, ri); 290 if (kvm_sync) { 291 /* 292 * Only sync if the previous list->cpustate sync succeeded. 293 * Rather than tracking the success/failure state for every 294 * item in the list, we just recheck "does the raw write we must 295 * have made in write_list_to_cpustate() read back OK" here. 296 */ 297 uint64_t oldval = cpu->cpreg_values[i]; 298 299 if (oldval == newval) { 300 continue; 301 } 302 303 write_raw_cp_reg(&cpu->env, ri, oldval); 304 if (read_raw_cp_reg(&cpu->env, ri) != oldval) { 305 continue; 306 } 307 308 write_raw_cp_reg(&cpu->env, ri, newval); 309 } 310 cpu->cpreg_values[i] = newval; 311 } 312 return ok; 313 } 314 315 bool write_list_to_cpustate(ARMCPU *cpu) 316 { 317 int i; 318 bool ok = true; 319 320 for (i = 0; i < cpu->cpreg_array_len; i++) { 321 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 322 uint64_t v = cpu->cpreg_values[i]; 323 const ARMCPRegInfo *ri; 324 325 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 326 if (!ri) { 327 ok = false; 328 continue; 329 } 330 if (ri->type & ARM_CP_NO_RAW) { 331 continue; 332 } 333 /* Write value and confirm it reads back as written 334 * (to catch read-only registers and partially read-only 335 * registers where the incoming migration value doesn't match) 336 */ 337 write_raw_cp_reg(&cpu->env, ri, v); 338 if (read_raw_cp_reg(&cpu->env, ri) != v) { 339 ok = false; 340 } 341 } 342 return ok; 343 } 344 345 static void add_cpreg_to_list(gpointer key, gpointer opaque) 346 { 347 ARMCPU *cpu = opaque; 348 uint64_t regidx; 349 const ARMCPRegInfo *ri; 350 351 regidx = *(uint32_t *)key; 352 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 353 354 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 355 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); 356 /* The value array need not be initialized at this point */ 357 cpu->cpreg_array_len++; 358 } 359 } 360 361 static void count_cpreg(gpointer key, gpointer opaque) 362 { 363 ARMCPU *cpu = opaque; 364 uint64_t regidx; 365 const ARMCPRegInfo *ri; 366 367 regidx = *(uint32_t *)key; 368 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 369 370 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 371 cpu->cpreg_array_len++; 372 } 373 } 374 375 static gint cpreg_key_compare(gconstpointer a, gconstpointer b) 376 { 377 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a); 378 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b); 379 380 if (aidx > bidx) { 381 return 1; 382 } 383 if (aidx < bidx) { 384 return -1; 385 } 386 return 0; 387 } 388 389 void init_cpreg_list(ARMCPU *cpu) 390 { 391 /* Initialise the cpreg_tuples[] array based on the cp_regs hash. 392 * Note that we require cpreg_tuples[] to be sorted by key ID. 393 */ 394 GList *keys; 395 int arraylen; 396 397 keys = g_hash_table_get_keys(cpu->cp_regs); 398 keys = g_list_sort(keys, cpreg_key_compare); 399 400 cpu->cpreg_array_len = 0; 401 402 g_list_foreach(keys, count_cpreg, cpu); 403 404 arraylen = cpu->cpreg_array_len; 405 cpu->cpreg_indexes = g_new(uint64_t, arraylen); 406 cpu->cpreg_values = g_new(uint64_t, arraylen); 407 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); 408 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); 409 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; 410 cpu->cpreg_array_len = 0; 411 412 g_list_foreach(keys, add_cpreg_to_list, cpu); 413 414 assert(cpu->cpreg_array_len == arraylen); 415 416 g_list_free(keys); 417 } 418 419 /* 420 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but 421 * they are accessible when EL3 is using AArch64 regardless of EL3.NS. 422 * 423 * access_el3_aa32ns: Used to check AArch32 register views. 424 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views. 425 */ 426 static CPAccessResult access_el3_aa32ns(CPUARMState *env, 427 const ARMCPRegInfo *ri, 428 bool isread) 429 { 430 bool secure = arm_is_secure_below_el3(env); 431 432 assert(!arm_el_is_aa64(env, 3)); 433 if (secure) { 434 return CP_ACCESS_TRAP_UNCATEGORIZED; 435 } 436 return CP_ACCESS_OK; 437 } 438 439 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env, 440 const ARMCPRegInfo *ri, 441 bool isread) 442 { 443 if (!arm_el_is_aa64(env, 3)) { 444 return access_el3_aa32ns(env, ri, isread); 445 } 446 return CP_ACCESS_OK; 447 } 448 449 /* Some secure-only AArch32 registers trap to EL3 if used from 450 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts). 451 * Note that an access from Secure EL1 can only happen if EL3 is AArch64. 452 * We assume that the .access field is set to PL1_RW. 453 */ 454 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env, 455 const ARMCPRegInfo *ri, 456 bool isread) 457 { 458 if (arm_current_el(env) == 3) { 459 return CP_ACCESS_OK; 460 } 461 if (arm_is_secure_below_el3(env)) { 462 return CP_ACCESS_TRAP_EL3; 463 } 464 /* This will be EL1 NS and EL2 NS, which just UNDEF */ 465 return CP_ACCESS_TRAP_UNCATEGORIZED; 466 } 467 468 /* Check for traps to "powerdown debug" registers, which are controlled 469 * by MDCR.TDOSA 470 */ 471 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri, 472 bool isread) 473 { 474 int el = arm_current_el(env); 475 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) || 476 (env->cp15.mdcr_el2 & MDCR_TDE) || 477 (arm_hcr_el2_eff(env) & HCR_TGE); 478 479 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) { 480 return CP_ACCESS_TRAP_EL2; 481 } 482 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) { 483 return CP_ACCESS_TRAP_EL3; 484 } 485 return CP_ACCESS_OK; 486 } 487 488 /* Check for traps to "debug ROM" registers, which are controlled 489 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3. 490 */ 491 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri, 492 bool isread) 493 { 494 int el = arm_current_el(env); 495 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) || 496 (env->cp15.mdcr_el2 & MDCR_TDE) || 497 (arm_hcr_el2_eff(env) & HCR_TGE); 498 499 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) { 500 return CP_ACCESS_TRAP_EL2; 501 } 502 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 503 return CP_ACCESS_TRAP_EL3; 504 } 505 return CP_ACCESS_OK; 506 } 507 508 /* Check for traps to general debug registers, which are controlled 509 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3. 510 */ 511 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri, 512 bool isread) 513 { 514 int el = arm_current_el(env); 515 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) || 516 (env->cp15.mdcr_el2 & MDCR_TDE) || 517 (arm_hcr_el2_eff(env) & HCR_TGE); 518 519 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) { 520 return CP_ACCESS_TRAP_EL2; 521 } 522 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 523 return CP_ACCESS_TRAP_EL3; 524 } 525 return CP_ACCESS_OK; 526 } 527 528 /* Check for traps to performance monitor registers, which are controlled 529 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3. 530 */ 531 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri, 532 bool isread) 533 { 534 int el = arm_current_el(env); 535 536 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 537 && !arm_is_secure_below_el3(env)) { 538 return CP_ACCESS_TRAP_EL2; 539 } 540 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 541 return CP_ACCESS_TRAP_EL3; 542 } 543 return CP_ACCESS_OK; 544 } 545 546 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 547 { 548 ARMCPU *cpu = arm_env_get_cpu(env); 549 550 raw_write(env, ri, value); 551 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */ 552 } 553 554 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 555 { 556 ARMCPU *cpu = arm_env_get_cpu(env); 557 558 if (raw_read(env, ri) != value) { 559 /* Unlike real hardware the qemu TLB uses virtual addresses, 560 * not modified virtual addresses, so this causes a TLB flush. 561 */ 562 tlb_flush(CPU(cpu)); 563 raw_write(env, ri, value); 564 } 565 } 566 567 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, 568 uint64_t value) 569 { 570 ARMCPU *cpu = arm_env_get_cpu(env); 571 572 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA) 573 && !extended_addresses_enabled(env)) { 574 /* For VMSA (when not using the LPAE long descriptor page table 575 * format) this register includes the ASID, so do a TLB flush. 576 * For PMSA it is purely a process ID and no action is needed. 577 */ 578 tlb_flush(CPU(cpu)); 579 } 580 raw_write(env, ri, value); 581 } 582 583 /* IS variants of TLB operations must affect all cores */ 584 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 585 uint64_t value) 586 { 587 CPUState *cs = ENV_GET_CPU(env); 588 589 tlb_flush_all_cpus_synced(cs); 590 } 591 592 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 593 uint64_t value) 594 { 595 CPUState *cs = ENV_GET_CPU(env); 596 597 tlb_flush_all_cpus_synced(cs); 598 } 599 600 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 601 uint64_t value) 602 { 603 CPUState *cs = ENV_GET_CPU(env); 604 605 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 606 } 607 608 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 609 uint64_t value) 610 { 611 CPUState *cs = ENV_GET_CPU(env); 612 613 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 614 } 615 616 /* 617 * Non-IS variants of TLB operations are upgraded to 618 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to 619 * force broadcast of these operations. 620 */ 621 static bool tlb_force_broadcast(CPUARMState *env) 622 { 623 return (env->cp15.hcr_el2 & HCR_FB) && 624 arm_current_el(env) == 1 && arm_is_secure_below_el3(env); 625 } 626 627 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, 628 uint64_t value) 629 { 630 /* Invalidate all (TLBIALL) */ 631 ARMCPU *cpu = arm_env_get_cpu(env); 632 633 if (tlb_force_broadcast(env)) { 634 tlbiall_is_write(env, NULL, value); 635 return; 636 } 637 638 tlb_flush(CPU(cpu)); 639 } 640 641 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, 642 uint64_t value) 643 { 644 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ 645 ARMCPU *cpu = arm_env_get_cpu(env); 646 647 if (tlb_force_broadcast(env)) { 648 tlbimva_is_write(env, NULL, value); 649 return; 650 } 651 652 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); 653 } 654 655 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, 656 uint64_t value) 657 { 658 /* Invalidate by ASID (TLBIASID) */ 659 ARMCPU *cpu = arm_env_get_cpu(env); 660 661 if (tlb_force_broadcast(env)) { 662 tlbiasid_is_write(env, NULL, value); 663 return; 664 } 665 666 tlb_flush(CPU(cpu)); 667 } 668 669 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, 670 uint64_t value) 671 { 672 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ 673 ARMCPU *cpu = arm_env_get_cpu(env); 674 675 if (tlb_force_broadcast(env)) { 676 tlbimvaa_is_write(env, NULL, value); 677 return; 678 } 679 680 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); 681 } 682 683 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri, 684 uint64_t value) 685 { 686 CPUState *cs = ENV_GET_CPU(env); 687 688 tlb_flush_by_mmuidx(cs, 689 ARMMMUIdxBit_S12NSE1 | 690 ARMMMUIdxBit_S12NSE0 | 691 ARMMMUIdxBit_S2NS); 692 } 693 694 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 695 uint64_t value) 696 { 697 CPUState *cs = ENV_GET_CPU(env); 698 699 tlb_flush_by_mmuidx_all_cpus_synced(cs, 700 ARMMMUIdxBit_S12NSE1 | 701 ARMMMUIdxBit_S12NSE0 | 702 ARMMMUIdxBit_S2NS); 703 } 704 705 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri, 706 uint64_t value) 707 { 708 /* Invalidate by IPA. This has to invalidate any structures that 709 * contain only stage 2 translation information, but does not need 710 * to apply to structures that contain combined stage 1 and stage 2 711 * translation information. 712 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. 713 */ 714 CPUState *cs = ENV_GET_CPU(env); 715 uint64_t pageaddr; 716 717 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 718 return; 719 } 720 721 pageaddr = sextract64(value << 12, 0, 40); 722 723 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS); 724 } 725 726 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 727 uint64_t value) 728 { 729 CPUState *cs = ENV_GET_CPU(env); 730 uint64_t pageaddr; 731 732 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 733 return; 734 } 735 736 pageaddr = sextract64(value << 12, 0, 40); 737 738 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 739 ARMMMUIdxBit_S2NS); 740 } 741 742 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 743 uint64_t value) 744 { 745 CPUState *cs = ENV_GET_CPU(env); 746 747 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2); 748 } 749 750 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 751 uint64_t value) 752 { 753 CPUState *cs = ENV_GET_CPU(env); 754 755 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2); 756 } 757 758 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 759 uint64_t value) 760 { 761 CPUState *cs = ENV_GET_CPU(env); 762 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 763 764 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2); 765 } 766 767 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 768 uint64_t value) 769 { 770 CPUState *cs = ENV_GET_CPU(env); 771 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 772 773 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 774 ARMMMUIdxBit_S1E2); 775 } 776 777 static const ARMCPRegInfo cp_reginfo[] = { 778 /* Define the secure and non-secure FCSE identifier CP registers 779 * separately because there is no secure bank in V8 (no _EL3). This allows 780 * the secure register to be properly reset and migrated. There is also no 781 * v8 EL1 version of the register so the non-secure instance stands alone. 782 */ 783 { .name = "FCSEIDR", 784 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 785 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 786 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns), 787 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 788 { .name = "FCSEIDR_S", 789 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 790 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 791 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s), 792 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 793 /* Define the secure and non-secure context identifier CP registers 794 * separately because there is no secure bank in V8 (no _EL3). This allows 795 * the secure register to be properly reset and migrated. In the 796 * non-secure case, the 32-bit register will have reset and migration 797 * disabled during registration as it is handled by the 64-bit instance. 798 */ 799 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH, 800 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 801 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 802 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]), 803 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 804 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32, 805 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 806 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 807 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s), 808 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 809 REGINFO_SENTINEL 810 }; 811 812 static const ARMCPRegInfo not_v8_cp_reginfo[] = { 813 /* NB: Some of these registers exist in v8 but with more precise 814 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). 815 */ 816 /* MMU Domain access control / MPU write buffer control */ 817 { .name = "DACR", 818 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, 819 .access = PL1_RW, .resetvalue = 0, 820 .writefn = dacr_write, .raw_writefn = raw_write, 821 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 822 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 823 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs. 824 * For v6 and v5, these mappings are overly broad. 825 */ 826 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0, 827 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 828 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1, 829 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 830 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4, 831 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 832 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8, 833 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 834 /* Cache maintenance ops; some of this space may be overridden later. */ 835 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 836 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 837 .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, 838 REGINFO_SENTINEL 839 }; 840 841 static const ARMCPRegInfo not_v6_cp_reginfo[] = { 842 /* Not all pre-v6 cores implemented this WFI, so this is slightly 843 * over-broad. 844 */ 845 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, 846 .access = PL1_W, .type = ARM_CP_WFI }, 847 REGINFO_SENTINEL 848 }; 849 850 static const ARMCPRegInfo not_v7_cp_reginfo[] = { 851 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which 852 * is UNPREDICTABLE; we choose to NOP as most implementations do). 853 */ 854 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 855 .access = PL1_W, .type = ARM_CP_WFI }, 856 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice 857 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and 858 * OMAPCP will override this space. 859 */ 860 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, 861 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), 862 .resetvalue = 0 }, 863 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, 864 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), 865 .resetvalue = 0 }, 866 /* v6 doesn't have the cache ID registers but Linux reads them anyway */ 867 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, 868 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 869 .resetvalue = 0 }, 870 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; 871 * implementing it as RAZ means the "debug architecture version" bits 872 * will read as a reserved value, which should cause Linux to not try 873 * to use the debug hardware. 874 */ 875 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 876 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 877 /* MMU TLB control. Note that the wildcarding means we cover not just 878 * the unified TLB ops but also the dside/iside/inner-shareable variants. 879 */ 880 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, 881 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, 882 .type = ARM_CP_NO_RAW }, 883 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, 884 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, 885 .type = ARM_CP_NO_RAW }, 886 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, 887 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, 888 .type = ARM_CP_NO_RAW }, 889 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, 890 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, 891 .type = ARM_CP_NO_RAW }, 892 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2, 893 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP }, 894 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2, 895 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP }, 896 REGINFO_SENTINEL 897 }; 898 899 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, 900 uint64_t value) 901 { 902 uint32_t mask = 0; 903 904 /* In ARMv8 most bits of CPACR_EL1 are RES0. */ 905 if (!arm_feature(env, ARM_FEATURE_V8)) { 906 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. 907 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. 908 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. 909 */ 910 if (arm_feature(env, ARM_FEATURE_VFP)) { 911 /* VFP coprocessor: cp10 & cp11 [23:20] */ 912 mask |= (1 << 31) | (1 << 30) | (0xf << 20); 913 914 if (!arm_feature(env, ARM_FEATURE_NEON)) { 915 /* ASEDIS [31] bit is RAO/WI */ 916 value |= (1 << 31); 917 } 918 919 /* VFPv3 and upwards with NEON implement 32 double precision 920 * registers (D0-D31). 921 */ 922 if (!arm_feature(env, ARM_FEATURE_NEON) || 923 !arm_feature(env, ARM_FEATURE_VFP3)) { 924 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ 925 value |= (1 << 30); 926 } 927 } 928 value &= mask; 929 } 930 env->cp15.cpacr_el1 = value; 931 } 932 933 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 934 { 935 /* Call cpacr_write() so that we reset with the correct RAO bits set 936 * for our CPU features. 937 */ 938 cpacr_write(env, ri, 0); 939 } 940 941 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 942 bool isread) 943 { 944 if (arm_feature(env, ARM_FEATURE_V8)) { 945 /* Check if CPACR accesses are to be trapped to EL2 */ 946 if (arm_current_el(env) == 1 && 947 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) { 948 return CP_ACCESS_TRAP_EL2; 949 /* Check if CPACR accesses are to be trapped to EL3 */ 950 } else if (arm_current_el(env) < 3 && 951 (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 952 return CP_ACCESS_TRAP_EL3; 953 } 954 } 955 956 return CP_ACCESS_OK; 957 } 958 959 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri, 960 bool isread) 961 { 962 /* Check if CPTR accesses are set to trap to EL3 */ 963 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 964 return CP_ACCESS_TRAP_EL3; 965 } 966 967 return CP_ACCESS_OK; 968 } 969 970 static const ARMCPRegInfo v6_cp_reginfo[] = { 971 /* prefetch by MVA in v6, NOP in v7 */ 972 { .name = "MVA_prefetch", 973 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, 974 .access = PL1_W, .type = ARM_CP_NOP }, 975 /* We need to break the TB after ISB to execute self-modifying code 976 * correctly and also to take any pending interrupts immediately. 977 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag. 978 */ 979 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, 980 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore }, 981 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, 982 .access = PL0_W, .type = ARM_CP_NOP }, 983 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, 984 .access = PL0_W, .type = ARM_CP_NOP }, 985 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, 986 .access = PL1_RW, 987 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), 988 offsetof(CPUARMState, cp15.ifar_ns) }, 989 .resetvalue = 0, }, 990 /* Watchpoint Fault Address Register : should actually only be present 991 * for 1136, 1176, 11MPCore. 992 */ 993 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, 994 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, 995 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, 996 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access, 997 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1), 998 .resetfn = cpacr_reset, .writefn = cpacr_write }, 999 REGINFO_SENTINEL 1000 }; 1001 1002 /* Definitions for the PMU registers */ 1003 #define PMCRN_MASK 0xf800 1004 #define PMCRN_SHIFT 11 1005 #define PMCRLC 0x40 1006 #define PMCRDP 0x10 1007 #define PMCRD 0x8 1008 #define PMCRC 0x4 1009 #define PMCRP 0x2 1010 #define PMCRE 0x1 1011 1012 #define PMXEVTYPER_P 0x80000000 1013 #define PMXEVTYPER_U 0x40000000 1014 #define PMXEVTYPER_NSK 0x20000000 1015 #define PMXEVTYPER_NSU 0x10000000 1016 #define PMXEVTYPER_NSH 0x08000000 1017 #define PMXEVTYPER_M 0x04000000 1018 #define PMXEVTYPER_MT 0x02000000 1019 #define PMXEVTYPER_EVTCOUNT 0x0000ffff 1020 #define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \ 1021 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \ 1022 PMXEVTYPER_M | PMXEVTYPER_MT | \ 1023 PMXEVTYPER_EVTCOUNT) 1024 1025 #define PMCCFILTR 0xf8000000 1026 #define PMCCFILTR_M PMXEVTYPER_M 1027 #define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M) 1028 1029 static inline uint32_t pmu_num_counters(CPUARMState *env) 1030 { 1031 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT; 1032 } 1033 1034 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */ 1035 static inline uint64_t pmu_counter_mask(CPUARMState *env) 1036 { 1037 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1); 1038 } 1039 1040 typedef struct pm_event { 1041 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */ 1042 /* If the event is supported on this CPU (used to generate PMCEID[01]) */ 1043 bool (*supported)(CPUARMState *); 1044 /* 1045 * Retrieve the current count of the underlying event. The programmed 1046 * counters hold a difference from the return value from this function 1047 */ 1048 uint64_t (*get_count)(CPUARMState *); 1049 /* 1050 * Return how many nanoseconds it will take (at a minimum) for count events 1051 * to occur. A negative value indicates the counter will never overflow, or 1052 * that the counter has otherwise arranged for the overflow bit to be set 1053 * and the PMU interrupt to be raised on overflow. 1054 */ 1055 int64_t (*ns_per_count)(uint64_t); 1056 } pm_event; 1057 1058 static bool event_always_supported(CPUARMState *env) 1059 { 1060 return true; 1061 } 1062 1063 static uint64_t swinc_get_count(CPUARMState *env) 1064 { 1065 /* 1066 * SW_INCR events are written directly to the pmevcntr's by writes to 1067 * PMSWINC, so there is no underlying count maintained by the PMU itself 1068 */ 1069 return 0; 1070 } 1071 1072 static int64_t swinc_ns_per(uint64_t ignored) 1073 { 1074 return -1; 1075 } 1076 1077 /* 1078 * Return the underlying cycle count for the PMU cycle counters. If we're in 1079 * usermode, simply return 0. 1080 */ 1081 static uint64_t cycles_get_count(CPUARMState *env) 1082 { 1083 #ifndef CONFIG_USER_ONLY 1084 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1085 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 1086 #else 1087 return cpu_get_host_ticks(); 1088 #endif 1089 } 1090 1091 #ifndef CONFIG_USER_ONLY 1092 static int64_t cycles_ns_per(uint64_t cycles) 1093 { 1094 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles; 1095 } 1096 1097 static bool instructions_supported(CPUARMState *env) 1098 { 1099 return use_icount == 1 /* Precise instruction counting */; 1100 } 1101 1102 static uint64_t instructions_get_count(CPUARMState *env) 1103 { 1104 return (uint64_t)cpu_get_icount_raw(); 1105 } 1106 1107 static int64_t instructions_ns_per(uint64_t icount) 1108 { 1109 return cpu_icount_to_ns((int64_t)icount); 1110 } 1111 #endif 1112 1113 static const pm_event pm_events[] = { 1114 { .number = 0x000, /* SW_INCR */ 1115 .supported = event_always_supported, 1116 .get_count = swinc_get_count, 1117 .ns_per_count = swinc_ns_per, 1118 }, 1119 #ifndef CONFIG_USER_ONLY 1120 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */ 1121 .supported = instructions_supported, 1122 .get_count = instructions_get_count, 1123 .ns_per_count = instructions_ns_per, 1124 }, 1125 { .number = 0x011, /* CPU_CYCLES, Cycle */ 1126 .supported = event_always_supported, 1127 .get_count = cycles_get_count, 1128 .ns_per_count = cycles_ns_per, 1129 } 1130 #endif 1131 }; 1132 1133 /* 1134 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of 1135 * events (i.e. the statistical profiling extension), this implementation 1136 * should first be updated to something sparse instead of the current 1137 * supported_event_map[] array. 1138 */ 1139 #define MAX_EVENT_ID 0x11 1140 #define UNSUPPORTED_EVENT UINT16_MAX 1141 static uint16_t supported_event_map[MAX_EVENT_ID + 1]; 1142 1143 /* 1144 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map 1145 * of ARM event numbers to indices in our pm_events array. 1146 * 1147 * Note: Events in the 0x40XX range are not currently supported. 1148 */ 1149 void pmu_init(ARMCPU *cpu) 1150 { 1151 unsigned int i; 1152 1153 /* 1154 * Empty supported_event_map and cpu->pmceid[01] before adding supported 1155 * events to them 1156 */ 1157 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) { 1158 supported_event_map[i] = UNSUPPORTED_EVENT; 1159 } 1160 cpu->pmceid0 = 0; 1161 cpu->pmceid1 = 0; 1162 1163 for (i = 0; i < ARRAY_SIZE(pm_events); i++) { 1164 const pm_event *cnt = &pm_events[i]; 1165 assert(cnt->number <= MAX_EVENT_ID); 1166 /* We do not currently support events in the 0x40xx range */ 1167 assert(cnt->number <= 0x3f); 1168 1169 if (cnt->supported(&cpu->env)) { 1170 supported_event_map[cnt->number] = i; 1171 uint64_t event_mask = 1ULL << (cnt->number & 0x1f); 1172 if (cnt->number & 0x20) { 1173 cpu->pmceid1 |= event_mask; 1174 } else { 1175 cpu->pmceid0 |= event_mask; 1176 } 1177 } 1178 } 1179 } 1180 1181 /* 1182 * Check at runtime whether a PMU event is supported for the current machine 1183 */ 1184 static bool event_supported(uint16_t number) 1185 { 1186 if (number > MAX_EVENT_ID) { 1187 return false; 1188 } 1189 return supported_event_map[number] != UNSUPPORTED_EVENT; 1190 } 1191 1192 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri, 1193 bool isread) 1194 { 1195 /* Performance monitor registers user accessibility is controlled 1196 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable 1197 * trapping to EL2 or EL3 for other accesses. 1198 */ 1199 int el = arm_current_el(env); 1200 1201 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) { 1202 return CP_ACCESS_TRAP; 1203 } 1204 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 1205 && !arm_is_secure_below_el3(env)) { 1206 return CP_ACCESS_TRAP_EL2; 1207 } 1208 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 1209 return CP_ACCESS_TRAP_EL3; 1210 } 1211 1212 return CP_ACCESS_OK; 1213 } 1214 1215 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env, 1216 const ARMCPRegInfo *ri, 1217 bool isread) 1218 { 1219 /* ER: event counter read trap control */ 1220 if (arm_feature(env, ARM_FEATURE_V8) 1221 && arm_current_el(env) == 0 1222 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0 1223 && isread) { 1224 return CP_ACCESS_OK; 1225 } 1226 1227 return pmreg_access(env, ri, isread); 1228 } 1229 1230 static CPAccessResult pmreg_access_swinc(CPUARMState *env, 1231 const ARMCPRegInfo *ri, 1232 bool isread) 1233 { 1234 /* SW: software increment write trap control */ 1235 if (arm_feature(env, ARM_FEATURE_V8) 1236 && arm_current_el(env) == 0 1237 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0 1238 && !isread) { 1239 return CP_ACCESS_OK; 1240 } 1241 1242 return pmreg_access(env, ri, isread); 1243 } 1244 1245 static CPAccessResult pmreg_access_selr(CPUARMState *env, 1246 const ARMCPRegInfo *ri, 1247 bool isread) 1248 { 1249 /* ER: event counter read trap control */ 1250 if (arm_feature(env, ARM_FEATURE_V8) 1251 && arm_current_el(env) == 0 1252 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) { 1253 return CP_ACCESS_OK; 1254 } 1255 1256 return pmreg_access(env, ri, isread); 1257 } 1258 1259 static CPAccessResult pmreg_access_ccntr(CPUARMState *env, 1260 const ARMCPRegInfo *ri, 1261 bool isread) 1262 { 1263 /* CR: cycle counter read trap control */ 1264 if (arm_feature(env, ARM_FEATURE_V8) 1265 && arm_current_el(env) == 0 1266 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0 1267 && isread) { 1268 return CP_ACCESS_OK; 1269 } 1270 1271 return pmreg_access(env, ri, isread); 1272 } 1273 1274 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using 1275 * the current EL, security state, and register configuration. 1276 */ 1277 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter) 1278 { 1279 uint64_t filter; 1280 bool e, p, u, nsk, nsu, nsh, m; 1281 bool enabled, prohibited, filtered; 1282 bool secure = arm_is_secure(env); 1283 int el = arm_current_el(env); 1284 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN; 1285 1286 if (!arm_feature(env, ARM_FEATURE_PMU)) { 1287 return false; 1288 } 1289 1290 if (!arm_feature(env, ARM_FEATURE_EL2) || 1291 (counter < hpmn || counter == 31)) { 1292 e = env->cp15.c9_pmcr & PMCRE; 1293 } else { 1294 e = env->cp15.mdcr_el2 & MDCR_HPME; 1295 } 1296 enabled = e && (env->cp15.c9_pmcnten & (1 << counter)); 1297 1298 if (!secure) { 1299 if (el == 2 && (counter < hpmn || counter == 31)) { 1300 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD; 1301 } else { 1302 prohibited = false; 1303 } 1304 } else { 1305 prohibited = arm_feature(env, ARM_FEATURE_EL3) && 1306 (env->cp15.mdcr_el3 & MDCR_SPME); 1307 } 1308 1309 if (prohibited && counter == 31) { 1310 prohibited = env->cp15.c9_pmcr & PMCRDP; 1311 } 1312 1313 if (counter == 31) { 1314 filter = env->cp15.pmccfiltr_el0; 1315 } else { 1316 filter = env->cp15.c14_pmevtyper[counter]; 1317 } 1318 1319 p = filter & PMXEVTYPER_P; 1320 u = filter & PMXEVTYPER_U; 1321 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK); 1322 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU); 1323 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH); 1324 m = arm_el_is_aa64(env, 1) && 1325 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M); 1326 1327 if (el == 0) { 1328 filtered = secure ? u : u != nsu; 1329 } else if (el == 1) { 1330 filtered = secure ? p : p != nsk; 1331 } else if (el == 2) { 1332 filtered = !nsh; 1333 } else { /* EL3 */ 1334 filtered = m != p; 1335 } 1336 1337 if (counter != 31) { 1338 /* 1339 * If not checking PMCCNTR, ensure the counter is setup to an event we 1340 * support 1341 */ 1342 uint16_t event = filter & PMXEVTYPER_EVTCOUNT; 1343 if (!event_supported(event)) { 1344 return false; 1345 } 1346 } 1347 1348 return enabled && !prohibited && !filtered; 1349 } 1350 1351 static void pmu_update_irq(CPUARMState *env) 1352 { 1353 ARMCPU *cpu = arm_env_get_cpu(env); 1354 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) && 1355 (env->cp15.c9_pminten & env->cp15.c9_pmovsr)); 1356 } 1357 1358 /* 1359 * Ensure c15_ccnt is the guest-visible count so that operations such as 1360 * enabling/disabling the counter or filtering, modifying the count itself, 1361 * etc. can be done logically. This is essentially a no-op if the counter is 1362 * not enabled at the time of the call. 1363 */ 1364 static void pmccntr_op_start(CPUARMState *env) 1365 { 1366 uint64_t cycles = cycles_get_count(env); 1367 1368 if (pmu_counter_enabled(env, 31)) { 1369 uint64_t eff_cycles = cycles; 1370 if (env->cp15.c9_pmcr & PMCRD) { 1371 /* Increment once every 64 processor clock cycles */ 1372 eff_cycles /= 64; 1373 } 1374 1375 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta; 1376 1377 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \ 1378 1ull << 63 : 1ull << 31; 1379 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) { 1380 env->cp15.c9_pmovsr |= (1 << 31); 1381 pmu_update_irq(env); 1382 } 1383 1384 env->cp15.c15_ccnt = new_pmccntr; 1385 } 1386 env->cp15.c15_ccnt_delta = cycles; 1387 } 1388 1389 /* 1390 * If PMCCNTR is enabled, recalculate the delta between the clock and the 1391 * guest-visible count. A call to pmccntr_op_finish should follow every call to 1392 * pmccntr_op_start. 1393 */ 1394 static void pmccntr_op_finish(CPUARMState *env) 1395 { 1396 if (pmu_counter_enabled(env, 31)) { 1397 #ifndef CONFIG_USER_ONLY 1398 /* Calculate when the counter will next overflow */ 1399 uint64_t remaining_cycles = -env->cp15.c15_ccnt; 1400 if (!(env->cp15.c9_pmcr & PMCRLC)) { 1401 remaining_cycles = (uint32_t)remaining_cycles; 1402 } 1403 int64_t overflow_in = cycles_ns_per(remaining_cycles); 1404 1405 if (overflow_in > 0) { 1406 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1407 overflow_in; 1408 ARMCPU *cpu = arm_env_get_cpu(env); 1409 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1410 } 1411 #endif 1412 1413 uint64_t prev_cycles = env->cp15.c15_ccnt_delta; 1414 if (env->cp15.c9_pmcr & PMCRD) { 1415 /* Increment once every 64 processor clock cycles */ 1416 prev_cycles /= 64; 1417 } 1418 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt; 1419 } 1420 } 1421 1422 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter) 1423 { 1424 1425 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1426 uint64_t count = 0; 1427 if (event_supported(event)) { 1428 uint16_t event_idx = supported_event_map[event]; 1429 count = pm_events[event_idx].get_count(env); 1430 } 1431 1432 if (pmu_counter_enabled(env, counter)) { 1433 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter]; 1434 1435 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) { 1436 env->cp15.c9_pmovsr |= (1 << counter); 1437 pmu_update_irq(env); 1438 } 1439 env->cp15.c14_pmevcntr[counter] = new_pmevcntr; 1440 } 1441 env->cp15.c14_pmevcntr_delta[counter] = count; 1442 } 1443 1444 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter) 1445 { 1446 if (pmu_counter_enabled(env, counter)) { 1447 #ifndef CONFIG_USER_ONLY 1448 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1449 uint16_t event_idx = supported_event_map[event]; 1450 uint64_t delta = UINT32_MAX - 1451 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1; 1452 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta); 1453 1454 if (overflow_in > 0) { 1455 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1456 overflow_in; 1457 ARMCPU *cpu = arm_env_get_cpu(env); 1458 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1459 } 1460 #endif 1461 1462 env->cp15.c14_pmevcntr_delta[counter] -= 1463 env->cp15.c14_pmevcntr[counter]; 1464 } 1465 } 1466 1467 void pmu_op_start(CPUARMState *env) 1468 { 1469 unsigned int i; 1470 pmccntr_op_start(env); 1471 for (i = 0; i < pmu_num_counters(env); i++) { 1472 pmevcntr_op_start(env, i); 1473 } 1474 } 1475 1476 void pmu_op_finish(CPUARMState *env) 1477 { 1478 unsigned int i; 1479 pmccntr_op_finish(env); 1480 for (i = 0; i < pmu_num_counters(env); i++) { 1481 pmevcntr_op_finish(env, i); 1482 } 1483 } 1484 1485 void pmu_pre_el_change(ARMCPU *cpu, void *ignored) 1486 { 1487 pmu_op_start(&cpu->env); 1488 } 1489 1490 void pmu_post_el_change(ARMCPU *cpu, void *ignored) 1491 { 1492 pmu_op_finish(&cpu->env); 1493 } 1494 1495 void arm_pmu_timer_cb(void *opaque) 1496 { 1497 ARMCPU *cpu = opaque; 1498 1499 /* 1500 * Update all the counter values based on the current underlying counts, 1501 * triggering interrupts to be raised, if necessary. pmu_op_finish() also 1502 * has the effect of setting the cpu->pmu_timer to the next earliest time a 1503 * counter may expire. 1504 */ 1505 pmu_op_start(&cpu->env); 1506 pmu_op_finish(&cpu->env); 1507 } 1508 1509 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1510 uint64_t value) 1511 { 1512 pmu_op_start(env); 1513 1514 if (value & PMCRC) { 1515 /* The counter has been reset */ 1516 env->cp15.c15_ccnt = 0; 1517 } 1518 1519 if (value & PMCRP) { 1520 unsigned int i; 1521 for (i = 0; i < pmu_num_counters(env); i++) { 1522 env->cp15.c14_pmevcntr[i] = 0; 1523 } 1524 } 1525 1526 /* only the DP, X, D and E bits are writable */ 1527 env->cp15.c9_pmcr &= ~0x39; 1528 env->cp15.c9_pmcr |= (value & 0x39); 1529 1530 pmu_op_finish(env); 1531 } 1532 1533 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri, 1534 uint64_t value) 1535 { 1536 unsigned int i; 1537 for (i = 0; i < pmu_num_counters(env); i++) { 1538 /* Increment a counter's count iff: */ 1539 if ((value & (1 << i)) && /* counter's bit is set */ 1540 /* counter is enabled and not filtered */ 1541 pmu_counter_enabled(env, i) && 1542 /* counter is SW_INCR */ 1543 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) { 1544 pmevcntr_op_start(env, i); 1545 1546 /* 1547 * Detect if this write causes an overflow since we can't predict 1548 * PMSWINC overflows like we can for other events 1549 */ 1550 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1; 1551 1552 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) { 1553 env->cp15.c9_pmovsr |= (1 << i); 1554 pmu_update_irq(env); 1555 } 1556 1557 env->cp15.c14_pmevcntr[i] = new_pmswinc; 1558 1559 pmevcntr_op_finish(env, i); 1560 } 1561 } 1562 } 1563 1564 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1565 { 1566 uint64_t ret; 1567 pmccntr_op_start(env); 1568 ret = env->cp15.c15_ccnt; 1569 pmccntr_op_finish(env); 1570 return ret; 1571 } 1572 1573 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1574 uint64_t value) 1575 { 1576 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and 1577 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the 1578 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are 1579 * accessed. 1580 */ 1581 env->cp15.c9_pmselr = value & 0x1f; 1582 } 1583 1584 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1585 uint64_t value) 1586 { 1587 pmccntr_op_start(env); 1588 env->cp15.c15_ccnt = value; 1589 pmccntr_op_finish(env); 1590 } 1591 1592 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, 1593 uint64_t value) 1594 { 1595 uint64_t cur_val = pmccntr_read(env, NULL); 1596 1597 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); 1598 } 1599 1600 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1601 uint64_t value) 1602 { 1603 pmccntr_op_start(env); 1604 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0; 1605 pmccntr_op_finish(env); 1606 } 1607 1608 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri, 1609 uint64_t value) 1610 { 1611 pmccntr_op_start(env); 1612 /* M is not accessible from AArch32 */ 1613 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) | 1614 (value & PMCCFILTR); 1615 pmccntr_op_finish(env); 1616 } 1617 1618 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri) 1619 { 1620 /* M is not visible in AArch32 */ 1621 return env->cp15.pmccfiltr_el0 & PMCCFILTR; 1622 } 1623 1624 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1625 uint64_t value) 1626 { 1627 value &= pmu_counter_mask(env); 1628 env->cp15.c9_pmcnten |= value; 1629 } 1630 1631 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1632 uint64_t value) 1633 { 1634 value &= pmu_counter_mask(env); 1635 env->cp15.c9_pmcnten &= ~value; 1636 } 1637 1638 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1639 uint64_t value) 1640 { 1641 value &= pmu_counter_mask(env); 1642 env->cp15.c9_pmovsr &= ~value; 1643 pmu_update_irq(env); 1644 } 1645 1646 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1647 uint64_t value) 1648 { 1649 value &= pmu_counter_mask(env); 1650 env->cp15.c9_pmovsr |= value; 1651 pmu_update_irq(env); 1652 } 1653 1654 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1655 uint64_t value, const uint8_t counter) 1656 { 1657 if (counter == 31) { 1658 pmccfiltr_write(env, ri, value); 1659 } else if (counter < pmu_num_counters(env)) { 1660 pmevcntr_op_start(env, counter); 1661 1662 /* 1663 * If this counter's event type is changing, store the current 1664 * underlying count for the new type in c14_pmevcntr_delta[counter] so 1665 * pmevcntr_op_finish has the correct baseline when it converts back to 1666 * a delta. 1667 */ 1668 uint16_t old_event = env->cp15.c14_pmevtyper[counter] & 1669 PMXEVTYPER_EVTCOUNT; 1670 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT; 1671 if (old_event != new_event) { 1672 uint64_t count = 0; 1673 if (event_supported(new_event)) { 1674 uint16_t event_idx = supported_event_map[new_event]; 1675 count = pm_events[event_idx].get_count(env); 1676 } 1677 env->cp15.c14_pmevcntr_delta[counter] = count; 1678 } 1679 1680 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK; 1681 pmevcntr_op_finish(env, counter); 1682 } 1683 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when 1684 * PMSELR value is equal to or greater than the number of implemented 1685 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI. 1686 */ 1687 } 1688 1689 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri, 1690 const uint8_t counter) 1691 { 1692 if (counter == 31) { 1693 return env->cp15.pmccfiltr_el0; 1694 } else if (counter < pmu_num_counters(env)) { 1695 return env->cp15.c14_pmevtyper[counter]; 1696 } else { 1697 /* 1698 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER 1699 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write(). 1700 */ 1701 return 0; 1702 } 1703 } 1704 1705 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1706 uint64_t value) 1707 { 1708 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1709 pmevtyper_write(env, ri, value, counter); 1710 } 1711 1712 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1713 uint64_t value) 1714 { 1715 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1716 env->cp15.c14_pmevtyper[counter] = value; 1717 1718 /* 1719 * pmevtyper_rawwrite is called between a pair of pmu_op_start and 1720 * pmu_op_finish calls when loading saved state for a migration. Because 1721 * we're potentially updating the type of event here, the value written to 1722 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a 1723 * different counter type. Therefore, we need to set this value to the 1724 * current count for the counter type we're writing so that pmu_op_finish 1725 * has the correct count for its calculation. 1726 */ 1727 uint16_t event = value & PMXEVTYPER_EVTCOUNT; 1728 if (event_supported(event)) { 1729 uint16_t event_idx = supported_event_map[event]; 1730 env->cp15.c14_pmevcntr_delta[counter] = 1731 pm_events[event_idx].get_count(env); 1732 } 1733 } 1734 1735 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1736 { 1737 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1738 return pmevtyper_read(env, ri, counter); 1739 } 1740 1741 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1742 uint64_t value) 1743 { 1744 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31); 1745 } 1746 1747 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri) 1748 { 1749 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31); 1750 } 1751 1752 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1753 uint64_t value, uint8_t counter) 1754 { 1755 if (counter < pmu_num_counters(env)) { 1756 pmevcntr_op_start(env, counter); 1757 env->cp15.c14_pmevcntr[counter] = value; 1758 pmevcntr_op_finish(env, counter); 1759 } 1760 /* 1761 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1762 * are CONSTRAINED UNPREDICTABLE. 1763 */ 1764 } 1765 1766 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri, 1767 uint8_t counter) 1768 { 1769 if (counter < pmu_num_counters(env)) { 1770 uint64_t ret; 1771 pmevcntr_op_start(env, counter); 1772 ret = env->cp15.c14_pmevcntr[counter]; 1773 pmevcntr_op_finish(env, counter); 1774 return ret; 1775 } else { 1776 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1777 * are CONSTRAINED UNPREDICTABLE. */ 1778 return 0; 1779 } 1780 } 1781 1782 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1783 uint64_t value) 1784 { 1785 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1786 pmevcntr_write(env, ri, value, counter); 1787 } 1788 1789 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1790 { 1791 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1792 return pmevcntr_read(env, ri, counter); 1793 } 1794 1795 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1796 uint64_t value) 1797 { 1798 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1799 assert(counter < pmu_num_counters(env)); 1800 env->cp15.c14_pmevcntr[counter] = value; 1801 pmevcntr_write(env, ri, value, counter); 1802 } 1803 1804 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri) 1805 { 1806 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1807 assert(counter < pmu_num_counters(env)); 1808 return env->cp15.c14_pmevcntr[counter]; 1809 } 1810 1811 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1812 uint64_t value) 1813 { 1814 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31); 1815 } 1816 1817 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1818 { 1819 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31); 1820 } 1821 1822 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1823 uint64_t value) 1824 { 1825 if (arm_feature(env, ARM_FEATURE_V8)) { 1826 env->cp15.c9_pmuserenr = value & 0xf; 1827 } else { 1828 env->cp15.c9_pmuserenr = value & 1; 1829 } 1830 } 1831 1832 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1833 uint64_t value) 1834 { 1835 /* We have no event counters so only the C bit can be changed */ 1836 value &= pmu_counter_mask(env); 1837 env->cp15.c9_pminten |= value; 1838 pmu_update_irq(env); 1839 } 1840 1841 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1842 uint64_t value) 1843 { 1844 value &= pmu_counter_mask(env); 1845 env->cp15.c9_pminten &= ~value; 1846 pmu_update_irq(env); 1847 } 1848 1849 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 1850 uint64_t value) 1851 { 1852 /* Note that even though the AArch64 view of this register has bits 1853 * [10:0] all RES0 we can only mask the bottom 5, to comply with the 1854 * architectural requirements for bits which are RES0 only in some 1855 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 1856 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) 1857 */ 1858 raw_write(env, ri, value & ~0x1FULL); 1859 } 1860 1861 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 1862 { 1863 /* Begin with base v8.0 state. */ 1864 uint32_t valid_mask = 0x3fff; 1865 ARMCPU *cpu = arm_env_get_cpu(env); 1866 1867 if (arm_el_is_aa64(env, 3)) { 1868 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */ 1869 valid_mask &= ~SCR_NET; 1870 } else { 1871 valid_mask &= ~(SCR_RW | SCR_ST); 1872 } 1873 1874 if (!arm_feature(env, ARM_FEATURE_EL2)) { 1875 valid_mask &= ~SCR_HCE; 1876 1877 /* On ARMv7, SMD (or SCD as it is called in v7) is only 1878 * supported if EL2 exists. The bit is UNK/SBZP when 1879 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero 1880 * when EL2 is unavailable. 1881 * On ARMv8, this bit is always available. 1882 */ 1883 if (arm_feature(env, ARM_FEATURE_V7) && 1884 !arm_feature(env, ARM_FEATURE_V8)) { 1885 valid_mask &= ~SCR_SMD; 1886 } 1887 } 1888 if (cpu_isar_feature(aa64_lor, cpu)) { 1889 valid_mask |= SCR_TLOR; 1890 } 1891 if (cpu_isar_feature(aa64_pauth, cpu)) { 1892 valid_mask |= SCR_API | SCR_APK; 1893 } 1894 1895 /* Clear all-context RES0 bits. */ 1896 value &= valid_mask; 1897 raw_write(env, ri, value); 1898 } 1899 1900 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1901 { 1902 ARMCPU *cpu = arm_env_get_cpu(env); 1903 1904 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR 1905 * bank 1906 */ 1907 uint32_t index = A32_BANKED_REG_GET(env, csselr, 1908 ri->secure & ARM_CP_SECSTATE_S); 1909 1910 return cpu->ccsidr[index]; 1911 } 1912 1913 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1914 uint64_t value) 1915 { 1916 raw_write(env, ri, value & 0xf); 1917 } 1918 1919 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1920 { 1921 CPUState *cs = ENV_GET_CPU(env); 1922 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 1923 uint64_t ret = 0; 1924 1925 if (hcr_el2 & HCR_IMO) { 1926 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) { 1927 ret |= CPSR_I; 1928 } 1929 } else { 1930 if (cs->interrupt_request & CPU_INTERRUPT_HARD) { 1931 ret |= CPSR_I; 1932 } 1933 } 1934 1935 if (hcr_el2 & HCR_FMO) { 1936 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) { 1937 ret |= CPSR_F; 1938 } 1939 } else { 1940 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { 1941 ret |= CPSR_F; 1942 } 1943 } 1944 1945 /* External aborts are not possible in QEMU so A bit is always clear */ 1946 return ret; 1947 } 1948 1949 static const ARMCPRegInfo v7_cp_reginfo[] = { 1950 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ 1951 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 1952 .access = PL1_W, .type = ARM_CP_NOP }, 1953 /* Performance monitors are implementation defined in v7, 1954 * but with an ARM recommended set of registers, which we 1955 * follow. 1956 * 1957 * Performance registers fall into three categories: 1958 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) 1959 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) 1960 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) 1961 * For the cases controlled by PMUSERENR we must set .access to PL0_RW 1962 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. 1963 */ 1964 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, 1965 .access = PL0_RW, .type = ARM_CP_ALIAS, 1966 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1967 .writefn = pmcntenset_write, 1968 .accessfn = pmreg_access, 1969 .raw_writefn = raw_write }, 1970 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, 1971 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, 1972 .access = PL0_RW, .accessfn = pmreg_access, 1973 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, 1974 .writefn = pmcntenset_write, .raw_writefn = raw_write }, 1975 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, 1976 .access = PL0_RW, 1977 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1978 .accessfn = pmreg_access, 1979 .writefn = pmcntenclr_write, 1980 .type = ARM_CP_ALIAS }, 1981 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, 1982 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, 1983 .access = PL0_RW, .accessfn = pmreg_access, 1984 .type = ARM_CP_ALIAS, 1985 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), 1986 .writefn = pmcntenclr_write }, 1987 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, 1988 .access = PL0_RW, .type = ARM_CP_IO, 1989 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 1990 .accessfn = pmreg_access, 1991 .writefn = pmovsr_write, 1992 .raw_writefn = raw_write }, 1993 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64, 1994 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3, 1995 .access = PL0_RW, .accessfn = pmreg_access, 1996 .type = ARM_CP_ALIAS | ARM_CP_IO, 1997 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 1998 .writefn = pmovsr_write, 1999 .raw_writefn = raw_write }, 2000 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, 2001 .access = PL0_W, .accessfn = pmreg_access_swinc, 2002 .type = ARM_CP_NO_RAW | ARM_CP_IO, 2003 .writefn = pmswinc_write }, 2004 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64, 2005 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4, 2006 .access = PL0_W, .accessfn = pmreg_access_swinc, 2007 .type = ARM_CP_NO_RAW | ARM_CP_IO, 2008 .writefn = pmswinc_write }, 2009 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, 2010 .access = PL0_RW, .type = ARM_CP_ALIAS, 2011 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr), 2012 .accessfn = pmreg_access_selr, .writefn = pmselr_write, 2013 .raw_writefn = raw_write}, 2014 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64, 2015 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5, 2016 .access = PL0_RW, .accessfn = pmreg_access_selr, 2017 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr), 2018 .writefn = pmselr_write, .raw_writefn = raw_write, }, 2019 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, 2020 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO, 2021 .readfn = pmccntr_read, .writefn = pmccntr_write32, 2022 .accessfn = pmreg_access_ccntr }, 2023 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, 2024 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, 2025 .access = PL0_RW, .accessfn = pmreg_access_ccntr, 2026 .type = ARM_CP_IO, 2027 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt), 2028 .readfn = pmccntr_read, .writefn = pmccntr_write, 2029 .raw_readfn = raw_read, .raw_writefn = raw_write, }, 2030 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7, 2031 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32, 2032 .access = PL0_RW, .accessfn = pmreg_access, 2033 .type = ARM_CP_ALIAS | ARM_CP_IO, 2034 .resetvalue = 0, }, 2035 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, 2036 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, 2037 .writefn = pmccfiltr_write, .raw_writefn = raw_write, 2038 .access = PL0_RW, .accessfn = pmreg_access, 2039 .type = ARM_CP_IO, 2040 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), 2041 .resetvalue = 0, }, 2042 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, 2043 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2044 .accessfn = pmreg_access, 2045 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 2046 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64, 2047 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1, 2048 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2049 .accessfn = pmreg_access, 2050 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 2051 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, 2052 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2053 .accessfn = pmreg_access_xevcntr, 2054 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2055 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64, 2056 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2, 2057 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2058 .accessfn = pmreg_access_xevcntr, 2059 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2060 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, 2061 .access = PL0_R | PL1_RW, .accessfn = access_tpm, 2062 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr), 2063 .resetvalue = 0, 2064 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2065 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64, 2066 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0, 2067 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 2068 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), 2069 .resetvalue = 0, 2070 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2071 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, 2072 .access = PL1_RW, .accessfn = access_tpm, 2073 .type = ARM_CP_ALIAS | ARM_CP_IO, 2074 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten), 2075 .resetvalue = 0, 2076 .writefn = pmintenset_write, .raw_writefn = raw_write }, 2077 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64, 2078 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1, 2079 .access = PL1_RW, .accessfn = access_tpm, 2080 .type = ARM_CP_IO, 2081 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2082 .writefn = pmintenset_write, .raw_writefn = raw_write, 2083 .resetvalue = 0x0 }, 2084 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, 2085 .access = PL1_RW, .accessfn = access_tpm, 2086 .type = ARM_CP_ALIAS | ARM_CP_IO, 2087 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2088 .writefn = pmintenclr_write, }, 2089 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64, 2090 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2, 2091 .access = PL1_RW, .accessfn = access_tpm, 2092 .type = ARM_CP_ALIAS | ARM_CP_IO, 2093 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2094 .writefn = pmintenclr_write }, 2095 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, 2096 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, 2097 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW }, 2098 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, 2099 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, 2100 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0, 2101 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), 2102 offsetof(CPUARMState, cp15.csselr_ns) } }, 2103 /* Auxiliary ID register: this actually has an IMPDEF value but for now 2104 * just RAZ for all cores: 2105 */ 2106 { .name = "AIDR", .state = ARM_CP_STATE_BOTH, 2107 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, 2108 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 2109 /* Auxiliary fault status registers: these also are IMPDEF, and we 2110 * choose to RAZ/WI for all cores. 2111 */ 2112 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, 2113 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, 2114 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 2115 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, 2116 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, 2117 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 2118 /* MAIR can just read-as-written because we don't implement caches 2119 * and so don't need to care about memory attributes. 2120 */ 2121 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, 2122 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 2123 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]), 2124 .resetvalue = 0 }, 2125 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64, 2126 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0, 2127 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]), 2128 .resetvalue = 0 }, 2129 /* For non-long-descriptor page tables these are PRRR and NMRR; 2130 * regardless they still act as reads-as-written for QEMU. 2131 */ 2132 /* MAIR0/1 are defined separately from their 64-bit counterpart which 2133 * allows them to assign the correct fieldoffset based on the endianness 2134 * handled in the field definitions. 2135 */ 2136 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, 2137 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW, 2138 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s), 2139 offsetof(CPUARMState, cp15.mair0_ns) }, 2140 .resetfn = arm_cp_reset_ignore }, 2141 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, 2142 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW, 2143 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s), 2144 offsetof(CPUARMState, cp15.mair1_ns) }, 2145 .resetfn = arm_cp_reset_ignore }, 2146 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, 2147 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, 2148 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read }, 2149 /* 32 bit ITLB invalidates */ 2150 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0, 2151 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 2152 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, 2153 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 2154 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2, 2155 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 2156 /* 32 bit DTLB invalidates */ 2157 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0, 2158 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 2159 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, 2160 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 2161 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2, 2162 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 2163 /* 32 bit TLB invalidates */ 2164 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 2165 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 2166 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 2167 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 2168 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 2169 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 2170 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 2171 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, 2172 REGINFO_SENTINEL 2173 }; 2174 2175 static const ARMCPRegInfo v7mp_cp_reginfo[] = { 2176 /* 32 bit TLB invalidates, Inner Shareable */ 2177 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 2178 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write }, 2179 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 2180 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, 2181 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 2182 .type = ARM_CP_NO_RAW, .access = PL1_W, 2183 .writefn = tlbiasid_is_write }, 2184 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 2185 .type = ARM_CP_NO_RAW, .access = PL1_W, 2186 .writefn = tlbimvaa_is_write }, 2187 REGINFO_SENTINEL 2188 }; 2189 2190 static const ARMCPRegInfo pmovsset_cp_reginfo[] = { 2191 /* PMOVSSET is not implemented in v7 before v7ve */ 2192 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3, 2193 .access = PL0_RW, .accessfn = pmreg_access, 2194 .type = ARM_CP_ALIAS | ARM_CP_IO, 2195 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 2196 .writefn = pmovsset_write, 2197 .raw_writefn = raw_write }, 2198 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64, 2199 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3, 2200 .access = PL0_RW, .accessfn = pmreg_access, 2201 .type = ARM_CP_ALIAS | ARM_CP_IO, 2202 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 2203 .writefn = pmovsset_write, 2204 .raw_writefn = raw_write }, 2205 REGINFO_SENTINEL 2206 }; 2207 2208 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2209 uint64_t value) 2210 { 2211 value &= 1; 2212 env->teecr = value; 2213 } 2214 2215 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri, 2216 bool isread) 2217 { 2218 if (arm_current_el(env) == 0 && (env->teecr & 1)) { 2219 return CP_ACCESS_TRAP; 2220 } 2221 return CP_ACCESS_OK; 2222 } 2223 2224 static const ARMCPRegInfo t2ee_cp_reginfo[] = { 2225 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, 2226 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), 2227 .resetvalue = 0, 2228 .writefn = teecr_write }, 2229 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, 2230 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), 2231 .accessfn = teehbr_access, .resetvalue = 0 }, 2232 REGINFO_SENTINEL 2233 }; 2234 2235 static const ARMCPRegInfo v6k_cp_reginfo[] = { 2236 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, 2237 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, 2238 .access = PL0_RW, 2239 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 }, 2240 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, 2241 .access = PL0_RW, 2242 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s), 2243 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, 2244 .resetfn = arm_cp_reset_ignore }, 2245 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, 2246 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, 2247 .access = PL0_R|PL1_W, 2248 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]), 2249 .resetvalue = 0}, 2250 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, 2251 .access = PL0_R|PL1_W, 2252 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s), 2253 offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, 2254 .resetfn = arm_cp_reset_ignore }, 2255 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64, 2256 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, 2257 .access = PL1_RW, 2258 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 }, 2259 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4, 2260 .access = PL1_RW, 2261 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s), 2262 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) }, 2263 .resetvalue = 0 }, 2264 REGINFO_SENTINEL 2265 }; 2266 2267 #ifndef CONFIG_USER_ONLY 2268 2269 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, 2270 bool isread) 2271 { 2272 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero. 2273 * Writable only at the highest implemented exception level. 2274 */ 2275 int el = arm_current_el(env); 2276 2277 switch (el) { 2278 case 0: 2279 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) { 2280 return CP_ACCESS_TRAP; 2281 } 2282 break; 2283 case 1: 2284 if (!isread && ri->state == ARM_CP_STATE_AA32 && 2285 arm_is_secure_below_el3(env)) { 2286 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */ 2287 return CP_ACCESS_TRAP_UNCATEGORIZED; 2288 } 2289 break; 2290 case 2: 2291 case 3: 2292 break; 2293 } 2294 2295 if (!isread && el < arm_highest_el(env)) { 2296 return CP_ACCESS_TRAP_UNCATEGORIZED; 2297 } 2298 2299 return CP_ACCESS_OK; 2300 } 2301 2302 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx, 2303 bool isread) 2304 { 2305 unsigned int cur_el = arm_current_el(env); 2306 bool secure = arm_is_secure(env); 2307 2308 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */ 2309 if (cur_el == 0 && 2310 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) { 2311 return CP_ACCESS_TRAP; 2312 } 2313 2314 if (arm_feature(env, ARM_FEATURE_EL2) && 2315 timeridx == GTIMER_PHYS && !secure && cur_el < 2 && 2316 !extract32(env->cp15.cnthctl_el2, 0, 1)) { 2317 return CP_ACCESS_TRAP_EL2; 2318 } 2319 return CP_ACCESS_OK; 2320 } 2321 2322 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx, 2323 bool isread) 2324 { 2325 unsigned int cur_el = arm_current_el(env); 2326 bool secure = arm_is_secure(env); 2327 2328 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if 2329 * EL0[PV]TEN is zero. 2330 */ 2331 if (cur_el == 0 && 2332 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { 2333 return CP_ACCESS_TRAP; 2334 } 2335 2336 if (arm_feature(env, ARM_FEATURE_EL2) && 2337 timeridx == GTIMER_PHYS && !secure && cur_el < 2 && 2338 !extract32(env->cp15.cnthctl_el2, 1, 1)) { 2339 return CP_ACCESS_TRAP_EL2; 2340 } 2341 return CP_ACCESS_OK; 2342 } 2343 2344 static CPAccessResult gt_pct_access(CPUARMState *env, 2345 const ARMCPRegInfo *ri, 2346 bool isread) 2347 { 2348 return gt_counter_access(env, GTIMER_PHYS, isread); 2349 } 2350 2351 static CPAccessResult gt_vct_access(CPUARMState *env, 2352 const ARMCPRegInfo *ri, 2353 bool isread) 2354 { 2355 return gt_counter_access(env, GTIMER_VIRT, isread); 2356 } 2357 2358 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2359 bool isread) 2360 { 2361 return gt_timer_access(env, GTIMER_PHYS, isread); 2362 } 2363 2364 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2365 bool isread) 2366 { 2367 return gt_timer_access(env, GTIMER_VIRT, isread); 2368 } 2369 2370 static CPAccessResult gt_stimer_access(CPUARMState *env, 2371 const ARMCPRegInfo *ri, 2372 bool isread) 2373 { 2374 /* The AArch64 register view of the secure physical timer is 2375 * always accessible from EL3, and configurably accessible from 2376 * Secure EL1. 2377 */ 2378 switch (arm_current_el(env)) { 2379 case 1: 2380 if (!arm_is_secure(env)) { 2381 return CP_ACCESS_TRAP; 2382 } 2383 if (!(env->cp15.scr_el3 & SCR_ST)) { 2384 return CP_ACCESS_TRAP_EL3; 2385 } 2386 return CP_ACCESS_OK; 2387 case 0: 2388 case 2: 2389 return CP_ACCESS_TRAP; 2390 case 3: 2391 return CP_ACCESS_OK; 2392 default: 2393 g_assert_not_reached(); 2394 } 2395 } 2396 2397 static uint64_t gt_get_countervalue(CPUARMState *env) 2398 { 2399 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE; 2400 } 2401 2402 static void gt_recalc_timer(ARMCPU *cpu, int timeridx) 2403 { 2404 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; 2405 2406 if (gt->ctl & 1) { 2407 /* Timer enabled: calculate and set current ISTATUS, irq, and 2408 * reset timer to when ISTATUS next has to change 2409 */ 2410 uint64_t offset = timeridx == GTIMER_VIRT ? 2411 cpu->env.cp15.cntvoff_el2 : 0; 2412 uint64_t count = gt_get_countervalue(&cpu->env); 2413 /* Note that this must be unsigned 64 bit arithmetic: */ 2414 int istatus = count - offset >= gt->cval; 2415 uint64_t nexttick; 2416 int irqstate; 2417 2418 gt->ctl = deposit32(gt->ctl, 2, 1, istatus); 2419 2420 irqstate = (istatus && !(gt->ctl & 2)); 2421 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 2422 2423 if (istatus) { 2424 /* Next transition is when count rolls back over to zero */ 2425 nexttick = UINT64_MAX; 2426 } else { 2427 /* Next transition is when we hit cval */ 2428 nexttick = gt->cval + offset; 2429 } 2430 /* Note that the desired next expiry time might be beyond the 2431 * signed-64-bit range of a QEMUTimer -- in this case we just 2432 * set the timer for as far in the future as possible. When the 2433 * timer expires we will reset the timer for any remaining period. 2434 */ 2435 if (nexttick > INT64_MAX / GTIMER_SCALE) { 2436 nexttick = INT64_MAX / GTIMER_SCALE; 2437 } 2438 timer_mod(cpu->gt_timer[timeridx], nexttick); 2439 trace_arm_gt_recalc(timeridx, irqstate, nexttick); 2440 } else { 2441 /* Timer disabled: ISTATUS and timer output always clear */ 2442 gt->ctl &= ~4; 2443 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0); 2444 timer_del(cpu->gt_timer[timeridx]); 2445 trace_arm_gt_recalc_disabled(timeridx); 2446 } 2447 } 2448 2449 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri, 2450 int timeridx) 2451 { 2452 ARMCPU *cpu = arm_env_get_cpu(env); 2453 2454 timer_del(cpu->gt_timer[timeridx]); 2455 } 2456 2457 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2458 { 2459 return gt_get_countervalue(env); 2460 } 2461 2462 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2463 { 2464 return gt_get_countervalue(env) - env->cp15.cntvoff_el2; 2465 } 2466 2467 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2468 int timeridx, 2469 uint64_t value) 2470 { 2471 trace_arm_gt_cval_write(timeridx, value); 2472 env->cp15.c14_timer[timeridx].cval = value; 2473 gt_recalc_timer(arm_env_get_cpu(env), timeridx); 2474 } 2475 2476 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, 2477 int timeridx) 2478 { 2479 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; 2480 2481 return (uint32_t)(env->cp15.c14_timer[timeridx].cval - 2482 (gt_get_countervalue(env) - offset)); 2483 } 2484 2485 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2486 int timeridx, 2487 uint64_t value) 2488 { 2489 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; 2490 2491 trace_arm_gt_tval_write(timeridx, value); 2492 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset + 2493 sextract64(value, 0, 32); 2494 gt_recalc_timer(arm_env_get_cpu(env), timeridx); 2495 } 2496 2497 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2498 int timeridx, 2499 uint64_t value) 2500 { 2501 ARMCPU *cpu = arm_env_get_cpu(env); 2502 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; 2503 2504 trace_arm_gt_ctl_write(timeridx, value); 2505 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); 2506 if ((oldval ^ value) & 1) { 2507 /* Enable toggled */ 2508 gt_recalc_timer(cpu, timeridx); 2509 } else if ((oldval ^ value) & 2) { 2510 /* IMASK toggled: don't need to recalculate, 2511 * just set the interrupt line based on ISTATUS 2512 */ 2513 int irqstate = (oldval & 4) && !(value & 2); 2514 2515 trace_arm_gt_imask_toggle(timeridx, irqstate); 2516 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 2517 } 2518 } 2519 2520 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2521 { 2522 gt_timer_reset(env, ri, GTIMER_PHYS); 2523 } 2524 2525 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2526 uint64_t value) 2527 { 2528 gt_cval_write(env, ri, GTIMER_PHYS, value); 2529 } 2530 2531 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2532 { 2533 return gt_tval_read(env, ri, GTIMER_PHYS); 2534 } 2535 2536 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2537 uint64_t value) 2538 { 2539 gt_tval_write(env, ri, GTIMER_PHYS, value); 2540 } 2541 2542 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2543 uint64_t value) 2544 { 2545 gt_ctl_write(env, ri, GTIMER_PHYS, value); 2546 } 2547 2548 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2549 { 2550 gt_timer_reset(env, ri, GTIMER_VIRT); 2551 } 2552 2553 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2554 uint64_t value) 2555 { 2556 gt_cval_write(env, ri, GTIMER_VIRT, value); 2557 } 2558 2559 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2560 { 2561 return gt_tval_read(env, ri, GTIMER_VIRT); 2562 } 2563 2564 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2565 uint64_t value) 2566 { 2567 gt_tval_write(env, ri, GTIMER_VIRT, value); 2568 } 2569 2570 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2571 uint64_t value) 2572 { 2573 gt_ctl_write(env, ri, GTIMER_VIRT, value); 2574 } 2575 2576 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri, 2577 uint64_t value) 2578 { 2579 ARMCPU *cpu = arm_env_get_cpu(env); 2580 2581 trace_arm_gt_cntvoff_write(value); 2582 raw_write(env, ri, value); 2583 gt_recalc_timer(cpu, GTIMER_VIRT); 2584 } 2585 2586 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2587 { 2588 gt_timer_reset(env, ri, GTIMER_HYP); 2589 } 2590 2591 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2592 uint64_t value) 2593 { 2594 gt_cval_write(env, ri, GTIMER_HYP, value); 2595 } 2596 2597 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2598 { 2599 return gt_tval_read(env, ri, GTIMER_HYP); 2600 } 2601 2602 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2603 uint64_t value) 2604 { 2605 gt_tval_write(env, ri, GTIMER_HYP, value); 2606 } 2607 2608 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2609 uint64_t value) 2610 { 2611 gt_ctl_write(env, ri, GTIMER_HYP, value); 2612 } 2613 2614 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2615 { 2616 gt_timer_reset(env, ri, GTIMER_SEC); 2617 } 2618 2619 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2620 uint64_t value) 2621 { 2622 gt_cval_write(env, ri, GTIMER_SEC, value); 2623 } 2624 2625 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2626 { 2627 return gt_tval_read(env, ri, GTIMER_SEC); 2628 } 2629 2630 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2631 uint64_t value) 2632 { 2633 gt_tval_write(env, ri, GTIMER_SEC, value); 2634 } 2635 2636 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2637 uint64_t value) 2638 { 2639 gt_ctl_write(env, ri, GTIMER_SEC, value); 2640 } 2641 2642 void arm_gt_ptimer_cb(void *opaque) 2643 { 2644 ARMCPU *cpu = opaque; 2645 2646 gt_recalc_timer(cpu, GTIMER_PHYS); 2647 } 2648 2649 void arm_gt_vtimer_cb(void *opaque) 2650 { 2651 ARMCPU *cpu = opaque; 2652 2653 gt_recalc_timer(cpu, GTIMER_VIRT); 2654 } 2655 2656 void arm_gt_htimer_cb(void *opaque) 2657 { 2658 ARMCPU *cpu = opaque; 2659 2660 gt_recalc_timer(cpu, GTIMER_HYP); 2661 } 2662 2663 void arm_gt_stimer_cb(void *opaque) 2664 { 2665 ARMCPU *cpu = opaque; 2666 2667 gt_recalc_timer(cpu, GTIMER_SEC); 2668 } 2669 2670 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 2671 /* Note that CNTFRQ is purely reads-as-written for the benefit 2672 * of software; writing it doesn't actually change the timer frequency. 2673 * Our reset value matches the fixed frequency we implement the timer at. 2674 */ 2675 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, 2676 .type = ARM_CP_ALIAS, 2677 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 2678 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), 2679 }, 2680 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 2681 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 2682 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 2683 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 2684 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE, 2685 }, 2686 /* overall control: mostly access permissions */ 2687 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, 2688 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, 2689 .access = PL1_RW, 2690 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), 2691 .resetvalue = 0, 2692 }, 2693 /* per-timer control */ 2694 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 2695 .secure = ARM_CP_SECSTATE_NS, 2696 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 2697 .accessfn = gt_ptimer_access, 2698 .fieldoffset = offsetoflow32(CPUARMState, 2699 cp15.c14_timer[GTIMER_PHYS].ctl), 2700 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, 2701 }, 2702 { .name = "CNTP_CTL_S", 2703 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 2704 .secure = ARM_CP_SECSTATE_S, 2705 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 2706 .accessfn = gt_ptimer_access, 2707 .fieldoffset = offsetoflow32(CPUARMState, 2708 cp15.c14_timer[GTIMER_SEC].ctl), 2709 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 2710 }, 2711 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, 2712 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, 2713 .type = ARM_CP_IO, .access = PL0_RW, 2714 .accessfn = gt_ptimer_access, 2715 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 2716 .resetvalue = 0, 2717 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, 2718 }, 2719 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, 2720 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 2721 .accessfn = gt_vtimer_access, 2722 .fieldoffset = offsetoflow32(CPUARMState, 2723 cp15.c14_timer[GTIMER_VIRT].ctl), 2724 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, 2725 }, 2726 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, 2727 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, 2728 .type = ARM_CP_IO, .access = PL0_RW, 2729 .accessfn = gt_vtimer_access, 2730 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 2731 .resetvalue = 0, 2732 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, 2733 }, 2734 /* TimerValue views: a 32 bit downcounting view of the underlying state */ 2735 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 2736 .secure = ARM_CP_SECSTATE_NS, 2737 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2738 .accessfn = gt_ptimer_access, 2739 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, 2740 }, 2741 { .name = "CNTP_TVAL_S", 2742 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 2743 .secure = ARM_CP_SECSTATE_S, 2744 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2745 .accessfn = gt_ptimer_access, 2746 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write, 2747 }, 2748 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, 2749 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, 2750 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2751 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset, 2752 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, 2753 }, 2754 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, 2755 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2756 .accessfn = gt_vtimer_access, 2757 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, 2758 }, 2759 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, 2760 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, 2761 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2762 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset, 2763 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, 2764 }, 2765 /* The counter itself */ 2766 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, 2767 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 2768 .accessfn = gt_pct_access, 2769 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, 2770 }, 2771 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, 2772 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, 2773 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2774 .accessfn = gt_pct_access, .readfn = gt_cnt_read, 2775 }, 2776 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, 2777 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 2778 .accessfn = gt_vct_access, 2779 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, 2780 }, 2781 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 2782 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 2783 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2784 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, 2785 }, 2786 /* Comparison value, indicating when the timer goes off */ 2787 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, 2788 .secure = ARM_CP_SECSTATE_NS, 2789 .access = PL0_RW, 2790 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2791 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 2792 .accessfn = gt_ptimer_access, 2793 .writefn = gt_phys_cval_write, .raw_writefn = raw_write, 2794 }, 2795 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2, 2796 .secure = ARM_CP_SECSTATE_S, 2797 .access = PL0_RW, 2798 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2799 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 2800 .accessfn = gt_ptimer_access, 2801 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 2802 }, 2803 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, 2804 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, 2805 .access = PL0_RW, 2806 .type = ARM_CP_IO, 2807 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 2808 .resetvalue = 0, .accessfn = gt_ptimer_access, 2809 .writefn = gt_phys_cval_write, .raw_writefn = raw_write, 2810 }, 2811 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, 2812 .access = PL0_RW, 2813 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2814 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 2815 .accessfn = gt_vtimer_access, 2816 .writefn = gt_virt_cval_write, .raw_writefn = raw_write, 2817 }, 2818 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, 2819 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, 2820 .access = PL0_RW, 2821 .type = ARM_CP_IO, 2822 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 2823 .resetvalue = 0, .accessfn = gt_vtimer_access, 2824 .writefn = gt_virt_cval_write, .raw_writefn = raw_write, 2825 }, 2826 /* Secure timer -- this is actually restricted to only EL3 2827 * and configurably Secure-EL1 via the accessfn. 2828 */ 2829 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64, 2830 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0, 2831 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW, 2832 .accessfn = gt_stimer_access, 2833 .readfn = gt_sec_tval_read, 2834 .writefn = gt_sec_tval_write, 2835 .resetfn = gt_sec_timer_reset, 2836 }, 2837 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64, 2838 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1, 2839 .type = ARM_CP_IO, .access = PL1_RW, 2840 .accessfn = gt_stimer_access, 2841 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl), 2842 .resetvalue = 0, 2843 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 2844 }, 2845 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64, 2846 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2, 2847 .type = ARM_CP_IO, .access = PL1_RW, 2848 .accessfn = gt_stimer_access, 2849 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 2850 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 2851 }, 2852 REGINFO_SENTINEL 2853 }; 2854 2855 #else 2856 2857 /* In user-mode most of the generic timer registers are inaccessible 2858 * however modern kernels (4.12+) allow access to cntvct_el0 2859 */ 2860 2861 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2862 { 2863 /* Currently we have no support for QEMUTimer in linux-user so we 2864 * can't call gt_get_countervalue(env), instead we directly 2865 * call the lower level functions. 2866 */ 2867 return cpu_get_clock() / GTIMER_SCALE; 2868 } 2869 2870 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 2871 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 2872 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 2873 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */, 2874 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 2875 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE, 2876 }, 2877 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 2878 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 2879 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2880 .readfn = gt_virt_cnt_read, 2881 }, 2882 REGINFO_SENTINEL 2883 }; 2884 2885 #endif 2886 2887 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 2888 { 2889 if (arm_feature(env, ARM_FEATURE_LPAE)) { 2890 raw_write(env, ri, value); 2891 } else if (arm_feature(env, ARM_FEATURE_V7)) { 2892 raw_write(env, ri, value & 0xfffff6ff); 2893 } else { 2894 raw_write(env, ri, value & 0xfffff1ff); 2895 } 2896 } 2897 2898 #ifndef CONFIG_USER_ONLY 2899 /* get_phys_addr() isn't present for user-mode-only targets */ 2900 2901 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri, 2902 bool isread) 2903 { 2904 if (ri->opc2 & 4) { 2905 /* The ATS12NSO* operations must trap to EL3 if executed in 2906 * Secure EL1 (which can only happen if EL3 is AArch64). 2907 * They are simply UNDEF if executed from NS EL1. 2908 * They function normally from EL2 or EL3. 2909 */ 2910 if (arm_current_el(env) == 1) { 2911 if (arm_is_secure_below_el3(env)) { 2912 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3; 2913 } 2914 return CP_ACCESS_TRAP_UNCATEGORIZED; 2915 } 2916 } 2917 return CP_ACCESS_OK; 2918 } 2919 2920 static uint64_t do_ats_write(CPUARMState *env, uint64_t value, 2921 MMUAccessType access_type, ARMMMUIdx mmu_idx) 2922 { 2923 hwaddr phys_addr; 2924 target_ulong page_size; 2925 int prot; 2926 bool ret; 2927 uint64_t par64; 2928 bool format64 = false; 2929 MemTxAttrs attrs = {}; 2930 ARMMMUFaultInfo fi = {}; 2931 ARMCacheAttrs cacheattrs = {}; 2932 2933 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs, 2934 &prot, &page_size, &fi, &cacheattrs); 2935 2936 if (is_a64(env)) { 2937 format64 = true; 2938 } else if (arm_feature(env, ARM_FEATURE_LPAE)) { 2939 /* 2940 * ATS1Cxx: 2941 * * TTBCR.EAE determines whether the result is returned using the 2942 * 32-bit or the 64-bit PAR format 2943 * * Instructions executed in Hyp mode always use the 64bit format 2944 * 2945 * ATS1S2NSOxx uses the 64bit format if any of the following is true: 2946 * * The Non-secure TTBCR.EAE bit is set to 1 2947 * * The implementation includes EL2, and the value of HCR.VM is 1 2948 * 2949 * (Note that HCR.DC makes HCR.VM behave as if it is 1.) 2950 * 2951 * ATS1Hx always uses the 64bit format. 2952 */ 2953 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx); 2954 2955 if (arm_feature(env, ARM_FEATURE_EL2)) { 2956 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 2957 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC); 2958 } else { 2959 format64 |= arm_current_el(env) == 2; 2960 } 2961 } 2962 } 2963 2964 if (format64) { 2965 /* Create a 64-bit PAR */ 2966 par64 = (1 << 11); /* LPAE bit always set */ 2967 if (!ret) { 2968 par64 |= phys_addr & ~0xfffULL; 2969 if (!attrs.secure) { 2970 par64 |= (1 << 9); /* NS */ 2971 } 2972 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */ 2973 par64 |= cacheattrs.shareability << 7; /* SH */ 2974 } else { 2975 uint32_t fsr = arm_fi_to_lfsc(&fi); 2976 2977 par64 |= 1; /* F */ 2978 par64 |= (fsr & 0x3f) << 1; /* FS */ 2979 if (fi.stage2) { 2980 par64 |= (1 << 9); /* S */ 2981 } 2982 if (fi.s1ptw) { 2983 par64 |= (1 << 8); /* PTW */ 2984 } 2985 } 2986 } else { 2987 /* fsr is a DFSR/IFSR value for the short descriptor 2988 * translation table format (with WnR always clear). 2989 * Convert it to a 32-bit PAR. 2990 */ 2991 if (!ret) { 2992 /* We do not set any attribute bits in the PAR */ 2993 if (page_size == (1 << 24) 2994 && arm_feature(env, ARM_FEATURE_V7)) { 2995 par64 = (phys_addr & 0xff000000) | (1 << 1); 2996 } else { 2997 par64 = phys_addr & 0xfffff000; 2998 } 2999 if (!attrs.secure) { 3000 par64 |= (1 << 9); /* NS */ 3001 } 3002 } else { 3003 uint32_t fsr = arm_fi_to_sfsc(&fi); 3004 3005 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) | 3006 ((fsr & 0xf) << 1) | 1; 3007 } 3008 } 3009 return par64; 3010 } 3011 3012 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3013 { 3014 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3015 uint64_t par64; 3016 ARMMMUIdx mmu_idx; 3017 int el = arm_current_el(env); 3018 bool secure = arm_is_secure_below_el3(env); 3019 3020 switch (ri->opc2 & 6) { 3021 case 0: 3022 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */ 3023 switch (el) { 3024 case 3: 3025 mmu_idx = ARMMMUIdx_S1E3; 3026 break; 3027 case 2: 3028 mmu_idx = ARMMMUIdx_S1NSE1; 3029 break; 3030 case 1: 3031 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; 3032 break; 3033 default: 3034 g_assert_not_reached(); 3035 } 3036 break; 3037 case 2: 3038 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */ 3039 switch (el) { 3040 case 3: 3041 mmu_idx = ARMMMUIdx_S1SE0; 3042 break; 3043 case 2: 3044 mmu_idx = ARMMMUIdx_S1NSE0; 3045 break; 3046 case 1: 3047 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; 3048 break; 3049 default: 3050 g_assert_not_reached(); 3051 } 3052 break; 3053 case 4: 3054 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */ 3055 mmu_idx = ARMMMUIdx_S12NSE1; 3056 break; 3057 case 6: 3058 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */ 3059 mmu_idx = ARMMMUIdx_S12NSE0; 3060 break; 3061 default: 3062 g_assert_not_reached(); 3063 } 3064 3065 par64 = do_ats_write(env, value, access_type, mmu_idx); 3066 3067 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3068 } 3069 3070 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri, 3071 uint64_t value) 3072 { 3073 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3074 uint64_t par64; 3075 3076 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S1E2); 3077 3078 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3079 } 3080 3081 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri, 3082 bool isread) 3083 { 3084 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) { 3085 return CP_ACCESS_TRAP; 3086 } 3087 return CP_ACCESS_OK; 3088 } 3089 3090 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri, 3091 uint64_t value) 3092 { 3093 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3094 ARMMMUIdx mmu_idx; 3095 int secure = arm_is_secure_below_el3(env); 3096 3097 switch (ri->opc2 & 6) { 3098 case 0: 3099 switch (ri->opc1) { 3100 case 0: /* AT S1E1R, AT S1E1W */ 3101 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; 3102 break; 3103 case 4: /* AT S1E2R, AT S1E2W */ 3104 mmu_idx = ARMMMUIdx_S1E2; 3105 break; 3106 case 6: /* AT S1E3R, AT S1E3W */ 3107 mmu_idx = ARMMMUIdx_S1E3; 3108 break; 3109 default: 3110 g_assert_not_reached(); 3111 } 3112 break; 3113 case 2: /* AT S1E0R, AT S1E0W */ 3114 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; 3115 break; 3116 case 4: /* AT S12E1R, AT S12E1W */ 3117 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1; 3118 break; 3119 case 6: /* AT S12E0R, AT S12E0W */ 3120 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0; 3121 break; 3122 default: 3123 g_assert_not_reached(); 3124 } 3125 3126 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx); 3127 } 3128 #endif 3129 3130 static const ARMCPRegInfo vapa_cp_reginfo[] = { 3131 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, 3132 .access = PL1_RW, .resetvalue = 0, 3133 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), 3134 offsetoflow32(CPUARMState, cp15.par_ns) }, 3135 .writefn = par_write }, 3136 #ifndef CONFIG_USER_ONLY 3137 /* This underdecoding is safe because the reginfo is NO_RAW. */ 3138 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, 3139 .access = PL1_W, .accessfn = ats_access, 3140 .writefn = ats_write, .type = ARM_CP_NO_RAW }, 3141 #endif 3142 REGINFO_SENTINEL 3143 }; 3144 3145 /* Return basic MPU access permission bits. */ 3146 static uint32_t simple_mpu_ap_bits(uint32_t val) 3147 { 3148 uint32_t ret; 3149 uint32_t mask; 3150 int i; 3151 ret = 0; 3152 mask = 3; 3153 for (i = 0; i < 16; i += 2) { 3154 ret |= (val >> i) & mask; 3155 mask <<= 2; 3156 } 3157 return ret; 3158 } 3159 3160 /* Pad basic MPU access permission bits to extended format. */ 3161 static uint32_t extended_mpu_ap_bits(uint32_t val) 3162 { 3163 uint32_t ret; 3164 uint32_t mask; 3165 int i; 3166 ret = 0; 3167 mask = 3; 3168 for (i = 0; i < 16; i += 2) { 3169 ret |= (val & mask) << i; 3170 mask <<= 2; 3171 } 3172 return ret; 3173 } 3174 3175 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3176 uint64_t value) 3177 { 3178 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); 3179 } 3180 3181 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3182 { 3183 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); 3184 } 3185 3186 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3187 uint64_t value) 3188 { 3189 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); 3190 } 3191 3192 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3193 { 3194 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); 3195 } 3196 3197 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri) 3198 { 3199 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3200 3201 if (!u32p) { 3202 return 0; 3203 } 3204 3205 u32p += env->pmsav7.rnr[M_REG_NS]; 3206 return *u32p; 3207 } 3208 3209 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri, 3210 uint64_t value) 3211 { 3212 ARMCPU *cpu = arm_env_get_cpu(env); 3213 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3214 3215 if (!u32p) { 3216 return; 3217 } 3218 3219 u32p += env->pmsav7.rnr[M_REG_NS]; 3220 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3221 *u32p = value; 3222 } 3223 3224 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3225 uint64_t value) 3226 { 3227 ARMCPU *cpu = arm_env_get_cpu(env); 3228 uint32_t nrgs = cpu->pmsav7_dregion; 3229 3230 if (value >= nrgs) { 3231 qemu_log_mask(LOG_GUEST_ERROR, 3232 "PMSAv7 RGNR write >= # supported regions, %" PRIu32 3233 " > %" PRIu32 "\n", (uint32_t)value, nrgs); 3234 return; 3235 } 3236 3237 raw_write(env, ri, value); 3238 } 3239 3240 static const ARMCPRegInfo pmsav7_cp_reginfo[] = { 3241 /* Reset for all these registers is handled in arm_cpu_reset(), 3242 * because the PMSAv7 is also used by M-profile CPUs, which do 3243 * not register cpregs but still need the state to be reset. 3244 */ 3245 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0, 3246 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3247 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar), 3248 .readfn = pmsav7_read, .writefn = pmsav7_write, 3249 .resetfn = arm_cp_reset_ignore }, 3250 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2, 3251 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3252 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr), 3253 .readfn = pmsav7_read, .writefn = pmsav7_write, 3254 .resetfn = arm_cp_reset_ignore }, 3255 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4, 3256 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3257 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr), 3258 .readfn = pmsav7_read, .writefn = pmsav7_write, 3259 .resetfn = arm_cp_reset_ignore }, 3260 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0, 3261 .access = PL1_RW, 3262 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]), 3263 .writefn = pmsav7_rgnr_write, 3264 .resetfn = arm_cp_reset_ignore }, 3265 REGINFO_SENTINEL 3266 }; 3267 3268 static const ARMCPRegInfo pmsav5_cp_reginfo[] = { 3269 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 3270 .access = PL1_RW, .type = ARM_CP_ALIAS, 3271 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 3272 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, 3273 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 3274 .access = PL1_RW, .type = ARM_CP_ALIAS, 3275 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 3276 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, 3277 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, 3278 .access = PL1_RW, 3279 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 3280 .resetvalue = 0, }, 3281 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, 3282 .access = PL1_RW, 3283 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 3284 .resetvalue = 0, }, 3285 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 3286 .access = PL1_RW, 3287 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, 3288 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, 3289 .access = PL1_RW, 3290 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, 3291 /* Protection region base and size registers */ 3292 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, 3293 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3294 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, 3295 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, 3296 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3297 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, 3298 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, 3299 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3300 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, 3301 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, 3302 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3303 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, 3304 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, 3305 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3306 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, 3307 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, 3308 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3309 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, 3310 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, 3311 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3312 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, 3313 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, 3314 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3315 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, 3316 REGINFO_SENTINEL 3317 }; 3318 3319 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 3320 uint64_t value) 3321 { 3322 TCR *tcr = raw_ptr(env, ri); 3323 int maskshift = extract32(value, 0, 3); 3324 3325 if (!arm_feature(env, ARM_FEATURE_V8)) { 3326 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { 3327 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when 3328 * using Long-desciptor translation table format */ 3329 value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); 3330 } else if (arm_feature(env, ARM_FEATURE_EL3)) { 3331 /* In an implementation that includes the Security Extensions 3332 * TTBCR has additional fields PD0 [4] and PD1 [5] for 3333 * Short-descriptor translation table format. 3334 */ 3335 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; 3336 } else { 3337 value &= TTBCR_N; 3338 } 3339 } 3340 3341 /* Update the masks corresponding to the TCR bank being written 3342 * Note that we always calculate mask and base_mask, but 3343 * they are only used for short-descriptor tables (ie if EAE is 0); 3344 * for long-descriptor tables the TCR fields are used differently 3345 * and the mask and base_mask values are meaningless. 3346 */ 3347 tcr->raw_tcr = value; 3348 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift); 3349 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift); 3350 } 3351 3352 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3353 uint64_t value) 3354 { 3355 ARMCPU *cpu = arm_env_get_cpu(env); 3356 TCR *tcr = raw_ptr(env, ri); 3357 3358 if (arm_feature(env, ARM_FEATURE_LPAE)) { 3359 /* With LPAE the TTBCR could result in a change of ASID 3360 * via the TTBCR.A1 bit, so do a TLB flush. 3361 */ 3362 tlb_flush(CPU(cpu)); 3363 } 3364 /* Preserve the high half of TCR_EL1, set via TTBCR2. */ 3365 value = deposit64(tcr->raw_tcr, 0, 32, value); 3366 vmsa_ttbcr_raw_write(env, ri, value); 3367 } 3368 3369 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3370 { 3371 TCR *tcr = raw_ptr(env, ri); 3372 3373 /* Reset both the TCR as well as the masks corresponding to the bank of 3374 * the TCR being reset. 3375 */ 3376 tcr->raw_tcr = 0; 3377 tcr->mask = 0; 3378 tcr->base_mask = 0xffffc000u; 3379 } 3380 3381 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3382 uint64_t value) 3383 { 3384 ARMCPU *cpu = arm_env_get_cpu(env); 3385 TCR *tcr = raw_ptr(env, ri); 3386 3387 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ 3388 tlb_flush(CPU(cpu)); 3389 tcr->raw_tcr = value; 3390 } 3391 3392 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3393 uint64_t value) 3394 { 3395 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */ 3396 if (cpreg_field_is_64bit(ri) && 3397 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) { 3398 ARMCPU *cpu = arm_env_get_cpu(env); 3399 tlb_flush(CPU(cpu)); 3400 } 3401 raw_write(env, ri, value); 3402 } 3403 3404 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3405 uint64_t value) 3406 { 3407 ARMCPU *cpu = arm_env_get_cpu(env); 3408 CPUState *cs = CPU(cpu); 3409 3410 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */ 3411 if (raw_read(env, ri) != value) { 3412 tlb_flush_by_mmuidx(cs, 3413 ARMMMUIdxBit_S12NSE1 | 3414 ARMMMUIdxBit_S12NSE0 | 3415 ARMMMUIdxBit_S2NS); 3416 raw_write(env, ri, value); 3417 } 3418 } 3419 3420 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = { 3421 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 3422 .access = PL1_RW, .type = ARM_CP_ALIAS, 3423 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), 3424 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, }, 3425 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 3426 .access = PL1_RW, .resetvalue = 0, 3427 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), 3428 offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, 3429 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, 3430 .access = PL1_RW, .resetvalue = 0, 3431 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), 3432 offsetof(CPUARMState, cp15.dfar_ns) } }, 3433 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, 3434 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, 3435 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), 3436 .resetvalue = 0, }, 3437 REGINFO_SENTINEL 3438 }; 3439 3440 static const ARMCPRegInfo vmsa_cp_reginfo[] = { 3441 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, 3442 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, 3443 .access = PL1_RW, 3444 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, 3445 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, 3446 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, 3447 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 3448 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 3449 offsetof(CPUARMState, cp15.ttbr0_ns) } }, 3450 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, 3451 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, 3452 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 3453 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 3454 offsetof(CPUARMState, cp15.ttbr1_ns) } }, 3455 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, 3456 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 3457 .access = PL1_RW, .writefn = vmsa_tcr_el1_write, 3458 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write, 3459 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, 3460 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 3461 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write, 3462 .raw_writefn = vmsa_ttbcr_raw_write, 3463 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), 3464 offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, 3465 REGINFO_SENTINEL 3466 }; 3467 3468 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing 3469 * qemu tlbs nor adjusting cached masks. 3470 */ 3471 static const ARMCPRegInfo ttbcr2_reginfo = { 3472 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3, 3473 .access = PL1_RW, .type = ARM_CP_ALIAS, 3474 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]), 3475 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) }, 3476 }; 3477 3478 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, 3479 uint64_t value) 3480 { 3481 env->cp15.c15_ticonfig = value & 0xe7; 3482 /* The OS_TYPE bit in this register changes the reported CPUID! */ 3483 env->cp15.c0_cpuid = (value & (1 << 5)) ? 3484 ARM_CPUID_TI915T : ARM_CPUID_TI925T; 3485 } 3486 3487 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, 3488 uint64_t value) 3489 { 3490 env->cp15.c15_threadid = value & 0xffff; 3491 } 3492 3493 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, 3494 uint64_t value) 3495 { 3496 /* Wait-for-interrupt (deprecated) */ 3497 cpu_interrupt(CPU(arm_env_get_cpu(env)), CPU_INTERRUPT_HALT); 3498 } 3499 3500 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, 3501 uint64_t value) 3502 { 3503 /* On OMAP there are registers indicating the max/min index of dcache lines 3504 * containing a dirty line; cache flush operations have to reset these. 3505 */ 3506 env->cp15.c15_i_max = 0x000; 3507 env->cp15.c15_i_min = 0xff0; 3508 } 3509 3510 static const ARMCPRegInfo omap_cp_reginfo[] = { 3511 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, 3512 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, 3513 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), 3514 .resetvalue = 0, }, 3515 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, 3516 .access = PL1_RW, .type = ARM_CP_NOP }, 3517 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, 3518 .access = PL1_RW, 3519 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, 3520 .writefn = omap_ticonfig_write }, 3521 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, 3522 .access = PL1_RW, 3523 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, 3524 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, 3525 .access = PL1_RW, .resetvalue = 0xff0, 3526 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, 3527 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, 3528 .access = PL1_RW, 3529 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, 3530 .writefn = omap_threadid_write }, 3531 { .name = "TI925T_STATUS", .cp = 15, .crn = 15, 3532 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 3533 .type = ARM_CP_NO_RAW, 3534 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, 3535 /* TODO: Peripheral port remap register: 3536 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller 3537 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), 3538 * when MMU is off. 3539 */ 3540 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 3541 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 3542 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW, 3543 .writefn = omap_cachemaint_write }, 3544 { .name = "C9", .cp = 15, .crn = 9, 3545 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, 3546 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, 3547 REGINFO_SENTINEL 3548 }; 3549 3550 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3551 uint64_t value) 3552 { 3553 env->cp15.c15_cpar = value & 0x3fff; 3554 } 3555 3556 static const ARMCPRegInfo xscale_cp_reginfo[] = { 3557 { .name = "XSCALE_CPAR", 3558 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 3559 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, 3560 .writefn = xscale_cpar_write, }, 3561 { .name = "XSCALE_AUXCR", 3562 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, 3563 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), 3564 .resetvalue = 0, }, 3565 /* XScale specific cache-lockdown: since we have no cache we NOP these 3566 * and hope the guest does not really rely on cache behaviour. 3567 */ 3568 { .name = "XSCALE_LOCK_ICACHE_LINE", 3569 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, 3570 .access = PL1_W, .type = ARM_CP_NOP }, 3571 { .name = "XSCALE_UNLOCK_ICACHE", 3572 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, 3573 .access = PL1_W, .type = ARM_CP_NOP }, 3574 { .name = "XSCALE_DCACHE_LOCK", 3575 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, 3576 .access = PL1_RW, .type = ARM_CP_NOP }, 3577 { .name = "XSCALE_UNLOCK_DCACHE", 3578 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, 3579 .access = PL1_W, .type = ARM_CP_NOP }, 3580 REGINFO_SENTINEL 3581 }; 3582 3583 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { 3584 /* RAZ/WI the whole crn=15 space, when we don't have a more specific 3585 * implementation of this implementation-defined space. 3586 * Ideally this should eventually disappear in favour of actually 3587 * implementing the correct behaviour for all cores. 3588 */ 3589 { .name = "C15_IMPDEF", .cp = 15, .crn = 15, 3590 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 3591 .access = PL1_RW, 3592 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE, 3593 .resetvalue = 0 }, 3594 REGINFO_SENTINEL 3595 }; 3596 3597 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { 3598 /* Cache status: RAZ because we have no cache so it's always clean */ 3599 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, 3600 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 3601 .resetvalue = 0 }, 3602 REGINFO_SENTINEL 3603 }; 3604 3605 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { 3606 /* We never have a a block transfer operation in progress */ 3607 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, 3608 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 3609 .resetvalue = 0 }, 3610 /* The cache ops themselves: these all NOP for QEMU */ 3611 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, 3612 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3613 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, 3614 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3615 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, 3616 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3617 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, 3618 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3619 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, 3620 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3621 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, 3622 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3623 REGINFO_SENTINEL 3624 }; 3625 3626 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { 3627 /* The cache test-and-clean instructions always return (1 << 30) 3628 * to indicate that there are no dirty cache lines. 3629 */ 3630 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, 3631 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 3632 .resetvalue = (1 << 30) }, 3633 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, 3634 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 3635 .resetvalue = (1 << 30) }, 3636 REGINFO_SENTINEL 3637 }; 3638 3639 static const ARMCPRegInfo strongarm_cp_reginfo[] = { 3640 /* Ignore ReadBuffer accesses */ 3641 { .name = "C9_READBUFFER", .cp = 15, .crn = 9, 3642 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 3643 .access = PL1_RW, .resetvalue = 0, 3644 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW }, 3645 REGINFO_SENTINEL 3646 }; 3647 3648 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3649 { 3650 ARMCPU *cpu = arm_env_get_cpu(env); 3651 unsigned int cur_el = arm_current_el(env); 3652 bool secure = arm_is_secure(env); 3653 3654 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 3655 return env->cp15.vpidr_el2; 3656 } 3657 return raw_read(env, ri); 3658 } 3659 3660 static uint64_t mpidr_read_val(CPUARMState *env) 3661 { 3662 ARMCPU *cpu = ARM_CPU(arm_env_get_cpu(env)); 3663 uint64_t mpidr = cpu->mp_affinity; 3664 3665 if (arm_feature(env, ARM_FEATURE_V7MP)) { 3666 mpidr |= (1U << 31); 3667 /* Cores which are uniprocessor (non-coherent) 3668 * but still implement the MP extensions set 3669 * bit 30. (For instance, Cortex-R5). 3670 */ 3671 if (cpu->mp_is_up) { 3672 mpidr |= (1u << 30); 3673 } 3674 } 3675 return mpidr; 3676 } 3677 3678 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3679 { 3680 unsigned int cur_el = arm_current_el(env); 3681 bool secure = arm_is_secure(env); 3682 3683 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 3684 return env->cp15.vmpidr_el2; 3685 } 3686 return mpidr_read_val(env); 3687 } 3688 3689 static const ARMCPRegInfo lpae_cp_reginfo[] = { 3690 /* NOP AMAIR0/1 */ 3691 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, 3692 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, 3693 .access = PL1_RW, .type = ARM_CP_CONST, 3694 .resetvalue = 0 }, 3695 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ 3696 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, 3697 .access = PL1_RW, .type = ARM_CP_CONST, 3698 .resetvalue = 0 }, 3699 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, 3700 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, 3701 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), 3702 offsetof(CPUARMState, cp15.par_ns)} }, 3703 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, 3704 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3705 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 3706 offsetof(CPUARMState, cp15.ttbr0_ns) }, 3707 .writefn = vmsa_ttbr_write, }, 3708 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, 3709 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3710 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 3711 offsetof(CPUARMState, cp15.ttbr1_ns) }, 3712 .writefn = vmsa_ttbr_write, }, 3713 REGINFO_SENTINEL 3714 }; 3715 3716 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3717 { 3718 return vfp_get_fpcr(env); 3719 } 3720 3721 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3722 uint64_t value) 3723 { 3724 vfp_set_fpcr(env, value); 3725 } 3726 3727 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3728 { 3729 return vfp_get_fpsr(env); 3730 } 3731 3732 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3733 uint64_t value) 3734 { 3735 vfp_set_fpsr(env, value); 3736 } 3737 3738 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri, 3739 bool isread) 3740 { 3741 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) { 3742 return CP_ACCESS_TRAP; 3743 } 3744 return CP_ACCESS_OK; 3745 } 3746 3747 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, 3748 uint64_t value) 3749 { 3750 env->daif = value & PSTATE_DAIF; 3751 } 3752 3753 static CPAccessResult aa64_cacheop_access(CPUARMState *env, 3754 const ARMCPRegInfo *ri, 3755 bool isread) 3756 { 3757 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless 3758 * SCTLR_EL1.UCI is set. 3759 */ 3760 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) { 3761 return CP_ACCESS_TRAP; 3762 } 3763 return CP_ACCESS_OK; 3764 } 3765 3766 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions 3767 * Page D4-1736 (DDI0487A.b) 3768 */ 3769 3770 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3771 uint64_t value) 3772 { 3773 CPUState *cs = ENV_GET_CPU(env); 3774 bool sec = arm_is_secure_below_el3(env); 3775 3776 if (sec) { 3777 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3778 ARMMMUIdxBit_S1SE1 | 3779 ARMMMUIdxBit_S1SE0); 3780 } else { 3781 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3782 ARMMMUIdxBit_S12NSE1 | 3783 ARMMMUIdxBit_S12NSE0); 3784 } 3785 } 3786 3787 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3788 uint64_t value) 3789 { 3790 CPUState *cs = ENV_GET_CPU(env); 3791 3792 if (tlb_force_broadcast(env)) { 3793 tlbi_aa64_vmalle1is_write(env, NULL, value); 3794 return; 3795 } 3796 3797 if (arm_is_secure_below_el3(env)) { 3798 tlb_flush_by_mmuidx(cs, 3799 ARMMMUIdxBit_S1SE1 | 3800 ARMMMUIdxBit_S1SE0); 3801 } else { 3802 tlb_flush_by_mmuidx(cs, 3803 ARMMMUIdxBit_S12NSE1 | 3804 ARMMMUIdxBit_S12NSE0); 3805 } 3806 } 3807 3808 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3809 uint64_t value) 3810 { 3811 /* Note that the 'ALL' scope must invalidate both stage 1 and 3812 * stage 2 translations, whereas most other scopes only invalidate 3813 * stage 1 translations. 3814 */ 3815 ARMCPU *cpu = arm_env_get_cpu(env); 3816 CPUState *cs = CPU(cpu); 3817 3818 if (arm_is_secure_below_el3(env)) { 3819 tlb_flush_by_mmuidx(cs, 3820 ARMMMUIdxBit_S1SE1 | 3821 ARMMMUIdxBit_S1SE0); 3822 } else { 3823 if (arm_feature(env, ARM_FEATURE_EL2)) { 3824 tlb_flush_by_mmuidx(cs, 3825 ARMMMUIdxBit_S12NSE1 | 3826 ARMMMUIdxBit_S12NSE0 | 3827 ARMMMUIdxBit_S2NS); 3828 } else { 3829 tlb_flush_by_mmuidx(cs, 3830 ARMMMUIdxBit_S12NSE1 | 3831 ARMMMUIdxBit_S12NSE0); 3832 } 3833 } 3834 } 3835 3836 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3837 uint64_t value) 3838 { 3839 ARMCPU *cpu = arm_env_get_cpu(env); 3840 CPUState *cs = CPU(cpu); 3841 3842 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2); 3843 } 3844 3845 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri, 3846 uint64_t value) 3847 { 3848 ARMCPU *cpu = arm_env_get_cpu(env); 3849 CPUState *cs = CPU(cpu); 3850 3851 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3); 3852 } 3853 3854 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3855 uint64_t value) 3856 { 3857 /* Note that the 'ALL' scope must invalidate both stage 1 and 3858 * stage 2 translations, whereas most other scopes only invalidate 3859 * stage 1 translations. 3860 */ 3861 CPUState *cs = ENV_GET_CPU(env); 3862 bool sec = arm_is_secure_below_el3(env); 3863 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2); 3864 3865 if (sec) { 3866 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3867 ARMMMUIdxBit_S1SE1 | 3868 ARMMMUIdxBit_S1SE0); 3869 } else if (has_el2) { 3870 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3871 ARMMMUIdxBit_S12NSE1 | 3872 ARMMMUIdxBit_S12NSE0 | 3873 ARMMMUIdxBit_S2NS); 3874 } else { 3875 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3876 ARMMMUIdxBit_S12NSE1 | 3877 ARMMMUIdxBit_S12NSE0); 3878 } 3879 } 3880 3881 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3882 uint64_t value) 3883 { 3884 CPUState *cs = ENV_GET_CPU(env); 3885 3886 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2); 3887 } 3888 3889 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3890 uint64_t value) 3891 { 3892 CPUState *cs = ENV_GET_CPU(env); 3893 3894 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3); 3895 } 3896 3897 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3898 uint64_t value) 3899 { 3900 /* Invalidate by VA, EL2 3901 * Currently handles both VAE2 and VALE2, since we don't support 3902 * flush-last-level-only. 3903 */ 3904 ARMCPU *cpu = arm_env_get_cpu(env); 3905 CPUState *cs = CPU(cpu); 3906 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3907 3908 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2); 3909 } 3910 3911 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri, 3912 uint64_t value) 3913 { 3914 /* Invalidate by VA, EL3 3915 * Currently handles both VAE3 and VALE3, since we don't support 3916 * flush-last-level-only. 3917 */ 3918 ARMCPU *cpu = arm_env_get_cpu(env); 3919 CPUState *cs = CPU(cpu); 3920 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3921 3922 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3); 3923 } 3924 3925 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3926 uint64_t value) 3927 { 3928 ARMCPU *cpu = arm_env_get_cpu(env); 3929 CPUState *cs = CPU(cpu); 3930 bool sec = arm_is_secure_below_el3(env); 3931 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3932 3933 if (sec) { 3934 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3935 ARMMMUIdxBit_S1SE1 | 3936 ARMMMUIdxBit_S1SE0); 3937 } else { 3938 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3939 ARMMMUIdxBit_S12NSE1 | 3940 ARMMMUIdxBit_S12NSE0); 3941 } 3942 } 3943 3944 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3945 uint64_t value) 3946 { 3947 /* Invalidate by VA, EL1&0 (AArch64 version). 3948 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1, 3949 * since we don't support flush-for-specific-ASID-only or 3950 * flush-last-level-only. 3951 */ 3952 ARMCPU *cpu = arm_env_get_cpu(env); 3953 CPUState *cs = CPU(cpu); 3954 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3955 3956 if (tlb_force_broadcast(env)) { 3957 tlbi_aa64_vae1is_write(env, NULL, value); 3958 return; 3959 } 3960 3961 if (arm_is_secure_below_el3(env)) { 3962 tlb_flush_page_by_mmuidx(cs, pageaddr, 3963 ARMMMUIdxBit_S1SE1 | 3964 ARMMMUIdxBit_S1SE0); 3965 } else { 3966 tlb_flush_page_by_mmuidx(cs, pageaddr, 3967 ARMMMUIdxBit_S12NSE1 | 3968 ARMMMUIdxBit_S12NSE0); 3969 } 3970 } 3971 3972 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3973 uint64_t value) 3974 { 3975 CPUState *cs = ENV_GET_CPU(env); 3976 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3977 3978 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3979 ARMMMUIdxBit_S1E2); 3980 } 3981 3982 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3983 uint64_t value) 3984 { 3985 CPUState *cs = ENV_GET_CPU(env); 3986 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3987 3988 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3989 ARMMMUIdxBit_S1E3); 3990 } 3991 3992 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3993 uint64_t value) 3994 { 3995 /* Invalidate by IPA. This has to invalidate any structures that 3996 * contain only stage 2 translation information, but does not need 3997 * to apply to structures that contain combined stage 1 and stage 2 3998 * translation information. 3999 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. 4000 */ 4001 ARMCPU *cpu = arm_env_get_cpu(env); 4002 CPUState *cs = CPU(cpu); 4003 uint64_t pageaddr; 4004 4005 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 4006 return; 4007 } 4008 4009 pageaddr = sextract64(value << 12, 0, 48); 4010 4011 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS); 4012 } 4013 4014 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4015 uint64_t value) 4016 { 4017 CPUState *cs = ENV_GET_CPU(env); 4018 uint64_t pageaddr; 4019 4020 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 4021 return; 4022 } 4023 4024 pageaddr = sextract64(value << 12, 0, 48); 4025 4026 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 4027 ARMMMUIdxBit_S2NS); 4028 } 4029 4030 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri, 4031 bool isread) 4032 { 4033 /* We don't implement EL2, so the only control on DC ZVA is the 4034 * bit in the SCTLR which can prohibit access for EL0. 4035 */ 4036 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) { 4037 return CP_ACCESS_TRAP; 4038 } 4039 return CP_ACCESS_OK; 4040 } 4041 4042 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) 4043 { 4044 ARMCPU *cpu = arm_env_get_cpu(env); 4045 int dzp_bit = 1 << 4; 4046 4047 /* DZP indicates whether DC ZVA access is allowed */ 4048 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) { 4049 dzp_bit = 0; 4050 } 4051 return cpu->dcz_blocksize | dzp_bit; 4052 } 4053 4054 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 4055 bool isread) 4056 { 4057 if (!(env->pstate & PSTATE_SP)) { 4058 /* Access to SP_EL0 is undefined if it's being used as 4059 * the stack pointer. 4060 */ 4061 return CP_ACCESS_TRAP_UNCATEGORIZED; 4062 } 4063 return CP_ACCESS_OK; 4064 } 4065 4066 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) 4067 { 4068 return env->pstate & PSTATE_SP; 4069 } 4070 4071 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 4072 { 4073 update_spsel(env, val); 4074 } 4075 4076 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4077 uint64_t value) 4078 { 4079 ARMCPU *cpu = arm_env_get_cpu(env); 4080 4081 if (raw_read(env, ri) == value) { 4082 /* Skip the TLB flush if nothing actually changed; Linux likes 4083 * to do a lot of pointless SCTLR writes. 4084 */ 4085 return; 4086 } 4087 4088 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) { 4089 /* M bit is RAZ/WI for PMSA with no MPU implemented */ 4090 value &= ~SCTLR_M; 4091 } 4092 4093 raw_write(env, ri, value); 4094 /* ??? Lots of these bits are not implemented. */ 4095 /* This may enable/disable the MMU, so do a TLB flush. */ 4096 tlb_flush(CPU(cpu)); 4097 } 4098 4099 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri, 4100 bool isread) 4101 { 4102 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) { 4103 return CP_ACCESS_TRAP_FP_EL2; 4104 } 4105 if (env->cp15.cptr_el[3] & CPTR_TFP) { 4106 return CP_ACCESS_TRAP_FP_EL3; 4107 } 4108 return CP_ACCESS_OK; 4109 } 4110 4111 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4112 uint64_t value) 4113 { 4114 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK; 4115 } 4116 4117 static const ARMCPRegInfo v8_cp_reginfo[] = { 4118 /* Minimal set of EL0-visible registers. This will need to be expanded 4119 * significantly for system emulation of AArch64 CPUs. 4120 */ 4121 { .name = "NZCV", .state = ARM_CP_STATE_AA64, 4122 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, 4123 .access = PL0_RW, .type = ARM_CP_NZCV }, 4124 { .name = "DAIF", .state = ARM_CP_STATE_AA64, 4125 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, 4126 .type = ARM_CP_NO_RAW, 4127 .access = PL0_RW, .accessfn = aa64_daif_access, 4128 .fieldoffset = offsetof(CPUARMState, daif), 4129 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, 4130 { .name = "FPCR", .state = ARM_CP_STATE_AA64, 4131 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, 4132 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 4133 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, 4134 { .name = "FPSR", .state = ARM_CP_STATE_AA64, 4135 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, 4136 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 4137 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, 4138 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, 4139 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, 4140 .access = PL0_R, .type = ARM_CP_NO_RAW, 4141 .readfn = aa64_dczid_read }, 4142 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, 4143 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, 4144 .access = PL0_W, .type = ARM_CP_DC_ZVA, 4145 #ifndef CONFIG_USER_ONLY 4146 /* Avoid overhead of an access check that always passes in user-mode */ 4147 .accessfn = aa64_zva_access, 4148 #endif 4149 }, 4150 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, 4151 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, 4152 .access = PL1_R, .type = ARM_CP_CURRENTEL }, 4153 /* Cache ops: all NOPs since we don't emulate caches */ 4154 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, 4155 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 4156 .access = PL1_W, .type = ARM_CP_NOP }, 4157 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, 4158 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 4159 .access = PL1_W, .type = ARM_CP_NOP }, 4160 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, 4161 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, 4162 .access = PL0_W, .type = ARM_CP_NOP, 4163 .accessfn = aa64_cacheop_access }, 4164 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, 4165 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 4166 .access = PL1_W, .type = ARM_CP_NOP }, 4167 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, 4168 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 4169 .access = PL1_W, .type = ARM_CP_NOP }, 4170 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, 4171 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, 4172 .access = PL0_W, .type = ARM_CP_NOP, 4173 .accessfn = aa64_cacheop_access }, 4174 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, 4175 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 4176 .access = PL1_W, .type = ARM_CP_NOP }, 4177 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, 4178 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, 4179 .access = PL0_W, .type = ARM_CP_NOP, 4180 .accessfn = aa64_cacheop_access }, 4181 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, 4182 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, 4183 .access = PL0_W, .type = ARM_CP_NOP, 4184 .accessfn = aa64_cacheop_access }, 4185 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, 4186 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 4187 .access = PL1_W, .type = ARM_CP_NOP }, 4188 /* TLBI operations */ 4189 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64, 4190 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 4191 .access = PL1_W, .type = ARM_CP_NO_RAW, 4192 .writefn = tlbi_aa64_vmalle1is_write }, 4193 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64, 4194 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 4195 .access = PL1_W, .type = ARM_CP_NO_RAW, 4196 .writefn = tlbi_aa64_vae1is_write }, 4197 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64, 4198 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 4199 .access = PL1_W, .type = ARM_CP_NO_RAW, 4200 .writefn = tlbi_aa64_vmalle1is_write }, 4201 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64, 4202 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 4203 .access = PL1_W, .type = ARM_CP_NO_RAW, 4204 .writefn = tlbi_aa64_vae1is_write }, 4205 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64, 4206 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 4207 .access = PL1_W, .type = ARM_CP_NO_RAW, 4208 .writefn = tlbi_aa64_vae1is_write }, 4209 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64, 4210 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 4211 .access = PL1_W, .type = ARM_CP_NO_RAW, 4212 .writefn = tlbi_aa64_vae1is_write }, 4213 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64, 4214 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 4215 .access = PL1_W, .type = ARM_CP_NO_RAW, 4216 .writefn = tlbi_aa64_vmalle1_write }, 4217 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64, 4218 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 4219 .access = PL1_W, .type = ARM_CP_NO_RAW, 4220 .writefn = tlbi_aa64_vae1_write }, 4221 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64, 4222 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 4223 .access = PL1_W, .type = ARM_CP_NO_RAW, 4224 .writefn = tlbi_aa64_vmalle1_write }, 4225 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64, 4226 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 4227 .access = PL1_W, .type = ARM_CP_NO_RAW, 4228 .writefn = tlbi_aa64_vae1_write }, 4229 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64, 4230 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 4231 .access = PL1_W, .type = ARM_CP_NO_RAW, 4232 .writefn = tlbi_aa64_vae1_write }, 4233 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64, 4234 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 4235 .access = PL1_W, .type = ARM_CP_NO_RAW, 4236 .writefn = tlbi_aa64_vae1_write }, 4237 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64, 4238 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 4239 .access = PL2_W, .type = ARM_CP_NO_RAW, 4240 .writefn = tlbi_aa64_ipas2e1is_write }, 4241 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64, 4242 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 4243 .access = PL2_W, .type = ARM_CP_NO_RAW, 4244 .writefn = tlbi_aa64_ipas2e1is_write }, 4245 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64, 4246 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 4247 .access = PL2_W, .type = ARM_CP_NO_RAW, 4248 .writefn = tlbi_aa64_alle1is_write }, 4249 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64, 4250 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6, 4251 .access = PL2_W, .type = ARM_CP_NO_RAW, 4252 .writefn = tlbi_aa64_alle1is_write }, 4253 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64, 4254 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 4255 .access = PL2_W, .type = ARM_CP_NO_RAW, 4256 .writefn = tlbi_aa64_ipas2e1_write }, 4257 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64, 4258 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 4259 .access = PL2_W, .type = ARM_CP_NO_RAW, 4260 .writefn = tlbi_aa64_ipas2e1_write }, 4261 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64, 4262 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 4263 .access = PL2_W, .type = ARM_CP_NO_RAW, 4264 .writefn = tlbi_aa64_alle1_write }, 4265 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64, 4266 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6, 4267 .access = PL2_W, .type = ARM_CP_NO_RAW, 4268 .writefn = tlbi_aa64_alle1is_write }, 4269 #ifndef CONFIG_USER_ONLY 4270 /* 64 bit address translation operations */ 4271 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, 4272 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, 4273 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4274 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, 4275 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, 4276 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4277 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, 4278 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, 4279 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4280 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, 4281 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, 4282 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4283 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64, 4284 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4, 4285 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4286 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64, 4287 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5, 4288 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4289 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64, 4290 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6, 4291 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4292 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64, 4293 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7, 4294 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4295 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */ 4296 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64, 4297 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0, 4298 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4299 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64, 4300 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1, 4301 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4302 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64, 4303 .type = ARM_CP_ALIAS, 4304 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0, 4305 .access = PL1_RW, .resetvalue = 0, 4306 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]), 4307 .writefn = par_write }, 4308 #endif 4309 /* TLB invalidate last level of translation table walk */ 4310 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 4311 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, 4312 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 4313 .type = ARM_CP_NO_RAW, .access = PL1_W, 4314 .writefn = tlbimvaa_is_write }, 4315 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 4316 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 4317 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 4318 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, 4319 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 4320 .type = ARM_CP_NO_RAW, .access = PL2_W, 4321 .writefn = tlbimva_hyp_write }, 4322 { .name = "TLBIMVALHIS", 4323 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 4324 .type = ARM_CP_NO_RAW, .access = PL2_W, 4325 .writefn = tlbimva_hyp_is_write }, 4326 { .name = "TLBIIPAS2", 4327 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 4328 .type = ARM_CP_NO_RAW, .access = PL2_W, 4329 .writefn = tlbiipas2_write }, 4330 { .name = "TLBIIPAS2IS", 4331 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 4332 .type = ARM_CP_NO_RAW, .access = PL2_W, 4333 .writefn = tlbiipas2_is_write }, 4334 { .name = "TLBIIPAS2L", 4335 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 4336 .type = ARM_CP_NO_RAW, .access = PL2_W, 4337 .writefn = tlbiipas2_write }, 4338 { .name = "TLBIIPAS2LIS", 4339 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 4340 .type = ARM_CP_NO_RAW, .access = PL2_W, 4341 .writefn = tlbiipas2_is_write }, 4342 /* 32 bit cache operations */ 4343 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 4344 .type = ARM_CP_NOP, .access = PL1_W }, 4345 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, 4346 .type = ARM_CP_NOP, .access = PL1_W }, 4347 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 4348 .type = ARM_CP_NOP, .access = PL1_W }, 4349 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, 4350 .type = ARM_CP_NOP, .access = PL1_W }, 4351 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, 4352 .type = ARM_CP_NOP, .access = PL1_W }, 4353 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, 4354 .type = ARM_CP_NOP, .access = PL1_W }, 4355 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 4356 .type = ARM_CP_NOP, .access = PL1_W }, 4357 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 4358 .type = ARM_CP_NOP, .access = PL1_W }, 4359 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, 4360 .type = ARM_CP_NOP, .access = PL1_W }, 4361 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 4362 .type = ARM_CP_NOP, .access = PL1_W }, 4363 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, 4364 .type = ARM_CP_NOP, .access = PL1_W }, 4365 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, 4366 .type = ARM_CP_NOP, .access = PL1_W }, 4367 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 4368 .type = ARM_CP_NOP, .access = PL1_W }, 4369 /* MMU Domain access control / MPU write buffer control */ 4370 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, 4371 .access = PL1_RW, .resetvalue = 0, 4372 .writefn = dacr_write, .raw_writefn = raw_write, 4373 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 4374 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 4375 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, 4376 .type = ARM_CP_ALIAS, 4377 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, 4378 .access = PL1_RW, 4379 .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, 4380 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, 4381 .type = ARM_CP_ALIAS, 4382 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, 4383 .access = PL1_RW, 4384 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) }, 4385 /* We rely on the access checks not allowing the guest to write to the 4386 * state field when SPSel indicates that it's being used as the stack 4387 * pointer. 4388 */ 4389 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, 4390 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, 4391 .access = PL1_RW, .accessfn = sp_el0_access, 4392 .type = ARM_CP_ALIAS, 4393 .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, 4394 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64, 4395 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0, 4396 .access = PL2_RW, .type = ARM_CP_ALIAS, 4397 .fieldoffset = offsetof(CPUARMState, sp_el[1]) }, 4398 { .name = "SPSel", .state = ARM_CP_STATE_AA64, 4399 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, 4400 .type = ARM_CP_NO_RAW, 4401 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, 4402 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64, 4403 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0, 4404 .type = ARM_CP_ALIAS, 4405 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]), 4406 .access = PL2_RW, .accessfn = fpexc32_access }, 4407 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, 4408 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, 4409 .access = PL2_RW, .resetvalue = 0, 4410 .writefn = dacr_write, .raw_writefn = raw_write, 4411 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, 4412 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, 4413 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, 4414 .access = PL2_RW, .resetvalue = 0, 4415 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, 4416 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64, 4417 .type = ARM_CP_ALIAS, 4418 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0, 4419 .access = PL2_RW, 4420 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) }, 4421 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64, 4422 .type = ARM_CP_ALIAS, 4423 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1, 4424 .access = PL2_RW, 4425 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) }, 4426 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64, 4427 .type = ARM_CP_ALIAS, 4428 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2, 4429 .access = PL2_RW, 4430 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) }, 4431 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64, 4432 .type = ARM_CP_ALIAS, 4433 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3, 4434 .access = PL2_RW, 4435 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) }, 4436 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64, 4437 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1, 4438 .resetvalue = 0, 4439 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) }, 4440 { .name = "SDCR", .type = ARM_CP_ALIAS, 4441 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1, 4442 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 4443 .writefn = sdcr_write, 4444 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) }, 4445 REGINFO_SENTINEL 4446 }; 4447 4448 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */ 4449 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = { 4450 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 4451 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 4452 .access = PL2_RW, 4453 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, 4454 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH, 4455 .type = ARM_CP_NO_RAW, 4456 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 4457 .access = PL2_RW, 4458 .type = ARM_CP_CONST, .resetvalue = 0 }, 4459 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH, 4460 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7, 4461 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4462 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 4463 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 4464 .access = PL2_RW, 4465 .type = ARM_CP_CONST, .resetvalue = 0 }, 4466 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 4467 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 4468 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4469 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 4470 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 4471 .access = PL2_RW, .type = ARM_CP_CONST, 4472 .resetvalue = 0 }, 4473 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 4474 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 4475 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4476 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 4477 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 4478 .access = PL2_RW, .type = ARM_CP_CONST, 4479 .resetvalue = 0 }, 4480 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 4481 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 4482 .access = PL2_RW, .type = ARM_CP_CONST, 4483 .resetvalue = 0 }, 4484 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 4485 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 4486 .access = PL2_RW, .type = ARM_CP_CONST, 4487 .resetvalue = 0 }, 4488 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 4489 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 4490 .access = PL2_RW, .type = ARM_CP_CONST, 4491 .resetvalue = 0 }, 4492 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 4493 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 4494 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4495 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH, 4496 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 4497 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 4498 .type = ARM_CP_CONST, .resetvalue = 0 }, 4499 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 4500 .cp = 15, .opc1 = 6, .crm = 2, 4501 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4502 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 4503 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 4504 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 4505 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4506 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 4507 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 4508 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4509 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 4510 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 4511 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4512 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 4513 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 4514 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4515 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 4516 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 4517 .resetvalue = 0 }, 4518 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 4519 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 4520 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4521 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 4522 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 4523 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4524 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 4525 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 4526 .resetvalue = 0 }, 4527 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 4528 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 4529 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4530 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 4531 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 4532 .resetvalue = 0 }, 4533 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 4534 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 4535 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4536 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 4537 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 4538 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4539 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 4540 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 4541 .access = PL2_RW, .accessfn = access_tda, 4542 .type = ARM_CP_CONST, .resetvalue = 0 }, 4543 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH, 4544 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4545 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 4546 .type = ARM_CP_CONST, .resetvalue = 0 }, 4547 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 4548 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 4549 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4550 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 4551 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 4552 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4553 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 4554 .type = ARM_CP_CONST, 4555 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 4556 .access = PL2_RW, .resetvalue = 0 }, 4557 REGINFO_SENTINEL 4558 }; 4559 4560 /* Ditto, but for registers which exist in ARMv8 but not v7 */ 4561 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = { 4562 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 4563 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 4564 .access = PL2_RW, 4565 .type = ARM_CP_CONST, .resetvalue = 0 }, 4566 REGINFO_SENTINEL 4567 }; 4568 4569 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 4570 { 4571 ARMCPU *cpu = arm_env_get_cpu(env); 4572 uint64_t valid_mask = HCR_MASK; 4573 4574 if (arm_feature(env, ARM_FEATURE_EL3)) { 4575 valid_mask &= ~HCR_HCD; 4576 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) { 4577 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented. 4578 * However, if we're using the SMC PSCI conduit then QEMU is 4579 * effectively acting like EL3 firmware and so the guest at 4580 * EL2 should retain the ability to prevent EL1 from being 4581 * able to make SMC calls into the ersatz firmware, so in 4582 * that case HCR.TSC should be read/write. 4583 */ 4584 valid_mask &= ~HCR_TSC; 4585 } 4586 if (cpu_isar_feature(aa64_lor, cpu)) { 4587 valid_mask |= HCR_TLOR; 4588 } 4589 if (cpu_isar_feature(aa64_pauth, cpu)) { 4590 valid_mask |= HCR_API | HCR_APK; 4591 } 4592 4593 /* Clear RES0 bits. */ 4594 value &= valid_mask; 4595 4596 /* These bits change the MMU setup: 4597 * HCR_VM enables stage 2 translation 4598 * HCR_PTW forbids certain page-table setups 4599 * HCR_DC Disables stage1 and enables stage2 translation 4600 */ 4601 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) { 4602 tlb_flush(CPU(cpu)); 4603 } 4604 env->cp15.hcr_el2 = value; 4605 4606 /* 4607 * Updates to VI and VF require us to update the status of 4608 * virtual interrupts, which are the logical OR of these bits 4609 * and the state of the input lines from the GIC. (This requires 4610 * that we have the iothread lock, which is done by marking the 4611 * reginfo structs as ARM_CP_IO.) 4612 * Note that if a write to HCR pends a VIRQ or VFIQ it is never 4613 * possible for it to be taken immediately, because VIRQ and 4614 * VFIQ are masked unless running at EL0 or EL1, and HCR 4615 * can only be written at EL2. 4616 */ 4617 g_assert(qemu_mutex_iothread_locked()); 4618 arm_cpu_update_virq(cpu); 4619 arm_cpu_update_vfiq(cpu); 4620 } 4621 4622 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri, 4623 uint64_t value) 4624 { 4625 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */ 4626 value = deposit64(env->cp15.hcr_el2, 32, 32, value); 4627 hcr_write(env, NULL, value); 4628 } 4629 4630 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri, 4631 uint64_t value) 4632 { 4633 /* Handle HCR write, i.e. write to low half of HCR_EL2 */ 4634 value = deposit64(env->cp15.hcr_el2, 0, 32, value); 4635 hcr_write(env, NULL, value); 4636 } 4637 4638 /* 4639 * Return the effective value of HCR_EL2. 4640 * Bits that are not included here: 4641 * RW (read from SCR_EL3.RW as needed) 4642 */ 4643 uint64_t arm_hcr_el2_eff(CPUARMState *env) 4644 { 4645 uint64_t ret = env->cp15.hcr_el2; 4646 4647 if (arm_is_secure_below_el3(env)) { 4648 /* 4649 * "This register has no effect if EL2 is not enabled in the 4650 * current Security state". This is ARMv8.4-SecEL2 speak for 4651 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1). 4652 * 4653 * Prior to that, the language was "In an implementation that 4654 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves 4655 * as if this field is 0 for all purposes other than a direct 4656 * read or write access of HCR_EL2". With lots of enumeration 4657 * on a per-field basis. In current QEMU, this is condition 4658 * is arm_is_secure_below_el3. 4659 * 4660 * Since the v8.4 language applies to the entire register, and 4661 * appears to be backward compatible, use that. 4662 */ 4663 ret = 0; 4664 } else if (ret & HCR_TGE) { 4665 /* These bits are up-to-date as of ARMv8.4. */ 4666 if (ret & HCR_E2H) { 4667 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO | 4668 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE | 4669 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU | 4670 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE); 4671 } else { 4672 ret |= HCR_FMO | HCR_IMO | HCR_AMO; 4673 } 4674 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE | 4675 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR | 4676 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM | 4677 HCR_TLOR); 4678 } 4679 4680 return ret; 4681 } 4682 4683 static const ARMCPRegInfo el2_cp_reginfo[] = { 4684 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, 4685 .type = ARM_CP_IO, 4686 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 4687 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 4688 .writefn = hcr_write }, 4689 { .name = "HCR", .state = ARM_CP_STATE_AA32, 4690 .type = ARM_CP_ALIAS | ARM_CP_IO, 4691 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 4692 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 4693 .writefn = hcr_writelow }, 4694 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH, 4695 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7, 4696 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4697 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, 4698 .type = ARM_CP_ALIAS, 4699 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, 4700 .access = PL2_RW, 4701 .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, 4702 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 4703 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 4704 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, 4705 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 4706 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 4707 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, 4708 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 4709 .type = ARM_CP_ALIAS, 4710 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 4711 .access = PL2_RW, 4712 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) }, 4713 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, 4714 .type = ARM_CP_ALIAS, 4715 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, 4716 .access = PL2_RW, 4717 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) }, 4718 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 4719 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 4720 .access = PL2_RW, .writefn = vbar_write, 4721 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), 4722 .resetvalue = 0 }, 4723 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64, 4724 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0, 4725 .access = PL3_RW, .type = ARM_CP_ALIAS, 4726 .fieldoffset = offsetof(CPUARMState, sp_el[2]) }, 4727 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 4728 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 4729 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0, 4730 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]) }, 4731 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 4732 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 4733 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]), 4734 .resetvalue = 0 }, 4735 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 4736 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 4737 .access = PL2_RW, .type = ARM_CP_ALIAS, 4738 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) }, 4739 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 4740 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 4741 .access = PL2_RW, .type = ARM_CP_CONST, 4742 .resetvalue = 0 }, 4743 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */ 4744 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 4745 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 4746 .access = PL2_RW, .type = ARM_CP_CONST, 4747 .resetvalue = 0 }, 4748 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 4749 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 4750 .access = PL2_RW, .type = ARM_CP_CONST, 4751 .resetvalue = 0 }, 4752 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 4753 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 4754 .access = PL2_RW, .type = ARM_CP_CONST, 4755 .resetvalue = 0 }, 4756 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 4757 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 4758 .access = PL2_RW, 4759 /* no .writefn needed as this can't cause an ASID change; 4760 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 4761 */ 4762 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) }, 4763 { .name = "VTCR", .state = ARM_CP_STATE_AA32, 4764 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 4765 .type = ARM_CP_ALIAS, 4766 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4767 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 4768 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64, 4769 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 4770 .access = PL2_RW, 4771 /* no .writefn needed as this can't cause an ASID change; 4772 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 4773 */ 4774 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 4775 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 4776 .cp = 15, .opc1 = 6, .crm = 2, 4777 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4778 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4779 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2), 4780 .writefn = vttbr_write }, 4781 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 4782 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 4783 .access = PL2_RW, .writefn = vttbr_write, 4784 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) }, 4785 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 4786 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 4787 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write, 4788 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) }, 4789 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 4790 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 4791 .access = PL2_RW, .resetvalue = 0, 4792 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) }, 4793 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 4794 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 4795 .access = PL2_RW, .resetvalue = 0, 4796 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 4797 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 4798 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4799 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 4800 { .name = "TLBIALLNSNH", 4801 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 4802 .type = ARM_CP_NO_RAW, .access = PL2_W, 4803 .writefn = tlbiall_nsnh_write }, 4804 { .name = "TLBIALLNSNHIS", 4805 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 4806 .type = ARM_CP_NO_RAW, .access = PL2_W, 4807 .writefn = tlbiall_nsnh_is_write }, 4808 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 4809 .type = ARM_CP_NO_RAW, .access = PL2_W, 4810 .writefn = tlbiall_hyp_write }, 4811 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 4812 .type = ARM_CP_NO_RAW, .access = PL2_W, 4813 .writefn = tlbiall_hyp_is_write }, 4814 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 4815 .type = ARM_CP_NO_RAW, .access = PL2_W, 4816 .writefn = tlbimva_hyp_write }, 4817 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 4818 .type = ARM_CP_NO_RAW, .access = PL2_W, 4819 .writefn = tlbimva_hyp_is_write }, 4820 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64, 4821 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 4822 .type = ARM_CP_NO_RAW, .access = PL2_W, 4823 .writefn = tlbi_aa64_alle2_write }, 4824 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64, 4825 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 4826 .type = ARM_CP_NO_RAW, .access = PL2_W, 4827 .writefn = tlbi_aa64_vae2_write }, 4828 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64, 4829 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 4830 .access = PL2_W, .type = ARM_CP_NO_RAW, 4831 .writefn = tlbi_aa64_vae2_write }, 4832 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64, 4833 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 4834 .access = PL2_W, .type = ARM_CP_NO_RAW, 4835 .writefn = tlbi_aa64_alle2is_write }, 4836 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64, 4837 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 4838 .type = ARM_CP_NO_RAW, .access = PL2_W, 4839 .writefn = tlbi_aa64_vae2is_write }, 4840 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64, 4841 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 4842 .access = PL2_W, .type = ARM_CP_NO_RAW, 4843 .writefn = tlbi_aa64_vae2is_write }, 4844 #ifndef CONFIG_USER_ONLY 4845 /* Unlike the other EL2-related AT operations, these must 4846 * UNDEF from EL3 if EL2 is not implemented, which is why we 4847 * define them here rather than with the rest of the AT ops. 4848 */ 4849 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64, 4850 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 4851 .access = PL2_W, .accessfn = at_s1e2_access, 4852 .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4853 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64, 4854 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 4855 .access = PL2_W, .accessfn = at_s1e2_access, 4856 .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4857 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE 4858 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3 4859 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose 4860 * to behave as if SCR.NS was 1. 4861 */ 4862 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 4863 .access = PL2_W, 4864 .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, 4865 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 4866 .access = PL2_W, 4867 .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, 4868 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 4869 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 4870 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the 4871 * reset values as IMPDEF. We choose to reset to 3 to comply with 4872 * both ARMv7 and ARMv8. 4873 */ 4874 .access = PL2_RW, .resetvalue = 3, 4875 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) }, 4876 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 4877 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 4878 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, 4879 .writefn = gt_cntvoff_write, 4880 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 4881 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 4882 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO, 4883 .writefn = gt_cntvoff_write, 4884 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 4885 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 4886 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 4887 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 4888 .type = ARM_CP_IO, .access = PL2_RW, 4889 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 4890 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 4891 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 4892 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO, 4893 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 4894 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 4895 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 4896 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 4897 .resetfn = gt_hyp_timer_reset, 4898 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write }, 4899 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 4900 .type = ARM_CP_IO, 4901 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 4902 .access = PL2_RW, 4903 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl), 4904 .resetvalue = 0, 4905 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write }, 4906 #endif 4907 /* The only field of MDCR_EL2 that has a defined architectural reset value 4908 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we 4909 * don't implement any PMU event counters, so using zero as a reset 4910 * value for MDCR_EL2 is okay 4911 */ 4912 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 4913 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 4914 .access = PL2_RW, .resetvalue = 0, 4915 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), }, 4916 { .name = "HPFAR", .state = ARM_CP_STATE_AA32, 4917 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4918 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4919 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 4920 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64, 4921 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4922 .access = PL2_RW, 4923 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 4924 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 4925 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 4926 .access = PL2_RW, 4927 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) }, 4928 REGINFO_SENTINEL 4929 }; 4930 4931 static const ARMCPRegInfo el2_v8_cp_reginfo[] = { 4932 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 4933 .type = ARM_CP_ALIAS | ARM_CP_IO, 4934 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 4935 .access = PL2_RW, 4936 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2), 4937 .writefn = hcr_writehigh }, 4938 REGINFO_SENTINEL 4939 }; 4940 4941 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 4942 bool isread) 4943 { 4944 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2. 4945 * At Secure EL1 it traps to EL3. 4946 */ 4947 if (arm_current_el(env) == 3) { 4948 return CP_ACCESS_OK; 4949 } 4950 if (arm_is_secure_below_el3(env)) { 4951 return CP_ACCESS_TRAP_EL3; 4952 } 4953 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */ 4954 if (isread) { 4955 return CP_ACCESS_OK; 4956 } 4957 return CP_ACCESS_TRAP_UNCATEGORIZED; 4958 } 4959 4960 static const ARMCPRegInfo el3_cp_reginfo[] = { 4961 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, 4962 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, 4963 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), 4964 .resetvalue = 0, .writefn = scr_write }, 4965 { .name = "SCR", .type = ARM_CP_ALIAS, 4966 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, 4967 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 4968 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), 4969 .writefn = scr_write }, 4970 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, 4971 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, 4972 .access = PL3_RW, .resetvalue = 0, 4973 .fieldoffset = offsetof(CPUARMState, cp15.sder) }, 4974 { .name = "SDER", 4975 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, 4976 .access = PL3_RW, .resetvalue = 0, 4977 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, 4978 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 4979 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 4980 .writefn = vbar_write, .resetvalue = 0, 4981 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, 4982 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, 4983 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, 4984 .access = PL3_RW, .resetvalue = 0, 4985 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, 4986 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, 4987 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, 4988 .access = PL3_RW, 4989 /* no .writefn needed as this can't cause an ASID change; 4990 * we must provide a .raw_writefn and .resetfn because we handle 4991 * reset and migration for the AArch32 TTBCR(S), which might be 4992 * using mask and base_mask. 4993 */ 4994 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write, 4995 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, 4996 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, 4997 .type = ARM_CP_ALIAS, 4998 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, 4999 .access = PL3_RW, 5000 .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, 5001 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, 5002 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, 5003 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, 5004 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, 5005 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, 5006 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, 5007 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, 5008 .type = ARM_CP_ALIAS, 5009 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, 5010 .access = PL3_RW, 5011 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) }, 5012 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, 5013 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, 5014 .access = PL3_RW, .writefn = vbar_write, 5015 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), 5016 .resetvalue = 0 }, 5017 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64, 5018 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2, 5019 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0, 5020 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) }, 5021 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64, 5022 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2, 5023 .access = PL3_RW, .resetvalue = 0, 5024 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) }, 5025 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64, 5026 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0, 5027 .access = PL3_RW, .type = ARM_CP_CONST, 5028 .resetvalue = 0 }, 5029 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH, 5030 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0, 5031 .access = PL3_RW, .type = ARM_CP_CONST, 5032 .resetvalue = 0 }, 5033 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH, 5034 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1, 5035 .access = PL3_RW, .type = ARM_CP_CONST, 5036 .resetvalue = 0 }, 5037 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64, 5038 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0, 5039 .access = PL3_W, .type = ARM_CP_NO_RAW, 5040 .writefn = tlbi_aa64_alle3is_write }, 5041 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64, 5042 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1, 5043 .access = PL3_W, .type = ARM_CP_NO_RAW, 5044 .writefn = tlbi_aa64_vae3is_write }, 5045 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64, 5046 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5, 5047 .access = PL3_W, .type = ARM_CP_NO_RAW, 5048 .writefn = tlbi_aa64_vae3is_write }, 5049 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64, 5050 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0, 5051 .access = PL3_W, .type = ARM_CP_NO_RAW, 5052 .writefn = tlbi_aa64_alle3_write }, 5053 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64, 5054 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1, 5055 .access = PL3_W, .type = ARM_CP_NO_RAW, 5056 .writefn = tlbi_aa64_vae3_write }, 5057 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64, 5058 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5, 5059 .access = PL3_W, .type = ARM_CP_NO_RAW, 5060 .writefn = tlbi_aa64_vae3_write }, 5061 REGINFO_SENTINEL 5062 }; 5063 5064 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 5065 bool isread) 5066 { 5067 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64, 5068 * but the AArch32 CTR has its own reginfo struct) 5069 */ 5070 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) { 5071 return CP_ACCESS_TRAP; 5072 } 5073 return CP_ACCESS_OK; 5074 } 5075 5076 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri, 5077 uint64_t value) 5078 { 5079 /* Writes to OSLAR_EL1 may update the OS lock status, which can be 5080 * read via a bit in OSLSR_EL1. 5081 */ 5082 int oslock; 5083 5084 if (ri->state == ARM_CP_STATE_AA32) { 5085 oslock = (value == 0xC5ACCE55); 5086 } else { 5087 oslock = value & 1; 5088 } 5089 5090 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock); 5091 } 5092 5093 static const ARMCPRegInfo debug_cp_reginfo[] = { 5094 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped 5095 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1; 5096 * unlike DBGDRAR it is never accessible from EL0. 5097 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64 5098 * accessor. 5099 */ 5100 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, 5101 .access = PL0_R, .accessfn = access_tdra, 5102 .type = ARM_CP_CONST, .resetvalue = 0 }, 5103 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64, 5104 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 5105 .access = PL1_R, .accessfn = access_tdra, 5106 .type = ARM_CP_CONST, .resetvalue = 0 }, 5107 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 5108 .access = PL0_R, .accessfn = access_tdra, 5109 .type = ARM_CP_CONST, .resetvalue = 0 }, 5110 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */ 5111 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH, 5112 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 5113 .access = PL1_RW, .accessfn = access_tda, 5114 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), 5115 .resetvalue = 0 }, 5116 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1. 5117 * We don't implement the configurable EL0 access. 5118 */ 5119 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH, 5120 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 5121 .type = ARM_CP_ALIAS, 5122 .access = PL1_R, .accessfn = access_tda, 5123 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), }, 5124 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH, 5125 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4, 5126 .access = PL1_W, .type = ARM_CP_NO_RAW, 5127 .accessfn = access_tdosa, 5128 .writefn = oslar_write }, 5129 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH, 5130 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4, 5131 .access = PL1_R, .resetvalue = 10, 5132 .accessfn = access_tdosa, 5133 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) }, 5134 /* Dummy OSDLR_EL1: 32-bit Linux will read this */ 5135 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH, 5136 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4, 5137 .access = PL1_RW, .accessfn = access_tdosa, 5138 .type = ARM_CP_NOP }, 5139 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't 5140 * implement vector catch debug events yet. 5141 */ 5142 { .name = "DBGVCR", 5143 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 5144 .access = PL1_RW, .accessfn = access_tda, 5145 .type = ARM_CP_NOP }, 5146 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor 5147 * to save and restore a 32-bit guest's DBGVCR) 5148 */ 5149 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64, 5150 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0, 5151 .access = PL2_RW, .accessfn = access_tda, 5152 .type = ARM_CP_NOP }, 5153 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications 5154 * Channel but Linux may try to access this register. The 32-bit 5155 * alias is DBGDCCINT. 5156 */ 5157 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH, 5158 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 5159 .access = PL1_RW, .accessfn = access_tda, 5160 .type = ARM_CP_NOP }, 5161 REGINFO_SENTINEL 5162 }; 5163 5164 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = { 5165 /* 64 bit access versions of the (dummy) debug registers */ 5166 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0, 5167 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 5168 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0, 5169 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 5170 REGINFO_SENTINEL 5171 }; 5172 5173 /* Return the exception level to which exceptions should be taken 5174 * via SVEAccessTrap. If an exception should be routed through 5175 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should 5176 * take care of raising that exception. 5177 * C.f. the ARM pseudocode function CheckSVEEnabled. 5178 */ 5179 int sve_exception_el(CPUARMState *env, int el) 5180 { 5181 #ifndef CONFIG_USER_ONLY 5182 if (el <= 1) { 5183 bool disabled = false; 5184 5185 /* The CPACR.ZEN controls traps to EL1: 5186 * 0, 2 : trap EL0 and EL1 accesses 5187 * 1 : trap only EL0 accesses 5188 * 3 : trap no accesses 5189 */ 5190 if (!extract32(env->cp15.cpacr_el1, 16, 1)) { 5191 disabled = true; 5192 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) { 5193 disabled = el == 0; 5194 } 5195 if (disabled) { 5196 /* route_to_el2 */ 5197 return (arm_feature(env, ARM_FEATURE_EL2) 5198 && (arm_hcr_el2_eff(env) & HCR_TGE) ? 2 : 1); 5199 } 5200 5201 /* Check CPACR.FPEN. */ 5202 if (!extract32(env->cp15.cpacr_el1, 20, 1)) { 5203 disabled = true; 5204 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) { 5205 disabled = el == 0; 5206 } 5207 if (disabled) { 5208 return 0; 5209 } 5210 } 5211 5212 /* CPTR_EL2. Since TZ and TFP are positive, 5213 * they will be zero when EL2 is not present. 5214 */ 5215 if (el <= 2 && !arm_is_secure_below_el3(env)) { 5216 if (env->cp15.cptr_el[2] & CPTR_TZ) { 5217 return 2; 5218 } 5219 if (env->cp15.cptr_el[2] & CPTR_TFP) { 5220 return 0; 5221 } 5222 } 5223 5224 /* CPTR_EL3. Since EZ is negative we must check for EL3. */ 5225 if (arm_feature(env, ARM_FEATURE_EL3) 5226 && !(env->cp15.cptr_el[3] & CPTR_EZ)) { 5227 return 3; 5228 } 5229 #endif 5230 return 0; 5231 } 5232 5233 /* 5234 * Given that SVE is enabled, return the vector length for EL. 5235 */ 5236 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el) 5237 { 5238 ARMCPU *cpu = arm_env_get_cpu(env); 5239 uint32_t zcr_len = cpu->sve_max_vq - 1; 5240 5241 if (el <= 1) { 5242 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]); 5243 } 5244 if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) { 5245 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]); 5246 } 5247 if (el < 3 && arm_feature(env, ARM_FEATURE_EL3)) { 5248 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]); 5249 } 5250 return zcr_len; 5251 } 5252 5253 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5254 uint64_t value) 5255 { 5256 int cur_el = arm_current_el(env); 5257 int old_len = sve_zcr_len_for_el(env, cur_el); 5258 int new_len; 5259 5260 /* Bits other than [3:0] are RAZ/WI. */ 5261 raw_write(env, ri, value & 0xf); 5262 5263 /* 5264 * Because we arrived here, we know both FP and SVE are enabled; 5265 * otherwise we would have trapped access to the ZCR_ELn register. 5266 */ 5267 new_len = sve_zcr_len_for_el(env, cur_el); 5268 if (new_len < old_len) { 5269 aarch64_sve_narrow_vq(env, new_len + 1); 5270 } 5271 } 5272 5273 static const ARMCPRegInfo zcr_el1_reginfo = { 5274 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64, 5275 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0, 5276 .access = PL1_RW, .type = ARM_CP_SVE, 5277 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]), 5278 .writefn = zcr_write, .raw_writefn = raw_write 5279 }; 5280 5281 static const ARMCPRegInfo zcr_el2_reginfo = { 5282 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 5283 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 5284 .access = PL2_RW, .type = ARM_CP_SVE, 5285 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]), 5286 .writefn = zcr_write, .raw_writefn = raw_write 5287 }; 5288 5289 static const ARMCPRegInfo zcr_no_el2_reginfo = { 5290 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 5291 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 5292 .access = PL2_RW, .type = ARM_CP_SVE, 5293 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore 5294 }; 5295 5296 static const ARMCPRegInfo zcr_el3_reginfo = { 5297 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64, 5298 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0, 5299 .access = PL3_RW, .type = ARM_CP_SVE, 5300 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]), 5301 .writefn = zcr_write, .raw_writefn = raw_write 5302 }; 5303 5304 void hw_watchpoint_update(ARMCPU *cpu, int n) 5305 { 5306 CPUARMState *env = &cpu->env; 5307 vaddr len = 0; 5308 vaddr wvr = env->cp15.dbgwvr[n]; 5309 uint64_t wcr = env->cp15.dbgwcr[n]; 5310 int mask; 5311 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 5312 5313 if (env->cpu_watchpoint[n]) { 5314 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]); 5315 env->cpu_watchpoint[n] = NULL; 5316 } 5317 5318 if (!extract64(wcr, 0, 1)) { 5319 /* E bit clear : watchpoint disabled */ 5320 return; 5321 } 5322 5323 switch (extract64(wcr, 3, 2)) { 5324 case 0: 5325 /* LSC 00 is reserved and must behave as if the wp is disabled */ 5326 return; 5327 case 1: 5328 flags |= BP_MEM_READ; 5329 break; 5330 case 2: 5331 flags |= BP_MEM_WRITE; 5332 break; 5333 case 3: 5334 flags |= BP_MEM_ACCESS; 5335 break; 5336 } 5337 5338 /* Attempts to use both MASK and BAS fields simultaneously are 5339 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case, 5340 * thus generating a watchpoint for every byte in the masked region. 5341 */ 5342 mask = extract64(wcr, 24, 4); 5343 if (mask == 1 || mask == 2) { 5344 /* Reserved values of MASK; we must act as if the mask value was 5345 * some non-reserved value, or as if the watchpoint were disabled. 5346 * We choose the latter. 5347 */ 5348 return; 5349 } else if (mask) { 5350 /* Watchpoint covers an aligned area up to 2GB in size */ 5351 len = 1ULL << mask; 5352 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE 5353 * whether the watchpoint fires when the unmasked bits match; we opt 5354 * to generate the exceptions. 5355 */ 5356 wvr &= ~(len - 1); 5357 } else { 5358 /* Watchpoint covers bytes defined by the byte address select bits */ 5359 int bas = extract64(wcr, 5, 8); 5360 int basstart; 5361 5362 if (bas == 0) { 5363 /* This must act as if the watchpoint is disabled */ 5364 return; 5365 } 5366 5367 if (extract64(wvr, 2, 1)) { 5368 /* Deprecated case of an only 4-aligned address. BAS[7:4] are 5369 * ignored, and BAS[3:0] define which bytes to watch. 5370 */ 5371 bas &= 0xf; 5372 } 5373 /* The BAS bits are supposed to be programmed to indicate a contiguous 5374 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether 5375 * we fire for each byte in the word/doubleword addressed by the WVR. 5376 * We choose to ignore any non-zero bits after the first range of 1s. 5377 */ 5378 basstart = ctz32(bas); 5379 len = cto32(bas >> basstart); 5380 wvr += basstart; 5381 } 5382 5383 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags, 5384 &env->cpu_watchpoint[n]); 5385 } 5386 5387 void hw_watchpoint_update_all(ARMCPU *cpu) 5388 { 5389 int i; 5390 CPUARMState *env = &cpu->env; 5391 5392 /* Completely clear out existing QEMU watchpoints and our array, to 5393 * avoid possible stale entries following migration load. 5394 */ 5395 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU); 5396 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint)); 5397 5398 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) { 5399 hw_watchpoint_update(cpu, i); 5400 } 5401 } 5402 5403 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5404 uint64_t value) 5405 { 5406 ARMCPU *cpu = arm_env_get_cpu(env); 5407 int i = ri->crm; 5408 5409 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the 5410 * register reads and behaves as if values written are sign extended. 5411 * Bits [1:0] are RES0. 5412 */ 5413 value = sextract64(value, 0, 49) & ~3ULL; 5414 5415 raw_write(env, ri, value); 5416 hw_watchpoint_update(cpu, i); 5417 } 5418 5419 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5420 uint64_t value) 5421 { 5422 ARMCPU *cpu = arm_env_get_cpu(env); 5423 int i = ri->crm; 5424 5425 raw_write(env, ri, value); 5426 hw_watchpoint_update(cpu, i); 5427 } 5428 5429 void hw_breakpoint_update(ARMCPU *cpu, int n) 5430 { 5431 CPUARMState *env = &cpu->env; 5432 uint64_t bvr = env->cp15.dbgbvr[n]; 5433 uint64_t bcr = env->cp15.dbgbcr[n]; 5434 vaddr addr; 5435 int bt; 5436 int flags = BP_CPU; 5437 5438 if (env->cpu_breakpoint[n]) { 5439 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]); 5440 env->cpu_breakpoint[n] = NULL; 5441 } 5442 5443 if (!extract64(bcr, 0, 1)) { 5444 /* E bit clear : watchpoint disabled */ 5445 return; 5446 } 5447 5448 bt = extract64(bcr, 20, 4); 5449 5450 switch (bt) { 5451 case 4: /* unlinked address mismatch (reserved if AArch64) */ 5452 case 5: /* linked address mismatch (reserved if AArch64) */ 5453 qemu_log_mask(LOG_UNIMP, 5454 "arm: address mismatch breakpoint types not implemented\n"); 5455 return; 5456 case 0: /* unlinked address match */ 5457 case 1: /* linked address match */ 5458 { 5459 /* Bits [63:49] are hardwired to the value of bit [48]; that is, 5460 * we behave as if the register was sign extended. Bits [1:0] are 5461 * RES0. The BAS field is used to allow setting breakpoints on 16 5462 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether 5463 * a bp will fire if the addresses covered by the bp and the addresses 5464 * covered by the insn overlap but the insn doesn't start at the 5465 * start of the bp address range. We choose to require the insn and 5466 * the bp to have the same address. The constraints on writing to 5467 * BAS enforced in dbgbcr_write mean we have only four cases: 5468 * 0b0000 => no breakpoint 5469 * 0b0011 => breakpoint on addr 5470 * 0b1100 => breakpoint on addr + 2 5471 * 0b1111 => breakpoint on addr 5472 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c). 5473 */ 5474 int bas = extract64(bcr, 5, 4); 5475 addr = sextract64(bvr, 0, 49) & ~3ULL; 5476 if (bas == 0) { 5477 return; 5478 } 5479 if (bas == 0xc) { 5480 addr += 2; 5481 } 5482 break; 5483 } 5484 case 2: /* unlinked context ID match */ 5485 case 8: /* unlinked VMID match (reserved if no EL2) */ 5486 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */ 5487 qemu_log_mask(LOG_UNIMP, 5488 "arm: unlinked context breakpoint types not implemented\n"); 5489 return; 5490 case 9: /* linked VMID match (reserved if no EL2) */ 5491 case 11: /* linked context ID and VMID match (reserved if no EL2) */ 5492 case 3: /* linked context ID match */ 5493 default: 5494 /* We must generate no events for Linked context matches (unless 5495 * they are linked to by some other bp/wp, which is handled in 5496 * updates for the linking bp/wp). We choose to also generate no events 5497 * for reserved values. 5498 */ 5499 return; 5500 } 5501 5502 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]); 5503 } 5504 5505 void hw_breakpoint_update_all(ARMCPU *cpu) 5506 { 5507 int i; 5508 CPUARMState *env = &cpu->env; 5509 5510 /* Completely clear out existing QEMU breakpoints and our array, to 5511 * avoid possible stale entries following migration load. 5512 */ 5513 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU); 5514 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint)); 5515 5516 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) { 5517 hw_breakpoint_update(cpu, i); 5518 } 5519 } 5520 5521 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5522 uint64_t value) 5523 { 5524 ARMCPU *cpu = arm_env_get_cpu(env); 5525 int i = ri->crm; 5526 5527 raw_write(env, ri, value); 5528 hw_breakpoint_update(cpu, i); 5529 } 5530 5531 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5532 uint64_t value) 5533 { 5534 ARMCPU *cpu = arm_env_get_cpu(env); 5535 int i = ri->crm; 5536 5537 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only 5538 * copy of BAS[0]. 5539 */ 5540 value = deposit64(value, 6, 1, extract64(value, 5, 1)); 5541 value = deposit64(value, 8, 1, extract64(value, 7, 1)); 5542 5543 raw_write(env, ri, value); 5544 hw_breakpoint_update(cpu, i); 5545 } 5546 5547 static void define_debug_regs(ARMCPU *cpu) 5548 { 5549 /* Define v7 and v8 architectural debug registers. 5550 * These are just dummy implementations for now. 5551 */ 5552 int i; 5553 int wrps, brps, ctx_cmps; 5554 ARMCPRegInfo dbgdidr = { 5555 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 5556 .access = PL0_R, .accessfn = access_tda, 5557 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr, 5558 }; 5559 5560 /* Note that all these register fields hold "number of Xs minus 1". */ 5561 brps = extract32(cpu->dbgdidr, 24, 4); 5562 wrps = extract32(cpu->dbgdidr, 28, 4); 5563 ctx_cmps = extract32(cpu->dbgdidr, 20, 4); 5564 5565 assert(ctx_cmps <= brps); 5566 5567 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties 5568 * of the debug registers such as number of breakpoints; 5569 * check that if they both exist then they agree. 5570 */ 5571 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 5572 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps); 5573 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps); 5574 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps); 5575 } 5576 5577 define_one_arm_cp_reg(cpu, &dbgdidr); 5578 define_arm_cp_regs(cpu, debug_cp_reginfo); 5579 5580 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) { 5581 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo); 5582 } 5583 5584 for (i = 0; i < brps + 1; i++) { 5585 ARMCPRegInfo dbgregs[] = { 5586 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH, 5587 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4, 5588 .access = PL1_RW, .accessfn = access_tda, 5589 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]), 5590 .writefn = dbgbvr_write, .raw_writefn = raw_write 5591 }, 5592 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH, 5593 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5, 5594 .access = PL1_RW, .accessfn = access_tda, 5595 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]), 5596 .writefn = dbgbcr_write, .raw_writefn = raw_write 5597 }, 5598 REGINFO_SENTINEL 5599 }; 5600 define_arm_cp_regs(cpu, dbgregs); 5601 } 5602 5603 for (i = 0; i < wrps + 1; i++) { 5604 ARMCPRegInfo dbgregs[] = { 5605 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH, 5606 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6, 5607 .access = PL1_RW, .accessfn = access_tda, 5608 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]), 5609 .writefn = dbgwvr_write, .raw_writefn = raw_write 5610 }, 5611 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH, 5612 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7, 5613 .access = PL1_RW, .accessfn = access_tda, 5614 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]), 5615 .writefn = dbgwcr_write, .raw_writefn = raw_write 5616 }, 5617 REGINFO_SENTINEL 5618 }; 5619 define_arm_cp_regs(cpu, dbgregs); 5620 } 5621 } 5622 5623 /* We don't know until after realize whether there's a GICv3 5624 * attached, and that is what registers the gicv3 sysregs. 5625 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1 5626 * at runtime. 5627 */ 5628 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri) 5629 { 5630 ARMCPU *cpu = arm_env_get_cpu(env); 5631 uint64_t pfr1 = cpu->id_pfr1; 5632 5633 if (env->gicv3state) { 5634 pfr1 |= 1 << 28; 5635 } 5636 return pfr1; 5637 } 5638 5639 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri) 5640 { 5641 ARMCPU *cpu = arm_env_get_cpu(env); 5642 uint64_t pfr0 = cpu->isar.id_aa64pfr0; 5643 5644 if (env->gicv3state) { 5645 pfr0 |= 1 << 24; 5646 } 5647 return pfr0; 5648 } 5649 5650 /* Shared logic between LORID and the rest of the LOR* registers. 5651 * Secure state has already been delt with. 5652 */ 5653 static CPAccessResult access_lor_ns(CPUARMState *env) 5654 { 5655 int el = arm_current_el(env); 5656 5657 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) { 5658 return CP_ACCESS_TRAP_EL2; 5659 } 5660 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) { 5661 return CP_ACCESS_TRAP_EL3; 5662 } 5663 return CP_ACCESS_OK; 5664 } 5665 5666 static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri, 5667 bool isread) 5668 { 5669 if (arm_is_secure_below_el3(env)) { 5670 /* Access ok in secure mode. */ 5671 return CP_ACCESS_OK; 5672 } 5673 return access_lor_ns(env); 5674 } 5675 5676 static CPAccessResult access_lor_other(CPUARMState *env, 5677 const ARMCPRegInfo *ri, bool isread) 5678 { 5679 if (arm_is_secure_below_el3(env)) { 5680 /* Access denied in secure mode. */ 5681 return CP_ACCESS_TRAP; 5682 } 5683 return access_lor_ns(env); 5684 } 5685 5686 #ifdef TARGET_AARCH64 5687 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri, 5688 bool isread) 5689 { 5690 int el = arm_current_el(env); 5691 5692 if (el < 2 && 5693 arm_feature(env, ARM_FEATURE_EL2) && 5694 !(arm_hcr_el2_eff(env) & HCR_APK)) { 5695 return CP_ACCESS_TRAP_EL2; 5696 } 5697 if (el < 3 && 5698 arm_feature(env, ARM_FEATURE_EL3) && 5699 !(env->cp15.scr_el3 & SCR_APK)) { 5700 return CP_ACCESS_TRAP_EL3; 5701 } 5702 return CP_ACCESS_OK; 5703 } 5704 5705 static const ARMCPRegInfo pauth_reginfo[] = { 5706 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5707 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0, 5708 .access = PL1_RW, .accessfn = access_pauth, 5709 .fieldoffset = offsetof(CPUARMState, apda_key.lo) }, 5710 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5711 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1, 5712 .access = PL1_RW, .accessfn = access_pauth, 5713 .fieldoffset = offsetof(CPUARMState, apda_key.hi) }, 5714 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5715 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2, 5716 .access = PL1_RW, .accessfn = access_pauth, 5717 .fieldoffset = offsetof(CPUARMState, apdb_key.lo) }, 5718 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5719 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3, 5720 .access = PL1_RW, .accessfn = access_pauth, 5721 .fieldoffset = offsetof(CPUARMState, apdb_key.hi) }, 5722 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5723 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0, 5724 .access = PL1_RW, .accessfn = access_pauth, 5725 .fieldoffset = offsetof(CPUARMState, apga_key.lo) }, 5726 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5727 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1, 5728 .access = PL1_RW, .accessfn = access_pauth, 5729 .fieldoffset = offsetof(CPUARMState, apga_key.hi) }, 5730 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5731 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0, 5732 .access = PL1_RW, .accessfn = access_pauth, 5733 .fieldoffset = offsetof(CPUARMState, apia_key.lo) }, 5734 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5735 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1, 5736 .access = PL1_RW, .accessfn = access_pauth, 5737 .fieldoffset = offsetof(CPUARMState, apia_key.hi) }, 5738 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5739 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2, 5740 .access = PL1_RW, .accessfn = access_pauth, 5741 .fieldoffset = offsetof(CPUARMState, apib_key.lo) }, 5742 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5743 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3, 5744 .access = PL1_RW, .accessfn = access_pauth, 5745 .fieldoffset = offsetof(CPUARMState, apib_key.hi) }, 5746 REGINFO_SENTINEL 5747 }; 5748 #endif 5749 5750 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri, 5751 bool isread) 5752 { 5753 int el = arm_current_el(env); 5754 5755 if (el == 0) { 5756 uint64_t sctlr = arm_sctlr(env, el); 5757 if (!(sctlr & SCTLR_EnRCTX)) { 5758 return CP_ACCESS_TRAP; 5759 } 5760 } else if (el == 1) { 5761 uint64_t hcr = arm_hcr_el2_eff(env); 5762 if (hcr & HCR_NV) { 5763 return CP_ACCESS_TRAP_EL2; 5764 } 5765 } 5766 return CP_ACCESS_OK; 5767 } 5768 5769 static const ARMCPRegInfo predinv_reginfo[] = { 5770 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64, 5771 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4, 5772 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5773 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64, 5774 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5, 5775 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5776 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64, 5777 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7, 5778 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5779 /* 5780 * Note the AArch32 opcodes have a different OPC1. 5781 */ 5782 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32, 5783 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4, 5784 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5785 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32, 5786 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5, 5787 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5788 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32, 5789 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7, 5790 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5791 REGINFO_SENTINEL 5792 }; 5793 5794 void register_cp_regs_for_features(ARMCPU *cpu) 5795 { 5796 /* Register all the coprocessor registers based on feature bits */ 5797 CPUARMState *env = &cpu->env; 5798 if (arm_feature(env, ARM_FEATURE_M)) { 5799 /* M profile has no coprocessor registers */ 5800 return; 5801 } 5802 5803 define_arm_cp_regs(cpu, cp_reginfo); 5804 if (!arm_feature(env, ARM_FEATURE_V8)) { 5805 /* Must go early as it is full of wildcards that may be 5806 * overridden by later definitions. 5807 */ 5808 define_arm_cp_regs(cpu, not_v8_cp_reginfo); 5809 } 5810 5811 if (arm_feature(env, ARM_FEATURE_V6)) { 5812 /* The ID registers all have impdef reset values */ 5813 ARMCPRegInfo v6_idregs[] = { 5814 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, 5815 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 5816 .access = PL1_R, .type = ARM_CP_CONST, 5817 .resetvalue = cpu->id_pfr0 }, 5818 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know 5819 * the value of the GIC field until after we define these regs. 5820 */ 5821 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, 5822 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, 5823 .access = PL1_R, .type = ARM_CP_NO_RAW, 5824 .readfn = id_pfr1_read, 5825 .writefn = arm_cp_write_ignore }, 5826 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, 5827 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, 5828 .access = PL1_R, .type = ARM_CP_CONST, 5829 .resetvalue = cpu->id_dfr0 }, 5830 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, 5831 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, 5832 .access = PL1_R, .type = ARM_CP_CONST, 5833 .resetvalue = cpu->id_afr0 }, 5834 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, 5835 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, 5836 .access = PL1_R, .type = ARM_CP_CONST, 5837 .resetvalue = cpu->id_mmfr0 }, 5838 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, 5839 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, 5840 .access = PL1_R, .type = ARM_CP_CONST, 5841 .resetvalue = cpu->id_mmfr1 }, 5842 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, 5843 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, 5844 .access = PL1_R, .type = ARM_CP_CONST, 5845 .resetvalue = cpu->id_mmfr2 }, 5846 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, 5847 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, 5848 .access = PL1_R, .type = ARM_CP_CONST, 5849 .resetvalue = cpu->id_mmfr3 }, 5850 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, 5851 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 5852 .access = PL1_R, .type = ARM_CP_CONST, 5853 .resetvalue = cpu->isar.id_isar0 }, 5854 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, 5855 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, 5856 .access = PL1_R, .type = ARM_CP_CONST, 5857 .resetvalue = cpu->isar.id_isar1 }, 5858 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, 5859 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 5860 .access = PL1_R, .type = ARM_CP_CONST, 5861 .resetvalue = cpu->isar.id_isar2 }, 5862 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, 5863 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, 5864 .access = PL1_R, .type = ARM_CP_CONST, 5865 .resetvalue = cpu->isar.id_isar3 }, 5866 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, 5867 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, 5868 .access = PL1_R, .type = ARM_CP_CONST, 5869 .resetvalue = cpu->isar.id_isar4 }, 5870 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, 5871 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, 5872 .access = PL1_R, .type = ARM_CP_CONST, 5873 .resetvalue = cpu->isar.id_isar5 }, 5874 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH, 5875 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6, 5876 .access = PL1_R, .type = ARM_CP_CONST, 5877 .resetvalue = cpu->id_mmfr4 }, 5878 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH, 5879 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7, 5880 .access = PL1_R, .type = ARM_CP_CONST, 5881 .resetvalue = cpu->isar.id_isar6 }, 5882 REGINFO_SENTINEL 5883 }; 5884 define_arm_cp_regs(cpu, v6_idregs); 5885 define_arm_cp_regs(cpu, v6_cp_reginfo); 5886 } else { 5887 define_arm_cp_regs(cpu, not_v6_cp_reginfo); 5888 } 5889 if (arm_feature(env, ARM_FEATURE_V6K)) { 5890 define_arm_cp_regs(cpu, v6k_cp_reginfo); 5891 } 5892 if (arm_feature(env, ARM_FEATURE_V7MP) && 5893 !arm_feature(env, ARM_FEATURE_PMSA)) { 5894 define_arm_cp_regs(cpu, v7mp_cp_reginfo); 5895 } 5896 if (arm_feature(env, ARM_FEATURE_V7VE)) { 5897 define_arm_cp_regs(cpu, pmovsset_cp_reginfo); 5898 } 5899 if (arm_feature(env, ARM_FEATURE_V7)) { 5900 /* v7 performance monitor control register: same implementor 5901 * field as main ID register, and we implement four counters in 5902 * addition to the cycle count register. 5903 */ 5904 unsigned int i, pmcrn = 4; 5905 ARMCPRegInfo pmcr = { 5906 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, 5907 .access = PL0_RW, 5908 .type = ARM_CP_IO | ARM_CP_ALIAS, 5909 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), 5910 .accessfn = pmreg_access, .writefn = pmcr_write, 5911 .raw_writefn = raw_write, 5912 }; 5913 ARMCPRegInfo pmcr64 = { 5914 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, 5915 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, 5916 .access = PL0_RW, .accessfn = pmreg_access, 5917 .type = ARM_CP_IO, 5918 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), 5919 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT), 5920 .writefn = pmcr_write, .raw_writefn = raw_write, 5921 }; 5922 define_one_arm_cp_reg(cpu, &pmcr); 5923 define_one_arm_cp_reg(cpu, &pmcr64); 5924 for (i = 0; i < pmcrn; i++) { 5925 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i); 5926 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i); 5927 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i); 5928 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i); 5929 ARMCPRegInfo pmev_regs[] = { 5930 { .name = pmevcntr_name, .cp = 15, .crn = 14, 5931 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 5932 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 5933 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 5934 .accessfn = pmreg_access }, 5935 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64, 5936 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)), 5937 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access, 5938 .type = ARM_CP_IO, 5939 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 5940 .raw_readfn = pmevcntr_rawread, 5941 .raw_writefn = pmevcntr_rawwrite }, 5942 { .name = pmevtyper_name, .cp = 15, .crn = 14, 5943 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 5944 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 5945 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 5946 .accessfn = pmreg_access }, 5947 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64, 5948 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)), 5949 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access, 5950 .type = ARM_CP_IO, 5951 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 5952 .raw_writefn = pmevtyper_rawwrite }, 5953 REGINFO_SENTINEL 5954 }; 5955 define_arm_cp_regs(cpu, pmev_regs); 5956 g_free(pmevcntr_name); 5957 g_free(pmevcntr_el0_name); 5958 g_free(pmevtyper_name); 5959 g_free(pmevtyper_el0_name); 5960 } 5961 ARMCPRegInfo clidr = { 5962 .name = "CLIDR", .state = ARM_CP_STATE_BOTH, 5963 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, 5964 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr 5965 }; 5966 define_one_arm_cp_reg(cpu, &clidr); 5967 define_arm_cp_regs(cpu, v7_cp_reginfo); 5968 define_debug_regs(cpu); 5969 } else { 5970 define_arm_cp_regs(cpu, not_v7_cp_reginfo); 5971 } 5972 if (FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) >= 4 && 5973 FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) != 0xf) { 5974 ARMCPRegInfo v81_pmu_regs[] = { 5975 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32, 5976 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4, 5977 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5978 .resetvalue = extract64(cpu->pmceid0, 32, 32) }, 5979 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32, 5980 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5, 5981 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 5982 .resetvalue = extract64(cpu->pmceid1, 32, 32) }, 5983 REGINFO_SENTINEL 5984 }; 5985 define_arm_cp_regs(cpu, v81_pmu_regs); 5986 } 5987 if (arm_feature(env, ARM_FEATURE_V8)) { 5988 /* AArch64 ID registers, which all have impdef reset values. 5989 * Note that within the ID register ranges the unused slots 5990 * must all RAZ, not UNDEF; future architecture versions may 5991 * define new registers here. 5992 */ 5993 ARMCPRegInfo v8_idregs[] = { 5994 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't 5995 * know the right value for the GIC field until after we 5996 * define these regs. 5997 */ 5998 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, 5999 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, 6000 .access = PL1_R, .type = ARM_CP_NO_RAW, 6001 .readfn = id_aa64pfr0_read, 6002 .writefn = arm_cp_write_ignore }, 6003 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, 6004 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, 6005 .access = PL1_R, .type = ARM_CP_CONST, 6006 .resetvalue = cpu->isar.id_aa64pfr1}, 6007 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6008 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2, 6009 .access = PL1_R, .type = ARM_CP_CONST, 6010 .resetvalue = 0 }, 6011 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6012 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3, 6013 .access = PL1_R, .type = ARM_CP_CONST, 6014 .resetvalue = 0 }, 6015 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64, 6016 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4, 6017 .access = PL1_R, .type = ARM_CP_CONST, 6018 /* At present, only SVEver == 0 is defined anyway. */ 6019 .resetvalue = 0 }, 6020 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6021 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5, 6022 .access = PL1_R, .type = ARM_CP_CONST, 6023 .resetvalue = 0 }, 6024 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6025 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6, 6026 .access = PL1_R, .type = ARM_CP_CONST, 6027 .resetvalue = 0 }, 6028 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6029 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7, 6030 .access = PL1_R, .type = ARM_CP_CONST, 6031 .resetvalue = 0 }, 6032 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, 6033 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, 6034 .access = PL1_R, .type = ARM_CP_CONST, 6035 .resetvalue = cpu->id_aa64dfr0 }, 6036 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, 6037 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, 6038 .access = PL1_R, .type = ARM_CP_CONST, 6039 .resetvalue = cpu->id_aa64dfr1 }, 6040 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6041 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2, 6042 .access = PL1_R, .type = ARM_CP_CONST, 6043 .resetvalue = 0 }, 6044 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6045 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3, 6046 .access = PL1_R, .type = ARM_CP_CONST, 6047 .resetvalue = 0 }, 6048 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, 6049 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, 6050 .access = PL1_R, .type = ARM_CP_CONST, 6051 .resetvalue = cpu->id_aa64afr0 }, 6052 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, 6053 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, 6054 .access = PL1_R, .type = ARM_CP_CONST, 6055 .resetvalue = cpu->id_aa64afr1 }, 6056 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6057 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6, 6058 .access = PL1_R, .type = ARM_CP_CONST, 6059 .resetvalue = 0 }, 6060 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6061 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7, 6062 .access = PL1_R, .type = ARM_CP_CONST, 6063 .resetvalue = 0 }, 6064 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, 6065 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, 6066 .access = PL1_R, .type = ARM_CP_CONST, 6067 .resetvalue = cpu->isar.id_aa64isar0 }, 6068 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, 6069 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, 6070 .access = PL1_R, .type = ARM_CP_CONST, 6071 .resetvalue = cpu->isar.id_aa64isar1 }, 6072 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6073 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2, 6074 .access = PL1_R, .type = ARM_CP_CONST, 6075 .resetvalue = 0 }, 6076 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6077 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3, 6078 .access = PL1_R, .type = ARM_CP_CONST, 6079 .resetvalue = 0 }, 6080 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6081 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4, 6082 .access = PL1_R, .type = ARM_CP_CONST, 6083 .resetvalue = 0 }, 6084 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6085 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5, 6086 .access = PL1_R, .type = ARM_CP_CONST, 6087 .resetvalue = 0 }, 6088 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6089 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6, 6090 .access = PL1_R, .type = ARM_CP_CONST, 6091 .resetvalue = 0 }, 6092 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6093 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7, 6094 .access = PL1_R, .type = ARM_CP_CONST, 6095 .resetvalue = 0 }, 6096 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, 6097 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 6098 .access = PL1_R, .type = ARM_CP_CONST, 6099 .resetvalue = cpu->isar.id_aa64mmfr0 }, 6100 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, 6101 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, 6102 .access = PL1_R, .type = ARM_CP_CONST, 6103 .resetvalue = cpu->isar.id_aa64mmfr1 }, 6104 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6105 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2, 6106 .access = PL1_R, .type = ARM_CP_CONST, 6107 .resetvalue = 0 }, 6108 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6109 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3, 6110 .access = PL1_R, .type = ARM_CP_CONST, 6111 .resetvalue = 0 }, 6112 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6113 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4, 6114 .access = PL1_R, .type = ARM_CP_CONST, 6115 .resetvalue = 0 }, 6116 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6117 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5, 6118 .access = PL1_R, .type = ARM_CP_CONST, 6119 .resetvalue = 0 }, 6120 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6121 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6, 6122 .access = PL1_R, .type = ARM_CP_CONST, 6123 .resetvalue = 0 }, 6124 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6125 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7, 6126 .access = PL1_R, .type = ARM_CP_CONST, 6127 .resetvalue = 0 }, 6128 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, 6129 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 6130 .access = PL1_R, .type = ARM_CP_CONST, 6131 .resetvalue = cpu->isar.mvfr0 }, 6132 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, 6133 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 6134 .access = PL1_R, .type = ARM_CP_CONST, 6135 .resetvalue = cpu->isar.mvfr1 }, 6136 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, 6137 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 6138 .access = PL1_R, .type = ARM_CP_CONST, 6139 .resetvalue = cpu->isar.mvfr2 }, 6140 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6141 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3, 6142 .access = PL1_R, .type = ARM_CP_CONST, 6143 .resetvalue = 0 }, 6144 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6145 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4, 6146 .access = PL1_R, .type = ARM_CP_CONST, 6147 .resetvalue = 0 }, 6148 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6149 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5, 6150 .access = PL1_R, .type = ARM_CP_CONST, 6151 .resetvalue = 0 }, 6152 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6153 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6, 6154 .access = PL1_R, .type = ARM_CP_CONST, 6155 .resetvalue = 0 }, 6156 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6157 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7, 6158 .access = PL1_R, .type = ARM_CP_CONST, 6159 .resetvalue = 0 }, 6160 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32, 6161 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6, 6162 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6163 .resetvalue = extract64(cpu->pmceid0, 0, 32) }, 6164 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64, 6165 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6, 6166 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6167 .resetvalue = cpu->pmceid0 }, 6168 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32, 6169 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7, 6170 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6171 .resetvalue = extract64(cpu->pmceid1, 0, 32) }, 6172 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64, 6173 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7, 6174 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6175 .resetvalue = cpu->pmceid1 }, 6176 REGINFO_SENTINEL 6177 }; 6178 #ifdef CONFIG_USER_ONLY 6179 ARMCPRegUserSpaceInfo v8_user_idregs[] = { 6180 { .name = "ID_AA64PFR0_EL1", 6181 .exported_bits = 0x000f000f00ff0000, 6182 .fixed_bits = 0x0000000000000011 }, 6183 { .name = "ID_AA64PFR1_EL1", 6184 .exported_bits = 0x00000000000000f0 }, 6185 { .name = "ID_AA64PFR*_EL1_RESERVED", 6186 .is_glob = true }, 6187 { .name = "ID_AA64ZFR0_EL1" }, 6188 { .name = "ID_AA64MMFR0_EL1", 6189 .fixed_bits = 0x00000000ff000000 }, 6190 { .name = "ID_AA64MMFR1_EL1" }, 6191 { .name = "ID_AA64MMFR*_EL1_RESERVED", 6192 .is_glob = true }, 6193 { .name = "ID_AA64DFR0_EL1", 6194 .fixed_bits = 0x0000000000000006 }, 6195 { .name = "ID_AA64DFR1_EL1" }, 6196 { .name = "ID_AA64DFR*_EL1_RESERVED", 6197 .is_glob = true }, 6198 { .name = "ID_AA64AFR*", 6199 .is_glob = true }, 6200 { .name = "ID_AA64ISAR0_EL1", 6201 .exported_bits = 0x00fffffff0fffff0 }, 6202 { .name = "ID_AA64ISAR1_EL1", 6203 .exported_bits = 0x000000f0ffffffff }, 6204 { .name = "ID_AA64ISAR*_EL1_RESERVED", 6205 .is_glob = true }, 6206 REGUSERINFO_SENTINEL 6207 }; 6208 modify_arm_cp_regs(v8_idregs, v8_user_idregs); 6209 #endif 6210 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */ 6211 if (!arm_feature(env, ARM_FEATURE_EL3) && 6212 !arm_feature(env, ARM_FEATURE_EL2)) { 6213 ARMCPRegInfo rvbar = { 6214 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64, 6215 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 6216 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar 6217 }; 6218 define_one_arm_cp_reg(cpu, &rvbar); 6219 } 6220 define_arm_cp_regs(cpu, v8_idregs); 6221 define_arm_cp_regs(cpu, v8_cp_reginfo); 6222 } 6223 if (arm_feature(env, ARM_FEATURE_EL2)) { 6224 uint64_t vmpidr_def = mpidr_read_val(env); 6225 ARMCPRegInfo vpidr_regs[] = { 6226 { .name = "VPIDR", .state = ARM_CP_STATE_AA32, 6227 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 6228 .access = PL2_RW, .accessfn = access_el3_aa32ns, 6229 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS, 6230 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) }, 6231 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64, 6232 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 6233 .access = PL2_RW, .resetvalue = cpu->midr, 6234 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 6235 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32, 6236 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 6237 .access = PL2_RW, .accessfn = access_el3_aa32ns, 6238 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS, 6239 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) }, 6240 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64, 6241 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 6242 .access = PL2_RW, 6243 .resetvalue = vmpidr_def, 6244 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, 6245 REGINFO_SENTINEL 6246 }; 6247 define_arm_cp_regs(cpu, vpidr_regs); 6248 define_arm_cp_regs(cpu, el2_cp_reginfo); 6249 if (arm_feature(env, ARM_FEATURE_V8)) { 6250 define_arm_cp_regs(cpu, el2_v8_cp_reginfo); 6251 } 6252 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */ 6253 if (!arm_feature(env, ARM_FEATURE_EL3)) { 6254 ARMCPRegInfo rvbar = { 6255 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64, 6256 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1, 6257 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar 6258 }; 6259 define_one_arm_cp_reg(cpu, &rvbar); 6260 } 6261 } else { 6262 /* If EL2 is missing but higher ELs are enabled, we need to 6263 * register the no_el2 reginfos. 6264 */ 6265 if (arm_feature(env, ARM_FEATURE_EL3)) { 6266 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value 6267 * of MIDR_EL1 and MPIDR_EL1. 6268 */ 6269 ARMCPRegInfo vpidr_regs[] = { 6270 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH, 6271 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 6272 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 6273 .type = ARM_CP_CONST, .resetvalue = cpu->midr, 6274 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 6275 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH, 6276 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 6277 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 6278 .type = ARM_CP_NO_RAW, 6279 .writefn = arm_cp_write_ignore, .readfn = mpidr_read }, 6280 REGINFO_SENTINEL 6281 }; 6282 define_arm_cp_regs(cpu, vpidr_regs); 6283 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo); 6284 if (arm_feature(env, ARM_FEATURE_V8)) { 6285 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo); 6286 } 6287 } 6288 } 6289 if (arm_feature(env, ARM_FEATURE_EL3)) { 6290 define_arm_cp_regs(cpu, el3_cp_reginfo); 6291 ARMCPRegInfo el3_regs[] = { 6292 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64, 6293 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1, 6294 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar }, 6295 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, 6296 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, 6297 .access = PL3_RW, 6298 .raw_writefn = raw_write, .writefn = sctlr_write, 6299 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]), 6300 .resetvalue = cpu->reset_sctlr }, 6301 REGINFO_SENTINEL 6302 }; 6303 6304 define_arm_cp_regs(cpu, el3_regs); 6305 } 6306 /* The behaviour of NSACR is sufficiently various that we don't 6307 * try to describe it in a single reginfo: 6308 * if EL3 is 64 bit, then trap to EL3 from S EL1, 6309 * reads as constant 0xc00 from NS EL1 and NS EL2 6310 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2 6311 * if v7 without EL3, register doesn't exist 6312 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2 6313 */ 6314 if (arm_feature(env, ARM_FEATURE_EL3)) { 6315 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 6316 ARMCPRegInfo nsacr = { 6317 .name = "NSACR", .type = ARM_CP_CONST, 6318 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 6319 .access = PL1_RW, .accessfn = nsacr_access, 6320 .resetvalue = 0xc00 6321 }; 6322 define_one_arm_cp_reg(cpu, &nsacr); 6323 } else { 6324 ARMCPRegInfo nsacr = { 6325 .name = "NSACR", 6326 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 6327 .access = PL3_RW | PL1_R, 6328 .resetvalue = 0, 6329 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) 6330 }; 6331 define_one_arm_cp_reg(cpu, &nsacr); 6332 } 6333 } else { 6334 if (arm_feature(env, ARM_FEATURE_V8)) { 6335 ARMCPRegInfo nsacr = { 6336 .name = "NSACR", .type = ARM_CP_CONST, 6337 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 6338 .access = PL1_R, 6339 .resetvalue = 0xc00 6340 }; 6341 define_one_arm_cp_reg(cpu, &nsacr); 6342 } 6343 } 6344 6345 if (arm_feature(env, ARM_FEATURE_PMSA)) { 6346 if (arm_feature(env, ARM_FEATURE_V6)) { 6347 /* PMSAv6 not implemented */ 6348 assert(arm_feature(env, ARM_FEATURE_V7)); 6349 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 6350 define_arm_cp_regs(cpu, pmsav7_cp_reginfo); 6351 } else { 6352 define_arm_cp_regs(cpu, pmsav5_cp_reginfo); 6353 } 6354 } else { 6355 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 6356 define_arm_cp_regs(cpu, vmsa_cp_reginfo); 6357 /* TTCBR2 is introduced with ARMv8.2-A32HPD. */ 6358 if (FIELD_EX32(cpu->id_mmfr4, ID_MMFR4, HPDS) != 0) { 6359 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo); 6360 } 6361 } 6362 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { 6363 define_arm_cp_regs(cpu, t2ee_cp_reginfo); 6364 } 6365 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 6366 define_arm_cp_regs(cpu, generic_timer_cp_reginfo); 6367 } 6368 if (arm_feature(env, ARM_FEATURE_VAPA)) { 6369 define_arm_cp_regs(cpu, vapa_cp_reginfo); 6370 } 6371 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { 6372 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); 6373 } 6374 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { 6375 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); 6376 } 6377 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { 6378 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); 6379 } 6380 if (arm_feature(env, ARM_FEATURE_OMAPCP)) { 6381 define_arm_cp_regs(cpu, omap_cp_reginfo); 6382 } 6383 if (arm_feature(env, ARM_FEATURE_STRONGARM)) { 6384 define_arm_cp_regs(cpu, strongarm_cp_reginfo); 6385 } 6386 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 6387 define_arm_cp_regs(cpu, xscale_cp_reginfo); 6388 } 6389 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { 6390 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); 6391 } 6392 if (arm_feature(env, ARM_FEATURE_LPAE)) { 6393 define_arm_cp_regs(cpu, lpae_cp_reginfo); 6394 } 6395 /* Slightly awkwardly, the OMAP and StrongARM cores need all of 6396 * cp15 crn=0 to be writes-ignored, whereas for other cores they should 6397 * be read-only (ie write causes UNDEF exception). 6398 */ 6399 { 6400 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { 6401 /* Pre-v8 MIDR space. 6402 * Note that the MIDR isn't a simple constant register because 6403 * of the TI925 behaviour where writes to another register can 6404 * cause the MIDR value to change. 6405 * 6406 * Unimplemented registers in the c15 0 0 0 space default to 6407 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR 6408 * and friends override accordingly. 6409 */ 6410 { .name = "MIDR", 6411 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, 6412 .access = PL1_R, .resetvalue = cpu->midr, 6413 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, 6414 .readfn = midr_read, 6415 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 6416 .type = ARM_CP_OVERRIDE }, 6417 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ 6418 { .name = "DUMMY", 6419 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, 6420 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6421 { .name = "DUMMY", 6422 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, 6423 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6424 { .name = "DUMMY", 6425 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, 6426 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6427 { .name = "DUMMY", 6428 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, 6429 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6430 { .name = "DUMMY", 6431 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, 6432 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6433 REGINFO_SENTINEL 6434 }; 6435 ARMCPRegInfo id_v8_midr_cp_reginfo[] = { 6436 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, 6437 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, 6438 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr, 6439 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 6440 .readfn = midr_read }, 6441 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */ 6442 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 6443 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 6444 .access = PL1_R, .resetvalue = cpu->midr }, 6445 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 6446 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7, 6447 .access = PL1_R, .resetvalue = cpu->midr }, 6448 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, 6449 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, 6450 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr }, 6451 REGINFO_SENTINEL 6452 }; 6453 ARMCPRegInfo id_cp_reginfo[] = { 6454 /* These are common to v8 and pre-v8 */ 6455 { .name = "CTR", 6456 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, 6457 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 6458 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, 6459 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, 6460 .access = PL0_R, .accessfn = ctr_el0_access, 6461 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 6462 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ 6463 { .name = "TCMTR", 6464 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, 6465 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6466 REGINFO_SENTINEL 6467 }; 6468 /* TLBTR is specific to VMSA */ 6469 ARMCPRegInfo id_tlbtr_reginfo = { 6470 .name = "TLBTR", 6471 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, 6472 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0, 6473 }; 6474 /* MPUIR is specific to PMSA V6+ */ 6475 ARMCPRegInfo id_mpuir_reginfo = { 6476 .name = "MPUIR", 6477 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 6478 .access = PL1_R, .type = ARM_CP_CONST, 6479 .resetvalue = cpu->pmsav7_dregion << 8 6480 }; 6481 ARMCPRegInfo crn0_wi_reginfo = { 6482 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, 6483 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, 6484 .type = ARM_CP_NOP | ARM_CP_OVERRIDE 6485 }; 6486 #ifdef CONFIG_USER_ONLY 6487 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = { 6488 { .name = "MIDR_EL1", 6489 .exported_bits = 0x00000000ffffffff }, 6490 { .name = "REVIDR_EL1" }, 6491 REGUSERINFO_SENTINEL 6492 }; 6493 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo); 6494 #endif 6495 if (arm_feature(env, ARM_FEATURE_OMAPCP) || 6496 arm_feature(env, ARM_FEATURE_STRONGARM)) { 6497 ARMCPRegInfo *r; 6498 /* Register the blanket "writes ignored" value first to cover the 6499 * whole space. Then update the specific ID registers to allow write 6500 * access, so that they ignore writes rather than causing them to 6501 * UNDEF. 6502 */ 6503 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); 6504 for (r = id_pre_v8_midr_cp_reginfo; 6505 r->type != ARM_CP_SENTINEL; r++) { 6506 r->access = PL1_RW; 6507 } 6508 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) { 6509 r->access = PL1_RW; 6510 } 6511 id_mpuir_reginfo.access = PL1_RW; 6512 id_tlbtr_reginfo.access = PL1_RW; 6513 } 6514 if (arm_feature(env, ARM_FEATURE_V8)) { 6515 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); 6516 } else { 6517 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); 6518 } 6519 define_arm_cp_regs(cpu, id_cp_reginfo); 6520 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 6521 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo); 6522 } else if (arm_feature(env, ARM_FEATURE_V7)) { 6523 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 6524 } 6525 } 6526 6527 if (arm_feature(env, ARM_FEATURE_MPIDR)) { 6528 ARMCPRegInfo mpidr_cp_reginfo[] = { 6529 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH, 6530 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, 6531 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW }, 6532 REGINFO_SENTINEL 6533 }; 6534 #ifdef CONFIG_USER_ONLY 6535 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = { 6536 { .name = "MPIDR_EL1", 6537 .fixed_bits = 0x0000000080000000 }, 6538 REGUSERINFO_SENTINEL 6539 }; 6540 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo); 6541 #endif 6542 define_arm_cp_regs(cpu, mpidr_cp_reginfo); 6543 } 6544 6545 if (arm_feature(env, ARM_FEATURE_AUXCR)) { 6546 ARMCPRegInfo auxcr_reginfo[] = { 6547 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, 6548 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, 6549 .access = PL1_RW, .type = ARM_CP_CONST, 6550 .resetvalue = cpu->reset_auxcr }, 6551 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH, 6552 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1, 6553 .access = PL2_RW, .type = ARM_CP_CONST, 6554 .resetvalue = 0 }, 6555 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64, 6556 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1, 6557 .access = PL3_RW, .type = ARM_CP_CONST, 6558 .resetvalue = 0 }, 6559 REGINFO_SENTINEL 6560 }; 6561 define_arm_cp_regs(cpu, auxcr_reginfo); 6562 if (arm_feature(env, ARM_FEATURE_V8)) { 6563 /* HACTLR2 maps to ACTLR_EL2[63:32] and is not in ARMv7 */ 6564 ARMCPRegInfo hactlr2_reginfo = { 6565 .name = "HACTLR2", .state = ARM_CP_STATE_AA32, 6566 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3, 6567 .access = PL2_RW, .type = ARM_CP_CONST, 6568 .resetvalue = 0 6569 }; 6570 define_one_arm_cp_reg(cpu, &hactlr2_reginfo); 6571 } 6572 } 6573 6574 if (arm_feature(env, ARM_FEATURE_CBAR)) { 6575 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 6576 /* 32 bit view is [31:18] 0...0 [43:32]. */ 6577 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) 6578 | extract64(cpu->reset_cbar, 32, 12); 6579 ARMCPRegInfo cbar_reginfo[] = { 6580 { .name = "CBAR", 6581 .type = ARM_CP_CONST, 6582 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 6583 .access = PL1_R, .resetvalue = cpu->reset_cbar }, 6584 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, 6585 .type = ARM_CP_CONST, 6586 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, 6587 .access = PL1_R, .resetvalue = cbar32 }, 6588 REGINFO_SENTINEL 6589 }; 6590 /* We don't implement a r/w 64 bit CBAR currently */ 6591 assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); 6592 define_arm_cp_regs(cpu, cbar_reginfo); 6593 } else { 6594 ARMCPRegInfo cbar = { 6595 .name = "CBAR", 6596 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 6597 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar, 6598 .fieldoffset = offsetof(CPUARMState, 6599 cp15.c15_config_base_address) 6600 }; 6601 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 6602 cbar.access = PL1_R; 6603 cbar.fieldoffset = 0; 6604 cbar.type = ARM_CP_CONST; 6605 } 6606 define_one_arm_cp_reg(cpu, &cbar); 6607 } 6608 } 6609 6610 if (arm_feature(env, ARM_FEATURE_VBAR)) { 6611 ARMCPRegInfo vbar_cp_reginfo[] = { 6612 { .name = "VBAR", .state = ARM_CP_STATE_BOTH, 6613 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, 6614 .access = PL1_RW, .writefn = vbar_write, 6615 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s), 6616 offsetof(CPUARMState, cp15.vbar_ns) }, 6617 .resetvalue = 0 }, 6618 REGINFO_SENTINEL 6619 }; 6620 define_arm_cp_regs(cpu, vbar_cp_reginfo); 6621 } 6622 6623 /* Generic registers whose values depend on the implementation */ 6624 { 6625 ARMCPRegInfo sctlr = { 6626 .name = "SCTLR", .state = ARM_CP_STATE_BOTH, 6627 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 6628 .access = PL1_RW, 6629 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), 6630 offsetof(CPUARMState, cp15.sctlr_ns) }, 6631 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, 6632 .raw_writefn = raw_write, 6633 }; 6634 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 6635 /* Normally we would always end the TB on an SCTLR write, but Linux 6636 * arch/arm/mach-pxa/sleep.S expects two instructions following 6637 * an MMU enable to execute from cache. Imitate this behaviour. 6638 */ 6639 sctlr.type |= ARM_CP_SUPPRESS_TB_END; 6640 } 6641 define_one_arm_cp_reg(cpu, &sctlr); 6642 } 6643 6644 if (cpu_isar_feature(aa64_lor, cpu)) { 6645 /* 6646 * A trivial implementation of ARMv8.1-LOR leaves all of these 6647 * registers fixed at 0, which indicates that there are zero 6648 * supported Limited Ordering regions. 6649 */ 6650 static const ARMCPRegInfo lor_reginfo[] = { 6651 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64, 6652 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0, 6653 .access = PL1_RW, .accessfn = access_lor_other, 6654 .type = ARM_CP_CONST, .resetvalue = 0 }, 6655 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64, 6656 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1, 6657 .access = PL1_RW, .accessfn = access_lor_other, 6658 .type = ARM_CP_CONST, .resetvalue = 0 }, 6659 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64, 6660 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2, 6661 .access = PL1_RW, .accessfn = access_lor_other, 6662 .type = ARM_CP_CONST, .resetvalue = 0 }, 6663 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64, 6664 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3, 6665 .access = PL1_RW, .accessfn = access_lor_other, 6666 .type = ARM_CP_CONST, .resetvalue = 0 }, 6667 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64, 6668 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7, 6669 .access = PL1_R, .accessfn = access_lorid, 6670 .type = ARM_CP_CONST, .resetvalue = 0 }, 6671 REGINFO_SENTINEL 6672 }; 6673 define_arm_cp_regs(cpu, lor_reginfo); 6674 } 6675 6676 if (cpu_isar_feature(aa64_sve, cpu)) { 6677 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo); 6678 if (arm_feature(env, ARM_FEATURE_EL2)) { 6679 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo); 6680 } else { 6681 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo); 6682 } 6683 if (arm_feature(env, ARM_FEATURE_EL3)) { 6684 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo); 6685 } 6686 } 6687 6688 #ifdef TARGET_AARCH64 6689 if (cpu_isar_feature(aa64_pauth, cpu)) { 6690 define_arm_cp_regs(cpu, pauth_reginfo); 6691 } 6692 #endif 6693 6694 /* 6695 * While all v8.0 cpus support aarch64, QEMU does have configurations 6696 * that do not set ID_AA64ISAR1, e.g. user-only qemu-arm -cpu max, 6697 * which will set ID_ISAR6. 6698 */ 6699 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) 6700 ? cpu_isar_feature(aa64_predinv, cpu) 6701 : cpu_isar_feature(aa32_predinv, cpu)) { 6702 define_arm_cp_regs(cpu, predinv_reginfo); 6703 } 6704 } 6705 6706 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) 6707 { 6708 CPUState *cs = CPU(cpu); 6709 CPUARMState *env = &cpu->env; 6710 6711 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 6712 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg, 6713 aarch64_fpu_gdb_set_reg, 6714 34, "aarch64-fpu.xml", 0); 6715 } else if (arm_feature(env, ARM_FEATURE_NEON)) { 6716 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 6717 51, "arm-neon.xml", 0); 6718 } else if (arm_feature(env, ARM_FEATURE_VFP3)) { 6719 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 6720 35, "arm-vfp3.xml", 0); 6721 } else if (arm_feature(env, ARM_FEATURE_VFP)) { 6722 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 6723 19, "arm-vfp.xml", 0); 6724 } 6725 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg, 6726 arm_gen_dynamic_xml(cs), 6727 "system-registers.xml", 0); 6728 } 6729 6730 /* Sort alphabetically by type name, except for "any". */ 6731 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) 6732 { 6733 ObjectClass *class_a = (ObjectClass *)a; 6734 ObjectClass *class_b = (ObjectClass *)b; 6735 const char *name_a, *name_b; 6736 6737 name_a = object_class_get_name(class_a); 6738 name_b = object_class_get_name(class_b); 6739 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) { 6740 return 1; 6741 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) { 6742 return -1; 6743 } else { 6744 return strcmp(name_a, name_b); 6745 } 6746 } 6747 6748 static void arm_cpu_list_entry(gpointer data, gpointer user_data) 6749 { 6750 ObjectClass *oc = data; 6751 const char *typename; 6752 char *name; 6753 6754 typename = object_class_get_name(oc); 6755 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU)); 6756 qemu_printf(" %s\n", name); 6757 g_free(name); 6758 } 6759 6760 void arm_cpu_list(void) 6761 { 6762 GSList *list; 6763 6764 list = object_class_get_list(TYPE_ARM_CPU, false); 6765 list = g_slist_sort(list, arm_cpu_list_compare); 6766 qemu_printf("Available CPUs:\n"); 6767 g_slist_foreach(list, arm_cpu_list_entry, NULL); 6768 g_slist_free(list); 6769 } 6770 6771 static void arm_cpu_add_definition(gpointer data, gpointer user_data) 6772 { 6773 ObjectClass *oc = data; 6774 CpuDefinitionInfoList **cpu_list = user_data; 6775 CpuDefinitionInfoList *entry; 6776 CpuDefinitionInfo *info; 6777 const char *typename; 6778 6779 typename = object_class_get_name(oc); 6780 info = g_malloc0(sizeof(*info)); 6781 info->name = g_strndup(typename, 6782 strlen(typename) - strlen("-" TYPE_ARM_CPU)); 6783 info->q_typename = g_strdup(typename); 6784 6785 entry = g_malloc0(sizeof(*entry)); 6786 entry->value = info; 6787 entry->next = *cpu_list; 6788 *cpu_list = entry; 6789 } 6790 6791 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp) 6792 { 6793 CpuDefinitionInfoList *cpu_list = NULL; 6794 GSList *list; 6795 6796 list = object_class_get_list(TYPE_ARM_CPU, false); 6797 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list); 6798 g_slist_free(list); 6799 6800 return cpu_list; 6801 } 6802 6803 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, 6804 void *opaque, int state, int secstate, 6805 int crm, int opc1, int opc2, 6806 const char *name) 6807 { 6808 /* Private utility function for define_one_arm_cp_reg_with_opaque(): 6809 * add a single reginfo struct to the hash table. 6810 */ 6811 uint32_t *key = g_new(uint32_t, 1); 6812 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); 6813 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; 6814 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0; 6815 6816 r2->name = g_strdup(name); 6817 /* Reset the secure state to the specific incoming state. This is 6818 * necessary as the register may have been defined with both states. 6819 */ 6820 r2->secure = secstate; 6821 6822 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 6823 /* Register is banked (using both entries in array). 6824 * Overwriting fieldoffset as the array is only used to define 6825 * banked registers but later only fieldoffset is used. 6826 */ 6827 r2->fieldoffset = r->bank_fieldoffsets[ns]; 6828 } 6829 6830 if (state == ARM_CP_STATE_AA32) { 6831 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 6832 /* If the register is banked then we don't need to migrate or 6833 * reset the 32-bit instance in certain cases: 6834 * 6835 * 1) If the register has both 32-bit and 64-bit instances then we 6836 * can count on the 64-bit instance taking care of the 6837 * non-secure bank. 6838 * 2) If ARMv8 is enabled then we can count on a 64-bit version 6839 * taking care of the secure bank. This requires that separate 6840 * 32 and 64-bit definitions are provided. 6841 */ 6842 if ((r->state == ARM_CP_STATE_BOTH && ns) || 6843 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) { 6844 r2->type |= ARM_CP_ALIAS; 6845 } 6846 } else if ((secstate != r->secure) && !ns) { 6847 /* The register is not banked so we only want to allow migration of 6848 * the non-secure instance. 6849 */ 6850 r2->type |= ARM_CP_ALIAS; 6851 } 6852 6853 if (r->state == ARM_CP_STATE_BOTH) { 6854 /* We assume it is a cp15 register if the .cp field is left unset. 6855 */ 6856 if (r2->cp == 0) { 6857 r2->cp = 15; 6858 } 6859 6860 #ifdef HOST_WORDS_BIGENDIAN 6861 if (r2->fieldoffset) { 6862 r2->fieldoffset += sizeof(uint32_t); 6863 } 6864 #endif 6865 } 6866 } 6867 if (state == ARM_CP_STATE_AA64) { 6868 /* To allow abbreviation of ARMCPRegInfo 6869 * definitions, we treat cp == 0 as equivalent to 6870 * the value for "standard guest-visible sysreg". 6871 * STATE_BOTH definitions are also always "standard 6872 * sysreg" in their AArch64 view (the .cp value may 6873 * be non-zero for the benefit of the AArch32 view). 6874 */ 6875 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) { 6876 r2->cp = CP_REG_ARM64_SYSREG_CP; 6877 } 6878 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm, 6879 r2->opc0, opc1, opc2); 6880 } else { 6881 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2); 6882 } 6883 if (opaque) { 6884 r2->opaque = opaque; 6885 } 6886 /* reginfo passed to helpers is correct for the actual access, 6887 * and is never ARM_CP_STATE_BOTH: 6888 */ 6889 r2->state = state; 6890 /* Make sure reginfo passed to helpers for wildcarded regs 6891 * has the correct crm/opc1/opc2 for this reg, not CP_ANY: 6892 */ 6893 r2->crm = crm; 6894 r2->opc1 = opc1; 6895 r2->opc2 = opc2; 6896 /* By convention, for wildcarded registers only the first 6897 * entry is used for migration; the others are marked as 6898 * ALIAS so we don't try to transfer the register 6899 * multiple times. Special registers (ie NOP/WFI) are 6900 * never migratable and not even raw-accessible. 6901 */ 6902 if ((r->type & ARM_CP_SPECIAL)) { 6903 r2->type |= ARM_CP_NO_RAW; 6904 } 6905 if (((r->crm == CP_ANY) && crm != 0) || 6906 ((r->opc1 == CP_ANY) && opc1 != 0) || 6907 ((r->opc2 == CP_ANY) && opc2 != 0)) { 6908 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB; 6909 } 6910 6911 /* Check that raw accesses are either forbidden or handled. Note that 6912 * we can't assert this earlier because the setup of fieldoffset for 6913 * banked registers has to be done first. 6914 */ 6915 if (!(r2->type & ARM_CP_NO_RAW)) { 6916 assert(!raw_accessors_invalid(r2)); 6917 } 6918 6919 /* Overriding of an existing definition must be explicitly 6920 * requested. 6921 */ 6922 if (!(r->type & ARM_CP_OVERRIDE)) { 6923 ARMCPRegInfo *oldreg; 6924 oldreg = g_hash_table_lookup(cpu->cp_regs, key); 6925 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { 6926 fprintf(stderr, "Register redefined: cp=%d %d bit " 6927 "crn=%d crm=%d opc1=%d opc2=%d, " 6928 "was %s, now %s\n", r2->cp, 32 + 32 * is64, 6929 r2->crn, r2->crm, r2->opc1, r2->opc2, 6930 oldreg->name, r2->name); 6931 g_assert_not_reached(); 6932 } 6933 } 6934 g_hash_table_insert(cpu->cp_regs, key, r2); 6935 } 6936 6937 6938 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, 6939 const ARMCPRegInfo *r, void *opaque) 6940 { 6941 /* Define implementations of coprocessor registers. 6942 * We store these in a hashtable because typically 6943 * there are less than 150 registers in a space which 6944 * is 16*16*16*8*8 = 262144 in size. 6945 * Wildcarding is supported for the crm, opc1 and opc2 fields. 6946 * If a register is defined twice then the second definition is 6947 * used, so this can be used to define some generic registers and 6948 * then override them with implementation specific variations. 6949 * At least one of the original and the second definition should 6950 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard 6951 * against accidental use. 6952 * 6953 * The state field defines whether the register is to be 6954 * visible in the AArch32 or AArch64 execution state. If the 6955 * state is set to ARM_CP_STATE_BOTH then we synthesise a 6956 * reginfo structure for the AArch32 view, which sees the lower 6957 * 32 bits of the 64 bit register. 6958 * 6959 * Only registers visible in AArch64 may set r->opc0; opc0 cannot 6960 * be wildcarded. AArch64 registers are always considered to be 64 6961 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of 6962 * the register, if any. 6963 */ 6964 int crm, opc1, opc2, state; 6965 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; 6966 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; 6967 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; 6968 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; 6969 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; 6970 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; 6971 /* 64 bit registers have only CRm and Opc1 fields */ 6972 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); 6973 /* op0 only exists in the AArch64 encodings */ 6974 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); 6975 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ 6976 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); 6977 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1 6978 * encodes a minimum access level for the register. We roll this 6979 * runtime check into our general permission check code, so check 6980 * here that the reginfo's specified permissions are strict enough 6981 * to encompass the generic architectural permission check. 6982 */ 6983 if (r->state != ARM_CP_STATE_AA32) { 6984 int mask = 0; 6985 switch (r->opc1) { 6986 case 0: 6987 /* min_EL EL1, but some accessible to EL0 via kernel ABI */ 6988 mask = PL0U_R | PL1_RW; 6989 break; 6990 case 1: case 2: 6991 /* min_EL EL1 */ 6992 mask = PL1_RW; 6993 break; 6994 case 3: 6995 /* min_EL EL0 */ 6996 mask = PL0_RW; 6997 break; 6998 case 4: 6999 /* min_EL EL2 */ 7000 mask = PL2_RW; 7001 break; 7002 case 5: 7003 /* unallocated encoding, so not possible */ 7004 assert(false); 7005 break; 7006 case 6: 7007 /* min_EL EL3 */ 7008 mask = PL3_RW; 7009 break; 7010 case 7: 7011 /* min_EL EL1, secure mode only (we don't check the latter) */ 7012 mask = PL1_RW; 7013 break; 7014 default: 7015 /* broken reginfo with out-of-range opc1 */ 7016 assert(false); 7017 break; 7018 } 7019 /* assert our permissions are not too lax (stricter is fine) */ 7020 assert((r->access & ~mask) == 0); 7021 } 7022 7023 /* Check that the register definition has enough info to handle 7024 * reads and writes if they are permitted. 7025 */ 7026 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { 7027 if (r->access & PL3_R) { 7028 assert((r->fieldoffset || 7029 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 7030 r->readfn); 7031 } 7032 if (r->access & PL3_W) { 7033 assert((r->fieldoffset || 7034 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 7035 r->writefn); 7036 } 7037 } 7038 /* Bad type field probably means missing sentinel at end of reg list */ 7039 assert(cptype_valid(r->type)); 7040 for (crm = crmmin; crm <= crmmax; crm++) { 7041 for (opc1 = opc1min; opc1 <= opc1max; opc1++) { 7042 for (opc2 = opc2min; opc2 <= opc2max; opc2++) { 7043 for (state = ARM_CP_STATE_AA32; 7044 state <= ARM_CP_STATE_AA64; state++) { 7045 if (r->state != state && r->state != ARM_CP_STATE_BOTH) { 7046 continue; 7047 } 7048 if (state == ARM_CP_STATE_AA32) { 7049 /* Under AArch32 CP registers can be common 7050 * (same for secure and non-secure world) or banked. 7051 */ 7052 char *name; 7053 7054 switch (r->secure) { 7055 case ARM_CP_SECSTATE_S: 7056 case ARM_CP_SECSTATE_NS: 7057 add_cpreg_to_hashtable(cpu, r, opaque, state, 7058 r->secure, crm, opc1, opc2, 7059 r->name); 7060 break; 7061 default: 7062 name = g_strdup_printf("%s_S", r->name); 7063 add_cpreg_to_hashtable(cpu, r, opaque, state, 7064 ARM_CP_SECSTATE_S, 7065 crm, opc1, opc2, name); 7066 g_free(name); 7067 add_cpreg_to_hashtable(cpu, r, opaque, state, 7068 ARM_CP_SECSTATE_NS, 7069 crm, opc1, opc2, r->name); 7070 break; 7071 } 7072 } else { 7073 /* AArch64 registers get mapped to non-secure instance 7074 * of AArch32 */ 7075 add_cpreg_to_hashtable(cpu, r, opaque, state, 7076 ARM_CP_SECSTATE_NS, 7077 crm, opc1, opc2, r->name); 7078 } 7079 } 7080 } 7081 } 7082 } 7083 } 7084 7085 void define_arm_cp_regs_with_opaque(ARMCPU *cpu, 7086 const ARMCPRegInfo *regs, void *opaque) 7087 { 7088 /* Define a whole list of registers */ 7089 const ARMCPRegInfo *r; 7090 for (r = regs; r->type != ARM_CP_SENTINEL; r++) { 7091 define_one_arm_cp_reg_with_opaque(cpu, r, opaque); 7092 } 7093 } 7094 7095 /* 7096 * Modify ARMCPRegInfo for access from userspace. 7097 * 7098 * This is a data driven modification directed by 7099 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as 7100 * user-space cannot alter any values and dynamic values pertaining to 7101 * execution state are hidden from user space view anyway. 7102 */ 7103 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods) 7104 { 7105 const ARMCPRegUserSpaceInfo *m; 7106 ARMCPRegInfo *r; 7107 7108 for (m = mods; m->name; m++) { 7109 GPatternSpec *pat = NULL; 7110 if (m->is_glob) { 7111 pat = g_pattern_spec_new(m->name); 7112 } 7113 for (r = regs; r->type != ARM_CP_SENTINEL; r++) { 7114 if (pat && g_pattern_match_string(pat, r->name)) { 7115 r->type = ARM_CP_CONST; 7116 r->access = PL0U_R; 7117 r->resetvalue = 0; 7118 /* continue */ 7119 } else if (strcmp(r->name, m->name) == 0) { 7120 r->type = ARM_CP_CONST; 7121 r->access = PL0U_R; 7122 r->resetvalue &= m->exported_bits; 7123 r->resetvalue |= m->fixed_bits; 7124 break; 7125 } 7126 } 7127 if (pat) { 7128 g_pattern_spec_free(pat); 7129 } 7130 } 7131 } 7132 7133 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) 7134 { 7135 return g_hash_table_lookup(cpregs, &encoded_cp); 7136 } 7137 7138 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, 7139 uint64_t value) 7140 { 7141 /* Helper coprocessor write function for write-ignore registers */ 7142 } 7143 7144 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) 7145 { 7146 /* Helper coprocessor write function for read-as-zero registers */ 7147 return 0; 7148 } 7149 7150 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) 7151 { 7152 /* Helper coprocessor reset function for do-nothing-on-reset registers */ 7153 } 7154 7155 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type) 7156 { 7157 /* Return true if it is not valid for us to switch to 7158 * this CPU mode (ie all the UNPREDICTABLE cases in 7159 * the ARM ARM CPSRWriteByInstr pseudocode). 7160 */ 7161 7162 /* Changes to or from Hyp via MSR and CPS are illegal. */ 7163 if (write_type == CPSRWriteByInstr && 7164 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP || 7165 mode == ARM_CPU_MODE_HYP)) { 7166 return 1; 7167 } 7168 7169 switch (mode) { 7170 case ARM_CPU_MODE_USR: 7171 return 0; 7172 case ARM_CPU_MODE_SYS: 7173 case ARM_CPU_MODE_SVC: 7174 case ARM_CPU_MODE_ABT: 7175 case ARM_CPU_MODE_UND: 7176 case ARM_CPU_MODE_IRQ: 7177 case ARM_CPU_MODE_FIQ: 7178 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7 7179 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.) 7180 */ 7181 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR 7182 * and CPS are treated as illegal mode changes. 7183 */ 7184 if (write_type == CPSRWriteByInstr && 7185 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON && 7186 (arm_hcr_el2_eff(env) & HCR_TGE)) { 7187 return 1; 7188 } 7189 return 0; 7190 case ARM_CPU_MODE_HYP: 7191 return !arm_feature(env, ARM_FEATURE_EL2) 7192 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env); 7193 case ARM_CPU_MODE_MON: 7194 return arm_current_el(env) < 3; 7195 default: 7196 return 1; 7197 } 7198 } 7199 7200 uint32_t cpsr_read(CPUARMState *env) 7201 { 7202 int ZF; 7203 ZF = (env->ZF == 0); 7204 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | 7205 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) 7206 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) 7207 | ((env->condexec_bits & 0xfc) << 8) 7208 | (env->GE << 16) | (env->daif & CPSR_AIF); 7209 } 7210 7211 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask, 7212 CPSRWriteType write_type) 7213 { 7214 uint32_t changed_daif; 7215 7216 if (mask & CPSR_NZCV) { 7217 env->ZF = (~val) & CPSR_Z; 7218 env->NF = val; 7219 env->CF = (val >> 29) & 1; 7220 env->VF = (val << 3) & 0x80000000; 7221 } 7222 if (mask & CPSR_Q) 7223 env->QF = ((val & CPSR_Q) != 0); 7224 if (mask & CPSR_T) 7225 env->thumb = ((val & CPSR_T) != 0); 7226 if (mask & CPSR_IT_0_1) { 7227 env->condexec_bits &= ~3; 7228 env->condexec_bits |= (val >> 25) & 3; 7229 } 7230 if (mask & CPSR_IT_2_7) { 7231 env->condexec_bits &= 3; 7232 env->condexec_bits |= (val >> 8) & 0xfc; 7233 } 7234 if (mask & CPSR_GE) { 7235 env->GE = (val >> 16) & 0xf; 7236 } 7237 7238 /* In a V7 implementation that includes the security extensions but does 7239 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control 7240 * whether non-secure software is allowed to change the CPSR_F and CPSR_A 7241 * bits respectively. 7242 * 7243 * In a V8 implementation, it is permitted for privileged software to 7244 * change the CPSR A/F bits regardless of the SCR.AW/FW bits. 7245 */ 7246 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) && 7247 arm_feature(env, ARM_FEATURE_EL3) && 7248 !arm_feature(env, ARM_FEATURE_EL2) && 7249 !arm_is_secure(env)) { 7250 7251 changed_daif = (env->daif ^ val) & mask; 7252 7253 if (changed_daif & CPSR_A) { 7254 /* Check to see if we are allowed to change the masking of async 7255 * abort exceptions from a non-secure state. 7256 */ 7257 if (!(env->cp15.scr_el3 & SCR_AW)) { 7258 qemu_log_mask(LOG_GUEST_ERROR, 7259 "Ignoring attempt to switch CPSR_A flag from " 7260 "non-secure world with SCR.AW bit clear\n"); 7261 mask &= ~CPSR_A; 7262 } 7263 } 7264 7265 if (changed_daif & CPSR_F) { 7266 /* Check to see if we are allowed to change the masking of FIQ 7267 * exceptions from a non-secure state. 7268 */ 7269 if (!(env->cp15.scr_el3 & SCR_FW)) { 7270 qemu_log_mask(LOG_GUEST_ERROR, 7271 "Ignoring attempt to switch CPSR_F flag from " 7272 "non-secure world with SCR.FW bit clear\n"); 7273 mask &= ~CPSR_F; 7274 } 7275 7276 /* Check whether non-maskable FIQ (NMFI) support is enabled. 7277 * If this bit is set software is not allowed to mask 7278 * FIQs, but is allowed to set CPSR_F to 0. 7279 */ 7280 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && 7281 (val & CPSR_F)) { 7282 qemu_log_mask(LOG_GUEST_ERROR, 7283 "Ignoring attempt to enable CPSR_F flag " 7284 "(non-maskable FIQ [NMFI] support enabled)\n"); 7285 mask &= ~CPSR_F; 7286 } 7287 } 7288 } 7289 7290 env->daif &= ~(CPSR_AIF & mask); 7291 env->daif |= val & CPSR_AIF & mask; 7292 7293 if (write_type != CPSRWriteRaw && 7294 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) { 7295 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) { 7296 /* Note that we can only get here in USR mode if this is a 7297 * gdb stub write; for this case we follow the architectural 7298 * behaviour for guest writes in USR mode of ignoring an attempt 7299 * to switch mode. (Those are caught by translate.c for writes 7300 * triggered by guest instructions.) 7301 */ 7302 mask &= ~CPSR_M; 7303 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) { 7304 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in 7305 * v7, and has defined behaviour in v8: 7306 * + leave CPSR.M untouched 7307 * + allow changes to the other CPSR fields 7308 * + set PSTATE.IL 7309 * For user changes via the GDB stub, we don't set PSTATE.IL, 7310 * as this would be unnecessarily harsh for a user error. 7311 */ 7312 mask &= ~CPSR_M; 7313 if (write_type != CPSRWriteByGDBStub && 7314 arm_feature(env, ARM_FEATURE_V8)) { 7315 mask |= CPSR_IL; 7316 val |= CPSR_IL; 7317 } 7318 qemu_log_mask(LOG_GUEST_ERROR, 7319 "Illegal AArch32 mode switch attempt from %s to %s\n", 7320 aarch32_mode_name(env->uncached_cpsr), 7321 aarch32_mode_name(val)); 7322 } else { 7323 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n", 7324 write_type == CPSRWriteExceptionReturn ? 7325 "Exception return from AArch32" : 7326 "AArch32 mode switch from", 7327 aarch32_mode_name(env->uncached_cpsr), 7328 aarch32_mode_name(val), env->regs[15]); 7329 switch_mode(env, val & CPSR_M); 7330 } 7331 } 7332 mask &= ~CACHED_CPSR_BITS; 7333 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); 7334 } 7335 7336 /* Sign/zero extend */ 7337 uint32_t HELPER(sxtb16)(uint32_t x) 7338 { 7339 uint32_t res; 7340 res = (uint16_t)(int8_t)x; 7341 res |= (uint32_t)(int8_t)(x >> 16) << 16; 7342 return res; 7343 } 7344 7345 uint32_t HELPER(uxtb16)(uint32_t x) 7346 { 7347 uint32_t res; 7348 res = (uint16_t)(uint8_t)x; 7349 res |= (uint32_t)(uint8_t)(x >> 16) << 16; 7350 return res; 7351 } 7352 7353 int32_t HELPER(sdiv)(int32_t num, int32_t den) 7354 { 7355 if (den == 0) 7356 return 0; 7357 if (num == INT_MIN && den == -1) 7358 return INT_MIN; 7359 return num / den; 7360 } 7361 7362 uint32_t HELPER(udiv)(uint32_t num, uint32_t den) 7363 { 7364 if (den == 0) 7365 return 0; 7366 return num / den; 7367 } 7368 7369 uint32_t HELPER(rbit)(uint32_t x) 7370 { 7371 return revbit32(x); 7372 } 7373 7374 #ifdef CONFIG_USER_ONLY 7375 7376 /* These should probably raise undefined insn exceptions. */ 7377 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) 7378 { 7379 ARMCPU *cpu = arm_env_get_cpu(env); 7380 7381 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg); 7382 } 7383 7384 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) 7385 { 7386 ARMCPU *cpu = arm_env_get_cpu(env); 7387 7388 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg); 7389 return 0; 7390 } 7391 7392 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) 7393 { 7394 /* translate.c should never generate calls here in user-only mode */ 7395 g_assert_not_reached(); 7396 } 7397 7398 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) 7399 { 7400 /* translate.c should never generate calls here in user-only mode */ 7401 g_assert_not_reached(); 7402 } 7403 7404 void HELPER(v7m_preserve_fp_state)(CPUARMState *env) 7405 { 7406 /* translate.c should never generate calls here in user-only mode */ 7407 g_assert_not_reached(); 7408 } 7409 7410 void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr) 7411 { 7412 /* translate.c should never generate calls here in user-only mode */ 7413 g_assert_not_reached(); 7414 } 7415 7416 void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr) 7417 { 7418 /* translate.c should never generate calls here in user-only mode */ 7419 g_assert_not_reached(); 7420 } 7421 7422 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) 7423 { 7424 /* The TT instructions can be used by unprivileged code, but in 7425 * user-only emulation we don't have the MPU. 7426 * Luckily since we know we are NonSecure unprivileged (and that in 7427 * turn means that the A flag wasn't specified), all the bits in the 7428 * register must be zero: 7429 * IREGION: 0 because IRVALID is 0 7430 * IRVALID: 0 because NS 7431 * S: 0 because NS 7432 * NSRW: 0 because NS 7433 * NSR: 0 because NS 7434 * RW: 0 because unpriv and A flag not set 7435 * R: 0 because unpriv and A flag not set 7436 * SRVALID: 0 because NS 7437 * MRVALID: 0 because unpriv and A flag not set 7438 * SREGION: 0 becaus SRVALID is 0 7439 * MREGION: 0 because MRVALID is 0 7440 */ 7441 return 0; 7442 } 7443 7444 static void switch_mode(CPUARMState *env, int mode) 7445 { 7446 ARMCPU *cpu = arm_env_get_cpu(env); 7447 7448 if (mode != ARM_CPU_MODE_USR) { 7449 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); 7450 } 7451 } 7452 7453 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 7454 uint32_t cur_el, bool secure) 7455 { 7456 return 1; 7457 } 7458 7459 void aarch64_sync_64_to_32(CPUARMState *env) 7460 { 7461 g_assert_not_reached(); 7462 } 7463 7464 #else 7465 7466 static void switch_mode(CPUARMState *env, int mode) 7467 { 7468 int old_mode; 7469 int i; 7470 7471 old_mode = env->uncached_cpsr & CPSR_M; 7472 if (mode == old_mode) 7473 return; 7474 7475 if (old_mode == ARM_CPU_MODE_FIQ) { 7476 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); 7477 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); 7478 } else if (mode == ARM_CPU_MODE_FIQ) { 7479 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); 7480 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); 7481 } 7482 7483 i = bank_number(old_mode); 7484 env->banked_r13[i] = env->regs[13]; 7485 env->banked_spsr[i] = env->spsr; 7486 7487 i = bank_number(mode); 7488 env->regs[13] = env->banked_r13[i]; 7489 env->spsr = env->banked_spsr[i]; 7490 7491 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14]; 7492 env->regs[14] = env->banked_r14[r14_bank_number(mode)]; 7493 } 7494 7495 /* Physical Interrupt Target EL Lookup Table 7496 * 7497 * [ From ARM ARM section G1.13.4 (Table G1-15) ] 7498 * 7499 * The below multi-dimensional table is used for looking up the target 7500 * exception level given numerous condition criteria. Specifically, the 7501 * target EL is based on SCR and HCR routing controls as well as the 7502 * currently executing EL and secure state. 7503 * 7504 * Dimensions: 7505 * target_el_table[2][2][2][2][2][4] 7506 * | | | | | +--- Current EL 7507 * | | | | +------ Non-secure(0)/Secure(1) 7508 * | | | +--------- HCR mask override 7509 * | | +------------ SCR exec state control 7510 * | +--------------- SCR mask override 7511 * +------------------ 32-bit(0)/64-bit(1) EL3 7512 * 7513 * The table values are as such: 7514 * 0-3 = EL0-EL3 7515 * -1 = Cannot occur 7516 * 7517 * The ARM ARM target EL table includes entries indicating that an "exception 7518 * is not taken". The two cases where this is applicable are: 7519 * 1) An exception is taken from EL3 but the SCR does not have the exception 7520 * routed to EL3. 7521 * 2) An exception is taken from EL2 but the HCR does not have the exception 7522 * routed to EL2. 7523 * In these two cases, the below table contain a target of EL1. This value is 7524 * returned as it is expected that the consumer of the table data will check 7525 * for "target EL >= current EL" to ensure the exception is not taken. 7526 * 7527 * SCR HCR 7528 * 64 EA AMO From 7529 * BIT IRQ IMO Non-secure Secure 7530 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 7531 */ 7532 static const int8_t target_el_table[2][2][2][2][2][4] = { 7533 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 7534 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, 7535 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 7536 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, 7537 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 7538 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, 7539 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 7540 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, 7541 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, 7542 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},}, 7543 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },}, 7544 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},}, 7545 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 7546 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, 7547 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 7548 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},}, 7549 }; 7550 7551 /* 7552 * Determine the target EL for physical exceptions 7553 */ 7554 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 7555 uint32_t cur_el, bool secure) 7556 { 7557 CPUARMState *env = cs->env_ptr; 7558 bool rw; 7559 bool scr; 7560 bool hcr; 7561 int target_el; 7562 /* Is the highest EL AArch64? */ 7563 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64); 7564 uint64_t hcr_el2; 7565 7566 if (arm_feature(env, ARM_FEATURE_EL3)) { 7567 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW); 7568 } else { 7569 /* Either EL2 is the highest EL (and so the EL2 register width 7570 * is given by is64); or there is no EL2 or EL3, in which case 7571 * the value of 'rw' does not affect the table lookup anyway. 7572 */ 7573 rw = is64; 7574 } 7575 7576 hcr_el2 = arm_hcr_el2_eff(env); 7577 switch (excp_idx) { 7578 case EXCP_IRQ: 7579 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); 7580 hcr = hcr_el2 & HCR_IMO; 7581 break; 7582 case EXCP_FIQ: 7583 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); 7584 hcr = hcr_el2 & HCR_FMO; 7585 break; 7586 default: 7587 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); 7588 hcr = hcr_el2 & HCR_AMO; 7589 break; 7590 }; 7591 7592 /* Perform a table-lookup for the target EL given the current state */ 7593 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; 7594 7595 assert(target_el > 0); 7596 7597 return target_el; 7598 } 7599 7600 /* 7601 * Return true if the v7M CPACR permits access to the FPU for the specified 7602 * security state and privilege level. 7603 */ 7604 static bool v7m_cpacr_pass(CPUARMState *env, bool is_secure, bool is_priv) 7605 { 7606 switch (extract32(env->v7m.cpacr[is_secure], 20, 2)) { 7607 case 0: 7608 case 2: /* UNPREDICTABLE: we treat like 0 */ 7609 return false; 7610 case 1: 7611 return is_priv; 7612 case 3: 7613 return true; 7614 default: 7615 g_assert_not_reached(); 7616 } 7617 } 7618 7619 /* 7620 * What kind of stack write are we doing? This affects how exceptions 7621 * generated during the stacking are treated. 7622 */ 7623 typedef enum StackingMode { 7624 STACK_NORMAL, 7625 STACK_IGNFAULTS, 7626 STACK_LAZYFP, 7627 } StackingMode; 7628 7629 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value, 7630 ARMMMUIdx mmu_idx, StackingMode mode) 7631 { 7632 CPUState *cs = CPU(cpu); 7633 CPUARMState *env = &cpu->env; 7634 MemTxAttrs attrs = {}; 7635 MemTxResult txres; 7636 target_ulong page_size; 7637 hwaddr physaddr; 7638 int prot; 7639 ARMMMUFaultInfo fi = {}; 7640 bool secure = mmu_idx & ARM_MMU_IDX_M_S; 7641 int exc; 7642 bool exc_secure; 7643 7644 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr, 7645 &attrs, &prot, &page_size, &fi, NULL)) { 7646 /* MPU/SAU lookup failed */ 7647 if (fi.type == ARMFault_QEMU_SFault) { 7648 if (mode == STACK_LAZYFP) { 7649 qemu_log_mask(CPU_LOG_INT, 7650 "...SecureFault with SFSR.LSPERR " 7651 "during lazy stacking\n"); 7652 env->v7m.sfsr |= R_V7M_SFSR_LSPERR_MASK; 7653 } else { 7654 qemu_log_mask(CPU_LOG_INT, 7655 "...SecureFault with SFSR.AUVIOL " 7656 "during stacking\n"); 7657 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK; 7658 } 7659 env->v7m.sfsr |= R_V7M_SFSR_SFARVALID_MASK; 7660 env->v7m.sfar = addr; 7661 exc = ARMV7M_EXCP_SECURE; 7662 exc_secure = false; 7663 } else { 7664 if (mode == STACK_LAZYFP) { 7665 qemu_log_mask(CPU_LOG_INT, 7666 "...MemManageFault with CFSR.MLSPERR\n"); 7667 env->v7m.cfsr[secure] |= R_V7M_CFSR_MLSPERR_MASK; 7668 } else { 7669 qemu_log_mask(CPU_LOG_INT, 7670 "...MemManageFault with CFSR.MSTKERR\n"); 7671 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK; 7672 } 7673 exc = ARMV7M_EXCP_MEM; 7674 exc_secure = secure; 7675 } 7676 goto pend_fault; 7677 } 7678 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value, 7679 attrs, &txres); 7680 if (txres != MEMTX_OK) { 7681 /* BusFault trying to write the data */ 7682 if (mode == STACK_LAZYFP) { 7683 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.LSPERR\n"); 7684 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_LSPERR_MASK; 7685 } else { 7686 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n"); 7687 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK; 7688 } 7689 exc = ARMV7M_EXCP_BUS; 7690 exc_secure = false; 7691 goto pend_fault; 7692 } 7693 return true; 7694 7695 pend_fault: 7696 /* By pending the exception at this point we are making 7697 * the IMPDEF choice "overridden exceptions pended" (see the 7698 * MergeExcInfo() pseudocode). The other choice would be to not 7699 * pend them now and then make a choice about which to throw away 7700 * later if we have two derived exceptions. 7701 * The only case when we must not pend the exception but instead 7702 * throw it away is if we are doing the push of the callee registers 7703 * and we've already generated a derived exception (this is indicated 7704 * by the caller passing STACK_IGNFAULTS). Even in this case we will 7705 * still update the fault status registers. 7706 */ 7707 switch (mode) { 7708 case STACK_NORMAL: 7709 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure); 7710 break; 7711 case STACK_LAZYFP: 7712 armv7m_nvic_set_pending_lazyfp(env->nvic, exc, exc_secure); 7713 break; 7714 case STACK_IGNFAULTS: 7715 break; 7716 } 7717 return false; 7718 } 7719 7720 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr, 7721 ARMMMUIdx mmu_idx) 7722 { 7723 CPUState *cs = CPU(cpu); 7724 CPUARMState *env = &cpu->env; 7725 MemTxAttrs attrs = {}; 7726 MemTxResult txres; 7727 target_ulong page_size; 7728 hwaddr physaddr; 7729 int prot; 7730 ARMMMUFaultInfo fi = {}; 7731 bool secure = mmu_idx & ARM_MMU_IDX_M_S; 7732 int exc; 7733 bool exc_secure; 7734 uint32_t value; 7735 7736 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr, 7737 &attrs, &prot, &page_size, &fi, NULL)) { 7738 /* MPU/SAU lookup failed */ 7739 if (fi.type == ARMFault_QEMU_SFault) { 7740 qemu_log_mask(CPU_LOG_INT, 7741 "...SecureFault with SFSR.AUVIOL during unstack\n"); 7742 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK; 7743 env->v7m.sfar = addr; 7744 exc = ARMV7M_EXCP_SECURE; 7745 exc_secure = false; 7746 } else { 7747 qemu_log_mask(CPU_LOG_INT, 7748 "...MemManageFault with CFSR.MUNSTKERR\n"); 7749 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK; 7750 exc = ARMV7M_EXCP_MEM; 7751 exc_secure = secure; 7752 } 7753 goto pend_fault; 7754 } 7755 7756 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr, 7757 attrs, &txres); 7758 if (txres != MEMTX_OK) { 7759 /* BusFault trying to read the data */ 7760 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n"); 7761 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK; 7762 exc = ARMV7M_EXCP_BUS; 7763 exc_secure = false; 7764 goto pend_fault; 7765 } 7766 7767 *dest = value; 7768 return true; 7769 7770 pend_fault: 7771 /* By pending the exception at this point we are making 7772 * the IMPDEF choice "overridden exceptions pended" (see the 7773 * MergeExcInfo() pseudocode). The other choice would be to not 7774 * pend them now and then make a choice about which to throw away 7775 * later if we have two derived exceptions. 7776 */ 7777 armv7m_nvic_set_pending(env->nvic, exc, exc_secure); 7778 return false; 7779 } 7780 7781 void HELPER(v7m_preserve_fp_state)(CPUARMState *env) 7782 { 7783 /* 7784 * Preserve FP state (because LSPACT was set and we are about 7785 * to execute an FP instruction). This corresponds to the 7786 * PreserveFPState() pseudocode. 7787 * We may throw an exception if the stacking fails. 7788 */ 7789 ARMCPU *cpu = arm_env_get_cpu(env); 7790 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 7791 bool negpri = !(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_HFRDY_MASK); 7792 bool is_priv = !(env->v7m.fpccr[is_secure] & R_V7M_FPCCR_USER_MASK); 7793 bool splimviol = env->v7m.fpccr[is_secure] & R_V7M_FPCCR_SPLIMVIOL_MASK; 7794 uint32_t fpcar = env->v7m.fpcar[is_secure]; 7795 bool stacked_ok = true; 7796 bool ts = is_secure && (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK); 7797 bool take_exception; 7798 7799 /* Take the iothread lock as we are going to touch the NVIC */ 7800 qemu_mutex_lock_iothread(); 7801 7802 /* Check the background context had access to the FPU */ 7803 if (!v7m_cpacr_pass(env, is_secure, is_priv)) { 7804 armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, is_secure); 7805 env->v7m.cfsr[is_secure] |= R_V7M_CFSR_NOCP_MASK; 7806 stacked_ok = false; 7807 } else if (!is_secure && !extract32(env->v7m.nsacr, 10, 1)) { 7808 armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S); 7809 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK; 7810 stacked_ok = false; 7811 } 7812 7813 if (!splimviol && stacked_ok) { 7814 /* We only stack if the stack limit wasn't violated */ 7815 int i; 7816 ARMMMUIdx mmu_idx; 7817 7818 mmu_idx = arm_v7m_mmu_idx_all(env, is_secure, is_priv, negpri); 7819 for (i = 0; i < (ts ? 32 : 16); i += 2) { 7820 uint64_t dn = *aa32_vfp_dreg(env, i / 2); 7821 uint32_t faddr = fpcar + 4 * i; 7822 uint32_t slo = extract64(dn, 0, 32); 7823 uint32_t shi = extract64(dn, 32, 32); 7824 7825 if (i >= 16) { 7826 faddr += 8; /* skip the slot for the FPSCR */ 7827 } 7828 stacked_ok = stacked_ok && 7829 v7m_stack_write(cpu, faddr, slo, mmu_idx, STACK_LAZYFP) && 7830 v7m_stack_write(cpu, faddr + 4, shi, mmu_idx, STACK_LAZYFP); 7831 } 7832 7833 stacked_ok = stacked_ok && 7834 v7m_stack_write(cpu, fpcar + 0x40, 7835 vfp_get_fpscr(env), mmu_idx, STACK_LAZYFP); 7836 } 7837 7838 /* 7839 * We definitely pended an exception, but it's possible that it 7840 * might not be able to be taken now. If its priority permits us 7841 * to take it now, then we must not update the LSPACT or FP regs, 7842 * but instead jump out to take the exception immediately. 7843 * If it's just pending and won't be taken until the current 7844 * handler exits, then we do update LSPACT and the FP regs. 7845 */ 7846 take_exception = !stacked_ok && 7847 armv7m_nvic_can_take_pending_exception(env->nvic); 7848 7849 qemu_mutex_unlock_iothread(); 7850 7851 if (take_exception) { 7852 raise_exception_ra(env, EXCP_LAZYFP, 0, 1, GETPC()); 7853 } 7854 7855 env->v7m.fpccr[is_secure] &= ~R_V7M_FPCCR_LSPACT_MASK; 7856 7857 if (ts) { 7858 /* Clear s0 to s31 and the FPSCR */ 7859 int i; 7860 7861 for (i = 0; i < 32; i += 2) { 7862 *aa32_vfp_dreg(env, i / 2) = 0; 7863 } 7864 vfp_set_fpscr(env, 0); 7865 } 7866 /* 7867 * Otherwise s0 to s15 and FPSCR are UNKNOWN; we choose to leave them 7868 * unchanged. 7869 */ 7870 } 7871 7872 /* Write to v7M CONTROL.SPSEL bit for the specified security bank. 7873 * This may change the current stack pointer between Main and Process 7874 * stack pointers if it is done for the CONTROL register for the current 7875 * security state. 7876 */ 7877 static void write_v7m_control_spsel_for_secstate(CPUARMState *env, 7878 bool new_spsel, 7879 bool secstate) 7880 { 7881 bool old_is_psp = v7m_using_psp(env); 7882 7883 env->v7m.control[secstate] = 7884 deposit32(env->v7m.control[secstate], 7885 R_V7M_CONTROL_SPSEL_SHIFT, 7886 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel); 7887 7888 if (secstate == env->v7m.secure) { 7889 bool new_is_psp = v7m_using_psp(env); 7890 uint32_t tmp; 7891 7892 if (old_is_psp != new_is_psp) { 7893 tmp = env->v7m.other_sp; 7894 env->v7m.other_sp = env->regs[13]; 7895 env->regs[13] = tmp; 7896 } 7897 } 7898 } 7899 7900 /* Write to v7M CONTROL.SPSEL bit. This may change the current 7901 * stack pointer between Main and Process stack pointers. 7902 */ 7903 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel) 7904 { 7905 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure); 7906 } 7907 7908 void write_v7m_exception(CPUARMState *env, uint32_t new_exc) 7909 { 7910 /* Write a new value to v7m.exception, thus transitioning into or out 7911 * of Handler mode; this may result in a change of active stack pointer. 7912 */ 7913 bool new_is_psp, old_is_psp = v7m_using_psp(env); 7914 uint32_t tmp; 7915 7916 env->v7m.exception = new_exc; 7917 7918 new_is_psp = v7m_using_psp(env); 7919 7920 if (old_is_psp != new_is_psp) { 7921 tmp = env->v7m.other_sp; 7922 env->v7m.other_sp = env->regs[13]; 7923 env->regs[13] = tmp; 7924 } 7925 } 7926 7927 /* Switch M profile security state between NS and S */ 7928 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate) 7929 { 7930 uint32_t new_ss_msp, new_ss_psp; 7931 7932 if (env->v7m.secure == new_secstate) { 7933 return; 7934 } 7935 7936 /* All the banked state is accessed by looking at env->v7m.secure 7937 * except for the stack pointer; rearrange the SP appropriately. 7938 */ 7939 new_ss_msp = env->v7m.other_ss_msp; 7940 new_ss_psp = env->v7m.other_ss_psp; 7941 7942 if (v7m_using_psp(env)) { 7943 env->v7m.other_ss_psp = env->regs[13]; 7944 env->v7m.other_ss_msp = env->v7m.other_sp; 7945 } else { 7946 env->v7m.other_ss_msp = env->regs[13]; 7947 env->v7m.other_ss_psp = env->v7m.other_sp; 7948 } 7949 7950 env->v7m.secure = new_secstate; 7951 7952 if (v7m_using_psp(env)) { 7953 env->regs[13] = new_ss_psp; 7954 env->v7m.other_sp = new_ss_msp; 7955 } else { 7956 env->regs[13] = new_ss_msp; 7957 env->v7m.other_sp = new_ss_psp; 7958 } 7959 } 7960 7961 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) 7962 { 7963 /* Handle v7M BXNS: 7964 * - if the return value is a magic value, do exception return (like BX) 7965 * - otherwise bit 0 of the return value is the target security state 7966 */ 7967 uint32_t min_magic; 7968 7969 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 7970 /* Covers FNC_RETURN and EXC_RETURN magic */ 7971 min_magic = FNC_RETURN_MIN_MAGIC; 7972 } else { 7973 /* EXC_RETURN magic only */ 7974 min_magic = EXC_RETURN_MIN_MAGIC; 7975 } 7976 7977 if (dest >= min_magic) { 7978 /* This is an exception return magic value; put it where 7979 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT. 7980 * Note that if we ever add gen_ss_advance() singlestep support to 7981 * M profile this should count as an "instruction execution complete" 7982 * event (compare gen_bx_excret_final_code()). 7983 */ 7984 env->regs[15] = dest & ~1; 7985 env->thumb = dest & 1; 7986 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT); 7987 /* notreached */ 7988 } 7989 7990 /* translate.c should have made BXNS UNDEF unless we're secure */ 7991 assert(env->v7m.secure); 7992 7993 if (!(dest & 1)) { 7994 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 7995 } 7996 switch_v7m_security_state(env, dest & 1); 7997 env->thumb = 1; 7998 env->regs[15] = dest & ~1; 7999 } 8000 8001 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) 8002 { 8003 /* Handle v7M BLXNS: 8004 * - bit 0 of the destination address is the target security state 8005 */ 8006 8007 /* At this point regs[15] is the address just after the BLXNS */ 8008 uint32_t nextinst = env->regs[15] | 1; 8009 uint32_t sp = env->regs[13] - 8; 8010 uint32_t saved_psr; 8011 8012 /* translate.c will have made BLXNS UNDEF unless we're secure */ 8013 assert(env->v7m.secure); 8014 8015 if (dest & 1) { 8016 /* target is Secure, so this is just a normal BLX, 8017 * except that the low bit doesn't indicate Thumb/not. 8018 */ 8019 env->regs[14] = nextinst; 8020 env->thumb = 1; 8021 env->regs[15] = dest & ~1; 8022 return; 8023 } 8024 8025 /* Target is non-secure: first push a stack frame */ 8026 if (!QEMU_IS_ALIGNED(sp, 8)) { 8027 qemu_log_mask(LOG_GUEST_ERROR, 8028 "BLXNS with misaligned SP is UNPREDICTABLE\n"); 8029 } 8030 8031 if (sp < v7m_sp_limit(env)) { 8032 raise_exception(env, EXCP_STKOF, 0, 1); 8033 } 8034 8035 saved_psr = env->v7m.exception; 8036 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) { 8037 saved_psr |= XPSR_SFPA; 8038 } 8039 8040 /* Note that these stores can throw exceptions on MPU faults */ 8041 cpu_stl_data(env, sp, nextinst); 8042 cpu_stl_data(env, sp + 4, saved_psr); 8043 8044 env->regs[13] = sp; 8045 env->regs[14] = 0xfeffffff; 8046 if (arm_v7m_is_handler_mode(env)) { 8047 /* Write a dummy value to IPSR, to avoid leaking the current secure 8048 * exception number to non-secure code. This is guaranteed not 8049 * to cause write_v7m_exception() to actually change stacks. 8050 */ 8051 write_v7m_exception(env, 1); 8052 } 8053 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 8054 switch_v7m_security_state(env, 0); 8055 env->thumb = 1; 8056 env->regs[15] = dest; 8057 } 8058 8059 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode, 8060 bool spsel) 8061 { 8062 /* Return a pointer to the location where we currently store the 8063 * stack pointer for the requested security state and thread mode. 8064 * This pointer will become invalid if the CPU state is updated 8065 * such that the stack pointers are switched around (eg changing 8066 * the SPSEL control bit). 8067 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode(). 8068 * Unlike that pseudocode, we require the caller to pass us in the 8069 * SPSEL control bit value; this is because we also use this 8070 * function in handling of pushing of the callee-saves registers 8071 * part of the v8M stack frame (pseudocode PushCalleeStack()), 8072 * and in the tailchain codepath the SPSEL bit comes from the exception 8073 * return magic LR value from the previous exception. The pseudocode 8074 * opencodes the stack-selection in PushCalleeStack(), but we prefer 8075 * to make this utility function generic enough to do the job. 8076 */ 8077 bool want_psp = threadmode && spsel; 8078 8079 if (secure == env->v7m.secure) { 8080 if (want_psp == v7m_using_psp(env)) { 8081 return &env->regs[13]; 8082 } else { 8083 return &env->v7m.other_sp; 8084 } 8085 } else { 8086 if (want_psp) { 8087 return &env->v7m.other_ss_psp; 8088 } else { 8089 return &env->v7m.other_ss_msp; 8090 } 8091 } 8092 } 8093 8094 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure, 8095 uint32_t *pvec) 8096 { 8097 CPUState *cs = CPU(cpu); 8098 CPUARMState *env = &cpu->env; 8099 MemTxResult result; 8100 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4; 8101 uint32_t vector_entry; 8102 MemTxAttrs attrs = {}; 8103 ARMMMUIdx mmu_idx; 8104 bool exc_secure; 8105 8106 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true); 8107 8108 /* We don't do a get_phys_addr() here because the rules for vector 8109 * loads are special: they always use the default memory map, and 8110 * the default memory map permits reads from all addresses. 8111 * Since there's no easy way to pass through to pmsav8_mpu_lookup() 8112 * that we want this special case which would always say "yes", 8113 * we just do the SAU lookup here followed by a direct physical load. 8114 */ 8115 attrs.secure = targets_secure; 8116 attrs.user = false; 8117 8118 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8119 V8M_SAttributes sattrs = {}; 8120 8121 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); 8122 if (sattrs.ns) { 8123 attrs.secure = false; 8124 } else if (!targets_secure) { 8125 /* NS access to S memory */ 8126 goto load_fail; 8127 } 8128 } 8129 8130 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr, 8131 attrs, &result); 8132 if (result != MEMTX_OK) { 8133 goto load_fail; 8134 } 8135 *pvec = vector_entry; 8136 return true; 8137 8138 load_fail: 8139 /* All vector table fetch fails are reported as HardFault, with 8140 * HFSR.VECTTBL and .FORCED set. (FORCED is set because 8141 * technically the underlying exception is a MemManage or BusFault 8142 * that is escalated to HardFault.) This is a terminal exception, 8143 * so we will either take the HardFault immediately or else enter 8144 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()). 8145 */ 8146 exc_secure = targets_secure || 8147 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK); 8148 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK; 8149 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure); 8150 return false; 8151 } 8152 8153 static uint32_t v7m_integrity_sig(CPUARMState *env, uint32_t lr) 8154 { 8155 /* 8156 * Return the integrity signature value for the callee-saves 8157 * stack frame section. @lr is the exception return payload/LR value 8158 * whose FType bit forms bit 0 of the signature if FP is present. 8159 */ 8160 uint32_t sig = 0xfefa125a; 8161 8162 if (!arm_feature(env, ARM_FEATURE_VFP) || (lr & R_V7M_EXCRET_FTYPE_MASK)) { 8163 sig |= 1; 8164 } 8165 return sig; 8166 } 8167 8168 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain, 8169 bool ignore_faults) 8170 { 8171 /* For v8M, push the callee-saves register part of the stack frame. 8172 * Compare the v8M pseudocode PushCalleeStack(). 8173 * In the tailchaining case this may not be the current stack. 8174 */ 8175 CPUARMState *env = &cpu->env; 8176 uint32_t *frame_sp_p; 8177 uint32_t frameptr; 8178 ARMMMUIdx mmu_idx; 8179 bool stacked_ok; 8180 uint32_t limit; 8181 bool want_psp; 8182 uint32_t sig; 8183 StackingMode smode = ignore_faults ? STACK_IGNFAULTS : STACK_NORMAL; 8184 8185 if (dotailchain) { 8186 bool mode = lr & R_V7M_EXCRET_MODE_MASK; 8187 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) || 8188 !mode; 8189 8190 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv); 8191 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode, 8192 lr & R_V7M_EXCRET_SPSEL_MASK); 8193 want_psp = mode && (lr & R_V7M_EXCRET_SPSEL_MASK); 8194 if (want_psp) { 8195 limit = env->v7m.psplim[M_REG_S]; 8196 } else { 8197 limit = env->v7m.msplim[M_REG_S]; 8198 } 8199 } else { 8200 mmu_idx = arm_mmu_idx(env); 8201 frame_sp_p = &env->regs[13]; 8202 limit = v7m_sp_limit(env); 8203 } 8204 8205 frameptr = *frame_sp_p - 0x28; 8206 if (frameptr < limit) { 8207 /* 8208 * Stack limit failure: set SP to the limit value, and generate 8209 * STKOF UsageFault. Stack pushes below the limit must not be 8210 * performed. It is IMPDEF whether pushes above the limit are 8211 * performed; we choose not to. 8212 */ 8213 qemu_log_mask(CPU_LOG_INT, 8214 "...STKOF during callee-saves register stacking\n"); 8215 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 8216 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 8217 env->v7m.secure); 8218 *frame_sp_p = limit; 8219 return true; 8220 } 8221 8222 /* Write as much of the stack frame as we can. A write failure may 8223 * cause us to pend a derived exception. 8224 */ 8225 sig = v7m_integrity_sig(env, lr); 8226 stacked_ok = 8227 v7m_stack_write(cpu, frameptr, sig, mmu_idx, smode) && 8228 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx, smode) && 8229 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx, smode) && 8230 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx, smode) && 8231 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx, smode) && 8232 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx, smode) && 8233 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx, smode) && 8234 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx, smode) && 8235 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx, smode); 8236 8237 /* Update SP regardless of whether any of the stack accesses failed. */ 8238 *frame_sp_p = frameptr; 8239 8240 return !stacked_ok; 8241 } 8242 8243 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain, 8244 bool ignore_stackfaults) 8245 { 8246 /* Do the "take the exception" parts of exception entry, 8247 * but not the pushing of state to the stack. This is 8248 * similar to the pseudocode ExceptionTaken() function. 8249 */ 8250 CPUARMState *env = &cpu->env; 8251 uint32_t addr; 8252 bool targets_secure; 8253 int exc; 8254 bool push_failed = false; 8255 8256 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure); 8257 qemu_log_mask(CPU_LOG_INT, "...taking pending %s exception %d\n", 8258 targets_secure ? "secure" : "nonsecure", exc); 8259 8260 if (dotailchain) { 8261 /* Sanitize LR FType and PREFIX bits */ 8262 if (!arm_feature(env, ARM_FEATURE_VFP)) { 8263 lr |= R_V7M_EXCRET_FTYPE_MASK; 8264 } 8265 lr = deposit32(lr, 24, 8, 0xff); 8266 } 8267 8268 if (arm_feature(env, ARM_FEATURE_V8)) { 8269 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 8270 (lr & R_V7M_EXCRET_S_MASK)) { 8271 /* The background code (the owner of the registers in the 8272 * exception frame) is Secure. This means it may either already 8273 * have or now needs to push callee-saves registers. 8274 */ 8275 if (targets_secure) { 8276 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) { 8277 /* We took an exception from Secure to NonSecure 8278 * (which means the callee-saved registers got stacked) 8279 * and are now tailchaining to a Secure exception. 8280 * Clear DCRS so eventual return from this Secure 8281 * exception unstacks the callee-saved registers. 8282 */ 8283 lr &= ~R_V7M_EXCRET_DCRS_MASK; 8284 } 8285 } else { 8286 /* We're going to a non-secure exception; push the 8287 * callee-saves registers to the stack now, if they're 8288 * not already saved. 8289 */ 8290 if (lr & R_V7M_EXCRET_DCRS_MASK && 8291 !(dotailchain && !(lr & R_V7M_EXCRET_ES_MASK))) { 8292 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain, 8293 ignore_stackfaults); 8294 } 8295 lr |= R_V7M_EXCRET_DCRS_MASK; 8296 } 8297 } 8298 8299 lr &= ~R_V7M_EXCRET_ES_MASK; 8300 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8301 lr |= R_V7M_EXCRET_ES_MASK; 8302 } 8303 lr &= ~R_V7M_EXCRET_SPSEL_MASK; 8304 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) { 8305 lr |= R_V7M_EXCRET_SPSEL_MASK; 8306 } 8307 8308 /* Clear registers if necessary to prevent non-secure exception 8309 * code being able to see register values from secure code. 8310 * Where register values become architecturally UNKNOWN we leave 8311 * them with their previous values. 8312 */ 8313 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8314 if (!targets_secure) { 8315 /* Always clear the caller-saved registers (they have been 8316 * pushed to the stack earlier in v7m_push_stack()). 8317 * Clear callee-saved registers if the background code is 8318 * Secure (in which case these regs were saved in 8319 * v7m_push_callee_stack()). 8320 */ 8321 int i; 8322 8323 for (i = 0; i < 13; i++) { 8324 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */ 8325 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) { 8326 env->regs[i] = 0; 8327 } 8328 } 8329 /* Clear EAPSR */ 8330 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT); 8331 } 8332 } 8333 } 8334 8335 if (push_failed && !ignore_stackfaults) { 8336 /* Derived exception on callee-saves register stacking: 8337 * we might now want to take a different exception which 8338 * targets a different security state, so try again from the top. 8339 */ 8340 qemu_log_mask(CPU_LOG_INT, 8341 "...derived exception on callee-saves register stacking"); 8342 v7m_exception_taken(cpu, lr, true, true); 8343 return; 8344 } 8345 8346 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) { 8347 /* Vector load failed: derived exception */ 8348 qemu_log_mask(CPU_LOG_INT, "...derived exception on vector table load"); 8349 v7m_exception_taken(cpu, lr, true, true); 8350 return; 8351 } 8352 8353 /* Now we've done everything that might cause a derived exception 8354 * we can go ahead and activate whichever exception we're going to 8355 * take (which might now be the derived exception). 8356 */ 8357 armv7m_nvic_acknowledge_irq(env->nvic); 8358 8359 /* Switch to target security state -- must do this before writing SPSEL */ 8360 switch_v7m_security_state(env, targets_secure); 8361 write_v7m_control_spsel(env, 0); 8362 arm_clear_exclusive(env); 8363 /* Clear SFPA and FPCA (has no effect if no FPU) */ 8364 env->v7m.control[M_REG_S] &= 8365 ~(R_V7M_CONTROL_FPCA_MASK | R_V7M_CONTROL_SFPA_MASK); 8366 /* Clear IT bits */ 8367 env->condexec_bits = 0; 8368 env->regs[14] = lr; 8369 env->regs[15] = addr & 0xfffffffe; 8370 env->thumb = addr & 1; 8371 } 8372 8373 static void v7m_update_fpccr(CPUARMState *env, uint32_t frameptr, 8374 bool apply_splim) 8375 { 8376 /* 8377 * Like the pseudocode UpdateFPCCR: save state in FPCAR and FPCCR 8378 * that we will need later in order to do lazy FP reg stacking. 8379 */ 8380 bool is_secure = env->v7m.secure; 8381 void *nvic = env->nvic; 8382 /* 8383 * Some bits are unbanked and live always in fpccr[M_REG_S]; some bits 8384 * are banked and we want to update the bit in the bank for the 8385 * current security state; and in one case we want to specifically 8386 * update the NS banked version of a bit even if we are secure. 8387 */ 8388 uint32_t *fpccr_s = &env->v7m.fpccr[M_REG_S]; 8389 uint32_t *fpccr_ns = &env->v7m.fpccr[M_REG_NS]; 8390 uint32_t *fpccr = &env->v7m.fpccr[is_secure]; 8391 bool hfrdy, bfrdy, mmrdy, ns_ufrdy, s_ufrdy, sfrdy, monrdy; 8392 8393 env->v7m.fpcar[is_secure] = frameptr & ~0x7; 8394 8395 if (apply_splim && arm_feature(env, ARM_FEATURE_V8)) { 8396 bool splimviol; 8397 uint32_t splim = v7m_sp_limit(env); 8398 bool ign = armv7m_nvic_neg_prio_requested(nvic, is_secure) && 8399 (env->v7m.ccr[is_secure] & R_V7M_CCR_STKOFHFNMIGN_MASK); 8400 8401 splimviol = !ign && frameptr < splim; 8402 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, SPLIMVIOL, splimviol); 8403 } 8404 8405 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, LSPACT, 1); 8406 8407 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, S, is_secure); 8408 8409 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, USER, arm_current_el(env) == 0); 8410 8411 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, THREAD, 8412 !arm_v7m_is_handler_mode(env)); 8413 8414 hfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_HARD, false); 8415 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, HFRDY, hfrdy); 8416 8417 bfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_BUS, false); 8418 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, BFRDY, bfrdy); 8419 8420 mmrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_MEM, is_secure); 8421 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, MMRDY, mmrdy); 8422 8423 ns_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, false); 8424 *fpccr_ns = FIELD_DP32(*fpccr_ns, V7M_FPCCR, UFRDY, ns_ufrdy); 8425 8426 monrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_DEBUG, false); 8427 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, MONRDY, monrdy); 8428 8429 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8430 s_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, true); 8431 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, UFRDY, s_ufrdy); 8432 8433 sfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_SECURE, false); 8434 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, SFRDY, sfrdy); 8435 } 8436 } 8437 8438 void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr) 8439 { 8440 /* fptr is the value of Rn, the frame pointer we store the FP regs to */ 8441 bool s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 8442 bool lspact = env->v7m.fpccr[s] & R_V7M_FPCCR_LSPACT_MASK; 8443 8444 assert(env->v7m.secure); 8445 8446 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { 8447 return; 8448 } 8449 8450 /* Check access to the coprocessor is permitted */ 8451 if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) { 8452 raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC()); 8453 } 8454 8455 if (lspact) { 8456 /* LSPACT should not be active when there is active FP state */ 8457 raise_exception_ra(env, EXCP_LSERR, 0, 1, GETPC()); 8458 } 8459 8460 if (fptr & 7) { 8461 raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC()); 8462 } 8463 8464 /* 8465 * Note that we do not use v7m_stack_write() here, because the 8466 * accesses should not set the FSR bits for stacking errors if they 8467 * fail. (In pseudocode terms, they are AccType_NORMAL, not AccType_STACK 8468 * or AccType_LAZYFP). Faults in cpu_stl_data() will throw exceptions 8469 * and longjmp out. 8470 */ 8471 if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) { 8472 bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK; 8473 int i; 8474 8475 for (i = 0; i < (ts ? 32 : 16); i += 2) { 8476 uint64_t dn = *aa32_vfp_dreg(env, i / 2); 8477 uint32_t faddr = fptr + 4 * i; 8478 uint32_t slo = extract64(dn, 0, 32); 8479 uint32_t shi = extract64(dn, 32, 32); 8480 8481 if (i >= 16) { 8482 faddr += 8; /* skip the slot for the FPSCR */ 8483 } 8484 cpu_stl_data(env, faddr, slo); 8485 cpu_stl_data(env, faddr + 4, shi); 8486 } 8487 cpu_stl_data(env, fptr + 0x40, vfp_get_fpscr(env)); 8488 8489 /* 8490 * If TS is 0 then s0 to s15 and FPSCR are UNKNOWN; we choose to 8491 * leave them unchanged, matching our choice in v7m_preserve_fp_state. 8492 */ 8493 if (ts) { 8494 for (i = 0; i < 32; i += 2) { 8495 *aa32_vfp_dreg(env, i / 2) = 0; 8496 } 8497 vfp_set_fpscr(env, 0); 8498 } 8499 } else { 8500 v7m_update_fpccr(env, fptr, false); 8501 } 8502 8503 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; 8504 } 8505 8506 void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr) 8507 { 8508 /* fptr is the value of Rn, the frame pointer we load the FP regs from */ 8509 assert(env->v7m.secure); 8510 8511 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { 8512 return; 8513 } 8514 8515 /* Check access to the coprocessor is permitted */ 8516 if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) { 8517 raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC()); 8518 } 8519 8520 if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) { 8521 /* State in FP is still valid */ 8522 env->v7m.fpccr[M_REG_S] &= ~R_V7M_FPCCR_LSPACT_MASK; 8523 } else { 8524 bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK; 8525 int i; 8526 uint32_t fpscr; 8527 8528 if (fptr & 7) { 8529 raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC()); 8530 } 8531 8532 for (i = 0; i < (ts ? 32 : 16); i += 2) { 8533 uint32_t slo, shi; 8534 uint64_t dn; 8535 uint32_t faddr = fptr + 4 * i; 8536 8537 if (i >= 16) { 8538 faddr += 8; /* skip the slot for the FPSCR */ 8539 } 8540 8541 slo = cpu_ldl_data(env, faddr); 8542 shi = cpu_ldl_data(env, faddr + 4); 8543 8544 dn = (uint64_t) shi << 32 | slo; 8545 *aa32_vfp_dreg(env, i / 2) = dn; 8546 } 8547 fpscr = cpu_ldl_data(env, fptr + 0x40); 8548 vfp_set_fpscr(env, fpscr); 8549 } 8550 8551 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK; 8552 } 8553 8554 static bool v7m_push_stack(ARMCPU *cpu) 8555 { 8556 /* Do the "set up stack frame" part of exception entry, 8557 * similar to pseudocode PushStack(). 8558 * Return true if we generate a derived exception (and so 8559 * should ignore further stack faults trying to process 8560 * that derived exception.) 8561 */ 8562 bool stacked_ok = true, limitviol = false; 8563 CPUARMState *env = &cpu->env; 8564 uint32_t xpsr = xpsr_read(env); 8565 uint32_t frameptr = env->regs[13]; 8566 ARMMMUIdx mmu_idx = arm_mmu_idx(env); 8567 uint32_t framesize; 8568 bool nsacr_cp10 = extract32(env->v7m.nsacr, 10, 1); 8569 8570 if ((env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) && 8571 (env->v7m.secure || nsacr_cp10)) { 8572 if (env->v7m.secure && 8573 env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK) { 8574 framesize = 0xa8; 8575 } else { 8576 framesize = 0x68; 8577 } 8578 } else { 8579 framesize = 0x20; 8580 } 8581 8582 /* Align stack pointer if the guest wants that */ 8583 if ((frameptr & 4) && 8584 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) { 8585 frameptr -= 4; 8586 xpsr |= XPSR_SPREALIGN; 8587 } 8588 8589 xpsr &= ~XPSR_SFPA; 8590 if (env->v7m.secure && 8591 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { 8592 xpsr |= XPSR_SFPA; 8593 } 8594 8595 frameptr -= framesize; 8596 8597 if (arm_feature(env, ARM_FEATURE_V8)) { 8598 uint32_t limit = v7m_sp_limit(env); 8599 8600 if (frameptr < limit) { 8601 /* 8602 * Stack limit failure: set SP to the limit value, and generate 8603 * STKOF UsageFault. Stack pushes below the limit must not be 8604 * performed. It is IMPDEF whether pushes above the limit are 8605 * performed; we choose not to. 8606 */ 8607 qemu_log_mask(CPU_LOG_INT, 8608 "...STKOF during stacking\n"); 8609 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 8610 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 8611 env->v7m.secure); 8612 env->regs[13] = limit; 8613 /* 8614 * We won't try to perform any further memory accesses but 8615 * we must continue through the following code to check for 8616 * permission faults during FPU state preservation, and we 8617 * must update FPCCR if lazy stacking is enabled. 8618 */ 8619 limitviol = true; 8620 stacked_ok = false; 8621 } 8622 } 8623 8624 /* Write as much of the stack frame as we can. If we fail a stack 8625 * write this will result in a derived exception being pended 8626 * (which may be taken in preference to the one we started with 8627 * if it has higher priority). 8628 */ 8629 stacked_ok = stacked_ok && 8630 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, STACK_NORMAL) && 8631 v7m_stack_write(cpu, frameptr + 4, env->regs[1], 8632 mmu_idx, STACK_NORMAL) && 8633 v7m_stack_write(cpu, frameptr + 8, env->regs[2], 8634 mmu_idx, STACK_NORMAL) && 8635 v7m_stack_write(cpu, frameptr + 12, env->regs[3], 8636 mmu_idx, STACK_NORMAL) && 8637 v7m_stack_write(cpu, frameptr + 16, env->regs[12], 8638 mmu_idx, STACK_NORMAL) && 8639 v7m_stack_write(cpu, frameptr + 20, env->regs[14], 8640 mmu_idx, STACK_NORMAL) && 8641 v7m_stack_write(cpu, frameptr + 24, env->regs[15], 8642 mmu_idx, STACK_NORMAL) && 8643 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, STACK_NORMAL); 8644 8645 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) { 8646 /* FPU is active, try to save its registers */ 8647 bool fpccr_s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 8648 bool lspact = env->v7m.fpccr[fpccr_s] & R_V7M_FPCCR_LSPACT_MASK; 8649 8650 if (lspact && arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8651 qemu_log_mask(CPU_LOG_INT, 8652 "...SecureFault because LSPACT and FPCA both set\n"); 8653 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; 8654 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 8655 } else if (!env->v7m.secure && !nsacr_cp10) { 8656 qemu_log_mask(CPU_LOG_INT, 8657 "...Secure UsageFault with CFSR.NOCP because " 8658 "NSACR.CP10 prevents stacking FP regs\n"); 8659 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S); 8660 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK; 8661 } else { 8662 if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) { 8663 /* Lazy stacking disabled, save registers now */ 8664 int i; 8665 bool cpacr_pass = v7m_cpacr_pass(env, env->v7m.secure, 8666 arm_current_el(env) != 0); 8667 8668 if (stacked_ok && !cpacr_pass) { 8669 /* 8670 * Take UsageFault if CPACR forbids access. The pseudocode 8671 * here does a full CheckCPEnabled() but we know the NSACR 8672 * check can never fail as we have already handled that. 8673 */ 8674 qemu_log_mask(CPU_LOG_INT, 8675 "...UsageFault with CFSR.NOCP because " 8676 "CPACR.CP10 prevents stacking FP regs\n"); 8677 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 8678 env->v7m.secure); 8679 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK; 8680 stacked_ok = false; 8681 } 8682 8683 for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) { 8684 uint64_t dn = *aa32_vfp_dreg(env, i / 2); 8685 uint32_t faddr = frameptr + 0x20 + 4 * i; 8686 uint32_t slo = extract64(dn, 0, 32); 8687 uint32_t shi = extract64(dn, 32, 32); 8688 8689 if (i >= 16) { 8690 faddr += 8; /* skip the slot for the FPSCR */ 8691 } 8692 stacked_ok = stacked_ok && 8693 v7m_stack_write(cpu, faddr, slo, 8694 mmu_idx, STACK_NORMAL) && 8695 v7m_stack_write(cpu, faddr + 4, shi, 8696 mmu_idx, STACK_NORMAL); 8697 } 8698 stacked_ok = stacked_ok && 8699 v7m_stack_write(cpu, frameptr + 0x60, 8700 vfp_get_fpscr(env), mmu_idx, STACK_NORMAL); 8701 if (cpacr_pass) { 8702 for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) { 8703 *aa32_vfp_dreg(env, i / 2) = 0; 8704 } 8705 vfp_set_fpscr(env, 0); 8706 } 8707 } else { 8708 /* Lazy stacking enabled, save necessary info to stack later */ 8709 v7m_update_fpccr(env, frameptr + 0x20, true); 8710 } 8711 } 8712 } 8713 8714 /* 8715 * If we broke a stack limit then SP was already updated earlier; 8716 * otherwise we update SP regardless of whether any of the stack 8717 * accesses failed or we took some other kind of fault. 8718 */ 8719 if (!limitviol) { 8720 env->regs[13] = frameptr; 8721 } 8722 8723 return !stacked_ok; 8724 } 8725 8726 static void do_v7m_exception_exit(ARMCPU *cpu) 8727 { 8728 CPUARMState *env = &cpu->env; 8729 uint32_t excret; 8730 uint32_t xpsr; 8731 bool ufault = false; 8732 bool sfault = false; 8733 bool return_to_sp_process; 8734 bool return_to_handler; 8735 bool rettobase = false; 8736 bool exc_secure = false; 8737 bool return_to_secure; 8738 bool ftype; 8739 bool restore_s16_s31; 8740 8741 /* If we're not in Handler mode then jumps to magic exception-exit 8742 * addresses don't have magic behaviour. However for the v8M 8743 * security extensions the magic secure-function-return has to 8744 * work in thread mode too, so to avoid doing an extra check in 8745 * the generated code we allow exception-exit magic to also cause the 8746 * internal exception and bring us here in thread mode. Correct code 8747 * will never try to do this (the following insn fetch will always 8748 * fault) so we the overhead of having taken an unnecessary exception 8749 * doesn't matter. 8750 */ 8751 if (!arm_v7m_is_handler_mode(env)) { 8752 return; 8753 } 8754 8755 /* In the spec pseudocode ExceptionReturn() is called directly 8756 * from BXWritePC() and gets the full target PC value including 8757 * bit zero. In QEMU's implementation we treat it as a normal 8758 * jump-to-register (which is then caught later on), and so split 8759 * the target value up between env->regs[15] and env->thumb in 8760 * gen_bx(). Reconstitute it. 8761 */ 8762 excret = env->regs[15]; 8763 if (env->thumb) { 8764 excret |= 1; 8765 } 8766 8767 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32 8768 " previous exception %d\n", 8769 excret, env->v7m.exception); 8770 8771 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) { 8772 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception " 8773 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n", 8774 excret); 8775 } 8776 8777 ftype = excret & R_V7M_EXCRET_FTYPE_MASK; 8778 8779 if (!arm_feature(env, ARM_FEATURE_VFP) && !ftype) { 8780 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero FTYPE in exception " 8781 "exit PC value 0x%" PRIx32 " is UNPREDICTABLE " 8782 "if FPU not present\n", 8783 excret); 8784 ftype = true; 8785 } 8786 8787 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8788 /* EXC_RETURN.ES validation check (R_SMFL). We must do this before 8789 * we pick which FAULTMASK to clear. 8790 */ 8791 if (!env->v7m.secure && 8792 ((excret & R_V7M_EXCRET_ES_MASK) || 8793 !(excret & R_V7M_EXCRET_DCRS_MASK))) { 8794 sfault = 1; 8795 /* For all other purposes, treat ES as 0 (R_HXSR) */ 8796 excret &= ~R_V7M_EXCRET_ES_MASK; 8797 } 8798 exc_secure = excret & R_V7M_EXCRET_ES_MASK; 8799 } 8800 8801 if (env->v7m.exception != ARMV7M_EXCP_NMI) { 8802 /* Auto-clear FAULTMASK on return from other than NMI. 8803 * If the security extension is implemented then this only 8804 * happens if the raw execution priority is >= 0; the 8805 * value of the ES bit in the exception return value indicates 8806 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.) 8807 */ 8808 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8809 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) { 8810 env->v7m.faultmask[exc_secure] = 0; 8811 } 8812 } else { 8813 env->v7m.faultmask[M_REG_NS] = 0; 8814 } 8815 } 8816 8817 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception, 8818 exc_secure)) { 8819 case -1: 8820 /* attempt to exit an exception that isn't active */ 8821 ufault = true; 8822 break; 8823 case 0: 8824 /* still an irq active now */ 8825 break; 8826 case 1: 8827 /* we returned to base exception level, no nesting. 8828 * (In the pseudocode this is written using "NestedActivation != 1" 8829 * where we have 'rettobase == false'.) 8830 */ 8831 rettobase = true; 8832 break; 8833 default: 8834 g_assert_not_reached(); 8835 } 8836 8837 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK); 8838 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK; 8839 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) && 8840 (excret & R_V7M_EXCRET_S_MASK); 8841 8842 if (arm_feature(env, ARM_FEATURE_V8)) { 8843 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8844 /* UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP); 8845 * we choose to take the UsageFault. 8846 */ 8847 if ((excret & R_V7M_EXCRET_S_MASK) || 8848 (excret & R_V7M_EXCRET_ES_MASK) || 8849 !(excret & R_V7M_EXCRET_DCRS_MASK)) { 8850 ufault = true; 8851 } 8852 } 8853 if (excret & R_V7M_EXCRET_RES0_MASK) { 8854 ufault = true; 8855 } 8856 } else { 8857 /* For v7M we only recognize certain combinations of the low bits */ 8858 switch (excret & 0xf) { 8859 case 1: /* Return to Handler */ 8860 break; 8861 case 13: /* Return to Thread using Process stack */ 8862 case 9: /* Return to Thread using Main stack */ 8863 /* We only need to check NONBASETHRDENA for v7M, because in 8864 * v8M this bit does not exist (it is RES1). 8865 */ 8866 if (!rettobase && 8867 !(env->v7m.ccr[env->v7m.secure] & 8868 R_V7M_CCR_NONBASETHRDENA_MASK)) { 8869 ufault = true; 8870 } 8871 break; 8872 default: 8873 ufault = true; 8874 } 8875 } 8876 8877 /* 8878 * Set CONTROL.SPSEL from excret.SPSEL. Since we're still in 8879 * Handler mode (and will be until we write the new XPSR.Interrupt 8880 * field) this does not switch around the current stack pointer. 8881 * We must do this before we do any kind of tailchaining, including 8882 * for the derived exceptions on integrity check failures, or we will 8883 * give the guest an incorrect EXCRET.SPSEL value on exception entry. 8884 */ 8885 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure); 8886 8887 /* 8888 * Clear scratch FP values left in caller saved registers; this 8889 * must happen before any kind of tail chaining. 8890 */ 8891 if ((env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_CLRONRET_MASK) && 8892 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) { 8893 if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) { 8894 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; 8895 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 8896 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 8897 "stackframe: error during lazy state deactivation\n"); 8898 v7m_exception_taken(cpu, excret, true, false); 8899 return; 8900 } else { 8901 /* Clear s0..s15 and FPSCR */ 8902 int i; 8903 8904 for (i = 0; i < 16; i += 2) { 8905 *aa32_vfp_dreg(env, i / 2) = 0; 8906 } 8907 vfp_set_fpscr(env, 0); 8908 } 8909 } 8910 8911 if (sfault) { 8912 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK; 8913 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 8914 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 8915 "stackframe: failed EXC_RETURN.ES validity check\n"); 8916 v7m_exception_taken(cpu, excret, true, false); 8917 return; 8918 } 8919 8920 if (ufault) { 8921 /* Bad exception return: instead of popping the exception 8922 * stack, directly take a usage fault on the current stack. 8923 */ 8924 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 8925 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 8926 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " 8927 "stackframe: failed exception return integrity check\n"); 8928 v7m_exception_taken(cpu, excret, true, false); 8929 return; 8930 } 8931 8932 /* 8933 * Tailchaining: if there is currently a pending exception that 8934 * is high enough priority to preempt execution at the level we're 8935 * about to return to, then just directly take that exception now, 8936 * avoiding an unstack-and-then-stack. Note that now we have 8937 * deactivated the previous exception by calling armv7m_nvic_complete_irq() 8938 * our current execution priority is already the execution priority we are 8939 * returning to -- none of the state we would unstack or set based on 8940 * the EXCRET value affects it. 8941 */ 8942 if (armv7m_nvic_can_take_pending_exception(env->nvic)) { 8943 qemu_log_mask(CPU_LOG_INT, "...tailchaining to pending exception\n"); 8944 v7m_exception_taken(cpu, excret, true, false); 8945 return; 8946 } 8947 8948 switch_v7m_security_state(env, return_to_secure); 8949 8950 { 8951 /* The stack pointer we should be reading the exception frame from 8952 * depends on bits in the magic exception return type value (and 8953 * for v8M isn't necessarily the stack pointer we will eventually 8954 * end up resuming execution with). Get a pointer to the location 8955 * in the CPU state struct where the SP we need is currently being 8956 * stored; we will use and modify it in place. 8957 * We use this limited C variable scope so we don't accidentally 8958 * use 'frame_sp_p' after we do something that makes it invalid. 8959 */ 8960 uint32_t *frame_sp_p = get_v7m_sp_ptr(env, 8961 return_to_secure, 8962 !return_to_handler, 8963 return_to_sp_process); 8964 uint32_t frameptr = *frame_sp_p; 8965 bool pop_ok = true; 8966 ARMMMUIdx mmu_idx; 8967 bool return_to_priv = return_to_handler || 8968 !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK); 8969 8970 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure, 8971 return_to_priv); 8972 8973 if (!QEMU_IS_ALIGNED(frameptr, 8) && 8974 arm_feature(env, ARM_FEATURE_V8)) { 8975 qemu_log_mask(LOG_GUEST_ERROR, 8976 "M profile exception return with non-8-aligned SP " 8977 "for destination state is UNPREDICTABLE\n"); 8978 } 8979 8980 /* Do we need to pop callee-saved registers? */ 8981 if (return_to_secure && 8982 ((excret & R_V7M_EXCRET_ES_MASK) == 0 || 8983 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) { 8984 uint32_t actual_sig; 8985 8986 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx); 8987 8988 if (pop_ok && v7m_integrity_sig(env, excret) != actual_sig) { 8989 /* Take a SecureFault on the current stack */ 8990 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK; 8991 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 8992 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 8993 "stackframe: failed exception return integrity " 8994 "signature check\n"); 8995 v7m_exception_taken(cpu, excret, true, false); 8996 return; 8997 } 8998 8999 pop_ok = pop_ok && 9000 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) && 9001 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) && 9002 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) && 9003 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) && 9004 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) && 9005 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) && 9006 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) && 9007 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx); 9008 9009 frameptr += 0x28; 9010 } 9011 9012 /* Pop registers */ 9013 pop_ok = pop_ok && 9014 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) && 9015 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) && 9016 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) && 9017 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) && 9018 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) && 9019 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) && 9020 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) && 9021 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx); 9022 9023 if (!pop_ok) { 9024 /* v7m_stack_read() pended a fault, so take it (as a tail 9025 * chained exception on the same stack frame) 9026 */ 9027 qemu_log_mask(CPU_LOG_INT, "...derived exception on unstacking\n"); 9028 v7m_exception_taken(cpu, excret, true, false); 9029 return; 9030 } 9031 9032 /* Returning from an exception with a PC with bit 0 set is defined 9033 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified 9034 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore 9035 * the lsbit, and there are several RTOSes out there which incorrectly 9036 * assume the r15 in the stack frame should be a Thumb-style "lsbit 9037 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but 9038 * complain about the badly behaved guest. 9039 */ 9040 if (env->regs[15] & 1) { 9041 env->regs[15] &= ~1U; 9042 if (!arm_feature(env, ARM_FEATURE_V8)) { 9043 qemu_log_mask(LOG_GUEST_ERROR, 9044 "M profile return from interrupt with misaligned " 9045 "PC is UNPREDICTABLE on v7M\n"); 9046 } 9047 } 9048 9049 if (arm_feature(env, ARM_FEATURE_V8)) { 9050 /* For v8M we have to check whether the xPSR exception field 9051 * matches the EXCRET value for return to handler/thread 9052 * before we commit to changing the SP and xPSR. 9053 */ 9054 bool will_be_handler = (xpsr & XPSR_EXCP) != 0; 9055 if (return_to_handler != will_be_handler) { 9056 /* Take an INVPC UsageFault on the current stack. 9057 * By this point we will have switched to the security state 9058 * for the background state, so this UsageFault will target 9059 * that state. 9060 */ 9061 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 9062 env->v7m.secure); 9063 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 9064 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " 9065 "stackframe: failed exception return integrity " 9066 "check\n"); 9067 v7m_exception_taken(cpu, excret, true, false); 9068 return; 9069 } 9070 } 9071 9072 if (!ftype) { 9073 /* FP present and we need to handle it */ 9074 if (!return_to_secure && 9075 (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK)) { 9076 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9077 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; 9078 qemu_log_mask(CPU_LOG_INT, 9079 "...taking SecureFault on existing stackframe: " 9080 "Secure LSPACT set but exception return is " 9081 "not to secure state\n"); 9082 v7m_exception_taken(cpu, excret, true, false); 9083 return; 9084 } 9085 9086 restore_s16_s31 = return_to_secure && 9087 (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK); 9088 9089 if (env->v7m.fpccr[return_to_secure] & R_V7M_FPCCR_LSPACT_MASK) { 9090 /* State in FPU is still valid, just clear LSPACT */ 9091 env->v7m.fpccr[return_to_secure] &= ~R_V7M_FPCCR_LSPACT_MASK; 9092 } else { 9093 int i; 9094 uint32_t fpscr; 9095 bool cpacr_pass, nsacr_pass; 9096 9097 cpacr_pass = v7m_cpacr_pass(env, return_to_secure, 9098 return_to_priv); 9099 nsacr_pass = return_to_secure || 9100 extract32(env->v7m.nsacr, 10, 1); 9101 9102 if (!cpacr_pass) { 9103 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 9104 return_to_secure); 9105 env->v7m.cfsr[return_to_secure] |= R_V7M_CFSR_NOCP_MASK; 9106 qemu_log_mask(CPU_LOG_INT, 9107 "...taking UsageFault on existing " 9108 "stackframe: CPACR.CP10 prevents unstacking " 9109 "FP regs\n"); 9110 v7m_exception_taken(cpu, excret, true, false); 9111 return; 9112 } else if (!nsacr_pass) { 9113 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, true); 9114 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_INVPC_MASK; 9115 qemu_log_mask(CPU_LOG_INT, 9116 "...taking Secure UsageFault on existing " 9117 "stackframe: NSACR.CP10 prevents unstacking " 9118 "FP regs\n"); 9119 v7m_exception_taken(cpu, excret, true, false); 9120 return; 9121 } 9122 9123 for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) { 9124 uint32_t slo, shi; 9125 uint64_t dn; 9126 uint32_t faddr = frameptr + 0x20 + 4 * i; 9127 9128 if (i >= 16) { 9129 faddr += 8; /* Skip the slot for the FPSCR */ 9130 } 9131 9132 pop_ok = pop_ok && 9133 v7m_stack_read(cpu, &slo, faddr, mmu_idx) && 9134 v7m_stack_read(cpu, &shi, faddr + 4, mmu_idx); 9135 9136 if (!pop_ok) { 9137 break; 9138 } 9139 9140 dn = (uint64_t)shi << 32 | slo; 9141 *aa32_vfp_dreg(env, i / 2) = dn; 9142 } 9143 pop_ok = pop_ok && 9144 v7m_stack_read(cpu, &fpscr, frameptr + 0x60, mmu_idx); 9145 if (pop_ok) { 9146 vfp_set_fpscr(env, fpscr); 9147 } 9148 if (!pop_ok) { 9149 /* 9150 * These regs are 0 if security extension present; 9151 * otherwise merely UNKNOWN. We zero always. 9152 */ 9153 for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) { 9154 *aa32_vfp_dreg(env, i / 2) = 0; 9155 } 9156 vfp_set_fpscr(env, 0); 9157 } 9158 } 9159 } 9160 env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S], 9161 V7M_CONTROL, FPCA, !ftype); 9162 9163 /* Commit to consuming the stack frame */ 9164 frameptr += 0x20; 9165 if (!ftype) { 9166 frameptr += 0x48; 9167 if (restore_s16_s31) { 9168 frameptr += 0x40; 9169 } 9170 } 9171 /* Undo stack alignment (the SPREALIGN bit indicates that the original 9172 * pre-exception SP was not 8-aligned and we added a padding word to 9173 * align it, so we undo this by ORing in the bit that increases it 9174 * from the current 8-aligned value to the 8-unaligned value. (Adding 4 9175 * would work too but a logical OR is how the pseudocode specifies it.) 9176 */ 9177 if (xpsr & XPSR_SPREALIGN) { 9178 frameptr |= 4; 9179 } 9180 *frame_sp_p = frameptr; 9181 } 9182 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */ 9183 xpsr_write(env, xpsr, ~(XPSR_SPREALIGN | XPSR_SFPA)); 9184 9185 if (env->v7m.secure) { 9186 bool sfpa = xpsr & XPSR_SFPA; 9187 9188 env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S], 9189 V7M_CONTROL, SFPA, sfpa); 9190 } 9191 9192 /* The restored xPSR exception field will be zero if we're 9193 * resuming in Thread mode. If that doesn't match what the 9194 * exception return excret specified then this is a UsageFault. 9195 * v7M requires we make this check here; v8M did it earlier. 9196 */ 9197 if (return_to_handler != arm_v7m_is_handler_mode(env)) { 9198 /* Take an INVPC UsageFault by pushing the stack again; 9199 * we know we're v7M so this is never a Secure UsageFault. 9200 */ 9201 bool ignore_stackfaults; 9202 9203 assert(!arm_feature(env, ARM_FEATURE_V8)); 9204 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false); 9205 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 9206 ignore_stackfaults = v7m_push_stack(cpu); 9207 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: " 9208 "failed exception return integrity check\n"); 9209 v7m_exception_taken(cpu, excret, false, ignore_stackfaults); 9210 return; 9211 } 9212 9213 /* Otherwise, we have a successful exception exit. */ 9214 arm_clear_exclusive(env); 9215 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n"); 9216 } 9217 9218 static bool do_v7m_function_return(ARMCPU *cpu) 9219 { 9220 /* v8M security extensions magic function return. 9221 * We may either: 9222 * (1) throw an exception (longjump) 9223 * (2) return true if we successfully handled the function return 9224 * (3) return false if we failed a consistency check and have 9225 * pended a UsageFault that needs to be taken now 9226 * 9227 * At this point the magic return value is split between env->regs[15] 9228 * and env->thumb. We don't bother to reconstitute it because we don't 9229 * need it (all values are handled the same way). 9230 */ 9231 CPUARMState *env = &cpu->env; 9232 uint32_t newpc, newpsr, newpsr_exc; 9233 9234 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n"); 9235 9236 { 9237 bool threadmode, spsel; 9238 TCGMemOpIdx oi; 9239 ARMMMUIdx mmu_idx; 9240 uint32_t *frame_sp_p; 9241 uint32_t frameptr; 9242 9243 /* Pull the return address and IPSR from the Secure stack */ 9244 threadmode = !arm_v7m_is_handler_mode(env); 9245 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK; 9246 9247 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel); 9248 frameptr = *frame_sp_p; 9249 9250 /* These loads may throw an exception (for MPU faults). We want to 9251 * do them as secure, so work out what MMU index that is. 9252 */ 9253 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); 9254 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx)); 9255 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0); 9256 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0); 9257 9258 /* Consistency checks on new IPSR */ 9259 newpsr_exc = newpsr & XPSR_EXCP; 9260 if (!((env->v7m.exception == 0 && newpsr_exc == 0) || 9261 (env->v7m.exception == 1 && newpsr_exc != 0))) { 9262 /* Pend the fault and tell our caller to take it */ 9263 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 9264 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 9265 env->v7m.secure); 9266 qemu_log_mask(CPU_LOG_INT, 9267 "...taking INVPC UsageFault: " 9268 "IPSR consistency check failed\n"); 9269 return false; 9270 } 9271 9272 *frame_sp_p = frameptr + 8; 9273 } 9274 9275 /* This invalidates frame_sp_p */ 9276 switch_v7m_security_state(env, true); 9277 env->v7m.exception = newpsr_exc; 9278 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 9279 if (newpsr & XPSR_SFPA) { 9280 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK; 9281 } 9282 xpsr_write(env, 0, XPSR_IT); 9283 env->thumb = newpc & 1; 9284 env->regs[15] = newpc & ~1; 9285 9286 qemu_log_mask(CPU_LOG_INT, "...function return successful\n"); 9287 return true; 9288 } 9289 9290 static void arm_log_exception(int idx) 9291 { 9292 if (qemu_loglevel_mask(CPU_LOG_INT)) { 9293 const char *exc = NULL; 9294 static const char * const excnames[] = { 9295 [EXCP_UDEF] = "Undefined Instruction", 9296 [EXCP_SWI] = "SVC", 9297 [EXCP_PREFETCH_ABORT] = "Prefetch Abort", 9298 [EXCP_DATA_ABORT] = "Data Abort", 9299 [EXCP_IRQ] = "IRQ", 9300 [EXCP_FIQ] = "FIQ", 9301 [EXCP_BKPT] = "Breakpoint", 9302 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit", 9303 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage", 9304 [EXCP_HVC] = "Hypervisor Call", 9305 [EXCP_HYP_TRAP] = "Hypervisor Trap", 9306 [EXCP_SMC] = "Secure Monitor Call", 9307 [EXCP_VIRQ] = "Virtual IRQ", 9308 [EXCP_VFIQ] = "Virtual FIQ", 9309 [EXCP_SEMIHOST] = "Semihosting call", 9310 [EXCP_NOCP] = "v7M NOCP UsageFault", 9311 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault", 9312 [EXCP_STKOF] = "v8M STKOF UsageFault", 9313 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking", 9314 [EXCP_LSERR] = "v8M LSERR UsageFault", 9315 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault", 9316 }; 9317 9318 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { 9319 exc = excnames[idx]; 9320 } 9321 if (!exc) { 9322 exc = "unknown"; 9323 } 9324 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc); 9325 } 9326 } 9327 9328 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx, 9329 uint32_t addr, uint16_t *insn) 9330 { 9331 /* Load a 16-bit portion of a v7M instruction, returning true on success, 9332 * or false on failure (in which case we will have pended the appropriate 9333 * exception). 9334 * We need to do the instruction fetch's MPU and SAU checks 9335 * like this because there is no MMU index that would allow 9336 * doing the load with a single function call. Instead we must 9337 * first check that the security attributes permit the load 9338 * and that they don't mismatch on the two halves of the instruction, 9339 * and then we do the load as a secure load (ie using the security 9340 * attributes of the address, not the CPU, as architecturally required). 9341 */ 9342 CPUState *cs = CPU(cpu); 9343 CPUARMState *env = &cpu->env; 9344 V8M_SAttributes sattrs = {}; 9345 MemTxAttrs attrs = {}; 9346 ARMMMUFaultInfo fi = {}; 9347 MemTxResult txres; 9348 target_ulong page_size; 9349 hwaddr physaddr; 9350 int prot; 9351 9352 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs); 9353 if (!sattrs.nsc || sattrs.ns) { 9354 /* This must be the second half of the insn, and it straddles a 9355 * region boundary with the second half not being S&NSC. 9356 */ 9357 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 9358 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9359 qemu_log_mask(CPU_LOG_INT, 9360 "...really SecureFault with SFSR.INVEP\n"); 9361 return false; 9362 } 9363 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx, 9364 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) { 9365 /* the MPU lookup failed */ 9366 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; 9367 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure); 9368 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n"); 9369 return false; 9370 } 9371 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr, 9372 attrs, &txres); 9373 if (txres != MEMTX_OK) { 9374 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; 9375 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); 9376 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n"); 9377 return false; 9378 } 9379 return true; 9380 } 9381 9382 static bool v7m_handle_execute_nsc(ARMCPU *cpu) 9383 { 9384 /* Check whether this attempt to execute code in a Secure & NS-Callable 9385 * memory region is for an SG instruction; if so, then emulate the 9386 * effect of the SG instruction and return true. Otherwise pend 9387 * the correct kind of exception and return false. 9388 */ 9389 CPUARMState *env = &cpu->env; 9390 ARMMMUIdx mmu_idx; 9391 uint16_t insn; 9392 9393 /* We should never get here unless get_phys_addr_pmsav8() caused 9394 * an exception for NS executing in S&NSC memory. 9395 */ 9396 assert(!env->v7m.secure); 9397 assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); 9398 9399 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */ 9400 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); 9401 9402 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) { 9403 return false; 9404 } 9405 9406 if (!env->thumb) { 9407 goto gen_invep; 9408 } 9409 9410 if (insn != 0xe97f) { 9411 /* Not an SG instruction first half (we choose the IMPDEF 9412 * early-SG-check option). 9413 */ 9414 goto gen_invep; 9415 } 9416 9417 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) { 9418 return false; 9419 } 9420 9421 if (insn != 0xe97f) { 9422 /* Not an SG instruction second half (yes, both halves of the SG 9423 * insn have the same hex value) 9424 */ 9425 goto gen_invep; 9426 } 9427 9428 /* OK, we have confirmed that we really have an SG instruction. 9429 * We know we're NS in S memory so don't need to repeat those checks. 9430 */ 9431 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32 9432 ", executing it\n", env->regs[15]); 9433 env->regs[14] &= ~1; 9434 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 9435 switch_v7m_security_state(env, true); 9436 xpsr_write(env, 0, XPSR_IT); 9437 env->regs[15] += 4; 9438 return true; 9439 9440 gen_invep: 9441 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 9442 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9443 qemu_log_mask(CPU_LOG_INT, 9444 "...really SecureFault with SFSR.INVEP\n"); 9445 return false; 9446 } 9447 9448 void arm_v7m_cpu_do_interrupt(CPUState *cs) 9449 { 9450 ARMCPU *cpu = ARM_CPU(cs); 9451 CPUARMState *env = &cpu->env; 9452 uint32_t lr; 9453 bool ignore_stackfaults; 9454 9455 arm_log_exception(cs->exception_index); 9456 9457 /* For exceptions we just mark as pending on the NVIC, and let that 9458 handle it. */ 9459 switch (cs->exception_index) { 9460 case EXCP_UDEF: 9461 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9462 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK; 9463 break; 9464 case EXCP_NOCP: 9465 { 9466 /* 9467 * NOCP might be directed to something other than the current 9468 * security state if this fault is because of NSACR; we indicate 9469 * the target security state using exception.target_el. 9470 */ 9471 int target_secstate; 9472 9473 if (env->exception.target_el == 3) { 9474 target_secstate = M_REG_S; 9475 } else { 9476 target_secstate = env->v7m.secure; 9477 } 9478 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, target_secstate); 9479 env->v7m.cfsr[target_secstate] |= R_V7M_CFSR_NOCP_MASK; 9480 break; 9481 } 9482 case EXCP_INVSTATE: 9483 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9484 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK; 9485 break; 9486 case EXCP_STKOF: 9487 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9488 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 9489 break; 9490 case EXCP_LSERR: 9491 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9492 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; 9493 break; 9494 case EXCP_UNALIGNED: 9495 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9496 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNALIGNED_MASK; 9497 break; 9498 case EXCP_SWI: 9499 /* The PC already points to the next instruction. */ 9500 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure); 9501 break; 9502 case EXCP_PREFETCH_ABORT: 9503 case EXCP_DATA_ABORT: 9504 /* Note that for M profile we don't have a guest facing FSR, but 9505 * the env->exception.fsr will be populated by the code that 9506 * raises the fault, in the A profile short-descriptor format. 9507 */ 9508 switch (env->exception.fsr & 0xf) { 9509 case M_FAKE_FSR_NSC_EXEC: 9510 /* Exception generated when we try to execute code at an address 9511 * which is marked as Secure & Non-Secure Callable and the CPU 9512 * is in the Non-Secure state. The only instruction which can 9513 * be executed like this is SG (and that only if both halves of 9514 * the SG instruction have the same security attributes.) 9515 * Everything else must generate an INVEP SecureFault, so we 9516 * emulate the SG instruction here. 9517 */ 9518 if (v7m_handle_execute_nsc(cpu)) { 9519 return; 9520 } 9521 break; 9522 case M_FAKE_FSR_SFAULT: 9523 /* Various flavours of SecureFault for attempts to execute or 9524 * access data in the wrong security state. 9525 */ 9526 switch (cs->exception_index) { 9527 case EXCP_PREFETCH_ABORT: 9528 if (env->v7m.secure) { 9529 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK; 9530 qemu_log_mask(CPU_LOG_INT, 9531 "...really SecureFault with SFSR.INVTRAN\n"); 9532 } else { 9533 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 9534 qemu_log_mask(CPU_LOG_INT, 9535 "...really SecureFault with SFSR.INVEP\n"); 9536 } 9537 break; 9538 case EXCP_DATA_ABORT: 9539 /* This must be an NS access to S memory */ 9540 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK; 9541 qemu_log_mask(CPU_LOG_INT, 9542 "...really SecureFault with SFSR.AUVIOL\n"); 9543 break; 9544 } 9545 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9546 break; 9547 case 0x8: /* External Abort */ 9548 switch (cs->exception_index) { 9549 case EXCP_PREFETCH_ABORT: 9550 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; 9551 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n"); 9552 break; 9553 case EXCP_DATA_ABORT: 9554 env->v7m.cfsr[M_REG_NS] |= 9555 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK); 9556 env->v7m.bfar = env->exception.vaddress; 9557 qemu_log_mask(CPU_LOG_INT, 9558 "...with CFSR.PRECISERR and BFAR 0x%x\n", 9559 env->v7m.bfar); 9560 break; 9561 } 9562 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); 9563 break; 9564 default: 9565 /* All other FSR values are either MPU faults or "can't happen 9566 * for M profile" cases. 9567 */ 9568 switch (cs->exception_index) { 9569 case EXCP_PREFETCH_ABORT: 9570 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; 9571 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n"); 9572 break; 9573 case EXCP_DATA_ABORT: 9574 env->v7m.cfsr[env->v7m.secure] |= 9575 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK); 9576 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress; 9577 qemu_log_mask(CPU_LOG_INT, 9578 "...with CFSR.DACCVIOL and MMFAR 0x%x\n", 9579 env->v7m.mmfar[env->v7m.secure]); 9580 break; 9581 } 9582 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, 9583 env->v7m.secure); 9584 break; 9585 } 9586 break; 9587 case EXCP_BKPT: 9588 if (semihosting_enabled()) { 9589 int nr; 9590 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff; 9591 if (nr == 0xab) { 9592 env->regs[15] += 2; 9593 qemu_log_mask(CPU_LOG_INT, 9594 "...handling as semihosting call 0x%x\n", 9595 env->regs[0]); 9596 env->regs[0] = do_arm_semihosting(env); 9597 return; 9598 } 9599 } 9600 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false); 9601 break; 9602 case EXCP_IRQ: 9603 break; 9604 case EXCP_EXCEPTION_EXIT: 9605 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) { 9606 /* Must be v8M security extension function return */ 9607 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC); 9608 assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); 9609 if (do_v7m_function_return(cpu)) { 9610 return; 9611 } 9612 } else { 9613 do_v7m_exception_exit(cpu); 9614 return; 9615 } 9616 break; 9617 case EXCP_LAZYFP: 9618 /* 9619 * We already pended the specific exception in the NVIC in the 9620 * v7m_preserve_fp_state() helper function. 9621 */ 9622 break; 9623 default: 9624 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 9625 return; /* Never happens. Keep compiler happy. */ 9626 } 9627 9628 if (arm_feature(env, ARM_FEATURE_V8)) { 9629 lr = R_V7M_EXCRET_RES1_MASK | 9630 R_V7M_EXCRET_DCRS_MASK; 9631 /* The S bit indicates whether we should return to Secure 9632 * or NonSecure (ie our current state). 9633 * The ES bit indicates whether we're taking this exception 9634 * to Secure or NonSecure (ie our target state). We set it 9635 * later, in v7m_exception_taken(). 9636 * The SPSEL bit is also set in v7m_exception_taken() for v8M. 9637 * This corresponds to the ARM ARM pseudocode for v8M setting 9638 * some LR bits in PushStack() and some in ExceptionTaken(); 9639 * the distinction matters for the tailchain cases where we 9640 * can take an exception without pushing the stack. 9641 */ 9642 if (env->v7m.secure) { 9643 lr |= R_V7M_EXCRET_S_MASK; 9644 } 9645 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) { 9646 lr |= R_V7M_EXCRET_FTYPE_MASK; 9647 } 9648 } else { 9649 lr = R_V7M_EXCRET_RES1_MASK | 9650 R_V7M_EXCRET_S_MASK | 9651 R_V7M_EXCRET_DCRS_MASK | 9652 R_V7M_EXCRET_FTYPE_MASK | 9653 R_V7M_EXCRET_ES_MASK; 9654 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) { 9655 lr |= R_V7M_EXCRET_SPSEL_MASK; 9656 } 9657 } 9658 if (!arm_v7m_is_handler_mode(env)) { 9659 lr |= R_V7M_EXCRET_MODE_MASK; 9660 } 9661 9662 ignore_stackfaults = v7m_push_stack(cpu); 9663 v7m_exception_taken(cpu, lr, false, ignore_stackfaults); 9664 } 9665 9666 /* Function used to synchronize QEMU's AArch64 register set with AArch32 9667 * register set. This is necessary when switching between AArch32 and AArch64 9668 * execution state. 9669 */ 9670 void aarch64_sync_32_to_64(CPUARMState *env) 9671 { 9672 int i; 9673 uint32_t mode = env->uncached_cpsr & CPSR_M; 9674 9675 /* We can blanket copy R[0:7] to X[0:7] */ 9676 for (i = 0; i < 8; i++) { 9677 env->xregs[i] = env->regs[i]; 9678 } 9679 9680 /* Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12. 9681 * Otherwise, they come from the banked user regs. 9682 */ 9683 if (mode == ARM_CPU_MODE_FIQ) { 9684 for (i = 8; i < 13; i++) { 9685 env->xregs[i] = env->usr_regs[i - 8]; 9686 } 9687 } else { 9688 for (i = 8; i < 13; i++) { 9689 env->xregs[i] = env->regs[i]; 9690 } 9691 } 9692 9693 /* Registers x13-x23 are the various mode SP and FP registers. Registers 9694 * r13 and r14 are only copied if we are in that mode, otherwise we copy 9695 * from the mode banked register. 9696 */ 9697 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 9698 env->xregs[13] = env->regs[13]; 9699 env->xregs[14] = env->regs[14]; 9700 } else { 9701 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)]; 9702 /* HYP is an exception in that it is copied from r14 */ 9703 if (mode == ARM_CPU_MODE_HYP) { 9704 env->xregs[14] = env->regs[14]; 9705 } else { 9706 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)]; 9707 } 9708 } 9709 9710 if (mode == ARM_CPU_MODE_HYP) { 9711 env->xregs[15] = env->regs[13]; 9712 } else { 9713 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)]; 9714 } 9715 9716 if (mode == ARM_CPU_MODE_IRQ) { 9717 env->xregs[16] = env->regs[14]; 9718 env->xregs[17] = env->regs[13]; 9719 } else { 9720 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)]; 9721 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)]; 9722 } 9723 9724 if (mode == ARM_CPU_MODE_SVC) { 9725 env->xregs[18] = env->regs[14]; 9726 env->xregs[19] = env->regs[13]; 9727 } else { 9728 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)]; 9729 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)]; 9730 } 9731 9732 if (mode == ARM_CPU_MODE_ABT) { 9733 env->xregs[20] = env->regs[14]; 9734 env->xregs[21] = env->regs[13]; 9735 } else { 9736 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)]; 9737 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)]; 9738 } 9739 9740 if (mode == ARM_CPU_MODE_UND) { 9741 env->xregs[22] = env->regs[14]; 9742 env->xregs[23] = env->regs[13]; 9743 } else { 9744 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)]; 9745 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)]; 9746 } 9747 9748 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 9749 * mode, then we can copy from r8-r14. Otherwise, we copy from the 9750 * FIQ bank for r8-r14. 9751 */ 9752 if (mode == ARM_CPU_MODE_FIQ) { 9753 for (i = 24; i < 31; i++) { 9754 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */ 9755 } 9756 } else { 9757 for (i = 24; i < 29; i++) { 9758 env->xregs[i] = env->fiq_regs[i - 24]; 9759 } 9760 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)]; 9761 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)]; 9762 } 9763 9764 env->pc = env->regs[15]; 9765 } 9766 9767 /* Function used to synchronize QEMU's AArch32 register set with AArch64 9768 * register set. This is necessary when switching between AArch32 and AArch64 9769 * execution state. 9770 */ 9771 void aarch64_sync_64_to_32(CPUARMState *env) 9772 { 9773 int i; 9774 uint32_t mode = env->uncached_cpsr & CPSR_M; 9775 9776 /* We can blanket copy X[0:7] to R[0:7] */ 9777 for (i = 0; i < 8; i++) { 9778 env->regs[i] = env->xregs[i]; 9779 } 9780 9781 /* Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12. 9782 * Otherwise, we copy x8-x12 into the banked user regs. 9783 */ 9784 if (mode == ARM_CPU_MODE_FIQ) { 9785 for (i = 8; i < 13; i++) { 9786 env->usr_regs[i - 8] = env->xregs[i]; 9787 } 9788 } else { 9789 for (i = 8; i < 13; i++) { 9790 env->regs[i] = env->xregs[i]; 9791 } 9792 } 9793 9794 /* Registers r13 & r14 depend on the current mode. 9795 * If we are in a given mode, we copy the corresponding x registers to r13 9796 * and r14. Otherwise, we copy the x register to the banked r13 and r14 9797 * for the mode. 9798 */ 9799 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 9800 env->regs[13] = env->xregs[13]; 9801 env->regs[14] = env->xregs[14]; 9802 } else { 9803 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13]; 9804 9805 /* HYP is an exception in that it does not have its own banked r14 but 9806 * shares the USR r14 9807 */ 9808 if (mode == ARM_CPU_MODE_HYP) { 9809 env->regs[14] = env->xregs[14]; 9810 } else { 9811 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14]; 9812 } 9813 } 9814 9815 if (mode == ARM_CPU_MODE_HYP) { 9816 env->regs[13] = env->xregs[15]; 9817 } else { 9818 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15]; 9819 } 9820 9821 if (mode == ARM_CPU_MODE_IRQ) { 9822 env->regs[14] = env->xregs[16]; 9823 env->regs[13] = env->xregs[17]; 9824 } else { 9825 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16]; 9826 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17]; 9827 } 9828 9829 if (mode == ARM_CPU_MODE_SVC) { 9830 env->regs[14] = env->xregs[18]; 9831 env->regs[13] = env->xregs[19]; 9832 } else { 9833 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18]; 9834 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19]; 9835 } 9836 9837 if (mode == ARM_CPU_MODE_ABT) { 9838 env->regs[14] = env->xregs[20]; 9839 env->regs[13] = env->xregs[21]; 9840 } else { 9841 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20]; 9842 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21]; 9843 } 9844 9845 if (mode == ARM_CPU_MODE_UND) { 9846 env->regs[14] = env->xregs[22]; 9847 env->regs[13] = env->xregs[23]; 9848 } else { 9849 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22]; 9850 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23]; 9851 } 9852 9853 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 9854 * mode, then we can copy to r8-r14. Otherwise, we copy to the 9855 * FIQ bank for r8-r14. 9856 */ 9857 if (mode == ARM_CPU_MODE_FIQ) { 9858 for (i = 24; i < 31; i++) { 9859 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */ 9860 } 9861 } else { 9862 for (i = 24; i < 29; i++) { 9863 env->fiq_regs[i - 24] = env->xregs[i]; 9864 } 9865 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29]; 9866 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30]; 9867 } 9868 9869 env->regs[15] = env->pc; 9870 } 9871 9872 static void take_aarch32_exception(CPUARMState *env, int new_mode, 9873 uint32_t mask, uint32_t offset, 9874 uint32_t newpc) 9875 { 9876 /* Change the CPU state so as to actually take the exception. */ 9877 switch_mode(env, new_mode); 9878 /* 9879 * For exceptions taken to AArch32 we must clear the SS bit in both 9880 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. 9881 */ 9882 env->uncached_cpsr &= ~PSTATE_SS; 9883 env->spsr = cpsr_read(env); 9884 /* Clear IT bits. */ 9885 env->condexec_bits = 0; 9886 /* Switch to the new mode, and to the correct instruction set. */ 9887 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; 9888 /* Set new mode endianness */ 9889 env->uncached_cpsr &= ~CPSR_E; 9890 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) { 9891 env->uncached_cpsr |= CPSR_E; 9892 } 9893 /* J and IL must always be cleared for exception entry */ 9894 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J); 9895 env->daif |= mask; 9896 9897 if (new_mode == ARM_CPU_MODE_HYP) { 9898 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0; 9899 env->elr_el[2] = env->regs[15]; 9900 } else { 9901 /* 9902 * this is a lie, as there was no c1_sys on V4T/V5, but who cares 9903 * and we should just guard the thumb mode on V4 9904 */ 9905 if (arm_feature(env, ARM_FEATURE_V4T)) { 9906 env->thumb = 9907 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; 9908 } 9909 env->regs[14] = env->regs[15] + offset; 9910 } 9911 env->regs[15] = newpc; 9912 } 9913 9914 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs) 9915 { 9916 /* 9917 * Handle exception entry to Hyp mode; this is sufficiently 9918 * different to entry to other AArch32 modes that we handle it 9919 * separately here. 9920 * 9921 * The vector table entry used is always the 0x14 Hyp mode entry point, 9922 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp. 9923 * The offset applied to the preferred return address is always zero 9924 * (see DDI0487C.a section G1.12.3). 9925 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values. 9926 */ 9927 uint32_t addr, mask; 9928 ARMCPU *cpu = ARM_CPU(cs); 9929 CPUARMState *env = &cpu->env; 9930 9931 switch (cs->exception_index) { 9932 case EXCP_UDEF: 9933 addr = 0x04; 9934 break; 9935 case EXCP_SWI: 9936 addr = 0x14; 9937 break; 9938 case EXCP_BKPT: 9939 /* Fall through to prefetch abort. */ 9940 case EXCP_PREFETCH_ABORT: 9941 env->cp15.ifar_s = env->exception.vaddress; 9942 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n", 9943 (uint32_t)env->exception.vaddress); 9944 addr = 0x0c; 9945 break; 9946 case EXCP_DATA_ABORT: 9947 env->cp15.dfar_s = env->exception.vaddress; 9948 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n", 9949 (uint32_t)env->exception.vaddress); 9950 addr = 0x10; 9951 break; 9952 case EXCP_IRQ: 9953 addr = 0x18; 9954 break; 9955 case EXCP_FIQ: 9956 addr = 0x1c; 9957 break; 9958 case EXCP_HVC: 9959 addr = 0x08; 9960 break; 9961 case EXCP_HYP_TRAP: 9962 addr = 0x14; 9963 default: 9964 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 9965 } 9966 9967 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) { 9968 if (!arm_feature(env, ARM_FEATURE_V8)) { 9969 /* 9970 * QEMU syndrome values are v8-style. v7 has the IL bit 9971 * UNK/SBZP for "field not valid" cases, where v8 uses RES1. 9972 * If this is a v7 CPU, squash the IL bit in those cases. 9973 */ 9974 if (cs->exception_index == EXCP_PREFETCH_ABORT || 9975 (cs->exception_index == EXCP_DATA_ABORT && 9976 !(env->exception.syndrome & ARM_EL_ISV)) || 9977 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) { 9978 env->exception.syndrome &= ~ARM_EL_IL; 9979 } 9980 } 9981 env->cp15.esr_el[2] = env->exception.syndrome; 9982 } 9983 9984 if (arm_current_el(env) != 2 && addr < 0x14) { 9985 addr = 0x14; 9986 } 9987 9988 mask = 0; 9989 if (!(env->cp15.scr_el3 & SCR_EA)) { 9990 mask |= CPSR_A; 9991 } 9992 if (!(env->cp15.scr_el3 & SCR_IRQ)) { 9993 mask |= CPSR_I; 9994 } 9995 if (!(env->cp15.scr_el3 & SCR_FIQ)) { 9996 mask |= CPSR_F; 9997 } 9998 9999 addr += env->cp15.hvbar; 10000 10001 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr); 10002 } 10003 10004 static void arm_cpu_do_interrupt_aarch32(CPUState *cs) 10005 { 10006 ARMCPU *cpu = ARM_CPU(cs); 10007 CPUARMState *env = &cpu->env; 10008 uint32_t addr; 10009 uint32_t mask; 10010 int new_mode; 10011 uint32_t offset; 10012 uint32_t moe; 10013 10014 /* If this is a debug exception we must update the DBGDSCR.MOE bits */ 10015 switch (syn_get_ec(env->exception.syndrome)) { 10016 case EC_BREAKPOINT: 10017 case EC_BREAKPOINT_SAME_EL: 10018 moe = 1; 10019 break; 10020 case EC_WATCHPOINT: 10021 case EC_WATCHPOINT_SAME_EL: 10022 moe = 10; 10023 break; 10024 case EC_AA32_BKPT: 10025 moe = 3; 10026 break; 10027 case EC_VECTORCATCH: 10028 moe = 5; 10029 break; 10030 default: 10031 moe = 0; 10032 break; 10033 } 10034 10035 if (moe) { 10036 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); 10037 } 10038 10039 if (env->exception.target_el == 2) { 10040 arm_cpu_do_interrupt_aarch32_hyp(cs); 10041 return; 10042 } 10043 10044 switch (cs->exception_index) { 10045 case EXCP_UDEF: 10046 new_mode = ARM_CPU_MODE_UND; 10047 addr = 0x04; 10048 mask = CPSR_I; 10049 if (env->thumb) 10050 offset = 2; 10051 else 10052 offset = 4; 10053 break; 10054 case EXCP_SWI: 10055 new_mode = ARM_CPU_MODE_SVC; 10056 addr = 0x08; 10057 mask = CPSR_I; 10058 /* The PC already points to the next instruction. */ 10059 offset = 0; 10060 break; 10061 case EXCP_BKPT: 10062 /* Fall through to prefetch abort. */ 10063 case EXCP_PREFETCH_ABORT: 10064 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); 10065 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); 10066 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", 10067 env->exception.fsr, (uint32_t)env->exception.vaddress); 10068 new_mode = ARM_CPU_MODE_ABT; 10069 addr = 0x0c; 10070 mask = CPSR_A | CPSR_I; 10071 offset = 4; 10072 break; 10073 case EXCP_DATA_ABORT: 10074 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 10075 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); 10076 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", 10077 env->exception.fsr, 10078 (uint32_t)env->exception.vaddress); 10079 new_mode = ARM_CPU_MODE_ABT; 10080 addr = 0x10; 10081 mask = CPSR_A | CPSR_I; 10082 offset = 8; 10083 break; 10084 case EXCP_IRQ: 10085 new_mode = ARM_CPU_MODE_IRQ; 10086 addr = 0x18; 10087 /* Disable IRQ and imprecise data aborts. */ 10088 mask = CPSR_A | CPSR_I; 10089 offset = 4; 10090 if (env->cp15.scr_el3 & SCR_IRQ) { 10091 /* IRQ routed to monitor mode */ 10092 new_mode = ARM_CPU_MODE_MON; 10093 mask |= CPSR_F; 10094 } 10095 break; 10096 case EXCP_FIQ: 10097 new_mode = ARM_CPU_MODE_FIQ; 10098 addr = 0x1c; 10099 /* Disable FIQ, IRQ and imprecise data aborts. */ 10100 mask = CPSR_A | CPSR_I | CPSR_F; 10101 if (env->cp15.scr_el3 & SCR_FIQ) { 10102 /* FIQ routed to monitor mode */ 10103 new_mode = ARM_CPU_MODE_MON; 10104 } 10105 offset = 4; 10106 break; 10107 case EXCP_VIRQ: 10108 new_mode = ARM_CPU_MODE_IRQ; 10109 addr = 0x18; 10110 /* Disable IRQ and imprecise data aborts. */ 10111 mask = CPSR_A | CPSR_I; 10112 offset = 4; 10113 break; 10114 case EXCP_VFIQ: 10115 new_mode = ARM_CPU_MODE_FIQ; 10116 addr = 0x1c; 10117 /* Disable FIQ, IRQ and imprecise data aborts. */ 10118 mask = CPSR_A | CPSR_I | CPSR_F; 10119 offset = 4; 10120 break; 10121 case EXCP_SMC: 10122 new_mode = ARM_CPU_MODE_MON; 10123 addr = 0x08; 10124 mask = CPSR_A | CPSR_I | CPSR_F; 10125 offset = 0; 10126 break; 10127 default: 10128 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10129 return; /* Never happens. Keep compiler happy. */ 10130 } 10131 10132 if (new_mode == ARM_CPU_MODE_MON) { 10133 addr += env->cp15.mvbar; 10134 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 10135 /* High vectors. When enabled, base address cannot be remapped. */ 10136 addr += 0xffff0000; 10137 } else { 10138 /* ARM v7 architectures provide a vector base address register to remap 10139 * the interrupt vector table. 10140 * This register is only followed in non-monitor mode, and is banked. 10141 * Note: only bits 31:5 are valid. 10142 */ 10143 addr += A32_BANKED_CURRENT_REG_GET(env, vbar); 10144 } 10145 10146 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 10147 env->cp15.scr_el3 &= ~SCR_NS; 10148 } 10149 10150 take_aarch32_exception(env, new_mode, mask, offset, addr); 10151 } 10152 10153 /* Handle exception entry to a target EL which is using AArch64 */ 10154 static void arm_cpu_do_interrupt_aarch64(CPUState *cs) 10155 { 10156 ARMCPU *cpu = ARM_CPU(cs); 10157 CPUARMState *env = &cpu->env; 10158 unsigned int new_el = env->exception.target_el; 10159 target_ulong addr = env->cp15.vbar_el[new_el]; 10160 unsigned int new_mode = aarch64_pstate_mode(new_el, true); 10161 unsigned int cur_el = arm_current_el(env); 10162 10163 /* 10164 * Note that new_el can never be 0. If cur_el is 0, then 10165 * el0_a64 is is_a64(), else el0_a64 is ignored. 10166 */ 10167 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env)); 10168 10169 if (cur_el < new_el) { 10170 /* Entry vector offset depends on whether the implemented EL 10171 * immediately lower than the target level is using AArch32 or AArch64 10172 */ 10173 bool is_aa64; 10174 10175 switch (new_el) { 10176 case 3: 10177 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0; 10178 break; 10179 case 2: 10180 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0; 10181 break; 10182 case 1: 10183 is_aa64 = is_a64(env); 10184 break; 10185 default: 10186 g_assert_not_reached(); 10187 } 10188 10189 if (is_aa64) { 10190 addr += 0x400; 10191 } else { 10192 addr += 0x600; 10193 } 10194 } else if (pstate_read(env) & PSTATE_SP) { 10195 addr += 0x200; 10196 } 10197 10198 switch (cs->exception_index) { 10199 case EXCP_PREFETCH_ABORT: 10200 case EXCP_DATA_ABORT: 10201 env->cp15.far_el[new_el] = env->exception.vaddress; 10202 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n", 10203 env->cp15.far_el[new_el]); 10204 /* fall through */ 10205 case EXCP_BKPT: 10206 case EXCP_UDEF: 10207 case EXCP_SWI: 10208 case EXCP_HVC: 10209 case EXCP_HYP_TRAP: 10210 case EXCP_SMC: 10211 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) { 10212 /* 10213 * QEMU internal FP/SIMD syndromes from AArch32 include the 10214 * TA and coproc fields which are only exposed if the exception 10215 * is taken to AArch32 Hyp mode. Mask them out to get a valid 10216 * AArch64 format syndrome. 10217 */ 10218 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20); 10219 } 10220 env->cp15.esr_el[new_el] = env->exception.syndrome; 10221 break; 10222 case EXCP_IRQ: 10223 case EXCP_VIRQ: 10224 addr += 0x80; 10225 break; 10226 case EXCP_FIQ: 10227 case EXCP_VFIQ: 10228 addr += 0x100; 10229 break; 10230 case EXCP_SEMIHOST: 10231 qemu_log_mask(CPU_LOG_INT, 10232 "...handling as semihosting call 0x%" PRIx64 "\n", 10233 env->xregs[0]); 10234 env->xregs[0] = do_arm_semihosting(env); 10235 return; 10236 default: 10237 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10238 } 10239 10240 if (is_a64(env)) { 10241 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env); 10242 aarch64_save_sp(env, arm_current_el(env)); 10243 env->elr_el[new_el] = env->pc; 10244 } else { 10245 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env); 10246 env->elr_el[new_el] = env->regs[15]; 10247 10248 aarch64_sync_32_to_64(env); 10249 10250 env->condexec_bits = 0; 10251 } 10252 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n", 10253 env->elr_el[new_el]); 10254 10255 pstate_write(env, PSTATE_DAIF | new_mode); 10256 env->aarch64 = 1; 10257 aarch64_restore_sp(env, new_el); 10258 10259 env->pc = addr; 10260 10261 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n", 10262 new_el, env->pc, pstate_read(env)); 10263 } 10264 10265 static inline bool check_for_semihosting(CPUState *cs) 10266 { 10267 /* Check whether this exception is a semihosting call; if so 10268 * then handle it and return true; otherwise return false. 10269 */ 10270 ARMCPU *cpu = ARM_CPU(cs); 10271 CPUARMState *env = &cpu->env; 10272 10273 if (is_a64(env)) { 10274 if (cs->exception_index == EXCP_SEMIHOST) { 10275 /* This is always the 64-bit semihosting exception. 10276 * The "is this usermode" and "is semihosting enabled" 10277 * checks have been done at translate time. 10278 */ 10279 qemu_log_mask(CPU_LOG_INT, 10280 "...handling as semihosting call 0x%" PRIx64 "\n", 10281 env->xregs[0]); 10282 env->xregs[0] = do_arm_semihosting(env); 10283 return true; 10284 } 10285 return false; 10286 } else { 10287 uint32_t imm; 10288 10289 /* Only intercept calls from privileged modes, to provide some 10290 * semblance of security. 10291 */ 10292 if (cs->exception_index != EXCP_SEMIHOST && 10293 (!semihosting_enabled() || 10294 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) { 10295 return false; 10296 } 10297 10298 switch (cs->exception_index) { 10299 case EXCP_SEMIHOST: 10300 /* This is always a semihosting call; the "is this usermode" 10301 * and "is semihosting enabled" checks have been done at 10302 * translate time. 10303 */ 10304 break; 10305 case EXCP_SWI: 10306 /* Check for semihosting interrupt. */ 10307 if (env->thumb) { 10308 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env)) 10309 & 0xff; 10310 if (imm == 0xab) { 10311 break; 10312 } 10313 } else { 10314 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env)) 10315 & 0xffffff; 10316 if (imm == 0x123456) { 10317 break; 10318 } 10319 } 10320 return false; 10321 case EXCP_BKPT: 10322 /* See if this is a semihosting syscall. */ 10323 if (env->thumb) { 10324 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) 10325 & 0xff; 10326 if (imm == 0xab) { 10327 env->regs[15] += 2; 10328 break; 10329 } 10330 } 10331 return false; 10332 default: 10333 return false; 10334 } 10335 10336 qemu_log_mask(CPU_LOG_INT, 10337 "...handling as semihosting call 0x%x\n", 10338 env->regs[0]); 10339 env->regs[0] = do_arm_semihosting(env); 10340 return true; 10341 } 10342 } 10343 10344 /* Handle a CPU exception for A and R profile CPUs. 10345 * Do any appropriate logging, handle PSCI calls, and then hand off 10346 * to the AArch64-entry or AArch32-entry function depending on the 10347 * target exception level's register width. 10348 */ 10349 void arm_cpu_do_interrupt(CPUState *cs) 10350 { 10351 ARMCPU *cpu = ARM_CPU(cs); 10352 CPUARMState *env = &cpu->env; 10353 unsigned int new_el = env->exception.target_el; 10354 10355 assert(!arm_feature(env, ARM_FEATURE_M)); 10356 10357 arm_log_exception(cs->exception_index); 10358 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env), 10359 new_el); 10360 if (qemu_loglevel_mask(CPU_LOG_INT) 10361 && !excp_is_internal(cs->exception_index)) { 10362 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n", 10363 syn_get_ec(env->exception.syndrome), 10364 env->exception.syndrome); 10365 } 10366 10367 if (arm_is_psci_call(cpu, cs->exception_index)) { 10368 arm_handle_psci_call(cpu); 10369 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); 10370 return; 10371 } 10372 10373 /* Semihosting semantics depend on the register width of the 10374 * code that caused the exception, not the target exception level, 10375 * so must be handled here. 10376 */ 10377 if (check_for_semihosting(cs)) { 10378 return; 10379 } 10380 10381 /* Hooks may change global state so BQL should be held, also the 10382 * BQL needs to be held for any modification of 10383 * cs->interrupt_request. 10384 */ 10385 g_assert(qemu_mutex_iothread_locked()); 10386 10387 arm_call_pre_el_change_hook(cpu); 10388 10389 assert(!excp_is_internal(cs->exception_index)); 10390 if (arm_el_is_aa64(env, new_el)) { 10391 arm_cpu_do_interrupt_aarch64(cs); 10392 } else { 10393 arm_cpu_do_interrupt_aarch32(cs); 10394 } 10395 10396 arm_call_el_change_hook(cpu); 10397 10398 if (!kvm_enabled()) { 10399 cs->interrupt_request |= CPU_INTERRUPT_EXITTB; 10400 } 10401 } 10402 #endif /* !CONFIG_USER_ONLY */ 10403 10404 /* Return the exception level which controls this address translation regime */ 10405 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx) 10406 { 10407 switch (mmu_idx) { 10408 case ARMMMUIdx_S2NS: 10409 case ARMMMUIdx_S1E2: 10410 return 2; 10411 case ARMMMUIdx_S1E3: 10412 return 3; 10413 case ARMMMUIdx_S1SE0: 10414 return arm_el_is_aa64(env, 3) ? 1 : 3; 10415 case ARMMMUIdx_S1SE1: 10416 case ARMMMUIdx_S1NSE0: 10417 case ARMMMUIdx_S1NSE1: 10418 case ARMMMUIdx_MPrivNegPri: 10419 case ARMMMUIdx_MUserNegPri: 10420 case ARMMMUIdx_MPriv: 10421 case ARMMMUIdx_MUser: 10422 case ARMMMUIdx_MSPrivNegPri: 10423 case ARMMMUIdx_MSUserNegPri: 10424 case ARMMMUIdx_MSPriv: 10425 case ARMMMUIdx_MSUser: 10426 return 1; 10427 default: 10428 g_assert_not_reached(); 10429 } 10430 } 10431 10432 #ifndef CONFIG_USER_ONLY 10433 10434 /* Return the SCTLR value which controls this address translation regime */ 10435 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx) 10436 { 10437 return env->cp15.sctlr_el[regime_el(env, mmu_idx)]; 10438 } 10439 10440 /* Return true if the specified stage of address translation is disabled */ 10441 static inline bool regime_translation_disabled(CPUARMState *env, 10442 ARMMMUIdx mmu_idx) 10443 { 10444 if (arm_feature(env, ARM_FEATURE_M)) { 10445 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] & 10446 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) { 10447 case R_V7M_MPU_CTRL_ENABLE_MASK: 10448 /* Enabled, but not for HardFault and NMI */ 10449 return mmu_idx & ARM_MMU_IDX_M_NEGPRI; 10450 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK: 10451 /* Enabled for all cases */ 10452 return false; 10453 case 0: 10454 default: 10455 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but 10456 * we warned about that in armv7m_nvic.c when the guest set it. 10457 */ 10458 return true; 10459 } 10460 } 10461 10462 if (mmu_idx == ARMMMUIdx_S2NS) { 10463 /* HCR.DC means HCR.VM behaves as 1 */ 10464 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0; 10465 } 10466 10467 if (env->cp15.hcr_el2 & HCR_TGE) { 10468 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */ 10469 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) { 10470 return true; 10471 } 10472 } 10473 10474 if ((env->cp15.hcr_el2 & HCR_DC) && 10475 (mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1)) { 10476 /* HCR.DC means SCTLR_EL1.M behaves as 0 */ 10477 return true; 10478 } 10479 10480 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0; 10481 } 10482 10483 static inline bool regime_translation_big_endian(CPUARMState *env, 10484 ARMMMUIdx mmu_idx) 10485 { 10486 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0; 10487 } 10488 10489 /* Return the TTBR associated with this translation regime */ 10490 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx, 10491 int ttbrn) 10492 { 10493 if (mmu_idx == ARMMMUIdx_S2NS) { 10494 return env->cp15.vttbr_el2; 10495 } 10496 if (ttbrn == 0) { 10497 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)]; 10498 } else { 10499 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)]; 10500 } 10501 } 10502 10503 #endif /* !CONFIG_USER_ONLY */ 10504 10505 /* Return the TCR controlling this translation regime */ 10506 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx) 10507 { 10508 if (mmu_idx == ARMMMUIdx_S2NS) { 10509 return &env->cp15.vtcr_el2; 10510 } 10511 return &env->cp15.tcr_el[regime_el(env, mmu_idx)]; 10512 } 10513 10514 /* Convert a possible stage1+2 MMU index into the appropriate 10515 * stage 1 MMU index 10516 */ 10517 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx) 10518 { 10519 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 10520 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0); 10521 } 10522 return mmu_idx; 10523 } 10524 10525 /* Return true if the translation regime is using LPAE format page tables */ 10526 static inline bool regime_using_lpae_format(CPUARMState *env, 10527 ARMMMUIdx mmu_idx) 10528 { 10529 int el = regime_el(env, mmu_idx); 10530 if (el == 2 || arm_el_is_aa64(env, el)) { 10531 return true; 10532 } 10533 if (arm_feature(env, ARM_FEATURE_LPAE) 10534 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) { 10535 return true; 10536 } 10537 return false; 10538 } 10539 10540 /* Returns true if the stage 1 translation regime is using LPAE format page 10541 * tables. Used when raising alignment exceptions, whose FSR changes depending 10542 * on whether the long or short descriptor format is in use. */ 10543 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx) 10544 { 10545 mmu_idx = stage_1_mmu_idx(mmu_idx); 10546 10547 return regime_using_lpae_format(env, mmu_idx); 10548 } 10549 10550 #ifndef CONFIG_USER_ONLY 10551 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx) 10552 { 10553 switch (mmu_idx) { 10554 case ARMMMUIdx_S1SE0: 10555 case ARMMMUIdx_S1NSE0: 10556 case ARMMMUIdx_MUser: 10557 case ARMMMUIdx_MSUser: 10558 case ARMMMUIdx_MUserNegPri: 10559 case ARMMMUIdx_MSUserNegPri: 10560 return true; 10561 default: 10562 return false; 10563 case ARMMMUIdx_S12NSE0: 10564 case ARMMMUIdx_S12NSE1: 10565 g_assert_not_reached(); 10566 } 10567 } 10568 10569 /* Translate section/page access permissions to page 10570 * R/W protection flags 10571 * 10572 * @env: CPUARMState 10573 * @mmu_idx: MMU index indicating required translation regime 10574 * @ap: The 3-bit access permissions (AP[2:0]) 10575 * @domain_prot: The 2-bit domain access permissions 10576 */ 10577 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, 10578 int ap, int domain_prot) 10579 { 10580 bool is_user = regime_is_user(env, mmu_idx); 10581 10582 if (domain_prot == 3) { 10583 return PAGE_READ | PAGE_WRITE; 10584 } 10585 10586 switch (ap) { 10587 case 0: 10588 if (arm_feature(env, ARM_FEATURE_V7)) { 10589 return 0; 10590 } 10591 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) { 10592 case SCTLR_S: 10593 return is_user ? 0 : PAGE_READ; 10594 case SCTLR_R: 10595 return PAGE_READ; 10596 default: 10597 return 0; 10598 } 10599 case 1: 10600 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 10601 case 2: 10602 if (is_user) { 10603 return PAGE_READ; 10604 } else { 10605 return PAGE_READ | PAGE_WRITE; 10606 } 10607 case 3: 10608 return PAGE_READ | PAGE_WRITE; 10609 case 4: /* Reserved. */ 10610 return 0; 10611 case 5: 10612 return is_user ? 0 : PAGE_READ; 10613 case 6: 10614 return PAGE_READ; 10615 case 7: 10616 if (!arm_feature(env, ARM_FEATURE_V6K)) { 10617 return 0; 10618 } 10619 return PAGE_READ; 10620 default: 10621 g_assert_not_reached(); 10622 } 10623 } 10624 10625 /* Translate section/page access permissions to page 10626 * R/W protection flags. 10627 * 10628 * @ap: The 2-bit simple AP (AP[2:1]) 10629 * @is_user: TRUE if accessing from PL0 10630 */ 10631 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user) 10632 { 10633 switch (ap) { 10634 case 0: 10635 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 10636 case 1: 10637 return PAGE_READ | PAGE_WRITE; 10638 case 2: 10639 return is_user ? 0 : PAGE_READ; 10640 case 3: 10641 return PAGE_READ; 10642 default: 10643 g_assert_not_reached(); 10644 } 10645 } 10646 10647 static inline int 10648 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap) 10649 { 10650 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx)); 10651 } 10652 10653 /* Translate S2 section/page access permissions to protection flags 10654 * 10655 * @env: CPUARMState 10656 * @s2ap: The 2-bit stage2 access permissions (S2AP) 10657 * @xn: XN (execute-never) bit 10658 */ 10659 static int get_S2prot(CPUARMState *env, int s2ap, int xn) 10660 { 10661 int prot = 0; 10662 10663 if (s2ap & 1) { 10664 prot |= PAGE_READ; 10665 } 10666 if (s2ap & 2) { 10667 prot |= PAGE_WRITE; 10668 } 10669 if (!xn) { 10670 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) { 10671 prot |= PAGE_EXEC; 10672 } 10673 } 10674 return prot; 10675 } 10676 10677 /* Translate section/page access permissions to protection flags 10678 * 10679 * @env: CPUARMState 10680 * @mmu_idx: MMU index indicating required translation regime 10681 * @is_aa64: TRUE if AArch64 10682 * @ap: The 2-bit simple AP (AP[2:1]) 10683 * @ns: NS (non-secure) bit 10684 * @xn: XN (execute-never) bit 10685 * @pxn: PXN (privileged execute-never) bit 10686 */ 10687 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64, 10688 int ap, int ns, int xn, int pxn) 10689 { 10690 bool is_user = regime_is_user(env, mmu_idx); 10691 int prot_rw, user_rw; 10692 bool have_wxn; 10693 int wxn = 0; 10694 10695 assert(mmu_idx != ARMMMUIdx_S2NS); 10696 10697 user_rw = simple_ap_to_rw_prot_is_user(ap, true); 10698 if (is_user) { 10699 prot_rw = user_rw; 10700 } else { 10701 prot_rw = simple_ap_to_rw_prot_is_user(ap, false); 10702 } 10703 10704 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) { 10705 return prot_rw; 10706 } 10707 10708 /* TODO have_wxn should be replaced with 10709 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2) 10710 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE 10711 * compatible processors have EL2, which is required for [U]WXN. 10712 */ 10713 have_wxn = arm_feature(env, ARM_FEATURE_LPAE); 10714 10715 if (have_wxn) { 10716 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN; 10717 } 10718 10719 if (is_aa64) { 10720 switch (regime_el(env, mmu_idx)) { 10721 case 1: 10722 if (!is_user) { 10723 xn = pxn || (user_rw & PAGE_WRITE); 10724 } 10725 break; 10726 case 2: 10727 case 3: 10728 break; 10729 } 10730 } else if (arm_feature(env, ARM_FEATURE_V7)) { 10731 switch (regime_el(env, mmu_idx)) { 10732 case 1: 10733 case 3: 10734 if (is_user) { 10735 xn = xn || !(user_rw & PAGE_READ); 10736 } else { 10737 int uwxn = 0; 10738 if (have_wxn) { 10739 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN; 10740 } 10741 xn = xn || !(prot_rw & PAGE_READ) || pxn || 10742 (uwxn && (user_rw & PAGE_WRITE)); 10743 } 10744 break; 10745 case 2: 10746 break; 10747 } 10748 } else { 10749 xn = wxn = 0; 10750 } 10751 10752 if (xn || (wxn && (prot_rw & PAGE_WRITE))) { 10753 return prot_rw; 10754 } 10755 return prot_rw | PAGE_EXEC; 10756 } 10757 10758 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx, 10759 uint32_t *table, uint32_t address) 10760 { 10761 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */ 10762 TCR *tcr = regime_tcr(env, mmu_idx); 10763 10764 if (address & tcr->mask) { 10765 if (tcr->raw_tcr & TTBCR_PD1) { 10766 /* Translation table walk disabled for TTBR1 */ 10767 return false; 10768 } 10769 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000; 10770 } else { 10771 if (tcr->raw_tcr & TTBCR_PD0) { 10772 /* Translation table walk disabled for TTBR0 */ 10773 return false; 10774 } 10775 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask; 10776 } 10777 *table |= (address >> 18) & 0x3ffc; 10778 return true; 10779 } 10780 10781 /* Translate a S1 pagetable walk through S2 if needed. */ 10782 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx, 10783 hwaddr addr, MemTxAttrs txattrs, 10784 ARMMMUFaultInfo *fi) 10785 { 10786 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) && 10787 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) { 10788 target_ulong s2size; 10789 hwaddr s2pa; 10790 int s2prot; 10791 int ret; 10792 ARMCacheAttrs cacheattrs = {}; 10793 ARMCacheAttrs *pcacheattrs = NULL; 10794 10795 if (env->cp15.hcr_el2 & HCR_PTW) { 10796 /* 10797 * PTW means we must fault if this S1 walk touches S2 Device 10798 * memory; otherwise we don't care about the attributes and can 10799 * save the S2 translation the effort of computing them. 10800 */ 10801 pcacheattrs = &cacheattrs; 10802 } 10803 10804 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa, 10805 &txattrs, &s2prot, &s2size, fi, pcacheattrs); 10806 if (ret) { 10807 assert(fi->type != ARMFault_None); 10808 fi->s2addr = addr; 10809 fi->stage2 = true; 10810 fi->s1ptw = true; 10811 return ~0; 10812 } 10813 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) { 10814 /* Access was to Device memory: generate Permission fault */ 10815 fi->type = ARMFault_Permission; 10816 fi->s2addr = addr; 10817 fi->stage2 = true; 10818 fi->s1ptw = true; 10819 return ~0; 10820 } 10821 addr = s2pa; 10822 } 10823 return addr; 10824 } 10825 10826 /* All loads done in the course of a page table walk go through here. */ 10827 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure, 10828 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 10829 { 10830 ARMCPU *cpu = ARM_CPU(cs); 10831 CPUARMState *env = &cpu->env; 10832 MemTxAttrs attrs = {}; 10833 MemTxResult result = MEMTX_OK; 10834 AddressSpace *as; 10835 uint32_t data; 10836 10837 attrs.secure = is_secure; 10838 as = arm_addressspace(cs, attrs); 10839 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 10840 if (fi->s1ptw) { 10841 return 0; 10842 } 10843 if (regime_translation_big_endian(env, mmu_idx)) { 10844 data = address_space_ldl_be(as, addr, attrs, &result); 10845 } else { 10846 data = address_space_ldl_le(as, addr, attrs, &result); 10847 } 10848 if (result == MEMTX_OK) { 10849 return data; 10850 } 10851 fi->type = ARMFault_SyncExternalOnWalk; 10852 fi->ea = arm_extabort_type(result); 10853 return 0; 10854 } 10855 10856 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure, 10857 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 10858 { 10859 ARMCPU *cpu = ARM_CPU(cs); 10860 CPUARMState *env = &cpu->env; 10861 MemTxAttrs attrs = {}; 10862 MemTxResult result = MEMTX_OK; 10863 AddressSpace *as; 10864 uint64_t data; 10865 10866 attrs.secure = is_secure; 10867 as = arm_addressspace(cs, attrs); 10868 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 10869 if (fi->s1ptw) { 10870 return 0; 10871 } 10872 if (regime_translation_big_endian(env, mmu_idx)) { 10873 data = address_space_ldq_be(as, addr, attrs, &result); 10874 } else { 10875 data = address_space_ldq_le(as, addr, attrs, &result); 10876 } 10877 if (result == MEMTX_OK) { 10878 return data; 10879 } 10880 fi->type = ARMFault_SyncExternalOnWalk; 10881 fi->ea = arm_extabort_type(result); 10882 return 0; 10883 } 10884 10885 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address, 10886 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10887 hwaddr *phys_ptr, int *prot, 10888 target_ulong *page_size, 10889 ARMMMUFaultInfo *fi) 10890 { 10891 CPUState *cs = CPU(arm_env_get_cpu(env)); 10892 int level = 1; 10893 uint32_t table; 10894 uint32_t desc; 10895 int type; 10896 int ap; 10897 int domain = 0; 10898 int domain_prot; 10899 hwaddr phys_addr; 10900 uint32_t dacr; 10901 10902 /* Pagetable walk. */ 10903 /* Lookup l1 descriptor. */ 10904 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 10905 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 10906 fi->type = ARMFault_Translation; 10907 goto do_fault; 10908 } 10909 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 10910 mmu_idx, fi); 10911 if (fi->type != ARMFault_None) { 10912 goto do_fault; 10913 } 10914 type = (desc & 3); 10915 domain = (desc >> 5) & 0x0f; 10916 if (regime_el(env, mmu_idx) == 1) { 10917 dacr = env->cp15.dacr_ns; 10918 } else { 10919 dacr = env->cp15.dacr_s; 10920 } 10921 domain_prot = (dacr >> (domain * 2)) & 3; 10922 if (type == 0) { 10923 /* Section translation fault. */ 10924 fi->type = ARMFault_Translation; 10925 goto do_fault; 10926 } 10927 if (type != 2) { 10928 level = 2; 10929 } 10930 if (domain_prot == 0 || domain_prot == 2) { 10931 fi->type = ARMFault_Domain; 10932 goto do_fault; 10933 } 10934 if (type == 2) { 10935 /* 1Mb section. */ 10936 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 10937 ap = (desc >> 10) & 3; 10938 *page_size = 1024 * 1024; 10939 } else { 10940 /* Lookup l2 entry. */ 10941 if (type == 1) { 10942 /* Coarse pagetable. */ 10943 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 10944 } else { 10945 /* Fine pagetable. */ 10946 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); 10947 } 10948 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 10949 mmu_idx, fi); 10950 if (fi->type != ARMFault_None) { 10951 goto do_fault; 10952 } 10953 switch (desc & 3) { 10954 case 0: /* Page translation fault. */ 10955 fi->type = ARMFault_Translation; 10956 goto do_fault; 10957 case 1: /* 64k page. */ 10958 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 10959 ap = (desc >> (4 + ((address >> 13) & 6))) & 3; 10960 *page_size = 0x10000; 10961 break; 10962 case 2: /* 4k page. */ 10963 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 10964 ap = (desc >> (4 + ((address >> 9) & 6))) & 3; 10965 *page_size = 0x1000; 10966 break; 10967 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */ 10968 if (type == 1) { 10969 /* ARMv6/XScale extended small page format */ 10970 if (arm_feature(env, ARM_FEATURE_XSCALE) 10971 || arm_feature(env, ARM_FEATURE_V6)) { 10972 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 10973 *page_size = 0x1000; 10974 } else { 10975 /* UNPREDICTABLE in ARMv5; we choose to take a 10976 * page translation fault. 10977 */ 10978 fi->type = ARMFault_Translation; 10979 goto do_fault; 10980 } 10981 } else { 10982 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); 10983 *page_size = 0x400; 10984 } 10985 ap = (desc >> 4) & 3; 10986 break; 10987 default: 10988 /* Never happens, but compiler isn't smart enough to tell. */ 10989 abort(); 10990 } 10991 } 10992 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 10993 *prot |= *prot ? PAGE_EXEC : 0; 10994 if (!(*prot & (1 << access_type))) { 10995 /* Access permission fault. */ 10996 fi->type = ARMFault_Permission; 10997 goto do_fault; 10998 } 10999 *phys_ptr = phys_addr; 11000 return false; 11001 do_fault: 11002 fi->domain = domain; 11003 fi->level = level; 11004 return true; 11005 } 11006 11007 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address, 11008 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11009 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 11010 target_ulong *page_size, ARMMMUFaultInfo *fi) 11011 { 11012 CPUState *cs = CPU(arm_env_get_cpu(env)); 11013 int level = 1; 11014 uint32_t table; 11015 uint32_t desc; 11016 uint32_t xn; 11017 uint32_t pxn = 0; 11018 int type; 11019 int ap; 11020 int domain = 0; 11021 int domain_prot; 11022 hwaddr phys_addr; 11023 uint32_t dacr; 11024 bool ns; 11025 11026 /* Pagetable walk. */ 11027 /* Lookup l1 descriptor. */ 11028 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 11029 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 11030 fi->type = ARMFault_Translation; 11031 goto do_fault; 11032 } 11033 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 11034 mmu_idx, fi); 11035 if (fi->type != ARMFault_None) { 11036 goto do_fault; 11037 } 11038 type = (desc & 3); 11039 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) { 11040 /* Section translation fault, or attempt to use the encoding 11041 * which is Reserved on implementations without PXN. 11042 */ 11043 fi->type = ARMFault_Translation; 11044 goto do_fault; 11045 } 11046 if ((type == 1) || !(desc & (1 << 18))) { 11047 /* Page or Section. */ 11048 domain = (desc >> 5) & 0x0f; 11049 } 11050 if (regime_el(env, mmu_idx) == 1) { 11051 dacr = env->cp15.dacr_ns; 11052 } else { 11053 dacr = env->cp15.dacr_s; 11054 } 11055 if (type == 1) { 11056 level = 2; 11057 } 11058 domain_prot = (dacr >> (domain * 2)) & 3; 11059 if (domain_prot == 0 || domain_prot == 2) { 11060 /* Section or Page domain fault */ 11061 fi->type = ARMFault_Domain; 11062 goto do_fault; 11063 } 11064 if (type != 1) { 11065 if (desc & (1 << 18)) { 11066 /* Supersection. */ 11067 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); 11068 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32; 11069 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36; 11070 *page_size = 0x1000000; 11071 } else { 11072 /* Section. */ 11073 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 11074 *page_size = 0x100000; 11075 } 11076 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); 11077 xn = desc & (1 << 4); 11078 pxn = desc & 1; 11079 ns = extract32(desc, 19, 1); 11080 } else { 11081 if (arm_feature(env, ARM_FEATURE_PXN)) { 11082 pxn = (desc >> 2) & 1; 11083 } 11084 ns = extract32(desc, 3, 1); 11085 /* Lookup l2 entry. */ 11086 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 11087 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 11088 mmu_idx, fi); 11089 if (fi->type != ARMFault_None) { 11090 goto do_fault; 11091 } 11092 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); 11093 switch (desc & 3) { 11094 case 0: /* Page translation fault. */ 11095 fi->type = ARMFault_Translation; 11096 goto do_fault; 11097 case 1: /* 64k page. */ 11098 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 11099 xn = desc & (1 << 15); 11100 *page_size = 0x10000; 11101 break; 11102 case 2: case 3: /* 4k page. */ 11103 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 11104 xn = desc & 1; 11105 *page_size = 0x1000; 11106 break; 11107 default: 11108 /* Never happens, but compiler isn't smart enough to tell. */ 11109 abort(); 11110 } 11111 } 11112 if (domain_prot == 3) { 11113 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 11114 } else { 11115 if (pxn && !regime_is_user(env, mmu_idx)) { 11116 xn = 1; 11117 } 11118 if (xn && access_type == MMU_INST_FETCH) { 11119 fi->type = ARMFault_Permission; 11120 goto do_fault; 11121 } 11122 11123 if (arm_feature(env, ARM_FEATURE_V6K) && 11124 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) { 11125 /* The simplified model uses AP[0] as an access control bit. */ 11126 if ((ap & 1) == 0) { 11127 /* Access flag fault. */ 11128 fi->type = ARMFault_AccessFlag; 11129 goto do_fault; 11130 } 11131 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1); 11132 } else { 11133 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 11134 } 11135 if (*prot && !xn) { 11136 *prot |= PAGE_EXEC; 11137 } 11138 if (!(*prot & (1 << access_type))) { 11139 /* Access permission fault. */ 11140 fi->type = ARMFault_Permission; 11141 goto do_fault; 11142 } 11143 } 11144 if (ns) { 11145 /* The NS bit will (as required by the architecture) have no effect if 11146 * the CPU doesn't support TZ or this is a non-secure translation 11147 * regime, because the attribute will already be non-secure. 11148 */ 11149 attrs->secure = false; 11150 } 11151 *phys_ptr = phys_addr; 11152 return false; 11153 do_fault: 11154 fi->domain = domain; 11155 fi->level = level; 11156 return true; 11157 } 11158 11159 /* 11160 * check_s2_mmu_setup 11161 * @cpu: ARMCPU 11162 * @is_aa64: True if the translation regime is in AArch64 state 11163 * @startlevel: Suggested starting level 11164 * @inputsize: Bitsize of IPAs 11165 * @stride: Page-table stride (See the ARM ARM) 11166 * 11167 * Returns true if the suggested S2 translation parameters are OK and 11168 * false otherwise. 11169 */ 11170 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level, 11171 int inputsize, int stride) 11172 { 11173 const int grainsize = stride + 3; 11174 int startsizecheck; 11175 11176 /* Negative levels are never allowed. */ 11177 if (level < 0) { 11178 return false; 11179 } 11180 11181 startsizecheck = inputsize - ((3 - level) * stride + grainsize); 11182 if (startsizecheck < 1 || startsizecheck > stride + 4) { 11183 return false; 11184 } 11185 11186 if (is_aa64) { 11187 CPUARMState *env = &cpu->env; 11188 unsigned int pamax = arm_pamax(cpu); 11189 11190 switch (stride) { 11191 case 13: /* 64KB Pages. */ 11192 if (level == 0 || (level == 1 && pamax <= 42)) { 11193 return false; 11194 } 11195 break; 11196 case 11: /* 16KB Pages. */ 11197 if (level == 0 || (level == 1 && pamax <= 40)) { 11198 return false; 11199 } 11200 break; 11201 case 9: /* 4KB Pages. */ 11202 if (level == 0 && pamax <= 42) { 11203 return false; 11204 } 11205 break; 11206 default: 11207 g_assert_not_reached(); 11208 } 11209 11210 /* Inputsize checks. */ 11211 if (inputsize > pamax && 11212 (arm_el_is_aa64(env, 1) || inputsize > 40)) { 11213 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */ 11214 return false; 11215 } 11216 } else { 11217 /* AArch32 only supports 4KB pages. Assert on that. */ 11218 assert(stride == 9); 11219 11220 if (level == 0) { 11221 return false; 11222 } 11223 } 11224 return true; 11225 } 11226 11227 /* Translate from the 4-bit stage 2 representation of 11228 * memory attributes (without cache-allocation hints) to 11229 * the 8-bit representation of the stage 1 MAIR registers 11230 * (which includes allocation hints). 11231 * 11232 * ref: shared/translation/attrs/S2AttrDecode() 11233 * .../S2ConvertAttrsHints() 11234 */ 11235 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs) 11236 { 11237 uint8_t hiattr = extract32(s2attrs, 2, 2); 11238 uint8_t loattr = extract32(s2attrs, 0, 2); 11239 uint8_t hihint = 0, lohint = 0; 11240 11241 if (hiattr != 0) { /* normal memory */ 11242 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */ 11243 hiattr = loattr = 1; /* non-cacheable */ 11244 } else { 11245 if (hiattr != 1) { /* Write-through or write-back */ 11246 hihint = 3; /* RW allocate */ 11247 } 11248 if (loattr != 1) { /* Write-through or write-back */ 11249 lohint = 3; /* RW allocate */ 11250 } 11251 } 11252 } 11253 11254 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint; 11255 } 11256 #endif /* !CONFIG_USER_ONLY */ 11257 11258 ARMVAParameters aa64_va_parameters_both(CPUARMState *env, uint64_t va, 11259 ARMMMUIdx mmu_idx) 11260 { 11261 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr; 11262 uint32_t el = regime_el(env, mmu_idx); 11263 bool tbi, tbid, epd, hpd, using16k, using64k; 11264 int select, tsz; 11265 11266 /* 11267 * Bit 55 is always between the two regions, and is canonical for 11268 * determining if address tagging is enabled. 11269 */ 11270 select = extract64(va, 55, 1); 11271 11272 if (el > 1) { 11273 tsz = extract32(tcr, 0, 6); 11274 using64k = extract32(tcr, 14, 1); 11275 using16k = extract32(tcr, 15, 1); 11276 if (mmu_idx == ARMMMUIdx_S2NS) { 11277 /* VTCR_EL2 */ 11278 tbi = tbid = hpd = false; 11279 } else { 11280 tbi = extract32(tcr, 20, 1); 11281 hpd = extract32(tcr, 24, 1); 11282 tbid = extract32(tcr, 29, 1); 11283 } 11284 epd = false; 11285 } else if (!select) { 11286 tsz = extract32(tcr, 0, 6); 11287 epd = extract32(tcr, 7, 1); 11288 using64k = extract32(tcr, 14, 1); 11289 using16k = extract32(tcr, 15, 1); 11290 tbi = extract64(tcr, 37, 1); 11291 hpd = extract64(tcr, 41, 1); 11292 tbid = extract64(tcr, 51, 1); 11293 } else { 11294 int tg = extract32(tcr, 30, 2); 11295 using16k = tg == 1; 11296 using64k = tg == 3; 11297 tsz = extract32(tcr, 16, 6); 11298 epd = extract32(tcr, 23, 1); 11299 tbi = extract64(tcr, 38, 1); 11300 hpd = extract64(tcr, 42, 1); 11301 tbid = extract64(tcr, 52, 1); 11302 } 11303 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */ 11304 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */ 11305 11306 return (ARMVAParameters) { 11307 .tsz = tsz, 11308 .select = select, 11309 .tbi = tbi, 11310 .tbid = tbid, 11311 .epd = epd, 11312 .hpd = hpd, 11313 .using16k = using16k, 11314 .using64k = using64k, 11315 }; 11316 } 11317 11318 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va, 11319 ARMMMUIdx mmu_idx, bool data) 11320 { 11321 ARMVAParameters ret = aa64_va_parameters_both(env, va, mmu_idx); 11322 11323 /* Present TBI as a composite with TBID. */ 11324 ret.tbi &= (data || !ret.tbid); 11325 return ret; 11326 } 11327 11328 #ifndef CONFIG_USER_ONLY 11329 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va, 11330 ARMMMUIdx mmu_idx) 11331 { 11332 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr; 11333 uint32_t el = regime_el(env, mmu_idx); 11334 int select, tsz; 11335 bool epd, hpd; 11336 11337 if (mmu_idx == ARMMMUIdx_S2NS) { 11338 /* VTCR */ 11339 bool sext = extract32(tcr, 4, 1); 11340 bool sign = extract32(tcr, 3, 1); 11341 11342 /* 11343 * If the sign-extend bit is not the same as t0sz[3], the result 11344 * is unpredictable. Flag this as a guest error. 11345 */ 11346 if (sign != sext) { 11347 qemu_log_mask(LOG_GUEST_ERROR, 11348 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n"); 11349 } 11350 tsz = sextract32(tcr, 0, 4) + 8; 11351 select = 0; 11352 hpd = false; 11353 epd = false; 11354 } else if (el == 2) { 11355 /* HTCR */ 11356 tsz = extract32(tcr, 0, 3); 11357 select = 0; 11358 hpd = extract64(tcr, 24, 1); 11359 epd = false; 11360 } else { 11361 int t0sz = extract32(tcr, 0, 3); 11362 int t1sz = extract32(tcr, 16, 3); 11363 11364 if (t1sz == 0) { 11365 select = va > (0xffffffffu >> t0sz); 11366 } else { 11367 /* Note that we will detect errors later. */ 11368 select = va >= ~(0xffffffffu >> t1sz); 11369 } 11370 if (!select) { 11371 tsz = t0sz; 11372 epd = extract32(tcr, 7, 1); 11373 hpd = extract64(tcr, 41, 1); 11374 } else { 11375 tsz = t1sz; 11376 epd = extract32(tcr, 23, 1); 11377 hpd = extract64(tcr, 42, 1); 11378 } 11379 /* For aarch32, hpd0 is not enabled without t2e as well. */ 11380 hpd &= extract32(tcr, 6, 1); 11381 } 11382 11383 return (ARMVAParameters) { 11384 .tsz = tsz, 11385 .select = select, 11386 .epd = epd, 11387 .hpd = hpd, 11388 }; 11389 } 11390 11391 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, 11392 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11393 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 11394 target_ulong *page_size_ptr, 11395 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 11396 { 11397 ARMCPU *cpu = arm_env_get_cpu(env); 11398 CPUState *cs = CPU(cpu); 11399 /* Read an LPAE long-descriptor translation table. */ 11400 ARMFaultType fault_type = ARMFault_Translation; 11401 uint32_t level; 11402 ARMVAParameters param; 11403 uint64_t ttbr; 11404 hwaddr descaddr, indexmask, indexmask_grainsize; 11405 uint32_t tableattrs; 11406 target_ulong page_size; 11407 uint32_t attrs; 11408 int32_t stride; 11409 int addrsize, inputsize; 11410 TCR *tcr = regime_tcr(env, mmu_idx); 11411 int ap, ns, xn, pxn; 11412 uint32_t el = regime_el(env, mmu_idx); 11413 bool ttbr1_valid; 11414 uint64_t descaddrmask; 11415 bool aarch64 = arm_el_is_aa64(env, el); 11416 bool guarded = false; 11417 11418 /* TODO: 11419 * This code does not handle the different format TCR for VTCR_EL2. 11420 * This code also does not support shareability levels. 11421 * Attribute and permission bit handling should also be checked when adding 11422 * support for those page table walks. 11423 */ 11424 if (aarch64) { 11425 param = aa64_va_parameters(env, address, mmu_idx, 11426 access_type != MMU_INST_FETCH); 11427 level = 0; 11428 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it 11429 * invalid. 11430 */ 11431 ttbr1_valid = (el < 2); 11432 addrsize = 64 - 8 * param.tbi; 11433 inputsize = 64 - param.tsz; 11434 } else { 11435 param = aa32_va_parameters(env, address, mmu_idx); 11436 level = 1; 11437 /* There is no TTBR1 for EL2 */ 11438 ttbr1_valid = (el != 2); 11439 addrsize = (mmu_idx == ARMMMUIdx_S2NS ? 40 : 32); 11440 inputsize = addrsize - param.tsz; 11441 } 11442 11443 /* 11444 * We determined the region when collecting the parameters, but we 11445 * have not yet validated that the address is valid for the region. 11446 * Extract the top bits and verify that they all match select. 11447 * 11448 * For aa32, if inputsize == addrsize, then we have selected the 11449 * region by exclusion in aa32_va_parameters and there is no more 11450 * validation to do here. 11451 */ 11452 if (inputsize < addrsize) { 11453 target_ulong top_bits = sextract64(address, inputsize, 11454 addrsize - inputsize); 11455 if (-top_bits != param.select || (param.select && !ttbr1_valid)) { 11456 /* The gap between the two regions is a Translation fault */ 11457 fault_type = ARMFault_Translation; 11458 goto do_fault; 11459 } 11460 } 11461 11462 if (param.using64k) { 11463 stride = 13; 11464 } else if (param.using16k) { 11465 stride = 11; 11466 } else { 11467 stride = 9; 11468 } 11469 11470 /* Note that QEMU ignores shareability and cacheability attributes, 11471 * so we don't need to do anything with the SH, ORGN, IRGN fields 11472 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the 11473 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently 11474 * implement any ASID-like capability so we can ignore it (instead 11475 * we will always flush the TLB any time the ASID is changed). 11476 */ 11477 ttbr = regime_ttbr(env, mmu_idx, param.select); 11478 11479 /* Here we should have set up all the parameters for the translation: 11480 * inputsize, ttbr, epd, stride, tbi 11481 */ 11482 11483 if (param.epd) { 11484 /* Translation table walk disabled => Translation fault on TLB miss 11485 * Note: This is always 0 on 64-bit EL2 and EL3. 11486 */ 11487 goto do_fault; 11488 } 11489 11490 if (mmu_idx != ARMMMUIdx_S2NS) { 11491 /* The starting level depends on the virtual address size (which can 11492 * be up to 48 bits) and the translation granule size. It indicates 11493 * the number of strides (stride bits at a time) needed to 11494 * consume the bits of the input address. In the pseudocode this is: 11495 * level = 4 - RoundUp((inputsize - grainsize) / stride) 11496 * where their 'inputsize' is our 'inputsize', 'grainsize' is 11497 * our 'stride + 3' and 'stride' is our 'stride'. 11498 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying: 11499 * = 4 - (inputsize - stride - 3 + stride - 1) / stride 11500 * = 4 - (inputsize - 4) / stride; 11501 */ 11502 level = 4 - (inputsize - 4) / stride; 11503 } else { 11504 /* For stage 2 translations the starting level is specified by the 11505 * VTCR_EL2.SL0 field (whose interpretation depends on the page size) 11506 */ 11507 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2); 11508 uint32_t startlevel; 11509 bool ok; 11510 11511 if (!aarch64 || stride == 9) { 11512 /* AArch32 or 4KB pages */ 11513 startlevel = 2 - sl0; 11514 } else { 11515 /* 16KB or 64KB pages */ 11516 startlevel = 3 - sl0; 11517 } 11518 11519 /* Check that the starting level is valid. */ 11520 ok = check_s2_mmu_setup(cpu, aarch64, startlevel, 11521 inputsize, stride); 11522 if (!ok) { 11523 fault_type = ARMFault_Translation; 11524 goto do_fault; 11525 } 11526 level = startlevel; 11527 } 11528 11529 indexmask_grainsize = (1ULL << (stride + 3)) - 1; 11530 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1; 11531 11532 /* Now we can extract the actual base address from the TTBR */ 11533 descaddr = extract64(ttbr, 0, 48); 11534 descaddr &= ~indexmask; 11535 11536 /* The address field in the descriptor goes up to bit 39 for ARMv7 11537 * but up to bit 47 for ARMv8, but we use the descaddrmask 11538 * up to bit 39 for AArch32, because we don't need other bits in that case 11539 * to construct next descriptor address (anyway they should be all zeroes). 11540 */ 11541 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) & 11542 ~indexmask_grainsize; 11543 11544 /* Secure accesses start with the page table in secure memory and 11545 * can be downgraded to non-secure at any step. Non-secure accesses 11546 * remain non-secure. We implement this by just ORing in the NSTable/NS 11547 * bits at each step. 11548 */ 11549 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4); 11550 for (;;) { 11551 uint64_t descriptor; 11552 bool nstable; 11553 11554 descaddr |= (address >> (stride * (4 - level))) & indexmask; 11555 descaddr &= ~7ULL; 11556 nstable = extract32(tableattrs, 4, 1); 11557 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi); 11558 if (fi->type != ARMFault_None) { 11559 goto do_fault; 11560 } 11561 11562 if (!(descriptor & 1) || 11563 (!(descriptor & 2) && (level == 3))) { 11564 /* Invalid, or the Reserved level 3 encoding */ 11565 goto do_fault; 11566 } 11567 descaddr = descriptor & descaddrmask; 11568 11569 if ((descriptor & 2) && (level < 3)) { 11570 /* Table entry. The top five bits are attributes which may 11571 * propagate down through lower levels of the table (and 11572 * which are all arranged so that 0 means "no effect", so 11573 * we can gather them up by ORing in the bits at each level). 11574 */ 11575 tableattrs |= extract64(descriptor, 59, 5); 11576 level++; 11577 indexmask = indexmask_grainsize; 11578 continue; 11579 } 11580 /* Block entry at level 1 or 2, or page entry at level 3. 11581 * These are basically the same thing, although the number 11582 * of bits we pull in from the vaddr varies. 11583 */ 11584 page_size = (1ULL << ((stride * (4 - level)) + 3)); 11585 descaddr |= (address & (page_size - 1)); 11586 /* Extract attributes from the descriptor */ 11587 attrs = extract64(descriptor, 2, 10) 11588 | (extract64(descriptor, 52, 12) << 10); 11589 11590 if (mmu_idx == ARMMMUIdx_S2NS) { 11591 /* Stage 2 table descriptors do not include any attribute fields */ 11592 break; 11593 } 11594 /* Merge in attributes from table descriptors */ 11595 attrs |= nstable << 3; /* NS */ 11596 guarded = extract64(descriptor, 50, 1); /* GP */ 11597 if (param.hpd) { 11598 /* HPD disables all the table attributes except NSTable. */ 11599 break; 11600 } 11601 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */ 11602 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1 11603 * means "force PL1 access only", which means forcing AP[1] to 0. 11604 */ 11605 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */ 11606 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */ 11607 break; 11608 } 11609 /* Here descaddr is the final physical address, and attributes 11610 * are all in attrs. 11611 */ 11612 fault_type = ARMFault_AccessFlag; 11613 if ((attrs & (1 << 8)) == 0) { 11614 /* Access flag */ 11615 goto do_fault; 11616 } 11617 11618 ap = extract32(attrs, 4, 2); 11619 xn = extract32(attrs, 12, 1); 11620 11621 if (mmu_idx == ARMMMUIdx_S2NS) { 11622 ns = true; 11623 *prot = get_S2prot(env, ap, xn); 11624 } else { 11625 ns = extract32(attrs, 3, 1); 11626 pxn = extract32(attrs, 11, 1); 11627 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn); 11628 } 11629 11630 fault_type = ARMFault_Permission; 11631 if (!(*prot & (1 << access_type))) { 11632 goto do_fault; 11633 } 11634 11635 if (ns) { 11636 /* The NS bit will (as required by the architecture) have no effect if 11637 * the CPU doesn't support TZ or this is a non-secure translation 11638 * regime, because the attribute will already be non-secure. 11639 */ 11640 txattrs->secure = false; 11641 } 11642 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */ 11643 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) { 11644 txattrs->target_tlb_bit0 = true; 11645 } 11646 11647 if (cacheattrs != NULL) { 11648 if (mmu_idx == ARMMMUIdx_S2NS) { 11649 cacheattrs->attrs = convert_stage2_attrs(env, 11650 extract32(attrs, 0, 4)); 11651 } else { 11652 /* Index into MAIR registers for cache attributes */ 11653 uint8_t attrindx = extract32(attrs, 0, 3); 11654 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)]; 11655 assert(attrindx <= 7); 11656 cacheattrs->attrs = extract64(mair, attrindx * 8, 8); 11657 } 11658 cacheattrs->shareability = extract32(attrs, 6, 2); 11659 } 11660 11661 *phys_ptr = descaddr; 11662 *page_size_ptr = page_size; 11663 return false; 11664 11665 do_fault: 11666 fi->type = fault_type; 11667 fi->level = level; 11668 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */ 11669 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS); 11670 return true; 11671 } 11672 11673 static inline void get_phys_addr_pmsav7_default(CPUARMState *env, 11674 ARMMMUIdx mmu_idx, 11675 int32_t address, int *prot) 11676 { 11677 if (!arm_feature(env, ARM_FEATURE_M)) { 11678 *prot = PAGE_READ | PAGE_WRITE; 11679 switch (address) { 11680 case 0xF0000000 ... 0xFFFFFFFF: 11681 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { 11682 /* hivecs execing is ok */ 11683 *prot |= PAGE_EXEC; 11684 } 11685 break; 11686 case 0x00000000 ... 0x7FFFFFFF: 11687 *prot |= PAGE_EXEC; 11688 break; 11689 } 11690 } else { 11691 /* Default system address map for M profile cores. 11692 * The architecture specifies which regions are execute-never; 11693 * at the MPU level no other checks are defined. 11694 */ 11695 switch (address) { 11696 case 0x00000000 ... 0x1fffffff: /* ROM */ 11697 case 0x20000000 ... 0x3fffffff: /* SRAM */ 11698 case 0x60000000 ... 0x7fffffff: /* RAM */ 11699 case 0x80000000 ... 0x9fffffff: /* RAM */ 11700 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 11701 break; 11702 case 0x40000000 ... 0x5fffffff: /* Peripheral */ 11703 case 0xa0000000 ... 0xbfffffff: /* Device */ 11704 case 0xc0000000 ... 0xdfffffff: /* Device */ 11705 case 0xe0000000 ... 0xffffffff: /* System */ 11706 *prot = PAGE_READ | PAGE_WRITE; 11707 break; 11708 default: 11709 g_assert_not_reached(); 11710 } 11711 } 11712 } 11713 11714 static bool pmsav7_use_background_region(ARMCPU *cpu, 11715 ARMMMUIdx mmu_idx, bool is_user) 11716 { 11717 /* Return true if we should use the default memory map as a 11718 * "background" region if there are no hits against any MPU regions. 11719 */ 11720 CPUARMState *env = &cpu->env; 11721 11722 if (is_user) { 11723 return false; 11724 } 11725 11726 if (arm_feature(env, ARM_FEATURE_M)) { 11727 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] 11728 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK; 11729 } else { 11730 return regime_sctlr(env, mmu_idx) & SCTLR_BR; 11731 } 11732 } 11733 11734 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address) 11735 { 11736 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */ 11737 return arm_feature(env, ARM_FEATURE_M) && 11738 extract32(address, 20, 12) == 0xe00; 11739 } 11740 11741 static inline bool m_is_system_region(CPUARMState *env, uint32_t address) 11742 { 11743 /* True if address is in the M profile system region 11744 * 0xe0000000 - 0xffffffff 11745 */ 11746 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7; 11747 } 11748 11749 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address, 11750 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11751 hwaddr *phys_ptr, int *prot, 11752 target_ulong *page_size, 11753 ARMMMUFaultInfo *fi) 11754 { 11755 ARMCPU *cpu = arm_env_get_cpu(env); 11756 int n; 11757 bool is_user = regime_is_user(env, mmu_idx); 11758 11759 *phys_ptr = address; 11760 *page_size = TARGET_PAGE_SIZE; 11761 *prot = 0; 11762 11763 if (regime_translation_disabled(env, mmu_idx) || 11764 m_is_ppb_region(env, address)) { 11765 /* MPU disabled or M profile PPB access: use default memory map. 11766 * The other case which uses the default memory map in the 11767 * v7M ARM ARM pseudocode is exception vector reads from the vector 11768 * table. In QEMU those accesses are done in arm_v7m_load_vector(), 11769 * which always does a direct read using address_space_ldl(), rather 11770 * than going via this function, so we don't need to check that here. 11771 */ 11772 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 11773 } else { /* MPU enabled */ 11774 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 11775 /* region search */ 11776 uint32_t base = env->pmsav7.drbar[n]; 11777 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5); 11778 uint32_t rmask; 11779 bool srdis = false; 11780 11781 if (!(env->pmsav7.drsr[n] & 0x1)) { 11782 continue; 11783 } 11784 11785 if (!rsize) { 11786 qemu_log_mask(LOG_GUEST_ERROR, 11787 "DRSR[%d]: Rsize field cannot be 0\n", n); 11788 continue; 11789 } 11790 rsize++; 11791 rmask = (1ull << rsize) - 1; 11792 11793 if (base & rmask) { 11794 qemu_log_mask(LOG_GUEST_ERROR, 11795 "DRBAR[%d]: 0x%" PRIx32 " misaligned " 11796 "to DRSR region size, mask = 0x%" PRIx32 "\n", 11797 n, base, rmask); 11798 continue; 11799 } 11800 11801 if (address < base || address > base + rmask) { 11802 /* 11803 * Address not in this region. We must check whether the 11804 * region covers addresses in the same page as our address. 11805 * In that case we must not report a size that covers the 11806 * whole page for a subsequent hit against a different MPU 11807 * region or the background region, because it would result in 11808 * incorrect TLB hits for subsequent accesses to addresses that 11809 * are in this MPU region. 11810 */ 11811 if (ranges_overlap(base, rmask, 11812 address & TARGET_PAGE_MASK, 11813 TARGET_PAGE_SIZE)) { 11814 *page_size = 1; 11815 } 11816 continue; 11817 } 11818 11819 /* Region matched */ 11820 11821 if (rsize >= 8) { /* no subregions for regions < 256 bytes */ 11822 int i, snd; 11823 uint32_t srdis_mask; 11824 11825 rsize -= 3; /* sub region size (power of 2) */ 11826 snd = ((address - base) >> rsize) & 0x7; 11827 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1); 11828 11829 srdis_mask = srdis ? 0x3 : 0x0; 11830 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) { 11831 /* This will check in groups of 2, 4 and then 8, whether 11832 * the subregion bits are consistent. rsize is incremented 11833 * back up to give the region size, considering consistent 11834 * adjacent subregions as one region. Stop testing if rsize 11835 * is already big enough for an entire QEMU page. 11836 */ 11837 int snd_rounded = snd & ~(i - 1); 11838 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n], 11839 snd_rounded + 8, i); 11840 if (srdis_mask ^ srdis_multi) { 11841 break; 11842 } 11843 srdis_mask = (srdis_mask << i) | srdis_mask; 11844 rsize++; 11845 } 11846 } 11847 if (srdis) { 11848 continue; 11849 } 11850 if (rsize < TARGET_PAGE_BITS) { 11851 *page_size = 1 << rsize; 11852 } 11853 break; 11854 } 11855 11856 if (n == -1) { /* no hits */ 11857 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 11858 /* background fault */ 11859 fi->type = ARMFault_Background; 11860 return true; 11861 } 11862 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 11863 } else { /* a MPU hit! */ 11864 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3); 11865 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1); 11866 11867 if (m_is_system_region(env, address)) { 11868 /* System space is always execute never */ 11869 xn = 1; 11870 } 11871 11872 if (is_user) { /* User mode AP bit decoding */ 11873 switch (ap) { 11874 case 0: 11875 case 1: 11876 case 5: 11877 break; /* no access */ 11878 case 3: 11879 *prot |= PAGE_WRITE; 11880 /* fall through */ 11881 case 2: 11882 case 6: 11883 *prot |= PAGE_READ | PAGE_EXEC; 11884 break; 11885 case 7: 11886 /* for v7M, same as 6; for R profile a reserved value */ 11887 if (arm_feature(env, ARM_FEATURE_M)) { 11888 *prot |= PAGE_READ | PAGE_EXEC; 11889 break; 11890 } 11891 /* fall through */ 11892 default: 11893 qemu_log_mask(LOG_GUEST_ERROR, 11894 "DRACR[%d]: Bad value for AP bits: 0x%" 11895 PRIx32 "\n", n, ap); 11896 } 11897 } else { /* Priv. mode AP bits decoding */ 11898 switch (ap) { 11899 case 0: 11900 break; /* no access */ 11901 case 1: 11902 case 2: 11903 case 3: 11904 *prot |= PAGE_WRITE; 11905 /* fall through */ 11906 case 5: 11907 case 6: 11908 *prot |= PAGE_READ | PAGE_EXEC; 11909 break; 11910 case 7: 11911 /* for v7M, same as 6; for R profile a reserved value */ 11912 if (arm_feature(env, ARM_FEATURE_M)) { 11913 *prot |= PAGE_READ | PAGE_EXEC; 11914 break; 11915 } 11916 /* fall through */ 11917 default: 11918 qemu_log_mask(LOG_GUEST_ERROR, 11919 "DRACR[%d]: Bad value for AP bits: 0x%" 11920 PRIx32 "\n", n, ap); 11921 } 11922 } 11923 11924 /* execute never */ 11925 if (xn) { 11926 *prot &= ~PAGE_EXEC; 11927 } 11928 } 11929 } 11930 11931 fi->type = ARMFault_Permission; 11932 fi->level = 1; 11933 return !(*prot & (1 << access_type)); 11934 } 11935 11936 static bool v8m_is_sau_exempt(CPUARMState *env, 11937 uint32_t address, MMUAccessType access_type) 11938 { 11939 /* The architecture specifies that certain address ranges are 11940 * exempt from v8M SAU/IDAU checks. 11941 */ 11942 return 11943 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) || 11944 (address >= 0xe0000000 && address <= 0xe0002fff) || 11945 (address >= 0xe000e000 && address <= 0xe000efff) || 11946 (address >= 0xe002e000 && address <= 0xe002efff) || 11947 (address >= 0xe0040000 && address <= 0xe0041fff) || 11948 (address >= 0xe00ff000 && address <= 0xe00fffff); 11949 } 11950 11951 static void v8m_security_lookup(CPUARMState *env, uint32_t address, 11952 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11953 V8M_SAttributes *sattrs) 11954 { 11955 /* Look up the security attributes for this address. Compare the 11956 * pseudocode SecurityCheck() function. 11957 * We assume the caller has zero-initialized *sattrs. 11958 */ 11959 ARMCPU *cpu = arm_env_get_cpu(env); 11960 int r; 11961 bool idau_exempt = false, idau_ns = true, idau_nsc = true; 11962 int idau_region = IREGION_NOTVALID; 11963 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 11964 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 11965 11966 if (cpu->idau) { 11967 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau); 11968 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau); 11969 11970 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns, 11971 &idau_nsc); 11972 } 11973 11974 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) { 11975 /* 0xf0000000..0xffffffff is always S for insn fetches */ 11976 return; 11977 } 11978 11979 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) { 11980 sattrs->ns = !regime_is_secure(env, mmu_idx); 11981 return; 11982 } 11983 11984 if (idau_region != IREGION_NOTVALID) { 11985 sattrs->irvalid = true; 11986 sattrs->iregion = idau_region; 11987 } 11988 11989 switch (env->sau.ctrl & 3) { 11990 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */ 11991 break; 11992 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */ 11993 sattrs->ns = true; 11994 break; 11995 default: /* SAU.ENABLE == 1 */ 11996 for (r = 0; r < cpu->sau_sregion; r++) { 11997 if (env->sau.rlar[r] & 1) { 11998 uint32_t base = env->sau.rbar[r] & ~0x1f; 11999 uint32_t limit = env->sau.rlar[r] | 0x1f; 12000 12001 if (base <= address && limit >= address) { 12002 if (base > addr_page_base || limit < addr_page_limit) { 12003 sattrs->subpage = true; 12004 } 12005 if (sattrs->srvalid) { 12006 /* If we hit in more than one region then we must report 12007 * as Secure, not NS-Callable, with no valid region 12008 * number info. 12009 */ 12010 sattrs->ns = false; 12011 sattrs->nsc = false; 12012 sattrs->sregion = 0; 12013 sattrs->srvalid = false; 12014 break; 12015 } else { 12016 if (env->sau.rlar[r] & 2) { 12017 sattrs->nsc = true; 12018 } else { 12019 sattrs->ns = true; 12020 } 12021 sattrs->srvalid = true; 12022 sattrs->sregion = r; 12023 } 12024 } else { 12025 /* 12026 * Address not in this region. We must check whether the 12027 * region covers addresses in the same page as our address. 12028 * In that case we must not report a size that covers the 12029 * whole page for a subsequent hit against a different MPU 12030 * region or the background region, because it would result 12031 * in incorrect TLB hits for subsequent accesses to 12032 * addresses that are in this MPU region. 12033 */ 12034 if (limit >= base && 12035 ranges_overlap(base, limit - base + 1, 12036 addr_page_base, 12037 TARGET_PAGE_SIZE)) { 12038 sattrs->subpage = true; 12039 } 12040 } 12041 } 12042 } 12043 break; 12044 } 12045 12046 /* 12047 * The IDAU will override the SAU lookup results if it specifies 12048 * higher security than the SAU does. 12049 */ 12050 if (!idau_ns) { 12051 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) { 12052 sattrs->ns = false; 12053 sattrs->nsc = idau_nsc; 12054 } 12055 } 12056 } 12057 12058 static bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address, 12059 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12060 hwaddr *phys_ptr, MemTxAttrs *txattrs, 12061 int *prot, bool *is_subpage, 12062 ARMMMUFaultInfo *fi, uint32_t *mregion) 12063 { 12064 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check 12065 * that a full phys-to-virt translation does). 12066 * mregion is (if not NULL) set to the region number which matched, 12067 * or -1 if no region number is returned (MPU off, address did not 12068 * hit a region, address hit in multiple regions). 12069 * We set is_subpage to true if the region hit doesn't cover the 12070 * entire TARGET_PAGE the address is within. 12071 */ 12072 ARMCPU *cpu = arm_env_get_cpu(env); 12073 bool is_user = regime_is_user(env, mmu_idx); 12074 uint32_t secure = regime_is_secure(env, mmu_idx); 12075 int n; 12076 int matchregion = -1; 12077 bool hit = false; 12078 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 12079 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 12080 12081 *is_subpage = false; 12082 *phys_ptr = address; 12083 *prot = 0; 12084 if (mregion) { 12085 *mregion = -1; 12086 } 12087 12088 /* Unlike the ARM ARM pseudocode, we don't need to check whether this 12089 * was an exception vector read from the vector table (which is always 12090 * done using the default system address map), because those accesses 12091 * are done in arm_v7m_load_vector(), which always does a direct 12092 * read using address_space_ldl(), rather than going via this function. 12093 */ 12094 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */ 12095 hit = true; 12096 } else if (m_is_ppb_region(env, address)) { 12097 hit = true; 12098 } else { 12099 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 12100 hit = true; 12101 } 12102 12103 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 12104 /* region search */ 12105 /* Note that the base address is bits [31:5] from the register 12106 * with bits [4:0] all zeroes, but the limit address is bits 12107 * [31:5] from the register with bits [4:0] all ones. 12108 */ 12109 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f; 12110 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f; 12111 12112 if (!(env->pmsav8.rlar[secure][n] & 0x1)) { 12113 /* Region disabled */ 12114 continue; 12115 } 12116 12117 if (address < base || address > limit) { 12118 /* 12119 * Address not in this region. We must check whether the 12120 * region covers addresses in the same page as our address. 12121 * In that case we must not report a size that covers the 12122 * whole page for a subsequent hit against a different MPU 12123 * region or the background region, because it would result in 12124 * incorrect TLB hits for subsequent accesses to addresses that 12125 * are in this MPU region. 12126 */ 12127 if (limit >= base && 12128 ranges_overlap(base, limit - base + 1, 12129 addr_page_base, 12130 TARGET_PAGE_SIZE)) { 12131 *is_subpage = true; 12132 } 12133 continue; 12134 } 12135 12136 if (base > addr_page_base || limit < addr_page_limit) { 12137 *is_subpage = true; 12138 } 12139 12140 if (matchregion != -1) { 12141 /* Multiple regions match -- always a failure (unlike 12142 * PMSAv7 where highest-numbered-region wins) 12143 */ 12144 fi->type = ARMFault_Permission; 12145 fi->level = 1; 12146 return true; 12147 } 12148 12149 matchregion = n; 12150 hit = true; 12151 } 12152 } 12153 12154 if (!hit) { 12155 /* background fault */ 12156 fi->type = ARMFault_Background; 12157 return true; 12158 } 12159 12160 if (matchregion == -1) { 12161 /* hit using the background region */ 12162 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 12163 } else { 12164 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2); 12165 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1); 12166 12167 if (m_is_system_region(env, address)) { 12168 /* System space is always execute never */ 12169 xn = 1; 12170 } 12171 12172 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap); 12173 if (*prot && !xn) { 12174 *prot |= PAGE_EXEC; 12175 } 12176 /* We don't need to look the attribute up in the MAIR0/MAIR1 12177 * registers because that only tells us about cacheability. 12178 */ 12179 if (mregion) { 12180 *mregion = matchregion; 12181 } 12182 } 12183 12184 fi->type = ARMFault_Permission; 12185 fi->level = 1; 12186 return !(*prot & (1 << access_type)); 12187 } 12188 12189 12190 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address, 12191 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12192 hwaddr *phys_ptr, MemTxAttrs *txattrs, 12193 int *prot, target_ulong *page_size, 12194 ARMMMUFaultInfo *fi) 12195 { 12196 uint32_t secure = regime_is_secure(env, mmu_idx); 12197 V8M_SAttributes sattrs = {}; 12198 bool ret; 12199 bool mpu_is_subpage; 12200 12201 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 12202 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs); 12203 if (access_type == MMU_INST_FETCH) { 12204 /* Instruction fetches always use the MMU bank and the 12205 * transaction attribute determined by the fetch address, 12206 * regardless of CPU state. This is painful for QEMU 12207 * to handle, because it would mean we need to encode 12208 * into the mmu_idx not just the (user, negpri) information 12209 * for the current security state but also that for the 12210 * other security state, which would balloon the number 12211 * of mmu_idx values needed alarmingly. 12212 * Fortunately we can avoid this because it's not actually 12213 * possible to arbitrarily execute code from memory with 12214 * the wrong security attribute: it will always generate 12215 * an exception of some kind or another, apart from the 12216 * special case of an NS CPU executing an SG instruction 12217 * in S&NSC memory. So we always just fail the translation 12218 * here and sort things out in the exception handler 12219 * (including possibly emulating an SG instruction). 12220 */ 12221 if (sattrs.ns != !secure) { 12222 if (sattrs.nsc) { 12223 fi->type = ARMFault_QEMU_NSCExec; 12224 } else { 12225 fi->type = ARMFault_QEMU_SFault; 12226 } 12227 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 12228 *phys_ptr = address; 12229 *prot = 0; 12230 return true; 12231 } 12232 } else { 12233 /* For data accesses we always use the MMU bank indicated 12234 * by the current CPU state, but the security attributes 12235 * might downgrade a secure access to nonsecure. 12236 */ 12237 if (sattrs.ns) { 12238 txattrs->secure = false; 12239 } else if (!secure) { 12240 /* NS access to S memory must fault. 12241 * Architecturally we should first check whether the 12242 * MPU information for this address indicates that we 12243 * are doing an unaligned access to Device memory, which 12244 * should generate a UsageFault instead. QEMU does not 12245 * currently check for that kind of unaligned access though. 12246 * If we added it we would need to do so as a special case 12247 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt(). 12248 */ 12249 fi->type = ARMFault_QEMU_SFault; 12250 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 12251 *phys_ptr = address; 12252 *prot = 0; 12253 return true; 12254 } 12255 } 12256 } 12257 12258 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr, 12259 txattrs, prot, &mpu_is_subpage, fi, NULL); 12260 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE; 12261 return ret; 12262 } 12263 12264 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address, 12265 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12266 hwaddr *phys_ptr, int *prot, 12267 ARMMMUFaultInfo *fi) 12268 { 12269 int n; 12270 uint32_t mask; 12271 uint32_t base; 12272 bool is_user = regime_is_user(env, mmu_idx); 12273 12274 if (regime_translation_disabled(env, mmu_idx)) { 12275 /* MPU disabled. */ 12276 *phys_ptr = address; 12277 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 12278 return false; 12279 } 12280 12281 *phys_ptr = address; 12282 for (n = 7; n >= 0; n--) { 12283 base = env->cp15.c6_region[n]; 12284 if ((base & 1) == 0) { 12285 continue; 12286 } 12287 mask = 1 << ((base >> 1) & 0x1f); 12288 /* Keep this shift separate from the above to avoid an 12289 (undefined) << 32. */ 12290 mask = (mask << 1) - 1; 12291 if (((base ^ address) & ~mask) == 0) { 12292 break; 12293 } 12294 } 12295 if (n < 0) { 12296 fi->type = ARMFault_Background; 12297 return true; 12298 } 12299 12300 if (access_type == MMU_INST_FETCH) { 12301 mask = env->cp15.pmsav5_insn_ap; 12302 } else { 12303 mask = env->cp15.pmsav5_data_ap; 12304 } 12305 mask = (mask >> (n * 4)) & 0xf; 12306 switch (mask) { 12307 case 0: 12308 fi->type = ARMFault_Permission; 12309 fi->level = 1; 12310 return true; 12311 case 1: 12312 if (is_user) { 12313 fi->type = ARMFault_Permission; 12314 fi->level = 1; 12315 return true; 12316 } 12317 *prot = PAGE_READ | PAGE_WRITE; 12318 break; 12319 case 2: 12320 *prot = PAGE_READ; 12321 if (!is_user) { 12322 *prot |= PAGE_WRITE; 12323 } 12324 break; 12325 case 3: 12326 *prot = PAGE_READ | PAGE_WRITE; 12327 break; 12328 case 5: 12329 if (is_user) { 12330 fi->type = ARMFault_Permission; 12331 fi->level = 1; 12332 return true; 12333 } 12334 *prot = PAGE_READ; 12335 break; 12336 case 6: 12337 *prot = PAGE_READ; 12338 break; 12339 default: 12340 /* Bad permission. */ 12341 fi->type = ARMFault_Permission; 12342 fi->level = 1; 12343 return true; 12344 } 12345 *prot |= PAGE_EXEC; 12346 return false; 12347 } 12348 12349 /* Combine either inner or outer cacheability attributes for normal 12350 * memory, according to table D4-42 and pseudocode procedure 12351 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM). 12352 * 12353 * NB: only stage 1 includes allocation hints (RW bits), leading to 12354 * some asymmetry. 12355 */ 12356 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2) 12357 { 12358 if (s1 == 4 || s2 == 4) { 12359 /* non-cacheable has precedence */ 12360 return 4; 12361 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) { 12362 /* stage 1 write-through takes precedence */ 12363 return s1; 12364 } else if (extract32(s2, 2, 2) == 2) { 12365 /* stage 2 write-through takes precedence, but the allocation hint 12366 * is still taken from stage 1 12367 */ 12368 return (2 << 2) | extract32(s1, 0, 2); 12369 } else { /* write-back */ 12370 return s1; 12371 } 12372 } 12373 12374 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4 12375 * and CombineS1S2Desc() 12376 * 12377 * @s1: Attributes from stage 1 walk 12378 * @s2: Attributes from stage 2 walk 12379 */ 12380 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2) 12381 { 12382 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4); 12383 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4); 12384 ARMCacheAttrs ret; 12385 12386 /* Combine shareability attributes (table D4-43) */ 12387 if (s1.shareability == 2 || s2.shareability == 2) { 12388 /* if either are outer-shareable, the result is outer-shareable */ 12389 ret.shareability = 2; 12390 } else if (s1.shareability == 3 || s2.shareability == 3) { 12391 /* if either are inner-shareable, the result is inner-shareable */ 12392 ret.shareability = 3; 12393 } else { 12394 /* both non-shareable */ 12395 ret.shareability = 0; 12396 } 12397 12398 /* Combine memory type and cacheability attributes */ 12399 if (s1hi == 0 || s2hi == 0) { 12400 /* Device has precedence over normal */ 12401 if (s1lo == 0 || s2lo == 0) { 12402 /* nGnRnE has precedence over anything */ 12403 ret.attrs = 0; 12404 } else if (s1lo == 4 || s2lo == 4) { 12405 /* non-Reordering has precedence over Reordering */ 12406 ret.attrs = 4; /* nGnRE */ 12407 } else if (s1lo == 8 || s2lo == 8) { 12408 /* non-Gathering has precedence over Gathering */ 12409 ret.attrs = 8; /* nGRE */ 12410 } else { 12411 ret.attrs = 0xc; /* GRE */ 12412 } 12413 12414 /* Any location for which the resultant memory type is any 12415 * type of Device memory is always treated as Outer Shareable. 12416 */ 12417 ret.shareability = 2; 12418 } else { /* Normal memory */ 12419 /* Outer/inner cacheability combine independently */ 12420 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4 12421 | combine_cacheattr_nibble(s1lo, s2lo); 12422 12423 if (ret.attrs == 0x44) { 12424 /* Any location for which the resultant memory type is Normal 12425 * Inner Non-cacheable, Outer Non-cacheable is always treated 12426 * as Outer Shareable. 12427 */ 12428 ret.shareability = 2; 12429 } 12430 } 12431 12432 return ret; 12433 } 12434 12435 12436 /* get_phys_addr - get the physical address for this virtual address 12437 * 12438 * Find the physical address corresponding to the given virtual address, 12439 * by doing a translation table walk on MMU based systems or using the 12440 * MPU state on MPU based systems. 12441 * 12442 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs, 12443 * prot and page_size may not be filled in, and the populated fsr value provides 12444 * information on why the translation aborted, in the format of a 12445 * DFSR/IFSR fault register, with the following caveats: 12446 * * we honour the short vs long DFSR format differences. 12447 * * the WnR bit is never set (the caller must do this). 12448 * * for PSMAv5 based systems we don't bother to return a full FSR format 12449 * value. 12450 * 12451 * @env: CPUARMState 12452 * @address: virtual address to get physical address for 12453 * @access_type: 0 for read, 1 for write, 2 for execute 12454 * @mmu_idx: MMU index indicating required translation regime 12455 * @phys_ptr: set to the physical address corresponding to the virtual address 12456 * @attrs: set to the memory transaction attributes to use 12457 * @prot: set to the permissions for the page containing phys_ptr 12458 * @page_size: set to the size of the page containing phys_ptr 12459 * @fi: set to fault info if the translation fails 12460 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes 12461 */ 12462 static bool get_phys_addr(CPUARMState *env, target_ulong address, 12463 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12464 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 12465 target_ulong *page_size, 12466 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 12467 { 12468 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 12469 /* Call ourselves recursively to do the stage 1 and then stage 2 12470 * translations. 12471 */ 12472 if (arm_feature(env, ARM_FEATURE_EL2)) { 12473 hwaddr ipa; 12474 int s2_prot; 12475 int ret; 12476 ARMCacheAttrs cacheattrs2 = {}; 12477 12478 ret = get_phys_addr(env, address, access_type, 12479 stage_1_mmu_idx(mmu_idx), &ipa, attrs, 12480 prot, page_size, fi, cacheattrs); 12481 12482 /* If S1 fails or S2 is disabled, return early. */ 12483 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) { 12484 *phys_ptr = ipa; 12485 return ret; 12486 } 12487 12488 /* S1 is done. Now do S2 translation. */ 12489 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS, 12490 phys_ptr, attrs, &s2_prot, 12491 page_size, fi, 12492 cacheattrs != NULL ? &cacheattrs2 : NULL); 12493 fi->s2addr = ipa; 12494 /* Combine the S1 and S2 perms. */ 12495 *prot &= s2_prot; 12496 12497 /* Combine the S1 and S2 cache attributes, if needed */ 12498 if (!ret && cacheattrs != NULL) { 12499 if (env->cp15.hcr_el2 & HCR_DC) { 12500 /* 12501 * HCR.DC forces the first stage attributes to 12502 * Normal Non-Shareable, 12503 * Inner Write-Back Read-Allocate Write-Allocate, 12504 * Outer Write-Back Read-Allocate Write-Allocate. 12505 */ 12506 cacheattrs->attrs = 0xff; 12507 cacheattrs->shareability = 0; 12508 } 12509 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2); 12510 } 12511 12512 return ret; 12513 } else { 12514 /* 12515 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1. 12516 */ 12517 mmu_idx = stage_1_mmu_idx(mmu_idx); 12518 } 12519 } 12520 12521 /* The page table entries may downgrade secure to non-secure, but 12522 * cannot upgrade an non-secure translation regime's attributes 12523 * to secure. 12524 */ 12525 attrs->secure = regime_is_secure(env, mmu_idx); 12526 attrs->user = regime_is_user(env, mmu_idx); 12527 12528 /* Fast Context Switch Extension. This doesn't exist at all in v8. 12529 * In v7 and earlier it affects all stage 1 translations. 12530 */ 12531 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS 12532 && !arm_feature(env, ARM_FEATURE_V8)) { 12533 if (regime_el(env, mmu_idx) == 3) { 12534 address += env->cp15.fcseidr_s; 12535 } else { 12536 address += env->cp15.fcseidr_ns; 12537 } 12538 } 12539 12540 if (arm_feature(env, ARM_FEATURE_PMSA)) { 12541 bool ret; 12542 *page_size = TARGET_PAGE_SIZE; 12543 12544 if (arm_feature(env, ARM_FEATURE_V8)) { 12545 /* PMSAv8 */ 12546 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx, 12547 phys_ptr, attrs, prot, page_size, fi); 12548 } else if (arm_feature(env, ARM_FEATURE_V7)) { 12549 /* PMSAv7 */ 12550 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx, 12551 phys_ptr, prot, page_size, fi); 12552 } else { 12553 /* Pre-v7 MPU */ 12554 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx, 12555 phys_ptr, prot, fi); 12556 } 12557 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32 12558 " mmu_idx %u -> %s (prot %c%c%c)\n", 12559 access_type == MMU_DATA_LOAD ? "reading" : 12560 (access_type == MMU_DATA_STORE ? "writing" : "execute"), 12561 (uint32_t)address, mmu_idx, 12562 ret ? "Miss" : "Hit", 12563 *prot & PAGE_READ ? 'r' : '-', 12564 *prot & PAGE_WRITE ? 'w' : '-', 12565 *prot & PAGE_EXEC ? 'x' : '-'); 12566 12567 return ret; 12568 } 12569 12570 /* Definitely a real MMU, not an MPU */ 12571 12572 if (regime_translation_disabled(env, mmu_idx)) { 12573 /* MMU disabled. */ 12574 *phys_ptr = address; 12575 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 12576 *page_size = TARGET_PAGE_SIZE; 12577 return 0; 12578 } 12579 12580 if (regime_using_lpae_format(env, mmu_idx)) { 12581 return get_phys_addr_lpae(env, address, access_type, mmu_idx, 12582 phys_ptr, attrs, prot, page_size, 12583 fi, cacheattrs); 12584 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) { 12585 return get_phys_addr_v6(env, address, access_type, mmu_idx, 12586 phys_ptr, attrs, prot, page_size, fi); 12587 } else { 12588 return get_phys_addr_v5(env, address, access_type, mmu_idx, 12589 phys_ptr, prot, page_size, fi); 12590 } 12591 } 12592 12593 /* Walk the page table and (if the mapping exists) add the page 12594 * to the TLB. Return false on success, or true on failure. Populate 12595 * fsr with ARM DFSR/IFSR fault register format value on failure. 12596 */ 12597 bool arm_tlb_fill(CPUState *cs, vaddr address, 12598 MMUAccessType access_type, int mmu_idx, 12599 ARMMMUFaultInfo *fi) 12600 { 12601 ARMCPU *cpu = ARM_CPU(cs); 12602 CPUARMState *env = &cpu->env; 12603 hwaddr phys_addr; 12604 target_ulong page_size; 12605 int prot; 12606 int ret; 12607 MemTxAttrs attrs = {}; 12608 12609 ret = get_phys_addr(env, address, access_type, 12610 core_to_arm_mmu_idx(env, mmu_idx), &phys_addr, 12611 &attrs, &prot, &page_size, fi, NULL); 12612 if (!ret) { 12613 /* 12614 * Map a single [sub]page. Regions smaller than our declared 12615 * target page size are handled specially, so for those we 12616 * pass in the exact addresses. 12617 */ 12618 if (page_size >= TARGET_PAGE_SIZE) { 12619 phys_addr &= TARGET_PAGE_MASK; 12620 address &= TARGET_PAGE_MASK; 12621 } 12622 tlb_set_page_with_attrs(cs, address, phys_addr, attrs, 12623 prot, mmu_idx, page_size); 12624 return 0; 12625 } 12626 12627 return ret; 12628 } 12629 12630 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr, 12631 MemTxAttrs *attrs) 12632 { 12633 ARMCPU *cpu = ARM_CPU(cs); 12634 CPUARMState *env = &cpu->env; 12635 hwaddr phys_addr; 12636 target_ulong page_size; 12637 int prot; 12638 bool ret; 12639 ARMMMUFaultInfo fi = {}; 12640 ARMMMUIdx mmu_idx = arm_mmu_idx(env); 12641 12642 *attrs = (MemTxAttrs) {}; 12643 12644 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr, 12645 attrs, &prot, &page_size, &fi, NULL); 12646 12647 if (ret) { 12648 return -1; 12649 } 12650 return phys_addr; 12651 } 12652 12653 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) 12654 { 12655 uint32_t mask; 12656 unsigned el = arm_current_el(env); 12657 12658 /* First handle registers which unprivileged can read */ 12659 12660 switch (reg) { 12661 case 0 ... 7: /* xPSR sub-fields */ 12662 mask = 0; 12663 if ((reg & 1) && el) { 12664 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */ 12665 } 12666 if (!(reg & 4)) { 12667 mask |= XPSR_NZCV | XPSR_Q; /* APSR */ 12668 } 12669 /* EPSR reads as zero */ 12670 return xpsr_read(env) & mask; 12671 break; 12672 case 20: /* CONTROL */ 12673 { 12674 uint32_t value = env->v7m.control[env->v7m.secure]; 12675 if (!env->v7m.secure) { 12676 /* SFPA is RAZ/WI from NS; FPCA is stored in the M_REG_S bank */ 12677 value |= env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK; 12678 } 12679 return value; 12680 } 12681 case 0x94: /* CONTROL_NS */ 12682 /* We have to handle this here because unprivileged Secure code 12683 * can read the NS CONTROL register. 12684 */ 12685 if (!env->v7m.secure) { 12686 return 0; 12687 } 12688 return env->v7m.control[M_REG_NS] | 12689 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK); 12690 } 12691 12692 if (el == 0) { 12693 return 0; /* unprivileged reads others as zero */ 12694 } 12695 12696 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 12697 switch (reg) { 12698 case 0x88: /* MSP_NS */ 12699 if (!env->v7m.secure) { 12700 return 0; 12701 } 12702 return env->v7m.other_ss_msp; 12703 case 0x89: /* PSP_NS */ 12704 if (!env->v7m.secure) { 12705 return 0; 12706 } 12707 return env->v7m.other_ss_psp; 12708 case 0x8a: /* MSPLIM_NS */ 12709 if (!env->v7m.secure) { 12710 return 0; 12711 } 12712 return env->v7m.msplim[M_REG_NS]; 12713 case 0x8b: /* PSPLIM_NS */ 12714 if (!env->v7m.secure) { 12715 return 0; 12716 } 12717 return env->v7m.psplim[M_REG_NS]; 12718 case 0x90: /* PRIMASK_NS */ 12719 if (!env->v7m.secure) { 12720 return 0; 12721 } 12722 return env->v7m.primask[M_REG_NS]; 12723 case 0x91: /* BASEPRI_NS */ 12724 if (!env->v7m.secure) { 12725 return 0; 12726 } 12727 return env->v7m.basepri[M_REG_NS]; 12728 case 0x93: /* FAULTMASK_NS */ 12729 if (!env->v7m.secure) { 12730 return 0; 12731 } 12732 return env->v7m.faultmask[M_REG_NS]; 12733 case 0x98: /* SP_NS */ 12734 { 12735 /* This gives the non-secure SP selected based on whether we're 12736 * currently in handler mode or not, using the NS CONTROL.SPSEL. 12737 */ 12738 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; 12739 12740 if (!env->v7m.secure) { 12741 return 0; 12742 } 12743 if (!arm_v7m_is_handler_mode(env) && spsel) { 12744 return env->v7m.other_ss_psp; 12745 } else { 12746 return env->v7m.other_ss_msp; 12747 } 12748 } 12749 default: 12750 break; 12751 } 12752 } 12753 12754 switch (reg) { 12755 case 8: /* MSP */ 12756 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13]; 12757 case 9: /* PSP */ 12758 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp; 12759 case 10: /* MSPLIM */ 12760 if (!arm_feature(env, ARM_FEATURE_V8)) { 12761 goto bad_reg; 12762 } 12763 return env->v7m.msplim[env->v7m.secure]; 12764 case 11: /* PSPLIM */ 12765 if (!arm_feature(env, ARM_FEATURE_V8)) { 12766 goto bad_reg; 12767 } 12768 return env->v7m.psplim[env->v7m.secure]; 12769 case 16: /* PRIMASK */ 12770 return env->v7m.primask[env->v7m.secure]; 12771 case 17: /* BASEPRI */ 12772 case 18: /* BASEPRI_MAX */ 12773 return env->v7m.basepri[env->v7m.secure]; 12774 case 19: /* FAULTMASK */ 12775 return env->v7m.faultmask[env->v7m.secure]; 12776 default: 12777 bad_reg: 12778 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special" 12779 " register %d\n", reg); 12780 return 0; 12781 } 12782 } 12783 12784 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val) 12785 { 12786 /* We're passed bits [11..0] of the instruction; extract 12787 * SYSm and the mask bits. 12788 * Invalid combinations of SYSm and mask are UNPREDICTABLE; 12789 * we choose to treat them as if the mask bits were valid. 12790 * NB that the pseudocode 'mask' variable is bits [11..10], 12791 * whereas ours is [11..8]. 12792 */ 12793 uint32_t mask = extract32(maskreg, 8, 4); 12794 uint32_t reg = extract32(maskreg, 0, 8); 12795 int cur_el = arm_current_el(env); 12796 12797 if (cur_el == 0 && reg > 7 && reg != 20) { 12798 /* 12799 * only xPSR sub-fields and CONTROL.SFPA may be written by 12800 * unprivileged code 12801 */ 12802 return; 12803 } 12804 12805 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 12806 switch (reg) { 12807 case 0x88: /* MSP_NS */ 12808 if (!env->v7m.secure) { 12809 return; 12810 } 12811 env->v7m.other_ss_msp = val; 12812 return; 12813 case 0x89: /* PSP_NS */ 12814 if (!env->v7m.secure) { 12815 return; 12816 } 12817 env->v7m.other_ss_psp = val; 12818 return; 12819 case 0x8a: /* MSPLIM_NS */ 12820 if (!env->v7m.secure) { 12821 return; 12822 } 12823 env->v7m.msplim[M_REG_NS] = val & ~7; 12824 return; 12825 case 0x8b: /* PSPLIM_NS */ 12826 if (!env->v7m.secure) { 12827 return; 12828 } 12829 env->v7m.psplim[M_REG_NS] = val & ~7; 12830 return; 12831 case 0x90: /* PRIMASK_NS */ 12832 if (!env->v7m.secure) { 12833 return; 12834 } 12835 env->v7m.primask[M_REG_NS] = val & 1; 12836 return; 12837 case 0x91: /* BASEPRI_NS */ 12838 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { 12839 return; 12840 } 12841 env->v7m.basepri[M_REG_NS] = val & 0xff; 12842 return; 12843 case 0x93: /* FAULTMASK_NS */ 12844 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { 12845 return; 12846 } 12847 env->v7m.faultmask[M_REG_NS] = val & 1; 12848 return; 12849 case 0x94: /* CONTROL_NS */ 12850 if (!env->v7m.secure) { 12851 return; 12852 } 12853 write_v7m_control_spsel_for_secstate(env, 12854 val & R_V7M_CONTROL_SPSEL_MASK, 12855 M_REG_NS); 12856 if (arm_feature(env, ARM_FEATURE_M_MAIN)) { 12857 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK; 12858 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK; 12859 } 12860 /* 12861 * SFPA is RAZ/WI from NS. FPCA is RO if NSACR.CP10 == 0, 12862 * RES0 if the FPU is not present, and is stored in the S bank 12863 */ 12864 if (arm_feature(env, ARM_FEATURE_VFP) && 12865 extract32(env->v7m.nsacr, 10, 1)) { 12866 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; 12867 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK; 12868 } 12869 return; 12870 case 0x98: /* SP_NS */ 12871 { 12872 /* This gives the non-secure SP selected based on whether we're 12873 * currently in handler mode or not, using the NS CONTROL.SPSEL. 12874 */ 12875 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; 12876 bool is_psp = !arm_v7m_is_handler_mode(env) && spsel; 12877 uint32_t limit; 12878 12879 if (!env->v7m.secure) { 12880 return; 12881 } 12882 12883 limit = is_psp ? env->v7m.psplim[false] : env->v7m.msplim[false]; 12884 12885 if (val < limit) { 12886 CPUState *cs = CPU(arm_env_get_cpu(env)); 12887 12888 cpu_restore_state(cs, GETPC(), true); 12889 raise_exception(env, EXCP_STKOF, 0, 1); 12890 } 12891 12892 if (is_psp) { 12893 env->v7m.other_ss_psp = val; 12894 } else { 12895 env->v7m.other_ss_msp = val; 12896 } 12897 return; 12898 } 12899 default: 12900 break; 12901 } 12902 } 12903 12904 switch (reg) { 12905 case 0 ... 7: /* xPSR sub-fields */ 12906 /* only APSR is actually writable */ 12907 if (!(reg & 4)) { 12908 uint32_t apsrmask = 0; 12909 12910 if (mask & 8) { 12911 apsrmask |= XPSR_NZCV | XPSR_Q; 12912 } 12913 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) { 12914 apsrmask |= XPSR_GE; 12915 } 12916 xpsr_write(env, val, apsrmask); 12917 } 12918 break; 12919 case 8: /* MSP */ 12920 if (v7m_using_psp(env)) { 12921 env->v7m.other_sp = val; 12922 } else { 12923 env->regs[13] = val; 12924 } 12925 break; 12926 case 9: /* PSP */ 12927 if (v7m_using_psp(env)) { 12928 env->regs[13] = val; 12929 } else { 12930 env->v7m.other_sp = val; 12931 } 12932 break; 12933 case 10: /* MSPLIM */ 12934 if (!arm_feature(env, ARM_FEATURE_V8)) { 12935 goto bad_reg; 12936 } 12937 env->v7m.msplim[env->v7m.secure] = val & ~7; 12938 break; 12939 case 11: /* PSPLIM */ 12940 if (!arm_feature(env, ARM_FEATURE_V8)) { 12941 goto bad_reg; 12942 } 12943 env->v7m.psplim[env->v7m.secure] = val & ~7; 12944 break; 12945 case 16: /* PRIMASK */ 12946 env->v7m.primask[env->v7m.secure] = val & 1; 12947 break; 12948 case 17: /* BASEPRI */ 12949 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 12950 goto bad_reg; 12951 } 12952 env->v7m.basepri[env->v7m.secure] = val & 0xff; 12953 break; 12954 case 18: /* BASEPRI_MAX */ 12955 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 12956 goto bad_reg; 12957 } 12958 val &= 0xff; 12959 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure] 12960 || env->v7m.basepri[env->v7m.secure] == 0)) { 12961 env->v7m.basepri[env->v7m.secure] = val; 12962 } 12963 break; 12964 case 19: /* FAULTMASK */ 12965 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 12966 goto bad_reg; 12967 } 12968 env->v7m.faultmask[env->v7m.secure] = val & 1; 12969 break; 12970 case 20: /* CONTROL */ 12971 /* 12972 * Writing to the SPSEL bit only has an effect if we are in 12973 * thread mode; other bits can be updated by any privileged code. 12974 * write_v7m_control_spsel() deals with updating the SPSEL bit in 12975 * env->v7m.control, so we only need update the others. 12976 * For v7M, we must just ignore explicit writes to SPSEL in handler 12977 * mode; for v8M the write is permitted but will have no effect. 12978 * All these bits are writes-ignored from non-privileged code, 12979 * except for SFPA. 12980 */ 12981 if (cur_el > 0 && (arm_feature(env, ARM_FEATURE_V8) || 12982 !arm_v7m_is_handler_mode(env))) { 12983 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0); 12984 } 12985 if (cur_el > 0 && arm_feature(env, ARM_FEATURE_M_MAIN)) { 12986 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK; 12987 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK; 12988 } 12989 if (arm_feature(env, ARM_FEATURE_VFP)) { 12990 /* 12991 * SFPA is RAZ/WI from NS or if no FPU. 12992 * FPCA is RO if NSACR.CP10 == 0, RES0 if the FPU is not present. 12993 * Both are stored in the S bank. 12994 */ 12995 if (env->v7m.secure) { 12996 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 12997 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_SFPA_MASK; 12998 } 12999 if (cur_el > 0 && 13000 (env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_SECURITY) || 13001 extract32(env->v7m.nsacr, 10, 1))) { 13002 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; 13003 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK; 13004 } 13005 } 13006 break; 13007 default: 13008 bad_reg: 13009 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special" 13010 " register %d\n", reg); 13011 return; 13012 } 13013 } 13014 13015 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) 13016 { 13017 /* Implement the TT instruction. op is bits [7:6] of the insn. */ 13018 bool forceunpriv = op & 1; 13019 bool alt = op & 2; 13020 V8M_SAttributes sattrs = {}; 13021 uint32_t tt_resp; 13022 bool r, rw, nsr, nsrw, mrvalid; 13023 int prot; 13024 ARMMMUFaultInfo fi = {}; 13025 MemTxAttrs attrs = {}; 13026 hwaddr phys_addr; 13027 ARMMMUIdx mmu_idx; 13028 uint32_t mregion; 13029 bool targetpriv; 13030 bool targetsec = env->v7m.secure; 13031 bool is_subpage; 13032 13033 /* Work out what the security state and privilege level we're 13034 * interested in is... 13035 */ 13036 if (alt) { 13037 targetsec = !targetsec; 13038 } 13039 13040 if (forceunpriv) { 13041 targetpriv = false; 13042 } else { 13043 targetpriv = arm_v7m_is_handler_mode(env) || 13044 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK); 13045 } 13046 13047 /* ...and then figure out which MMU index this is */ 13048 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv); 13049 13050 /* We know that the MPU and SAU don't care about the access type 13051 * for our purposes beyond that we don't want to claim to be 13052 * an insn fetch, so we arbitrarily call this a read. 13053 */ 13054 13055 /* MPU region info only available for privileged or if 13056 * inspecting the other MPU state. 13057 */ 13058 if (arm_current_el(env) != 0 || alt) { 13059 /* We can ignore the return value as prot is always set */ 13060 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, 13061 &phys_addr, &attrs, &prot, &is_subpage, 13062 &fi, &mregion); 13063 if (mregion == -1) { 13064 mrvalid = false; 13065 mregion = 0; 13066 } else { 13067 mrvalid = true; 13068 } 13069 r = prot & PAGE_READ; 13070 rw = prot & PAGE_WRITE; 13071 } else { 13072 r = false; 13073 rw = false; 13074 mrvalid = false; 13075 mregion = 0; 13076 } 13077 13078 if (env->v7m.secure) { 13079 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); 13080 nsr = sattrs.ns && r; 13081 nsrw = sattrs.ns && rw; 13082 } else { 13083 sattrs.ns = true; 13084 nsr = false; 13085 nsrw = false; 13086 } 13087 13088 tt_resp = (sattrs.iregion << 24) | 13089 (sattrs.irvalid << 23) | 13090 ((!sattrs.ns) << 22) | 13091 (nsrw << 21) | 13092 (nsr << 20) | 13093 (rw << 19) | 13094 (r << 18) | 13095 (sattrs.srvalid << 17) | 13096 (mrvalid << 16) | 13097 (sattrs.sregion << 8) | 13098 mregion; 13099 13100 return tt_resp; 13101 } 13102 13103 #endif 13104 13105 void HELPER(dc_zva)(CPUARMState *env, uint64_t vaddr_in) 13106 { 13107 /* Implement DC ZVA, which zeroes a fixed-length block of memory. 13108 * Note that we do not implement the (architecturally mandated) 13109 * alignment fault for attempts to use this on Device memory 13110 * (which matches the usual QEMU behaviour of not implementing either 13111 * alignment faults or any memory attribute handling). 13112 */ 13113 13114 ARMCPU *cpu = arm_env_get_cpu(env); 13115 uint64_t blocklen = 4 << cpu->dcz_blocksize; 13116 uint64_t vaddr = vaddr_in & ~(blocklen - 1); 13117 13118 #ifndef CONFIG_USER_ONLY 13119 { 13120 /* Slightly awkwardly, QEMU's TARGET_PAGE_SIZE may be less than 13121 * the block size so we might have to do more than one TLB lookup. 13122 * We know that in fact for any v8 CPU the page size is at least 4K 13123 * and the block size must be 2K or less, but TARGET_PAGE_SIZE is only 13124 * 1K as an artefact of legacy v5 subpage support being present in the 13125 * same QEMU executable. 13126 */ 13127 int maxidx = DIV_ROUND_UP(blocklen, TARGET_PAGE_SIZE); 13128 void *hostaddr[maxidx]; 13129 int try, i; 13130 unsigned mmu_idx = cpu_mmu_index(env, false); 13131 TCGMemOpIdx oi = make_memop_idx(MO_UB, mmu_idx); 13132 13133 for (try = 0; try < 2; try++) { 13134 13135 for (i = 0; i < maxidx; i++) { 13136 hostaddr[i] = tlb_vaddr_to_host(env, 13137 vaddr + TARGET_PAGE_SIZE * i, 13138 1, mmu_idx); 13139 if (!hostaddr[i]) { 13140 break; 13141 } 13142 } 13143 if (i == maxidx) { 13144 /* If it's all in the TLB it's fair game for just writing to; 13145 * we know we don't need to update dirty status, etc. 13146 */ 13147 for (i = 0; i < maxidx - 1; i++) { 13148 memset(hostaddr[i], 0, TARGET_PAGE_SIZE); 13149 } 13150 memset(hostaddr[i], 0, blocklen - (i * TARGET_PAGE_SIZE)); 13151 return; 13152 } 13153 /* OK, try a store and see if we can populate the tlb. This 13154 * might cause an exception if the memory isn't writable, 13155 * in which case we will longjmp out of here. We must for 13156 * this purpose use the actual register value passed to us 13157 * so that we get the fault address right. 13158 */ 13159 helper_ret_stb_mmu(env, vaddr_in, 0, oi, GETPC()); 13160 /* Now we can populate the other TLB entries, if any */ 13161 for (i = 0; i < maxidx; i++) { 13162 uint64_t va = vaddr + TARGET_PAGE_SIZE * i; 13163 if (va != (vaddr_in & TARGET_PAGE_MASK)) { 13164 helper_ret_stb_mmu(env, va, 0, oi, GETPC()); 13165 } 13166 } 13167 } 13168 13169 /* Slow path (probably attempt to do this to an I/O device or 13170 * similar, or clearing of a block of code we have translations 13171 * cached for). Just do a series of byte writes as the architecture 13172 * demands. It's not worth trying to use a cpu_physical_memory_map(), 13173 * memset(), unmap() sequence here because: 13174 * + we'd need to account for the blocksize being larger than a page 13175 * + the direct-RAM access case is almost always going to be dealt 13176 * with in the fastpath code above, so there's no speed benefit 13177 * + we would have to deal with the map returning NULL because the 13178 * bounce buffer was in use 13179 */ 13180 for (i = 0; i < blocklen; i++) { 13181 helper_ret_stb_mmu(env, vaddr + i, 0, oi, GETPC()); 13182 } 13183 } 13184 #else 13185 memset(g2h(vaddr), 0, blocklen); 13186 #endif 13187 } 13188 13189 /* Note that signed overflow is undefined in C. The following routines are 13190 careful to use unsigned types where modulo arithmetic is required. 13191 Failure to do so _will_ break on newer gcc. */ 13192 13193 /* Signed saturating arithmetic. */ 13194 13195 /* Perform 16-bit signed saturating addition. */ 13196 static inline uint16_t add16_sat(uint16_t a, uint16_t b) 13197 { 13198 uint16_t res; 13199 13200 res = a + b; 13201 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { 13202 if (a & 0x8000) 13203 res = 0x8000; 13204 else 13205 res = 0x7fff; 13206 } 13207 return res; 13208 } 13209 13210 /* Perform 8-bit signed saturating addition. */ 13211 static inline uint8_t add8_sat(uint8_t a, uint8_t b) 13212 { 13213 uint8_t res; 13214 13215 res = a + b; 13216 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { 13217 if (a & 0x80) 13218 res = 0x80; 13219 else 13220 res = 0x7f; 13221 } 13222 return res; 13223 } 13224 13225 /* Perform 16-bit signed saturating subtraction. */ 13226 static inline uint16_t sub16_sat(uint16_t a, uint16_t b) 13227 { 13228 uint16_t res; 13229 13230 res = a - b; 13231 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { 13232 if (a & 0x8000) 13233 res = 0x8000; 13234 else 13235 res = 0x7fff; 13236 } 13237 return res; 13238 } 13239 13240 /* Perform 8-bit signed saturating subtraction. */ 13241 static inline uint8_t sub8_sat(uint8_t a, uint8_t b) 13242 { 13243 uint8_t res; 13244 13245 res = a - b; 13246 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { 13247 if (a & 0x80) 13248 res = 0x80; 13249 else 13250 res = 0x7f; 13251 } 13252 return res; 13253 } 13254 13255 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); 13256 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); 13257 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); 13258 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); 13259 #define PFX q 13260 13261 #include "op_addsub.h" 13262 13263 /* Unsigned saturating arithmetic. */ 13264 static inline uint16_t add16_usat(uint16_t a, uint16_t b) 13265 { 13266 uint16_t res; 13267 res = a + b; 13268 if (res < a) 13269 res = 0xffff; 13270 return res; 13271 } 13272 13273 static inline uint16_t sub16_usat(uint16_t a, uint16_t b) 13274 { 13275 if (a > b) 13276 return a - b; 13277 else 13278 return 0; 13279 } 13280 13281 static inline uint8_t add8_usat(uint8_t a, uint8_t b) 13282 { 13283 uint8_t res; 13284 res = a + b; 13285 if (res < a) 13286 res = 0xff; 13287 return res; 13288 } 13289 13290 static inline uint8_t sub8_usat(uint8_t a, uint8_t b) 13291 { 13292 if (a > b) 13293 return a - b; 13294 else 13295 return 0; 13296 } 13297 13298 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); 13299 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); 13300 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); 13301 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); 13302 #define PFX uq 13303 13304 #include "op_addsub.h" 13305 13306 /* Signed modulo arithmetic. */ 13307 #define SARITH16(a, b, n, op) do { \ 13308 int32_t sum; \ 13309 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ 13310 RESULT(sum, n, 16); \ 13311 if (sum >= 0) \ 13312 ge |= 3 << (n * 2); \ 13313 } while(0) 13314 13315 #define SARITH8(a, b, n, op) do { \ 13316 int32_t sum; \ 13317 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ 13318 RESULT(sum, n, 8); \ 13319 if (sum >= 0) \ 13320 ge |= 1 << n; \ 13321 } while(0) 13322 13323 13324 #define ADD16(a, b, n) SARITH16(a, b, n, +) 13325 #define SUB16(a, b, n) SARITH16(a, b, n, -) 13326 #define ADD8(a, b, n) SARITH8(a, b, n, +) 13327 #define SUB8(a, b, n) SARITH8(a, b, n, -) 13328 #define PFX s 13329 #define ARITH_GE 13330 13331 #include "op_addsub.h" 13332 13333 /* Unsigned modulo arithmetic. */ 13334 #define ADD16(a, b, n) do { \ 13335 uint32_t sum; \ 13336 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ 13337 RESULT(sum, n, 16); \ 13338 if ((sum >> 16) == 1) \ 13339 ge |= 3 << (n * 2); \ 13340 } while(0) 13341 13342 #define ADD8(a, b, n) do { \ 13343 uint32_t sum; \ 13344 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ 13345 RESULT(sum, n, 8); \ 13346 if ((sum >> 8) == 1) \ 13347 ge |= 1 << n; \ 13348 } while(0) 13349 13350 #define SUB16(a, b, n) do { \ 13351 uint32_t sum; \ 13352 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ 13353 RESULT(sum, n, 16); \ 13354 if ((sum >> 16) == 0) \ 13355 ge |= 3 << (n * 2); \ 13356 } while(0) 13357 13358 #define SUB8(a, b, n) do { \ 13359 uint32_t sum; \ 13360 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ 13361 RESULT(sum, n, 8); \ 13362 if ((sum >> 8) == 0) \ 13363 ge |= 1 << n; \ 13364 } while(0) 13365 13366 #define PFX u 13367 #define ARITH_GE 13368 13369 #include "op_addsub.h" 13370 13371 /* Halved signed arithmetic. */ 13372 #define ADD16(a, b, n) \ 13373 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) 13374 #define SUB16(a, b, n) \ 13375 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) 13376 #define ADD8(a, b, n) \ 13377 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) 13378 #define SUB8(a, b, n) \ 13379 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) 13380 #define PFX sh 13381 13382 #include "op_addsub.h" 13383 13384 /* Halved unsigned arithmetic. */ 13385 #define ADD16(a, b, n) \ 13386 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) 13387 #define SUB16(a, b, n) \ 13388 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) 13389 #define ADD8(a, b, n) \ 13390 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) 13391 #define SUB8(a, b, n) \ 13392 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) 13393 #define PFX uh 13394 13395 #include "op_addsub.h" 13396 13397 static inline uint8_t do_usad(uint8_t a, uint8_t b) 13398 { 13399 if (a > b) 13400 return a - b; 13401 else 13402 return b - a; 13403 } 13404 13405 /* Unsigned sum of absolute byte differences. */ 13406 uint32_t HELPER(usad8)(uint32_t a, uint32_t b) 13407 { 13408 uint32_t sum; 13409 sum = do_usad(a, b); 13410 sum += do_usad(a >> 8, b >> 8); 13411 sum += do_usad(a >> 16, b >>16); 13412 sum += do_usad(a >> 24, b >> 24); 13413 return sum; 13414 } 13415 13416 /* For ARMv6 SEL instruction. */ 13417 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) 13418 { 13419 uint32_t mask; 13420 13421 mask = 0; 13422 if (flags & 1) 13423 mask |= 0xff; 13424 if (flags & 2) 13425 mask |= 0xff00; 13426 if (flags & 4) 13427 mask |= 0xff0000; 13428 if (flags & 8) 13429 mask |= 0xff000000; 13430 return (a & mask) | (b & ~mask); 13431 } 13432 13433 /* CRC helpers. 13434 * The upper bytes of val (above the number specified by 'bytes') must have 13435 * been zeroed out by the caller. 13436 */ 13437 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes) 13438 { 13439 uint8_t buf[4]; 13440 13441 stl_le_p(buf, val); 13442 13443 /* zlib crc32 converts the accumulator and output to one's complement. */ 13444 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; 13445 } 13446 13447 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) 13448 { 13449 uint8_t buf[4]; 13450 13451 stl_le_p(buf, val); 13452 13453 /* Linux crc32c converts the output to one's complement. */ 13454 return crc32c(acc, buf, bytes) ^ 0xffffffff; 13455 } 13456 13457 /* Return the exception level to which FP-disabled exceptions should 13458 * be taken, or 0 if FP is enabled. 13459 */ 13460 int fp_exception_el(CPUARMState *env, int cur_el) 13461 { 13462 #ifndef CONFIG_USER_ONLY 13463 int fpen; 13464 13465 /* CPACR and the CPTR registers don't exist before v6, so FP is 13466 * always accessible 13467 */ 13468 if (!arm_feature(env, ARM_FEATURE_V6)) { 13469 return 0; 13470 } 13471 13472 if (arm_feature(env, ARM_FEATURE_M)) { 13473 /* CPACR can cause a NOCP UsageFault taken to current security state */ 13474 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) { 13475 return 1; 13476 } 13477 13478 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) { 13479 if (!extract32(env->v7m.nsacr, 10, 1)) { 13480 /* FP insns cause a NOCP UsageFault taken to Secure */ 13481 return 3; 13482 } 13483 } 13484 13485 return 0; 13486 } 13487 13488 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit: 13489 * 0, 2 : trap EL0 and EL1/PL1 accesses 13490 * 1 : trap only EL0 accesses 13491 * 3 : trap no accesses 13492 */ 13493 fpen = extract32(env->cp15.cpacr_el1, 20, 2); 13494 switch (fpen) { 13495 case 0: 13496 case 2: 13497 if (cur_el == 0 || cur_el == 1) { 13498 /* Trap to PL1, which might be EL1 or EL3 */ 13499 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) { 13500 return 3; 13501 } 13502 return 1; 13503 } 13504 if (cur_el == 3 && !is_a64(env)) { 13505 /* Secure PL1 running at EL3 */ 13506 return 3; 13507 } 13508 break; 13509 case 1: 13510 if (cur_el == 0) { 13511 return 1; 13512 } 13513 break; 13514 case 3: 13515 break; 13516 } 13517 13518 /* For the CPTR registers we don't need to guard with an ARM_FEATURE 13519 * check because zero bits in the registers mean "don't trap". 13520 */ 13521 13522 /* CPTR_EL2 : present in v7VE or v8 */ 13523 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1) 13524 && !arm_is_secure_below_el3(env)) { 13525 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */ 13526 return 2; 13527 } 13528 13529 /* CPTR_EL3 : present in v8 */ 13530 if (extract32(env->cp15.cptr_el[3], 10, 1)) { 13531 /* Trap all FP ops to EL3 */ 13532 return 3; 13533 } 13534 #endif 13535 return 0; 13536 } 13537 13538 ARMMMUIdx arm_v7m_mmu_idx_all(CPUARMState *env, 13539 bool secstate, bool priv, bool negpri) 13540 { 13541 ARMMMUIdx mmu_idx = ARM_MMU_IDX_M; 13542 13543 if (priv) { 13544 mmu_idx |= ARM_MMU_IDX_M_PRIV; 13545 } 13546 13547 if (negpri) { 13548 mmu_idx |= ARM_MMU_IDX_M_NEGPRI; 13549 } 13550 13551 if (secstate) { 13552 mmu_idx |= ARM_MMU_IDX_M_S; 13553 } 13554 13555 return mmu_idx; 13556 } 13557 13558 ARMMMUIdx arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState *env, 13559 bool secstate, bool priv) 13560 { 13561 bool negpri = armv7m_nvic_neg_prio_requested(env->nvic, secstate); 13562 13563 return arm_v7m_mmu_idx_all(env, secstate, priv, negpri); 13564 } 13565 13566 /* Return the MMU index for a v7M CPU in the specified security state */ 13567 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate) 13568 { 13569 bool priv = arm_current_el(env) != 0; 13570 13571 return arm_v7m_mmu_idx_for_secstate_and_priv(env, secstate, priv); 13572 } 13573 13574 ARMMMUIdx arm_mmu_idx(CPUARMState *env) 13575 { 13576 int el; 13577 13578 if (arm_feature(env, ARM_FEATURE_M)) { 13579 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure); 13580 } 13581 13582 el = arm_current_el(env); 13583 if (el < 2 && arm_is_secure_below_el3(env)) { 13584 return ARMMMUIdx_S1SE0 + el; 13585 } else { 13586 return ARMMMUIdx_S12NSE0 + el; 13587 } 13588 } 13589 13590 int cpu_mmu_index(CPUARMState *env, bool ifetch) 13591 { 13592 return arm_to_core_mmu_idx(arm_mmu_idx(env)); 13593 } 13594 13595 #ifndef CONFIG_USER_ONLY 13596 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env) 13597 { 13598 return stage_1_mmu_idx(arm_mmu_idx(env)); 13599 } 13600 #endif 13601 13602 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc, 13603 target_ulong *cs_base, uint32_t *pflags) 13604 { 13605 ARMMMUIdx mmu_idx = arm_mmu_idx(env); 13606 int current_el = arm_current_el(env); 13607 int fp_el = fp_exception_el(env, current_el); 13608 uint32_t flags = 0; 13609 13610 if (is_a64(env)) { 13611 ARMCPU *cpu = arm_env_get_cpu(env); 13612 uint64_t sctlr; 13613 13614 *pc = env->pc; 13615 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1); 13616 13617 /* Get control bits for tagged addresses. */ 13618 { 13619 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx); 13620 ARMVAParameters p0 = aa64_va_parameters_both(env, 0, stage1); 13621 int tbii, tbid; 13622 13623 /* FIXME: ARMv8.1-VHE S2 translation regime. */ 13624 if (regime_el(env, stage1) < 2) { 13625 ARMVAParameters p1 = aa64_va_parameters_both(env, -1, stage1); 13626 tbid = (p1.tbi << 1) | p0.tbi; 13627 tbii = tbid & ~((p1.tbid << 1) | p0.tbid); 13628 } else { 13629 tbid = p0.tbi; 13630 tbii = tbid & !p0.tbid; 13631 } 13632 13633 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii); 13634 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid); 13635 } 13636 13637 if (cpu_isar_feature(aa64_sve, cpu)) { 13638 int sve_el = sve_exception_el(env, current_el); 13639 uint32_t zcr_len; 13640 13641 /* If SVE is disabled, but FP is enabled, 13642 * then the effective len is 0. 13643 */ 13644 if (sve_el != 0 && fp_el == 0) { 13645 zcr_len = 0; 13646 } else { 13647 zcr_len = sve_zcr_len_for_el(env, current_el); 13648 } 13649 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el); 13650 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len); 13651 } 13652 13653 sctlr = arm_sctlr(env, current_el); 13654 13655 if (cpu_isar_feature(aa64_pauth, cpu)) { 13656 /* 13657 * In order to save space in flags, we record only whether 13658 * pauth is "inactive", meaning all insns are implemented as 13659 * a nop, or "active" when some action must be performed. 13660 * The decision of which action to take is left to a helper. 13661 */ 13662 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) { 13663 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1); 13664 } 13665 } 13666 13667 if (cpu_isar_feature(aa64_bti, cpu)) { 13668 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */ 13669 if (sctlr & (current_el == 0 ? SCTLR_BT0 : SCTLR_BT1)) { 13670 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1); 13671 } 13672 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype); 13673 } 13674 } else { 13675 *pc = env->regs[15]; 13676 flags = FIELD_DP32(flags, TBFLAG_A32, THUMB, env->thumb); 13677 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN, env->vfp.vec_len); 13678 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE, env->vfp.vec_stride); 13679 flags = FIELD_DP32(flags, TBFLAG_A32, CONDEXEC, env->condexec_bits); 13680 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, arm_sctlr_b(env)); 13681 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env)); 13682 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30) 13683 || arm_el_is_aa64(env, 1) || arm_feature(env, ARM_FEATURE_M)) { 13684 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1); 13685 } 13686 /* Note that XSCALE_CPAR shares bits with VECSTRIDE */ 13687 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 13688 flags = FIELD_DP32(flags, TBFLAG_A32, 13689 XSCALE_CPAR, env->cp15.c15_cpar); 13690 } 13691 } 13692 13693 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX, arm_to_core_mmu_idx(mmu_idx)); 13694 13695 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine 13696 * states defined in the ARM ARM for software singlestep: 13697 * SS_ACTIVE PSTATE.SS State 13698 * 0 x Inactive (the TB flag for SS is always 0) 13699 * 1 0 Active-pending 13700 * 1 1 Active-not-pending 13701 */ 13702 if (arm_singlestep_active(env)) { 13703 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1); 13704 if (is_a64(env)) { 13705 if (env->pstate & PSTATE_SS) { 13706 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1); 13707 } 13708 } else { 13709 if (env->uncached_cpsr & PSTATE_SS) { 13710 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1); 13711 } 13712 } 13713 } 13714 if (arm_cpu_data_is_big_endian(env)) { 13715 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1); 13716 } 13717 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el); 13718 13719 if (arm_v7m_is_handler_mode(env)) { 13720 flags = FIELD_DP32(flags, TBFLAG_A32, HANDLER, 1); 13721 } 13722 13723 /* v8M always applies stack limit checks unless CCR.STKOFHFNMIGN is 13724 * suppressing them because the requested execution priority is less than 0. 13725 */ 13726 if (arm_feature(env, ARM_FEATURE_V8) && 13727 arm_feature(env, ARM_FEATURE_M) && 13728 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) && 13729 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) { 13730 flags = FIELD_DP32(flags, TBFLAG_A32, STACKCHECK, 1); 13731 } 13732 13733 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 13734 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S) != env->v7m.secure) { 13735 flags = FIELD_DP32(flags, TBFLAG_A32, FPCCR_S_WRONG, 1); 13736 } 13737 13738 if (arm_feature(env, ARM_FEATURE_M) && 13739 (env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) && 13740 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) || 13741 (env->v7m.secure && 13742 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) { 13743 /* 13744 * ASPEN is set, but FPCA/SFPA indicate that there is no active 13745 * FP context; we must create a new FP context before executing 13746 * any FP insn. 13747 */ 13748 flags = FIELD_DP32(flags, TBFLAG_A32, NEW_FP_CTXT_NEEDED, 1); 13749 } 13750 13751 if (arm_feature(env, ARM_FEATURE_M)) { 13752 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 13753 13754 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) { 13755 flags = FIELD_DP32(flags, TBFLAG_A32, LSPACT, 1); 13756 } 13757 } 13758 13759 *pflags = flags; 13760 *cs_base = 0; 13761 } 13762 13763 #ifdef TARGET_AARCH64 13764 /* 13765 * The manual says that when SVE is enabled and VQ is widened the 13766 * implementation is allowed to zero the previously inaccessible 13767 * portion of the registers. The corollary to that is that when 13768 * SVE is enabled and VQ is narrowed we are also allowed to zero 13769 * the now inaccessible portion of the registers. 13770 * 13771 * The intent of this is that no predicate bit beyond VQ is ever set. 13772 * Which means that some operations on predicate registers themselves 13773 * may operate on full uint64_t or even unrolled across the maximum 13774 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally 13775 * may well be cheaper than conditionals to restrict the operation 13776 * to the relevant portion of a uint16_t[16]. 13777 */ 13778 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq) 13779 { 13780 int i, j; 13781 uint64_t pmask; 13782 13783 assert(vq >= 1 && vq <= ARM_MAX_VQ); 13784 assert(vq <= arm_env_get_cpu(env)->sve_max_vq); 13785 13786 /* Zap the high bits of the zregs. */ 13787 for (i = 0; i < 32; i++) { 13788 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq)); 13789 } 13790 13791 /* Zap the high bits of the pregs and ffr. */ 13792 pmask = 0; 13793 if (vq & 3) { 13794 pmask = ~(-1ULL << (16 * (vq & 3))); 13795 } 13796 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) { 13797 for (i = 0; i < 17; ++i) { 13798 env->vfp.pregs[i].p[j] &= pmask; 13799 } 13800 pmask = 0; 13801 } 13802 } 13803 13804 /* 13805 * Notice a change in SVE vector size when changing EL. 13806 */ 13807 void aarch64_sve_change_el(CPUARMState *env, int old_el, 13808 int new_el, bool el0_a64) 13809 { 13810 ARMCPU *cpu = arm_env_get_cpu(env); 13811 int old_len, new_len; 13812 bool old_a64, new_a64; 13813 13814 /* Nothing to do if no SVE. */ 13815 if (!cpu_isar_feature(aa64_sve, cpu)) { 13816 return; 13817 } 13818 13819 /* Nothing to do if FP is disabled in either EL. */ 13820 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) { 13821 return; 13822 } 13823 13824 /* 13825 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped 13826 * at ELx, or not available because the EL is in AArch32 state, then 13827 * for all purposes other than a direct read, the ZCR_ELx.LEN field 13828 * has an effective value of 0". 13829 * 13830 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0). 13831 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition 13832 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that 13833 * we already have the correct register contents when encountering the 13834 * vq0->vq0 transition between EL0->EL1. 13835 */ 13836 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64; 13837 old_len = (old_a64 && !sve_exception_el(env, old_el) 13838 ? sve_zcr_len_for_el(env, old_el) : 0); 13839 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64; 13840 new_len = (new_a64 && !sve_exception_el(env, new_el) 13841 ? sve_zcr_len_for_el(env, new_el) : 0); 13842 13843 /* When changing vector length, clear inaccessible state. */ 13844 if (new_len < old_len) { 13845 aarch64_sve_narrow_vq(env, new_len + 1); 13846 } 13847 } 13848 #endif 13849