1 /* 2 * ARM generic helpers. 3 * 4 * This code is licensed under the GNU GPL v2 or later. 5 * 6 * SPDX-License-Identifier: GPL-2.0-or-later 7 */ 8 #include "qemu/osdep.h" 9 #include "qemu/units.h" 10 #include "target/arm/idau.h" 11 #include "trace.h" 12 #include "cpu.h" 13 #include "internals.h" 14 #include "exec/gdbstub.h" 15 #include "exec/helper-proto.h" 16 #include "qemu/host-utils.h" 17 #include "sysemu/sysemu.h" 18 #include "qemu/bitops.h" 19 #include "qemu/crc32c.h" 20 #include "qemu/qemu-print.h" 21 #include "exec/exec-all.h" 22 #include "exec/cpu_ldst.h" 23 #include "arm_ldst.h" 24 #include <zlib.h> /* For crc32 */ 25 #include "hw/semihosting/semihost.h" 26 #include "sysemu/cpus.h" 27 #include "sysemu/kvm.h" 28 #include "qemu/range.h" 29 #include "qapi/qapi-commands-target.h" 30 #include "qapi/error.h" 31 #include "qemu/guest-random.h" 32 33 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */ 34 35 #ifndef CONFIG_USER_ONLY 36 37 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, 38 MMUAccessType access_type, ARMMMUIdx mmu_idx, 39 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 40 target_ulong *page_size_ptr, 41 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs); 42 #endif 43 44 static void switch_mode(CPUARMState *env, int mode); 45 46 static int vfp_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) 47 { 48 int nregs; 49 50 /* VFP data registers are always little-endian. */ 51 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; 52 if (reg < nregs) { 53 stq_le_p(buf, *aa32_vfp_dreg(env, reg)); 54 return 8; 55 } 56 if (arm_feature(env, ARM_FEATURE_NEON)) { 57 /* Aliases for Q regs. */ 58 nregs += 16; 59 if (reg < nregs) { 60 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 61 stq_le_p(buf, q[0]); 62 stq_le_p(buf + 8, q[1]); 63 return 16; 64 } 65 } 66 switch (reg - nregs) { 67 case 0: stl_p(buf, env->vfp.xregs[ARM_VFP_FPSID]); return 4; 68 case 1: stl_p(buf, vfp_get_fpscr(env)); return 4; 69 case 2: stl_p(buf, env->vfp.xregs[ARM_VFP_FPEXC]); return 4; 70 } 71 return 0; 72 } 73 74 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 75 { 76 int nregs; 77 78 nregs = arm_feature(env, ARM_FEATURE_VFP3) ? 32 : 16; 79 if (reg < nregs) { 80 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf); 81 return 8; 82 } 83 if (arm_feature(env, ARM_FEATURE_NEON)) { 84 nregs += 16; 85 if (reg < nregs) { 86 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 87 q[0] = ldq_le_p(buf); 88 q[1] = ldq_le_p(buf + 8); 89 return 16; 90 } 91 } 92 switch (reg - nregs) { 93 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; 94 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4; 95 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; 96 } 97 return 0; 98 } 99 100 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, uint8_t *buf, int reg) 101 { 102 switch (reg) { 103 case 0 ... 31: 104 /* 128 bit FP register */ 105 { 106 uint64_t *q = aa64_vfp_qreg(env, reg); 107 stq_le_p(buf, q[0]); 108 stq_le_p(buf + 8, q[1]); 109 return 16; 110 } 111 case 32: 112 /* FPSR */ 113 stl_p(buf, vfp_get_fpsr(env)); 114 return 4; 115 case 33: 116 /* FPCR */ 117 stl_p(buf, vfp_get_fpcr(env)); 118 return 4; 119 default: 120 return 0; 121 } 122 } 123 124 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 125 { 126 switch (reg) { 127 case 0 ... 31: 128 /* 128 bit FP register */ 129 { 130 uint64_t *q = aa64_vfp_qreg(env, reg); 131 q[0] = ldq_le_p(buf); 132 q[1] = ldq_le_p(buf + 8); 133 return 16; 134 } 135 case 32: 136 /* FPSR */ 137 vfp_set_fpsr(env, ldl_p(buf)); 138 return 4; 139 case 33: 140 /* FPCR */ 141 vfp_set_fpcr(env, ldl_p(buf)); 142 return 4; 143 default: 144 return 0; 145 } 146 } 147 148 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) 149 { 150 assert(ri->fieldoffset); 151 if (cpreg_field_is_64bit(ri)) { 152 return CPREG_FIELD64(env, ri); 153 } else { 154 return CPREG_FIELD32(env, ri); 155 } 156 } 157 158 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 159 uint64_t value) 160 { 161 assert(ri->fieldoffset); 162 if (cpreg_field_is_64bit(ri)) { 163 CPREG_FIELD64(env, ri) = value; 164 } else { 165 CPREG_FIELD32(env, ri) = value; 166 } 167 } 168 169 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) 170 { 171 return (char *)env + ri->fieldoffset; 172 } 173 174 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) 175 { 176 /* Raw read of a coprocessor register (as needed for migration, etc). */ 177 if (ri->type & ARM_CP_CONST) { 178 return ri->resetvalue; 179 } else if (ri->raw_readfn) { 180 return ri->raw_readfn(env, ri); 181 } else if (ri->readfn) { 182 return ri->readfn(env, ri); 183 } else { 184 return raw_read(env, ri); 185 } 186 } 187 188 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, 189 uint64_t v) 190 { 191 /* Raw write of a coprocessor register (as needed for migration, etc). 192 * Note that constant registers are treated as write-ignored; the 193 * caller should check for success by whether a readback gives the 194 * value written. 195 */ 196 if (ri->type & ARM_CP_CONST) { 197 return; 198 } else if (ri->raw_writefn) { 199 ri->raw_writefn(env, ri, v); 200 } else if (ri->writefn) { 201 ri->writefn(env, ri, v); 202 } else { 203 raw_write(env, ri, v); 204 } 205 } 206 207 static int arm_gdb_get_sysreg(CPUARMState *env, uint8_t *buf, int reg) 208 { 209 ARMCPU *cpu = env_archcpu(env); 210 const ARMCPRegInfo *ri; 211 uint32_t key; 212 213 key = cpu->dyn_xml.cpregs_keys[reg]; 214 ri = get_arm_cp_reginfo(cpu->cp_regs, key); 215 if (ri) { 216 if (cpreg_field_is_64bit(ri)) { 217 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri)); 218 } else { 219 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri)); 220 } 221 } 222 return 0; 223 } 224 225 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg) 226 { 227 return 0; 228 } 229 230 static bool raw_accessors_invalid(const ARMCPRegInfo *ri) 231 { 232 /* Return true if the regdef would cause an assertion if you called 233 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a 234 * program bug for it not to have the NO_RAW flag). 235 * NB that returning false here doesn't necessarily mean that calling 236 * read/write_raw_cp_reg() is safe, because we can't distinguish "has 237 * read/write access functions which are safe for raw use" from "has 238 * read/write access functions which have side effects but has forgotten 239 * to provide raw access functions". 240 * The tests here line up with the conditions in read/write_raw_cp_reg() 241 * and assertions in raw_read()/raw_write(). 242 */ 243 if ((ri->type & ARM_CP_CONST) || 244 ri->fieldoffset || 245 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { 246 return false; 247 } 248 return true; 249 } 250 251 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync) 252 { 253 /* Write the coprocessor state from cpu->env to the (index,value) list. */ 254 int i; 255 bool ok = true; 256 257 for (i = 0; i < cpu->cpreg_array_len; i++) { 258 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 259 const ARMCPRegInfo *ri; 260 uint64_t newval; 261 262 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 263 if (!ri) { 264 ok = false; 265 continue; 266 } 267 if (ri->type & ARM_CP_NO_RAW) { 268 continue; 269 } 270 271 newval = read_raw_cp_reg(&cpu->env, ri); 272 if (kvm_sync) { 273 /* 274 * Only sync if the previous list->cpustate sync succeeded. 275 * Rather than tracking the success/failure state for every 276 * item in the list, we just recheck "does the raw write we must 277 * have made in write_list_to_cpustate() read back OK" here. 278 */ 279 uint64_t oldval = cpu->cpreg_values[i]; 280 281 if (oldval == newval) { 282 continue; 283 } 284 285 write_raw_cp_reg(&cpu->env, ri, oldval); 286 if (read_raw_cp_reg(&cpu->env, ri) != oldval) { 287 continue; 288 } 289 290 write_raw_cp_reg(&cpu->env, ri, newval); 291 } 292 cpu->cpreg_values[i] = newval; 293 } 294 return ok; 295 } 296 297 bool write_list_to_cpustate(ARMCPU *cpu) 298 { 299 int i; 300 bool ok = true; 301 302 for (i = 0; i < cpu->cpreg_array_len; i++) { 303 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 304 uint64_t v = cpu->cpreg_values[i]; 305 const ARMCPRegInfo *ri; 306 307 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 308 if (!ri) { 309 ok = false; 310 continue; 311 } 312 if (ri->type & ARM_CP_NO_RAW) { 313 continue; 314 } 315 /* Write value and confirm it reads back as written 316 * (to catch read-only registers and partially read-only 317 * registers where the incoming migration value doesn't match) 318 */ 319 write_raw_cp_reg(&cpu->env, ri, v); 320 if (read_raw_cp_reg(&cpu->env, ri) != v) { 321 ok = false; 322 } 323 } 324 return ok; 325 } 326 327 static void add_cpreg_to_list(gpointer key, gpointer opaque) 328 { 329 ARMCPU *cpu = opaque; 330 uint64_t regidx; 331 const ARMCPRegInfo *ri; 332 333 regidx = *(uint32_t *)key; 334 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 335 336 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 337 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); 338 /* The value array need not be initialized at this point */ 339 cpu->cpreg_array_len++; 340 } 341 } 342 343 static void count_cpreg(gpointer key, gpointer opaque) 344 { 345 ARMCPU *cpu = opaque; 346 uint64_t regidx; 347 const ARMCPRegInfo *ri; 348 349 regidx = *(uint32_t *)key; 350 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 351 352 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 353 cpu->cpreg_array_len++; 354 } 355 } 356 357 static gint cpreg_key_compare(gconstpointer a, gconstpointer b) 358 { 359 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a); 360 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b); 361 362 if (aidx > bidx) { 363 return 1; 364 } 365 if (aidx < bidx) { 366 return -1; 367 } 368 return 0; 369 } 370 371 void init_cpreg_list(ARMCPU *cpu) 372 { 373 /* Initialise the cpreg_tuples[] array based on the cp_regs hash. 374 * Note that we require cpreg_tuples[] to be sorted by key ID. 375 */ 376 GList *keys; 377 int arraylen; 378 379 keys = g_hash_table_get_keys(cpu->cp_regs); 380 keys = g_list_sort(keys, cpreg_key_compare); 381 382 cpu->cpreg_array_len = 0; 383 384 g_list_foreach(keys, count_cpreg, cpu); 385 386 arraylen = cpu->cpreg_array_len; 387 cpu->cpreg_indexes = g_new(uint64_t, arraylen); 388 cpu->cpreg_values = g_new(uint64_t, arraylen); 389 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); 390 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); 391 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; 392 cpu->cpreg_array_len = 0; 393 394 g_list_foreach(keys, add_cpreg_to_list, cpu); 395 396 assert(cpu->cpreg_array_len == arraylen); 397 398 g_list_free(keys); 399 } 400 401 /* 402 * Some registers are not accessible if EL3.NS=0 and EL3 is using AArch32 but 403 * they are accessible when EL3 is using AArch64 regardless of EL3.NS. 404 * 405 * access_el3_aa32ns: Used to check AArch32 register views. 406 * access_el3_aa32ns_aa64any: Used to check both AArch32/64 register views. 407 */ 408 static CPAccessResult access_el3_aa32ns(CPUARMState *env, 409 const ARMCPRegInfo *ri, 410 bool isread) 411 { 412 bool secure = arm_is_secure_below_el3(env); 413 414 assert(!arm_el_is_aa64(env, 3)); 415 if (secure) { 416 return CP_ACCESS_TRAP_UNCATEGORIZED; 417 } 418 return CP_ACCESS_OK; 419 } 420 421 static CPAccessResult access_el3_aa32ns_aa64any(CPUARMState *env, 422 const ARMCPRegInfo *ri, 423 bool isread) 424 { 425 if (!arm_el_is_aa64(env, 3)) { 426 return access_el3_aa32ns(env, ri, isread); 427 } 428 return CP_ACCESS_OK; 429 } 430 431 /* Some secure-only AArch32 registers trap to EL3 if used from 432 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts). 433 * Note that an access from Secure EL1 can only happen if EL3 is AArch64. 434 * We assume that the .access field is set to PL1_RW. 435 */ 436 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env, 437 const ARMCPRegInfo *ri, 438 bool isread) 439 { 440 if (arm_current_el(env) == 3) { 441 return CP_ACCESS_OK; 442 } 443 if (arm_is_secure_below_el3(env)) { 444 return CP_ACCESS_TRAP_EL3; 445 } 446 /* This will be EL1 NS and EL2 NS, which just UNDEF */ 447 return CP_ACCESS_TRAP_UNCATEGORIZED; 448 } 449 450 /* Check for traps to "powerdown debug" registers, which are controlled 451 * by MDCR.TDOSA 452 */ 453 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri, 454 bool isread) 455 { 456 int el = arm_current_el(env); 457 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) || 458 (env->cp15.mdcr_el2 & MDCR_TDE) || 459 (arm_hcr_el2_eff(env) & HCR_TGE); 460 461 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) { 462 return CP_ACCESS_TRAP_EL2; 463 } 464 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) { 465 return CP_ACCESS_TRAP_EL3; 466 } 467 return CP_ACCESS_OK; 468 } 469 470 /* Check for traps to "debug ROM" registers, which are controlled 471 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3. 472 */ 473 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri, 474 bool isread) 475 { 476 int el = arm_current_el(env); 477 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) || 478 (env->cp15.mdcr_el2 & MDCR_TDE) || 479 (arm_hcr_el2_eff(env) & HCR_TGE); 480 481 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) { 482 return CP_ACCESS_TRAP_EL2; 483 } 484 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 485 return CP_ACCESS_TRAP_EL3; 486 } 487 return CP_ACCESS_OK; 488 } 489 490 /* Check for traps to general debug registers, which are controlled 491 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3. 492 */ 493 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri, 494 bool isread) 495 { 496 int el = arm_current_el(env); 497 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) || 498 (env->cp15.mdcr_el2 & MDCR_TDE) || 499 (arm_hcr_el2_eff(env) & HCR_TGE); 500 501 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) { 502 return CP_ACCESS_TRAP_EL2; 503 } 504 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 505 return CP_ACCESS_TRAP_EL3; 506 } 507 return CP_ACCESS_OK; 508 } 509 510 /* Check for traps to performance monitor registers, which are controlled 511 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3. 512 */ 513 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri, 514 bool isread) 515 { 516 int el = arm_current_el(env); 517 518 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 519 && !arm_is_secure_below_el3(env)) { 520 return CP_ACCESS_TRAP_EL2; 521 } 522 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 523 return CP_ACCESS_TRAP_EL3; 524 } 525 return CP_ACCESS_OK; 526 } 527 528 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 529 { 530 ARMCPU *cpu = env_archcpu(env); 531 532 raw_write(env, ri, value); 533 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */ 534 } 535 536 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 537 { 538 ARMCPU *cpu = env_archcpu(env); 539 540 if (raw_read(env, ri) != value) { 541 /* Unlike real hardware the qemu TLB uses virtual addresses, 542 * not modified virtual addresses, so this causes a TLB flush. 543 */ 544 tlb_flush(CPU(cpu)); 545 raw_write(env, ri, value); 546 } 547 } 548 549 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, 550 uint64_t value) 551 { 552 ARMCPU *cpu = env_archcpu(env); 553 554 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA) 555 && !extended_addresses_enabled(env)) { 556 /* For VMSA (when not using the LPAE long descriptor page table 557 * format) this register includes the ASID, so do a TLB flush. 558 * For PMSA it is purely a process ID and no action is needed. 559 */ 560 tlb_flush(CPU(cpu)); 561 } 562 raw_write(env, ri, value); 563 } 564 565 /* IS variants of TLB operations must affect all cores */ 566 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 567 uint64_t value) 568 { 569 CPUState *cs = env_cpu(env); 570 571 tlb_flush_all_cpus_synced(cs); 572 } 573 574 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 575 uint64_t value) 576 { 577 CPUState *cs = env_cpu(env); 578 579 tlb_flush_all_cpus_synced(cs); 580 } 581 582 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 583 uint64_t value) 584 { 585 CPUState *cs = env_cpu(env); 586 587 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 588 } 589 590 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 591 uint64_t value) 592 { 593 CPUState *cs = env_cpu(env); 594 595 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 596 } 597 598 /* 599 * Non-IS variants of TLB operations are upgraded to 600 * IS versions if we are at NS EL1 and HCR_EL2.FB is set to 601 * force broadcast of these operations. 602 */ 603 static bool tlb_force_broadcast(CPUARMState *env) 604 { 605 return (env->cp15.hcr_el2 & HCR_FB) && 606 arm_current_el(env) == 1 && arm_is_secure_below_el3(env); 607 } 608 609 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, 610 uint64_t value) 611 { 612 /* Invalidate all (TLBIALL) */ 613 ARMCPU *cpu = env_archcpu(env); 614 615 if (tlb_force_broadcast(env)) { 616 tlbiall_is_write(env, NULL, value); 617 return; 618 } 619 620 tlb_flush(CPU(cpu)); 621 } 622 623 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, 624 uint64_t value) 625 { 626 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ 627 ARMCPU *cpu = env_archcpu(env); 628 629 if (tlb_force_broadcast(env)) { 630 tlbimva_is_write(env, NULL, value); 631 return; 632 } 633 634 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); 635 } 636 637 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, 638 uint64_t value) 639 { 640 /* Invalidate by ASID (TLBIASID) */ 641 ARMCPU *cpu = env_archcpu(env); 642 643 if (tlb_force_broadcast(env)) { 644 tlbiasid_is_write(env, NULL, value); 645 return; 646 } 647 648 tlb_flush(CPU(cpu)); 649 } 650 651 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, 652 uint64_t value) 653 { 654 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ 655 ARMCPU *cpu = env_archcpu(env); 656 657 if (tlb_force_broadcast(env)) { 658 tlbimvaa_is_write(env, NULL, value); 659 return; 660 } 661 662 tlb_flush_page(CPU(cpu), value & TARGET_PAGE_MASK); 663 } 664 665 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri, 666 uint64_t value) 667 { 668 CPUState *cs = env_cpu(env); 669 670 tlb_flush_by_mmuidx(cs, 671 ARMMMUIdxBit_S12NSE1 | 672 ARMMMUIdxBit_S12NSE0 | 673 ARMMMUIdxBit_S2NS); 674 } 675 676 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 677 uint64_t value) 678 { 679 CPUState *cs = env_cpu(env); 680 681 tlb_flush_by_mmuidx_all_cpus_synced(cs, 682 ARMMMUIdxBit_S12NSE1 | 683 ARMMMUIdxBit_S12NSE0 | 684 ARMMMUIdxBit_S2NS); 685 } 686 687 static void tlbiipas2_write(CPUARMState *env, const ARMCPRegInfo *ri, 688 uint64_t value) 689 { 690 /* Invalidate by IPA. This has to invalidate any structures that 691 * contain only stage 2 translation information, but does not need 692 * to apply to structures that contain combined stage 1 and stage 2 693 * translation information. 694 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. 695 */ 696 CPUState *cs = env_cpu(env); 697 uint64_t pageaddr; 698 699 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 700 return; 701 } 702 703 pageaddr = sextract64(value << 12, 0, 40); 704 705 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS); 706 } 707 708 static void tlbiipas2_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 709 uint64_t value) 710 { 711 CPUState *cs = env_cpu(env); 712 uint64_t pageaddr; 713 714 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 715 return; 716 } 717 718 pageaddr = sextract64(value << 12, 0, 40); 719 720 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 721 ARMMMUIdxBit_S2NS); 722 } 723 724 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 725 uint64_t value) 726 { 727 CPUState *cs = env_cpu(env); 728 729 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2); 730 } 731 732 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 733 uint64_t value) 734 { 735 CPUState *cs = env_cpu(env); 736 737 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2); 738 } 739 740 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 741 uint64_t value) 742 { 743 CPUState *cs = env_cpu(env); 744 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 745 746 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2); 747 } 748 749 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 750 uint64_t value) 751 { 752 CPUState *cs = env_cpu(env); 753 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 754 755 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 756 ARMMMUIdxBit_S1E2); 757 } 758 759 static const ARMCPRegInfo cp_reginfo[] = { 760 /* Define the secure and non-secure FCSE identifier CP registers 761 * separately because there is no secure bank in V8 (no _EL3). This allows 762 * the secure register to be properly reset and migrated. There is also no 763 * v8 EL1 version of the register so the non-secure instance stands alone. 764 */ 765 { .name = "FCSEIDR", 766 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 767 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 768 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns), 769 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 770 { .name = "FCSEIDR_S", 771 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 772 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 773 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s), 774 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 775 /* Define the secure and non-secure context identifier CP registers 776 * separately because there is no secure bank in V8 (no _EL3). This allows 777 * the secure register to be properly reset and migrated. In the 778 * non-secure case, the 32-bit register will have reset and migration 779 * disabled during registration as it is handled by the 64-bit instance. 780 */ 781 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH, 782 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 783 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 784 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]), 785 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 786 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32, 787 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 788 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 789 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s), 790 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 791 REGINFO_SENTINEL 792 }; 793 794 static const ARMCPRegInfo not_v8_cp_reginfo[] = { 795 /* NB: Some of these registers exist in v8 but with more precise 796 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). 797 */ 798 /* MMU Domain access control / MPU write buffer control */ 799 { .name = "DACR", 800 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, 801 .access = PL1_RW, .resetvalue = 0, 802 .writefn = dacr_write, .raw_writefn = raw_write, 803 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 804 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 805 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs. 806 * For v6 and v5, these mappings are overly broad. 807 */ 808 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0, 809 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 810 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1, 811 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 812 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4, 813 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 814 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8, 815 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 816 /* Cache maintenance ops; some of this space may be overridden later. */ 817 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 818 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 819 .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, 820 REGINFO_SENTINEL 821 }; 822 823 static const ARMCPRegInfo not_v6_cp_reginfo[] = { 824 /* Not all pre-v6 cores implemented this WFI, so this is slightly 825 * over-broad. 826 */ 827 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, 828 .access = PL1_W, .type = ARM_CP_WFI }, 829 REGINFO_SENTINEL 830 }; 831 832 static const ARMCPRegInfo not_v7_cp_reginfo[] = { 833 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which 834 * is UNPREDICTABLE; we choose to NOP as most implementations do). 835 */ 836 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 837 .access = PL1_W, .type = ARM_CP_WFI }, 838 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice 839 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and 840 * OMAPCP will override this space. 841 */ 842 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, 843 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), 844 .resetvalue = 0 }, 845 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, 846 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), 847 .resetvalue = 0 }, 848 /* v6 doesn't have the cache ID registers but Linux reads them anyway */ 849 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, 850 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 851 .resetvalue = 0 }, 852 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; 853 * implementing it as RAZ means the "debug architecture version" bits 854 * will read as a reserved value, which should cause Linux to not try 855 * to use the debug hardware. 856 */ 857 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 858 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 859 /* MMU TLB control. Note that the wildcarding means we cover not just 860 * the unified TLB ops but also the dside/iside/inner-shareable variants. 861 */ 862 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, 863 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, 864 .type = ARM_CP_NO_RAW }, 865 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, 866 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, 867 .type = ARM_CP_NO_RAW }, 868 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, 869 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, 870 .type = ARM_CP_NO_RAW }, 871 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, 872 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, 873 .type = ARM_CP_NO_RAW }, 874 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2, 875 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP }, 876 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2, 877 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP }, 878 REGINFO_SENTINEL 879 }; 880 881 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, 882 uint64_t value) 883 { 884 uint32_t mask = 0; 885 886 /* In ARMv8 most bits of CPACR_EL1 are RES0. */ 887 if (!arm_feature(env, ARM_FEATURE_V8)) { 888 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. 889 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. 890 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. 891 */ 892 if (arm_feature(env, ARM_FEATURE_VFP)) { 893 /* VFP coprocessor: cp10 & cp11 [23:20] */ 894 mask |= (1 << 31) | (1 << 30) | (0xf << 20); 895 896 if (!arm_feature(env, ARM_FEATURE_NEON)) { 897 /* ASEDIS [31] bit is RAO/WI */ 898 value |= (1 << 31); 899 } 900 901 /* VFPv3 and upwards with NEON implement 32 double precision 902 * registers (D0-D31). 903 */ 904 if (!arm_feature(env, ARM_FEATURE_NEON) || 905 !arm_feature(env, ARM_FEATURE_VFP3)) { 906 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ 907 value |= (1 << 30); 908 } 909 } 910 value &= mask; 911 } 912 913 /* 914 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10 915 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00. 916 */ 917 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 918 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 919 value &= ~(0xf << 20); 920 value |= env->cp15.cpacr_el1 & (0xf << 20); 921 } 922 923 env->cp15.cpacr_el1 = value; 924 } 925 926 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri) 927 { 928 /* 929 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10 930 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00. 931 */ 932 uint64_t value = env->cp15.cpacr_el1; 933 934 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 935 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 936 value &= ~(0xf << 20); 937 } 938 return value; 939 } 940 941 942 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 943 { 944 /* Call cpacr_write() so that we reset with the correct RAO bits set 945 * for our CPU features. 946 */ 947 cpacr_write(env, ri, 0); 948 } 949 950 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 951 bool isread) 952 { 953 if (arm_feature(env, ARM_FEATURE_V8)) { 954 /* Check if CPACR accesses are to be trapped to EL2 */ 955 if (arm_current_el(env) == 1 && 956 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) { 957 return CP_ACCESS_TRAP_EL2; 958 /* Check if CPACR accesses are to be trapped to EL3 */ 959 } else if (arm_current_el(env) < 3 && 960 (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 961 return CP_ACCESS_TRAP_EL3; 962 } 963 } 964 965 return CP_ACCESS_OK; 966 } 967 968 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri, 969 bool isread) 970 { 971 /* Check if CPTR accesses are set to trap to EL3 */ 972 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 973 return CP_ACCESS_TRAP_EL3; 974 } 975 976 return CP_ACCESS_OK; 977 } 978 979 static const ARMCPRegInfo v6_cp_reginfo[] = { 980 /* prefetch by MVA in v6, NOP in v7 */ 981 { .name = "MVA_prefetch", 982 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, 983 .access = PL1_W, .type = ARM_CP_NOP }, 984 /* We need to break the TB after ISB to execute self-modifying code 985 * correctly and also to take any pending interrupts immediately. 986 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag. 987 */ 988 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, 989 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore }, 990 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, 991 .access = PL0_W, .type = ARM_CP_NOP }, 992 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, 993 .access = PL0_W, .type = ARM_CP_NOP }, 994 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, 995 .access = PL1_RW, 996 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), 997 offsetof(CPUARMState, cp15.ifar_ns) }, 998 .resetvalue = 0, }, 999 /* Watchpoint Fault Address Register : should actually only be present 1000 * for 1136, 1176, 11MPCore. 1001 */ 1002 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, 1003 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, 1004 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, 1005 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access, 1006 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1), 1007 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read }, 1008 REGINFO_SENTINEL 1009 }; 1010 1011 /* Definitions for the PMU registers */ 1012 #define PMCRN_MASK 0xf800 1013 #define PMCRN_SHIFT 11 1014 #define PMCRLC 0x40 1015 #define PMCRDP 0x10 1016 #define PMCRD 0x8 1017 #define PMCRC 0x4 1018 #define PMCRP 0x2 1019 #define PMCRE 0x1 1020 1021 #define PMXEVTYPER_P 0x80000000 1022 #define PMXEVTYPER_U 0x40000000 1023 #define PMXEVTYPER_NSK 0x20000000 1024 #define PMXEVTYPER_NSU 0x10000000 1025 #define PMXEVTYPER_NSH 0x08000000 1026 #define PMXEVTYPER_M 0x04000000 1027 #define PMXEVTYPER_MT 0x02000000 1028 #define PMXEVTYPER_EVTCOUNT 0x0000ffff 1029 #define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \ 1030 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \ 1031 PMXEVTYPER_M | PMXEVTYPER_MT | \ 1032 PMXEVTYPER_EVTCOUNT) 1033 1034 #define PMCCFILTR 0xf8000000 1035 #define PMCCFILTR_M PMXEVTYPER_M 1036 #define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M) 1037 1038 static inline uint32_t pmu_num_counters(CPUARMState *env) 1039 { 1040 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT; 1041 } 1042 1043 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */ 1044 static inline uint64_t pmu_counter_mask(CPUARMState *env) 1045 { 1046 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1); 1047 } 1048 1049 typedef struct pm_event { 1050 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */ 1051 /* If the event is supported on this CPU (used to generate PMCEID[01]) */ 1052 bool (*supported)(CPUARMState *); 1053 /* 1054 * Retrieve the current count of the underlying event. The programmed 1055 * counters hold a difference from the return value from this function 1056 */ 1057 uint64_t (*get_count)(CPUARMState *); 1058 /* 1059 * Return how many nanoseconds it will take (at a minimum) for count events 1060 * to occur. A negative value indicates the counter will never overflow, or 1061 * that the counter has otherwise arranged for the overflow bit to be set 1062 * and the PMU interrupt to be raised on overflow. 1063 */ 1064 int64_t (*ns_per_count)(uint64_t); 1065 } pm_event; 1066 1067 static bool event_always_supported(CPUARMState *env) 1068 { 1069 return true; 1070 } 1071 1072 static uint64_t swinc_get_count(CPUARMState *env) 1073 { 1074 /* 1075 * SW_INCR events are written directly to the pmevcntr's by writes to 1076 * PMSWINC, so there is no underlying count maintained by the PMU itself 1077 */ 1078 return 0; 1079 } 1080 1081 static int64_t swinc_ns_per(uint64_t ignored) 1082 { 1083 return -1; 1084 } 1085 1086 /* 1087 * Return the underlying cycle count for the PMU cycle counters. If we're in 1088 * usermode, simply return 0. 1089 */ 1090 static uint64_t cycles_get_count(CPUARMState *env) 1091 { 1092 #ifndef CONFIG_USER_ONLY 1093 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1094 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 1095 #else 1096 return cpu_get_host_ticks(); 1097 #endif 1098 } 1099 1100 #ifndef CONFIG_USER_ONLY 1101 static int64_t cycles_ns_per(uint64_t cycles) 1102 { 1103 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles; 1104 } 1105 1106 static bool instructions_supported(CPUARMState *env) 1107 { 1108 return use_icount == 1 /* Precise instruction counting */; 1109 } 1110 1111 static uint64_t instructions_get_count(CPUARMState *env) 1112 { 1113 return (uint64_t)cpu_get_icount_raw(); 1114 } 1115 1116 static int64_t instructions_ns_per(uint64_t icount) 1117 { 1118 return cpu_icount_to_ns((int64_t)icount); 1119 } 1120 #endif 1121 1122 static const pm_event pm_events[] = { 1123 { .number = 0x000, /* SW_INCR */ 1124 .supported = event_always_supported, 1125 .get_count = swinc_get_count, 1126 .ns_per_count = swinc_ns_per, 1127 }, 1128 #ifndef CONFIG_USER_ONLY 1129 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */ 1130 .supported = instructions_supported, 1131 .get_count = instructions_get_count, 1132 .ns_per_count = instructions_ns_per, 1133 }, 1134 { .number = 0x011, /* CPU_CYCLES, Cycle */ 1135 .supported = event_always_supported, 1136 .get_count = cycles_get_count, 1137 .ns_per_count = cycles_ns_per, 1138 } 1139 #endif 1140 }; 1141 1142 /* 1143 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of 1144 * events (i.e. the statistical profiling extension), this implementation 1145 * should first be updated to something sparse instead of the current 1146 * supported_event_map[] array. 1147 */ 1148 #define MAX_EVENT_ID 0x11 1149 #define UNSUPPORTED_EVENT UINT16_MAX 1150 static uint16_t supported_event_map[MAX_EVENT_ID + 1]; 1151 1152 /* 1153 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map 1154 * of ARM event numbers to indices in our pm_events array. 1155 * 1156 * Note: Events in the 0x40XX range are not currently supported. 1157 */ 1158 void pmu_init(ARMCPU *cpu) 1159 { 1160 unsigned int i; 1161 1162 /* 1163 * Empty supported_event_map and cpu->pmceid[01] before adding supported 1164 * events to them 1165 */ 1166 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) { 1167 supported_event_map[i] = UNSUPPORTED_EVENT; 1168 } 1169 cpu->pmceid0 = 0; 1170 cpu->pmceid1 = 0; 1171 1172 for (i = 0; i < ARRAY_SIZE(pm_events); i++) { 1173 const pm_event *cnt = &pm_events[i]; 1174 assert(cnt->number <= MAX_EVENT_ID); 1175 /* We do not currently support events in the 0x40xx range */ 1176 assert(cnt->number <= 0x3f); 1177 1178 if (cnt->supported(&cpu->env)) { 1179 supported_event_map[cnt->number] = i; 1180 uint64_t event_mask = 1ULL << (cnt->number & 0x1f); 1181 if (cnt->number & 0x20) { 1182 cpu->pmceid1 |= event_mask; 1183 } else { 1184 cpu->pmceid0 |= event_mask; 1185 } 1186 } 1187 } 1188 } 1189 1190 /* 1191 * Check at runtime whether a PMU event is supported for the current machine 1192 */ 1193 static bool event_supported(uint16_t number) 1194 { 1195 if (number > MAX_EVENT_ID) { 1196 return false; 1197 } 1198 return supported_event_map[number] != UNSUPPORTED_EVENT; 1199 } 1200 1201 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri, 1202 bool isread) 1203 { 1204 /* Performance monitor registers user accessibility is controlled 1205 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable 1206 * trapping to EL2 or EL3 for other accesses. 1207 */ 1208 int el = arm_current_el(env); 1209 1210 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) { 1211 return CP_ACCESS_TRAP; 1212 } 1213 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 1214 && !arm_is_secure_below_el3(env)) { 1215 return CP_ACCESS_TRAP_EL2; 1216 } 1217 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 1218 return CP_ACCESS_TRAP_EL3; 1219 } 1220 1221 return CP_ACCESS_OK; 1222 } 1223 1224 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env, 1225 const ARMCPRegInfo *ri, 1226 bool isread) 1227 { 1228 /* ER: event counter read trap control */ 1229 if (arm_feature(env, ARM_FEATURE_V8) 1230 && arm_current_el(env) == 0 1231 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0 1232 && isread) { 1233 return CP_ACCESS_OK; 1234 } 1235 1236 return pmreg_access(env, ri, isread); 1237 } 1238 1239 static CPAccessResult pmreg_access_swinc(CPUARMState *env, 1240 const ARMCPRegInfo *ri, 1241 bool isread) 1242 { 1243 /* SW: software increment write trap control */ 1244 if (arm_feature(env, ARM_FEATURE_V8) 1245 && arm_current_el(env) == 0 1246 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0 1247 && !isread) { 1248 return CP_ACCESS_OK; 1249 } 1250 1251 return pmreg_access(env, ri, isread); 1252 } 1253 1254 static CPAccessResult pmreg_access_selr(CPUARMState *env, 1255 const ARMCPRegInfo *ri, 1256 bool isread) 1257 { 1258 /* ER: event counter read trap control */ 1259 if (arm_feature(env, ARM_FEATURE_V8) 1260 && arm_current_el(env) == 0 1261 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) { 1262 return CP_ACCESS_OK; 1263 } 1264 1265 return pmreg_access(env, ri, isread); 1266 } 1267 1268 static CPAccessResult pmreg_access_ccntr(CPUARMState *env, 1269 const ARMCPRegInfo *ri, 1270 bool isread) 1271 { 1272 /* CR: cycle counter read trap control */ 1273 if (arm_feature(env, ARM_FEATURE_V8) 1274 && arm_current_el(env) == 0 1275 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0 1276 && isread) { 1277 return CP_ACCESS_OK; 1278 } 1279 1280 return pmreg_access(env, ri, isread); 1281 } 1282 1283 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using 1284 * the current EL, security state, and register configuration. 1285 */ 1286 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter) 1287 { 1288 uint64_t filter; 1289 bool e, p, u, nsk, nsu, nsh, m; 1290 bool enabled, prohibited, filtered; 1291 bool secure = arm_is_secure(env); 1292 int el = arm_current_el(env); 1293 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN; 1294 1295 if (!arm_feature(env, ARM_FEATURE_PMU)) { 1296 return false; 1297 } 1298 1299 if (!arm_feature(env, ARM_FEATURE_EL2) || 1300 (counter < hpmn || counter == 31)) { 1301 e = env->cp15.c9_pmcr & PMCRE; 1302 } else { 1303 e = env->cp15.mdcr_el2 & MDCR_HPME; 1304 } 1305 enabled = e && (env->cp15.c9_pmcnten & (1 << counter)); 1306 1307 if (!secure) { 1308 if (el == 2 && (counter < hpmn || counter == 31)) { 1309 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD; 1310 } else { 1311 prohibited = false; 1312 } 1313 } else { 1314 prohibited = arm_feature(env, ARM_FEATURE_EL3) && 1315 (env->cp15.mdcr_el3 & MDCR_SPME); 1316 } 1317 1318 if (prohibited && counter == 31) { 1319 prohibited = env->cp15.c9_pmcr & PMCRDP; 1320 } 1321 1322 if (counter == 31) { 1323 filter = env->cp15.pmccfiltr_el0; 1324 } else { 1325 filter = env->cp15.c14_pmevtyper[counter]; 1326 } 1327 1328 p = filter & PMXEVTYPER_P; 1329 u = filter & PMXEVTYPER_U; 1330 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK); 1331 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU); 1332 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH); 1333 m = arm_el_is_aa64(env, 1) && 1334 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M); 1335 1336 if (el == 0) { 1337 filtered = secure ? u : u != nsu; 1338 } else if (el == 1) { 1339 filtered = secure ? p : p != nsk; 1340 } else if (el == 2) { 1341 filtered = !nsh; 1342 } else { /* EL3 */ 1343 filtered = m != p; 1344 } 1345 1346 if (counter != 31) { 1347 /* 1348 * If not checking PMCCNTR, ensure the counter is setup to an event we 1349 * support 1350 */ 1351 uint16_t event = filter & PMXEVTYPER_EVTCOUNT; 1352 if (!event_supported(event)) { 1353 return false; 1354 } 1355 } 1356 1357 return enabled && !prohibited && !filtered; 1358 } 1359 1360 static void pmu_update_irq(CPUARMState *env) 1361 { 1362 ARMCPU *cpu = env_archcpu(env); 1363 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) && 1364 (env->cp15.c9_pminten & env->cp15.c9_pmovsr)); 1365 } 1366 1367 /* 1368 * Ensure c15_ccnt is the guest-visible count so that operations such as 1369 * enabling/disabling the counter or filtering, modifying the count itself, 1370 * etc. can be done logically. This is essentially a no-op if the counter is 1371 * not enabled at the time of the call. 1372 */ 1373 static void pmccntr_op_start(CPUARMState *env) 1374 { 1375 uint64_t cycles = cycles_get_count(env); 1376 1377 if (pmu_counter_enabled(env, 31)) { 1378 uint64_t eff_cycles = cycles; 1379 if (env->cp15.c9_pmcr & PMCRD) { 1380 /* Increment once every 64 processor clock cycles */ 1381 eff_cycles /= 64; 1382 } 1383 1384 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta; 1385 1386 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \ 1387 1ull << 63 : 1ull << 31; 1388 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) { 1389 env->cp15.c9_pmovsr |= (1 << 31); 1390 pmu_update_irq(env); 1391 } 1392 1393 env->cp15.c15_ccnt = new_pmccntr; 1394 } 1395 env->cp15.c15_ccnt_delta = cycles; 1396 } 1397 1398 /* 1399 * If PMCCNTR is enabled, recalculate the delta between the clock and the 1400 * guest-visible count. A call to pmccntr_op_finish should follow every call to 1401 * pmccntr_op_start. 1402 */ 1403 static void pmccntr_op_finish(CPUARMState *env) 1404 { 1405 if (pmu_counter_enabled(env, 31)) { 1406 #ifndef CONFIG_USER_ONLY 1407 /* Calculate when the counter will next overflow */ 1408 uint64_t remaining_cycles = -env->cp15.c15_ccnt; 1409 if (!(env->cp15.c9_pmcr & PMCRLC)) { 1410 remaining_cycles = (uint32_t)remaining_cycles; 1411 } 1412 int64_t overflow_in = cycles_ns_per(remaining_cycles); 1413 1414 if (overflow_in > 0) { 1415 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1416 overflow_in; 1417 ARMCPU *cpu = env_archcpu(env); 1418 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1419 } 1420 #endif 1421 1422 uint64_t prev_cycles = env->cp15.c15_ccnt_delta; 1423 if (env->cp15.c9_pmcr & PMCRD) { 1424 /* Increment once every 64 processor clock cycles */ 1425 prev_cycles /= 64; 1426 } 1427 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt; 1428 } 1429 } 1430 1431 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter) 1432 { 1433 1434 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1435 uint64_t count = 0; 1436 if (event_supported(event)) { 1437 uint16_t event_idx = supported_event_map[event]; 1438 count = pm_events[event_idx].get_count(env); 1439 } 1440 1441 if (pmu_counter_enabled(env, counter)) { 1442 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter]; 1443 1444 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) { 1445 env->cp15.c9_pmovsr |= (1 << counter); 1446 pmu_update_irq(env); 1447 } 1448 env->cp15.c14_pmevcntr[counter] = new_pmevcntr; 1449 } 1450 env->cp15.c14_pmevcntr_delta[counter] = count; 1451 } 1452 1453 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter) 1454 { 1455 if (pmu_counter_enabled(env, counter)) { 1456 #ifndef CONFIG_USER_ONLY 1457 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1458 uint16_t event_idx = supported_event_map[event]; 1459 uint64_t delta = UINT32_MAX - 1460 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1; 1461 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta); 1462 1463 if (overflow_in > 0) { 1464 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1465 overflow_in; 1466 ARMCPU *cpu = env_archcpu(env); 1467 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1468 } 1469 #endif 1470 1471 env->cp15.c14_pmevcntr_delta[counter] -= 1472 env->cp15.c14_pmevcntr[counter]; 1473 } 1474 } 1475 1476 void pmu_op_start(CPUARMState *env) 1477 { 1478 unsigned int i; 1479 pmccntr_op_start(env); 1480 for (i = 0; i < pmu_num_counters(env); i++) { 1481 pmevcntr_op_start(env, i); 1482 } 1483 } 1484 1485 void pmu_op_finish(CPUARMState *env) 1486 { 1487 unsigned int i; 1488 pmccntr_op_finish(env); 1489 for (i = 0; i < pmu_num_counters(env); i++) { 1490 pmevcntr_op_finish(env, i); 1491 } 1492 } 1493 1494 void pmu_pre_el_change(ARMCPU *cpu, void *ignored) 1495 { 1496 pmu_op_start(&cpu->env); 1497 } 1498 1499 void pmu_post_el_change(ARMCPU *cpu, void *ignored) 1500 { 1501 pmu_op_finish(&cpu->env); 1502 } 1503 1504 void arm_pmu_timer_cb(void *opaque) 1505 { 1506 ARMCPU *cpu = opaque; 1507 1508 /* 1509 * Update all the counter values based on the current underlying counts, 1510 * triggering interrupts to be raised, if necessary. pmu_op_finish() also 1511 * has the effect of setting the cpu->pmu_timer to the next earliest time a 1512 * counter may expire. 1513 */ 1514 pmu_op_start(&cpu->env); 1515 pmu_op_finish(&cpu->env); 1516 } 1517 1518 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1519 uint64_t value) 1520 { 1521 pmu_op_start(env); 1522 1523 if (value & PMCRC) { 1524 /* The counter has been reset */ 1525 env->cp15.c15_ccnt = 0; 1526 } 1527 1528 if (value & PMCRP) { 1529 unsigned int i; 1530 for (i = 0; i < pmu_num_counters(env); i++) { 1531 env->cp15.c14_pmevcntr[i] = 0; 1532 } 1533 } 1534 1535 /* only the DP, X, D and E bits are writable */ 1536 env->cp15.c9_pmcr &= ~0x39; 1537 env->cp15.c9_pmcr |= (value & 0x39); 1538 1539 pmu_op_finish(env); 1540 } 1541 1542 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri, 1543 uint64_t value) 1544 { 1545 unsigned int i; 1546 for (i = 0; i < pmu_num_counters(env); i++) { 1547 /* Increment a counter's count iff: */ 1548 if ((value & (1 << i)) && /* counter's bit is set */ 1549 /* counter is enabled and not filtered */ 1550 pmu_counter_enabled(env, i) && 1551 /* counter is SW_INCR */ 1552 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) { 1553 pmevcntr_op_start(env, i); 1554 1555 /* 1556 * Detect if this write causes an overflow since we can't predict 1557 * PMSWINC overflows like we can for other events 1558 */ 1559 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1; 1560 1561 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) { 1562 env->cp15.c9_pmovsr |= (1 << i); 1563 pmu_update_irq(env); 1564 } 1565 1566 env->cp15.c14_pmevcntr[i] = new_pmswinc; 1567 1568 pmevcntr_op_finish(env, i); 1569 } 1570 } 1571 } 1572 1573 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1574 { 1575 uint64_t ret; 1576 pmccntr_op_start(env); 1577 ret = env->cp15.c15_ccnt; 1578 pmccntr_op_finish(env); 1579 return ret; 1580 } 1581 1582 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1583 uint64_t value) 1584 { 1585 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and 1586 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the 1587 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are 1588 * accessed. 1589 */ 1590 env->cp15.c9_pmselr = value & 0x1f; 1591 } 1592 1593 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1594 uint64_t value) 1595 { 1596 pmccntr_op_start(env); 1597 env->cp15.c15_ccnt = value; 1598 pmccntr_op_finish(env); 1599 } 1600 1601 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, 1602 uint64_t value) 1603 { 1604 uint64_t cur_val = pmccntr_read(env, NULL); 1605 1606 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); 1607 } 1608 1609 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1610 uint64_t value) 1611 { 1612 pmccntr_op_start(env); 1613 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0; 1614 pmccntr_op_finish(env); 1615 } 1616 1617 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri, 1618 uint64_t value) 1619 { 1620 pmccntr_op_start(env); 1621 /* M is not accessible from AArch32 */ 1622 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) | 1623 (value & PMCCFILTR); 1624 pmccntr_op_finish(env); 1625 } 1626 1627 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri) 1628 { 1629 /* M is not visible in AArch32 */ 1630 return env->cp15.pmccfiltr_el0 & PMCCFILTR; 1631 } 1632 1633 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1634 uint64_t value) 1635 { 1636 value &= pmu_counter_mask(env); 1637 env->cp15.c9_pmcnten |= value; 1638 } 1639 1640 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1641 uint64_t value) 1642 { 1643 value &= pmu_counter_mask(env); 1644 env->cp15.c9_pmcnten &= ~value; 1645 } 1646 1647 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1648 uint64_t value) 1649 { 1650 value &= pmu_counter_mask(env); 1651 env->cp15.c9_pmovsr &= ~value; 1652 pmu_update_irq(env); 1653 } 1654 1655 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1656 uint64_t value) 1657 { 1658 value &= pmu_counter_mask(env); 1659 env->cp15.c9_pmovsr |= value; 1660 pmu_update_irq(env); 1661 } 1662 1663 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1664 uint64_t value, const uint8_t counter) 1665 { 1666 if (counter == 31) { 1667 pmccfiltr_write(env, ri, value); 1668 } else if (counter < pmu_num_counters(env)) { 1669 pmevcntr_op_start(env, counter); 1670 1671 /* 1672 * If this counter's event type is changing, store the current 1673 * underlying count for the new type in c14_pmevcntr_delta[counter] so 1674 * pmevcntr_op_finish has the correct baseline when it converts back to 1675 * a delta. 1676 */ 1677 uint16_t old_event = env->cp15.c14_pmevtyper[counter] & 1678 PMXEVTYPER_EVTCOUNT; 1679 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT; 1680 if (old_event != new_event) { 1681 uint64_t count = 0; 1682 if (event_supported(new_event)) { 1683 uint16_t event_idx = supported_event_map[new_event]; 1684 count = pm_events[event_idx].get_count(env); 1685 } 1686 env->cp15.c14_pmevcntr_delta[counter] = count; 1687 } 1688 1689 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK; 1690 pmevcntr_op_finish(env, counter); 1691 } 1692 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when 1693 * PMSELR value is equal to or greater than the number of implemented 1694 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI. 1695 */ 1696 } 1697 1698 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri, 1699 const uint8_t counter) 1700 { 1701 if (counter == 31) { 1702 return env->cp15.pmccfiltr_el0; 1703 } else if (counter < pmu_num_counters(env)) { 1704 return env->cp15.c14_pmevtyper[counter]; 1705 } else { 1706 /* 1707 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER 1708 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write(). 1709 */ 1710 return 0; 1711 } 1712 } 1713 1714 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1715 uint64_t value) 1716 { 1717 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1718 pmevtyper_write(env, ri, value, counter); 1719 } 1720 1721 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1722 uint64_t value) 1723 { 1724 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1725 env->cp15.c14_pmevtyper[counter] = value; 1726 1727 /* 1728 * pmevtyper_rawwrite is called between a pair of pmu_op_start and 1729 * pmu_op_finish calls when loading saved state for a migration. Because 1730 * we're potentially updating the type of event here, the value written to 1731 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a 1732 * different counter type. Therefore, we need to set this value to the 1733 * current count for the counter type we're writing so that pmu_op_finish 1734 * has the correct count for its calculation. 1735 */ 1736 uint16_t event = value & PMXEVTYPER_EVTCOUNT; 1737 if (event_supported(event)) { 1738 uint16_t event_idx = supported_event_map[event]; 1739 env->cp15.c14_pmevcntr_delta[counter] = 1740 pm_events[event_idx].get_count(env); 1741 } 1742 } 1743 1744 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1745 { 1746 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1747 return pmevtyper_read(env, ri, counter); 1748 } 1749 1750 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1751 uint64_t value) 1752 { 1753 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31); 1754 } 1755 1756 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri) 1757 { 1758 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31); 1759 } 1760 1761 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1762 uint64_t value, uint8_t counter) 1763 { 1764 if (counter < pmu_num_counters(env)) { 1765 pmevcntr_op_start(env, counter); 1766 env->cp15.c14_pmevcntr[counter] = value; 1767 pmevcntr_op_finish(env, counter); 1768 } 1769 /* 1770 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1771 * are CONSTRAINED UNPREDICTABLE. 1772 */ 1773 } 1774 1775 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri, 1776 uint8_t counter) 1777 { 1778 if (counter < pmu_num_counters(env)) { 1779 uint64_t ret; 1780 pmevcntr_op_start(env, counter); 1781 ret = env->cp15.c14_pmevcntr[counter]; 1782 pmevcntr_op_finish(env, counter); 1783 return ret; 1784 } else { 1785 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1786 * are CONSTRAINED UNPREDICTABLE. */ 1787 return 0; 1788 } 1789 } 1790 1791 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1792 uint64_t value) 1793 { 1794 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1795 pmevcntr_write(env, ri, value, counter); 1796 } 1797 1798 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1799 { 1800 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1801 return pmevcntr_read(env, ri, counter); 1802 } 1803 1804 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1805 uint64_t value) 1806 { 1807 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1808 assert(counter < pmu_num_counters(env)); 1809 env->cp15.c14_pmevcntr[counter] = value; 1810 pmevcntr_write(env, ri, value, counter); 1811 } 1812 1813 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri) 1814 { 1815 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1816 assert(counter < pmu_num_counters(env)); 1817 return env->cp15.c14_pmevcntr[counter]; 1818 } 1819 1820 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1821 uint64_t value) 1822 { 1823 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31); 1824 } 1825 1826 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1827 { 1828 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31); 1829 } 1830 1831 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1832 uint64_t value) 1833 { 1834 if (arm_feature(env, ARM_FEATURE_V8)) { 1835 env->cp15.c9_pmuserenr = value & 0xf; 1836 } else { 1837 env->cp15.c9_pmuserenr = value & 1; 1838 } 1839 } 1840 1841 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1842 uint64_t value) 1843 { 1844 /* We have no event counters so only the C bit can be changed */ 1845 value &= pmu_counter_mask(env); 1846 env->cp15.c9_pminten |= value; 1847 pmu_update_irq(env); 1848 } 1849 1850 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1851 uint64_t value) 1852 { 1853 value &= pmu_counter_mask(env); 1854 env->cp15.c9_pminten &= ~value; 1855 pmu_update_irq(env); 1856 } 1857 1858 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 1859 uint64_t value) 1860 { 1861 /* Note that even though the AArch64 view of this register has bits 1862 * [10:0] all RES0 we can only mask the bottom 5, to comply with the 1863 * architectural requirements for bits which are RES0 only in some 1864 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 1865 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) 1866 */ 1867 raw_write(env, ri, value & ~0x1FULL); 1868 } 1869 1870 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 1871 { 1872 /* Begin with base v8.0 state. */ 1873 uint32_t valid_mask = 0x3fff; 1874 ARMCPU *cpu = env_archcpu(env); 1875 1876 if (arm_el_is_aa64(env, 3)) { 1877 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */ 1878 valid_mask &= ~SCR_NET; 1879 } else { 1880 valid_mask &= ~(SCR_RW | SCR_ST); 1881 } 1882 1883 if (!arm_feature(env, ARM_FEATURE_EL2)) { 1884 valid_mask &= ~SCR_HCE; 1885 1886 /* On ARMv7, SMD (or SCD as it is called in v7) is only 1887 * supported if EL2 exists. The bit is UNK/SBZP when 1888 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero 1889 * when EL2 is unavailable. 1890 * On ARMv8, this bit is always available. 1891 */ 1892 if (arm_feature(env, ARM_FEATURE_V7) && 1893 !arm_feature(env, ARM_FEATURE_V8)) { 1894 valid_mask &= ~SCR_SMD; 1895 } 1896 } 1897 if (cpu_isar_feature(aa64_lor, cpu)) { 1898 valid_mask |= SCR_TLOR; 1899 } 1900 if (cpu_isar_feature(aa64_pauth, cpu)) { 1901 valid_mask |= SCR_API | SCR_APK; 1902 } 1903 1904 /* Clear all-context RES0 bits. */ 1905 value &= valid_mask; 1906 raw_write(env, ri, value); 1907 } 1908 1909 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1910 { 1911 ARMCPU *cpu = env_archcpu(env); 1912 1913 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR 1914 * bank 1915 */ 1916 uint32_t index = A32_BANKED_REG_GET(env, csselr, 1917 ri->secure & ARM_CP_SECSTATE_S); 1918 1919 return cpu->ccsidr[index]; 1920 } 1921 1922 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1923 uint64_t value) 1924 { 1925 raw_write(env, ri, value & 0xf); 1926 } 1927 1928 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1929 { 1930 CPUState *cs = env_cpu(env); 1931 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 1932 uint64_t ret = 0; 1933 1934 if (hcr_el2 & HCR_IMO) { 1935 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) { 1936 ret |= CPSR_I; 1937 } 1938 } else { 1939 if (cs->interrupt_request & CPU_INTERRUPT_HARD) { 1940 ret |= CPSR_I; 1941 } 1942 } 1943 1944 if (hcr_el2 & HCR_FMO) { 1945 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) { 1946 ret |= CPSR_F; 1947 } 1948 } else { 1949 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { 1950 ret |= CPSR_F; 1951 } 1952 } 1953 1954 /* External aborts are not possible in QEMU so A bit is always clear */ 1955 return ret; 1956 } 1957 1958 static const ARMCPRegInfo v7_cp_reginfo[] = { 1959 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ 1960 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 1961 .access = PL1_W, .type = ARM_CP_NOP }, 1962 /* Performance monitors are implementation defined in v7, 1963 * but with an ARM recommended set of registers, which we 1964 * follow. 1965 * 1966 * Performance registers fall into three categories: 1967 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) 1968 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) 1969 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) 1970 * For the cases controlled by PMUSERENR we must set .access to PL0_RW 1971 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. 1972 */ 1973 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, 1974 .access = PL0_RW, .type = ARM_CP_ALIAS, 1975 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1976 .writefn = pmcntenset_write, 1977 .accessfn = pmreg_access, 1978 .raw_writefn = raw_write }, 1979 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, 1980 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, 1981 .access = PL0_RW, .accessfn = pmreg_access, 1982 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, 1983 .writefn = pmcntenset_write, .raw_writefn = raw_write }, 1984 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, 1985 .access = PL0_RW, 1986 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1987 .accessfn = pmreg_access, 1988 .writefn = pmcntenclr_write, 1989 .type = ARM_CP_ALIAS }, 1990 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, 1991 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, 1992 .access = PL0_RW, .accessfn = pmreg_access, 1993 .type = ARM_CP_ALIAS, 1994 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), 1995 .writefn = pmcntenclr_write }, 1996 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, 1997 .access = PL0_RW, .type = ARM_CP_IO, 1998 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 1999 .accessfn = pmreg_access, 2000 .writefn = pmovsr_write, 2001 .raw_writefn = raw_write }, 2002 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64, 2003 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3, 2004 .access = PL0_RW, .accessfn = pmreg_access, 2005 .type = ARM_CP_ALIAS | ARM_CP_IO, 2006 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 2007 .writefn = pmovsr_write, 2008 .raw_writefn = raw_write }, 2009 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, 2010 .access = PL0_W, .accessfn = pmreg_access_swinc, 2011 .type = ARM_CP_NO_RAW | ARM_CP_IO, 2012 .writefn = pmswinc_write }, 2013 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64, 2014 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4, 2015 .access = PL0_W, .accessfn = pmreg_access_swinc, 2016 .type = ARM_CP_NO_RAW | ARM_CP_IO, 2017 .writefn = pmswinc_write }, 2018 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, 2019 .access = PL0_RW, .type = ARM_CP_ALIAS, 2020 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr), 2021 .accessfn = pmreg_access_selr, .writefn = pmselr_write, 2022 .raw_writefn = raw_write}, 2023 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64, 2024 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5, 2025 .access = PL0_RW, .accessfn = pmreg_access_selr, 2026 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr), 2027 .writefn = pmselr_write, .raw_writefn = raw_write, }, 2028 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, 2029 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO, 2030 .readfn = pmccntr_read, .writefn = pmccntr_write32, 2031 .accessfn = pmreg_access_ccntr }, 2032 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, 2033 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, 2034 .access = PL0_RW, .accessfn = pmreg_access_ccntr, 2035 .type = ARM_CP_IO, 2036 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt), 2037 .readfn = pmccntr_read, .writefn = pmccntr_write, 2038 .raw_readfn = raw_read, .raw_writefn = raw_write, }, 2039 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7, 2040 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32, 2041 .access = PL0_RW, .accessfn = pmreg_access, 2042 .type = ARM_CP_ALIAS | ARM_CP_IO, 2043 .resetvalue = 0, }, 2044 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, 2045 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, 2046 .writefn = pmccfiltr_write, .raw_writefn = raw_write, 2047 .access = PL0_RW, .accessfn = pmreg_access, 2048 .type = ARM_CP_IO, 2049 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), 2050 .resetvalue = 0, }, 2051 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, 2052 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2053 .accessfn = pmreg_access, 2054 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 2055 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64, 2056 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1, 2057 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2058 .accessfn = pmreg_access, 2059 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 2060 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, 2061 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2062 .accessfn = pmreg_access_xevcntr, 2063 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2064 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64, 2065 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2, 2066 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2067 .accessfn = pmreg_access_xevcntr, 2068 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2069 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, 2070 .access = PL0_R | PL1_RW, .accessfn = access_tpm, 2071 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr), 2072 .resetvalue = 0, 2073 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2074 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64, 2075 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0, 2076 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 2077 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), 2078 .resetvalue = 0, 2079 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2080 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, 2081 .access = PL1_RW, .accessfn = access_tpm, 2082 .type = ARM_CP_ALIAS | ARM_CP_IO, 2083 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten), 2084 .resetvalue = 0, 2085 .writefn = pmintenset_write, .raw_writefn = raw_write }, 2086 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64, 2087 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1, 2088 .access = PL1_RW, .accessfn = access_tpm, 2089 .type = ARM_CP_IO, 2090 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2091 .writefn = pmintenset_write, .raw_writefn = raw_write, 2092 .resetvalue = 0x0 }, 2093 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, 2094 .access = PL1_RW, .accessfn = access_tpm, 2095 .type = ARM_CP_ALIAS | ARM_CP_IO, 2096 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2097 .writefn = pmintenclr_write, }, 2098 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64, 2099 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2, 2100 .access = PL1_RW, .accessfn = access_tpm, 2101 .type = ARM_CP_ALIAS | ARM_CP_IO, 2102 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2103 .writefn = pmintenclr_write }, 2104 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, 2105 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, 2106 .access = PL1_R, .readfn = ccsidr_read, .type = ARM_CP_NO_RAW }, 2107 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, 2108 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, 2109 .access = PL1_RW, .writefn = csselr_write, .resetvalue = 0, 2110 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), 2111 offsetof(CPUARMState, cp15.csselr_ns) } }, 2112 /* Auxiliary ID register: this actually has an IMPDEF value but for now 2113 * just RAZ for all cores: 2114 */ 2115 { .name = "AIDR", .state = ARM_CP_STATE_BOTH, 2116 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, 2117 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 2118 /* Auxiliary fault status registers: these also are IMPDEF, and we 2119 * choose to RAZ/WI for all cores. 2120 */ 2121 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, 2122 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, 2123 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 2124 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, 2125 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, 2126 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 2127 /* MAIR can just read-as-written because we don't implement caches 2128 * and so don't need to care about memory attributes. 2129 */ 2130 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, 2131 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 2132 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]), 2133 .resetvalue = 0 }, 2134 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64, 2135 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0, 2136 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]), 2137 .resetvalue = 0 }, 2138 /* For non-long-descriptor page tables these are PRRR and NMRR; 2139 * regardless they still act as reads-as-written for QEMU. 2140 */ 2141 /* MAIR0/1 are defined separately from their 64-bit counterpart which 2142 * allows them to assign the correct fieldoffset based on the endianness 2143 * handled in the field definitions. 2144 */ 2145 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, 2146 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, .access = PL1_RW, 2147 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s), 2148 offsetof(CPUARMState, cp15.mair0_ns) }, 2149 .resetfn = arm_cp_reset_ignore }, 2150 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, 2151 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, .access = PL1_RW, 2152 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s), 2153 offsetof(CPUARMState, cp15.mair1_ns) }, 2154 .resetfn = arm_cp_reset_ignore }, 2155 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, 2156 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, 2157 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read }, 2158 /* 32 bit ITLB invalidates */ 2159 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0, 2160 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 2161 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, 2162 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 2163 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2, 2164 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 2165 /* 32 bit DTLB invalidates */ 2166 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0, 2167 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 2168 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, 2169 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 2170 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2, 2171 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 2172 /* 32 bit TLB invalidates */ 2173 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 2174 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_write }, 2175 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 2176 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 2177 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 2178 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiasid_write }, 2179 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 2180 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, 2181 REGINFO_SENTINEL 2182 }; 2183 2184 static const ARMCPRegInfo v7mp_cp_reginfo[] = { 2185 /* 32 bit TLB invalidates, Inner Shareable */ 2186 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 2187 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbiall_is_write }, 2188 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 2189 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, 2190 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 2191 .type = ARM_CP_NO_RAW, .access = PL1_W, 2192 .writefn = tlbiasid_is_write }, 2193 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 2194 .type = ARM_CP_NO_RAW, .access = PL1_W, 2195 .writefn = tlbimvaa_is_write }, 2196 REGINFO_SENTINEL 2197 }; 2198 2199 static const ARMCPRegInfo pmovsset_cp_reginfo[] = { 2200 /* PMOVSSET is not implemented in v7 before v7ve */ 2201 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3, 2202 .access = PL0_RW, .accessfn = pmreg_access, 2203 .type = ARM_CP_ALIAS | ARM_CP_IO, 2204 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 2205 .writefn = pmovsset_write, 2206 .raw_writefn = raw_write }, 2207 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64, 2208 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3, 2209 .access = PL0_RW, .accessfn = pmreg_access, 2210 .type = ARM_CP_ALIAS | ARM_CP_IO, 2211 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 2212 .writefn = pmovsset_write, 2213 .raw_writefn = raw_write }, 2214 REGINFO_SENTINEL 2215 }; 2216 2217 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2218 uint64_t value) 2219 { 2220 value &= 1; 2221 env->teecr = value; 2222 } 2223 2224 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri, 2225 bool isread) 2226 { 2227 if (arm_current_el(env) == 0 && (env->teecr & 1)) { 2228 return CP_ACCESS_TRAP; 2229 } 2230 return CP_ACCESS_OK; 2231 } 2232 2233 static const ARMCPRegInfo t2ee_cp_reginfo[] = { 2234 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, 2235 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), 2236 .resetvalue = 0, 2237 .writefn = teecr_write }, 2238 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, 2239 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), 2240 .accessfn = teehbr_access, .resetvalue = 0 }, 2241 REGINFO_SENTINEL 2242 }; 2243 2244 static const ARMCPRegInfo v6k_cp_reginfo[] = { 2245 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, 2246 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, 2247 .access = PL0_RW, 2248 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 }, 2249 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, 2250 .access = PL0_RW, 2251 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s), 2252 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, 2253 .resetfn = arm_cp_reset_ignore }, 2254 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, 2255 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, 2256 .access = PL0_R|PL1_W, 2257 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]), 2258 .resetvalue = 0}, 2259 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, 2260 .access = PL0_R|PL1_W, 2261 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s), 2262 offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, 2263 .resetfn = arm_cp_reset_ignore }, 2264 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64, 2265 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, 2266 .access = PL1_RW, 2267 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 }, 2268 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4, 2269 .access = PL1_RW, 2270 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s), 2271 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) }, 2272 .resetvalue = 0 }, 2273 REGINFO_SENTINEL 2274 }; 2275 2276 #ifndef CONFIG_USER_ONLY 2277 2278 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, 2279 bool isread) 2280 { 2281 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero. 2282 * Writable only at the highest implemented exception level. 2283 */ 2284 int el = arm_current_el(env); 2285 2286 switch (el) { 2287 case 0: 2288 if (!extract32(env->cp15.c14_cntkctl, 0, 2)) { 2289 return CP_ACCESS_TRAP; 2290 } 2291 break; 2292 case 1: 2293 if (!isread && ri->state == ARM_CP_STATE_AA32 && 2294 arm_is_secure_below_el3(env)) { 2295 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */ 2296 return CP_ACCESS_TRAP_UNCATEGORIZED; 2297 } 2298 break; 2299 case 2: 2300 case 3: 2301 break; 2302 } 2303 2304 if (!isread && el < arm_highest_el(env)) { 2305 return CP_ACCESS_TRAP_UNCATEGORIZED; 2306 } 2307 2308 return CP_ACCESS_OK; 2309 } 2310 2311 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx, 2312 bool isread) 2313 { 2314 unsigned int cur_el = arm_current_el(env); 2315 bool secure = arm_is_secure(env); 2316 2317 /* CNT[PV]CT: not visible from PL0 if ELO[PV]CTEN is zero */ 2318 if (cur_el == 0 && 2319 !extract32(env->cp15.c14_cntkctl, timeridx, 1)) { 2320 return CP_ACCESS_TRAP; 2321 } 2322 2323 if (arm_feature(env, ARM_FEATURE_EL2) && 2324 timeridx == GTIMER_PHYS && !secure && cur_el < 2 && 2325 !extract32(env->cp15.cnthctl_el2, 0, 1)) { 2326 return CP_ACCESS_TRAP_EL2; 2327 } 2328 return CP_ACCESS_OK; 2329 } 2330 2331 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx, 2332 bool isread) 2333 { 2334 unsigned int cur_el = arm_current_el(env); 2335 bool secure = arm_is_secure(env); 2336 2337 /* CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from PL0 if 2338 * EL0[PV]TEN is zero. 2339 */ 2340 if (cur_el == 0 && 2341 !extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { 2342 return CP_ACCESS_TRAP; 2343 } 2344 2345 if (arm_feature(env, ARM_FEATURE_EL2) && 2346 timeridx == GTIMER_PHYS && !secure && cur_el < 2 && 2347 !extract32(env->cp15.cnthctl_el2, 1, 1)) { 2348 return CP_ACCESS_TRAP_EL2; 2349 } 2350 return CP_ACCESS_OK; 2351 } 2352 2353 static CPAccessResult gt_pct_access(CPUARMState *env, 2354 const ARMCPRegInfo *ri, 2355 bool isread) 2356 { 2357 return gt_counter_access(env, GTIMER_PHYS, isread); 2358 } 2359 2360 static CPAccessResult gt_vct_access(CPUARMState *env, 2361 const ARMCPRegInfo *ri, 2362 bool isread) 2363 { 2364 return gt_counter_access(env, GTIMER_VIRT, isread); 2365 } 2366 2367 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2368 bool isread) 2369 { 2370 return gt_timer_access(env, GTIMER_PHYS, isread); 2371 } 2372 2373 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2374 bool isread) 2375 { 2376 return gt_timer_access(env, GTIMER_VIRT, isread); 2377 } 2378 2379 static CPAccessResult gt_stimer_access(CPUARMState *env, 2380 const ARMCPRegInfo *ri, 2381 bool isread) 2382 { 2383 /* The AArch64 register view of the secure physical timer is 2384 * always accessible from EL3, and configurably accessible from 2385 * Secure EL1. 2386 */ 2387 switch (arm_current_el(env)) { 2388 case 1: 2389 if (!arm_is_secure(env)) { 2390 return CP_ACCESS_TRAP; 2391 } 2392 if (!(env->cp15.scr_el3 & SCR_ST)) { 2393 return CP_ACCESS_TRAP_EL3; 2394 } 2395 return CP_ACCESS_OK; 2396 case 0: 2397 case 2: 2398 return CP_ACCESS_TRAP; 2399 case 3: 2400 return CP_ACCESS_OK; 2401 default: 2402 g_assert_not_reached(); 2403 } 2404 } 2405 2406 static uint64_t gt_get_countervalue(CPUARMState *env) 2407 { 2408 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / GTIMER_SCALE; 2409 } 2410 2411 static void gt_recalc_timer(ARMCPU *cpu, int timeridx) 2412 { 2413 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; 2414 2415 if (gt->ctl & 1) { 2416 /* Timer enabled: calculate and set current ISTATUS, irq, and 2417 * reset timer to when ISTATUS next has to change 2418 */ 2419 uint64_t offset = timeridx == GTIMER_VIRT ? 2420 cpu->env.cp15.cntvoff_el2 : 0; 2421 uint64_t count = gt_get_countervalue(&cpu->env); 2422 /* Note that this must be unsigned 64 bit arithmetic: */ 2423 int istatus = count - offset >= gt->cval; 2424 uint64_t nexttick; 2425 int irqstate; 2426 2427 gt->ctl = deposit32(gt->ctl, 2, 1, istatus); 2428 2429 irqstate = (istatus && !(gt->ctl & 2)); 2430 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 2431 2432 if (istatus) { 2433 /* Next transition is when count rolls back over to zero */ 2434 nexttick = UINT64_MAX; 2435 } else { 2436 /* Next transition is when we hit cval */ 2437 nexttick = gt->cval + offset; 2438 } 2439 /* Note that the desired next expiry time might be beyond the 2440 * signed-64-bit range of a QEMUTimer -- in this case we just 2441 * set the timer for as far in the future as possible. When the 2442 * timer expires we will reset the timer for any remaining period. 2443 */ 2444 if (nexttick > INT64_MAX / GTIMER_SCALE) { 2445 nexttick = INT64_MAX / GTIMER_SCALE; 2446 } 2447 timer_mod(cpu->gt_timer[timeridx], nexttick); 2448 trace_arm_gt_recalc(timeridx, irqstate, nexttick); 2449 } else { 2450 /* Timer disabled: ISTATUS and timer output always clear */ 2451 gt->ctl &= ~4; 2452 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0); 2453 timer_del(cpu->gt_timer[timeridx]); 2454 trace_arm_gt_recalc_disabled(timeridx); 2455 } 2456 } 2457 2458 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri, 2459 int timeridx) 2460 { 2461 ARMCPU *cpu = env_archcpu(env); 2462 2463 timer_del(cpu->gt_timer[timeridx]); 2464 } 2465 2466 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2467 { 2468 return gt_get_countervalue(env); 2469 } 2470 2471 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2472 { 2473 return gt_get_countervalue(env) - env->cp15.cntvoff_el2; 2474 } 2475 2476 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2477 int timeridx, 2478 uint64_t value) 2479 { 2480 trace_arm_gt_cval_write(timeridx, value); 2481 env->cp15.c14_timer[timeridx].cval = value; 2482 gt_recalc_timer(env_archcpu(env), timeridx); 2483 } 2484 2485 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, 2486 int timeridx) 2487 { 2488 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; 2489 2490 return (uint32_t)(env->cp15.c14_timer[timeridx].cval - 2491 (gt_get_countervalue(env) - offset)); 2492 } 2493 2494 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2495 int timeridx, 2496 uint64_t value) 2497 { 2498 uint64_t offset = timeridx == GTIMER_VIRT ? env->cp15.cntvoff_el2 : 0; 2499 2500 trace_arm_gt_tval_write(timeridx, value); 2501 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset + 2502 sextract64(value, 0, 32); 2503 gt_recalc_timer(env_archcpu(env), timeridx); 2504 } 2505 2506 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2507 int timeridx, 2508 uint64_t value) 2509 { 2510 ARMCPU *cpu = env_archcpu(env); 2511 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; 2512 2513 trace_arm_gt_ctl_write(timeridx, value); 2514 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); 2515 if ((oldval ^ value) & 1) { 2516 /* Enable toggled */ 2517 gt_recalc_timer(cpu, timeridx); 2518 } else if ((oldval ^ value) & 2) { 2519 /* IMASK toggled: don't need to recalculate, 2520 * just set the interrupt line based on ISTATUS 2521 */ 2522 int irqstate = (oldval & 4) && !(value & 2); 2523 2524 trace_arm_gt_imask_toggle(timeridx, irqstate); 2525 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 2526 } 2527 } 2528 2529 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2530 { 2531 gt_timer_reset(env, ri, GTIMER_PHYS); 2532 } 2533 2534 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2535 uint64_t value) 2536 { 2537 gt_cval_write(env, ri, GTIMER_PHYS, value); 2538 } 2539 2540 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2541 { 2542 return gt_tval_read(env, ri, GTIMER_PHYS); 2543 } 2544 2545 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2546 uint64_t value) 2547 { 2548 gt_tval_write(env, ri, GTIMER_PHYS, value); 2549 } 2550 2551 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2552 uint64_t value) 2553 { 2554 gt_ctl_write(env, ri, GTIMER_PHYS, value); 2555 } 2556 2557 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2558 { 2559 gt_timer_reset(env, ri, GTIMER_VIRT); 2560 } 2561 2562 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2563 uint64_t value) 2564 { 2565 gt_cval_write(env, ri, GTIMER_VIRT, value); 2566 } 2567 2568 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2569 { 2570 return gt_tval_read(env, ri, GTIMER_VIRT); 2571 } 2572 2573 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2574 uint64_t value) 2575 { 2576 gt_tval_write(env, ri, GTIMER_VIRT, value); 2577 } 2578 2579 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2580 uint64_t value) 2581 { 2582 gt_ctl_write(env, ri, GTIMER_VIRT, value); 2583 } 2584 2585 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri, 2586 uint64_t value) 2587 { 2588 ARMCPU *cpu = env_archcpu(env); 2589 2590 trace_arm_gt_cntvoff_write(value); 2591 raw_write(env, ri, value); 2592 gt_recalc_timer(cpu, GTIMER_VIRT); 2593 } 2594 2595 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2596 { 2597 gt_timer_reset(env, ri, GTIMER_HYP); 2598 } 2599 2600 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2601 uint64_t value) 2602 { 2603 gt_cval_write(env, ri, GTIMER_HYP, value); 2604 } 2605 2606 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2607 { 2608 return gt_tval_read(env, ri, GTIMER_HYP); 2609 } 2610 2611 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2612 uint64_t value) 2613 { 2614 gt_tval_write(env, ri, GTIMER_HYP, value); 2615 } 2616 2617 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2618 uint64_t value) 2619 { 2620 gt_ctl_write(env, ri, GTIMER_HYP, value); 2621 } 2622 2623 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2624 { 2625 gt_timer_reset(env, ri, GTIMER_SEC); 2626 } 2627 2628 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2629 uint64_t value) 2630 { 2631 gt_cval_write(env, ri, GTIMER_SEC, value); 2632 } 2633 2634 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2635 { 2636 return gt_tval_read(env, ri, GTIMER_SEC); 2637 } 2638 2639 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2640 uint64_t value) 2641 { 2642 gt_tval_write(env, ri, GTIMER_SEC, value); 2643 } 2644 2645 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2646 uint64_t value) 2647 { 2648 gt_ctl_write(env, ri, GTIMER_SEC, value); 2649 } 2650 2651 void arm_gt_ptimer_cb(void *opaque) 2652 { 2653 ARMCPU *cpu = opaque; 2654 2655 gt_recalc_timer(cpu, GTIMER_PHYS); 2656 } 2657 2658 void arm_gt_vtimer_cb(void *opaque) 2659 { 2660 ARMCPU *cpu = opaque; 2661 2662 gt_recalc_timer(cpu, GTIMER_VIRT); 2663 } 2664 2665 void arm_gt_htimer_cb(void *opaque) 2666 { 2667 ARMCPU *cpu = opaque; 2668 2669 gt_recalc_timer(cpu, GTIMER_HYP); 2670 } 2671 2672 void arm_gt_stimer_cb(void *opaque) 2673 { 2674 ARMCPU *cpu = opaque; 2675 2676 gt_recalc_timer(cpu, GTIMER_SEC); 2677 } 2678 2679 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 2680 /* Note that CNTFRQ is purely reads-as-written for the benefit 2681 * of software; writing it doesn't actually change the timer frequency. 2682 * Our reset value matches the fixed frequency we implement the timer at. 2683 */ 2684 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, 2685 .type = ARM_CP_ALIAS, 2686 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 2687 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), 2688 }, 2689 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 2690 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 2691 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 2692 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 2693 .resetvalue = (1000 * 1000 * 1000) / GTIMER_SCALE, 2694 }, 2695 /* overall control: mostly access permissions */ 2696 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, 2697 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, 2698 .access = PL1_RW, 2699 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), 2700 .resetvalue = 0, 2701 }, 2702 /* per-timer control */ 2703 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 2704 .secure = ARM_CP_SECSTATE_NS, 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_PHYS].ctl), 2709 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, 2710 }, 2711 { .name = "CNTP_CTL_S", 2712 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 2713 .secure = ARM_CP_SECSTATE_S, 2714 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 2715 .accessfn = gt_ptimer_access, 2716 .fieldoffset = offsetoflow32(CPUARMState, 2717 cp15.c14_timer[GTIMER_SEC].ctl), 2718 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 2719 }, 2720 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, 2721 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, 2722 .type = ARM_CP_IO, .access = PL0_RW, 2723 .accessfn = gt_ptimer_access, 2724 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 2725 .resetvalue = 0, 2726 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write, 2727 }, 2728 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, 2729 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 2730 .accessfn = gt_vtimer_access, 2731 .fieldoffset = offsetoflow32(CPUARMState, 2732 cp15.c14_timer[GTIMER_VIRT].ctl), 2733 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, 2734 }, 2735 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, 2736 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, 2737 .type = ARM_CP_IO, .access = PL0_RW, 2738 .accessfn = gt_vtimer_access, 2739 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 2740 .resetvalue = 0, 2741 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write, 2742 }, 2743 /* TimerValue views: a 32 bit downcounting view of the underlying state */ 2744 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 2745 .secure = ARM_CP_SECSTATE_NS, 2746 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2747 .accessfn = gt_ptimer_access, 2748 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, 2749 }, 2750 { .name = "CNTP_TVAL_S", 2751 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 2752 .secure = ARM_CP_SECSTATE_S, 2753 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2754 .accessfn = gt_ptimer_access, 2755 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write, 2756 }, 2757 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, 2758 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, 2759 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2760 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset, 2761 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write, 2762 }, 2763 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, 2764 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2765 .accessfn = gt_vtimer_access, 2766 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, 2767 }, 2768 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, 2769 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, 2770 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 2771 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset, 2772 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write, 2773 }, 2774 /* The counter itself */ 2775 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, 2776 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 2777 .accessfn = gt_pct_access, 2778 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, 2779 }, 2780 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, 2781 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, 2782 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2783 .accessfn = gt_pct_access, .readfn = gt_cnt_read, 2784 }, 2785 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, 2786 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 2787 .accessfn = gt_vct_access, 2788 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, 2789 }, 2790 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 2791 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 2792 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2793 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, 2794 }, 2795 /* Comparison value, indicating when the timer goes off */ 2796 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, 2797 .secure = ARM_CP_SECSTATE_NS, 2798 .access = PL0_RW, 2799 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2800 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 2801 .accessfn = gt_ptimer_access, 2802 .writefn = gt_phys_cval_write, .raw_writefn = raw_write, 2803 }, 2804 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2, 2805 .secure = ARM_CP_SECSTATE_S, 2806 .access = PL0_RW, 2807 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2808 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 2809 .accessfn = gt_ptimer_access, 2810 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 2811 }, 2812 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, 2813 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, 2814 .access = PL0_RW, 2815 .type = ARM_CP_IO, 2816 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 2817 .resetvalue = 0, .accessfn = gt_ptimer_access, 2818 .writefn = gt_phys_cval_write, .raw_writefn = raw_write, 2819 }, 2820 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, 2821 .access = PL0_RW, 2822 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 2823 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 2824 .accessfn = gt_vtimer_access, 2825 .writefn = gt_virt_cval_write, .raw_writefn = raw_write, 2826 }, 2827 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, 2828 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, 2829 .access = PL0_RW, 2830 .type = ARM_CP_IO, 2831 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 2832 .resetvalue = 0, .accessfn = gt_vtimer_access, 2833 .writefn = gt_virt_cval_write, .raw_writefn = raw_write, 2834 }, 2835 /* Secure timer -- this is actually restricted to only EL3 2836 * and configurably Secure-EL1 via the accessfn. 2837 */ 2838 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64, 2839 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0, 2840 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW, 2841 .accessfn = gt_stimer_access, 2842 .readfn = gt_sec_tval_read, 2843 .writefn = gt_sec_tval_write, 2844 .resetfn = gt_sec_timer_reset, 2845 }, 2846 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64, 2847 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1, 2848 .type = ARM_CP_IO, .access = PL1_RW, 2849 .accessfn = gt_stimer_access, 2850 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl), 2851 .resetvalue = 0, 2852 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 2853 }, 2854 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64, 2855 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2, 2856 .type = ARM_CP_IO, .access = PL1_RW, 2857 .accessfn = gt_stimer_access, 2858 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 2859 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 2860 }, 2861 REGINFO_SENTINEL 2862 }; 2863 2864 #else 2865 2866 /* In user-mode most of the generic timer registers are inaccessible 2867 * however modern kernels (4.12+) allow access to cntvct_el0 2868 */ 2869 2870 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2871 { 2872 /* Currently we have no support for QEMUTimer in linux-user so we 2873 * can't call gt_get_countervalue(env), instead we directly 2874 * call the lower level functions. 2875 */ 2876 return cpu_get_clock() / GTIMER_SCALE; 2877 } 2878 2879 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 2880 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 2881 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 2882 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */, 2883 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 2884 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE, 2885 }, 2886 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 2887 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 2888 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2889 .readfn = gt_virt_cnt_read, 2890 }, 2891 REGINFO_SENTINEL 2892 }; 2893 2894 #endif 2895 2896 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 2897 { 2898 if (arm_feature(env, ARM_FEATURE_LPAE)) { 2899 raw_write(env, ri, value); 2900 } else if (arm_feature(env, ARM_FEATURE_V7)) { 2901 raw_write(env, ri, value & 0xfffff6ff); 2902 } else { 2903 raw_write(env, ri, value & 0xfffff1ff); 2904 } 2905 } 2906 2907 #ifndef CONFIG_USER_ONLY 2908 /* get_phys_addr() isn't present for user-mode-only targets */ 2909 2910 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri, 2911 bool isread) 2912 { 2913 if (ri->opc2 & 4) { 2914 /* The ATS12NSO* operations must trap to EL3 if executed in 2915 * Secure EL1 (which can only happen if EL3 is AArch64). 2916 * They are simply UNDEF if executed from NS EL1. 2917 * They function normally from EL2 or EL3. 2918 */ 2919 if (arm_current_el(env) == 1) { 2920 if (arm_is_secure_below_el3(env)) { 2921 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3; 2922 } 2923 return CP_ACCESS_TRAP_UNCATEGORIZED; 2924 } 2925 } 2926 return CP_ACCESS_OK; 2927 } 2928 2929 static uint64_t do_ats_write(CPUARMState *env, uint64_t value, 2930 MMUAccessType access_type, ARMMMUIdx mmu_idx) 2931 { 2932 hwaddr phys_addr; 2933 target_ulong page_size; 2934 int prot; 2935 bool ret; 2936 uint64_t par64; 2937 bool format64 = false; 2938 MemTxAttrs attrs = {}; 2939 ARMMMUFaultInfo fi = {}; 2940 ARMCacheAttrs cacheattrs = {}; 2941 2942 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs, 2943 &prot, &page_size, &fi, &cacheattrs); 2944 2945 if (is_a64(env)) { 2946 format64 = true; 2947 } else if (arm_feature(env, ARM_FEATURE_LPAE)) { 2948 /* 2949 * ATS1Cxx: 2950 * * TTBCR.EAE determines whether the result is returned using the 2951 * 32-bit or the 64-bit PAR format 2952 * * Instructions executed in Hyp mode always use the 64bit format 2953 * 2954 * ATS1S2NSOxx uses the 64bit format if any of the following is true: 2955 * * The Non-secure TTBCR.EAE bit is set to 1 2956 * * The implementation includes EL2, and the value of HCR.VM is 1 2957 * 2958 * (Note that HCR.DC makes HCR.VM behave as if it is 1.) 2959 * 2960 * ATS1Hx always uses the 64bit format. 2961 */ 2962 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx); 2963 2964 if (arm_feature(env, ARM_FEATURE_EL2)) { 2965 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 2966 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC); 2967 } else { 2968 format64 |= arm_current_el(env) == 2; 2969 } 2970 } 2971 } 2972 2973 if (format64) { 2974 /* Create a 64-bit PAR */ 2975 par64 = (1 << 11); /* LPAE bit always set */ 2976 if (!ret) { 2977 par64 |= phys_addr & ~0xfffULL; 2978 if (!attrs.secure) { 2979 par64 |= (1 << 9); /* NS */ 2980 } 2981 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */ 2982 par64 |= cacheattrs.shareability << 7; /* SH */ 2983 } else { 2984 uint32_t fsr = arm_fi_to_lfsc(&fi); 2985 2986 par64 |= 1; /* F */ 2987 par64 |= (fsr & 0x3f) << 1; /* FS */ 2988 if (fi.stage2) { 2989 par64 |= (1 << 9); /* S */ 2990 } 2991 if (fi.s1ptw) { 2992 par64 |= (1 << 8); /* PTW */ 2993 } 2994 } 2995 } else { 2996 /* fsr is a DFSR/IFSR value for the short descriptor 2997 * translation table format (with WnR always clear). 2998 * Convert it to a 32-bit PAR. 2999 */ 3000 if (!ret) { 3001 /* We do not set any attribute bits in the PAR */ 3002 if (page_size == (1 << 24) 3003 && arm_feature(env, ARM_FEATURE_V7)) { 3004 par64 = (phys_addr & 0xff000000) | (1 << 1); 3005 } else { 3006 par64 = phys_addr & 0xfffff000; 3007 } 3008 if (!attrs.secure) { 3009 par64 |= (1 << 9); /* NS */ 3010 } 3011 } else { 3012 uint32_t fsr = arm_fi_to_sfsc(&fi); 3013 3014 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) | 3015 ((fsr & 0xf) << 1) | 1; 3016 } 3017 } 3018 return par64; 3019 } 3020 3021 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3022 { 3023 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3024 uint64_t par64; 3025 ARMMMUIdx mmu_idx; 3026 int el = arm_current_el(env); 3027 bool secure = arm_is_secure_below_el3(env); 3028 3029 switch (ri->opc2 & 6) { 3030 case 0: 3031 /* stage 1 current state PL1: ATS1CPR, ATS1CPW */ 3032 switch (el) { 3033 case 3: 3034 mmu_idx = ARMMMUIdx_S1E3; 3035 break; 3036 case 2: 3037 mmu_idx = ARMMMUIdx_S1NSE1; 3038 break; 3039 case 1: 3040 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; 3041 break; 3042 default: 3043 g_assert_not_reached(); 3044 } 3045 break; 3046 case 2: 3047 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */ 3048 switch (el) { 3049 case 3: 3050 mmu_idx = ARMMMUIdx_S1SE0; 3051 break; 3052 case 2: 3053 mmu_idx = ARMMMUIdx_S1NSE0; 3054 break; 3055 case 1: 3056 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; 3057 break; 3058 default: 3059 g_assert_not_reached(); 3060 } 3061 break; 3062 case 4: 3063 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */ 3064 mmu_idx = ARMMMUIdx_S12NSE1; 3065 break; 3066 case 6: 3067 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */ 3068 mmu_idx = ARMMMUIdx_S12NSE0; 3069 break; 3070 default: 3071 g_assert_not_reached(); 3072 } 3073 3074 par64 = do_ats_write(env, value, access_type, mmu_idx); 3075 3076 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3077 } 3078 3079 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri, 3080 uint64_t value) 3081 { 3082 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3083 uint64_t par64; 3084 3085 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_S1E2); 3086 3087 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3088 } 3089 3090 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri, 3091 bool isread) 3092 { 3093 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) { 3094 return CP_ACCESS_TRAP; 3095 } 3096 return CP_ACCESS_OK; 3097 } 3098 3099 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri, 3100 uint64_t value) 3101 { 3102 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3103 ARMMMUIdx mmu_idx; 3104 int secure = arm_is_secure_below_el3(env); 3105 3106 switch (ri->opc2 & 6) { 3107 case 0: 3108 switch (ri->opc1) { 3109 case 0: /* AT S1E1R, AT S1E1W */ 3110 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S1NSE1; 3111 break; 3112 case 4: /* AT S1E2R, AT S1E2W */ 3113 mmu_idx = ARMMMUIdx_S1E2; 3114 break; 3115 case 6: /* AT S1E3R, AT S1E3W */ 3116 mmu_idx = ARMMMUIdx_S1E3; 3117 break; 3118 default: 3119 g_assert_not_reached(); 3120 } 3121 break; 3122 case 2: /* AT S1E0R, AT S1E0W */ 3123 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S1NSE0; 3124 break; 3125 case 4: /* AT S12E1R, AT S12E1W */ 3126 mmu_idx = secure ? ARMMMUIdx_S1SE1 : ARMMMUIdx_S12NSE1; 3127 break; 3128 case 6: /* AT S12E0R, AT S12E0W */ 3129 mmu_idx = secure ? ARMMMUIdx_S1SE0 : ARMMMUIdx_S12NSE0; 3130 break; 3131 default: 3132 g_assert_not_reached(); 3133 } 3134 3135 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx); 3136 } 3137 #endif 3138 3139 static const ARMCPRegInfo vapa_cp_reginfo[] = { 3140 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, 3141 .access = PL1_RW, .resetvalue = 0, 3142 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), 3143 offsetoflow32(CPUARMState, cp15.par_ns) }, 3144 .writefn = par_write }, 3145 #ifndef CONFIG_USER_ONLY 3146 /* This underdecoding is safe because the reginfo is NO_RAW. */ 3147 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, 3148 .access = PL1_W, .accessfn = ats_access, 3149 .writefn = ats_write, .type = ARM_CP_NO_RAW }, 3150 #endif 3151 REGINFO_SENTINEL 3152 }; 3153 3154 /* Return basic MPU access permission bits. */ 3155 static uint32_t simple_mpu_ap_bits(uint32_t val) 3156 { 3157 uint32_t ret; 3158 uint32_t mask; 3159 int i; 3160 ret = 0; 3161 mask = 3; 3162 for (i = 0; i < 16; i += 2) { 3163 ret |= (val >> i) & mask; 3164 mask <<= 2; 3165 } 3166 return ret; 3167 } 3168 3169 /* Pad basic MPU access permission bits to extended format. */ 3170 static uint32_t extended_mpu_ap_bits(uint32_t val) 3171 { 3172 uint32_t ret; 3173 uint32_t mask; 3174 int i; 3175 ret = 0; 3176 mask = 3; 3177 for (i = 0; i < 16; i += 2) { 3178 ret |= (val & mask) << i; 3179 mask <<= 2; 3180 } 3181 return ret; 3182 } 3183 3184 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3185 uint64_t value) 3186 { 3187 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); 3188 } 3189 3190 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3191 { 3192 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); 3193 } 3194 3195 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3196 uint64_t value) 3197 { 3198 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); 3199 } 3200 3201 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3202 { 3203 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); 3204 } 3205 3206 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri) 3207 { 3208 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3209 3210 if (!u32p) { 3211 return 0; 3212 } 3213 3214 u32p += env->pmsav7.rnr[M_REG_NS]; 3215 return *u32p; 3216 } 3217 3218 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri, 3219 uint64_t value) 3220 { 3221 ARMCPU *cpu = env_archcpu(env); 3222 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3223 3224 if (!u32p) { 3225 return; 3226 } 3227 3228 u32p += env->pmsav7.rnr[M_REG_NS]; 3229 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3230 *u32p = value; 3231 } 3232 3233 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3234 uint64_t value) 3235 { 3236 ARMCPU *cpu = env_archcpu(env); 3237 uint32_t nrgs = cpu->pmsav7_dregion; 3238 3239 if (value >= nrgs) { 3240 qemu_log_mask(LOG_GUEST_ERROR, 3241 "PMSAv7 RGNR write >= # supported regions, %" PRIu32 3242 " > %" PRIu32 "\n", (uint32_t)value, nrgs); 3243 return; 3244 } 3245 3246 raw_write(env, ri, value); 3247 } 3248 3249 static const ARMCPRegInfo pmsav7_cp_reginfo[] = { 3250 /* Reset for all these registers is handled in arm_cpu_reset(), 3251 * because the PMSAv7 is also used by M-profile CPUs, which do 3252 * not register cpregs but still need the state to be reset. 3253 */ 3254 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0, 3255 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3256 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar), 3257 .readfn = pmsav7_read, .writefn = pmsav7_write, 3258 .resetfn = arm_cp_reset_ignore }, 3259 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2, 3260 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3261 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr), 3262 .readfn = pmsav7_read, .writefn = pmsav7_write, 3263 .resetfn = arm_cp_reset_ignore }, 3264 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4, 3265 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3266 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr), 3267 .readfn = pmsav7_read, .writefn = pmsav7_write, 3268 .resetfn = arm_cp_reset_ignore }, 3269 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0, 3270 .access = PL1_RW, 3271 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]), 3272 .writefn = pmsav7_rgnr_write, 3273 .resetfn = arm_cp_reset_ignore }, 3274 REGINFO_SENTINEL 3275 }; 3276 3277 static const ARMCPRegInfo pmsav5_cp_reginfo[] = { 3278 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 3279 .access = PL1_RW, .type = ARM_CP_ALIAS, 3280 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 3281 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, 3282 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 3283 .access = PL1_RW, .type = ARM_CP_ALIAS, 3284 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 3285 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, 3286 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, 3287 .access = PL1_RW, 3288 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 3289 .resetvalue = 0, }, 3290 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, 3291 .access = PL1_RW, 3292 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 3293 .resetvalue = 0, }, 3294 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 3295 .access = PL1_RW, 3296 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, 3297 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, 3298 .access = PL1_RW, 3299 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, 3300 /* Protection region base and size registers */ 3301 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, 3302 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3303 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, 3304 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, 3305 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3306 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, 3307 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, 3308 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3309 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, 3310 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, 3311 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3312 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, 3313 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, 3314 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3315 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, 3316 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, 3317 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3318 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, 3319 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, 3320 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3321 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, 3322 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, 3323 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3324 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, 3325 REGINFO_SENTINEL 3326 }; 3327 3328 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 3329 uint64_t value) 3330 { 3331 TCR *tcr = raw_ptr(env, ri); 3332 int maskshift = extract32(value, 0, 3); 3333 3334 if (!arm_feature(env, ARM_FEATURE_V8)) { 3335 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { 3336 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when 3337 * using Long-desciptor translation table format */ 3338 value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); 3339 } else if (arm_feature(env, ARM_FEATURE_EL3)) { 3340 /* In an implementation that includes the Security Extensions 3341 * TTBCR has additional fields PD0 [4] and PD1 [5] for 3342 * Short-descriptor translation table format. 3343 */ 3344 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; 3345 } else { 3346 value &= TTBCR_N; 3347 } 3348 } 3349 3350 /* Update the masks corresponding to the TCR bank being written 3351 * Note that we always calculate mask and base_mask, but 3352 * they are only used for short-descriptor tables (ie if EAE is 0); 3353 * for long-descriptor tables the TCR fields are used differently 3354 * and the mask and base_mask values are meaningless. 3355 */ 3356 tcr->raw_tcr = value; 3357 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift); 3358 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift); 3359 } 3360 3361 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3362 uint64_t value) 3363 { 3364 ARMCPU *cpu = env_archcpu(env); 3365 TCR *tcr = raw_ptr(env, ri); 3366 3367 if (arm_feature(env, ARM_FEATURE_LPAE)) { 3368 /* With LPAE the TTBCR could result in a change of ASID 3369 * via the TTBCR.A1 bit, so do a TLB flush. 3370 */ 3371 tlb_flush(CPU(cpu)); 3372 } 3373 /* Preserve the high half of TCR_EL1, set via TTBCR2. */ 3374 value = deposit64(tcr->raw_tcr, 0, 32, value); 3375 vmsa_ttbcr_raw_write(env, ri, value); 3376 } 3377 3378 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3379 { 3380 TCR *tcr = raw_ptr(env, ri); 3381 3382 /* Reset both the TCR as well as the masks corresponding to the bank of 3383 * the TCR being reset. 3384 */ 3385 tcr->raw_tcr = 0; 3386 tcr->mask = 0; 3387 tcr->base_mask = 0xffffc000u; 3388 } 3389 3390 static void vmsa_tcr_el1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3391 uint64_t value) 3392 { 3393 ARMCPU *cpu = env_archcpu(env); 3394 TCR *tcr = raw_ptr(env, ri); 3395 3396 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ 3397 tlb_flush(CPU(cpu)); 3398 tcr->raw_tcr = value; 3399 } 3400 3401 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3402 uint64_t value) 3403 { 3404 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */ 3405 if (cpreg_field_is_64bit(ri) && 3406 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) { 3407 ARMCPU *cpu = env_archcpu(env); 3408 tlb_flush(CPU(cpu)); 3409 } 3410 raw_write(env, ri, value); 3411 } 3412 3413 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3414 uint64_t value) 3415 { 3416 ARMCPU *cpu = env_archcpu(env); 3417 CPUState *cs = CPU(cpu); 3418 3419 /* Accesses to VTTBR may change the VMID so we must flush the TLB. */ 3420 if (raw_read(env, ri) != value) { 3421 tlb_flush_by_mmuidx(cs, 3422 ARMMMUIdxBit_S12NSE1 | 3423 ARMMMUIdxBit_S12NSE0 | 3424 ARMMMUIdxBit_S2NS); 3425 raw_write(env, ri, value); 3426 } 3427 } 3428 3429 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = { 3430 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 3431 .access = PL1_RW, .type = ARM_CP_ALIAS, 3432 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), 3433 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, }, 3434 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 3435 .access = PL1_RW, .resetvalue = 0, 3436 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), 3437 offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, 3438 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, 3439 .access = PL1_RW, .resetvalue = 0, 3440 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), 3441 offsetof(CPUARMState, cp15.dfar_ns) } }, 3442 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, 3443 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, 3444 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), 3445 .resetvalue = 0, }, 3446 REGINFO_SENTINEL 3447 }; 3448 3449 static const ARMCPRegInfo vmsa_cp_reginfo[] = { 3450 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, 3451 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, 3452 .access = PL1_RW, 3453 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, 3454 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, 3455 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, 3456 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 3457 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 3458 offsetof(CPUARMState, cp15.ttbr0_ns) } }, 3459 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, 3460 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, 3461 .access = PL1_RW, .writefn = vmsa_ttbr_write, .resetvalue = 0, 3462 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 3463 offsetof(CPUARMState, cp15.ttbr1_ns) } }, 3464 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, 3465 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 3466 .access = PL1_RW, .writefn = vmsa_tcr_el1_write, 3467 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write, 3468 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, 3469 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 3470 .access = PL1_RW, .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write, 3471 .raw_writefn = vmsa_ttbcr_raw_write, 3472 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), 3473 offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, 3474 REGINFO_SENTINEL 3475 }; 3476 3477 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing 3478 * qemu tlbs nor adjusting cached masks. 3479 */ 3480 static const ARMCPRegInfo ttbcr2_reginfo = { 3481 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3, 3482 .access = PL1_RW, .type = ARM_CP_ALIAS, 3483 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]), 3484 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) }, 3485 }; 3486 3487 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, 3488 uint64_t value) 3489 { 3490 env->cp15.c15_ticonfig = value & 0xe7; 3491 /* The OS_TYPE bit in this register changes the reported CPUID! */ 3492 env->cp15.c0_cpuid = (value & (1 << 5)) ? 3493 ARM_CPUID_TI915T : ARM_CPUID_TI925T; 3494 } 3495 3496 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, 3497 uint64_t value) 3498 { 3499 env->cp15.c15_threadid = value & 0xffff; 3500 } 3501 3502 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, 3503 uint64_t value) 3504 { 3505 /* Wait-for-interrupt (deprecated) */ 3506 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT); 3507 } 3508 3509 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, 3510 uint64_t value) 3511 { 3512 /* On OMAP there are registers indicating the max/min index of dcache lines 3513 * containing a dirty line; cache flush operations have to reset these. 3514 */ 3515 env->cp15.c15_i_max = 0x000; 3516 env->cp15.c15_i_min = 0xff0; 3517 } 3518 3519 static const ARMCPRegInfo omap_cp_reginfo[] = { 3520 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, 3521 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, 3522 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), 3523 .resetvalue = 0, }, 3524 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, 3525 .access = PL1_RW, .type = ARM_CP_NOP }, 3526 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, 3527 .access = PL1_RW, 3528 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, 3529 .writefn = omap_ticonfig_write }, 3530 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, 3531 .access = PL1_RW, 3532 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, 3533 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, 3534 .access = PL1_RW, .resetvalue = 0xff0, 3535 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, 3536 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, 3537 .access = PL1_RW, 3538 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, 3539 .writefn = omap_threadid_write }, 3540 { .name = "TI925T_STATUS", .cp = 15, .crn = 15, 3541 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 3542 .type = ARM_CP_NO_RAW, 3543 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, 3544 /* TODO: Peripheral port remap register: 3545 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller 3546 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), 3547 * when MMU is off. 3548 */ 3549 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 3550 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 3551 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW, 3552 .writefn = omap_cachemaint_write }, 3553 { .name = "C9", .cp = 15, .crn = 9, 3554 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, 3555 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, 3556 REGINFO_SENTINEL 3557 }; 3558 3559 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3560 uint64_t value) 3561 { 3562 env->cp15.c15_cpar = value & 0x3fff; 3563 } 3564 3565 static const ARMCPRegInfo xscale_cp_reginfo[] = { 3566 { .name = "XSCALE_CPAR", 3567 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 3568 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, 3569 .writefn = xscale_cpar_write, }, 3570 { .name = "XSCALE_AUXCR", 3571 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, 3572 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), 3573 .resetvalue = 0, }, 3574 /* XScale specific cache-lockdown: since we have no cache we NOP these 3575 * and hope the guest does not really rely on cache behaviour. 3576 */ 3577 { .name = "XSCALE_LOCK_ICACHE_LINE", 3578 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, 3579 .access = PL1_W, .type = ARM_CP_NOP }, 3580 { .name = "XSCALE_UNLOCK_ICACHE", 3581 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, 3582 .access = PL1_W, .type = ARM_CP_NOP }, 3583 { .name = "XSCALE_DCACHE_LOCK", 3584 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, 3585 .access = PL1_RW, .type = ARM_CP_NOP }, 3586 { .name = "XSCALE_UNLOCK_DCACHE", 3587 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, 3588 .access = PL1_W, .type = ARM_CP_NOP }, 3589 REGINFO_SENTINEL 3590 }; 3591 3592 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { 3593 /* RAZ/WI the whole crn=15 space, when we don't have a more specific 3594 * implementation of this implementation-defined space. 3595 * Ideally this should eventually disappear in favour of actually 3596 * implementing the correct behaviour for all cores. 3597 */ 3598 { .name = "C15_IMPDEF", .cp = 15, .crn = 15, 3599 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 3600 .access = PL1_RW, 3601 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE, 3602 .resetvalue = 0 }, 3603 REGINFO_SENTINEL 3604 }; 3605 3606 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { 3607 /* Cache status: RAZ because we have no cache so it's always clean */ 3608 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, 3609 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 3610 .resetvalue = 0 }, 3611 REGINFO_SENTINEL 3612 }; 3613 3614 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { 3615 /* We never have a a block transfer operation in progress */ 3616 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, 3617 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 3618 .resetvalue = 0 }, 3619 /* The cache ops themselves: these all NOP for QEMU */ 3620 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, 3621 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3622 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, 3623 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3624 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, 3625 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3626 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, 3627 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3628 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, 3629 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3630 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, 3631 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 3632 REGINFO_SENTINEL 3633 }; 3634 3635 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { 3636 /* The cache test-and-clean instructions always return (1 << 30) 3637 * to indicate that there are no dirty cache lines. 3638 */ 3639 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, 3640 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 3641 .resetvalue = (1 << 30) }, 3642 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, 3643 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 3644 .resetvalue = (1 << 30) }, 3645 REGINFO_SENTINEL 3646 }; 3647 3648 static const ARMCPRegInfo strongarm_cp_reginfo[] = { 3649 /* Ignore ReadBuffer accesses */ 3650 { .name = "C9_READBUFFER", .cp = 15, .crn = 9, 3651 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 3652 .access = PL1_RW, .resetvalue = 0, 3653 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW }, 3654 REGINFO_SENTINEL 3655 }; 3656 3657 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3658 { 3659 ARMCPU *cpu = env_archcpu(env); 3660 unsigned int cur_el = arm_current_el(env); 3661 bool secure = arm_is_secure(env); 3662 3663 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 3664 return env->cp15.vpidr_el2; 3665 } 3666 return raw_read(env, ri); 3667 } 3668 3669 static uint64_t mpidr_read_val(CPUARMState *env) 3670 { 3671 ARMCPU *cpu = env_archcpu(env); 3672 uint64_t mpidr = cpu->mp_affinity; 3673 3674 if (arm_feature(env, ARM_FEATURE_V7MP)) { 3675 mpidr |= (1U << 31); 3676 /* Cores which are uniprocessor (non-coherent) 3677 * but still implement the MP extensions set 3678 * bit 30. (For instance, Cortex-R5). 3679 */ 3680 if (cpu->mp_is_up) { 3681 mpidr |= (1u << 30); 3682 } 3683 } 3684 return mpidr; 3685 } 3686 3687 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3688 { 3689 unsigned int cur_el = arm_current_el(env); 3690 bool secure = arm_is_secure(env); 3691 3692 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 3693 return env->cp15.vmpidr_el2; 3694 } 3695 return mpidr_read_val(env); 3696 } 3697 3698 static const ARMCPRegInfo lpae_cp_reginfo[] = { 3699 /* NOP AMAIR0/1 */ 3700 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, 3701 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, 3702 .access = PL1_RW, .type = ARM_CP_CONST, 3703 .resetvalue = 0 }, 3704 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ 3705 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, 3706 .access = PL1_RW, .type = ARM_CP_CONST, 3707 .resetvalue = 0 }, 3708 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, 3709 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, 3710 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), 3711 offsetof(CPUARMState, cp15.par_ns)} }, 3712 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, 3713 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3714 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 3715 offsetof(CPUARMState, cp15.ttbr0_ns) }, 3716 .writefn = vmsa_ttbr_write, }, 3717 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, 3718 .access = PL1_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 3719 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 3720 offsetof(CPUARMState, cp15.ttbr1_ns) }, 3721 .writefn = vmsa_ttbr_write, }, 3722 REGINFO_SENTINEL 3723 }; 3724 3725 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3726 { 3727 return vfp_get_fpcr(env); 3728 } 3729 3730 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3731 uint64_t value) 3732 { 3733 vfp_set_fpcr(env, value); 3734 } 3735 3736 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) 3737 { 3738 return vfp_get_fpsr(env); 3739 } 3740 3741 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3742 uint64_t value) 3743 { 3744 vfp_set_fpsr(env, value); 3745 } 3746 3747 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri, 3748 bool isread) 3749 { 3750 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UMA)) { 3751 return CP_ACCESS_TRAP; 3752 } 3753 return CP_ACCESS_OK; 3754 } 3755 3756 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, 3757 uint64_t value) 3758 { 3759 env->daif = value & PSTATE_DAIF; 3760 } 3761 3762 static CPAccessResult aa64_cacheop_access(CPUARMState *env, 3763 const ARMCPRegInfo *ri, 3764 bool isread) 3765 { 3766 /* Cache invalidate/clean: NOP, but EL0 must UNDEF unless 3767 * SCTLR_EL1.UCI is set. 3768 */ 3769 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCI)) { 3770 return CP_ACCESS_TRAP; 3771 } 3772 return CP_ACCESS_OK; 3773 } 3774 3775 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions 3776 * Page D4-1736 (DDI0487A.b) 3777 */ 3778 3779 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3780 uint64_t value) 3781 { 3782 CPUState *cs = env_cpu(env); 3783 bool sec = arm_is_secure_below_el3(env); 3784 3785 if (sec) { 3786 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3787 ARMMMUIdxBit_S1SE1 | 3788 ARMMMUIdxBit_S1SE0); 3789 } else { 3790 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3791 ARMMMUIdxBit_S12NSE1 | 3792 ARMMMUIdxBit_S12NSE0); 3793 } 3794 } 3795 3796 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3797 uint64_t value) 3798 { 3799 CPUState *cs = env_cpu(env); 3800 3801 if (tlb_force_broadcast(env)) { 3802 tlbi_aa64_vmalle1is_write(env, NULL, value); 3803 return; 3804 } 3805 3806 if (arm_is_secure_below_el3(env)) { 3807 tlb_flush_by_mmuidx(cs, 3808 ARMMMUIdxBit_S1SE1 | 3809 ARMMMUIdxBit_S1SE0); 3810 } else { 3811 tlb_flush_by_mmuidx(cs, 3812 ARMMMUIdxBit_S12NSE1 | 3813 ARMMMUIdxBit_S12NSE0); 3814 } 3815 } 3816 3817 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3818 uint64_t value) 3819 { 3820 /* Note that the 'ALL' scope must invalidate both stage 1 and 3821 * stage 2 translations, whereas most other scopes only invalidate 3822 * stage 1 translations. 3823 */ 3824 ARMCPU *cpu = env_archcpu(env); 3825 CPUState *cs = CPU(cpu); 3826 3827 if (arm_is_secure_below_el3(env)) { 3828 tlb_flush_by_mmuidx(cs, 3829 ARMMMUIdxBit_S1SE1 | 3830 ARMMMUIdxBit_S1SE0); 3831 } else { 3832 if (arm_feature(env, ARM_FEATURE_EL2)) { 3833 tlb_flush_by_mmuidx(cs, 3834 ARMMMUIdxBit_S12NSE1 | 3835 ARMMMUIdxBit_S12NSE0 | 3836 ARMMMUIdxBit_S2NS); 3837 } else { 3838 tlb_flush_by_mmuidx(cs, 3839 ARMMMUIdxBit_S12NSE1 | 3840 ARMMMUIdxBit_S12NSE0); 3841 } 3842 } 3843 } 3844 3845 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3846 uint64_t value) 3847 { 3848 ARMCPU *cpu = env_archcpu(env); 3849 CPUState *cs = CPU(cpu); 3850 3851 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E2); 3852 } 3853 3854 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri, 3855 uint64_t value) 3856 { 3857 ARMCPU *cpu = env_archcpu(env); 3858 CPUState *cs = CPU(cpu); 3859 3860 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_S1E3); 3861 } 3862 3863 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3864 uint64_t value) 3865 { 3866 /* Note that the 'ALL' scope must invalidate both stage 1 and 3867 * stage 2 translations, whereas most other scopes only invalidate 3868 * stage 1 translations. 3869 */ 3870 CPUState *cs = env_cpu(env); 3871 bool sec = arm_is_secure_below_el3(env); 3872 bool has_el2 = arm_feature(env, ARM_FEATURE_EL2); 3873 3874 if (sec) { 3875 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3876 ARMMMUIdxBit_S1SE1 | 3877 ARMMMUIdxBit_S1SE0); 3878 } else if (has_el2) { 3879 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3880 ARMMMUIdxBit_S12NSE1 | 3881 ARMMMUIdxBit_S12NSE0 | 3882 ARMMMUIdxBit_S2NS); 3883 } else { 3884 tlb_flush_by_mmuidx_all_cpus_synced(cs, 3885 ARMMMUIdxBit_S12NSE1 | 3886 ARMMMUIdxBit_S12NSE0); 3887 } 3888 } 3889 3890 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3891 uint64_t value) 3892 { 3893 CPUState *cs = env_cpu(env); 3894 3895 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E2); 3896 } 3897 3898 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3899 uint64_t value) 3900 { 3901 CPUState *cs = env_cpu(env); 3902 3903 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_S1E3); 3904 } 3905 3906 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3907 uint64_t value) 3908 { 3909 /* Invalidate by VA, EL2 3910 * Currently handles both VAE2 and VALE2, since we don't support 3911 * flush-last-level-only. 3912 */ 3913 ARMCPU *cpu = env_archcpu(env); 3914 CPUState *cs = CPU(cpu); 3915 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3916 3917 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E2); 3918 } 3919 3920 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri, 3921 uint64_t value) 3922 { 3923 /* Invalidate by VA, EL3 3924 * Currently handles both VAE3 and VALE3, since we don't support 3925 * flush-last-level-only. 3926 */ 3927 ARMCPU *cpu = env_archcpu(env); 3928 CPUState *cs = CPU(cpu); 3929 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3930 3931 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S1E3); 3932 } 3933 3934 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3935 uint64_t value) 3936 { 3937 ARMCPU *cpu = env_archcpu(env); 3938 CPUState *cs = CPU(cpu); 3939 bool sec = arm_is_secure_below_el3(env); 3940 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3941 3942 if (sec) { 3943 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3944 ARMMMUIdxBit_S1SE1 | 3945 ARMMMUIdxBit_S1SE0); 3946 } else { 3947 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3948 ARMMMUIdxBit_S12NSE1 | 3949 ARMMMUIdxBit_S12NSE0); 3950 } 3951 } 3952 3953 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri, 3954 uint64_t value) 3955 { 3956 /* Invalidate by VA, EL1&0 (AArch64 version). 3957 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1, 3958 * since we don't support flush-for-specific-ASID-only or 3959 * flush-last-level-only. 3960 */ 3961 ARMCPU *cpu = env_archcpu(env); 3962 CPUState *cs = CPU(cpu); 3963 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3964 3965 if (tlb_force_broadcast(env)) { 3966 tlbi_aa64_vae1is_write(env, NULL, value); 3967 return; 3968 } 3969 3970 if (arm_is_secure_below_el3(env)) { 3971 tlb_flush_page_by_mmuidx(cs, pageaddr, 3972 ARMMMUIdxBit_S1SE1 | 3973 ARMMMUIdxBit_S1SE0); 3974 } else { 3975 tlb_flush_page_by_mmuidx(cs, pageaddr, 3976 ARMMMUIdxBit_S12NSE1 | 3977 ARMMMUIdxBit_S12NSE0); 3978 } 3979 } 3980 3981 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3982 uint64_t value) 3983 { 3984 CPUState *cs = env_cpu(env); 3985 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3986 3987 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3988 ARMMMUIdxBit_S1E2); 3989 } 3990 3991 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 3992 uint64_t value) 3993 { 3994 CPUState *cs = env_cpu(env); 3995 uint64_t pageaddr = sextract64(value << 12, 0, 56); 3996 3997 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 3998 ARMMMUIdxBit_S1E3); 3999 } 4000 4001 static void tlbi_aa64_ipas2e1_write(CPUARMState *env, const ARMCPRegInfo *ri, 4002 uint64_t value) 4003 { 4004 /* Invalidate by IPA. This has to invalidate any structures that 4005 * contain only stage 2 translation information, but does not need 4006 * to apply to structures that contain combined stage 1 and stage 2 4007 * translation information. 4008 * This must NOP if EL2 isn't implemented or SCR_EL3.NS is zero. 4009 */ 4010 ARMCPU *cpu = env_archcpu(env); 4011 CPUState *cs = CPU(cpu); 4012 uint64_t pageaddr; 4013 4014 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 4015 return; 4016 } 4017 4018 pageaddr = sextract64(value << 12, 0, 48); 4019 4020 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_S2NS); 4021 } 4022 4023 static void tlbi_aa64_ipas2e1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4024 uint64_t value) 4025 { 4026 CPUState *cs = env_cpu(env); 4027 uint64_t pageaddr; 4028 4029 if (!arm_feature(env, ARM_FEATURE_EL2) || !(env->cp15.scr_el3 & SCR_NS)) { 4030 return; 4031 } 4032 4033 pageaddr = sextract64(value << 12, 0, 48); 4034 4035 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 4036 ARMMMUIdxBit_S2NS); 4037 } 4038 4039 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri, 4040 bool isread) 4041 { 4042 /* We don't implement EL2, so the only control on DC ZVA is the 4043 * bit in the SCTLR which can prohibit access for EL0. 4044 */ 4045 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_DZE)) { 4046 return CP_ACCESS_TRAP; 4047 } 4048 return CP_ACCESS_OK; 4049 } 4050 4051 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) 4052 { 4053 ARMCPU *cpu = env_archcpu(env); 4054 int dzp_bit = 1 << 4; 4055 4056 /* DZP indicates whether DC ZVA access is allowed */ 4057 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) { 4058 dzp_bit = 0; 4059 } 4060 return cpu->dcz_blocksize | dzp_bit; 4061 } 4062 4063 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 4064 bool isread) 4065 { 4066 if (!(env->pstate & PSTATE_SP)) { 4067 /* Access to SP_EL0 is undefined if it's being used as 4068 * the stack pointer. 4069 */ 4070 return CP_ACCESS_TRAP_UNCATEGORIZED; 4071 } 4072 return CP_ACCESS_OK; 4073 } 4074 4075 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) 4076 { 4077 return env->pstate & PSTATE_SP; 4078 } 4079 4080 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 4081 { 4082 update_spsel(env, val); 4083 } 4084 4085 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4086 uint64_t value) 4087 { 4088 ARMCPU *cpu = env_archcpu(env); 4089 4090 if (raw_read(env, ri) == value) { 4091 /* Skip the TLB flush if nothing actually changed; Linux likes 4092 * to do a lot of pointless SCTLR writes. 4093 */ 4094 return; 4095 } 4096 4097 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) { 4098 /* M bit is RAZ/WI for PMSA with no MPU implemented */ 4099 value &= ~SCTLR_M; 4100 } 4101 4102 raw_write(env, ri, value); 4103 /* ??? Lots of these bits are not implemented. */ 4104 /* This may enable/disable the MMU, so do a TLB flush. */ 4105 tlb_flush(CPU(cpu)); 4106 } 4107 4108 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri, 4109 bool isread) 4110 { 4111 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) { 4112 return CP_ACCESS_TRAP_FP_EL2; 4113 } 4114 if (env->cp15.cptr_el[3] & CPTR_TFP) { 4115 return CP_ACCESS_TRAP_FP_EL3; 4116 } 4117 return CP_ACCESS_OK; 4118 } 4119 4120 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4121 uint64_t value) 4122 { 4123 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK; 4124 } 4125 4126 static const ARMCPRegInfo v8_cp_reginfo[] = { 4127 /* Minimal set of EL0-visible registers. This will need to be expanded 4128 * significantly for system emulation of AArch64 CPUs. 4129 */ 4130 { .name = "NZCV", .state = ARM_CP_STATE_AA64, 4131 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, 4132 .access = PL0_RW, .type = ARM_CP_NZCV }, 4133 { .name = "DAIF", .state = ARM_CP_STATE_AA64, 4134 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, 4135 .type = ARM_CP_NO_RAW, 4136 .access = PL0_RW, .accessfn = aa64_daif_access, 4137 .fieldoffset = offsetof(CPUARMState, daif), 4138 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, 4139 { .name = "FPCR", .state = ARM_CP_STATE_AA64, 4140 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, 4141 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 4142 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, 4143 { .name = "FPSR", .state = ARM_CP_STATE_AA64, 4144 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, 4145 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 4146 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, 4147 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, 4148 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, 4149 .access = PL0_R, .type = ARM_CP_NO_RAW, 4150 .readfn = aa64_dczid_read }, 4151 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, 4152 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, 4153 .access = PL0_W, .type = ARM_CP_DC_ZVA, 4154 #ifndef CONFIG_USER_ONLY 4155 /* Avoid overhead of an access check that always passes in user-mode */ 4156 .accessfn = aa64_zva_access, 4157 #endif 4158 }, 4159 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, 4160 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, 4161 .access = PL1_R, .type = ARM_CP_CURRENTEL }, 4162 /* Cache ops: all NOPs since we don't emulate caches */ 4163 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, 4164 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 4165 .access = PL1_W, .type = ARM_CP_NOP }, 4166 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, 4167 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 4168 .access = PL1_W, .type = ARM_CP_NOP }, 4169 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, 4170 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, 4171 .access = PL0_W, .type = ARM_CP_NOP, 4172 .accessfn = aa64_cacheop_access }, 4173 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, 4174 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 4175 .access = PL1_W, .type = ARM_CP_NOP }, 4176 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, 4177 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 4178 .access = PL1_W, .type = ARM_CP_NOP }, 4179 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, 4180 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, 4181 .access = PL0_W, .type = ARM_CP_NOP, 4182 .accessfn = aa64_cacheop_access }, 4183 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, 4184 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 4185 .access = PL1_W, .type = ARM_CP_NOP }, 4186 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, 4187 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, 4188 .access = PL0_W, .type = ARM_CP_NOP, 4189 .accessfn = aa64_cacheop_access }, 4190 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, 4191 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, 4192 .access = PL0_W, .type = ARM_CP_NOP, 4193 .accessfn = aa64_cacheop_access }, 4194 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, 4195 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 4196 .access = PL1_W, .type = ARM_CP_NOP }, 4197 /* TLBI operations */ 4198 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64, 4199 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 4200 .access = PL1_W, .type = ARM_CP_NO_RAW, 4201 .writefn = tlbi_aa64_vmalle1is_write }, 4202 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64, 4203 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 4204 .access = PL1_W, .type = ARM_CP_NO_RAW, 4205 .writefn = tlbi_aa64_vae1is_write }, 4206 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64, 4207 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 4208 .access = PL1_W, .type = ARM_CP_NO_RAW, 4209 .writefn = tlbi_aa64_vmalle1is_write }, 4210 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64, 4211 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 4212 .access = PL1_W, .type = ARM_CP_NO_RAW, 4213 .writefn = tlbi_aa64_vae1is_write }, 4214 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64, 4215 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 4216 .access = PL1_W, .type = ARM_CP_NO_RAW, 4217 .writefn = tlbi_aa64_vae1is_write }, 4218 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64, 4219 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 4220 .access = PL1_W, .type = ARM_CP_NO_RAW, 4221 .writefn = tlbi_aa64_vae1is_write }, 4222 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64, 4223 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 4224 .access = PL1_W, .type = ARM_CP_NO_RAW, 4225 .writefn = tlbi_aa64_vmalle1_write }, 4226 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64, 4227 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 4228 .access = PL1_W, .type = ARM_CP_NO_RAW, 4229 .writefn = tlbi_aa64_vae1_write }, 4230 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64, 4231 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 4232 .access = PL1_W, .type = ARM_CP_NO_RAW, 4233 .writefn = tlbi_aa64_vmalle1_write }, 4234 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64, 4235 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 4236 .access = PL1_W, .type = ARM_CP_NO_RAW, 4237 .writefn = tlbi_aa64_vae1_write }, 4238 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64, 4239 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 4240 .access = PL1_W, .type = ARM_CP_NO_RAW, 4241 .writefn = tlbi_aa64_vae1_write }, 4242 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64, 4243 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 4244 .access = PL1_W, .type = ARM_CP_NO_RAW, 4245 .writefn = tlbi_aa64_vae1_write }, 4246 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64, 4247 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 4248 .access = PL2_W, .type = ARM_CP_NO_RAW, 4249 .writefn = tlbi_aa64_ipas2e1is_write }, 4250 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64, 4251 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 4252 .access = PL2_W, .type = ARM_CP_NO_RAW, 4253 .writefn = tlbi_aa64_ipas2e1is_write }, 4254 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64, 4255 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 4256 .access = PL2_W, .type = ARM_CP_NO_RAW, 4257 .writefn = tlbi_aa64_alle1is_write }, 4258 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64, 4259 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6, 4260 .access = PL2_W, .type = ARM_CP_NO_RAW, 4261 .writefn = tlbi_aa64_alle1is_write }, 4262 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64, 4263 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 4264 .access = PL2_W, .type = ARM_CP_NO_RAW, 4265 .writefn = tlbi_aa64_ipas2e1_write }, 4266 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64, 4267 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 4268 .access = PL2_W, .type = ARM_CP_NO_RAW, 4269 .writefn = tlbi_aa64_ipas2e1_write }, 4270 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64, 4271 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 4272 .access = PL2_W, .type = ARM_CP_NO_RAW, 4273 .writefn = tlbi_aa64_alle1_write }, 4274 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64, 4275 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6, 4276 .access = PL2_W, .type = ARM_CP_NO_RAW, 4277 .writefn = tlbi_aa64_alle1is_write }, 4278 #ifndef CONFIG_USER_ONLY 4279 /* 64 bit address translation operations */ 4280 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, 4281 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, 4282 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4283 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, 4284 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, 4285 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4286 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, 4287 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, 4288 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4289 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, 4290 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, 4291 .access = PL1_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4292 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64, 4293 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4, 4294 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4295 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64, 4296 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5, 4297 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4298 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64, 4299 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6, 4300 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4301 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64, 4302 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7, 4303 .access = PL2_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4304 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */ 4305 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64, 4306 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0, 4307 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4308 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64, 4309 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1, 4310 .access = PL3_W, .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4311 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64, 4312 .type = ARM_CP_ALIAS, 4313 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0, 4314 .access = PL1_RW, .resetvalue = 0, 4315 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]), 4316 .writefn = par_write }, 4317 #endif 4318 /* TLB invalidate last level of translation table walk */ 4319 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 4320 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_is_write }, 4321 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 4322 .type = ARM_CP_NO_RAW, .access = PL1_W, 4323 .writefn = tlbimvaa_is_write }, 4324 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 4325 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimva_write }, 4326 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 4327 .type = ARM_CP_NO_RAW, .access = PL1_W, .writefn = tlbimvaa_write }, 4328 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 4329 .type = ARM_CP_NO_RAW, .access = PL2_W, 4330 .writefn = tlbimva_hyp_write }, 4331 { .name = "TLBIMVALHIS", 4332 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 4333 .type = ARM_CP_NO_RAW, .access = PL2_W, 4334 .writefn = tlbimva_hyp_is_write }, 4335 { .name = "TLBIIPAS2", 4336 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 4337 .type = ARM_CP_NO_RAW, .access = PL2_W, 4338 .writefn = tlbiipas2_write }, 4339 { .name = "TLBIIPAS2IS", 4340 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 4341 .type = ARM_CP_NO_RAW, .access = PL2_W, 4342 .writefn = tlbiipas2_is_write }, 4343 { .name = "TLBIIPAS2L", 4344 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 4345 .type = ARM_CP_NO_RAW, .access = PL2_W, 4346 .writefn = tlbiipas2_write }, 4347 { .name = "TLBIIPAS2LIS", 4348 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 4349 .type = ARM_CP_NO_RAW, .access = PL2_W, 4350 .writefn = tlbiipas2_is_write }, 4351 /* 32 bit cache operations */ 4352 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 4353 .type = ARM_CP_NOP, .access = PL1_W }, 4354 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, 4355 .type = ARM_CP_NOP, .access = PL1_W }, 4356 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 4357 .type = ARM_CP_NOP, .access = PL1_W }, 4358 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, 4359 .type = ARM_CP_NOP, .access = PL1_W }, 4360 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, 4361 .type = ARM_CP_NOP, .access = PL1_W }, 4362 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, 4363 .type = ARM_CP_NOP, .access = PL1_W }, 4364 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 4365 .type = ARM_CP_NOP, .access = PL1_W }, 4366 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 4367 .type = ARM_CP_NOP, .access = PL1_W }, 4368 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, 4369 .type = ARM_CP_NOP, .access = PL1_W }, 4370 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 4371 .type = ARM_CP_NOP, .access = PL1_W }, 4372 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, 4373 .type = ARM_CP_NOP, .access = PL1_W }, 4374 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, 4375 .type = ARM_CP_NOP, .access = PL1_W }, 4376 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 4377 .type = ARM_CP_NOP, .access = PL1_W }, 4378 /* MMU Domain access control / MPU write buffer control */ 4379 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, 4380 .access = PL1_RW, .resetvalue = 0, 4381 .writefn = dacr_write, .raw_writefn = raw_write, 4382 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 4383 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 4384 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, 4385 .type = ARM_CP_ALIAS, 4386 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, 4387 .access = PL1_RW, 4388 .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, 4389 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, 4390 .type = ARM_CP_ALIAS, 4391 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, 4392 .access = PL1_RW, 4393 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) }, 4394 /* We rely on the access checks not allowing the guest to write to the 4395 * state field when SPSel indicates that it's being used as the stack 4396 * pointer. 4397 */ 4398 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, 4399 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, 4400 .access = PL1_RW, .accessfn = sp_el0_access, 4401 .type = ARM_CP_ALIAS, 4402 .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, 4403 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64, 4404 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0, 4405 .access = PL2_RW, .type = ARM_CP_ALIAS, 4406 .fieldoffset = offsetof(CPUARMState, sp_el[1]) }, 4407 { .name = "SPSel", .state = ARM_CP_STATE_AA64, 4408 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, 4409 .type = ARM_CP_NO_RAW, 4410 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, 4411 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64, 4412 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0, 4413 .type = ARM_CP_ALIAS, 4414 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]), 4415 .access = PL2_RW, .accessfn = fpexc32_access }, 4416 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, 4417 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, 4418 .access = PL2_RW, .resetvalue = 0, 4419 .writefn = dacr_write, .raw_writefn = raw_write, 4420 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, 4421 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, 4422 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, 4423 .access = PL2_RW, .resetvalue = 0, 4424 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, 4425 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64, 4426 .type = ARM_CP_ALIAS, 4427 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0, 4428 .access = PL2_RW, 4429 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) }, 4430 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64, 4431 .type = ARM_CP_ALIAS, 4432 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1, 4433 .access = PL2_RW, 4434 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) }, 4435 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64, 4436 .type = ARM_CP_ALIAS, 4437 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2, 4438 .access = PL2_RW, 4439 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) }, 4440 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64, 4441 .type = ARM_CP_ALIAS, 4442 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3, 4443 .access = PL2_RW, 4444 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) }, 4445 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64, 4446 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1, 4447 .resetvalue = 0, 4448 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) }, 4449 { .name = "SDCR", .type = ARM_CP_ALIAS, 4450 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1, 4451 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 4452 .writefn = sdcr_write, 4453 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) }, 4454 REGINFO_SENTINEL 4455 }; 4456 4457 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */ 4458 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = { 4459 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 4460 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 4461 .access = PL2_RW, 4462 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, 4463 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH, 4464 .type = ARM_CP_NO_RAW, 4465 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 4466 .access = PL2_RW, 4467 .type = ARM_CP_CONST, .resetvalue = 0 }, 4468 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH, 4469 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7, 4470 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4471 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 4472 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 4473 .access = PL2_RW, 4474 .type = ARM_CP_CONST, .resetvalue = 0 }, 4475 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 4476 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 4477 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4478 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 4479 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 4480 .access = PL2_RW, .type = ARM_CP_CONST, 4481 .resetvalue = 0 }, 4482 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 4483 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 4484 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4485 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 4486 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 4487 .access = PL2_RW, .type = ARM_CP_CONST, 4488 .resetvalue = 0 }, 4489 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 4490 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 4491 .access = PL2_RW, .type = ARM_CP_CONST, 4492 .resetvalue = 0 }, 4493 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 4494 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 4495 .access = PL2_RW, .type = ARM_CP_CONST, 4496 .resetvalue = 0 }, 4497 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 4498 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 4499 .access = PL2_RW, .type = ARM_CP_CONST, 4500 .resetvalue = 0 }, 4501 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 4502 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 4503 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4504 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH, 4505 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 4506 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 4507 .type = ARM_CP_CONST, .resetvalue = 0 }, 4508 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 4509 .cp = 15, .opc1 = 6, .crm = 2, 4510 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4511 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 4512 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 4513 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 4514 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4515 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 4516 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 4517 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4518 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 4519 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 4520 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4521 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 4522 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 4523 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4524 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 4525 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 4526 .resetvalue = 0 }, 4527 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 4528 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 4529 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4530 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 4531 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 4532 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4533 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 4534 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 4535 .resetvalue = 0 }, 4536 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 4537 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 4538 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4539 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 4540 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 4541 .resetvalue = 0 }, 4542 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 4543 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 4544 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4545 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 4546 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 4547 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4548 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 4549 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 4550 .access = PL2_RW, .accessfn = access_tda, 4551 .type = ARM_CP_CONST, .resetvalue = 0 }, 4552 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH, 4553 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4554 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 4555 .type = ARM_CP_CONST, .resetvalue = 0 }, 4556 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 4557 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 4558 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4559 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 4560 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 4561 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4562 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 4563 .type = ARM_CP_CONST, 4564 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 4565 .access = PL2_RW, .resetvalue = 0 }, 4566 REGINFO_SENTINEL 4567 }; 4568 4569 /* Ditto, but for registers which exist in ARMv8 but not v7 */ 4570 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = { 4571 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 4572 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 4573 .access = PL2_RW, 4574 .type = ARM_CP_CONST, .resetvalue = 0 }, 4575 REGINFO_SENTINEL 4576 }; 4577 4578 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 4579 { 4580 ARMCPU *cpu = env_archcpu(env); 4581 uint64_t valid_mask = HCR_MASK; 4582 4583 if (arm_feature(env, ARM_FEATURE_EL3)) { 4584 valid_mask &= ~HCR_HCD; 4585 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) { 4586 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented. 4587 * However, if we're using the SMC PSCI conduit then QEMU is 4588 * effectively acting like EL3 firmware and so the guest at 4589 * EL2 should retain the ability to prevent EL1 from being 4590 * able to make SMC calls into the ersatz firmware, so in 4591 * that case HCR.TSC should be read/write. 4592 */ 4593 valid_mask &= ~HCR_TSC; 4594 } 4595 if (cpu_isar_feature(aa64_lor, cpu)) { 4596 valid_mask |= HCR_TLOR; 4597 } 4598 if (cpu_isar_feature(aa64_pauth, cpu)) { 4599 valid_mask |= HCR_API | HCR_APK; 4600 } 4601 4602 /* Clear RES0 bits. */ 4603 value &= valid_mask; 4604 4605 /* These bits change the MMU setup: 4606 * HCR_VM enables stage 2 translation 4607 * HCR_PTW forbids certain page-table setups 4608 * HCR_DC Disables stage1 and enables stage2 translation 4609 */ 4610 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC)) { 4611 tlb_flush(CPU(cpu)); 4612 } 4613 env->cp15.hcr_el2 = value; 4614 4615 /* 4616 * Updates to VI and VF require us to update the status of 4617 * virtual interrupts, which are the logical OR of these bits 4618 * and the state of the input lines from the GIC. (This requires 4619 * that we have the iothread lock, which is done by marking the 4620 * reginfo structs as ARM_CP_IO.) 4621 * Note that if a write to HCR pends a VIRQ or VFIQ it is never 4622 * possible for it to be taken immediately, because VIRQ and 4623 * VFIQ are masked unless running at EL0 or EL1, and HCR 4624 * can only be written at EL2. 4625 */ 4626 g_assert(qemu_mutex_iothread_locked()); 4627 arm_cpu_update_virq(cpu); 4628 arm_cpu_update_vfiq(cpu); 4629 } 4630 4631 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri, 4632 uint64_t value) 4633 { 4634 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */ 4635 value = deposit64(env->cp15.hcr_el2, 32, 32, value); 4636 hcr_write(env, NULL, value); 4637 } 4638 4639 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri, 4640 uint64_t value) 4641 { 4642 /* Handle HCR write, i.e. write to low half of HCR_EL2 */ 4643 value = deposit64(env->cp15.hcr_el2, 0, 32, value); 4644 hcr_write(env, NULL, value); 4645 } 4646 4647 /* 4648 * Return the effective value of HCR_EL2. 4649 * Bits that are not included here: 4650 * RW (read from SCR_EL3.RW as needed) 4651 */ 4652 uint64_t arm_hcr_el2_eff(CPUARMState *env) 4653 { 4654 uint64_t ret = env->cp15.hcr_el2; 4655 4656 if (arm_is_secure_below_el3(env)) { 4657 /* 4658 * "This register has no effect if EL2 is not enabled in the 4659 * current Security state". This is ARMv8.4-SecEL2 speak for 4660 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1). 4661 * 4662 * Prior to that, the language was "In an implementation that 4663 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves 4664 * as if this field is 0 for all purposes other than a direct 4665 * read or write access of HCR_EL2". With lots of enumeration 4666 * on a per-field basis. In current QEMU, this is condition 4667 * is arm_is_secure_below_el3. 4668 * 4669 * Since the v8.4 language applies to the entire register, and 4670 * appears to be backward compatible, use that. 4671 */ 4672 ret = 0; 4673 } else if (ret & HCR_TGE) { 4674 /* These bits are up-to-date as of ARMv8.4. */ 4675 if (ret & HCR_E2H) { 4676 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO | 4677 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE | 4678 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU | 4679 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE); 4680 } else { 4681 ret |= HCR_FMO | HCR_IMO | HCR_AMO; 4682 } 4683 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE | 4684 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR | 4685 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM | 4686 HCR_TLOR); 4687 } 4688 4689 return ret; 4690 } 4691 4692 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 4693 uint64_t value) 4694 { 4695 /* 4696 * For A-profile AArch32 EL3, if NSACR.CP10 4697 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1. 4698 */ 4699 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 4700 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 4701 value &= ~(0x3 << 10); 4702 value |= env->cp15.cptr_el[2] & (0x3 << 10); 4703 } 4704 env->cp15.cptr_el[2] = value; 4705 } 4706 4707 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri) 4708 { 4709 /* 4710 * For A-profile AArch32 EL3, if NSACR.CP10 4711 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1. 4712 */ 4713 uint64_t value = env->cp15.cptr_el[2]; 4714 4715 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 4716 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 4717 value |= 0x3 << 10; 4718 } 4719 return value; 4720 } 4721 4722 static const ARMCPRegInfo el2_cp_reginfo[] = { 4723 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, 4724 .type = ARM_CP_IO, 4725 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 4726 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 4727 .writefn = hcr_write }, 4728 { .name = "HCR", .state = ARM_CP_STATE_AA32, 4729 .type = ARM_CP_ALIAS | ARM_CP_IO, 4730 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 4731 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 4732 .writefn = hcr_writelow }, 4733 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH, 4734 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7, 4735 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 4736 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, 4737 .type = ARM_CP_ALIAS, 4738 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, 4739 .access = PL2_RW, 4740 .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, 4741 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 4742 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 4743 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, 4744 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 4745 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 4746 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, 4747 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 4748 .type = ARM_CP_ALIAS, 4749 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 4750 .access = PL2_RW, 4751 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) }, 4752 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, 4753 .type = ARM_CP_ALIAS, 4754 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, 4755 .access = PL2_RW, 4756 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) }, 4757 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 4758 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 4759 .access = PL2_RW, .writefn = vbar_write, 4760 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), 4761 .resetvalue = 0 }, 4762 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64, 4763 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0, 4764 .access = PL3_RW, .type = ARM_CP_ALIAS, 4765 .fieldoffset = offsetof(CPUARMState, sp_el[2]) }, 4766 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 4767 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 4768 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0, 4769 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]), 4770 .readfn = cptr_el2_read, .writefn = cptr_el2_write }, 4771 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 4772 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 4773 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]), 4774 .resetvalue = 0 }, 4775 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 4776 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 4777 .access = PL2_RW, .type = ARM_CP_ALIAS, 4778 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) }, 4779 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 4780 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 4781 .access = PL2_RW, .type = ARM_CP_CONST, 4782 .resetvalue = 0 }, 4783 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */ 4784 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 4785 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 4786 .access = PL2_RW, .type = ARM_CP_CONST, 4787 .resetvalue = 0 }, 4788 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 4789 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 4790 .access = PL2_RW, .type = ARM_CP_CONST, 4791 .resetvalue = 0 }, 4792 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 4793 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 4794 .access = PL2_RW, .type = ARM_CP_CONST, 4795 .resetvalue = 0 }, 4796 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 4797 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 4798 .access = PL2_RW, 4799 /* no .writefn needed as this can't cause an ASID change; 4800 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 4801 */ 4802 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) }, 4803 { .name = "VTCR", .state = ARM_CP_STATE_AA32, 4804 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 4805 .type = ARM_CP_ALIAS, 4806 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4807 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 4808 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64, 4809 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 4810 .access = PL2_RW, 4811 /* no .writefn needed as this can't cause an ASID change; 4812 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 4813 */ 4814 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 4815 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 4816 .cp = 15, .opc1 = 6, .crm = 2, 4817 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4818 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4819 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2), 4820 .writefn = vttbr_write }, 4821 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 4822 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 4823 .access = PL2_RW, .writefn = vttbr_write, 4824 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) }, 4825 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 4826 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 4827 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write, 4828 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) }, 4829 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 4830 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 4831 .access = PL2_RW, .resetvalue = 0, 4832 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) }, 4833 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 4834 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 4835 .access = PL2_RW, .resetvalue = 0, 4836 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 4837 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 4838 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4839 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 4840 { .name = "TLBIALLNSNH", 4841 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 4842 .type = ARM_CP_NO_RAW, .access = PL2_W, 4843 .writefn = tlbiall_nsnh_write }, 4844 { .name = "TLBIALLNSNHIS", 4845 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 4846 .type = ARM_CP_NO_RAW, .access = PL2_W, 4847 .writefn = tlbiall_nsnh_is_write }, 4848 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 4849 .type = ARM_CP_NO_RAW, .access = PL2_W, 4850 .writefn = tlbiall_hyp_write }, 4851 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 4852 .type = ARM_CP_NO_RAW, .access = PL2_W, 4853 .writefn = tlbiall_hyp_is_write }, 4854 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 4855 .type = ARM_CP_NO_RAW, .access = PL2_W, 4856 .writefn = tlbimva_hyp_write }, 4857 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 4858 .type = ARM_CP_NO_RAW, .access = PL2_W, 4859 .writefn = tlbimva_hyp_is_write }, 4860 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64, 4861 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 4862 .type = ARM_CP_NO_RAW, .access = PL2_W, 4863 .writefn = tlbi_aa64_alle2_write }, 4864 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64, 4865 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 4866 .type = ARM_CP_NO_RAW, .access = PL2_W, 4867 .writefn = tlbi_aa64_vae2_write }, 4868 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64, 4869 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 4870 .access = PL2_W, .type = ARM_CP_NO_RAW, 4871 .writefn = tlbi_aa64_vae2_write }, 4872 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64, 4873 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 4874 .access = PL2_W, .type = ARM_CP_NO_RAW, 4875 .writefn = tlbi_aa64_alle2is_write }, 4876 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64, 4877 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 4878 .type = ARM_CP_NO_RAW, .access = PL2_W, 4879 .writefn = tlbi_aa64_vae2is_write }, 4880 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64, 4881 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 4882 .access = PL2_W, .type = ARM_CP_NO_RAW, 4883 .writefn = tlbi_aa64_vae2is_write }, 4884 #ifndef CONFIG_USER_ONLY 4885 /* Unlike the other EL2-related AT operations, these must 4886 * UNDEF from EL3 if EL2 is not implemented, which is why we 4887 * define them here rather than with the rest of the AT ops. 4888 */ 4889 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64, 4890 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 4891 .access = PL2_W, .accessfn = at_s1e2_access, 4892 .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4893 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64, 4894 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 4895 .access = PL2_W, .accessfn = at_s1e2_access, 4896 .type = ARM_CP_NO_RAW, .writefn = ats_write64 }, 4897 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE 4898 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3 4899 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose 4900 * to behave as if SCR.NS was 1. 4901 */ 4902 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 4903 .access = PL2_W, 4904 .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, 4905 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 4906 .access = PL2_W, 4907 .writefn = ats1h_write, .type = ARM_CP_NO_RAW }, 4908 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 4909 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 4910 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the 4911 * reset values as IMPDEF. We choose to reset to 3 to comply with 4912 * both ARMv7 and ARMv8. 4913 */ 4914 .access = PL2_RW, .resetvalue = 3, 4915 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) }, 4916 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 4917 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 4918 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, 4919 .writefn = gt_cntvoff_write, 4920 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 4921 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 4922 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO, 4923 .writefn = gt_cntvoff_write, 4924 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 4925 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 4926 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 4927 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 4928 .type = ARM_CP_IO, .access = PL2_RW, 4929 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 4930 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 4931 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 4932 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO, 4933 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 4934 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 4935 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 4936 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 4937 .resetfn = gt_hyp_timer_reset, 4938 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write }, 4939 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 4940 .type = ARM_CP_IO, 4941 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 4942 .access = PL2_RW, 4943 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl), 4944 .resetvalue = 0, 4945 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write }, 4946 #endif 4947 /* The only field of MDCR_EL2 that has a defined architectural reset value 4948 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we 4949 * don't implement any PMU event counters, so using zero as a reset 4950 * value for MDCR_EL2 is okay 4951 */ 4952 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 4953 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 4954 .access = PL2_RW, .resetvalue = 0, 4955 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), }, 4956 { .name = "HPFAR", .state = ARM_CP_STATE_AA32, 4957 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4958 .access = PL2_RW, .accessfn = access_el3_aa32ns, 4959 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 4960 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64, 4961 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 4962 .access = PL2_RW, 4963 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 4964 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 4965 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 4966 .access = PL2_RW, 4967 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) }, 4968 REGINFO_SENTINEL 4969 }; 4970 4971 static const ARMCPRegInfo el2_v8_cp_reginfo[] = { 4972 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 4973 .type = ARM_CP_ALIAS | ARM_CP_IO, 4974 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 4975 .access = PL2_RW, 4976 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2), 4977 .writefn = hcr_writehigh }, 4978 REGINFO_SENTINEL 4979 }; 4980 4981 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 4982 bool isread) 4983 { 4984 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2. 4985 * At Secure EL1 it traps to EL3. 4986 */ 4987 if (arm_current_el(env) == 3) { 4988 return CP_ACCESS_OK; 4989 } 4990 if (arm_is_secure_below_el3(env)) { 4991 return CP_ACCESS_TRAP_EL3; 4992 } 4993 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */ 4994 if (isread) { 4995 return CP_ACCESS_OK; 4996 } 4997 return CP_ACCESS_TRAP_UNCATEGORIZED; 4998 } 4999 5000 static const ARMCPRegInfo el3_cp_reginfo[] = { 5001 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, 5002 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, 5003 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), 5004 .resetvalue = 0, .writefn = scr_write }, 5005 { .name = "SCR", .type = ARM_CP_ALIAS, 5006 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, 5007 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5008 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), 5009 .writefn = scr_write }, 5010 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, 5011 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, 5012 .access = PL3_RW, .resetvalue = 0, 5013 .fieldoffset = offsetof(CPUARMState, cp15.sder) }, 5014 { .name = "SDER", 5015 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, 5016 .access = PL3_RW, .resetvalue = 0, 5017 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, 5018 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 5019 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5020 .writefn = vbar_write, .resetvalue = 0, 5021 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, 5022 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, 5023 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, 5024 .access = PL3_RW, .resetvalue = 0, 5025 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, 5026 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, 5027 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, 5028 .access = PL3_RW, 5029 /* no .writefn needed as this can't cause an ASID change; 5030 * we must provide a .raw_writefn and .resetfn because we handle 5031 * reset and migration for the AArch32 TTBCR(S), which might be 5032 * using mask and base_mask. 5033 */ 5034 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write, 5035 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, 5036 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, 5037 .type = ARM_CP_ALIAS, 5038 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, 5039 .access = PL3_RW, 5040 .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, 5041 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, 5042 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, 5043 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, 5044 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, 5045 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, 5046 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, 5047 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, 5048 .type = ARM_CP_ALIAS, 5049 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, 5050 .access = PL3_RW, 5051 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) }, 5052 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, 5053 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, 5054 .access = PL3_RW, .writefn = vbar_write, 5055 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), 5056 .resetvalue = 0 }, 5057 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64, 5058 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2, 5059 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0, 5060 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) }, 5061 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64, 5062 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2, 5063 .access = PL3_RW, .resetvalue = 0, 5064 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) }, 5065 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64, 5066 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0, 5067 .access = PL3_RW, .type = ARM_CP_CONST, 5068 .resetvalue = 0 }, 5069 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH, 5070 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0, 5071 .access = PL3_RW, .type = ARM_CP_CONST, 5072 .resetvalue = 0 }, 5073 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH, 5074 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1, 5075 .access = PL3_RW, .type = ARM_CP_CONST, 5076 .resetvalue = 0 }, 5077 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64, 5078 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0, 5079 .access = PL3_W, .type = ARM_CP_NO_RAW, 5080 .writefn = tlbi_aa64_alle3is_write }, 5081 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64, 5082 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1, 5083 .access = PL3_W, .type = ARM_CP_NO_RAW, 5084 .writefn = tlbi_aa64_vae3is_write }, 5085 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64, 5086 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5, 5087 .access = PL3_W, .type = ARM_CP_NO_RAW, 5088 .writefn = tlbi_aa64_vae3is_write }, 5089 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64, 5090 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0, 5091 .access = PL3_W, .type = ARM_CP_NO_RAW, 5092 .writefn = tlbi_aa64_alle3_write }, 5093 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64, 5094 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1, 5095 .access = PL3_W, .type = ARM_CP_NO_RAW, 5096 .writefn = tlbi_aa64_vae3_write }, 5097 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64, 5098 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5, 5099 .access = PL3_W, .type = ARM_CP_NO_RAW, 5100 .writefn = tlbi_aa64_vae3_write }, 5101 REGINFO_SENTINEL 5102 }; 5103 5104 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 5105 bool isread) 5106 { 5107 /* Only accessible in EL0 if SCTLR.UCT is set (and only in AArch64, 5108 * but the AArch32 CTR has its own reginfo struct) 5109 */ 5110 if (arm_current_el(env) == 0 && !(env->cp15.sctlr_el[1] & SCTLR_UCT)) { 5111 return CP_ACCESS_TRAP; 5112 } 5113 return CP_ACCESS_OK; 5114 } 5115 5116 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri, 5117 uint64_t value) 5118 { 5119 /* Writes to OSLAR_EL1 may update the OS lock status, which can be 5120 * read via a bit in OSLSR_EL1. 5121 */ 5122 int oslock; 5123 5124 if (ri->state == ARM_CP_STATE_AA32) { 5125 oslock = (value == 0xC5ACCE55); 5126 } else { 5127 oslock = value & 1; 5128 } 5129 5130 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock); 5131 } 5132 5133 static const ARMCPRegInfo debug_cp_reginfo[] = { 5134 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped 5135 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1; 5136 * unlike DBGDRAR it is never accessible from EL0. 5137 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64 5138 * accessor. 5139 */ 5140 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, 5141 .access = PL0_R, .accessfn = access_tdra, 5142 .type = ARM_CP_CONST, .resetvalue = 0 }, 5143 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64, 5144 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 5145 .access = PL1_R, .accessfn = access_tdra, 5146 .type = ARM_CP_CONST, .resetvalue = 0 }, 5147 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 5148 .access = PL0_R, .accessfn = access_tdra, 5149 .type = ARM_CP_CONST, .resetvalue = 0 }, 5150 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */ 5151 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH, 5152 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 5153 .access = PL1_RW, .accessfn = access_tda, 5154 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), 5155 .resetvalue = 0 }, 5156 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1. 5157 * We don't implement the configurable EL0 access. 5158 */ 5159 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH, 5160 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 5161 .type = ARM_CP_ALIAS, 5162 .access = PL1_R, .accessfn = access_tda, 5163 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), }, 5164 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH, 5165 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4, 5166 .access = PL1_W, .type = ARM_CP_NO_RAW, 5167 .accessfn = access_tdosa, 5168 .writefn = oslar_write }, 5169 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH, 5170 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4, 5171 .access = PL1_R, .resetvalue = 10, 5172 .accessfn = access_tdosa, 5173 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) }, 5174 /* Dummy OSDLR_EL1: 32-bit Linux will read this */ 5175 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH, 5176 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4, 5177 .access = PL1_RW, .accessfn = access_tdosa, 5178 .type = ARM_CP_NOP }, 5179 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't 5180 * implement vector catch debug events yet. 5181 */ 5182 { .name = "DBGVCR", 5183 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 5184 .access = PL1_RW, .accessfn = access_tda, 5185 .type = ARM_CP_NOP }, 5186 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor 5187 * to save and restore a 32-bit guest's DBGVCR) 5188 */ 5189 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64, 5190 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0, 5191 .access = PL2_RW, .accessfn = access_tda, 5192 .type = ARM_CP_NOP }, 5193 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications 5194 * Channel but Linux may try to access this register. The 32-bit 5195 * alias is DBGDCCINT. 5196 */ 5197 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH, 5198 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 5199 .access = PL1_RW, .accessfn = access_tda, 5200 .type = ARM_CP_NOP }, 5201 REGINFO_SENTINEL 5202 }; 5203 5204 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = { 5205 /* 64 bit access versions of the (dummy) debug registers */ 5206 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0, 5207 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 5208 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0, 5209 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 5210 REGINFO_SENTINEL 5211 }; 5212 5213 /* Return the exception level to which exceptions should be taken 5214 * via SVEAccessTrap. If an exception should be routed through 5215 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should 5216 * take care of raising that exception. 5217 * C.f. the ARM pseudocode function CheckSVEEnabled. 5218 */ 5219 int sve_exception_el(CPUARMState *env, int el) 5220 { 5221 #ifndef CONFIG_USER_ONLY 5222 if (el <= 1) { 5223 bool disabled = false; 5224 5225 /* The CPACR.ZEN controls traps to EL1: 5226 * 0, 2 : trap EL0 and EL1 accesses 5227 * 1 : trap only EL0 accesses 5228 * 3 : trap no accesses 5229 */ 5230 if (!extract32(env->cp15.cpacr_el1, 16, 1)) { 5231 disabled = true; 5232 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) { 5233 disabled = el == 0; 5234 } 5235 if (disabled) { 5236 /* route_to_el2 */ 5237 return (arm_feature(env, ARM_FEATURE_EL2) 5238 && (arm_hcr_el2_eff(env) & HCR_TGE) ? 2 : 1); 5239 } 5240 5241 /* Check CPACR.FPEN. */ 5242 if (!extract32(env->cp15.cpacr_el1, 20, 1)) { 5243 disabled = true; 5244 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) { 5245 disabled = el == 0; 5246 } 5247 if (disabled) { 5248 return 0; 5249 } 5250 } 5251 5252 /* CPTR_EL2. Since TZ and TFP are positive, 5253 * they will be zero when EL2 is not present. 5254 */ 5255 if (el <= 2 && !arm_is_secure_below_el3(env)) { 5256 if (env->cp15.cptr_el[2] & CPTR_TZ) { 5257 return 2; 5258 } 5259 if (env->cp15.cptr_el[2] & CPTR_TFP) { 5260 return 0; 5261 } 5262 } 5263 5264 /* CPTR_EL3. Since EZ is negative we must check for EL3. */ 5265 if (arm_feature(env, ARM_FEATURE_EL3) 5266 && !(env->cp15.cptr_el[3] & CPTR_EZ)) { 5267 return 3; 5268 } 5269 #endif 5270 return 0; 5271 } 5272 5273 /* 5274 * Given that SVE is enabled, return the vector length for EL. 5275 */ 5276 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el) 5277 { 5278 ARMCPU *cpu = env_archcpu(env); 5279 uint32_t zcr_len = cpu->sve_max_vq - 1; 5280 5281 if (el <= 1) { 5282 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]); 5283 } 5284 if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) { 5285 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]); 5286 } 5287 if (el < 3 && arm_feature(env, ARM_FEATURE_EL3)) { 5288 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]); 5289 } 5290 return zcr_len; 5291 } 5292 5293 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5294 uint64_t value) 5295 { 5296 int cur_el = arm_current_el(env); 5297 int old_len = sve_zcr_len_for_el(env, cur_el); 5298 int new_len; 5299 5300 /* Bits other than [3:0] are RAZ/WI. */ 5301 raw_write(env, ri, value & 0xf); 5302 5303 /* 5304 * Because we arrived here, we know both FP and SVE are enabled; 5305 * otherwise we would have trapped access to the ZCR_ELn register. 5306 */ 5307 new_len = sve_zcr_len_for_el(env, cur_el); 5308 if (new_len < old_len) { 5309 aarch64_sve_narrow_vq(env, new_len + 1); 5310 } 5311 } 5312 5313 static const ARMCPRegInfo zcr_el1_reginfo = { 5314 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64, 5315 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0, 5316 .access = PL1_RW, .type = ARM_CP_SVE, 5317 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]), 5318 .writefn = zcr_write, .raw_writefn = raw_write 5319 }; 5320 5321 static const ARMCPRegInfo zcr_el2_reginfo = { 5322 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 5323 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 5324 .access = PL2_RW, .type = ARM_CP_SVE, 5325 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]), 5326 .writefn = zcr_write, .raw_writefn = raw_write 5327 }; 5328 5329 static const ARMCPRegInfo zcr_no_el2_reginfo = { 5330 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 5331 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 5332 .access = PL2_RW, .type = ARM_CP_SVE, 5333 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore 5334 }; 5335 5336 static const ARMCPRegInfo zcr_el3_reginfo = { 5337 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64, 5338 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0, 5339 .access = PL3_RW, .type = ARM_CP_SVE, 5340 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]), 5341 .writefn = zcr_write, .raw_writefn = raw_write 5342 }; 5343 5344 void hw_watchpoint_update(ARMCPU *cpu, int n) 5345 { 5346 CPUARMState *env = &cpu->env; 5347 vaddr len = 0; 5348 vaddr wvr = env->cp15.dbgwvr[n]; 5349 uint64_t wcr = env->cp15.dbgwcr[n]; 5350 int mask; 5351 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 5352 5353 if (env->cpu_watchpoint[n]) { 5354 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]); 5355 env->cpu_watchpoint[n] = NULL; 5356 } 5357 5358 if (!extract64(wcr, 0, 1)) { 5359 /* E bit clear : watchpoint disabled */ 5360 return; 5361 } 5362 5363 switch (extract64(wcr, 3, 2)) { 5364 case 0: 5365 /* LSC 00 is reserved and must behave as if the wp is disabled */ 5366 return; 5367 case 1: 5368 flags |= BP_MEM_READ; 5369 break; 5370 case 2: 5371 flags |= BP_MEM_WRITE; 5372 break; 5373 case 3: 5374 flags |= BP_MEM_ACCESS; 5375 break; 5376 } 5377 5378 /* Attempts to use both MASK and BAS fields simultaneously are 5379 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case, 5380 * thus generating a watchpoint for every byte in the masked region. 5381 */ 5382 mask = extract64(wcr, 24, 4); 5383 if (mask == 1 || mask == 2) { 5384 /* Reserved values of MASK; we must act as if the mask value was 5385 * some non-reserved value, or as if the watchpoint were disabled. 5386 * We choose the latter. 5387 */ 5388 return; 5389 } else if (mask) { 5390 /* Watchpoint covers an aligned area up to 2GB in size */ 5391 len = 1ULL << mask; 5392 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE 5393 * whether the watchpoint fires when the unmasked bits match; we opt 5394 * to generate the exceptions. 5395 */ 5396 wvr &= ~(len - 1); 5397 } else { 5398 /* Watchpoint covers bytes defined by the byte address select bits */ 5399 int bas = extract64(wcr, 5, 8); 5400 int basstart; 5401 5402 if (bas == 0) { 5403 /* This must act as if the watchpoint is disabled */ 5404 return; 5405 } 5406 5407 if (extract64(wvr, 2, 1)) { 5408 /* Deprecated case of an only 4-aligned address. BAS[7:4] are 5409 * ignored, and BAS[3:0] define which bytes to watch. 5410 */ 5411 bas &= 0xf; 5412 } 5413 /* The BAS bits are supposed to be programmed to indicate a contiguous 5414 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether 5415 * we fire for each byte in the word/doubleword addressed by the WVR. 5416 * We choose to ignore any non-zero bits after the first range of 1s. 5417 */ 5418 basstart = ctz32(bas); 5419 len = cto32(bas >> basstart); 5420 wvr += basstart; 5421 } 5422 5423 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags, 5424 &env->cpu_watchpoint[n]); 5425 } 5426 5427 void hw_watchpoint_update_all(ARMCPU *cpu) 5428 { 5429 int i; 5430 CPUARMState *env = &cpu->env; 5431 5432 /* Completely clear out existing QEMU watchpoints and our array, to 5433 * avoid possible stale entries following migration load. 5434 */ 5435 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU); 5436 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint)); 5437 5438 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) { 5439 hw_watchpoint_update(cpu, i); 5440 } 5441 } 5442 5443 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5444 uint64_t value) 5445 { 5446 ARMCPU *cpu = env_archcpu(env); 5447 int i = ri->crm; 5448 5449 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the 5450 * register reads and behaves as if values written are sign extended. 5451 * Bits [1:0] are RES0. 5452 */ 5453 value = sextract64(value, 0, 49) & ~3ULL; 5454 5455 raw_write(env, ri, value); 5456 hw_watchpoint_update(cpu, i); 5457 } 5458 5459 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5460 uint64_t value) 5461 { 5462 ARMCPU *cpu = env_archcpu(env); 5463 int i = ri->crm; 5464 5465 raw_write(env, ri, value); 5466 hw_watchpoint_update(cpu, i); 5467 } 5468 5469 void hw_breakpoint_update(ARMCPU *cpu, int n) 5470 { 5471 CPUARMState *env = &cpu->env; 5472 uint64_t bvr = env->cp15.dbgbvr[n]; 5473 uint64_t bcr = env->cp15.dbgbcr[n]; 5474 vaddr addr; 5475 int bt; 5476 int flags = BP_CPU; 5477 5478 if (env->cpu_breakpoint[n]) { 5479 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]); 5480 env->cpu_breakpoint[n] = NULL; 5481 } 5482 5483 if (!extract64(bcr, 0, 1)) { 5484 /* E bit clear : watchpoint disabled */ 5485 return; 5486 } 5487 5488 bt = extract64(bcr, 20, 4); 5489 5490 switch (bt) { 5491 case 4: /* unlinked address mismatch (reserved if AArch64) */ 5492 case 5: /* linked address mismatch (reserved if AArch64) */ 5493 qemu_log_mask(LOG_UNIMP, 5494 "arm: address mismatch breakpoint types not implemented\n"); 5495 return; 5496 case 0: /* unlinked address match */ 5497 case 1: /* linked address match */ 5498 { 5499 /* Bits [63:49] are hardwired to the value of bit [48]; that is, 5500 * we behave as if the register was sign extended. Bits [1:0] are 5501 * RES0. The BAS field is used to allow setting breakpoints on 16 5502 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether 5503 * a bp will fire if the addresses covered by the bp and the addresses 5504 * covered by the insn overlap but the insn doesn't start at the 5505 * start of the bp address range. We choose to require the insn and 5506 * the bp to have the same address. The constraints on writing to 5507 * BAS enforced in dbgbcr_write mean we have only four cases: 5508 * 0b0000 => no breakpoint 5509 * 0b0011 => breakpoint on addr 5510 * 0b1100 => breakpoint on addr + 2 5511 * 0b1111 => breakpoint on addr 5512 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c). 5513 */ 5514 int bas = extract64(bcr, 5, 4); 5515 addr = sextract64(bvr, 0, 49) & ~3ULL; 5516 if (bas == 0) { 5517 return; 5518 } 5519 if (bas == 0xc) { 5520 addr += 2; 5521 } 5522 break; 5523 } 5524 case 2: /* unlinked context ID match */ 5525 case 8: /* unlinked VMID match (reserved if no EL2) */ 5526 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */ 5527 qemu_log_mask(LOG_UNIMP, 5528 "arm: unlinked context breakpoint types not implemented\n"); 5529 return; 5530 case 9: /* linked VMID match (reserved if no EL2) */ 5531 case 11: /* linked context ID and VMID match (reserved if no EL2) */ 5532 case 3: /* linked context ID match */ 5533 default: 5534 /* We must generate no events for Linked context matches (unless 5535 * they are linked to by some other bp/wp, which is handled in 5536 * updates for the linking bp/wp). We choose to also generate no events 5537 * for reserved values. 5538 */ 5539 return; 5540 } 5541 5542 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]); 5543 } 5544 5545 void hw_breakpoint_update_all(ARMCPU *cpu) 5546 { 5547 int i; 5548 CPUARMState *env = &cpu->env; 5549 5550 /* Completely clear out existing QEMU breakpoints and our array, to 5551 * avoid possible stale entries following migration load. 5552 */ 5553 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU); 5554 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint)); 5555 5556 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) { 5557 hw_breakpoint_update(cpu, i); 5558 } 5559 } 5560 5561 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5562 uint64_t value) 5563 { 5564 ARMCPU *cpu = env_archcpu(env); 5565 int i = ri->crm; 5566 5567 raw_write(env, ri, value); 5568 hw_breakpoint_update(cpu, i); 5569 } 5570 5571 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 5572 uint64_t value) 5573 { 5574 ARMCPU *cpu = env_archcpu(env); 5575 int i = ri->crm; 5576 5577 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only 5578 * copy of BAS[0]. 5579 */ 5580 value = deposit64(value, 6, 1, extract64(value, 5, 1)); 5581 value = deposit64(value, 8, 1, extract64(value, 7, 1)); 5582 5583 raw_write(env, ri, value); 5584 hw_breakpoint_update(cpu, i); 5585 } 5586 5587 static void define_debug_regs(ARMCPU *cpu) 5588 { 5589 /* Define v7 and v8 architectural debug registers. 5590 * These are just dummy implementations for now. 5591 */ 5592 int i; 5593 int wrps, brps, ctx_cmps; 5594 ARMCPRegInfo dbgdidr = { 5595 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 5596 .access = PL0_R, .accessfn = access_tda, 5597 .type = ARM_CP_CONST, .resetvalue = cpu->dbgdidr, 5598 }; 5599 5600 /* Note that all these register fields hold "number of Xs minus 1". */ 5601 brps = extract32(cpu->dbgdidr, 24, 4); 5602 wrps = extract32(cpu->dbgdidr, 28, 4); 5603 ctx_cmps = extract32(cpu->dbgdidr, 20, 4); 5604 5605 assert(ctx_cmps <= brps); 5606 5607 /* The DBGDIDR and ID_AA64DFR0_EL1 define various properties 5608 * of the debug registers such as number of breakpoints; 5609 * check that if they both exist then they agree. 5610 */ 5611 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64)) { 5612 assert(extract32(cpu->id_aa64dfr0, 12, 4) == brps); 5613 assert(extract32(cpu->id_aa64dfr0, 20, 4) == wrps); 5614 assert(extract32(cpu->id_aa64dfr0, 28, 4) == ctx_cmps); 5615 } 5616 5617 define_one_arm_cp_reg(cpu, &dbgdidr); 5618 define_arm_cp_regs(cpu, debug_cp_reginfo); 5619 5620 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) { 5621 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo); 5622 } 5623 5624 for (i = 0; i < brps + 1; i++) { 5625 ARMCPRegInfo dbgregs[] = { 5626 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH, 5627 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4, 5628 .access = PL1_RW, .accessfn = access_tda, 5629 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]), 5630 .writefn = dbgbvr_write, .raw_writefn = raw_write 5631 }, 5632 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH, 5633 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5, 5634 .access = PL1_RW, .accessfn = access_tda, 5635 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]), 5636 .writefn = dbgbcr_write, .raw_writefn = raw_write 5637 }, 5638 REGINFO_SENTINEL 5639 }; 5640 define_arm_cp_regs(cpu, dbgregs); 5641 } 5642 5643 for (i = 0; i < wrps + 1; i++) { 5644 ARMCPRegInfo dbgregs[] = { 5645 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH, 5646 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6, 5647 .access = PL1_RW, .accessfn = access_tda, 5648 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]), 5649 .writefn = dbgwvr_write, .raw_writefn = raw_write 5650 }, 5651 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH, 5652 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7, 5653 .access = PL1_RW, .accessfn = access_tda, 5654 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]), 5655 .writefn = dbgwcr_write, .raw_writefn = raw_write 5656 }, 5657 REGINFO_SENTINEL 5658 }; 5659 define_arm_cp_regs(cpu, dbgregs); 5660 } 5661 } 5662 5663 /* We don't know until after realize whether there's a GICv3 5664 * attached, and that is what registers the gicv3 sysregs. 5665 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1 5666 * at runtime. 5667 */ 5668 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri) 5669 { 5670 ARMCPU *cpu = env_archcpu(env); 5671 uint64_t pfr1 = cpu->id_pfr1; 5672 5673 if (env->gicv3state) { 5674 pfr1 |= 1 << 28; 5675 } 5676 return pfr1; 5677 } 5678 5679 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri) 5680 { 5681 ARMCPU *cpu = env_archcpu(env); 5682 uint64_t pfr0 = cpu->isar.id_aa64pfr0; 5683 5684 if (env->gicv3state) { 5685 pfr0 |= 1 << 24; 5686 } 5687 return pfr0; 5688 } 5689 5690 /* Shared logic between LORID and the rest of the LOR* registers. 5691 * Secure state has already been delt with. 5692 */ 5693 static CPAccessResult access_lor_ns(CPUARMState *env) 5694 { 5695 int el = arm_current_el(env); 5696 5697 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) { 5698 return CP_ACCESS_TRAP_EL2; 5699 } 5700 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) { 5701 return CP_ACCESS_TRAP_EL3; 5702 } 5703 return CP_ACCESS_OK; 5704 } 5705 5706 static CPAccessResult access_lorid(CPUARMState *env, const ARMCPRegInfo *ri, 5707 bool isread) 5708 { 5709 if (arm_is_secure_below_el3(env)) { 5710 /* Access ok in secure mode. */ 5711 return CP_ACCESS_OK; 5712 } 5713 return access_lor_ns(env); 5714 } 5715 5716 static CPAccessResult access_lor_other(CPUARMState *env, 5717 const ARMCPRegInfo *ri, bool isread) 5718 { 5719 if (arm_is_secure_below_el3(env)) { 5720 /* Access denied in secure mode. */ 5721 return CP_ACCESS_TRAP; 5722 } 5723 return access_lor_ns(env); 5724 } 5725 5726 #ifdef TARGET_AARCH64 5727 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri, 5728 bool isread) 5729 { 5730 int el = arm_current_el(env); 5731 5732 if (el < 2 && 5733 arm_feature(env, ARM_FEATURE_EL2) && 5734 !(arm_hcr_el2_eff(env) & HCR_APK)) { 5735 return CP_ACCESS_TRAP_EL2; 5736 } 5737 if (el < 3 && 5738 arm_feature(env, ARM_FEATURE_EL3) && 5739 !(env->cp15.scr_el3 & SCR_APK)) { 5740 return CP_ACCESS_TRAP_EL3; 5741 } 5742 return CP_ACCESS_OK; 5743 } 5744 5745 static const ARMCPRegInfo pauth_reginfo[] = { 5746 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5747 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0, 5748 .access = PL1_RW, .accessfn = access_pauth, 5749 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) }, 5750 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5751 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1, 5752 .access = PL1_RW, .accessfn = access_pauth, 5753 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) }, 5754 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5755 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2, 5756 .access = PL1_RW, .accessfn = access_pauth, 5757 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) }, 5758 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5759 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3, 5760 .access = PL1_RW, .accessfn = access_pauth, 5761 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) }, 5762 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5763 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0, 5764 .access = PL1_RW, .accessfn = access_pauth, 5765 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) }, 5766 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5767 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1, 5768 .access = PL1_RW, .accessfn = access_pauth, 5769 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) }, 5770 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5771 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0, 5772 .access = PL1_RW, .accessfn = access_pauth, 5773 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) }, 5774 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5775 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1, 5776 .access = PL1_RW, .accessfn = access_pauth, 5777 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) }, 5778 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 5779 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2, 5780 .access = PL1_RW, .accessfn = access_pauth, 5781 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) }, 5782 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 5783 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3, 5784 .access = PL1_RW, .accessfn = access_pauth, 5785 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) }, 5786 REGINFO_SENTINEL 5787 }; 5788 5789 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 5790 { 5791 Error *err = NULL; 5792 uint64_t ret; 5793 5794 /* Success sets NZCV = 0000. */ 5795 env->NF = env->CF = env->VF = 0, env->ZF = 1; 5796 5797 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) { 5798 /* 5799 * ??? Failed, for unknown reasons in the crypto subsystem. 5800 * The best we can do is log the reason and return the 5801 * timed-out indication to the guest. There is no reason 5802 * we know to expect this failure to be transitory, so the 5803 * guest may well hang retrying the operation. 5804 */ 5805 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s", 5806 ri->name, error_get_pretty(err)); 5807 error_free(err); 5808 5809 env->ZF = 0; /* NZCF = 0100 */ 5810 return 0; 5811 } 5812 return ret; 5813 } 5814 5815 /* We do not support re-seeding, so the two registers operate the same. */ 5816 static const ARMCPRegInfo rndr_reginfo[] = { 5817 { .name = "RNDR", .state = ARM_CP_STATE_AA64, 5818 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO, 5819 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0, 5820 .access = PL0_R, .readfn = rndr_readfn }, 5821 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64, 5822 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO, 5823 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1, 5824 .access = PL0_R, .readfn = rndr_readfn }, 5825 REGINFO_SENTINEL 5826 }; 5827 #endif 5828 5829 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri, 5830 bool isread) 5831 { 5832 int el = arm_current_el(env); 5833 5834 if (el == 0) { 5835 uint64_t sctlr = arm_sctlr(env, el); 5836 if (!(sctlr & SCTLR_EnRCTX)) { 5837 return CP_ACCESS_TRAP; 5838 } 5839 } else if (el == 1) { 5840 uint64_t hcr = arm_hcr_el2_eff(env); 5841 if (hcr & HCR_NV) { 5842 return CP_ACCESS_TRAP_EL2; 5843 } 5844 } 5845 return CP_ACCESS_OK; 5846 } 5847 5848 static const ARMCPRegInfo predinv_reginfo[] = { 5849 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64, 5850 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4, 5851 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5852 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64, 5853 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5, 5854 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5855 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64, 5856 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7, 5857 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5858 /* 5859 * Note the AArch32 opcodes have a different OPC1. 5860 */ 5861 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32, 5862 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4, 5863 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5864 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32, 5865 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5, 5866 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5867 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32, 5868 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7, 5869 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 5870 REGINFO_SENTINEL 5871 }; 5872 5873 void register_cp_regs_for_features(ARMCPU *cpu) 5874 { 5875 /* Register all the coprocessor registers based on feature bits */ 5876 CPUARMState *env = &cpu->env; 5877 if (arm_feature(env, ARM_FEATURE_M)) { 5878 /* M profile has no coprocessor registers */ 5879 return; 5880 } 5881 5882 define_arm_cp_regs(cpu, cp_reginfo); 5883 if (!arm_feature(env, ARM_FEATURE_V8)) { 5884 /* Must go early as it is full of wildcards that may be 5885 * overridden by later definitions. 5886 */ 5887 define_arm_cp_regs(cpu, not_v8_cp_reginfo); 5888 } 5889 5890 if (arm_feature(env, ARM_FEATURE_V6)) { 5891 /* The ID registers all have impdef reset values */ 5892 ARMCPRegInfo v6_idregs[] = { 5893 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, 5894 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 5895 .access = PL1_R, .type = ARM_CP_CONST, 5896 .resetvalue = cpu->id_pfr0 }, 5897 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know 5898 * the value of the GIC field until after we define these regs. 5899 */ 5900 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, 5901 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, 5902 .access = PL1_R, .type = ARM_CP_NO_RAW, 5903 .readfn = id_pfr1_read, 5904 .writefn = arm_cp_write_ignore }, 5905 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, 5906 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, 5907 .access = PL1_R, .type = ARM_CP_CONST, 5908 .resetvalue = cpu->id_dfr0 }, 5909 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, 5910 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, 5911 .access = PL1_R, .type = ARM_CP_CONST, 5912 .resetvalue = cpu->id_afr0 }, 5913 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, 5914 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, 5915 .access = PL1_R, .type = ARM_CP_CONST, 5916 .resetvalue = cpu->id_mmfr0 }, 5917 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, 5918 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, 5919 .access = PL1_R, .type = ARM_CP_CONST, 5920 .resetvalue = cpu->id_mmfr1 }, 5921 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, 5922 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, 5923 .access = PL1_R, .type = ARM_CP_CONST, 5924 .resetvalue = cpu->id_mmfr2 }, 5925 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, 5926 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, 5927 .access = PL1_R, .type = ARM_CP_CONST, 5928 .resetvalue = cpu->id_mmfr3 }, 5929 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, 5930 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 5931 .access = PL1_R, .type = ARM_CP_CONST, 5932 .resetvalue = cpu->isar.id_isar0 }, 5933 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, 5934 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, 5935 .access = PL1_R, .type = ARM_CP_CONST, 5936 .resetvalue = cpu->isar.id_isar1 }, 5937 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, 5938 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 5939 .access = PL1_R, .type = ARM_CP_CONST, 5940 .resetvalue = cpu->isar.id_isar2 }, 5941 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, 5942 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, 5943 .access = PL1_R, .type = ARM_CP_CONST, 5944 .resetvalue = cpu->isar.id_isar3 }, 5945 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, 5946 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, 5947 .access = PL1_R, .type = ARM_CP_CONST, 5948 .resetvalue = cpu->isar.id_isar4 }, 5949 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, 5950 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, 5951 .access = PL1_R, .type = ARM_CP_CONST, 5952 .resetvalue = cpu->isar.id_isar5 }, 5953 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH, 5954 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6, 5955 .access = PL1_R, .type = ARM_CP_CONST, 5956 .resetvalue = cpu->id_mmfr4 }, 5957 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH, 5958 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7, 5959 .access = PL1_R, .type = ARM_CP_CONST, 5960 .resetvalue = cpu->isar.id_isar6 }, 5961 REGINFO_SENTINEL 5962 }; 5963 define_arm_cp_regs(cpu, v6_idregs); 5964 define_arm_cp_regs(cpu, v6_cp_reginfo); 5965 } else { 5966 define_arm_cp_regs(cpu, not_v6_cp_reginfo); 5967 } 5968 if (arm_feature(env, ARM_FEATURE_V6K)) { 5969 define_arm_cp_regs(cpu, v6k_cp_reginfo); 5970 } 5971 if (arm_feature(env, ARM_FEATURE_V7MP) && 5972 !arm_feature(env, ARM_FEATURE_PMSA)) { 5973 define_arm_cp_regs(cpu, v7mp_cp_reginfo); 5974 } 5975 if (arm_feature(env, ARM_FEATURE_V7VE)) { 5976 define_arm_cp_regs(cpu, pmovsset_cp_reginfo); 5977 } 5978 if (arm_feature(env, ARM_FEATURE_V7)) { 5979 /* v7 performance monitor control register: same implementor 5980 * field as main ID register, and we implement four counters in 5981 * addition to the cycle count register. 5982 */ 5983 unsigned int i, pmcrn = 4; 5984 ARMCPRegInfo pmcr = { 5985 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, 5986 .access = PL0_RW, 5987 .type = ARM_CP_IO | ARM_CP_ALIAS, 5988 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), 5989 .accessfn = pmreg_access, .writefn = pmcr_write, 5990 .raw_writefn = raw_write, 5991 }; 5992 ARMCPRegInfo pmcr64 = { 5993 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, 5994 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, 5995 .access = PL0_RW, .accessfn = pmreg_access, 5996 .type = ARM_CP_IO, 5997 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), 5998 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT), 5999 .writefn = pmcr_write, .raw_writefn = raw_write, 6000 }; 6001 define_one_arm_cp_reg(cpu, &pmcr); 6002 define_one_arm_cp_reg(cpu, &pmcr64); 6003 for (i = 0; i < pmcrn; i++) { 6004 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i); 6005 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i); 6006 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i); 6007 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i); 6008 ARMCPRegInfo pmev_regs[] = { 6009 { .name = pmevcntr_name, .cp = 15, .crn = 14, 6010 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 6011 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 6012 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 6013 .accessfn = pmreg_access }, 6014 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64, 6015 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)), 6016 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access, 6017 .type = ARM_CP_IO, 6018 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 6019 .raw_readfn = pmevcntr_rawread, 6020 .raw_writefn = pmevcntr_rawwrite }, 6021 { .name = pmevtyper_name, .cp = 15, .crn = 14, 6022 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 6023 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 6024 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 6025 .accessfn = pmreg_access }, 6026 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64, 6027 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)), 6028 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access, 6029 .type = ARM_CP_IO, 6030 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 6031 .raw_writefn = pmevtyper_rawwrite }, 6032 REGINFO_SENTINEL 6033 }; 6034 define_arm_cp_regs(cpu, pmev_regs); 6035 g_free(pmevcntr_name); 6036 g_free(pmevcntr_el0_name); 6037 g_free(pmevtyper_name); 6038 g_free(pmevtyper_el0_name); 6039 } 6040 ARMCPRegInfo clidr = { 6041 .name = "CLIDR", .state = ARM_CP_STATE_BOTH, 6042 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, 6043 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->clidr 6044 }; 6045 define_one_arm_cp_reg(cpu, &clidr); 6046 define_arm_cp_regs(cpu, v7_cp_reginfo); 6047 define_debug_regs(cpu); 6048 } else { 6049 define_arm_cp_regs(cpu, not_v7_cp_reginfo); 6050 } 6051 if (FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) >= 4 && 6052 FIELD_EX32(cpu->id_dfr0, ID_DFR0, PERFMON) != 0xf) { 6053 ARMCPRegInfo v81_pmu_regs[] = { 6054 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32, 6055 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4, 6056 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6057 .resetvalue = extract64(cpu->pmceid0, 32, 32) }, 6058 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32, 6059 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5, 6060 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6061 .resetvalue = extract64(cpu->pmceid1, 32, 32) }, 6062 REGINFO_SENTINEL 6063 }; 6064 define_arm_cp_regs(cpu, v81_pmu_regs); 6065 } 6066 if (arm_feature(env, ARM_FEATURE_V8)) { 6067 /* AArch64 ID registers, which all have impdef reset values. 6068 * Note that within the ID register ranges the unused slots 6069 * must all RAZ, not UNDEF; future architecture versions may 6070 * define new registers here. 6071 */ 6072 ARMCPRegInfo v8_idregs[] = { 6073 /* ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST because we don't 6074 * know the right value for the GIC field until after we 6075 * define these regs. 6076 */ 6077 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, 6078 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, 6079 .access = PL1_R, .type = ARM_CP_NO_RAW, 6080 .readfn = id_aa64pfr0_read, 6081 .writefn = arm_cp_write_ignore }, 6082 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, 6083 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, 6084 .access = PL1_R, .type = ARM_CP_CONST, 6085 .resetvalue = cpu->isar.id_aa64pfr1}, 6086 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6087 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2, 6088 .access = PL1_R, .type = ARM_CP_CONST, 6089 .resetvalue = 0 }, 6090 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6091 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3, 6092 .access = PL1_R, .type = ARM_CP_CONST, 6093 .resetvalue = 0 }, 6094 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64, 6095 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4, 6096 .access = PL1_R, .type = ARM_CP_CONST, 6097 /* At present, only SVEver == 0 is defined anyway. */ 6098 .resetvalue = 0 }, 6099 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6100 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5, 6101 .access = PL1_R, .type = ARM_CP_CONST, 6102 .resetvalue = 0 }, 6103 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6104 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6, 6105 .access = PL1_R, .type = ARM_CP_CONST, 6106 .resetvalue = 0 }, 6107 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6108 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7, 6109 .access = PL1_R, .type = ARM_CP_CONST, 6110 .resetvalue = 0 }, 6111 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, 6112 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, 6113 .access = PL1_R, .type = ARM_CP_CONST, 6114 .resetvalue = cpu->id_aa64dfr0 }, 6115 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, 6116 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, 6117 .access = PL1_R, .type = ARM_CP_CONST, 6118 .resetvalue = cpu->id_aa64dfr1 }, 6119 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6120 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2, 6121 .access = PL1_R, .type = ARM_CP_CONST, 6122 .resetvalue = 0 }, 6123 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6124 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3, 6125 .access = PL1_R, .type = ARM_CP_CONST, 6126 .resetvalue = 0 }, 6127 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, 6128 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, 6129 .access = PL1_R, .type = ARM_CP_CONST, 6130 .resetvalue = cpu->id_aa64afr0 }, 6131 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, 6132 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, 6133 .access = PL1_R, .type = ARM_CP_CONST, 6134 .resetvalue = cpu->id_aa64afr1 }, 6135 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6136 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6, 6137 .access = PL1_R, .type = ARM_CP_CONST, 6138 .resetvalue = 0 }, 6139 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6140 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7, 6141 .access = PL1_R, .type = ARM_CP_CONST, 6142 .resetvalue = 0 }, 6143 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, 6144 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, 6145 .access = PL1_R, .type = ARM_CP_CONST, 6146 .resetvalue = cpu->isar.id_aa64isar0 }, 6147 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, 6148 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, 6149 .access = PL1_R, .type = ARM_CP_CONST, 6150 .resetvalue = cpu->isar.id_aa64isar1 }, 6151 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6152 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2, 6153 .access = PL1_R, .type = ARM_CP_CONST, 6154 .resetvalue = 0 }, 6155 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6156 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3, 6157 .access = PL1_R, .type = ARM_CP_CONST, 6158 .resetvalue = 0 }, 6159 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6160 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4, 6161 .access = PL1_R, .type = ARM_CP_CONST, 6162 .resetvalue = 0 }, 6163 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6164 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5, 6165 .access = PL1_R, .type = ARM_CP_CONST, 6166 .resetvalue = 0 }, 6167 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6168 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6, 6169 .access = PL1_R, .type = ARM_CP_CONST, 6170 .resetvalue = 0 }, 6171 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6172 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7, 6173 .access = PL1_R, .type = ARM_CP_CONST, 6174 .resetvalue = 0 }, 6175 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, 6176 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 6177 .access = PL1_R, .type = ARM_CP_CONST, 6178 .resetvalue = cpu->isar.id_aa64mmfr0 }, 6179 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, 6180 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, 6181 .access = PL1_R, .type = ARM_CP_CONST, 6182 .resetvalue = cpu->isar.id_aa64mmfr1 }, 6183 { .name = "ID_AA64MMFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6184 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2, 6185 .access = PL1_R, .type = ARM_CP_CONST, 6186 .resetvalue = 0 }, 6187 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6188 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3, 6189 .access = PL1_R, .type = ARM_CP_CONST, 6190 .resetvalue = 0 }, 6191 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6192 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4, 6193 .access = PL1_R, .type = ARM_CP_CONST, 6194 .resetvalue = 0 }, 6195 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6196 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5, 6197 .access = PL1_R, .type = ARM_CP_CONST, 6198 .resetvalue = 0 }, 6199 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6200 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6, 6201 .access = PL1_R, .type = ARM_CP_CONST, 6202 .resetvalue = 0 }, 6203 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6204 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7, 6205 .access = PL1_R, .type = ARM_CP_CONST, 6206 .resetvalue = 0 }, 6207 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, 6208 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 6209 .access = PL1_R, .type = ARM_CP_CONST, 6210 .resetvalue = cpu->isar.mvfr0 }, 6211 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, 6212 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 6213 .access = PL1_R, .type = ARM_CP_CONST, 6214 .resetvalue = cpu->isar.mvfr1 }, 6215 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, 6216 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 6217 .access = PL1_R, .type = ARM_CP_CONST, 6218 .resetvalue = cpu->isar.mvfr2 }, 6219 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6220 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3, 6221 .access = PL1_R, .type = ARM_CP_CONST, 6222 .resetvalue = 0 }, 6223 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6224 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4, 6225 .access = PL1_R, .type = ARM_CP_CONST, 6226 .resetvalue = 0 }, 6227 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6228 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5, 6229 .access = PL1_R, .type = ARM_CP_CONST, 6230 .resetvalue = 0 }, 6231 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6232 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6, 6233 .access = PL1_R, .type = ARM_CP_CONST, 6234 .resetvalue = 0 }, 6235 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 6236 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7, 6237 .access = PL1_R, .type = ARM_CP_CONST, 6238 .resetvalue = 0 }, 6239 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32, 6240 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6, 6241 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6242 .resetvalue = extract64(cpu->pmceid0, 0, 32) }, 6243 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64, 6244 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6, 6245 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6246 .resetvalue = cpu->pmceid0 }, 6247 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32, 6248 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7, 6249 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6250 .resetvalue = extract64(cpu->pmceid1, 0, 32) }, 6251 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64, 6252 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7, 6253 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6254 .resetvalue = cpu->pmceid1 }, 6255 REGINFO_SENTINEL 6256 }; 6257 #ifdef CONFIG_USER_ONLY 6258 ARMCPRegUserSpaceInfo v8_user_idregs[] = { 6259 { .name = "ID_AA64PFR0_EL1", 6260 .exported_bits = 0x000f000f00ff0000, 6261 .fixed_bits = 0x0000000000000011 }, 6262 { .name = "ID_AA64PFR1_EL1", 6263 .exported_bits = 0x00000000000000f0 }, 6264 { .name = "ID_AA64PFR*_EL1_RESERVED", 6265 .is_glob = true }, 6266 { .name = "ID_AA64ZFR0_EL1" }, 6267 { .name = "ID_AA64MMFR0_EL1", 6268 .fixed_bits = 0x00000000ff000000 }, 6269 { .name = "ID_AA64MMFR1_EL1" }, 6270 { .name = "ID_AA64MMFR*_EL1_RESERVED", 6271 .is_glob = true }, 6272 { .name = "ID_AA64DFR0_EL1", 6273 .fixed_bits = 0x0000000000000006 }, 6274 { .name = "ID_AA64DFR1_EL1" }, 6275 { .name = "ID_AA64DFR*_EL1_RESERVED", 6276 .is_glob = true }, 6277 { .name = "ID_AA64AFR*", 6278 .is_glob = true }, 6279 { .name = "ID_AA64ISAR0_EL1", 6280 .exported_bits = 0x00fffffff0fffff0 }, 6281 { .name = "ID_AA64ISAR1_EL1", 6282 .exported_bits = 0x000000f0ffffffff }, 6283 { .name = "ID_AA64ISAR*_EL1_RESERVED", 6284 .is_glob = true }, 6285 REGUSERINFO_SENTINEL 6286 }; 6287 modify_arm_cp_regs(v8_idregs, v8_user_idregs); 6288 #endif 6289 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */ 6290 if (!arm_feature(env, ARM_FEATURE_EL3) && 6291 !arm_feature(env, ARM_FEATURE_EL2)) { 6292 ARMCPRegInfo rvbar = { 6293 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64, 6294 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 6295 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar 6296 }; 6297 define_one_arm_cp_reg(cpu, &rvbar); 6298 } 6299 define_arm_cp_regs(cpu, v8_idregs); 6300 define_arm_cp_regs(cpu, v8_cp_reginfo); 6301 } 6302 if (arm_feature(env, ARM_FEATURE_EL2)) { 6303 uint64_t vmpidr_def = mpidr_read_val(env); 6304 ARMCPRegInfo vpidr_regs[] = { 6305 { .name = "VPIDR", .state = ARM_CP_STATE_AA32, 6306 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 6307 .access = PL2_RW, .accessfn = access_el3_aa32ns, 6308 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS, 6309 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) }, 6310 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64, 6311 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 6312 .access = PL2_RW, .resetvalue = cpu->midr, 6313 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 6314 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32, 6315 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 6316 .access = PL2_RW, .accessfn = access_el3_aa32ns, 6317 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS, 6318 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) }, 6319 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64, 6320 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 6321 .access = PL2_RW, 6322 .resetvalue = vmpidr_def, 6323 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, 6324 REGINFO_SENTINEL 6325 }; 6326 define_arm_cp_regs(cpu, vpidr_regs); 6327 define_arm_cp_regs(cpu, el2_cp_reginfo); 6328 if (arm_feature(env, ARM_FEATURE_V8)) { 6329 define_arm_cp_regs(cpu, el2_v8_cp_reginfo); 6330 } 6331 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */ 6332 if (!arm_feature(env, ARM_FEATURE_EL3)) { 6333 ARMCPRegInfo rvbar = { 6334 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64, 6335 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1, 6336 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar 6337 }; 6338 define_one_arm_cp_reg(cpu, &rvbar); 6339 } 6340 } else { 6341 /* If EL2 is missing but higher ELs are enabled, we need to 6342 * register the no_el2 reginfos. 6343 */ 6344 if (arm_feature(env, ARM_FEATURE_EL3)) { 6345 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value 6346 * of MIDR_EL1 and MPIDR_EL1. 6347 */ 6348 ARMCPRegInfo vpidr_regs[] = { 6349 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH, 6350 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 6351 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 6352 .type = ARM_CP_CONST, .resetvalue = cpu->midr, 6353 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 6354 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH, 6355 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 6356 .access = PL2_RW, .accessfn = access_el3_aa32ns_aa64any, 6357 .type = ARM_CP_NO_RAW, 6358 .writefn = arm_cp_write_ignore, .readfn = mpidr_read }, 6359 REGINFO_SENTINEL 6360 }; 6361 define_arm_cp_regs(cpu, vpidr_regs); 6362 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo); 6363 if (arm_feature(env, ARM_FEATURE_V8)) { 6364 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo); 6365 } 6366 } 6367 } 6368 if (arm_feature(env, ARM_FEATURE_EL3)) { 6369 define_arm_cp_regs(cpu, el3_cp_reginfo); 6370 ARMCPRegInfo el3_regs[] = { 6371 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64, 6372 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1, 6373 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar }, 6374 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, 6375 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, 6376 .access = PL3_RW, 6377 .raw_writefn = raw_write, .writefn = sctlr_write, 6378 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]), 6379 .resetvalue = cpu->reset_sctlr }, 6380 REGINFO_SENTINEL 6381 }; 6382 6383 define_arm_cp_regs(cpu, el3_regs); 6384 } 6385 /* The behaviour of NSACR is sufficiently various that we don't 6386 * try to describe it in a single reginfo: 6387 * if EL3 is 64 bit, then trap to EL3 from S EL1, 6388 * reads as constant 0xc00 from NS EL1 and NS EL2 6389 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2 6390 * if v7 without EL3, register doesn't exist 6391 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2 6392 */ 6393 if (arm_feature(env, ARM_FEATURE_EL3)) { 6394 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 6395 ARMCPRegInfo nsacr = { 6396 .name = "NSACR", .type = ARM_CP_CONST, 6397 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 6398 .access = PL1_RW, .accessfn = nsacr_access, 6399 .resetvalue = 0xc00 6400 }; 6401 define_one_arm_cp_reg(cpu, &nsacr); 6402 } else { 6403 ARMCPRegInfo nsacr = { 6404 .name = "NSACR", 6405 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 6406 .access = PL3_RW | PL1_R, 6407 .resetvalue = 0, 6408 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) 6409 }; 6410 define_one_arm_cp_reg(cpu, &nsacr); 6411 } 6412 } else { 6413 if (arm_feature(env, ARM_FEATURE_V8)) { 6414 ARMCPRegInfo nsacr = { 6415 .name = "NSACR", .type = ARM_CP_CONST, 6416 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 6417 .access = PL1_R, 6418 .resetvalue = 0xc00 6419 }; 6420 define_one_arm_cp_reg(cpu, &nsacr); 6421 } 6422 } 6423 6424 if (arm_feature(env, ARM_FEATURE_PMSA)) { 6425 if (arm_feature(env, ARM_FEATURE_V6)) { 6426 /* PMSAv6 not implemented */ 6427 assert(arm_feature(env, ARM_FEATURE_V7)); 6428 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 6429 define_arm_cp_regs(cpu, pmsav7_cp_reginfo); 6430 } else { 6431 define_arm_cp_regs(cpu, pmsav5_cp_reginfo); 6432 } 6433 } else { 6434 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 6435 define_arm_cp_regs(cpu, vmsa_cp_reginfo); 6436 /* TTCBR2 is introduced with ARMv8.2-A32HPD. */ 6437 if (FIELD_EX32(cpu->id_mmfr4, ID_MMFR4, HPDS) != 0) { 6438 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo); 6439 } 6440 } 6441 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { 6442 define_arm_cp_regs(cpu, t2ee_cp_reginfo); 6443 } 6444 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 6445 define_arm_cp_regs(cpu, generic_timer_cp_reginfo); 6446 } 6447 if (arm_feature(env, ARM_FEATURE_VAPA)) { 6448 define_arm_cp_regs(cpu, vapa_cp_reginfo); 6449 } 6450 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { 6451 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); 6452 } 6453 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { 6454 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); 6455 } 6456 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { 6457 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); 6458 } 6459 if (arm_feature(env, ARM_FEATURE_OMAPCP)) { 6460 define_arm_cp_regs(cpu, omap_cp_reginfo); 6461 } 6462 if (arm_feature(env, ARM_FEATURE_STRONGARM)) { 6463 define_arm_cp_regs(cpu, strongarm_cp_reginfo); 6464 } 6465 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 6466 define_arm_cp_regs(cpu, xscale_cp_reginfo); 6467 } 6468 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { 6469 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); 6470 } 6471 if (arm_feature(env, ARM_FEATURE_LPAE)) { 6472 define_arm_cp_regs(cpu, lpae_cp_reginfo); 6473 } 6474 /* Slightly awkwardly, the OMAP and StrongARM cores need all of 6475 * cp15 crn=0 to be writes-ignored, whereas for other cores they should 6476 * be read-only (ie write causes UNDEF exception). 6477 */ 6478 { 6479 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { 6480 /* Pre-v8 MIDR space. 6481 * Note that the MIDR isn't a simple constant register because 6482 * of the TI925 behaviour where writes to another register can 6483 * cause the MIDR value to change. 6484 * 6485 * Unimplemented registers in the c15 0 0 0 space default to 6486 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR 6487 * and friends override accordingly. 6488 */ 6489 { .name = "MIDR", 6490 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, 6491 .access = PL1_R, .resetvalue = cpu->midr, 6492 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, 6493 .readfn = midr_read, 6494 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 6495 .type = ARM_CP_OVERRIDE }, 6496 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ 6497 { .name = "DUMMY", 6498 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, 6499 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6500 { .name = "DUMMY", 6501 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, 6502 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6503 { .name = "DUMMY", 6504 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, 6505 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6506 { .name = "DUMMY", 6507 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, 6508 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6509 { .name = "DUMMY", 6510 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, 6511 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6512 REGINFO_SENTINEL 6513 }; 6514 ARMCPRegInfo id_v8_midr_cp_reginfo[] = { 6515 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, 6516 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, 6517 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr, 6518 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 6519 .readfn = midr_read }, 6520 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */ 6521 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 6522 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 6523 .access = PL1_R, .resetvalue = cpu->midr }, 6524 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 6525 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7, 6526 .access = PL1_R, .resetvalue = cpu->midr }, 6527 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, 6528 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, 6529 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->revidr }, 6530 REGINFO_SENTINEL 6531 }; 6532 ARMCPRegInfo id_cp_reginfo[] = { 6533 /* These are common to v8 and pre-v8 */ 6534 { .name = "CTR", 6535 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, 6536 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 6537 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, 6538 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, 6539 .access = PL0_R, .accessfn = ctr_el0_access, 6540 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 6541 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ 6542 { .name = "TCMTR", 6543 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, 6544 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 6545 REGINFO_SENTINEL 6546 }; 6547 /* TLBTR is specific to VMSA */ 6548 ARMCPRegInfo id_tlbtr_reginfo = { 6549 .name = "TLBTR", 6550 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, 6551 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0, 6552 }; 6553 /* MPUIR is specific to PMSA V6+ */ 6554 ARMCPRegInfo id_mpuir_reginfo = { 6555 .name = "MPUIR", 6556 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 6557 .access = PL1_R, .type = ARM_CP_CONST, 6558 .resetvalue = cpu->pmsav7_dregion << 8 6559 }; 6560 ARMCPRegInfo crn0_wi_reginfo = { 6561 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, 6562 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, 6563 .type = ARM_CP_NOP | ARM_CP_OVERRIDE 6564 }; 6565 #ifdef CONFIG_USER_ONLY 6566 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = { 6567 { .name = "MIDR_EL1", 6568 .exported_bits = 0x00000000ffffffff }, 6569 { .name = "REVIDR_EL1" }, 6570 REGUSERINFO_SENTINEL 6571 }; 6572 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo); 6573 #endif 6574 if (arm_feature(env, ARM_FEATURE_OMAPCP) || 6575 arm_feature(env, ARM_FEATURE_STRONGARM)) { 6576 ARMCPRegInfo *r; 6577 /* Register the blanket "writes ignored" value first to cover the 6578 * whole space. Then update the specific ID registers to allow write 6579 * access, so that they ignore writes rather than causing them to 6580 * UNDEF. 6581 */ 6582 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); 6583 for (r = id_pre_v8_midr_cp_reginfo; 6584 r->type != ARM_CP_SENTINEL; r++) { 6585 r->access = PL1_RW; 6586 } 6587 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) { 6588 r->access = PL1_RW; 6589 } 6590 id_mpuir_reginfo.access = PL1_RW; 6591 id_tlbtr_reginfo.access = PL1_RW; 6592 } 6593 if (arm_feature(env, ARM_FEATURE_V8)) { 6594 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); 6595 } else { 6596 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); 6597 } 6598 define_arm_cp_regs(cpu, id_cp_reginfo); 6599 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 6600 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo); 6601 } else if (arm_feature(env, ARM_FEATURE_V7)) { 6602 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 6603 } 6604 } 6605 6606 if (arm_feature(env, ARM_FEATURE_MPIDR)) { 6607 ARMCPRegInfo mpidr_cp_reginfo[] = { 6608 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH, 6609 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, 6610 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW }, 6611 REGINFO_SENTINEL 6612 }; 6613 #ifdef CONFIG_USER_ONLY 6614 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = { 6615 { .name = "MPIDR_EL1", 6616 .fixed_bits = 0x0000000080000000 }, 6617 REGUSERINFO_SENTINEL 6618 }; 6619 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo); 6620 #endif 6621 define_arm_cp_regs(cpu, mpidr_cp_reginfo); 6622 } 6623 6624 if (arm_feature(env, ARM_FEATURE_AUXCR)) { 6625 ARMCPRegInfo auxcr_reginfo[] = { 6626 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, 6627 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, 6628 .access = PL1_RW, .type = ARM_CP_CONST, 6629 .resetvalue = cpu->reset_auxcr }, 6630 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH, 6631 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1, 6632 .access = PL2_RW, .type = ARM_CP_CONST, 6633 .resetvalue = 0 }, 6634 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64, 6635 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1, 6636 .access = PL3_RW, .type = ARM_CP_CONST, 6637 .resetvalue = 0 }, 6638 REGINFO_SENTINEL 6639 }; 6640 define_arm_cp_regs(cpu, auxcr_reginfo); 6641 if (arm_feature(env, ARM_FEATURE_V8)) { 6642 /* HACTLR2 maps to ACTLR_EL2[63:32] and is not in ARMv7 */ 6643 ARMCPRegInfo hactlr2_reginfo = { 6644 .name = "HACTLR2", .state = ARM_CP_STATE_AA32, 6645 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3, 6646 .access = PL2_RW, .type = ARM_CP_CONST, 6647 .resetvalue = 0 6648 }; 6649 define_one_arm_cp_reg(cpu, &hactlr2_reginfo); 6650 } 6651 } 6652 6653 if (arm_feature(env, ARM_FEATURE_CBAR)) { 6654 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 6655 /* 32 bit view is [31:18] 0...0 [43:32]. */ 6656 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) 6657 | extract64(cpu->reset_cbar, 32, 12); 6658 ARMCPRegInfo cbar_reginfo[] = { 6659 { .name = "CBAR", 6660 .type = ARM_CP_CONST, 6661 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 6662 .access = PL1_R, .resetvalue = cpu->reset_cbar }, 6663 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, 6664 .type = ARM_CP_CONST, 6665 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, 6666 .access = PL1_R, .resetvalue = cbar32 }, 6667 REGINFO_SENTINEL 6668 }; 6669 /* We don't implement a r/w 64 bit CBAR currently */ 6670 assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); 6671 define_arm_cp_regs(cpu, cbar_reginfo); 6672 } else { 6673 ARMCPRegInfo cbar = { 6674 .name = "CBAR", 6675 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 6676 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar, 6677 .fieldoffset = offsetof(CPUARMState, 6678 cp15.c15_config_base_address) 6679 }; 6680 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 6681 cbar.access = PL1_R; 6682 cbar.fieldoffset = 0; 6683 cbar.type = ARM_CP_CONST; 6684 } 6685 define_one_arm_cp_reg(cpu, &cbar); 6686 } 6687 } 6688 6689 if (arm_feature(env, ARM_FEATURE_VBAR)) { 6690 ARMCPRegInfo vbar_cp_reginfo[] = { 6691 { .name = "VBAR", .state = ARM_CP_STATE_BOTH, 6692 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, 6693 .access = PL1_RW, .writefn = vbar_write, 6694 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s), 6695 offsetof(CPUARMState, cp15.vbar_ns) }, 6696 .resetvalue = 0 }, 6697 REGINFO_SENTINEL 6698 }; 6699 define_arm_cp_regs(cpu, vbar_cp_reginfo); 6700 } 6701 6702 /* Generic registers whose values depend on the implementation */ 6703 { 6704 ARMCPRegInfo sctlr = { 6705 .name = "SCTLR", .state = ARM_CP_STATE_BOTH, 6706 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 6707 .access = PL1_RW, 6708 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), 6709 offsetof(CPUARMState, cp15.sctlr_ns) }, 6710 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, 6711 .raw_writefn = raw_write, 6712 }; 6713 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 6714 /* Normally we would always end the TB on an SCTLR write, but Linux 6715 * arch/arm/mach-pxa/sleep.S expects two instructions following 6716 * an MMU enable to execute from cache. Imitate this behaviour. 6717 */ 6718 sctlr.type |= ARM_CP_SUPPRESS_TB_END; 6719 } 6720 define_one_arm_cp_reg(cpu, &sctlr); 6721 } 6722 6723 if (cpu_isar_feature(aa64_lor, cpu)) { 6724 /* 6725 * A trivial implementation of ARMv8.1-LOR leaves all of these 6726 * registers fixed at 0, which indicates that there are zero 6727 * supported Limited Ordering regions. 6728 */ 6729 static const ARMCPRegInfo lor_reginfo[] = { 6730 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64, 6731 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0, 6732 .access = PL1_RW, .accessfn = access_lor_other, 6733 .type = ARM_CP_CONST, .resetvalue = 0 }, 6734 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64, 6735 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1, 6736 .access = PL1_RW, .accessfn = access_lor_other, 6737 .type = ARM_CP_CONST, .resetvalue = 0 }, 6738 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64, 6739 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2, 6740 .access = PL1_RW, .accessfn = access_lor_other, 6741 .type = ARM_CP_CONST, .resetvalue = 0 }, 6742 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64, 6743 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3, 6744 .access = PL1_RW, .accessfn = access_lor_other, 6745 .type = ARM_CP_CONST, .resetvalue = 0 }, 6746 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64, 6747 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7, 6748 .access = PL1_R, .accessfn = access_lorid, 6749 .type = ARM_CP_CONST, .resetvalue = 0 }, 6750 REGINFO_SENTINEL 6751 }; 6752 define_arm_cp_regs(cpu, lor_reginfo); 6753 } 6754 6755 if (cpu_isar_feature(aa64_sve, cpu)) { 6756 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo); 6757 if (arm_feature(env, ARM_FEATURE_EL2)) { 6758 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo); 6759 } else { 6760 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo); 6761 } 6762 if (arm_feature(env, ARM_FEATURE_EL3)) { 6763 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo); 6764 } 6765 } 6766 6767 #ifdef TARGET_AARCH64 6768 if (cpu_isar_feature(aa64_pauth, cpu)) { 6769 define_arm_cp_regs(cpu, pauth_reginfo); 6770 } 6771 if (cpu_isar_feature(aa64_rndr, cpu)) { 6772 define_arm_cp_regs(cpu, rndr_reginfo); 6773 } 6774 #endif 6775 6776 /* 6777 * While all v8.0 cpus support aarch64, QEMU does have configurations 6778 * that do not set ID_AA64ISAR1, e.g. user-only qemu-arm -cpu max, 6779 * which will set ID_ISAR6. 6780 */ 6781 if (arm_feature(&cpu->env, ARM_FEATURE_AARCH64) 6782 ? cpu_isar_feature(aa64_predinv, cpu) 6783 : cpu_isar_feature(aa32_predinv, cpu)) { 6784 define_arm_cp_regs(cpu, predinv_reginfo); 6785 } 6786 } 6787 6788 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) 6789 { 6790 CPUState *cs = CPU(cpu); 6791 CPUARMState *env = &cpu->env; 6792 6793 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 6794 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg, 6795 aarch64_fpu_gdb_set_reg, 6796 34, "aarch64-fpu.xml", 0); 6797 } else if (arm_feature(env, ARM_FEATURE_NEON)) { 6798 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 6799 51, "arm-neon.xml", 0); 6800 } else if (arm_feature(env, ARM_FEATURE_VFP3)) { 6801 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 6802 35, "arm-vfp3.xml", 0); 6803 } else if (arm_feature(env, ARM_FEATURE_VFP)) { 6804 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 6805 19, "arm-vfp.xml", 0); 6806 } 6807 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg, 6808 arm_gen_dynamic_xml(cs), 6809 "system-registers.xml", 0); 6810 } 6811 6812 /* Sort alphabetically by type name, except for "any". */ 6813 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) 6814 { 6815 ObjectClass *class_a = (ObjectClass *)a; 6816 ObjectClass *class_b = (ObjectClass *)b; 6817 const char *name_a, *name_b; 6818 6819 name_a = object_class_get_name(class_a); 6820 name_b = object_class_get_name(class_b); 6821 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) { 6822 return 1; 6823 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) { 6824 return -1; 6825 } else { 6826 return strcmp(name_a, name_b); 6827 } 6828 } 6829 6830 static void arm_cpu_list_entry(gpointer data, gpointer user_data) 6831 { 6832 ObjectClass *oc = data; 6833 const char *typename; 6834 char *name; 6835 6836 typename = object_class_get_name(oc); 6837 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU)); 6838 qemu_printf(" %s\n", name); 6839 g_free(name); 6840 } 6841 6842 void arm_cpu_list(void) 6843 { 6844 GSList *list; 6845 6846 list = object_class_get_list(TYPE_ARM_CPU, false); 6847 list = g_slist_sort(list, arm_cpu_list_compare); 6848 qemu_printf("Available CPUs:\n"); 6849 g_slist_foreach(list, arm_cpu_list_entry, NULL); 6850 g_slist_free(list); 6851 } 6852 6853 static void arm_cpu_add_definition(gpointer data, gpointer user_data) 6854 { 6855 ObjectClass *oc = data; 6856 CpuDefinitionInfoList **cpu_list = user_data; 6857 CpuDefinitionInfoList *entry; 6858 CpuDefinitionInfo *info; 6859 const char *typename; 6860 6861 typename = object_class_get_name(oc); 6862 info = g_malloc0(sizeof(*info)); 6863 info->name = g_strndup(typename, 6864 strlen(typename) - strlen("-" TYPE_ARM_CPU)); 6865 info->q_typename = g_strdup(typename); 6866 6867 entry = g_malloc0(sizeof(*entry)); 6868 entry->value = info; 6869 entry->next = *cpu_list; 6870 *cpu_list = entry; 6871 } 6872 6873 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp) 6874 { 6875 CpuDefinitionInfoList *cpu_list = NULL; 6876 GSList *list; 6877 6878 list = object_class_get_list(TYPE_ARM_CPU, false); 6879 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list); 6880 g_slist_free(list); 6881 6882 return cpu_list; 6883 } 6884 6885 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, 6886 void *opaque, int state, int secstate, 6887 int crm, int opc1, int opc2, 6888 const char *name) 6889 { 6890 /* Private utility function for define_one_arm_cp_reg_with_opaque(): 6891 * add a single reginfo struct to the hash table. 6892 */ 6893 uint32_t *key = g_new(uint32_t, 1); 6894 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); 6895 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; 6896 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0; 6897 6898 r2->name = g_strdup(name); 6899 /* Reset the secure state to the specific incoming state. This is 6900 * necessary as the register may have been defined with both states. 6901 */ 6902 r2->secure = secstate; 6903 6904 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 6905 /* Register is banked (using both entries in array). 6906 * Overwriting fieldoffset as the array is only used to define 6907 * banked registers but later only fieldoffset is used. 6908 */ 6909 r2->fieldoffset = r->bank_fieldoffsets[ns]; 6910 } 6911 6912 if (state == ARM_CP_STATE_AA32) { 6913 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 6914 /* If the register is banked then we don't need to migrate or 6915 * reset the 32-bit instance in certain cases: 6916 * 6917 * 1) If the register has both 32-bit and 64-bit instances then we 6918 * can count on the 64-bit instance taking care of the 6919 * non-secure bank. 6920 * 2) If ARMv8 is enabled then we can count on a 64-bit version 6921 * taking care of the secure bank. This requires that separate 6922 * 32 and 64-bit definitions are provided. 6923 */ 6924 if ((r->state == ARM_CP_STATE_BOTH && ns) || 6925 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) { 6926 r2->type |= ARM_CP_ALIAS; 6927 } 6928 } else if ((secstate != r->secure) && !ns) { 6929 /* The register is not banked so we only want to allow migration of 6930 * the non-secure instance. 6931 */ 6932 r2->type |= ARM_CP_ALIAS; 6933 } 6934 6935 if (r->state == ARM_CP_STATE_BOTH) { 6936 /* We assume it is a cp15 register if the .cp field is left unset. 6937 */ 6938 if (r2->cp == 0) { 6939 r2->cp = 15; 6940 } 6941 6942 #ifdef HOST_WORDS_BIGENDIAN 6943 if (r2->fieldoffset) { 6944 r2->fieldoffset += sizeof(uint32_t); 6945 } 6946 #endif 6947 } 6948 } 6949 if (state == ARM_CP_STATE_AA64) { 6950 /* To allow abbreviation of ARMCPRegInfo 6951 * definitions, we treat cp == 0 as equivalent to 6952 * the value for "standard guest-visible sysreg". 6953 * STATE_BOTH definitions are also always "standard 6954 * sysreg" in their AArch64 view (the .cp value may 6955 * be non-zero for the benefit of the AArch32 view). 6956 */ 6957 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) { 6958 r2->cp = CP_REG_ARM64_SYSREG_CP; 6959 } 6960 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm, 6961 r2->opc0, opc1, opc2); 6962 } else { 6963 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2); 6964 } 6965 if (opaque) { 6966 r2->opaque = opaque; 6967 } 6968 /* reginfo passed to helpers is correct for the actual access, 6969 * and is never ARM_CP_STATE_BOTH: 6970 */ 6971 r2->state = state; 6972 /* Make sure reginfo passed to helpers for wildcarded regs 6973 * has the correct crm/opc1/opc2 for this reg, not CP_ANY: 6974 */ 6975 r2->crm = crm; 6976 r2->opc1 = opc1; 6977 r2->opc2 = opc2; 6978 /* By convention, for wildcarded registers only the first 6979 * entry is used for migration; the others are marked as 6980 * ALIAS so we don't try to transfer the register 6981 * multiple times. Special registers (ie NOP/WFI) are 6982 * never migratable and not even raw-accessible. 6983 */ 6984 if ((r->type & ARM_CP_SPECIAL)) { 6985 r2->type |= ARM_CP_NO_RAW; 6986 } 6987 if (((r->crm == CP_ANY) && crm != 0) || 6988 ((r->opc1 == CP_ANY) && opc1 != 0) || 6989 ((r->opc2 == CP_ANY) && opc2 != 0)) { 6990 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB; 6991 } 6992 6993 /* Check that raw accesses are either forbidden or handled. Note that 6994 * we can't assert this earlier because the setup of fieldoffset for 6995 * banked registers has to be done first. 6996 */ 6997 if (!(r2->type & ARM_CP_NO_RAW)) { 6998 assert(!raw_accessors_invalid(r2)); 6999 } 7000 7001 /* Overriding of an existing definition must be explicitly 7002 * requested. 7003 */ 7004 if (!(r->type & ARM_CP_OVERRIDE)) { 7005 ARMCPRegInfo *oldreg; 7006 oldreg = g_hash_table_lookup(cpu->cp_regs, key); 7007 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { 7008 fprintf(stderr, "Register redefined: cp=%d %d bit " 7009 "crn=%d crm=%d opc1=%d opc2=%d, " 7010 "was %s, now %s\n", r2->cp, 32 + 32 * is64, 7011 r2->crn, r2->crm, r2->opc1, r2->opc2, 7012 oldreg->name, r2->name); 7013 g_assert_not_reached(); 7014 } 7015 } 7016 g_hash_table_insert(cpu->cp_regs, key, r2); 7017 } 7018 7019 7020 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, 7021 const ARMCPRegInfo *r, void *opaque) 7022 { 7023 /* Define implementations of coprocessor registers. 7024 * We store these in a hashtable because typically 7025 * there are less than 150 registers in a space which 7026 * is 16*16*16*8*8 = 262144 in size. 7027 * Wildcarding is supported for the crm, opc1 and opc2 fields. 7028 * If a register is defined twice then the second definition is 7029 * used, so this can be used to define some generic registers and 7030 * then override them with implementation specific variations. 7031 * At least one of the original and the second definition should 7032 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard 7033 * against accidental use. 7034 * 7035 * The state field defines whether the register is to be 7036 * visible in the AArch32 or AArch64 execution state. If the 7037 * state is set to ARM_CP_STATE_BOTH then we synthesise a 7038 * reginfo structure for the AArch32 view, which sees the lower 7039 * 32 bits of the 64 bit register. 7040 * 7041 * Only registers visible in AArch64 may set r->opc0; opc0 cannot 7042 * be wildcarded. AArch64 registers are always considered to be 64 7043 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of 7044 * the register, if any. 7045 */ 7046 int crm, opc1, opc2, state; 7047 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; 7048 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; 7049 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; 7050 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; 7051 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; 7052 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; 7053 /* 64 bit registers have only CRm and Opc1 fields */ 7054 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); 7055 /* op0 only exists in the AArch64 encodings */ 7056 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); 7057 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ 7058 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); 7059 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1 7060 * encodes a minimum access level for the register. We roll this 7061 * runtime check into our general permission check code, so check 7062 * here that the reginfo's specified permissions are strict enough 7063 * to encompass the generic architectural permission check. 7064 */ 7065 if (r->state != ARM_CP_STATE_AA32) { 7066 int mask = 0; 7067 switch (r->opc1) { 7068 case 0: 7069 /* min_EL EL1, but some accessible to EL0 via kernel ABI */ 7070 mask = PL0U_R | PL1_RW; 7071 break; 7072 case 1: case 2: 7073 /* min_EL EL1 */ 7074 mask = PL1_RW; 7075 break; 7076 case 3: 7077 /* min_EL EL0 */ 7078 mask = PL0_RW; 7079 break; 7080 case 4: 7081 /* min_EL EL2 */ 7082 mask = PL2_RW; 7083 break; 7084 case 5: 7085 /* unallocated encoding, so not possible */ 7086 assert(false); 7087 break; 7088 case 6: 7089 /* min_EL EL3 */ 7090 mask = PL3_RW; 7091 break; 7092 case 7: 7093 /* min_EL EL1, secure mode only (we don't check the latter) */ 7094 mask = PL1_RW; 7095 break; 7096 default: 7097 /* broken reginfo with out-of-range opc1 */ 7098 assert(false); 7099 break; 7100 } 7101 /* assert our permissions are not too lax (stricter is fine) */ 7102 assert((r->access & ~mask) == 0); 7103 } 7104 7105 /* Check that the register definition has enough info to handle 7106 * reads and writes if they are permitted. 7107 */ 7108 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { 7109 if (r->access & PL3_R) { 7110 assert((r->fieldoffset || 7111 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 7112 r->readfn); 7113 } 7114 if (r->access & PL3_W) { 7115 assert((r->fieldoffset || 7116 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 7117 r->writefn); 7118 } 7119 } 7120 /* Bad type field probably means missing sentinel at end of reg list */ 7121 assert(cptype_valid(r->type)); 7122 for (crm = crmmin; crm <= crmmax; crm++) { 7123 for (opc1 = opc1min; opc1 <= opc1max; opc1++) { 7124 for (opc2 = opc2min; opc2 <= opc2max; opc2++) { 7125 for (state = ARM_CP_STATE_AA32; 7126 state <= ARM_CP_STATE_AA64; state++) { 7127 if (r->state != state && r->state != ARM_CP_STATE_BOTH) { 7128 continue; 7129 } 7130 if (state == ARM_CP_STATE_AA32) { 7131 /* Under AArch32 CP registers can be common 7132 * (same for secure and non-secure world) or banked. 7133 */ 7134 char *name; 7135 7136 switch (r->secure) { 7137 case ARM_CP_SECSTATE_S: 7138 case ARM_CP_SECSTATE_NS: 7139 add_cpreg_to_hashtable(cpu, r, opaque, state, 7140 r->secure, crm, opc1, opc2, 7141 r->name); 7142 break; 7143 default: 7144 name = g_strdup_printf("%s_S", r->name); 7145 add_cpreg_to_hashtable(cpu, r, opaque, state, 7146 ARM_CP_SECSTATE_S, 7147 crm, opc1, opc2, name); 7148 g_free(name); 7149 add_cpreg_to_hashtable(cpu, r, opaque, state, 7150 ARM_CP_SECSTATE_NS, 7151 crm, opc1, opc2, r->name); 7152 break; 7153 } 7154 } else { 7155 /* AArch64 registers get mapped to non-secure instance 7156 * of AArch32 */ 7157 add_cpreg_to_hashtable(cpu, r, opaque, state, 7158 ARM_CP_SECSTATE_NS, 7159 crm, opc1, opc2, r->name); 7160 } 7161 } 7162 } 7163 } 7164 } 7165 } 7166 7167 void define_arm_cp_regs_with_opaque(ARMCPU *cpu, 7168 const ARMCPRegInfo *regs, void *opaque) 7169 { 7170 /* Define a whole list of registers */ 7171 const ARMCPRegInfo *r; 7172 for (r = regs; r->type != ARM_CP_SENTINEL; r++) { 7173 define_one_arm_cp_reg_with_opaque(cpu, r, opaque); 7174 } 7175 } 7176 7177 /* 7178 * Modify ARMCPRegInfo for access from userspace. 7179 * 7180 * This is a data driven modification directed by 7181 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as 7182 * user-space cannot alter any values and dynamic values pertaining to 7183 * execution state are hidden from user space view anyway. 7184 */ 7185 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods) 7186 { 7187 const ARMCPRegUserSpaceInfo *m; 7188 ARMCPRegInfo *r; 7189 7190 for (m = mods; m->name; m++) { 7191 GPatternSpec *pat = NULL; 7192 if (m->is_glob) { 7193 pat = g_pattern_spec_new(m->name); 7194 } 7195 for (r = regs; r->type != ARM_CP_SENTINEL; r++) { 7196 if (pat && g_pattern_match_string(pat, r->name)) { 7197 r->type = ARM_CP_CONST; 7198 r->access = PL0U_R; 7199 r->resetvalue = 0; 7200 /* continue */ 7201 } else if (strcmp(r->name, m->name) == 0) { 7202 r->type = ARM_CP_CONST; 7203 r->access = PL0U_R; 7204 r->resetvalue &= m->exported_bits; 7205 r->resetvalue |= m->fixed_bits; 7206 break; 7207 } 7208 } 7209 if (pat) { 7210 g_pattern_spec_free(pat); 7211 } 7212 } 7213 } 7214 7215 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) 7216 { 7217 return g_hash_table_lookup(cpregs, &encoded_cp); 7218 } 7219 7220 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, 7221 uint64_t value) 7222 { 7223 /* Helper coprocessor write function for write-ignore registers */ 7224 } 7225 7226 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) 7227 { 7228 /* Helper coprocessor write function for read-as-zero registers */ 7229 return 0; 7230 } 7231 7232 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) 7233 { 7234 /* Helper coprocessor reset function for do-nothing-on-reset registers */ 7235 } 7236 7237 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type) 7238 { 7239 /* Return true if it is not valid for us to switch to 7240 * this CPU mode (ie all the UNPREDICTABLE cases in 7241 * the ARM ARM CPSRWriteByInstr pseudocode). 7242 */ 7243 7244 /* Changes to or from Hyp via MSR and CPS are illegal. */ 7245 if (write_type == CPSRWriteByInstr && 7246 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP || 7247 mode == ARM_CPU_MODE_HYP)) { 7248 return 1; 7249 } 7250 7251 switch (mode) { 7252 case ARM_CPU_MODE_USR: 7253 return 0; 7254 case ARM_CPU_MODE_SYS: 7255 case ARM_CPU_MODE_SVC: 7256 case ARM_CPU_MODE_ABT: 7257 case ARM_CPU_MODE_UND: 7258 case ARM_CPU_MODE_IRQ: 7259 case ARM_CPU_MODE_FIQ: 7260 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7 7261 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.) 7262 */ 7263 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR 7264 * and CPS are treated as illegal mode changes. 7265 */ 7266 if (write_type == CPSRWriteByInstr && 7267 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON && 7268 (arm_hcr_el2_eff(env) & HCR_TGE)) { 7269 return 1; 7270 } 7271 return 0; 7272 case ARM_CPU_MODE_HYP: 7273 return !arm_feature(env, ARM_FEATURE_EL2) 7274 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env); 7275 case ARM_CPU_MODE_MON: 7276 return arm_current_el(env) < 3; 7277 default: 7278 return 1; 7279 } 7280 } 7281 7282 uint32_t cpsr_read(CPUARMState *env) 7283 { 7284 int ZF; 7285 ZF = (env->ZF == 0); 7286 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | 7287 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) 7288 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) 7289 | ((env->condexec_bits & 0xfc) << 8) 7290 | (env->GE << 16) | (env->daif & CPSR_AIF); 7291 } 7292 7293 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask, 7294 CPSRWriteType write_type) 7295 { 7296 uint32_t changed_daif; 7297 7298 if (mask & CPSR_NZCV) { 7299 env->ZF = (~val) & CPSR_Z; 7300 env->NF = val; 7301 env->CF = (val >> 29) & 1; 7302 env->VF = (val << 3) & 0x80000000; 7303 } 7304 if (mask & CPSR_Q) 7305 env->QF = ((val & CPSR_Q) != 0); 7306 if (mask & CPSR_T) 7307 env->thumb = ((val & CPSR_T) != 0); 7308 if (mask & CPSR_IT_0_1) { 7309 env->condexec_bits &= ~3; 7310 env->condexec_bits |= (val >> 25) & 3; 7311 } 7312 if (mask & CPSR_IT_2_7) { 7313 env->condexec_bits &= 3; 7314 env->condexec_bits |= (val >> 8) & 0xfc; 7315 } 7316 if (mask & CPSR_GE) { 7317 env->GE = (val >> 16) & 0xf; 7318 } 7319 7320 /* In a V7 implementation that includes the security extensions but does 7321 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control 7322 * whether non-secure software is allowed to change the CPSR_F and CPSR_A 7323 * bits respectively. 7324 * 7325 * In a V8 implementation, it is permitted for privileged software to 7326 * change the CPSR A/F bits regardless of the SCR.AW/FW bits. 7327 */ 7328 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) && 7329 arm_feature(env, ARM_FEATURE_EL3) && 7330 !arm_feature(env, ARM_FEATURE_EL2) && 7331 !arm_is_secure(env)) { 7332 7333 changed_daif = (env->daif ^ val) & mask; 7334 7335 if (changed_daif & CPSR_A) { 7336 /* Check to see if we are allowed to change the masking of async 7337 * abort exceptions from a non-secure state. 7338 */ 7339 if (!(env->cp15.scr_el3 & SCR_AW)) { 7340 qemu_log_mask(LOG_GUEST_ERROR, 7341 "Ignoring attempt to switch CPSR_A flag from " 7342 "non-secure world with SCR.AW bit clear\n"); 7343 mask &= ~CPSR_A; 7344 } 7345 } 7346 7347 if (changed_daif & CPSR_F) { 7348 /* Check to see if we are allowed to change the masking of FIQ 7349 * exceptions from a non-secure state. 7350 */ 7351 if (!(env->cp15.scr_el3 & SCR_FW)) { 7352 qemu_log_mask(LOG_GUEST_ERROR, 7353 "Ignoring attempt to switch CPSR_F flag from " 7354 "non-secure world with SCR.FW bit clear\n"); 7355 mask &= ~CPSR_F; 7356 } 7357 7358 /* Check whether non-maskable FIQ (NMFI) support is enabled. 7359 * If this bit is set software is not allowed to mask 7360 * FIQs, but is allowed to set CPSR_F to 0. 7361 */ 7362 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && 7363 (val & CPSR_F)) { 7364 qemu_log_mask(LOG_GUEST_ERROR, 7365 "Ignoring attempt to enable CPSR_F flag " 7366 "(non-maskable FIQ [NMFI] support enabled)\n"); 7367 mask &= ~CPSR_F; 7368 } 7369 } 7370 } 7371 7372 env->daif &= ~(CPSR_AIF & mask); 7373 env->daif |= val & CPSR_AIF & mask; 7374 7375 if (write_type != CPSRWriteRaw && 7376 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) { 7377 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) { 7378 /* Note that we can only get here in USR mode if this is a 7379 * gdb stub write; for this case we follow the architectural 7380 * behaviour for guest writes in USR mode of ignoring an attempt 7381 * to switch mode. (Those are caught by translate.c for writes 7382 * triggered by guest instructions.) 7383 */ 7384 mask &= ~CPSR_M; 7385 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) { 7386 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in 7387 * v7, and has defined behaviour in v8: 7388 * + leave CPSR.M untouched 7389 * + allow changes to the other CPSR fields 7390 * + set PSTATE.IL 7391 * For user changes via the GDB stub, we don't set PSTATE.IL, 7392 * as this would be unnecessarily harsh for a user error. 7393 */ 7394 mask &= ~CPSR_M; 7395 if (write_type != CPSRWriteByGDBStub && 7396 arm_feature(env, ARM_FEATURE_V8)) { 7397 mask |= CPSR_IL; 7398 val |= CPSR_IL; 7399 } 7400 qemu_log_mask(LOG_GUEST_ERROR, 7401 "Illegal AArch32 mode switch attempt from %s to %s\n", 7402 aarch32_mode_name(env->uncached_cpsr), 7403 aarch32_mode_name(val)); 7404 } else { 7405 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n", 7406 write_type == CPSRWriteExceptionReturn ? 7407 "Exception return from AArch32" : 7408 "AArch32 mode switch from", 7409 aarch32_mode_name(env->uncached_cpsr), 7410 aarch32_mode_name(val), env->regs[15]); 7411 switch_mode(env, val & CPSR_M); 7412 } 7413 } 7414 mask &= ~CACHED_CPSR_BITS; 7415 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); 7416 } 7417 7418 /* Sign/zero extend */ 7419 uint32_t HELPER(sxtb16)(uint32_t x) 7420 { 7421 uint32_t res; 7422 res = (uint16_t)(int8_t)x; 7423 res |= (uint32_t)(int8_t)(x >> 16) << 16; 7424 return res; 7425 } 7426 7427 uint32_t HELPER(uxtb16)(uint32_t x) 7428 { 7429 uint32_t res; 7430 res = (uint16_t)(uint8_t)x; 7431 res |= (uint32_t)(uint8_t)(x >> 16) << 16; 7432 return res; 7433 } 7434 7435 int32_t HELPER(sdiv)(int32_t num, int32_t den) 7436 { 7437 if (den == 0) 7438 return 0; 7439 if (num == INT_MIN && den == -1) 7440 return INT_MIN; 7441 return num / den; 7442 } 7443 7444 uint32_t HELPER(udiv)(uint32_t num, uint32_t den) 7445 { 7446 if (den == 0) 7447 return 0; 7448 return num / den; 7449 } 7450 7451 uint32_t HELPER(rbit)(uint32_t x) 7452 { 7453 return revbit32(x); 7454 } 7455 7456 #ifdef CONFIG_USER_ONLY 7457 7458 /* These should probably raise undefined insn exceptions. */ 7459 void HELPER(v7m_msr)(CPUARMState *env, uint32_t reg, uint32_t val) 7460 { 7461 ARMCPU *cpu = env_archcpu(env); 7462 7463 cpu_abort(CPU(cpu), "v7m_msr %d\n", reg); 7464 } 7465 7466 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) 7467 { 7468 ARMCPU *cpu = env_archcpu(env); 7469 7470 cpu_abort(CPU(cpu), "v7m_mrs %d\n", reg); 7471 return 0; 7472 } 7473 7474 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) 7475 { 7476 /* translate.c should never generate calls here in user-only mode */ 7477 g_assert_not_reached(); 7478 } 7479 7480 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) 7481 { 7482 /* translate.c should never generate calls here in user-only mode */ 7483 g_assert_not_reached(); 7484 } 7485 7486 void HELPER(v7m_preserve_fp_state)(CPUARMState *env) 7487 { 7488 /* translate.c should never generate calls here in user-only mode */ 7489 g_assert_not_reached(); 7490 } 7491 7492 void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr) 7493 { 7494 /* translate.c should never generate calls here in user-only mode */ 7495 g_assert_not_reached(); 7496 } 7497 7498 void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr) 7499 { 7500 /* translate.c should never generate calls here in user-only mode */ 7501 g_assert_not_reached(); 7502 } 7503 7504 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) 7505 { 7506 /* 7507 * The TT instructions can be used by unprivileged code, but in 7508 * user-only emulation we don't have the MPU. 7509 * Luckily since we know we are NonSecure unprivileged (and that in 7510 * turn means that the A flag wasn't specified), all the bits in the 7511 * register must be zero: 7512 * IREGION: 0 because IRVALID is 0 7513 * IRVALID: 0 because NS 7514 * S: 0 because NS 7515 * NSRW: 0 because NS 7516 * NSR: 0 because NS 7517 * RW: 0 because unpriv and A flag not set 7518 * R: 0 because unpriv and A flag not set 7519 * SRVALID: 0 because NS 7520 * MRVALID: 0 because unpriv and A flag not set 7521 * SREGION: 0 becaus SRVALID is 0 7522 * MREGION: 0 because MRVALID is 0 7523 */ 7524 return 0; 7525 } 7526 7527 static void switch_mode(CPUARMState *env, int mode) 7528 { 7529 ARMCPU *cpu = env_archcpu(env); 7530 7531 if (mode != ARM_CPU_MODE_USR) { 7532 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); 7533 } 7534 } 7535 7536 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 7537 uint32_t cur_el, bool secure) 7538 { 7539 return 1; 7540 } 7541 7542 void aarch64_sync_64_to_32(CPUARMState *env) 7543 { 7544 g_assert_not_reached(); 7545 } 7546 7547 #else 7548 7549 static void switch_mode(CPUARMState *env, int mode) 7550 { 7551 int old_mode; 7552 int i; 7553 7554 old_mode = env->uncached_cpsr & CPSR_M; 7555 if (mode == old_mode) 7556 return; 7557 7558 if (old_mode == ARM_CPU_MODE_FIQ) { 7559 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); 7560 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); 7561 } else if (mode == ARM_CPU_MODE_FIQ) { 7562 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); 7563 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); 7564 } 7565 7566 i = bank_number(old_mode); 7567 env->banked_r13[i] = env->regs[13]; 7568 env->banked_spsr[i] = env->spsr; 7569 7570 i = bank_number(mode); 7571 env->regs[13] = env->banked_r13[i]; 7572 env->spsr = env->banked_spsr[i]; 7573 7574 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14]; 7575 env->regs[14] = env->banked_r14[r14_bank_number(mode)]; 7576 } 7577 7578 /* Physical Interrupt Target EL Lookup Table 7579 * 7580 * [ From ARM ARM section G1.13.4 (Table G1-15) ] 7581 * 7582 * The below multi-dimensional table is used for looking up the target 7583 * exception level given numerous condition criteria. Specifically, the 7584 * target EL is based on SCR and HCR routing controls as well as the 7585 * currently executing EL and secure state. 7586 * 7587 * Dimensions: 7588 * target_el_table[2][2][2][2][2][4] 7589 * | | | | | +--- Current EL 7590 * | | | | +------ Non-secure(0)/Secure(1) 7591 * | | | +--------- HCR mask override 7592 * | | +------------ SCR exec state control 7593 * | +--------------- SCR mask override 7594 * +------------------ 32-bit(0)/64-bit(1) EL3 7595 * 7596 * The table values are as such: 7597 * 0-3 = EL0-EL3 7598 * -1 = Cannot occur 7599 * 7600 * The ARM ARM target EL table includes entries indicating that an "exception 7601 * is not taken". The two cases where this is applicable are: 7602 * 1) An exception is taken from EL3 but the SCR does not have the exception 7603 * routed to EL3. 7604 * 2) An exception is taken from EL2 but the HCR does not have the exception 7605 * routed to EL2. 7606 * In these two cases, the below table contain a target of EL1. This value is 7607 * returned as it is expected that the consumer of the table data will check 7608 * for "target EL >= current EL" to ensure the exception is not taken. 7609 * 7610 * SCR HCR 7611 * 64 EA AMO From 7612 * BIT IRQ IMO Non-secure Secure 7613 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 7614 */ 7615 static const int8_t target_el_table[2][2][2][2][2][4] = { 7616 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 7617 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, 7618 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 7619 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, 7620 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 7621 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, 7622 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 7623 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, 7624 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, 7625 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},}, 7626 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },}, 7627 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},}, 7628 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 7629 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, 7630 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 7631 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},}, 7632 }; 7633 7634 /* 7635 * Determine the target EL for physical exceptions 7636 */ 7637 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 7638 uint32_t cur_el, bool secure) 7639 { 7640 CPUARMState *env = cs->env_ptr; 7641 bool rw; 7642 bool scr; 7643 bool hcr; 7644 int target_el; 7645 /* Is the highest EL AArch64? */ 7646 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64); 7647 uint64_t hcr_el2; 7648 7649 if (arm_feature(env, ARM_FEATURE_EL3)) { 7650 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW); 7651 } else { 7652 /* Either EL2 is the highest EL (and so the EL2 register width 7653 * is given by is64); or there is no EL2 or EL3, in which case 7654 * the value of 'rw' does not affect the table lookup anyway. 7655 */ 7656 rw = is64; 7657 } 7658 7659 hcr_el2 = arm_hcr_el2_eff(env); 7660 switch (excp_idx) { 7661 case EXCP_IRQ: 7662 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); 7663 hcr = hcr_el2 & HCR_IMO; 7664 break; 7665 case EXCP_FIQ: 7666 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); 7667 hcr = hcr_el2 & HCR_FMO; 7668 break; 7669 default: 7670 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); 7671 hcr = hcr_el2 & HCR_AMO; 7672 break; 7673 }; 7674 7675 /* Perform a table-lookup for the target EL given the current state */ 7676 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; 7677 7678 assert(target_el > 0); 7679 7680 return target_el; 7681 } 7682 7683 void arm_log_exception(int idx) 7684 { 7685 if (qemu_loglevel_mask(CPU_LOG_INT)) { 7686 const char *exc = NULL; 7687 static const char * const excnames[] = { 7688 [EXCP_UDEF] = "Undefined Instruction", 7689 [EXCP_SWI] = "SVC", 7690 [EXCP_PREFETCH_ABORT] = "Prefetch Abort", 7691 [EXCP_DATA_ABORT] = "Data Abort", 7692 [EXCP_IRQ] = "IRQ", 7693 [EXCP_FIQ] = "FIQ", 7694 [EXCP_BKPT] = "Breakpoint", 7695 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit", 7696 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage", 7697 [EXCP_HVC] = "Hypervisor Call", 7698 [EXCP_HYP_TRAP] = "Hypervisor Trap", 7699 [EXCP_SMC] = "Secure Monitor Call", 7700 [EXCP_VIRQ] = "Virtual IRQ", 7701 [EXCP_VFIQ] = "Virtual FIQ", 7702 [EXCP_SEMIHOST] = "Semihosting call", 7703 [EXCP_NOCP] = "v7M NOCP UsageFault", 7704 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault", 7705 [EXCP_STKOF] = "v8M STKOF UsageFault", 7706 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking", 7707 [EXCP_LSERR] = "v8M LSERR UsageFault", 7708 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault", 7709 }; 7710 7711 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { 7712 exc = excnames[idx]; 7713 } 7714 if (!exc) { 7715 exc = "unknown"; 7716 } 7717 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc); 7718 } 7719 } 7720 7721 /* 7722 * What kind of stack write are we doing? This affects how exceptions 7723 * generated during the stacking are treated. 7724 */ 7725 typedef enum StackingMode { 7726 STACK_NORMAL, 7727 STACK_IGNFAULTS, 7728 STACK_LAZYFP, 7729 } StackingMode; 7730 7731 static bool v7m_stack_write(ARMCPU *cpu, uint32_t addr, uint32_t value, 7732 ARMMMUIdx mmu_idx, StackingMode mode) 7733 { 7734 CPUState *cs = CPU(cpu); 7735 CPUARMState *env = &cpu->env; 7736 MemTxAttrs attrs = {}; 7737 MemTxResult txres; 7738 target_ulong page_size; 7739 hwaddr physaddr; 7740 int prot; 7741 ARMMMUFaultInfo fi = {}; 7742 bool secure = mmu_idx & ARM_MMU_IDX_M_S; 7743 int exc; 7744 bool exc_secure; 7745 7746 if (get_phys_addr(env, addr, MMU_DATA_STORE, mmu_idx, &physaddr, 7747 &attrs, &prot, &page_size, &fi, NULL)) { 7748 /* MPU/SAU lookup failed */ 7749 if (fi.type == ARMFault_QEMU_SFault) { 7750 if (mode == STACK_LAZYFP) { 7751 qemu_log_mask(CPU_LOG_INT, 7752 "...SecureFault with SFSR.LSPERR " 7753 "during lazy stacking\n"); 7754 env->v7m.sfsr |= R_V7M_SFSR_LSPERR_MASK; 7755 } else { 7756 qemu_log_mask(CPU_LOG_INT, 7757 "...SecureFault with SFSR.AUVIOL " 7758 "during stacking\n"); 7759 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK; 7760 } 7761 env->v7m.sfsr |= R_V7M_SFSR_SFARVALID_MASK; 7762 env->v7m.sfar = addr; 7763 exc = ARMV7M_EXCP_SECURE; 7764 exc_secure = false; 7765 } else { 7766 if (mode == STACK_LAZYFP) { 7767 qemu_log_mask(CPU_LOG_INT, 7768 "...MemManageFault with CFSR.MLSPERR\n"); 7769 env->v7m.cfsr[secure] |= R_V7M_CFSR_MLSPERR_MASK; 7770 } else { 7771 qemu_log_mask(CPU_LOG_INT, 7772 "...MemManageFault with CFSR.MSTKERR\n"); 7773 env->v7m.cfsr[secure] |= R_V7M_CFSR_MSTKERR_MASK; 7774 } 7775 exc = ARMV7M_EXCP_MEM; 7776 exc_secure = secure; 7777 } 7778 goto pend_fault; 7779 } 7780 address_space_stl_le(arm_addressspace(cs, attrs), physaddr, value, 7781 attrs, &txres); 7782 if (txres != MEMTX_OK) { 7783 /* BusFault trying to write the data */ 7784 if (mode == STACK_LAZYFP) { 7785 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.LSPERR\n"); 7786 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_LSPERR_MASK; 7787 } else { 7788 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.STKERR\n"); 7789 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_STKERR_MASK; 7790 } 7791 exc = ARMV7M_EXCP_BUS; 7792 exc_secure = false; 7793 goto pend_fault; 7794 } 7795 return true; 7796 7797 pend_fault: 7798 /* 7799 * By pending the exception at this point we are making 7800 * the IMPDEF choice "overridden exceptions pended" (see the 7801 * MergeExcInfo() pseudocode). The other choice would be to not 7802 * pend them now and then make a choice about which to throw away 7803 * later if we have two derived exceptions. 7804 * The only case when we must not pend the exception but instead 7805 * throw it away is if we are doing the push of the callee registers 7806 * and we've already generated a derived exception (this is indicated 7807 * by the caller passing STACK_IGNFAULTS). Even in this case we will 7808 * still update the fault status registers. 7809 */ 7810 switch (mode) { 7811 case STACK_NORMAL: 7812 armv7m_nvic_set_pending_derived(env->nvic, exc, exc_secure); 7813 break; 7814 case STACK_LAZYFP: 7815 armv7m_nvic_set_pending_lazyfp(env->nvic, exc, exc_secure); 7816 break; 7817 case STACK_IGNFAULTS: 7818 break; 7819 } 7820 return false; 7821 } 7822 7823 static bool v7m_stack_read(ARMCPU *cpu, uint32_t *dest, uint32_t addr, 7824 ARMMMUIdx mmu_idx) 7825 { 7826 CPUState *cs = CPU(cpu); 7827 CPUARMState *env = &cpu->env; 7828 MemTxAttrs attrs = {}; 7829 MemTxResult txres; 7830 target_ulong page_size; 7831 hwaddr physaddr; 7832 int prot; 7833 ARMMMUFaultInfo fi = {}; 7834 bool secure = mmu_idx & ARM_MMU_IDX_M_S; 7835 int exc; 7836 bool exc_secure; 7837 uint32_t value; 7838 7839 if (get_phys_addr(env, addr, MMU_DATA_LOAD, mmu_idx, &physaddr, 7840 &attrs, &prot, &page_size, &fi, NULL)) { 7841 /* MPU/SAU lookup failed */ 7842 if (fi.type == ARMFault_QEMU_SFault) { 7843 qemu_log_mask(CPU_LOG_INT, 7844 "...SecureFault with SFSR.AUVIOL during unstack\n"); 7845 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK | R_V7M_SFSR_SFARVALID_MASK; 7846 env->v7m.sfar = addr; 7847 exc = ARMV7M_EXCP_SECURE; 7848 exc_secure = false; 7849 } else { 7850 qemu_log_mask(CPU_LOG_INT, 7851 "...MemManageFault with CFSR.MUNSTKERR\n"); 7852 env->v7m.cfsr[secure] |= R_V7M_CFSR_MUNSTKERR_MASK; 7853 exc = ARMV7M_EXCP_MEM; 7854 exc_secure = secure; 7855 } 7856 goto pend_fault; 7857 } 7858 7859 value = address_space_ldl(arm_addressspace(cs, attrs), physaddr, 7860 attrs, &txres); 7861 if (txres != MEMTX_OK) { 7862 /* BusFault trying to read the data */ 7863 qemu_log_mask(CPU_LOG_INT, "...BusFault with BFSR.UNSTKERR\n"); 7864 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_UNSTKERR_MASK; 7865 exc = ARMV7M_EXCP_BUS; 7866 exc_secure = false; 7867 goto pend_fault; 7868 } 7869 7870 *dest = value; 7871 return true; 7872 7873 pend_fault: 7874 /* 7875 * By pending the exception at this point we are making 7876 * the IMPDEF choice "overridden exceptions pended" (see the 7877 * MergeExcInfo() pseudocode). The other choice would be to not 7878 * pend them now and then make a choice about which to throw away 7879 * later if we have two derived exceptions. 7880 */ 7881 armv7m_nvic_set_pending(env->nvic, exc, exc_secure); 7882 return false; 7883 } 7884 7885 void HELPER(v7m_preserve_fp_state)(CPUARMState *env) 7886 { 7887 /* 7888 * Preserve FP state (because LSPACT was set and we are about 7889 * to execute an FP instruction). This corresponds to the 7890 * PreserveFPState() pseudocode. 7891 * We may throw an exception if the stacking fails. 7892 */ 7893 ARMCPU *cpu = env_archcpu(env); 7894 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 7895 bool negpri = !(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_HFRDY_MASK); 7896 bool is_priv = !(env->v7m.fpccr[is_secure] & R_V7M_FPCCR_USER_MASK); 7897 bool splimviol = env->v7m.fpccr[is_secure] & R_V7M_FPCCR_SPLIMVIOL_MASK; 7898 uint32_t fpcar = env->v7m.fpcar[is_secure]; 7899 bool stacked_ok = true; 7900 bool ts = is_secure && (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK); 7901 bool take_exception; 7902 7903 /* Take the iothread lock as we are going to touch the NVIC */ 7904 qemu_mutex_lock_iothread(); 7905 7906 /* Check the background context had access to the FPU */ 7907 if (!v7m_cpacr_pass(env, is_secure, is_priv)) { 7908 armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, is_secure); 7909 env->v7m.cfsr[is_secure] |= R_V7M_CFSR_NOCP_MASK; 7910 stacked_ok = false; 7911 } else if (!is_secure && !extract32(env->v7m.nsacr, 10, 1)) { 7912 armv7m_nvic_set_pending_lazyfp(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S); 7913 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK; 7914 stacked_ok = false; 7915 } 7916 7917 if (!splimviol && stacked_ok) { 7918 /* We only stack if the stack limit wasn't violated */ 7919 int i; 7920 ARMMMUIdx mmu_idx; 7921 7922 mmu_idx = arm_v7m_mmu_idx_all(env, is_secure, is_priv, negpri); 7923 for (i = 0; i < (ts ? 32 : 16); i += 2) { 7924 uint64_t dn = *aa32_vfp_dreg(env, i / 2); 7925 uint32_t faddr = fpcar + 4 * i; 7926 uint32_t slo = extract64(dn, 0, 32); 7927 uint32_t shi = extract64(dn, 32, 32); 7928 7929 if (i >= 16) { 7930 faddr += 8; /* skip the slot for the FPSCR */ 7931 } 7932 stacked_ok = stacked_ok && 7933 v7m_stack_write(cpu, faddr, slo, mmu_idx, STACK_LAZYFP) && 7934 v7m_stack_write(cpu, faddr + 4, shi, mmu_idx, STACK_LAZYFP); 7935 } 7936 7937 stacked_ok = stacked_ok && 7938 v7m_stack_write(cpu, fpcar + 0x40, 7939 vfp_get_fpscr(env), mmu_idx, STACK_LAZYFP); 7940 } 7941 7942 /* 7943 * We definitely pended an exception, but it's possible that it 7944 * might not be able to be taken now. If its priority permits us 7945 * to take it now, then we must not update the LSPACT or FP regs, 7946 * but instead jump out to take the exception immediately. 7947 * If it's just pending and won't be taken until the current 7948 * handler exits, then we do update LSPACT and the FP regs. 7949 */ 7950 take_exception = !stacked_ok && 7951 armv7m_nvic_can_take_pending_exception(env->nvic); 7952 7953 qemu_mutex_unlock_iothread(); 7954 7955 if (take_exception) { 7956 raise_exception_ra(env, EXCP_LAZYFP, 0, 1, GETPC()); 7957 } 7958 7959 env->v7m.fpccr[is_secure] &= ~R_V7M_FPCCR_LSPACT_MASK; 7960 7961 if (ts) { 7962 /* Clear s0 to s31 and the FPSCR */ 7963 int i; 7964 7965 for (i = 0; i < 32; i += 2) { 7966 *aa32_vfp_dreg(env, i / 2) = 0; 7967 } 7968 vfp_set_fpscr(env, 0); 7969 } 7970 /* 7971 * Otherwise s0 to s15 and FPSCR are UNKNOWN; we choose to leave them 7972 * unchanged. 7973 */ 7974 } 7975 7976 /* 7977 * Write to v7M CONTROL.SPSEL bit for the specified security bank. 7978 * This may change the current stack pointer between Main and Process 7979 * stack pointers if it is done for the CONTROL register for the current 7980 * security state. 7981 */ 7982 static void write_v7m_control_spsel_for_secstate(CPUARMState *env, 7983 bool new_spsel, 7984 bool secstate) 7985 { 7986 bool old_is_psp = v7m_using_psp(env); 7987 7988 env->v7m.control[secstate] = 7989 deposit32(env->v7m.control[secstate], 7990 R_V7M_CONTROL_SPSEL_SHIFT, 7991 R_V7M_CONTROL_SPSEL_LENGTH, new_spsel); 7992 7993 if (secstate == env->v7m.secure) { 7994 bool new_is_psp = v7m_using_psp(env); 7995 uint32_t tmp; 7996 7997 if (old_is_psp != new_is_psp) { 7998 tmp = env->v7m.other_sp; 7999 env->v7m.other_sp = env->regs[13]; 8000 env->regs[13] = tmp; 8001 } 8002 } 8003 } 8004 8005 /* 8006 * Write to v7M CONTROL.SPSEL bit. This may change the current 8007 * stack pointer between Main and Process stack pointers. 8008 */ 8009 static void write_v7m_control_spsel(CPUARMState *env, bool new_spsel) 8010 { 8011 write_v7m_control_spsel_for_secstate(env, new_spsel, env->v7m.secure); 8012 } 8013 8014 void write_v7m_exception(CPUARMState *env, uint32_t new_exc) 8015 { 8016 /* 8017 * Write a new value to v7m.exception, thus transitioning into or out 8018 * of Handler mode; this may result in a change of active stack pointer. 8019 */ 8020 bool new_is_psp, old_is_psp = v7m_using_psp(env); 8021 uint32_t tmp; 8022 8023 env->v7m.exception = new_exc; 8024 8025 new_is_psp = v7m_using_psp(env); 8026 8027 if (old_is_psp != new_is_psp) { 8028 tmp = env->v7m.other_sp; 8029 env->v7m.other_sp = env->regs[13]; 8030 env->regs[13] = tmp; 8031 } 8032 } 8033 8034 /* Switch M profile security state between NS and S */ 8035 static void switch_v7m_security_state(CPUARMState *env, bool new_secstate) 8036 { 8037 uint32_t new_ss_msp, new_ss_psp; 8038 8039 if (env->v7m.secure == new_secstate) { 8040 return; 8041 } 8042 8043 /* 8044 * All the banked state is accessed by looking at env->v7m.secure 8045 * except for the stack pointer; rearrange the SP appropriately. 8046 */ 8047 new_ss_msp = env->v7m.other_ss_msp; 8048 new_ss_psp = env->v7m.other_ss_psp; 8049 8050 if (v7m_using_psp(env)) { 8051 env->v7m.other_ss_psp = env->regs[13]; 8052 env->v7m.other_ss_msp = env->v7m.other_sp; 8053 } else { 8054 env->v7m.other_ss_msp = env->regs[13]; 8055 env->v7m.other_ss_psp = env->v7m.other_sp; 8056 } 8057 8058 env->v7m.secure = new_secstate; 8059 8060 if (v7m_using_psp(env)) { 8061 env->regs[13] = new_ss_psp; 8062 env->v7m.other_sp = new_ss_msp; 8063 } else { 8064 env->regs[13] = new_ss_msp; 8065 env->v7m.other_sp = new_ss_psp; 8066 } 8067 } 8068 8069 void HELPER(v7m_bxns)(CPUARMState *env, uint32_t dest) 8070 { 8071 /* 8072 * Handle v7M BXNS: 8073 * - if the return value is a magic value, do exception return (like BX) 8074 * - otherwise bit 0 of the return value is the target security state 8075 */ 8076 uint32_t min_magic; 8077 8078 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8079 /* Covers FNC_RETURN and EXC_RETURN magic */ 8080 min_magic = FNC_RETURN_MIN_MAGIC; 8081 } else { 8082 /* EXC_RETURN magic only */ 8083 min_magic = EXC_RETURN_MIN_MAGIC; 8084 } 8085 8086 if (dest >= min_magic) { 8087 /* 8088 * This is an exception return magic value; put it where 8089 * do_v7m_exception_exit() expects and raise EXCEPTION_EXIT. 8090 * Note that if we ever add gen_ss_advance() singlestep support to 8091 * M profile this should count as an "instruction execution complete" 8092 * event (compare gen_bx_excret_final_code()). 8093 */ 8094 env->regs[15] = dest & ~1; 8095 env->thumb = dest & 1; 8096 HELPER(exception_internal)(env, EXCP_EXCEPTION_EXIT); 8097 /* notreached */ 8098 } 8099 8100 /* translate.c should have made BXNS UNDEF unless we're secure */ 8101 assert(env->v7m.secure); 8102 8103 if (!(dest & 1)) { 8104 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 8105 } 8106 switch_v7m_security_state(env, dest & 1); 8107 env->thumb = 1; 8108 env->regs[15] = dest & ~1; 8109 } 8110 8111 void HELPER(v7m_blxns)(CPUARMState *env, uint32_t dest) 8112 { 8113 /* 8114 * Handle v7M BLXNS: 8115 * - bit 0 of the destination address is the target security state 8116 */ 8117 8118 /* At this point regs[15] is the address just after the BLXNS */ 8119 uint32_t nextinst = env->regs[15] | 1; 8120 uint32_t sp = env->regs[13] - 8; 8121 uint32_t saved_psr; 8122 8123 /* translate.c will have made BLXNS UNDEF unless we're secure */ 8124 assert(env->v7m.secure); 8125 8126 if (dest & 1) { 8127 /* 8128 * Target is Secure, so this is just a normal BLX, 8129 * except that the low bit doesn't indicate Thumb/not. 8130 */ 8131 env->regs[14] = nextinst; 8132 env->thumb = 1; 8133 env->regs[15] = dest & ~1; 8134 return; 8135 } 8136 8137 /* Target is non-secure: first push a stack frame */ 8138 if (!QEMU_IS_ALIGNED(sp, 8)) { 8139 qemu_log_mask(LOG_GUEST_ERROR, 8140 "BLXNS with misaligned SP is UNPREDICTABLE\n"); 8141 } 8142 8143 if (sp < v7m_sp_limit(env)) { 8144 raise_exception(env, EXCP_STKOF, 0, 1); 8145 } 8146 8147 saved_psr = env->v7m.exception; 8148 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK) { 8149 saved_psr |= XPSR_SFPA; 8150 } 8151 8152 /* Note that these stores can throw exceptions on MPU faults */ 8153 cpu_stl_data(env, sp, nextinst); 8154 cpu_stl_data(env, sp + 4, saved_psr); 8155 8156 env->regs[13] = sp; 8157 env->regs[14] = 0xfeffffff; 8158 if (arm_v7m_is_handler_mode(env)) { 8159 /* 8160 * Write a dummy value to IPSR, to avoid leaking the current secure 8161 * exception number to non-secure code. This is guaranteed not 8162 * to cause write_v7m_exception() to actually change stacks. 8163 */ 8164 write_v7m_exception(env, 1); 8165 } 8166 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 8167 switch_v7m_security_state(env, 0); 8168 env->thumb = 1; 8169 env->regs[15] = dest; 8170 } 8171 8172 static uint32_t *get_v7m_sp_ptr(CPUARMState *env, bool secure, bool threadmode, 8173 bool spsel) 8174 { 8175 /* 8176 * Return a pointer to the location where we currently store the 8177 * stack pointer for the requested security state and thread mode. 8178 * This pointer will become invalid if the CPU state is updated 8179 * such that the stack pointers are switched around (eg changing 8180 * the SPSEL control bit). 8181 * Compare the v8M ARM ARM pseudocode LookUpSP_with_security_mode(). 8182 * Unlike that pseudocode, we require the caller to pass us in the 8183 * SPSEL control bit value; this is because we also use this 8184 * function in handling of pushing of the callee-saves registers 8185 * part of the v8M stack frame (pseudocode PushCalleeStack()), 8186 * and in the tailchain codepath the SPSEL bit comes from the exception 8187 * return magic LR value from the previous exception. The pseudocode 8188 * opencodes the stack-selection in PushCalleeStack(), but we prefer 8189 * to make this utility function generic enough to do the job. 8190 */ 8191 bool want_psp = threadmode && spsel; 8192 8193 if (secure == env->v7m.secure) { 8194 if (want_psp == v7m_using_psp(env)) { 8195 return &env->regs[13]; 8196 } else { 8197 return &env->v7m.other_sp; 8198 } 8199 } else { 8200 if (want_psp) { 8201 return &env->v7m.other_ss_psp; 8202 } else { 8203 return &env->v7m.other_ss_msp; 8204 } 8205 } 8206 } 8207 8208 static bool arm_v7m_load_vector(ARMCPU *cpu, int exc, bool targets_secure, 8209 uint32_t *pvec) 8210 { 8211 CPUState *cs = CPU(cpu); 8212 CPUARMState *env = &cpu->env; 8213 MemTxResult result; 8214 uint32_t addr = env->v7m.vecbase[targets_secure] + exc * 4; 8215 uint32_t vector_entry; 8216 MemTxAttrs attrs = {}; 8217 ARMMMUIdx mmu_idx; 8218 bool exc_secure; 8219 8220 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targets_secure, true); 8221 8222 /* 8223 * We don't do a get_phys_addr() here because the rules for vector 8224 * loads are special: they always use the default memory map, and 8225 * the default memory map permits reads from all addresses. 8226 * Since there's no easy way to pass through to pmsav8_mpu_lookup() 8227 * that we want this special case which would always say "yes", 8228 * we just do the SAU lookup here followed by a direct physical load. 8229 */ 8230 attrs.secure = targets_secure; 8231 attrs.user = false; 8232 8233 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8234 V8M_SAttributes sattrs = {}; 8235 8236 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); 8237 if (sattrs.ns) { 8238 attrs.secure = false; 8239 } else if (!targets_secure) { 8240 /* NS access to S memory */ 8241 goto load_fail; 8242 } 8243 } 8244 8245 vector_entry = address_space_ldl(arm_addressspace(cs, attrs), addr, 8246 attrs, &result); 8247 if (result != MEMTX_OK) { 8248 goto load_fail; 8249 } 8250 *pvec = vector_entry; 8251 return true; 8252 8253 load_fail: 8254 /* 8255 * All vector table fetch fails are reported as HardFault, with 8256 * HFSR.VECTTBL and .FORCED set. (FORCED is set because 8257 * technically the underlying exception is a MemManage or BusFault 8258 * that is escalated to HardFault.) This is a terminal exception, 8259 * so we will either take the HardFault immediately or else enter 8260 * lockup (the latter case is handled in armv7m_nvic_set_pending_derived()). 8261 */ 8262 exc_secure = targets_secure || 8263 !(cpu->env.v7m.aircr & R_V7M_AIRCR_BFHFNMINS_MASK); 8264 env->v7m.hfsr |= R_V7M_HFSR_VECTTBL_MASK | R_V7M_HFSR_FORCED_MASK; 8265 armv7m_nvic_set_pending_derived(env->nvic, ARMV7M_EXCP_HARD, exc_secure); 8266 return false; 8267 } 8268 8269 static uint32_t v7m_integrity_sig(CPUARMState *env, uint32_t lr) 8270 { 8271 /* 8272 * Return the integrity signature value for the callee-saves 8273 * stack frame section. @lr is the exception return payload/LR value 8274 * whose FType bit forms bit 0 of the signature if FP is present. 8275 */ 8276 uint32_t sig = 0xfefa125a; 8277 8278 if (!arm_feature(env, ARM_FEATURE_VFP) || (lr & R_V7M_EXCRET_FTYPE_MASK)) { 8279 sig |= 1; 8280 } 8281 return sig; 8282 } 8283 8284 static bool v7m_push_callee_stack(ARMCPU *cpu, uint32_t lr, bool dotailchain, 8285 bool ignore_faults) 8286 { 8287 /* 8288 * For v8M, push the callee-saves register part of the stack frame. 8289 * Compare the v8M pseudocode PushCalleeStack(). 8290 * In the tailchaining case this may not be the current stack. 8291 */ 8292 CPUARMState *env = &cpu->env; 8293 uint32_t *frame_sp_p; 8294 uint32_t frameptr; 8295 ARMMMUIdx mmu_idx; 8296 bool stacked_ok; 8297 uint32_t limit; 8298 bool want_psp; 8299 uint32_t sig; 8300 StackingMode smode = ignore_faults ? STACK_IGNFAULTS : STACK_NORMAL; 8301 8302 if (dotailchain) { 8303 bool mode = lr & R_V7M_EXCRET_MODE_MASK; 8304 bool priv = !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_NPRIV_MASK) || 8305 !mode; 8306 8307 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, M_REG_S, priv); 8308 frame_sp_p = get_v7m_sp_ptr(env, M_REG_S, mode, 8309 lr & R_V7M_EXCRET_SPSEL_MASK); 8310 want_psp = mode && (lr & R_V7M_EXCRET_SPSEL_MASK); 8311 if (want_psp) { 8312 limit = env->v7m.psplim[M_REG_S]; 8313 } else { 8314 limit = env->v7m.msplim[M_REG_S]; 8315 } 8316 } else { 8317 mmu_idx = arm_mmu_idx(env); 8318 frame_sp_p = &env->regs[13]; 8319 limit = v7m_sp_limit(env); 8320 } 8321 8322 frameptr = *frame_sp_p - 0x28; 8323 if (frameptr < limit) { 8324 /* 8325 * Stack limit failure: set SP to the limit value, and generate 8326 * STKOF UsageFault. Stack pushes below the limit must not be 8327 * performed. It is IMPDEF whether pushes above the limit are 8328 * performed; we choose not to. 8329 */ 8330 qemu_log_mask(CPU_LOG_INT, 8331 "...STKOF during callee-saves register stacking\n"); 8332 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 8333 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 8334 env->v7m.secure); 8335 *frame_sp_p = limit; 8336 return true; 8337 } 8338 8339 /* 8340 * Write as much of the stack frame as we can. A write failure may 8341 * cause us to pend a derived exception. 8342 */ 8343 sig = v7m_integrity_sig(env, lr); 8344 stacked_ok = 8345 v7m_stack_write(cpu, frameptr, sig, mmu_idx, smode) && 8346 v7m_stack_write(cpu, frameptr + 0x8, env->regs[4], mmu_idx, smode) && 8347 v7m_stack_write(cpu, frameptr + 0xc, env->regs[5], mmu_idx, smode) && 8348 v7m_stack_write(cpu, frameptr + 0x10, env->regs[6], mmu_idx, smode) && 8349 v7m_stack_write(cpu, frameptr + 0x14, env->regs[7], mmu_idx, smode) && 8350 v7m_stack_write(cpu, frameptr + 0x18, env->regs[8], mmu_idx, smode) && 8351 v7m_stack_write(cpu, frameptr + 0x1c, env->regs[9], mmu_idx, smode) && 8352 v7m_stack_write(cpu, frameptr + 0x20, env->regs[10], mmu_idx, smode) && 8353 v7m_stack_write(cpu, frameptr + 0x24, env->regs[11], mmu_idx, smode); 8354 8355 /* Update SP regardless of whether any of the stack accesses failed. */ 8356 *frame_sp_p = frameptr; 8357 8358 return !stacked_ok; 8359 } 8360 8361 static void v7m_exception_taken(ARMCPU *cpu, uint32_t lr, bool dotailchain, 8362 bool ignore_stackfaults) 8363 { 8364 /* 8365 * Do the "take the exception" parts of exception entry, 8366 * but not the pushing of state to the stack. This is 8367 * similar to the pseudocode ExceptionTaken() function. 8368 */ 8369 CPUARMState *env = &cpu->env; 8370 uint32_t addr; 8371 bool targets_secure; 8372 int exc; 8373 bool push_failed = false; 8374 8375 armv7m_nvic_get_pending_irq_info(env->nvic, &exc, &targets_secure); 8376 qemu_log_mask(CPU_LOG_INT, "...taking pending %s exception %d\n", 8377 targets_secure ? "secure" : "nonsecure", exc); 8378 8379 if (dotailchain) { 8380 /* Sanitize LR FType and PREFIX bits */ 8381 if (!arm_feature(env, ARM_FEATURE_VFP)) { 8382 lr |= R_V7M_EXCRET_FTYPE_MASK; 8383 } 8384 lr = deposit32(lr, 24, 8, 0xff); 8385 } 8386 8387 if (arm_feature(env, ARM_FEATURE_V8)) { 8388 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 8389 (lr & R_V7M_EXCRET_S_MASK)) { 8390 /* 8391 * The background code (the owner of the registers in the 8392 * exception frame) is Secure. This means it may either already 8393 * have or now needs to push callee-saves registers. 8394 */ 8395 if (targets_secure) { 8396 if (dotailchain && !(lr & R_V7M_EXCRET_ES_MASK)) { 8397 /* 8398 * We took an exception from Secure to NonSecure 8399 * (which means the callee-saved registers got stacked) 8400 * and are now tailchaining to a Secure exception. 8401 * Clear DCRS so eventual return from this Secure 8402 * exception unstacks the callee-saved registers. 8403 */ 8404 lr &= ~R_V7M_EXCRET_DCRS_MASK; 8405 } 8406 } else { 8407 /* 8408 * We're going to a non-secure exception; push the 8409 * callee-saves registers to the stack now, if they're 8410 * not already saved. 8411 */ 8412 if (lr & R_V7M_EXCRET_DCRS_MASK && 8413 !(dotailchain && !(lr & R_V7M_EXCRET_ES_MASK))) { 8414 push_failed = v7m_push_callee_stack(cpu, lr, dotailchain, 8415 ignore_stackfaults); 8416 } 8417 lr |= R_V7M_EXCRET_DCRS_MASK; 8418 } 8419 } 8420 8421 lr &= ~R_V7M_EXCRET_ES_MASK; 8422 if (targets_secure || !arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8423 lr |= R_V7M_EXCRET_ES_MASK; 8424 } 8425 lr &= ~R_V7M_EXCRET_SPSEL_MASK; 8426 if (env->v7m.control[targets_secure] & R_V7M_CONTROL_SPSEL_MASK) { 8427 lr |= R_V7M_EXCRET_SPSEL_MASK; 8428 } 8429 8430 /* 8431 * Clear registers if necessary to prevent non-secure exception 8432 * code being able to see register values from secure code. 8433 * Where register values become architecturally UNKNOWN we leave 8434 * them with their previous values. 8435 */ 8436 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8437 if (!targets_secure) { 8438 /* 8439 * Always clear the caller-saved registers (they have been 8440 * pushed to the stack earlier in v7m_push_stack()). 8441 * Clear callee-saved registers if the background code is 8442 * Secure (in which case these regs were saved in 8443 * v7m_push_callee_stack()). 8444 */ 8445 int i; 8446 8447 for (i = 0; i < 13; i++) { 8448 /* r4..r11 are callee-saves, zero only if EXCRET.S == 1 */ 8449 if (i < 4 || i > 11 || (lr & R_V7M_EXCRET_S_MASK)) { 8450 env->regs[i] = 0; 8451 } 8452 } 8453 /* Clear EAPSR */ 8454 xpsr_write(env, 0, XPSR_NZCV | XPSR_Q | XPSR_GE | XPSR_IT); 8455 } 8456 } 8457 } 8458 8459 if (push_failed && !ignore_stackfaults) { 8460 /* 8461 * Derived exception on callee-saves register stacking: 8462 * we might now want to take a different exception which 8463 * targets a different security state, so try again from the top. 8464 */ 8465 qemu_log_mask(CPU_LOG_INT, 8466 "...derived exception on callee-saves register stacking"); 8467 v7m_exception_taken(cpu, lr, true, true); 8468 return; 8469 } 8470 8471 if (!arm_v7m_load_vector(cpu, exc, targets_secure, &addr)) { 8472 /* Vector load failed: derived exception */ 8473 qemu_log_mask(CPU_LOG_INT, "...derived exception on vector table load"); 8474 v7m_exception_taken(cpu, lr, true, true); 8475 return; 8476 } 8477 8478 /* 8479 * Now we've done everything that might cause a derived exception 8480 * we can go ahead and activate whichever exception we're going to 8481 * take (which might now be the derived exception). 8482 */ 8483 armv7m_nvic_acknowledge_irq(env->nvic); 8484 8485 /* Switch to target security state -- must do this before writing SPSEL */ 8486 switch_v7m_security_state(env, targets_secure); 8487 write_v7m_control_spsel(env, 0); 8488 arm_clear_exclusive(env); 8489 /* Clear SFPA and FPCA (has no effect if no FPU) */ 8490 env->v7m.control[M_REG_S] &= 8491 ~(R_V7M_CONTROL_FPCA_MASK | R_V7M_CONTROL_SFPA_MASK); 8492 /* Clear IT bits */ 8493 env->condexec_bits = 0; 8494 env->regs[14] = lr; 8495 env->regs[15] = addr & 0xfffffffe; 8496 env->thumb = addr & 1; 8497 } 8498 8499 static void v7m_update_fpccr(CPUARMState *env, uint32_t frameptr, 8500 bool apply_splim) 8501 { 8502 /* 8503 * Like the pseudocode UpdateFPCCR: save state in FPCAR and FPCCR 8504 * that we will need later in order to do lazy FP reg stacking. 8505 */ 8506 bool is_secure = env->v7m.secure; 8507 void *nvic = env->nvic; 8508 /* 8509 * Some bits are unbanked and live always in fpccr[M_REG_S]; some bits 8510 * are banked and we want to update the bit in the bank for the 8511 * current security state; and in one case we want to specifically 8512 * update the NS banked version of a bit even if we are secure. 8513 */ 8514 uint32_t *fpccr_s = &env->v7m.fpccr[M_REG_S]; 8515 uint32_t *fpccr_ns = &env->v7m.fpccr[M_REG_NS]; 8516 uint32_t *fpccr = &env->v7m.fpccr[is_secure]; 8517 bool hfrdy, bfrdy, mmrdy, ns_ufrdy, s_ufrdy, sfrdy, monrdy; 8518 8519 env->v7m.fpcar[is_secure] = frameptr & ~0x7; 8520 8521 if (apply_splim && arm_feature(env, ARM_FEATURE_V8)) { 8522 bool splimviol; 8523 uint32_t splim = v7m_sp_limit(env); 8524 bool ign = armv7m_nvic_neg_prio_requested(nvic, is_secure) && 8525 (env->v7m.ccr[is_secure] & R_V7M_CCR_STKOFHFNMIGN_MASK); 8526 8527 splimviol = !ign && frameptr < splim; 8528 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, SPLIMVIOL, splimviol); 8529 } 8530 8531 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, LSPACT, 1); 8532 8533 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, S, is_secure); 8534 8535 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, USER, arm_current_el(env) == 0); 8536 8537 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, THREAD, 8538 !arm_v7m_is_handler_mode(env)); 8539 8540 hfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_HARD, false); 8541 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, HFRDY, hfrdy); 8542 8543 bfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_BUS, false); 8544 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, BFRDY, bfrdy); 8545 8546 mmrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_MEM, is_secure); 8547 *fpccr = FIELD_DP32(*fpccr, V7M_FPCCR, MMRDY, mmrdy); 8548 8549 ns_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, false); 8550 *fpccr_ns = FIELD_DP32(*fpccr_ns, V7M_FPCCR, UFRDY, ns_ufrdy); 8551 8552 monrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_DEBUG, false); 8553 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, MONRDY, monrdy); 8554 8555 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8556 s_ufrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_USAGE, true); 8557 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, UFRDY, s_ufrdy); 8558 8559 sfrdy = armv7m_nvic_get_ready_status(nvic, ARMV7M_EXCP_SECURE, false); 8560 *fpccr_s = FIELD_DP32(*fpccr_s, V7M_FPCCR, SFRDY, sfrdy); 8561 } 8562 } 8563 8564 void HELPER(v7m_vlstm)(CPUARMState *env, uint32_t fptr) 8565 { 8566 /* fptr is the value of Rn, the frame pointer we store the FP regs to */ 8567 bool s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 8568 bool lspact = env->v7m.fpccr[s] & R_V7M_FPCCR_LSPACT_MASK; 8569 8570 assert(env->v7m.secure); 8571 8572 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { 8573 return; 8574 } 8575 8576 /* Check access to the coprocessor is permitted */ 8577 if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) { 8578 raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC()); 8579 } 8580 8581 if (lspact) { 8582 /* LSPACT should not be active when there is active FP state */ 8583 raise_exception_ra(env, EXCP_LSERR, 0, 1, GETPC()); 8584 } 8585 8586 if (fptr & 7) { 8587 raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC()); 8588 } 8589 8590 /* 8591 * Note that we do not use v7m_stack_write() here, because the 8592 * accesses should not set the FSR bits for stacking errors if they 8593 * fail. (In pseudocode terms, they are AccType_NORMAL, not AccType_STACK 8594 * or AccType_LAZYFP). Faults in cpu_stl_data() will throw exceptions 8595 * and longjmp out. 8596 */ 8597 if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) { 8598 bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK; 8599 int i; 8600 8601 for (i = 0; i < (ts ? 32 : 16); i += 2) { 8602 uint64_t dn = *aa32_vfp_dreg(env, i / 2); 8603 uint32_t faddr = fptr + 4 * i; 8604 uint32_t slo = extract64(dn, 0, 32); 8605 uint32_t shi = extract64(dn, 32, 32); 8606 8607 if (i >= 16) { 8608 faddr += 8; /* skip the slot for the FPSCR */ 8609 } 8610 cpu_stl_data(env, faddr, slo); 8611 cpu_stl_data(env, faddr + 4, shi); 8612 } 8613 cpu_stl_data(env, fptr + 0x40, vfp_get_fpscr(env)); 8614 8615 /* 8616 * If TS is 0 then s0 to s15 and FPSCR are UNKNOWN; we choose to 8617 * leave them unchanged, matching our choice in v7m_preserve_fp_state. 8618 */ 8619 if (ts) { 8620 for (i = 0; i < 32; i += 2) { 8621 *aa32_vfp_dreg(env, i / 2) = 0; 8622 } 8623 vfp_set_fpscr(env, 0); 8624 } 8625 } else { 8626 v7m_update_fpccr(env, fptr, false); 8627 } 8628 8629 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; 8630 } 8631 8632 void HELPER(v7m_vlldm)(CPUARMState *env, uint32_t fptr) 8633 { 8634 /* fptr is the value of Rn, the frame pointer we load the FP regs from */ 8635 assert(env->v7m.secure); 8636 8637 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { 8638 return; 8639 } 8640 8641 /* Check access to the coprocessor is permitted */ 8642 if (!v7m_cpacr_pass(env, true, arm_current_el(env) != 0)) { 8643 raise_exception_ra(env, EXCP_NOCP, 0, 1, GETPC()); 8644 } 8645 8646 if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) { 8647 /* State in FP is still valid */ 8648 env->v7m.fpccr[M_REG_S] &= ~R_V7M_FPCCR_LSPACT_MASK; 8649 } else { 8650 bool ts = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK; 8651 int i; 8652 uint32_t fpscr; 8653 8654 if (fptr & 7) { 8655 raise_exception_ra(env, EXCP_UNALIGNED, 0, 1, GETPC()); 8656 } 8657 8658 for (i = 0; i < (ts ? 32 : 16); i += 2) { 8659 uint32_t slo, shi; 8660 uint64_t dn; 8661 uint32_t faddr = fptr + 4 * i; 8662 8663 if (i >= 16) { 8664 faddr += 8; /* skip the slot for the FPSCR */ 8665 } 8666 8667 slo = cpu_ldl_data(env, faddr); 8668 shi = cpu_ldl_data(env, faddr + 4); 8669 8670 dn = (uint64_t) shi << 32 | slo; 8671 *aa32_vfp_dreg(env, i / 2) = dn; 8672 } 8673 fpscr = cpu_ldl_data(env, fptr + 0x40); 8674 vfp_set_fpscr(env, fpscr); 8675 } 8676 8677 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_FPCA_MASK; 8678 } 8679 8680 static bool v7m_push_stack(ARMCPU *cpu) 8681 { 8682 /* 8683 * Do the "set up stack frame" part of exception entry, 8684 * similar to pseudocode PushStack(). 8685 * Return true if we generate a derived exception (and so 8686 * should ignore further stack faults trying to process 8687 * that derived exception.) 8688 */ 8689 bool stacked_ok = true, limitviol = false; 8690 CPUARMState *env = &cpu->env; 8691 uint32_t xpsr = xpsr_read(env); 8692 uint32_t frameptr = env->regs[13]; 8693 ARMMMUIdx mmu_idx = arm_mmu_idx(env); 8694 uint32_t framesize; 8695 bool nsacr_cp10 = extract32(env->v7m.nsacr, 10, 1); 8696 8697 if ((env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) && 8698 (env->v7m.secure || nsacr_cp10)) { 8699 if (env->v7m.secure && 8700 env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK) { 8701 framesize = 0xa8; 8702 } else { 8703 framesize = 0x68; 8704 } 8705 } else { 8706 framesize = 0x20; 8707 } 8708 8709 /* Align stack pointer if the guest wants that */ 8710 if ((frameptr & 4) && 8711 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKALIGN_MASK)) { 8712 frameptr -= 4; 8713 xpsr |= XPSR_SPREALIGN; 8714 } 8715 8716 xpsr &= ~XPSR_SFPA; 8717 if (env->v7m.secure && 8718 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)) { 8719 xpsr |= XPSR_SFPA; 8720 } 8721 8722 frameptr -= framesize; 8723 8724 if (arm_feature(env, ARM_FEATURE_V8)) { 8725 uint32_t limit = v7m_sp_limit(env); 8726 8727 if (frameptr < limit) { 8728 /* 8729 * Stack limit failure: set SP to the limit value, and generate 8730 * STKOF UsageFault. Stack pushes below the limit must not be 8731 * performed. It is IMPDEF whether pushes above the limit are 8732 * performed; we choose not to. 8733 */ 8734 qemu_log_mask(CPU_LOG_INT, 8735 "...STKOF during stacking\n"); 8736 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 8737 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 8738 env->v7m.secure); 8739 env->regs[13] = limit; 8740 /* 8741 * We won't try to perform any further memory accesses but 8742 * we must continue through the following code to check for 8743 * permission faults during FPU state preservation, and we 8744 * must update FPCCR if lazy stacking is enabled. 8745 */ 8746 limitviol = true; 8747 stacked_ok = false; 8748 } 8749 } 8750 8751 /* 8752 * Write as much of the stack frame as we can. If we fail a stack 8753 * write this will result in a derived exception being pended 8754 * (which may be taken in preference to the one we started with 8755 * if it has higher priority). 8756 */ 8757 stacked_ok = stacked_ok && 8758 v7m_stack_write(cpu, frameptr, env->regs[0], mmu_idx, STACK_NORMAL) && 8759 v7m_stack_write(cpu, frameptr + 4, env->regs[1], 8760 mmu_idx, STACK_NORMAL) && 8761 v7m_stack_write(cpu, frameptr + 8, env->regs[2], 8762 mmu_idx, STACK_NORMAL) && 8763 v7m_stack_write(cpu, frameptr + 12, env->regs[3], 8764 mmu_idx, STACK_NORMAL) && 8765 v7m_stack_write(cpu, frameptr + 16, env->regs[12], 8766 mmu_idx, STACK_NORMAL) && 8767 v7m_stack_write(cpu, frameptr + 20, env->regs[14], 8768 mmu_idx, STACK_NORMAL) && 8769 v7m_stack_write(cpu, frameptr + 24, env->regs[15], 8770 mmu_idx, STACK_NORMAL) && 8771 v7m_stack_write(cpu, frameptr + 28, xpsr, mmu_idx, STACK_NORMAL); 8772 8773 if (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) { 8774 /* FPU is active, try to save its registers */ 8775 bool fpccr_s = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 8776 bool lspact = env->v7m.fpccr[fpccr_s] & R_V7M_FPCCR_LSPACT_MASK; 8777 8778 if (lspact && arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8779 qemu_log_mask(CPU_LOG_INT, 8780 "...SecureFault because LSPACT and FPCA both set\n"); 8781 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; 8782 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 8783 } else if (!env->v7m.secure && !nsacr_cp10) { 8784 qemu_log_mask(CPU_LOG_INT, 8785 "...Secure UsageFault with CFSR.NOCP because " 8786 "NSACR.CP10 prevents stacking FP regs\n"); 8787 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, M_REG_S); 8788 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_NOCP_MASK; 8789 } else { 8790 if (!(env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPEN_MASK)) { 8791 /* Lazy stacking disabled, save registers now */ 8792 int i; 8793 bool cpacr_pass = v7m_cpacr_pass(env, env->v7m.secure, 8794 arm_current_el(env) != 0); 8795 8796 if (stacked_ok && !cpacr_pass) { 8797 /* 8798 * Take UsageFault if CPACR forbids access. The pseudocode 8799 * here does a full CheckCPEnabled() but we know the NSACR 8800 * check can never fail as we have already handled that. 8801 */ 8802 qemu_log_mask(CPU_LOG_INT, 8803 "...UsageFault with CFSR.NOCP because " 8804 "CPACR.CP10 prevents stacking FP regs\n"); 8805 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 8806 env->v7m.secure); 8807 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_NOCP_MASK; 8808 stacked_ok = false; 8809 } 8810 8811 for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) { 8812 uint64_t dn = *aa32_vfp_dreg(env, i / 2); 8813 uint32_t faddr = frameptr + 0x20 + 4 * i; 8814 uint32_t slo = extract64(dn, 0, 32); 8815 uint32_t shi = extract64(dn, 32, 32); 8816 8817 if (i >= 16) { 8818 faddr += 8; /* skip the slot for the FPSCR */ 8819 } 8820 stacked_ok = stacked_ok && 8821 v7m_stack_write(cpu, faddr, slo, 8822 mmu_idx, STACK_NORMAL) && 8823 v7m_stack_write(cpu, faddr + 4, shi, 8824 mmu_idx, STACK_NORMAL); 8825 } 8826 stacked_ok = stacked_ok && 8827 v7m_stack_write(cpu, frameptr + 0x60, 8828 vfp_get_fpscr(env), mmu_idx, STACK_NORMAL); 8829 if (cpacr_pass) { 8830 for (i = 0; i < ((framesize == 0xa8) ? 32 : 16); i += 2) { 8831 *aa32_vfp_dreg(env, i / 2) = 0; 8832 } 8833 vfp_set_fpscr(env, 0); 8834 } 8835 } else { 8836 /* Lazy stacking enabled, save necessary info to stack later */ 8837 v7m_update_fpccr(env, frameptr + 0x20, true); 8838 } 8839 } 8840 } 8841 8842 /* 8843 * If we broke a stack limit then SP was already updated earlier; 8844 * otherwise we update SP regardless of whether any of the stack 8845 * accesses failed or we took some other kind of fault. 8846 */ 8847 if (!limitviol) { 8848 env->regs[13] = frameptr; 8849 } 8850 8851 return !stacked_ok; 8852 } 8853 8854 static void do_v7m_exception_exit(ARMCPU *cpu) 8855 { 8856 CPUARMState *env = &cpu->env; 8857 uint32_t excret; 8858 uint32_t xpsr, xpsr_mask; 8859 bool ufault = false; 8860 bool sfault = false; 8861 bool return_to_sp_process; 8862 bool return_to_handler; 8863 bool rettobase = false; 8864 bool exc_secure = false; 8865 bool return_to_secure; 8866 bool ftype; 8867 bool restore_s16_s31; 8868 8869 /* 8870 * If we're not in Handler mode then jumps to magic exception-exit 8871 * addresses don't have magic behaviour. However for the v8M 8872 * security extensions the magic secure-function-return has to 8873 * work in thread mode too, so to avoid doing an extra check in 8874 * the generated code we allow exception-exit magic to also cause the 8875 * internal exception and bring us here in thread mode. Correct code 8876 * will never try to do this (the following insn fetch will always 8877 * fault) so we the overhead of having taken an unnecessary exception 8878 * doesn't matter. 8879 */ 8880 if (!arm_v7m_is_handler_mode(env)) { 8881 return; 8882 } 8883 8884 /* 8885 * In the spec pseudocode ExceptionReturn() is called directly 8886 * from BXWritePC() and gets the full target PC value including 8887 * bit zero. In QEMU's implementation we treat it as a normal 8888 * jump-to-register (which is then caught later on), and so split 8889 * the target value up between env->regs[15] and env->thumb in 8890 * gen_bx(). Reconstitute it. 8891 */ 8892 excret = env->regs[15]; 8893 if (env->thumb) { 8894 excret |= 1; 8895 } 8896 8897 qemu_log_mask(CPU_LOG_INT, "Exception return: magic PC %" PRIx32 8898 " previous exception %d\n", 8899 excret, env->v7m.exception); 8900 8901 if ((excret & R_V7M_EXCRET_RES1_MASK) != R_V7M_EXCRET_RES1_MASK) { 8902 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero high bits in exception " 8903 "exit PC value 0x%" PRIx32 " are UNPREDICTABLE\n", 8904 excret); 8905 } 8906 8907 ftype = excret & R_V7M_EXCRET_FTYPE_MASK; 8908 8909 if (!arm_feature(env, ARM_FEATURE_VFP) && !ftype) { 8910 qemu_log_mask(LOG_GUEST_ERROR, "M profile: zero FTYPE in exception " 8911 "exit PC value 0x%" PRIx32 " is UNPREDICTABLE " 8912 "if FPU not present\n", 8913 excret); 8914 ftype = true; 8915 } 8916 8917 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8918 /* 8919 * EXC_RETURN.ES validation check (R_SMFL). We must do this before 8920 * we pick which FAULTMASK to clear. 8921 */ 8922 if (!env->v7m.secure && 8923 ((excret & R_V7M_EXCRET_ES_MASK) || 8924 !(excret & R_V7M_EXCRET_DCRS_MASK))) { 8925 sfault = 1; 8926 /* For all other purposes, treat ES as 0 (R_HXSR) */ 8927 excret &= ~R_V7M_EXCRET_ES_MASK; 8928 } 8929 exc_secure = excret & R_V7M_EXCRET_ES_MASK; 8930 } 8931 8932 if (env->v7m.exception != ARMV7M_EXCP_NMI) { 8933 /* 8934 * Auto-clear FAULTMASK on return from other than NMI. 8935 * If the security extension is implemented then this only 8936 * happens if the raw execution priority is >= 0; the 8937 * value of the ES bit in the exception return value indicates 8938 * which security state's faultmask to clear. (v8M ARM ARM R_KBNF.) 8939 */ 8940 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8941 if (armv7m_nvic_raw_execution_priority(env->nvic) >= 0) { 8942 env->v7m.faultmask[exc_secure] = 0; 8943 } 8944 } else { 8945 env->v7m.faultmask[M_REG_NS] = 0; 8946 } 8947 } 8948 8949 switch (armv7m_nvic_complete_irq(env->nvic, env->v7m.exception, 8950 exc_secure)) { 8951 case -1: 8952 /* attempt to exit an exception that isn't active */ 8953 ufault = true; 8954 break; 8955 case 0: 8956 /* still an irq active now */ 8957 break; 8958 case 1: 8959 /* 8960 * We returned to base exception level, no nesting. 8961 * (In the pseudocode this is written using "NestedActivation != 1" 8962 * where we have 'rettobase == false'.) 8963 */ 8964 rettobase = true; 8965 break; 8966 default: 8967 g_assert_not_reached(); 8968 } 8969 8970 return_to_handler = !(excret & R_V7M_EXCRET_MODE_MASK); 8971 return_to_sp_process = excret & R_V7M_EXCRET_SPSEL_MASK; 8972 return_to_secure = arm_feature(env, ARM_FEATURE_M_SECURITY) && 8973 (excret & R_V7M_EXCRET_S_MASK); 8974 8975 if (arm_feature(env, ARM_FEATURE_V8)) { 8976 if (!arm_feature(env, ARM_FEATURE_M_SECURITY)) { 8977 /* 8978 * UNPREDICTABLE if S == 1 or DCRS == 0 or ES == 1 (R_XLCP); 8979 * we choose to take the UsageFault. 8980 */ 8981 if ((excret & R_V7M_EXCRET_S_MASK) || 8982 (excret & R_V7M_EXCRET_ES_MASK) || 8983 !(excret & R_V7M_EXCRET_DCRS_MASK)) { 8984 ufault = true; 8985 } 8986 } 8987 if (excret & R_V7M_EXCRET_RES0_MASK) { 8988 ufault = true; 8989 } 8990 } else { 8991 /* For v7M we only recognize certain combinations of the low bits */ 8992 switch (excret & 0xf) { 8993 case 1: /* Return to Handler */ 8994 break; 8995 case 13: /* Return to Thread using Process stack */ 8996 case 9: /* Return to Thread using Main stack */ 8997 /* 8998 * We only need to check NONBASETHRDENA for v7M, because in 8999 * v8M this bit does not exist (it is RES1). 9000 */ 9001 if (!rettobase && 9002 !(env->v7m.ccr[env->v7m.secure] & 9003 R_V7M_CCR_NONBASETHRDENA_MASK)) { 9004 ufault = true; 9005 } 9006 break; 9007 default: 9008 ufault = true; 9009 } 9010 } 9011 9012 /* 9013 * Set CONTROL.SPSEL from excret.SPSEL. Since we're still in 9014 * Handler mode (and will be until we write the new XPSR.Interrupt 9015 * field) this does not switch around the current stack pointer. 9016 * We must do this before we do any kind of tailchaining, including 9017 * for the derived exceptions on integrity check failures, or we will 9018 * give the guest an incorrect EXCRET.SPSEL value on exception entry. 9019 */ 9020 write_v7m_control_spsel_for_secstate(env, return_to_sp_process, exc_secure); 9021 9022 /* 9023 * Clear scratch FP values left in caller saved registers; this 9024 * must happen before any kind of tail chaining. 9025 */ 9026 if ((env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_CLRONRET_MASK) && 9027 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) { 9028 if (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK) { 9029 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; 9030 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9031 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 9032 "stackframe: error during lazy state deactivation\n"); 9033 v7m_exception_taken(cpu, excret, true, false); 9034 return; 9035 } else { 9036 /* Clear s0..s15 and FPSCR */ 9037 int i; 9038 9039 for (i = 0; i < 16; i += 2) { 9040 *aa32_vfp_dreg(env, i / 2) = 0; 9041 } 9042 vfp_set_fpscr(env, 0); 9043 } 9044 } 9045 9046 if (sfault) { 9047 env->v7m.sfsr |= R_V7M_SFSR_INVER_MASK; 9048 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9049 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 9050 "stackframe: failed EXC_RETURN.ES validity check\n"); 9051 v7m_exception_taken(cpu, excret, true, false); 9052 return; 9053 } 9054 9055 if (ufault) { 9056 /* 9057 * Bad exception return: instead of popping the exception 9058 * stack, directly take a usage fault on the current stack. 9059 */ 9060 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 9061 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9062 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " 9063 "stackframe: failed exception return integrity check\n"); 9064 v7m_exception_taken(cpu, excret, true, false); 9065 return; 9066 } 9067 9068 /* 9069 * Tailchaining: if there is currently a pending exception that 9070 * is high enough priority to preempt execution at the level we're 9071 * about to return to, then just directly take that exception now, 9072 * avoiding an unstack-and-then-stack. Note that now we have 9073 * deactivated the previous exception by calling armv7m_nvic_complete_irq() 9074 * our current execution priority is already the execution priority we are 9075 * returning to -- none of the state we would unstack or set based on 9076 * the EXCRET value affects it. 9077 */ 9078 if (armv7m_nvic_can_take_pending_exception(env->nvic)) { 9079 qemu_log_mask(CPU_LOG_INT, "...tailchaining to pending exception\n"); 9080 v7m_exception_taken(cpu, excret, true, false); 9081 return; 9082 } 9083 9084 switch_v7m_security_state(env, return_to_secure); 9085 9086 { 9087 /* 9088 * The stack pointer we should be reading the exception frame from 9089 * depends on bits in the magic exception return type value (and 9090 * for v8M isn't necessarily the stack pointer we will eventually 9091 * end up resuming execution with). Get a pointer to the location 9092 * in the CPU state struct where the SP we need is currently being 9093 * stored; we will use and modify it in place. 9094 * We use this limited C variable scope so we don't accidentally 9095 * use 'frame_sp_p' after we do something that makes it invalid. 9096 */ 9097 uint32_t *frame_sp_p = get_v7m_sp_ptr(env, 9098 return_to_secure, 9099 !return_to_handler, 9100 return_to_sp_process); 9101 uint32_t frameptr = *frame_sp_p; 9102 bool pop_ok = true; 9103 ARMMMUIdx mmu_idx; 9104 bool return_to_priv = return_to_handler || 9105 !(env->v7m.control[return_to_secure] & R_V7M_CONTROL_NPRIV_MASK); 9106 9107 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, return_to_secure, 9108 return_to_priv); 9109 9110 if (!QEMU_IS_ALIGNED(frameptr, 8) && 9111 arm_feature(env, ARM_FEATURE_V8)) { 9112 qemu_log_mask(LOG_GUEST_ERROR, 9113 "M profile exception return with non-8-aligned SP " 9114 "for destination state is UNPREDICTABLE\n"); 9115 } 9116 9117 /* Do we need to pop callee-saved registers? */ 9118 if (return_to_secure && 9119 ((excret & R_V7M_EXCRET_ES_MASK) == 0 || 9120 (excret & R_V7M_EXCRET_DCRS_MASK) == 0)) { 9121 uint32_t actual_sig; 9122 9123 pop_ok = v7m_stack_read(cpu, &actual_sig, frameptr, mmu_idx); 9124 9125 if (pop_ok && v7m_integrity_sig(env, excret) != actual_sig) { 9126 /* Take a SecureFault on the current stack */ 9127 env->v7m.sfsr |= R_V7M_SFSR_INVIS_MASK; 9128 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9129 qemu_log_mask(CPU_LOG_INT, "...taking SecureFault on existing " 9130 "stackframe: failed exception return integrity " 9131 "signature check\n"); 9132 v7m_exception_taken(cpu, excret, true, false); 9133 return; 9134 } 9135 9136 pop_ok = pop_ok && 9137 v7m_stack_read(cpu, &env->regs[4], frameptr + 0x8, mmu_idx) && 9138 v7m_stack_read(cpu, &env->regs[5], frameptr + 0xc, mmu_idx) && 9139 v7m_stack_read(cpu, &env->regs[6], frameptr + 0x10, mmu_idx) && 9140 v7m_stack_read(cpu, &env->regs[7], frameptr + 0x14, mmu_idx) && 9141 v7m_stack_read(cpu, &env->regs[8], frameptr + 0x18, mmu_idx) && 9142 v7m_stack_read(cpu, &env->regs[9], frameptr + 0x1c, mmu_idx) && 9143 v7m_stack_read(cpu, &env->regs[10], frameptr + 0x20, mmu_idx) && 9144 v7m_stack_read(cpu, &env->regs[11], frameptr + 0x24, mmu_idx); 9145 9146 frameptr += 0x28; 9147 } 9148 9149 /* Pop registers */ 9150 pop_ok = pop_ok && 9151 v7m_stack_read(cpu, &env->regs[0], frameptr, mmu_idx) && 9152 v7m_stack_read(cpu, &env->regs[1], frameptr + 0x4, mmu_idx) && 9153 v7m_stack_read(cpu, &env->regs[2], frameptr + 0x8, mmu_idx) && 9154 v7m_stack_read(cpu, &env->regs[3], frameptr + 0xc, mmu_idx) && 9155 v7m_stack_read(cpu, &env->regs[12], frameptr + 0x10, mmu_idx) && 9156 v7m_stack_read(cpu, &env->regs[14], frameptr + 0x14, mmu_idx) && 9157 v7m_stack_read(cpu, &env->regs[15], frameptr + 0x18, mmu_idx) && 9158 v7m_stack_read(cpu, &xpsr, frameptr + 0x1c, mmu_idx); 9159 9160 if (!pop_ok) { 9161 /* 9162 * v7m_stack_read() pended a fault, so take it (as a tail 9163 * chained exception on the same stack frame) 9164 */ 9165 qemu_log_mask(CPU_LOG_INT, "...derived exception on unstacking\n"); 9166 v7m_exception_taken(cpu, excret, true, false); 9167 return; 9168 } 9169 9170 /* 9171 * Returning from an exception with a PC with bit 0 set is defined 9172 * behaviour on v8M (bit 0 is ignored), but for v7M it was specified 9173 * to be UNPREDICTABLE. In practice actual v7M hardware seems to ignore 9174 * the lsbit, and there are several RTOSes out there which incorrectly 9175 * assume the r15 in the stack frame should be a Thumb-style "lsbit 9176 * indicates ARM/Thumb" value, so ignore the bit on v7M as well, but 9177 * complain about the badly behaved guest. 9178 */ 9179 if (env->regs[15] & 1) { 9180 env->regs[15] &= ~1U; 9181 if (!arm_feature(env, ARM_FEATURE_V8)) { 9182 qemu_log_mask(LOG_GUEST_ERROR, 9183 "M profile return from interrupt with misaligned " 9184 "PC is UNPREDICTABLE on v7M\n"); 9185 } 9186 } 9187 9188 if (arm_feature(env, ARM_FEATURE_V8)) { 9189 /* 9190 * For v8M we have to check whether the xPSR exception field 9191 * matches the EXCRET value for return to handler/thread 9192 * before we commit to changing the SP and xPSR. 9193 */ 9194 bool will_be_handler = (xpsr & XPSR_EXCP) != 0; 9195 if (return_to_handler != will_be_handler) { 9196 /* 9197 * Take an INVPC UsageFault on the current stack. 9198 * By this point we will have switched to the security state 9199 * for the background state, so this UsageFault will target 9200 * that state. 9201 */ 9202 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 9203 env->v7m.secure); 9204 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 9205 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on existing " 9206 "stackframe: failed exception return integrity " 9207 "check\n"); 9208 v7m_exception_taken(cpu, excret, true, false); 9209 return; 9210 } 9211 } 9212 9213 if (!ftype) { 9214 /* FP present and we need to handle it */ 9215 if (!return_to_secure && 9216 (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_LSPACT_MASK)) { 9217 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9218 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; 9219 qemu_log_mask(CPU_LOG_INT, 9220 "...taking SecureFault on existing stackframe: " 9221 "Secure LSPACT set but exception return is " 9222 "not to secure state\n"); 9223 v7m_exception_taken(cpu, excret, true, false); 9224 return; 9225 } 9226 9227 restore_s16_s31 = return_to_secure && 9228 (env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_TS_MASK); 9229 9230 if (env->v7m.fpccr[return_to_secure] & R_V7M_FPCCR_LSPACT_MASK) { 9231 /* State in FPU is still valid, just clear LSPACT */ 9232 env->v7m.fpccr[return_to_secure] &= ~R_V7M_FPCCR_LSPACT_MASK; 9233 } else { 9234 int i; 9235 uint32_t fpscr; 9236 bool cpacr_pass, nsacr_pass; 9237 9238 cpacr_pass = v7m_cpacr_pass(env, return_to_secure, 9239 return_to_priv); 9240 nsacr_pass = return_to_secure || 9241 extract32(env->v7m.nsacr, 10, 1); 9242 9243 if (!cpacr_pass) { 9244 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 9245 return_to_secure); 9246 env->v7m.cfsr[return_to_secure] |= R_V7M_CFSR_NOCP_MASK; 9247 qemu_log_mask(CPU_LOG_INT, 9248 "...taking UsageFault on existing " 9249 "stackframe: CPACR.CP10 prevents unstacking " 9250 "FP regs\n"); 9251 v7m_exception_taken(cpu, excret, true, false); 9252 return; 9253 } else if (!nsacr_pass) { 9254 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, true); 9255 env->v7m.cfsr[M_REG_S] |= R_V7M_CFSR_INVPC_MASK; 9256 qemu_log_mask(CPU_LOG_INT, 9257 "...taking Secure UsageFault on existing " 9258 "stackframe: NSACR.CP10 prevents unstacking " 9259 "FP regs\n"); 9260 v7m_exception_taken(cpu, excret, true, false); 9261 return; 9262 } 9263 9264 for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) { 9265 uint32_t slo, shi; 9266 uint64_t dn; 9267 uint32_t faddr = frameptr + 0x20 + 4 * i; 9268 9269 if (i >= 16) { 9270 faddr += 8; /* Skip the slot for the FPSCR */ 9271 } 9272 9273 pop_ok = pop_ok && 9274 v7m_stack_read(cpu, &slo, faddr, mmu_idx) && 9275 v7m_stack_read(cpu, &shi, faddr + 4, mmu_idx); 9276 9277 if (!pop_ok) { 9278 break; 9279 } 9280 9281 dn = (uint64_t)shi << 32 | slo; 9282 *aa32_vfp_dreg(env, i / 2) = dn; 9283 } 9284 pop_ok = pop_ok && 9285 v7m_stack_read(cpu, &fpscr, frameptr + 0x60, mmu_idx); 9286 if (pop_ok) { 9287 vfp_set_fpscr(env, fpscr); 9288 } 9289 if (!pop_ok) { 9290 /* 9291 * These regs are 0 if security extension present; 9292 * otherwise merely UNKNOWN. We zero always. 9293 */ 9294 for (i = 0; i < (restore_s16_s31 ? 32 : 16); i += 2) { 9295 *aa32_vfp_dreg(env, i / 2) = 0; 9296 } 9297 vfp_set_fpscr(env, 0); 9298 } 9299 } 9300 } 9301 env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S], 9302 V7M_CONTROL, FPCA, !ftype); 9303 9304 /* Commit to consuming the stack frame */ 9305 frameptr += 0x20; 9306 if (!ftype) { 9307 frameptr += 0x48; 9308 if (restore_s16_s31) { 9309 frameptr += 0x40; 9310 } 9311 } 9312 /* 9313 * Undo stack alignment (the SPREALIGN bit indicates that the original 9314 * pre-exception SP was not 8-aligned and we added a padding word to 9315 * align it, so we undo this by ORing in the bit that increases it 9316 * from the current 8-aligned value to the 8-unaligned value. (Adding 4 9317 * would work too but a logical OR is how the pseudocode specifies it.) 9318 */ 9319 if (xpsr & XPSR_SPREALIGN) { 9320 frameptr |= 4; 9321 } 9322 *frame_sp_p = frameptr; 9323 } 9324 9325 xpsr_mask = ~(XPSR_SPREALIGN | XPSR_SFPA); 9326 if (!arm_feature(env, ARM_FEATURE_THUMB_DSP)) { 9327 xpsr_mask &= ~XPSR_GE; 9328 } 9329 /* This xpsr_write() will invalidate frame_sp_p as it may switch stack */ 9330 xpsr_write(env, xpsr, xpsr_mask); 9331 9332 if (env->v7m.secure) { 9333 bool sfpa = xpsr & XPSR_SFPA; 9334 9335 env->v7m.control[M_REG_S] = FIELD_DP32(env->v7m.control[M_REG_S], 9336 V7M_CONTROL, SFPA, sfpa); 9337 } 9338 9339 /* 9340 * The restored xPSR exception field will be zero if we're 9341 * resuming in Thread mode. If that doesn't match what the 9342 * exception return excret specified then this is a UsageFault. 9343 * v7M requires we make this check here; v8M did it earlier. 9344 */ 9345 if (return_to_handler != arm_v7m_is_handler_mode(env)) { 9346 /* 9347 * Take an INVPC UsageFault by pushing the stack again; 9348 * we know we're v7M so this is never a Secure UsageFault. 9349 */ 9350 bool ignore_stackfaults; 9351 9352 assert(!arm_feature(env, ARM_FEATURE_V8)); 9353 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, false); 9354 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 9355 ignore_stackfaults = v7m_push_stack(cpu); 9356 qemu_log_mask(CPU_LOG_INT, "...taking UsageFault on new stackframe: " 9357 "failed exception return integrity check\n"); 9358 v7m_exception_taken(cpu, excret, false, ignore_stackfaults); 9359 return; 9360 } 9361 9362 /* Otherwise, we have a successful exception exit. */ 9363 arm_clear_exclusive(env); 9364 qemu_log_mask(CPU_LOG_INT, "...successful exception return\n"); 9365 } 9366 9367 static bool do_v7m_function_return(ARMCPU *cpu) 9368 { 9369 /* 9370 * v8M security extensions magic function return. 9371 * We may either: 9372 * (1) throw an exception (longjump) 9373 * (2) return true if we successfully handled the function return 9374 * (3) return false if we failed a consistency check and have 9375 * pended a UsageFault that needs to be taken now 9376 * 9377 * At this point the magic return value is split between env->regs[15] 9378 * and env->thumb. We don't bother to reconstitute it because we don't 9379 * need it (all values are handled the same way). 9380 */ 9381 CPUARMState *env = &cpu->env; 9382 uint32_t newpc, newpsr, newpsr_exc; 9383 9384 qemu_log_mask(CPU_LOG_INT, "...really v7M secure function return\n"); 9385 9386 { 9387 bool threadmode, spsel; 9388 TCGMemOpIdx oi; 9389 ARMMMUIdx mmu_idx; 9390 uint32_t *frame_sp_p; 9391 uint32_t frameptr; 9392 9393 /* Pull the return address and IPSR from the Secure stack */ 9394 threadmode = !arm_v7m_is_handler_mode(env); 9395 spsel = env->v7m.control[M_REG_S] & R_V7M_CONTROL_SPSEL_MASK; 9396 9397 frame_sp_p = get_v7m_sp_ptr(env, true, threadmode, spsel); 9398 frameptr = *frame_sp_p; 9399 9400 /* 9401 * These loads may throw an exception (for MPU faults). We want to 9402 * do them as secure, so work out what MMU index that is. 9403 */ 9404 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); 9405 oi = make_memop_idx(MO_LE, arm_to_core_mmu_idx(mmu_idx)); 9406 newpc = helper_le_ldul_mmu(env, frameptr, oi, 0); 9407 newpsr = helper_le_ldul_mmu(env, frameptr + 4, oi, 0); 9408 9409 /* Consistency checks on new IPSR */ 9410 newpsr_exc = newpsr & XPSR_EXCP; 9411 if (!((env->v7m.exception == 0 && newpsr_exc == 0) || 9412 (env->v7m.exception == 1 && newpsr_exc != 0))) { 9413 /* Pend the fault and tell our caller to take it */ 9414 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVPC_MASK; 9415 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, 9416 env->v7m.secure); 9417 qemu_log_mask(CPU_LOG_INT, 9418 "...taking INVPC UsageFault: " 9419 "IPSR consistency check failed\n"); 9420 return false; 9421 } 9422 9423 *frame_sp_p = frameptr + 8; 9424 } 9425 9426 /* This invalidates frame_sp_p */ 9427 switch_v7m_security_state(env, true); 9428 env->v7m.exception = newpsr_exc; 9429 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 9430 if (newpsr & XPSR_SFPA) { 9431 env->v7m.control[M_REG_S] |= R_V7M_CONTROL_SFPA_MASK; 9432 } 9433 xpsr_write(env, 0, XPSR_IT); 9434 env->thumb = newpc & 1; 9435 env->regs[15] = newpc & ~1; 9436 9437 qemu_log_mask(CPU_LOG_INT, "...function return successful\n"); 9438 return true; 9439 } 9440 9441 static bool v7m_read_half_insn(ARMCPU *cpu, ARMMMUIdx mmu_idx, 9442 uint32_t addr, uint16_t *insn) 9443 { 9444 /* 9445 * Load a 16-bit portion of a v7M instruction, returning true on success, 9446 * or false on failure (in which case we will have pended the appropriate 9447 * exception). 9448 * We need to do the instruction fetch's MPU and SAU checks 9449 * like this because there is no MMU index that would allow 9450 * doing the load with a single function call. Instead we must 9451 * first check that the security attributes permit the load 9452 * and that they don't mismatch on the two halves of the instruction, 9453 * and then we do the load as a secure load (ie using the security 9454 * attributes of the address, not the CPU, as architecturally required). 9455 */ 9456 CPUState *cs = CPU(cpu); 9457 CPUARMState *env = &cpu->env; 9458 V8M_SAttributes sattrs = {}; 9459 MemTxAttrs attrs = {}; 9460 ARMMMUFaultInfo fi = {}; 9461 MemTxResult txres; 9462 target_ulong page_size; 9463 hwaddr physaddr; 9464 int prot; 9465 9466 v8m_security_lookup(env, addr, MMU_INST_FETCH, mmu_idx, &sattrs); 9467 if (!sattrs.nsc || sattrs.ns) { 9468 /* 9469 * This must be the second half of the insn, and it straddles a 9470 * region boundary with the second half not being S&NSC. 9471 */ 9472 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 9473 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9474 qemu_log_mask(CPU_LOG_INT, 9475 "...really SecureFault with SFSR.INVEP\n"); 9476 return false; 9477 } 9478 if (get_phys_addr(env, addr, MMU_INST_FETCH, mmu_idx, 9479 &physaddr, &attrs, &prot, &page_size, &fi, NULL)) { 9480 /* the MPU lookup failed */ 9481 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; 9482 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, env->v7m.secure); 9483 qemu_log_mask(CPU_LOG_INT, "...really MemManage with CFSR.IACCVIOL\n"); 9484 return false; 9485 } 9486 *insn = address_space_lduw_le(arm_addressspace(cs, attrs), physaddr, 9487 attrs, &txres); 9488 if (txres != MEMTX_OK) { 9489 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; 9490 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); 9491 qemu_log_mask(CPU_LOG_INT, "...really BusFault with CFSR.IBUSERR\n"); 9492 return false; 9493 } 9494 return true; 9495 } 9496 9497 static bool v7m_handle_execute_nsc(ARMCPU *cpu) 9498 { 9499 /* 9500 * Check whether this attempt to execute code in a Secure & NS-Callable 9501 * memory region is for an SG instruction; if so, then emulate the 9502 * effect of the SG instruction and return true. Otherwise pend 9503 * the correct kind of exception and return false. 9504 */ 9505 CPUARMState *env = &cpu->env; 9506 ARMMMUIdx mmu_idx; 9507 uint16_t insn; 9508 9509 /* 9510 * We should never get here unless get_phys_addr_pmsav8() caused 9511 * an exception for NS executing in S&NSC memory. 9512 */ 9513 assert(!env->v7m.secure); 9514 assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); 9515 9516 /* We want to do the MPU lookup as secure; work out what mmu_idx that is */ 9517 mmu_idx = arm_v7m_mmu_idx_for_secstate(env, true); 9518 9519 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15], &insn)) { 9520 return false; 9521 } 9522 9523 if (!env->thumb) { 9524 goto gen_invep; 9525 } 9526 9527 if (insn != 0xe97f) { 9528 /* 9529 * Not an SG instruction first half (we choose the IMPDEF 9530 * early-SG-check option). 9531 */ 9532 goto gen_invep; 9533 } 9534 9535 if (!v7m_read_half_insn(cpu, mmu_idx, env->regs[15] + 2, &insn)) { 9536 return false; 9537 } 9538 9539 if (insn != 0xe97f) { 9540 /* 9541 * Not an SG instruction second half (yes, both halves of the SG 9542 * insn have the same hex value) 9543 */ 9544 goto gen_invep; 9545 } 9546 9547 /* 9548 * OK, we have confirmed that we really have an SG instruction. 9549 * We know we're NS in S memory so don't need to repeat those checks. 9550 */ 9551 qemu_log_mask(CPU_LOG_INT, "...really an SG instruction at 0x%08" PRIx32 9552 ", executing it\n", env->regs[15]); 9553 env->regs[14] &= ~1; 9554 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 9555 switch_v7m_security_state(env, true); 9556 xpsr_write(env, 0, XPSR_IT); 9557 env->regs[15] += 4; 9558 return true; 9559 9560 gen_invep: 9561 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 9562 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9563 qemu_log_mask(CPU_LOG_INT, 9564 "...really SecureFault with SFSR.INVEP\n"); 9565 return false; 9566 } 9567 9568 void arm_v7m_cpu_do_interrupt(CPUState *cs) 9569 { 9570 ARMCPU *cpu = ARM_CPU(cs); 9571 CPUARMState *env = &cpu->env; 9572 uint32_t lr; 9573 bool ignore_stackfaults; 9574 9575 arm_log_exception(cs->exception_index); 9576 9577 /* 9578 * For exceptions we just mark as pending on the NVIC, and let that 9579 * handle it. 9580 */ 9581 switch (cs->exception_index) { 9582 case EXCP_UDEF: 9583 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9584 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNDEFINSTR_MASK; 9585 break; 9586 case EXCP_NOCP: 9587 { 9588 /* 9589 * NOCP might be directed to something other than the current 9590 * security state if this fault is because of NSACR; we indicate 9591 * the target security state using exception.target_el. 9592 */ 9593 int target_secstate; 9594 9595 if (env->exception.target_el == 3) { 9596 target_secstate = M_REG_S; 9597 } else { 9598 target_secstate = env->v7m.secure; 9599 } 9600 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, target_secstate); 9601 env->v7m.cfsr[target_secstate] |= R_V7M_CFSR_NOCP_MASK; 9602 break; 9603 } 9604 case EXCP_INVSTATE: 9605 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9606 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_INVSTATE_MASK; 9607 break; 9608 case EXCP_STKOF: 9609 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9610 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_STKOF_MASK; 9611 break; 9612 case EXCP_LSERR: 9613 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9614 env->v7m.sfsr |= R_V7M_SFSR_LSERR_MASK; 9615 break; 9616 case EXCP_UNALIGNED: 9617 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_USAGE, env->v7m.secure); 9618 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_UNALIGNED_MASK; 9619 break; 9620 case EXCP_SWI: 9621 /* The PC already points to the next instruction. */ 9622 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SVC, env->v7m.secure); 9623 break; 9624 case EXCP_PREFETCH_ABORT: 9625 case EXCP_DATA_ABORT: 9626 /* 9627 * Note that for M profile we don't have a guest facing FSR, but 9628 * the env->exception.fsr will be populated by the code that 9629 * raises the fault, in the A profile short-descriptor format. 9630 */ 9631 switch (env->exception.fsr & 0xf) { 9632 case M_FAKE_FSR_NSC_EXEC: 9633 /* 9634 * Exception generated when we try to execute code at an address 9635 * which is marked as Secure & Non-Secure Callable and the CPU 9636 * is in the Non-Secure state. The only instruction which can 9637 * be executed like this is SG (and that only if both halves of 9638 * the SG instruction have the same security attributes.) 9639 * Everything else must generate an INVEP SecureFault, so we 9640 * emulate the SG instruction here. 9641 */ 9642 if (v7m_handle_execute_nsc(cpu)) { 9643 return; 9644 } 9645 break; 9646 case M_FAKE_FSR_SFAULT: 9647 /* 9648 * Various flavours of SecureFault for attempts to execute or 9649 * access data in the wrong security state. 9650 */ 9651 switch (cs->exception_index) { 9652 case EXCP_PREFETCH_ABORT: 9653 if (env->v7m.secure) { 9654 env->v7m.sfsr |= R_V7M_SFSR_INVTRAN_MASK; 9655 qemu_log_mask(CPU_LOG_INT, 9656 "...really SecureFault with SFSR.INVTRAN\n"); 9657 } else { 9658 env->v7m.sfsr |= R_V7M_SFSR_INVEP_MASK; 9659 qemu_log_mask(CPU_LOG_INT, 9660 "...really SecureFault with SFSR.INVEP\n"); 9661 } 9662 break; 9663 case EXCP_DATA_ABORT: 9664 /* This must be an NS access to S memory */ 9665 env->v7m.sfsr |= R_V7M_SFSR_AUVIOL_MASK; 9666 qemu_log_mask(CPU_LOG_INT, 9667 "...really SecureFault with SFSR.AUVIOL\n"); 9668 break; 9669 } 9670 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_SECURE, false); 9671 break; 9672 case 0x8: /* External Abort */ 9673 switch (cs->exception_index) { 9674 case EXCP_PREFETCH_ABORT: 9675 env->v7m.cfsr[M_REG_NS] |= R_V7M_CFSR_IBUSERR_MASK; 9676 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IBUSERR\n"); 9677 break; 9678 case EXCP_DATA_ABORT: 9679 env->v7m.cfsr[M_REG_NS] |= 9680 (R_V7M_CFSR_PRECISERR_MASK | R_V7M_CFSR_BFARVALID_MASK); 9681 env->v7m.bfar = env->exception.vaddress; 9682 qemu_log_mask(CPU_LOG_INT, 9683 "...with CFSR.PRECISERR and BFAR 0x%x\n", 9684 env->v7m.bfar); 9685 break; 9686 } 9687 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_BUS, false); 9688 break; 9689 default: 9690 /* 9691 * All other FSR values are either MPU faults or "can't happen 9692 * for M profile" cases. 9693 */ 9694 switch (cs->exception_index) { 9695 case EXCP_PREFETCH_ABORT: 9696 env->v7m.cfsr[env->v7m.secure] |= R_V7M_CFSR_IACCVIOL_MASK; 9697 qemu_log_mask(CPU_LOG_INT, "...with CFSR.IACCVIOL\n"); 9698 break; 9699 case EXCP_DATA_ABORT: 9700 env->v7m.cfsr[env->v7m.secure] |= 9701 (R_V7M_CFSR_DACCVIOL_MASK | R_V7M_CFSR_MMARVALID_MASK); 9702 env->v7m.mmfar[env->v7m.secure] = env->exception.vaddress; 9703 qemu_log_mask(CPU_LOG_INT, 9704 "...with CFSR.DACCVIOL and MMFAR 0x%x\n", 9705 env->v7m.mmfar[env->v7m.secure]); 9706 break; 9707 } 9708 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_MEM, 9709 env->v7m.secure); 9710 break; 9711 } 9712 break; 9713 case EXCP_BKPT: 9714 if (semihosting_enabled()) { 9715 int nr; 9716 nr = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) & 0xff; 9717 if (nr == 0xab) { 9718 env->regs[15] += 2; 9719 qemu_log_mask(CPU_LOG_INT, 9720 "...handling as semihosting call 0x%x\n", 9721 env->regs[0]); 9722 env->regs[0] = do_arm_semihosting(env); 9723 return; 9724 } 9725 } 9726 armv7m_nvic_set_pending(env->nvic, ARMV7M_EXCP_DEBUG, false); 9727 break; 9728 case EXCP_IRQ: 9729 break; 9730 case EXCP_EXCEPTION_EXIT: 9731 if (env->regs[15] < EXC_RETURN_MIN_MAGIC) { 9732 /* Must be v8M security extension function return */ 9733 assert(env->regs[15] >= FNC_RETURN_MIN_MAGIC); 9734 assert(arm_feature(env, ARM_FEATURE_M_SECURITY)); 9735 if (do_v7m_function_return(cpu)) { 9736 return; 9737 } 9738 } else { 9739 do_v7m_exception_exit(cpu); 9740 return; 9741 } 9742 break; 9743 case EXCP_LAZYFP: 9744 /* 9745 * We already pended the specific exception in the NVIC in the 9746 * v7m_preserve_fp_state() helper function. 9747 */ 9748 break; 9749 default: 9750 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 9751 return; /* Never happens. Keep compiler happy. */ 9752 } 9753 9754 if (arm_feature(env, ARM_FEATURE_V8)) { 9755 lr = R_V7M_EXCRET_RES1_MASK | 9756 R_V7M_EXCRET_DCRS_MASK; 9757 /* 9758 * The S bit indicates whether we should return to Secure 9759 * or NonSecure (ie our current state). 9760 * The ES bit indicates whether we're taking this exception 9761 * to Secure or NonSecure (ie our target state). We set it 9762 * later, in v7m_exception_taken(). 9763 * The SPSEL bit is also set in v7m_exception_taken() for v8M. 9764 * This corresponds to the ARM ARM pseudocode for v8M setting 9765 * some LR bits in PushStack() and some in ExceptionTaken(); 9766 * the distinction matters for the tailchain cases where we 9767 * can take an exception without pushing the stack. 9768 */ 9769 if (env->v7m.secure) { 9770 lr |= R_V7M_EXCRET_S_MASK; 9771 } 9772 if (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK)) { 9773 lr |= R_V7M_EXCRET_FTYPE_MASK; 9774 } 9775 } else { 9776 lr = R_V7M_EXCRET_RES1_MASK | 9777 R_V7M_EXCRET_S_MASK | 9778 R_V7M_EXCRET_DCRS_MASK | 9779 R_V7M_EXCRET_FTYPE_MASK | 9780 R_V7M_EXCRET_ES_MASK; 9781 if (env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK) { 9782 lr |= R_V7M_EXCRET_SPSEL_MASK; 9783 } 9784 } 9785 if (!arm_v7m_is_handler_mode(env)) { 9786 lr |= R_V7M_EXCRET_MODE_MASK; 9787 } 9788 9789 ignore_stackfaults = v7m_push_stack(cpu); 9790 v7m_exception_taken(cpu, lr, false, ignore_stackfaults); 9791 } 9792 9793 /* 9794 * Function used to synchronize QEMU's AArch64 register set with AArch32 9795 * register set. This is necessary when switching between AArch32 and AArch64 9796 * execution state. 9797 */ 9798 void aarch64_sync_32_to_64(CPUARMState *env) 9799 { 9800 int i; 9801 uint32_t mode = env->uncached_cpsr & CPSR_M; 9802 9803 /* We can blanket copy R[0:7] to X[0:7] */ 9804 for (i = 0; i < 8; i++) { 9805 env->xregs[i] = env->regs[i]; 9806 } 9807 9808 /* 9809 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12. 9810 * Otherwise, they come from the banked user regs. 9811 */ 9812 if (mode == ARM_CPU_MODE_FIQ) { 9813 for (i = 8; i < 13; i++) { 9814 env->xregs[i] = env->usr_regs[i - 8]; 9815 } 9816 } else { 9817 for (i = 8; i < 13; i++) { 9818 env->xregs[i] = env->regs[i]; 9819 } 9820 } 9821 9822 /* 9823 * Registers x13-x23 are the various mode SP and FP registers. Registers 9824 * r13 and r14 are only copied if we are in that mode, otherwise we copy 9825 * from the mode banked register. 9826 */ 9827 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 9828 env->xregs[13] = env->regs[13]; 9829 env->xregs[14] = env->regs[14]; 9830 } else { 9831 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)]; 9832 /* HYP is an exception in that it is copied from r14 */ 9833 if (mode == ARM_CPU_MODE_HYP) { 9834 env->xregs[14] = env->regs[14]; 9835 } else { 9836 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)]; 9837 } 9838 } 9839 9840 if (mode == ARM_CPU_MODE_HYP) { 9841 env->xregs[15] = env->regs[13]; 9842 } else { 9843 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)]; 9844 } 9845 9846 if (mode == ARM_CPU_MODE_IRQ) { 9847 env->xregs[16] = env->regs[14]; 9848 env->xregs[17] = env->regs[13]; 9849 } else { 9850 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)]; 9851 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)]; 9852 } 9853 9854 if (mode == ARM_CPU_MODE_SVC) { 9855 env->xregs[18] = env->regs[14]; 9856 env->xregs[19] = env->regs[13]; 9857 } else { 9858 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)]; 9859 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)]; 9860 } 9861 9862 if (mode == ARM_CPU_MODE_ABT) { 9863 env->xregs[20] = env->regs[14]; 9864 env->xregs[21] = env->regs[13]; 9865 } else { 9866 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)]; 9867 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)]; 9868 } 9869 9870 if (mode == ARM_CPU_MODE_UND) { 9871 env->xregs[22] = env->regs[14]; 9872 env->xregs[23] = env->regs[13]; 9873 } else { 9874 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)]; 9875 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)]; 9876 } 9877 9878 /* 9879 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 9880 * mode, then we can copy from r8-r14. Otherwise, we copy from the 9881 * FIQ bank for r8-r14. 9882 */ 9883 if (mode == ARM_CPU_MODE_FIQ) { 9884 for (i = 24; i < 31; i++) { 9885 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */ 9886 } 9887 } else { 9888 for (i = 24; i < 29; i++) { 9889 env->xregs[i] = env->fiq_regs[i - 24]; 9890 } 9891 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)]; 9892 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)]; 9893 } 9894 9895 env->pc = env->regs[15]; 9896 } 9897 9898 /* 9899 * Function used to synchronize QEMU's AArch32 register set with AArch64 9900 * register set. This is necessary when switching between AArch32 and AArch64 9901 * execution state. 9902 */ 9903 void aarch64_sync_64_to_32(CPUARMState *env) 9904 { 9905 int i; 9906 uint32_t mode = env->uncached_cpsr & CPSR_M; 9907 9908 /* We can blanket copy X[0:7] to R[0:7] */ 9909 for (i = 0; i < 8; i++) { 9910 env->regs[i] = env->xregs[i]; 9911 } 9912 9913 /* 9914 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12. 9915 * Otherwise, we copy x8-x12 into the banked user regs. 9916 */ 9917 if (mode == ARM_CPU_MODE_FIQ) { 9918 for (i = 8; i < 13; i++) { 9919 env->usr_regs[i - 8] = env->xregs[i]; 9920 } 9921 } else { 9922 for (i = 8; i < 13; i++) { 9923 env->regs[i] = env->xregs[i]; 9924 } 9925 } 9926 9927 /* 9928 * Registers r13 & r14 depend on the current mode. 9929 * If we are in a given mode, we copy the corresponding x registers to r13 9930 * and r14. Otherwise, we copy the x register to the banked r13 and r14 9931 * for the mode. 9932 */ 9933 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 9934 env->regs[13] = env->xregs[13]; 9935 env->regs[14] = env->xregs[14]; 9936 } else { 9937 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13]; 9938 9939 /* 9940 * HYP is an exception in that it does not have its own banked r14 but 9941 * shares the USR r14 9942 */ 9943 if (mode == ARM_CPU_MODE_HYP) { 9944 env->regs[14] = env->xregs[14]; 9945 } else { 9946 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14]; 9947 } 9948 } 9949 9950 if (mode == ARM_CPU_MODE_HYP) { 9951 env->regs[13] = env->xregs[15]; 9952 } else { 9953 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15]; 9954 } 9955 9956 if (mode == ARM_CPU_MODE_IRQ) { 9957 env->regs[14] = env->xregs[16]; 9958 env->regs[13] = env->xregs[17]; 9959 } else { 9960 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16]; 9961 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17]; 9962 } 9963 9964 if (mode == ARM_CPU_MODE_SVC) { 9965 env->regs[14] = env->xregs[18]; 9966 env->regs[13] = env->xregs[19]; 9967 } else { 9968 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18]; 9969 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19]; 9970 } 9971 9972 if (mode == ARM_CPU_MODE_ABT) { 9973 env->regs[14] = env->xregs[20]; 9974 env->regs[13] = env->xregs[21]; 9975 } else { 9976 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20]; 9977 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21]; 9978 } 9979 9980 if (mode == ARM_CPU_MODE_UND) { 9981 env->regs[14] = env->xregs[22]; 9982 env->regs[13] = env->xregs[23]; 9983 } else { 9984 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22]; 9985 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23]; 9986 } 9987 9988 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 9989 * mode, then we can copy to r8-r14. Otherwise, we copy to the 9990 * FIQ bank for r8-r14. 9991 */ 9992 if (mode == ARM_CPU_MODE_FIQ) { 9993 for (i = 24; i < 31; i++) { 9994 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */ 9995 } 9996 } else { 9997 for (i = 24; i < 29; i++) { 9998 env->fiq_regs[i - 24] = env->xregs[i]; 9999 } 10000 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29]; 10001 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30]; 10002 } 10003 10004 env->regs[15] = env->pc; 10005 } 10006 10007 static void take_aarch32_exception(CPUARMState *env, int new_mode, 10008 uint32_t mask, uint32_t offset, 10009 uint32_t newpc) 10010 { 10011 /* Change the CPU state so as to actually take the exception. */ 10012 switch_mode(env, new_mode); 10013 /* 10014 * For exceptions taken to AArch32 we must clear the SS bit in both 10015 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. 10016 */ 10017 env->uncached_cpsr &= ~PSTATE_SS; 10018 env->spsr = cpsr_read(env); 10019 /* Clear IT bits. */ 10020 env->condexec_bits = 0; 10021 /* Switch to the new mode, and to the correct instruction set. */ 10022 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; 10023 /* Set new mode endianness */ 10024 env->uncached_cpsr &= ~CPSR_E; 10025 if (env->cp15.sctlr_el[arm_current_el(env)] & SCTLR_EE) { 10026 env->uncached_cpsr |= CPSR_E; 10027 } 10028 /* J and IL must always be cleared for exception entry */ 10029 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J); 10030 env->daif |= mask; 10031 10032 if (new_mode == ARM_CPU_MODE_HYP) { 10033 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0; 10034 env->elr_el[2] = env->regs[15]; 10035 } else { 10036 /* 10037 * this is a lie, as there was no c1_sys on V4T/V5, but who cares 10038 * and we should just guard the thumb mode on V4 10039 */ 10040 if (arm_feature(env, ARM_FEATURE_V4T)) { 10041 env->thumb = 10042 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; 10043 } 10044 env->regs[14] = env->regs[15] + offset; 10045 } 10046 env->regs[15] = newpc; 10047 } 10048 10049 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs) 10050 { 10051 /* 10052 * Handle exception entry to Hyp mode; this is sufficiently 10053 * different to entry to other AArch32 modes that we handle it 10054 * separately here. 10055 * 10056 * The vector table entry used is always the 0x14 Hyp mode entry point, 10057 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp. 10058 * The offset applied to the preferred return address is always zero 10059 * (see DDI0487C.a section G1.12.3). 10060 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values. 10061 */ 10062 uint32_t addr, mask; 10063 ARMCPU *cpu = ARM_CPU(cs); 10064 CPUARMState *env = &cpu->env; 10065 10066 switch (cs->exception_index) { 10067 case EXCP_UDEF: 10068 addr = 0x04; 10069 break; 10070 case EXCP_SWI: 10071 addr = 0x14; 10072 break; 10073 case EXCP_BKPT: 10074 /* Fall through to prefetch abort. */ 10075 case EXCP_PREFETCH_ABORT: 10076 env->cp15.ifar_s = env->exception.vaddress; 10077 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n", 10078 (uint32_t)env->exception.vaddress); 10079 addr = 0x0c; 10080 break; 10081 case EXCP_DATA_ABORT: 10082 env->cp15.dfar_s = env->exception.vaddress; 10083 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n", 10084 (uint32_t)env->exception.vaddress); 10085 addr = 0x10; 10086 break; 10087 case EXCP_IRQ: 10088 addr = 0x18; 10089 break; 10090 case EXCP_FIQ: 10091 addr = 0x1c; 10092 break; 10093 case EXCP_HVC: 10094 addr = 0x08; 10095 break; 10096 case EXCP_HYP_TRAP: 10097 addr = 0x14; 10098 default: 10099 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10100 } 10101 10102 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) { 10103 if (!arm_feature(env, ARM_FEATURE_V8)) { 10104 /* 10105 * QEMU syndrome values are v8-style. v7 has the IL bit 10106 * UNK/SBZP for "field not valid" cases, where v8 uses RES1. 10107 * If this is a v7 CPU, squash the IL bit in those cases. 10108 */ 10109 if (cs->exception_index == EXCP_PREFETCH_ABORT || 10110 (cs->exception_index == EXCP_DATA_ABORT && 10111 !(env->exception.syndrome & ARM_EL_ISV)) || 10112 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) { 10113 env->exception.syndrome &= ~ARM_EL_IL; 10114 } 10115 } 10116 env->cp15.esr_el[2] = env->exception.syndrome; 10117 } 10118 10119 if (arm_current_el(env) != 2 && addr < 0x14) { 10120 addr = 0x14; 10121 } 10122 10123 mask = 0; 10124 if (!(env->cp15.scr_el3 & SCR_EA)) { 10125 mask |= CPSR_A; 10126 } 10127 if (!(env->cp15.scr_el3 & SCR_IRQ)) { 10128 mask |= CPSR_I; 10129 } 10130 if (!(env->cp15.scr_el3 & SCR_FIQ)) { 10131 mask |= CPSR_F; 10132 } 10133 10134 addr += env->cp15.hvbar; 10135 10136 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr); 10137 } 10138 10139 static void arm_cpu_do_interrupt_aarch32(CPUState *cs) 10140 { 10141 ARMCPU *cpu = ARM_CPU(cs); 10142 CPUARMState *env = &cpu->env; 10143 uint32_t addr; 10144 uint32_t mask; 10145 int new_mode; 10146 uint32_t offset; 10147 uint32_t moe; 10148 10149 /* If this is a debug exception we must update the DBGDSCR.MOE bits */ 10150 switch (syn_get_ec(env->exception.syndrome)) { 10151 case EC_BREAKPOINT: 10152 case EC_BREAKPOINT_SAME_EL: 10153 moe = 1; 10154 break; 10155 case EC_WATCHPOINT: 10156 case EC_WATCHPOINT_SAME_EL: 10157 moe = 10; 10158 break; 10159 case EC_AA32_BKPT: 10160 moe = 3; 10161 break; 10162 case EC_VECTORCATCH: 10163 moe = 5; 10164 break; 10165 default: 10166 moe = 0; 10167 break; 10168 } 10169 10170 if (moe) { 10171 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); 10172 } 10173 10174 if (env->exception.target_el == 2) { 10175 arm_cpu_do_interrupt_aarch32_hyp(cs); 10176 return; 10177 } 10178 10179 switch (cs->exception_index) { 10180 case EXCP_UDEF: 10181 new_mode = ARM_CPU_MODE_UND; 10182 addr = 0x04; 10183 mask = CPSR_I; 10184 if (env->thumb) 10185 offset = 2; 10186 else 10187 offset = 4; 10188 break; 10189 case EXCP_SWI: 10190 new_mode = ARM_CPU_MODE_SVC; 10191 addr = 0x08; 10192 mask = CPSR_I; 10193 /* The PC already points to the next instruction. */ 10194 offset = 0; 10195 break; 10196 case EXCP_BKPT: 10197 /* Fall through to prefetch abort. */ 10198 case EXCP_PREFETCH_ABORT: 10199 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); 10200 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); 10201 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", 10202 env->exception.fsr, (uint32_t)env->exception.vaddress); 10203 new_mode = ARM_CPU_MODE_ABT; 10204 addr = 0x0c; 10205 mask = CPSR_A | CPSR_I; 10206 offset = 4; 10207 break; 10208 case EXCP_DATA_ABORT: 10209 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 10210 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); 10211 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", 10212 env->exception.fsr, 10213 (uint32_t)env->exception.vaddress); 10214 new_mode = ARM_CPU_MODE_ABT; 10215 addr = 0x10; 10216 mask = CPSR_A | CPSR_I; 10217 offset = 8; 10218 break; 10219 case EXCP_IRQ: 10220 new_mode = ARM_CPU_MODE_IRQ; 10221 addr = 0x18; 10222 /* Disable IRQ and imprecise data aborts. */ 10223 mask = CPSR_A | CPSR_I; 10224 offset = 4; 10225 if (env->cp15.scr_el3 & SCR_IRQ) { 10226 /* IRQ routed to monitor mode */ 10227 new_mode = ARM_CPU_MODE_MON; 10228 mask |= CPSR_F; 10229 } 10230 break; 10231 case EXCP_FIQ: 10232 new_mode = ARM_CPU_MODE_FIQ; 10233 addr = 0x1c; 10234 /* Disable FIQ, IRQ and imprecise data aborts. */ 10235 mask = CPSR_A | CPSR_I | CPSR_F; 10236 if (env->cp15.scr_el3 & SCR_FIQ) { 10237 /* FIQ routed to monitor mode */ 10238 new_mode = ARM_CPU_MODE_MON; 10239 } 10240 offset = 4; 10241 break; 10242 case EXCP_VIRQ: 10243 new_mode = ARM_CPU_MODE_IRQ; 10244 addr = 0x18; 10245 /* Disable IRQ and imprecise data aborts. */ 10246 mask = CPSR_A | CPSR_I; 10247 offset = 4; 10248 break; 10249 case EXCP_VFIQ: 10250 new_mode = ARM_CPU_MODE_FIQ; 10251 addr = 0x1c; 10252 /* Disable FIQ, IRQ and imprecise data aborts. */ 10253 mask = CPSR_A | CPSR_I | CPSR_F; 10254 offset = 4; 10255 break; 10256 case EXCP_SMC: 10257 new_mode = ARM_CPU_MODE_MON; 10258 addr = 0x08; 10259 mask = CPSR_A | CPSR_I | CPSR_F; 10260 offset = 0; 10261 break; 10262 default: 10263 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10264 return; /* Never happens. Keep compiler happy. */ 10265 } 10266 10267 if (new_mode == ARM_CPU_MODE_MON) { 10268 addr += env->cp15.mvbar; 10269 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 10270 /* High vectors. When enabled, base address cannot be remapped. */ 10271 addr += 0xffff0000; 10272 } else { 10273 /* ARM v7 architectures provide a vector base address register to remap 10274 * the interrupt vector table. 10275 * This register is only followed in non-monitor mode, and is banked. 10276 * Note: only bits 31:5 are valid. 10277 */ 10278 addr += A32_BANKED_CURRENT_REG_GET(env, vbar); 10279 } 10280 10281 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 10282 env->cp15.scr_el3 &= ~SCR_NS; 10283 } 10284 10285 take_aarch32_exception(env, new_mode, mask, offset, addr); 10286 } 10287 10288 /* Handle exception entry to a target EL which is using AArch64 */ 10289 static void arm_cpu_do_interrupt_aarch64(CPUState *cs) 10290 { 10291 ARMCPU *cpu = ARM_CPU(cs); 10292 CPUARMState *env = &cpu->env; 10293 unsigned int new_el = env->exception.target_el; 10294 target_ulong addr = env->cp15.vbar_el[new_el]; 10295 unsigned int new_mode = aarch64_pstate_mode(new_el, true); 10296 unsigned int cur_el = arm_current_el(env); 10297 10298 /* 10299 * Note that new_el can never be 0. If cur_el is 0, then 10300 * el0_a64 is is_a64(), else el0_a64 is ignored. 10301 */ 10302 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env)); 10303 10304 if (cur_el < new_el) { 10305 /* Entry vector offset depends on whether the implemented EL 10306 * immediately lower than the target level is using AArch32 or AArch64 10307 */ 10308 bool is_aa64; 10309 10310 switch (new_el) { 10311 case 3: 10312 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0; 10313 break; 10314 case 2: 10315 is_aa64 = (env->cp15.hcr_el2 & HCR_RW) != 0; 10316 break; 10317 case 1: 10318 is_aa64 = is_a64(env); 10319 break; 10320 default: 10321 g_assert_not_reached(); 10322 } 10323 10324 if (is_aa64) { 10325 addr += 0x400; 10326 } else { 10327 addr += 0x600; 10328 } 10329 } else if (pstate_read(env) & PSTATE_SP) { 10330 addr += 0x200; 10331 } 10332 10333 switch (cs->exception_index) { 10334 case EXCP_PREFETCH_ABORT: 10335 case EXCP_DATA_ABORT: 10336 env->cp15.far_el[new_el] = env->exception.vaddress; 10337 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n", 10338 env->cp15.far_el[new_el]); 10339 /* fall through */ 10340 case EXCP_BKPT: 10341 case EXCP_UDEF: 10342 case EXCP_SWI: 10343 case EXCP_HVC: 10344 case EXCP_HYP_TRAP: 10345 case EXCP_SMC: 10346 if (syn_get_ec(env->exception.syndrome) == EC_ADVSIMDFPACCESSTRAP) { 10347 /* 10348 * QEMU internal FP/SIMD syndromes from AArch32 include the 10349 * TA and coproc fields which are only exposed if the exception 10350 * is taken to AArch32 Hyp mode. Mask them out to get a valid 10351 * AArch64 format syndrome. 10352 */ 10353 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20); 10354 } 10355 env->cp15.esr_el[new_el] = env->exception.syndrome; 10356 break; 10357 case EXCP_IRQ: 10358 case EXCP_VIRQ: 10359 addr += 0x80; 10360 break; 10361 case EXCP_FIQ: 10362 case EXCP_VFIQ: 10363 addr += 0x100; 10364 break; 10365 case EXCP_SEMIHOST: 10366 qemu_log_mask(CPU_LOG_INT, 10367 "...handling as semihosting call 0x%" PRIx64 "\n", 10368 env->xregs[0]); 10369 env->xregs[0] = do_arm_semihosting(env); 10370 return; 10371 default: 10372 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10373 } 10374 10375 if (is_a64(env)) { 10376 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = pstate_read(env); 10377 aarch64_save_sp(env, arm_current_el(env)); 10378 env->elr_el[new_el] = env->pc; 10379 } else { 10380 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = cpsr_read(env); 10381 env->elr_el[new_el] = env->regs[15]; 10382 10383 aarch64_sync_32_to_64(env); 10384 10385 env->condexec_bits = 0; 10386 } 10387 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n", 10388 env->elr_el[new_el]); 10389 10390 pstate_write(env, PSTATE_DAIF | new_mode); 10391 env->aarch64 = 1; 10392 aarch64_restore_sp(env, new_el); 10393 10394 env->pc = addr; 10395 10396 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n", 10397 new_el, env->pc, pstate_read(env)); 10398 } 10399 10400 static inline bool check_for_semihosting(CPUState *cs) 10401 { 10402 /* Check whether this exception is a semihosting call; if so 10403 * then handle it and return true; otherwise return false. 10404 */ 10405 ARMCPU *cpu = ARM_CPU(cs); 10406 CPUARMState *env = &cpu->env; 10407 10408 if (is_a64(env)) { 10409 if (cs->exception_index == EXCP_SEMIHOST) { 10410 /* This is always the 64-bit semihosting exception. 10411 * The "is this usermode" and "is semihosting enabled" 10412 * checks have been done at translate time. 10413 */ 10414 qemu_log_mask(CPU_LOG_INT, 10415 "...handling as semihosting call 0x%" PRIx64 "\n", 10416 env->xregs[0]); 10417 env->xregs[0] = do_arm_semihosting(env); 10418 return true; 10419 } 10420 return false; 10421 } else { 10422 uint32_t imm; 10423 10424 /* Only intercept calls from privileged modes, to provide some 10425 * semblance of security. 10426 */ 10427 if (cs->exception_index != EXCP_SEMIHOST && 10428 (!semihosting_enabled() || 10429 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR))) { 10430 return false; 10431 } 10432 10433 switch (cs->exception_index) { 10434 case EXCP_SEMIHOST: 10435 /* This is always a semihosting call; the "is this usermode" 10436 * and "is semihosting enabled" checks have been done at 10437 * translate time. 10438 */ 10439 break; 10440 case EXCP_SWI: 10441 /* Check for semihosting interrupt. */ 10442 if (env->thumb) { 10443 imm = arm_lduw_code(env, env->regs[15] - 2, arm_sctlr_b(env)) 10444 & 0xff; 10445 if (imm == 0xab) { 10446 break; 10447 } 10448 } else { 10449 imm = arm_ldl_code(env, env->regs[15] - 4, arm_sctlr_b(env)) 10450 & 0xffffff; 10451 if (imm == 0x123456) { 10452 break; 10453 } 10454 } 10455 return false; 10456 case EXCP_BKPT: 10457 /* See if this is a semihosting syscall. */ 10458 if (env->thumb) { 10459 imm = arm_lduw_code(env, env->regs[15], arm_sctlr_b(env)) 10460 & 0xff; 10461 if (imm == 0xab) { 10462 env->regs[15] += 2; 10463 break; 10464 } 10465 } 10466 return false; 10467 default: 10468 return false; 10469 } 10470 10471 qemu_log_mask(CPU_LOG_INT, 10472 "...handling as semihosting call 0x%x\n", 10473 env->regs[0]); 10474 env->regs[0] = do_arm_semihosting(env); 10475 return true; 10476 } 10477 } 10478 10479 /* Handle a CPU exception for A and R profile CPUs. 10480 * Do any appropriate logging, handle PSCI calls, and then hand off 10481 * to the AArch64-entry or AArch32-entry function depending on the 10482 * target exception level's register width. 10483 */ 10484 void arm_cpu_do_interrupt(CPUState *cs) 10485 { 10486 ARMCPU *cpu = ARM_CPU(cs); 10487 CPUARMState *env = &cpu->env; 10488 unsigned int new_el = env->exception.target_el; 10489 10490 assert(!arm_feature(env, ARM_FEATURE_M)); 10491 10492 arm_log_exception(cs->exception_index); 10493 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env), 10494 new_el); 10495 if (qemu_loglevel_mask(CPU_LOG_INT) 10496 && !excp_is_internal(cs->exception_index)) { 10497 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n", 10498 syn_get_ec(env->exception.syndrome), 10499 env->exception.syndrome); 10500 } 10501 10502 if (arm_is_psci_call(cpu, cs->exception_index)) { 10503 arm_handle_psci_call(cpu); 10504 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); 10505 return; 10506 } 10507 10508 /* Semihosting semantics depend on the register width of the 10509 * code that caused the exception, not the target exception level, 10510 * so must be handled here. 10511 */ 10512 if (check_for_semihosting(cs)) { 10513 return; 10514 } 10515 10516 /* Hooks may change global state so BQL should be held, also the 10517 * BQL needs to be held for any modification of 10518 * cs->interrupt_request. 10519 */ 10520 g_assert(qemu_mutex_iothread_locked()); 10521 10522 arm_call_pre_el_change_hook(cpu); 10523 10524 assert(!excp_is_internal(cs->exception_index)); 10525 if (arm_el_is_aa64(env, new_el)) { 10526 arm_cpu_do_interrupt_aarch64(cs); 10527 } else { 10528 arm_cpu_do_interrupt_aarch32(cs); 10529 } 10530 10531 arm_call_el_change_hook(cpu); 10532 10533 if (!kvm_enabled()) { 10534 cs->interrupt_request |= CPU_INTERRUPT_EXITTB; 10535 } 10536 } 10537 #endif /* !CONFIG_USER_ONLY */ 10538 10539 /* Return the exception level which controls this address translation regime */ 10540 static inline uint32_t regime_el(CPUARMState *env, ARMMMUIdx mmu_idx) 10541 { 10542 switch (mmu_idx) { 10543 case ARMMMUIdx_S2NS: 10544 case ARMMMUIdx_S1E2: 10545 return 2; 10546 case ARMMMUIdx_S1E3: 10547 return 3; 10548 case ARMMMUIdx_S1SE0: 10549 return arm_el_is_aa64(env, 3) ? 1 : 3; 10550 case ARMMMUIdx_S1SE1: 10551 case ARMMMUIdx_S1NSE0: 10552 case ARMMMUIdx_S1NSE1: 10553 case ARMMMUIdx_MPrivNegPri: 10554 case ARMMMUIdx_MUserNegPri: 10555 case ARMMMUIdx_MPriv: 10556 case ARMMMUIdx_MUser: 10557 case ARMMMUIdx_MSPrivNegPri: 10558 case ARMMMUIdx_MSUserNegPri: 10559 case ARMMMUIdx_MSPriv: 10560 case ARMMMUIdx_MSUser: 10561 return 1; 10562 default: 10563 g_assert_not_reached(); 10564 } 10565 } 10566 10567 #ifndef CONFIG_USER_ONLY 10568 10569 /* Return the SCTLR value which controls this address translation regime */ 10570 static inline uint32_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx) 10571 { 10572 return env->cp15.sctlr_el[regime_el(env, mmu_idx)]; 10573 } 10574 10575 /* Return true if the specified stage of address translation is disabled */ 10576 static inline bool regime_translation_disabled(CPUARMState *env, 10577 ARMMMUIdx mmu_idx) 10578 { 10579 if (arm_feature(env, ARM_FEATURE_M)) { 10580 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] & 10581 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) { 10582 case R_V7M_MPU_CTRL_ENABLE_MASK: 10583 /* Enabled, but not for HardFault and NMI */ 10584 return mmu_idx & ARM_MMU_IDX_M_NEGPRI; 10585 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK: 10586 /* Enabled for all cases */ 10587 return false; 10588 case 0: 10589 default: 10590 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but 10591 * we warned about that in armv7m_nvic.c when the guest set it. 10592 */ 10593 return true; 10594 } 10595 } 10596 10597 if (mmu_idx == ARMMMUIdx_S2NS) { 10598 /* HCR.DC means HCR.VM behaves as 1 */ 10599 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0; 10600 } 10601 10602 if (env->cp15.hcr_el2 & HCR_TGE) { 10603 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */ 10604 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) { 10605 return true; 10606 } 10607 } 10608 10609 if ((env->cp15.hcr_el2 & HCR_DC) && 10610 (mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1)) { 10611 /* HCR.DC means SCTLR_EL1.M behaves as 0 */ 10612 return true; 10613 } 10614 10615 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0; 10616 } 10617 10618 static inline bool regime_translation_big_endian(CPUARMState *env, 10619 ARMMMUIdx mmu_idx) 10620 { 10621 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0; 10622 } 10623 10624 /* Return the TTBR associated with this translation regime */ 10625 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx, 10626 int ttbrn) 10627 { 10628 if (mmu_idx == ARMMMUIdx_S2NS) { 10629 return env->cp15.vttbr_el2; 10630 } 10631 if (ttbrn == 0) { 10632 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)]; 10633 } else { 10634 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)]; 10635 } 10636 } 10637 10638 #endif /* !CONFIG_USER_ONLY */ 10639 10640 /* Return the TCR controlling this translation regime */ 10641 static inline TCR *regime_tcr(CPUARMState *env, ARMMMUIdx mmu_idx) 10642 { 10643 if (mmu_idx == ARMMMUIdx_S2NS) { 10644 return &env->cp15.vtcr_el2; 10645 } 10646 return &env->cp15.tcr_el[regime_el(env, mmu_idx)]; 10647 } 10648 10649 /* Convert a possible stage1+2 MMU index into the appropriate 10650 * stage 1 MMU index 10651 */ 10652 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx) 10653 { 10654 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 10655 mmu_idx += (ARMMMUIdx_S1NSE0 - ARMMMUIdx_S12NSE0); 10656 } 10657 return mmu_idx; 10658 } 10659 10660 /* Return true if the translation regime is using LPAE format page tables */ 10661 static inline bool regime_using_lpae_format(CPUARMState *env, 10662 ARMMMUIdx mmu_idx) 10663 { 10664 int el = regime_el(env, mmu_idx); 10665 if (el == 2 || arm_el_is_aa64(env, el)) { 10666 return true; 10667 } 10668 if (arm_feature(env, ARM_FEATURE_LPAE) 10669 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) { 10670 return true; 10671 } 10672 return false; 10673 } 10674 10675 /* Returns true if the stage 1 translation regime is using LPAE format page 10676 * tables. Used when raising alignment exceptions, whose FSR changes depending 10677 * on whether the long or short descriptor format is in use. */ 10678 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx) 10679 { 10680 mmu_idx = stage_1_mmu_idx(mmu_idx); 10681 10682 return regime_using_lpae_format(env, mmu_idx); 10683 } 10684 10685 #ifndef CONFIG_USER_ONLY 10686 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx) 10687 { 10688 switch (mmu_idx) { 10689 case ARMMMUIdx_S1SE0: 10690 case ARMMMUIdx_S1NSE0: 10691 case ARMMMUIdx_MUser: 10692 case ARMMMUIdx_MSUser: 10693 case ARMMMUIdx_MUserNegPri: 10694 case ARMMMUIdx_MSUserNegPri: 10695 return true; 10696 default: 10697 return false; 10698 case ARMMMUIdx_S12NSE0: 10699 case ARMMMUIdx_S12NSE1: 10700 g_assert_not_reached(); 10701 } 10702 } 10703 10704 /* Translate section/page access permissions to page 10705 * R/W protection flags 10706 * 10707 * @env: CPUARMState 10708 * @mmu_idx: MMU index indicating required translation regime 10709 * @ap: The 3-bit access permissions (AP[2:0]) 10710 * @domain_prot: The 2-bit domain access permissions 10711 */ 10712 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, 10713 int ap, int domain_prot) 10714 { 10715 bool is_user = regime_is_user(env, mmu_idx); 10716 10717 if (domain_prot == 3) { 10718 return PAGE_READ | PAGE_WRITE; 10719 } 10720 10721 switch (ap) { 10722 case 0: 10723 if (arm_feature(env, ARM_FEATURE_V7)) { 10724 return 0; 10725 } 10726 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) { 10727 case SCTLR_S: 10728 return is_user ? 0 : PAGE_READ; 10729 case SCTLR_R: 10730 return PAGE_READ; 10731 default: 10732 return 0; 10733 } 10734 case 1: 10735 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 10736 case 2: 10737 if (is_user) { 10738 return PAGE_READ; 10739 } else { 10740 return PAGE_READ | PAGE_WRITE; 10741 } 10742 case 3: 10743 return PAGE_READ | PAGE_WRITE; 10744 case 4: /* Reserved. */ 10745 return 0; 10746 case 5: 10747 return is_user ? 0 : PAGE_READ; 10748 case 6: 10749 return PAGE_READ; 10750 case 7: 10751 if (!arm_feature(env, ARM_FEATURE_V6K)) { 10752 return 0; 10753 } 10754 return PAGE_READ; 10755 default: 10756 g_assert_not_reached(); 10757 } 10758 } 10759 10760 /* Translate section/page access permissions to page 10761 * R/W protection flags. 10762 * 10763 * @ap: The 2-bit simple AP (AP[2:1]) 10764 * @is_user: TRUE if accessing from PL0 10765 */ 10766 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user) 10767 { 10768 switch (ap) { 10769 case 0: 10770 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 10771 case 1: 10772 return PAGE_READ | PAGE_WRITE; 10773 case 2: 10774 return is_user ? 0 : PAGE_READ; 10775 case 3: 10776 return PAGE_READ; 10777 default: 10778 g_assert_not_reached(); 10779 } 10780 } 10781 10782 static inline int 10783 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap) 10784 { 10785 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx)); 10786 } 10787 10788 /* Translate S2 section/page access permissions to protection flags 10789 * 10790 * @env: CPUARMState 10791 * @s2ap: The 2-bit stage2 access permissions (S2AP) 10792 * @xn: XN (execute-never) bit 10793 */ 10794 static int get_S2prot(CPUARMState *env, int s2ap, int xn) 10795 { 10796 int prot = 0; 10797 10798 if (s2ap & 1) { 10799 prot |= PAGE_READ; 10800 } 10801 if (s2ap & 2) { 10802 prot |= PAGE_WRITE; 10803 } 10804 if (!xn) { 10805 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) { 10806 prot |= PAGE_EXEC; 10807 } 10808 } 10809 return prot; 10810 } 10811 10812 /* Translate section/page access permissions to protection flags 10813 * 10814 * @env: CPUARMState 10815 * @mmu_idx: MMU index indicating required translation regime 10816 * @is_aa64: TRUE if AArch64 10817 * @ap: The 2-bit simple AP (AP[2:1]) 10818 * @ns: NS (non-secure) bit 10819 * @xn: XN (execute-never) bit 10820 * @pxn: PXN (privileged execute-never) bit 10821 */ 10822 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64, 10823 int ap, int ns, int xn, int pxn) 10824 { 10825 bool is_user = regime_is_user(env, mmu_idx); 10826 int prot_rw, user_rw; 10827 bool have_wxn; 10828 int wxn = 0; 10829 10830 assert(mmu_idx != ARMMMUIdx_S2NS); 10831 10832 user_rw = simple_ap_to_rw_prot_is_user(ap, true); 10833 if (is_user) { 10834 prot_rw = user_rw; 10835 } else { 10836 prot_rw = simple_ap_to_rw_prot_is_user(ap, false); 10837 } 10838 10839 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) { 10840 return prot_rw; 10841 } 10842 10843 /* TODO have_wxn should be replaced with 10844 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2) 10845 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE 10846 * compatible processors have EL2, which is required for [U]WXN. 10847 */ 10848 have_wxn = arm_feature(env, ARM_FEATURE_LPAE); 10849 10850 if (have_wxn) { 10851 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN; 10852 } 10853 10854 if (is_aa64) { 10855 switch (regime_el(env, mmu_idx)) { 10856 case 1: 10857 if (!is_user) { 10858 xn = pxn || (user_rw & PAGE_WRITE); 10859 } 10860 break; 10861 case 2: 10862 case 3: 10863 break; 10864 } 10865 } else if (arm_feature(env, ARM_FEATURE_V7)) { 10866 switch (regime_el(env, mmu_idx)) { 10867 case 1: 10868 case 3: 10869 if (is_user) { 10870 xn = xn || !(user_rw & PAGE_READ); 10871 } else { 10872 int uwxn = 0; 10873 if (have_wxn) { 10874 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN; 10875 } 10876 xn = xn || !(prot_rw & PAGE_READ) || pxn || 10877 (uwxn && (user_rw & PAGE_WRITE)); 10878 } 10879 break; 10880 case 2: 10881 break; 10882 } 10883 } else { 10884 xn = wxn = 0; 10885 } 10886 10887 if (xn || (wxn && (prot_rw & PAGE_WRITE))) { 10888 return prot_rw; 10889 } 10890 return prot_rw | PAGE_EXEC; 10891 } 10892 10893 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx, 10894 uint32_t *table, uint32_t address) 10895 { 10896 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */ 10897 TCR *tcr = regime_tcr(env, mmu_idx); 10898 10899 if (address & tcr->mask) { 10900 if (tcr->raw_tcr & TTBCR_PD1) { 10901 /* Translation table walk disabled for TTBR1 */ 10902 return false; 10903 } 10904 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000; 10905 } else { 10906 if (tcr->raw_tcr & TTBCR_PD0) { 10907 /* Translation table walk disabled for TTBR0 */ 10908 return false; 10909 } 10910 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask; 10911 } 10912 *table |= (address >> 18) & 0x3ffc; 10913 return true; 10914 } 10915 10916 /* Translate a S1 pagetable walk through S2 if needed. */ 10917 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx, 10918 hwaddr addr, MemTxAttrs txattrs, 10919 ARMMMUFaultInfo *fi) 10920 { 10921 if ((mmu_idx == ARMMMUIdx_S1NSE0 || mmu_idx == ARMMMUIdx_S1NSE1) && 10922 !regime_translation_disabled(env, ARMMMUIdx_S2NS)) { 10923 target_ulong s2size; 10924 hwaddr s2pa; 10925 int s2prot; 10926 int ret; 10927 ARMCacheAttrs cacheattrs = {}; 10928 ARMCacheAttrs *pcacheattrs = NULL; 10929 10930 if (env->cp15.hcr_el2 & HCR_PTW) { 10931 /* 10932 * PTW means we must fault if this S1 walk touches S2 Device 10933 * memory; otherwise we don't care about the attributes and can 10934 * save the S2 translation the effort of computing them. 10935 */ 10936 pcacheattrs = &cacheattrs; 10937 } 10938 10939 ret = get_phys_addr_lpae(env, addr, 0, ARMMMUIdx_S2NS, &s2pa, 10940 &txattrs, &s2prot, &s2size, fi, pcacheattrs); 10941 if (ret) { 10942 assert(fi->type != ARMFault_None); 10943 fi->s2addr = addr; 10944 fi->stage2 = true; 10945 fi->s1ptw = true; 10946 return ~0; 10947 } 10948 if (pcacheattrs && (pcacheattrs->attrs & 0xf0) == 0) { 10949 /* Access was to Device memory: generate Permission fault */ 10950 fi->type = ARMFault_Permission; 10951 fi->s2addr = addr; 10952 fi->stage2 = true; 10953 fi->s1ptw = true; 10954 return ~0; 10955 } 10956 addr = s2pa; 10957 } 10958 return addr; 10959 } 10960 10961 /* All loads done in the course of a page table walk go through here. */ 10962 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure, 10963 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 10964 { 10965 ARMCPU *cpu = ARM_CPU(cs); 10966 CPUARMState *env = &cpu->env; 10967 MemTxAttrs attrs = {}; 10968 MemTxResult result = MEMTX_OK; 10969 AddressSpace *as; 10970 uint32_t data; 10971 10972 attrs.secure = is_secure; 10973 as = arm_addressspace(cs, attrs); 10974 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 10975 if (fi->s1ptw) { 10976 return 0; 10977 } 10978 if (regime_translation_big_endian(env, mmu_idx)) { 10979 data = address_space_ldl_be(as, addr, attrs, &result); 10980 } else { 10981 data = address_space_ldl_le(as, addr, attrs, &result); 10982 } 10983 if (result == MEMTX_OK) { 10984 return data; 10985 } 10986 fi->type = ARMFault_SyncExternalOnWalk; 10987 fi->ea = arm_extabort_type(result); 10988 return 0; 10989 } 10990 10991 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure, 10992 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 10993 { 10994 ARMCPU *cpu = ARM_CPU(cs); 10995 CPUARMState *env = &cpu->env; 10996 MemTxAttrs attrs = {}; 10997 MemTxResult result = MEMTX_OK; 10998 AddressSpace *as; 10999 uint64_t data; 11000 11001 attrs.secure = is_secure; 11002 as = arm_addressspace(cs, attrs); 11003 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 11004 if (fi->s1ptw) { 11005 return 0; 11006 } 11007 if (regime_translation_big_endian(env, mmu_idx)) { 11008 data = address_space_ldq_be(as, addr, attrs, &result); 11009 } else { 11010 data = address_space_ldq_le(as, addr, attrs, &result); 11011 } 11012 if (result == MEMTX_OK) { 11013 return data; 11014 } 11015 fi->type = ARMFault_SyncExternalOnWalk; 11016 fi->ea = arm_extabort_type(result); 11017 return 0; 11018 } 11019 11020 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address, 11021 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11022 hwaddr *phys_ptr, int *prot, 11023 target_ulong *page_size, 11024 ARMMMUFaultInfo *fi) 11025 { 11026 CPUState *cs = env_cpu(env); 11027 int level = 1; 11028 uint32_t table; 11029 uint32_t desc; 11030 int type; 11031 int ap; 11032 int domain = 0; 11033 int domain_prot; 11034 hwaddr phys_addr; 11035 uint32_t dacr; 11036 11037 /* Pagetable walk. */ 11038 /* Lookup l1 descriptor. */ 11039 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 11040 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 11041 fi->type = ARMFault_Translation; 11042 goto do_fault; 11043 } 11044 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 11045 mmu_idx, fi); 11046 if (fi->type != ARMFault_None) { 11047 goto do_fault; 11048 } 11049 type = (desc & 3); 11050 domain = (desc >> 5) & 0x0f; 11051 if (regime_el(env, mmu_idx) == 1) { 11052 dacr = env->cp15.dacr_ns; 11053 } else { 11054 dacr = env->cp15.dacr_s; 11055 } 11056 domain_prot = (dacr >> (domain * 2)) & 3; 11057 if (type == 0) { 11058 /* Section translation fault. */ 11059 fi->type = ARMFault_Translation; 11060 goto do_fault; 11061 } 11062 if (type != 2) { 11063 level = 2; 11064 } 11065 if (domain_prot == 0 || domain_prot == 2) { 11066 fi->type = ARMFault_Domain; 11067 goto do_fault; 11068 } 11069 if (type == 2) { 11070 /* 1Mb section. */ 11071 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 11072 ap = (desc >> 10) & 3; 11073 *page_size = 1024 * 1024; 11074 } else { 11075 /* Lookup l2 entry. */ 11076 if (type == 1) { 11077 /* Coarse pagetable. */ 11078 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 11079 } else { 11080 /* Fine pagetable. */ 11081 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); 11082 } 11083 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 11084 mmu_idx, fi); 11085 if (fi->type != ARMFault_None) { 11086 goto do_fault; 11087 } 11088 switch (desc & 3) { 11089 case 0: /* Page translation fault. */ 11090 fi->type = ARMFault_Translation; 11091 goto do_fault; 11092 case 1: /* 64k page. */ 11093 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 11094 ap = (desc >> (4 + ((address >> 13) & 6))) & 3; 11095 *page_size = 0x10000; 11096 break; 11097 case 2: /* 4k page. */ 11098 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 11099 ap = (desc >> (4 + ((address >> 9) & 6))) & 3; 11100 *page_size = 0x1000; 11101 break; 11102 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */ 11103 if (type == 1) { 11104 /* ARMv6/XScale extended small page format */ 11105 if (arm_feature(env, ARM_FEATURE_XSCALE) 11106 || arm_feature(env, ARM_FEATURE_V6)) { 11107 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 11108 *page_size = 0x1000; 11109 } else { 11110 /* UNPREDICTABLE in ARMv5; we choose to take a 11111 * page translation fault. 11112 */ 11113 fi->type = ARMFault_Translation; 11114 goto do_fault; 11115 } 11116 } else { 11117 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); 11118 *page_size = 0x400; 11119 } 11120 ap = (desc >> 4) & 3; 11121 break; 11122 default: 11123 /* Never happens, but compiler isn't smart enough to tell. */ 11124 abort(); 11125 } 11126 } 11127 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 11128 *prot |= *prot ? PAGE_EXEC : 0; 11129 if (!(*prot & (1 << access_type))) { 11130 /* Access permission fault. */ 11131 fi->type = ARMFault_Permission; 11132 goto do_fault; 11133 } 11134 *phys_ptr = phys_addr; 11135 return false; 11136 do_fault: 11137 fi->domain = domain; 11138 fi->level = level; 11139 return true; 11140 } 11141 11142 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address, 11143 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11144 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 11145 target_ulong *page_size, ARMMMUFaultInfo *fi) 11146 { 11147 CPUState *cs = env_cpu(env); 11148 int level = 1; 11149 uint32_t table; 11150 uint32_t desc; 11151 uint32_t xn; 11152 uint32_t pxn = 0; 11153 int type; 11154 int ap; 11155 int domain = 0; 11156 int domain_prot; 11157 hwaddr phys_addr; 11158 uint32_t dacr; 11159 bool ns; 11160 11161 /* Pagetable walk. */ 11162 /* Lookup l1 descriptor. */ 11163 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 11164 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 11165 fi->type = ARMFault_Translation; 11166 goto do_fault; 11167 } 11168 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 11169 mmu_idx, fi); 11170 if (fi->type != ARMFault_None) { 11171 goto do_fault; 11172 } 11173 type = (desc & 3); 11174 if (type == 0 || (type == 3 && !arm_feature(env, ARM_FEATURE_PXN))) { 11175 /* Section translation fault, or attempt to use the encoding 11176 * which is Reserved on implementations without PXN. 11177 */ 11178 fi->type = ARMFault_Translation; 11179 goto do_fault; 11180 } 11181 if ((type == 1) || !(desc & (1 << 18))) { 11182 /* Page or Section. */ 11183 domain = (desc >> 5) & 0x0f; 11184 } 11185 if (regime_el(env, mmu_idx) == 1) { 11186 dacr = env->cp15.dacr_ns; 11187 } else { 11188 dacr = env->cp15.dacr_s; 11189 } 11190 if (type == 1) { 11191 level = 2; 11192 } 11193 domain_prot = (dacr >> (domain * 2)) & 3; 11194 if (domain_prot == 0 || domain_prot == 2) { 11195 /* Section or Page domain fault */ 11196 fi->type = ARMFault_Domain; 11197 goto do_fault; 11198 } 11199 if (type != 1) { 11200 if (desc & (1 << 18)) { 11201 /* Supersection. */ 11202 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); 11203 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32; 11204 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36; 11205 *page_size = 0x1000000; 11206 } else { 11207 /* Section. */ 11208 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 11209 *page_size = 0x100000; 11210 } 11211 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); 11212 xn = desc & (1 << 4); 11213 pxn = desc & 1; 11214 ns = extract32(desc, 19, 1); 11215 } else { 11216 if (arm_feature(env, ARM_FEATURE_PXN)) { 11217 pxn = (desc >> 2) & 1; 11218 } 11219 ns = extract32(desc, 3, 1); 11220 /* Lookup l2 entry. */ 11221 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 11222 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 11223 mmu_idx, fi); 11224 if (fi->type != ARMFault_None) { 11225 goto do_fault; 11226 } 11227 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); 11228 switch (desc & 3) { 11229 case 0: /* Page translation fault. */ 11230 fi->type = ARMFault_Translation; 11231 goto do_fault; 11232 case 1: /* 64k page. */ 11233 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 11234 xn = desc & (1 << 15); 11235 *page_size = 0x10000; 11236 break; 11237 case 2: case 3: /* 4k page. */ 11238 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 11239 xn = desc & 1; 11240 *page_size = 0x1000; 11241 break; 11242 default: 11243 /* Never happens, but compiler isn't smart enough to tell. */ 11244 abort(); 11245 } 11246 } 11247 if (domain_prot == 3) { 11248 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 11249 } else { 11250 if (pxn && !regime_is_user(env, mmu_idx)) { 11251 xn = 1; 11252 } 11253 if (xn && access_type == MMU_INST_FETCH) { 11254 fi->type = ARMFault_Permission; 11255 goto do_fault; 11256 } 11257 11258 if (arm_feature(env, ARM_FEATURE_V6K) && 11259 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) { 11260 /* The simplified model uses AP[0] as an access control bit. */ 11261 if ((ap & 1) == 0) { 11262 /* Access flag fault. */ 11263 fi->type = ARMFault_AccessFlag; 11264 goto do_fault; 11265 } 11266 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1); 11267 } else { 11268 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 11269 } 11270 if (*prot && !xn) { 11271 *prot |= PAGE_EXEC; 11272 } 11273 if (!(*prot & (1 << access_type))) { 11274 /* Access permission fault. */ 11275 fi->type = ARMFault_Permission; 11276 goto do_fault; 11277 } 11278 } 11279 if (ns) { 11280 /* The NS bit will (as required by the architecture) have no effect if 11281 * the CPU doesn't support TZ or this is a non-secure translation 11282 * regime, because the attribute will already be non-secure. 11283 */ 11284 attrs->secure = false; 11285 } 11286 *phys_ptr = phys_addr; 11287 return false; 11288 do_fault: 11289 fi->domain = domain; 11290 fi->level = level; 11291 return true; 11292 } 11293 11294 /* 11295 * check_s2_mmu_setup 11296 * @cpu: ARMCPU 11297 * @is_aa64: True if the translation regime is in AArch64 state 11298 * @startlevel: Suggested starting level 11299 * @inputsize: Bitsize of IPAs 11300 * @stride: Page-table stride (See the ARM ARM) 11301 * 11302 * Returns true if the suggested S2 translation parameters are OK and 11303 * false otherwise. 11304 */ 11305 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level, 11306 int inputsize, int stride) 11307 { 11308 const int grainsize = stride + 3; 11309 int startsizecheck; 11310 11311 /* Negative levels are never allowed. */ 11312 if (level < 0) { 11313 return false; 11314 } 11315 11316 startsizecheck = inputsize - ((3 - level) * stride + grainsize); 11317 if (startsizecheck < 1 || startsizecheck > stride + 4) { 11318 return false; 11319 } 11320 11321 if (is_aa64) { 11322 CPUARMState *env = &cpu->env; 11323 unsigned int pamax = arm_pamax(cpu); 11324 11325 switch (stride) { 11326 case 13: /* 64KB Pages. */ 11327 if (level == 0 || (level == 1 && pamax <= 42)) { 11328 return false; 11329 } 11330 break; 11331 case 11: /* 16KB Pages. */ 11332 if (level == 0 || (level == 1 && pamax <= 40)) { 11333 return false; 11334 } 11335 break; 11336 case 9: /* 4KB Pages. */ 11337 if (level == 0 && pamax <= 42) { 11338 return false; 11339 } 11340 break; 11341 default: 11342 g_assert_not_reached(); 11343 } 11344 11345 /* Inputsize checks. */ 11346 if (inputsize > pamax && 11347 (arm_el_is_aa64(env, 1) || inputsize > 40)) { 11348 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */ 11349 return false; 11350 } 11351 } else { 11352 /* AArch32 only supports 4KB pages. Assert on that. */ 11353 assert(stride == 9); 11354 11355 if (level == 0) { 11356 return false; 11357 } 11358 } 11359 return true; 11360 } 11361 11362 /* Translate from the 4-bit stage 2 representation of 11363 * memory attributes (without cache-allocation hints) to 11364 * the 8-bit representation of the stage 1 MAIR registers 11365 * (which includes allocation hints). 11366 * 11367 * ref: shared/translation/attrs/S2AttrDecode() 11368 * .../S2ConvertAttrsHints() 11369 */ 11370 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs) 11371 { 11372 uint8_t hiattr = extract32(s2attrs, 2, 2); 11373 uint8_t loattr = extract32(s2attrs, 0, 2); 11374 uint8_t hihint = 0, lohint = 0; 11375 11376 if (hiattr != 0) { /* normal memory */ 11377 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */ 11378 hiattr = loattr = 1; /* non-cacheable */ 11379 } else { 11380 if (hiattr != 1) { /* Write-through or write-back */ 11381 hihint = 3; /* RW allocate */ 11382 } 11383 if (loattr != 1) { /* Write-through or write-back */ 11384 lohint = 3; /* RW allocate */ 11385 } 11386 } 11387 } 11388 11389 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint; 11390 } 11391 #endif /* !CONFIG_USER_ONLY */ 11392 11393 ARMVAParameters aa64_va_parameters_both(CPUARMState *env, uint64_t va, 11394 ARMMMUIdx mmu_idx) 11395 { 11396 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr; 11397 uint32_t el = regime_el(env, mmu_idx); 11398 bool tbi, tbid, epd, hpd, using16k, using64k; 11399 int select, tsz; 11400 11401 /* 11402 * Bit 55 is always between the two regions, and is canonical for 11403 * determining if address tagging is enabled. 11404 */ 11405 select = extract64(va, 55, 1); 11406 11407 if (el > 1) { 11408 tsz = extract32(tcr, 0, 6); 11409 using64k = extract32(tcr, 14, 1); 11410 using16k = extract32(tcr, 15, 1); 11411 if (mmu_idx == ARMMMUIdx_S2NS) { 11412 /* VTCR_EL2 */ 11413 tbi = tbid = hpd = false; 11414 } else { 11415 tbi = extract32(tcr, 20, 1); 11416 hpd = extract32(tcr, 24, 1); 11417 tbid = extract32(tcr, 29, 1); 11418 } 11419 epd = false; 11420 } else if (!select) { 11421 tsz = extract32(tcr, 0, 6); 11422 epd = extract32(tcr, 7, 1); 11423 using64k = extract32(tcr, 14, 1); 11424 using16k = extract32(tcr, 15, 1); 11425 tbi = extract64(tcr, 37, 1); 11426 hpd = extract64(tcr, 41, 1); 11427 tbid = extract64(tcr, 51, 1); 11428 } else { 11429 int tg = extract32(tcr, 30, 2); 11430 using16k = tg == 1; 11431 using64k = tg == 3; 11432 tsz = extract32(tcr, 16, 6); 11433 epd = extract32(tcr, 23, 1); 11434 tbi = extract64(tcr, 38, 1); 11435 hpd = extract64(tcr, 42, 1); 11436 tbid = extract64(tcr, 52, 1); 11437 } 11438 tsz = MIN(tsz, 39); /* TODO: ARMv8.4-TTST */ 11439 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */ 11440 11441 return (ARMVAParameters) { 11442 .tsz = tsz, 11443 .select = select, 11444 .tbi = tbi, 11445 .tbid = tbid, 11446 .epd = epd, 11447 .hpd = hpd, 11448 .using16k = using16k, 11449 .using64k = using64k, 11450 }; 11451 } 11452 11453 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va, 11454 ARMMMUIdx mmu_idx, bool data) 11455 { 11456 ARMVAParameters ret = aa64_va_parameters_both(env, va, mmu_idx); 11457 11458 /* Present TBI as a composite with TBID. */ 11459 ret.tbi &= (data || !ret.tbid); 11460 return ret; 11461 } 11462 11463 #ifndef CONFIG_USER_ONLY 11464 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va, 11465 ARMMMUIdx mmu_idx) 11466 { 11467 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr; 11468 uint32_t el = regime_el(env, mmu_idx); 11469 int select, tsz; 11470 bool epd, hpd; 11471 11472 if (mmu_idx == ARMMMUIdx_S2NS) { 11473 /* VTCR */ 11474 bool sext = extract32(tcr, 4, 1); 11475 bool sign = extract32(tcr, 3, 1); 11476 11477 /* 11478 * If the sign-extend bit is not the same as t0sz[3], the result 11479 * is unpredictable. Flag this as a guest error. 11480 */ 11481 if (sign != sext) { 11482 qemu_log_mask(LOG_GUEST_ERROR, 11483 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n"); 11484 } 11485 tsz = sextract32(tcr, 0, 4) + 8; 11486 select = 0; 11487 hpd = false; 11488 epd = false; 11489 } else if (el == 2) { 11490 /* HTCR */ 11491 tsz = extract32(tcr, 0, 3); 11492 select = 0; 11493 hpd = extract64(tcr, 24, 1); 11494 epd = false; 11495 } else { 11496 int t0sz = extract32(tcr, 0, 3); 11497 int t1sz = extract32(tcr, 16, 3); 11498 11499 if (t1sz == 0) { 11500 select = va > (0xffffffffu >> t0sz); 11501 } else { 11502 /* Note that we will detect errors later. */ 11503 select = va >= ~(0xffffffffu >> t1sz); 11504 } 11505 if (!select) { 11506 tsz = t0sz; 11507 epd = extract32(tcr, 7, 1); 11508 hpd = extract64(tcr, 41, 1); 11509 } else { 11510 tsz = t1sz; 11511 epd = extract32(tcr, 23, 1); 11512 hpd = extract64(tcr, 42, 1); 11513 } 11514 /* For aarch32, hpd0 is not enabled without t2e as well. */ 11515 hpd &= extract32(tcr, 6, 1); 11516 } 11517 11518 return (ARMVAParameters) { 11519 .tsz = tsz, 11520 .select = select, 11521 .epd = epd, 11522 .hpd = hpd, 11523 }; 11524 } 11525 11526 static bool get_phys_addr_lpae(CPUARMState *env, target_ulong address, 11527 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11528 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 11529 target_ulong *page_size_ptr, 11530 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 11531 { 11532 ARMCPU *cpu = env_archcpu(env); 11533 CPUState *cs = CPU(cpu); 11534 /* Read an LPAE long-descriptor translation table. */ 11535 ARMFaultType fault_type = ARMFault_Translation; 11536 uint32_t level; 11537 ARMVAParameters param; 11538 uint64_t ttbr; 11539 hwaddr descaddr, indexmask, indexmask_grainsize; 11540 uint32_t tableattrs; 11541 target_ulong page_size; 11542 uint32_t attrs; 11543 int32_t stride; 11544 int addrsize, inputsize; 11545 TCR *tcr = regime_tcr(env, mmu_idx); 11546 int ap, ns, xn, pxn; 11547 uint32_t el = regime_el(env, mmu_idx); 11548 bool ttbr1_valid; 11549 uint64_t descaddrmask; 11550 bool aarch64 = arm_el_is_aa64(env, el); 11551 bool guarded = false; 11552 11553 /* TODO: 11554 * This code does not handle the different format TCR for VTCR_EL2. 11555 * This code also does not support shareability levels. 11556 * Attribute and permission bit handling should also be checked when adding 11557 * support for those page table walks. 11558 */ 11559 if (aarch64) { 11560 param = aa64_va_parameters(env, address, mmu_idx, 11561 access_type != MMU_INST_FETCH); 11562 level = 0; 11563 /* If we are in 64-bit EL2 or EL3 then there is no TTBR1, so mark it 11564 * invalid. 11565 */ 11566 ttbr1_valid = (el < 2); 11567 addrsize = 64 - 8 * param.tbi; 11568 inputsize = 64 - param.tsz; 11569 } else { 11570 param = aa32_va_parameters(env, address, mmu_idx); 11571 level = 1; 11572 /* There is no TTBR1 for EL2 */ 11573 ttbr1_valid = (el != 2); 11574 addrsize = (mmu_idx == ARMMMUIdx_S2NS ? 40 : 32); 11575 inputsize = addrsize - param.tsz; 11576 } 11577 11578 /* 11579 * We determined the region when collecting the parameters, but we 11580 * have not yet validated that the address is valid for the region. 11581 * Extract the top bits and verify that they all match select. 11582 * 11583 * For aa32, if inputsize == addrsize, then we have selected the 11584 * region by exclusion in aa32_va_parameters and there is no more 11585 * validation to do here. 11586 */ 11587 if (inputsize < addrsize) { 11588 target_ulong top_bits = sextract64(address, inputsize, 11589 addrsize - inputsize); 11590 if (-top_bits != param.select || (param.select && !ttbr1_valid)) { 11591 /* The gap between the two regions is a Translation fault */ 11592 fault_type = ARMFault_Translation; 11593 goto do_fault; 11594 } 11595 } 11596 11597 if (param.using64k) { 11598 stride = 13; 11599 } else if (param.using16k) { 11600 stride = 11; 11601 } else { 11602 stride = 9; 11603 } 11604 11605 /* Note that QEMU ignores shareability and cacheability attributes, 11606 * so we don't need to do anything with the SH, ORGN, IRGN fields 11607 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the 11608 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently 11609 * implement any ASID-like capability so we can ignore it (instead 11610 * we will always flush the TLB any time the ASID is changed). 11611 */ 11612 ttbr = regime_ttbr(env, mmu_idx, param.select); 11613 11614 /* Here we should have set up all the parameters for the translation: 11615 * inputsize, ttbr, epd, stride, tbi 11616 */ 11617 11618 if (param.epd) { 11619 /* Translation table walk disabled => Translation fault on TLB miss 11620 * Note: This is always 0 on 64-bit EL2 and EL3. 11621 */ 11622 goto do_fault; 11623 } 11624 11625 if (mmu_idx != ARMMMUIdx_S2NS) { 11626 /* The starting level depends on the virtual address size (which can 11627 * be up to 48 bits) and the translation granule size. It indicates 11628 * the number of strides (stride bits at a time) needed to 11629 * consume the bits of the input address. In the pseudocode this is: 11630 * level = 4 - RoundUp((inputsize - grainsize) / stride) 11631 * where their 'inputsize' is our 'inputsize', 'grainsize' is 11632 * our 'stride + 3' and 'stride' is our 'stride'. 11633 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying: 11634 * = 4 - (inputsize - stride - 3 + stride - 1) / stride 11635 * = 4 - (inputsize - 4) / stride; 11636 */ 11637 level = 4 - (inputsize - 4) / stride; 11638 } else { 11639 /* For stage 2 translations the starting level is specified by the 11640 * VTCR_EL2.SL0 field (whose interpretation depends on the page size) 11641 */ 11642 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2); 11643 uint32_t startlevel; 11644 bool ok; 11645 11646 if (!aarch64 || stride == 9) { 11647 /* AArch32 or 4KB pages */ 11648 startlevel = 2 - sl0; 11649 } else { 11650 /* 16KB or 64KB pages */ 11651 startlevel = 3 - sl0; 11652 } 11653 11654 /* Check that the starting level is valid. */ 11655 ok = check_s2_mmu_setup(cpu, aarch64, startlevel, 11656 inputsize, stride); 11657 if (!ok) { 11658 fault_type = ARMFault_Translation; 11659 goto do_fault; 11660 } 11661 level = startlevel; 11662 } 11663 11664 indexmask_grainsize = (1ULL << (stride + 3)) - 1; 11665 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1; 11666 11667 /* Now we can extract the actual base address from the TTBR */ 11668 descaddr = extract64(ttbr, 0, 48); 11669 descaddr &= ~indexmask; 11670 11671 /* The address field in the descriptor goes up to bit 39 for ARMv7 11672 * but up to bit 47 for ARMv8, but we use the descaddrmask 11673 * up to bit 39 for AArch32, because we don't need other bits in that case 11674 * to construct next descriptor address (anyway they should be all zeroes). 11675 */ 11676 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) & 11677 ~indexmask_grainsize; 11678 11679 /* Secure accesses start with the page table in secure memory and 11680 * can be downgraded to non-secure at any step. Non-secure accesses 11681 * remain non-secure. We implement this by just ORing in the NSTable/NS 11682 * bits at each step. 11683 */ 11684 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4); 11685 for (;;) { 11686 uint64_t descriptor; 11687 bool nstable; 11688 11689 descaddr |= (address >> (stride * (4 - level))) & indexmask; 11690 descaddr &= ~7ULL; 11691 nstable = extract32(tableattrs, 4, 1); 11692 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi); 11693 if (fi->type != ARMFault_None) { 11694 goto do_fault; 11695 } 11696 11697 if (!(descriptor & 1) || 11698 (!(descriptor & 2) && (level == 3))) { 11699 /* Invalid, or the Reserved level 3 encoding */ 11700 goto do_fault; 11701 } 11702 descaddr = descriptor & descaddrmask; 11703 11704 if ((descriptor & 2) && (level < 3)) { 11705 /* Table entry. The top five bits are attributes which may 11706 * propagate down through lower levels of the table (and 11707 * which are all arranged so that 0 means "no effect", so 11708 * we can gather them up by ORing in the bits at each level). 11709 */ 11710 tableattrs |= extract64(descriptor, 59, 5); 11711 level++; 11712 indexmask = indexmask_grainsize; 11713 continue; 11714 } 11715 /* Block entry at level 1 or 2, or page entry at level 3. 11716 * These are basically the same thing, although the number 11717 * of bits we pull in from the vaddr varies. 11718 */ 11719 page_size = (1ULL << ((stride * (4 - level)) + 3)); 11720 descaddr |= (address & (page_size - 1)); 11721 /* Extract attributes from the descriptor */ 11722 attrs = extract64(descriptor, 2, 10) 11723 | (extract64(descriptor, 52, 12) << 10); 11724 11725 if (mmu_idx == ARMMMUIdx_S2NS) { 11726 /* Stage 2 table descriptors do not include any attribute fields */ 11727 break; 11728 } 11729 /* Merge in attributes from table descriptors */ 11730 attrs |= nstable << 3; /* NS */ 11731 guarded = extract64(descriptor, 50, 1); /* GP */ 11732 if (param.hpd) { 11733 /* HPD disables all the table attributes except NSTable. */ 11734 break; 11735 } 11736 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */ 11737 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1 11738 * means "force PL1 access only", which means forcing AP[1] to 0. 11739 */ 11740 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */ 11741 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */ 11742 break; 11743 } 11744 /* Here descaddr is the final physical address, and attributes 11745 * are all in attrs. 11746 */ 11747 fault_type = ARMFault_AccessFlag; 11748 if ((attrs & (1 << 8)) == 0) { 11749 /* Access flag */ 11750 goto do_fault; 11751 } 11752 11753 ap = extract32(attrs, 4, 2); 11754 xn = extract32(attrs, 12, 1); 11755 11756 if (mmu_idx == ARMMMUIdx_S2NS) { 11757 ns = true; 11758 *prot = get_S2prot(env, ap, xn); 11759 } else { 11760 ns = extract32(attrs, 3, 1); 11761 pxn = extract32(attrs, 11, 1); 11762 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn); 11763 } 11764 11765 fault_type = ARMFault_Permission; 11766 if (!(*prot & (1 << access_type))) { 11767 goto do_fault; 11768 } 11769 11770 if (ns) { 11771 /* The NS bit will (as required by the architecture) have no effect if 11772 * the CPU doesn't support TZ or this is a non-secure translation 11773 * regime, because the attribute will already be non-secure. 11774 */ 11775 txattrs->secure = false; 11776 } 11777 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */ 11778 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) { 11779 txattrs->target_tlb_bit0 = true; 11780 } 11781 11782 if (cacheattrs != NULL) { 11783 if (mmu_idx == ARMMMUIdx_S2NS) { 11784 cacheattrs->attrs = convert_stage2_attrs(env, 11785 extract32(attrs, 0, 4)); 11786 } else { 11787 /* Index into MAIR registers for cache attributes */ 11788 uint8_t attrindx = extract32(attrs, 0, 3); 11789 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)]; 11790 assert(attrindx <= 7); 11791 cacheattrs->attrs = extract64(mair, attrindx * 8, 8); 11792 } 11793 cacheattrs->shareability = extract32(attrs, 6, 2); 11794 } 11795 11796 *phys_ptr = descaddr; 11797 *page_size_ptr = page_size; 11798 return false; 11799 11800 do_fault: 11801 fi->type = fault_type; 11802 fi->level = level; 11803 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */ 11804 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_S2NS); 11805 return true; 11806 } 11807 11808 static inline void get_phys_addr_pmsav7_default(CPUARMState *env, 11809 ARMMMUIdx mmu_idx, 11810 int32_t address, int *prot) 11811 { 11812 if (!arm_feature(env, ARM_FEATURE_M)) { 11813 *prot = PAGE_READ | PAGE_WRITE; 11814 switch (address) { 11815 case 0xF0000000 ... 0xFFFFFFFF: 11816 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { 11817 /* hivecs execing is ok */ 11818 *prot |= PAGE_EXEC; 11819 } 11820 break; 11821 case 0x00000000 ... 0x7FFFFFFF: 11822 *prot |= PAGE_EXEC; 11823 break; 11824 } 11825 } else { 11826 /* Default system address map for M profile cores. 11827 * The architecture specifies which regions are execute-never; 11828 * at the MPU level no other checks are defined. 11829 */ 11830 switch (address) { 11831 case 0x00000000 ... 0x1fffffff: /* ROM */ 11832 case 0x20000000 ... 0x3fffffff: /* SRAM */ 11833 case 0x60000000 ... 0x7fffffff: /* RAM */ 11834 case 0x80000000 ... 0x9fffffff: /* RAM */ 11835 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 11836 break; 11837 case 0x40000000 ... 0x5fffffff: /* Peripheral */ 11838 case 0xa0000000 ... 0xbfffffff: /* Device */ 11839 case 0xc0000000 ... 0xdfffffff: /* Device */ 11840 case 0xe0000000 ... 0xffffffff: /* System */ 11841 *prot = PAGE_READ | PAGE_WRITE; 11842 break; 11843 default: 11844 g_assert_not_reached(); 11845 } 11846 } 11847 } 11848 11849 static bool pmsav7_use_background_region(ARMCPU *cpu, 11850 ARMMMUIdx mmu_idx, bool is_user) 11851 { 11852 /* Return true if we should use the default memory map as a 11853 * "background" region if there are no hits against any MPU regions. 11854 */ 11855 CPUARMState *env = &cpu->env; 11856 11857 if (is_user) { 11858 return false; 11859 } 11860 11861 if (arm_feature(env, ARM_FEATURE_M)) { 11862 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] 11863 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK; 11864 } else { 11865 return regime_sctlr(env, mmu_idx) & SCTLR_BR; 11866 } 11867 } 11868 11869 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address) 11870 { 11871 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */ 11872 return arm_feature(env, ARM_FEATURE_M) && 11873 extract32(address, 20, 12) == 0xe00; 11874 } 11875 11876 static inline bool m_is_system_region(CPUARMState *env, uint32_t address) 11877 { 11878 /* True if address is in the M profile system region 11879 * 0xe0000000 - 0xffffffff 11880 */ 11881 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7; 11882 } 11883 11884 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address, 11885 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11886 hwaddr *phys_ptr, int *prot, 11887 target_ulong *page_size, 11888 ARMMMUFaultInfo *fi) 11889 { 11890 ARMCPU *cpu = env_archcpu(env); 11891 int n; 11892 bool is_user = regime_is_user(env, mmu_idx); 11893 11894 *phys_ptr = address; 11895 *page_size = TARGET_PAGE_SIZE; 11896 *prot = 0; 11897 11898 if (regime_translation_disabled(env, mmu_idx) || 11899 m_is_ppb_region(env, address)) { 11900 /* MPU disabled or M profile PPB access: use default memory map. 11901 * The other case which uses the default memory map in the 11902 * v7M ARM ARM pseudocode is exception vector reads from the vector 11903 * table. In QEMU those accesses are done in arm_v7m_load_vector(), 11904 * which always does a direct read using address_space_ldl(), rather 11905 * than going via this function, so we don't need to check that here. 11906 */ 11907 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 11908 } else { /* MPU enabled */ 11909 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 11910 /* region search */ 11911 uint32_t base = env->pmsav7.drbar[n]; 11912 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5); 11913 uint32_t rmask; 11914 bool srdis = false; 11915 11916 if (!(env->pmsav7.drsr[n] & 0x1)) { 11917 continue; 11918 } 11919 11920 if (!rsize) { 11921 qemu_log_mask(LOG_GUEST_ERROR, 11922 "DRSR[%d]: Rsize field cannot be 0\n", n); 11923 continue; 11924 } 11925 rsize++; 11926 rmask = (1ull << rsize) - 1; 11927 11928 if (base & rmask) { 11929 qemu_log_mask(LOG_GUEST_ERROR, 11930 "DRBAR[%d]: 0x%" PRIx32 " misaligned " 11931 "to DRSR region size, mask = 0x%" PRIx32 "\n", 11932 n, base, rmask); 11933 continue; 11934 } 11935 11936 if (address < base || address > base + rmask) { 11937 /* 11938 * Address not in this region. We must check whether the 11939 * region covers addresses in the same page as our address. 11940 * In that case we must not report a size that covers the 11941 * whole page for a subsequent hit against a different MPU 11942 * region or the background region, because it would result in 11943 * incorrect TLB hits for subsequent accesses to addresses that 11944 * are in this MPU region. 11945 */ 11946 if (ranges_overlap(base, rmask, 11947 address & TARGET_PAGE_MASK, 11948 TARGET_PAGE_SIZE)) { 11949 *page_size = 1; 11950 } 11951 continue; 11952 } 11953 11954 /* Region matched */ 11955 11956 if (rsize >= 8) { /* no subregions for regions < 256 bytes */ 11957 int i, snd; 11958 uint32_t srdis_mask; 11959 11960 rsize -= 3; /* sub region size (power of 2) */ 11961 snd = ((address - base) >> rsize) & 0x7; 11962 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1); 11963 11964 srdis_mask = srdis ? 0x3 : 0x0; 11965 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) { 11966 /* This will check in groups of 2, 4 and then 8, whether 11967 * the subregion bits are consistent. rsize is incremented 11968 * back up to give the region size, considering consistent 11969 * adjacent subregions as one region. Stop testing if rsize 11970 * is already big enough for an entire QEMU page. 11971 */ 11972 int snd_rounded = snd & ~(i - 1); 11973 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n], 11974 snd_rounded + 8, i); 11975 if (srdis_mask ^ srdis_multi) { 11976 break; 11977 } 11978 srdis_mask = (srdis_mask << i) | srdis_mask; 11979 rsize++; 11980 } 11981 } 11982 if (srdis) { 11983 continue; 11984 } 11985 if (rsize < TARGET_PAGE_BITS) { 11986 *page_size = 1 << rsize; 11987 } 11988 break; 11989 } 11990 11991 if (n == -1) { /* no hits */ 11992 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 11993 /* background fault */ 11994 fi->type = ARMFault_Background; 11995 return true; 11996 } 11997 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 11998 } else { /* a MPU hit! */ 11999 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3); 12000 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1); 12001 12002 if (m_is_system_region(env, address)) { 12003 /* System space is always execute never */ 12004 xn = 1; 12005 } 12006 12007 if (is_user) { /* User mode AP bit decoding */ 12008 switch (ap) { 12009 case 0: 12010 case 1: 12011 case 5: 12012 break; /* no access */ 12013 case 3: 12014 *prot |= PAGE_WRITE; 12015 /* fall through */ 12016 case 2: 12017 case 6: 12018 *prot |= PAGE_READ | PAGE_EXEC; 12019 break; 12020 case 7: 12021 /* for v7M, same as 6; for R profile a reserved value */ 12022 if (arm_feature(env, ARM_FEATURE_M)) { 12023 *prot |= PAGE_READ | PAGE_EXEC; 12024 break; 12025 } 12026 /* fall through */ 12027 default: 12028 qemu_log_mask(LOG_GUEST_ERROR, 12029 "DRACR[%d]: Bad value for AP bits: 0x%" 12030 PRIx32 "\n", n, ap); 12031 } 12032 } else { /* Priv. mode AP bits decoding */ 12033 switch (ap) { 12034 case 0: 12035 break; /* no access */ 12036 case 1: 12037 case 2: 12038 case 3: 12039 *prot |= PAGE_WRITE; 12040 /* fall through */ 12041 case 5: 12042 case 6: 12043 *prot |= PAGE_READ | PAGE_EXEC; 12044 break; 12045 case 7: 12046 /* for v7M, same as 6; for R profile a reserved value */ 12047 if (arm_feature(env, ARM_FEATURE_M)) { 12048 *prot |= PAGE_READ | PAGE_EXEC; 12049 break; 12050 } 12051 /* fall through */ 12052 default: 12053 qemu_log_mask(LOG_GUEST_ERROR, 12054 "DRACR[%d]: Bad value for AP bits: 0x%" 12055 PRIx32 "\n", n, ap); 12056 } 12057 } 12058 12059 /* execute never */ 12060 if (xn) { 12061 *prot &= ~PAGE_EXEC; 12062 } 12063 } 12064 } 12065 12066 fi->type = ARMFault_Permission; 12067 fi->level = 1; 12068 return !(*prot & (1 << access_type)); 12069 } 12070 12071 static bool v8m_is_sau_exempt(CPUARMState *env, 12072 uint32_t address, MMUAccessType access_type) 12073 { 12074 /* The architecture specifies that certain address ranges are 12075 * exempt from v8M SAU/IDAU checks. 12076 */ 12077 return 12078 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) || 12079 (address >= 0xe0000000 && address <= 0xe0002fff) || 12080 (address >= 0xe000e000 && address <= 0xe000efff) || 12081 (address >= 0xe002e000 && address <= 0xe002efff) || 12082 (address >= 0xe0040000 && address <= 0xe0041fff) || 12083 (address >= 0xe00ff000 && address <= 0xe00fffff); 12084 } 12085 12086 void v8m_security_lookup(CPUARMState *env, uint32_t address, 12087 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12088 V8M_SAttributes *sattrs) 12089 { 12090 /* Look up the security attributes for this address. Compare the 12091 * pseudocode SecurityCheck() function. 12092 * We assume the caller has zero-initialized *sattrs. 12093 */ 12094 ARMCPU *cpu = env_archcpu(env); 12095 int r; 12096 bool idau_exempt = false, idau_ns = true, idau_nsc = true; 12097 int idau_region = IREGION_NOTVALID; 12098 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 12099 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 12100 12101 if (cpu->idau) { 12102 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau); 12103 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau); 12104 12105 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns, 12106 &idau_nsc); 12107 } 12108 12109 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) { 12110 /* 0xf0000000..0xffffffff is always S for insn fetches */ 12111 return; 12112 } 12113 12114 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) { 12115 sattrs->ns = !regime_is_secure(env, mmu_idx); 12116 return; 12117 } 12118 12119 if (idau_region != IREGION_NOTVALID) { 12120 sattrs->irvalid = true; 12121 sattrs->iregion = idau_region; 12122 } 12123 12124 switch (env->sau.ctrl & 3) { 12125 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */ 12126 break; 12127 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */ 12128 sattrs->ns = true; 12129 break; 12130 default: /* SAU.ENABLE == 1 */ 12131 for (r = 0; r < cpu->sau_sregion; r++) { 12132 if (env->sau.rlar[r] & 1) { 12133 uint32_t base = env->sau.rbar[r] & ~0x1f; 12134 uint32_t limit = env->sau.rlar[r] | 0x1f; 12135 12136 if (base <= address && limit >= address) { 12137 if (base > addr_page_base || limit < addr_page_limit) { 12138 sattrs->subpage = true; 12139 } 12140 if (sattrs->srvalid) { 12141 /* If we hit in more than one region then we must report 12142 * as Secure, not NS-Callable, with no valid region 12143 * number info. 12144 */ 12145 sattrs->ns = false; 12146 sattrs->nsc = false; 12147 sattrs->sregion = 0; 12148 sattrs->srvalid = false; 12149 break; 12150 } else { 12151 if (env->sau.rlar[r] & 2) { 12152 sattrs->nsc = true; 12153 } else { 12154 sattrs->ns = true; 12155 } 12156 sattrs->srvalid = true; 12157 sattrs->sregion = r; 12158 } 12159 } else { 12160 /* 12161 * Address not in this region. We must check whether the 12162 * region covers addresses in the same page as our address. 12163 * In that case we must not report a size that covers the 12164 * whole page for a subsequent hit against a different MPU 12165 * region or the background region, because it would result 12166 * in incorrect TLB hits for subsequent accesses to 12167 * addresses that are in this MPU region. 12168 */ 12169 if (limit >= base && 12170 ranges_overlap(base, limit - base + 1, 12171 addr_page_base, 12172 TARGET_PAGE_SIZE)) { 12173 sattrs->subpage = true; 12174 } 12175 } 12176 } 12177 } 12178 break; 12179 } 12180 12181 /* 12182 * The IDAU will override the SAU lookup results if it specifies 12183 * higher security than the SAU does. 12184 */ 12185 if (!idau_ns) { 12186 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) { 12187 sattrs->ns = false; 12188 sattrs->nsc = idau_nsc; 12189 } 12190 } 12191 } 12192 12193 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address, 12194 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12195 hwaddr *phys_ptr, MemTxAttrs *txattrs, 12196 int *prot, bool *is_subpage, 12197 ARMMMUFaultInfo *fi, uint32_t *mregion) 12198 { 12199 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check 12200 * that a full phys-to-virt translation does). 12201 * mregion is (if not NULL) set to the region number which matched, 12202 * or -1 if no region number is returned (MPU off, address did not 12203 * hit a region, address hit in multiple regions). 12204 * We set is_subpage to true if the region hit doesn't cover the 12205 * entire TARGET_PAGE the address is within. 12206 */ 12207 ARMCPU *cpu = env_archcpu(env); 12208 bool is_user = regime_is_user(env, mmu_idx); 12209 uint32_t secure = regime_is_secure(env, mmu_idx); 12210 int n; 12211 int matchregion = -1; 12212 bool hit = false; 12213 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 12214 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 12215 12216 *is_subpage = false; 12217 *phys_ptr = address; 12218 *prot = 0; 12219 if (mregion) { 12220 *mregion = -1; 12221 } 12222 12223 /* Unlike the ARM ARM pseudocode, we don't need to check whether this 12224 * was an exception vector read from the vector table (which is always 12225 * done using the default system address map), because those accesses 12226 * are done in arm_v7m_load_vector(), which always does a direct 12227 * read using address_space_ldl(), rather than going via this function. 12228 */ 12229 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */ 12230 hit = true; 12231 } else if (m_is_ppb_region(env, address)) { 12232 hit = true; 12233 } else { 12234 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 12235 hit = true; 12236 } 12237 12238 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 12239 /* region search */ 12240 /* Note that the base address is bits [31:5] from the register 12241 * with bits [4:0] all zeroes, but the limit address is bits 12242 * [31:5] from the register with bits [4:0] all ones. 12243 */ 12244 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f; 12245 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f; 12246 12247 if (!(env->pmsav8.rlar[secure][n] & 0x1)) { 12248 /* Region disabled */ 12249 continue; 12250 } 12251 12252 if (address < base || address > limit) { 12253 /* 12254 * Address not in this region. We must check whether the 12255 * region covers addresses in the same page as our address. 12256 * In that case we must not report a size that covers the 12257 * whole page for a subsequent hit against a different MPU 12258 * region or the background region, because it would result in 12259 * incorrect TLB hits for subsequent accesses to addresses that 12260 * are in this MPU region. 12261 */ 12262 if (limit >= base && 12263 ranges_overlap(base, limit - base + 1, 12264 addr_page_base, 12265 TARGET_PAGE_SIZE)) { 12266 *is_subpage = true; 12267 } 12268 continue; 12269 } 12270 12271 if (base > addr_page_base || limit < addr_page_limit) { 12272 *is_subpage = true; 12273 } 12274 12275 if (matchregion != -1) { 12276 /* Multiple regions match -- always a failure (unlike 12277 * PMSAv7 where highest-numbered-region wins) 12278 */ 12279 fi->type = ARMFault_Permission; 12280 fi->level = 1; 12281 return true; 12282 } 12283 12284 matchregion = n; 12285 hit = true; 12286 } 12287 } 12288 12289 if (!hit) { 12290 /* background fault */ 12291 fi->type = ARMFault_Background; 12292 return true; 12293 } 12294 12295 if (matchregion == -1) { 12296 /* hit using the background region */ 12297 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 12298 } else { 12299 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2); 12300 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1); 12301 12302 if (m_is_system_region(env, address)) { 12303 /* System space is always execute never */ 12304 xn = 1; 12305 } 12306 12307 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap); 12308 if (*prot && !xn) { 12309 *prot |= PAGE_EXEC; 12310 } 12311 /* We don't need to look the attribute up in the MAIR0/MAIR1 12312 * registers because that only tells us about cacheability. 12313 */ 12314 if (mregion) { 12315 *mregion = matchregion; 12316 } 12317 } 12318 12319 fi->type = ARMFault_Permission; 12320 fi->level = 1; 12321 return !(*prot & (1 << access_type)); 12322 } 12323 12324 12325 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address, 12326 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12327 hwaddr *phys_ptr, MemTxAttrs *txattrs, 12328 int *prot, target_ulong *page_size, 12329 ARMMMUFaultInfo *fi) 12330 { 12331 uint32_t secure = regime_is_secure(env, mmu_idx); 12332 V8M_SAttributes sattrs = {}; 12333 bool ret; 12334 bool mpu_is_subpage; 12335 12336 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 12337 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs); 12338 if (access_type == MMU_INST_FETCH) { 12339 /* Instruction fetches always use the MMU bank and the 12340 * transaction attribute determined by the fetch address, 12341 * regardless of CPU state. This is painful for QEMU 12342 * to handle, because it would mean we need to encode 12343 * into the mmu_idx not just the (user, negpri) information 12344 * for the current security state but also that for the 12345 * other security state, which would balloon the number 12346 * of mmu_idx values needed alarmingly. 12347 * Fortunately we can avoid this because it's not actually 12348 * possible to arbitrarily execute code from memory with 12349 * the wrong security attribute: it will always generate 12350 * an exception of some kind or another, apart from the 12351 * special case of an NS CPU executing an SG instruction 12352 * in S&NSC memory. So we always just fail the translation 12353 * here and sort things out in the exception handler 12354 * (including possibly emulating an SG instruction). 12355 */ 12356 if (sattrs.ns != !secure) { 12357 if (sattrs.nsc) { 12358 fi->type = ARMFault_QEMU_NSCExec; 12359 } else { 12360 fi->type = ARMFault_QEMU_SFault; 12361 } 12362 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 12363 *phys_ptr = address; 12364 *prot = 0; 12365 return true; 12366 } 12367 } else { 12368 /* For data accesses we always use the MMU bank indicated 12369 * by the current CPU state, but the security attributes 12370 * might downgrade a secure access to nonsecure. 12371 */ 12372 if (sattrs.ns) { 12373 txattrs->secure = false; 12374 } else if (!secure) { 12375 /* NS access to S memory must fault. 12376 * Architecturally we should first check whether the 12377 * MPU information for this address indicates that we 12378 * are doing an unaligned access to Device memory, which 12379 * should generate a UsageFault instead. QEMU does not 12380 * currently check for that kind of unaligned access though. 12381 * If we added it we would need to do so as a special case 12382 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt(). 12383 */ 12384 fi->type = ARMFault_QEMU_SFault; 12385 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 12386 *phys_ptr = address; 12387 *prot = 0; 12388 return true; 12389 } 12390 } 12391 } 12392 12393 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr, 12394 txattrs, prot, &mpu_is_subpage, fi, NULL); 12395 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE; 12396 return ret; 12397 } 12398 12399 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address, 12400 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12401 hwaddr *phys_ptr, int *prot, 12402 ARMMMUFaultInfo *fi) 12403 { 12404 int n; 12405 uint32_t mask; 12406 uint32_t base; 12407 bool is_user = regime_is_user(env, mmu_idx); 12408 12409 if (regime_translation_disabled(env, mmu_idx)) { 12410 /* MPU disabled. */ 12411 *phys_ptr = address; 12412 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 12413 return false; 12414 } 12415 12416 *phys_ptr = address; 12417 for (n = 7; n >= 0; n--) { 12418 base = env->cp15.c6_region[n]; 12419 if ((base & 1) == 0) { 12420 continue; 12421 } 12422 mask = 1 << ((base >> 1) & 0x1f); 12423 /* Keep this shift separate from the above to avoid an 12424 (undefined) << 32. */ 12425 mask = (mask << 1) - 1; 12426 if (((base ^ address) & ~mask) == 0) { 12427 break; 12428 } 12429 } 12430 if (n < 0) { 12431 fi->type = ARMFault_Background; 12432 return true; 12433 } 12434 12435 if (access_type == MMU_INST_FETCH) { 12436 mask = env->cp15.pmsav5_insn_ap; 12437 } else { 12438 mask = env->cp15.pmsav5_data_ap; 12439 } 12440 mask = (mask >> (n * 4)) & 0xf; 12441 switch (mask) { 12442 case 0: 12443 fi->type = ARMFault_Permission; 12444 fi->level = 1; 12445 return true; 12446 case 1: 12447 if (is_user) { 12448 fi->type = ARMFault_Permission; 12449 fi->level = 1; 12450 return true; 12451 } 12452 *prot = PAGE_READ | PAGE_WRITE; 12453 break; 12454 case 2: 12455 *prot = PAGE_READ; 12456 if (!is_user) { 12457 *prot |= PAGE_WRITE; 12458 } 12459 break; 12460 case 3: 12461 *prot = PAGE_READ | PAGE_WRITE; 12462 break; 12463 case 5: 12464 if (is_user) { 12465 fi->type = ARMFault_Permission; 12466 fi->level = 1; 12467 return true; 12468 } 12469 *prot = PAGE_READ; 12470 break; 12471 case 6: 12472 *prot = PAGE_READ; 12473 break; 12474 default: 12475 /* Bad permission. */ 12476 fi->type = ARMFault_Permission; 12477 fi->level = 1; 12478 return true; 12479 } 12480 *prot |= PAGE_EXEC; 12481 return false; 12482 } 12483 12484 /* Combine either inner or outer cacheability attributes for normal 12485 * memory, according to table D4-42 and pseudocode procedure 12486 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM). 12487 * 12488 * NB: only stage 1 includes allocation hints (RW bits), leading to 12489 * some asymmetry. 12490 */ 12491 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2) 12492 { 12493 if (s1 == 4 || s2 == 4) { 12494 /* non-cacheable has precedence */ 12495 return 4; 12496 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) { 12497 /* stage 1 write-through takes precedence */ 12498 return s1; 12499 } else if (extract32(s2, 2, 2) == 2) { 12500 /* stage 2 write-through takes precedence, but the allocation hint 12501 * is still taken from stage 1 12502 */ 12503 return (2 << 2) | extract32(s1, 0, 2); 12504 } else { /* write-back */ 12505 return s1; 12506 } 12507 } 12508 12509 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4 12510 * and CombineS1S2Desc() 12511 * 12512 * @s1: Attributes from stage 1 walk 12513 * @s2: Attributes from stage 2 walk 12514 */ 12515 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2) 12516 { 12517 uint8_t s1lo = extract32(s1.attrs, 0, 4), s2lo = extract32(s2.attrs, 0, 4); 12518 uint8_t s1hi = extract32(s1.attrs, 4, 4), s2hi = extract32(s2.attrs, 4, 4); 12519 ARMCacheAttrs ret; 12520 12521 /* Combine shareability attributes (table D4-43) */ 12522 if (s1.shareability == 2 || s2.shareability == 2) { 12523 /* if either are outer-shareable, the result is outer-shareable */ 12524 ret.shareability = 2; 12525 } else if (s1.shareability == 3 || s2.shareability == 3) { 12526 /* if either are inner-shareable, the result is inner-shareable */ 12527 ret.shareability = 3; 12528 } else { 12529 /* both non-shareable */ 12530 ret.shareability = 0; 12531 } 12532 12533 /* Combine memory type and cacheability attributes */ 12534 if (s1hi == 0 || s2hi == 0) { 12535 /* Device has precedence over normal */ 12536 if (s1lo == 0 || s2lo == 0) { 12537 /* nGnRnE has precedence over anything */ 12538 ret.attrs = 0; 12539 } else if (s1lo == 4 || s2lo == 4) { 12540 /* non-Reordering has precedence over Reordering */ 12541 ret.attrs = 4; /* nGnRE */ 12542 } else if (s1lo == 8 || s2lo == 8) { 12543 /* non-Gathering has precedence over Gathering */ 12544 ret.attrs = 8; /* nGRE */ 12545 } else { 12546 ret.attrs = 0xc; /* GRE */ 12547 } 12548 12549 /* Any location for which the resultant memory type is any 12550 * type of Device memory is always treated as Outer Shareable. 12551 */ 12552 ret.shareability = 2; 12553 } else { /* Normal memory */ 12554 /* Outer/inner cacheability combine independently */ 12555 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4 12556 | combine_cacheattr_nibble(s1lo, s2lo); 12557 12558 if (ret.attrs == 0x44) { 12559 /* Any location for which the resultant memory type is Normal 12560 * Inner Non-cacheable, Outer Non-cacheable is always treated 12561 * as Outer Shareable. 12562 */ 12563 ret.shareability = 2; 12564 } 12565 } 12566 12567 return ret; 12568 } 12569 12570 12571 /* get_phys_addr - get the physical address for this virtual address 12572 * 12573 * Find the physical address corresponding to the given virtual address, 12574 * by doing a translation table walk on MMU based systems or using the 12575 * MPU state on MPU based systems. 12576 * 12577 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs, 12578 * prot and page_size may not be filled in, and the populated fsr value provides 12579 * information on why the translation aborted, in the format of a 12580 * DFSR/IFSR fault register, with the following caveats: 12581 * * we honour the short vs long DFSR format differences. 12582 * * the WnR bit is never set (the caller must do this). 12583 * * for PSMAv5 based systems we don't bother to return a full FSR format 12584 * value. 12585 * 12586 * @env: CPUARMState 12587 * @address: virtual address to get physical address for 12588 * @access_type: 0 for read, 1 for write, 2 for execute 12589 * @mmu_idx: MMU index indicating required translation regime 12590 * @phys_ptr: set to the physical address corresponding to the virtual address 12591 * @attrs: set to the memory transaction attributes to use 12592 * @prot: set to the permissions for the page containing phys_ptr 12593 * @page_size: set to the size of the page containing phys_ptr 12594 * @fi: set to fault info if the translation fails 12595 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes 12596 */ 12597 bool get_phys_addr(CPUARMState *env, target_ulong address, 12598 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12599 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 12600 target_ulong *page_size, 12601 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 12602 { 12603 if (mmu_idx == ARMMMUIdx_S12NSE0 || mmu_idx == ARMMMUIdx_S12NSE1) { 12604 /* Call ourselves recursively to do the stage 1 and then stage 2 12605 * translations. 12606 */ 12607 if (arm_feature(env, ARM_FEATURE_EL2)) { 12608 hwaddr ipa; 12609 int s2_prot; 12610 int ret; 12611 ARMCacheAttrs cacheattrs2 = {}; 12612 12613 ret = get_phys_addr(env, address, access_type, 12614 stage_1_mmu_idx(mmu_idx), &ipa, attrs, 12615 prot, page_size, fi, cacheattrs); 12616 12617 /* If S1 fails or S2 is disabled, return early. */ 12618 if (ret || regime_translation_disabled(env, ARMMMUIdx_S2NS)) { 12619 *phys_ptr = ipa; 12620 return ret; 12621 } 12622 12623 /* S1 is done. Now do S2 translation. */ 12624 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_S2NS, 12625 phys_ptr, attrs, &s2_prot, 12626 page_size, fi, 12627 cacheattrs != NULL ? &cacheattrs2 : NULL); 12628 fi->s2addr = ipa; 12629 /* Combine the S1 and S2 perms. */ 12630 *prot &= s2_prot; 12631 12632 /* Combine the S1 and S2 cache attributes, if needed */ 12633 if (!ret && cacheattrs != NULL) { 12634 if (env->cp15.hcr_el2 & HCR_DC) { 12635 /* 12636 * HCR.DC forces the first stage attributes to 12637 * Normal Non-Shareable, 12638 * Inner Write-Back Read-Allocate Write-Allocate, 12639 * Outer Write-Back Read-Allocate Write-Allocate. 12640 */ 12641 cacheattrs->attrs = 0xff; 12642 cacheattrs->shareability = 0; 12643 } 12644 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2); 12645 } 12646 12647 return ret; 12648 } else { 12649 /* 12650 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1. 12651 */ 12652 mmu_idx = stage_1_mmu_idx(mmu_idx); 12653 } 12654 } 12655 12656 /* The page table entries may downgrade secure to non-secure, but 12657 * cannot upgrade an non-secure translation regime's attributes 12658 * to secure. 12659 */ 12660 attrs->secure = regime_is_secure(env, mmu_idx); 12661 attrs->user = regime_is_user(env, mmu_idx); 12662 12663 /* Fast Context Switch Extension. This doesn't exist at all in v8. 12664 * In v7 and earlier it affects all stage 1 translations. 12665 */ 12666 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_S2NS 12667 && !arm_feature(env, ARM_FEATURE_V8)) { 12668 if (regime_el(env, mmu_idx) == 3) { 12669 address += env->cp15.fcseidr_s; 12670 } else { 12671 address += env->cp15.fcseidr_ns; 12672 } 12673 } 12674 12675 if (arm_feature(env, ARM_FEATURE_PMSA)) { 12676 bool ret; 12677 *page_size = TARGET_PAGE_SIZE; 12678 12679 if (arm_feature(env, ARM_FEATURE_V8)) { 12680 /* PMSAv8 */ 12681 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx, 12682 phys_ptr, attrs, prot, page_size, fi); 12683 } else if (arm_feature(env, ARM_FEATURE_V7)) { 12684 /* PMSAv7 */ 12685 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx, 12686 phys_ptr, prot, page_size, fi); 12687 } else { 12688 /* Pre-v7 MPU */ 12689 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx, 12690 phys_ptr, prot, fi); 12691 } 12692 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32 12693 " mmu_idx %u -> %s (prot %c%c%c)\n", 12694 access_type == MMU_DATA_LOAD ? "reading" : 12695 (access_type == MMU_DATA_STORE ? "writing" : "execute"), 12696 (uint32_t)address, mmu_idx, 12697 ret ? "Miss" : "Hit", 12698 *prot & PAGE_READ ? 'r' : '-', 12699 *prot & PAGE_WRITE ? 'w' : '-', 12700 *prot & PAGE_EXEC ? 'x' : '-'); 12701 12702 return ret; 12703 } 12704 12705 /* Definitely a real MMU, not an MPU */ 12706 12707 if (regime_translation_disabled(env, mmu_idx)) { 12708 /* MMU disabled. */ 12709 *phys_ptr = address; 12710 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 12711 *page_size = TARGET_PAGE_SIZE; 12712 return 0; 12713 } 12714 12715 if (regime_using_lpae_format(env, mmu_idx)) { 12716 return get_phys_addr_lpae(env, address, access_type, mmu_idx, 12717 phys_ptr, attrs, prot, page_size, 12718 fi, cacheattrs); 12719 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) { 12720 return get_phys_addr_v6(env, address, access_type, mmu_idx, 12721 phys_ptr, attrs, prot, page_size, fi); 12722 } else { 12723 return get_phys_addr_v5(env, address, access_type, mmu_idx, 12724 phys_ptr, prot, page_size, fi); 12725 } 12726 } 12727 12728 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr, 12729 MemTxAttrs *attrs) 12730 { 12731 ARMCPU *cpu = ARM_CPU(cs); 12732 CPUARMState *env = &cpu->env; 12733 hwaddr phys_addr; 12734 target_ulong page_size; 12735 int prot; 12736 bool ret; 12737 ARMMMUFaultInfo fi = {}; 12738 ARMMMUIdx mmu_idx = arm_mmu_idx(env); 12739 12740 *attrs = (MemTxAttrs) {}; 12741 12742 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr, 12743 attrs, &prot, &page_size, &fi, NULL); 12744 12745 if (ret) { 12746 return -1; 12747 } 12748 return phys_addr; 12749 } 12750 12751 uint32_t HELPER(v7m_mrs)(CPUARMState *env, uint32_t reg) 12752 { 12753 uint32_t mask; 12754 unsigned el = arm_current_el(env); 12755 12756 /* First handle registers which unprivileged can read */ 12757 12758 switch (reg) { 12759 case 0 ... 7: /* xPSR sub-fields */ 12760 mask = 0; 12761 if ((reg & 1) && el) { 12762 mask |= XPSR_EXCP; /* IPSR (unpriv. reads as zero) */ 12763 } 12764 if (!(reg & 4)) { 12765 mask |= XPSR_NZCV | XPSR_Q; /* APSR */ 12766 if (arm_feature(env, ARM_FEATURE_THUMB_DSP)) { 12767 mask |= XPSR_GE; 12768 } 12769 } 12770 /* EPSR reads as zero */ 12771 return xpsr_read(env) & mask; 12772 break; 12773 case 20: /* CONTROL */ 12774 { 12775 uint32_t value = env->v7m.control[env->v7m.secure]; 12776 if (!env->v7m.secure) { 12777 /* SFPA is RAZ/WI from NS; FPCA is stored in the M_REG_S bank */ 12778 value |= env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK; 12779 } 12780 return value; 12781 } 12782 case 0x94: /* CONTROL_NS */ 12783 /* 12784 * We have to handle this here because unprivileged Secure code 12785 * can read the NS CONTROL register. 12786 */ 12787 if (!env->v7m.secure) { 12788 return 0; 12789 } 12790 return env->v7m.control[M_REG_NS] | 12791 (env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK); 12792 } 12793 12794 if (el == 0) { 12795 return 0; /* unprivileged reads others as zero */ 12796 } 12797 12798 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 12799 switch (reg) { 12800 case 0x88: /* MSP_NS */ 12801 if (!env->v7m.secure) { 12802 return 0; 12803 } 12804 return env->v7m.other_ss_msp; 12805 case 0x89: /* PSP_NS */ 12806 if (!env->v7m.secure) { 12807 return 0; 12808 } 12809 return env->v7m.other_ss_psp; 12810 case 0x8a: /* MSPLIM_NS */ 12811 if (!env->v7m.secure) { 12812 return 0; 12813 } 12814 return env->v7m.msplim[M_REG_NS]; 12815 case 0x8b: /* PSPLIM_NS */ 12816 if (!env->v7m.secure) { 12817 return 0; 12818 } 12819 return env->v7m.psplim[M_REG_NS]; 12820 case 0x90: /* PRIMASK_NS */ 12821 if (!env->v7m.secure) { 12822 return 0; 12823 } 12824 return env->v7m.primask[M_REG_NS]; 12825 case 0x91: /* BASEPRI_NS */ 12826 if (!env->v7m.secure) { 12827 return 0; 12828 } 12829 return env->v7m.basepri[M_REG_NS]; 12830 case 0x93: /* FAULTMASK_NS */ 12831 if (!env->v7m.secure) { 12832 return 0; 12833 } 12834 return env->v7m.faultmask[M_REG_NS]; 12835 case 0x98: /* SP_NS */ 12836 { 12837 /* 12838 * This gives the non-secure SP selected based on whether we're 12839 * currently in handler mode or not, using the NS CONTROL.SPSEL. 12840 */ 12841 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; 12842 12843 if (!env->v7m.secure) { 12844 return 0; 12845 } 12846 if (!arm_v7m_is_handler_mode(env) && spsel) { 12847 return env->v7m.other_ss_psp; 12848 } else { 12849 return env->v7m.other_ss_msp; 12850 } 12851 } 12852 default: 12853 break; 12854 } 12855 } 12856 12857 switch (reg) { 12858 case 8: /* MSP */ 12859 return v7m_using_psp(env) ? env->v7m.other_sp : env->regs[13]; 12860 case 9: /* PSP */ 12861 return v7m_using_psp(env) ? env->regs[13] : env->v7m.other_sp; 12862 case 10: /* MSPLIM */ 12863 if (!arm_feature(env, ARM_FEATURE_V8)) { 12864 goto bad_reg; 12865 } 12866 return env->v7m.msplim[env->v7m.secure]; 12867 case 11: /* PSPLIM */ 12868 if (!arm_feature(env, ARM_FEATURE_V8)) { 12869 goto bad_reg; 12870 } 12871 return env->v7m.psplim[env->v7m.secure]; 12872 case 16: /* PRIMASK */ 12873 return env->v7m.primask[env->v7m.secure]; 12874 case 17: /* BASEPRI */ 12875 case 18: /* BASEPRI_MAX */ 12876 return env->v7m.basepri[env->v7m.secure]; 12877 case 19: /* FAULTMASK */ 12878 return env->v7m.faultmask[env->v7m.secure]; 12879 default: 12880 bad_reg: 12881 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to read unknown special" 12882 " register %d\n", reg); 12883 return 0; 12884 } 12885 } 12886 12887 void HELPER(v7m_msr)(CPUARMState *env, uint32_t maskreg, uint32_t val) 12888 { 12889 /* 12890 * We're passed bits [11..0] of the instruction; extract 12891 * SYSm and the mask bits. 12892 * Invalid combinations of SYSm and mask are UNPREDICTABLE; 12893 * we choose to treat them as if the mask bits were valid. 12894 * NB that the pseudocode 'mask' variable is bits [11..10], 12895 * whereas ours is [11..8]. 12896 */ 12897 uint32_t mask = extract32(maskreg, 8, 4); 12898 uint32_t reg = extract32(maskreg, 0, 8); 12899 int cur_el = arm_current_el(env); 12900 12901 if (cur_el == 0 && reg > 7 && reg != 20) { 12902 /* 12903 * only xPSR sub-fields and CONTROL.SFPA may be written by 12904 * unprivileged code 12905 */ 12906 return; 12907 } 12908 12909 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 12910 switch (reg) { 12911 case 0x88: /* MSP_NS */ 12912 if (!env->v7m.secure) { 12913 return; 12914 } 12915 env->v7m.other_ss_msp = val; 12916 return; 12917 case 0x89: /* PSP_NS */ 12918 if (!env->v7m.secure) { 12919 return; 12920 } 12921 env->v7m.other_ss_psp = val; 12922 return; 12923 case 0x8a: /* MSPLIM_NS */ 12924 if (!env->v7m.secure) { 12925 return; 12926 } 12927 env->v7m.msplim[M_REG_NS] = val & ~7; 12928 return; 12929 case 0x8b: /* PSPLIM_NS */ 12930 if (!env->v7m.secure) { 12931 return; 12932 } 12933 env->v7m.psplim[M_REG_NS] = val & ~7; 12934 return; 12935 case 0x90: /* PRIMASK_NS */ 12936 if (!env->v7m.secure) { 12937 return; 12938 } 12939 env->v7m.primask[M_REG_NS] = val & 1; 12940 return; 12941 case 0x91: /* BASEPRI_NS */ 12942 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { 12943 return; 12944 } 12945 env->v7m.basepri[M_REG_NS] = val & 0xff; 12946 return; 12947 case 0x93: /* FAULTMASK_NS */ 12948 if (!env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_MAIN)) { 12949 return; 12950 } 12951 env->v7m.faultmask[M_REG_NS] = val & 1; 12952 return; 12953 case 0x94: /* CONTROL_NS */ 12954 if (!env->v7m.secure) { 12955 return; 12956 } 12957 write_v7m_control_spsel_for_secstate(env, 12958 val & R_V7M_CONTROL_SPSEL_MASK, 12959 M_REG_NS); 12960 if (arm_feature(env, ARM_FEATURE_M_MAIN)) { 12961 env->v7m.control[M_REG_NS] &= ~R_V7M_CONTROL_NPRIV_MASK; 12962 env->v7m.control[M_REG_NS] |= val & R_V7M_CONTROL_NPRIV_MASK; 12963 } 12964 /* 12965 * SFPA is RAZ/WI from NS. FPCA is RO if NSACR.CP10 == 0, 12966 * RES0 if the FPU is not present, and is stored in the S bank 12967 */ 12968 if (arm_feature(env, ARM_FEATURE_VFP) && 12969 extract32(env->v7m.nsacr, 10, 1)) { 12970 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; 12971 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK; 12972 } 12973 return; 12974 case 0x98: /* SP_NS */ 12975 { 12976 /* 12977 * This gives the non-secure SP selected based on whether we're 12978 * currently in handler mode or not, using the NS CONTROL.SPSEL. 12979 */ 12980 bool spsel = env->v7m.control[M_REG_NS] & R_V7M_CONTROL_SPSEL_MASK; 12981 bool is_psp = !arm_v7m_is_handler_mode(env) && spsel; 12982 uint32_t limit; 12983 12984 if (!env->v7m.secure) { 12985 return; 12986 } 12987 12988 limit = is_psp ? env->v7m.psplim[false] : env->v7m.msplim[false]; 12989 12990 if (val < limit) { 12991 CPUState *cs = env_cpu(env); 12992 12993 cpu_restore_state(cs, GETPC(), true); 12994 raise_exception(env, EXCP_STKOF, 0, 1); 12995 } 12996 12997 if (is_psp) { 12998 env->v7m.other_ss_psp = val; 12999 } else { 13000 env->v7m.other_ss_msp = val; 13001 } 13002 return; 13003 } 13004 default: 13005 break; 13006 } 13007 } 13008 13009 switch (reg) { 13010 case 0 ... 7: /* xPSR sub-fields */ 13011 /* only APSR is actually writable */ 13012 if (!(reg & 4)) { 13013 uint32_t apsrmask = 0; 13014 13015 if (mask & 8) { 13016 apsrmask |= XPSR_NZCV | XPSR_Q; 13017 } 13018 if ((mask & 4) && arm_feature(env, ARM_FEATURE_THUMB_DSP)) { 13019 apsrmask |= XPSR_GE; 13020 } 13021 xpsr_write(env, val, apsrmask); 13022 } 13023 break; 13024 case 8: /* MSP */ 13025 if (v7m_using_psp(env)) { 13026 env->v7m.other_sp = val; 13027 } else { 13028 env->regs[13] = val; 13029 } 13030 break; 13031 case 9: /* PSP */ 13032 if (v7m_using_psp(env)) { 13033 env->regs[13] = val; 13034 } else { 13035 env->v7m.other_sp = val; 13036 } 13037 break; 13038 case 10: /* MSPLIM */ 13039 if (!arm_feature(env, ARM_FEATURE_V8)) { 13040 goto bad_reg; 13041 } 13042 env->v7m.msplim[env->v7m.secure] = val & ~7; 13043 break; 13044 case 11: /* PSPLIM */ 13045 if (!arm_feature(env, ARM_FEATURE_V8)) { 13046 goto bad_reg; 13047 } 13048 env->v7m.psplim[env->v7m.secure] = val & ~7; 13049 break; 13050 case 16: /* PRIMASK */ 13051 env->v7m.primask[env->v7m.secure] = val & 1; 13052 break; 13053 case 17: /* BASEPRI */ 13054 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 13055 goto bad_reg; 13056 } 13057 env->v7m.basepri[env->v7m.secure] = val & 0xff; 13058 break; 13059 case 18: /* BASEPRI_MAX */ 13060 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 13061 goto bad_reg; 13062 } 13063 val &= 0xff; 13064 if (val != 0 && (val < env->v7m.basepri[env->v7m.secure] 13065 || env->v7m.basepri[env->v7m.secure] == 0)) { 13066 env->v7m.basepri[env->v7m.secure] = val; 13067 } 13068 break; 13069 case 19: /* FAULTMASK */ 13070 if (!arm_feature(env, ARM_FEATURE_M_MAIN)) { 13071 goto bad_reg; 13072 } 13073 env->v7m.faultmask[env->v7m.secure] = val & 1; 13074 break; 13075 case 20: /* CONTROL */ 13076 /* 13077 * Writing to the SPSEL bit only has an effect if we are in 13078 * thread mode; other bits can be updated by any privileged code. 13079 * write_v7m_control_spsel() deals with updating the SPSEL bit in 13080 * env->v7m.control, so we only need update the others. 13081 * For v7M, we must just ignore explicit writes to SPSEL in handler 13082 * mode; for v8M the write is permitted but will have no effect. 13083 * All these bits are writes-ignored from non-privileged code, 13084 * except for SFPA. 13085 */ 13086 if (cur_el > 0 && (arm_feature(env, ARM_FEATURE_V8) || 13087 !arm_v7m_is_handler_mode(env))) { 13088 write_v7m_control_spsel(env, (val & R_V7M_CONTROL_SPSEL_MASK) != 0); 13089 } 13090 if (cur_el > 0 && arm_feature(env, ARM_FEATURE_M_MAIN)) { 13091 env->v7m.control[env->v7m.secure] &= ~R_V7M_CONTROL_NPRIV_MASK; 13092 env->v7m.control[env->v7m.secure] |= val & R_V7M_CONTROL_NPRIV_MASK; 13093 } 13094 if (arm_feature(env, ARM_FEATURE_VFP)) { 13095 /* 13096 * SFPA is RAZ/WI from NS or if no FPU. 13097 * FPCA is RO if NSACR.CP10 == 0, RES0 if the FPU is not present. 13098 * Both are stored in the S bank. 13099 */ 13100 if (env->v7m.secure) { 13101 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_SFPA_MASK; 13102 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_SFPA_MASK; 13103 } 13104 if (cur_el > 0 && 13105 (env->v7m.secure || !arm_feature(env, ARM_FEATURE_M_SECURITY) || 13106 extract32(env->v7m.nsacr, 10, 1))) { 13107 env->v7m.control[M_REG_S] &= ~R_V7M_CONTROL_FPCA_MASK; 13108 env->v7m.control[M_REG_S] |= val & R_V7M_CONTROL_FPCA_MASK; 13109 } 13110 } 13111 break; 13112 default: 13113 bad_reg: 13114 qemu_log_mask(LOG_GUEST_ERROR, "Attempt to write unknown special" 13115 " register %d\n", reg); 13116 return; 13117 } 13118 } 13119 13120 uint32_t HELPER(v7m_tt)(CPUARMState *env, uint32_t addr, uint32_t op) 13121 { 13122 /* Implement the TT instruction. op is bits [7:6] of the insn. */ 13123 bool forceunpriv = op & 1; 13124 bool alt = op & 2; 13125 V8M_SAttributes sattrs = {}; 13126 uint32_t tt_resp; 13127 bool r, rw, nsr, nsrw, mrvalid; 13128 int prot; 13129 ARMMMUFaultInfo fi = {}; 13130 MemTxAttrs attrs = {}; 13131 hwaddr phys_addr; 13132 ARMMMUIdx mmu_idx; 13133 uint32_t mregion; 13134 bool targetpriv; 13135 bool targetsec = env->v7m.secure; 13136 bool is_subpage; 13137 13138 /* 13139 * Work out what the security state and privilege level we're 13140 * interested in is... 13141 */ 13142 if (alt) { 13143 targetsec = !targetsec; 13144 } 13145 13146 if (forceunpriv) { 13147 targetpriv = false; 13148 } else { 13149 targetpriv = arm_v7m_is_handler_mode(env) || 13150 !(env->v7m.control[targetsec] & R_V7M_CONTROL_NPRIV_MASK); 13151 } 13152 13153 /* ...and then figure out which MMU index this is */ 13154 mmu_idx = arm_v7m_mmu_idx_for_secstate_and_priv(env, targetsec, targetpriv); 13155 13156 /* 13157 * We know that the MPU and SAU don't care about the access type 13158 * for our purposes beyond that we don't want to claim to be 13159 * an insn fetch, so we arbitrarily call this a read. 13160 */ 13161 13162 /* 13163 * MPU region info only available for privileged or if 13164 * inspecting the other MPU state. 13165 */ 13166 if (arm_current_el(env) != 0 || alt) { 13167 /* We can ignore the return value as prot is always set */ 13168 pmsav8_mpu_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, 13169 &phys_addr, &attrs, &prot, &is_subpage, 13170 &fi, &mregion); 13171 if (mregion == -1) { 13172 mrvalid = false; 13173 mregion = 0; 13174 } else { 13175 mrvalid = true; 13176 } 13177 r = prot & PAGE_READ; 13178 rw = prot & PAGE_WRITE; 13179 } else { 13180 r = false; 13181 rw = false; 13182 mrvalid = false; 13183 mregion = 0; 13184 } 13185 13186 if (env->v7m.secure) { 13187 v8m_security_lookup(env, addr, MMU_DATA_LOAD, mmu_idx, &sattrs); 13188 nsr = sattrs.ns && r; 13189 nsrw = sattrs.ns && rw; 13190 } else { 13191 sattrs.ns = true; 13192 nsr = false; 13193 nsrw = false; 13194 } 13195 13196 tt_resp = (sattrs.iregion << 24) | 13197 (sattrs.irvalid << 23) | 13198 ((!sattrs.ns) << 22) | 13199 (nsrw << 21) | 13200 (nsr << 20) | 13201 (rw << 19) | 13202 (r << 18) | 13203 (sattrs.srvalid << 17) | 13204 (mrvalid << 16) | 13205 (sattrs.sregion << 8) | 13206 mregion; 13207 13208 return tt_resp; 13209 } 13210 13211 #endif 13212 13213 /* Note that signed overflow is undefined in C. The following routines are 13214 careful to use unsigned types where modulo arithmetic is required. 13215 Failure to do so _will_ break on newer gcc. */ 13216 13217 /* Signed saturating arithmetic. */ 13218 13219 /* Perform 16-bit signed saturating addition. */ 13220 static inline uint16_t add16_sat(uint16_t a, uint16_t b) 13221 { 13222 uint16_t res; 13223 13224 res = a + b; 13225 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { 13226 if (a & 0x8000) 13227 res = 0x8000; 13228 else 13229 res = 0x7fff; 13230 } 13231 return res; 13232 } 13233 13234 /* Perform 8-bit signed saturating addition. */ 13235 static inline uint8_t add8_sat(uint8_t a, uint8_t b) 13236 { 13237 uint8_t res; 13238 13239 res = a + b; 13240 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { 13241 if (a & 0x80) 13242 res = 0x80; 13243 else 13244 res = 0x7f; 13245 } 13246 return res; 13247 } 13248 13249 /* Perform 16-bit signed saturating subtraction. */ 13250 static inline uint16_t sub16_sat(uint16_t a, uint16_t b) 13251 { 13252 uint16_t res; 13253 13254 res = a - b; 13255 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { 13256 if (a & 0x8000) 13257 res = 0x8000; 13258 else 13259 res = 0x7fff; 13260 } 13261 return res; 13262 } 13263 13264 /* Perform 8-bit signed saturating subtraction. */ 13265 static inline uint8_t sub8_sat(uint8_t a, uint8_t b) 13266 { 13267 uint8_t res; 13268 13269 res = a - b; 13270 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { 13271 if (a & 0x80) 13272 res = 0x80; 13273 else 13274 res = 0x7f; 13275 } 13276 return res; 13277 } 13278 13279 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); 13280 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); 13281 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); 13282 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); 13283 #define PFX q 13284 13285 #include "op_addsub.h" 13286 13287 /* Unsigned saturating arithmetic. */ 13288 static inline uint16_t add16_usat(uint16_t a, uint16_t b) 13289 { 13290 uint16_t res; 13291 res = a + b; 13292 if (res < a) 13293 res = 0xffff; 13294 return res; 13295 } 13296 13297 static inline uint16_t sub16_usat(uint16_t a, uint16_t b) 13298 { 13299 if (a > b) 13300 return a - b; 13301 else 13302 return 0; 13303 } 13304 13305 static inline uint8_t add8_usat(uint8_t a, uint8_t b) 13306 { 13307 uint8_t res; 13308 res = a + b; 13309 if (res < a) 13310 res = 0xff; 13311 return res; 13312 } 13313 13314 static inline uint8_t sub8_usat(uint8_t a, uint8_t b) 13315 { 13316 if (a > b) 13317 return a - b; 13318 else 13319 return 0; 13320 } 13321 13322 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); 13323 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); 13324 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); 13325 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); 13326 #define PFX uq 13327 13328 #include "op_addsub.h" 13329 13330 /* Signed modulo arithmetic. */ 13331 #define SARITH16(a, b, n, op) do { \ 13332 int32_t sum; \ 13333 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ 13334 RESULT(sum, n, 16); \ 13335 if (sum >= 0) \ 13336 ge |= 3 << (n * 2); \ 13337 } while(0) 13338 13339 #define SARITH8(a, b, n, op) do { \ 13340 int32_t sum; \ 13341 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ 13342 RESULT(sum, n, 8); \ 13343 if (sum >= 0) \ 13344 ge |= 1 << n; \ 13345 } while(0) 13346 13347 13348 #define ADD16(a, b, n) SARITH16(a, b, n, +) 13349 #define SUB16(a, b, n) SARITH16(a, b, n, -) 13350 #define ADD8(a, b, n) SARITH8(a, b, n, +) 13351 #define SUB8(a, b, n) SARITH8(a, b, n, -) 13352 #define PFX s 13353 #define ARITH_GE 13354 13355 #include "op_addsub.h" 13356 13357 /* Unsigned modulo arithmetic. */ 13358 #define ADD16(a, b, n) do { \ 13359 uint32_t sum; \ 13360 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ 13361 RESULT(sum, n, 16); \ 13362 if ((sum >> 16) == 1) \ 13363 ge |= 3 << (n * 2); \ 13364 } while(0) 13365 13366 #define ADD8(a, b, n) do { \ 13367 uint32_t sum; \ 13368 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ 13369 RESULT(sum, n, 8); \ 13370 if ((sum >> 8) == 1) \ 13371 ge |= 1 << n; \ 13372 } while(0) 13373 13374 #define SUB16(a, b, n) do { \ 13375 uint32_t sum; \ 13376 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ 13377 RESULT(sum, n, 16); \ 13378 if ((sum >> 16) == 0) \ 13379 ge |= 3 << (n * 2); \ 13380 } while(0) 13381 13382 #define SUB8(a, b, n) do { \ 13383 uint32_t sum; \ 13384 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ 13385 RESULT(sum, n, 8); \ 13386 if ((sum >> 8) == 0) \ 13387 ge |= 1 << n; \ 13388 } while(0) 13389 13390 #define PFX u 13391 #define ARITH_GE 13392 13393 #include "op_addsub.h" 13394 13395 /* Halved signed arithmetic. */ 13396 #define ADD16(a, b, n) \ 13397 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) 13398 #define SUB16(a, b, n) \ 13399 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) 13400 #define ADD8(a, b, n) \ 13401 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) 13402 #define SUB8(a, b, n) \ 13403 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) 13404 #define PFX sh 13405 13406 #include "op_addsub.h" 13407 13408 /* Halved unsigned arithmetic. */ 13409 #define ADD16(a, b, n) \ 13410 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) 13411 #define SUB16(a, b, n) \ 13412 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) 13413 #define ADD8(a, b, n) \ 13414 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) 13415 #define SUB8(a, b, n) \ 13416 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) 13417 #define PFX uh 13418 13419 #include "op_addsub.h" 13420 13421 static inline uint8_t do_usad(uint8_t a, uint8_t b) 13422 { 13423 if (a > b) 13424 return a - b; 13425 else 13426 return b - a; 13427 } 13428 13429 /* Unsigned sum of absolute byte differences. */ 13430 uint32_t HELPER(usad8)(uint32_t a, uint32_t b) 13431 { 13432 uint32_t sum; 13433 sum = do_usad(a, b); 13434 sum += do_usad(a >> 8, b >> 8); 13435 sum += do_usad(a >> 16, b >>16); 13436 sum += do_usad(a >> 24, b >> 24); 13437 return sum; 13438 } 13439 13440 /* For ARMv6 SEL instruction. */ 13441 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) 13442 { 13443 uint32_t mask; 13444 13445 mask = 0; 13446 if (flags & 1) 13447 mask |= 0xff; 13448 if (flags & 2) 13449 mask |= 0xff00; 13450 if (flags & 4) 13451 mask |= 0xff0000; 13452 if (flags & 8) 13453 mask |= 0xff000000; 13454 return (a & mask) | (b & ~mask); 13455 } 13456 13457 /* CRC helpers. 13458 * The upper bytes of val (above the number specified by 'bytes') must have 13459 * been zeroed out by the caller. 13460 */ 13461 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes) 13462 { 13463 uint8_t buf[4]; 13464 13465 stl_le_p(buf, val); 13466 13467 /* zlib crc32 converts the accumulator and output to one's complement. */ 13468 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; 13469 } 13470 13471 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) 13472 { 13473 uint8_t buf[4]; 13474 13475 stl_le_p(buf, val); 13476 13477 /* Linux crc32c converts the output to one's complement. */ 13478 return crc32c(acc, buf, bytes) ^ 0xffffffff; 13479 } 13480 13481 /* Return the exception level to which FP-disabled exceptions should 13482 * be taken, or 0 if FP is enabled. 13483 */ 13484 int fp_exception_el(CPUARMState *env, int cur_el) 13485 { 13486 #ifndef CONFIG_USER_ONLY 13487 int fpen; 13488 13489 /* CPACR and the CPTR registers don't exist before v6, so FP is 13490 * always accessible 13491 */ 13492 if (!arm_feature(env, ARM_FEATURE_V6)) { 13493 return 0; 13494 } 13495 13496 if (arm_feature(env, ARM_FEATURE_M)) { 13497 /* CPACR can cause a NOCP UsageFault taken to current security state */ 13498 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) { 13499 return 1; 13500 } 13501 13502 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) { 13503 if (!extract32(env->v7m.nsacr, 10, 1)) { 13504 /* FP insns cause a NOCP UsageFault taken to Secure */ 13505 return 3; 13506 } 13507 } 13508 13509 return 0; 13510 } 13511 13512 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit: 13513 * 0, 2 : trap EL0 and EL1/PL1 accesses 13514 * 1 : trap only EL0 accesses 13515 * 3 : trap no accesses 13516 */ 13517 fpen = extract32(env->cp15.cpacr_el1, 20, 2); 13518 switch (fpen) { 13519 case 0: 13520 case 2: 13521 if (cur_el == 0 || cur_el == 1) { 13522 /* Trap to PL1, which might be EL1 or EL3 */ 13523 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) { 13524 return 3; 13525 } 13526 return 1; 13527 } 13528 if (cur_el == 3 && !is_a64(env)) { 13529 /* Secure PL1 running at EL3 */ 13530 return 3; 13531 } 13532 break; 13533 case 1: 13534 if (cur_el == 0) { 13535 return 1; 13536 } 13537 break; 13538 case 3: 13539 break; 13540 } 13541 13542 /* 13543 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode 13544 * to control non-secure access to the FPU. It doesn't have any 13545 * effect if EL3 is AArch64 or if EL3 doesn't exist at all. 13546 */ 13547 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 13548 cur_el <= 2 && !arm_is_secure_below_el3(env))) { 13549 if (!extract32(env->cp15.nsacr, 10, 1)) { 13550 /* FP insns act as UNDEF */ 13551 return cur_el == 2 ? 2 : 1; 13552 } 13553 } 13554 13555 /* For the CPTR registers we don't need to guard with an ARM_FEATURE 13556 * check because zero bits in the registers mean "don't trap". 13557 */ 13558 13559 /* CPTR_EL2 : present in v7VE or v8 */ 13560 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1) 13561 && !arm_is_secure_below_el3(env)) { 13562 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */ 13563 return 2; 13564 } 13565 13566 /* CPTR_EL3 : present in v8 */ 13567 if (extract32(env->cp15.cptr_el[3], 10, 1)) { 13568 /* Trap all FP ops to EL3 */ 13569 return 3; 13570 } 13571 #endif 13572 return 0; 13573 } 13574 13575 ARMMMUIdx arm_v7m_mmu_idx_all(CPUARMState *env, 13576 bool secstate, bool priv, bool negpri) 13577 { 13578 ARMMMUIdx mmu_idx = ARM_MMU_IDX_M; 13579 13580 if (priv) { 13581 mmu_idx |= ARM_MMU_IDX_M_PRIV; 13582 } 13583 13584 if (negpri) { 13585 mmu_idx |= ARM_MMU_IDX_M_NEGPRI; 13586 } 13587 13588 if (secstate) { 13589 mmu_idx |= ARM_MMU_IDX_M_S; 13590 } 13591 13592 return mmu_idx; 13593 } 13594 13595 ARMMMUIdx arm_v7m_mmu_idx_for_secstate_and_priv(CPUARMState *env, 13596 bool secstate, bool priv) 13597 { 13598 bool negpri = armv7m_nvic_neg_prio_requested(env->nvic, secstate); 13599 13600 return arm_v7m_mmu_idx_all(env, secstate, priv, negpri); 13601 } 13602 13603 /* Return the MMU index for a v7M CPU in the specified security state */ 13604 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate) 13605 { 13606 bool priv = arm_current_el(env) != 0; 13607 13608 return arm_v7m_mmu_idx_for_secstate_and_priv(env, secstate, priv); 13609 } 13610 13611 ARMMMUIdx arm_mmu_idx(CPUARMState *env) 13612 { 13613 int el; 13614 13615 if (arm_feature(env, ARM_FEATURE_M)) { 13616 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure); 13617 } 13618 13619 el = arm_current_el(env); 13620 if (el < 2 && arm_is_secure_below_el3(env)) { 13621 return ARMMMUIdx_S1SE0 + el; 13622 } else { 13623 return ARMMMUIdx_S12NSE0 + el; 13624 } 13625 } 13626 13627 int cpu_mmu_index(CPUARMState *env, bool ifetch) 13628 { 13629 return arm_to_core_mmu_idx(arm_mmu_idx(env)); 13630 } 13631 13632 #ifndef CONFIG_USER_ONLY 13633 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env) 13634 { 13635 return stage_1_mmu_idx(arm_mmu_idx(env)); 13636 } 13637 #endif 13638 13639 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc, 13640 target_ulong *cs_base, uint32_t *pflags) 13641 { 13642 ARMMMUIdx mmu_idx = arm_mmu_idx(env); 13643 int current_el = arm_current_el(env); 13644 int fp_el = fp_exception_el(env, current_el); 13645 uint32_t flags = 0; 13646 13647 if (is_a64(env)) { 13648 ARMCPU *cpu = env_archcpu(env); 13649 uint64_t sctlr; 13650 13651 *pc = env->pc; 13652 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1); 13653 13654 /* Get control bits for tagged addresses. */ 13655 { 13656 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx); 13657 ARMVAParameters p0 = aa64_va_parameters_both(env, 0, stage1); 13658 int tbii, tbid; 13659 13660 /* FIXME: ARMv8.1-VHE S2 translation regime. */ 13661 if (regime_el(env, stage1) < 2) { 13662 ARMVAParameters p1 = aa64_va_parameters_both(env, -1, stage1); 13663 tbid = (p1.tbi << 1) | p0.tbi; 13664 tbii = tbid & ~((p1.tbid << 1) | p0.tbid); 13665 } else { 13666 tbid = p0.tbi; 13667 tbii = tbid & !p0.tbid; 13668 } 13669 13670 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii); 13671 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid); 13672 } 13673 13674 if (cpu_isar_feature(aa64_sve, cpu)) { 13675 int sve_el = sve_exception_el(env, current_el); 13676 uint32_t zcr_len; 13677 13678 /* If SVE is disabled, but FP is enabled, 13679 * then the effective len is 0. 13680 */ 13681 if (sve_el != 0 && fp_el == 0) { 13682 zcr_len = 0; 13683 } else { 13684 zcr_len = sve_zcr_len_for_el(env, current_el); 13685 } 13686 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el); 13687 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len); 13688 } 13689 13690 sctlr = arm_sctlr(env, current_el); 13691 13692 if (cpu_isar_feature(aa64_pauth, cpu)) { 13693 /* 13694 * In order to save space in flags, we record only whether 13695 * pauth is "inactive", meaning all insns are implemented as 13696 * a nop, or "active" when some action must be performed. 13697 * The decision of which action to take is left to a helper. 13698 */ 13699 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) { 13700 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1); 13701 } 13702 } 13703 13704 if (cpu_isar_feature(aa64_bti, cpu)) { 13705 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */ 13706 if (sctlr & (current_el == 0 ? SCTLR_BT0 : SCTLR_BT1)) { 13707 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1); 13708 } 13709 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype); 13710 } 13711 } else { 13712 *pc = env->regs[15]; 13713 flags = FIELD_DP32(flags, TBFLAG_A32, THUMB, env->thumb); 13714 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN, env->vfp.vec_len); 13715 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE, env->vfp.vec_stride); 13716 flags = FIELD_DP32(flags, TBFLAG_A32, CONDEXEC, env->condexec_bits); 13717 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, arm_sctlr_b(env)); 13718 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env)); 13719 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30) 13720 || arm_el_is_aa64(env, 1) || arm_feature(env, ARM_FEATURE_M)) { 13721 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1); 13722 } 13723 /* Note that XSCALE_CPAR shares bits with VECSTRIDE */ 13724 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 13725 flags = FIELD_DP32(flags, TBFLAG_A32, 13726 XSCALE_CPAR, env->cp15.c15_cpar); 13727 } 13728 } 13729 13730 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX, arm_to_core_mmu_idx(mmu_idx)); 13731 13732 /* The SS_ACTIVE and PSTATE_SS bits correspond to the state machine 13733 * states defined in the ARM ARM for software singlestep: 13734 * SS_ACTIVE PSTATE.SS State 13735 * 0 x Inactive (the TB flag for SS is always 0) 13736 * 1 0 Active-pending 13737 * 1 1 Active-not-pending 13738 */ 13739 if (arm_singlestep_active(env)) { 13740 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1); 13741 if (is_a64(env)) { 13742 if (env->pstate & PSTATE_SS) { 13743 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1); 13744 } 13745 } else { 13746 if (env->uncached_cpsr & PSTATE_SS) { 13747 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1); 13748 } 13749 } 13750 } 13751 if (arm_cpu_data_is_big_endian(env)) { 13752 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1); 13753 } 13754 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el); 13755 13756 if (arm_v7m_is_handler_mode(env)) { 13757 flags = FIELD_DP32(flags, TBFLAG_A32, HANDLER, 1); 13758 } 13759 13760 /* v8M always applies stack limit checks unless CCR.STKOFHFNMIGN is 13761 * suppressing them because the requested execution priority is less than 0. 13762 */ 13763 if (arm_feature(env, ARM_FEATURE_V8) && 13764 arm_feature(env, ARM_FEATURE_M) && 13765 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) && 13766 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) { 13767 flags = FIELD_DP32(flags, TBFLAG_A32, STACKCHECK, 1); 13768 } 13769 13770 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 13771 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S) != env->v7m.secure) { 13772 flags = FIELD_DP32(flags, TBFLAG_A32, FPCCR_S_WRONG, 1); 13773 } 13774 13775 if (arm_feature(env, ARM_FEATURE_M) && 13776 (env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) && 13777 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) || 13778 (env->v7m.secure && 13779 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) { 13780 /* 13781 * ASPEN is set, but FPCA/SFPA indicate that there is no active 13782 * FP context; we must create a new FP context before executing 13783 * any FP insn. 13784 */ 13785 flags = FIELD_DP32(flags, TBFLAG_A32, NEW_FP_CTXT_NEEDED, 1); 13786 } 13787 13788 if (arm_feature(env, ARM_FEATURE_M)) { 13789 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 13790 13791 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) { 13792 flags = FIELD_DP32(flags, TBFLAG_A32, LSPACT, 1); 13793 } 13794 } 13795 13796 *pflags = flags; 13797 *cs_base = 0; 13798 } 13799 13800 #ifdef TARGET_AARCH64 13801 /* 13802 * The manual says that when SVE is enabled and VQ is widened the 13803 * implementation is allowed to zero the previously inaccessible 13804 * portion of the registers. The corollary to that is that when 13805 * SVE is enabled and VQ is narrowed we are also allowed to zero 13806 * the now inaccessible portion of the registers. 13807 * 13808 * The intent of this is that no predicate bit beyond VQ is ever set. 13809 * Which means that some operations on predicate registers themselves 13810 * may operate on full uint64_t or even unrolled across the maximum 13811 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally 13812 * may well be cheaper than conditionals to restrict the operation 13813 * to the relevant portion of a uint16_t[16]. 13814 */ 13815 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq) 13816 { 13817 int i, j; 13818 uint64_t pmask; 13819 13820 assert(vq >= 1 && vq <= ARM_MAX_VQ); 13821 assert(vq <= env_archcpu(env)->sve_max_vq); 13822 13823 /* Zap the high bits of the zregs. */ 13824 for (i = 0; i < 32; i++) { 13825 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq)); 13826 } 13827 13828 /* Zap the high bits of the pregs and ffr. */ 13829 pmask = 0; 13830 if (vq & 3) { 13831 pmask = ~(-1ULL << (16 * (vq & 3))); 13832 } 13833 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) { 13834 for (i = 0; i < 17; ++i) { 13835 env->vfp.pregs[i].p[j] &= pmask; 13836 } 13837 pmask = 0; 13838 } 13839 } 13840 13841 /* 13842 * Notice a change in SVE vector size when changing EL. 13843 */ 13844 void aarch64_sve_change_el(CPUARMState *env, int old_el, 13845 int new_el, bool el0_a64) 13846 { 13847 ARMCPU *cpu = env_archcpu(env); 13848 int old_len, new_len; 13849 bool old_a64, new_a64; 13850 13851 /* Nothing to do if no SVE. */ 13852 if (!cpu_isar_feature(aa64_sve, cpu)) { 13853 return; 13854 } 13855 13856 /* Nothing to do if FP is disabled in either EL. */ 13857 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) { 13858 return; 13859 } 13860 13861 /* 13862 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped 13863 * at ELx, or not available because the EL is in AArch32 state, then 13864 * for all purposes other than a direct read, the ZCR_ELx.LEN field 13865 * has an effective value of 0". 13866 * 13867 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0). 13868 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition 13869 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that 13870 * we already have the correct register contents when encountering the 13871 * vq0->vq0 transition between EL0->EL1. 13872 */ 13873 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64; 13874 old_len = (old_a64 && !sve_exception_el(env, old_el) 13875 ? sve_zcr_len_for_el(env, old_el) : 0); 13876 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64; 13877 new_len = (new_a64 && !sve_exception_el(env, new_el) 13878 ? sve_zcr_len_for_el(env, new_el) : 0); 13879 13880 /* When changing vector length, clear inaccessible state. */ 13881 if (new_len < old_len) { 13882 aarch64_sve_narrow_vq(env, new_len + 1); 13883 } 13884 } 13885 #endif 13886