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 9 #include "qemu/osdep.h" 10 #include "qemu/units.h" 11 #include "target/arm/idau.h" 12 #include "trace.h" 13 #include "cpu.h" 14 #include "internals.h" 15 #include "exec/gdbstub.h" 16 #include "exec/helper-proto.h" 17 #include "qemu/host-utils.h" 18 #include "qemu/main-loop.h" 19 #include "qemu/bitops.h" 20 #include "qemu/crc32c.h" 21 #include "qemu/qemu-print.h" 22 #include "exec/exec-all.h" 23 #include <zlib.h> /* For crc32 */ 24 #include "hw/irq.h" 25 #include "hw/semihosting/semihost.h" 26 #include "sysemu/cpus.h" 27 #include "sysemu/cpu-timers.h" 28 #include "sysemu/kvm.h" 29 #include "sysemu/tcg.h" 30 #include "qemu/range.h" 31 #include "qapi/qapi-commands-machine-target.h" 32 #include "qapi/error.h" 33 #include "qemu/guest-random.h" 34 #ifdef CONFIG_TCG 35 #include "arm_ldst.h" 36 #include "exec/cpu_ldst.h" 37 #include "hw/semihosting/common-semi.h" 38 #endif 39 40 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */ 41 42 #ifndef CONFIG_USER_ONLY 43 44 static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address, 45 MMUAccessType access_type, ARMMMUIdx mmu_idx, 46 bool s1_is_el0, 47 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 48 target_ulong *page_size_ptr, 49 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 50 __attribute__((nonnull)); 51 #endif 52 53 static void switch_mode(CPUARMState *env, int mode); 54 static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx); 55 56 static int vfp_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg) 57 { 58 ARMCPU *cpu = env_archcpu(env); 59 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16; 60 61 /* VFP data registers are always little-endian. */ 62 if (reg < nregs) { 63 return gdb_get_reg64(buf, *aa32_vfp_dreg(env, reg)); 64 } 65 if (arm_feature(env, ARM_FEATURE_NEON)) { 66 /* Aliases for Q regs. */ 67 nregs += 16; 68 if (reg < nregs) { 69 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 70 return gdb_get_reg128(buf, q[0], q[1]); 71 } 72 } 73 switch (reg - nregs) { 74 case 0: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPSID]); break; 75 case 1: return gdb_get_reg32(buf, vfp_get_fpscr(env)); break; 76 case 2: return gdb_get_reg32(buf, env->vfp.xregs[ARM_VFP_FPEXC]); break; 77 } 78 return 0; 79 } 80 81 static int vfp_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 82 { 83 ARMCPU *cpu = env_archcpu(env); 84 int nregs = cpu_isar_feature(aa32_simd_r32, cpu) ? 32 : 16; 85 86 if (reg < nregs) { 87 *aa32_vfp_dreg(env, reg) = ldq_le_p(buf); 88 return 8; 89 } 90 if (arm_feature(env, ARM_FEATURE_NEON)) { 91 nregs += 16; 92 if (reg < nregs) { 93 uint64_t *q = aa32_vfp_qreg(env, reg - 32); 94 q[0] = ldq_le_p(buf); 95 q[1] = ldq_le_p(buf + 8); 96 return 16; 97 } 98 } 99 switch (reg - nregs) { 100 case 0: env->vfp.xregs[ARM_VFP_FPSID] = ldl_p(buf); return 4; 101 case 1: vfp_set_fpscr(env, ldl_p(buf)); return 4; 102 case 2: env->vfp.xregs[ARM_VFP_FPEXC] = ldl_p(buf) & (1 << 30); return 4; 103 } 104 return 0; 105 } 106 107 static int aarch64_fpu_gdb_get_reg(CPUARMState *env, GByteArray *buf, int reg) 108 { 109 switch (reg) { 110 case 0 ... 31: 111 { 112 /* 128 bit FP register - quads are in LE order */ 113 uint64_t *q = aa64_vfp_qreg(env, reg); 114 return gdb_get_reg128(buf, q[1], q[0]); 115 } 116 case 32: 117 /* FPSR */ 118 return gdb_get_reg32(buf, vfp_get_fpsr(env)); 119 case 33: 120 /* FPCR */ 121 return gdb_get_reg32(buf,vfp_get_fpcr(env)); 122 default: 123 return 0; 124 } 125 } 126 127 static int aarch64_fpu_gdb_set_reg(CPUARMState *env, uint8_t *buf, int reg) 128 { 129 switch (reg) { 130 case 0 ... 31: 131 /* 128 bit FP register */ 132 { 133 uint64_t *q = aa64_vfp_qreg(env, reg); 134 q[0] = ldq_le_p(buf); 135 q[1] = ldq_le_p(buf + 8); 136 return 16; 137 } 138 case 32: 139 /* FPSR */ 140 vfp_set_fpsr(env, ldl_p(buf)); 141 return 4; 142 case 33: 143 /* FPCR */ 144 vfp_set_fpcr(env, ldl_p(buf)); 145 return 4; 146 default: 147 return 0; 148 } 149 } 150 151 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) 152 { 153 assert(ri->fieldoffset); 154 if (cpreg_field_is_64bit(ri)) { 155 return CPREG_FIELD64(env, ri); 156 } else { 157 return CPREG_FIELD32(env, ri); 158 } 159 } 160 161 static void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 162 uint64_t value) 163 { 164 assert(ri->fieldoffset); 165 if (cpreg_field_is_64bit(ri)) { 166 CPREG_FIELD64(env, ri) = value; 167 } else { 168 CPREG_FIELD32(env, ri) = value; 169 } 170 } 171 172 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) 173 { 174 return (char *)env + ri->fieldoffset; 175 } 176 177 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) 178 { 179 /* Raw read of a coprocessor register (as needed for migration, etc). */ 180 if (ri->type & ARM_CP_CONST) { 181 return ri->resetvalue; 182 } else if (ri->raw_readfn) { 183 return ri->raw_readfn(env, ri); 184 } else if (ri->readfn) { 185 return ri->readfn(env, ri); 186 } else { 187 return raw_read(env, ri); 188 } 189 } 190 191 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, 192 uint64_t v) 193 { 194 /* Raw write of a coprocessor register (as needed for migration, etc). 195 * Note that constant registers are treated as write-ignored; the 196 * caller should check for success by whether a readback gives the 197 * value written. 198 */ 199 if (ri->type & ARM_CP_CONST) { 200 return; 201 } else if (ri->raw_writefn) { 202 ri->raw_writefn(env, ri, v); 203 } else if (ri->writefn) { 204 ri->writefn(env, ri, v); 205 } else { 206 raw_write(env, ri, v); 207 } 208 } 209 210 /** 211 * arm_get/set_gdb_*: get/set a gdb register 212 * @env: the CPU state 213 * @buf: a buffer to copy to/from 214 * @reg: register number (offset from start of group) 215 * 216 * We return the number of bytes copied 217 */ 218 219 static int arm_gdb_get_sysreg(CPUARMState *env, GByteArray *buf, int reg) 220 { 221 ARMCPU *cpu = env_archcpu(env); 222 const ARMCPRegInfo *ri; 223 uint32_t key; 224 225 key = cpu->dyn_sysreg_xml.data.cpregs.keys[reg]; 226 ri = get_arm_cp_reginfo(cpu->cp_regs, key); 227 if (ri) { 228 if (cpreg_field_is_64bit(ri)) { 229 return gdb_get_reg64(buf, (uint64_t)read_raw_cp_reg(env, ri)); 230 } else { 231 return gdb_get_reg32(buf, (uint32_t)read_raw_cp_reg(env, ri)); 232 } 233 } 234 return 0; 235 } 236 237 static int arm_gdb_set_sysreg(CPUARMState *env, uint8_t *buf, int reg) 238 { 239 return 0; 240 } 241 242 #ifdef TARGET_AARCH64 243 static int arm_gdb_get_svereg(CPUARMState *env, GByteArray *buf, int reg) 244 { 245 ARMCPU *cpu = env_archcpu(env); 246 247 switch (reg) { 248 /* The first 32 registers are the zregs */ 249 case 0 ... 31: 250 { 251 int vq, len = 0; 252 for (vq = 0; vq < cpu->sve_max_vq; vq++) { 253 len += gdb_get_reg128(buf, 254 env->vfp.zregs[reg].d[vq * 2 + 1], 255 env->vfp.zregs[reg].d[vq * 2]); 256 } 257 return len; 258 } 259 case 32: 260 return gdb_get_reg32(buf, vfp_get_fpsr(env)); 261 case 33: 262 return gdb_get_reg32(buf, vfp_get_fpcr(env)); 263 /* then 16 predicates and the ffr */ 264 case 34 ... 50: 265 { 266 int preg = reg - 34; 267 int vq, len = 0; 268 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) { 269 len += gdb_get_reg64(buf, env->vfp.pregs[preg].p[vq / 4]); 270 } 271 return len; 272 } 273 case 51: 274 { 275 /* 276 * We report in Vector Granules (VG) which is 64bit in a Z reg 277 * while the ZCR works in Vector Quads (VQ) which is 128bit chunks. 278 */ 279 int vq = sve_zcr_len_for_el(env, arm_current_el(env)) + 1; 280 return gdb_get_reg64(buf, vq * 2); 281 } 282 default: 283 /* gdbstub asked for something out our range */ 284 qemu_log_mask(LOG_UNIMP, "%s: out of range register %d", __func__, reg); 285 break; 286 } 287 288 return 0; 289 } 290 291 static int arm_gdb_set_svereg(CPUARMState *env, uint8_t *buf, int reg) 292 { 293 ARMCPU *cpu = env_archcpu(env); 294 295 /* The first 32 registers are the zregs */ 296 switch (reg) { 297 /* The first 32 registers are the zregs */ 298 case 0 ... 31: 299 { 300 int vq, len = 0; 301 uint64_t *p = (uint64_t *) buf; 302 for (vq = 0; vq < cpu->sve_max_vq; vq++) { 303 env->vfp.zregs[reg].d[vq * 2 + 1] = *p++; 304 env->vfp.zregs[reg].d[vq * 2] = *p++; 305 len += 16; 306 } 307 return len; 308 } 309 case 32: 310 vfp_set_fpsr(env, *(uint32_t *)buf); 311 return 4; 312 case 33: 313 vfp_set_fpcr(env, *(uint32_t *)buf); 314 return 4; 315 case 34 ... 50: 316 { 317 int preg = reg - 34; 318 int vq, len = 0; 319 uint64_t *p = (uint64_t *) buf; 320 for (vq = 0; vq < cpu->sve_max_vq; vq = vq + 4) { 321 env->vfp.pregs[preg].p[vq / 4] = *p++; 322 len += 8; 323 } 324 return len; 325 } 326 case 51: 327 /* cannot set vg via gdbstub */ 328 return 0; 329 default: 330 /* gdbstub asked for something out our range */ 331 break; 332 } 333 334 return 0; 335 } 336 #endif /* TARGET_AARCH64 */ 337 338 static bool raw_accessors_invalid(const ARMCPRegInfo *ri) 339 { 340 /* Return true if the regdef would cause an assertion if you called 341 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a 342 * program bug for it not to have the NO_RAW flag). 343 * NB that returning false here doesn't necessarily mean that calling 344 * read/write_raw_cp_reg() is safe, because we can't distinguish "has 345 * read/write access functions which are safe for raw use" from "has 346 * read/write access functions which have side effects but has forgotten 347 * to provide raw access functions". 348 * The tests here line up with the conditions in read/write_raw_cp_reg() 349 * and assertions in raw_read()/raw_write(). 350 */ 351 if ((ri->type & ARM_CP_CONST) || 352 ri->fieldoffset || 353 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { 354 return false; 355 } 356 return true; 357 } 358 359 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync) 360 { 361 /* Write the coprocessor state from cpu->env to the (index,value) list. */ 362 int i; 363 bool ok = true; 364 365 for (i = 0; i < cpu->cpreg_array_len; i++) { 366 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 367 const ARMCPRegInfo *ri; 368 uint64_t newval; 369 370 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 371 if (!ri) { 372 ok = false; 373 continue; 374 } 375 if (ri->type & ARM_CP_NO_RAW) { 376 continue; 377 } 378 379 newval = read_raw_cp_reg(&cpu->env, ri); 380 if (kvm_sync) { 381 /* 382 * Only sync if the previous list->cpustate sync succeeded. 383 * Rather than tracking the success/failure state for every 384 * item in the list, we just recheck "does the raw write we must 385 * have made in write_list_to_cpustate() read back OK" here. 386 */ 387 uint64_t oldval = cpu->cpreg_values[i]; 388 389 if (oldval == newval) { 390 continue; 391 } 392 393 write_raw_cp_reg(&cpu->env, ri, oldval); 394 if (read_raw_cp_reg(&cpu->env, ri) != oldval) { 395 continue; 396 } 397 398 write_raw_cp_reg(&cpu->env, ri, newval); 399 } 400 cpu->cpreg_values[i] = newval; 401 } 402 return ok; 403 } 404 405 bool write_list_to_cpustate(ARMCPU *cpu) 406 { 407 int i; 408 bool ok = true; 409 410 for (i = 0; i < cpu->cpreg_array_len; i++) { 411 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 412 uint64_t v = cpu->cpreg_values[i]; 413 const ARMCPRegInfo *ri; 414 415 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 416 if (!ri) { 417 ok = false; 418 continue; 419 } 420 if (ri->type & ARM_CP_NO_RAW) { 421 continue; 422 } 423 /* Write value and confirm it reads back as written 424 * (to catch read-only registers and partially read-only 425 * registers where the incoming migration value doesn't match) 426 */ 427 write_raw_cp_reg(&cpu->env, ri, v); 428 if (read_raw_cp_reg(&cpu->env, ri) != v) { 429 ok = false; 430 } 431 } 432 return ok; 433 } 434 435 static void add_cpreg_to_list(gpointer key, gpointer opaque) 436 { 437 ARMCPU *cpu = opaque; 438 uint64_t regidx; 439 const ARMCPRegInfo *ri; 440 441 regidx = *(uint32_t *)key; 442 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 443 444 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 445 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); 446 /* The value array need not be initialized at this point */ 447 cpu->cpreg_array_len++; 448 } 449 } 450 451 static void count_cpreg(gpointer key, gpointer opaque) 452 { 453 ARMCPU *cpu = opaque; 454 uint64_t regidx; 455 const ARMCPRegInfo *ri; 456 457 regidx = *(uint32_t *)key; 458 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 459 460 if (!(ri->type & (ARM_CP_NO_RAW|ARM_CP_ALIAS))) { 461 cpu->cpreg_array_len++; 462 } 463 } 464 465 static gint cpreg_key_compare(gconstpointer a, gconstpointer b) 466 { 467 uint64_t aidx = cpreg_to_kvm_id(*(uint32_t *)a); 468 uint64_t bidx = cpreg_to_kvm_id(*(uint32_t *)b); 469 470 if (aidx > bidx) { 471 return 1; 472 } 473 if (aidx < bidx) { 474 return -1; 475 } 476 return 0; 477 } 478 479 void init_cpreg_list(ARMCPU *cpu) 480 { 481 /* Initialise the cpreg_tuples[] array based on the cp_regs hash. 482 * Note that we require cpreg_tuples[] to be sorted by key ID. 483 */ 484 GList *keys; 485 int arraylen; 486 487 keys = g_hash_table_get_keys(cpu->cp_regs); 488 keys = g_list_sort(keys, cpreg_key_compare); 489 490 cpu->cpreg_array_len = 0; 491 492 g_list_foreach(keys, count_cpreg, cpu); 493 494 arraylen = cpu->cpreg_array_len; 495 cpu->cpreg_indexes = g_new(uint64_t, arraylen); 496 cpu->cpreg_values = g_new(uint64_t, arraylen); 497 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); 498 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); 499 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; 500 cpu->cpreg_array_len = 0; 501 502 g_list_foreach(keys, add_cpreg_to_list, cpu); 503 504 assert(cpu->cpreg_array_len == arraylen); 505 506 g_list_free(keys); 507 } 508 509 /* 510 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0. 511 */ 512 static CPAccessResult access_el3_aa32ns(CPUARMState *env, 513 const ARMCPRegInfo *ri, 514 bool isread) 515 { 516 if (!is_a64(env) && arm_current_el(env) == 3 && 517 arm_is_secure_below_el3(env)) { 518 return CP_ACCESS_TRAP_UNCATEGORIZED; 519 } 520 return CP_ACCESS_OK; 521 } 522 523 /* Some secure-only AArch32 registers trap to EL3 if used from 524 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts). 525 * Note that an access from Secure EL1 can only happen if EL3 is AArch64. 526 * We assume that the .access field is set to PL1_RW. 527 */ 528 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env, 529 const ARMCPRegInfo *ri, 530 bool isread) 531 { 532 if (arm_current_el(env) == 3) { 533 return CP_ACCESS_OK; 534 } 535 if (arm_is_secure_below_el3(env)) { 536 return CP_ACCESS_TRAP_EL3; 537 } 538 /* This will be EL1 NS and EL2 NS, which just UNDEF */ 539 return CP_ACCESS_TRAP_UNCATEGORIZED; 540 } 541 542 /* Check for traps to "powerdown debug" registers, which are controlled 543 * by MDCR.TDOSA 544 */ 545 static CPAccessResult access_tdosa(CPUARMState *env, const ARMCPRegInfo *ri, 546 bool isread) 547 { 548 int el = arm_current_el(env); 549 bool mdcr_el2_tdosa = (env->cp15.mdcr_el2 & MDCR_TDOSA) || 550 (env->cp15.mdcr_el2 & MDCR_TDE) || 551 (arm_hcr_el2_eff(env) & HCR_TGE); 552 553 if (el < 2 && mdcr_el2_tdosa && !arm_is_secure_below_el3(env)) { 554 return CP_ACCESS_TRAP_EL2; 555 } 556 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDOSA)) { 557 return CP_ACCESS_TRAP_EL3; 558 } 559 return CP_ACCESS_OK; 560 } 561 562 /* Check for traps to "debug ROM" registers, which are controlled 563 * by MDCR_EL2.TDRA for EL2 but by the more general MDCR_EL3.TDA for EL3. 564 */ 565 static CPAccessResult access_tdra(CPUARMState *env, const ARMCPRegInfo *ri, 566 bool isread) 567 { 568 int el = arm_current_el(env); 569 bool mdcr_el2_tdra = (env->cp15.mdcr_el2 & MDCR_TDRA) || 570 (env->cp15.mdcr_el2 & MDCR_TDE) || 571 (arm_hcr_el2_eff(env) & HCR_TGE); 572 573 if (el < 2 && mdcr_el2_tdra && !arm_is_secure_below_el3(env)) { 574 return CP_ACCESS_TRAP_EL2; 575 } 576 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 577 return CP_ACCESS_TRAP_EL3; 578 } 579 return CP_ACCESS_OK; 580 } 581 582 /* Check for traps to general debug registers, which are controlled 583 * by MDCR_EL2.TDA for EL2 and MDCR_EL3.TDA for EL3. 584 */ 585 static CPAccessResult access_tda(CPUARMState *env, const ARMCPRegInfo *ri, 586 bool isread) 587 { 588 int el = arm_current_el(env); 589 bool mdcr_el2_tda = (env->cp15.mdcr_el2 & MDCR_TDA) || 590 (env->cp15.mdcr_el2 & MDCR_TDE) || 591 (arm_hcr_el2_eff(env) & HCR_TGE); 592 593 if (el < 2 && mdcr_el2_tda && !arm_is_secure_below_el3(env)) { 594 return CP_ACCESS_TRAP_EL2; 595 } 596 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TDA)) { 597 return CP_ACCESS_TRAP_EL3; 598 } 599 return CP_ACCESS_OK; 600 } 601 602 /* Check for traps to performance monitor registers, which are controlled 603 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3. 604 */ 605 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri, 606 bool isread) 607 { 608 int el = arm_current_el(env); 609 610 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 611 && !arm_is_secure_below_el3(env)) { 612 return CP_ACCESS_TRAP_EL2; 613 } 614 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 615 return CP_ACCESS_TRAP_EL3; 616 } 617 return CP_ACCESS_OK; 618 } 619 620 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */ 621 static CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri, 622 bool isread) 623 { 624 if (arm_current_el(env) == 1) { 625 uint64_t trap = isread ? HCR_TRVM : HCR_TVM; 626 if (arm_hcr_el2_eff(env) & trap) { 627 return CP_ACCESS_TRAP_EL2; 628 } 629 } 630 return CP_ACCESS_OK; 631 } 632 633 /* Check for traps from EL1 due to HCR_EL2.TSW. */ 634 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri, 635 bool isread) 636 { 637 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) { 638 return CP_ACCESS_TRAP_EL2; 639 } 640 return CP_ACCESS_OK; 641 } 642 643 /* Check for traps from EL1 due to HCR_EL2.TACR. */ 644 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri, 645 bool isread) 646 { 647 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) { 648 return CP_ACCESS_TRAP_EL2; 649 } 650 return CP_ACCESS_OK; 651 } 652 653 /* Check for traps from EL1 due to HCR_EL2.TTLB. */ 654 static CPAccessResult access_ttlb(CPUARMState *env, const ARMCPRegInfo *ri, 655 bool isread) 656 { 657 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TTLB)) { 658 return CP_ACCESS_TRAP_EL2; 659 } 660 return CP_ACCESS_OK; 661 } 662 663 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 664 { 665 ARMCPU *cpu = env_archcpu(env); 666 667 raw_write(env, ri, value); 668 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */ 669 } 670 671 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 672 { 673 ARMCPU *cpu = env_archcpu(env); 674 675 if (raw_read(env, ri) != value) { 676 /* Unlike real hardware the qemu TLB uses virtual addresses, 677 * not modified virtual addresses, so this causes a TLB flush. 678 */ 679 tlb_flush(CPU(cpu)); 680 raw_write(env, ri, value); 681 } 682 } 683 684 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, 685 uint64_t value) 686 { 687 ARMCPU *cpu = env_archcpu(env); 688 689 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA) 690 && !extended_addresses_enabled(env)) { 691 /* For VMSA (when not using the LPAE long descriptor page table 692 * format) this register includes the ASID, so do a TLB flush. 693 * For PMSA it is purely a process ID and no action is needed. 694 */ 695 tlb_flush(CPU(cpu)); 696 } 697 raw_write(env, ri, value); 698 } 699 700 /* IS variants of TLB operations must affect all cores */ 701 static void tlbiall_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 702 uint64_t value) 703 { 704 CPUState *cs = env_cpu(env); 705 706 tlb_flush_all_cpus_synced(cs); 707 } 708 709 static void tlbiasid_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 710 uint64_t value) 711 { 712 CPUState *cs = env_cpu(env); 713 714 tlb_flush_all_cpus_synced(cs); 715 } 716 717 static void tlbimva_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 718 uint64_t value) 719 { 720 CPUState *cs = env_cpu(env); 721 722 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 723 } 724 725 static void tlbimvaa_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 726 uint64_t value) 727 { 728 CPUState *cs = env_cpu(env); 729 730 tlb_flush_page_all_cpus_synced(cs, value & TARGET_PAGE_MASK); 731 } 732 733 /* 734 * Non-IS variants of TLB operations are upgraded to 735 * IS versions if we are at EL1 and HCR_EL2.FB is effectively set to 736 * force broadcast of these operations. 737 */ 738 static bool tlb_force_broadcast(CPUARMState *env) 739 { 740 return arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_FB); 741 } 742 743 static void tlbiall_write(CPUARMState *env, const ARMCPRegInfo *ri, 744 uint64_t value) 745 { 746 /* Invalidate all (TLBIALL) */ 747 CPUState *cs = env_cpu(env); 748 749 if (tlb_force_broadcast(env)) { 750 tlb_flush_all_cpus_synced(cs); 751 } else { 752 tlb_flush(cs); 753 } 754 } 755 756 static void tlbimva_write(CPUARMState *env, const ARMCPRegInfo *ri, 757 uint64_t value) 758 { 759 /* Invalidate single TLB entry by MVA and ASID (TLBIMVA) */ 760 CPUState *cs = env_cpu(env); 761 762 value &= TARGET_PAGE_MASK; 763 if (tlb_force_broadcast(env)) { 764 tlb_flush_page_all_cpus_synced(cs, value); 765 } else { 766 tlb_flush_page(cs, value); 767 } 768 } 769 770 static void tlbiasid_write(CPUARMState *env, const ARMCPRegInfo *ri, 771 uint64_t value) 772 { 773 /* Invalidate by ASID (TLBIASID) */ 774 CPUState *cs = env_cpu(env); 775 776 if (tlb_force_broadcast(env)) { 777 tlb_flush_all_cpus_synced(cs); 778 } else { 779 tlb_flush(cs); 780 } 781 } 782 783 static void tlbimvaa_write(CPUARMState *env, const ARMCPRegInfo *ri, 784 uint64_t value) 785 { 786 /* Invalidate single entry by MVA, all ASIDs (TLBIMVAA) */ 787 CPUState *cs = env_cpu(env); 788 789 value &= TARGET_PAGE_MASK; 790 if (tlb_force_broadcast(env)) { 791 tlb_flush_page_all_cpus_synced(cs, value); 792 } else { 793 tlb_flush_page(cs, value); 794 } 795 } 796 797 static void tlbiall_nsnh_write(CPUARMState *env, const ARMCPRegInfo *ri, 798 uint64_t value) 799 { 800 CPUState *cs = env_cpu(env); 801 802 tlb_flush_by_mmuidx(cs, 803 ARMMMUIdxBit_E10_1 | 804 ARMMMUIdxBit_E10_1_PAN | 805 ARMMMUIdxBit_E10_0); 806 } 807 808 static void tlbiall_nsnh_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 809 uint64_t value) 810 { 811 CPUState *cs = env_cpu(env); 812 813 tlb_flush_by_mmuidx_all_cpus_synced(cs, 814 ARMMMUIdxBit_E10_1 | 815 ARMMMUIdxBit_E10_1_PAN | 816 ARMMMUIdxBit_E10_0); 817 } 818 819 820 static void tlbiall_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 821 uint64_t value) 822 { 823 CPUState *cs = env_cpu(env); 824 825 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_E2); 826 } 827 828 static void tlbiall_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 829 uint64_t value) 830 { 831 CPUState *cs = env_cpu(env); 832 833 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_E2); 834 } 835 836 static void tlbimva_hyp_write(CPUARMState *env, const ARMCPRegInfo *ri, 837 uint64_t value) 838 { 839 CPUState *cs = env_cpu(env); 840 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 841 842 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_E2); 843 } 844 845 static void tlbimva_hyp_is_write(CPUARMState *env, const ARMCPRegInfo *ri, 846 uint64_t value) 847 { 848 CPUState *cs = env_cpu(env); 849 uint64_t pageaddr = value & ~MAKE_64BIT_MASK(0, 12); 850 851 tlb_flush_page_by_mmuidx_all_cpus_synced(cs, pageaddr, 852 ARMMMUIdxBit_E2); 853 } 854 855 static const ARMCPRegInfo cp_reginfo[] = { 856 /* Define the secure and non-secure FCSE identifier CP registers 857 * separately because there is no secure bank in V8 (no _EL3). This allows 858 * the secure register to be properly reset and migrated. There is also no 859 * v8 EL1 version of the register so the non-secure instance stands alone. 860 */ 861 { .name = "FCSEIDR", 862 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 863 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 864 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns), 865 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 866 { .name = "FCSEIDR_S", 867 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 868 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 869 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s), 870 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 871 /* Define the secure and non-secure context identifier CP registers 872 * separately because there is no secure bank in V8 (no _EL3). This allows 873 * the secure register to be properly reset and migrated. In the 874 * non-secure case, the 32-bit register will have reset and migration 875 * disabled during registration as it is handled by the 64-bit instance. 876 */ 877 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH, 878 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 879 .access = PL1_RW, .accessfn = access_tvm_trvm, 880 .secure = ARM_CP_SECSTATE_NS, 881 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]), 882 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 883 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32, 884 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 885 .access = PL1_RW, .accessfn = access_tvm_trvm, 886 .secure = ARM_CP_SECSTATE_S, 887 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s), 888 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 889 REGINFO_SENTINEL 890 }; 891 892 static const ARMCPRegInfo not_v8_cp_reginfo[] = { 893 /* NB: Some of these registers exist in v8 but with more precise 894 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). 895 */ 896 /* MMU Domain access control / MPU write buffer control */ 897 { .name = "DACR", 898 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, 899 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 900 .writefn = dacr_write, .raw_writefn = raw_write, 901 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 902 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 903 /* ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs. 904 * For v6 and v5, these mappings are overly broad. 905 */ 906 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0, 907 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 908 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1, 909 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 910 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4, 911 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 912 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8, 913 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 914 /* Cache maintenance ops; some of this space may be overridden later. */ 915 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 916 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 917 .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, 918 REGINFO_SENTINEL 919 }; 920 921 static const ARMCPRegInfo not_v6_cp_reginfo[] = { 922 /* Not all pre-v6 cores implemented this WFI, so this is slightly 923 * over-broad. 924 */ 925 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, 926 .access = PL1_W, .type = ARM_CP_WFI }, 927 REGINFO_SENTINEL 928 }; 929 930 static const ARMCPRegInfo not_v7_cp_reginfo[] = { 931 /* Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which 932 * is UNPREDICTABLE; we choose to NOP as most implementations do). 933 */ 934 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 935 .access = PL1_W, .type = ARM_CP_WFI }, 936 /* L1 cache lockdown. Not architectural in v6 and earlier but in practice 937 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and 938 * OMAPCP will override this space. 939 */ 940 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, 941 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), 942 .resetvalue = 0 }, 943 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, 944 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), 945 .resetvalue = 0 }, 946 /* v6 doesn't have the cache ID registers but Linux reads them anyway */ 947 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, 948 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 949 .resetvalue = 0 }, 950 /* We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; 951 * implementing it as RAZ means the "debug architecture version" bits 952 * will read as a reserved value, which should cause Linux to not try 953 * to use the debug hardware. 954 */ 955 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 956 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 957 /* MMU TLB control. Note that the wildcarding means we cover not just 958 * the unified TLB ops but also the dside/iside/inner-shareable variants. 959 */ 960 { .name = "TLBIALL", .cp = 15, .crn = 8, .crm = CP_ANY, 961 .opc1 = CP_ANY, .opc2 = 0, .access = PL1_W, .writefn = tlbiall_write, 962 .type = ARM_CP_NO_RAW }, 963 { .name = "TLBIMVA", .cp = 15, .crn = 8, .crm = CP_ANY, 964 .opc1 = CP_ANY, .opc2 = 1, .access = PL1_W, .writefn = tlbimva_write, 965 .type = ARM_CP_NO_RAW }, 966 { .name = "TLBIASID", .cp = 15, .crn = 8, .crm = CP_ANY, 967 .opc1 = CP_ANY, .opc2 = 2, .access = PL1_W, .writefn = tlbiasid_write, 968 .type = ARM_CP_NO_RAW }, 969 { .name = "TLBIMVAA", .cp = 15, .crn = 8, .crm = CP_ANY, 970 .opc1 = CP_ANY, .opc2 = 3, .access = PL1_W, .writefn = tlbimvaa_write, 971 .type = ARM_CP_NO_RAW }, 972 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2, 973 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP }, 974 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2, 975 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP }, 976 REGINFO_SENTINEL 977 }; 978 979 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, 980 uint64_t value) 981 { 982 uint32_t mask = 0; 983 984 /* In ARMv8 most bits of CPACR_EL1 are RES0. */ 985 if (!arm_feature(env, ARM_FEATURE_V8)) { 986 /* ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. 987 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. 988 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. 989 */ 990 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) { 991 /* VFP coprocessor: cp10 & cp11 [23:20] */ 992 mask |= (1 << 31) | (1 << 30) | (0xf << 20); 993 994 if (!arm_feature(env, ARM_FEATURE_NEON)) { 995 /* ASEDIS [31] bit is RAO/WI */ 996 value |= (1 << 31); 997 } 998 999 /* VFPv3 and upwards with NEON implement 32 double precision 1000 * registers (D0-D31). 1001 */ 1002 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) { 1003 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ 1004 value |= (1 << 30); 1005 } 1006 } 1007 value &= mask; 1008 } 1009 1010 /* 1011 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10 1012 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00. 1013 */ 1014 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 1015 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 1016 value &= ~(0xf << 20); 1017 value |= env->cp15.cpacr_el1 & (0xf << 20); 1018 } 1019 1020 env->cp15.cpacr_el1 = value; 1021 } 1022 1023 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1024 { 1025 /* 1026 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10 1027 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00. 1028 */ 1029 uint64_t value = env->cp15.cpacr_el1; 1030 1031 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 1032 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 1033 value &= ~(0xf << 20); 1034 } 1035 return value; 1036 } 1037 1038 1039 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1040 { 1041 /* Call cpacr_write() so that we reset with the correct RAO bits set 1042 * for our CPU features. 1043 */ 1044 cpacr_write(env, ri, 0); 1045 } 1046 1047 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 1048 bool isread) 1049 { 1050 if (arm_feature(env, ARM_FEATURE_V8)) { 1051 /* Check if CPACR accesses are to be trapped to EL2 */ 1052 if (arm_current_el(env) == 1 && 1053 (env->cp15.cptr_el[2] & CPTR_TCPAC) && !arm_is_secure(env)) { 1054 return CP_ACCESS_TRAP_EL2; 1055 /* Check if CPACR accesses are to be trapped to EL3 */ 1056 } else if (arm_current_el(env) < 3 && 1057 (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 1058 return CP_ACCESS_TRAP_EL3; 1059 } 1060 } 1061 1062 return CP_ACCESS_OK; 1063 } 1064 1065 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri, 1066 bool isread) 1067 { 1068 /* Check if CPTR accesses are set to trap to EL3 */ 1069 if (arm_current_el(env) == 2 && (env->cp15.cptr_el[3] & CPTR_TCPAC)) { 1070 return CP_ACCESS_TRAP_EL3; 1071 } 1072 1073 return CP_ACCESS_OK; 1074 } 1075 1076 static const ARMCPRegInfo v6_cp_reginfo[] = { 1077 /* prefetch by MVA in v6, NOP in v7 */ 1078 { .name = "MVA_prefetch", 1079 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, 1080 .access = PL1_W, .type = ARM_CP_NOP }, 1081 /* We need to break the TB after ISB to execute self-modifying code 1082 * correctly and also to take any pending interrupts immediately. 1083 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag. 1084 */ 1085 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, 1086 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore }, 1087 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, 1088 .access = PL0_W, .type = ARM_CP_NOP }, 1089 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, 1090 .access = PL0_W, .type = ARM_CP_NOP }, 1091 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, 1092 .access = PL1_RW, .accessfn = access_tvm_trvm, 1093 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), 1094 offsetof(CPUARMState, cp15.ifar_ns) }, 1095 .resetvalue = 0, }, 1096 /* Watchpoint Fault Address Register : should actually only be present 1097 * for 1136, 1176, 11MPCore. 1098 */ 1099 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, 1100 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, 1101 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, 1102 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access, 1103 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1), 1104 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read }, 1105 REGINFO_SENTINEL 1106 }; 1107 1108 /* Definitions for the PMU registers */ 1109 #define PMCRN_MASK 0xf800 1110 #define PMCRN_SHIFT 11 1111 #define PMCRLC 0x40 1112 #define PMCRDP 0x20 1113 #define PMCRX 0x10 1114 #define PMCRD 0x8 1115 #define PMCRC 0x4 1116 #define PMCRP 0x2 1117 #define PMCRE 0x1 1118 /* 1119 * Mask of PMCR bits writeable by guest (not including WO bits like C, P, 1120 * which can be written as 1 to trigger behaviour but which stay RAZ). 1121 */ 1122 #define PMCR_WRITEABLE_MASK (PMCRLC | PMCRDP | PMCRX | PMCRD | PMCRE) 1123 1124 #define PMXEVTYPER_P 0x80000000 1125 #define PMXEVTYPER_U 0x40000000 1126 #define PMXEVTYPER_NSK 0x20000000 1127 #define PMXEVTYPER_NSU 0x10000000 1128 #define PMXEVTYPER_NSH 0x08000000 1129 #define PMXEVTYPER_M 0x04000000 1130 #define PMXEVTYPER_MT 0x02000000 1131 #define PMXEVTYPER_EVTCOUNT 0x0000ffff 1132 #define PMXEVTYPER_MASK (PMXEVTYPER_P | PMXEVTYPER_U | PMXEVTYPER_NSK | \ 1133 PMXEVTYPER_NSU | PMXEVTYPER_NSH | \ 1134 PMXEVTYPER_M | PMXEVTYPER_MT | \ 1135 PMXEVTYPER_EVTCOUNT) 1136 1137 #define PMCCFILTR 0xf8000000 1138 #define PMCCFILTR_M PMXEVTYPER_M 1139 #define PMCCFILTR_EL0 (PMCCFILTR | PMCCFILTR_M) 1140 1141 static inline uint32_t pmu_num_counters(CPUARMState *env) 1142 { 1143 return (env->cp15.c9_pmcr & PMCRN_MASK) >> PMCRN_SHIFT; 1144 } 1145 1146 /* Bits allowed to be set/cleared for PMCNTEN* and PMINTEN* */ 1147 static inline uint64_t pmu_counter_mask(CPUARMState *env) 1148 { 1149 return (1 << 31) | ((1 << pmu_num_counters(env)) - 1); 1150 } 1151 1152 typedef struct pm_event { 1153 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */ 1154 /* If the event is supported on this CPU (used to generate PMCEID[01]) */ 1155 bool (*supported)(CPUARMState *); 1156 /* 1157 * Retrieve the current count of the underlying event. The programmed 1158 * counters hold a difference from the return value from this function 1159 */ 1160 uint64_t (*get_count)(CPUARMState *); 1161 /* 1162 * Return how many nanoseconds it will take (at a minimum) for count events 1163 * to occur. A negative value indicates the counter will never overflow, or 1164 * that the counter has otherwise arranged for the overflow bit to be set 1165 * and the PMU interrupt to be raised on overflow. 1166 */ 1167 int64_t (*ns_per_count)(uint64_t); 1168 } pm_event; 1169 1170 static bool event_always_supported(CPUARMState *env) 1171 { 1172 return true; 1173 } 1174 1175 static uint64_t swinc_get_count(CPUARMState *env) 1176 { 1177 /* 1178 * SW_INCR events are written directly to the pmevcntr's by writes to 1179 * PMSWINC, so there is no underlying count maintained by the PMU itself 1180 */ 1181 return 0; 1182 } 1183 1184 static int64_t swinc_ns_per(uint64_t ignored) 1185 { 1186 return -1; 1187 } 1188 1189 /* 1190 * Return the underlying cycle count for the PMU cycle counters. If we're in 1191 * usermode, simply return 0. 1192 */ 1193 static uint64_t cycles_get_count(CPUARMState *env) 1194 { 1195 #ifndef CONFIG_USER_ONLY 1196 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1197 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 1198 #else 1199 return cpu_get_host_ticks(); 1200 #endif 1201 } 1202 1203 #ifndef CONFIG_USER_ONLY 1204 static int64_t cycles_ns_per(uint64_t cycles) 1205 { 1206 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles; 1207 } 1208 1209 static bool instructions_supported(CPUARMState *env) 1210 { 1211 return icount_enabled() == 1; /* Precise instruction counting */ 1212 } 1213 1214 static uint64_t instructions_get_count(CPUARMState *env) 1215 { 1216 return (uint64_t)icount_get_raw(); 1217 } 1218 1219 static int64_t instructions_ns_per(uint64_t icount) 1220 { 1221 return icount_to_ns((int64_t)icount); 1222 } 1223 #endif 1224 1225 static bool pmu_8_1_events_supported(CPUARMState *env) 1226 { 1227 /* For events which are supported in any v8.1 PMU */ 1228 return cpu_isar_feature(any_pmu_8_1, env_archcpu(env)); 1229 } 1230 1231 static bool pmu_8_4_events_supported(CPUARMState *env) 1232 { 1233 /* For events which are supported in any v8.1 PMU */ 1234 return cpu_isar_feature(any_pmu_8_4, env_archcpu(env)); 1235 } 1236 1237 static uint64_t zero_event_get_count(CPUARMState *env) 1238 { 1239 /* For events which on QEMU never fire, so their count is always zero */ 1240 return 0; 1241 } 1242 1243 static int64_t zero_event_ns_per(uint64_t cycles) 1244 { 1245 /* An event which never fires can never overflow */ 1246 return -1; 1247 } 1248 1249 static const pm_event pm_events[] = { 1250 { .number = 0x000, /* SW_INCR */ 1251 .supported = event_always_supported, 1252 .get_count = swinc_get_count, 1253 .ns_per_count = swinc_ns_per, 1254 }, 1255 #ifndef CONFIG_USER_ONLY 1256 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */ 1257 .supported = instructions_supported, 1258 .get_count = instructions_get_count, 1259 .ns_per_count = instructions_ns_per, 1260 }, 1261 { .number = 0x011, /* CPU_CYCLES, Cycle */ 1262 .supported = event_always_supported, 1263 .get_count = cycles_get_count, 1264 .ns_per_count = cycles_ns_per, 1265 }, 1266 #endif 1267 { .number = 0x023, /* STALL_FRONTEND */ 1268 .supported = pmu_8_1_events_supported, 1269 .get_count = zero_event_get_count, 1270 .ns_per_count = zero_event_ns_per, 1271 }, 1272 { .number = 0x024, /* STALL_BACKEND */ 1273 .supported = pmu_8_1_events_supported, 1274 .get_count = zero_event_get_count, 1275 .ns_per_count = zero_event_ns_per, 1276 }, 1277 { .number = 0x03c, /* STALL */ 1278 .supported = pmu_8_4_events_supported, 1279 .get_count = zero_event_get_count, 1280 .ns_per_count = zero_event_ns_per, 1281 }, 1282 }; 1283 1284 /* 1285 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of 1286 * events (i.e. the statistical profiling extension), this implementation 1287 * should first be updated to something sparse instead of the current 1288 * supported_event_map[] array. 1289 */ 1290 #define MAX_EVENT_ID 0x3c 1291 #define UNSUPPORTED_EVENT UINT16_MAX 1292 static uint16_t supported_event_map[MAX_EVENT_ID + 1]; 1293 1294 /* 1295 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map 1296 * of ARM event numbers to indices in our pm_events array. 1297 * 1298 * Note: Events in the 0x40XX range are not currently supported. 1299 */ 1300 void pmu_init(ARMCPU *cpu) 1301 { 1302 unsigned int i; 1303 1304 /* 1305 * Empty supported_event_map and cpu->pmceid[01] before adding supported 1306 * events to them 1307 */ 1308 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) { 1309 supported_event_map[i] = UNSUPPORTED_EVENT; 1310 } 1311 cpu->pmceid0 = 0; 1312 cpu->pmceid1 = 0; 1313 1314 for (i = 0; i < ARRAY_SIZE(pm_events); i++) { 1315 const pm_event *cnt = &pm_events[i]; 1316 assert(cnt->number <= MAX_EVENT_ID); 1317 /* We do not currently support events in the 0x40xx range */ 1318 assert(cnt->number <= 0x3f); 1319 1320 if (cnt->supported(&cpu->env)) { 1321 supported_event_map[cnt->number] = i; 1322 uint64_t event_mask = 1ULL << (cnt->number & 0x1f); 1323 if (cnt->number & 0x20) { 1324 cpu->pmceid1 |= event_mask; 1325 } else { 1326 cpu->pmceid0 |= event_mask; 1327 } 1328 } 1329 } 1330 } 1331 1332 /* 1333 * Check at runtime whether a PMU event is supported for the current machine 1334 */ 1335 static bool event_supported(uint16_t number) 1336 { 1337 if (number > MAX_EVENT_ID) { 1338 return false; 1339 } 1340 return supported_event_map[number] != UNSUPPORTED_EVENT; 1341 } 1342 1343 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri, 1344 bool isread) 1345 { 1346 /* Performance monitor registers user accessibility is controlled 1347 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable 1348 * trapping to EL2 or EL3 for other accesses. 1349 */ 1350 int el = arm_current_el(env); 1351 1352 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) { 1353 return CP_ACCESS_TRAP; 1354 } 1355 if (el < 2 && (env->cp15.mdcr_el2 & MDCR_TPM) 1356 && !arm_is_secure_below_el3(env)) { 1357 return CP_ACCESS_TRAP_EL2; 1358 } 1359 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 1360 return CP_ACCESS_TRAP_EL3; 1361 } 1362 1363 return CP_ACCESS_OK; 1364 } 1365 1366 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env, 1367 const ARMCPRegInfo *ri, 1368 bool isread) 1369 { 1370 /* ER: event counter read trap control */ 1371 if (arm_feature(env, ARM_FEATURE_V8) 1372 && arm_current_el(env) == 0 1373 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0 1374 && isread) { 1375 return CP_ACCESS_OK; 1376 } 1377 1378 return pmreg_access(env, ri, isread); 1379 } 1380 1381 static CPAccessResult pmreg_access_swinc(CPUARMState *env, 1382 const ARMCPRegInfo *ri, 1383 bool isread) 1384 { 1385 /* SW: software increment write trap control */ 1386 if (arm_feature(env, ARM_FEATURE_V8) 1387 && arm_current_el(env) == 0 1388 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0 1389 && !isread) { 1390 return CP_ACCESS_OK; 1391 } 1392 1393 return pmreg_access(env, ri, isread); 1394 } 1395 1396 static CPAccessResult pmreg_access_selr(CPUARMState *env, 1397 const ARMCPRegInfo *ri, 1398 bool isread) 1399 { 1400 /* ER: event counter read trap control */ 1401 if (arm_feature(env, ARM_FEATURE_V8) 1402 && arm_current_el(env) == 0 1403 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) { 1404 return CP_ACCESS_OK; 1405 } 1406 1407 return pmreg_access(env, ri, isread); 1408 } 1409 1410 static CPAccessResult pmreg_access_ccntr(CPUARMState *env, 1411 const ARMCPRegInfo *ri, 1412 bool isread) 1413 { 1414 /* CR: cycle counter read trap control */ 1415 if (arm_feature(env, ARM_FEATURE_V8) 1416 && arm_current_el(env) == 0 1417 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0 1418 && isread) { 1419 return CP_ACCESS_OK; 1420 } 1421 1422 return pmreg_access(env, ri, isread); 1423 } 1424 1425 /* Returns true if the counter (pass 31 for PMCCNTR) should count events using 1426 * the current EL, security state, and register configuration. 1427 */ 1428 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter) 1429 { 1430 uint64_t filter; 1431 bool e, p, u, nsk, nsu, nsh, m; 1432 bool enabled, prohibited, filtered; 1433 bool secure = arm_is_secure(env); 1434 int el = arm_current_el(env); 1435 uint8_t hpmn = env->cp15.mdcr_el2 & MDCR_HPMN; 1436 1437 if (!arm_feature(env, ARM_FEATURE_PMU)) { 1438 return false; 1439 } 1440 1441 if (!arm_feature(env, ARM_FEATURE_EL2) || 1442 (counter < hpmn || counter == 31)) { 1443 e = env->cp15.c9_pmcr & PMCRE; 1444 } else { 1445 e = env->cp15.mdcr_el2 & MDCR_HPME; 1446 } 1447 enabled = e && (env->cp15.c9_pmcnten & (1 << counter)); 1448 1449 if (!secure) { 1450 if (el == 2 && (counter < hpmn || counter == 31)) { 1451 prohibited = env->cp15.mdcr_el2 & MDCR_HPMD; 1452 } else { 1453 prohibited = false; 1454 } 1455 } else { 1456 prohibited = arm_feature(env, ARM_FEATURE_EL3) && 1457 !(env->cp15.mdcr_el3 & MDCR_SPME); 1458 } 1459 1460 if (prohibited && counter == 31) { 1461 prohibited = env->cp15.c9_pmcr & PMCRDP; 1462 } 1463 1464 if (counter == 31) { 1465 filter = env->cp15.pmccfiltr_el0; 1466 } else { 1467 filter = env->cp15.c14_pmevtyper[counter]; 1468 } 1469 1470 p = filter & PMXEVTYPER_P; 1471 u = filter & PMXEVTYPER_U; 1472 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK); 1473 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU); 1474 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH); 1475 m = arm_el_is_aa64(env, 1) && 1476 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M); 1477 1478 if (el == 0) { 1479 filtered = secure ? u : u != nsu; 1480 } else if (el == 1) { 1481 filtered = secure ? p : p != nsk; 1482 } else if (el == 2) { 1483 filtered = !nsh; 1484 } else { /* EL3 */ 1485 filtered = m != p; 1486 } 1487 1488 if (counter != 31) { 1489 /* 1490 * If not checking PMCCNTR, ensure the counter is setup to an event we 1491 * support 1492 */ 1493 uint16_t event = filter & PMXEVTYPER_EVTCOUNT; 1494 if (!event_supported(event)) { 1495 return false; 1496 } 1497 } 1498 1499 return enabled && !prohibited && !filtered; 1500 } 1501 1502 static void pmu_update_irq(CPUARMState *env) 1503 { 1504 ARMCPU *cpu = env_archcpu(env); 1505 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) && 1506 (env->cp15.c9_pminten & env->cp15.c9_pmovsr)); 1507 } 1508 1509 /* 1510 * Ensure c15_ccnt is the guest-visible count so that operations such as 1511 * enabling/disabling the counter or filtering, modifying the count itself, 1512 * etc. can be done logically. This is essentially a no-op if the counter is 1513 * not enabled at the time of the call. 1514 */ 1515 static void pmccntr_op_start(CPUARMState *env) 1516 { 1517 uint64_t cycles = cycles_get_count(env); 1518 1519 if (pmu_counter_enabled(env, 31)) { 1520 uint64_t eff_cycles = cycles; 1521 if (env->cp15.c9_pmcr & PMCRD) { 1522 /* Increment once every 64 processor clock cycles */ 1523 eff_cycles /= 64; 1524 } 1525 1526 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta; 1527 1528 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \ 1529 1ull << 63 : 1ull << 31; 1530 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) { 1531 env->cp15.c9_pmovsr |= (1 << 31); 1532 pmu_update_irq(env); 1533 } 1534 1535 env->cp15.c15_ccnt = new_pmccntr; 1536 } 1537 env->cp15.c15_ccnt_delta = cycles; 1538 } 1539 1540 /* 1541 * If PMCCNTR is enabled, recalculate the delta between the clock and the 1542 * guest-visible count. A call to pmccntr_op_finish should follow every call to 1543 * pmccntr_op_start. 1544 */ 1545 static void pmccntr_op_finish(CPUARMState *env) 1546 { 1547 if (pmu_counter_enabled(env, 31)) { 1548 #ifndef CONFIG_USER_ONLY 1549 /* Calculate when the counter will next overflow */ 1550 uint64_t remaining_cycles = -env->cp15.c15_ccnt; 1551 if (!(env->cp15.c9_pmcr & PMCRLC)) { 1552 remaining_cycles = (uint32_t)remaining_cycles; 1553 } 1554 int64_t overflow_in = cycles_ns_per(remaining_cycles); 1555 1556 if (overflow_in > 0) { 1557 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1558 overflow_in; 1559 ARMCPU *cpu = env_archcpu(env); 1560 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1561 } 1562 #endif 1563 1564 uint64_t prev_cycles = env->cp15.c15_ccnt_delta; 1565 if (env->cp15.c9_pmcr & PMCRD) { 1566 /* Increment once every 64 processor clock cycles */ 1567 prev_cycles /= 64; 1568 } 1569 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt; 1570 } 1571 } 1572 1573 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter) 1574 { 1575 1576 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1577 uint64_t count = 0; 1578 if (event_supported(event)) { 1579 uint16_t event_idx = supported_event_map[event]; 1580 count = pm_events[event_idx].get_count(env); 1581 } 1582 1583 if (pmu_counter_enabled(env, counter)) { 1584 uint32_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter]; 1585 1586 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & INT32_MIN) { 1587 env->cp15.c9_pmovsr |= (1 << counter); 1588 pmu_update_irq(env); 1589 } 1590 env->cp15.c14_pmevcntr[counter] = new_pmevcntr; 1591 } 1592 env->cp15.c14_pmevcntr_delta[counter] = count; 1593 } 1594 1595 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter) 1596 { 1597 if (pmu_counter_enabled(env, counter)) { 1598 #ifndef CONFIG_USER_ONLY 1599 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1600 uint16_t event_idx = supported_event_map[event]; 1601 uint64_t delta = UINT32_MAX - 1602 (uint32_t)env->cp15.c14_pmevcntr[counter] + 1; 1603 int64_t overflow_in = pm_events[event_idx].ns_per_count(delta); 1604 1605 if (overflow_in > 0) { 1606 int64_t overflow_at = qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) + 1607 overflow_in; 1608 ARMCPU *cpu = env_archcpu(env); 1609 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1610 } 1611 #endif 1612 1613 env->cp15.c14_pmevcntr_delta[counter] -= 1614 env->cp15.c14_pmevcntr[counter]; 1615 } 1616 } 1617 1618 void pmu_op_start(CPUARMState *env) 1619 { 1620 unsigned int i; 1621 pmccntr_op_start(env); 1622 for (i = 0; i < pmu_num_counters(env); i++) { 1623 pmevcntr_op_start(env, i); 1624 } 1625 } 1626 1627 void pmu_op_finish(CPUARMState *env) 1628 { 1629 unsigned int i; 1630 pmccntr_op_finish(env); 1631 for (i = 0; i < pmu_num_counters(env); i++) { 1632 pmevcntr_op_finish(env, i); 1633 } 1634 } 1635 1636 void pmu_pre_el_change(ARMCPU *cpu, void *ignored) 1637 { 1638 pmu_op_start(&cpu->env); 1639 } 1640 1641 void pmu_post_el_change(ARMCPU *cpu, void *ignored) 1642 { 1643 pmu_op_finish(&cpu->env); 1644 } 1645 1646 void arm_pmu_timer_cb(void *opaque) 1647 { 1648 ARMCPU *cpu = opaque; 1649 1650 /* 1651 * Update all the counter values based on the current underlying counts, 1652 * triggering interrupts to be raised, if necessary. pmu_op_finish() also 1653 * has the effect of setting the cpu->pmu_timer to the next earliest time a 1654 * counter may expire. 1655 */ 1656 pmu_op_start(&cpu->env); 1657 pmu_op_finish(&cpu->env); 1658 } 1659 1660 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1661 uint64_t value) 1662 { 1663 pmu_op_start(env); 1664 1665 if (value & PMCRC) { 1666 /* The counter has been reset */ 1667 env->cp15.c15_ccnt = 0; 1668 } 1669 1670 if (value & PMCRP) { 1671 unsigned int i; 1672 for (i = 0; i < pmu_num_counters(env); i++) { 1673 env->cp15.c14_pmevcntr[i] = 0; 1674 } 1675 } 1676 1677 env->cp15.c9_pmcr &= ~PMCR_WRITEABLE_MASK; 1678 env->cp15.c9_pmcr |= (value & PMCR_WRITEABLE_MASK); 1679 1680 pmu_op_finish(env); 1681 } 1682 1683 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri, 1684 uint64_t value) 1685 { 1686 unsigned int i; 1687 for (i = 0; i < pmu_num_counters(env); i++) { 1688 /* Increment a counter's count iff: */ 1689 if ((value & (1 << i)) && /* counter's bit is set */ 1690 /* counter is enabled and not filtered */ 1691 pmu_counter_enabled(env, i) && 1692 /* counter is SW_INCR */ 1693 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) { 1694 pmevcntr_op_start(env, i); 1695 1696 /* 1697 * Detect if this write causes an overflow since we can't predict 1698 * PMSWINC overflows like we can for other events 1699 */ 1700 uint32_t new_pmswinc = env->cp15.c14_pmevcntr[i] + 1; 1701 1702 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & INT32_MIN) { 1703 env->cp15.c9_pmovsr |= (1 << i); 1704 pmu_update_irq(env); 1705 } 1706 1707 env->cp15.c14_pmevcntr[i] = new_pmswinc; 1708 1709 pmevcntr_op_finish(env, i); 1710 } 1711 } 1712 } 1713 1714 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1715 { 1716 uint64_t ret; 1717 pmccntr_op_start(env); 1718 ret = env->cp15.c15_ccnt; 1719 pmccntr_op_finish(env); 1720 return ret; 1721 } 1722 1723 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1724 uint64_t value) 1725 { 1726 /* The value of PMSELR.SEL affects the behavior of PMXEVTYPER and 1727 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the 1728 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are 1729 * accessed. 1730 */ 1731 env->cp15.c9_pmselr = value & 0x1f; 1732 } 1733 1734 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1735 uint64_t value) 1736 { 1737 pmccntr_op_start(env); 1738 env->cp15.c15_ccnt = value; 1739 pmccntr_op_finish(env); 1740 } 1741 1742 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, 1743 uint64_t value) 1744 { 1745 uint64_t cur_val = pmccntr_read(env, NULL); 1746 1747 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); 1748 } 1749 1750 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1751 uint64_t value) 1752 { 1753 pmccntr_op_start(env); 1754 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0; 1755 pmccntr_op_finish(env); 1756 } 1757 1758 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri, 1759 uint64_t value) 1760 { 1761 pmccntr_op_start(env); 1762 /* M is not accessible from AArch32 */ 1763 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) | 1764 (value & PMCCFILTR); 1765 pmccntr_op_finish(env); 1766 } 1767 1768 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri) 1769 { 1770 /* M is not visible in AArch32 */ 1771 return env->cp15.pmccfiltr_el0 & PMCCFILTR; 1772 } 1773 1774 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1775 uint64_t value) 1776 { 1777 value &= pmu_counter_mask(env); 1778 env->cp15.c9_pmcnten |= value; 1779 } 1780 1781 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1782 uint64_t value) 1783 { 1784 value &= pmu_counter_mask(env); 1785 env->cp15.c9_pmcnten &= ~value; 1786 } 1787 1788 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1789 uint64_t value) 1790 { 1791 value &= pmu_counter_mask(env); 1792 env->cp15.c9_pmovsr &= ~value; 1793 pmu_update_irq(env); 1794 } 1795 1796 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1797 uint64_t value) 1798 { 1799 value &= pmu_counter_mask(env); 1800 env->cp15.c9_pmovsr |= value; 1801 pmu_update_irq(env); 1802 } 1803 1804 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1805 uint64_t value, const uint8_t counter) 1806 { 1807 if (counter == 31) { 1808 pmccfiltr_write(env, ri, value); 1809 } else if (counter < pmu_num_counters(env)) { 1810 pmevcntr_op_start(env, counter); 1811 1812 /* 1813 * If this counter's event type is changing, store the current 1814 * underlying count for the new type in c14_pmevcntr_delta[counter] so 1815 * pmevcntr_op_finish has the correct baseline when it converts back to 1816 * a delta. 1817 */ 1818 uint16_t old_event = env->cp15.c14_pmevtyper[counter] & 1819 PMXEVTYPER_EVTCOUNT; 1820 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT; 1821 if (old_event != new_event) { 1822 uint64_t count = 0; 1823 if (event_supported(new_event)) { 1824 uint16_t event_idx = supported_event_map[new_event]; 1825 count = pm_events[event_idx].get_count(env); 1826 } 1827 env->cp15.c14_pmevcntr_delta[counter] = count; 1828 } 1829 1830 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK; 1831 pmevcntr_op_finish(env, counter); 1832 } 1833 /* Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when 1834 * PMSELR value is equal to or greater than the number of implemented 1835 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI. 1836 */ 1837 } 1838 1839 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri, 1840 const uint8_t counter) 1841 { 1842 if (counter == 31) { 1843 return env->cp15.pmccfiltr_el0; 1844 } else if (counter < pmu_num_counters(env)) { 1845 return env->cp15.c14_pmevtyper[counter]; 1846 } else { 1847 /* 1848 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER 1849 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write(). 1850 */ 1851 return 0; 1852 } 1853 } 1854 1855 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1856 uint64_t value) 1857 { 1858 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1859 pmevtyper_write(env, ri, value, counter); 1860 } 1861 1862 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1863 uint64_t value) 1864 { 1865 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1866 env->cp15.c14_pmevtyper[counter] = value; 1867 1868 /* 1869 * pmevtyper_rawwrite is called between a pair of pmu_op_start and 1870 * pmu_op_finish calls when loading saved state for a migration. Because 1871 * we're potentially updating the type of event here, the value written to 1872 * c14_pmevcntr_delta by the preceeding pmu_op_start call may be for a 1873 * different counter type. Therefore, we need to set this value to the 1874 * current count for the counter type we're writing so that pmu_op_finish 1875 * has the correct count for its calculation. 1876 */ 1877 uint16_t event = value & PMXEVTYPER_EVTCOUNT; 1878 if (event_supported(event)) { 1879 uint16_t event_idx = supported_event_map[event]; 1880 env->cp15.c14_pmevcntr_delta[counter] = 1881 pm_events[event_idx].get_count(env); 1882 } 1883 } 1884 1885 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1886 { 1887 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1888 return pmevtyper_read(env, ri, counter); 1889 } 1890 1891 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1892 uint64_t value) 1893 { 1894 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31); 1895 } 1896 1897 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri) 1898 { 1899 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31); 1900 } 1901 1902 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1903 uint64_t value, uint8_t counter) 1904 { 1905 if (counter < pmu_num_counters(env)) { 1906 pmevcntr_op_start(env, counter); 1907 env->cp15.c14_pmevcntr[counter] = value; 1908 pmevcntr_op_finish(env, counter); 1909 } 1910 /* 1911 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1912 * are CONSTRAINED UNPREDICTABLE. 1913 */ 1914 } 1915 1916 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri, 1917 uint8_t counter) 1918 { 1919 if (counter < pmu_num_counters(env)) { 1920 uint64_t ret; 1921 pmevcntr_op_start(env, counter); 1922 ret = env->cp15.c14_pmevcntr[counter]; 1923 pmevcntr_op_finish(env, counter); 1924 return ret; 1925 } else { 1926 /* We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1927 * are CONSTRAINED UNPREDICTABLE. */ 1928 return 0; 1929 } 1930 } 1931 1932 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1933 uint64_t value) 1934 { 1935 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1936 pmevcntr_write(env, ri, value, counter); 1937 } 1938 1939 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1940 { 1941 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1942 return pmevcntr_read(env, ri, counter); 1943 } 1944 1945 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1946 uint64_t value) 1947 { 1948 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1949 assert(counter < pmu_num_counters(env)); 1950 env->cp15.c14_pmevcntr[counter] = value; 1951 pmevcntr_write(env, ri, value, counter); 1952 } 1953 1954 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri) 1955 { 1956 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1957 assert(counter < pmu_num_counters(env)); 1958 return env->cp15.c14_pmevcntr[counter]; 1959 } 1960 1961 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1962 uint64_t value) 1963 { 1964 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31); 1965 } 1966 1967 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1968 { 1969 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31); 1970 } 1971 1972 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1973 uint64_t value) 1974 { 1975 if (arm_feature(env, ARM_FEATURE_V8)) { 1976 env->cp15.c9_pmuserenr = value & 0xf; 1977 } else { 1978 env->cp15.c9_pmuserenr = value & 1; 1979 } 1980 } 1981 1982 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1983 uint64_t value) 1984 { 1985 /* We have no event counters so only the C bit can be changed */ 1986 value &= pmu_counter_mask(env); 1987 env->cp15.c9_pminten |= value; 1988 pmu_update_irq(env); 1989 } 1990 1991 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1992 uint64_t value) 1993 { 1994 value &= pmu_counter_mask(env); 1995 env->cp15.c9_pminten &= ~value; 1996 pmu_update_irq(env); 1997 } 1998 1999 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 2000 uint64_t value) 2001 { 2002 /* Note that even though the AArch64 view of this register has bits 2003 * [10:0] all RES0 we can only mask the bottom 5, to comply with the 2004 * architectural requirements for bits which are RES0 only in some 2005 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 2006 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) 2007 */ 2008 raw_write(env, ri, value & ~0x1FULL); 2009 } 2010 2011 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 2012 { 2013 /* Begin with base v8.0 state. */ 2014 uint32_t valid_mask = 0x3fff; 2015 ARMCPU *cpu = env_archcpu(env); 2016 2017 if (ri->state == ARM_CP_STATE_AA64) { 2018 value |= SCR_FW | SCR_AW; /* these two bits are RES1. */ 2019 valid_mask &= ~SCR_NET; 2020 2021 if (cpu_isar_feature(aa64_lor, cpu)) { 2022 valid_mask |= SCR_TLOR; 2023 } 2024 if (cpu_isar_feature(aa64_pauth, cpu)) { 2025 valid_mask |= SCR_API | SCR_APK; 2026 } 2027 if (cpu_isar_feature(aa64_mte, cpu)) { 2028 valid_mask |= SCR_ATA; 2029 } 2030 } else { 2031 valid_mask &= ~(SCR_RW | SCR_ST); 2032 } 2033 2034 if (!arm_feature(env, ARM_FEATURE_EL2)) { 2035 valid_mask &= ~SCR_HCE; 2036 2037 /* On ARMv7, SMD (or SCD as it is called in v7) is only 2038 * supported if EL2 exists. The bit is UNK/SBZP when 2039 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero 2040 * when EL2 is unavailable. 2041 * On ARMv8, this bit is always available. 2042 */ 2043 if (arm_feature(env, ARM_FEATURE_V7) && 2044 !arm_feature(env, ARM_FEATURE_V8)) { 2045 valid_mask &= ~SCR_SMD; 2046 } 2047 } 2048 2049 /* Clear all-context RES0 bits. */ 2050 value &= valid_mask; 2051 raw_write(env, ri, value); 2052 } 2053 2054 static CPAccessResult access_aa64_tid2(CPUARMState *env, 2055 const ARMCPRegInfo *ri, 2056 bool isread) 2057 { 2058 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID2)) { 2059 return CP_ACCESS_TRAP_EL2; 2060 } 2061 2062 return CP_ACCESS_OK; 2063 } 2064 2065 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2066 { 2067 ARMCPU *cpu = env_archcpu(env); 2068 2069 /* Acquire the CSSELR index from the bank corresponding to the CCSIDR 2070 * bank 2071 */ 2072 uint32_t index = A32_BANKED_REG_GET(env, csselr, 2073 ri->secure & ARM_CP_SECSTATE_S); 2074 2075 return cpu->ccsidr[index]; 2076 } 2077 2078 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2079 uint64_t value) 2080 { 2081 raw_write(env, ri, value & 0xf); 2082 } 2083 2084 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) 2085 { 2086 CPUState *cs = env_cpu(env); 2087 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 2088 uint64_t ret = 0; 2089 bool allow_virt = (arm_current_el(env) == 1 && 2090 (!arm_is_secure_below_el3(env) || 2091 (env->cp15.scr_el3 & SCR_EEL2))); 2092 2093 if (allow_virt && (hcr_el2 & HCR_IMO)) { 2094 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) { 2095 ret |= CPSR_I; 2096 } 2097 } else { 2098 if (cs->interrupt_request & CPU_INTERRUPT_HARD) { 2099 ret |= CPSR_I; 2100 } 2101 } 2102 2103 if (allow_virt && (hcr_el2 & HCR_FMO)) { 2104 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) { 2105 ret |= CPSR_F; 2106 } 2107 } else { 2108 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { 2109 ret |= CPSR_F; 2110 } 2111 } 2112 2113 /* External aborts are not possible in QEMU so A bit is always clear */ 2114 return ret; 2115 } 2116 2117 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri, 2118 bool isread) 2119 { 2120 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) { 2121 return CP_ACCESS_TRAP_EL2; 2122 } 2123 2124 return CP_ACCESS_OK; 2125 } 2126 2127 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri, 2128 bool isread) 2129 { 2130 if (arm_feature(env, ARM_FEATURE_V8)) { 2131 return access_aa64_tid1(env, ri, isread); 2132 } 2133 2134 return CP_ACCESS_OK; 2135 } 2136 2137 static const ARMCPRegInfo v7_cp_reginfo[] = { 2138 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ 2139 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 2140 .access = PL1_W, .type = ARM_CP_NOP }, 2141 /* Performance monitors are implementation defined in v7, 2142 * but with an ARM recommended set of registers, which we 2143 * follow. 2144 * 2145 * Performance registers fall into three categories: 2146 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) 2147 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) 2148 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) 2149 * For the cases controlled by PMUSERENR we must set .access to PL0_RW 2150 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. 2151 */ 2152 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, 2153 .access = PL0_RW, .type = ARM_CP_ALIAS, 2154 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 2155 .writefn = pmcntenset_write, 2156 .accessfn = pmreg_access, 2157 .raw_writefn = raw_write }, 2158 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, 2159 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, 2160 .access = PL0_RW, .accessfn = pmreg_access, 2161 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, 2162 .writefn = pmcntenset_write, .raw_writefn = raw_write }, 2163 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, 2164 .access = PL0_RW, 2165 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 2166 .accessfn = pmreg_access, 2167 .writefn = pmcntenclr_write, 2168 .type = ARM_CP_ALIAS }, 2169 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, 2170 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, 2171 .access = PL0_RW, .accessfn = pmreg_access, 2172 .type = ARM_CP_ALIAS, 2173 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), 2174 .writefn = pmcntenclr_write }, 2175 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, 2176 .access = PL0_RW, .type = ARM_CP_IO, 2177 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 2178 .accessfn = pmreg_access, 2179 .writefn = pmovsr_write, 2180 .raw_writefn = raw_write }, 2181 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64, 2182 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3, 2183 .access = PL0_RW, .accessfn = pmreg_access, 2184 .type = ARM_CP_ALIAS | ARM_CP_IO, 2185 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 2186 .writefn = pmovsr_write, 2187 .raw_writefn = raw_write }, 2188 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, 2189 .access = PL0_W, .accessfn = pmreg_access_swinc, 2190 .type = ARM_CP_NO_RAW | ARM_CP_IO, 2191 .writefn = pmswinc_write }, 2192 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64, 2193 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4, 2194 .access = PL0_W, .accessfn = pmreg_access_swinc, 2195 .type = ARM_CP_NO_RAW | ARM_CP_IO, 2196 .writefn = pmswinc_write }, 2197 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, 2198 .access = PL0_RW, .type = ARM_CP_ALIAS, 2199 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr), 2200 .accessfn = pmreg_access_selr, .writefn = pmselr_write, 2201 .raw_writefn = raw_write}, 2202 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64, 2203 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5, 2204 .access = PL0_RW, .accessfn = pmreg_access_selr, 2205 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr), 2206 .writefn = pmselr_write, .raw_writefn = raw_write, }, 2207 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, 2208 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO, 2209 .readfn = pmccntr_read, .writefn = pmccntr_write32, 2210 .accessfn = pmreg_access_ccntr }, 2211 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, 2212 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, 2213 .access = PL0_RW, .accessfn = pmreg_access_ccntr, 2214 .type = ARM_CP_IO, 2215 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt), 2216 .readfn = pmccntr_read, .writefn = pmccntr_write, 2217 .raw_readfn = raw_read, .raw_writefn = raw_write, }, 2218 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7, 2219 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32, 2220 .access = PL0_RW, .accessfn = pmreg_access, 2221 .type = ARM_CP_ALIAS | ARM_CP_IO, 2222 .resetvalue = 0, }, 2223 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, 2224 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, 2225 .writefn = pmccfiltr_write, .raw_writefn = raw_write, 2226 .access = PL0_RW, .accessfn = pmreg_access, 2227 .type = ARM_CP_IO, 2228 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), 2229 .resetvalue = 0, }, 2230 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, 2231 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2232 .accessfn = pmreg_access, 2233 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 2234 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64, 2235 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1, 2236 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2237 .accessfn = pmreg_access, 2238 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 2239 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, 2240 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2241 .accessfn = pmreg_access_xevcntr, 2242 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2243 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64, 2244 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2, 2245 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 2246 .accessfn = pmreg_access_xevcntr, 2247 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2248 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, 2249 .access = PL0_R | PL1_RW, .accessfn = access_tpm, 2250 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr), 2251 .resetvalue = 0, 2252 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2253 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64, 2254 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0, 2255 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 2256 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), 2257 .resetvalue = 0, 2258 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2259 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, 2260 .access = PL1_RW, .accessfn = access_tpm, 2261 .type = ARM_CP_ALIAS | ARM_CP_IO, 2262 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten), 2263 .resetvalue = 0, 2264 .writefn = pmintenset_write, .raw_writefn = raw_write }, 2265 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64, 2266 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1, 2267 .access = PL1_RW, .accessfn = access_tpm, 2268 .type = ARM_CP_IO, 2269 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2270 .writefn = pmintenset_write, .raw_writefn = raw_write, 2271 .resetvalue = 0x0 }, 2272 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, 2273 .access = PL1_RW, .accessfn = access_tpm, 2274 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW, 2275 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2276 .writefn = pmintenclr_write, }, 2277 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64, 2278 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2, 2279 .access = PL1_RW, .accessfn = access_tpm, 2280 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW, 2281 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2282 .writefn = pmintenclr_write }, 2283 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, 2284 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, 2285 .access = PL1_R, 2286 .accessfn = access_aa64_tid2, 2287 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW }, 2288 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, 2289 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, 2290 .access = PL1_RW, 2291 .accessfn = access_aa64_tid2, 2292 .writefn = csselr_write, .resetvalue = 0, 2293 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), 2294 offsetof(CPUARMState, cp15.csselr_ns) } }, 2295 /* Auxiliary ID register: this actually has an IMPDEF value but for now 2296 * just RAZ for all cores: 2297 */ 2298 { .name = "AIDR", .state = ARM_CP_STATE_BOTH, 2299 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, 2300 .access = PL1_R, .type = ARM_CP_CONST, 2301 .accessfn = access_aa64_tid1, 2302 .resetvalue = 0 }, 2303 /* Auxiliary fault status registers: these also are IMPDEF, and we 2304 * choose to RAZ/WI for all cores. 2305 */ 2306 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, 2307 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, 2308 .access = PL1_RW, .accessfn = access_tvm_trvm, 2309 .type = ARM_CP_CONST, .resetvalue = 0 }, 2310 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, 2311 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, 2312 .access = PL1_RW, .accessfn = access_tvm_trvm, 2313 .type = ARM_CP_CONST, .resetvalue = 0 }, 2314 /* MAIR can just read-as-written because we don't implement caches 2315 * and so don't need to care about memory attributes. 2316 */ 2317 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, 2318 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 2319 .access = PL1_RW, .accessfn = access_tvm_trvm, 2320 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]), 2321 .resetvalue = 0 }, 2322 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64, 2323 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0, 2324 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]), 2325 .resetvalue = 0 }, 2326 /* For non-long-descriptor page tables these are PRRR and NMRR; 2327 * regardless they still act as reads-as-written for QEMU. 2328 */ 2329 /* MAIR0/1 are defined separately from their 64-bit counterpart which 2330 * allows them to assign the correct fieldoffset based on the endianness 2331 * handled in the field definitions. 2332 */ 2333 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, 2334 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 2335 .access = PL1_RW, .accessfn = access_tvm_trvm, 2336 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s), 2337 offsetof(CPUARMState, cp15.mair0_ns) }, 2338 .resetfn = arm_cp_reset_ignore }, 2339 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, 2340 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, 2341 .access = PL1_RW, .accessfn = access_tvm_trvm, 2342 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s), 2343 offsetof(CPUARMState, cp15.mair1_ns) }, 2344 .resetfn = arm_cp_reset_ignore }, 2345 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, 2346 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, 2347 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read }, 2348 /* 32 bit ITLB invalidates */ 2349 { .name = "ITLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 0, 2350 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2351 .writefn = tlbiall_write }, 2352 { .name = "ITLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 1, 2353 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2354 .writefn = tlbimva_write }, 2355 { .name = "ITLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 5, .opc2 = 2, 2356 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2357 .writefn = tlbiasid_write }, 2358 /* 32 bit DTLB invalidates */ 2359 { .name = "DTLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 0, 2360 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2361 .writefn = tlbiall_write }, 2362 { .name = "DTLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 1, 2363 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2364 .writefn = tlbimva_write }, 2365 { .name = "DTLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 6, .opc2 = 2, 2366 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2367 .writefn = tlbiasid_write }, 2368 /* 32 bit TLB invalidates */ 2369 { .name = "TLBIALL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 2370 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2371 .writefn = tlbiall_write }, 2372 { .name = "TLBIMVA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 2373 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2374 .writefn = tlbimva_write }, 2375 { .name = "TLBIASID", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 2376 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2377 .writefn = tlbiasid_write }, 2378 { .name = "TLBIMVAA", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 2379 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2380 .writefn = tlbimvaa_write }, 2381 REGINFO_SENTINEL 2382 }; 2383 2384 static const ARMCPRegInfo v7mp_cp_reginfo[] = { 2385 /* 32 bit TLB invalidates, Inner Shareable */ 2386 { .name = "TLBIALLIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 2387 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2388 .writefn = tlbiall_is_write }, 2389 { .name = "TLBIMVAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 2390 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2391 .writefn = tlbimva_is_write }, 2392 { .name = "TLBIASIDIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 2393 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2394 .writefn = tlbiasid_is_write }, 2395 { .name = "TLBIMVAAIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 2396 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 2397 .writefn = tlbimvaa_is_write }, 2398 REGINFO_SENTINEL 2399 }; 2400 2401 static const ARMCPRegInfo pmovsset_cp_reginfo[] = { 2402 /* PMOVSSET is not implemented in v7 before v7ve */ 2403 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3, 2404 .access = PL0_RW, .accessfn = pmreg_access, 2405 .type = ARM_CP_ALIAS | ARM_CP_IO, 2406 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 2407 .writefn = pmovsset_write, 2408 .raw_writefn = raw_write }, 2409 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64, 2410 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3, 2411 .access = PL0_RW, .accessfn = pmreg_access, 2412 .type = ARM_CP_ALIAS | ARM_CP_IO, 2413 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 2414 .writefn = pmovsset_write, 2415 .raw_writefn = raw_write }, 2416 REGINFO_SENTINEL 2417 }; 2418 2419 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2420 uint64_t value) 2421 { 2422 value &= 1; 2423 env->teecr = value; 2424 } 2425 2426 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri, 2427 bool isread) 2428 { 2429 if (arm_current_el(env) == 0 && (env->teecr & 1)) { 2430 return CP_ACCESS_TRAP; 2431 } 2432 return CP_ACCESS_OK; 2433 } 2434 2435 static const ARMCPRegInfo t2ee_cp_reginfo[] = { 2436 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, 2437 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), 2438 .resetvalue = 0, 2439 .writefn = teecr_write }, 2440 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, 2441 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), 2442 .accessfn = teehbr_access, .resetvalue = 0 }, 2443 REGINFO_SENTINEL 2444 }; 2445 2446 static const ARMCPRegInfo v6k_cp_reginfo[] = { 2447 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, 2448 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, 2449 .access = PL0_RW, 2450 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 }, 2451 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, 2452 .access = PL0_RW, 2453 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s), 2454 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, 2455 .resetfn = arm_cp_reset_ignore }, 2456 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, 2457 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, 2458 .access = PL0_R|PL1_W, 2459 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]), 2460 .resetvalue = 0}, 2461 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, 2462 .access = PL0_R|PL1_W, 2463 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s), 2464 offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, 2465 .resetfn = arm_cp_reset_ignore }, 2466 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64, 2467 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, 2468 .access = PL1_RW, 2469 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 }, 2470 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4, 2471 .access = PL1_RW, 2472 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s), 2473 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) }, 2474 .resetvalue = 0 }, 2475 REGINFO_SENTINEL 2476 }; 2477 2478 #ifndef CONFIG_USER_ONLY 2479 2480 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, 2481 bool isread) 2482 { 2483 /* CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero. 2484 * Writable only at the highest implemented exception level. 2485 */ 2486 int el = arm_current_el(env); 2487 uint64_t hcr; 2488 uint32_t cntkctl; 2489 2490 switch (el) { 2491 case 0: 2492 hcr = arm_hcr_el2_eff(env); 2493 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2494 cntkctl = env->cp15.cnthctl_el2; 2495 } else { 2496 cntkctl = env->cp15.c14_cntkctl; 2497 } 2498 if (!extract32(cntkctl, 0, 2)) { 2499 return CP_ACCESS_TRAP; 2500 } 2501 break; 2502 case 1: 2503 if (!isread && ri->state == ARM_CP_STATE_AA32 && 2504 arm_is_secure_below_el3(env)) { 2505 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */ 2506 return CP_ACCESS_TRAP_UNCATEGORIZED; 2507 } 2508 break; 2509 case 2: 2510 case 3: 2511 break; 2512 } 2513 2514 if (!isread && el < arm_highest_el(env)) { 2515 return CP_ACCESS_TRAP_UNCATEGORIZED; 2516 } 2517 2518 return CP_ACCESS_OK; 2519 } 2520 2521 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx, 2522 bool isread) 2523 { 2524 unsigned int cur_el = arm_current_el(env); 2525 bool secure = arm_is_secure(env); 2526 uint64_t hcr = arm_hcr_el2_eff(env); 2527 2528 switch (cur_el) { 2529 case 0: 2530 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */ 2531 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2532 return (extract32(env->cp15.cnthctl_el2, timeridx, 1) 2533 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2); 2534 } 2535 2536 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */ 2537 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) { 2538 return CP_ACCESS_TRAP; 2539 } 2540 2541 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PCTEN. */ 2542 if (hcr & HCR_E2H) { 2543 if (timeridx == GTIMER_PHYS && 2544 !extract32(env->cp15.cnthctl_el2, 10, 1)) { 2545 return CP_ACCESS_TRAP_EL2; 2546 } 2547 } else { 2548 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */ 2549 if (arm_feature(env, ARM_FEATURE_EL2) && 2550 timeridx == GTIMER_PHYS && !secure && 2551 !extract32(env->cp15.cnthctl_el2, 1, 1)) { 2552 return CP_ACCESS_TRAP_EL2; 2553 } 2554 } 2555 break; 2556 2557 case 1: 2558 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */ 2559 if (arm_feature(env, ARM_FEATURE_EL2) && 2560 timeridx == GTIMER_PHYS && !secure && 2561 (hcr & HCR_E2H 2562 ? !extract32(env->cp15.cnthctl_el2, 10, 1) 2563 : !extract32(env->cp15.cnthctl_el2, 0, 1))) { 2564 return CP_ACCESS_TRAP_EL2; 2565 } 2566 break; 2567 } 2568 return CP_ACCESS_OK; 2569 } 2570 2571 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx, 2572 bool isread) 2573 { 2574 unsigned int cur_el = arm_current_el(env); 2575 bool secure = arm_is_secure(env); 2576 uint64_t hcr = arm_hcr_el2_eff(env); 2577 2578 switch (cur_el) { 2579 case 0: 2580 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2581 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */ 2582 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1) 2583 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2); 2584 } 2585 2586 /* 2587 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from 2588 * EL0 if EL0[PV]TEN is zero. 2589 */ 2590 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { 2591 return CP_ACCESS_TRAP; 2592 } 2593 /* fall through */ 2594 2595 case 1: 2596 if (arm_feature(env, ARM_FEATURE_EL2) && 2597 timeridx == GTIMER_PHYS && !secure) { 2598 if (hcr & HCR_E2H) { 2599 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */ 2600 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) { 2601 return CP_ACCESS_TRAP_EL2; 2602 } 2603 } else { 2604 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */ 2605 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) { 2606 return CP_ACCESS_TRAP_EL2; 2607 } 2608 } 2609 } 2610 break; 2611 } 2612 return CP_ACCESS_OK; 2613 } 2614 2615 static CPAccessResult gt_pct_access(CPUARMState *env, 2616 const ARMCPRegInfo *ri, 2617 bool isread) 2618 { 2619 return gt_counter_access(env, GTIMER_PHYS, isread); 2620 } 2621 2622 static CPAccessResult gt_vct_access(CPUARMState *env, 2623 const ARMCPRegInfo *ri, 2624 bool isread) 2625 { 2626 return gt_counter_access(env, GTIMER_VIRT, isread); 2627 } 2628 2629 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2630 bool isread) 2631 { 2632 return gt_timer_access(env, GTIMER_PHYS, isread); 2633 } 2634 2635 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2636 bool isread) 2637 { 2638 return gt_timer_access(env, GTIMER_VIRT, isread); 2639 } 2640 2641 static CPAccessResult gt_stimer_access(CPUARMState *env, 2642 const ARMCPRegInfo *ri, 2643 bool isread) 2644 { 2645 /* The AArch64 register view of the secure physical timer is 2646 * always accessible from EL3, and configurably accessible from 2647 * Secure EL1. 2648 */ 2649 switch (arm_current_el(env)) { 2650 case 1: 2651 if (!arm_is_secure(env)) { 2652 return CP_ACCESS_TRAP; 2653 } 2654 if (!(env->cp15.scr_el3 & SCR_ST)) { 2655 return CP_ACCESS_TRAP_EL3; 2656 } 2657 return CP_ACCESS_OK; 2658 case 0: 2659 case 2: 2660 return CP_ACCESS_TRAP; 2661 case 3: 2662 return CP_ACCESS_OK; 2663 default: 2664 g_assert_not_reached(); 2665 } 2666 } 2667 2668 static uint64_t gt_get_countervalue(CPUARMState *env) 2669 { 2670 ARMCPU *cpu = env_archcpu(env); 2671 2672 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu); 2673 } 2674 2675 static void gt_recalc_timer(ARMCPU *cpu, int timeridx) 2676 { 2677 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; 2678 2679 if (gt->ctl & 1) { 2680 /* Timer enabled: calculate and set current ISTATUS, irq, and 2681 * reset timer to when ISTATUS next has to change 2682 */ 2683 uint64_t offset = timeridx == GTIMER_VIRT ? 2684 cpu->env.cp15.cntvoff_el2 : 0; 2685 uint64_t count = gt_get_countervalue(&cpu->env); 2686 /* Note that this must be unsigned 64 bit arithmetic: */ 2687 int istatus = count - offset >= gt->cval; 2688 uint64_t nexttick; 2689 int irqstate; 2690 2691 gt->ctl = deposit32(gt->ctl, 2, 1, istatus); 2692 2693 irqstate = (istatus && !(gt->ctl & 2)); 2694 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 2695 2696 if (istatus) { 2697 /* Next transition is when count rolls back over to zero */ 2698 nexttick = UINT64_MAX; 2699 } else { 2700 /* Next transition is when we hit cval */ 2701 nexttick = gt->cval + offset; 2702 } 2703 /* Note that the desired next expiry time might be beyond the 2704 * signed-64-bit range of a QEMUTimer -- in this case we just 2705 * set the timer for as far in the future as possible. When the 2706 * timer expires we will reset the timer for any remaining period. 2707 */ 2708 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) { 2709 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX); 2710 } else { 2711 timer_mod(cpu->gt_timer[timeridx], nexttick); 2712 } 2713 trace_arm_gt_recalc(timeridx, irqstate, nexttick); 2714 } else { 2715 /* Timer disabled: ISTATUS and timer output always clear */ 2716 gt->ctl &= ~4; 2717 qemu_set_irq(cpu->gt_timer_outputs[timeridx], 0); 2718 timer_del(cpu->gt_timer[timeridx]); 2719 trace_arm_gt_recalc_disabled(timeridx); 2720 } 2721 } 2722 2723 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri, 2724 int timeridx) 2725 { 2726 ARMCPU *cpu = env_archcpu(env); 2727 2728 timer_del(cpu->gt_timer[timeridx]); 2729 } 2730 2731 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2732 { 2733 return gt_get_countervalue(env); 2734 } 2735 2736 static uint64_t gt_virt_cnt_offset(CPUARMState *env) 2737 { 2738 uint64_t hcr; 2739 2740 switch (arm_current_el(env)) { 2741 case 2: 2742 hcr = arm_hcr_el2_eff(env); 2743 if (hcr & HCR_E2H) { 2744 return 0; 2745 } 2746 break; 2747 case 0: 2748 hcr = arm_hcr_el2_eff(env); 2749 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2750 return 0; 2751 } 2752 break; 2753 } 2754 2755 return env->cp15.cntvoff_el2; 2756 } 2757 2758 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2759 { 2760 return gt_get_countervalue(env) - gt_virt_cnt_offset(env); 2761 } 2762 2763 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2764 int timeridx, 2765 uint64_t value) 2766 { 2767 trace_arm_gt_cval_write(timeridx, value); 2768 env->cp15.c14_timer[timeridx].cval = value; 2769 gt_recalc_timer(env_archcpu(env), timeridx); 2770 } 2771 2772 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, 2773 int timeridx) 2774 { 2775 uint64_t offset = 0; 2776 2777 switch (timeridx) { 2778 case GTIMER_VIRT: 2779 case GTIMER_HYPVIRT: 2780 offset = gt_virt_cnt_offset(env); 2781 break; 2782 } 2783 2784 return (uint32_t)(env->cp15.c14_timer[timeridx].cval - 2785 (gt_get_countervalue(env) - offset)); 2786 } 2787 2788 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2789 int timeridx, 2790 uint64_t value) 2791 { 2792 uint64_t offset = 0; 2793 2794 switch (timeridx) { 2795 case GTIMER_VIRT: 2796 case GTIMER_HYPVIRT: 2797 offset = gt_virt_cnt_offset(env); 2798 break; 2799 } 2800 2801 trace_arm_gt_tval_write(timeridx, value); 2802 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset + 2803 sextract64(value, 0, 32); 2804 gt_recalc_timer(env_archcpu(env), timeridx); 2805 } 2806 2807 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2808 int timeridx, 2809 uint64_t value) 2810 { 2811 ARMCPU *cpu = env_archcpu(env); 2812 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; 2813 2814 trace_arm_gt_ctl_write(timeridx, value); 2815 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); 2816 if ((oldval ^ value) & 1) { 2817 /* Enable toggled */ 2818 gt_recalc_timer(cpu, timeridx); 2819 } else if ((oldval ^ value) & 2) { 2820 /* IMASK toggled: don't need to recalculate, 2821 * just set the interrupt line based on ISTATUS 2822 */ 2823 int irqstate = (oldval & 4) && !(value & 2); 2824 2825 trace_arm_gt_imask_toggle(timeridx, irqstate); 2826 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 2827 } 2828 } 2829 2830 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2831 { 2832 gt_timer_reset(env, ri, GTIMER_PHYS); 2833 } 2834 2835 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2836 uint64_t value) 2837 { 2838 gt_cval_write(env, ri, GTIMER_PHYS, value); 2839 } 2840 2841 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2842 { 2843 return gt_tval_read(env, ri, GTIMER_PHYS); 2844 } 2845 2846 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2847 uint64_t value) 2848 { 2849 gt_tval_write(env, ri, GTIMER_PHYS, value); 2850 } 2851 2852 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2853 uint64_t value) 2854 { 2855 gt_ctl_write(env, ri, GTIMER_PHYS, value); 2856 } 2857 2858 static int gt_phys_redir_timeridx(CPUARMState *env) 2859 { 2860 switch (arm_mmu_idx(env)) { 2861 case ARMMMUIdx_E20_0: 2862 case ARMMMUIdx_E20_2: 2863 case ARMMMUIdx_E20_2_PAN: 2864 return GTIMER_HYP; 2865 default: 2866 return GTIMER_PHYS; 2867 } 2868 } 2869 2870 static int gt_virt_redir_timeridx(CPUARMState *env) 2871 { 2872 switch (arm_mmu_idx(env)) { 2873 case ARMMMUIdx_E20_0: 2874 case ARMMMUIdx_E20_2: 2875 case ARMMMUIdx_E20_2_PAN: 2876 return GTIMER_HYPVIRT; 2877 default: 2878 return GTIMER_VIRT; 2879 } 2880 } 2881 2882 static uint64_t gt_phys_redir_cval_read(CPUARMState *env, 2883 const ARMCPRegInfo *ri) 2884 { 2885 int timeridx = gt_phys_redir_timeridx(env); 2886 return env->cp15.c14_timer[timeridx].cval; 2887 } 2888 2889 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2890 uint64_t value) 2891 { 2892 int timeridx = gt_phys_redir_timeridx(env); 2893 gt_cval_write(env, ri, timeridx, value); 2894 } 2895 2896 static uint64_t gt_phys_redir_tval_read(CPUARMState *env, 2897 const ARMCPRegInfo *ri) 2898 { 2899 int timeridx = gt_phys_redir_timeridx(env); 2900 return gt_tval_read(env, ri, timeridx); 2901 } 2902 2903 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2904 uint64_t value) 2905 { 2906 int timeridx = gt_phys_redir_timeridx(env); 2907 gt_tval_write(env, ri, timeridx, value); 2908 } 2909 2910 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env, 2911 const ARMCPRegInfo *ri) 2912 { 2913 int timeridx = gt_phys_redir_timeridx(env); 2914 return env->cp15.c14_timer[timeridx].ctl; 2915 } 2916 2917 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2918 uint64_t value) 2919 { 2920 int timeridx = gt_phys_redir_timeridx(env); 2921 gt_ctl_write(env, ri, timeridx, value); 2922 } 2923 2924 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2925 { 2926 gt_timer_reset(env, ri, GTIMER_VIRT); 2927 } 2928 2929 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2930 uint64_t value) 2931 { 2932 gt_cval_write(env, ri, GTIMER_VIRT, value); 2933 } 2934 2935 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2936 { 2937 return gt_tval_read(env, ri, GTIMER_VIRT); 2938 } 2939 2940 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2941 uint64_t value) 2942 { 2943 gt_tval_write(env, ri, GTIMER_VIRT, value); 2944 } 2945 2946 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2947 uint64_t value) 2948 { 2949 gt_ctl_write(env, ri, GTIMER_VIRT, value); 2950 } 2951 2952 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri, 2953 uint64_t value) 2954 { 2955 ARMCPU *cpu = env_archcpu(env); 2956 2957 trace_arm_gt_cntvoff_write(value); 2958 raw_write(env, ri, value); 2959 gt_recalc_timer(cpu, GTIMER_VIRT); 2960 } 2961 2962 static uint64_t gt_virt_redir_cval_read(CPUARMState *env, 2963 const ARMCPRegInfo *ri) 2964 { 2965 int timeridx = gt_virt_redir_timeridx(env); 2966 return env->cp15.c14_timer[timeridx].cval; 2967 } 2968 2969 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2970 uint64_t value) 2971 { 2972 int timeridx = gt_virt_redir_timeridx(env); 2973 gt_cval_write(env, ri, timeridx, value); 2974 } 2975 2976 static uint64_t gt_virt_redir_tval_read(CPUARMState *env, 2977 const ARMCPRegInfo *ri) 2978 { 2979 int timeridx = gt_virt_redir_timeridx(env); 2980 return gt_tval_read(env, ri, timeridx); 2981 } 2982 2983 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2984 uint64_t value) 2985 { 2986 int timeridx = gt_virt_redir_timeridx(env); 2987 gt_tval_write(env, ri, timeridx, value); 2988 } 2989 2990 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env, 2991 const ARMCPRegInfo *ri) 2992 { 2993 int timeridx = gt_virt_redir_timeridx(env); 2994 return env->cp15.c14_timer[timeridx].ctl; 2995 } 2996 2997 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2998 uint64_t value) 2999 { 3000 int timeridx = gt_virt_redir_timeridx(env); 3001 gt_ctl_write(env, ri, timeridx, value); 3002 } 3003 3004 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3005 { 3006 gt_timer_reset(env, ri, GTIMER_HYP); 3007 } 3008 3009 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3010 uint64_t value) 3011 { 3012 gt_cval_write(env, ri, GTIMER_HYP, value); 3013 } 3014 3015 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3016 { 3017 return gt_tval_read(env, ri, GTIMER_HYP); 3018 } 3019 3020 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3021 uint64_t value) 3022 { 3023 gt_tval_write(env, ri, GTIMER_HYP, value); 3024 } 3025 3026 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3027 uint64_t value) 3028 { 3029 gt_ctl_write(env, ri, GTIMER_HYP, value); 3030 } 3031 3032 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3033 { 3034 gt_timer_reset(env, ri, GTIMER_SEC); 3035 } 3036 3037 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3038 uint64_t value) 3039 { 3040 gt_cval_write(env, ri, GTIMER_SEC, value); 3041 } 3042 3043 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3044 { 3045 return gt_tval_read(env, ri, GTIMER_SEC); 3046 } 3047 3048 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3049 uint64_t value) 3050 { 3051 gt_tval_write(env, ri, GTIMER_SEC, value); 3052 } 3053 3054 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3055 uint64_t value) 3056 { 3057 gt_ctl_write(env, ri, GTIMER_SEC, value); 3058 } 3059 3060 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3061 { 3062 gt_timer_reset(env, ri, GTIMER_HYPVIRT); 3063 } 3064 3065 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3066 uint64_t value) 3067 { 3068 gt_cval_write(env, ri, GTIMER_HYPVIRT, value); 3069 } 3070 3071 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3072 { 3073 return gt_tval_read(env, ri, GTIMER_HYPVIRT); 3074 } 3075 3076 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3077 uint64_t value) 3078 { 3079 gt_tval_write(env, ri, GTIMER_HYPVIRT, value); 3080 } 3081 3082 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3083 uint64_t value) 3084 { 3085 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value); 3086 } 3087 3088 void arm_gt_ptimer_cb(void *opaque) 3089 { 3090 ARMCPU *cpu = opaque; 3091 3092 gt_recalc_timer(cpu, GTIMER_PHYS); 3093 } 3094 3095 void arm_gt_vtimer_cb(void *opaque) 3096 { 3097 ARMCPU *cpu = opaque; 3098 3099 gt_recalc_timer(cpu, GTIMER_VIRT); 3100 } 3101 3102 void arm_gt_htimer_cb(void *opaque) 3103 { 3104 ARMCPU *cpu = opaque; 3105 3106 gt_recalc_timer(cpu, GTIMER_HYP); 3107 } 3108 3109 void arm_gt_stimer_cb(void *opaque) 3110 { 3111 ARMCPU *cpu = opaque; 3112 3113 gt_recalc_timer(cpu, GTIMER_SEC); 3114 } 3115 3116 void arm_gt_hvtimer_cb(void *opaque) 3117 { 3118 ARMCPU *cpu = opaque; 3119 3120 gt_recalc_timer(cpu, GTIMER_HYPVIRT); 3121 } 3122 3123 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque) 3124 { 3125 ARMCPU *cpu = env_archcpu(env); 3126 3127 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz; 3128 } 3129 3130 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 3131 /* Note that CNTFRQ is purely reads-as-written for the benefit 3132 * of software; writing it doesn't actually change the timer frequency. 3133 * Our reset value matches the fixed frequency we implement the timer at. 3134 */ 3135 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, 3136 .type = ARM_CP_ALIAS, 3137 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 3138 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), 3139 }, 3140 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 3141 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 3142 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 3143 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 3144 .resetfn = arm_gt_cntfrq_reset, 3145 }, 3146 /* overall control: mostly access permissions */ 3147 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, 3148 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, 3149 .access = PL1_RW, 3150 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), 3151 .resetvalue = 0, 3152 }, 3153 /* per-timer control */ 3154 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 3155 .secure = ARM_CP_SECSTATE_NS, 3156 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3157 .accessfn = gt_ptimer_access, 3158 .fieldoffset = offsetoflow32(CPUARMState, 3159 cp15.c14_timer[GTIMER_PHYS].ctl), 3160 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read, 3161 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write, 3162 }, 3163 { .name = "CNTP_CTL_S", 3164 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 3165 .secure = ARM_CP_SECSTATE_S, 3166 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3167 .accessfn = gt_ptimer_access, 3168 .fieldoffset = offsetoflow32(CPUARMState, 3169 cp15.c14_timer[GTIMER_SEC].ctl), 3170 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 3171 }, 3172 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, 3173 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, 3174 .type = ARM_CP_IO, .access = PL0_RW, 3175 .accessfn = gt_ptimer_access, 3176 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 3177 .resetvalue = 0, 3178 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read, 3179 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write, 3180 }, 3181 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, 3182 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3183 .accessfn = gt_vtimer_access, 3184 .fieldoffset = offsetoflow32(CPUARMState, 3185 cp15.c14_timer[GTIMER_VIRT].ctl), 3186 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read, 3187 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write, 3188 }, 3189 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, 3190 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, 3191 .type = ARM_CP_IO, .access = PL0_RW, 3192 .accessfn = gt_vtimer_access, 3193 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 3194 .resetvalue = 0, 3195 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read, 3196 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write, 3197 }, 3198 /* TimerValue views: a 32 bit downcounting view of the underlying state */ 3199 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 3200 .secure = ARM_CP_SECSTATE_NS, 3201 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3202 .accessfn = gt_ptimer_access, 3203 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write, 3204 }, 3205 { .name = "CNTP_TVAL_S", 3206 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 3207 .secure = ARM_CP_SECSTATE_S, 3208 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3209 .accessfn = gt_ptimer_access, 3210 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write, 3211 }, 3212 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, 3213 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, 3214 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3215 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset, 3216 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write, 3217 }, 3218 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, 3219 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3220 .accessfn = gt_vtimer_access, 3221 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write, 3222 }, 3223 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, 3224 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, 3225 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3226 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset, 3227 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write, 3228 }, 3229 /* The counter itself */ 3230 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, 3231 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 3232 .accessfn = gt_pct_access, 3233 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, 3234 }, 3235 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, 3236 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, 3237 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3238 .accessfn = gt_pct_access, .readfn = gt_cnt_read, 3239 }, 3240 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, 3241 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 3242 .accessfn = gt_vct_access, 3243 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, 3244 }, 3245 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 3246 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 3247 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3248 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, 3249 }, 3250 /* Comparison value, indicating when the timer goes off */ 3251 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, 3252 .secure = ARM_CP_SECSTATE_NS, 3253 .access = PL0_RW, 3254 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3255 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 3256 .accessfn = gt_ptimer_access, 3257 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read, 3258 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write, 3259 }, 3260 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2, 3261 .secure = ARM_CP_SECSTATE_S, 3262 .access = PL0_RW, 3263 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3264 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 3265 .accessfn = gt_ptimer_access, 3266 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 3267 }, 3268 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, 3269 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, 3270 .access = PL0_RW, 3271 .type = ARM_CP_IO, 3272 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 3273 .resetvalue = 0, .accessfn = gt_ptimer_access, 3274 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read, 3275 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write, 3276 }, 3277 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, 3278 .access = PL0_RW, 3279 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3280 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 3281 .accessfn = gt_vtimer_access, 3282 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read, 3283 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write, 3284 }, 3285 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, 3286 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, 3287 .access = PL0_RW, 3288 .type = ARM_CP_IO, 3289 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 3290 .resetvalue = 0, .accessfn = gt_vtimer_access, 3291 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read, 3292 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write, 3293 }, 3294 /* Secure timer -- this is actually restricted to only EL3 3295 * and configurably Secure-EL1 via the accessfn. 3296 */ 3297 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64, 3298 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0, 3299 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW, 3300 .accessfn = gt_stimer_access, 3301 .readfn = gt_sec_tval_read, 3302 .writefn = gt_sec_tval_write, 3303 .resetfn = gt_sec_timer_reset, 3304 }, 3305 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64, 3306 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1, 3307 .type = ARM_CP_IO, .access = PL1_RW, 3308 .accessfn = gt_stimer_access, 3309 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl), 3310 .resetvalue = 0, 3311 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 3312 }, 3313 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64, 3314 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2, 3315 .type = ARM_CP_IO, .access = PL1_RW, 3316 .accessfn = gt_stimer_access, 3317 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 3318 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 3319 }, 3320 REGINFO_SENTINEL 3321 }; 3322 3323 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri, 3324 bool isread) 3325 { 3326 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) { 3327 return CP_ACCESS_TRAP; 3328 } 3329 return CP_ACCESS_OK; 3330 } 3331 3332 #else 3333 3334 /* In user-mode most of the generic timer registers are inaccessible 3335 * however modern kernels (4.12+) allow access to cntvct_el0 3336 */ 3337 3338 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 3339 { 3340 ARMCPU *cpu = env_archcpu(env); 3341 3342 /* Currently we have no support for QEMUTimer in linux-user so we 3343 * can't call gt_get_countervalue(env), instead we directly 3344 * call the lower level functions. 3345 */ 3346 return cpu_get_clock() / gt_cntfrq_period_ns(cpu); 3347 } 3348 3349 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 3350 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 3351 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 3352 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */, 3353 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 3354 .resetvalue = NANOSECONDS_PER_SECOND / GTIMER_SCALE, 3355 }, 3356 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 3357 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 3358 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3359 .readfn = gt_virt_cnt_read, 3360 }, 3361 REGINFO_SENTINEL 3362 }; 3363 3364 #endif 3365 3366 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3367 { 3368 if (arm_feature(env, ARM_FEATURE_LPAE)) { 3369 raw_write(env, ri, value); 3370 } else if (arm_feature(env, ARM_FEATURE_V7)) { 3371 raw_write(env, ri, value & 0xfffff6ff); 3372 } else { 3373 raw_write(env, ri, value & 0xfffff1ff); 3374 } 3375 } 3376 3377 #ifndef CONFIG_USER_ONLY 3378 /* get_phys_addr() isn't present for user-mode-only targets */ 3379 3380 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri, 3381 bool isread) 3382 { 3383 if (ri->opc2 & 4) { 3384 /* The ATS12NSO* operations must trap to EL3 if executed in 3385 * Secure EL1 (which can only happen if EL3 is AArch64). 3386 * They are simply UNDEF if executed from NS EL1. 3387 * They function normally from EL2 or EL3. 3388 */ 3389 if (arm_current_el(env) == 1) { 3390 if (arm_is_secure_below_el3(env)) { 3391 return CP_ACCESS_TRAP_UNCATEGORIZED_EL3; 3392 } 3393 return CP_ACCESS_TRAP_UNCATEGORIZED; 3394 } 3395 } 3396 return CP_ACCESS_OK; 3397 } 3398 3399 #ifdef CONFIG_TCG 3400 static uint64_t do_ats_write(CPUARMState *env, uint64_t value, 3401 MMUAccessType access_type, ARMMMUIdx mmu_idx) 3402 { 3403 hwaddr phys_addr; 3404 target_ulong page_size; 3405 int prot; 3406 bool ret; 3407 uint64_t par64; 3408 bool format64 = false; 3409 MemTxAttrs attrs = {}; 3410 ARMMMUFaultInfo fi = {}; 3411 ARMCacheAttrs cacheattrs = {}; 3412 3413 ret = get_phys_addr(env, value, access_type, mmu_idx, &phys_addr, &attrs, 3414 &prot, &page_size, &fi, &cacheattrs); 3415 3416 if (ret) { 3417 /* 3418 * Some kinds of translation fault must cause exceptions rather 3419 * than being reported in the PAR. 3420 */ 3421 int current_el = arm_current_el(env); 3422 int target_el; 3423 uint32_t syn, fsr, fsc; 3424 bool take_exc = false; 3425 3426 if (fi.s1ptw && current_el == 1 && !arm_is_secure(env) 3427 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) { 3428 /* 3429 * Synchronous stage 2 fault on an access made as part of the 3430 * translation table walk for AT S1E0* or AT S1E1* insn 3431 * executed from NS EL1. If this is a synchronous external abort 3432 * and SCR_EL3.EA == 1, then we take a synchronous external abort 3433 * to EL3. Otherwise the fault is taken as an exception to EL2, 3434 * and HPFAR_EL2 holds the faulting IPA. 3435 */ 3436 if (fi.type == ARMFault_SyncExternalOnWalk && 3437 (env->cp15.scr_el3 & SCR_EA)) { 3438 target_el = 3; 3439 } else { 3440 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4; 3441 target_el = 2; 3442 } 3443 take_exc = true; 3444 } else if (fi.type == ARMFault_SyncExternalOnWalk) { 3445 /* 3446 * Synchronous external aborts during a translation table walk 3447 * are taken as Data Abort exceptions. 3448 */ 3449 if (fi.stage2) { 3450 if (current_el == 3) { 3451 target_el = 3; 3452 } else { 3453 target_el = 2; 3454 } 3455 } else { 3456 target_el = exception_target_el(env); 3457 } 3458 take_exc = true; 3459 } 3460 3461 if (take_exc) { 3462 /* Construct FSR and FSC using same logic as arm_deliver_fault() */ 3463 if (target_el == 2 || arm_el_is_aa64(env, target_el) || 3464 arm_s1_regime_using_lpae_format(env, mmu_idx)) { 3465 fsr = arm_fi_to_lfsc(&fi); 3466 fsc = extract32(fsr, 0, 6); 3467 } else { 3468 fsr = arm_fi_to_sfsc(&fi); 3469 fsc = 0x3f; 3470 } 3471 /* 3472 * Report exception with ESR indicating a fault due to a 3473 * translation table walk for a cache maintenance instruction. 3474 */ 3475 syn = syn_data_abort_no_iss(current_el == target_el, 0, 3476 fi.ea, 1, fi.s1ptw, 1, fsc); 3477 env->exception.vaddress = value; 3478 env->exception.fsr = fsr; 3479 raise_exception(env, EXCP_DATA_ABORT, syn, target_el); 3480 } 3481 } 3482 3483 if (is_a64(env)) { 3484 format64 = true; 3485 } else if (arm_feature(env, ARM_FEATURE_LPAE)) { 3486 /* 3487 * ATS1Cxx: 3488 * * TTBCR.EAE determines whether the result is returned using the 3489 * 32-bit or the 64-bit PAR format 3490 * * Instructions executed in Hyp mode always use the 64bit format 3491 * 3492 * ATS1S2NSOxx uses the 64bit format if any of the following is true: 3493 * * The Non-secure TTBCR.EAE bit is set to 1 3494 * * The implementation includes EL2, and the value of HCR.VM is 1 3495 * 3496 * (Note that HCR.DC makes HCR.VM behave as if it is 1.) 3497 * 3498 * ATS1Hx always uses the 64bit format. 3499 */ 3500 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx); 3501 3502 if (arm_feature(env, ARM_FEATURE_EL2)) { 3503 if (mmu_idx == ARMMMUIdx_E10_0 || 3504 mmu_idx == ARMMMUIdx_E10_1 || 3505 mmu_idx == ARMMMUIdx_E10_1_PAN) { 3506 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC); 3507 } else { 3508 format64 |= arm_current_el(env) == 2; 3509 } 3510 } 3511 } 3512 3513 if (format64) { 3514 /* Create a 64-bit PAR */ 3515 par64 = (1 << 11); /* LPAE bit always set */ 3516 if (!ret) { 3517 par64 |= phys_addr & ~0xfffULL; 3518 if (!attrs.secure) { 3519 par64 |= (1 << 9); /* NS */ 3520 } 3521 par64 |= (uint64_t)cacheattrs.attrs << 56; /* ATTR */ 3522 par64 |= cacheattrs.shareability << 7; /* SH */ 3523 } else { 3524 uint32_t fsr = arm_fi_to_lfsc(&fi); 3525 3526 par64 |= 1; /* F */ 3527 par64 |= (fsr & 0x3f) << 1; /* FS */ 3528 if (fi.stage2) { 3529 par64 |= (1 << 9); /* S */ 3530 } 3531 if (fi.s1ptw) { 3532 par64 |= (1 << 8); /* PTW */ 3533 } 3534 } 3535 } else { 3536 /* fsr is a DFSR/IFSR value for the short descriptor 3537 * translation table format (with WnR always clear). 3538 * Convert it to a 32-bit PAR. 3539 */ 3540 if (!ret) { 3541 /* We do not set any attribute bits in the PAR */ 3542 if (page_size == (1 << 24) 3543 && arm_feature(env, ARM_FEATURE_V7)) { 3544 par64 = (phys_addr & 0xff000000) | (1 << 1); 3545 } else { 3546 par64 = phys_addr & 0xfffff000; 3547 } 3548 if (!attrs.secure) { 3549 par64 |= (1 << 9); /* NS */ 3550 } 3551 } else { 3552 uint32_t fsr = arm_fi_to_sfsc(&fi); 3553 3554 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) | 3555 ((fsr & 0xf) << 1) | 1; 3556 } 3557 } 3558 return par64; 3559 } 3560 #endif /* CONFIG_TCG */ 3561 3562 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3563 { 3564 #ifdef CONFIG_TCG 3565 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3566 uint64_t par64; 3567 ARMMMUIdx mmu_idx; 3568 int el = arm_current_el(env); 3569 bool secure = arm_is_secure_below_el3(env); 3570 3571 switch (ri->opc2 & 6) { 3572 case 0: 3573 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */ 3574 switch (el) { 3575 case 3: 3576 mmu_idx = ARMMMUIdx_SE3; 3577 break; 3578 case 2: 3579 g_assert(!secure); /* TODO: ARMv8.4-SecEL2 */ 3580 /* fall through */ 3581 case 1: 3582 if (ri->crm == 9 && (env->uncached_cpsr & CPSR_PAN)) { 3583 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN 3584 : ARMMMUIdx_Stage1_E1_PAN); 3585 } else { 3586 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1; 3587 } 3588 break; 3589 default: 3590 g_assert_not_reached(); 3591 } 3592 break; 3593 case 2: 3594 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */ 3595 switch (el) { 3596 case 3: 3597 mmu_idx = ARMMMUIdx_SE10_0; 3598 break; 3599 case 2: 3600 mmu_idx = ARMMMUIdx_Stage1_E0; 3601 break; 3602 case 1: 3603 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0; 3604 break; 3605 default: 3606 g_assert_not_reached(); 3607 } 3608 break; 3609 case 4: 3610 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */ 3611 mmu_idx = ARMMMUIdx_E10_1; 3612 break; 3613 case 6: 3614 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */ 3615 mmu_idx = ARMMMUIdx_E10_0; 3616 break; 3617 default: 3618 g_assert_not_reached(); 3619 } 3620 3621 par64 = do_ats_write(env, value, access_type, mmu_idx); 3622 3623 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3624 #else 3625 /* Handled by hardware accelerator. */ 3626 g_assert_not_reached(); 3627 #endif /* CONFIG_TCG */ 3628 } 3629 3630 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri, 3631 uint64_t value) 3632 { 3633 #ifdef CONFIG_TCG 3634 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3635 uint64_t par64; 3636 3637 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2); 3638 3639 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3640 #else 3641 /* Handled by hardware accelerator. */ 3642 g_assert_not_reached(); 3643 #endif /* CONFIG_TCG */ 3644 } 3645 3646 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri, 3647 bool isread) 3648 { 3649 if (arm_current_el(env) == 3 && !(env->cp15.scr_el3 & SCR_NS)) { 3650 return CP_ACCESS_TRAP; 3651 } 3652 return CP_ACCESS_OK; 3653 } 3654 3655 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri, 3656 uint64_t value) 3657 { 3658 #ifdef CONFIG_TCG 3659 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3660 ARMMMUIdx mmu_idx; 3661 int secure = arm_is_secure_below_el3(env); 3662 3663 switch (ri->opc2 & 6) { 3664 case 0: 3665 switch (ri->opc1) { 3666 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */ 3667 if (ri->crm == 9 && (env->pstate & PSTATE_PAN)) { 3668 mmu_idx = (secure ? ARMMMUIdx_SE10_1_PAN 3669 : ARMMMUIdx_Stage1_E1_PAN); 3670 } else { 3671 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_Stage1_E1; 3672 } 3673 break; 3674 case 4: /* AT S1E2R, AT S1E2W */ 3675 mmu_idx = ARMMMUIdx_E2; 3676 break; 3677 case 6: /* AT S1E3R, AT S1E3W */ 3678 mmu_idx = ARMMMUIdx_SE3; 3679 break; 3680 default: 3681 g_assert_not_reached(); 3682 } 3683 break; 3684 case 2: /* AT S1E0R, AT S1E0W */ 3685 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_Stage1_E0; 3686 break; 3687 case 4: /* AT S12E1R, AT S12E1W */ 3688 mmu_idx = secure ? ARMMMUIdx_SE10_1 : ARMMMUIdx_E10_1; 3689 break; 3690 case 6: /* AT S12E0R, AT S12E0W */ 3691 mmu_idx = secure ? ARMMMUIdx_SE10_0 : ARMMMUIdx_E10_0; 3692 break; 3693 default: 3694 g_assert_not_reached(); 3695 } 3696 3697 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx); 3698 #else 3699 /* Handled by hardware accelerator. */ 3700 g_assert_not_reached(); 3701 #endif /* CONFIG_TCG */ 3702 } 3703 #endif 3704 3705 static const ARMCPRegInfo vapa_cp_reginfo[] = { 3706 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, 3707 .access = PL1_RW, .resetvalue = 0, 3708 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), 3709 offsetoflow32(CPUARMState, cp15.par_ns) }, 3710 .writefn = par_write }, 3711 #ifndef CONFIG_USER_ONLY 3712 /* This underdecoding is safe because the reginfo is NO_RAW. */ 3713 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, 3714 .access = PL1_W, .accessfn = ats_access, 3715 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 3716 #endif 3717 REGINFO_SENTINEL 3718 }; 3719 3720 /* Return basic MPU access permission bits. */ 3721 static uint32_t simple_mpu_ap_bits(uint32_t val) 3722 { 3723 uint32_t ret; 3724 uint32_t mask; 3725 int i; 3726 ret = 0; 3727 mask = 3; 3728 for (i = 0; i < 16; i += 2) { 3729 ret |= (val >> i) & mask; 3730 mask <<= 2; 3731 } 3732 return ret; 3733 } 3734 3735 /* Pad basic MPU access permission bits to extended format. */ 3736 static uint32_t extended_mpu_ap_bits(uint32_t val) 3737 { 3738 uint32_t ret; 3739 uint32_t mask; 3740 int i; 3741 ret = 0; 3742 mask = 3; 3743 for (i = 0; i < 16; i += 2) { 3744 ret |= (val & mask) << i; 3745 mask <<= 2; 3746 } 3747 return ret; 3748 } 3749 3750 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3751 uint64_t value) 3752 { 3753 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); 3754 } 3755 3756 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3757 { 3758 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); 3759 } 3760 3761 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3762 uint64_t value) 3763 { 3764 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); 3765 } 3766 3767 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3768 { 3769 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); 3770 } 3771 3772 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri) 3773 { 3774 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3775 3776 if (!u32p) { 3777 return 0; 3778 } 3779 3780 u32p += env->pmsav7.rnr[M_REG_NS]; 3781 return *u32p; 3782 } 3783 3784 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri, 3785 uint64_t value) 3786 { 3787 ARMCPU *cpu = env_archcpu(env); 3788 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3789 3790 if (!u32p) { 3791 return; 3792 } 3793 3794 u32p += env->pmsav7.rnr[M_REG_NS]; 3795 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3796 *u32p = value; 3797 } 3798 3799 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3800 uint64_t value) 3801 { 3802 ARMCPU *cpu = env_archcpu(env); 3803 uint32_t nrgs = cpu->pmsav7_dregion; 3804 3805 if (value >= nrgs) { 3806 qemu_log_mask(LOG_GUEST_ERROR, 3807 "PMSAv7 RGNR write >= # supported regions, %" PRIu32 3808 " > %" PRIu32 "\n", (uint32_t)value, nrgs); 3809 return; 3810 } 3811 3812 raw_write(env, ri, value); 3813 } 3814 3815 static const ARMCPRegInfo pmsav7_cp_reginfo[] = { 3816 /* Reset for all these registers is handled in arm_cpu_reset(), 3817 * because the PMSAv7 is also used by M-profile CPUs, which do 3818 * not register cpregs but still need the state to be reset. 3819 */ 3820 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0, 3821 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3822 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar), 3823 .readfn = pmsav7_read, .writefn = pmsav7_write, 3824 .resetfn = arm_cp_reset_ignore }, 3825 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2, 3826 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3827 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr), 3828 .readfn = pmsav7_read, .writefn = pmsav7_write, 3829 .resetfn = arm_cp_reset_ignore }, 3830 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4, 3831 .access = PL1_RW, .type = ARM_CP_NO_RAW, 3832 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr), 3833 .readfn = pmsav7_read, .writefn = pmsav7_write, 3834 .resetfn = arm_cp_reset_ignore }, 3835 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0, 3836 .access = PL1_RW, 3837 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]), 3838 .writefn = pmsav7_rgnr_write, 3839 .resetfn = arm_cp_reset_ignore }, 3840 REGINFO_SENTINEL 3841 }; 3842 3843 static const ARMCPRegInfo pmsav5_cp_reginfo[] = { 3844 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 3845 .access = PL1_RW, .type = ARM_CP_ALIAS, 3846 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 3847 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, 3848 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 3849 .access = PL1_RW, .type = ARM_CP_ALIAS, 3850 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 3851 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, 3852 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, 3853 .access = PL1_RW, 3854 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 3855 .resetvalue = 0, }, 3856 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, 3857 .access = PL1_RW, 3858 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 3859 .resetvalue = 0, }, 3860 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 3861 .access = PL1_RW, 3862 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, 3863 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, 3864 .access = PL1_RW, 3865 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, 3866 /* Protection region base and size registers */ 3867 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, 3868 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3869 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, 3870 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, 3871 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3872 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, 3873 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, 3874 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3875 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, 3876 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, 3877 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3878 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, 3879 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, 3880 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3881 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, 3882 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, 3883 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3884 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, 3885 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, 3886 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3887 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, 3888 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, 3889 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 3890 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, 3891 REGINFO_SENTINEL 3892 }; 3893 3894 static void vmsa_ttbcr_raw_write(CPUARMState *env, const ARMCPRegInfo *ri, 3895 uint64_t value) 3896 { 3897 TCR *tcr = raw_ptr(env, ri); 3898 int maskshift = extract32(value, 0, 3); 3899 3900 if (!arm_feature(env, ARM_FEATURE_V8)) { 3901 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { 3902 /* Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when 3903 * using Long-desciptor translation table format */ 3904 value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); 3905 } else if (arm_feature(env, ARM_FEATURE_EL3)) { 3906 /* In an implementation that includes the Security Extensions 3907 * TTBCR has additional fields PD0 [4] and PD1 [5] for 3908 * Short-descriptor translation table format. 3909 */ 3910 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; 3911 } else { 3912 value &= TTBCR_N; 3913 } 3914 } 3915 3916 /* Update the masks corresponding to the TCR bank being written 3917 * Note that we always calculate mask and base_mask, but 3918 * they are only used for short-descriptor tables (ie if EAE is 0); 3919 * for long-descriptor tables the TCR fields are used differently 3920 * and the mask and base_mask values are meaningless. 3921 */ 3922 tcr->raw_tcr = value; 3923 tcr->mask = ~(((uint32_t)0xffffffffu) >> maskshift); 3924 tcr->base_mask = ~((uint32_t)0x3fffu >> maskshift); 3925 } 3926 3927 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3928 uint64_t value) 3929 { 3930 ARMCPU *cpu = env_archcpu(env); 3931 TCR *tcr = raw_ptr(env, ri); 3932 3933 if (arm_feature(env, ARM_FEATURE_LPAE)) { 3934 /* With LPAE the TTBCR could result in a change of ASID 3935 * via the TTBCR.A1 bit, so do a TLB flush. 3936 */ 3937 tlb_flush(CPU(cpu)); 3938 } 3939 /* Preserve the high half of TCR_EL1, set via TTBCR2. */ 3940 value = deposit64(tcr->raw_tcr, 0, 32, value); 3941 vmsa_ttbcr_raw_write(env, ri, value); 3942 } 3943 3944 static void vmsa_ttbcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3945 { 3946 TCR *tcr = raw_ptr(env, ri); 3947 3948 /* Reset both the TCR as well as the masks corresponding to the bank of 3949 * the TCR being reset. 3950 */ 3951 tcr->raw_tcr = 0; 3952 tcr->mask = 0; 3953 tcr->base_mask = 0xffffc000u; 3954 } 3955 3956 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri, 3957 uint64_t value) 3958 { 3959 ARMCPU *cpu = env_archcpu(env); 3960 TCR *tcr = raw_ptr(env, ri); 3961 3962 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ 3963 tlb_flush(CPU(cpu)); 3964 tcr->raw_tcr = value; 3965 } 3966 3967 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3968 uint64_t value) 3969 { 3970 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */ 3971 if (cpreg_field_is_64bit(ri) && 3972 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) { 3973 ARMCPU *cpu = env_archcpu(env); 3974 tlb_flush(CPU(cpu)); 3975 } 3976 raw_write(env, ri, value); 3977 } 3978 3979 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 3980 uint64_t value) 3981 { 3982 /* 3983 * If we are running with E2&0 regime, then an ASID is active. 3984 * Flush if that might be changing. Note we're not checking 3985 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that 3986 * holds the active ASID, only checking the field that might. 3987 */ 3988 if (extract64(raw_read(env, ri) ^ value, 48, 16) && 3989 (arm_hcr_el2_eff(env) & HCR_E2H)) { 3990 tlb_flush_by_mmuidx(env_cpu(env), 3991 ARMMMUIdxBit_E20_2 | 3992 ARMMMUIdxBit_E20_2_PAN | 3993 ARMMMUIdxBit_E20_0); 3994 } 3995 raw_write(env, ri, value); 3996 } 3997 3998 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3999 uint64_t value) 4000 { 4001 ARMCPU *cpu = env_archcpu(env); 4002 CPUState *cs = CPU(cpu); 4003 4004 /* 4005 * A change in VMID to the stage2 page table (Stage2) invalidates 4006 * the combined stage 1&2 tlbs (EL10_1 and EL10_0). 4007 */ 4008 if (raw_read(env, ri) != value) { 4009 tlb_flush_by_mmuidx(cs, 4010 ARMMMUIdxBit_E10_1 | 4011 ARMMMUIdxBit_E10_1_PAN | 4012 ARMMMUIdxBit_E10_0); 4013 raw_write(env, ri, value); 4014 } 4015 } 4016 4017 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = { 4018 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 4019 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS, 4020 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), 4021 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, }, 4022 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 4023 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 4024 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), 4025 offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, 4026 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, 4027 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 4028 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), 4029 offsetof(CPUARMState, cp15.dfar_ns) } }, 4030 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, 4031 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, 4032 .access = PL1_RW, .accessfn = access_tvm_trvm, 4033 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), 4034 .resetvalue = 0, }, 4035 REGINFO_SENTINEL 4036 }; 4037 4038 static const ARMCPRegInfo vmsa_cp_reginfo[] = { 4039 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, 4040 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, 4041 .access = PL1_RW, .accessfn = access_tvm_trvm, 4042 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, 4043 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, 4044 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, 4045 .access = PL1_RW, .accessfn = access_tvm_trvm, 4046 .writefn = vmsa_ttbr_write, .resetvalue = 0, 4047 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 4048 offsetof(CPUARMState, cp15.ttbr0_ns) } }, 4049 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, 4050 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, 4051 .access = PL1_RW, .accessfn = access_tvm_trvm, 4052 .writefn = vmsa_ttbr_write, .resetvalue = 0, 4053 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 4054 offsetof(CPUARMState, cp15.ttbr1_ns) } }, 4055 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, 4056 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 4057 .access = PL1_RW, .accessfn = access_tvm_trvm, 4058 .writefn = vmsa_tcr_el12_write, 4059 .resetfn = vmsa_ttbcr_reset, .raw_writefn = raw_write, 4060 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, 4061 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 4062 .access = PL1_RW, .accessfn = access_tvm_trvm, 4063 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write, 4064 .raw_writefn = vmsa_ttbcr_raw_write, 4065 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), 4066 offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, 4067 REGINFO_SENTINEL 4068 }; 4069 4070 /* Note that unlike TTBCR, writing to TTBCR2 does not require flushing 4071 * qemu tlbs nor adjusting cached masks. 4072 */ 4073 static const ARMCPRegInfo ttbcr2_reginfo = { 4074 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3, 4075 .access = PL1_RW, .accessfn = access_tvm_trvm, 4076 .type = ARM_CP_ALIAS, 4077 .bank_fieldoffsets = { offsetofhigh32(CPUARMState, cp15.tcr_el[3]), 4078 offsetofhigh32(CPUARMState, cp15.tcr_el[1]) }, 4079 }; 4080 4081 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, 4082 uint64_t value) 4083 { 4084 env->cp15.c15_ticonfig = value & 0xe7; 4085 /* The OS_TYPE bit in this register changes the reported CPUID! */ 4086 env->cp15.c0_cpuid = (value & (1 << 5)) ? 4087 ARM_CPUID_TI915T : ARM_CPUID_TI925T; 4088 } 4089 4090 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, 4091 uint64_t value) 4092 { 4093 env->cp15.c15_threadid = value & 0xffff; 4094 } 4095 4096 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, 4097 uint64_t value) 4098 { 4099 /* Wait-for-interrupt (deprecated) */ 4100 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT); 4101 } 4102 4103 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, 4104 uint64_t value) 4105 { 4106 /* On OMAP there are registers indicating the max/min index of dcache lines 4107 * containing a dirty line; cache flush operations have to reset these. 4108 */ 4109 env->cp15.c15_i_max = 0x000; 4110 env->cp15.c15_i_min = 0xff0; 4111 } 4112 4113 static const ARMCPRegInfo omap_cp_reginfo[] = { 4114 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, 4115 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, 4116 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), 4117 .resetvalue = 0, }, 4118 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, 4119 .access = PL1_RW, .type = ARM_CP_NOP }, 4120 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, 4121 .access = PL1_RW, 4122 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, 4123 .writefn = omap_ticonfig_write }, 4124 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, 4125 .access = PL1_RW, 4126 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, 4127 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, 4128 .access = PL1_RW, .resetvalue = 0xff0, 4129 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, 4130 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, 4131 .access = PL1_RW, 4132 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, 4133 .writefn = omap_threadid_write }, 4134 { .name = "TI925T_STATUS", .cp = 15, .crn = 15, 4135 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 4136 .type = ARM_CP_NO_RAW, 4137 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, 4138 /* TODO: Peripheral port remap register: 4139 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller 4140 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), 4141 * when MMU is off. 4142 */ 4143 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 4144 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 4145 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW, 4146 .writefn = omap_cachemaint_write }, 4147 { .name = "C9", .cp = 15, .crn = 9, 4148 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, 4149 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, 4150 REGINFO_SENTINEL 4151 }; 4152 4153 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, 4154 uint64_t value) 4155 { 4156 env->cp15.c15_cpar = value & 0x3fff; 4157 } 4158 4159 static const ARMCPRegInfo xscale_cp_reginfo[] = { 4160 { .name = "XSCALE_CPAR", 4161 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 4162 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, 4163 .writefn = xscale_cpar_write, }, 4164 { .name = "XSCALE_AUXCR", 4165 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, 4166 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), 4167 .resetvalue = 0, }, 4168 /* XScale specific cache-lockdown: since we have no cache we NOP these 4169 * and hope the guest does not really rely on cache behaviour. 4170 */ 4171 { .name = "XSCALE_LOCK_ICACHE_LINE", 4172 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, 4173 .access = PL1_W, .type = ARM_CP_NOP }, 4174 { .name = "XSCALE_UNLOCK_ICACHE", 4175 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, 4176 .access = PL1_W, .type = ARM_CP_NOP }, 4177 { .name = "XSCALE_DCACHE_LOCK", 4178 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, 4179 .access = PL1_RW, .type = ARM_CP_NOP }, 4180 { .name = "XSCALE_UNLOCK_DCACHE", 4181 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, 4182 .access = PL1_W, .type = ARM_CP_NOP }, 4183 REGINFO_SENTINEL 4184 }; 4185 4186 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { 4187 /* RAZ/WI the whole crn=15 space, when we don't have a more specific 4188 * implementation of this implementation-defined space. 4189 * Ideally this should eventually disappear in favour of actually 4190 * implementing the correct behaviour for all cores. 4191 */ 4192 { .name = "C15_IMPDEF", .cp = 15, .crn = 15, 4193 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 4194 .access = PL1_RW, 4195 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE, 4196 .resetvalue = 0 }, 4197 REGINFO_SENTINEL 4198 }; 4199 4200 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { 4201 /* Cache status: RAZ because we have no cache so it's always clean */ 4202 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, 4203 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4204 .resetvalue = 0 }, 4205 REGINFO_SENTINEL 4206 }; 4207 4208 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { 4209 /* We never have a a block transfer operation in progress */ 4210 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, 4211 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4212 .resetvalue = 0 }, 4213 /* The cache ops themselves: these all NOP for QEMU */ 4214 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, 4215 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 4216 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, 4217 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 4218 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, 4219 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 4220 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, 4221 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 4222 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, 4223 .access = PL0_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 4224 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, 4225 .access = PL1_W, .type = ARM_CP_NOP|ARM_CP_64BIT }, 4226 REGINFO_SENTINEL 4227 }; 4228 4229 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { 4230 /* The cache test-and-clean instructions always return (1 << 30) 4231 * to indicate that there are no dirty cache lines. 4232 */ 4233 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, 4234 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4235 .resetvalue = (1 << 30) }, 4236 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, 4237 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4238 .resetvalue = (1 << 30) }, 4239 REGINFO_SENTINEL 4240 }; 4241 4242 static const ARMCPRegInfo strongarm_cp_reginfo[] = { 4243 /* Ignore ReadBuffer accesses */ 4244 { .name = "C9_READBUFFER", .cp = 15, .crn = 9, 4245 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 4246 .access = PL1_RW, .resetvalue = 0, 4247 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW }, 4248 REGINFO_SENTINEL 4249 }; 4250 4251 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4252 { 4253 ARMCPU *cpu = env_archcpu(env); 4254 unsigned int cur_el = arm_current_el(env); 4255 bool secure = arm_is_secure(env); 4256 4257 if (arm_feature(&cpu->env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 4258 return env->cp15.vpidr_el2; 4259 } 4260 return raw_read(env, ri); 4261 } 4262 4263 static uint64_t mpidr_read_val(CPUARMState *env) 4264 { 4265 ARMCPU *cpu = env_archcpu(env); 4266 uint64_t mpidr = cpu->mp_affinity; 4267 4268 if (arm_feature(env, ARM_FEATURE_V7MP)) { 4269 mpidr |= (1U << 31); 4270 /* Cores which are uniprocessor (non-coherent) 4271 * but still implement the MP extensions set 4272 * bit 30. (For instance, Cortex-R5). 4273 */ 4274 if (cpu->mp_is_up) { 4275 mpidr |= (1u << 30); 4276 } 4277 } 4278 return mpidr; 4279 } 4280 4281 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4282 { 4283 unsigned int cur_el = arm_current_el(env); 4284 bool secure = arm_is_secure(env); 4285 4286 if (arm_feature(env, ARM_FEATURE_EL2) && !secure && cur_el == 1) { 4287 return env->cp15.vmpidr_el2; 4288 } 4289 return mpidr_read_val(env); 4290 } 4291 4292 static const ARMCPRegInfo lpae_cp_reginfo[] = { 4293 /* NOP AMAIR0/1 */ 4294 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, 4295 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, 4296 .access = PL1_RW, .accessfn = access_tvm_trvm, 4297 .type = ARM_CP_CONST, .resetvalue = 0 }, 4298 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ 4299 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, 4300 .access = PL1_RW, .accessfn = access_tvm_trvm, 4301 .type = ARM_CP_CONST, .resetvalue = 0 }, 4302 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, 4303 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, 4304 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), 4305 offsetof(CPUARMState, cp15.par_ns)} }, 4306 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, 4307 .access = PL1_RW, .accessfn = access_tvm_trvm, 4308 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4309 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 4310 offsetof(CPUARMState, cp15.ttbr0_ns) }, 4311 .writefn = vmsa_ttbr_write, }, 4312 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, 4313 .access = PL1_RW, .accessfn = access_tvm_trvm, 4314 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4315 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 4316 offsetof(CPUARMState, cp15.ttbr1_ns) }, 4317 .writefn = vmsa_ttbr_write, }, 4318 REGINFO_SENTINEL 4319 }; 4320 4321 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4322 { 4323 return vfp_get_fpcr(env); 4324 } 4325 4326 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4327 uint64_t value) 4328 { 4329 vfp_set_fpcr(env, value); 4330 } 4331 4332 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4333 { 4334 return vfp_get_fpsr(env); 4335 } 4336 4337 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4338 uint64_t value) 4339 { 4340 vfp_set_fpsr(env, value); 4341 } 4342 4343 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri, 4344 bool isread) 4345 { 4346 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) { 4347 return CP_ACCESS_TRAP; 4348 } 4349 return CP_ACCESS_OK; 4350 } 4351 4352 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, 4353 uint64_t value) 4354 { 4355 env->daif = value & PSTATE_DAIF; 4356 } 4357 4358 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri) 4359 { 4360 return env->pstate & PSTATE_PAN; 4361 } 4362 4363 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri, 4364 uint64_t value) 4365 { 4366 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN); 4367 } 4368 4369 static const ARMCPRegInfo pan_reginfo = { 4370 .name = "PAN", .state = ARM_CP_STATE_AA64, 4371 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3, 4372 .type = ARM_CP_NO_RAW, .access = PL1_RW, 4373 .readfn = aa64_pan_read, .writefn = aa64_pan_write 4374 }; 4375 4376 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri) 4377 { 4378 return env->pstate & PSTATE_UAO; 4379 } 4380 4381 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri, 4382 uint64_t value) 4383 { 4384 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO); 4385 } 4386 4387 static const ARMCPRegInfo uao_reginfo = { 4388 .name = "UAO", .state = ARM_CP_STATE_AA64, 4389 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4, 4390 .type = ARM_CP_NO_RAW, .access = PL1_RW, 4391 .readfn = aa64_uao_read, .writefn = aa64_uao_write 4392 }; 4393 4394 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env, 4395 const ARMCPRegInfo *ri, 4396 bool isread) 4397 { 4398 /* Cache invalidate/clean to Point of Coherency or Persistence... */ 4399 switch (arm_current_el(env)) { 4400 case 0: 4401 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */ 4402 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) { 4403 return CP_ACCESS_TRAP; 4404 } 4405 /* fall through */ 4406 case 1: 4407 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */ 4408 if (arm_hcr_el2_eff(env) & HCR_TPCP) { 4409 return CP_ACCESS_TRAP_EL2; 4410 } 4411 break; 4412 } 4413 return CP_ACCESS_OK; 4414 } 4415 4416 static CPAccessResult aa64_cacheop_pou_access(CPUARMState *env, 4417 const ARMCPRegInfo *ri, 4418 bool isread) 4419 { 4420 /* Cache invalidate/clean to Point of Unification... */ 4421 switch (arm_current_el(env)) { 4422 case 0: 4423 /* ... EL0 must UNDEF unless SCTLR_EL1.UCI is set. */ 4424 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) { 4425 return CP_ACCESS_TRAP; 4426 } 4427 /* fall through */ 4428 case 1: 4429 /* ... EL1 must trap to EL2 if HCR_EL2.TPU is set. */ 4430 if (arm_hcr_el2_eff(env) & HCR_TPU) { 4431 return CP_ACCESS_TRAP_EL2; 4432 } 4433 break; 4434 } 4435 return CP_ACCESS_OK; 4436 } 4437 4438 /* See: D4.7.2 TLB maintenance requirements and the TLB maintenance instructions 4439 * Page D4-1736 (DDI0487A.b) 4440 */ 4441 4442 static int vae1_tlbmask(CPUARMState *env) 4443 { 4444 /* Since we exclude secure first, we may read HCR_EL2 directly. */ 4445 if (arm_is_secure_below_el3(env)) { 4446 return ARMMMUIdxBit_SE10_1 | 4447 ARMMMUIdxBit_SE10_1_PAN | 4448 ARMMMUIdxBit_SE10_0; 4449 } else if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) 4450 == (HCR_E2H | HCR_TGE)) { 4451 return ARMMMUIdxBit_E20_2 | 4452 ARMMMUIdxBit_E20_2_PAN | 4453 ARMMMUIdxBit_E20_0; 4454 } else { 4455 return ARMMMUIdxBit_E10_1 | 4456 ARMMMUIdxBit_E10_1_PAN | 4457 ARMMMUIdxBit_E10_0; 4458 } 4459 } 4460 4461 /* Return 56 if TBI is enabled, 64 otherwise. */ 4462 static int tlbbits_for_regime(CPUARMState *env, ARMMMUIdx mmu_idx, 4463 uint64_t addr) 4464 { 4465 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr; 4466 int tbi = aa64_va_parameter_tbi(tcr, mmu_idx); 4467 int select = extract64(addr, 55, 1); 4468 4469 return (tbi >> select) & 1 ? 56 : 64; 4470 } 4471 4472 static int vae1_tlbbits(CPUARMState *env, uint64_t addr) 4473 { 4474 ARMMMUIdx mmu_idx; 4475 4476 /* Only the regime of the mmu_idx below is significant. */ 4477 if (arm_is_secure_below_el3(env)) { 4478 mmu_idx = ARMMMUIdx_SE10_0; 4479 } else if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) 4480 == (HCR_E2H | HCR_TGE)) { 4481 mmu_idx = ARMMMUIdx_E20_0; 4482 } else { 4483 mmu_idx = ARMMMUIdx_E10_0; 4484 } 4485 return tlbbits_for_regime(env, mmu_idx, addr); 4486 } 4487 4488 static void tlbi_aa64_vmalle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4489 uint64_t value) 4490 { 4491 CPUState *cs = env_cpu(env); 4492 int mask = vae1_tlbmask(env); 4493 4494 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask); 4495 } 4496 4497 static void tlbi_aa64_vmalle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 4498 uint64_t value) 4499 { 4500 CPUState *cs = env_cpu(env); 4501 int mask = vae1_tlbmask(env); 4502 4503 if (tlb_force_broadcast(env)) { 4504 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask); 4505 } else { 4506 tlb_flush_by_mmuidx(cs, mask); 4507 } 4508 } 4509 4510 static int alle1_tlbmask(CPUARMState *env) 4511 { 4512 /* 4513 * Note that the 'ALL' scope must invalidate both stage 1 and 4514 * stage 2 translations, whereas most other scopes only invalidate 4515 * stage 1 translations. 4516 */ 4517 if (arm_is_secure_below_el3(env)) { 4518 return ARMMMUIdxBit_SE10_1 | 4519 ARMMMUIdxBit_SE10_1_PAN | 4520 ARMMMUIdxBit_SE10_0; 4521 } else { 4522 return ARMMMUIdxBit_E10_1 | 4523 ARMMMUIdxBit_E10_1_PAN | 4524 ARMMMUIdxBit_E10_0; 4525 } 4526 } 4527 4528 static int e2_tlbmask(CPUARMState *env) 4529 { 4530 /* TODO: ARMv8.4-SecEL2 */ 4531 return ARMMMUIdxBit_E20_0 | 4532 ARMMMUIdxBit_E20_2 | 4533 ARMMMUIdxBit_E20_2_PAN | 4534 ARMMMUIdxBit_E2; 4535 } 4536 4537 static void tlbi_aa64_alle1_write(CPUARMState *env, const ARMCPRegInfo *ri, 4538 uint64_t value) 4539 { 4540 CPUState *cs = env_cpu(env); 4541 int mask = alle1_tlbmask(env); 4542 4543 tlb_flush_by_mmuidx(cs, mask); 4544 } 4545 4546 static void tlbi_aa64_alle2_write(CPUARMState *env, const ARMCPRegInfo *ri, 4547 uint64_t value) 4548 { 4549 CPUState *cs = env_cpu(env); 4550 int mask = e2_tlbmask(env); 4551 4552 tlb_flush_by_mmuidx(cs, mask); 4553 } 4554 4555 static void tlbi_aa64_alle3_write(CPUARMState *env, const ARMCPRegInfo *ri, 4556 uint64_t value) 4557 { 4558 ARMCPU *cpu = env_archcpu(env); 4559 CPUState *cs = CPU(cpu); 4560 4561 tlb_flush_by_mmuidx(cs, ARMMMUIdxBit_SE3); 4562 } 4563 4564 static void tlbi_aa64_alle1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4565 uint64_t value) 4566 { 4567 CPUState *cs = env_cpu(env); 4568 int mask = alle1_tlbmask(env); 4569 4570 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask); 4571 } 4572 4573 static void tlbi_aa64_alle2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4574 uint64_t value) 4575 { 4576 CPUState *cs = env_cpu(env); 4577 int mask = e2_tlbmask(env); 4578 4579 tlb_flush_by_mmuidx_all_cpus_synced(cs, mask); 4580 } 4581 4582 static void tlbi_aa64_alle3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4583 uint64_t value) 4584 { 4585 CPUState *cs = env_cpu(env); 4586 4587 tlb_flush_by_mmuidx_all_cpus_synced(cs, ARMMMUIdxBit_SE3); 4588 } 4589 4590 static void tlbi_aa64_vae2_write(CPUARMState *env, const ARMCPRegInfo *ri, 4591 uint64_t value) 4592 { 4593 /* Invalidate by VA, EL2 4594 * Currently handles both VAE2 and VALE2, since we don't support 4595 * flush-last-level-only. 4596 */ 4597 CPUState *cs = env_cpu(env); 4598 int mask = e2_tlbmask(env); 4599 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4600 4601 tlb_flush_page_by_mmuidx(cs, pageaddr, mask); 4602 } 4603 4604 static void tlbi_aa64_vae3_write(CPUARMState *env, const ARMCPRegInfo *ri, 4605 uint64_t value) 4606 { 4607 /* Invalidate by VA, EL3 4608 * Currently handles both VAE3 and VALE3, since we don't support 4609 * flush-last-level-only. 4610 */ 4611 ARMCPU *cpu = env_archcpu(env); 4612 CPUState *cs = CPU(cpu); 4613 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4614 4615 tlb_flush_page_by_mmuidx(cs, pageaddr, ARMMMUIdxBit_SE3); 4616 } 4617 4618 static void tlbi_aa64_vae1is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4619 uint64_t value) 4620 { 4621 CPUState *cs = env_cpu(env); 4622 int mask = vae1_tlbmask(env); 4623 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4624 int bits = vae1_tlbbits(env, pageaddr); 4625 4626 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits); 4627 } 4628 4629 static void tlbi_aa64_vae1_write(CPUARMState *env, const ARMCPRegInfo *ri, 4630 uint64_t value) 4631 { 4632 /* Invalidate by VA, EL1&0 (AArch64 version). 4633 * Currently handles all of VAE1, VAAE1, VAALE1 and VALE1, 4634 * since we don't support flush-for-specific-ASID-only or 4635 * flush-last-level-only. 4636 */ 4637 CPUState *cs = env_cpu(env); 4638 int mask = vae1_tlbmask(env); 4639 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4640 int bits = vae1_tlbbits(env, pageaddr); 4641 4642 if (tlb_force_broadcast(env)) { 4643 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, mask, bits); 4644 } else { 4645 tlb_flush_page_bits_by_mmuidx(cs, pageaddr, mask, bits); 4646 } 4647 } 4648 4649 static void tlbi_aa64_vae2is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4650 uint64_t value) 4651 { 4652 CPUState *cs = env_cpu(env); 4653 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4654 int bits = tlbbits_for_regime(env, ARMMMUIdx_E2, pageaddr); 4655 4656 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, 4657 ARMMMUIdxBit_E2, bits); 4658 } 4659 4660 static void tlbi_aa64_vae3is_write(CPUARMState *env, const ARMCPRegInfo *ri, 4661 uint64_t value) 4662 { 4663 CPUState *cs = env_cpu(env); 4664 uint64_t pageaddr = sextract64(value << 12, 0, 56); 4665 int bits = tlbbits_for_regime(env, ARMMMUIdx_SE3, pageaddr); 4666 4667 tlb_flush_page_bits_by_mmuidx_all_cpus_synced(cs, pageaddr, 4668 ARMMMUIdxBit_SE3, bits); 4669 } 4670 4671 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri, 4672 bool isread) 4673 { 4674 int cur_el = arm_current_el(env); 4675 4676 if (cur_el < 2) { 4677 uint64_t hcr = arm_hcr_el2_eff(env); 4678 4679 if (cur_el == 0) { 4680 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 4681 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) { 4682 return CP_ACCESS_TRAP_EL2; 4683 } 4684 } else { 4685 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) { 4686 return CP_ACCESS_TRAP; 4687 } 4688 if (hcr & HCR_TDZ) { 4689 return CP_ACCESS_TRAP_EL2; 4690 } 4691 } 4692 } else if (hcr & HCR_TDZ) { 4693 return CP_ACCESS_TRAP_EL2; 4694 } 4695 } 4696 return CP_ACCESS_OK; 4697 } 4698 4699 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) 4700 { 4701 ARMCPU *cpu = env_archcpu(env); 4702 int dzp_bit = 1 << 4; 4703 4704 /* DZP indicates whether DC ZVA access is allowed */ 4705 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) { 4706 dzp_bit = 0; 4707 } 4708 return cpu->dcz_blocksize | dzp_bit; 4709 } 4710 4711 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 4712 bool isread) 4713 { 4714 if (!(env->pstate & PSTATE_SP)) { 4715 /* Access to SP_EL0 is undefined if it's being used as 4716 * the stack pointer. 4717 */ 4718 return CP_ACCESS_TRAP_UNCATEGORIZED; 4719 } 4720 return CP_ACCESS_OK; 4721 } 4722 4723 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) 4724 { 4725 return env->pstate & PSTATE_SP; 4726 } 4727 4728 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 4729 { 4730 update_spsel(env, val); 4731 } 4732 4733 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4734 uint64_t value) 4735 { 4736 ARMCPU *cpu = env_archcpu(env); 4737 4738 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) { 4739 /* M bit is RAZ/WI for PMSA with no MPU implemented */ 4740 value &= ~SCTLR_M; 4741 } 4742 4743 /* ??? Lots of these bits are not implemented. */ 4744 4745 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) { 4746 if (ri->opc1 == 6) { /* SCTLR_EL3 */ 4747 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA); 4748 } else { 4749 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF | 4750 SCTLR_ATA0 | SCTLR_ATA); 4751 } 4752 } 4753 4754 if (raw_read(env, ri) == value) { 4755 /* Skip the TLB flush if nothing actually changed; Linux likes 4756 * to do a lot of pointless SCTLR writes. 4757 */ 4758 return; 4759 } 4760 4761 raw_write(env, ri, value); 4762 4763 /* This may enable/disable the MMU, so do a TLB flush. */ 4764 tlb_flush(CPU(cpu)); 4765 4766 if (ri->type & ARM_CP_SUPPRESS_TB_END) { 4767 /* 4768 * Normally we would always end the TB on an SCTLR write; see the 4769 * comment in ARMCPRegInfo sctlr initialization below for why Xscale 4770 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild 4771 * of hflags from the translator, so do it here. 4772 */ 4773 arm_rebuild_hflags(env); 4774 } 4775 } 4776 4777 static CPAccessResult fpexc32_access(CPUARMState *env, const ARMCPRegInfo *ri, 4778 bool isread) 4779 { 4780 if ((env->cp15.cptr_el[2] & CPTR_TFP) && arm_current_el(env) == 2) { 4781 return CP_ACCESS_TRAP_FP_EL2; 4782 } 4783 if (env->cp15.cptr_el[3] & CPTR_TFP) { 4784 return CP_ACCESS_TRAP_FP_EL3; 4785 } 4786 return CP_ACCESS_OK; 4787 } 4788 4789 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4790 uint64_t value) 4791 { 4792 env->cp15.mdcr_el3 = value & SDCR_VALID_MASK; 4793 } 4794 4795 static const ARMCPRegInfo v8_cp_reginfo[] = { 4796 /* Minimal set of EL0-visible registers. This will need to be expanded 4797 * significantly for system emulation of AArch64 CPUs. 4798 */ 4799 { .name = "NZCV", .state = ARM_CP_STATE_AA64, 4800 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, 4801 .access = PL0_RW, .type = ARM_CP_NZCV }, 4802 { .name = "DAIF", .state = ARM_CP_STATE_AA64, 4803 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, 4804 .type = ARM_CP_NO_RAW, 4805 .access = PL0_RW, .accessfn = aa64_daif_access, 4806 .fieldoffset = offsetof(CPUARMState, daif), 4807 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, 4808 { .name = "FPCR", .state = ARM_CP_STATE_AA64, 4809 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, 4810 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 4811 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, 4812 { .name = "FPSR", .state = ARM_CP_STATE_AA64, 4813 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, 4814 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 4815 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, 4816 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, 4817 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, 4818 .access = PL0_R, .type = ARM_CP_NO_RAW, 4819 .readfn = aa64_dczid_read }, 4820 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, 4821 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, 4822 .access = PL0_W, .type = ARM_CP_DC_ZVA, 4823 #ifndef CONFIG_USER_ONLY 4824 /* Avoid overhead of an access check that always passes in user-mode */ 4825 .accessfn = aa64_zva_access, 4826 #endif 4827 }, 4828 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, 4829 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, 4830 .access = PL1_R, .type = ARM_CP_CURRENTEL }, 4831 /* Cache ops: all NOPs since we don't emulate caches */ 4832 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, 4833 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 4834 .access = PL1_W, .type = ARM_CP_NOP, 4835 .accessfn = aa64_cacheop_pou_access }, 4836 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, 4837 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 4838 .access = PL1_W, .type = ARM_CP_NOP, 4839 .accessfn = aa64_cacheop_pou_access }, 4840 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, 4841 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, 4842 .access = PL0_W, .type = ARM_CP_NOP, 4843 .accessfn = aa64_cacheop_pou_access }, 4844 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, 4845 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 4846 .access = PL1_W, .accessfn = aa64_cacheop_poc_access, 4847 .type = ARM_CP_NOP }, 4848 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, 4849 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 4850 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 4851 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, 4852 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, 4853 .access = PL0_W, .type = ARM_CP_NOP, 4854 .accessfn = aa64_cacheop_poc_access }, 4855 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, 4856 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 4857 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 4858 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, 4859 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, 4860 .access = PL0_W, .type = ARM_CP_NOP, 4861 .accessfn = aa64_cacheop_pou_access }, 4862 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, 4863 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, 4864 .access = PL0_W, .type = ARM_CP_NOP, 4865 .accessfn = aa64_cacheop_poc_access }, 4866 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, 4867 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 4868 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 4869 /* TLBI operations */ 4870 { .name = "TLBI_VMALLE1IS", .state = ARM_CP_STATE_AA64, 4871 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 0, 4872 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4873 .writefn = tlbi_aa64_vmalle1is_write }, 4874 { .name = "TLBI_VAE1IS", .state = ARM_CP_STATE_AA64, 4875 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 1, 4876 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4877 .writefn = tlbi_aa64_vae1is_write }, 4878 { .name = "TLBI_ASIDE1IS", .state = ARM_CP_STATE_AA64, 4879 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 2, 4880 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4881 .writefn = tlbi_aa64_vmalle1is_write }, 4882 { .name = "TLBI_VAAE1IS", .state = ARM_CP_STATE_AA64, 4883 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 3, 4884 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4885 .writefn = tlbi_aa64_vae1is_write }, 4886 { .name = "TLBI_VALE1IS", .state = ARM_CP_STATE_AA64, 4887 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 4888 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4889 .writefn = tlbi_aa64_vae1is_write }, 4890 { .name = "TLBI_VAALE1IS", .state = ARM_CP_STATE_AA64, 4891 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 4892 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4893 .writefn = tlbi_aa64_vae1is_write }, 4894 { .name = "TLBI_VMALLE1", .state = ARM_CP_STATE_AA64, 4895 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 0, 4896 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4897 .writefn = tlbi_aa64_vmalle1_write }, 4898 { .name = "TLBI_VAE1", .state = ARM_CP_STATE_AA64, 4899 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 1, 4900 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4901 .writefn = tlbi_aa64_vae1_write }, 4902 { .name = "TLBI_ASIDE1", .state = ARM_CP_STATE_AA64, 4903 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 2, 4904 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4905 .writefn = tlbi_aa64_vmalle1_write }, 4906 { .name = "TLBI_VAAE1", .state = ARM_CP_STATE_AA64, 4907 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 3, 4908 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4909 .writefn = tlbi_aa64_vae1_write }, 4910 { .name = "TLBI_VALE1", .state = ARM_CP_STATE_AA64, 4911 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 4912 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4913 .writefn = tlbi_aa64_vae1_write }, 4914 { .name = "TLBI_VAALE1", .state = ARM_CP_STATE_AA64, 4915 .opc0 = 1, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 4916 .access = PL1_W, .accessfn = access_ttlb, .type = ARM_CP_NO_RAW, 4917 .writefn = tlbi_aa64_vae1_write }, 4918 { .name = "TLBI_IPAS2E1IS", .state = ARM_CP_STATE_AA64, 4919 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 4920 .access = PL2_W, .type = ARM_CP_NOP }, 4921 { .name = "TLBI_IPAS2LE1IS", .state = ARM_CP_STATE_AA64, 4922 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 4923 .access = PL2_W, .type = ARM_CP_NOP }, 4924 { .name = "TLBI_ALLE1IS", .state = ARM_CP_STATE_AA64, 4925 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 4926 .access = PL2_W, .type = ARM_CP_NO_RAW, 4927 .writefn = tlbi_aa64_alle1is_write }, 4928 { .name = "TLBI_VMALLS12E1IS", .state = ARM_CP_STATE_AA64, 4929 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 6, 4930 .access = PL2_W, .type = ARM_CP_NO_RAW, 4931 .writefn = tlbi_aa64_alle1is_write }, 4932 { .name = "TLBI_IPAS2E1", .state = ARM_CP_STATE_AA64, 4933 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 4934 .access = PL2_W, .type = ARM_CP_NOP }, 4935 { .name = "TLBI_IPAS2LE1", .state = ARM_CP_STATE_AA64, 4936 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 4937 .access = PL2_W, .type = ARM_CP_NOP }, 4938 { .name = "TLBI_ALLE1", .state = ARM_CP_STATE_AA64, 4939 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 4940 .access = PL2_W, .type = ARM_CP_NO_RAW, 4941 .writefn = tlbi_aa64_alle1_write }, 4942 { .name = "TLBI_VMALLS12E1", .state = ARM_CP_STATE_AA64, 4943 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 6, 4944 .access = PL2_W, .type = ARM_CP_NO_RAW, 4945 .writefn = tlbi_aa64_alle1is_write }, 4946 #ifndef CONFIG_USER_ONLY 4947 /* 64 bit address translation operations */ 4948 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, 4949 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, 4950 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4951 .writefn = ats_write64 }, 4952 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, 4953 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, 4954 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4955 .writefn = ats_write64 }, 4956 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, 4957 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, 4958 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4959 .writefn = ats_write64 }, 4960 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, 4961 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, 4962 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4963 .writefn = ats_write64 }, 4964 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64, 4965 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4, 4966 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4967 .writefn = ats_write64 }, 4968 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64, 4969 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5, 4970 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4971 .writefn = ats_write64 }, 4972 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64, 4973 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6, 4974 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4975 .writefn = ats_write64 }, 4976 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64, 4977 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7, 4978 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4979 .writefn = ats_write64 }, 4980 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */ 4981 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64, 4982 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0, 4983 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4984 .writefn = ats_write64 }, 4985 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64, 4986 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1, 4987 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 4988 .writefn = ats_write64 }, 4989 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64, 4990 .type = ARM_CP_ALIAS, 4991 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0, 4992 .access = PL1_RW, .resetvalue = 0, 4993 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]), 4994 .writefn = par_write }, 4995 #endif 4996 /* TLB invalidate last level of translation table walk */ 4997 { .name = "TLBIMVALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 5, 4998 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 4999 .writefn = tlbimva_is_write }, 5000 { .name = "TLBIMVAALIS", .cp = 15, .opc1 = 0, .crn = 8, .crm = 3, .opc2 = 7, 5001 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 5002 .writefn = tlbimvaa_is_write }, 5003 { .name = "TLBIMVAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 5, 5004 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 5005 .writefn = tlbimva_write }, 5006 { .name = "TLBIMVAAL", .cp = 15, .opc1 = 0, .crn = 8, .crm = 7, .opc2 = 7, 5007 .type = ARM_CP_NO_RAW, .access = PL1_W, .accessfn = access_ttlb, 5008 .writefn = tlbimvaa_write }, 5009 { .name = "TLBIMVALH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 5010 .type = ARM_CP_NO_RAW, .access = PL2_W, 5011 .writefn = tlbimva_hyp_write }, 5012 { .name = "TLBIMVALHIS", 5013 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 5014 .type = ARM_CP_NO_RAW, .access = PL2_W, 5015 .writefn = tlbimva_hyp_is_write }, 5016 { .name = "TLBIIPAS2", 5017 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 1, 5018 .type = ARM_CP_NOP, .access = PL2_W }, 5019 { .name = "TLBIIPAS2IS", 5020 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 1, 5021 .type = ARM_CP_NOP, .access = PL2_W }, 5022 { .name = "TLBIIPAS2L", 5023 .cp = 15, .opc1 = 4, .crn = 8, .crm = 4, .opc2 = 5, 5024 .type = ARM_CP_NOP, .access = PL2_W }, 5025 { .name = "TLBIIPAS2LIS", 5026 .cp = 15, .opc1 = 4, .crn = 8, .crm = 0, .opc2 = 5, 5027 .type = ARM_CP_NOP, .access = PL2_W }, 5028 /* 32 bit cache operations */ 5029 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 5030 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access }, 5031 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, 5032 .type = ARM_CP_NOP, .access = PL1_W }, 5033 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 5034 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access }, 5035 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, 5036 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access }, 5037 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, 5038 .type = ARM_CP_NOP, .access = PL1_W }, 5039 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, 5040 .type = ARM_CP_NOP, .access = PL1_W }, 5041 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 5042 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5043 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 5044 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5045 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, 5046 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5047 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 5048 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5049 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, 5050 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_pou_access }, 5051 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, 5052 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5053 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 5054 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5055 /* MMU Domain access control / MPU write buffer control */ 5056 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, 5057 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 5058 .writefn = dacr_write, .raw_writefn = raw_write, 5059 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 5060 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 5061 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, 5062 .type = ARM_CP_ALIAS, 5063 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, 5064 .access = PL1_RW, 5065 .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, 5066 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, 5067 .type = ARM_CP_ALIAS, 5068 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, 5069 .access = PL1_RW, 5070 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) }, 5071 /* We rely on the access checks not allowing the guest to write to the 5072 * state field when SPSel indicates that it's being used as the stack 5073 * pointer. 5074 */ 5075 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, 5076 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, 5077 .access = PL1_RW, .accessfn = sp_el0_access, 5078 .type = ARM_CP_ALIAS, 5079 .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, 5080 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64, 5081 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0, 5082 .access = PL2_RW, .type = ARM_CP_ALIAS, 5083 .fieldoffset = offsetof(CPUARMState, sp_el[1]) }, 5084 { .name = "SPSel", .state = ARM_CP_STATE_AA64, 5085 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, 5086 .type = ARM_CP_NO_RAW, 5087 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, 5088 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64, 5089 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0, 5090 .type = ARM_CP_ALIAS, 5091 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]), 5092 .access = PL2_RW, .accessfn = fpexc32_access }, 5093 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, 5094 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, 5095 .access = PL2_RW, .resetvalue = 0, 5096 .writefn = dacr_write, .raw_writefn = raw_write, 5097 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, 5098 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, 5099 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, 5100 .access = PL2_RW, .resetvalue = 0, 5101 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, 5102 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64, 5103 .type = ARM_CP_ALIAS, 5104 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0, 5105 .access = PL2_RW, 5106 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) }, 5107 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64, 5108 .type = ARM_CP_ALIAS, 5109 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1, 5110 .access = PL2_RW, 5111 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) }, 5112 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64, 5113 .type = ARM_CP_ALIAS, 5114 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2, 5115 .access = PL2_RW, 5116 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) }, 5117 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64, 5118 .type = ARM_CP_ALIAS, 5119 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3, 5120 .access = PL2_RW, 5121 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) }, 5122 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64, 5123 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1, 5124 .resetvalue = 0, 5125 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) }, 5126 { .name = "SDCR", .type = ARM_CP_ALIAS, 5127 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1, 5128 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5129 .writefn = sdcr_write, 5130 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) }, 5131 REGINFO_SENTINEL 5132 }; 5133 5134 /* Used to describe the behaviour of EL2 regs when EL2 does not exist. */ 5135 static const ARMCPRegInfo el3_no_el2_cp_reginfo[] = { 5136 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 5137 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 5138 .access = PL2_RW, 5139 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore }, 5140 { .name = "HCR_EL2", .state = ARM_CP_STATE_BOTH, 5141 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 5142 .access = PL2_RW, 5143 .type = ARM_CP_CONST, .resetvalue = 0 }, 5144 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH, 5145 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7, 5146 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5147 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 5148 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 5149 .access = PL2_RW, 5150 .type = ARM_CP_CONST, .resetvalue = 0 }, 5151 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 5152 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 5153 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5154 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 5155 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 5156 .access = PL2_RW, .type = ARM_CP_CONST, 5157 .resetvalue = 0 }, 5158 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 5159 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 5160 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5161 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 5162 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 5163 .access = PL2_RW, .type = ARM_CP_CONST, 5164 .resetvalue = 0 }, 5165 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 5166 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 5167 .access = PL2_RW, .type = ARM_CP_CONST, 5168 .resetvalue = 0 }, 5169 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 5170 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 5171 .access = PL2_RW, .type = ARM_CP_CONST, 5172 .resetvalue = 0 }, 5173 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 5174 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 5175 .access = PL2_RW, .type = ARM_CP_CONST, 5176 .resetvalue = 0 }, 5177 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 5178 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 5179 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5180 { .name = "VTCR_EL2", .state = ARM_CP_STATE_BOTH, 5181 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 5182 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5183 .type = ARM_CP_CONST, .resetvalue = 0 }, 5184 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 5185 .cp = 15, .opc1 = 6, .crm = 2, 5186 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5187 .type = ARM_CP_CONST | ARM_CP_64BIT, .resetvalue = 0 }, 5188 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 5189 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 5190 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5191 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 5192 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 5193 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5194 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 5195 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 5196 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5197 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 5198 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 5199 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5200 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 5201 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 5202 .resetvalue = 0 }, 5203 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 5204 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 5205 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5206 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 5207 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 5208 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5209 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 5210 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 5211 .resetvalue = 0 }, 5212 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 5213 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 5214 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5215 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 5216 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_CONST, 5217 .resetvalue = 0 }, 5218 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 5219 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 5220 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5221 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 5222 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 5223 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5224 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 5225 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 5226 .access = PL2_RW, .accessfn = access_tda, 5227 .type = ARM_CP_CONST, .resetvalue = 0 }, 5228 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_BOTH, 5229 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 5230 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5231 .type = ARM_CP_CONST, .resetvalue = 0 }, 5232 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 5233 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 5234 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5235 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 5236 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 5237 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5238 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 5239 .type = ARM_CP_CONST, 5240 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 5241 .access = PL2_RW, .resetvalue = 0 }, 5242 REGINFO_SENTINEL 5243 }; 5244 5245 /* Ditto, but for registers which exist in ARMv8 but not v7 */ 5246 static const ARMCPRegInfo el3_no_el2_v8_cp_reginfo[] = { 5247 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 5248 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 5249 .access = PL2_RW, 5250 .type = ARM_CP_CONST, .resetvalue = 0 }, 5251 REGINFO_SENTINEL 5252 }; 5253 5254 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask) 5255 { 5256 ARMCPU *cpu = env_archcpu(env); 5257 5258 if (arm_feature(env, ARM_FEATURE_V8)) { 5259 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */ 5260 } else { 5261 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */ 5262 } 5263 5264 if (arm_feature(env, ARM_FEATURE_EL3)) { 5265 valid_mask &= ~HCR_HCD; 5266 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) { 5267 /* Architecturally HCR.TSC is RES0 if EL3 is not implemented. 5268 * However, if we're using the SMC PSCI conduit then QEMU is 5269 * effectively acting like EL3 firmware and so the guest at 5270 * EL2 should retain the ability to prevent EL1 from being 5271 * able to make SMC calls into the ersatz firmware, so in 5272 * that case HCR.TSC should be read/write. 5273 */ 5274 valid_mask &= ~HCR_TSC; 5275 } 5276 5277 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5278 if (cpu_isar_feature(aa64_vh, cpu)) { 5279 valid_mask |= HCR_E2H; 5280 } 5281 if (cpu_isar_feature(aa64_lor, cpu)) { 5282 valid_mask |= HCR_TLOR; 5283 } 5284 if (cpu_isar_feature(aa64_pauth, cpu)) { 5285 valid_mask |= HCR_API | HCR_APK; 5286 } 5287 if (cpu_isar_feature(aa64_mte, cpu)) { 5288 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5; 5289 } 5290 } 5291 5292 /* Clear RES0 bits. */ 5293 value &= valid_mask; 5294 5295 /* 5296 * These bits change the MMU setup: 5297 * HCR_VM enables stage 2 translation 5298 * HCR_PTW forbids certain page-table setups 5299 * HCR_DC disables stage1 and enables stage2 translation 5300 * HCR_DCT enables tagging on (disabled) stage1 translation 5301 */ 5302 if ((env->cp15.hcr_el2 ^ value) & (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT)) { 5303 tlb_flush(CPU(cpu)); 5304 } 5305 env->cp15.hcr_el2 = value; 5306 5307 /* 5308 * Updates to VI and VF require us to update the status of 5309 * virtual interrupts, which are the logical OR of these bits 5310 * and the state of the input lines from the GIC. (This requires 5311 * that we have the iothread lock, which is done by marking the 5312 * reginfo structs as ARM_CP_IO.) 5313 * Note that if a write to HCR pends a VIRQ or VFIQ it is never 5314 * possible for it to be taken immediately, because VIRQ and 5315 * VFIQ are masked unless running at EL0 or EL1, and HCR 5316 * can only be written at EL2. 5317 */ 5318 g_assert(qemu_mutex_iothread_locked()); 5319 arm_cpu_update_virq(cpu); 5320 arm_cpu_update_vfiq(cpu); 5321 } 5322 5323 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 5324 { 5325 do_hcr_write(env, value, 0); 5326 } 5327 5328 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri, 5329 uint64_t value) 5330 { 5331 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */ 5332 value = deposit64(env->cp15.hcr_el2, 32, 32, value); 5333 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32)); 5334 } 5335 5336 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri, 5337 uint64_t value) 5338 { 5339 /* Handle HCR write, i.e. write to low half of HCR_EL2 */ 5340 value = deposit64(env->cp15.hcr_el2, 0, 32, value); 5341 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32)); 5342 } 5343 5344 /* 5345 * Return the effective value of HCR_EL2. 5346 * Bits that are not included here: 5347 * RW (read from SCR_EL3.RW as needed) 5348 */ 5349 uint64_t arm_hcr_el2_eff(CPUARMState *env) 5350 { 5351 uint64_t ret = env->cp15.hcr_el2; 5352 5353 if (arm_is_secure_below_el3(env)) { 5354 /* 5355 * "This register has no effect if EL2 is not enabled in the 5356 * current Security state". This is ARMv8.4-SecEL2 speak for 5357 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1). 5358 * 5359 * Prior to that, the language was "In an implementation that 5360 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves 5361 * as if this field is 0 for all purposes other than a direct 5362 * read or write access of HCR_EL2". With lots of enumeration 5363 * on a per-field basis. In current QEMU, this is condition 5364 * is arm_is_secure_below_el3. 5365 * 5366 * Since the v8.4 language applies to the entire register, and 5367 * appears to be backward compatible, use that. 5368 */ 5369 return 0; 5370 } 5371 5372 /* 5373 * For a cpu that supports both aarch64 and aarch32, we can set bits 5374 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32. 5375 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32. 5376 */ 5377 if (!arm_el_is_aa64(env, 2)) { 5378 uint64_t aa32_valid; 5379 5380 /* 5381 * These bits are up-to-date as of ARMv8.6. 5382 * For HCR, it's easiest to list just the 2 bits that are invalid. 5383 * For HCR2, list those that are valid. 5384 */ 5385 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ); 5386 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE | 5387 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS); 5388 ret &= aa32_valid; 5389 } 5390 5391 if (ret & HCR_TGE) { 5392 /* These bits are up-to-date as of ARMv8.6. */ 5393 if (ret & HCR_E2H) { 5394 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO | 5395 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE | 5396 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU | 5397 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE | 5398 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT | 5399 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5); 5400 } else { 5401 ret |= HCR_FMO | HCR_IMO | HCR_AMO; 5402 } 5403 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE | 5404 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR | 5405 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM | 5406 HCR_TLOR); 5407 } 5408 5409 return ret; 5410 } 5411 5412 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 5413 uint64_t value) 5414 { 5415 /* 5416 * For A-profile AArch32 EL3, if NSACR.CP10 5417 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1. 5418 */ 5419 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 5420 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 5421 value &= ~(0x3 << 10); 5422 value |= env->cp15.cptr_el[2] & (0x3 << 10); 5423 } 5424 env->cp15.cptr_el[2] = value; 5425 } 5426 5427 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri) 5428 { 5429 /* 5430 * For A-profile AArch32 EL3, if NSACR.CP10 5431 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1. 5432 */ 5433 uint64_t value = env->cp15.cptr_el[2]; 5434 5435 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 5436 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 5437 value |= 0x3 << 10; 5438 } 5439 return value; 5440 } 5441 5442 static const ARMCPRegInfo el2_cp_reginfo[] = { 5443 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, 5444 .type = ARM_CP_IO, 5445 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 5446 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 5447 .writefn = hcr_write }, 5448 { .name = "HCR", .state = ARM_CP_STATE_AA32, 5449 .type = ARM_CP_ALIAS | ARM_CP_IO, 5450 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 5451 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 5452 .writefn = hcr_writelow }, 5453 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH, 5454 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7, 5455 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5456 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, 5457 .type = ARM_CP_ALIAS, 5458 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, 5459 .access = PL2_RW, 5460 .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, 5461 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 5462 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 5463 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, 5464 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 5465 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 5466 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, 5467 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 5468 .type = ARM_CP_ALIAS, 5469 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 5470 .access = PL2_RW, 5471 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) }, 5472 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, 5473 .type = ARM_CP_ALIAS, 5474 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, 5475 .access = PL2_RW, 5476 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) }, 5477 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 5478 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 5479 .access = PL2_RW, .writefn = vbar_write, 5480 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), 5481 .resetvalue = 0 }, 5482 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64, 5483 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0, 5484 .access = PL3_RW, .type = ARM_CP_ALIAS, 5485 .fieldoffset = offsetof(CPUARMState, sp_el[2]) }, 5486 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 5487 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 5488 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0, 5489 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]), 5490 .readfn = cptr_el2_read, .writefn = cptr_el2_write }, 5491 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 5492 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 5493 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]), 5494 .resetvalue = 0 }, 5495 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 5496 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 5497 .access = PL2_RW, .type = ARM_CP_ALIAS, 5498 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) }, 5499 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 5500 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 5501 .access = PL2_RW, .type = ARM_CP_CONST, 5502 .resetvalue = 0 }, 5503 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */ 5504 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 5505 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 5506 .access = PL2_RW, .type = ARM_CP_CONST, 5507 .resetvalue = 0 }, 5508 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 5509 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 5510 .access = PL2_RW, .type = ARM_CP_CONST, 5511 .resetvalue = 0 }, 5512 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 5513 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 5514 .access = PL2_RW, .type = ARM_CP_CONST, 5515 .resetvalue = 0 }, 5516 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 5517 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 5518 .access = PL2_RW, .writefn = vmsa_tcr_el12_write, 5519 /* no .raw_writefn or .resetfn needed as we never use mask/base_mask */ 5520 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) }, 5521 { .name = "VTCR", .state = ARM_CP_STATE_AA32, 5522 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 5523 .type = ARM_CP_ALIAS, 5524 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5525 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 5526 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64, 5527 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 5528 .access = PL2_RW, 5529 /* no .writefn needed as this can't cause an ASID change; 5530 * no .raw_writefn or .resetfn needed as we never use mask/base_mask 5531 */ 5532 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 5533 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 5534 .cp = 15, .opc1 = 6, .crm = 2, 5535 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 5536 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5537 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2), 5538 .writefn = vttbr_write }, 5539 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 5540 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 5541 .access = PL2_RW, .writefn = vttbr_write, 5542 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) }, 5543 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 5544 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 5545 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write, 5546 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) }, 5547 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 5548 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 5549 .access = PL2_RW, .resetvalue = 0, 5550 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) }, 5551 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 5552 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 5553 .access = PL2_RW, .resetvalue = 0, .writefn = vmsa_tcr_ttbr_el2_write, 5554 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 5555 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 5556 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 5557 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 5558 { .name = "TLBIALLNSNH", 5559 .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 4, 5560 .type = ARM_CP_NO_RAW, .access = PL2_W, 5561 .writefn = tlbiall_nsnh_write }, 5562 { .name = "TLBIALLNSNHIS", 5563 .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 4, 5564 .type = ARM_CP_NO_RAW, .access = PL2_W, 5565 .writefn = tlbiall_nsnh_is_write }, 5566 { .name = "TLBIALLH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 5567 .type = ARM_CP_NO_RAW, .access = PL2_W, 5568 .writefn = tlbiall_hyp_write }, 5569 { .name = "TLBIALLHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 5570 .type = ARM_CP_NO_RAW, .access = PL2_W, 5571 .writefn = tlbiall_hyp_is_write }, 5572 { .name = "TLBIMVAH", .cp = 15, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 5573 .type = ARM_CP_NO_RAW, .access = PL2_W, 5574 .writefn = tlbimva_hyp_write }, 5575 { .name = "TLBIMVAHIS", .cp = 15, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 5576 .type = ARM_CP_NO_RAW, .access = PL2_W, 5577 .writefn = tlbimva_hyp_is_write }, 5578 { .name = "TLBI_ALLE2", .state = ARM_CP_STATE_AA64, 5579 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 0, 5580 .type = ARM_CP_NO_RAW, .access = PL2_W, 5581 .writefn = tlbi_aa64_alle2_write }, 5582 { .name = "TLBI_VAE2", .state = ARM_CP_STATE_AA64, 5583 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 1, 5584 .type = ARM_CP_NO_RAW, .access = PL2_W, 5585 .writefn = tlbi_aa64_vae2_write }, 5586 { .name = "TLBI_VALE2", .state = ARM_CP_STATE_AA64, 5587 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 7, .opc2 = 5, 5588 .access = PL2_W, .type = ARM_CP_NO_RAW, 5589 .writefn = tlbi_aa64_vae2_write }, 5590 { .name = "TLBI_ALLE2IS", .state = ARM_CP_STATE_AA64, 5591 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 0, 5592 .access = PL2_W, .type = ARM_CP_NO_RAW, 5593 .writefn = tlbi_aa64_alle2is_write }, 5594 { .name = "TLBI_VAE2IS", .state = ARM_CP_STATE_AA64, 5595 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 1, 5596 .type = ARM_CP_NO_RAW, .access = PL2_W, 5597 .writefn = tlbi_aa64_vae2is_write }, 5598 { .name = "TLBI_VALE2IS", .state = ARM_CP_STATE_AA64, 5599 .opc0 = 1, .opc1 = 4, .crn = 8, .crm = 3, .opc2 = 5, 5600 .access = PL2_W, .type = ARM_CP_NO_RAW, 5601 .writefn = tlbi_aa64_vae2is_write }, 5602 #ifndef CONFIG_USER_ONLY 5603 /* Unlike the other EL2-related AT operations, these must 5604 * UNDEF from EL3 if EL2 is not implemented, which is why we 5605 * define them here rather than with the rest of the AT ops. 5606 */ 5607 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64, 5608 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 5609 .access = PL2_W, .accessfn = at_s1e2_access, 5610 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 }, 5611 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64, 5612 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 5613 .access = PL2_W, .accessfn = at_s1e2_access, 5614 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, .writefn = ats_write64 }, 5615 /* The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE 5616 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3 5617 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose 5618 * to behave as if SCR.NS was 1. 5619 */ 5620 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 5621 .access = PL2_W, 5622 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 5623 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 5624 .access = PL2_W, 5625 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 5626 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 5627 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 5628 /* ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the 5629 * reset values as IMPDEF. We choose to reset to 3 to comply with 5630 * both ARMv7 and ARMv8. 5631 */ 5632 .access = PL2_RW, .resetvalue = 3, 5633 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) }, 5634 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 5635 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 5636 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, 5637 .writefn = gt_cntvoff_write, 5638 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 5639 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 5640 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO, 5641 .writefn = gt_cntvoff_write, 5642 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 5643 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 5644 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 5645 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 5646 .type = ARM_CP_IO, .access = PL2_RW, 5647 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 5648 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 5649 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 5650 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO, 5651 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 5652 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 5653 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 5654 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 5655 .resetfn = gt_hyp_timer_reset, 5656 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write }, 5657 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 5658 .type = ARM_CP_IO, 5659 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 5660 .access = PL2_RW, 5661 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl), 5662 .resetvalue = 0, 5663 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write }, 5664 #endif 5665 /* The only field of MDCR_EL2 that has a defined architectural reset value 5666 * is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N; but we 5667 * don't implement any PMU event counters, so using zero as a reset 5668 * value for MDCR_EL2 is okay 5669 */ 5670 { .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, 5671 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 5672 .access = PL2_RW, .resetvalue = 0, 5673 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), }, 5674 { .name = "HPFAR", .state = ARM_CP_STATE_AA32, 5675 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 5676 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5677 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 5678 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64, 5679 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 5680 .access = PL2_RW, 5681 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 5682 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 5683 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 5684 .access = PL2_RW, 5685 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) }, 5686 REGINFO_SENTINEL 5687 }; 5688 5689 static const ARMCPRegInfo el2_v8_cp_reginfo[] = { 5690 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 5691 .type = ARM_CP_ALIAS | ARM_CP_IO, 5692 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 5693 .access = PL2_RW, 5694 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2), 5695 .writefn = hcr_writehigh }, 5696 REGINFO_SENTINEL 5697 }; 5698 5699 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 5700 bool isread) 5701 { 5702 /* The NSACR is RW at EL3, and RO for NS EL1 and NS EL2. 5703 * At Secure EL1 it traps to EL3. 5704 */ 5705 if (arm_current_el(env) == 3) { 5706 return CP_ACCESS_OK; 5707 } 5708 if (arm_is_secure_below_el3(env)) { 5709 return CP_ACCESS_TRAP_EL3; 5710 } 5711 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */ 5712 if (isread) { 5713 return CP_ACCESS_OK; 5714 } 5715 return CP_ACCESS_TRAP_UNCATEGORIZED; 5716 } 5717 5718 static const ARMCPRegInfo el3_cp_reginfo[] = { 5719 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, 5720 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, 5721 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), 5722 .resetvalue = 0, .writefn = scr_write }, 5723 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL, 5724 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, 5725 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5726 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), 5727 .writefn = scr_write }, 5728 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, 5729 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, 5730 .access = PL3_RW, .resetvalue = 0, 5731 .fieldoffset = offsetof(CPUARMState, cp15.sder) }, 5732 { .name = "SDER", 5733 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, 5734 .access = PL3_RW, .resetvalue = 0, 5735 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, 5736 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 5737 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5738 .writefn = vbar_write, .resetvalue = 0, 5739 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, 5740 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, 5741 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, 5742 .access = PL3_RW, .resetvalue = 0, 5743 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, 5744 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, 5745 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, 5746 .access = PL3_RW, 5747 /* no .writefn needed as this can't cause an ASID change; 5748 * we must provide a .raw_writefn and .resetfn because we handle 5749 * reset and migration for the AArch32 TTBCR(S), which might be 5750 * using mask and base_mask. 5751 */ 5752 .resetfn = vmsa_ttbcr_reset, .raw_writefn = vmsa_ttbcr_raw_write, 5753 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, 5754 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, 5755 .type = ARM_CP_ALIAS, 5756 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, 5757 .access = PL3_RW, 5758 .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, 5759 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, 5760 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, 5761 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, 5762 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, 5763 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, 5764 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, 5765 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, 5766 .type = ARM_CP_ALIAS, 5767 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, 5768 .access = PL3_RW, 5769 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) }, 5770 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, 5771 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, 5772 .access = PL3_RW, .writefn = vbar_write, 5773 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), 5774 .resetvalue = 0 }, 5775 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64, 5776 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2, 5777 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0, 5778 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) }, 5779 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64, 5780 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2, 5781 .access = PL3_RW, .resetvalue = 0, 5782 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) }, 5783 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64, 5784 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0, 5785 .access = PL3_RW, .type = ARM_CP_CONST, 5786 .resetvalue = 0 }, 5787 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH, 5788 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0, 5789 .access = PL3_RW, .type = ARM_CP_CONST, 5790 .resetvalue = 0 }, 5791 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH, 5792 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1, 5793 .access = PL3_RW, .type = ARM_CP_CONST, 5794 .resetvalue = 0 }, 5795 { .name = "TLBI_ALLE3IS", .state = ARM_CP_STATE_AA64, 5796 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 0, 5797 .access = PL3_W, .type = ARM_CP_NO_RAW, 5798 .writefn = tlbi_aa64_alle3is_write }, 5799 { .name = "TLBI_VAE3IS", .state = ARM_CP_STATE_AA64, 5800 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 1, 5801 .access = PL3_W, .type = ARM_CP_NO_RAW, 5802 .writefn = tlbi_aa64_vae3is_write }, 5803 { .name = "TLBI_VALE3IS", .state = ARM_CP_STATE_AA64, 5804 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 3, .opc2 = 5, 5805 .access = PL3_W, .type = ARM_CP_NO_RAW, 5806 .writefn = tlbi_aa64_vae3is_write }, 5807 { .name = "TLBI_ALLE3", .state = ARM_CP_STATE_AA64, 5808 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 0, 5809 .access = PL3_W, .type = ARM_CP_NO_RAW, 5810 .writefn = tlbi_aa64_alle3_write }, 5811 { .name = "TLBI_VAE3", .state = ARM_CP_STATE_AA64, 5812 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 1, 5813 .access = PL3_W, .type = ARM_CP_NO_RAW, 5814 .writefn = tlbi_aa64_vae3_write }, 5815 { .name = "TLBI_VALE3", .state = ARM_CP_STATE_AA64, 5816 .opc0 = 1, .opc1 = 6, .crn = 8, .crm = 7, .opc2 = 5, 5817 .access = PL3_W, .type = ARM_CP_NO_RAW, 5818 .writefn = tlbi_aa64_vae3_write }, 5819 REGINFO_SENTINEL 5820 }; 5821 5822 #ifndef CONFIG_USER_ONLY 5823 /* Test if system register redirection is to occur in the current state. */ 5824 static bool redirect_for_e2h(CPUARMState *env) 5825 { 5826 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H); 5827 } 5828 5829 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri) 5830 { 5831 CPReadFn *readfn; 5832 5833 if (redirect_for_e2h(env)) { 5834 /* Switch to the saved EL2 version of the register. */ 5835 ri = ri->opaque; 5836 readfn = ri->readfn; 5837 } else { 5838 readfn = ri->orig_readfn; 5839 } 5840 if (readfn == NULL) { 5841 readfn = raw_read; 5842 } 5843 return readfn(env, ri); 5844 } 5845 5846 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri, 5847 uint64_t value) 5848 { 5849 CPWriteFn *writefn; 5850 5851 if (redirect_for_e2h(env)) { 5852 /* Switch to the saved EL2 version of the register. */ 5853 ri = ri->opaque; 5854 writefn = ri->writefn; 5855 } else { 5856 writefn = ri->orig_writefn; 5857 } 5858 if (writefn == NULL) { 5859 writefn = raw_write; 5860 } 5861 writefn(env, ri, value); 5862 } 5863 5864 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu) 5865 { 5866 struct E2HAlias { 5867 uint32_t src_key, dst_key, new_key; 5868 const char *src_name, *dst_name, *new_name; 5869 bool (*feature)(const ARMISARegisters *id); 5870 }; 5871 5872 #define K(op0, op1, crn, crm, op2) \ 5873 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2) 5874 5875 static const struct E2HAlias aliases[] = { 5876 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0), 5877 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" }, 5878 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2), 5879 "CPACR", "CPTR_EL2", "CPACR_EL12" }, 5880 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0), 5881 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" }, 5882 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1), 5883 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" }, 5884 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2), 5885 "TCR_EL1", "TCR_EL2", "TCR_EL12" }, 5886 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0), 5887 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" }, 5888 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1), 5889 "ELR_EL1", "ELR_EL2", "ELR_EL12" }, 5890 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0), 5891 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" }, 5892 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1), 5893 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" }, 5894 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0), 5895 "ESR_EL1", "ESR_EL2", "ESR_EL12" }, 5896 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0), 5897 "FAR_EL1", "FAR_EL2", "FAR_EL12" }, 5898 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0), 5899 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" }, 5900 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0), 5901 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" }, 5902 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0), 5903 "VBAR", "VBAR_EL2", "VBAR_EL12" }, 5904 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1), 5905 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" }, 5906 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0), 5907 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" }, 5908 5909 /* 5910 * Note that redirection of ZCR is mentioned in the description 5911 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but 5912 * not in the summary table. 5913 */ 5914 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0), 5915 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve }, 5916 5917 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0), 5918 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte }, 5919 5920 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */ 5921 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */ 5922 }; 5923 #undef K 5924 5925 size_t i; 5926 5927 for (i = 0; i < ARRAY_SIZE(aliases); i++) { 5928 const struct E2HAlias *a = &aliases[i]; 5929 ARMCPRegInfo *src_reg, *dst_reg; 5930 5931 if (a->feature && !a->feature(&cpu->isar)) { 5932 continue; 5933 } 5934 5935 src_reg = g_hash_table_lookup(cpu->cp_regs, &a->src_key); 5936 dst_reg = g_hash_table_lookup(cpu->cp_regs, &a->dst_key); 5937 g_assert(src_reg != NULL); 5938 g_assert(dst_reg != NULL); 5939 5940 /* Cross-compare names to detect typos in the keys. */ 5941 g_assert(strcmp(src_reg->name, a->src_name) == 0); 5942 g_assert(strcmp(dst_reg->name, a->dst_name) == 0); 5943 5944 /* None of the core system registers use opaque; we will. */ 5945 g_assert(src_reg->opaque == NULL); 5946 5947 /* Create alias before redirection so we dup the right data. */ 5948 if (a->new_key) { 5949 ARMCPRegInfo *new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo)); 5950 uint32_t *new_key = g_memdup(&a->new_key, sizeof(uint32_t)); 5951 bool ok; 5952 5953 new_reg->name = a->new_name; 5954 new_reg->type |= ARM_CP_ALIAS; 5955 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */ 5956 new_reg->access &= PL2_RW | PL3_RW; 5957 5958 ok = g_hash_table_insert(cpu->cp_regs, new_key, new_reg); 5959 g_assert(ok); 5960 } 5961 5962 src_reg->opaque = dst_reg; 5963 src_reg->orig_readfn = src_reg->readfn ?: raw_read; 5964 src_reg->orig_writefn = src_reg->writefn ?: raw_write; 5965 if (!src_reg->raw_readfn) { 5966 src_reg->raw_readfn = raw_read; 5967 } 5968 if (!src_reg->raw_writefn) { 5969 src_reg->raw_writefn = raw_write; 5970 } 5971 src_reg->readfn = el2_e2h_read; 5972 src_reg->writefn = el2_e2h_write; 5973 } 5974 } 5975 #endif 5976 5977 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 5978 bool isread) 5979 { 5980 int cur_el = arm_current_el(env); 5981 5982 if (cur_el < 2) { 5983 uint64_t hcr = arm_hcr_el2_eff(env); 5984 5985 if (cur_el == 0) { 5986 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 5987 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) { 5988 return CP_ACCESS_TRAP_EL2; 5989 } 5990 } else { 5991 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) { 5992 return CP_ACCESS_TRAP; 5993 } 5994 if (hcr & HCR_TID2) { 5995 return CP_ACCESS_TRAP_EL2; 5996 } 5997 } 5998 } else if (hcr & HCR_TID2) { 5999 return CP_ACCESS_TRAP_EL2; 6000 } 6001 } 6002 6003 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) { 6004 return CP_ACCESS_TRAP_EL2; 6005 } 6006 6007 return CP_ACCESS_OK; 6008 } 6009 6010 static void oslar_write(CPUARMState *env, const ARMCPRegInfo *ri, 6011 uint64_t value) 6012 { 6013 /* Writes to OSLAR_EL1 may update the OS lock status, which can be 6014 * read via a bit in OSLSR_EL1. 6015 */ 6016 int oslock; 6017 6018 if (ri->state == ARM_CP_STATE_AA32) { 6019 oslock = (value == 0xC5ACCE55); 6020 } else { 6021 oslock = value & 1; 6022 } 6023 6024 env->cp15.oslsr_el1 = deposit32(env->cp15.oslsr_el1, 1, 1, oslock); 6025 } 6026 6027 static const ARMCPRegInfo debug_cp_reginfo[] = { 6028 /* DBGDRAR, DBGDSAR: always RAZ since we don't implement memory mapped 6029 * debug components. The AArch64 version of DBGDRAR is named MDRAR_EL1; 6030 * unlike DBGDRAR it is never accessible from EL0. 6031 * DBGDSAR is deprecated and must RAZ from v8 anyway, so it has no AArch64 6032 * accessor. 6033 */ 6034 { .name = "DBGDRAR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 0, 6035 .access = PL0_R, .accessfn = access_tdra, 6036 .type = ARM_CP_CONST, .resetvalue = 0 }, 6037 { .name = "MDRAR_EL1", .state = ARM_CP_STATE_AA64, 6038 .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 6039 .access = PL1_R, .accessfn = access_tdra, 6040 .type = ARM_CP_CONST, .resetvalue = 0 }, 6041 { .name = "DBGDSAR", .cp = 14, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 6042 .access = PL0_R, .accessfn = access_tdra, 6043 .type = ARM_CP_CONST, .resetvalue = 0 }, 6044 /* Monitor debug system control register; the 32-bit alias is DBGDSCRext. */ 6045 { .name = "MDSCR_EL1", .state = ARM_CP_STATE_BOTH, 6046 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 6047 .access = PL1_RW, .accessfn = access_tda, 6048 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), 6049 .resetvalue = 0 }, 6050 /* MDCCSR_EL0, aka DBGDSCRint. This is a read-only mirror of MDSCR_EL1. 6051 * We don't implement the configurable EL0 access. 6052 */ 6053 { .name = "MDCCSR_EL0", .state = ARM_CP_STATE_BOTH, 6054 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 6055 .type = ARM_CP_ALIAS, 6056 .access = PL1_R, .accessfn = access_tda, 6057 .fieldoffset = offsetof(CPUARMState, cp15.mdscr_el1), }, 6058 { .name = "OSLAR_EL1", .state = ARM_CP_STATE_BOTH, 6059 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 4, 6060 .access = PL1_W, .type = ARM_CP_NO_RAW, 6061 .accessfn = access_tdosa, 6062 .writefn = oslar_write }, 6063 { .name = "OSLSR_EL1", .state = ARM_CP_STATE_BOTH, 6064 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 4, 6065 .access = PL1_R, .resetvalue = 10, 6066 .accessfn = access_tdosa, 6067 .fieldoffset = offsetof(CPUARMState, cp15.oslsr_el1) }, 6068 /* Dummy OSDLR_EL1: 32-bit Linux will read this */ 6069 { .name = "OSDLR_EL1", .state = ARM_CP_STATE_BOTH, 6070 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 4, 6071 .access = PL1_RW, .accessfn = access_tdosa, 6072 .type = ARM_CP_NOP }, 6073 /* Dummy DBGVCR: Linux wants to clear this on startup, but we don't 6074 * implement vector catch debug events yet. 6075 */ 6076 { .name = "DBGVCR", 6077 .cp = 14, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 6078 .access = PL1_RW, .accessfn = access_tda, 6079 .type = ARM_CP_NOP }, 6080 /* Dummy DBGVCR32_EL2 (which is only for a 64-bit hypervisor 6081 * to save and restore a 32-bit guest's DBGVCR) 6082 */ 6083 { .name = "DBGVCR32_EL2", .state = ARM_CP_STATE_AA64, 6084 .opc0 = 2, .opc1 = 4, .crn = 0, .crm = 7, .opc2 = 0, 6085 .access = PL2_RW, .accessfn = access_tda, 6086 .type = ARM_CP_NOP }, 6087 /* Dummy MDCCINT_EL1, since we don't implement the Debug Communications 6088 * Channel but Linux may try to access this register. The 32-bit 6089 * alias is DBGDCCINT. 6090 */ 6091 { .name = "MDCCINT_EL1", .state = ARM_CP_STATE_BOTH, 6092 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 6093 .access = PL1_RW, .accessfn = access_tda, 6094 .type = ARM_CP_NOP }, 6095 REGINFO_SENTINEL 6096 }; 6097 6098 static const ARMCPRegInfo debug_lpae_cp_reginfo[] = { 6099 /* 64 bit access versions of the (dummy) debug registers */ 6100 { .name = "DBGDRAR", .cp = 14, .crm = 1, .opc1 = 0, 6101 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 6102 { .name = "DBGDSAR", .cp = 14, .crm = 2, .opc1 = 0, 6103 .access = PL0_R, .type = ARM_CP_CONST|ARM_CP_64BIT, .resetvalue = 0 }, 6104 REGINFO_SENTINEL 6105 }; 6106 6107 /* Return the exception level to which exceptions should be taken 6108 * via SVEAccessTrap. If an exception should be routed through 6109 * AArch64.AdvSIMDFPAccessTrap, return 0; fp_exception_el should 6110 * take care of raising that exception. 6111 * C.f. the ARM pseudocode function CheckSVEEnabled. 6112 */ 6113 int sve_exception_el(CPUARMState *env, int el) 6114 { 6115 #ifndef CONFIG_USER_ONLY 6116 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 6117 6118 if (el <= 1 && (hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 6119 bool disabled = false; 6120 6121 /* The CPACR.ZEN controls traps to EL1: 6122 * 0, 2 : trap EL0 and EL1 accesses 6123 * 1 : trap only EL0 accesses 6124 * 3 : trap no accesses 6125 */ 6126 if (!extract32(env->cp15.cpacr_el1, 16, 1)) { 6127 disabled = true; 6128 } else if (!extract32(env->cp15.cpacr_el1, 17, 1)) { 6129 disabled = el == 0; 6130 } 6131 if (disabled) { 6132 /* route_to_el2 */ 6133 return hcr_el2 & HCR_TGE ? 2 : 1; 6134 } 6135 6136 /* Check CPACR.FPEN. */ 6137 if (!extract32(env->cp15.cpacr_el1, 20, 1)) { 6138 disabled = true; 6139 } else if (!extract32(env->cp15.cpacr_el1, 21, 1)) { 6140 disabled = el == 0; 6141 } 6142 if (disabled) { 6143 return 0; 6144 } 6145 } 6146 6147 /* CPTR_EL2. Since TZ and TFP are positive, 6148 * they will be zero when EL2 is not present. 6149 */ 6150 if (el <= 2 && !arm_is_secure_below_el3(env)) { 6151 if (env->cp15.cptr_el[2] & CPTR_TZ) { 6152 return 2; 6153 } 6154 if (env->cp15.cptr_el[2] & CPTR_TFP) { 6155 return 0; 6156 } 6157 } 6158 6159 /* CPTR_EL3. Since EZ is negative we must check for EL3. */ 6160 if (arm_feature(env, ARM_FEATURE_EL3) 6161 && !(env->cp15.cptr_el[3] & CPTR_EZ)) { 6162 return 3; 6163 } 6164 #endif 6165 return 0; 6166 } 6167 6168 static uint32_t sve_zcr_get_valid_len(ARMCPU *cpu, uint32_t start_len) 6169 { 6170 uint32_t end_len; 6171 6172 end_len = start_len &= 0xf; 6173 if (!test_bit(start_len, cpu->sve_vq_map)) { 6174 end_len = find_last_bit(cpu->sve_vq_map, start_len); 6175 assert(end_len < start_len); 6176 } 6177 return end_len; 6178 } 6179 6180 /* 6181 * Given that SVE is enabled, return the vector length for EL. 6182 */ 6183 uint32_t sve_zcr_len_for_el(CPUARMState *env, int el) 6184 { 6185 ARMCPU *cpu = env_archcpu(env); 6186 uint32_t zcr_len = cpu->sve_max_vq - 1; 6187 6188 if (el <= 1) { 6189 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[1]); 6190 } 6191 if (el <= 2 && arm_feature(env, ARM_FEATURE_EL2)) { 6192 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[2]); 6193 } 6194 if (arm_feature(env, ARM_FEATURE_EL3)) { 6195 zcr_len = MIN(zcr_len, 0xf & (uint32_t)env->vfp.zcr_el[3]); 6196 } 6197 6198 return sve_zcr_get_valid_len(cpu, zcr_len); 6199 } 6200 6201 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6202 uint64_t value) 6203 { 6204 int cur_el = arm_current_el(env); 6205 int old_len = sve_zcr_len_for_el(env, cur_el); 6206 int new_len; 6207 6208 /* Bits other than [3:0] are RAZ/WI. */ 6209 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16); 6210 raw_write(env, ri, value & 0xf); 6211 6212 /* 6213 * Because we arrived here, we know both FP and SVE are enabled; 6214 * otherwise we would have trapped access to the ZCR_ELn register. 6215 */ 6216 new_len = sve_zcr_len_for_el(env, cur_el); 6217 if (new_len < old_len) { 6218 aarch64_sve_narrow_vq(env, new_len + 1); 6219 } 6220 } 6221 6222 static const ARMCPRegInfo zcr_el1_reginfo = { 6223 .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64, 6224 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0, 6225 .access = PL1_RW, .type = ARM_CP_SVE, 6226 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]), 6227 .writefn = zcr_write, .raw_writefn = raw_write 6228 }; 6229 6230 static const ARMCPRegInfo zcr_el2_reginfo = { 6231 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 6232 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 6233 .access = PL2_RW, .type = ARM_CP_SVE, 6234 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]), 6235 .writefn = zcr_write, .raw_writefn = raw_write 6236 }; 6237 6238 static const ARMCPRegInfo zcr_no_el2_reginfo = { 6239 .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 6240 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 6241 .access = PL2_RW, .type = ARM_CP_SVE, 6242 .readfn = arm_cp_read_zero, .writefn = arm_cp_write_ignore 6243 }; 6244 6245 static const ARMCPRegInfo zcr_el3_reginfo = { 6246 .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64, 6247 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0, 6248 .access = PL3_RW, .type = ARM_CP_SVE, 6249 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]), 6250 .writefn = zcr_write, .raw_writefn = raw_write 6251 }; 6252 6253 void hw_watchpoint_update(ARMCPU *cpu, int n) 6254 { 6255 CPUARMState *env = &cpu->env; 6256 vaddr len = 0; 6257 vaddr wvr = env->cp15.dbgwvr[n]; 6258 uint64_t wcr = env->cp15.dbgwcr[n]; 6259 int mask; 6260 int flags = BP_CPU | BP_STOP_BEFORE_ACCESS; 6261 6262 if (env->cpu_watchpoint[n]) { 6263 cpu_watchpoint_remove_by_ref(CPU(cpu), env->cpu_watchpoint[n]); 6264 env->cpu_watchpoint[n] = NULL; 6265 } 6266 6267 if (!extract64(wcr, 0, 1)) { 6268 /* E bit clear : watchpoint disabled */ 6269 return; 6270 } 6271 6272 switch (extract64(wcr, 3, 2)) { 6273 case 0: 6274 /* LSC 00 is reserved and must behave as if the wp is disabled */ 6275 return; 6276 case 1: 6277 flags |= BP_MEM_READ; 6278 break; 6279 case 2: 6280 flags |= BP_MEM_WRITE; 6281 break; 6282 case 3: 6283 flags |= BP_MEM_ACCESS; 6284 break; 6285 } 6286 6287 /* Attempts to use both MASK and BAS fields simultaneously are 6288 * CONSTRAINED UNPREDICTABLE; we opt to ignore BAS in this case, 6289 * thus generating a watchpoint for every byte in the masked region. 6290 */ 6291 mask = extract64(wcr, 24, 4); 6292 if (mask == 1 || mask == 2) { 6293 /* Reserved values of MASK; we must act as if the mask value was 6294 * some non-reserved value, or as if the watchpoint were disabled. 6295 * We choose the latter. 6296 */ 6297 return; 6298 } else if (mask) { 6299 /* Watchpoint covers an aligned area up to 2GB in size */ 6300 len = 1ULL << mask; 6301 /* If masked bits in WVR are not zero it's CONSTRAINED UNPREDICTABLE 6302 * whether the watchpoint fires when the unmasked bits match; we opt 6303 * to generate the exceptions. 6304 */ 6305 wvr &= ~(len - 1); 6306 } else { 6307 /* Watchpoint covers bytes defined by the byte address select bits */ 6308 int bas = extract64(wcr, 5, 8); 6309 int basstart; 6310 6311 if (extract64(wvr, 2, 1)) { 6312 /* Deprecated case of an only 4-aligned address. BAS[7:4] are 6313 * ignored, and BAS[3:0] define which bytes to watch. 6314 */ 6315 bas &= 0xf; 6316 } 6317 6318 if (bas == 0) { 6319 /* This must act as if the watchpoint is disabled */ 6320 return; 6321 } 6322 6323 /* The BAS bits are supposed to be programmed to indicate a contiguous 6324 * range of bytes. Otherwise it is CONSTRAINED UNPREDICTABLE whether 6325 * we fire for each byte in the word/doubleword addressed by the WVR. 6326 * We choose to ignore any non-zero bits after the first range of 1s. 6327 */ 6328 basstart = ctz32(bas); 6329 len = cto32(bas >> basstart); 6330 wvr += basstart; 6331 } 6332 6333 cpu_watchpoint_insert(CPU(cpu), wvr, len, flags, 6334 &env->cpu_watchpoint[n]); 6335 } 6336 6337 void hw_watchpoint_update_all(ARMCPU *cpu) 6338 { 6339 int i; 6340 CPUARMState *env = &cpu->env; 6341 6342 /* Completely clear out existing QEMU watchpoints and our array, to 6343 * avoid possible stale entries following migration load. 6344 */ 6345 cpu_watchpoint_remove_all(CPU(cpu), BP_CPU); 6346 memset(env->cpu_watchpoint, 0, sizeof(env->cpu_watchpoint)); 6347 6348 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_watchpoint); i++) { 6349 hw_watchpoint_update(cpu, i); 6350 } 6351 } 6352 6353 static void dbgwvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6354 uint64_t value) 6355 { 6356 ARMCPU *cpu = env_archcpu(env); 6357 int i = ri->crm; 6358 6359 /* Bits [63:49] are hardwired to the value of bit [48]; that is, the 6360 * register reads and behaves as if values written are sign extended. 6361 * Bits [1:0] are RES0. 6362 */ 6363 value = sextract64(value, 0, 49) & ~3ULL; 6364 6365 raw_write(env, ri, value); 6366 hw_watchpoint_update(cpu, i); 6367 } 6368 6369 static void dbgwcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6370 uint64_t value) 6371 { 6372 ARMCPU *cpu = env_archcpu(env); 6373 int i = ri->crm; 6374 6375 raw_write(env, ri, value); 6376 hw_watchpoint_update(cpu, i); 6377 } 6378 6379 void hw_breakpoint_update(ARMCPU *cpu, int n) 6380 { 6381 CPUARMState *env = &cpu->env; 6382 uint64_t bvr = env->cp15.dbgbvr[n]; 6383 uint64_t bcr = env->cp15.dbgbcr[n]; 6384 vaddr addr; 6385 int bt; 6386 int flags = BP_CPU; 6387 6388 if (env->cpu_breakpoint[n]) { 6389 cpu_breakpoint_remove_by_ref(CPU(cpu), env->cpu_breakpoint[n]); 6390 env->cpu_breakpoint[n] = NULL; 6391 } 6392 6393 if (!extract64(bcr, 0, 1)) { 6394 /* E bit clear : watchpoint disabled */ 6395 return; 6396 } 6397 6398 bt = extract64(bcr, 20, 4); 6399 6400 switch (bt) { 6401 case 4: /* unlinked address mismatch (reserved if AArch64) */ 6402 case 5: /* linked address mismatch (reserved if AArch64) */ 6403 qemu_log_mask(LOG_UNIMP, 6404 "arm: address mismatch breakpoint types not implemented\n"); 6405 return; 6406 case 0: /* unlinked address match */ 6407 case 1: /* linked address match */ 6408 { 6409 /* Bits [63:49] are hardwired to the value of bit [48]; that is, 6410 * we behave as if the register was sign extended. Bits [1:0] are 6411 * RES0. The BAS field is used to allow setting breakpoints on 16 6412 * bit wide instructions; it is CONSTRAINED UNPREDICTABLE whether 6413 * a bp will fire if the addresses covered by the bp and the addresses 6414 * covered by the insn overlap but the insn doesn't start at the 6415 * start of the bp address range. We choose to require the insn and 6416 * the bp to have the same address. The constraints on writing to 6417 * BAS enforced in dbgbcr_write mean we have only four cases: 6418 * 0b0000 => no breakpoint 6419 * 0b0011 => breakpoint on addr 6420 * 0b1100 => breakpoint on addr + 2 6421 * 0b1111 => breakpoint on addr 6422 * See also figure D2-3 in the v8 ARM ARM (DDI0487A.c). 6423 */ 6424 int bas = extract64(bcr, 5, 4); 6425 addr = sextract64(bvr, 0, 49) & ~3ULL; 6426 if (bas == 0) { 6427 return; 6428 } 6429 if (bas == 0xc) { 6430 addr += 2; 6431 } 6432 break; 6433 } 6434 case 2: /* unlinked context ID match */ 6435 case 8: /* unlinked VMID match (reserved if no EL2) */ 6436 case 10: /* unlinked context ID and VMID match (reserved if no EL2) */ 6437 qemu_log_mask(LOG_UNIMP, 6438 "arm: unlinked context breakpoint types not implemented\n"); 6439 return; 6440 case 9: /* linked VMID match (reserved if no EL2) */ 6441 case 11: /* linked context ID and VMID match (reserved if no EL2) */ 6442 case 3: /* linked context ID match */ 6443 default: 6444 /* We must generate no events for Linked context matches (unless 6445 * they are linked to by some other bp/wp, which is handled in 6446 * updates for the linking bp/wp). We choose to also generate no events 6447 * for reserved values. 6448 */ 6449 return; 6450 } 6451 6452 cpu_breakpoint_insert(CPU(cpu), addr, flags, &env->cpu_breakpoint[n]); 6453 } 6454 6455 void hw_breakpoint_update_all(ARMCPU *cpu) 6456 { 6457 int i; 6458 CPUARMState *env = &cpu->env; 6459 6460 /* Completely clear out existing QEMU breakpoints and our array, to 6461 * avoid possible stale entries following migration load. 6462 */ 6463 cpu_breakpoint_remove_all(CPU(cpu), BP_CPU); 6464 memset(env->cpu_breakpoint, 0, sizeof(env->cpu_breakpoint)); 6465 6466 for (i = 0; i < ARRAY_SIZE(cpu->env.cpu_breakpoint); i++) { 6467 hw_breakpoint_update(cpu, i); 6468 } 6469 } 6470 6471 static void dbgbvr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6472 uint64_t value) 6473 { 6474 ARMCPU *cpu = env_archcpu(env); 6475 int i = ri->crm; 6476 6477 raw_write(env, ri, value); 6478 hw_breakpoint_update(cpu, i); 6479 } 6480 6481 static void dbgbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6482 uint64_t value) 6483 { 6484 ARMCPU *cpu = env_archcpu(env); 6485 int i = ri->crm; 6486 6487 /* BAS[3] is a read-only copy of BAS[2], and BAS[1] a read-only 6488 * copy of BAS[0]. 6489 */ 6490 value = deposit64(value, 6, 1, extract64(value, 5, 1)); 6491 value = deposit64(value, 8, 1, extract64(value, 7, 1)); 6492 6493 raw_write(env, ri, value); 6494 hw_breakpoint_update(cpu, i); 6495 } 6496 6497 static void define_debug_regs(ARMCPU *cpu) 6498 { 6499 /* Define v7 and v8 architectural debug registers. 6500 * These are just dummy implementations for now. 6501 */ 6502 int i; 6503 int wrps, brps, ctx_cmps; 6504 ARMCPRegInfo dbgdidr = { 6505 .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 6506 .access = PL0_R, .accessfn = access_tda, 6507 .type = ARM_CP_CONST, .resetvalue = cpu->isar.dbgdidr, 6508 }; 6509 6510 /* Note that all these register fields hold "number of Xs minus 1". */ 6511 brps = arm_num_brps(cpu); 6512 wrps = arm_num_wrps(cpu); 6513 ctx_cmps = arm_num_ctx_cmps(cpu); 6514 6515 assert(ctx_cmps <= brps); 6516 6517 define_one_arm_cp_reg(cpu, &dbgdidr); 6518 define_arm_cp_regs(cpu, debug_cp_reginfo); 6519 6520 if (arm_feature(&cpu->env, ARM_FEATURE_LPAE)) { 6521 define_arm_cp_regs(cpu, debug_lpae_cp_reginfo); 6522 } 6523 6524 for (i = 0; i < brps; i++) { 6525 ARMCPRegInfo dbgregs[] = { 6526 { .name = "DBGBVR", .state = ARM_CP_STATE_BOTH, 6527 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 4, 6528 .access = PL1_RW, .accessfn = access_tda, 6529 .fieldoffset = offsetof(CPUARMState, cp15.dbgbvr[i]), 6530 .writefn = dbgbvr_write, .raw_writefn = raw_write 6531 }, 6532 { .name = "DBGBCR", .state = ARM_CP_STATE_BOTH, 6533 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 5, 6534 .access = PL1_RW, .accessfn = access_tda, 6535 .fieldoffset = offsetof(CPUARMState, cp15.dbgbcr[i]), 6536 .writefn = dbgbcr_write, .raw_writefn = raw_write 6537 }, 6538 REGINFO_SENTINEL 6539 }; 6540 define_arm_cp_regs(cpu, dbgregs); 6541 } 6542 6543 for (i = 0; i < wrps; i++) { 6544 ARMCPRegInfo dbgregs[] = { 6545 { .name = "DBGWVR", .state = ARM_CP_STATE_BOTH, 6546 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 6, 6547 .access = PL1_RW, .accessfn = access_tda, 6548 .fieldoffset = offsetof(CPUARMState, cp15.dbgwvr[i]), 6549 .writefn = dbgwvr_write, .raw_writefn = raw_write 6550 }, 6551 { .name = "DBGWCR", .state = ARM_CP_STATE_BOTH, 6552 .cp = 14, .opc0 = 2, .opc1 = 0, .crn = 0, .crm = i, .opc2 = 7, 6553 .access = PL1_RW, .accessfn = access_tda, 6554 .fieldoffset = offsetof(CPUARMState, cp15.dbgwcr[i]), 6555 .writefn = dbgwcr_write, .raw_writefn = raw_write 6556 }, 6557 REGINFO_SENTINEL 6558 }; 6559 define_arm_cp_regs(cpu, dbgregs); 6560 } 6561 } 6562 6563 static void define_pmu_regs(ARMCPU *cpu) 6564 { 6565 /* 6566 * v7 performance monitor control register: same implementor 6567 * field as main ID register, and we implement four counters in 6568 * addition to the cycle count register. 6569 */ 6570 unsigned int i, pmcrn = 4; 6571 ARMCPRegInfo pmcr = { 6572 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, 6573 .access = PL0_RW, 6574 .type = ARM_CP_IO | ARM_CP_ALIAS, 6575 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), 6576 .accessfn = pmreg_access, .writefn = pmcr_write, 6577 .raw_writefn = raw_write, 6578 }; 6579 ARMCPRegInfo pmcr64 = { 6580 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, 6581 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, 6582 .access = PL0_RW, .accessfn = pmreg_access, 6583 .type = ARM_CP_IO, 6584 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), 6585 .resetvalue = (cpu->midr & 0xff000000) | (pmcrn << PMCRN_SHIFT) | 6586 PMCRLC, 6587 .writefn = pmcr_write, .raw_writefn = raw_write, 6588 }; 6589 define_one_arm_cp_reg(cpu, &pmcr); 6590 define_one_arm_cp_reg(cpu, &pmcr64); 6591 for (i = 0; i < pmcrn; i++) { 6592 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i); 6593 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i); 6594 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i); 6595 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i); 6596 ARMCPRegInfo pmev_regs[] = { 6597 { .name = pmevcntr_name, .cp = 15, .crn = 14, 6598 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 6599 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 6600 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 6601 .accessfn = pmreg_access }, 6602 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64, 6603 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)), 6604 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access, 6605 .type = ARM_CP_IO, 6606 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 6607 .raw_readfn = pmevcntr_rawread, 6608 .raw_writefn = pmevcntr_rawwrite }, 6609 { .name = pmevtyper_name, .cp = 15, .crn = 14, 6610 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 6611 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 6612 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 6613 .accessfn = pmreg_access }, 6614 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64, 6615 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)), 6616 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access, 6617 .type = ARM_CP_IO, 6618 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 6619 .raw_writefn = pmevtyper_rawwrite }, 6620 REGINFO_SENTINEL 6621 }; 6622 define_arm_cp_regs(cpu, pmev_regs); 6623 g_free(pmevcntr_name); 6624 g_free(pmevcntr_el0_name); 6625 g_free(pmevtyper_name); 6626 g_free(pmevtyper_el0_name); 6627 } 6628 if (cpu_isar_feature(aa32_pmu_8_1, cpu)) { 6629 ARMCPRegInfo v81_pmu_regs[] = { 6630 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32, 6631 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4, 6632 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6633 .resetvalue = extract64(cpu->pmceid0, 32, 32) }, 6634 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32, 6635 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5, 6636 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6637 .resetvalue = extract64(cpu->pmceid1, 32, 32) }, 6638 REGINFO_SENTINEL 6639 }; 6640 define_arm_cp_regs(cpu, v81_pmu_regs); 6641 } 6642 if (cpu_isar_feature(any_pmu_8_4, cpu)) { 6643 static const ARMCPRegInfo v84_pmmir = { 6644 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH, 6645 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6, 6646 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6647 .resetvalue = 0 6648 }; 6649 define_one_arm_cp_reg(cpu, &v84_pmmir); 6650 } 6651 } 6652 6653 /* We don't know until after realize whether there's a GICv3 6654 * attached, and that is what registers the gicv3 sysregs. 6655 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1 6656 * at runtime. 6657 */ 6658 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri) 6659 { 6660 ARMCPU *cpu = env_archcpu(env); 6661 uint64_t pfr1 = cpu->isar.id_pfr1; 6662 6663 if (env->gicv3state) { 6664 pfr1 |= 1 << 28; 6665 } 6666 return pfr1; 6667 } 6668 6669 #ifndef CONFIG_USER_ONLY 6670 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri) 6671 { 6672 ARMCPU *cpu = env_archcpu(env); 6673 uint64_t pfr0 = cpu->isar.id_aa64pfr0; 6674 6675 if (env->gicv3state) { 6676 pfr0 |= 1 << 24; 6677 } 6678 return pfr0; 6679 } 6680 #endif 6681 6682 /* Shared logic between LORID and the rest of the LOR* registers. 6683 * Secure state exclusion has already been dealt with. 6684 */ 6685 static CPAccessResult access_lor_ns(CPUARMState *env, 6686 const ARMCPRegInfo *ri, bool isread) 6687 { 6688 int el = arm_current_el(env); 6689 6690 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) { 6691 return CP_ACCESS_TRAP_EL2; 6692 } 6693 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) { 6694 return CP_ACCESS_TRAP_EL3; 6695 } 6696 return CP_ACCESS_OK; 6697 } 6698 6699 static CPAccessResult access_lor_other(CPUARMState *env, 6700 const ARMCPRegInfo *ri, bool isread) 6701 { 6702 if (arm_is_secure_below_el3(env)) { 6703 /* Access denied in secure mode. */ 6704 return CP_ACCESS_TRAP; 6705 } 6706 return access_lor_ns(env, ri, isread); 6707 } 6708 6709 /* 6710 * A trivial implementation of ARMv8.1-LOR leaves all of these 6711 * registers fixed at 0, which indicates that there are zero 6712 * supported Limited Ordering regions. 6713 */ 6714 static const ARMCPRegInfo lor_reginfo[] = { 6715 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64, 6716 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0, 6717 .access = PL1_RW, .accessfn = access_lor_other, 6718 .type = ARM_CP_CONST, .resetvalue = 0 }, 6719 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64, 6720 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1, 6721 .access = PL1_RW, .accessfn = access_lor_other, 6722 .type = ARM_CP_CONST, .resetvalue = 0 }, 6723 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64, 6724 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2, 6725 .access = PL1_RW, .accessfn = access_lor_other, 6726 .type = ARM_CP_CONST, .resetvalue = 0 }, 6727 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64, 6728 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3, 6729 .access = PL1_RW, .accessfn = access_lor_other, 6730 .type = ARM_CP_CONST, .resetvalue = 0 }, 6731 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64, 6732 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7, 6733 .access = PL1_R, .accessfn = access_lor_ns, 6734 .type = ARM_CP_CONST, .resetvalue = 0 }, 6735 REGINFO_SENTINEL 6736 }; 6737 6738 #ifdef TARGET_AARCH64 6739 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri, 6740 bool isread) 6741 { 6742 int el = arm_current_el(env); 6743 6744 if (el < 2 && 6745 arm_feature(env, ARM_FEATURE_EL2) && 6746 !(arm_hcr_el2_eff(env) & HCR_APK)) { 6747 return CP_ACCESS_TRAP_EL2; 6748 } 6749 if (el < 3 && 6750 arm_feature(env, ARM_FEATURE_EL3) && 6751 !(env->cp15.scr_el3 & SCR_APK)) { 6752 return CP_ACCESS_TRAP_EL3; 6753 } 6754 return CP_ACCESS_OK; 6755 } 6756 6757 static const ARMCPRegInfo pauth_reginfo[] = { 6758 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 6759 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0, 6760 .access = PL1_RW, .accessfn = access_pauth, 6761 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) }, 6762 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 6763 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1, 6764 .access = PL1_RW, .accessfn = access_pauth, 6765 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) }, 6766 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 6767 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2, 6768 .access = PL1_RW, .accessfn = access_pauth, 6769 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) }, 6770 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 6771 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3, 6772 .access = PL1_RW, .accessfn = access_pauth, 6773 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) }, 6774 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 6775 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0, 6776 .access = PL1_RW, .accessfn = access_pauth, 6777 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) }, 6778 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 6779 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1, 6780 .access = PL1_RW, .accessfn = access_pauth, 6781 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) }, 6782 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 6783 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0, 6784 .access = PL1_RW, .accessfn = access_pauth, 6785 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) }, 6786 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 6787 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1, 6788 .access = PL1_RW, .accessfn = access_pauth, 6789 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) }, 6790 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 6791 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2, 6792 .access = PL1_RW, .accessfn = access_pauth, 6793 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) }, 6794 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 6795 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3, 6796 .access = PL1_RW, .accessfn = access_pauth, 6797 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) }, 6798 REGINFO_SENTINEL 6799 }; 6800 6801 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 6802 { 6803 Error *err = NULL; 6804 uint64_t ret; 6805 6806 /* Success sets NZCV = 0000. */ 6807 env->NF = env->CF = env->VF = 0, env->ZF = 1; 6808 6809 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) { 6810 /* 6811 * ??? Failed, for unknown reasons in the crypto subsystem. 6812 * The best we can do is log the reason and return the 6813 * timed-out indication to the guest. There is no reason 6814 * we know to expect this failure to be transitory, so the 6815 * guest may well hang retrying the operation. 6816 */ 6817 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s", 6818 ri->name, error_get_pretty(err)); 6819 error_free(err); 6820 6821 env->ZF = 0; /* NZCF = 0100 */ 6822 return 0; 6823 } 6824 return ret; 6825 } 6826 6827 /* We do not support re-seeding, so the two registers operate the same. */ 6828 static const ARMCPRegInfo rndr_reginfo[] = { 6829 { .name = "RNDR", .state = ARM_CP_STATE_AA64, 6830 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO, 6831 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0, 6832 .access = PL0_R, .readfn = rndr_readfn }, 6833 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64, 6834 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO, 6835 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1, 6836 .access = PL0_R, .readfn = rndr_readfn }, 6837 REGINFO_SENTINEL 6838 }; 6839 6840 #ifndef CONFIG_USER_ONLY 6841 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque, 6842 uint64_t value) 6843 { 6844 ARMCPU *cpu = env_archcpu(env); 6845 /* CTR_EL0 System register -> DminLine, bits [19:16] */ 6846 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF); 6847 uint64_t vaddr_in = (uint64_t) value; 6848 uint64_t vaddr = vaddr_in & ~(dline_size - 1); 6849 void *haddr; 6850 int mem_idx = cpu_mmu_index(env, false); 6851 6852 /* This won't be crossing page boundaries */ 6853 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC()); 6854 if (haddr) { 6855 6856 ram_addr_t offset; 6857 MemoryRegion *mr; 6858 6859 /* RCU lock is already being held */ 6860 mr = memory_region_from_host(haddr, &offset); 6861 6862 if (mr) { 6863 memory_region_writeback(mr, offset, dline_size); 6864 } 6865 } 6866 } 6867 6868 static const ARMCPRegInfo dcpop_reg[] = { 6869 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64, 6870 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1, 6871 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END, 6872 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn }, 6873 REGINFO_SENTINEL 6874 }; 6875 6876 static const ARMCPRegInfo dcpodp_reg[] = { 6877 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64, 6878 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1, 6879 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END, 6880 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn }, 6881 REGINFO_SENTINEL 6882 }; 6883 #endif /*CONFIG_USER_ONLY*/ 6884 6885 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri, 6886 bool isread) 6887 { 6888 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) { 6889 return CP_ACCESS_TRAP_EL2; 6890 } 6891 6892 return CP_ACCESS_OK; 6893 } 6894 6895 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri, 6896 bool isread) 6897 { 6898 int el = arm_current_el(env); 6899 6900 if (el < 2 && arm_feature(env, ARM_FEATURE_EL2)) { 6901 uint64_t hcr = arm_hcr_el2_eff(env); 6902 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) { 6903 return CP_ACCESS_TRAP_EL2; 6904 } 6905 } 6906 if (el < 3 && 6907 arm_feature(env, ARM_FEATURE_EL3) && 6908 !(env->cp15.scr_el3 & SCR_ATA)) { 6909 return CP_ACCESS_TRAP_EL3; 6910 } 6911 return CP_ACCESS_OK; 6912 } 6913 6914 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri) 6915 { 6916 return env->pstate & PSTATE_TCO; 6917 } 6918 6919 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 6920 { 6921 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO); 6922 } 6923 6924 static const ARMCPRegInfo mte_reginfo[] = { 6925 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64, 6926 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1, 6927 .access = PL1_RW, .accessfn = access_mte, 6928 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) }, 6929 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64, 6930 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0, 6931 .access = PL1_RW, .accessfn = access_mte, 6932 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) }, 6933 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64, 6934 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0, 6935 .access = PL2_RW, .accessfn = access_mte, 6936 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) }, 6937 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64, 6938 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0, 6939 .access = PL3_RW, 6940 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) }, 6941 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64, 6942 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5, 6943 .access = PL1_RW, .accessfn = access_mte, 6944 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) }, 6945 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64, 6946 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6, 6947 .access = PL1_RW, .accessfn = access_mte, 6948 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) }, 6949 { .name = "GMID_EL1", .state = ARM_CP_STATE_AA64, 6950 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4, 6951 .access = PL1_R, .accessfn = access_aa64_tid5, 6952 .type = ARM_CP_CONST, .resetvalue = GMID_EL1_BS }, 6953 { .name = "TCO", .state = ARM_CP_STATE_AA64, 6954 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7, 6955 .type = ARM_CP_NO_RAW, 6956 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write }, 6957 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64, 6958 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3, 6959 .type = ARM_CP_NOP, .access = PL1_W, 6960 .accessfn = aa64_cacheop_poc_access }, 6961 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64, 6962 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4, 6963 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 6964 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64, 6965 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5, 6966 .type = ARM_CP_NOP, .access = PL1_W, 6967 .accessfn = aa64_cacheop_poc_access }, 6968 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64, 6969 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6, 6970 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 6971 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64, 6972 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4, 6973 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 6974 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64, 6975 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6, 6976 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 6977 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64, 6978 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4, 6979 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 6980 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64, 6981 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6, 6982 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 6983 REGINFO_SENTINEL 6984 }; 6985 6986 static const ARMCPRegInfo mte_tco_ro_reginfo[] = { 6987 { .name = "TCO", .state = ARM_CP_STATE_AA64, 6988 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7, 6989 .type = ARM_CP_CONST, .access = PL0_RW, }, 6990 REGINFO_SENTINEL 6991 }; 6992 6993 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = { 6994 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64, 6995 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3, 6996 .type = ARM_CP_NOP, .access = PL0_W, 6997 .accessfn = aa64_cacheop_poc_access }, 6998 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64, 6999 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5, 7000 .type = ARM_CP_NOP, .access = PL0_W, 7001 .accessfn = aa64_cacheop_poc_access }, 7002 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64, 7003 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3, 7004 .type = ARM_CP_NOP, .access = PL0_W, 7005 .accessfn = aa64_cacheop_poc_access }, 7006 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64, 7007 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5, 7008 .type = ARM_CP_NOP, .access = PL0_W, 7009 .accessfn = aa64_cacheop_poc_access }, 7010 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64, 7011 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3, 7012 .type = ARM_CP_NOP, .access = PL0_W, 7013 .accessfn = aa64_cacheop_poc_access }, 7014 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64, 7015 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5, 7016 .type = ARM_CP_NOP, .access = PL0_W, 7017 .accessfn = aa64_cacheop_poc_access }, 7018 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64, 7019 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3, 7020 .type = ARM_CP_NOP, .access = PL0_W, 7021 .accessfn = aa64_cacheop_poc_access }, 7022 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64, 7023 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5, 7024 .type = ARM_CP_NOP, .access = PL0_W, 7025 .accessfn = aa64_cacheop_poc_access }, 7026 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64, 7027 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3, 7028 .access = PL0_W, .type = ARM_CP_DC_GVA, 7029 #ifndef CONFIG_USER_ONLY 7030 /* Avoid overhead of an access check that always passes in user-mode */ 7031 .accessfn = aa64_zva_access, 7032 #endif 7033 }, 7034 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64, 7035 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4, 7036 .access = PL0_W, .type = ARM_CP_DC_GZVA, 7037 #ifndef CONFIG_USER_ONLY 7038 /* Avoid overhead of an access check that always passes in user-mode */ 7039 .accessfn = aa64_zva_access, 7040 #endif 7041 }, 7042 REGINFO_SENTINEL 7043 }; 7044 7045 #endif 7046 7047 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri, 7048 bool isread) 7049 { 7050 int el = arm_current_el(env); 7051 7052 if (el == 0) { 7053 uint64_t sctlr = arm_sctlr(env, el); 7054 if (!(sctlr & SCTLR_EnRCTX)) { 7055 return CP_ACCESS_TRAP; 7056 } 7057 } else if (el == 1) { 7058 uint64_t hcr = arm_hcr_el2_eff(env); 7059 if (hcr & HCR_NV) { 7060 return CP_ACCESS_TRAP_EL2; 7061 } 7062 } 7063 return CP_ACCESS_OK; 7064 } 7065 7066 static const ARMCPRegInfo predinv_reginfo[] = { 7067 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64, 7068 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4, 7069 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7070 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64, 7071 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5, 7072 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7073 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64, 7074 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7, 7075 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7076 /* 7077 * Note the AArch32 opcodes have a different OPC1. 7078 */ 7079 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32, 7080 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4, 7081 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7082 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32, 7083 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5, 7084 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7085 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32, 7086 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7, 7087 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7088 REGINFO_SENTINEL 7089 }; 7090 7091 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri) 7092 { 7093 /* Read the high 32 bits of the current CCSIDR */ 7094 return extract64(ccsidr_read(env, ri), 32, 32); 7095 } 7096 7097 static const ARMCPRegInfo ccsidr2_reginfo[] = { 7098 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH, 7099 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2, 7100 .access = PL1_R, 7101 .accessfn = access_aa64_tid2, 7102 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW }, 7103 REGINFO_SENTINEL 7104 }; 7105 7106 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri, 7107 bool isread) 7108 { 7109 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) { 7110 return CP_ACCESS_TRAP_EL2; 7111 } 7112 7113 return CP_ACCESS_OK; 7114 } 7115 7116 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri, 7117 bool isread) 7118 { 7119 if (arm_feature(env, ARM_FEATURE_V8)) { 7120 return access_aa64_tid3(env, ri, isread); 7121 } 7122 7123 return CP_ACCESS_OK; 7124 } 7125 7126 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri, 7127 bool isread) 7128 { 7129 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) { 7130 return CP_ACCESS_TRAP_EL2; 7131 } 7132 7133 return CP_ACCESS_OK; 7134 } 7135 7136 static const ARMCPRegInfo jazelle_regs[] = { 7137 { .name = "JIDR", 7138 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0, 7139 .access = PL1_R, .accessfn = access_jazelle, 7140 .type = ARM_CP_CONST, .resetvalue = 0 }, 7141 { .name = "JOSCR", 7142 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0, 7143 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 7144 { .name = "JMCR", 7145 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0, 7146 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 7147 REGINFO_SENTINEL 7148 }; 7149 7150 static const ARMCPRegInfo vhe_reginfo[] = { 7151 { .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64, 7152 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1, 7153 .access = PL2_RW, 7154 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) }, 7155 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64, 7156 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1, 7157 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write, 7158 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) }, 7159 #ifndef CONFIG_USER_ONLY 7160 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64, 7161 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2, 7162 .fieldoffset = 7163 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval), 7164 .type = ARM_CP_IO, .access = PL2_RW, 7165 .writefn = gt_hv_cval_write, .raw_writefn = raw_write }, 7166 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 7167 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0, 7168 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 7169 .resetfn = gt_hv_timer_reset, 7170 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write }, 7171 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH, 7172 .type = ARM_CP_IO, 7173 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1, 7174 .access = PL2_RW, 7175 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl), 7176 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write }, 7177 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64, 7178 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1, 7179 .type = ARM_CP_IO | ARM_CP_ALIAS, 7180 .access = PL2_RW, .accessfn = e2h_access, 7181 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 7182 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write }, 7183 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64, 7184 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1, 7185 .type = ARM_CP_IO | ARM_CP_ALIAS, 7186 .access = PL2_RW, .accessfn = e2h_access, 7187 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 7188 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write }, 7189 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64, 7190 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0, 7191 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS, 7192 .access = PL2_RW, .accessfn = e2h_access, 7193 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write }, 7194 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64, 7195 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0, 7196 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS, 7197 .access = PL2_RW, .accessfn = e2h_access, 7198 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write }, 7199 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64, 7200 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2, 7201 .type = ARM_CP_IO | ARM_CP_ALIAS, 7202 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 7203 .access = PL2_RW, .accessfn = e2h_access, 7204 .writefn = gt_phys_cval_write, .raw_writefn = raw_write }, 7205 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64, 7206 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2, 7207 .type = ARM_CP_IO | ARM_CP_ALIAS, 7208 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 7209 .access = PL2_RW, .accessfn = e2h_access, 7210 .writefn = gt_virt_cval_write, .raw_writefn = raw_write }, 7211 #endif 7212 REGINFO_SENTINEL 7213 }; 7214 7215 #ifndef CONFIG_USER_ONLY 7216 static const ARMCPRegInfo ats1e1_reginfo[] = { 7217 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, 7218 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0, 7219 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 7220 .writefn = ats_write64 }, 7221 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, 7222 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1, 7223 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 7224 .writefn = ats_write64 }, 7225 REGINFO_SENTINEL 7226 }; 7227 7228 static const ARMCPRegInfo ats1cp_reginfo[] = { 7229 { .name = "ATS1CPRP", 7230 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0, 7231 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 7232 .writefn = ats_write }, 7233 { .name = "ATS1CPWP", 7234 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1, 7235 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 7236 .writefn = ats_write }, 7237 REGINFO_SENTINEL 7238 }; 7239 #endif 7240 7241 /* 7242 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and 7243 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field 7244 * is non-zero, which is never for ARMv7, optionally in ARMv8 7245 * and mandatorily for ARMv8.2 and up. 7246 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's 7247 * implementation is RAZ/WI we can ignore this detail, as we 7248 * do for ACTLR. 7249 */ 7250 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = { 7251 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32, 7252 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3, 7253 .access = PL1_RW, .accessfn = access_tacr, 7254 .type = ARM_CP_CONST, .resetvalue = 0 }, 7255 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32, 7256 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3, 7257 .access = PL2_RW, .type = ARM_CP_CONST, 7258 .resetvalue = 0 }, 7259 REGINFO_SENTINEL 7260 }; 7261 7262 void register_cp_regs_for_features(ARMCPU *cpu) 7263 { 7264 /* Register all the coprocessor registers based on feature bits */ 7265 CPUARMState *env = &cpu->env; 7266 if (arm_feature(env, ARM_FEATURE_M)) { 7267 /* M profile has no coprocessor registers */ 7268 return; 7269 } 7270 7271 define_arm_cp_regs(cpu, cp_reginfo); 7272 if (!arm_feature(env, ARM_FEATURE_V8)) { 7273 /* Must go early as it is full of wildcards that may be 7274 * overridden by later definitions. 7275 */ 7276 define_arm_cp_regs(cpu, not_v8_cp_reginfo); 7277 } 7278 7279 if (arm_feature(env, ARM_FEATURE_V6)) { 7280 /* The ID registers all have impdef reset values */ 7281 ARMCPRegInfo v6_idregs[] = { 7282 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, 7283 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 7284 .access = PL1_R, .type = ARM_CP_CONST, 7285 .accessfn = access_aa32_tid3, 7286 .resetvalue = cpu->isar.id_pfr0 }, 7287 /* ID_PFR1 is not a plain ARM_CP_CONST because we don't know 7288 * the value of the GIC field until after we define these regs. 7289 */ 7290 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, 7291 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, 7292 .access = PL1_R, .type = ARM_CP_NO_RAW, 7293 .accessfn = access_aa32_tid3, 7294 .readfn = id_pfr1_read, 7295 .writefn = arm_cp_write_ignore }, 7296 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, 7297 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, 7298 .access = PL1_R, .type = ARM_CP_CONST, 7299 .accessfn = access_aa32_tid3, 7300 .resetvalue = cpu->isar.id_dfr0 }, 7301 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, 7302 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, 7303 .access = PL1_R, .type = ARM_CP_CONST, 7304 .accessfn = access_aa32_tid3, 7305 .resetvalue = cpu->id_afr0 }, 7306 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, 7307 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, 7308 .access = PL1_R, .type = ARM_CP_CONST, 7309 .accessfn = access_aa32_tid3, 7310 .resetvalue = cpu->isar.id_mmfr0 }, 7311 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, 7312 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, 7313 .access = PL1_R, .type = ARM_CP_CONST, 7314 .accessfn = access_aa32_tid3, 7315 .resetvalue = cpu->isar.id_mmfr1 }, 7316 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, 7317 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, 7318 .access = PL1_R, .type = ARM_CP_CONST, 7319 .accessfn = access_aa32_tid3, 7320 .resetvalue = cpu->isar.id_mmfr2 }, 7321 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, 7322 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, 7323 .access = PL1_R, .type = ARM_CP_CONST, 7324 .accessfn = access_aa32_tid3, 7325 .resetvalue = cpu->isar.id_mmfr3 }, 7326 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, 7327 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 7328 .access = PL1_R, .type = ARM_CP_CONST, 7329 .accessfn = access_aa32_tid3, 7330 .resetvalue = cpu->isar.id_isar0 }, 7331 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, 7332 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, 7333 .access = PL1_R, .type = ARM_CP_CONST, 7334 .accessfn = access_aa32_tid3, 7335 .resetvalue = cpu->isar.id_isar1 }, 7336 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, 7337 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 7338 .access = PL1_R, .type = ARM_CP_CONST, 7339 .accessfn = access_aa32_tid3, 7340 .resetvalue = cpu->isar.id_isar2 }, 7341 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, 7342 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, 7343 .access = PL1_R, .type = ARM_CP_CONST, 7344 .accessfn = access_aa32_tid3, 7345 .resetvalue = cpu->isar.id_isar3 }, 7346 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, 7347 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, 7348 .access = PL1_R, .type = ARM_CP_CONST, 7349 .accessfn = access_aa32_tid3, 7350 .resetvalue = cpu->isar.id_isar4 }, 7351 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, 7352 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, 7353 .access = PL1_R, .type = ARM_CP_CONST, 7354 .accessfn = access_aa32_tid3, 7355 .resetvalue = cpu->isar.id_isar5 }, 7356 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH, 7357 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6, 7358 .access = PL1_R, .type = ARM_CP_CONST, 7359 .accessfn = access_aa32_tid3, 7360 .resetvalue = cpu->isar.id_mmfr4 }, 7361 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH, 7362 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7, 7363 .access = PL1_R, .type = ARM_CP_CONST, 7364 .accessfn = access_aa32_tid3, 7365 .resetvalue = cpu->isar.id_isar6 }, 7366 REGINFO_SENTINEL 7367 }; 7368 define_arm_cp_regs(cpu, v6_idregs); 7369 define_arm_cp_regs(cpu, v6_cp_reginfo); 7370 } else { 7371 define_arm_cp_regs(cpu, not_v6_cp_reginfo); 7372 } 7373 if (arm_feature(env, ARM_FEATURE_V6K)) { 7374 define_arm_cp_regs(cpu, v6k_cp_reginfo); 7375 } 7376 if (arm_feature(env, ARM_FEATURE_V7MP) && 7377 !arm_feature(env, ARM_FEATURE_PMSA)) { 7378 define_arm_cp_regs(cpu, v7mp_cp_reginfo); 7379 } 7380 if (arm_feature(env, ARM_FEATURE_V7VE)) { 7381 define_arm_cp_regs(cpu, pmovsset_cp_reginfo); 7382 } 7383 if (arm_feature(env, ARM_FEATURE_V7)) { 7384 ARMCPRegInfo clidr = { 7385 .name = "CLIDR", .state = ARM_CP_STATE_BOTH, 7386 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, 7387 .access = PL1_R, .type = ARM_CP_CONST, 7388 .accessfn = access_aa64_tid2, 7389 .resetvalue = cpu->clidr 7390 }; 7391 define_one_arm_cp_reg(cpu, &clidr); 7392 define_arm_cp_regs(cpu, v7_cp_reginfo); 7393 define_debug_regs(cpu); 7394 define_pmu_regs(cpu); 7395 } else { 7396 define_arm_cp_regs(cpu, not_v7_cp_reginfo); 7397 } 7398 if (arm_feature(env, ARM_FEATURE_V8)) { 7399 /* AArch64 ID registers, which all have impdef reset values. 7400 * Note that within the ID register ranges the unused slots 7401 * must all RAZ, not UNDEF; future architecture versions may 7402 * define new registers here. 7403 */ 7404 ARMCPRegInfo v8_idregs[] = { 7405 /* 7406 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system 7407 * emulation because we don't know the right value for the 7408 * GIC field until after we define these regs. 7409 */ 7410 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, 7411 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, 7412 .access = PL1_R, 7413 #ifdef CONFIG_USER_ONLY 7414 .type = ARM_CP_CONST, 7415 .resetvalue = cpu->isar.id_aa64pfr0 7416 #else 7417 .type = ARM_CP_NO_RAW, 7418 .accessfn = access_aa64_tid3, 7419 .readfn = id_aa64pfr0_read, 7420 .writefn = arm_cp_write_ignore 7421 #endif 7422 }, 7423 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, 7424 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, 7425 .access = PL1_R, .type = ARM_CP_CONST, 7426 .accessfn = access_aa64_tid3, 7427 .resetvalue = cpu->isar.id_aa64pfr1}, 7428 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7429 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2, 7430 .access = PL1_R, .type = ARM_CP_CONST, 7431 .accessfn = access_aa64_tid3, 7432 .resetvalue = 0 }, 7433 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7434 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3, 7435 .access = PL1_R, .type = ARM_CP_CONST, 7436 .accessfn = access_aa64_tid3, 7437 .resetvalue = 0 }, 7438 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64, 7439 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4, 7440 .access = PL1_R, .type = ARM_CP_CONST, 7441 .accessfn = access_aa64_tid3, 7442 /* At present, only SVEver == 0 is defined anyway. */ 7443 .resetvalue = 0 }, 7444 { .name = "ID_AA64PFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7445 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5, 7446 .access = PL1_R, .type = ARM_CP_CONST, 7447 .accessfn = access_aa64_tid3, 7448 .resetvalue = 0 }, 7449 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7450 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6, 7451 .access = PL1_R, .type = ARM_CP_CONST, 7452 .accessfn = access_aa64_tid3, 7453 .resetvalue = 0 }, 7454 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7455 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7, 7456 .access = PL1_R, .type = ARM_CP_CONST, 7457 .accessfn = access_aa64_tid3, 7458 .resetvalue = 0 }, 7459 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, 7460 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, 7461 .access = PL1_R, .type = ARM_CP_CONST, 7462 .accessfn = access_aa64_tid3, 7463 .resetvalue = cpu->isar.id_aa64dfr0 }, 7464 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, 7465 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, 7466 .access = PL1_R, .type = ARM_CP_CONST, 7467 .accessfn = access_aa64_tid3, 7468 .resetvalue = cpu->isar.id_aa64dfr1 }, 7469 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7470 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2, 7471 .access = PL1_R, .type = ARM_CP_CONST, 7472 .accessfn = access_aa64_tid3, 7473 .resetvalue = 0 }, 7474 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7475 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3, 7476 .access = PL1_R, .type = ARM_CP_CONST, 7477 .accessfn = access_aa64_tid3, 7478 .resetvalue = 0 }, 7479 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, 7480 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, 7481 .access = PL1_R, .type = ARM_CP_CONST, 7482 .accessfn = access_aa64_tid3, 7483 .resetvalue = cpu->id_aa64afr0 }, 7484 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, 7485 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, 7486 .access = PL1_R, .type = ARM_CP_CONST, 7487 .accessfn = access_aa64_tid3, 7488 .resetvalue = cpu->id_aa64afr1 }, 7489 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7490 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6, 7491 .access = PL1_R, .type = ARM_CP_CONST, 7492 .accessfn = access_aa64_tid3, 7493 .resetvalue = 0 }, 7494 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7495 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7, 7496 .access = PL1_R, .type = ARM_CP_CONST, 7497 .accessfn = access_aa64_tid3, 7498 .resetvalue = 0 }, 7499 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, 7500 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, 7501 .access = PL1_R, .type = ARM_CP_CONST, 7502 .accessfn = access_aa64_tid3, 7503 .resetvalue = cpu->isar.id_aa64isar0 }, 7504 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, 7505 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, 7506 .access = PL1_R, .type = ARM_CP_CONST, 7507 .accessfn = access_aa64_tid3, 7508 .resetvalue = cpu->isar.id_aa64isar1 }, 7509 { .name = "ID_AA64ISAR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7510 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2, 7511 .access = PL1_R, .type = ARM_CP_CONST, 7512 .accessfn = access_aa64_tid3, 7513 .resetvalue = 0 }, 7514 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7515 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3, 7516 .access = PL1_R, .type = ARM_CP_CONST, 7517 .accessfn = access_aa64_tid3, 7518 .resetvalue = 0 }, 7519 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7520 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4, 7521 .access = PL1_R, .type = ARM_CP_CONST, 7522 .accessfn = access_aa64_tid3, 7523 .resetvalue = 0 }, 7524 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7525 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5, 7526 .access = PL1_R, .type = ARM_CP_CONST, 7527 .accessfn = access_aa64_tid3, 7528 .resetvalue = 0 }, 7529 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7530 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6, 7531 .access = PL1_R, .type = ARM_CP_CONST, 7532 .accessfn = access_aa64_tid3, 7533 .resetvalue = 0 }, 7534 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7535 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7, 7536 .access = PL1_R, .type = ARM_CP_CONST, 7537 .accessfn = access_aa64_tid3, 7538 .resetvalue = 0 }, 7539 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, 7540 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 7541 .access = PL1_R, .type = ARM_CP_CONST, 7542 .accessfn = access_aa64_tid3, 7543 .resetvalue = cpu->isar.id_aa64mmfr0 }, 7544 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, 7545 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, 7546 .access = PL1_R, .type = ARM_CP_CONST, 7547 .accessfn = access_aa64_tid3, 7548 .resetvalue = cpu->isar.id_aa64mmfr1 }, 7549 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64, 7550 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2, 7551 .access = PL1_R, .type = ARM_CP_CONST, 7552 .accessfn = access_aa64_tid3, 7553 .resetvalue = cpu->isar.id_aa64mmfr2 }, 7554 { .name = "ID_AA64MMFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7555 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3, 7556 .access = PL1_R, .type = ARM_CP_CONST, 7557 .accessfn = access_aa64_tid3, 7558 .resetvalue = 0 }, 7559 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7560 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4, 7561 .access = PL1_R, .type = ARM_CP_CONST, 7562 .accessfn = access_aa64_tid3, 7563 .resetvalue = 0 }, 7564 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7565 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5, 7566 .access = PL1_R, .type = ARM_CP_CONST, 7567 .accessfn = access_aa64_tid3, 7568 .resetvalue = 0 }, 7569 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7570 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6, 7571 .access = PL1_R, .type = ARM_CP_CONST, 7572 .accessfn = access_aa64_tid3, 7573 .resetvalue = 0 }, 7574 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7575 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7, 7576 .access = PL1_R, .type = ARM_CP_CONST, 7577 .accessfn = access_aa64_tid3, 7578 .resetvalue = 0 }, 7579 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, 7580 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 7581 .access = PL1_R, .type = ARM_CP_CONST, 7582 .accessfn = access_aa64_tid3, 7583 .resetvalue = cpu->isar.mvfr0 }, 7584 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, 7585 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 7586 .access = PL1_R, .type = ARM_CP_CONST, 7587 .accessfn = access_aa64_tid3, 7588 .resetvalue = cpu->isar.mvfr1 }, 7589 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, 7590 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 7591 .access = PL1_R, .type = ARM_CP_CONST, 7592 .accessfn = access_aa64_tid3, 7593 .resetvalue = cpu->isar.mvfr2 }, 7594 { .name = "MVFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7595 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3, 7596 .access = PL1_R, .type = ARM_CP_CONST, 7597 .accessfn = access_aa64_tid3, 7598 .resetvalue = 0 }, 7599 { .name = "MVFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7600 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4, 7601 .access = PL1_R, .type = ARM_CP_CONST, 7602 .accessfn = access_aa64_tid3, 7603 .resetvalue = 0 }, 7604 { .name = "MVFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7605 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5, 7606 .access = PL1_R, .type = ARM_CP_CONST, 7607 .accessfn = access_aa64_tid3, 7608 .resetvalue = 0 }, 7609 { .name = "MVFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7610 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6, 7611 .access = PL1_R, .type = ARM_CP_CONST, 7612 .accessfn = access_aa64_tid3, 7613 .resetvalue = 0 }, 7614 { .name = "MVFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7615 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7, 7616 .access = PL1_R, .type = ARM_CP_CONST, 7617 .accessfn = access_aa64_tid3, 7618 .resetvalue = 0 }, 7619 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32, 7620 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6, 7621 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 7622 .resetvalue = extract64(cpu->pmceid0, 0, 32) }, 7623 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64, 7624 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6, 7625 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 7626 .resetvalue = cpu->pmceid0 }, 7627 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32, 7628 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7, 7629 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 7630 .resetvalue = extract64(cpu->pmceid1, 0, 32) }, 7631 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64, 7632 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7, 7633 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 7634 .resetvalue = cpu->pmceid1 }, 7635 REGINFO_SENTINEL 7636 }; 7637 #ifdef CONFIG_USER_ONLY 7638 ARMCPRegUserSpaceInfo v8_user_idregs[] = { 7639 { .name = "ID_AA64PFR0_EL1", 7640 .exported_bits = 0x000f000f00ff0000, 7641 .fixed_bits = 0x0000000000000011 }, 7642 { .name = "ID_AA64PFR1_EL1", 7643 .exported_bits = 0x00000000000000f0 }, 7644 { .name = "ID_AA64PFR*_EL1_RESERVED", 7645 .is_glob = true }, 7646 { .name = "ID_AA64ZFR0_EL1" }, 7647 { .name = "ID_AA64MMFR0_EL1", 7648 .fixed_bits = 0x00000000ff000000 }, 7649 { .name = "ID_AA64MMFR1_EL1" }, 7650 { .name = "ID_AA64MMFR*_EL1_RESERVED", 7651 .is_glob = true }, 7652 { .name = "ID_AA64DFR0_EL1", 7653 .fixed_bits = 0x0000000000000006 }, 7654 { .name = "ID_AA64DFR1_EL1" }, 7655 { .name = "ID_AA64DFR*_EL1_RESERVED", 7656 .is_glob = true }, 7657 { .name = "ID_AA64AFR*", 7658 .is_glob = true }, 7659 { .name = "ID_AA64ISAR0_EL1", 7660 .exported_bits = 0x00fffffff0fffff0 }, 7661 { .name = "ID_AA64ISAR1_EL1", 7662 .exported_bits = 0x000000f0ffffffff }, 7663 { .name = "ID_AA64ISAR*_EL1_RESERVED", 7664 .is_glob = true }, 7665 REGUSERINFO_SENTINEL 7666 }; 7667 modify_arm_cp_regs(v8_idregs, v8_user_idregs); 7668 #endif 7669 /* RVBAR_EL1 is only implemented if EL1 is the highest EL */ 7670 if (!arm_feature(env, ARM_FEATURE_EL3) && 7671 !arm_feature(env, ARM_FEATURE_EL2)) { 7672 ARMCPRegInfo rvbar = { 7673 .name = "RVBAR_EL1", .state = ARM_CP_STATE_AA64, 7674 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 7675 .type = ARM_CP_CONST, .access = PL1_R, .resetvalue = cpu->rvbar 7676 }; 7677 define_one_arm_cp_reg(cpu, &rvbar); 7678 } 7679 define_arm_cp_regs(cpu, v8_idregs); 7680 define_arm_cp_regs(cpu, v8_cp_reginfo); 7681 } 7682 if (arm_feature(env, ARM_FEATURE_EL2)) { 7683 uint64_t vmpidr_def = mpidr_read_val(env); 7684 ARMCPRegInfo vpidr_regs[] = { 7685 { .name = "VPIDR", .state = ARM_CP_STATE_AA32, 7686 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 7687 .access = PL2_RW, .accessfn = access_el3_aa32ns, 7688 .resetvalue = cpu->midr, .type = ARM_CP_ALIAS, 7689 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) }, 7690 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64, 7691 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 7692 .access = PL2_RW, .resetvalue = cpu->midr, 7693 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 7694 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32, 7695 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 7696 .access = PL2_RW, .accessfn = access_el3_aa32ns, 7697 .resetvalue = vmpidr_def, .type = ARM_CP_ALIAS, 7698 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) }, 7699 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64, 7700 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 7701 .access = PL2_RW, 7702 .resetvalue = vmpidr_def, 7703 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, 7704 REGINFO_SENTINEL 7705 }; 7706 define_arm_cp_regs(cpu, vpidr_regs); 7707 define_arm_cp_regs(cpu, el2_cp_reginfo); 7708 if (arm_feature(env, ARM_FEATURE_V8)) { 7709 define_arm_cp_regs(cpu, el2_v8_cp_reginfo); 7710 } 7711 /* RVBAR_EL2 is only implemented if EL2 is the highest EL */ 7712 if (!arm_feature(env, ARM_FEATURE_EL3)) { 7713 ARMCPRegInfo rvbar = { 7714 .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64, 7715 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1, 7716 .type = ARM_CP_CONST, .access = PL2_R, .resetvalue = cpu->rvbar 7717 }; 7718 define_one_arm_cp_reg(cpu, &rvbar); 7719 } 7720 } else { 7721 /* If EL2 is missing but higher ELs are enabled, we need to 7722 * register the no_el2 reginfos. 7723 */ 7724 if (arm_feature(env, ARM_FEATURE_EL3)) { 7725 /* When EL3 exists but not EL2, VPIDR and VMPIDR take the value 7726 * of MIDR_EL1 and MPIDR_EL1. 7727 */ 7728 ARMCPRegInfo vpidr_regs[] = { 7729 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_BOTH, 7730 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 7731 .access = PL2_RW, .accessfn = access_el3_aa32ns, 7732 .type = ARM_CP_CONST, .resetvalue = cpu->midr, 7733 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 7734 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_BOTH, 7735 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 7736 .access = PL2_RW, .accessfn = access_el3_aa32ns, 7737 .type = ARM_CP_NO_RAW, 7738 .writefn = arm_cp_write_ignore, .readfn = mpidr_read }, 7739 REGINFO_SENTINEL 7740 }; 7741 define_arm_cp_regs(cpu, vpidr_regs); 7742 define_arm_cp_regs(cpu, el3_no_el2_cp_reginfo); 7743 if (arm_feature(env, ARM_FEATURE_V8)) { 7744 define_arm_cp_regs(cpu, el3_no_el2_v8_cp_reginfo); 7745 } 7746 } 7747 } 7748 if (arm_feature(env, ARM_FEATURE_EL3)) { 7749 define_arm_cp_regs(cpu, el3_cp_reginfo); 7750 ARMCPRegInfo el3_regs[] = { 7751 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64, 7752 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1, 7753 .type = ARM_CP_CONST, .access = PL3_R, .resetvalue = cpu->rvbar }, 7754 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, 7755 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, 7756 .access = PL3_RW, 7757 .raw_writefn = raw_write, .writefn = sctlr_write, 7758 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]), 7759 .resetvalue = cpu->reset_sctlr }, 7760 REGINFO_SENTINEL 7761 }; 7762 7763 define_arm_cp_regs(cpu, el3_regs); 7764 } 7765 /* The behaviour of NSACR is sufficiently various that we don't 7766 * try to describe it in a single reginfo: 7767 * if EL3 is 64 bit, then trap to EL3 from S EL1, 7768 * reads as constant 0xc00 from NS EL1 and NS EL2 7769 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2 7770 * if v7 without EL3, register doesn't exist 7771 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2 7772 */ 7773 if (arm_feature(env, ARM_FEATURE_EL3)) { 7774 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 7775 ARMCPRegInfo nsacr = { 7776 .name = "NSACR", .type = ARM_CP_CONST, 7777 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 7778 .access = PL1_RW, .accessfn = nsacr_access, 7779 .resetvalue = 0xc00 7780 }; 7781 define_one_arm_cp_reg(cpu, &nsacr); 7782 } else { 7783 ARMCPRegInfo nsacr = { 7784 .name = "NSACR", 7785 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 7786 .access = PL3_RW | PL1_R, 7787 .resetvalue = 0, 7788 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) 7789 }; 7790 define_one_arm_cp_reg(cpu, &nsacr); 7791 } 7792 } else { 7793 if (arm_feature(env, ARM_FEATURE_V8)) { 7794 ARMCPRegInfo nsacr = { 7795 .name = "NSACR", .type = ARM_CP_CONST, 7796 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 7797 .access = PL1_R, 7798 .resetvalue = 0xc00 7799 }; 7800 define_one_arm_cp_reg(cpu, &nsacr); 7801 } 7802 } 7803 7804 if (arm_feature(env, ARM_FEATURE_PMSA)) { 7805 if (arm_feature(env, ARM_FEATURE_V6)) { 7806 /* PMSAv6 not implemented */ 7807 assert(arm_feature(env, ARM_FEATURE_V7)); 7808 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 7809 define_arm_cp_regs(cpu, pmsav7_cp_reginfo); 7810 } else { 7811 define_arm_cp_regs(cpu, pmsav5_cp_reginfo); 7812 } 7813 } else { 7814 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 7815 define_arm_cp_regs(cpu, vmsa_cp_reginfo); 7816 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */ 7817 if (cpu_isar_feature(aa32_hpd, cpu)) { 7818 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo); 7819 } 7820 } 7821 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { 7822 define_arm_cp_regs(cpu, t2ee_cp_reginfo); 7823 } 7824 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 7825 define_arm_cp_regs(cpu, generic_timer_cp_reginfo); 7826 } 7827 if (arm_feature(env, ARM_FEATURE_VAPA)) { 7828 define_arm_cp_regs(cpu, vapa_cp_reginfo); 7829 } 7830 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { 7831 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); 7832 } 7833 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { 7834 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); 7835 } 7836 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { 7837 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); 7838 } 7839 if (arm_feature(env, ARM_FEATURE_OMAPCP)) { 7840 define_arm_cp_regs(cpu, omap_cp_reginfo); 7841 } 7842 if (arm_feature(env, ARM_FEATURE_STRONGARM)) { 7843 define_arm_cp_regs(cpu, strongarm_cp_reginfo); 7844 } 7845 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 7846 define_arm_cp_regs(cpu, xscale_cp_reginfo); 7847 } 7848 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { 7849 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); 7850 } 7851 if (arm_feature(env, ARM_FEATURE_LPAE)) { 7852 define_arm_cp_regs(cpu, lpae_cp_reginfo); 7853 } 7854 if (cpu_isar_feature(aa32_jazelle, cpu)) { 7855 define_arm_cp_regs(cpu, jazelle_regs); 7856 } 7857 /* Slightly awkwardly, the OMAP and StrongARM cores need all of 7858 * cp15 crn=0 to be writes-ignored, whereas for other cores they should 7859 * be read-only (ie write causes UNDEF exception). 7860 */ 7861 { 7862 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { 7863 /* Pre-v8 MIDR space. 7864 * Note that the MIDR isn't a simple constant register because 7865 * of the TI925 behaviour where writes to another register can 7866 * cause the MIDR value to change. 7867 * 7868 * Unimplemented registers in the c15 0 0 0 space default to 7869 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR 7870 * and friends override accordingly. 7871 */ 7872 { .name = "MIDR", 7873 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, 7874 .access = PL1_R, .resetvalue = cpu->midr, 7875 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, 7876 .readfn = midr_read, 7877 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 7878 .type = ARM_CP_OVERRIDE }, 7879 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ 7880 { .name = "DUMMY", 7881 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, 7882 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 7883 { .name = "DUMMY", 7884 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, 7885 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 7886 { .name = "DUMMY", 7887 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, 7888 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 7889 { .name = "DUMMY", 7890 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, 7891 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 7892 { .name = "DUMMY", 7893 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, 7894 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 7895 REGINFO_SENTINEL 7896 }; 7897 ARMCPRegInfo id_v8_midr_cp_reginfo[] = { 7898 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, 7899 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, 7900 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr, 7901 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 7902 .readfn = midr_read }, 7903 /* crn = 0 op1 = 0 crm = 0 op2 = 4,7 : AArch32 aliases of MIDR */ 7904 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 7905 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 7906 .access = PL1_R, .resetvalue = cpu->midr }, 7907 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 7908 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7, 7909 .access = PL1_R, .resetvalue = cpu->midr }, 7910 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, 7911 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, 7912 .access = PL1_R, 7913 .accessfn = access_aa64_tid1, 7914 .type = ARM_CP_CONST, .resetvalue = cpu->revidr }, 7915 REGINFO_SENTINEL 7916 }; 7917 ARMCPRegInfo id_cp_reginfo[] = { 7918 /* These are common to v8 and pre-v8 */ 7919 { .name = "CTR", 7920 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, 7921 .access = PL1_R, .accessfn = ctr_el0_access, 7922 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 7923 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, 7924 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, 7925 .access = PL0_R, .accessfn = ctr_el0_access, 7926 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 7927 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ 7928 { .name = "TCMTR", 7929 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, 7930 .access = PL1_R, 7931 .accessfn = access_aa32_tid1, 7932 .type = ARM_CP_CONST, .resetvalue = 0 }, 7933 REGINFO_SENTINEL 7934 }; 7935 /* TLBTR is specific to VMSA */ 7936 ARMCPRegInfo id_tlbtr_reginfo = { 7937 .name = "TLBTR", 7938 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, 7939 .access = PL1_R, 7940 .accessfn = access_aa32_tid1, 7941 .type = ARM_CP_CONST, .resetvalue = 0, 7942 }; 7943 /* MPUIR is specific to PMSA V6+ */ 7944 ARMCPRegInfo id_mpuir_reginfo = { 7945 .name = "MPUIR", 7946 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 7947 .access = PL1_R, .type = ARM_CP_CONST, 7948 .resetvalue = cpu->pmsav7_dregion << 8 7949 }; 7950 ARMCPRegInfo crn0_wi_reginfo = { 7951 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, 7952 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, 7953 .type = ARM_CP_NOP | ARM_CP_OVERRIDE 7954 }; 7955 #ifdef CONFIG_USER_ONLY 7956 ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = { 7957 { .name = "MIDR_EL1", 7958 .exported_bits = 0x00000000ffffffff }, 7959 { .name = "REVIDR_EL1" }, 7960 REGUSERINFO_SENTINEL 7961 }; 7962 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo); 7963 #endif 7964 if (arm_feature(env, ARM_FEATURE_OMAPCP) || 7965 arm_feature(env, ARM_FEATURE_STRONGARM)) { 7966 ARMCPRegInfo *r; 7967 /* Register the blanket "writes ignored" value first to cover the 7968 * whole space. Then update the specific ID registers to allow write 7969 * access, so that they ignore writes rather than causing them to 7970 * UNDEF. 7971 */ 7972 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); 7973 for (r = id_pre_v8_midr_cp_reginfo; 7974 r->type != ARM_CP_SENTINEL; r++) { 7975 r->access = PL1_RW; 7976 } 7977 for (r = id_cp_reginfo; r->type != ARM_CP_SENTINEL; r++) { 7978 r->access = PL1_RW; 7979 } 7980 id_mpuir_reginfo.access = PL1_RW; 7981 id_tlbtr_reginfo.access = PL1_RW; 7982 } 7983 if (arm_feature(env, ARM_FEATURE_V8)) { 7984 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); 7985 } else { 7986 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); 7987 } 7988 define_arm_cp_regs(cpu, id_cp_reginfo); 7989 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 7990 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo); 7991 } else if (arm_feature(env, ARM_FEATURE_V7)) { 7992 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 7993 } 7994 } 7995 7996 if (arm_feature(env, ARM_FEATURE_MPIDR)) { 7997 ARMCPRegInfo mpidr_cp_reginfo[] = { 7998 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH, 7999 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, 8000 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW }, 8001 REGINFO_SENTINEL 8002 }; 8003 #ifdef CONFIG_USER_ONLY 8004 ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = { 8005 { .name = "MPIDR_EL1", 8006 .fixed_bits = 0x0000000080000000 }, 8007 REGUSERINFO_SENTINEL 8008 }; 8009 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo); 8010 #endif 8011 define_arm_cp_regs(cpu, mpidr_cp_reginfo); 8012 } 8013 8014 if (arm_feature(env, ARM_FEATURE_AUXCR)) { 8015 ARMCPRegInfo auxcr_reginfo[] = { 8016 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, 8017 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, 8018 .access = PL1_RW, .accessfn = access_tacr, 8019 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr }, 8020 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH, 8021 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1, 8022 .access = PL2_RW, .type = ARM_CP_CONST, 8023 .resetvalue = 0 }, 8024 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64, 8025 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1, 8026 .access = PL3_RW, .type = ARM_CP_CONST, 8027 .resetvalue = 0 }, 8028 REGINFO_SENTINEL 8029 }; 8030 define_arm_cp_regs(cpu, auxcr_reginfo); 8031 if (cpu_isar_feature(aa32_ac2, cpu)) { 8032 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo); 8033 } 8034 } 8035 8036 if (arm_feature(env, ARM_FEATURE_CBAR)) { 8037 /* 8038 * CBAR is IMPDEF, but common on Arm Cortex-A implementations. 8039 * There are two flavours: 8040 * (1) older 32-bit only cores have a simple 32-bit CBAR 8041 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a 8042 * 32-bit register visible to AArch32 at a different encoding 8043 * to the "flavour 1" register and with the bits rearranged to 8044 * be able to squash a 64-bit address into the 32-bit view. 8045 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but 8046 * in future if we support AArch32-only configs of some of the 8047 * AArch64 cores we might need to add a specific feature flag 8048 * to indicate cores with "flavour 2" CBAR. 8049 */ 8050 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 8051 /* 32 bit view is [31:18] 0...0 [43:32]. */ 8052 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) 8053 | extract64(cpu->reset_cbar, 32, 12); 8054 ARMCPRegInfo cbar_reginfo[] = { 8055 { .name = "CBAR", 8056 .type = ARM_CP_CONST, 8057 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0, 8058 .access = PL1_R, .resetvalue = cbar32 }, 8059 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, 8060 .type = ARM_CP_CONST, 8061 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, 8062 .access = PL1_R, .resetvalue = cpu->reset_cbar }, 8063 REGINFO_SENTINEL 8064 }; 8065 /* We don't implement a r/w 64 bit CBAR currently */ 8066 assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); 8067 define_arm_cp_regs(cpu, cbar_reginfo); 8068 } else { 8069 ARMCPRegInfo cbar = { 8070 .name = "CBAR", 8071 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 8072 .access = PL1_R|PL3_W, .resetvalue = cpu->reset_cbar, 8073 .fieldoffset = offsetof(CPUARMState, 8074 cp15.c15_config_base_address) 8075 }; 8076 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 8077 cbar.access = PL1_R; 8078 cbar.fieldoffset = 0; 8079 cbar.type = ARM_CP_CONST; 8080 } 8081 define_one_arm_cp_reg(cpu, &cbar); 8082 } 8083 } 8084 8085 if (arm_feature(env, ARM_FEATURE_VBAR)) { 8086 ARMCPRegInfo vbar_cp_reginfo[] = { 8087 { .name = "VBAR", .state = ARM_CP_STATE_BOTH, 8088 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, 8089 .access = PL1_RW, .writefn = vbar_write, 8090 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s), 8091 offsetof(CPUARMState, cp15.vbar_ns) }, 8092 .resetvalue = 0 }, 8093 REGINFO_SENTINEL 8094 }; 8095 define_arm_cp_regs(cpu, vbar_cp_reginfo); 8096 } 8097 8098 /* Generic registers whose values depend on the implementation */ 8099 { 8100 ARMCPRegInfo sctlr = { 8101 .name = "SCTLR", .state = ARM_CP_STATE_BOTH, 8102 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 8103 .access = PL1_RW, .accessfn = access_tvm_trvm, 8104 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), 8105 offsetof(CPUARMState, cp15.sctlr_ns) }, 8106 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, 8107 .raw_writefn = raw_write, 8108 }; 8109 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 8110 /* Normally we would always end the TB on an SCTLR write, but Linux 8111 * arch/arm/mach-pxa/sleep.S expects two instructions following 8112 * an MMU enable to execute from cache. Imitate this behaviour. 8113 */ 8114 sctlr.type |= ARM_CP_SUPPRESS_TB_END; 8115 } 8116 define_one_arm_cp_reg(cpu, &sctlr); 8117 } 8118 8119 if (cpu_isar_feature(aa64_lor, cpu)) { 8120 define_arm_cp_regs(cpu, lor_reginfo); 8121 } 8122 if (cpu_isar_feature(aa64_pan, cpu)) { 8123 define_one_arm_cp_reg(cpu, &pan_reginfo); 8124 } 8125 #ifndef CONFIG_USER_ONLY 8126 if (cpu_isar_feature(aa64_ats1e1, cpu)) { 8127 define_arm_cp_regs(cpu, ats1e1_reginfo); 8128 } 8129 if (cpu_isar_feature(aa32_ats1e1, cpu)) { 8130 define_arm_cp_regs(cpu, ats1cp_reginfo); 8131 } 8132 #endif 8133 if (cpu_isar_feature(aa64_uao, cpu)) { 8134 define_one_arm_cp_reg(cpu, &uao_reginfo); 8135 } 8136 8137 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) { 8138 define_arm_cp_regs(cpu, vhe_reginfo); 8139 } 8140 8141 if (cpu_isar_feature(aa64_sve, cpu)) { 8142 define_one_arm_cp_reg(cpu, &zcr_el1_reginfo); 8143 if (arm_feature(env, ARM_FEATURE_EL2)) { 8144 define_one_arm_cp_reg(cpu, &zcr_el2_reginfo); 8145 } else { 8146 define_one_arm_cp_reg(cpu, &zcr_no_el2_reginfo); 8147 } 8148 if (arm_feature(env, ARM_FEATURE_EL3)) { 8149 define_one_arm_cp_reg(cpu, &zcr_el3_reginfo); 8150 } 8151 } 8152 8153 #ifdef TARGET_AARCH64 8154 if (cpu_isar_feature(aa64_pauth, cpu)) { 8155 define_arm_cp_regs(cpu, pauth_reginfo); 8156 } 8157 if (cpu_isar_feature(aa64_rndr, cpu)) { 8158 define_arm_cp_regs(cpu, rndr_reginfo); 8159 } 8160 #ifndef CONFIG_USER_ONLY 8161 /* Data Cache clean instructions up to PoP */ 8162 if (cpu_isar_feature(aa64_dcpop, cpu)) { 8163 define_one_arm_cp_reg(cpu, dcpop_reg); 8164 8165 if (cpu_isar_feature(aa64_dcpodp, cpu)) { 8166 define_one_arm_cp_reg(cpu, dcpodp_reg); 8167 } 8168 } 8169 #endif /*CONFIG_USER_ONLY*/ 8170 8171 /* 8172 * If full MTE is enabled, add all of the system registers. 8173 * If only "instructions available at EL0" are enabled, 8174 * then define only a RAZ/WI version of PSTATE.TCO. 8175 */ 8176 if (cpu_isar_feature(aa64_mte, cpu)) { 8177 define_arm_cp_regs(cpu, mte_reginfo); 8178 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo); 8179 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) { 8180 define_arm_cp_regs(cpu, mte_tco_ro_reginfo); 8181 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo); 8182 } 8183 #endif 8184 8185 if (cpu_isar_feature(any_predinv, cpu)) { 8186 define_arm_cp_regs(cpu, predinv_reginfo); 8187 } 8188 8189 if (cpu_isar_feature(any_ccidx, cpu)) { 8190 define_arm_cp_regs(cpu, ccsidr2_reginfo); 8191 } 8192 8193 #ifndef CONFIG_USER_ONLY 8194 /* 8195 * Register redirections and aliases must be done last, 8196 * after the registers from the other extensions have been defined. 8197 */ 8198 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) { 8199 define_arm_vh_e2h_redirects_aliases(cpu); 8200 } 8201 #endif 8202 } 8203 8204 void arm_cpu_register_gdb_regs_for_features(ARMCPU *cpu) 8205 { 8206 CPUState *cs = CPU(cpu); 8207 CPUARMState *env = &cpu->env; 8208 8209 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 8210 /* 8211 * The lower part of each SVE register aliases to the FPU 8212 * registers so we don't need to include both. 8213 */ 8214 #ifdef TARGET_AARCH64 8215 if (isar_feature_aa64_sve(&cpu->isar)) { 8216 gdb_register_coprocessor(cs, arm_gdb_get_svereg, arm_gdb_set_svereg, 8217 arm_gen_dynamic_svereg_xml(cs, cs->gdb_num_regs), 8218 "sve-registers.xml", 0); 8219 } else 8220 #endif 8221 { 8222 gdb_register_coprocessor(cs, aarch64_fpu_gdb_get_reg, 8223 aarch64_fpu_gdb_set_reg, 8224 34, "aarch64-fpu.xml", 0); 8225 } 8226 } else if (arm_feature(env, ARM_FEATURE_NEON)) { 8227 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 8228 51, "arm-neon.xml", 0); 8229 } else if (cpu_isar_feature(aa32_simd_r32, cpu)) { 8230 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 8231 35, "arm-vfp3.xml", 0); 8232 } else if (cpu_isar_feature(aa32_vfp_simd, cpu)) { 8233 gdb_register_coprocessor(cs, vfp_gdb_get_reg, vfp_gdb_set_reg, 8234 19, "arm-vfp.xml", 0); 8235 } 8236 gdb_register_coprocessor(cs, arm_gdb_get_sysreg, arm_gdb_set_sysreg, 8237 arm_gen_dynamic_sysreg_xml(cs, cs->gdb_num_regs), 8238 "system-registers.xml", 0); 8239 8240 } 8241 8242 /* Sort alphabetically by type name, except for "any". */ 8243 static gint arm_cpu_list_compare(gconstpointer a, gconstpointer b) 8244 { 8245 ObjectClass *class_a = (ObjectClass *)a; 8246 ObjectClass *class_b = (ObjectClass *)b; 8247 const char *name_a, *name_b; 8248 8249 name_a = object_class_get_name(class_a); 8250 name_b = object_class_get_name(class_b); 8251 if (strcmp(name_a, "any-" TYPE_ARM_CPU) == 0) { 8252 return 1; 8253 } else if (strcmp(name_b, "any-" TYPE_ARM_CPU) == 0) { 8254 return -1; 8255 } else { 8256 return strcmp(name_a, name_b); 8257 } 8258 } 8259 8260 static void arm_cpu_list_entry(gpointer data, gpointer user_data) 8261 { 8262 ObjectClass *oc = data; 8263 const char *typename; 8264 char *name; 8265 8266 typename = object_class_get_name(oc); 8267 name = g_strndup(typename, strlen(typename) - strlen("-" TYPE_ARM_CPU)); 8268 qemu_printf(" %s\n", name); 8269 g_free(name); 8270 } 8271 8272 void arm_cpu_list(void) 8273 { 8274 GSList *list; 8275 8276 list = object_class_get_list(TYPE_ARM_CPU, false); 8277 list = g_slist_sort(list, arm_cpu_list_compare); 8278 qemu_printf("Available CPUs:\n"); 8279 g_slist_foreach(list, arm_cpu_list_entry, NULL); 8280 g_slist_free(list); 8281 } 8282 8283 static void arm_cpu_add_definition(gpointer data, gpointer user_data) 8284 { 8285 ObjectClass *oc = data; 8286 CpuDefinitionInfoList **cpu_list = user_data; 8287 CpuDefinitionInfo *info; 8288 const char *typename; 8289 8290 typename = object_class_get_name(oc); 8291 info = g_malloc0(sizeof(*info)); 8292 info->name = g_strndup(typename, 8293 strlen(typename) - strlen("-" TYPE_ARM_CPU)); 8294 info->q_typename = g_strdup(typename); 8295 8296 QAPI_LIST_PREPEND(*cpu_list, info); 8297 } 8298 8299 CpuDefinitionInfoList *qmp_query_cpu_definitions(Error **errp) 8300 { 8301 CpuDefinitionInfoList *cpu_list = NULL; 8302 GSList *list; 8303 8304 list = object_class_get_list(TYPE_ARM_CPU, false); 8305 g_slist_foreach(list, arm_cpu_add_definition, &cpu_list); 8306 g_slist_free(list); 8307 8308 return cpu_list; 8309 } 8310 8311 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, 8312 void *opaque, int state, int secstate, 8313 int crm, int opc1, int opc2, 8314 const char *name) 8315 { 8316 /* Private utility function for define_one_arm_cp_reg_with_opaque(): 8317 * add a single reginfo struct to the hash table. 8318 */ 8319 uint32_t *key = g_new(uint32_t, 1); 8320 ARMCPRegInfo *r2 = g_memdup(r, sizeof(ARMCPRegInfo)); 8321 int is64 = (r->type & ARM_CP_64BIT) ? 1 : 0; 8322 int ns = (secstate & ARM_CP_SECSTATE_NS) ? 1 : 0; 8323 8324 r2->name = g_strdup(name); 8325 /* Reset the secure state to the specific incoming state. This is 8326 * necessary as the register may have been defined with both states. 8327 */ 8328 r2->secure = secstate; 8329 8330 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 8331 /* Register is banked (using both entries in array). 8332 * Overwriting fieldoffset as the array is only used to define 8333 * banked registers but later only fieldoffset is used. 8334 */ 8335 r2->fieldoffset = r->bank_fieldoffsets[ns]; 8336 } 8337 8338 if (state == ARM_CP_STATE_AA32) { 8339 if (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]) { 8340 /* If the register is banked then we don't need to migrate or 8341 * reset the 32-bit instance in certain cases: 8342 * 8343 * 1) If the register has both 32-bit and 64-bit instances then we 8344 * can count on the 64-bit instance taking care of the 8345 * non-secure bank. 8346 * 2) If ARMv8 is enabled then we can count on a 64-bit version 8347 * taking care of the secure bank. This requires that separate 8348 * 32 and 64-bit definitions are provided. 8349 */ 8350 if ((r->state == ARM_CP_STATE_BOTH && ns) || 8351 (arm_feature(&cpu->env, ARM_FEATURE_V8) && !ns)) { 8352 r2->type |= ARM_CP_ALIAS; 8353 } 8354 } else if ((secstate != r->secure) && !ns) { 8355 /* The register is not banked so we only want to allow migration of 8356 * the non-secure instance. 8357 */ 8358 r2->type |= ARM_CP_ALIAS; 8359 } 8360 8361 if (r->state == ARM_CP_STATE_BOTH) { 8362 /* We assume it is a cp15 register if the .cp field is left unset. 8363 */ 8364 if (r2->cp == 0) { 8365 r2->cp = 15; 8366 } 8367 8368 #ifdef HOST_WORDS_BIGENDIAN 8369 if (r2->fieldoffset) { 8370 r2->fieldoffset += sizeof(uint32_t); 8371 } 8372 #endif 8373 } 8374 } 8375 if (state == ARM_CP_STATE_AA64) { 8376 /* To allow abbreviation of ARMCPRegInfo 8377 * definitions, we treat cp == 0 as equivalent to 8378 * the value for "standard guest-visible sysreg". 8379 * STATE_BOTH definitions are also always "standard 8380 * sysreg" in their AArch64 view (the .cp value may 8381 * be non-zero for the benefit of the AArch32 view). 8382 */ 8383 if (r->cp == 0 || r->state == ARM_CP_STATE_BOTH) { 8384 r2->cp = CP_REG_ARM64_SYSREG_CP; 8385 } 8386 *key = ENCODE_AA64_CP_REG(r2->cp, r2->crn, crm, 8387 r2->opc0, opc1, opc2); 8388 } else { 8389 *key = ENCODE_CP_REG(r2->cp, is64, ns, r2->crn, crm, opc1, opc2); 8390 } 8391 if (opaque) { 8392 r2->opaque = opaque; 8393 } 8394 /* reginfo passed to helpers is correct for the actual access, 8395 * and is never ARM_CP_STATE_BOTH: 8396 */ 8397 r2->state = state; 8398 /* Make sure reginfo passed to helpers for wildcarded regs 8399 * has the correct crm/opc1/opc2 for this reg, not CP_ANY: 8400 */ 8401 r2->crm = crm; 8402 r2->opc1 = opc1; 8403 r2->opc2 = opc2; 8404 /* By convention, for wildcarded registers only the first 8405 * entry is used for migration; the others are marked as 8406 * ALIAS so we don't try to transfer the register 8407 * multiple times. Special registers (ie NOP/WFI) are 8408 * never migratable and not even raw-accessible. 8409 */ 8410 if ((r->type & ARM_CP_SPECIAL)) { 8411 r2->type |= ARM_CP_NO_RAW; 8412 } 8413 if (((r->crm == CP_ANY) && crm != 0) || 8414 ((r->opc1 == CP_ANY) && opc1 != 0) || 8415 ((r->opc2 == CP_ANY) && opc2 != 0)) { 8416 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB; 8417 } 8418 8419 /* Check that raw accesses are either forbidden or handled. Note that 8420 * we can't assert this earlier because the setup of fieldoffset for 8421 * banked registers has to be done first. 8422 */ 8423 if (!(r2->type & ARM_CP_NO_RAW)) { 8424 assert(!raw_accessors_invalid(r2)); 8425 } 8426 8427 /* Overriding of an existing definition must be explicitly 8428 * requested. 8429 */ 8430 if (!(r->type & ARM_CP_OVERRIDE)) { 8431 ARMCPRegInfo *oldreg; 8432 oldreg = g_hash_table_lookup(cpu->cp_regs, key); 8433 if (oldreg && !(oldreg->type & ARM_CP_OVERRIDE)) { 8434 fprintf(stderr, "Register redefined: cp=%d %d bit " 8435 "crn=%d crm=%d opc1=%d opc2=%d, " 8436 "was %s, now %s\n", r2->cp, 32 + 32 * is64, 8437 r2->crn, r2->crm, r2->opc1, r2->opc2, 8438 oldreg->name, r2->name); 8439 g_assert_not_reached(); 8440 } 8441 } 8442 g_hash_table_insert(cpu->cp_regs, key, r2); 8443 } 8444 8445 8446 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, 8447 const ARMCPRegInfo *r, void *opaque) 8448 { 8449 /* Define implementations of coprocessor registers. 8450 * We store these in a hashtable because typically 8451 * there are less than 150 registers in a space which 8452 * is 16*16*16*8*8 = 262144 in size. 8453 * Wildcarding is supported for the crm, opc1 and opc2 fields. 8454 * If a register is defined twice then the second definition is 8455 * used, so this can be used to define some generic registers and 8456 * then override them with implementation specific variations. 8457 * At least one of the original and the second definition should 8458 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard 8459 * against accidental use. 8460 * 8461 * The state field defines whether the register is to be 8462 * visible in the AArch32 or AArch64 execution state. If the 8463 * state is set to ARM_CP_STATE_BOTH then we synthesise a 8464 * reginfo structure for the AArch32 view, which sees the lower 8465 * 32 bits of the 64 bit register. 8466 * 8467 * Only registers visible in AArch64 may set r->opc0; opc0 cannot 8468 * be wildcarded. AArch64 registers are always considered to be 64 8469 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of 8470 * the register, if any. 8471 */ 8472 int crm, opc1, opc2, state; 8473 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; 8474 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; 8475 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; 8476 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; 8477 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; 8478 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; 8479 /* 64 bit registers have only CRm and Opc1 fields */ 8480 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); 8481 /* op0 only exists in the AArch64 encodings */ 8482 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); 8483 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ 8484 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); 8485 /* 8486 * This API is only for Arm's system coprocessors (14 and 15) or 8487 * (M-profile or v7A-and-earlier only) for implementation defined 8488 * coprocessors in the range 0..7. Our decode assumes this, since 8489 * 8..13 can be used for other insns including VFP and Neon. See 8490 * valid_cp() in translate.c. Assert here that we haven't tried 8491 * to use an invalid coprocessor number. 8492 */ 8493 switch (r->state) { 8494 case ARM_CP_STATE_BOTH: 8495 /* 0 has a special meaning, but otherwise the same rules as AA32. */ 8496 if (r->cp == 0) { 8497 break; 8498 } 8499 /* fall through */ 8500 case ARM_CP_STATE_AA32: 8501 if (arm_feature(&cpu->env, ARM_FEATURE_V8) && 8502 !arm_feature(&cpu->env, ARM_FEATURE_M)) { 8503 assert(r->cp >= 14 && r->cp <= 15); 8504 } else { 8505 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15)); 8506 } 8507 break; 8508 case ARM_CP_STATE_AA64: 8509 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP); 8510 break; 8511 default: 8512 g_assert_not_reached(); 8513 } 8514 /* The AArch64 pseudocode CheckSystemAccess() specifies that op1 8515 * encodes a minimum access level for the register. We roll this 8516 * runtime check into our general permission check code, so check 8517 * here that the reginfo's specified permissions are strict enough 8518 * to encompass the generic architectural permission check. 8519 */ 8520 if (r->state != ARM_CP_STATE_AA32) { 8521 int mask = 0; 8522 switch (r->opc1) { 8523 case 0: 8524 /* min_EL EL1, but some accessible to EL0 via kernel ABI */ 8525 mask = PL0U_R | PL1_RW; 8526 break; 8527 case 1: case 2: 8528 /* min_EL EL1 */ 8529 mask = PL1_RW; 8530 break; 8531 case 3: 8532 /* min_EL EL0 */ 8533 mask = PL0_RW; 8534 break; 8535 case 4: 8536 case 5: 8537 /* min_EL EL2 */ 8538 mask = PL2_RW; 8539 break; 8540 case 6: 8541 /* min_EL EL3 */ 8542 mask = PL3_RW; 8543 break; 8544 case 7: 8545 /* min_EL EL1, secure mode only (we don't check the latter) */ 8546 mask = PL1_RW; 8547 break; 8548 default: 8549 /* broken reginfo with out-of-range opc1 */ 8550 assert(false); 8551 break; 8552 } 8553 /* assert our permissions are not too lax (stricter is fine) */ 8554 assert((r->access & ~mask) == 0); 8555 } 8556 8557 /* Check that the register definition has enough info to handle 8558 * reads and writes if they are permitted. 8559 */ 8560 if (!(r->type & (ARM_CP_SPECIAL|ARM_CP_CONST))) { 8561 if (r->access & PL3_R) { 8562 assert((r->fieldoffset || 8563 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 8564 r->readfn); 8565 } 8566 if (r->access & PL3_W) { 8567 assert((r->fieldoffset || 8568 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 8569 r->writefn); 8570 } 8571 } 8572 /* Bad type field probably means missing sentinel at end of reg list */ 8573 assert(cptype_valid(r->type)); 8574 for (crm = crmmin; crm <= crmmax; crm++) { 8575 for (opc1 = opc1min; opc1 <= opc1max; opc1++) { 8576 for (opc2 = opc2min; opc2 <= opc2max; opc2++) { 8577 for (state = ARM_CP_STATE_AA32; 8578 state <= ARM_CP_STATE_AA64; state++) { 8579 if (r->state != state && r->state != ARM_CP_STATE_BOTH) { 8580 continue; 8581 } 8582 if (state == ARM_CP_STATE_AA32) { 8583 /* Under AArch32 CP registers can be common 8584 * (same for secure and non-secure world) or banked. 8585 */ 8586 char *name; 8587 8588 switch (r->secure) { 8589 case ARM_CP_SECSTATE_S: 8590 case ARM_CP_SECSTATE_NS: 8591 add_cpreg_to_hashtable(cpu, r, opaque, state, 8592 r->secure, crm, opc1, opc2, 8593 r->name); 8594 break; 8595 default: 8596 name = g_strdup_printf("%s_S", r->name); 8597 add_cpreg_to_hashtable(cpu, r, opaque, state, 8598 ARM_CP_SECSTATE_S, 8599 crm, opc1, opc2, name); 8600 g_free(name); 8601 add_cpreg_to_hashtable(cpu, r, opaque, state, 8602 ARM_CP_SECSTATE_NS, 8603 crm, opc1, opc2, r->name); 8604 break; 8605 } 8606 } else { 8607 /* AArch64 registers get mapped to non-secure instance 8608 * of AArch32 */ 8609 add_cpreg_to_hashtable(cpu, r, opaque, state, 8610 ARM_CP_SECSTATE_NS, 8611 crm, opc1, opc2, r->name); 8612 } 8613 } 8614 } 8615 } 8616 } 8617 } 8618 8619 void define_arm_cp_regs_with_opaque(ARMCPU *cpu, 8620 const ARMCPRegInfo *regs, void *opaque) 8621 { 8622 /* Define a whole list of registers */ 8623 const ARMCPRegInfo *r; 8624 for (r = regs; r->type != ARM_CP_SENTINEL; r++) { 8625 define_one_arm_cp_reg_with_opaque(cpu, r, opaque); 8626 } 8627 } 8628 8629 /* 8630 * Modify ARMCPRegInfo for access from userspace. 8631 * 8632 * This is a data driven modification directed by 8633 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as 8634 * user-space cannot alter any values and dynamic values pertaining to 8635 * execution state are hidden from user space view anyway. 8636 */ 8637 void modify_arm_cp_regs(ARMCPRegInfo *regs, const ARMCPRegUserSpaceInfo *mods) 8638 { 8639 const ARMCPRegUserSpaceInfo *m; 8640 ARMCPRegInfo *r; 8641 8642 for (m = mods; m->name; m++) { 8643 GPatternSpec *pat = NULL; 8644 if (m->is_glob) { 8645 pat = g_pattern_spec_new(m->name); 8646 } 8647 for (r = regs; r->type != ARM_CP_SENTINEL; r++) { 8648 if (pat && g_pattern_match_string(pat, r->name)) { 8649 r->type = ARM_CP_CONST; 8650 r->access = PL0U_R; 8651 r->resetvalue = 0; 8652 /* continue */ 8653 } else if (strcmp(r->name, m->name) == 0) { 8654 r->type = ARM_CP_CONST; 8655 r->access = PL0U_R; 8656 r->resetvalue &= m->exported_bits; 8657 r->resetvalue |= m->fixed_bits; 8658 break; 8659 } 8660 } 8661 if (pat) { 8662 g_pattern_spec_free(pat); 8663 } 8664 } 8665 } 8666 8667 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) 8668 { 8669 return g_hash_table_lookup(cpregs, &encoded_cp); 8670 } 8671 8672 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, 8673 uint64_t value) 8674 { 8675 /* Helper coprocessor write function for write-ignore registers */ 8676 } 8677 8678 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) 8679 { 8680 /* Helper coprocessor write function for read-as-zero registers */ 8681 return 0; 8682 } 8683 8684 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) 8685 { 8686 /* Helper coprocessor reset function for do-nothing-on-reset registers */ 8687 } 8688 8689 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type) 8690 { 8691 /* Return true if it is not valid for us to switch to 8692 * this CPU mode (ie all the UNPREDICTABLE cases in 8693 * the ARM ARM CPSRWriteByInstr pseudocode). 8694 */ 8695 8696 /* Changes to or from Hyp via MSR and CPS are illegal. */ 8697 if (write_type == CPSRWriteByInstr && 8698 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP || 8699 mode == ARM_CPU_MODE_HYP)) { 8700 return 1; 8701 } 8702 8703 switch (mode) { 8704 case ARM_CPU_MODE_USR: 8705 return 0; 8706 case ARM_CPU_MODE_SYS: 8707 case ARM_CPU_MODE_SVC: 8708 case ARM_CPU_MODE_ABT: 8709 case ARM_CPU_MODE_UND: 8710 case ARM_CPU_MODE_IRQ: 8711 case ARM_CPU_MODE_FIQ: 8712 /* Note that we don't implement the IMPDEF NSACR.RFR which in v7 8713 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.) 8714 */ 8715 /* If HCR.TGE is set then changes from Monitor to NS PL1 via MSR 8716 * and CPS are treated as illegal mode changes. 8717 */ 8718 if (write_type == CPSRWriteByInstr && 8719 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON && 8720 (arm_hcr_el2_eff(env) & HCR_TGE)) { 8721 return 1; 8722 } 8723 return 0; 8724 case ARM_CPU_MODE_HYP: 8725 return !arm_feature(env, ARM_FEATURE_EL2) 8726 || arm_current_el(env) < 2 || arm_is_secure_below_el3(env); 8727 case ARM_CPU_MODE_MON: 8728 return arm_current_el(env) < 3; 8729 default: 8730 return 1; 8731 } 8732 } 8733 8734 uint32_t cpsr_read(CPUARMState *env) 8735 { 8736 int ZF; 8737 ZF = (env->ZF == 0); 8738 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | 8739 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) 8740 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) 8741 | ((env->condexec_bits & 0xfc) << 8) 8742 | (env->GE << 16) | (env->daif & CPSR_AIF); 8743 } 8744 8745 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask, 8746 CPSRWriteType write_type) 8747 { 8748 uint32_t changed_daif; 8749 8750 if (mask & CPSR_NZCV) { 8751 env->ZF = (~val) & CPSR_Z; 8752 env->NF = val; 8753 env->CF = (val >> 29) & 1; 8754 env->VF = (val << 3) & 0x80000000; 8755 } 8756 if (mask & CPSR_Q) 8757 env->QF = ((val & CPSR_Q) != 0); 8758 if (mask & CPSR_T) 8759 env->thumb = ((val & CPSR_T) != 0); 8760 if (mask & CPSR_IT_0_1) { 8761 env->condexec_bits &= ~3; 8762 env->condexec_bits |= (val >> 25) & 3; 8763 } 8764 if (mask & CPSR_IT_2_7) { 8765 env->condexec_bits &= 3; 8766 env->condexec_bits |= (val >> 8) & 0xfc; 8767 } 8768 if (mask & CPSR_GE) { 8769 env->GE = (val >> 16) & 0xf; 8770 } 8771 8772 /* In a V7 implementation that includes the security extensions but does 8773 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control 8774 * whether non-secure software is allowed to change the CPSR_F and CPSR_A 8775 * bits respectively. 8776 * 8777 * In a V8 implementation, it is permitted for privileged software to 8778 * change the CPSR A/F bits regardless of the SCR.AW/FW bits. 8779 */ 8780 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) && 8781 arm_feature(env, ARM_FEATURE_EL3) && 8782 !arm_feature(env, ARM_FEATURE_EL2) && 8783 !arm_is_secure(env)) { 8784 8785 changed_daif = (env->daif ^ val) & mask; 8786 8787 if (changed_daif & CPSR_A) { 8788 /* Check to see if we are allowed to change the masking of async 8789 * abort exceptions from a non-secure state. 8790 */ 8791 if (!(env->cp15.scr_el3 & SCR_AW)) { 8792 qemu_log_mask(LOG_GUEST_ERROR, 8793 "Ignoring attempt to switch CPSR_A flag from " 8794 "non-secure world with SCR.AW bit clear\n"); 8795 mask &= ~CPSR_A; 8796 } 8797 } 8798 8799 if (changed_daif & CPSR_F) { 8800 /* Check to see if we are allowed to change the masking of FIQ 8801 * exceptions from a non-secure state. 8802 */ 8803 if (!(env->cp15.scr_el3 & SCR_FW)) { 8804 qemu_log_mask(LOG_GUEST_ERROR, 8805 "Ignoring attempt to switch CPSR_F flag from " 8806 "non-secure world with SCR.FW bit clear\n"); 8807 mask &= ~CPSR_F; 8808 } 8809 8810 /* Check whether non-maskable FIQ (NMFI) support is enabled. 8811 * If this bit is set software is not allowed to mask 8812 * FIQs, but is allowed to set CPSR_F to 0. 8813 */ 8814 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && 8815 (val & CPSR_F)) { 8816 qemu_log_mask(LOG_GUEST_ERROR, 8817 "Ignoring attempt to enable CPSR_F flag " 8818 "(non-maskable FIQ [NMFI] support enabled)\n"); 8819 mask &= ~CPSR_F; 8820 } 8821 } 8822 } 8823 8824 env->daif &= ~(CPSR_AIF & mask); 8825 env->daif |= val & CPSR_AIF & mask; 8826 8827 if (write_type != CPSRWriteRaw && 8828 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) { 8829 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) { 8830 /* Note that we can only get here in USR mode if this is a 8831 * gdb stub write; for this case we follow the architectural 8832 * behaviour for guest writes in USR mode of ignoring an attempt 8833 * to switch mode. (Those are caught by translate.c for writes 8834 * triggered by guest instructions.) 8835 */ 8836 mask &= ~CPSR_M; 8837 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) { 8838 /* Attempt to switch to an invalid mode: this is UNPREDICTABLE in 8839 * v7, and has defined behaviour in v8: 8840 * + leave CPSR.M untouched 8841 * + allow changes to the other CPSR fields 8842 * + set PSTATE.IL 8843 * For user changes via the GDB stub, we don't set PSTATE.IL, 8844 * as this would be unnecessarily harsh for a user error. 8845 */ 8846 mask &= ~CPSR_M; 8847 if (write_type != CPSRWriteByGDBStub && 8848 arm_feature(env, ARM_FEATURE_V8)) { 8849 mask |= CPSR_IL; 8850 val |= CPSR_IL; 8851 } 8852 qemu_log_mask(LOG_GUEST_ERROR, 8853 "Illegal AArch32 mode switch attempt from %s to %s\n", 8854 aarch32_mode_name(env->uncached_cpsr), 8855 aarch32_mode_name(val)); 8856 } else { 8857 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n", 8858 write_type == CPSRWriteExceptionReturn ? 8859 "Exception return from AArch32" : 8860 "AArch32 mode switch from", 8861 aarch32_mode_name(env->uncached_cpsr), 8862 aarch32_mode_name(val), env->regs[15]); 8863 switch_mode(env, val & CPSR_M); 8864 } 8865 } 8866 mask &= ~CACHED_CPSR_BITS; 8867 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); 8868 } 8869 8870 /* Sign/zero extend */ 8871 uint32_t HELPER(sxtb16)(uint32_t x) 8872 { 8873 uint32_t res; 8874 res = (uint16_t)(int8_t)x; 8875 res |= (uint32_t)(int8_t)(x >> 16) << 16; 8876 return res; 8877 } 8878 8879 uint32_t HELPER(uxtb16)(uint32_t x) 8880 { 8881 uint32_t res; 8882 res = (uint16_t)(uint8_t)x; 8883 res |= (uint32_t)(uint8_t)(x >> 16) << 16; 8884 return res; 8885 } 8886 8887 int32_t HELPER(sdiv)(int32_t num, int32_t den) 8888 { 8889 if (den == 0) 8890 return 0; 8891 if (num == INT_MIN && den == -1) 8892 return INT_MIN; 8893 return num / den; 8894 } 8895 8896 uint32_t HELPER(udiv)(uint32_t num, uint32_t den) 8897 { 8898 if (den == 0) 8899 return 0; 8900 return num / den; 8901 } 8902 8903 uint32_t HELPER(rbit)(uint32_t x) 8904 { 8905 return revbit32(x); 8906 } 8907 8908 #ifdef CONFIG_USER_ONLY 8909 8910 static void switch_mode(CPUARMState *env, int mode) 8911 { 8912 ARMCPU *cpu = env_archcpu(env); 8913 8914 if (mode != ARM_CPU_MODE_USR) { 8915 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); 8916 } 8917 } 8918 8919 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 8920 uint32_t cur_el, bool secure) 8921 { 8922 return 1; 8923 } 8924 8925 void aarch64_sync_64_to_32(CPUARMState *env) 8926 { 8927 g_assert_not_reached(); 8928 } 8929 8930 #else 8931 8932 static void switch_mode(CPUARMState *env, int mode) 8933 { 8934 int old_mode; 8935 int i; 8936 8937 old_mode = env->uncached_cpsr & CPSR_M; 8938 if (mode == old_mode) 8939 return; 8940 8941 if (old_mode == ARM_CPU_MODE_FIQ) { 8942 memcpy (env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); 8943 memcpy (env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); 8944 } else if (mode == ARM_CPU_MODE_FIQ) { 8945 memcpy (env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); 8946 memcpy (env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); 8947 } 8948 8949 i = bank_number(old_mode); 8950 env->banked_r13[i] = env->regs[13]; 8951 env->banked_spsr[i] = env->spsr; 8952 8953 i = bank_number(mode); 8954 env->regs[13] = env->banked_r13[i]; 8955 env->spsr = env->banked_spsr[i]; 8956 8957 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14]; 8958 env->regs[14] = env->banked_r14[r14_bank_number(mode)]; 8959 } 8960 8961 /* Physical Interrupt Target EL Lookup Table 8962 * 8963 * [ From ARM ARM section G1.13.4 (Table G1-15) ] 8964 * 8965 * The below multi-dimensional table is used for looking up the target 8966 * exception level given numerous condition criteria. Specifically, the 8967 * target EL is based on SCR and HCR routing controls as well as the 8968 * currently executing EL and secure state. 8969 * 8970 * Dimensions: 8971 * target_el_table[2][2][2][2][2][4] 8972 * | | | | | +--- Current EL 8973 * | | | | +------ Non-secure(0)/Secure(1) 8974 * | | | +--------- HCR mask override 8975 * | | +------------ SCR exec state control 8976 * | +--------------- SCR mask override 8977 * +------------------ 32-bit(0)/64-bit(1) EL3 8978 * 8979 * The table values are as such: 8980 * 0-3 = EL0-EL3 8981 * -1 = Cannot occur 8982 * 8983 * The ARM ARM target EL table includes entries indicating that an "exception 8984 * is not taken". The two cases where this is applicable are: 8985 * 1) An exception is taken from EL3 but the SCR does not have the exception 8986 * routed to EL3. 8987 * 2) An exception is taken from EL2 but the HCR does not have the exception 8988 * routed to EL2. 8989 * In these two cases, the below table contain a target of EL1. This value is 8990 * returned as it is expected that the consumer of the table data will check 8991 * for "target EL >= current EL" to ensure the exception is not taken. 8992 * 8993 * SCR HCR 8994 * 64 EA AMO From 8995 * BIT IRQ IMO Non-secure Secure 8996 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 8997 */ 8998 static const int8_t target_el_table[2][2][2][2][2][4] = { 8999 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 9000 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, 9001 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 9002 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, 9003 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 9004 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, 9005 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 9006 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, 9007 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, 9008 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},}, 9009 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, -1, 1 },}, 9010 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 1, 1, -1, 1 },},},}, 9011 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 9012 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, 9013 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 9014 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},},},}, 9015 }; 9016 9017 /* 9018 * Determine the target EL for physical exceptions 9019 */ 9020 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 9021 uint32_t cur_el, bool secure) 9022 { 9023 CPUARMState *env = cs->env_ptr; 9024 bool rw; 9025 bool scr; 9026 bool hcr; 9027 int target_el; 9028 /* Is the highest EL AArch64? */ 9029 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64); 9030 uint64_t hcr_el2; 9031 9032 if (arm_feature(env, ARM_FEATURE_EL3)) { 9033 rw = ((env->cp15.scr_el3 & SCR_RW) == SCR_RW); 9034 } else { 9035 /* Either EL2 is the highest EL (and so the EL2 register width 9036 * is given by is64); or there is no EL2 or EL3, in which case 9037 * the value of 'rw' does not affect the table lookup anyway. 9038 */ 9039 rw = is64; 9040 } 9041 9042 hcr_el2 = arm_hcr_el2_eff(env); 9043 switch (excp_idx) { 9044 case EXCP_IRQ: 9045 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); 9046 hcr = hcr_el2 & HCR_IMO; 9047 break; 9048 case EXCP_FIQ: 9049 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); 9050 hcr = hcr_el2 & HCR_FMO; 9051 break; 9052 default: 9053 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); 9054 hcr = hcr_el2 & HCR_AMO; 9055 break; 9056 }; 9057 9058 /* 9059 * For these purposes, TGE and AMO/IMO/FMO both force the 9060 * interrupt to EL2. Fold TGE into the bit extracted above. 9061 */ 9062 hcr |= (hcr_el2 & HCR_TGE) != 0; 9063 9064 /* Perform a table-lookup for the target EL given the current state */ 9065 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; 9066 9067 assert(target_el > 0); 9068 9069 return target_el; 9070 } 9071 9072 void arm_log_exception(int idx) 9073 { 9074 if (qemu_loglevel_mask(CPU_LOG_INT)) { 9075 const char *exc = NULL; 9076 static const char * const excnames[] = { 9077 [EXCP_UDEF] = "Undefined Instruction", 9078 [EXCP_SWI] = "SVC", 9079 [EXCP_PREFETCH_ABORT] = "Prefetch Abort", 9080 [EXCP_DATA_ABORT] = "Data Abort", 9081 [EXCP_IRQ] = "IRQ", 9082 [EXCP_FIQ] = "FIQ", 9083 [EXCP_BKPT] = "Breakpoint", 9084 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit", 9085 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage", 9086 [EXCP_HVC] = "Hypervisor Call", 9087 [EXCP_HYP_TRAP] = "Hypervisor Trap", 9088 [EXCP_SMC] = "Secure Monitor Call", 9089 [EXCP_VIRQ] = "Virtual IRQ", 9090 [EXCP_VFIQ] = "Virtual FIQ", 9091 [EXCP_SEMIHOST] = "Semihosting call", 9092 [EXCP_NOCP] = "v7M NOCP UsageFault", 9093 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault", 9094 [EXCP_STKOF] = "v8M STKOF UsageFault", 9095 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking", 9096 [EXCP_LSERR] = "v8M LSERR UsageFault", 9097 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault", 9098 }; 9099 9100 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { 9101 exc = excnames[idx]; 9102 } 9103 if (!exc) { 9104 exc = "unknown"; 9105 } 9106 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s]\n", idx, exc); 9107 } 9108 } 9109 9110 /* 9111 * Function used to synchronize QEMU's AArch64 register set with AArch32 9112 * register set. This is necessary when switching between AArch32 and AArch64 9113 * execution state. 9114 */ 9115 void aarch64_sync_32_to_64(CPUARMState *env) 9116 { 9117 int i; 9118 uint32_t mode = env->uncached_cpsr & CPSR_M; 9119 9120 /* We can blanket copy R[0:7] to X[0:7] */ 9121 for (i = 0; i < 8; i++) { 9122 env->xregs[i] = env->regs[i]; 9123 } 9124 9125 /* 9126 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12. 9127 * Otherwise, they come from the banked user regs. 9128 */ 9129 if (mode == ARM_CPU_MODE_FIQ) { 9130 for (i = 8; i < 13; i++) { 9131 env->xregs[i] = env->usr_regs[i - 8]; 9132 } 9133 } else { 9134 for (i = 8; i < 13; i++) { 9135 env->xregs[i] = env->regs[i]; 9136 } 9137 } 9138 9139 /* 9140 * Registers x13-x23 are the various mode SP and FP registers. Registers 9141 * r13 and r14 are only copied if we are in that mode, otherwise we copy 9142 * from the mode banked register. 9143 */ 9144 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 9145 env->xregs[13] = env->regs[13]; 9146 env->xregs[14] = env->regs[14]; 9147 } else { 9148 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)]; 9149 /* HYP is an exception in that it is copied from r14 */ 9150 if (mode == ARM_CPU_MODE_HYP) { 9151 env->xregs[14] = env->regs[14]; 9152 } else { 9153 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)]; 9154 } 9155 } 9156 9157 if (mode == ARM_CPU_MODE_HYP) { 9158 env->xregs[15] = env->regs[13]; 9159 } else { 9160 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)]; 9161 } 9162 9163 if (mode == ARM_CPU_MODE_IRQ) { 9164 env->xregs[16] = env->regs[14]; 9165 env->xregs[17] = env->regs[13]; 9166 } else { 9167 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)]; 9168 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)]; 9169 } 9170 9171 if (mode == ARM_CPU_MODE_SVC) { 9172 env->xregs[18] = env->regs[14]; 9173 env->xregs[19] = env->regs[13]; 9174 } else { 9175 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)]; 9176 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)]; 9177 } 9178 9179 if (mode == ARM_CPU_MODE_ABT) { 9180 env->xregs[20] = env->regs[14]; 9181 env->xregs[21] = env->regs[13]; 9182 } else { 9183 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)]; 9184 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)]; 9185 } 9186 9187 if (mode == ARM_CPU_MODE_UND) { 9188 env->xregs[22] = env->regs[14]; 9189 env->xregs[23] = env->regs[13]; 9190 } else { 9191 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)]; 9192 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)]; 9193 } 9194 9195 /* 9196 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 9197 * mode, then we can copy from r8-r14. Otherwise, we copy from the 9198 * FIQ bank for r8-r14. 9199 */ 9200 if (mode == ARM_CPU_MODE_FIQ) { 9201 for (i = 24; i < 31; i++) { 9202 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */ 9203 } 9204 } else { 9205 for (i = 24; i < 29; i++) { 9206 env->xregs[i] = env->fiq_regs[i - 24]; 9207 } 9208 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)]; 9209 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)]; 9210 } 9211 9212 env->pc = env->regs[15]; 9213 } 9214 9215 /* 9216 * Function used to synchronize QEMU's AArch32 register set with AArch64 9217 * register set. This is necessary when switching between AArch32 and AArch64 9218 * execution state. 9219 */ 9220 void aarch64_sync_64_to_32(CPUARMState *env) 9221 { 9222 int i; 9223 uint32_t mode = env->uncached_cpsr & CPSR_M; 9224 9225 /* We can blanket copy X[0:7] to R[0:7] */ 9226 for (i = 0; i < 8; i++) { 9227 env->regs[i] = env->xregs[i]; 9228 } 9229 9230 /* 9231 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12. 9232 * Otherwise, we copy x8-x12 into the banked user regs. 9233 */ 9234 if (mode == ARM_CPU_MODE_FIQ) { 9235 for (i = 8; i < 13; i++) { 9236 env->usr_regs[i - 8] = env->xregs[i]; 9237 } 9238 } else { 9239 for (i = 8; i < 13; i++) { 9240 env->regs[i] = env->xregs[i]; 9241 } 9242 } 9243 9244 /* 9245 * Registers r13 & r14 depend on the current mode. 9246 * If we are in a given mode, we copy the corresponding x registers to r13 9247 * and r14. Otherwise, we copy the x register to the banked r13 and r14 9248 * for the mode. 9249 */ 9250 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 9251 env->regs[13] = env->xregs[13]; 9252 env->regs[14] = env->xregs[14]; 9253 } else { 9254 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13]; 9255 9256 /* 9257 * HYP is an exception in that it does not have its own banked r14 but 9258 * shares the USR r14 9259 */ 9260 if (mode == ARM_CPU_MODE_HYP) { 9261 env->regs[14] = env->xregs[14]; 9262 } else { 9263 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14]; 9264 } 9265 } 9266 9267 if (mode == ARM_CPU_MODE_HYP) { 9268 env->regs[13] = env->xregs[15]; 9269 } else { 9270 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15]; 9271 } 9272 9273 if (mode == ARM_CPU_MODE_IRQ) { 9274 env->regs[14] = env->xregs[16]; 9275 env->regs[13] = env->xregs[17]; 9276 } else { 9277 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16]; 9278 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17]; 9279 } 9280 9281 if (mode == ARM_CPU_MODE_SVC) { 9282 env->regs[14] = env->xregs[18]; 9283 env->regs[13] = env->xregs[19]; 9284 } else { 9285 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18]; 9286 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19]; 9287 } 9288 9289 if (mode == ARM_CPU_MODE_ABT) { 9290 env->regs[14] = env->xregs[20]; 9291 env->regs[13] = env->xregs[21]; 9292 } else { 9293 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20]; 9294 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21]; 9295 } 9296 9297 if (mode == ARM_CPU_MODE_UND) { 9298 env->regs[14] = env->xregs[22]; 9299 env->regs[13] = env->xregs[23]; 9300 } else { 9301 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22]; 9302 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23]; 9303 } 9304 9305 /* Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 9306 * mode, then we can copy to r8-r14. Otherwise, we copy to the 9307 * FIQ bank for r8-r14. 9308 */ 9309 if (mode == ARM_CPU_MODE_FIQ) { 9310 for (i = 24; i < 31; i++) { 9311 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */ 9312 } 9313 } else { 9314 for (i = 24; i < 29; i++) { 9315 env->fiq_regs[i - 24] = env->xregs[i]; 9316 } 9317 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29]; 9318 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30]; 9319 } 9320 9321 env->regs[15] = env->pc; 9322 } 9323 9324 static void take_aarch32_exception(CPUARMState *env, int new_mode, 9325 uint32_t mask, uint32_t offset, 9326 uint32_t newpc) 9327 { 9328 int new_el; 9329 9330 /* Change the CPU state so as to actually take the exception. */ 9331 switch_mode(env, new_mode); 9332 9333 /* 9334 * For exceptions taken to AArch32 we must clear the SS bit in both 9335 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. 9336 */ 9337 env->uncached_cpsr &= ~PSTATE_SS; 9338 env->spsr = cpsr_read(env); 9339 /* Clear IT bits. */ 9340 env->condexec_bits = 0; 9341 /* Switch to the new mode, and to the correct instruction set. */ 9342 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; 9343 9344 /* This must be after mode switching. */ 9345 new_el = arm_current_el(env); 9346 9347 /* Set new mode endianness */ 9348 env->uncached_cpsr &= ~CPSR_E; 9349 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) { 9350 env->uncached_cpsr |= CPSR_E; 9351 } 9352 /* J and IL must always be cleared for exception entry */ 9353 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J); 9354 env->daif |= mask; 9355 9356 if (new_mode == ARM_CPU_MODE_HYP) { 9357 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0; 9358 env->elr_el[2] = env->regs[15]; 9359 } else { 9360 /* CPSR.PAN is normally preserved preserved unless... */ 9361 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) { 9362 switch (new_el) { 9363 case 3: 9364 if (!arm_is_secure_below_el3(env)) { 9365 /* ... the target is EL3, from non-secure state. */ 9366 env->uncached_cpsr &= ~CPSR_PAN; 9367 break; 9368 } 9369 /* ... the target is EL3, from secure state ... */ 9370 /* fall through */ 9371 case 1: 9372 /* ... the target is EL1 and SCTLR.SPAN is 0. */ 9373 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) { 9374 env->uncached_cpsr |= CPSR_PAN; 9375 } 9376 break; 9377 } 9378 } 9379 /* 9380 * this is a lie, as there was no c1_sys on V4T/V5, but who cares 9381 * and we should just guard the thumb mode on V4 9382 */ 9383 if (arm_feature(env, ARM_FEATURE_V4T)) { 9384 env->thumb = 9385 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; 9386 } 9387 env->regs[14] = env->regs[15] + offset; 9388 } 9389 env->regs[15] = newpc; 9390 arm_rebuild_hflags(env); 9391 } 9392 9393 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs) 9394 { 9395 /* 9396 * Handle exception entry to Hyp mode; this is sufficiently 9397 * different to entry to other AArch32 modes that we handle it 9398 * separately here. 9399 * 9400 * The vector table entry used is always the 0x14 Hyp mode entry point, 9401 * unless this is an UNDEF/HVC/abort taken from Hyp to Hyp. 9402 * The offset applied to the preferred return address is always zero 9403 * (see DDI0487C.a section G1.12.3). 9404 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values. 9405 */ 9406 uint32_t addr, mask; 9407 ARMCPU *cpu = ARM_CPU(cs); 9408 CPUARMState *env = &cpu->env; 9409 9410 switch (cs->exception_index) { 9411 case EXCP_UDEF: 9412 addr = 0x04; 9413 break; 9414 case EXCP_SWI: 9415 addr = 0x14; 9416 break; 9417 case EXCP_BKPT: 9418 /* Fall through to prefetch abort. */ 9419 case EXCP_PREFETCH_ABORT: 9420 env->cp15.ifar_s = env->exception.vaddress; 9421 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n", 9422 (uint32_t)env->exception.vaddress); 9423 addr = 0x0c; 9424 break; 9425 case EXCP_DATA_ABORT: 9426 env->cp15.dfar_s = env->exception.vaddress; 9427 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n", 9428 (uint32_t)env->exception.vaddress); 9429 addr = 0x10; 9430 break; 9431 case EXCP_IRQ: 9432 addr = 0x18; 9433 break; 9434 case EXCP_FIQ: 9435 addr = 0x1c; 9436 break; 9437 case EXCP_HVC: 9438 addr = 0x08; 9439 break; 9440 case EXCP_HYP_TRAP: 9441 addr = 0x14; 9442 break; 9443 default: 9444 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 9445 } 9446 9447 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) { 9448 if (!arm_feature(env, ARM_FEATURE_V8)) { 9449 /* 9450 * QEMU syndrome values are v8-style. v7 has the IL bit 9451 * UNK/SBZP for "field not valid" cases, where v8 uses RES1. 9452 * If this is a v7 CPU, squash the IL bit in those cases. 9453 */ 9454 if (cs->exception_index == EXCP_PREFETCH_ABORT || 9455 (cs->exception_index == EXCP_DATA_ABORT && 9456 !(env->exception.syndrome & ARM_EL_ISV)) || 9457 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) { 9458 env->exception.syndrome &= ~ARM_EL_IL; 9459 } 9460 } 9461 env->cp15.esr_el[2] = env->exception.syndrome; 9462 } 9463 9464 if (arm_current_el(env) != 2 && addr < 0x14) { 9465 addr = 0x14; 9466 } 9467 9468 mask = 0; 9469 if (!(env->cp15.scr_el3 & SCR_EA)) { 9470 mask |= CPSR_A; 9471 } 9472 if (!(env->cp15.scr_el3 & SCR_IRQ)) { 9473 mask |= CPSR_I; 9474 } 9475 if (!(env->cp15.scr_el3 & SCR_FIQ)) { 9476 mask |= CPSR_F; 9477 } 9478 9479 addr += env->cp15.hvbar; 9480 9481 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr); 9482 } 9483 9484 static void arm_cpu_do_interrupt_aarch32(CPUState *cs) 9485 { 9486 ARMCPU *cpu = ARM_CPU(cs); 9487 CPUARMState *env = &cpu->env; 9488 uint32_t addr; 9489 uint32_t mask; 9490 int new_mode; 9491 uint32_t offset; 9492 uint32_t moe; 9493 9494 /* If this is a debug exception we must update the DBGDSCR.MOE bits */ 9495 switch (syn_get_ec(env->exception.syndrome)) { 9496 case EC_BREAKPOINT: 9497 case EC_BREAKPOINT_SAME_EL: 9498 moe = 1; 9499 break; 9500 case EC_WATCHPOINT: 9501 case EC_WATCHPOINT_SAME_EL: 9502 moe = 10; 9503 break; 9504 case EC_AA32_BKPT: 9505 moe = 3; 9506 break; 9507 case EC_VECTORCATCH: 9508 moe = 5; 9509 break; 9510 default: 9511 moe = 0; 9512 break; 9513 } 9514 9515 if (moe) { 9516 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); 9517 } 9518 9519 if (env->exception.target_el == 2) { 9520 arm_cpu_do_interrupt_aarch32_hyp(cs); 9521 return; 9522 } 9523 9524 switch (cs->exception_index) { 9525 case EXCP_UDEF: 9526 new_mode = ARM_CPU_MODE_UND; 9527 addr = 0x04; 9528 mask = CPSR_I; 9529 if (env->thumb) 9530 offset = 2; 9531 else 9532 offset = 4; 9533 break; 9534 case EXCP_SWI: 9535 new_mode = ARM_CPU_MODE_SVC; 9536 addr = 0x08; 9537 mask = CPSR_I; 9538 /* The PC already points to the next instruction. */ 9539 offset = 0; 9540 break; 9541 case EXCP_BKPT: 9542 /* Fall through to prefetch abort. */ 9543 case EXCP_PREFETCH_ABORT: 9544 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); 9545 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); 9546 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", 9547 env->exception.fsr, (uint32_t)env->exception.vaddress); 9548 new_mode = ARM_CPU_MODE_ABT; 9549 addr = 0x0c; 9550 mask = CPSR_A | CPSR_I; 9551 offset = 4; 9552 break; 9553 case EXCP_DATA_ABORT: 9554 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 9555 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); 9556 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", 9557 env->exception.fsr, 9558 (uint32_t)env->exception.vaddress); 9559 new_mode = ARM_CPU_MODE_ABT; 9560 addr = 0x10; 9561 mask = CPSR_A | CPSR_I; 9562 offset = 8; 9563 break; 9564 case EXCP_IRQ: 9565 new_mode = ARM_CPU_MODE_IRQ; 9566 addr = 0x18; 9567 /* Disable IRQ and imprecise data aborts. */ 9568 mask = CPSR_A | CPSR_I; 9569 offset = 4; 9570 if (env->cp15.scr_el3 & SCR_IRQ) { 9571 /* IRQ routed to monitor mode */ 9572 new_mode = ARM_CPU_MODE_MON; 9573 mask |= CPSR_F; 9574 } 9575 break; 9576 case EXCP_FIQ: 9577 new_mode = ARM_CPU_MODE_FIQ; 9578 addr = 0x1c; 9579 /* Disable FIQ, IRQ and imprecise data aborts. */ 9580 mask = CPSR_A | CPSR_I | CPSR_F; 9581 if (env->cp15.scr_el3 & SCR_FIQ) { 9582 /* FIQ routed to monitor mode */ 9583 new_mode = ARM_CPU_MODE_MON; 9584 } 9585 offset = 4; 9586 break; 9587 case EXCP_VIRQ: 9588 new_mode = ARM_CPU_MODE_IRQ; 9589 addr = 0x18; 9590 /* Disable IRQ and imprecise data aborts. */ 9591 mask = CPSR_A | CPSR_I; 9592 offset = 4; 9593 break; 9594 case EXCP_VFIQ: 9595 new_mode = ARM_CPU_MODE_FIQ; 9596 addr = 0x1c; 9597 /* Disable FIQ, IRQ and imprecise data aborts. */ 9598 mask = CPSR_A | CPSR_I | CPSR_F; 9599 offset = 4; 9600 break; 9601 case EXCP_SMC: 9602 new_mode = ARM_CPU_MODE_MON; 9603 addr = 0x08; 9604 mask = CPSR_A | CPSR_I | CPSR_F; 9605 offset = 0; 9606 break; 9607 default: 9608 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 9609 return; /* Never happens. Keep compiler happy. */ 9610 } 9611 9612 if (new_mode == ARM_CPU_MODE_MON) { 9613 addr += env->cp15.mvbar; 9614 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 9615 /* High vectors. When enabled, base address cannot be remapped. */ 9616 addr += 0xffff0000; 9617 } else { 9618 /* ARM v7 architectures provide a vector base address register to remap 9619 * the interrupt vector table. 9620 * This register is only followed in non-monitor mode, and is banked. 9621 * Note: only bits 31:5 are valid. 9622 */ 9623 addr += A32_BANKED_CURRENT_REG_GET(env, vbar); 9624 } 9625 9626 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 9627 env->cp15.scr_el3 &= ~SCR_NS; 9628 } 9629 9630 take_aarch32_exception(env, new_mode, mask, offset, addr); 9631 } 9632 9633 static int aarch64_regnum(CPUARMState *env, int aarch32_reg) 9634 { 9635 /* 9636 * Return the register number of the AArch64 view of the AArch32 9637 * register @aarch32_reg. The CPUARMState CPSR is assumed to still 9638 * be that of the AArch32 mode the exception came from. 9639 */ 9640 int mode = env->uncached_cpsr & CPSR_M; 9641 9642 switch (aarch32_reg) { 9643 case 0 ... 7: 9644 return aarch32_reg; 9645 case 8 ... 12: 9646 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg; 9647 case 13: 9648 switch (mode) { 9649 case ARM_CPU_MODE_USR: 9650 case ARM_CPU_MODE_SYS: 9651 return 13; 9652 case ARM_CPU_MODE_HYP: 9653 return 15; 9654 case ARM_CPU_MODE_IRQ: 9655 return 17; 9656 case ARM_CPU_MODE_SVC: 9657 return 19; 9658 case ARM_CPU_MODE_ABT: 9659 return 21; 9660 case ARM_CPU_MODE_UND: 9661 return 23; 9662 case ARM_CPU_MODE_FIQ: 9663 return 29; 9664 default: 9665 g_assert_not_reached(); 9666 } 9667 case 14: 9668 switch (mode) { 9669 case ARM_CPU_MODE_USR: 9670 case ARM_CPU_MODE_SYS: 9671 case ARM_CPU_MODE_HYP: 9672 return 14; 9673 case ARM_CPU_MODE_IRQ: 9674 return 16; 9675 case ARM_CPU_MODE_SVC: 9676 return 18; 9677 case ARM_CPU_MODE_ABT: 9678 return 20; 9679 case ARM_CPU_MODE_UND: 9680 return 22; 9681 case ARM_CPU_MODE_FIQ: 9682 return 30; 9683 default: 9684 g_assert_not_reached(); 9685 } 9686 case 15: 9687 return 31; 9688 default: 9689 g_assert_not_reached(); 9690 } 9691 } 9692 9693 /* Handle exception entry to a target EL which is using AArch64 */ 9694 static void arm_cpu_do_interrupt_aarch64(CPUState *cs) 9695 { 9696 ARMCPU *cpu = ARM_CPU(cs); 9697 CPUARMState *env = &cpu->env; 9698 unsigned int new_el = env->exception.target_el; 9699 target_ulong addr = env->cp15.vbar_el[new_el]; 9700 unsigned int new_mode = aarch64_pstate_mode(new_el, true); 9701 unsigned int old_mode; 9702 unsigned int cur_el = arm_current_el(env); 9703 int rt; 9704 9705 /* 9706 * Note that new_el can never be 0. If cur_el is 0, then 9707 * el0_a64 is is_a64(), else el0_a64 is ignored. 9708 */ 9709 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env)); 9710 9711 if (cur_el < new_el) { 9712 /* Entry vector offset depends on whether the implemented EL 9713 * immediately lower than the target level is using AArch32 or AArch64 9714 */ 9715 bool is_aa64; 9716 uint64_t hcr; 9717 9718 switch (new_el) { 9719 case 3: 9720 is_aa64 = (env->cp15.scr_el3 & SCR_RW) != 0; 9721 break; 9722 case 2: 9723 hcr = arm_hcr_el2_eff(env); 9724 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 9725 is_aa64 = (hcr & HCR_RW) != 0; 9726 break; 9727 } 9728 /* fall through */ 9729 case 1: 9730 is_aa64 = is_a64(env); 9731 break; 9732 default: 9733 g_assert_not_reached(); 9734 } 9735 9736 if (is_aa64) { 9737 addr += 0x400; 9738 } else { 9739 addr += 0x600; 9740 } 9741 } else if (pstate_read(env) & PSTATE_SP) { 9742 addr += 0x200; 9743 } 9744 9745 switch (cs->exception_index) { 9746 case EXCP_PREFETCH_ABORT: 9747 case EXCP_DATA_ABORT: 9748 env->cp15.far_el[new_el] = env->exception.vaddress; 9749 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n", 9750 env->cp15.far_el[new_el]); 9751 /* fall through */ 9752 case EXCP_BKPT: 9753 case EXCP_UDEF: 9754 case EXCP_SWI: 9755 case EXCP_HVC: 9756 case EXCP_HYP_TRAP: 9757 case EXCP_SMC: 9758 switch (syn_get_ec(env->exception.syndrome)) { 9759 case EC_ADVSIMDFPACCESSTRAP: 9760 /* 9761 * QEMU internal FP/SIMD syndromes from AArch32 include the 9762 * TA and coproc fields which are only exposed if the exception 9763 * is taken to AArch32 Hyp mode. Mask them out to get a valid 9764 * AArch64 format syndrome. 9765 */ 9766 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20); 9767 break; 9768 case EC_CP14RTTRAP: 9769 case EC_CP15RTTRAP: 9770 case EC_CP14DTTRAP: 9771 /* 9772 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently 9773 * the raw register field from the insn; when taking this to 9774 * AArch64 we must convert it to the AArch64 view of the register 9775 * number. Notice that we read a 4-bit AArch32 register number and 9776 * write back a 5-bit AArch64 one. 9777 */ 9778 rt = extract32(env->exception.syndrome, 5, 4); 9779 rt = aarch64_regnum(env, rt); 9780 env->exception.syndrome = deposit32(env->exception.syndrome, 9781 5, 5, rt); 9782 break; 9783 case EC_CP15RRTTRAP: 9784 case EC_CP14RRTTRAP: 9785 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */ 9786 rt = extract32(env->exception.syndrome, 5, 4); 9787 rt = aarch64_regnum(env, rt); 9788 env->exception.syndrome = deposit32(env->exception.syndrome, 9789 5, 5, rt); 9790 rt = extract32(env->exception.syndrome, 10, 4); 9791 rt = aarch64_regnum(env, rt); 9792 env->exception.syndrome = deposit32(env->exception.syndrome, 9793 10, 5, rt); 9794 break; 9795 } 9796 env->cp15.esr_el[new_el] = env->exception.syndrome; 9797 break; 9798 case EXCP_IRQ: 9799 case EXCP_VIRQ: 9800 addr += 0x80; 9801 break; 9802 case EXCP_FIQ: 9803 case EXCP_VFIQ: 9804 addr += 0x100; 9805 break; 9806 default: 9807 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 9808 } 9809 9810 if (is_a64(env)) { 9811 old_mode = pstate_read(env); 9812 aarch64_save_sp(env, arm_current_el(env)); 9813 env->elr_el[new_el] = env->pc; 9814 } else { 9815 old_mode = cpsr_read(env); 9816 env->elr_el[new_el] = env->regs[15]; 9817 9818 aarch64_sync_32_to_64(env); 9819 9820 env->condexec_bits = 0; 9821 } 9822 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode; 9823 9824 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n", 9825 env->elr_el[new_el]); 9826 9827 if (cpu_isar_feature(aa64_pan, cpu)) { 9828 /* The value of PSTATE.PAN is normally preserved, except when ... */ 9829 new_mode |= old_mode & PSTATE_PAN; 9830 switch (new_el) { 9831 case 2: 9832 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */ 9833 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) 9834 != (HCR_E2H | HCR_TGE)) { 9835 break; 9836 } 9837 /* fall through */ 9838 case 1: 9839 /* ... the target is EL1 ... */ 9840 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */ 9841 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) { 9842 new_mode |= PSTATE_PAN; 9843 } 9844 break; 9845 } 9846 } 9847 if (cpu_isar_feature(aa64_mte, cpu)) { 9848 new_mode |= PSTATE_TCO; 9849 } 9850 9851 pstate_write(env, PSTATE_DAIF | new_mode); 9852 env->aarch64 = 1; 9853 aarch64_restore_sp(env, new_el); 9854 helper_rebuild_hflags_a64(env, new_el); 9855 9856 env->pc = addr; 9857 9858 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n", 9859 new_el, env->pc, pstate_read(env)); 9860 } 9861 9862 /* 9863 * Do semihosting call and set the appropriate return value. All the 9864 * permission and validity checks have been done at translate time. 9865 * 9866 * We only see semihosting exceptions in TCG only as they are not 9867 * trapped to the hypervisor in KVM. 9868 */ 9869 #ifdef CONFIG_TCG 9870 static void handle_semihosting(CPUState *cs) 9871 { 9872 ARMCPU *cpu = ARM_CPU(cs); 9873 CPUARMState *env = &cpu->env; 9874 9875 if (is_a64(env)) { 9876 qemu_log_mask(CPU_LOG_INT, 9877 "...handling as semihosting call 0x%" PRIx64 "\n", 9878 env->xregs[0]); 9879 env->xregs[0] = do_common_semihosting(cs); 9880 env->pc += 4; 9881 } else { 9882 qemu_log_mask(CPU_LOG_INT, 9883 "...handling as semihosting call 0x%x\n", 9884 env->regs[0]); 9885 env->regs[0] = do_common_semihosting(cs); 9886 env->regs[15] += env->thumb ? 2 : 4; 9887 } 9888 } 9889 #endif 9890 9891 /* Handle a CPU exception for A and R profile CPUs. 9892 * Do any appropriate logging, handle PSCI calls, and then hand off 9893 * to the AArch64-entry or AArch32-entry function depending on the 9894 * target exception level's register width. 9895 */ 9896 void arm_cpu_do_interrupt(CPUState *cs) 9897 { 9898 ARMCPU *cpu = ARM_CPU(cs); 9899 CPUARMState *env = &cpu->env; 9900 unsigned int new_el = env->exception.target_el; 9901 9902 assert(!arm_feature(env, ARM_FEATURE_M)); 9903 9904 arm_log_exception(cs->exception_index); 9905 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env), 9906 new_el); 9907 if (qemu_loglevel_mask(CPU_LOG_INT) 9908 && !excp_is_internal(cs->exception_index)) { 9909 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n", 9910 syn_get_ec(env->exception.syndrome), 9911 env->exception.syndrome); 9912 } 9913 9914 if (arm_is_psci_call(cpu, cs->exception_index)) { 9915 arm_handle_psci_call(cpu); 9916 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); 9917 return; 9918 } 9919 9920 /* 9921 * Semihosting semantics depend on the register width of the code 9922 * that caused the exception, not the target exception level, so 9923 * must be handled here. 9924 */ 9925 #ifdef CONFIG_TCG 9926 if (cs->exception_index == EXCP_SEMIHOST) { 9927 handle_semihosting(cs); 9928 return; 9929 } 9930 #endif 9931 9932 /* Hooks may change global state so BQL should be held, also the 9933 * BQL needs to be held for any modification of 9934 * cs->interrupt_request. 9935 */ 9936 g_assert(qemu_mutex_iothread_locked()); 9937 9938 arm_call_pre_el_change_hook(cpu); 9939 9940 assert(!excp_is_internal(cs->exception_index)); 9941 if (arm_el_is_aa64(env, new_el)) { 9942 arm_cpu_do_interrupt_aarch64(cs); 9943 } else { 9944 arm_cpu_do_interrupt_aarch32(cs); 9945 } 9946 9947 arm_call_el_change_hook(cpu); 9948 9949 if (!kvm_enabled()) { 9950 cs->interrupt_request |= CPU_INTERRUPT_EXITTB; 9951 } 9952 } 9953 #endif /* !CONFIG_USER_ONLY */ 9954 9955 uint64_t arm_sctlr(CPUARMState *env, int el) 9956 { 9957 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0. */ 9958 if (el == 0) { 9959 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0); 9960 el = (mmu_idx == ARMMMUIdx_E20_0 ? 2 : 1); 9961 } 9962 return env->cp15.sctlr_el[el]; 9963 } 9964 9965 /* Return the SCTLR value which controls this address translation regime */ 9966 static inline uint64_t regime_sctlr(CPUARMState *env, ARMMMUIdx mmu_idx) 9967 { 9968 return env->cp15.sctlr_el[regime_el(env, mmu_idx)]; 9969 } 9970 9971 #ifndef CONFIG_USER_ONLY 9972 9973 /* Return true if the specified stage of address translation is disabled */ 9974 static inline bool regime_translation_disabled(CPUARMState *env, 9975 ARMMMUIdx mmu_idx) 9976 { 9977 if (arm_feature(env, ARM_FEATURE_M)) { 9978 switch (env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] & 9979 (R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK)) { 9980 case R_V7M_MPU_CTRL_ENABLE_MASK: 9981 /* Enabled, but not for HardFault and NMI */ 9982 return mmu_idx & ARM_MMU_IDX_M_NEGPRI; 9983 case R_V7M_MPU_CTRL_ENABLE_MASK | R_V7M_MPU_CTRL_HFNMIENA_MASK: 9984 /* Enabled for all cases */ 9985 return false; 9986 case 0: 9987 default: 9988 /* HFNMIENA set and ENABLE clear is UNPREDICTABLE, but 9989 * we warned about that in armv7m_nvic.c when the guest set it. 9990 */ 9991 return true; 9992 } 9993 } 9994 9995 if (mmu_idx == ARMMMUIdx_Stage2) { 9996 /* HCR.DC means HCR.VM behaves as 1 */ 9997 return (env->cp15.hcr_el2 & (HCR_DC | HCR_VM)) == 0; 9998 } 9999 10000 if (env->cp15.hcr_el2 & HCR_TGE) { 10001 /* TGE means that NS EL0/1 act as if SCTLR_EL1.M is zero */ 10002 if (!regime_is_secure(env, mmu_idx) && regime_el(env, mmu_idx) == 1) { 10003 return true; 10004 } 10005 } 10006 10007 if ((env->cp15.hcr_el2 & HCR_DC) && arm_mmu_idx_is_stage1_of_2(mmu_idx)) { 10008 /* HCR.DC means SCTLR_EL1.M behaves as 0 */ 10009 return true; 10010 } 10011 10012 return (regime_sctlr(env, mmu_idx) & SCTLR_M) == 0; 10013 } 10014 10015 static inline bool regime_translation_big_endian(CPUARMState *env, 10016 ARMMMUIdx mmu_idx) 10017 { 10018 return (regime_sctlr(env, mmu_idx) & SCTLR_EE) != 0; 10019 } 10020 10021 /* Return the TTBR associated with this translation regime */ 10022 static inline uint64_t regime_ttbr(CPUARMState *env, ARMMMUIdx mmu_idx, 10023 int ttbrn) 10024 { 10025 if (mmu_idx == ARMMMUIdx_Stage2) { 10026 return env->cp15.vttbr_el2; 10027 } 10028 if (ttbrn == 0) { 10029 return env->cp15.ttbr0_el[regime_el(env, mmu_idx)]; 10030 } else { 10031 return env->cp15.ttbr1_el[regime_el(env, mmu_idx)]; 10032 } 10033 } 10034 10035 #endif /* !CONFIG_USER_ONLY */ 10036 10037 /* Convert a possible stage1+2 MMU index into the appropriate 10038 * stage 1 MMU index 10039 */ 10040 static inline ARMMMUIdx stage_1_mmu_idx(ARMMMUIdx mmu_idx) 10041 { 10042 switch (mmu_idx) { 10043 case ARMMMUIdx_E10_0: 10044 return ARMMMUIdx_Stage1_E0; 10045 case ARMMMUIdx_E10_1: 10046 return ARMMMUIdx_Stage1_E1; 10047 case ARMMMUIdx_E10_1_PAN: 10048 return ARMMMUIdx_Stage1_E1_PAN; 10049 default: 10050 return mmu_idx; 10051 } 10052 } 10053 10054 /* Return true if the translation regime is using LPAE format page tables */ 10055 static inline bool regime_using_lpae_format(CPUARMState *env, 10056 ARMMMUIdx mmu_idx) 10057 { 10058 int el = regime_el(env, mmu_idx); 10059 if (el == 2 || arm_el_is_aa64(env, el)) { 10060 return true; 10061 } 10062 if (arm_feature(env, ARM_FEATURE_LPAE) 10063 && (regime_tcr(env, mmu_idx)->raw_tcr & TTBCR_EAE)) { 10064 return true; 10065 } 10066 return false; 10067 } 10068 10069 /* Returns true if the stage 1 translation regime is using LPAE format page 10070 * tables. Used when raising alignment exceptions, whose FSR changes depending 10071 * on whether the long or short descriptor format is in use. */ 10072 bool arm_s1_regime_using_lpae_format(CPUARMState *env, ARMMMUIdx mmu_idx) 10073 { 10074 mmu_idx = stage_1_mmu_idx(mmu_idx); 10075 10076 return regime_using_lpae_format(env, mmu_idx); 10077 } 10078 10079 #ifndef CONFIG_USER_ONLY 10080 static inline bool regime_is_user(CPUARMState *env, ARMMMUIdx mmu_idx) 10081 { 10082 switch (mmu_idx) { 10083 case ARMMMUIdx_SE10_0: 10084 case ARMMMUIdx_E20_0: 10085 case ARMMMUIdx_Stage1_E0: 10086 case ARMMMUIdx_MUser: 10087 case ARMMMUIdx_MSUser: 10088 case ARMMMUIdx_MUserNegPri: 10089 case ARMMMUIdx_MSUserNegPri: 10090 return true; 10091 default: 10092 return false; 10093 case ARMMMUIdx_E10_0: 10094 case ARMMMUIdx_E10_1: 10095 case ARMMMUIdx_E10_1_PAN: 10096 g_assert_not_reached(); 10097 } 10098 } 10099 10100 /* Translate section/page access permissions to page 10101 * R/W protection flags 10102 * 10103 * @env: CPUARMState 10104 * @mmu_idx: MMU index indicating required translation regime 10105 * @ap: The 3-bit access permissions (AP[2:0]) 10106 * @domain_prot: The 2-bit domain access permissions 10107 */ 10108 static inline int ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, 10109 int ap, int domain_prot) 10110 { 10111 bool is_user = regime_is_user(env, mmu_idx); 10112 10113 if (domain_prot == 3) { 10114 return PAGE_READ | PAGE_WRITE; 10115 } 10116 10117 switch (ap) { 10118 case 0: 10119 if (arm_feature(env, ARM_FEATURE_V7)) { 10120 return 0; 10121 } 10122 switch (regime_sctlr(env, mmu_idx) & (SCTLR_S | SCTLR_R)) { 10123 case SCTLR_S: 10124 return is_user ? 0 : PAGE_READ; 10125 case SCTLR_R: 10126 return PAGE_READ; 10127 default: 10128 return 0; 10129 } 10130 case 1: 10131 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 10132 case 2: 10133 if (is_user) { 10134 return PAGE_READ; 10135 } else { 10136 return PAGE_READ | PAGE_WRITE; 10137 } 10138 case 3: 10139 return PAGE_READ | PAGE_WRITE; 10140 case 4: /* Reserved. */ 10141 return 0; 10142 case 5: 10143 return is_user ? 0 : PAGE_READ; 10144 case 6: 10145 return PAGE_READ; 10146 case 7: 10147 if (!arm_feature(env, ARM_FEATURE_V6K)) { 10148 return 0; 10149 } 10150 return PAGE_READ; 10151 default: 10152 g_assert_not_reached(); 10153 } 10154 } 10155 10156 /* Translate section/page access permissions to page 10157 * R/W protection flags. 10158 * 10159 * @ap: The 2-bit simple AP (AP[2:1]) 10160 * @is_user: TRUE if accessing from PL0 10161 */ 10162 static inline int simple_ap_to_rw_prot_is_user(int ap, bool is_user) 10163 { 10164 switch (ap) { 10165 case 0: 10166 return is_user ? 0 : PAGE_READ | PAGE_WRITE; 10167 case 1: 10168 return PAGE_READ | PAGE_WRITE; 10169 case 2: 10170 return is_user ? 0 : PAGE_READ; 10171 case 3: 10172 return PAGE_READ; 10173 default: 10174 g_assert_not_reached(); 10175 } 10176 } 10177 10178 static inline int 10179 simple_ap_to_rw_prot(CPUARMState *env, ARMMMUIdx mmu_idx, int ap) 10180 { 10181 return simple_ap_to_rw_prot_is_user(ap, regime_is_user(env, mmu_idx)); 10182 } 10183 10184 /* Translate S2 section/page access permissions to protection flags 10185 * 10186 * @env: CPUARMState 10187 * @s2ap: The 2-bit stage2 access permissions (S2AP) 10188 * @xn: XN (execute-never) bits 10189 * @s1_is_el0: true if this is S2 of an S1+2 walk for EL0 10190 */ 10191 static int get_S2prot(CPUARMState *env, int s2ap, int xn, bool s1_is_el0) 10192 { 10193 int prot = 0; 10194 10195 if (s2ap & 1) { 10196 prot |= PAGE_READ; 10197 } 10198 if (s2ap & 2) { 10199 prot |= PAGE_WRITE; 10200 } 10201 10202 if (cpu_isar_feature(any_tts2uxn, env_archcpu(env))) { 10203 switch (xn) { 10204 case 0: 10205 prot |= PAGE_EXEC; 10206 break; 10207 case 1: 10208 if (s1_is_el0) { 10209 prot |= PAGE_EXEC; 10210 } 10211 break; 10212 case 2: 10213 break; 10214 case 3: 10215 if (!s1_is_el0) { 10216 prot |= PAGE_EXEC; 10217 } 10218 break; 10219 default: 10220 g_assert_not_reached(); 10221 } 10222 } else { 10223 if (!extract32(xn, 1, 1)) { 10224 if (arm_el_is_aa64(env, 2) || prot & PAGE_READ) { 10225 prot |= PAGE_EXEC; 10226 } 10227 } 10228 } 10229 return prot; 10230 } 10231 10232 /* Translate section/page access permissions to protection flags 10233 * 10234 * @env: CPUARMState 10235 * @mmu_idx: MMU index indicating required translation regime 10236 * @is_aa64: TRUE if AArch64 10237 * @ap: The 2-bit simple AP (AP[2:1]) 10238 * @ns: NS (non-secure) bit 10239 * @xn: XN (execute-never) bit 10240 * @pxn: PXN (privileged execute-never) bit 10241 */ 10242 static int get_S1prot(CPUARMState *env, ARMMMUIdx mmu_idx, bool is_aa64, 10243 int ap, int ns, int xn, int pxn) 10244 { 10245 bool is_user = regime_is_user(env, mmu_idx); 10246 int prot_rw, user_rw; 10247 bool have_wxn; 10248 int wxn = 0; 10249 10250 assert(mmu_idx != ARMMMUIdx_Stage2); 10251 10252 user_rw = simple_ap_to_rw_prot_is_user(ap, true); 10253 if (is_user) { 10254 prot_rw = user_rw; 10255 } else { 10256 if (user_rw && regime_is_pan(env, mmu_idx)) { 10257 /* PAN forbids data accesses but doesn't affect insn fetch */ 10258 prot_rw = 0; 10259 } else { 10260 prot_rw = simple_ap_to_rw_prot_is_user(ap, false); 10261 } 10262 } 10263 10264 if (ns && arm_is_secure(env) && (env->cp15.scr_el3 & SCR_SIF)) { 10265 return prot_rw; 10266 } 10267 10268 /* TODO have_wxn should be replaced with 10269 * ARM_FEATURE_V8 || (ARM_FEATURE_V7 && ARM_FEATURE_EL2) 10270 * when ARM_FEATURE_EL2 starts getting set. For now we assume all LPAE 10271 * compatible processors have EL2, which is required for [U]WXN. 10272 */ 10273 have_wxn = arm_feature(env, ARM_FEATURE_LPAE); 10274 10275 if (have_wxn) { 10276 wxn = regime_sctlr(env, mmu_idx) & SCTLR_WXN; 10277 } 10278 10279 if (is_aa64) { 10280 if (regime_has_2_ranges(mmu_idx) && !is_user) { 10281 xn = pxn || (user_rw & PAGE_WRITE); 10282 } 10283 } else if (arm_feature(env, ARM_FEATURE_V7)) { 10284 switch (regime_el(env, mmu_idx)) { 10285 case 1: 10286 case 3: 10287 if (is_user) { 10288 xn = xn || !(user_rw & PAGE_READ); 10289 } else { 10290 int uwxn = 0; 10291 if (have_wxn) { 10292 uwxn = regime_sctlr(env, mmu_idx) & SCTLR_UWXN; 10293 } 10294 xn = xn || !(prot_rw & PAGE_READ) || pxn || 10295 (uwxn && (user_rw & PAGE_WRITE)); 10296 } 10297 break; 10298 case 2: 10299 break; 10300 } 10301 } else { 10302 xn = wxn = 0; 10303 } 10304 10305 if (xn || (wxn && (prot_rw & PAGE_WRITE))) { 10306 return prot_rw; 10307 } 10308 return prot_rw | PAGE_EXEC; 10309 } 10310 10311 static bool get_level1_table_address(CPUARMState *env, ARMMMUIdx mmu_idx, 10312 uint32_t *table, uint32_t address) 10313 { 10314 /* Note that we can only get here for an AArch32 PL0/PL1 lookup */ 10315 TCR *tcr = regime_tcr(env, mmu_idx); 10316 10317 if (address & tcr->mask) { 10318 if (tcr->raw_tcr & TTBCR_PD1) { 10319 /* Translation table walk disabled for TTBR1 */ 10320 return false; 10321 } 10322 *table = regime_ttbr(env, mmu_idx, 1) & 0xffffc000; 10323 } else { 10324 if (tcr->raw_tcr & TTBCR_PD0) { 10325 /* Translation table walk disabled for TTBR0 */ 10326 return false; 10327 } 10328 *table = regime_ttbr(env, mmu_idx, 0) & tcr->base_mask; 10329 } 10330 *table |= (address >> 18) & 0x3ffc; 10331 return true; 10332 } 10333 10334 /* Translate a S1 pagetable walk through S2 if needed. */ 10335 static hwaddr S1_ptw_translate(CPUARMState *env, ARMMMUIdx mmu_idx, 10336 hwaddr addr, MemTxAttrs txattrs, 10337 ARMMMUFaultInfo *fi) 10338 { 10339 if (arm_mmu_idx_is_stage1_of_2(mmu_idx) && 10340 !regime_translation_disabled(env, ARMMMUIdx_Stage2)) { 10341 target_ulong s2size; 10342 hwaddr s2pa; 10343 int s2prot; 10344 int ret; 10345 ARMCacheAttrs cacheattrs = {}; 10346 10347 ret = get_phys_addr_lpae(env, addr, MMU_DATA_LOAD, ARMMMUIdx_Stage2, 10348 false, 10349 &s2pa, &txattrs, &s2prot, &s2size, fi, 10350 &cacheattrs); 10351 if (ret) { 10352 assert(fi->type != ARMFault_None); 10353 fi->s2addr = addr; 10354 fi->stage2 = true; 10355 fi->s1ptw = true; 10356 return ~0; 10357 } 10358 if ((env->cp15.hcr_el2 & HCR_PTW) && (cacheattrs.attrs & 0xf0) == 0) { 10359 /* 10360 * PTW set and S1 walk touched S2 Device memory: 10361 * generate Permission fault. 10362 */ 10363 fi->type = ARMFault_Permission; 10364 fi->s2addr = addr; 10365 fi->stage2 = true; 10366 fi->s1ptw = true; 10367 return ~0; 10368 } 10369 addr = s2pa; 10370 } 10371 return addr; 10372 } 10373 10374 /* All loads done in the course of a page table walk go through here. */ 10375 static uint32_t arm_ldl_ptw(CPUState *cs, hwaddr addr, bool is_secure, 10376 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 10377 { 10378 ARMCPU *cpu = ARM_CPU(cs); 10379 CPUARMState *env = &cpu->env; 10380 MemTxAttrs attrs = {}; 10381 MemTxResult result = MEMTX_OK; 10382 AddressSpace *as; 10383 uint32_t data; 10384 10385 attrs.secure = is_secure; 10386 as = arm_addressspace(cs, attrs); 10387 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 10388 if (fi->s1ptw) { 10389 return 0; 10390 } 10391 if (regime_translation_big_endian(env, mmu_idx)) { 10392 data = address_space_ldl_be(as, addr, attrs, &result); 10393 } else { 10394 data = address_space_ldl_le(as, addr, attrs, &result); 10395 } 10396 if (result == MEMTX_OK) { 10397 return data; 10398 } 10399 fi->type = ARMFault_SyncExternalOnWalk; 10400 fi->ea = arm_extabort_type(result); 10401 return 0; 10402 } 10403 10404 static uint64_t arm_ldq_ptw(CPUState *cs, hwaddr addr, bool is_secure, 10405 ARMMMUIdx mmu_idx, ARMMMUFaultInfo *fi) 10406 { 10407 ARMCPU *cpu = ARM_CPU(cs); 10408 CPUARMState *env = &cpu->env; 10409 MemTxAttrs attrs = {}; 10410 MemTxResult result = MEMTX_OK; 10411 AddressSpace *as; 10412 uint64_t data; 10413 10414 attrs.secure = is_secure; 10415 as = arm_addressspace(cs, attrs); 10416 addr = S1_ptw_translate(env, mmu_idx, addr, attrs, fi); 10417 if (fi->s1ptw) { 10418 return 0; 10419 } 10420 if (regime_translation_big_endian(env, mmu_idx)) { 10421 data = address_space_ldq_be(as, addr, attrs, &result); 10422 } else { 10423 data = address_space_ldq_le(as, addr, attrs, &result); 10424 } 10425 if (result == MEMTX_OK) { 10426 return data; 10427 } 10428 fi->type = ARMFault_SyncExternalOnWalk; 10429 fi->ea = arm_extabort_type(result); 10430 return 0; 10431 } 10432 10433 static bool get_phys_addr_v5(CPUARMState *env, uint32_t address, 10434 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10435 hwaddr *phys_ptr, int *prot, 10436 target_ulong *page_size, 10437 ARMMMUFaultInfo *fi) 10438 { 10439 CPUState *cs = env_cpu(env); 10440 int level = 1; 10441 uint32_t table; 10442 uint32_t desc; 10443 int type; 10444 int ap; 10445 int domain = 0; 10446 int domain_prot; 10447 hwaddr phys_addr; 10448 uint32_t dacr; 10449 10450 /* Pagetable walk. */ 10451 /* Lookup l1 descriptor. */ 10452 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 10453 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 10454 fi->type = ARMFault_Translation; 10455 goto do_fault; 10456 } 10457 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 10458 mmu_idx, fi); 10459 if (fi->type != ARMFault_None) { 10460 goto do_fault; 10461 } 10462 type = (desc & 3); 10463 domain = (desc >> 5) & 0x0f; 10464 if (regime_el(env, mmu_idx) == 1) { 10465 dacr = env->cp15.dacr_ns; 10466 } else { 10467 dacr = env->cp15.dacr_s; 10468 } 10469 domain_prot = (dacr >> (domain * 2)) & 3; 10470 if (type == 0) { 10471 /* Section translation fault. */ 10472 fi->type = ARMFault_Translation; 10473 goto do_fault; 10474 } 10475 if (type != 2) { 10476 level = 2; 10477 } 10478 if (domain_prot == 0 || domain_prot == 2) { 10479 fi->type = ARMFault_Domain; 10480 goto do_fault; 10481 } 10482 if (type == 2) { 10483 /* 1Mb section. */ 10484 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 10485 ap = (desc >> 10) & 3; 10486 *page_size = 1024 * 1024; 10487 } else { 10488 /* Lookup l2 entry. */ 10489 if (type == 1) { 10490 /* Coarse pagetable. */ 10491 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 10492 } else { 10493 /* Fine pagetable. */ 10494 table = (desc & 0xfffff000) | ((address >> 8) & 0xffc); 10495 } 10496 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 10497 mmu_idx, fi); 10498 if (fi->type != ARMFault_None) { 10499 goto do_fault; 10500 } 10501 switch (desc & 3) { 10502 case 0: /* Page translation fault. */ 10503 fi->type = ARMFault_Translation; 10504 goto do_fault; 10505 case 1: /* 64k page. */ 10506 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 10507 ap = (desc >> (4 + ((address >> 13) & 6))) & 3; 10508 *page_size = 0x10000; 10509 break; 10510 case 2: /* 4k page. */ 10511 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 10512 ap = (desc >> (4 + ((address >> 9) & 6))) & 3; 10513 *page_size = 0x1000; 10514 break; 10515 case 3: /* 1k page, or ARMv6/XScale "extended small (4k) page" */ 10516 if (type == 1) { 10517 /* ARMv6/XScale extended small page format */ 10518 if (arm_feature(env, ARM_FEATURE_XSCALE) 10519 || arm_feature(env, ARM_FEATURE_V6)) { 10520 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 10521 *page_size = 0x1000; 10522 } else { 10523 /* UNPREDICTABLE in ARMv5; we choose to take a 10524 * page translation fault. 10525 */ 10526 fi->type = ARMFault_Translation; 10527 goto do_fault; 10528 } 10529 } else { 10530 phys_addr = (desc & 0xfffffc00) | (address & 0x3ff); 10531 *page_size = 0x400; 10532 } 10533 ap = (desc >> 4) & 3; 10534 break; 10535 default: 10536 /* Never happens, but compiler isn't smart enough to tell. */ 10537 abort(); 10538 } 10539 } 10540 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 10541 *prot |= *prot ? PAGE_EXEC : 0; 10542 if (!(*prot & (1 << access_type))) { 10543 /* Access permission fault. */ 10544 fi->type = ARMFault_Permission; 10545 goto do_fault; 10546 } 10547 *phys_ptr = phys_addr; 10548 return false; 10549 do_fault: 10550 fi->domain = domain; 10551 fi->level = level; 10552 return true; 10553 } 10554 10555 static bool get_phys_addr_v6(CPUARMState *env, uint32_t address, 10556 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10557 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 10558 target_ulong *page_size, ARMMMUFaultInfo *fi) 10559 { 10560 CPUState *cs = env_cpu(env); 10561 ARMCPU *cpu = env_archcpu(env); 10562 int level = 1; 10563 uint32_t table; 10564 uint32_t desc; 10565 uint32_t xn; 10566 uint32_t pxn = 0; 10567 int type; 10568 int ap; 10569 int domain = 0; 10570 int domain_prot; 10571 hwaddr phys_addr; 10572 uint32_t dacr; 10573 bool ns; 10574 10575 /* Pagetable walk. */ 10576 /* Lookup l1 descriptor. */ 10577 if (!get_level1_table_address(env, mmu_idx, &table, address)) { 10578 /* Section translation fault if page walk is disabled by PD0 or PD1 */ 10579 fi->type = ARMFault_Translation; 10580 goto do_fault; 10581 } 10582 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 10583 mmu_idx, fi); 10584 if (fi->type != ARMFault_None) { 10585 goto do_fault; 10586 } 10587 type = (desc & 3); 10588 if (type == 0 || (type == 3 && !cpu_isar_feature(aa32_pxn, cpu))) { 10589 /* Section translation fault, or attempt to use the encoding 10590 * which is Reserved on implementations without PXN. 10591 */ 10592 fi->type = ARMFault_Translation; 10593 goto do_fault; 10594 } 10595 if ((type == 1) || !(desc & (1 << 18))) { 10596 /* Page or Section. */ 10597 domain = (desc >> 5) & 0x0f; 10598 } 10599 if (regime_el(env, mmu_idx) == 1) { 10600 dacr = env->cp15.dacr_ns; 10601 } else { 10602 dacr = env->cp15.dacr_s; 10603 } 10604 if (type == 1) { 10605 level = 2; 10606 } 10607 domain_prot = (dacr >> (domain * 2)) & 3; 10608 if (domain_prot == 0 || domain_prot == 2) { 10609 /* Section or Page domain fault */ 10610 fi->type = ARMFault_Domain; 10611 goto do_fault; 10612 } 10613 if (type != 1) { 10614 if (desc & (1 << 18)) { 10615 /* Supersection. */ 10616 phys_addr = (desc & 0xff000000) | (address & 0x00ffffff); 10617 phys_addr |= (uint64_t)extract32(desc, 20, 4) << 32; 10618 phys_addr |= (uint64_t)extract32(desc, 5, 4) << 36; 10619 *page_size = 0x1000000; 10620 } else { 10621 /* Section. */ 10622 phys_addr = (desc & 0xfff00000) | (address & 0x000fffff); 10623 *page_size = 0x100000; 10624 } 10625 ap = ((desc >> 10) & 3) | ((desc >> 13) & 4); 10626 xn = desc & (1 << 4); 10627 pxn = desc & 1; 10628 ns = extract32(desc, 19, 1); 10629 } else { 10630 if (cpu_isar_feature(aa32_pxn, cpu)) { 10631 pxn = (desc >> 2) & 1; 10632 } 10633 ns = extract32(desc, 3, 1); 10634 /* Lookup l2 entry. */ 10635 table = (desc & 0xfffffc00) | ((address >> 10) & 0x3fc); 10636 desc = arm_ldl_ptw(cs, table, regime_is_secure(env, mmu_idx), 10637 mmu_idx, fi); 10638 if (fi->type != ARMFault_None) { 10639 goto do_fault; 10640 } 10641 ap = ((desc >> 4) & 3) | ((desc >> 7) & 4); 10642 switch (desc & 3) { 10643 case 0: /* Page translation fault. */ 10644 fi->type = ARMFault_Translation; 10645 goto do_fault; 10646 case 1: /* 64k page. */ 10647 phys_addr = (desc & 0xffff0000) | (address & 0xffff); 10648 xn = desc & (1 << 15); 10649 *page_size = 0x10000; 10650 break; 10651 case 2: case 3: /* 4k page. */ 10652 phys_addr = (desc & 0xfffff000) | (address & 0xfff); 10653 xn = desc & 1; 10654 *page_size = 0x1000; 10655 break; 10656 default: 10657 /* Never happens, but compiler isn't smart enough to tell. */ 10658 abort(); 10659 } 10660 } 10661 if (domain_prot == 3) { 10662 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 10663 } else { 10664 if (pxn && !regime_is_user(env, mmu_idx)) { 10665 xn = 1; 10666 } 10667 if (xn && access_type == MMU_INST_FETCH) { 10668 fi->type = ARMFault_Permission; 10669 goto do_fault; 10670 } 10671 10672 if (arm_feature(env, ARM_FEATURE_V6K) && 10673 (regime_sctlr(env, mmu_idx) & SCTLR_AFE)) { 10674 /* The simplified model uses AP[0] as an access control bit. */ 10675 if ((ap & 1) == 0) { 10676 /* Access flag fault. */ 10677 fi->type = ARMFault_AccessFlag; 10678 goto do_fault; 10679 } 10680 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap >> 1); 10681 } else { 10682 *prot = ap_to_rw_prot(env, mmu_idx, ap, domain_prot); 10683 } 10684 if (*prot && !xn) { 10685 *prot |= PAGE_EXEC; 10686 } 10687 if (!(*prot & (1 << access_type))) { 10688 /* Access permission fault. */ 10689 fi->type = ARMFault_Permission; 10690 goto do_fault; 10691 } 10692 } 10693 if (ns) { 10694 /* The NS bit will (as required by the architecture) have no effect if 10695 * the CPU doesn't support TZ or this is a non-secure translation 10696 * regime, because the attribute will already be non-secure. 10697 */ 10698 attrs->secure = false; 10699 } 10700 *phys_ptr = phys_addr; 10701 return false; 10702 do_fault: 10703 fi->domain = domain; 10704 fi->level = level; 10705 return true; 10706 } 10707 10708 /* 10709 * check_s2_mmu_setup 10710 * @cpu: ARMCPU 10711 * @is_aa64: True if the translation regime is in AArch64 state 10712 * @startlevel: Suggested starting level 10713 * @inputsize: Bitsize of IPAs 10714 * @stride: Page-table stride (See the ARM ARM) 10715 * 10716 * Returns true if the suggested S2 translation parameters are OK and 10717 * false otherwise. 10718 */ 10719 static bool check_s2_mmu_setup(ARMCPU *cpu, bool is_aa64, int level, 10720 int inputsize, int stride) 10721 { 10722 const int grainsize = stride + 3; 10723 int startsizecheck; 10724 10725 /* Negative levels are never allowed. */ 10726 if (level < 0) { 10727 return false; 10728 } 10729 10730 startsizecheck = inputsize - ((3 - level) * stride + grainsize); 10731 if (startsizecheck < 1 || startsizecheck > stride + 4) { 10732 return false; 10733 } 10734 10735 if (is_aa64) { 10736 CPUARMState *env = &cpu->env; 10737 unsigned int pamax = arm_pamax(cpu); 10738 10739 switch (stride) { 10740 case 13: /* 64KB Pages. */ 10741 if (level == 0 || (level == 1 && pamax <= 42)) { 10742 return false; 10743 } 10744 break; 10745 case 11: /* 16KB Pages. */ 10746 if (level == 0 || (level == 1 && pamax <= 40)) { 10747 return false; 10748 } 10749 break; 10750 case 9: /* 4KB Pages. */ 10751 if (level == 0 && pamax <= 42) { 10752 return false; 10753 } 10754 break; 10755 default: 10756 g_assert_not_reached(); 10757 } 10758 10759 /* Inputsize checks. */ 10760 if (inputsize > pamax && 10761 (arm_el_is_aa64(env, 1) || inputsize > 40)) { 10762 /* This is CONSTRAINED UNPREDICTABLE and we choose to fault. */ 10763 return false; 10764 } 10765 } else { 10766 /* AArch32 only supports 4KB pages. Assert on that. */ 10767 assert(stride == 9); 10768 10769 if (level == 0) { 10770 return false; 10771 } 10772 } 10773 return true; 10774 } 10775 10776 /* Translate from the 4-bit stage 2 representation of 10777 * memory attributes (without cache-allocation hints) to 10778 * the 8-bit representation of the stage 1 MAIR registers 10779 * (which includes allocation hints). 10780 * 10781 * ref: shared/translation/attrs/S2AttrDecode() 10782 * .../S2ConvertAttrsHints() 10783 */ 10784 static uint8_t convert_stage2_attrs(CPUARMState *env, uint8_t s2attrs) 10785 { 10786 uint8_t hiattr = extract32(s2attrs, 2, 2); 10787 uint8_t loattr = extract32(s2attrs, 0, 2); 10788 uint8_t hihint = 0, lohint = 0; 10789 10790 if (hiattr != 0) { /* normal memory */ 10791 if ((env->cp15.hcr_el2 & HCR_CD) != 0) { /* cache disabled */ 10792 hiattr = loattr = 1; /* non-cacheable */ 10793 } else { 10794 if (hiattr != 1) { /* Write-through or write-back */ 10795 hihint = 3; /* RW allocate */ 10796 } 10797 if (loattr != 1) { /* Write-through or write-back */ 10798 lohint = 3; /* RW allocate */ 10799 } 10800 } 10801 } 10802 10803 return (hiattr << 6) | (hihint << 4) | (loattr << 2) | lohint; 10804 } 10805 #endif /* !CONFIG_USER_ONLY */ 10806 10807 static int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx) 10808 { 10809 if (regime_has_2_ranges(mmu_idx)) { 10810 return extract64(tcr, 37, 2); 10811 } else if (mmu_idx == ARMMMUIdx_Stage2) { 10812 return 0; /* VTCR_EL2 */ 10813 } else { 10814 /* Replicate the single TBI bit so we always have 2 bits. */ 10815 return extract32(tcr, 20, 1) * 3; 10816 } 10817 } 10818 10819 static int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx) 10820 { 10821 if (regime_has_2_ranges(mmu_idx)) { 10822 return extract64(tcr, 51, 2); 10823 } else if (mmu_idx == ARMMMUIdx_Stage2) { 10824 return 0; /* VTCR_EL2 */ 10825 } else { 10826 /* Replicate the single TBID bit so we always have 2 bits. */ 10827 return extract32(tcr, 29, 1) * 3; 10828 } 10829 } 10830 10831 static int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx) 10832 { 10833 if (regime_has_2_ranges(mmu_idx)) { 10834 return extract64(tcr, 57, 2); 10835 } else { 10836 /* Replicate the single TCMA bit so we always have 2 bits. */ 10837 return extract32(tcr, 30, 1) * 3; 10838 } 10839 } 10840 10841 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va, 10842 ARMMMUIdx mmu_idx, bool data) 10843 { 10844 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr; 10845 bool epd, hpd, using16k, using64k; 10846 int select, tsz, tbi, max_tsz; 10847 10848 if (!regime_has_2_ranges(mmu_idx)) { 10849 select = 0; 10850 tsz = extract32(tcr, 0, 6); 10851 using64k = extract32(tcr, 14, 1); 10852 using16k = extract32(tcr, 15, 1); 10853 if (mmu_idx == ARMMMUIdx_Stage2) { 10854 /* VTCR_EL2 */ 10855 hpd = false; 10856 } else { 10857 hpd = extract32(tcr, 24, 1); 10858 } 10859 epd = false; 10860 } else { 10861 /* 10862 * Bit 55 is always between the two regions, and is canonical for 10863 * determining if address tagging is enabled. 10864 */ 10865 select = extract64(va, 55, 1); 10866 if (!select) { 10867 tsz = extract32(tcr, 0, 6); 10868 epd = extract32(tcr, 7, 1); 10869 using64k = extract32(tcr, 14, 1); 10870 using16k = extract32(tcr, 15, 1); 10871 hpd = extract64(tcr, 41, 1); 10872 } else { 10873 int tg = extract32(tcr, 30, 2); 10874 using16k = tg == 1; 10875 using64k = tg == 3; 10876 tsz = extract32(tcr, 16, 6); 10877 epd = extract32(tcr, 23, 1); 10878 hpd = extract64(tcr, 42, 1); 10879 } 10880 } 10881 10882 if (cpu_isar_feature(aa64_st, env_archcpu(env))) { 10883 max_tsz = 48 - using64k; 10884 } else { 10885 max_tsz = 39; 10886 } 10887 10888 tsz = MIN(tsz, max_tsz); 10889 tsz = MAX(tsz, 16); /* TODO: ARMv8.2-LVA */ 10890 10891 /* Present TBI as a composite with TBID. */ 10892 tbi = aa64_va_parameter_tbi(tcr, mmu_idx); 10893 if (!data) { 10894 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx); 10895 } 10896 tbi = (tbi >> select) & 1; 10897 10898 return (ARMVAParameters) { 10899 .tsz = tsz, 10900 .select = select, 10901 .tbi = tbi, 10902 .epd = epd, 10903 .hpd = hpd, 10904 .using16k = using16k, 10905 .using64k = using64k, 10906 }; 10907 } 10908 10909 #ifndef CONFIG_USER_ONLY 10910 static ARMVAParameters aa32_va_parameters(CPUARMState *env, uint32_t va, 10911 ARMMMUIdx mmu_idx) 10912 { 10913 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr; 10914 uint32_t el = regime_el(env, mmu_idx); 10915 int select, tsz; 10916 bool epd, hpd; 10917 10918 if (mmu_idx == ARMMMUIdx_Stage2) { 10919 /* VTCR */ 10920 bool sext = extract32(tcr, 4, 1); 10921 bool sign = extract32(tcr, 3, 1); 10922 10923 /* 10924 * If the sign-extend bit is not the same as t0sz[3], the result 10925 * is unpredictable. Flag this as a guest error. 10926 */ 10927 if (sign != sext) { 10928 qemu_log_mask(LOG_GUEST_ERROR, 10929 "AArch32: VTCR.S / VTCR.T0SZ[3] mismatch\n"); 10930 } 10931 tsz = sextract32(tcr, 0, 4) + 8; 10932 select = 0; 10933 hpd = false; 10934 epd = false; 10935 } else if (el == 2) { 10936 /* HTCR */ 10937 tsz = extract32(tcr, 0, 3); 10938 select = 0; 10939 hpd = extract64(tcr, 24, 1); 10940 epd = false; 10941 } else { 10942 int t0sz = extract32(tcr, 0, 3); 10943 int t1sz = extract32(tcr, 16, 3); 10944 10945 if (t1sz == 0) { 10946 select = va > (0xffffffffu >> t0sz); 10947 } else { 10948 /* Note that we will detect errors later. */ 10949 select = va >= ~(0xffffffffu >> t1sz); 10950 } 10951 if (!select) { 10952 tsz = t0sz; 10953 epd = extract32(tcr, 7, 1); 10954 hpd = extract64(tcr, 41, 1); 10955 } else { 10956 tsz = t1sz; 10957 epd = extract32(tcr, 23, 1); 10958 hpd = extract64(tcr, 42, 1); 10959 } 10960 /* For aarch32, hpd0 is not enabled without t2e as well. */ 10961 hpd &= extract32(tcr, 6, 1); 10962 } 10963 10964 return (ARMVAParameters) { 10965 .tsz = tsz, 10966 .select = select, 10967 .epd = epd, 10968 .hpd = hpd, 10969 }; 10970 } 10971 10972 /** 10973 * get_phys_addr_lpae: perform one stage of page table walk, LPAE format 10974 * 10975 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs, 10976 * prot and page_size may not be filled in, and the populated fsr value provides 10977 * information on why the translation aborted, in the format of a long-format 10978 * DFSR/IFSR fault register, with the following caveats: 10979 * * the WnR bit is never set (the caller must do this). 10980 * 10981 * @env: CPUARMState 10982 * @address: virtual address to get physical address for 10983 * @access_type: MMU_DATA_LOAD, MMU_DATA_STORE or MMU_INST_FETCH 10984 * @mmu_idx: MMU index indicating required translation regime 10985 * @s1_is_el0: if @mmu_idx is ARMMMUIdx_Stage2 (so this is a stage 2 page table 10986 * walk), must be true if this is stage 2 of a stage 1+2 walk for an 10987 * EL0 access). If @mmu_idx is anything else, @s1_is_el0 is ignored. 10988 * @phys_ptr: set to the physical address corresponding to the virtual address 10989 * @attrs: set to the memory transaction attributes to use 10990 * @prot: set to the permissions for the page containing phys_ptr 10991 * @page_size_ptr: set to the size of the page containing phys_ptr 10992 * @fi: set to fault info if the translation fails 10993 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes 10994 */ 10995 static bool get_phys_addr_lpae(CPUARMState *env, uint64_t address, 10996 MMUAccessType access_type, ARMMMUIdx mmu_idx, 10997 bool s1_is_el0, 10998 hwaddr *phys_ptr, MemTxAttrs *txattrs, int *prot, 10999 target_ulong *page_size_ptr, 11000 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 11001 { 11002 ARMCPU *cpu = env_archcpu(env); 11003 CPUState *cs = CPU(cpu); 11004 /* Read an LPAE long-descriptor translation table. */ 11005 ARMFaultType fault_type = ARMFault_Translation; 11006 uint32_t level; 11007 ARMVAParameters param; 11008 uint64_t ttbr; 11009 hwaddr descaddr, indexmask, indexmask_grainsize; 11010 uint32_t tableattrs; 11011 target_ulong page_size; 11012 uint32_t attrs; 11013 int32_t stride; 11014 int addrsize, inputsize; 11015 TCR *tcr = regime_tcr(env, mmu_idx); 11016 int ap, ns, xn, pxn; 11017 uint32_t el = regime_el(env, mmu_idx); 11018 uint64_t descaddrmask; 11019 bool aarch64 = arm_el_is_aa64(env, el); 11020 bool guarded = false; 11021 11022 /* TODO: This code does not support shareability levels. */ 11023 if (aarch64) { 11024 param = aa64_va_parameters(env, address, mmu_idx, 11025 access_type != MMU_INST_FETCH); 11026 level = 0; 11027 addrsize = 64 - 8 * param.tbi; 11028 inputsize = 64 - param.tsz; 11029 } else { 11030 param = aa32_va_parameters(env, address, mmu_idx); 11031 level = 1; 11032 addrsize = (mmu_idx == ARMMMUIdx_Stage2 ? 40 : 32); 11033 inputsize = addrsize - param.tsz; 11034 } 11035 11036 /* 11037 * We determined the region when collecting the parameters, but we 11038 * have not yet validated that the address is valid for the region. 11039 * Extract the top bits and verify that they all match select. 11040 * 11041 * For aa32, if inputsize == addrsize, then we have selected the 11042 * region by exclusion in aa32_va_parameters and there is no more 11043 * validation to do here. 11044 */ 11045 if (inputsize < addrsize) { 11046 target_ulong top_bits = sextract64(address, inputsize, 11047 addrsize - inputsize); 11048 if (-top_bits != param.select) { 11049 /* The gap between the two regions is a Translation fault */ 11050 fault_type = ARMFault_Translation; 11051 goto do_fault; 11052 } 11053 } 11054 11055 if (param.using64k) { 11056 stride = 13; 11057 } else if (param.using16k) { 11058 stride = 11; 11059 } else { 11060 stride = 9; 11061 } 11062 11063 /* Note that QEMU ignores shareability and cacheability attributes, 11064 * so we don't need to do anything with the SH, ORGN, IRGN fields 11065 * in the TTBCR. Similarly, TTBCR:A1 selects whether we get the 11066 * ASID from TTBR0 or TTBR1, but QEMU's TLB doesn't currently 11067 * implement any ASID-like capability so we can ignore it (instead 11068 * we will always flush the TLB any time the ASID is changed). 11069 */ 11070 ttbr = regime_ttbr(env, mmu_idx, param.select); 11071 11072 /* Here we should have set up all the parameters for the translation: 11073 * inputsize, ttbr, epd, stride, tbi 11074 */ 11075 11076 if (param.epd) { 11077 /* Translation table walk disabled => Translation fault on TLB miss 11078 * Note: This is always 0 on 64-bit EL2 and EL3. 11079 */ 11080 goto do_fault; 11081 } 11082 11083 if (mmu_idx != ARMMMUIdx_Stage2) { 11084 /* The starting level depends on the virtual address size (which can 11085 * be up to 48 bits) and the translation granule size. It indicates 11086 * the number of strides (stride bits at a time) needed to 11087 * consume the bits of the input address. In the pseudocode this is: 11088 * level = 4 - RoundUp((inputsize - grainsize) / stride) 11089 * where their 'inputsize' is our 'inputsize', 'grainsize' is 11090 * our 'stride + 3' and 'stride' is our 'stride'. 11091 * Applying the usual "rounded up m/n is (m+n-1)/n" and simplifying: 11092 * = 4 - (inputsize - stride - 3 + stride - 1) / stride 11093 * = 4 - (inputsize - 4) / stride; 11094 */ 11095 level = 4 - (inputsize - 4) / stride; 11096 } else { 11097 /* For stage 2 translations the starting level is specified by the 11098 * VTCR_EL2.SL0 field (whose interpretation depends on the page size) 11099 */ 11100 uint32_t sl0 = extract32(tcr->raw_tcr, 6, 2); 11101 uint32_t startlevel; 11102 bool ok; 11103 11104 if (!aarch64 || stride == 9) { 11105 /* AArch32 or 4KB pages */ 11106 startlevel = 2 - sl0; 11107 11108 if (cpu_isar_feature(aa64_st, cpu)) { 11109 startlevel &= 3; 11110 } 11111 } else { 11112 /* 16KB or 64KB pages */ 11113 startlevel = 3 - sl0; 11114 } 11115 11116 /* Check that the starting level is valid. */ 11117 ok = check_s2_mmu_setup(cpu, aarch64, startlevel, 11118 inputsize, stride); 11119 if (!ok) { 11120 fault_type = ARMFault_Translation; 11121 goto do_fault; 11122 } 11123 level = startlevel; 11124 } 11125 11126 indexmask_grainsize = (1ULL << (stride + 3)) - 1; 11127 indexmask = (1ULL << (inputsize - (stride * (4 - level)))) - 1; 11128 11129 /* Now we can extract the actual base address from the TTBR */ 11130 descaddr = extract64(ttbr, 0, 48); 11131 /* 11132 * We rely on this masking to clear the RES0 bits at the bottom of the TTBR 11133 * and also to mask out CnP (bit 0) which could validly be non-zero. 11134 */ 11135 descaddr &= ~indexmask; 11136 11137 /* The address field in the descriptor goes up to bit 39 for ARMv7 11138 * but up to bit 47 for ARMv8, but we use the descaddrmask 11139 * up to bit 39 for AArch32, because we don't need other bits in that case 11140 * to construct next descriptor address (anyway they should be all zeroes). 11141 */ 11142 descaddrmask = ((1ull << (aarch64 ? 48 : 40)) - 1) & 11143 ~indexmask_grainsize; 11144 11145 /* Secure accesses start with the page table in secure memory and 11146 * can be downgraded to non-secure at any step. Non-secure accesses 11147 * remain non-secure. We implement this by just ORing in the NSTable/NS 11148 * bits at each step. 11149 */ 11150 tableattrs = regime_is_secure(env, mmu_idx) ? 0 : (1 << 4); 11151 for (;;) { 11152 uint64_t descriptor; 11153 bool nstable; 11154 11155 descaddr |= (address >> (stride * (4 - level))) & indexmask; 11156 descaddr &= ~7ULL; 11157 nstable = extract32(tableattrs, 4, 1); 11158 descriptor = arm_ldq_ptw(cs, descaddr, !nstable, mmu_idx, fi); 11159 if (fi->type != ARMFault_None) { 11160 goto do_fault; 11161 } 11162 11163 if (!(descriptor & 1) || 11164 (!(descriptor & 2) && (level == 3))) { 11165 /* Invalid, or the Reserved level 3 encoding */ 11166 goto do_fault; 11167 } 11168 descaddr = descriptor & descaddrmask; 11169 11170 if ((descriptor & 2) && (level < 3)) { 11171 /* Table entry. The top five bits are attributes which may 11172 * propagate down through lower levels of the table (and 11173 * which are all arranged so that 0 means "no effect", so 11174 * we can gather them up by ORing in the bits at each level). 11175 */ 11176 tableattrs |= extract64(descriptor, 59, 5); 11177 level++; 11178 indexmask = indexmask_grainsize; 11179 continue; 11180 } 11181 /* Block entry at level 1 or 2, or page entry at level 3. 11182 * These are basically the same thing, although the number 11183 * of bits we pull in from the vaddr varies. 11184 */ 11185 page_size = (1ULL << ((stride * (4 - level)) + 3)); 11186 descaddr |= (address & (page_size - 1)); 11187 /* Extract attributes from the descriptor */ 11188 attrs = extract64(descriptor, 2, 10) 11189 | (extract64(descriptor, 52, 12) << 10); 11190 11191 if (mmu_idx == ARMMMUIdx_Stage2) { 11192 /* Stage 2 table descriptors do not include any attribute fields */ 11193 break; 11194 } 11195 /* Merge in attributes from table descriptors */ 11196 attrs |= nstable << 3; /* NS */ 11197 guarded = extract64(descriptor, 50, 1); /* GP */ 11198 if (param.hpd) { 11199 /* HPD disables all the table attributes except NSTable. */ 11200 break; 11201 } 11202 attrs |= extract32(tableattrs, 0, 2) << 11; /* XN, PXN */ 11203 /* The sense of AP[1] vs APTable[0] is reversed, as APTable[0] == 1 11204 * means "force PL1 access only", which means forcing AP[1] to 0. 11205 */ 11206 attrs &= ~(extract32(tableattrs, 2, 1) << 4); /* !APT[0] => AP[1] */ 11207 attrs |= extract32(tableattrs, 3, 1) << 5; /* APT[1] => AP[2] */ 11208 break; 11209 } 11210 /* Here descaddr is the final physical address, and attributes 11211 * are all in attrs. 11212 */ 11213 fault_type = ARMFault_AccessFlag; 11214 if ((attrs & (1 << 8)) == 0) { 11215 /* Access flag */ 11216 goto do_fault; 11217 } 11218 11219 ap = extract32(attrs, 4, 2); 11220 11221 if (mmu_idx == ARMMMUIdx_Stage2) { 11222 ns = true; 11223 xn = extract32(attrs, 11, 2); 11224 *prot = get_S2prot(env, ap, xn, s1_is_el0); 11225 } else { 11226 ns = extract32(attrs, 3, 1); 11227 xn = extract32(attrs, 12, 1); 11228 pxn = extract32(attrs, 11, 1); 11229 *prot = get_S1prot(env, mmu_idx, aarch64, ap, ns, xn, pxn); 11230 } 11231 11232 fault_type = ARMFault_Permission; 11233 if (!(*prot & (1 << access_type))) { 11234 goto do_fault; 11235 } 11236 11237 if (ns) { 11238 /* The NS bit will (as required by the architecture) have no effect if 11239 * the CPU doesn't support TZ or this is a non-secure translation 11240 * regime, because the attribute will already be non-secure. 11241 */ 11242 txattrs->secure = false; 11243 } 11244 /* When in aarch64 mode, and BTI is enabled, remember GP in the IOTLB. */ 11245 if (aarch64 && guarded && cpu_isar_feature(aa64_bti, cpu)) { 11246 arm_tlb_bti_gp(txattrs) = true; 11247 } 11248 11249 if (mmu_idx == ARMMMUIdx_Stage2) { 11250 cacheattrs->attrs = convert_stage2_attrs(env, extract32(attrs, 0, 4)); 11251 } else { 11252 /* Index into MAIR registers for cache attributes */ 11253 uint8_t attrindx = extract32(attrs, 0, 3); 11254 uint64_t mair = env->cp15.mair_el[regime_el(env, mmu_idx)]; 11255 assert(attrindx <= 7); 11256 cacheattrs->attrs = extract64(mair, attrindx * 8, 8); 11257 } 11258 cacheattrs->shareability = extract32(attrs, 6, 2); 11259 11260 *phys_ptr = descaddr; 11261 *page_size_ptr = page_size; 11262 return false; 11263 11264 do_fault: 11265 fi->type = fault_type; 11266 fi->level = level; 11267 /* Tag the error as S2 for failed S1 PTW at S2 or ordinary S2. */ 11268 fi->stage2 = fi->s1ptw || (mmu_idx == ARMMMUIdx_Stage2); 11269 return true; 11270 } 11271 11272 static inline void get_phys_addr_pmsav7_default(CPUARMState *env, 11273 ARMMMUIdx mmu_idx, 11274 int32_t address, int *prot) 11275 { 11276 if (!arm_feature(env, ARM_FEATURE_M)) { 11277 *prot = PAGE_READ | PAGE_WRITE; 11278 switch (address) { 11279 case 0xF0000000 ... 0xFFFFFFFF: 11280 if (regime_sctlr(env, mmu_idx) & SCTLR_V) { 11281 /* hivecs execing is ok */ 11282 *prot |= PAGE_EXEC; 11283 } 11284 break; 11285 case 0x00000000 ... 0x7FFFFFFF: 11286 *prot |= PAGE_EXEC; 11287 break; 11288 } 11289 } else { 11290 /* Default system address map for M profile cores. 11291 * The architecture specifies which regions are execute-never; 11292 * at the MPU level no other checks are defined. 11293 */ 11294 switch (address) { 11295 case 0x00000000 ... 0x1fffffff: /* ROM */ 11296 case 0x20000000 ... 0x3fffffff: /* SRAM */ 11297 case 0x60000000 ... 0x7fffffff: /* RAM */ 11298 case 0x80000000 ... 0x9fffffff: /* RAM */ 11299 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 11300 break; 11301 case 0x40000000 ... 0x5fffffff: /* Peripheral */ 11302 case 0xa0000000 ... 0xbfffffff: /* Device */ 11303 case 0xc0000000 ... 0xdfffffff: /* Device */ 11304 case 0xe0000000 ... 0xffffffff: /* System */ 11305 *prot = PAGE_READ | PAGE_WRITE; 11306 break; 11307 default: 11308 g_assert_not_reached(); 11309 } 11310 } 11311 } 11312 11313 static bool pmsav7_use_background_region(ARMCPU *cpu, 11314 ARMMMUIdx mmu_idx, bool is_user) 11315 { 11316 /* Return true if we should use the default memory map as a 11317 * "background" region if there are no hits against any MPU regions. 11318 */ 11319 CPUARMState *env = &cpu->env; 11320 11321 if (is_user) { 11322 return false; 11323 } 11324 11325 if (arm_feature(env, ARM_FEATURE_M)) { 11326 return env->v7m.mpu_ctrl[regime_is_secure(env, mmu_idx)] 11327 & R_V7M_MPU_CTRL_PRIVDEFENA_MASK; 11328 } else { 11329 return regime_sctlr(env, mmu_idx) & SCTLR_BR; 11330 } 11331 } 11332 11333 static inline bool m_is_ppb_region(CPUARMState *env, uint32_t address) 11334 { 11335 /* True if address is in the M profile PPB region 0xe0000000 - 0xe00fffff */ 11336 return arm_feature(env, ARM_FEATURE_M) && 11337 extract32(address, 20, 12) == 0xe00; 11338 } 11339 11340 static inline bool m_is_system_region(CPUARMState *env, uint32_t address) 11341 { 11342 /* True if address is in the M profile system region 11343 * 0xe0000000 - 0xffffffff 11344 */ 11345 return arm_feature(env, ARM_FEATURE_M) && extract32(address, 29, 3) == 0x7; 11346 } 11347 11348 static bool get_phys_addr_pmsav7(CPUARMState *env, uint32_t address, 11349 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11350 hwaddr *phys_ptr, int *prot, 11351 target_ulong *page_size, 11352 ARMMMUFaultInfo *fi) 11353 { 11354 ARMCPU *cpu = env_archcpu(env); 11355 int n; 11356 bool is_user = regime_is_user(env, mmu_idx); 11357 11358 *phys_ptr = address; 11359 *page_size = TARGET_PAGE_SIZE; 11360 *prot = 0; 11361 11362 if (regime_translation_disabled(env, mmu_idx) || 11363 m_is_ppb_region(env, address)) { 11364 /* MPU disabled or M profile PPB access: use default memory map. 11365 * The other case which uses the default memory map in the 11366 * v7M ARM ARM pseudocode is exception vector reads from the vector 11367 * table. In QEMU those accesses are done in arm_v7m_load_vector(), 11368 * which always does a direct read using address_space_ldl(), rather 11369 * than going via this function, so we don't need to check that here. 11370 */ 11371 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 11372 } else { /* MPU enabled */ 11373 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 11374 /* region search */ 11375 uint32_t base = env->pmsav7.drbar[n]; 11376 uint32_t rsize = extract32(env->pmsav7.drsr[n], 1, 5); 11377 uint32_t rmask; 11378 bool srdis = false; 11379 11380 if (!(env->pmsav7.drsr[n] & 0x1)) { 11381 continue; 11382 } 11383 11384 if (!rsize) { 11385 qemu_log_mask(LOG_GUEST_ERROR, 11386 "DRSR[%d]: Rsize field cannot be 0\n", n); 11387 continue; 11388 } 11389 rsize++; 11390 rmask = (1ull << rsize) - 1; 11391 11392 if (base & rmask) { 11393 qemu_log_mask(LOG_GUEST_ERROR, 11394 "DRBAR[%d]: 0x%" PRIx32 " misaligned " 11395 "to DRSR region size, mask = 0x%" PRIx32 "\n", 11396 n, base, rmask); 11397 continue; 11398 } 11399 11400 if (address < base || address > base + rmask) { 11401 /* 11402 * Address not in this region. We must check whether the 11403 * region covers addresses in the same page as our address. 11404 * In that case we must not report a size that covers the 11405 * whole page for a subsequent hit against a different MPU 11406 * region or the background region, because it would result in 11407 * incorrect TLB hits for subsequent accesses to addresses that 11408 * are in this MPU region. 11409 */ 11410 if (ranges_overlap(base, rmask, 11411 address & TARGET_PAGE_MASK, 11412 TARGET_PAGE_SIZE)) { 11413 *page_size = 1; 11414 } 11415 continue; 11416 } 11417 11418 /* Region matched */ 11419 11420 if (rsize >= 8) { /* no subregions for regions < 256 bytes */ 11421 int i, snd; 11422 uint32_t srdis_mask; 11423 11424 rsize -= 3; /* sub region size (power of 2) */ 11425 snd = ((address - base) >> rsize) & 0x7; 11426 srdis = extract32(env->pmsav7.drsr[n], snd + 8, 1); 11427 11428 srdis_mask = srdis ? 0x3 : 0x0; 11429 for (i = 2; i <= 8 && rsize < TARGET_PAGE_BITS; i *= 2) { 11430 /* This will check in groups of 2, 4 and then 8, whether 11431 * the subregion bits are consistent. rsize is incremented 11432 * back up to give the region size, considering consistent 11433 * adjacent subregions as one region. Stop testing if rsize 11434 * is already big enough for an entire QEMU page. 11435 */ 11436 int snd_rounded = snd & ~(i - 1); 11437 uint32_t srdis_multi = extract32(env->pmsav7.drsr[n], 11438 snd_rounded + 8, i); 11439 if (srdis_mask ^ srdis_multi) { 11440 break; 11441 } 11442 srdis_mask = (srdis_mask << i) | srdis_mask; 11443 rsize++; 11444 } 11445 } 11446 if (srdis) { 11447 continue; 11448 } 11449 if (rsize < TARGET_PAGE_BITS) { 11450 *page_size = 1 << rsize; 11451 } 11452 break; 11453 } 11454 11455 if (n == -1) { /* no hits */ 11456 if (!pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 11457 /* background fault */ 11458 fi->type = ARMFault_Background; 11459 return true; 11460 } 11461 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 11462 } else { /* a MPU hit! */ 11463 uint32_t ap = extract32(env->pmsav7.dracr[n], 8, 3); 11464 uint32_t xn = extract32(env->pmsav7.dracr[n], 12, 1); 11465 11466 if (m_is_system_region(env, address)) { 11467 /* System space is always execute never */ 11468 xn = 1; 11469 } 11470 11471 if (is_user) { /* User mode AP bit decoding */ 11472 switch (ap) { 11473 case 0: 11474 case 1: 11475 case 5: 11476 break; /* no access */ 11477 case 3: 11478 *prot |= PAGE_WRITE; 11479 /* fall through */ 11480 case 2: 11481 case 6: 11482 *prot |= PAGE_READ | PAGE_EXEC; 11483 break; 11484 case 7: 11485 /* for v7M, same as 6; for R profile a reserved value */ 11486 if (arm_feature(env, ARM_FEATURE_M)) { 11487 *prot |= PAGE_READ | PAGE_EXEC; 11488 break; 11489 } 11490 /* fall through */ 11491 default: 11492 qemu_log_mask(LOG_GUEST_ERROR, 11493 "DRACR[%d]: Bad value for AP bits: 0x%" 11494 PRIx32 "\n", n, ap); 11495 } 11496 } else { /* Priv. mode AP bits decoding */ 11497 switch (ap) { 11498 case 0: 11499 break; /* no access */ 11500 case 1: 11501 case 2: 11502 case 3: 11503 *prot |= PAGE_WRITE; 11504 /* fall through */ 11505 case 5: 11506 case 6: 11507 *prot |= PAGE_READ | PAGE_EXEC; 11508 break; 11509 case 7: 11510 /* for v7M, same as 6; for R profile a reserved value */ 11511 if (arm_feature(env, ARM_FEATURE_M)) { 11512 *prot |= PAGE_READ | PAGE_EXEC; 11513 break; 11514 } 11515 /* fall through */ 11516 default: 11517 qemu_log_mask(LOG_GUEST_ERROR, 11518 "DRACR[%d]: Bad value for AP bits: 0x%" 11519 PRIx32 "\n", n, ap); 11520 } 11521 } 11522 11523 /* execute never */ 11524 if (xn) { 11525 *prot &= ~PAGE_EXEC; 11526 } 11527 } 11528 } 11529 11530 fi->type = ARMFault_Permission; 11531 fi->level = 1; 11532 return !(*prot & (1 << access_type)); 11533 } 11534 11535 static bool v8m_is_sau_exempt(CPUARMState *env, 11536 uint32_t address, MMUAccessType access_type) 11537 { 11538 /* The architecture specifies that certain address ranges are 11539 * exempt from v8M SAU/IDAU checks. 11540 */ 11541 return 11542 (access_type == MMU_INST_FETCH && m_is_system_region(env, address)) || 11543 (address >= 0xe0000000 && address <= 0xe0002fff) || 11544 (address >= 0xe000e000 && address <= 0xe000efff) || 11545 (address >= 0xe002e000 && address <= 0xe002efff) || 11546 (address >= 0xe0040000 && address <= 0xe0041fff) || 11547 (address >= 0xe00ff000 && address <= 0xe00fffff); 11548 } 11549 11550 void v8m_security_lookup(CPUARMState *env, uint32_t address, 11551 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11552 V8M_SAttributes *sattrs) 11553 { 11554 /* Look up the security attributes for this address. Compare the 11555 * pseudocode SecurityCheck() function. 11556 * We assume the caller has zero-initialized *sattrs. 11557 */ 11558 ARMCPU *cpu = env_archcpu(env); 11559 int r; 11560 bool idau_exempt = false, idau_ns = true, idau_nsc = true; 11561 int idau_region = IREGION_NOTVALID; 11562 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 11563 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 11564 11565 if (cpu->idau) { 11566 IDAUInterfaceClass *iic = IDAU_INTERFACE_GET_CLASS(cpu->idau); 11567 IDAUInterface *ii = IDAU_INTERFACE(cpu->idau); 11568 11569 iic->check(ii, address, &idau_region, &idau_exempt, &idau_ns, 11570 &idau_nsc); 11571 } 11572 11573 if (access_type == MMU_INST_FETCH && extract32(address, 28, 4) == 0xf) { 11574 /* 0xf0000000..0xffffffff is always S for insn fetches */ 11575 return; 11576 } 11577 11578 if (idau_exempt || v8m_is_sau_exempt(env, address, access_type)) { 11579 sattrs->ns = !regime_is_secure(env, mmu_idx); 11580 return; 11581 } 11582 11583 if (idau_region != IREGION_NOTVALID) { 11584 sattrs->irvalid = true; 11585 sattrs->iregion = idau_region; 11586 } 11587 11588 switch (env->sau.ctrl & 3) { 11589 case 0: /* SAU.ENABLE == 0, SAU.ALLNS == 0 */ 11590 break; 11591 case 2: /* SAU.ENABLE == 0, SAU.ALLNS == 1 */ 11592 sattrs->ns = true; 11593 break; 11594 default: /* SAU.ENABLE == 1 */ 11595 for (r = 0; r < cpu->sau_sregion; r++) { 11596 if (env->sau.rlar[r] & 1) { 11597 uint32_t base = env->sau.rbar[r] & ~0x1f; 11598 uint32_t limit = env->sau.rlar[r] | 0x1f; 11599 11600 if (base <= address && limit >= address) { 11601 if (base > addr_page_base || limit < addr_page_limit) { 11602 sattrs->subpage = true; 11603 } 11604 if (sattrs->srvalid) { 11605 /* If we hit in more than one region then we must report 11606 * as Secure, not NS-Callable, with no valid region 11607 * number info. 11608 */ 11609 sattrs->ns = false; 11610 sattrs->nsc = false; 11611 sattrs->sregion = 0; 11612 sattrs->srvalid = false; 11613 break; 11614 } else { 11615 if (env->sau.rlar[r] & 2) { 11616 sattrs->nsc = true; 11617 } else { 11618 sattrs->ns = true; 11619 } 11620 sattrs->srvalid = true; 11621 sattrs->sregion = r; 11622 } 11623 } else { 11624 /* 11625 * Address not in this region. We must check whether the 11626 * region covers addresses in the same page as our address. 11627 * In that case we must not report a size that covers the 11628 * whole page for a subsequent hit against a different MPU 11629 * region or the background region, because it would result 11630 * in incorrect TLB hits for subsequent accesses to 11631 * addresses that are in this MPU region. 11632 */ 11633 if (limit >= base && 11634 ranges_overlap(base, limit - base + 1, 11635 addr_page_base, 11636 TARGET_PAGE_SIZE)) { 11637 sattrs->subpage = true; 11638 } 11639 } 11640 } 11641 } 11642 break; 11643 } 11644 11645 /* 11646 * The IDAU will override the SAU lookup results if it specifies 11647 * higher security than the SAU does. 11648 */ 11649 if (!idau_ns) { 11650 if (sattrs->ns || (!idau_nsc && sattrs->nsc)) { 11651 sattrs->ns = false; 11652 sattrs->nsc = idau_nsc; 11653 } 11654 } 11655 } 11656 11657 bool pmsav8_mpu_lookup(CPUARMState *env, uint32_t address, 11658 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11659 hwaddr *phys_ptr, MemTxAttrs *txattrs, 11660 int *prot, bool *is_subpage, 11661 ARMMMUFaultInfo *fi, uint32_t *mregion) 11662 { 11663 /* Perform a PMSAv8 MPU lookup (without also doing the SAU check 11664 * that a full phys-to-virt translation does). 11665 * mregion is (if not NULL) set to the region number which matched, 11666 * or -1 if no region number is returned (MPU off, address did not 11667 * hit a region, address hit in multiple regions). 11668 * We set is_subpage to true if the region hit doesn't cover the 11669 * entire TARGET_PAGE the address is within. 11670 */ 11671 ARMCPU *cpu = env_archcpu(env); 11672 bool is_user = regime_is_user(env, mmu_idx); 11673 uint32_t secure = regime_is_secure(env, mmu_idx); 11674 int n; 11675 int matchregion = -1; 11676 bool hit = false; 11677 uint32_t addr_page_base = address & TARGET_PAGE_MASK; 11678 uint32_t addr_page_limit = addr_page_base + (TARGET_PAGE_SIZE - 1); 11679 11680 *is_subpage = false; 11681 *phys_ptr = address; 11682 *prot = 0; 11683 if (mregion) { 11684 *mregion = -1; 11685 } 11686 11687 /* Unlike the ARM ARM pseudocode, we don't need to check whether this 11688 * was an exception vector read from the vector table (which is always 11689 * done using the default system address map), because those accesses 11690 * are done in arm_v7m_load_vector(), which always does a direct 11691 * read using address_space_ldl(), rather than going via this function. 11692 */ 11693 if (regime_translation_disabled(env, mmu_idx)) { /* MPU disabled */ 11694 hit = true; 11695 } else if (m_is_ppb_region(env, address)) { 11696 hit = true; 11697 } else { 11698 if (pmsav7_use_background_region(cpu, mmu_idx, is_user)) { 11699 hit = true; 11700 } 11701 11702 for (n = (int)cpu->pmsav7_dregion - 1; n >= 0; n--) { 11703 /* region search */ 11704 /* Note that the base address is bits [31:5] from the register 11705 * with bits [4:0] all zeroes, but the limit address is bits 11706 * [31:5] from the register with bits [4:0] all ones. 11707 */ 11708 uint32_t base = env->pmsav8.rbar[secure][n] & ~0x1f; 11709 uint32_t limit = env->pmsav8.rlar[secure][n] | 0x1f; 11710 11711 if (!(env->pmsav8.rlar[secure][n] & 0x1)) { 11712 /* Region disabled */ 11713 continue; 11714 } 11715 11716 if (address < base || address > limit) { 11717 /* 11718 * Address not in this region. We must check whether the 11719 * region covers addresses in the same page as our address. 11720 * In that case we must not report a size that covers the 11721 * whole page for a subsequent hit against a different MPU 11722 * region or the background region, because it would result in 11723 * incorrect TLB hits for subsequent accesses to addresses that 11724 * are in this MPU region. 11725 */ 11726 if (limit >= base && 11727 ranges_overlap(base, limit - base + 1, 11728 addr_page_base, 11729 TARGET_PAGE_SIZE)) { 11730 *is_subpage = true; 11731 } 11732 continue; 11733 } 11734 11735 if (base > addr_page_base || limit < addr_page_limit) { 11736 *is_subpage = true; 11737 } 11738 11739 if (matchregion != -1) { 11740 /* Multiple regions match -- always a failure (unlike 11741 * PMSAv7 where highest-numbered-region wins) 11742 */ 11743 fi->type = ARMFault_Permission; 11744 fi->level = 1; 11745 return true; 11746 } 11747 11748 matchregion = n; 11749 hit = true; 11750 } 11751 } 11752 11753 if (!hit) { 11754 /* background fault */ 11755 fi->type = ARMFault_Background; 11756 return true; 11757 } 11758 11759 if (matchregion == -1) { 11760 /* hit using the background region */ 11761 get_phys_addr_pmsav7_default(env, mmu_idx, address, prot); 11762 } else { 11763 uint32_t ap = extract32(env->pmsav8.rbar[secure][matchregion], 1, 2); 11764 uint32_t xn = extract32(env->pmsav8.rbar[secure][matchregion], 0, 1); 11765 bool pxn = false; 11766 11767 if (arm_feature(env, ARM_FEATURE_V8_1M)) { 11768 pxn = extract32(env->pmsav8.rlar[secure][matchregion], 4, 1); 11769 } 11770 11771 if (m_is_system_region(env, address)) { 11772 /* System space is always execute never */ 11773 xn = 1; 11774 } 11775 11776 *prot = simple_ap_to_rw_prot(env, mmu_idx, ap); 11777 if (*prot && !xn && !(pxn && !is_user)) { 11778 *prot |= PAGE_EXEC; 11779 } 11780 /* We don't need to look the attribute up in the MAIR0/MAIR1 11781 * registers because that only tells us about cacheability. 11782 */ 11783 if (mregion) { 11784 *mregion = matchregion; 11785 } 11786 } 11787 11788 fi->type = ARMFault_Permission; 11789 fi->level = 1; 11790 return !(*prot & (1 << access_type)); 11791 } 11792 11793 11794 static bool get_phys_addr_pmsav8(CPUARMState *env, uint32_t address, 11795 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11796 hwaddr *phys_ptr, MemTxAttrs *txattrs, 11797 int *prot, target_ulong *page_size, 11798 ARMMMUFaultInfo *fi) 11799 { 11800 uint32_t secure = regime_is_secure(env, mmu_idx); 11801 V8M_SAttributes sattrs = {}; 11802 bool ret; 11803 bool mpu_is_subpage; 11804 11805 if (arm_feature(env, ARM_FEATURE_M_SECURITY)) { 11806 v8m_security_lookup(env, address, access_type, mmu_idx, &sattrs); 11807 if (access_type == MMU_INST_FETCH) { 11808 /* Instruction fetches always use the MMU bank and the 11809 * transaction attribute determined by the fetch address, 11810 * regardless of CPU state. This is painful for QEMU 11811 * to handle, because it would mean we need to encode 11812 * into the mmu_idx not just the (user, negpri) information 11813 * for the current security state but also that for the 11814 * other security state, which would balloon the number 11815 * of mmu_idx values needed alarmingly. 11816 * Fortunately we can avoid this because it's not actually 11817 * possible to arbitrarily execute code from memory with 11818 * the wrong security attribute: it will always generate 11819 * an exception of some kind or another, apart from the 11820 * special case of an NS CPU executing an SG instruction 11821 * in S&NSC memory. So we always just fail the translation 11822 * here and sort things out in the exception handler 11823 * (including possibly emulating an SG instruction). 11824 */ 11825 if (sattrs.ns != !secure) { 11826 if (sattrs.nsc) { 11827 fi->type = ARMFault_QEMU_NSCExec; 11828 } else { 11829 fi->type = ARMFault_QEMU_SFault; 11830 } 11831 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 11832 *phys_ptr = address; 11833 *prot = 0; 11834 return true; 11835 } 11836 } else { 11837 /* For data accesses we always use the MMU bank indicated 11838 * by the current CPU state, but the security attributes 11839 * might downgrade a secure access to nonsecure. 11840 */ 11841 if (sattrs.ns) { 11842 txattrs->secure = false; 11843 } else if (!secure) { 11844 /* NS access to S memory must fault. 11845 * Architecturally we should first check whether the 11846 * MPU information for this address indicates that we 11847 * are doing an unaligned access to Device memory, which 11848 * should generate a UsageFault instead. QEMU does not 11849 * currently check for that kind of unaligned access though. 11850 * If we added it we would need to do so as a special case 11851 * for M_FAKE_FSR_SFAULT in arm_v7m_cpu_do_interrupt(). 11852 */ 11853 fi->type = ARMFault_QEMU_SFault; 11854 *page_size = sattrs.subpage ? 1 : TARGET_PAGE_SIZE; 11855 *phys_ptr = address; 11856 *prot = 0; 11857 return true; 11858 } 11859 } 11860 } 11861 11862 ret = pmsav8_mpu_lookup(env, address, access_type, mmu_idx, phys_ptr, 11863 txattrs, prot, &mpu_is_subpage, fi, NULL); 11864 *page_size = sattrs.subpage || mpu_is_subpage ? 1 : TARGET_PAGE_SIZE; 11865 return ret; 11866 } 11867 11868 static bool get_phys_addr_pmsav5(CPUARMState *env, uint32_t address, 11869 MMUAccessType access_type, ARMMMUIdx mmu_idx, 11870 hwaddr *phys_ptr, int *prot, 11871 ARMMMUFaultInfo *fi) 11872 { 11873 int n; 11874 uint32_t mask; 11875 uint32_t base; 11876 bool is_user = regime_is_user(env, mmu_idx); 11877 11878 if (regime_translation_disabled(env, mmu_idx)) { 11879 /* MPU disabled. */ 11880 *phys_ptr = address; 11881 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 11882 return false; 11883 } 11884 11885 *phys_ptr = address; 11886 for (n = 7; n >= 0; n--) { 11887 base = env->cp15.c6_region[n]; 11888 if ((base & 1) == 0) { 11889 continue; 11890 } 11891 mask = 1 << ((base >> 1) & 0x1f); 11892 /* Keep this shift separate from the above to avoid an 11893 (undefined) << 32. */ 11894 mask = (mask << 1) - 1; 11895 if (((base ^ address) & ~mask) == 0) { 11896 break; 11897 } 11898 } 11899 if (n < 0) { 11900 fi->type = ARMFault_Background; 11901 return true; 11902 } 11903 11904 if (access_type == MMU_INST_FETCH) { 11905 mask = env->cp15.pmsav5_insn_ap; 11906 } else { 11907 mask = env->cp15.pmsav5_data_ap; 11908 } 11909 mask = (mask >> (n * 4)) & 0xf; 11910 switch (mask) { 11911 case 0: 11912 fi->type = ARMFault_Permission; 11913 fi->level = 1; 11914 return true; 11915 case 1: 11916 if (is_user) { 11917 fi->type = ARMFault_Permission; 11918 fi->level = 1; 11919 return true; 11920 } 11921 *prot = PAGE_READ | PAGE_WRITE; 11922 break; 11923 case 2: 11924 *prot = PAGE_READ; 11925 if (!is_user) { 11926 *prot |= PAGE_WRITE; 11927 } 11928 break; 11929 case 3: 11930 *prot = PAGE_READ | PAGE_WRITE; 11931 break; 11932 case 5: 11933 if (is_user) { 11934 fi->type = ARMFault_Permission; 11935 fi->level = 1; 11936 return true; 11937 } 11938 *prot = PAGE_READ; 11939 break; 11940 case 6: 11941 *prot = PAGE_READ; 11942 break; 11943 default: 11944 /* Bad permission. */ 11945 fi->type = ARMFault_Permission; 11946 fi->level = 1; 11947 return true; 11948 } 11949 *prot |= PAGE_EXEC; 11950 return false; 11951 } 11952 11953 /* Combine either inner or outer cacheability attributes for normal 11954 * memory, according to table D4-42 and pseudocode procedure 11955 * CombineS1S2AttrHints() of ARM DDI 0487B.b (the ARMv8 ARM). 11956 * 11957 * NB: only stage 1 includes allocation hints (RW bits), leading to 11958 * some asymmetry. 11959 */ 11960 static uint8_t combine_cacheattr_nibble(uint8_t s1, uint8_t s2) 11961 { 11962 if (s1 == 4 || s2 == 4) { 11963 /* non-cacheable has precedence */ 11964 return 4; 11965 } else if (extract32(s1, 2, 2) == 0 || extract32(s1, 2, 2) == 2) { 11966 /* stage 1 write-through takes precedence */ 11967 return s1; 11968 } else if (extract32(s2, 2, 2) == 2) { 11969 /* stage 2 write-through takes precedence, but the allocation hint 11970 * is still taken from stage 1 11971 */ 11972 return (2 << 2) | extract32(s1, 0, 2); 11973 } else { /* write-back */ 11974 return s1; 11975 } 11976 } 11977 11978 /* Combine S1 and S2 cacheability/shareability attributes, per D4.5.4 11979 * and CombineS1S2Desc() 11980 * 11981 * @s1: Attributes from stage 1 walk 11982 * @s2: Attributes from stage 2 walk 11983 */ 11984 static ARMCacheAttrs combine_cacheattrs(ARMCacheAttrs s1, ARMCacheAttrs s2) 11985 { 11986 uint8_t s1lo, s2lo, s1hi, s2hi; 11987 ARMCacheAttrs ret; 11988 bool tagged = false; 11989 11990 if (s1.attrs == 0xf0) { 11991 tagged = true; 11992 s1.attrs = 0xff; 11993 } 11994 11995 s1lo = extract32(s1.attrs, 0, 4); 11996 s2lo = extract32(s2.attrs, 0, 4); 11997 s1hi = extract32(s1.attrs, 4, 4); 11998 s2hi = extract32(s2.attrs, 4, 4); 11999 12000 /* Combine shareability attributes (table D4-43) */ 12001 if (s1.shareability == 2 || s2.shareability == 2) { 12002 /* if either are outer-shareable, the result is outer-shareable */ 12003 ret.shareability = 2; 12004 } else if (s1.shareability == 3 || s2.shareability == 3) { 12005 /* if either are inner-shareable, the result is inner-shareable */ 12006 ret.shareability = 3; 12007 } else { 12008 /* both non-shareable */ 12009 ret.shareability = 0; 12010 } 12011 12012 /* Combine memory type and cacheability attributes */ 12013 if (s1hi == 0 || s2hi == 0) { 12014 /* Device has precedence over normal */ 12015 if (s1lo == 0 || s2lo == 0) { 12016 /* nGnRnE has precedence over anything */ 12017 ret.attrs = 0; 12018 } else if (s1lo == 4 || s2lo == 4) { 12019 /* non-Reordering has precedence over Reordering */ 12020 ret.attrs = 4; /* nGnRE */ 12021 } else if (s1lo == 8 || s2lo == 8) { 12022 /* non-Gathering has precedence over Gathering */ 12023 ret.attrs = 8; /* nGRE */ 12024 } else { 12025 ret.attrs = 0xc; /* GRE */ 12026 } 12027 12028 /* Any location for which the resultant memory type is any 12029 * type of Device memory is always treated as Outer Shareable. 12030 */ 12031 ret.shareability = 2; 12032 } else { /* Normal memory */ 12033 /* Outer/inner cacheability combine independently */ 12034 ret.attrs = combine_cacheattr_nibble(s1hi, s2hi) << 4 12035 | combine_cacheattr_nibble(s1lo, s2lo); 12036 12037 if (ret.attrs == 0x44) { 12038 /* Any location for which the resultant memory type is Normal 12039 * Inner Non-cacheable, Outer Non-cacheable is always treated 12040 * as Outer Shareable. 12041 */ 12042 ret.shareability = 2; 12043 } 12044 } 12045 12046 /* TODO: CombineS1S2Desc does not consider transient, only WB, RWA. */ 12047 if (tagged && ret.attrs == 0xff) { 12048 ret.attrs = 0xf0; 12049 } 12050 12051 return ret; 12052 } 12053 12054 12055 /* get_phys_addr - get the physical address for this virtual address 12056 * 12057 * Find the physical address corresponding to the given virtual address, 12058 * by doing a translation table walk on MMU based systems or using the 12059 * MPU state on MPU based systems. 12060 * 12061 * Returns false if the translation was successful. Otherwise, phys_ptr, attrs, 12062 * prot and page_size may not be filled in, and the populated fsr value provides 12063 * information on why the translation aborted, in the format of a 12064 * DFSR/IFSR fault register, with the following caveats: 12065 * * we honour the short vs long DFSR format differences. 12066 * * the WnR bit is never set (the caller must do this). 12067 * * for PSMAv5 based systems we don't bother to return a full FSR format 12068 * value. 12069 * 12070 * @env: CPUARMState 12071 * @address: virtual address to get physical address for 12072 * @access_type: 0 for read, 1 for write, 2 for execute 12073 * @mmu_idx: MMU index indicating required translation regime 12074 * @phys_ptr: set to the physical address corresponding to the virtual address 12075 * @attrs: set to the memory transaction attributes to use 12076 * @prot: set to the permissions for the page containing phys_ptr 12077 * @page_size: set to the size of the page containing phys_ptr 12078 * @fi: set to fault info if the translation fails 12079 * @cacheattrs: (if non-NULL) set to the cacheability/shareability attributes 12080 */ 12081 bool get_phys_addr(CPUARMState *env, target_ulong address, 12082 MMUAccessType access_type, ARMMMUIdx mmu_idx, 12083 hwaddr *phys_ptr, MemTxAttrs *attrs, int *prot, 12084 target_ulong *page_size, 12085 ARMMMUFaultInfo *fi, ARMCacheAttrs *cacheattrs) 12086 { 12087 if (mmu_idx == ARMMMUIdx_E10_0 || 12088 mmu_idx == ARMMMUIdx_E10_1 || 12089 mmu_idx == ARMMMUIdx_E10_1_PAN) { 12090 /* Call ourselves recursively to do the stage 1 and then stage 2 12091 * translations. 12092 */ 12093 if (arm_feature(env, ARM_FEATURE_EL2)) { 12094 hwaddr ipa; 12095 int s2_prot; 12096 int ret; 12097 ARMCacheAttrs cacheattrs2 = {}; 12098 12099 ret = get_phys_addr(env, address, access_type, 12100 stage_1_mmu_idx(mmu_idx), &ipa, attrs, 12101 prot, page_size, fi, cacheattrs); 12102 12103 /* If S1 fails or S2 is disabled, return early. */ 12104 if (ret || regime_translation_disabled(env, ARMMMUIdx_Stage2)) { 12105 *phys_ptr = ipa; 12106 return ret; 12107 } 12108 12109 /* S1 is done. Now do S2 translation. */ 12110 ret = get_phys_addr_lpae(env, ipa, access_type, ARMMMUIdx_Stage2, 12111 mmu_idx == ARMMMUIdx_E10_0, 12112 phys_ptr, attrs, &s2_prot, 12113 page_size, fi, &cacheattrs2); 12114 fi->s2addr = ipa; 12115 /* Combine the S1 and S2 perms. */ 12116 *prot &= s2_prot; 12117 12118 /* If S2 fails, return early. */ 12119 if (ret) { 12120 return ret; 12121 } 12122 12123 /* Combine the S1 and S2 cache attributes. */ 12124 if (env->cp15.hcr_el2 & HCR_DC) { 12125 /* 12126 * HCR.DC forces the first stage attributes to 12127 * Normal Non-Shareable, 12128 * Inner Write-Back Read-Allocate Write-Allocate, 12129 * Outer Write-Back Read-Allocate Write-Allocate. 12130 * Do not overwrite Tagged within attrs. 12131 */ 12132 if (cacheattrs->attrs != 0xf0) { 12133 cacheattrs->attrs = 0xff; 12134 } 12135 cacheattrs->shareability = 0; 12136 } 12137 *cacheattrs = combine_cacheattrs(*cacheattrs, cacheattrs2); 12138 return 0; 12139 } else { 12140 /* 12141 * For non-EL2 CPUs a stage1+stage2 translation is just stage 1. 12142 */ 12143 mmu_idx = stage_1_mmu_idx(mmu_idx); 12144 } 12145 } 12146 12147 /* The page table entries may downgrade secure to non-secure, but 12148 * cannot upgrade an non-secure translation regime's attributes 12149 * to secure. 12150 */ 12151 attrs->secure = regime_is_secure(env, mmu_idx); 12152 attrs->user = regime_is_user(env, mmu_idx); 12153 12154 /* Fast Context Switch Extension. This doesn't exist at all in v8. 12155 * In v7 and earlier it affects all stage 1 translations. 12156 */ 12157 if (address < 0x02000000 && mmu_idx != ARMMMUIdx_Stage2 12158 && !arm_feature(env, ARM_FEATURE_V8)) { 12159 if (regime_el(env, mmu_idx) == 3) { 12160 address += env->cp15.fcseidr_s; 12161 } else { 12162 address += env->cp15.fcseidr_ns; 12163 } 12164 } 12165 12166 if (arm_feature(env, ARM_FEATURE_PMSA)) { 12167 bool ret; 12168 *page_size = TARGET_PAGE_SIZE; 12169 12170 if (arm_feature(env, ARM_FEATURE_V8)) { 12171 /* PMSAv8 */ 12172 ret = get_phys_addr_pmsav8(env, address, access_type, mmu_idx, 12173 phys_ptr, attrs, prot, page_size, fi); 12174 } else if (arm_feature(env, ARM_FEATURE_V7)) { 12175 /* PMSAv7 */ 12176 ret = get_phys_addr_pmsav7(env, address, access_type, mmu_idx, 12177 phys_ptr, prot, page_size, fi); 12178 } else { 12179 /* Pre-v7 MPU */ 12180 ret = get_phys_addr_pmsav5(env, address, access_type, mmu_idx, 12181 phys_ptr, prot, fi); 12182 } 12183 qemu_log_mask(CPU_LOG_MMU, "PMSA MPU lookup for %s at 0x%08" PRIx32 12184 " mmu_idx %u -> %s (prot %c%c%c)\n", 12185 access_type == MMU_DATA_LOAD ? "reading" : 12186 (access_type == MMU_DATA_STORE ? "writing" : "execute"), 12187 (uint32_t)address, mmu_idx, 12188 ret ? "Miss" : "Hit", 12189 *prot & PAGE_READ ? 'r' : '-', 12190 *prot & PAGE_WRITE ? 'w' : '-', 12191 *prot & PAGE_EXEC ? 'x' : '-'); 12192 12193 return ret; 12194 } 12195 12196 /* Definitely a real MMU, not an MPU */ 12197 12198 if (regime_translation_disabled(env, mmu_idx)) { 12199 uint64_t hcr; 12200 uint8_t memattr; 12201 12202 /* 12203 * MMU disabled. S1 addresses within aa64 translation regimes are 12204 * still checked for bounds -- see AArch64.TranslateAddressS1Off. 12205 */ 12206 if (mmu_idx != ARMMMUIdx_Stage2) { 12207 int r_el = regime_el(env, mmu_idx); 12208 if (arm_el_is_aa64(env, r_el)) { 12209 int pamax = arm_pamax(env_archcpu(env)); 12210 uint64_t tcr = env->cp15.tcr_el[r_el].raw_tcr; 12211 int addrtop, tbi; 12212 12213 tbi = aa64_va_parameter_tbi(tcr, mmu_idx); 12214 if (access_type == MMU_INST_FETCH) { 12215 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx); 12216 } 12217 tbi = (tbi >> extract64(address, 55, 1)) & 1; 12218 addrtop = (tbi ? 55 : 63); 12219 12220 if (extract64(address, pamax, addrtop - pamax + 1) != 0) { 12221 fi->type = ARMFault_AddressSize; 12222 fi->level = 0; 12223 fi->stage2 = false; 12224 return 1; 12225 } 12226 12227 /* 12228 * When TBI is disabled, we've just validated that all of the 12229 * bits above PAMax are zero, so logically we only need to 12230 * clear the top byte for TBI. But it's clearer to follow 12231 * the pseudocode set of addrdesc.paddress. 12232 */ 12233 address = extract64(address, 0, 52); 12234 } 12235 } 12236 *phys_ptr = address; 12237 *prot = PAGE_READ | PAGE_WRITE | PAGE_EXEC; 12238 *page_size = TARGET_PAGE_SIZE; 12239 12240 /* Fill in cacheattr a-la AArch64.TranslateAddressS1Off. */ 12241 hcr = arm_hcr_el2_eff(env); 12242 cacheattrs->shareability = 0; 12243 if (hcr & HCR_DC) { 12244 if (hcr & HCR_DCT) { 12245 memattr = 0xf0; /* Tagged, Normal, WB, RWA */ 12246 } else { 12247 memattr = 0xff; /* Normal, WB, RWA */ 12248 } 12249 } else if (access_type == MMU_INST_FETCH) { 12250 if (regime_sctlr(env, mmu_idx) & SCTLR_I) { 12251 memattr = 0xee; /* Normal, WT, RA, NT */ 12252 } else { 12253 memattr = 0x44; /* Normal, NC, No */ 12254 } 12255 cacheattrs->shareability = 2; /* outer sharable */ 12256 } else { 12257 memattr = 0x00; /* Device, nGnRnE */ 12258 } 12259 cacheattrs->attrs = memattr; 12260 return 0; 12261 } 12262 12263 if (regime_using_lpae_format(env, mmu_idx)) { 12264 return get_phys_addr_lpae(env, address, access_type, mmu_idx, false, 12265 phys_ptr, attrs, prot, page_size, 12266 fi, cacheattrs); 12267 } else if (regime_sctlr(env, mmu_idx) & SCTLR_XP) { 12268 return get_phys_addr_v6(env, address, access_type, mmu_idx, 12269 phys_ptr, attrs, prot, page_size, fi); 12270 } else { 12271 return get_phys_addr_v5(env, address, access_type, mmu_idx, 12272 phys_ptr, prot, page_size, fi); 12273 } 12274 } 12275 12276 hwaddr arm_cpu_get_phys_page_attrs_debug(CPUState *cs, vaddr addr, 12277 MemTxAttrs *attrs) 12278 { 12279 ARMCPU *cpu = ARM_CPU(cs); 12280 CPUARMState *env = &cpu->env; 12281 hwaddr phys_addr; 12282 target_ulong page_size; 12283 int prot; 12284 bool ret; 12285 ARMMMUFaultInfo fi = {}; 12286 ARMMMUIdx mmu_idx = arm_mmu_idx(env); 12287 ARMCacheAttrs cacheattrs = {}; 12288 12289 *attrs = (MemTxAttrs) {}; 12290 12291 ret = get_phys_addr(env, addr, 0, mmu_idx, &phys_addr, 12292 attrs, &prot, &page_size, &fi, &cacheattrs); 12293 12294 if (ret) { 12295 return -1; 12296 } 12297 return phys_addr; 12298 } 12299 12300 #endif 12301 12302 /* Note that signed overflow is undefined in C. The following routines are 12303 careful to use unsigned types where modulo arithmetic is required. 12304 Failure to do so _will_ break on newer gcc. */ 12305 12306 /* Signed saturating arithmetic. */ 12307 12308 /* Perform 16-bit signed saturating addition. */ 12309 static inline uint16_t add16_sat(uint16_t a, uint16_t b) 12310 { 12311 uint16_t res; 12312 12313 res = a + b; 12314 if (((res ^ a) & 0x8000) && !((a ^ b) & 0x8000)) { 12315 if (a & 0x8000) 12316 res = 0x8000; 12317 else 12318 res = 0x7fff; 12319 } 12320 return res; 12321 } 12322 12323 /* Perform 8-bit signed saturating addition. */ 12324 static inline uint8_t add8_sat(uint8_t a, uint8_t b) 12325 { 12326 uint8_t res; 12327 12328 res = a + b; 12329 if (((res ^ a) & 0x80) && !((a ^ b) & 0x80)) { 12330 if (a & 0x80) 12331 res = 0x80; 12332 else 12333 res = 0x7f; 12334 } 12335 return res; 12336 } 12337 12338 /* Perform 16-bit signed saturating subtraction. */ 12339 static inline uint16_t sub16_sat(uint16_t a, uint16_t b) 12340 { 12341 uint16_t res; 12342 12343 res = a - b; 12344 if (((res ^ a) & 0x8000) && ((a ^ b) & 0x8000)) { 12345 if (a & 0x8000) 12346 res = 0x8000; 12347 else 12348 res = 0x7fff; 12349 } 12350 return res; 12351 } 12352 12353 /* Perform 8-bit signed saturating subtraction. */ 12354 static inline uint8_t sub8_sat(uint8_t a, uint8_t b) 12355 { 12356 uint8_t res; 12357 12358 res = a - b; 12359 if (((res ^ a) & 0x80) && ((a ^ b) & 0x80)) { 12360 if (a & 0x80) 12361 res = 0x80; 12362 else 12363 res = 0x7f; 12364 } 12365 return res; 12366 } 12367 12368 #define ADD16(a, b, n) RESULT(add16_sat(a, b), n, 16); 12369 #define SUB16(a, b, n) RESULT(sub16_sat(a, b), n, 16); 12370 #define ADD8(a, b, n) RESULT(add8_sat(a, b), n, 8); 12371 #define SUB8(a, b, n) RESULT(sub8_sat(a, b), n, 8); 12372 #define PFX q 12373 12374 #include "op_addsub.h" 12375 12376 /* Unsigned saturating arithmetic. */ 12377 static inline uint16_t add16_usat(uint16_t a, uint16_t b) 12378 { 12379 uint16_t res; 12380 res = a + b; 12381 if (res < a) 12382 res = 0xffff; 12383 return res; 12384 } 12385 12386 static inline uint16_t sub16_usat(uint16_t a, uint16_t b) 12387 { 12388 if (a > b) 12389 return a - b; 12390 else 12391 return 0; 12392 } 12393 12394 static inline uint8_t add8_usat(uint8_t a, uint8_t b) 12395 { 12396 uint8_t res; 12397 res = a + b; 12398 if (res < a) 12399 res = 0xff; 12400 return res; 12401 } 12402 12403 static inline uint8_t sub8_usat(uint8_t a, uint8_t b) 12404 { 12405 if (a > b) 12406 return a - b; 12407 else 12408 return 0; 12409 } 12410 12411 #define ADD16(a, b, n) RESULT(add16_usat(a, b), n, 16); 12412 #define SUB16(a, b, n) RESULT(sub16_usat(a, b), n, 16); 12413 #define ADD8(a, b, n) RESULT(add8_usat(a, b), n, 8); 12414 #define SUB8(a, b, n) RESULT(sub8_usat(a, b), n, 8); 12415 #define PFX uq 12416 12417 #include "op_addsub.h" 12418 12419 /* Signed modulo arithmetic. */ 12420 #define SARITH16(a, b, n, op) do { \ 12421 int32_t sum; \ 12422 sum = (int32_t)(int16_t)(a) op (int32_t)(int16_t)(b); \ 12423 RESULT(sum, n, 16); \ 12424 if (sum >= 0) \ 12425 ge |= 3 << (n * 2); \ 12426 } while(0) 12427 12428 #define SARITH8(a, b, n, op) do { \ 12429 int32_t sum; \ 12430 sum = (int32_t)(int8_t)(a) op (int32_t)(int8_t)(b); \ 12431 RESULT(sum, n, 8); \ 12432 if (sum >= 0) \ 12433 ge |= 1 << n; \ 12434 } while(0) 12435 12436 12437 #define ADD16(a, b, n) SARITH16(a, b, n, +) 12438 #define SUB16(a, b, n) SARITH16(a, b, n, -) 12439 #define ADD8(a, b, n) SARITH8(a, b, n, +) 12440 #define SUB8(a, b, n) SARITH8(a, b, n, -) 12441 #define PFX s 12442 #define ARITH_GE 12443 12444 #include "op_addsub.h" 12445 12446 /* Unsigned modulo arithmetic. */ 12447 #define ADD16(a, b, n) do { \ 12448 uint32_t sum; \ 12449 sum = (uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b); \ 12450 RESULT(sum, n, 16); \ 12451 if ((sum >> 16) == 1) \ 12452 ge |= 3 << (n * 2); \ 12453 } while(0) 12454 12455 #define ADD8(a, b, n) do { \ 12456 uint32_t sum; \ 12457 sum = (uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b); \ 12458 RESULT(sum, n, 8); \ 12459 if ((sum >> 8) == 1) \ 12460 ge |= 1 << n; \ 12461 } while(0) 12462 12463 #define SUB16(a, b, n) do { \ 12464 uint32_t sum; \ 12465 sum = (uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b); \ 12466 RESULT(sum, n, 16); \ 12467 if ((sum >> 16) == 0) \ 12468 ge |= 3 << (n * 2); \ 12469 } while(0) 12470 12471 #define SUB8(a, b, n) do { \ 12472 uint32_t sum; \ 12473 sum = (uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b); \ 12474 RESULT(sum, n, 8); \ 12475 if ((sum >> 8) == 0) \ 12476 ge |= 1 << n; \ 12477 } while(0) 12478 12479 #define PFX u 12480 #define ARITH_GE 12481 12482 #include "op_addsub.h" 12483 12484 /* Halved signed arithmetic. */ 12485 #define ADD16(a, b, n) \ 12486 RESULT(((int32_t)(int16_t)(a) + (int32_t)(int16_t)(b)) >> 1, n, 16) 12487 #define SUB16(a, b, n) \ 12488 RESULT(((int32_t)(int16_t)(a) - (int32_t)(int16_t)(b)) >> 1, n, 16) 12489 #define ADD8(a, b, n) \ 12490 RESULT(((int32_t)(int8_t)(a) + (int32_t)(int8_t)(b)) >> 1, n, 8) 12491 #define SUB8(a, b, n) \ 12492 RESULT(((int32_t)(int8_t)(a) - (int32_t)(int8_t)(b)) >> 1, n, 8) 12493 #define PFX sh 12494 12495 #include "op_addsub.h" 12496 12497 /* Halved unsigned arithmetic. */ 12498 #define ADD16(a, b, n) \ 12499 RESULT(((uint32_t)(uint16_t)(a) + (uint32_t)(uint16_t)(b)) >> 1, n, 16) 12500 #define SUB16(a, b, n) \ 12501 RESULT(((uint32_t)(uint16_t)(a) - (uint32_t)(uint16_t)(b)) >> 1, n, 16) 12502 #define ADD8(a, b, n) \ 12503 RESULT(((uint32_t)(uint8_t)(a) + (uint32_t)(uint8_t)(b)) >> 1, n, 8) 12504 #define SUB8(a, b, n) \ 12505 RESULT(((uint32_t)(uint8_t)(a) - (uint32_t)(uint8_t)(b)) >> 1, n, 8) 12506 #define PFX uh 12507 12508 #include "op_addsub.h" 12509 12510 static inline uint8_t do_usad(uint8_t a, uint8_t b) 12511 { 12512 if (a > b) 12513 return a - b; 12514 else 12515 return b - a; 12516 } 12517 12518 /* Unsigned sum of absolute byte differences. */ 12519 uint32_t HELPER(usad8)(uint32_t a, uint32_t b) 12520 { 12521 uint32_t sum; 12522 sum = do_usad(a, b); 12523 sum += do_usad(a >> 8, b >> 8); 12524 sum += do_usad(a >> 16, b >> 16); 12525 sum += do_usad(a >> 24, b >> 24); 12526 return sum; 12527 } 12528 12529 /* For ARMv6 SEL instruction. */ 12530 uint32_t HELPER(sel_flags)(uint32_t flags, uint32_t a, uint32_t b) 12531 { 12532 uint32_t mask; 12533 12534 mask = 0; 12535 if (flags & 1) 12536 mask |= 0xff; 12537 if (flags & 2) 12538 mask |= 0xff00; 12539 if (flags & 4) 12540 mask |= 0xff0000; 12541 if (flags & 8) 12542 mask |= 0xff000000; 12543 return (a & mask) | (b & ~mask); 12544 } 12545 12546 /* CRC helpers. 12547 * The upper bytes of val (above the number specified by 'bytes') must have 12548 * been zeroed out by the caller. 12549 */ 12550 uint32_t HELPER(crc32)(uint32_t acc, uint32_t val, uint32_t bytes) 12551 { 12552 uint8_t buf[4]; 12553 12554 stl_le_p(buf, val); 12555 12556 /* zlib crc32 converts the accumulator and output to one's complement. */ 12557 return crc32(acc ^ 0xffffffff, buf, bytes) ^ 0xffffffff; 12558 } 12559 12560 uint32_t HELPER(crc32c)(uint32_t acc, uint32_t val, uint32_t bytes) 12561 { 12562 uint8_t buf[4]; 12563 12564 stl_le_p(buf, val); 12565 12566 /* Linux crc32c converts the output to one's complement. */ 12567 return crc32c(acc, buf, bytes) ^ 0xffffffff; 12568 } 12569 12570 /* Return the exception level to which FP-disabled exceptions should 12571 * be taken, or 0 if FP is enabled. 12572 */ 12573 int fp_exception_el(CPUARMState *env, int cur_el) 12574 { 12575 #ifndef CONFIG_USER_ONLY 12576 /* CPACR and the CPTR registers don't exist before v6, so FP is 12577 * always accessible 12578 */ 12579 if (!arm_feature(env, ARM_FEATURE_V6)) { 12580 return 0; 12581 } 12582 12583 if (arm_feature(env, ARM_FEATURE_M)) { 12584 /* CPACR can cause a NOCP UsageFault taken to current security state */ 12585 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) { 12586 return 1; 12587 } 12588 12589 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) { 12590 if (!extract32(env->v7m.nsacr, 10, 1)) { 12591 /* FP insns cause a NOCP UsageFault taken to Secure */ 12592 return 3; 12593 } 12594 } 12595 12596 return 0; 12597 } 12598 12599 /* The CPACR controls traps to EL1, or PL1 if we're 32 bit: 12600 * 0, 2 : trap EL0 and EL1/PL1 accesses 12601 * 1 : trap only EL0 accesses 12602 * 3 : trap no accesses 12603 * This register is ignored if E2H+TGE are both set. 12604 */ 12605 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 12606 int fpen = extract32(env->cp15.cpacr_el1, 20, 2); 12607 12608 switch (fpen) { 12609 case 0: 12610 case 2: 12611 if (cur_el == 0 || cur_el == 1) { 12612 /* Trap to PL1, which might be EL1 or EL3 */ 12613 if (arm_is_secure(env) && !arm_el_is_aa64(env, 3)) { 12614 return 3; 12615 } 12616 return 1; 12617 } 12618 if (cur_el == 3 && !is_a64(env)) { 12619 /* Secure PL1 running at EL3 */ 12620 return 3; 12621 } 12622 break; 12623 case 1: 12624 if (cur_el == 0) { 12625 return 1; 12626 } 12627 break; 12628 case 3: 12629 break; 12630 } 12631 } 12632 12633 /* 12634 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode 12635 * to control non-secure access to the FPU. It doesn't have any 12636 * effect if EL3 is AArch64 or if EL3 doesn't exist at all. 12637 */ 12638 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 12639 cur_el <= 2 && !arm_is_secure_below_el3(env))) { 12640 if (!extract32(env->cp15.nsacr, 10, 1)) { 12641 /* FP insns act as UNDEF */ 12642 return cur_el == 2 ? 2 : 1; 12643 } 12644 } 12645 12646 /* For the CPTR registers we don't need to guard with an ARM_FEATURE 12647 * check because zero bits in the registers mean "don't trap". 12648 */ 12649 12650 /* CPTR_EL2 : present in v7VE or v8 */ 12651 if (cur_el <= 2 && extract32(env->cp15.cptr_el[2], 10, 1) 12652 && !arm_is_secure_below_el3(env)) { 12653 /* Trap FP ops at EL2, NS-EL1 or NS-EL0 to EL2 */ 12654 return 2; 12655 } 12656 12657 /* CPTR_EL3 : present in v8 */ 12658 if (extract32(env->cp15.cptr_el[3], 10, 1)) { 12659 /* Trap all FP ops to EL3 */ 12660 return 3; 12661 } 12662 #endif 12663 return 0; 12664 } 12665 12666 /* Return the exception level we're running at if this is our mmu_idx */ 12667 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx) 12668 { 12669 if (mmu_idx & ARM_MMU_IDX_M) { 12670 return mmu_idx & ARM_MMU_IDX_M_PRIV; 12671 } 12672 12673 switch (mmu_idx) { 12674 case ARMMMUIdx_E10_0: 12675 case ARMMMUIdx_E20_0: 12676 case ARMMMUIdx_SE10_0: 12677 return 0; 12678 case ARMMMUIdx_E10_1: 12679 case ARMMMUIdx_E10_1_PAN: 12680 case ARMMMUIdx_SE10_1: 12681 case ARMMMUIdx_SE10_1_PAN: 12682 return 1; 12683 case ARMMMUIdx_E2: 12684 case ARMMMUIdx_E20_2: 12685 case ARMMMUIdx_E20_2_PAN: 12686 return 2; 12687 case ARMMMUIdx_SE3: 12688 return 3; 12689 default: 12690 g_assert_not_reached(); 12691 } 12692 } 12693 12694 #ifndef CONFIG_TCG 12695 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate) 12696 { 12697 g_assert_not_reached(); 12698 } 12699 #endif 12700 12701 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el) 12702 { 12703 if (arm_feature(env, ARM_FEATURE_M)) { 12704 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure); 12705 } 12706 12707 /* See ARM pseudo-function ELIsInHost. */ 12708 switch (el) { 12709 case 0: 12710 if (arm_is_secure_below_el3(env)) { 12711 return ARMMMUIdx_SE10_0; 12712 } 12713 if ((env->cp15.hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE) 12714 && arm_el_is_aa64(env, 2)) { 12715 return ARMMMUIdx_E20_0; 12716 } 12717 return ARMMMUIdx_E10_0; 12718 case 1: 12719 if (arm_is_secure_below_el3(env)) { 12720 if (env->pstate & PSTATE_PAN) { 12721 return ARMMMUIdx_SE10_1_PAN; 12722 } 12723 return ARMMMUIdx_SE10_1; 12724 } 12725 if (env->pstate & PSTATE_PAN) { 12726 return ARMMMUIdx_E10_1_PAN; 12727 } 12728 return ARMMMUIdx_E10_1; 12729 case 2: 12730 /* TODO: ARMv8.4-SecEL2 */ 12731 /* Note that TGE does not apply at EL2. */ 12732 if ((env->cp15.hcr_el2 & HCR_E2H) && arm_el_is_aa64(env, 2)) { 12733 if (env->pstate & PSTATE_PAN) { 12734 return ARMMMUIdx_E20_2_PAN; 12735 } 12736 return ARMMMUIdx_E20_2; 12737 } 12738 return ARMMMUIdx_E2; 12739 case 3: 12740 return ARMMMUIdx_SE3; 12741 default: 12742 g_assert_not_reached(); 12743 } 12744 } 12745 12746 ARMMMUIdx arm_mmu_idx(CPUARMState *env) 12747 { 12748 return arm_mmu_idx_el(env, arm_current_el(env)); 12749 } 12750 12751 #ifndef CONFIG_USER_ONLY 12752 ARMMMUIdx arm_stage1_mmu_idx(CPUARMState *env) 12753 { 12754 return stage_1_mmu_idx(arm_mmu_idx(env)); 12755 } 12756 #endif 12757 12758 static uint32_t rebuild_hflags_common(CPUARMState *env, int fp_el, 12759 ARMMMUIdx mmu_idx, uint32_t flags) 12760 { 12761 flags = FIELD_DP32(flags, TBFLAG_ANY, FPEXC_EL, fp_el); 12762 flags = FIELD_DP32(flags, TBFLAG_ANY, MMUIDX, 12763 arm_to_core_mmu_idx(mmu_idx)); 12764 12765 if (arm_singlestep_active(env)) { 12766 flags = FIELD_DP32(flags, TBFLAG_ANY, SS_ACTIVE, 1); 12767 } 12768 return flags; 12769 } 12770 12771 static uint32_t rebuild_hflags_common_32(CPUARMState *env, int fp_el, 12772 ARMMMUIdx mmu_idx, uint32_t flags) 12773 { 12774 bool sctlr_b = arm_sctlr_b(env); 12775 12776 if (sctlr_b) { 12777 flags = FIELD_DP32(flags, TBFLAG_A32, SCTLR_B, 1); 12778 } 12779 if (arm_cpu_data_is_big_endian_a32(env, sctlr_b)) { 12780 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1); 12781 } 12782 flags = FIELD_DP32(flags, TBFLAG_A32, NS, !access_secure_reg(env)); 12783 12784 return rebuild_hflags_common(env, fp_el, mmu_idx, flags); 12785 } 12786 12787 static uint32_t rebuild_hflags_m32(CPUARMState *env, int fp_el, 12788 ARMMMUIdx mmu_idx) 12789 { 12790 uint32_t flags = 0; 12791 12792 if (arm_v7m_is_handler_mode(env)) { 12793 flags = FIELD_DP32(flags, TBFLAG_M32, HANDLER, 1); 12794 } 12795 12796 /* 12797 * v8M always applies stack limit checks unless CCR.STKOFHFNMIGN 12798 * is suppressing them because the requested execution priority 12799 * is less than 0. 12800 */ 12801 if (arm_feature(env, ARM_FEATURE_V8) && 12802 !((mmu_idx & ARM_MMU_IDX_M_NEGPRI) && 12803 (env->v7m.ccr[env->v7m.secure] & R_V7M_CCR_STKOFHFNMIGN_MASK))) { 12804 flags = FIELD_DP32(flags, TBFLAG_M32, STACKCHECK, 1); 12805 } 12806 12807 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags); 12808 } 12809 12810 static uint32_t rebuild_hflags_aprofile(CPUARMState *env) 12811 { 12812 int flags = 0; 12813 12814 flags = FIELD_DP32(flags, TBFLAG_ANY, DEBUG_TARGET_EL, 12815 arm_debug_target_el(env)); 12816 return flags; 12817 } 12818 12819 static uint32_t rebuild_hflags_a32(CPUARMState *env, int fp_el, 12820 ARMMMUIdx mmu_idx) 12821 { 12822 uint32_t flags = rebuild_hflags_aprofile(env); 12823 12824 if (arm_el_is_aa64(env, 1)) { 12825 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1); 12826 } 12827 12828 if (arm_current_el(env) < 2 && env->cp15.hstr_el2 && 12829 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 12830 flags = FIELD_DP32(flags, TBFLAG_A32, HSTR_ACTIVE, 1); 12831 } 12832 12833 return rebuild_hflags_common_32(env, fp_el, mmu_idx, flags); 12834 } 12835 12836 static uint32_t rebuild_hflags_a64(CPUARMState *env, int el, int fp_el, 12837 ARMMMUIdx mmu_idx) 12838 { 12839 uint32_t flags = rebuild_hflags_aprofile(env); 12840 ARMMMUIdx stage1 = stage_1_mmu_idx(mmu_idx); 12841 uint64_t tcr = regime_tcr(env, mmu_idx)->raw_tcr; 12842 uint64_t sctlr; 12843 int tbii, tbid; 12844 12845 flags = FIELD_DP32(flags, TBFLAG_ANY, AARCH64_STATE, 1); 12846 12847 /* Get control bits for tagged addresses. */ 12848 tbid = aa64_va_parameter_tbi(tcr, mmu_idx); 12849 tbii = tbid & ~aa64_va_parameter_tbid(tcr, mmu_idx); 12850 12851 flags = FIELD_DP32(flags, TBFLAG_A64, TBII, tbii); 12852 flags = FIELD_DP32(flags, TBFLAG_A64, TBID, tbid); 12853 12854 if (cpu_isar_feature(aa64_sve, env_archcpu(env))) { 12855 int sve_el = sve_exception_el(env, el); 12856 uint32_t zcr_len; 12857 12858 /* 12859 * If SVE is disabled, but FP is enabled, 12860 * then the effective len is 0. 12861 */ 12862 if (sve_el != 0 && fp_el == 0) { 12863 zcr_len = 0; 12864 } else { 12865 zcr_len = sve_zcr_len_for_el(env, el); 12866 } 12867 flags = FIELD_DP32(flags, TBFLAG_A64, SVEEXC_EL, sve_el); 12868 flags = FIELD_DP32(flags, TBFLAG_A64, ZCR_LEN, zcr_len); 12869 } 12870 12871 sctlr = regime_sctlr(env, stage1); 12872 12873 if (arm_cpu_data_is_big_endian_a64(el, sctlr)) { 12874 flags = FIELD_DP32(flags, TBFLAG_ANY, BE_DATA, 1); 12875 } 12876 12877 if (cpu_isar_feature(aa64_pauth, env_archcpu(env))) { 12878 /* 12879 * In order to save space in flags, we record only whether 12880 * pauth is "inactive", meaning all insns are implemented as 12881 * a nop, or "active" when some action must be performed. 12882 * The decision of which action to take is left to a helper. 12883 */ 12884 if (sctlr & (SCTLR_EnIA | SCTLR_EnIB | SCTLR_EnDA | SCTLR_EnDB)) { 12885 flags = FIELD_DP32(flags, TBFLAG_A64, PAUTH_ACTIVE, 1); 12886 } 12887 } 12888 12889 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) { 12890 /* Note that SCTLR_EL[23].BT == SCTLR_BT1. */ 12891 if (sctlr & (el == 0 ? SCTLR_BT0 : SCTLR_BT1)) { 12892 flags = FIELD_DP32(flags, TBFLAG_A64, BT, 1); 12893 } 12894 } 12895 12896 /* Compute the condition for using AccType_UNPRIV for LDTR et al. */ 12897 if (!(env->pstate & PSTATE_UAO)) { 12898 switch (mmu_idx) { 12899 case ARMMMUIdx_E10_1: 12900 case ARMMMUIdx_E10_1_PAN: 12901 case ARMMMUIdx_SE10_1: 12902 case ARMMMUIdx_SE10_1_PAN: 12903 /* TODO: ARMv8.3-NV */ 12904 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1); 12905 break; 12906 case ARMMMUIdx_E20_2: 12907 case ARMMMUIdx_E20_2_PAN: 12908 /* TODO: ARMv8.4-SecEL2 */ 12909 /* 12910 * Note that EL20_2 is gated by HCR_EL2.E2H == 1, but EL20_0 is 12911 * gated by HCR_EL2.<E2H,TGE> == '11', and so is LDTR. 12912 */ 12913 if (env->cp15.hcr_el2 & HCR_TGE) { 12914 flags = FIELD_DP32(flags, TBFLAG_A64, UNPRIV, 1); 12915 } 12916 break; 12917 default: 12918 break; 12919 } 12920 } 12921 12922 if (cpu_isar_feature(aa64_mte, env_archcpu(env))) { 12923 /* 12924 * Set MTE_ACTIVE if any access may be Checked, and leave clear 12925 * if all accesses must be Unchecked: 12926 * 1) If no TBI, then there are no tags in the address to check, 12927 * 2) If Tag Check Override, then all accesses are Unchecked, 12928 * 3) If Tag Check Fail == 0, then Checked access have no effect, 12929 * 4) If no Allocation Tag Access, then all accesses are Unchecked. 12930 */ 12931 if (allocation_tag_access_enabled(env, el, sctlr)) { 12932 flags = FIELD_DP32(flags, TBFLAG_A64, ATA, 1); 12933 if (tbid 12934 && !(env->pstate & PSTATE_TCO) 12935 && (sctlr & (el == 0 ? SCTLR_TCF0 : SCTLR_TCF))) { 12936 flags = FIELD_DP32(flags, TBFLAG_A64, MTE_ACTIVE, 1); 12937 } 12938 } 12939 /* And again for unprivileged accesses, if required. */ 12940 if (FIELD_EX32(flags, TBFLAG_A64, UNPRIV) 12941 && tbid 12942 && !(env->pstate & PSTATE_TCO) 12943 && (sctlr & SCTLR_TCF) 12944 && allocation_tag_access_enabled(env, 0, sctlr)) { 12945 flags = FIELD_DP32(flags, TBFLAG_A64, MTE0_ACTIVE, 1); 12946 } 12947 /* Cache TCMA as well as TBI. */ 12948 flags = FIELD_DP32(flags, TBFLAG_A64, TCMA, 12949 aa64_va_parameter_tcma(tcr, mmu_idx)); 12950 } 12951 12952 return rebuild_hflags_common(env, fp_el, mmu_idx, flags); 12953 } 12954 12955 static uint32_t rebuild_hflags_internal(CPUARMState *env) 12956 { 12957 int el = arm_current_el(env); 12958 int fp_el = fp_exception_el(env, el); 12959 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el); 12960 12961 if (is_a64(env)) { 12962 return rebuild_hflags_a64(env, el, fp_el, mmu_idx); 12963 } else if (arm_feature(env, ARM_FEATURE_M)) { 12964 return rebuild_hflags_m32(env, fp_el, mmu_idx); 12965 } else { 12966 return rebuild_hflags_a32(env, fp_el, mmu_idx); 12967 } 12968 } 12969 12970 void arm_rebuild_hflags(CPUARMState *env) 12971 { 12972 env->hflags = rebuild_hflags_internal(env); 12973 } 12974 12975 /* 12976 * If we have triggered a EL state change we can't rely on the 12977 * translator having passed it to us, we need to recompute. 12978 */ 12979 void HELPER(rebuild_hflags_m32_newel)(CPUARMState *env) 12980 { 12981 int el = arm_current_el(env); 12982 int fp_el = fp_exception_el(env, el); 12983 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el); 12984 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx); 12985 } 12986 12987 void HELPER(rebuild_hflags_m32)(CPUARMState *env, int el) 12988 { 12989 int fp_el = fp_exception_el(env, el); 12990 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el); 12991 12992 env->hflags = rebuild_hflags_m32(env, fp_el, mmu_idx); 12993 } 12994 12995 /* 12996 * If we have triggered a EL state change we can't rely on the 12997 * translator having passed it to us, we need to recompute. 12998 */ 12999 void HELPER(rebuild_hflags_a32_newel)(CPUARMState *env) 13000 { 13001 int el = arm_current_el(env); 13002 int fp_el = fp_exception_el(env, el); 13003 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el); 13004 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx); 13005 } 13006 13007 void HELPER(rebuild_hflags_a32)(CPUARMState *env, int el) 13008 { 13009 int fp_el = fp_exception_el(env, el); 13010 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el); 13011 13012 env->hflags = rebuild_hflags_a32(env, fp_el, mmu_idx); 13013 } 13014 13015 void HELPER(rebuild_hflags_a64)(CPUARMState *env, int el) 13016 { 13017 int fp_el = fp_exception_el(env, el); 13018 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, el); 13019 13020 env->hflags = rebuild_hflags_a64(env, el, fp_el, mmu_idx); 13021 } 13022 13023 static inline void assert_hflags_rebuild_correctly(CPUARMState *env) 13024 { 13025 #ifdef CONFIG_DEBUG_TCG 13026 uint32_t env_flags_current = env->hflags; 13027 uint32_t env_flags_rebuilt = rebuild_hflags_internal(env); 13028 13029 if (unlikely(env_flags_current != env_flags_rebuilt)) { 13030 fprintf(stderr, "TCG hflags mismatch (current:0x%08x rebuilt:0x%08x)\n", 13031 env_flags_current, env_flags_rebuilt); 13032 abort(); 13033 } 13034 #endif 13035 } 13036 13037 void cpu_get_tb_cpu_state(CPUARMState *env, target_ulong *pc, 13038 target_ulong *cs_base, uint32_t *pflags) 13039 { 13040 uint32_t flags = env->hflags; 13041 uint32_t pstate_for_ss; 13042 13043 *cs_base = 0; 13044 assert_hflags_rebuild_correctly(env); 13045 13046 if (FIELD_EX32(flags, TBFLAG_ANY, AARCH64_STATE)) { 13047 *pc = env->pc; 13048 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) { 13049 flags = FIELD_DP32(flags, TBFLAG_A64, BTYPE, env->btype); 13050 } 13051 pstate_for_ss = env->pstate; 13052 } else { 13053 *pc = env->regs[15]; 13054 13055 if (arm_feature(env, ARM_FEATURE_M)) { 13056 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 13057 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S) 13058 != env->v7m.secure) { 13059 flags = FIELD_DP32(flags, TBFLAG_M32, FPCCR_S_WRONG, 1); 13060 } 13061 13062 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) && 13063 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) || 13064 (env->v7m.secure && 13065 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) { 13066 /* 13067 * ASPEN is set, but FPCA/SFPA indicate that there is no 13068 * active FP context; we must create a new FP context before 13069 * executing any FP insn. 13070 */ 13071 flags = FIELD_DP32(flags, TBFLAG_M32, NEW_FP_CTXT_NEEDED, 1); 13072 } 13073 13074 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 13075 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) { 13076 flags = FIELD_DP32(flags, TBFLAG_M32, LSPACT, 1); 13077 } 13078 } else { 13079 /* 13080 * Note that XSCALE_CPAR shares bits with VECSTRIDE. 13081 * Note that VECLEN+VECSTRIDE are RES0 for M-profile. 13082 */ 13083 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 13084 flags = FIELD_DP32(flags, TBFLAG_A32, 13085 XSCALE_CPAR, env->cp15.c15_cpar); 13086 } else { 13087 flags = FIELD_DP32(flags, TBFLAG_A32, VECLEN, 13088 env->vfp.vec_len); 13089 flags = FIELD_DP32(flags, TBFLAG_A32, VECSTRIDE, 13090 env->vfp.vec_stride); 13091 } 13092 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) { 13093 flags = FIELD_DP32(flags, TBFLAG_A32, VFPEN, 1); 13094 } 13095 } 13096 13097 flags = FIELD_DP32(flags, TBFLAG_AM32, THUMB, env->thumb); 13098 flags = FIELD_DP32(flags, TBFLAG_AM32, CONDEXEC, env->condexec_bits); 13099 pstate_for_ss = env->uncached_cpsr; 13100 } 13101 13102 /* 13103 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine 13104 * states defined in the ARM ARM for software singlestep: 13105 * SS_ACTIVE PSTATE.SS State 13106 * 0 x Inactive (the TB flag for SS is always 0) 13107 * 1 0 Active-pending 13108 * 1 1 Active-not-pending 13109 * SS_ACTIVE is set in hflags; PSTATE_SS is computed every TB. 13110 */ 13111 if (FIELD_EX32(flags, TBFLAG_ANY, SS_ACTIVE) && 13112 (pstate_for_ss & PSTATE_SS)) { 13113 flags = FIELD_DP32(flags, TBFLAG_ANY, PSTATE_SS, 1); 13114 } 13115 13116 *pflags = flags; 13117 } 13118 13119 #ifdef TARGET_AARCH64 13120 /* 13121 * The manual says that when SVE is enabled and VQ is widened the 13122 * implementation is allowed to zero the previously inaccessible 13123 * portion of the registers. The corollary to that is that when 13124 * SVE is enabled and VQ is narrowed we are also allowed to zero 13125 * the now inaccessible portion of the registers. 13126 * 13127 * The intent of this is that no predicate bit beyond VQ is ever set. 13128 * Which means that some operations on predicate registers themselves 13129 * may operate on full uint64_t or even unrolled across the maximum 13130 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally 13131 * may well be cheaper than conditionals to restrict the operation 13132 * to the relevant portion of a uint16_t[16]. 13133 */ 13134 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq) 13135 { 13136 int i, j; 13137 uint64_t pmask; 13138 13139 assert(vq >= 1 && vq <= ARM_MAX_VQ); 13140 assert(vq <= env_archcpu(env)->sve_max_vq); 13141 13142 /* Zap the high bits of the zregs. */ 13143 for (i = 0; i < 32; i++) { 13144 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq)); 13145 } 13146 13147 /* Zap the high bits of the pregs and ffr. */ 13148 pmask = 0; 13149 if (vq & 3) { 13150 pmask = ~(-1ULL << (16 * (vq & 3))); 13151 } 13152 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) { 13153 for (i = 0; i < 17; ++i) { 13154 env->vfp.pregs[i].p[j] &= pmask; 13155 } 13156 pmask = 0; 13157 } 13158 } 13159 13160 /* 13161 * Notice a change in SVE vector size when changing EL. 13162 */ 13163 void aarch64_sve_change_el(CPUARMState *env, int old_el, 13164 int new_el, bool el0_a64) 13165 { 13166 ARMCPU *cpu = env_archcpu(env); 13167 int old_len, new_len; 13168 bool old_a64, new_a64; 13169 13170 /* Nothing to do if no SVE. */ 13171 if (!cpu_isar_feature(aa64_sve, cpu)) { 13172 return; 13173 } 13174 13175 /* Nothing to do if FP is disabled in either EL. */ 13176 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) { 13177 return; 13178 } 13179 13180 /* 13181 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped 13182 * at ELx, or not available because the EL is in AArch32 state, then 13183 * for all purposes other than a direct read, the ZCR_ELx.LEN field 13184 * has an effective value of 0". 13185 * 13186 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0). 13187 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition 13188 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that 13189 * we already have the correct register contents when encountering the 13190 * vq0->vq0 transition between EL0->EL1. 13191 */ 13192 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64; 13193 old_len = (old_a64 && !sve_exception_el(env, old_el) 13194 ? sve_zcr_len_for_el(env, old_el) : 0); 13195 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64; 13196 new_len = (new_a64 && !sve_exception_el(env, new_el) 13197 ? sve_zcr_len_for_el(env, new_el) : 0); 13198 13199 /* When changing vector length, clear inaccessible state. */ 13200 if (new_len < old_len) { 13201 aarch64_sve_narrow_vq(env, new_len + 1); 13202 } 13203 } 13204 #endif 13205