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/log.h" 11 #include "trace.h" 12 #include "cpu.h" 13 #include "internals.h" 14 #include "cpu-features.h" 15 #include "exec/helper-proto.h" 16 #include "exec/page-protection.h" 17 #include "exec/mmap-lock.h" 18 #include "qemu/main-loop.h" 19 #include "qemu/timer.h" 20 #include "qemu/bitops.h" 21 #include "qemu/qemu-print.h" 22 #include "exec/cputlb.h" 23 #include "exec/exec-all.h" 24 #include "exec/translation-block.h" 25 #include "hw/irq.h" 26 #include "system/cpu-timers.h" 27 #include "exec/icount.h" 28 #include "system/kvm.h" 29 #include "system/tcg.h" 30 #include "qapi/error.h" 31 #include "qemu/guest-random.h" 32 #ifdef CONFIG_TCG 33 #include "semihosting/common-semi.h" 34 #endif 35 #include "cpregs.h" 36 #include "target/arm/gtimer.h" 37 38 #define ARM_CPU_FREQ 1000000000 /* FIXME: 1 GHz, should be configurable */ 39 40 static void switch_mode(CPUARMState *env, int mode); 41 42 static uint64_t raw_read(CPUARMState *env, const ARMCPRegInfo *ri) 43 { 44 assert(ri->fieldoffset); 45 if (cpreg_field_is_64bit(ri)) { 46 return CPREG_FIELD64(env, ri); 47 } else { 48 return CPREG_FIELD32(env, ri); 49 } 50 } 51 52 void raw_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 53 { 54 assert(ri->fieldoffset); 55 if (cpreg_field_is_64bit(ri)) { 56 CPREG_FIELD64(env, ri) = value; 57 } else { 58 CPREG_FIELD32(env, ri) = value; 59 } 60 } 61 62 static void *raw_ptr(CPUARMState *env, const ARMCPRegInfo *ri) 63 { 64 return (char *)env + ri->fieldoffset; 65 } 66 67 uint64_t read_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri) 68 { 69 /* Raw read of a coprocessor register (as needed for migration, etc). */ 70 if (ri->type & ARM_CP_CONST) { 71 return ri->resetvalue; 72 } else if (ri->raw_readfn) { 73 return ri->raw_readfn(env, ri); 74 } else if (ri->readfn) { 75 return ri->readfn(env, ri); 76 } else { 77 return raw_read(env, ri); 78 } 79 } 80 81 static void write_raw_cp_reg(CPUARMState *env, const ARMCPRegInfo *ri, 82 uint64_t v) 83 { 84 /* 85 * Raw write of a coprocessor register (as needed for migration, etc). 86 * Note that constant registers are treated as write-ignored; the 87 * caller should check for success by whether a readback gives the 88 * value written. 89 */ 90 if (ri->type & ARM_CP_CONST) { 91 return; 92 } else if (ri->raw_writefn) { 93 ri->raw_writefn(env, ri, v); 94 } else if (ri->writefn) { 95 ri->writefn(env, ri, v); 96 } else { 97 raw_write(env, ri, v); 98 } 99 } 100 101 static bool raw_accessors_invalid(const ARMCPRegInfo *ri) 102 { 103 /* 104 * Return true if the regdef would cause an assertion if you called 105 * read_raw_cp_reg() or write_raw_cp_reg() on it (ie if it is a 106 * program bug for it not to have the NO_RAW flag). 107 * NB that returning false here doesn't necessarily mean that calling 108 * read/write_raw_cp_reg() is safe, because we can't distinguish "has 109 * read/write access functions which are safe for raw use" from "has 110 * read/write access functions which have side effects but has forgotten 111 * to provide raw access functions". 112 * The tests here line up with the conditions in read/write_raw_cp_reg() 113 * and assertions in raw_read()/raw_write(). 114 */ 115 if ((ri->type & ARM_CP_CONST) || 116 ri->fieldoffset || 117 ((ri->raw_writefn || ri->writefn) && (ri->raw_readfn || ri->readfn))) { 118 return false; 119 } 120 return true; 121 } 122 123 bool write_cpustate_to_list(ARMCPU *cpu, bool kvm_sync) 124 { 125 /* Write the coprocessor state from cpu->env to the (index,value) list. */ 126 int i; 127 bool ok = true; 128 129 for (i = 0; i < cpu->cpreg_array_len; i++) { 130 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 131 const ARMCPRegInfo *ri; 132 uint64_t newval; 133 134 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 135 if (!ri) { 136 ok = false; 137 continue; 138 } 139 if (ri->type & ARM_CP_NO_RAW) { 140 continue; 141 } 142 143 newval = read_raw_cp_reg(&cpu->env, ri); 144 if (kvm_sync) { 145 /* 146 * Only sync if the previous list->cpustate sync succeeded. 147 * Rather than tracking the success/failure state for every 148 * item in the list, we just recheck "does the raw write we must 149 * have made in write_list_to_cpustate() read back OK" here. 150 */ 151 uint64_t oldval = cpu->cpreg_values[i]; 152 153 if (oldval == newval) { 154 continue; 155 } 156 157 write_raw_cp_reg(&cpu->env, ri, oldval); 158 if (read_raw_cp_reg(&cpu->env, ri) != oldval) { 159 continue; 160 } 161 162 write_raw_cp_reg(&cpu->env, ri, newval); 163 } 164 cpu->cpreg_values[i] = newval; 165 } 166 return ok; 167 } 168 169 bool write_list_to_cpustate(ARMCPU *cpu) 170 { 171 int i; 172 bool ok = true; 173 174 for (i = 0; i < cpu->cpreg_array_len; i++) { 175 uint32_t regidx = kvm_to_cpreg_id(cpu->cpreg_indexes[i]); 176 uint64_t v = cpu->cpreg_values[i]; 177 const ARMCPRegInfo *ri; 178 179 ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 180 if (!ri) { 181 ok = false; 182 continue; 183 } 184 if (ri->type & ARM_CP_NO_RAW) { 185 continue; 186 } 187 /* 188 * Write value and confirm it reads back as written 189 * (to catch read-only registers and partially read-only 190 * registers where the incoming migration value doesn't match) 191 */ 192 write_raw_cp_reg(&cpu->env, ri, v); 193 if (read_raw_cp_reg(&cpu->env, ri) != v) { 194 ok = false; 195 } 196 } 197 return ok; 198 } 199 200 static void add_cpreg_to_list(gpointer key, gpointer opaque) 201 { 202 ARMCPU *cpu = opaque; 203 uint32_t regidx = (uintptr_t)key; 204 const ARMCPRegInfo *ri = get_arm_cp_reginfo(cpu->cp_regs, regidx); 205 206 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) { 207 cpu->cpreg_indexes[cpu->cpreg_array_len] = cpreg_to_kvm_id(regidx); 208 /* The value array need not be initialized at this point */ 209 cpu->cpreg_array_len++; 210 } 211 } 212 213 static void count_cpreg(gpointer key, gpointer opaque) 214 { 215 ARMCPU *cpu = opaque; 216 const ARMCPRegInfo *ri; 217 218 ri = g_hash_table_lookup(cpu->cp_regs, key); 219 220 if (!(ri->type & (ARM_CP_NO_RAW | ARM_CP_ALIAS))) { 221 cpu->cpreg_array_len++; 222 } 223 } 224 225 static gint cpreg_key_compare(gconstpointer a, gconstpointer b) 226 { 227 uint64_t aidx = cpreg_to_kvm_id((uintptr_t)a); 228 uint64_t bidx = cpreg_to_kvm_id((uintptr_t)b); 229 230 if (aidx > bidx) { 231 return 1; 232 } 233 if (aidx < bidx) { 234 return -1; 235 } 236 return 0; 237 } 238 239 void init_cpreg_list(ARMCPU *cpu) 240 { 241 /* 242 * Initialise the cpreg_tuples[] array based on the cp_regs hash. 243 * Note that we require cpreg_tuples[] to be sorted by key ID. 244 */ 245 GList *keys; 246 int arraylen; 247 248 keys = g_hash_table_get_keys(cpu->cp_regs); 249 keys = g_list_sort(keys, cpreg_key_compare); 250 251 cpu->cpreg_array_len = 0; 252 253 g_list_foreach(keys, count_cpreg, cpu); 254 255 arraylen = cpu->cpreg_array_len; 256 cpu->cpreg_indexes = g_new(uint64_t, arraylen); 257 cpu->cpreg_values = g_new(uint64_t, arraylen); 258 cpu->cpreg_vmstate_indexes = g_new(uint64_t, arraylen); 259 cpu->cpreg_vmstate_values = g_new(uint64_t, arraylen); 260 cpu->cpreg_vmstate_array_len = cpu->cpreg_array_len; 261 cpu->cpreg_array_len = 0; 262 263 g_list_foreach(keys, add_cpreg_to_list, cpu); 264 265 assert(cpu->cpreg_array_len == arraylen); 266 267 g_list_free(keys); 268 } 269 270 static bool arm_pan_enabled(CPUARMState *env) 271 { 272 if (is_a64(env)) { 273 if ((arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1)) == (HCR_NV | HCR_NV1)) { 274 return false; 275 } 276 return env->pstate & PSTATE_PAN; 277 } else { 278 return env->uncached_cpsr & CPSR_PAN; 279 } 280 } 281 282 /* 283 * Some registers are not accessible from AArch32 EL3 if SCR.NS == 0. 284 */ 285 static CPAccessResult access_el3_aa32ns(CPUARMState *env, 286 const ARMCPRegInfo *ri, 287 bool isread) 288 { 289 if (!is_a64(env) && arm_current_el(env) == 3 && 290 arm_is_secure_below_el3(env)) { 291 return CP_ACCESS_UNDEFINED; 292 } 293 return CP_ACCESS_OK; 294 } 295 296 /* 297 * Some secure-only AArch32 registers trap to EL3 if used from 298 * Secure EL1 (but are just ordinary UNDEF in other non-EL3 contexts). 299 * Note that an access from Secure EL1 can only happen if EL3 is AArch64. 300 * We assume that the .access field is set to PL1_RW. 301 */ 302 static CPAccessResult access_trap_aa32s_el1(CPUARMState *env, 303 const ARMCPRegInfo *ri, 304 bool isread) 305 { 306 if (arm_current_el(env) == 3) { 307 return CP_ACCESS_OK; 308 } 309 if (arm_is_secure_below_el3(env)) { 310 if (env->cp15.scr_el3 & SCR_EEL2) { 311 return CP_ACCESS_TRAP_EL2; 312 } 313 return CP_ACCESS_TRAP_EL3; 314 } 315 /* This will be EL1 NS and EL2 NS, which just UNDEF */ 316 return CP_ACCESS_UNDEFINED; 317 } 318 319 /* 320 * Check for traps to performance monitor registers, which are controlled 321 * by MDCR_EL2.TPM for EL2 and MDCR_EL3.TPM for EL3. 322 */ 323 static CPAccessResult access_tpm(CPUARMState *env, const ARMCPRegInfo *ri, 324 bool isread) 325 { 326 int el = arm_current_el(env); 327 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env); 328 329 if (el < 2 && (mdcr_el2 & MDCR_TPM)) { 330 return CP_ACCESS_TRAP_EL2; 331 } 332 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 333 return CP_ACCESS_TRAP_EL3; 334 } 335 return CP_ACCESS_OK; 336 } 337 338 /* Check for traps from EL1 due to HCR_EL2.TVM and HCR_EL2.TRVM. */ 339 CPAccessResult access_tvm_trvm(CPUARMState *env, const ARMCPRegInfo *ri, 340 bool isread) 341 { 342 if (arm_current_el(env) == 1) { 343 uint64_t trap = isread ? HCR_TRVM : HCR_TVM; 344 if (arm_hcr_el2_eff(env) & trap) { 345 return CP_ACCESS_TRAP_EL2; 346 } 347 } 348 return CP_ACCESS_OK; 349 } 350 351 /* Check for traps from EL1 due to HCR_EL2.TSW. */ 352 static CPAccessResult access_tsw(CPUARMState *env, const ARMCPRegInfo *ri, 353 bool isread) 354 { 355 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TSW)) { 356 return CP_ACCESS_TRAP_EL2; 357 } 358 return CP_ACCESS_OK; 359 } 360 361 /* Check for traps from EL1 due to HCR_EL2.TACR. */ 362 static CPAccessResult access_tacr(CPUARMState *env, const ARMCPRegInfo *ri, 363 bool isread) 364 { 365 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TACR)) { 366 return CP_ACCESS_TRAP_EL2; 367 } 368 return CP_ACCESS_OK; 369 } 370 371 static void dacr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 372 { 373 ARMCPU *cpu = env_archcpu(env); 374 375 raw_write(env, ri, value); 376 tlb_flush(CPU(cpu)); /* Flush TLB as domain not tracked in TLB */ 377 } 378 379 static void fcse_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 380 { 381 ARMCPU *cpu = env_archcpu(env); 382 383 if (raw_read(env, ri) != value) { 384 /* 385 * Unlike real hardware the qemu TLB uses virtual addresses, 386 * not modified virtual addresses, so this causes a TLB flush. 387 */ 388 tlb_flush(CPU(cpu)); 389 raw_write(env, ri, value); 390 } 391 } 392 393 static void contextidr_write(CPUARMState *env, const ARMCPRegInfo *ri, 394 uint64_t value) 395 { 396 ARMCPU *cpu = env_archcpu(env); 397 398 if (raw_read(env, ri) != value && !arm_feature(env, ARM_FEATURE_PMSA) 399 && !extended_addresses_enabled(env)) { 400 /* 401 * For VMSA (when not using the LPAE long descriptor page table 402 * format) this register includes the ASID, so do a TLB flush. 403 * For PMSA it is purely a process ID and no action is needed. 404 */ 405 tlb_flush(CPU(cpu)); 406 } 407 raw_write(env, ri, value); 408 } 409 410 int alle1_tlbmask(CPUARMState *env) 411 { 412 /* 413 * Note that the 'ALL' scope must invalidate both stage 1 and 414 * stage 2 translations, whereas most other scopes only invalidate 415 * stage 1 translations. 416 * 417 * For AArch32 this is only used for TLBIALLNSNH and VTTBR 418 * writes, so only needs to apply to NS PL1&0, not S PL1&0. 419 */ 420 return (ARMMMUIdxBit_E10_1 | 421 ARMMMUIdxBit_E10_1_PAN | 422 ARMMMUIdxBit_E10_0 | 423 ARMMMUIdxBit_Stage2 | 424 ARMMMUIdxBit_Stage2_S); 425 } 426 427 static const ARMCPRegInfo cp_reginfo[] = { 428 /* 429 * Define the secure and non-secure FCSE identifier CP registers 430 * separately because there is no secure bank in V8 (no _EL3). This allows 431 * the secure register to be properly reset and migrated. There is also no 432 * v8 EL1 version of the register so the non-secure instance stands alone. 433 */ 434 { .name = "FCSEIDR", 435 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 436 .access = PL1_RW, .secure = ARM_CP_SECSTATE_NS, 437 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_ns), 438 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 439 { .name = "FCSEIDR_S", 440 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 0, 441 .access = PL1_RW, .secure = ARM_CP_SECSTATE_S, 442 .fieldoffset = offsetof(CPUARMState, cp15.fcseidr_s), 443 .resetvalue = 0, .writefn = fcse_write, .raw_writefn = raw_write, }, 444 /* 445 * Define the secure and non-secure context identifier CP registers 446 * separately because there is no secure bank in V8 (no _EL3). This allows 447 * the secure register to be properly reset and migrated. In the 448 * non-secure case, the 32-bit register will have reset and migration 449 * disabled during registration as it is handled by the 64-bit instance. 450 */ 451 { .name = "CONTEXTIDR_EL1", .state = ARM_CP_STATE_BOTH, 452 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 453 .access = PL1_RW, .accessfn = access_tvm_trvm, 454 .fgt = FGT_CONTEXTIDR_EL1, 455 .nv2_redirect_offset = 0x108 | NV2_REDIR_NV1, 456 .secure = ARM_CP_SECSTATE_NS, 457 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[1]), 458 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 459 { .name = "CONTEXTIDR_S", .state = ARM_CP_STATE_AA32, 460 .cp = 15, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 1, 461 .access = PL1_RW, .accessfn = access_tvm_trvm, 462 .secure = ARM_CP_SECSTATE_S, 463 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_s), 464 .resetvalue = 0, .writefn = contextidr_write, .raw_writefn = raw_write, }, 465 }; 466 467 static const ARMCPRegInfo not_v8_cp_reginfo[] = { 468 /* 469 * NB: Some of these registers exist in v8 but with more precise 470 * definitions that don't use CP_ANY wildcards (mostly in v8_cp_reginfo[]). 471 */ 472 /* MMU Domain access control / MPU write buffer control */ 473 { .name = "DACR", 474 .cp = 15, .opc1 = CP_ANY, .crn = 3, .crm = CP_ANY, .opc2 = CP_ANY, 475 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 476 .writefn = dacr_write, .raw_writefn = raw_write, 477 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 478 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 479 /* 480 * ARMv7 allocates a range of implementation defined TLB LOCKDOWN regs. 481 * For v6 and v5, these mappings are overly broad. 482 */ 483 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 0, 484 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 485 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 1, 486 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 487 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 4, 488 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 489 { .name = "TLB_LOCKDOWN", .cp = 15, .crn = 10, .crm = 8, 490 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_NOP }, 491 /* Cache maintenance ops; some of this space may be overridden later. */ 492 { .name = "CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 493 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 494 .type = ARM_CP_NOP | ARM_CP_OVERRIDE }, 495 }; 496 497 static const ARMCPRegInfo not_v6_cp_reginfo[] = { 498 /* 499 * Not all pre-v6 cores implemented this WFI, so this is slightly 500 * over-broad. 501 */ 502 { .name = "WFI_v5", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = 2, 503 .access = PL1_W, .type = ARM_CP_WFI }, 504 }; 505 506 static const ARMCPRegInfo not_v7_cp_reginfo[] = { 507 /* 508 * Standard v6 WFI (also used in some pre-v6 cores); not in v7 (which 509 * is UNPREDICTABLE; we choose to NOP as most implementations do). 510 */ 511 { .name = "WFI_v6", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 512 .access = PL1_W, .type = ARM_CP_WFI }, 513 /* 514 * L1 cache lockdown. Not architectural in v6 and earlier but in practice 515 * implemented in 926, 946, 1026, 1136, 1176 and 11MPCore. StrongARM and 516 * OMAPCP will override this space. 517 */ 518 { .name = "DLOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 0, 519 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_data), 520 .resetvalue = 0 }, 521 { .name = "ILOCKDOWN", .cp = 15, .crn = 9, .crm = 0, .opc1 = 0, .opc2 = 1, 522 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.c9_insn), 523 .resetvalue = 0 }, 524 /* v6 doesn't have the cache ID registers but Linux reads them anyway */ 525 { .name = "DUMMY", .cp = 15, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = CP_ANY, 526 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 527 .resetvalue = 0 }, 528 /* 529 * We don't implement pre-v7 debug but most CPUs had at least a DBGDIDR; 530 * implementing it as RAZ means the "debug architecture version" bits 531 * will read as a reserved value, which should cause Linux to not try 532 * to use the debug hardware. 533 */ 534 { .name = "DBGDIDR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 0, 535 .access = PL0_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 536 { .name = "PRRR", .cp = 15, .crn = 10, .crm = 2, 537 .opc1 = 0, .opc2 = 0, .access = PL1_RW, .type = ARM_CP_NOP }, 538 { .name = "NMRR", .cp = 15, .crn = 10, .crm = 2, 539 .opc1 = 0, .opc2 = 1, .access = PL1_RW, .type = ARM_CP_NOP }, 540 }; 541 542 static void cpacr_write(CPUARMState *env, const ARMCPRegInfo *ri, 543 uint64_t value) 544 { 545 uint32_t mask = 0; 546 547 /* In ARMv8 most bits of CPACR_EL1 are RES0. */ 548 if (!arm_feature(env, ARM_FEATURE_V8)) { 549 /* 550 * ARMv7 defines bits for unimplemented coprocessors as RAZ/WI. 551 * ASEDIS [31] and D32DIS [30] are both UNK/SBZP without VFP. 552 * TRCDIS [28] is RAZ/WI since we do not implement a trace macrocell. 553 */ 554 if (cpu_isar_feature(aa32_vfp_simd, env_archcpu(env))) { 555 /* VFP coprocessor: cp10 & cp11 [23:20] */ 556 mask |= R_CPACR_ASEDIS_MASK | 557 R_CPACR_D32DIS_MASK | 558 R_CPACR_CP11_MASK | 559 R_CPACR_CP10_MASK; 560 561 if (!arm_feature(env, ARM_FEATURE_NEON)) { 562 /* ASEDIS [31] bit is RAO/WI */ 563 value |= R_CPACR_ASEDIS_MASK; 564 } 565 566 /* 567 * VFPv3 and upwards with NEON implement 32 double precision 568 * registers (D0-D31). 569 */ 570 if (!cpu_isar_feature(aa32_simd_r32, env_archcpu(env))) { 571 /* D32DIS [30] is RAO/WI if D16-31 are not implemented. */ 572 value |= R_CPACR_D32DIS_MASK; 573 } 574 } 575 value &= mask; 576 } 577 578 /* 579 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10 580 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00. 581 */ 582 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 583 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 584 mask = R_CPACR_CP11_MASK | R_CPACR_CP10_MASK; 585 value = (value & ~mask) | (env->cp15.cpacr_el1 & mask); 586 } 587 588 env->cp15.cpacr_el1 = value; 589 } 590 591 static uint64_t cpacr_read(CPUARMState *env, const ARMCPRegInfo *ri) 592 { 593 /* 594 * For A-profile AArch32 EL3 (but not M-profile secure mode), if NSACR.CP10 595 * is 0 then CPACR.{CP11,CP10} ignore writes and read as 0b00. 596 */ 597 uint64_t value = env->cp15.cpacr_el1; 598 599 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 600 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 601 value = ~(R_CPACR_CP11_MASK | R_CPACR_CP10_MASK); 602 } 603 return value; 604 } 605 606 607 static void cpacr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 608 { 609 /* 610 * Call cpacr_write() so that we reset with the correct RAO bits set 611 * for our CPU features. 612 */ 613 cpacr_write(env, ri, 0); 614 } 615 616 static CPAccessResult cpacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 617 bool isread) 618 { 619 if (arm_feature(env, ARM_FEATURE_V8)) { 620 /* Check if CPACR accesses are to be trapped to EL2 */ 621 if (arm_current_el(env) == 1 && arm_is_el2_enabled(env) && 622 FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TCPAC)) { 623 return CP_ACCESS_TRAP_EL2; 624 /* Check if CPACR accesses are to be trapped to EL3 */ 625 } else if (arm_current_el(env) < 3 && 626 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) { 627 return CP_ACCESS_TRAP_EL3; 628 } 629 } 630 631 return CP_ACCESS_OK; 632 } 633 634 static CPAccessResult cptr_access(CPUARMState *env, const ARMCPRegInfo *ri, 635 bool isread) 636 { 637 /* Check if CPTR accesses are set to trap to EL3 */ 638 if (arm_current_el(env) == 2 && 639 FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TCPAC)) { 640 return CP_ACCESS_TRAP_EL3; 641 } 642 643 return CP_ACCESS_OK; 644 } 645 646 static const ARMCPRegInfo v6_cp_reginfo[] = { 647 /* prefetch by MVA in v6, NOP in v7 */ 648 { .name = "MVA_prefetch", 649 .cp = 15, .crn = 7, .crm = 13, .opc1 = 0, .opc2 = 1, 650 .access = PL1_W, .type = ARM_CP_NOP }, 651 /* 652 * We need to break the TB after ISB to execute self-modifying code 653 * correctly and also to take any pending interrupts immediately. 654 * So use arm_cp_write_ignore() function instead of ARM_CP_NOP flag. 655 */ 656 { .name = "ISB", .cp = 15, .crn = 7, .crm = 5, .opc1 = 0, .opc2 = 4, 657 .access = PL0_W, .type = ARM_CP_NO_RAW, .writefn = arm_cp_write_ignore }, 658 { .name = "DSB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 4, 659 .access = PL0_W, .type = ARM_CP_NOP }, 660 { .name = "DMB", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 5, 661 .access = PL0_W, .type = ARM_CP_NOP }, 662 { .name = "IFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 2, 663 .access = PL1_RW, .accessfn = access_tvm_trvm, 664 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ifar_s), 665 offsetof(CPUARMState, cp15.ifar_ns) }, 666 .resetvalue = 0, }, 667 /* 668 * Watchpoint Fault Address Register : should actually only be present 669 * for 1136, 1176, 11MPCore. 670 */ 671 { .name = "WFAR", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 1, 672 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0, }, 673 { .name = "CPACR", .state = ARM_CP_STATE_BOTH, .opc0 = 3, 674 .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 2, .accessfn = cpacr_access, 675 .fgt = FGT_CPACR_EL1, 676 .nv2_redirect_offset = 0x100 | NV2_REDIR_NV1, 677 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.cpacr_el1), 678 .resetfn = cpacr_reset, .writefn = cpacr_write, .readfn = cpacr_read }, 679 }; 680 681 typedef struct pm_event { 682 uint16_t number; /* PMEVTYPER.evtCount is 16 bits wide */ 683 /* If the event is supported on this CPU (used to generate PMCEID[01]) */ 684 bool (*supported)(CPUARMState *); 685 /* 686 * Retrieve the current count of the underlying event. The programmed 687 * counters hold a difference from the return value from this function 688 */ 689 uint64_t (*get_count)(CPUARMState *); 690 /* 691 * Return how many nanoseconds it will take (at a minimum) for count events 692 * to occur. A negative value indicates the counter will never overflow, or 693 * that the counter has otherwise arranged for the overflow bit to be set 694 * and the PMU interrupt to be raised on overflow. 695 */ 696 int64_t (*ns_per_count)(uint64_t); 697 } pm_event; 698 699 static bool event_always_supported(CPUARMState *env) 700 { 701 return true; 702 } 703 704 static uint64_t swinc_get_count(CPUARMState *env) 705 { 706 /* 707 * SW_INCR events are written directly to the pmevcntr's by writes to 708 * PMSWINC, so there is no underlying count maintained by the PMU itself 709 */ 710 return 0; 711 } 712 713 static int64_t swinc_ns_per(uint64_t ignored) 714 { 715 return -1; 716 } 717 718 /* 719 * Return the underlying cycle count for the PMU cycle counters. If we're in 720 * usermode, simply return 0. 721 */ 722 static uint64_t cycles_get_count(CPUARMState *env) 723 { 724 #ifndef CONFIG_USER_ONLY 725 return muldiv64(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 726 ARM_CPU_FREQ, NANOSECONDS_PER_SECOND); 727 #else 728 return cpu_get_host_ticks(); 729 #endif 730 } 731 732 #ifndef CONFIG_USER_ONLY 733 static int64_t cycles_ns_per(uint64_t cycles) 734 { 735 return (ARM_CPU_FREQ / NANOSECONDS_PER_SECOND) * cycles; 736 } 737 738 static bool instructions_supported(CPUARMState *env) 739 { 740 /* Precise instruction counting */ 741 return icount_enabled() == ICOUNT_PRECISE; 742 } 743 744 static uint64_t instructions_get_count(CPUARMState *env) 745 { 746 assert(icount_enabled() == ICOUNT_PRECISE); 747 return (uint64_t)icount_get_raw(); 748 } 749 750 static int64_t instructions_ns_per(uint64_t icount) 751 { 752 assert(icount_enabled() == ICOUNT_PRECISE); 753 return icount_to_ns((int64_t)icount); 754 } 755 #endif 756 757 static bool pmuv3p1_events_supported(CPUARMState *env) 758 { 759 /* For events which are supported in any v8.1 PMU */ 760 return cpu_isar_feature(any_pmuv3p1, env_archcpu(env)); 761 } 762 763 static bool pmuv3p4_events_supported(CPUARMState *env) 764 { 765 /* For events which are supported in any v8.1 PMU */ 766 return cpu_isar_feature(any_pmuv3p4, env_archcpu(env)); 767 } 768 769 static uint64_t zero_event_get_count(CPUARMState *env) 770 { 771 /* For events which on QEMU never fire, so their count is always zero */ 772 return 0; 773 } 774 775 static int64_t zero_event_ns_per(uint64_t cycles) 776 { 777 /* An event which never fires can never overflow */ 778 return -1; 779 } 780 781 static const pm_event pm_events[] = { 782 { .number = 0x000, /* SW_INCR */ 783 .supported = event_always_supported, 784 .get_count = swinc_get_count, 785 .ns_per_count = swinc_ns_per, 786 }, 787 #ifndef CONFIG_USER_ONLY 788 { .number = 0x008, /* INST_RETIRED, Instruction architecturally executed */ 789 .supported = instructions_supported, 790 .get_count = instructions_get_count, 791 .ns_per_count = instructions_ns_per, 792 }, 793 { .number = 0x011, /* CPU_CYCLES, Cycle */ 794 .supported = event_always_supported, 795 .get_count = cycles_get_count, 796 .ns_per_count = cycles_ns_per, 797 }, 798 #endif 799 { .number = 0x023, /* STALL_FRONTEND */ 800 .supported = pmuv3p1_events_supported, 801 .get_count = zero_event_get_count, 802 .ns_per_count = zero_event_ns_per, 803 }, 804 { .number = 0x024, /* STALL_BACKEND */ 805 .supported = pmuv3p1_events_supported, 806 .get_count = zero_event_get_count, 807 .ns_per_count = zero_event_ns_per, 808 }, 809 { .number = 0x03c, /* STALL */ 810 .supported = pmuv3p4_events_supported, 811 .get_count = zero_event_get_count, 812 .ns_per_count = zero_event_ns_per, 813 }, 814 }; 815 816 /* 817 * Note: Before increasing MAX_EVENT_ID beyond 0x3f into the 0x40xx range of 818 * events (i.e. the statistical profiling extension), this implementation 819 * should first be updated to something sparse instead of the current 820 * supported_event_map[] array. 821 */ 822 #define MAX_EVENT_ID 0x3c 823 #define UNSUPPORTED_EVENT UINT16_MAX 824 static uint16_t supported_event_map[MAX_EVENT_ID + 1]; 825 826 /* 827 * Called upon CPU initialization to initialize PMCEID[01]_EL0 and build a map 828 * of ARM event numbers to indices in our pm_events array. 829 * 830 * Note: Events in the 0x40XX range are not currently supported. 831 */ 832 void pmu_init(ARMCPU *cpu) 833 { 834 unsigned int i; 835 836 /* 837 * Empty supported_event_map and cpu->pmceid[01] before adding supported 838 * events to them 839 */ 840 for (i = 0; i < ARRAY_SIZE(supported_event_map); i++) { 841 supported_event_map[i] = UNSUPPORTED_EVENT; 842 } 843 cpu->pmceid0 = 0; 844 cpu->pmceid1 = 0; 845 846 for (i = 0; i < ARRAY_SIZE(pm_events); i++) { 847 const pm_event *cnt = &pm_events[i]; 848 assert(cnt->number <= MAX_EVENT_ID); 849 /* We do not currently support events in the 0x40xx range */ 850 assert(cnt->number <= 0x3f); 851 852 if (cnt->supported(&cpu->env)) { 853 supported_event_map[cnt->number] = i; 854 uint64_t event_mask = 1ULL << (cnt->number & 0x1f); 855 if (cnt->number & 0x20) { 856 cpu->pmceid1 |= event_mask; 857 } else { 858 cpu->pmceid0 |= event_mask; 859 } 860 } 861 } 862 } 863 864 /* 865 * Check at runtime whether a PMU event is supported for the current machine 866 */ 867 static bool event_supported(uint16_t number) 868 { 869 if (number > MAX_EVENT_ID) { 870 return false; 871 } 872 return supported_event_map[number] != UNSUPPORTED_EVENT; 873 } 874 875 static CPAccessResult pmreg_access(CPUARMState *env, const ARMCPRegInfo *ri, 876 bool isread) 877 { 878 /* 879 * Performance monitor registers user accessibility is controlled 880 * by PMUSERENR. MDCR_EL2.TPM and MDCR_EL3.TPM allow configurable 881 * trapping to EL2 or EL3 for other accesses. 882 */ 883 int el = arm_current_el(env); 884 uint64_t mdcr_el2 = arm_mdcr_el2_eff(env); 885 886 if (el == 0 && !(env->cp15.c9_pmuserenr & 1)) { 887 return CP_ACCESS_TRAP_EL1; 888 } 889 if (el < 2 && (mdcr_el2 & MDCR_TPM)) { 890 return CP_ACCESS_TRAP_EL2; 891 } 892 if (el < 3 && (env->cp15.mdcr_el3 & MDCR_TPM)) { 893 return CP_ACCESS_TRAP_EL3; 894 } 895 896 return CP_ACCESS_OK; 897 } 898 899 static CPAccessResult pmreg_access_xevcntr(CPUARMState *env, 900 const ARMCPRegInfo *ri, 901 bool isread) 902 { 903 /* ER: event counter read trap control */ 904 if (arm_feature(env, ARM_FEATURE_V8) 905 && arm_current_el(env) == 0 906 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0 907 && isread) { 908 return CP_ACCESS_OK; 909 } 910 911 return pmreg_access(env, ri, isread); 912 } 913 914 static CPAccessResult pmreg_access_swinc(CPUARMState *env, 915 const ARMCPRegInfo *ri, 916 bool isread) 917 { 918 /* SW: software increment write trap control */ 919 if (arm_feature(env, ARM_FEATURE_V8) 920 && arm_current_el(env) == 0 921 && (env->cp15.c9_pmuserenr & (1 << 1)) != 0 922 && !isread) { 923 return CP_ACCESS_OK; 924 } 925 926 return pmreg_access(env, ri, isread); 927 } 928 929 static CPAccessResult pmreg_access_selr(CPUARMState *env, 930 const ARMCPRegInfo *ri, 931 bool isread) 932 { 933 /* ER: event counter read trap control */ 934 if (arm_feature(env, ARM_FEATURE_V8) 935 && arm_current_el(env) == 0 936 && (env->cp15.c9_pmuserenr & (1 << 3)) != 0) { 937 return CP_ACCESS_OK; 938 } 939 940 return pmreg_access(env, ri, isread); 941 } 942 943 static CPAccessResult pmreg_access_ccntr(CPUARMState *env, 944 const ARMCPRegInfo *ri, 945 bool isread) 946 { 947 /* CR: cycle counter read trap control */ 948 if (arm_feature(env, ARM_FEATURE_V8) 949 && arm_current_el(env) == 0 950 && (env->cp15.c9_pmuserenr & (1 << 2)) != 0 951 && isread) { 952 return CP_ACCESS_OK; 953 } 954 955 return pmreg_access(env, ri, isread); 956 } 957 958 /* 959 * Bits in MDCR_EL2 and MDCR_EL3 which pmu_counter_enabled() looks at. 960 * We use these to decide whether we need to wrap a write to MDCR_EL2 961 * or MDCR_EL3 in pmu_op_start()/pmu_op_finish() calls. 962 */ 963 #define MDCR_EL2_PMU_ENABLE_BITS \ 964 (MDCR_HPME | MDCR_HPMD | MDCR_HPMN | MDCR_HCCD | MDCR_HLP) 965 #define MDCR_EL3_PMU_ENABLE_BITS (MDCR_SPME | MDCR_SCCD) 966 967 /* 968 * Returns true if the counter (pass 31 for PMCCNTR) should count events using 969 * the current EL, security state, and register configuration. 970 */ 971 static bool pmu_counter_enabled(CPUARMState *env, uint8_t counter) 972 { 973 uint64_t filter; 974 bool e, p, u, nsk, nsu, nsh, m; 975 bool enabled, prohibited = false, filtered; 976 bool secure = arm_is_secure(env); 977 int el = arm_current_el(env); 978 uint64_t mdcr_el2; 979 uint8_t hpmn; 980 981 /* 982 * We might be called for M-profile cores where MDCR_EL2 doesn't 983 * exist and arm_mdcr_el2_eff() will assert, so this early-exit check 984 * must be before we read that value. 985 */ 986 if (!arm_feature(env, ARM_FEATURE_PMU)) { 987 return false; 988 } 989 990 mdcr_el2 = arm_mdcr_el2_eff(env); 991 hpmn = mdcr_el2 & MDCR_HPMN; 992 993 if (!arm_feature(env, ARM_FEATURE_EL2) || 994 (counter < hpmn || counter == 31)) { 995 e = env->cp15.c9_pmcr & PMCRE; 996 } else { 997 e = mdcr_el2 & MDCR_HPME; 998 } 999 enabled = e && (env->cp15.c9_pmcnten & (1 << counter)); 1000 1001 /* Is event counting prohibited? */ 1002 if (el == 2 && (counter < hpmn || counter == 31)) { 1003 prohibited = mdcr_el2 & MDCR_HPMD; 1004 } 1005 if (secure) { 1006 prohibited = prohibited || !(env->cp15.mdcr_el3 & MDCR_SPME); 1007 } 1008 1009 if (counter == 31) { 1010 /* 1011 * The cycle counter defaults to running. PMCR.DP says "disable 1012 * the cycle counter when event counting is prohibited". 1013 * Some MDCR bits disable the cycle counter specifically. 1014 */ 1015 prohibited = prohibited && env->cp15.c9_pmcr & PMCRDP; 1016 if (cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) { 1017 if (secure) { 1018 prohibited = prohibited || (env->cp15.mdcr_el3 & MDCR_SCCD); 1019 } 1020 if (el == 2) { 1021 prohibited = prohibited || (mdcr_el2 & MDCR_HCCD); 1022 } 1023 } 1024 } 1025 1026 if (counter == 31) { 1027 filter = env->cp15.pmccfiltr_el0; 1028 } else { 1029 filter = env->cp15.c14_pmevtyper[counter]; 1030 } 1031 1032 p = filter & PMXEVTYPER_P; 1033 u = filter & PMXEVTYPER_U; 1034 nsk = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSK); 1035 nsu = arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_NSU); 1036 nsh = arm_feature(env, ARM_FEATURE_EL2) && (filter & PMXEVTYPER_NSH); 1037 m = arm_el_is_aa64(env, 1) && 1038 arm_feature(env, ARM_FEATURE_EL3) && (filter & PMXEVTYPER_M); 1039 1040 if (el == 0) { 1041 filtered = secure ? u : u != nsu; 1042 } else if (el == 1) { 1043 filtered = secure ? p : p != nsk; 1044 } else if (el == 2) { 1045 filtered = !nsh; 1046 } else { /* EL3 */ 1047 filtered = m != p; 1048 } 1049 1050 if (counter != 31) { 1051 /* 1052 * If not checking PMCCNTR, ensure the counter is setup to an event we 1053 * support 1054 */ 1055 uint16_t event = filter & PMXEVTYPER_EVTCOUNT; 1056 if (!event_supported(event)) { 1057 return false; 1058 } 1059 } 1060 1061 return enabled && !prohibited && !filtered; 1062 } 1063 1064 static void pmu_update_irq(CPUARMState *env) 1065 { 1066 ARMCPU *cpu = env_archcpu(env); 1067 qemu_set_irq(cpu->pmu_interrupt, (env->cp15.c9_pmcr & PMCRE) && 1068 (env->cp15.c9_pminten & env->cp15.c9_pmovsr)); 1069 } 1070 1071 static bool pmccntr_clockdiv_enabled(CPUARMState *env) 1072 { 1073 /* 1074 * Return true if the clock divider is enabled and the cycle counter 1075 * is supposed to tick only once every 64 clock cycles. This is 1076 * controlled by PMCR.D, but if PMCR.LC is set to enable the long 1077 * (64-bit) cycle counter PMCR.D has no effect. 1078 */ 1079 return (env->cp15.c9_pmcr & (PMCRD | PMCRLC)) == PMCRD; 1080 } 1081 1082 static bool pmevcntr_is_64_bit(CPUARMState *env, int counter) 1083 { 1084 /* Return true if the specified event counter is configured to be 64 bit */ 1085 1086 /* This isn't intended to be used with the cycle counter */ 1087 assert(counter < 31); 1088 1089 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) { 1090 return false; 1091 } 1092 1093 if (arm_feature(env, ARM_FEATURE_EL2)) { 1094 /* 1095 * MDCR_EL2.HLP still applies even when EL2 is disabled in the 1096 * current security state, so we don't use arm_mdcr_el2_eff() here. 1097 */ 1098 bool hlp = env->cp15.mdcr_el2 & MDCR_HLP; 1099 int hpmn = env->cp15.mdcr_el2 & MDCR_HPMN; 1100 1101 if (counter >= hpmn) { 1102 return hlp; 1103 } 1104 } 1105 return env->cp15.c9_pmcr & PMCRLP; 1106 } 1107 1108 /* 1109 * Ensure c15_ccnt is the guest-visible count so that operations such as 1110 * enabling/disabling the counter or filtering, modifying the count itself, 1111 * etc. can be done logically. This is essentially a no-op if the counter is 1112 * not enabled at the time of the call. 1113 */ 1114 static void pmccntr_op_start(CPUARMState *env) 1115 { 1116 uint64_t cycles = cycles_get_count(env); 1117 1118 if (pmu_counter_enabled(env, 31)) { 1119 uint64_t eff_cycles = cycles; 1120 if (pmccntr_clockdiv_enabled(env)) { 1121 eff_cycles /= 64; 1122 } 1123 1124 uint64_t new_pmccntr = eff_cycles - env->cp15.c15_ccnt_delta; 1125 1126 uint64_t overflow_mask = env->cp15.c9_pmcr & PMCRLC ? \ 1127 1ull << 63 : 1ull << 31; 1128 if (env->cp15.c15_ccnt & ~new_pmccntr & overflow_mask) { 1129 env->cp15.c9_pmovsr |= (1ULL << 31); 1130 pmu_update_irq(env); 1131 } 1132 1133 env->cp15.c15_ccnt = new_pmccntr; 1134 } 1135 env->cp15.c15_ccnt_delta = cycles; 1136 } 1137 1138 /* 1139 * If PMCCNTR is enabled, recalculate the delta between the clock and the 1140 * guest-visible count. A call to pmccntr_op_finish should follow every call to 1141 * pmccntr_op_start. 1142 */ 1143 static void pmccntr_op_finish(CPUARMState *env) 1144 { 1145 if (pmu_counter_enabled(env, 31)) { 1146 #ifndef CONFIG_USER_ONLY 1147 /* Calculate when the counter will next overflow */ 1148 uint64_t remaining_cycles = -env->cp15.c15_ccnt; 1149 if (!(env->cp15.c9_pmcr & PMCRLC)) { 1150 remaining_cycles = (uint32_t)remaining_cycles; 1151 } 1152 int64_t overflow_in = cycles_ns_per(remaining_cycles); 1153 1154 if (overflow_in > 0) { 1155 int64_t overflow_at; 1156 1157 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1158 overflow_in, &overflow_at)) { 1159 ARMCPU *cpu = env_archcpu(env); 1160 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1161 } 1162 } 1163 #endif 1164 1165 uint64_t prev_cycles = env->cp15.c15_ccnt_delta; 1166 if (pmccntr_clockdiv_enabled(env)) { 1167 prev_cycles /= 64; 1168 } 1169 env->cp15.c15_ccnt_delta = prev_cycles - env->cp15.c15_ccnt; 1170 } 1171 } 1172 1173 static void pmevcntr_op_start(CPUARMState *env, uint8_t counter) 1174 { 1175 1176 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1177 uint64_t count = 0; 1178 if (event_supported(event)) { 1179 uint16_t event_idx = supported_event_map[event]; 1180 count = pm_events[event_idx].get_count(env); 1181 } 1182 1183 if (pmu_counter_enabled(env, counter)) { 1184 uint64_t new_pmevcntr = count - env->cp15.c14_pmevcntr_delta[counter]; 1185 uint64_t overflow_mask = pmevcntr_is_64_bit(env, counter) ? 1186 1ULL << 63 : 1ULL << 31; 1187 1188 if (env->cp15.c14_pmevcntr[counter] & ~new_pmevcntr & overflow_mask) { 1189 env->cp15.c9_pmovsr |= (1 << counter); 1190 pmu_update_irq(env); 1191 } 1192 env->cp15.c14_pmevcntr[counter] = new_pmevcntr; 1193 } 1194 env->cp15.c14_pmevcntr_delta[counter] = count; 1195 } 1196 1197 static void pmevcntr_op_finish(CPUARMState *env, uint8_t counter) 1198 { 1199 if (pmu_counter_enabled(env, counter)) { 1200 #ifndef CONFIG_USER_ONLY 1201 uint16_t event = env->cp15.c14_pmevtyper[counter] & PMXEVTYPER_EVTCOUNT; 1202 uint16_t event_idx = supported_event_map[event]; 1203 uint64_t delta = -(env->cp15.c14_pmevcntr[counter] + 1); 1204 int64_t overflow_in; 1205 1206 if (!pmevcntr_is_64_bit(env, counter)) { 1207 delta = (uint32_t)delta; 1208 } 1209 overflow_in = pm_events[event_idx].ns_per_count(delta); 1210 1211 if (overflow_in > 0) { 1212 int64_t overflow_at; 1213 1214 if (!sadd64_overflow(qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL), 1215 overflow_in, &overflow_at)) { 1216 ARMCPU *cpu = env_archcpu(env); 1217 timer_mod_anticipate_ns(cpu->pmu_timer, overflow_at); 1218 } 1219 } 1220 #endif 1221 1222 env->cp15.c14_pmevcntr_delta[counter] -= 1223 env->cp15.c14_pmevcntr[counter]; 1224 } 1225 } 1226 1227 void pmu_op_start(CPUARMState *env) 1228 { 1229 unsigned int i; 1230 pmccntr_op_start(env); 1231 for (i = 0; i < pmu_num_counters(env); i++) { 1232 pmevcntr_op_start(env, i); 1233 } 1234 } 1235 1236 void pmu_op_finish(CPUARMState *env) 1237 { 1238 unsigned int i; 1239 pmccntr_op_finish(env); 1240 for (i = 0; i < pmu_num_counters(env); i++) { 1241 pmevcntr_op_finish(env, i); 1242 } 1243 } 1244 1245 void pmu_pre_el_change(ARMCPU *cpu, void *ignored) 1246 { 1247 pmu_op_start(&cpu->env); 1248 } 1249 1250 void pmu_post_el_change(ARMCPU *cpu, void *ignored) 1251 { 1252 pmu_op_finish(&cpu->env); 1253 } 1254 1255 void arm_pmu_timer_cb(void *opaque) 1256 { 1257 ARMCPU *cpu = opaque; 1258 1259 /* 1260 * Update all the counter values based on the current underlying counts, 1261 * triggering interrupts to be raised, if necessary. pmu_op_finish() also 1262 * has the effect of setting the cpu->pmu_timer to the next earliest time a 1263 * counter may expire. 1264 */ 1265 pmu_op_start(&cpu->env); 1266 pmu_op_finish(&cpu->env); 1267 } 1268 1269 static void pmcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1270 uint64_t value) 1271 { 1272 pmu_op_start(env); 1273 1274 if (value & PMCRC) { 1275 /* The counter has been reset */ 1276 env->cp15.c15_ccnt = 0; 1277 } 1278 1279 if (value & PMCRP) { 1280 unsigned int i; 1281 for (i = 0; i < pmu_num_counters(env); i++) { 1282 env->cp15.c14_pmevcntr[i] = 0; 1283 } 1284 } 1285 1286 env->cp15.c9_pmcr &= ~PMCR_WRITABLE_MASK; 1287 env->cp15.c9_pmcr |= (value & PMCR_WRITABLE_MASK); 1288 1289 pmu_op_finish(env); 1290 } 1291 1292 static uint64_t pmcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1293 { 1294 uint64_t pmcr = env->cp15.c9_pmcr; 1295 1296 /* 1297 * If EL2 is implemented and enabled for the current security state, reads 1298 * of PMCR.N from EL1 or EL0 return the value of MDCR_EL2.HPMN or HDCR.HPMN. 1299 */ 1300 if (arm_current_el(env) <= 1 && arm_is_el2_enabled(env)) { 1301 pmcr &= ~PMCRN_MASK; 1302 pmcr |= (env->cp15.mdcr_el2 & MDCR_HPMN) << PMCRN_SHIFT; 1303 } 1304 1305 return pmcr; 1306 } 1307 1308 static void pmswinc_write(CPUARMState *env, const ARMCPRegInfo *ri, 1309 uint64_t value) 1310 { 1311 unsigned int i; 1312 uint64_t overflow_mask, new_pmswinc; 1313 1314 for (i = 0; i < pmu_num_counters(env); i++) { 1315 /* Increment a counter's count iff: */ 1316 if ((value & (1 << i)) && /* counter's bit is set */ 1317 /* counter is enabled and not filtered */ 1318 pmu_counter_enabled(env, i) && 1319 /* counter is SW_INCR */ 1320 (env->cp15.c14_pmevtyper[i] & PMXEVTYPER_EVTCOUNT) == 0x0) { 1321 pmevcntr_op_start(env, i); 1322 1323 /* 1324 * Detect if this write causes an overflow since we can't predict 1325 * PMSWINC overflows like we can for other events 1326 */ 1327 new_pmswinc = env->cp15.c14_pmevcntr[i] + 1; 1328 1329 overflow_mask = pmevcntr_is_64_bit(env, i) ? 1330 1ULL << 63 : 1ULL << 31; 1331 1332 if (env->cp15.c14_pmevcntr[i] & ~new_pmswinc & overflow_mask) { 1333 env->cp15.c9_pmovsr |= (1 << i); 1334 pmu_update_irq(env); 1335 } 1336 1337 env->cp15.c14_pmevcntr[i] = new_pmswinc; 1338 1339 pmevcntr_op_finish(env, i); 1340 } 1341 } 1342 } 1343 1344 static uint64_t pmccntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1345 { 1346 uint64_t ret; 1347 pmccntr_op_start(env); 1348 ret = env->cp15.c15_ccnt; 1349 pmccntr_op_finish(env); 1350 return ret; 1351 } 1352 1353 static void pmselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1354 uint64_t value) 1355 { 1356 /* 1357 * The value of PMSELR.SEL affects the behavior of PMXEVTYPER and 1358 * PMXEVCNTR. We allow [0..31] to be written to PMSELR here; in the 1359 * meanwhile, we check PMSELR.SEL when PMXEVTYPER and PMXEVCNTR are 1360 * accessed. 1361 */ 1362 env->cp15.c9_pmselr = value & 0x1f; 1363 } 1364 1365 static void pmccntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1366 uint64_t value) 1367 { 1368 pmccntr_op_start(env); 1369 env->cp15.c15_ccnt = value; 1370 pmccntr_op_finish(env); 1371 } 1372 1373 static void pmccntr_write32(CPUARMState *env, const ARMCPRegInfo *ri, 1374 uint64_t value) 1375 { 1376 uint64_t cur_val = pmccntr_read(env, NULL); 1377 1378 pmccntr_write(env, ri, deposit64(cur_val, 0, 32, value)); 1379 } 1380 1381 static void pmccfiltr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1382 uint64_t value) 1383 { 1384 pmccntr_op_start(env); 1385 env->cp15.pmccfiltr_el0 = value & PMCCFILTR_EL0; 1386 pmccntr_op_finish(env); 1387 } 1388 1389 static void pmccfiltr_write_a32(CPUARMState *env, const ARMCPRegInfo *ri, 1390 uint64_t value) 1391 { 1392 pmccntr_op_start(env); 1393 /* M is not accessible from AArch32 */ 1394 env->cp15.pmccfiltr_el0 = (env->cp15.pmccfiltr_el0 & PMCCFILTR_M) | 1395 (value & PMCCFILTR); 1396 pmccntr_op_finish(env); 1397 } 1398 1399 static uint64_t pmccfiltr_read_a32(CPUARMState *env, const ARMCPRegInfo *ri) 1400 { 1401 /* M is not visible in AArch32 */ 1402 return env->cp15.pmccfiltr_el0 & PMCCFILTR; 1403 } 1404 1405 static void pmcntenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1406 uint64_t value) 1407 { 1408 pmu_op_start(env); 1409 value &= pmu_counter_mask(env); 1410 env->cp15.c9_pmcnten |= value; 1411 pmu_op_finish(env); 1412 } 1413 1414 static void pmcntenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1415 uint64_t value) 1416 { 1417 pmu_op_start(env); 1418 value &= pmu_counter_mask(env); 1419 env->cp15.c9_pmcnten &= ~value; 1420 pmu_op_finish(env); 1421 } 1422 1423 static void pmovsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1424 uint64_t value) 1425 { 1426 value &= pmu_counter_mask(env); 1427 env->cp15.c9_pmovsr &= ~value; 1428 pmu_update_irq(env); 1429 } 1430 1431 static void pmovsset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1432 uint64_t value) 1433 { 1434 value &= pmu_counter_mask(env); 1435 env->cp15.c9_pmovsr |= value; 1436 pmu_update_irq(env); 1437 } 1438 1439 static void pmevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1440 uint64_t value, const uint8_t counter) 1441 { 1442 if (counter == 31) { 1443 pmccfiltr_write(env, ri, value); 1444 } else if (counter < pmu_num_counters(env)) { 1445 pmevcntr_op_start(env, counter); 1446 1447 /* 1448 * If this counter's event type is changing, store the current 1449 * underlying count for the new type in c14_pmevcntr_delta[counter] so 1450 * pmevcntr_op_finish has the correct baseline when it converts back to 1451 * a delta. 1452 */ 1453 uint16_t old_event = env->cp15.c14_pmevtyper[counter] & 1454 PMXEVTYPER_EVTCOUNT; 1455 uint16_t new_event = value & PMXEVTYPER_EVTCOUNT; 1456 if (old_event != new_event) { 1457 uint64_t count = 0; 1458 if (event_supported(new_event)) { 1459 uint16_t event_idx = supported_event_map[new_event]; 1460 count = pm_events[event_idx].get_count(env); 1461 } 1462 env->cp15.c14_pmevcntr_delta[counter] = count; 1463 } 1464 1465 env->cp15.c14_pmevtyper[counter] = value & PMXEVTYPER_MASK; 1466 pmevcntr_op_finish(env, counter); 1467 } 1468 /* 1469 * Attempts to access PMXEVTYPER are CONSTRAINED UNPREDICTABLE when 1470 * PMSELR value is equal to or greater than the number of implemented 1471 * counters, but not equal to 0x1f. We opt to behave as a RAZ/WI. 1472 */ 1473 } 1474 1475 static uint64_t pmevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri, 1476 const uint8_t counter) 1477 { 1478 if (counter == 31) { 1479 return env->cp15.pmccfiltr_el0; 1480 } else if (counter < pmu_num_counters(env)) { 1481 return env->cp15.c14_pmevtyper[counter]; 1482 } else { 1483 /* 1484 * We opt to behave as a RAZ/WI when attempts to access PMXEVTYPER 1485 * are CONSTRAINED UNPREDICTABLE. See comments in pmevtyper_write(). 1486 */ 1487 return 0; 1488 } 1489 } 1490 1491 static void pmevtyper_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1492 uint64_t value) 1493 { 1494 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1495 pmevtyper_write(env, ri, value, counter); 1496 } 1497 1498 static void pmevtyper_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1499 uint64_t value) 1500 { 1501 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1502 env->cp15.c14_pmevtyper[counter] = value; 1503 1504 /* 1505 * pmevtyper_rawwrite is called between a pair of pmu_op_start and 1506 * pmu_op_finish calls when loading saved state for a migration. Because 1507 * we're potentially updating the type of event here, the value written to 1508 * c14_pmevcntr_delta by the preceding pmu_op_start call may be for a 1509 * different counter type. Therefore, we need to set this value to the 1510 * current count for the counter type we're writing so that pmu_op_finish 1511 * has the correct count for its calculation. 1512 */ 1513 uint16_t event = value & PMXEVTYPER_EVTCOUNT; 1514 if (event_supported(event)) { 1515 uint16_t event_idx = supported_event_map[event]; 1516 env->cp15.c14_pmevcntr_delta[counter] = 1517 pm_events[event_idx].get_count(env); 1518 } 1519 } 1520 1521 static uint64_t pmevtyper_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1522 { 1523 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1524 return pmevtyper_read(env, ri, counter); 1525 } 1526 1527 static void pmxevtyper_write(CPUARMState *env, const ARMCPRegInfo *ri, 1528 uint64_t value) 1529 { 1530 pmevtyper_write(env, ri, value, env->cp15.c9_pmselr & 31); 1531 } 1532 1533 static uint64_t pmxevtyper_read(CPUARMState *env, const ARMCPRegInfo *ri) 1534 { 1535 return pmevtyper_read(env, ri, env->cp15.c9_pmselr & 31); 1536 } 1537 1538 static void pmevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1539 uint64_t value, uint8_t counter) 1540 { 1541 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) { 1542 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */ 1543 value &= MAKE_64BIT_MASK(0, 32); 1544 } 1545 if (counter < pmu_num_counters(env)) { 1546 pmevcntr_op_start(env, counter); 1547 env->cp15.c14_pmevcntr[counter] = value; 1548 pmevcntr_op_finish(env, counter); 1549 } 1550 /* 1551 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1552 * are CONSTRAINED UNPREDICTABLE. 1553 */ 1554 } 1555 1556 static uint64_t pmevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri, 1557 uint8_t counter) 1558 { 1559 if (counter < pmu_num_counters(env)) { 1560 uint64_t ret; 1561 pmevcntr_op_start(env, counter); 1562 ret = env->cp15.c14_pmevcntr[counter]; 1563 pmevcntr_op_finish(env, counter); 1564 if (!cpu_isar_feature(any_pmuv3p5, env_archcpu(env))) { 1565 /* Before FEAT_PMUv3p5, top 32 bits of event counters are RES0 */ 1566 ret &= MAKE_64BIT_MASK(0, 32); 1567 } 1568 return ret; 1569 } else { 1570 /* 1571 * We opt to behave as a RAZ/WI when attempts to access PM[X]EVCNTR 1572 * are CONSTRAINED UNPREDICTABLE. 1573 */ 1574 return 0; 1575 } 1576 } 1577 1578 static void pmevcntr_writefn(CPUARMState *env, const ARMCPRegInfo *ri, 1579 uint64_t value) 1580 { 1581 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1582 pmevcntr_write(env, ri, value, counter); 1583 } 1584 1585 static uint64_t pmevcntr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 1586 { 1587 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1588 return pmevcntr_read(env, ri, counter); 1589 } 1590 1591 static void pmevcntr_rawwrite(CPUARMState *env, const ARMCPRegInfo *ri, 1592 uint64_t value) 1593 { 1594 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1595 assert(counter < pmu_num_counters(env)); 1596 env->cp15.c14_pmevcntr[counter] = value; 1597 pmevcntr_write(env, ri, value, counter); 1598 } 1599 1600 static uint64_t pmevcntr_rawread(CPUARMState *env, const ARMCPRegInfo *ri) 1601 { 1602 uint8_t counter = ((ri->crm & 3) << 3) | (ri->opc2 & 7); 1603 assert(counter < pmu_num_counters(env)); 1604 return env->cp15.c14_pmevcntr[counter]; 1605 } 1606 1607 static void pmxevcntr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1608 uint64_t value) 1609 { 1610 pmevcntr_write(env, ri, value, env->cp15.c9_pmselr & 31); 1611 } 1612 1613 static uint64_t pmxevcntr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1614 { 1615 return pmevcntr_read(env, ri, env->cp15.c9_pmselr & 31); 1616 } 1617 1618 static void pmuserenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1619 uint64_t value) 1620 { 1621 if (arm_feature(env, ARM_FEATURE_V8)) { 1622 env->cp15.c9_pmuserenr = value & 0xf; 1623 } else { 1624 env->cp15.c9_pmuserenr = value & 1; 1625 } 1626 } 1627 1628 static void pmintenset_write(CPUARMState *env, const ARMCPRegInfo *ri, 1629 uint64_t value) 1630 { 1631 /* We have no event counters so only the C bit can be changed */ 1632 value &= pmu_counter_mask(env); 1633 env->cp15.c9_pminten |= value; 1634 pmu_update_irq(env); 1635 } 1636 1637 static void pmintenclr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1638 uint64_t value) 1639 { 1640 value &= pmu_counter_mask(env); 1641 env->cp15.c9_pminten &= ~value; 1642 pmu_update_irq(env); 1643 } 1644 1645 static void vbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 1646 uint64_t value) 1647 { 1648 /* 1649 * Note that even though the AArch64 view of this register has bits 1650 * [10:0] all RES0 we can only mask the bottom 5, to comply with the 1651 * architectural requirements for bits which are RES0 only in some 1652 * contexts. (ARMv8 would permit us to do no masking at all, but ARMv7 1653 * requires the bottom five bits to be RAZ/WI because they're UNK/SBZP.) 1654 */ 1655 raw_write(env, ri, value & ~0x1FULL); 1656 } 1657 1658 static void scr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 1659 { 1660 /* Begin with base v8.0 state. */ 1661 uint64_t valid_mask = 0x3fff; 1662 ARMCPU *cpu = env_archcpu(env); 1663 uint64_t changed; 1664 1665 /* 1666 * Because SCR_EL3 is the "real" cpreg and SCR is the alias, reset always 1667 * passes the reginfo for SCR_EL3, which has type ARM_CP_STATE_AA64. 1668 * Instead, choose the format based on the mode of EL3. 1669 */ 1670 if (arm_el_is_aa64(env, 3)) { 1671 value |= SCR_FW | SCR_AW; /* RES1 */ 1672 valid_mask &= ~SCR_NET; /* RES0 */ 1673 1674 if (!cpu_isar_feature(aa64_aa32_el1, cpu) && 1675 !cpu_isar_feature(aa64_aa32_el2, cpu)) { 1676 value |= SCR_RW; /* RAO/WI */ 1677 } 1678 if (cpu_isar_feature(aa64_ras, cpu)) { 1679 valid_mask |= SCR_TERR; 1680 } 1681 if (cpu_isar_feature(aa64_lor, cpu)) { 1682 valid_mask |= SCR_TLOR; 1683 } 1684 if (cpu_isar_feature(aa64_pauth, cpu)) { 1685 valid_mask |= SCR_API | SCR_APK; 1686 } 1687 if (cpu_isar_feature(aa64_sel2, cpu)) { 1688 valid_mask |= SCR_EEL2; 1689 } else if (cpu_isar_feature(aa64_rme, cpu)) { 1690 /* With RME and without SEL2, NS is RES1 (R_GSWWH, I_DJJQJ). */ 1691 value |= SCR_NS; 1692 } 1693 if (cpu_isar_feature(aa64_mte, cpu)) { 1694 valid_mask |= SCR_ATA; 1695 } 1696 if (cpu_isar_feature(aa64_scxtnum, cpu)) { 1697 valid_mask |= SCR_ENSCXT; 1698 } 1699 if (cpu_isar_feature(aa64_doublefault, cpu)) { 1700 valid_mask |= SCR_EASE | SCR_NMEA; 1701 } 1702 if (cpu_isar_feature(aa64_sme, cpu)) { 1703 valid_mask |= SCR_ENTP2; 1704 } 1705 if (cpu_isar_feature(aa64_hcx, cpu)) { 1706 valid_mask |= SCR_HXEN; 1707 } 1708 if (cpu_isar_feature(aa64_fgt, cpu)) { 1709 valid_mask |= SCR_FGTEN; 1710 } 1711 if (cpu_isar_feature(aa64_rme, cpu)) { 1712 valid_mask |= SCR_NSE | SCR_GPF; 1713 } 1714 if (cpu_isar_feature(aa64_ecv, cpu)) { 1715 valid_mask |= SCR_ECVEN; 1716 } 1717 } else { 1718 valid_mask &= ~(SCR_RW | SCR_ST); 1719 if (cpu_isar_feature(aa32_ras, cpu)) { 1720 valid_mask |= SCR_TERR; 1721 } 1722 } 1723 1724 if (!arm_feature(env, ARM_FEATURE_EL2)) { 1725 valid_mask &= ~SCR_HCE; 1726 1727 /* 1728 * On ARMv7, SMD (or SCD as it is called in v7) is only 1729 * supported if EL2 exists. The bit is UNK/SBZP when 1730 * EL2 is unavailable. In QEMU ARMv7, we force it to always zero 1731 * when EL2 is unavailable. 1732 * On ARMv8, this bit is always available. 1733 */ 1734 if (arm_feature(env, ARM_FEATURE_V7) && 1735 !arm_feature(env, ARM_FEATURE_V8)) { 1736 valid_mask &= ~SCR_SMD; 1737 } 1738 } 1739 1740 /* Clear all-context RES0 bits. */ 1741 value &= valid_mask; 1742 changed = env->cp15.scr_el3 ^ value; 1743 env->cp15.scr_el3 = value; 1744 1745 /* 1746 * If SCR_EL3.{NS,NSE} changes, i.e. change of security state, 1747 * we must invalidate all TLBs below EL3. 1748 */ 1749 if (changed & (SCR_NS | SCR_NSE)) { 1750 tlb_flush_by_mmuidx(env_cpu(env), (ARMMMUIdxBit_E10_0 | 1751 ARMMMUIdxBit_E20_0 | 1752 ARMMMUIdxBit_E10_1 | 1753 ARMMMUIdxBit_E20_2 | 1754 ARMMMUIdxBit_E10_1_PAN | 1755 ARMMMUIdxBit_E20_2_PAN | 1756 ARMMMUIdxBit_E2)); 1757 } 1758 } 1759 1760 static void scr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 1761 { 1762 /* 1763 * scr_write will set the RES1 bits on an AArch64-only CPU. 1764 * The reset value will be 0x30 on an AArch64-only CPU and 0 otherwise. 1765 */ 1766 scr_write(env, ri, 0); 1767 } 1768 1769 static CPAccessResult access_tid4(CPUARMState *env, 1770 const ARMCPRegInfo *ri, 1771 bool isread) 1772 { 1773 if (arm_current_el(env) == 1 && 1774 (arm_hcr_el2_eff(env) & (HCR_TID2 | HCR_TID4))) { 1775 return CP_ACCESS_TRAP_EL2; 1776 } 1777 1778 return CP_ACCESS_OK; 1779 } 1780 1781 static uint64_t ccsidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1782 { 1783 ARMCPU *cpu = env_archcpu(env); 1784 1785 /* 1786 * Acquire the CSSELR index from the bank corresponding to the CCSIDR 1787 * bank 1788 */ 1789 uint32_t index = A32_BANKED_REG_GET(env, csselr, 1790 ri->secure & ARM_CP_SECSTATE_S); 1791 1792 return cpu->ccsidr[index]; 1793 } 1794 1795 static void csselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 1796 uint64_t value) 1797 { 1798 raw_write(env, ri, value & 0xf); 1799 } 1800 1801 static uint64_t isr_read(CPUARMState *env, const ARMCPRegInfo *ri) 1802 { 1803 CPUState *cs = env_cpu(env); 1804 bool el1 = arm_current_el(env) == 1; 1805 uint64_t hcr_el2 = el1 ? arm_hcr_el2_eff(env) : 0; 1806 uint64_t ret = 0; 1807 1808 if (hcr_el2 & HCR_IMO) { 1809 if (cs->interrupt_request & CPU_INTERRUPT_VIRQ) { 1810 ret |= CPSR_I; 1811 } 1812 if (cs->interrupt_request & CPU_INTERRUPT_VINMI) { 1813 ret |= ISR_IS; 1814 ret |= CPSR_I; 1815 } 1816 } else { 1817 if (cs->interrupt_request & CPU_INTERRUPT_HARD) { 1818 ret |= CPSR_I; 1819 } 1820 1821 if (cs->interrupt_request & CPU_INTERRUPT_NMI) { 1822 ret |= ISR_IS; 1823 ret |= CPSR_I; 1824 } 1825 } 1826 1827 if (hcr_el2 & HCR_FMO) { 1828 if (cs->interrupt_request & CPU_INTERRUPT_VFIQ) { 1829 ret |= CPSR_F; 1830 } 1831 if (cs->interrupt_request & CPU_INTERRUPT_VFNMI) { 1832 ret |= ISR_FS; 1833 ret |= CPSR_F; 1834 } 1835 } else { 1836 if (cs->interrupt_request & CPU_INTERRUPT_FIQ) { 1837 ret |= CPSR_F; 1838 } 1839 } 1840 1841 if (hcr_el2 & HCR_AMO) { 1842 if (cs->interrupt_request & CPU_INTERRUPT_VSERR) { 1843 ret |= CPSR_A; 1844 } 1845 } 1846 1847 return ret; 1848 } 1849 1850 static CPAccessResult access_aa64_tid1(CPUARMState *env, const ARMCPRegInfo *ri, 1851 bool isread) 1852 { 1853 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID1)) { 1854 return CP_ACCESS_TRAP_EL2; 1855 } 1856 1857 return CP_ACCESS_OK; 1858 } 1859 1860 static CPAccessResult access_aa32_tid1(CPUARMState *env, const ARMCPRegInfo *ri, 1861 bool isread) 1862 { 1863 if (arm_feature(env, ARM_FEATURE_V8)) { 1864 return access_aa64_tid1(env, ri, isread); 1865 } 1866 1867 return CP_ACCESS_OK; 1868 } 1869 1870 static const ARMCPRegInfo v7_cp_reginfo[] = { 1871 /* the old v6 WFI, UNPREDICTABLE in v7 but we choose to NOP */ 1872 { .name = "NOP", .cp = 15, .crn = 7, .crm = 0, .opc1 = 0, .opc2 = 4, 1873 .access = PL1_W, .type = ARM_CP_NOP }, 1874 /* 1875 * Performance monitors are implementation defined in v7, 1876 * but with an ARM recommended set of registers, which we 1877 * follow. 1878 * 1879 * Performance registers fall into three categories: 1880 * (a) always UNDEF in PL0, RW in PL1 (PMINTENSET, PMINTENCLR) 1881 * (b) RO in PL0 (ie UNDEF on write), RW in PL1 (PMUSERENR) 1882 * (c) UNDEF in PL0 if PMUSERENR.EN==0, otherwise accessible (all others) 1883 * For the cases controlled by PMUSERENR we must set .access to PL0_RW 1884 * or PL0_RO as appropriate and then check PMUSERENR in the helper fn. 1885 */ 1886 { .name = "PMCNTENSET", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 1, 1887 .access = PL0_RW, .type = ARM_CP_ALIAS | ARM_CP_IO, 1888 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1889 .writefn = pmcntenset_write, 1890 .accessfn = pmreg_access, 1891 .fgt = FGT_PMCNTEN, 1892 .raw_writefn = raw_write }, 1893 { .name = "PMCNTENSET_EL0", .state = ARM_CP_STATE_AA64, .type = ARM_CP_IO, 1894 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 1, 1895 .access = PL0_RW, .accessfn = pmreg_access, 1896 .fgt = FGT_PMCNTEN, 1897 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), .resetvalue = 0, 1898 .writefn = pmcntenset_write, .raw_writefn = raw_write }, 1899 { .name = "PMCNTENCLR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 2, 1900 .access = PL0_RW, 1901 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcnten), 1902 .accessfn = pmreg_access, 1903 .fgt = FGT_PMCNTEN, 1904 .writefn = pmcntenclr_write, 1905 .type = ARM_CP_ALIAS | ARM_CP_IO }, 1906 { .name = "PMCNTENCLR_EL0", .state = ARM_CP_STATE_AA64, 1907 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 2, 1908 .access = PL0_RW, .accessfn = pmreg_access, 1909 .fgt = FGT_PMCNTEN, 1910 .type = ARM_CP_ALIAS | ARM_CP_IO, 1911 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcnten), 1912 .writefn = pmcntenclr_write }, 1913 { .name = "PMOVSR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 3, 1914 .access = PL0_RW, .type = ARM_CP_IO, 1915 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 1916 .accessfn = pmreg_access, 1917 .fgt = FGT_PMOVS, 1918 .writefn = pmovsr_write, 1919 .raw_writefn = raw_write }, 1920 { .name = "PMOVSCLR_EL0", .state = ARM_CP_STATE_AA64, 1921 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 3, 1922 .access = PL0_RW, .accessfn = pmreg_access, 1923 .fgt = FGT_PMOVS, 1924 .type = ARM_CP_ALIAS | ARM_CP_IO, 1925 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 1926 .writefn = pmovsr_write, 1927 .raw_writefn = raw_write }, 1928 { .name = "PMSWINC", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 4, 1929 .access = PL0_W, .accessfn = pmreg_access_swinc, 1930 .fgt = FGT_PMSWINC_EL0, 1931 .type = ARM_CP_NO_RAW | ARM_CP_IO, 1932 .writefn = pmswinc_write }, 1933 { .name = "PMSWINC_EL0", .state = ARM_CP_STATE_AA64, 1934 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 4, 1935 .access = PL0_W, .accessfn = pmreg_access_swinc, 1936 .fgt = FGT_PMSWINC_EL0, 1937 .type = ARM_CP_NO_RAW | ARM_CP_IO, 1938 .writefn = pmswinc_write }, 1939 { .name = "PMSELR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 5, 1940 .access = PL0_RW, .type = ARM_CP_ALIAS, 1941 .fgt = FGT_PMSELR_EL0, 1942 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmselr), 1943 .accessfn = pmreg_access_selr, .writefn = pmselr_write, 1944 .raw_writefn = raw_write}, 1945 { .name = "PMSELR_EL0", .state = ARM_CP_STATE_AA64, 1946 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 5, 1947 .access = PL0_RW, .accessfn = pmreg_access_selr, 1948 .fgt = FGT_PMSELR_EL0, 1949 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmselr), 1950 .writefn = pmselr_write, .raw_writefn = raw_write, }, 1951 { .name = "PMCCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 0, 1952 .access = PL0_RW, .resetvalue = 0, .type = ARM_CP_ALIAS | ARM_CP_IO, 1953 .fgt = FGT_PMCCNTR_EL0, 1954 .readfn = pmccntr_read, .writefn = pmccntr_write32, 1955 .accessfn = pmreg_access_ccntr }, 1956 { .name = "PMCCNTR_EL0", .state = ARM_CP_STATE_AA64, 1957 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 0, 1958 .access = PL0_RW, .accessfn = pmreg_access_ccntr, 1959 .fgt = FGT_PMCCNTR_EL0, 1960 .type = ARM_CP_IO, 1961 .fieldoffset = offsetof(CPUARMState, cp15.c15_ccnt), 1962 .readfn = pmccntr_read, .writefn = pmccntr_write, 1963 .raw_readfn = raw_read, .raw_writefn = raw_write, }, 1964 { .name = "PMCCFILTR", .cp = 15, .opc1 = 0, .crn = 14, .crm = 15, .opc2 = 7, 1965 .writefn = pmccfiltr_write_a32, .readfn = pmccfiltr_read_a32, 1966 .access = PL0_RW, .accessfn = pmreg_access, 1967 .fgt = FGT_PMCCFILTR_EL0, 1968 .type = ARM_CP_ALIAS | ARM_CP_IO, 1969 .resetvalue = 0, }, 1970 { .name = "PMCCFILTR_EL0", .state = ARM_CP_STATE_AA64, 1971 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 15, .opc2 = 7, 1972 .writefn = pmccfiltr_write, .raw_writefn = raw_write, 1973 .access = PL0_RW, .accessfn = pmreg_access, 1974 .fgt = FGT_PMCCFILTR_EL0, 1975 .type = ARM_CP_IO, 1976 .fieldoffset = offsetof(CPUARMState, cp15.pmccfiltr_el0), 1977 .resetvalue = 0, }, 1978 { .name = "PMXEVTYPER", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 1, 1979 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 1980 .accessfn = pmreg_access, 1981 .fgt = FGT_PMEVTYPERN_EL0, 1982 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 1983 { .name = "PMXEVTYPER_EL0", .state = ARM_CP_STATE_AA64, 1984 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 1, 1985 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 1986 .accessfn = pmreg_access, 1987 .fgt = FGT_PMEVTYPERN_EL0, 1988 .writefn = pmxevtyper_write, .readfn = pmxevtyper_read }, 1989 { .name = "PMXEVCNTR", .cp = 15, .crn = 9, .crm = 13, .opc1 = 0, .opc2 = 2, 1990 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 1991 .accessfn = pmreg_access_xevcntr, 1992 .fgt = FGT_PMEVCNTRN_EL0, 1993 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 1994 { .name = "PMXEVCNTR_EL0", .state = ARM_CP_STATE_AA64, 1995 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 13, .opc2 = 2, 1996 .access = PL0_RW, .type = ARM_CP_NO_RAW | ARM_CP_IO, 1997 .accessfn = pmreg_access_xevcntr, 1998 .fgt = FGT_PMEVCNTRN_EL0, 1999 .writefn = pmxevcntr_write, .readfn = pmxevcntr_read }, 2000 { .name = "PMUSERENR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 0, 2001 .access = PL0_R | PL1_RW, .accessfn = access_tpm, 2002 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmuserenr), 2003 .resetvalue = 0, 2004 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2005 { .name = "PMUSERENR_EL0", .state = ARM_CP_STATE_AA64, 2006 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 0, 2007 .access = PL0_R | PL1_RW, .accessfn = access_tpm, .type = ARM_CP_ALIAS, 2008 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmuserenr), 2009 .resetvalue = 0, 2010 .writefn = pmuserenr_write, .raw_writefn = raw_write }, 2011 { .name = "PMINTENSET", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 1, 2012 .access = PL1_RW, .accessfn = access_tpm, 2013 .fgt = FGT_PMINTEN, 2014 .type = ARM_CP_ALIAS | ARM_CP_IO, 2015 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pminten), 2016 .resetvalue = 0, 2017 .writefn = pmintenset_write, .raw_writefn = raw_write }, 2018 { .name = "PMINTENSET_EL1", .state = ARM_CP_STATE_AA64, 2019 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 1, 2020 .access = PL1_RW, .accessfn = access_tpm, 2021 .fgt = FGT_PMINTEN, 2022 .type = ARM_CP_IO, 2023 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2024 .writefn = pmintenset_write, .raw_writefn = raw_write, 2025 .resetvalue = 0x0 }, 2026 { .name = "PMINTENCLR", .cp = 15, .crn = 9, .crm = 14, .opc1 = 0, .opc2 = 2, 2027 .access = PL1_RW, .accessfn = access_tpm, 2028 .fgt = FGT_PMINTEN, 2029 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW, 2030 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2031 .writefn = pmintenclr_write, }, 2032 { .name = "PMINTENCLR_EL1", .state = ARM_CP_STATE_AA64, 2033 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 2, 2034 .access = PL1_RW, .accessfn = access_tpm, 2035 .fgt = FGT_PMINTEN, 2036 .type = ARM_CP_ALIAS | ARM_CP_IO | ARM_CP_NO_RAW, 2037 .fieldoffset = offsetof(CPUARMState, cp15.c9_pminten), 2038 .writefn = pmintenclr_write }, 2039 { .name = "CCSIDR", .state = ARM_CP_STATE_BOTH, 2040 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 0, 2041 .access = PL1_R, 2042 .accessfn = access_tid4, 2043 .fgt = FGT_CCSIDR_EL1, 2044 .readfn = ccsidr_read, .type = ARM_CP_NO_RAW }, 2045 { .name = "CSSELR", .state = ARM_CP_STATE_BOTH, 2046 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 2, .opc2 = 0, 2047 .access = PL1_RW, 2048 .accessfn = access_tid4, 2049 .fgt = FGT_CSSELR_EL1, 2050 .writefn = csselr_write, .resetvalue = 0, 2051 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.csselr_s), 2052 offsetof(CPUARMState, cp15.csselr_ns) } }, 2053 /* 2054 * Auxiliary ID register: this actually has an IMPDEF value but for now 2055 * just RAZ for all cores: 2056 */ 2057 { .name = "AIDR", .state = ARM_CP_STATE_BOTH, 2058 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 7, 2059 .access = PL1_R, .type = ARM_CP_CONST, 2060 .accessfn = access_aa64_tid1, 2061 .fgt = FGT_AIDR_EL1, 2062 .resetvalue = 0 }, 2063 /* 2064 * Auxiliary fault status registers: these also are IMPDEF, and we 2065 * choose to RAZ/WI for all cores. 2066 */ 2067 { .name = "AFSR0_EL1", .state = ARM_CP_STATE_BOTH, 2068 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 0, 2069 .access = PL1_RW, .accessfn = access_tvm_trvm, 2070 .fgt = FGT_AFSR0_EL1, 2071 .nv2_redirect_offset = 0x128 | NV2_REDIR_NV1, 2072 .type = ARM_CP_CONST, .resetvalue = 0 }, 2073 { .name = "AFSR1_EL1", .state = ARM_CP_STATE_BOTH, 2074 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 1, .opc2 = 1, 2075 .access = PL1_RW, .accessfn = access_tvm_trvm, 2076 .fgt = FGT_AFSR1_EL1, 2077 .nv2_redirect_offset = 0x130 | NV2_REDIR_NV1, 2078 .type = ARM_CP_CONST, .resetvalue = 0 }, 2079 /* 2080 * MAIR can just read-as-written because we don't implement caches 2081 * and so don't need to care about memory attributes. 2082 */ 2083 { .name = "MAIR_EL1", .state = ARM_CP_STATE_AA64, 2084 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 2085 .access = PL1_RW, .accessfn = access_tvm_trvm, 2086 .fgt = FGT_MAIR_EL1, 2087 .nv2_redirect_offset = 0x140 | NV2_REDIR_NV1, 2088 .fieldoffset = offsetof(CPUARMState, cp15.mair_el[1]), 2089 .resetvalue = 0 }, 2090 { .name = "MAIR_EL3", .state = ARM_CP_STATE_AA64, 2091 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 2, .opc2 = 0, 2092 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[3]), 2093 .resetvalue = 0 }, 2094 /* 2095 * For non-long-descriptor page tables these are PRRR and NMRR; 2096 * regardless they still act as reads-as-written for QEMU. 2097 */ 2098 /* 2099 * MAIR0/1 are defined separately from their 64-bit counterpart which 2100 * allows them to assign the correct fieldoffset based on the endianness 2101 * handled in the field definitions. 2102 */ 2103 { .name = "MAIR0", .state = ARM_CP_STATE_AA32, 2104 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 0, 2105 .access = PL1_RW, .accessfn = access_tvm_trvm, 2106 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair0_s), 2107 offsetof(CPUARMState, cp15.mair0_ns) }, 2108 .resetfn = arm_cp_reset_ignore }, 2109 { .name = "MAIR1", .state = ARM_CP_STATE_AA32, 2110 .cp = 15, .opc1 = 0, .crn = 10, .crm = 2, .opc2 = 1, 2111 .access = PL1_RW, .accessfn = access_tvm_trvm, 2112 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.mair1_s), 2113 offsetof(CPUARMState, cp15.mair1_ns) }, 2114 .resetfn = arm_cp_reset_ignore }, 2115 { .name = "ISR_EL1", .state = ARM_CP_STATE_BOTH, 2116 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 0, 2117 .fgt = FGT_ISR_EL1, 2118 .type = ARM_CP_NO_RAW, .access = PL1_R, .readfn = isr_read }, 2119 }; 2120 2121 static const ARMCPRegInfo pmovsset_cp_reginfo[] = { 2122 /* PMOVSSET is not implemented in v7 before v7ve */ 2123 { .name = "PMOVSSET", .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 3, 2124 .access = PL0_RW, .accessfn = pmreg_access, 2125 .fgt = FGT_PMOVS, 2126 .type = ARM_CP_ALIAS | ARM_CP_IO, 2127 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmovsr), 2128 .writefn = pmovsset_write, 2129 .raw_writefn = raw_write }, 2130 { .name = "PMOVSSET_EL0", .state = ARM_CP_STATE_AA64, 2131 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 14, .opc2 = 3, 2132 .access = PL0_RW, .accessfn = pmreg_access, 2133 .fgt = FGT_PMOVS, 2134 .type = ARM_CP_ALIAS | ARM_CP_IO, 2135 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmovsr), 2136 .writefn = pmovsset_write, 2137 .raw_writefn = raw_write }, 2138 }; 2139 2140 static void teecr_write(CPUARMState *env, const ARMCPRegInfo *ri, 2141 uint64_t value) 2142 { 2143 value &= 1; 2144 env->teecr = value; 2145 } 2146 2147 static CPAccessResult teecr_access(CPUARMState *env, const ARMCPRegInfo *ri, 2148 bool isread) 2149 { 2150 /* 2151 * HSTR.TTEE only exists in v7A, not v8A, but v8A doesn't have T2EE 2152 * at all, so we don't need to check whether we're v8A. 2153 */ 2154 if (arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) && 2155 (env->cp15.hstr_el2 & HSTR_TTEE)) { 2156 return CP_ACCESS_TRAP_EL2; 2157 } 2158 return CP_ACCESS_OK; 2159 } 2160 2161 static CPAccessResult teehbr_access(CPUARMState *env, const ARMCPRegInfo *ri, 2162 bool isread) 2163 { 2164 if (arm_current_el(env) == 0 && (env->teecr & 1)) { 2165 return CP_ACCESS_TRAP_EL1; 2166 } 2167 return teecr_access(env, ri, isread); 2168 } 2169 2170 static const ARMCPRegInfo t2ee_cp_reginfo[] = { 2171 { .name = "TEECR", .cp = 14, .crn = 0, .crm = 0, .opc1 = 6, .opc2 = 0, 2172 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, teecr), 2173 .resetvalue = 0, 2174 .writefn = teecr_write, .accessfn = teecr_access }, 2175 { .name = "TEEHBR", .cp = 14, .crn = 1, .crm = 0, .opc1 = 6, .opc2 = 0, 2176 .access = PL0_RW, .fieldoffset = offsetof(CPUARMState, teehbr), 2177 .accessfn = teehbr_access, .resetvalue = 0 }, 2178 }; 2179 2180 static const ARMCPRegInfo v6k_cp_reginfo[] = { 2181 { .name = "TPIDR_EL0", .state = ARM_CP_STATE_AA64, 2182 .opc0 = 3, .opc1 = 3, .opc2 = 2, .crn = 13, .crm = 0, 2183 .access = PL0_RW, 2184 .fgt = FGT_TPIDR_EL0, 2185 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[0]), .resetvalue = 0 }, 2186 { .name = "TPIDRURW", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 2, 2187 .access = PL0_RW, 2188 .fgt = FGT_TPIDR_EL0, 2189 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrurw_s), 2190 offsetoflow32(CPUARMState, cp15.tpidrurw_ns) }, 2191 .resetfn = arm_cp_reset_ignore }, 2192 { .name = "TPIDRRO_EL0", .state = ARM_CP_STATE_AA64, 2193 .opc0 = 3, .opc1 = 3, .opc2 = 3, .crn = 13, .crm = 0, 2194 .access = PL0_R | PL1_W, 2195 .fgt = FGT_TPIDRRO_EL0, 2196 .fieldoffset = offsetof(CPUARMState, cp15.tpidrro_el[0]), 2197 .resetvalue = 0}, 2198 { .name = "TPIDRURO", .cp = 15, .crn = 13, .crm = 0, .opc1 = 0, .opc2 = 3, 2199 .access = PL0_R | PL1_W, 2200 .fgt = FGT_TPIDRRO_EL0, 2201 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidruro_s), 2202 offsetoflow32(CPUARMState, cp15.tpidruro_ns) }, 2203 .resetfn = arm_cp_reset_ignore }, 2204 { .name = "TPIDR_EL1", .state = ARM_CP_STATE_AA64, 2205 .opc0 = 3, .opc1 = 0, .opc2 = 4, .crn = 13, .crm = 0, 2206 .access = PL1_RW, 2207 .fgt = FGT_TPIDR_EL1, 2208 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[1]), .resetvalue = 0 }, 2209 { .name = "TPIDRPRW", .opc1 = 0, .cp = 15, .crn = 13, .crm = 0, .opc2 = 4, 2210 .access = PL1_RW, 2211 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tpidrprw_s), 2212 offsetoflow32(CPUARMState, cp15.tpidrprw_ns) }, 2213 .resetvalue = 0 }, 2214 }; 2215 2216 static void arm_gt_cntfrq_reset(CPUARMState *env, const ARMCPRegInfo *opaque) 2217 { 2218 ARMCPU *cpu = env_archcpu(env); 2219 2220 cpu->env.cp15.c14_cntfrq = cpu->gt_cntfrq_hz; 2221 } 2222 2223 #ifndef CONFIG_USER_ONLY 2224 2225 static CPAccessResult gt_cntfrq_access(CPUARMState *env, const ARMCPRegInfo *ri, 2226 bool isread) 2227 { 2228 /* 2229 * CNTFRQ: not visible from PL0 if both PL0PCTEN and PL0VCTEN are zero. 2230 * Writable only at the highest implemented exception level. 2231 */ 2232 int el = arm_current_el(env); 2233 uint64_t hcr; 2234 uint32_t cntkctl; 2235 2236 switch (el) { 2237 case 0: 2238 hcr = arm_hcr_el2_eff(env); 2239 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2240 cntkctl = env->cp15.cnthctl_el2; 2241 } else { 2242 cntkctl = env->cp15.c14_cntkctl; 2243 } 2244 if (!extract32(cntkctl, 0, 2)) { 2245 return CP_ACCESS_TRAP_EL1; 2246 } 2247 break; 2248 case 1: 2249 if (!isread && ri->state == ARM_CP_STATE_AA32 && 2250 arm_is_secure_below_el3(env)) { 2251 /* Accesses from 32-bit Secure EL1 UNDEF (*not* trap to EL3!) */ 2252 return CP_ACCESS_UNDEFINED; 2253 } 2254 break; 2255 case 2: 2256 case 3: 2257 break; 2258 } 2259 2260 if (!isread && el < arm_highest_el(env)) { 2261 return CP_ACCESS_UNDEFINED; 2262 } 2263 2264 return CP_ACCESS_OK; 2265 } 2266 2267 static CPAccessResult gt_counter_access(CPUARMState *env, int timeridx, 2268 bool isread) 2269 { 2270 unsigned int cur_el = arm_current_el(env); 2271 bool has_el2 = arm_is_el2_enabled(env); 2272 uint64_t hcr = arm_hcr_el2_eff(env); 2273 2274 switch (cur_el) { 2275 case 0: 2276 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]CTEN. */ 2277 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2278 return (extract32(env->cp15.cnthctl_el2, timeridx, 1) 2279 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2); 2280 } 2281 2282 /* CNT[PV]CT: not visible from PL0 if EL0[PV]CTEN is zero */ 2283 if (!extract32(env->cp15.c14_cntkctl, timeridx, 1)) { 2284 return CP_ACCESS_TRAP_EL1; 2285 } 2286 /* fall through */ 2287 case 1: 2288 /* Check CNTHCTL_EL2.EL1PCTEN, which changes location based on E2H. */ 2289 if (has_el2 && timeridx == GTIMER_PHYS && 2290 (hcr & HCR_E2H 2291 ? !extract32(env->cp15.cnthctl_el2, 10, 1) 2292 : !extract32(env->cp15.cnthctl_el2, 0, 1))) { 2293 return CP_ACCESS_TRAP_EL2; 2294 } 2295 if (has_el2 && timeridx == GTIMER_VIRT) { 2296 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVCT)) { 2297 return CP_ACCESS_TRAP_EL2; 2298 } 2299 } 2300 break; 2301 } 2302 return CP_ACCESS_OK; 2303 } 2304 2305 static CPAccessResult gt_timer_access(CPUARMState *env, int timeridx, 2306 bool isread) 2307 { 2308 unsigned int cur_el = arm_current_el(env); 2309 bool has_el2 = arm_is_el2_enabled(env); 2310 uint64_t hcr = arm_hcr_el2_eff(env); 2311 2312 switch (cur_el) { 2313 case 0: 2314 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2315 /* If HCR_EL2.<E2H,TGE> == '11': check CNTHCTL_EL2.EL0[PV]TEN. */ 2316 return (extract32(env->cp15.cnthctl_el2, 9 - timeridx, 1) 2317 ? CP_ACCESS_OK : CP_ACCESS_TRAP_EL2); 2318 } 2319 2320 /* 2321 * CNT[PV]_CVAL, CNT[PV]_CTL, CNT[PV]_TVAL: not visible from 2322 * EL0 if EL0[PV]TEN is zero. 2323 */ 2324 if (!extract32(env->cp15.c14_cntkctl, 9 - timeridx, 1)) { 2325 return CP_ACCESS_TRAP_EL1; 2326 } 2327 /* fall through */ 2328 2329 case 1: 2330 if (has_el2 && timeridx == GTIMER_PHYS) { 2331 if (hcr & HCR_E2H) { 2332 /* If HCR_EL2.<E2H,TGE> == '10': check CNTHCTL_EL2.EL1PTEN. */ 2333 if (!extract32(env->cp15.cnthctl_el2, 11, 1)) { 2334 return CP_ACCESS_TRAP_EL2; 2335 } 2336 } else { 2337 /* If HCR_EL2.<E2H> == 0: check CNTHCTL_EL2.EL1PCEN. */ 2338 if (!extract32(env->cp15.cnthctl_el2, 1, 1)) { 2339 return CP_ACCESS_TRAP_EL2; 2340 } 2341 } 2342 } 2343 if (has_el2 && timeridx == GTIMER_VIRT) { 2344 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1TVT)) { 2345 return CP_ACCESS_TRAP_EL2; 2346 } 2347 } 2348 break; 2349 } 2350 return CP_ACCESS_OK; 2351 } 2352 2353 static CPAccessResult gt_pct_access(CPUARMState *env, 2354 const ARMCPRegInfo *ri, 2355 bool isread) 2356 { 2357 return gt_counter_access(env, GTIMER_PHYS, isread); 2358 } 2359 2360 static CPAccessResult gt_vct_access(CPUARMState *env, 2361 const ARMCPRegInfo *ri, 2362 bool isread) 2363 { 2364 return gt_counter_access(env, GTIMER_VIRT, isread); 2365 } 2366 2367 static CPAccessResult gt_ptimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2368 bool isread) 2369 { 2370 return gt_timer_access(env, GTIMER_PHYS, isread); 2371 } 2372 2373 static CPAccessResult gt_vtimer_access(CPUARMState *env, const ARMCPRegInfo *ri, 2374 bool isread) 2375 { 2376 return gt_timer_access(env, GTIMER_VIRT, isread); 2377 } 2378 2379 static CPAccessResult gt_stimer_access(CPUARMState *env, 2380 const ARMCPRegInfo *ri, 2381 bool isread) 2382 { 2383 /* 2384 * The AArch64 register view of the secure physical timer is 2385 * always accessible from EL3, and configurably accessible from 2386 * Secure EL1. 2387 */ 2388 switch (arm_current_el(env)) { 2389 case 1: 2390 if (!arm_is_secure(env)) { 2391 return CP_ACCESS_UNDEFINED; 2392 } 2393 if (arm_is_el2_enabled(env)) { 2394 return CP_ACCESS_UNDEFINED; 2395 } 2396 if (!(env->cp15.scr_el3 & SCR_ST)) { 2397 return CP_ACCESS_TRAP_EL3; 2398 } 2399 return CP_ACCESS_OK; 2400 case 0: 2401 case 2: 2402 return CP_ACCESS_UNDEFINED; 2403 case 3: 2404 return CP_ACCESS_OK; 2405 default: 2406 g_assert_not_reached(); 2407 } 2408 } 2409 2410 static CPAccessResult gt_sel2timer_access(CPUARMState *env, 2411 const ARMCPRegInfo *ri, 2412 bool isread) 2413 { 2414 /* 2415 * The AArch64 register view of the secure EL2 timers are mostly 2416 * accessible from EL3 and EL2 although can also be trapped to EL2 2417 * from EL1 depending on nested virt config. 2418 */ 2419 switch (arm_current_el(env)) { 2420 case 0: /* UNDEFINED */ 2421 return CP_ACCESS_UNDEFINED; 2422 case 1: 2423 if (!arm_is_secure(env)) { 2424 /* UNDEFINED */ 2425 return CP_ACCESS_UNDEFINED; 2426 } else if (arm_hcr_el2_eff(env) & HCR_NV) { 2427 /* Aarch64.SystemAccessTrap(EL2, 0x18) */ 2428 return CP_ACCESS_TRAP_EL2; 2429 } 2430 /* UNDEFINED */ 2431 return CP_ACCESS_UNDEFINED; 2432 case 2: 2433 if (!arm_is_secure(env)) { 2434 /* UNDEFINED */ 2435 return CP_ACCESS_UNDEFINED; 2436 } 2437 return CP_ACCESS_OK; 2438 case 3: 2439 if (env->cp15.scr_el3 & SCR_EEL2) { 2440 return CP_ACCESS_OK; 2441 } else { 2442 return CP_ACCESS_UNDEFINED; 2443 } 2444 default: 2445 g_assert_not_reached(); 2446 } 2447 } 2448 2449 uint64_t gt_get_countervalue(CPUARMState *env) 2450 { 2451 ARMCPU *cpu = env_archcpu(env); 2452 2453 return qemu_clock_get_ns(QEMU_CLOCK_VIRTUAL) / gt_cntfrq_period_ns(cpu); 2454 } 2455 2456 static void gt_update_irq(ARMCPU *cpu, int timeridx) 2457 { 2458 CPUARMState *env = &cpu->env; 2459 uint64_t cnthctl = env->cp15.cnthctl_el2; 2460 ARMSecuritySpace ss = arm_security_space(env); 2461 /* ISTATUS && !IMASK */ 2462 int irqstate = (env->cp15.c14_timer[timeridx].ctl & 6) == 4; 2463 2464 /* 2465 * If bit CNTHCTL_EL2.CNT[VP]MASK is set, it overrides IMASK. 2466 * It is RES0 in Secure and NonSecure state. 2467 */ 2468 if ((ss == ARMSS_Root || ss == ARMSS_Realm) && 2469 ((timeridx == GTIMER_VIRT && (cnthctl & R_CNTHCTL_CNTVMASK_MASK)) || 2470 (timeridx == GTIMER_PHYS && (cnthctl & R_CNTHCTL_CNTPMASK_MASK)))) { 2471 irqstate = 0; 2472 } 2473 2474 qemu_set_irq(cpu->gt_timer_outputs[timeridx], irqstate); 2475 trace_arm_gt_update_irq(timeridx, irqstate); 2476 } 2477 2478 void gt_rme_post_el_change(ARMCPU *cpu, void *ignored) 2479 { 2480 /* 2481 * Changing security state between Root and Secure/NonSecure, which may 2482 * happen when switching EL, can change the effective value of CNTHCTL_EL2 2483 * mask bits. Update the IRQ state accordingly. 2484 */ 2485 gt_update_irq(cpu, GTIMER_VIRT); 2486 gt_update_irq(cpu, GTIMER_PHYS); 2487 } 2488 2489 static uint64_t gt_phys_raw_cnt_offset(CPUARMState *env) 2490 { 2491 if ((env->cp15.scr_el3 & SCR_ECVEN) && 2492 FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, ECV) && 2493 arm_is_el2_enabled(env) && 2494 (arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 2495 return env->cp15.cntpoff_el2; 2496 } 2497 return 0; 2498 } 2499 2500 static uint64_t gt_indirect_access_timer_offset(CPUARMState *env, int timeridx) 2501 { 2502 /* 2503 * Return the timer offset to use for indirect accesses to the timer. 2504 * This is the Offset value as defined in D12.2.4.1 "Operation of the 2505 * CompareValue views of the timers". 2506 * 2507 * The condition here is not always the same as the condition for 2508 * whether to apply an offset register when doing a direct read of 2509 * the counter sysreg; those conditions are described in the 2510 * access pseudocode for each counter register. 2511 */ 2512 switch (timeridx) { 2513 case GTIMER_PHYS: 2514 return gt_phys_raw_cnt_offset(env); 2515 case GTIMER_VIRT: 2516 return env->cp15.cntvoff_el2; 2517 case GTIMER_HYP: 2518 case GTIMER_SEC: 2519 case GTIMER_HYPVIRT: 2520 case GTIMER_S_EL2_PHYS: 2521 case GTIMER_S_EL2_VIRT: 2522 return 0; 2523 default: 2524 g_assert_not_reached(); 2525 } 2526 } 2527 2528 uint64_t gt_direct_access_timer_offset(CPUARMState *env, int timeridx) 2529 { 2530 /* 2531 * Return the timer offset to use for direct accesses to the 2532 * counter registers CNTPCT and CNTVCT, and for direct accesses 2533 * to the CNT*_TVAL registers. 2534 * 2535 * This isn't exactly the same as the indirect-access offset, 2536 * because here we also care about what EL the register access 2537 * is being made from. 2538 * 2539 * This corresponds to the access pseudocode for the registers. 2540 */ 2541 uint64_t hcr; 2542 2543 switch (timeridx) { 2544 case GTIMER_PHYS: 2545 if (arm_current_el(env) >= 2) { 2546 return 0; 2547 } 2548 return gt_phys_raw_cnt_offset(env); 2549 case GTIMER_VIRT: 2550 switch (arm_current_el(env)) { 2551 case 2: 2552 hcr = arm_hcr_el2_eff(env); 2553 if (hcr & HCR_E2H) { 2554 return 0; 2555 } 2556 break; 2557 case 0: 2558 hcr = arm_hcr_el2_eff(env); 2559 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 2560 return 0; 2561 } 2562 break; 2563 } 2564 return env->cp15.cntvoff_el2; 2565 case GTIMER_HYP: 2566 case GTIMER_SEC: 2567 case GTIMER_HYPVIRT: 2568 case GTIMER_S_EL2_PHYS: 2569 case GTIMER_S_EL2_VIRT: 2570 return 0; 2571 default: 2572 g_assert_not_reached(); 2573 } 2574 } 2575 2576 static void gt_recalc_timer(ARMCPU *cpu, int timeridx) 2577 { 2578 ARMGenericTimer *gt = &cpu->env.cp15.c14_timer[timeridx]; 2579 2580 if (gt->ctl & 1) { 2581 /* 2582 * Timer enabled: calculate and set current ISTATUS, irq, and 2583 * reset timer to when ISTATUS next has to change 2584 */ 2585 uint64_t offset = gt_indirect_access_timer_offset(&cpu->env, timeridx); 2586 uint64_t count = gt_get_countervalue(&cpu->env); 2587 /* Note that this must be unsigned 64 bit arithmetic: */ 2588 int istatus = count - offset >= gt->cval; 2589 uint64_t nexttick; 2590 2591 gt->ctl = deposit32(gt->ctl, 2, 1, istatus); 2592 2593 if (istatus) { 2594 /* 2595 * Next transition is when (count - offset) rolls back over to 0. 2596 * If offset > count then this is when count == offset; 2597 * if offset <= count then this is when count == offset + 2^64 2598 * For the latter case we set nexttick to an "as far in future 2599 * as possible" value and let the code below handle it. 2600 */ 2601 if (offset > count) { 2602 nexttick = offset; 2603 } else { 2604 nexttick = UINT64_MAX; 2605 } 2606 } else { 2607 /* 2608 * Next transition is when (count - offset) == cval, i.e. 2609 * when count == (cval + offset). 2610 * If that would overflow, then again we set up the next interrupt 2611 * for "as far in the future as possible" for the code below. 2612 */ 2613 if (uadd64_overflow(gt->cval, offset, &nexttick)) { 2614 nexttick = UINT64_MAX; 2615 } 2616 } 2617 /* 2618 * Note that the desired next expiry time might be beyond the 2619 * signed-64-bit range of a QEMUTimer -- in this case we just 2620 * set the timer for as far in the future as possible. When the 2621 * timer expires we will reset the timer for any remaining period. 2622 */ 2623 if (nexttick > INT64_MAX / gt_cntfrq_period_ns(cpu)) { 2624 timer_mod_ns(cpu->gt_timer[timeridx], INT64_MAX); 2625 } else { 2626 timer_mod(cpu->gt_timer[timeridx], nexttick); 2627 } 2628 trace_arm_gt_recalc(timeridx, nexttick); 2629 } else { 2630 /* Timer disabled: ISTATUS and timer output always clear */ 2631 gt->ctl &= ~4; 2632 timer_del(cpu->gt_timer[timeridx]); 2633 trace_arm_gt_recalc_disabled(timeridx); 2634 } 2635 gt_update_irq(cpu, timeridx); 2636 } 2637 2638 static void gt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri, 2639 int timeridx) 2640 { 2641 ARMCPU *cpu = env_archcpu(env); 2642 2643 timer_del(cpu->gt_timer[timeridx]); 2644 } 2645 2646 static uint64_t gt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2647 { 2648 uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_PHYS); 2649 return gt_get_countervalue(env) - offset; 2650 } 2651 2652 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 2653 { 2654 uint64_t offset = gt_direct_access_timer_offset(env, GTIMER_VIRT); 2655 return gt_get_countervalue(env) - offset; 2656 } 2657 2658 static void gt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2659 int timeridx, 2660 uint64_t value) 2661 { 2662 trace_arm_gt_cval_write(timeridx, value); 2663 env->cp15.c14_timer[timeridx].cval = value; 2664 gt_recalc_timer(env_archcpu(env), timeridx); 2665 } 2666 2667 static uint64_t do_tval_read(CPUARMState *env, int timeridx, uint64_t offset) 2668 { 2669 return (uint32_t)(env->cp15.c14_timer[timeridx].cval - 2670 (gt_get_countervalue(env) - offset)); 2671 } 2672 2673 static uint64_t gt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri, 2674 int timeridx) 2675 { 2676 uint64_t offset = gt_direct_access_timer_offset(env, timeridx); 2677 2678 return do_tval_read(env, timeridx, offset); 2679 } 2680 2681 static void do_tval_write(CPUARMState *env, int timeridx, uint64_t value, 2682 uint64_t offset) 2683 { 2684 trace_arm_gt_tval_write(timeridx, value); 2685 env->cp15.c14_timer[timeridx].cval = gt_get_countervalue(env) - offset + 2686 sextract64(value, 0, 32); 2687 gt_recalc_timer(env_archcpu(env), timeridx); 2688 } 2689 2690 static void gt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2691 int timeridx, 2692 uint64_t value) 2693 { 2694 uint64_t offset = gt_direct_access_timer_offset(env, timeridx); 2695 2696 do_tval_write(env, timeridx, value, offset); 2697 } 2698 2699 static void gt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2700 int timeridx, 2701 uint64_t value) 2702 { 2703 ARMCPU *cpu = env_archcpu(env); 2704 uint32_t oldval = env->cp15.c14_timer[timeridx].ctl; 2705 2706 trace_arm_gt_ctl_write(timeridx, value); 2707 env->cp15.c14_timer[timeridx].ctl = deposit64(oldval, 0, 2, value); 2708 if ((oldval ^ value) & 1) { 2709 /* Enable toggled */ 2710 gt_recalc_timer(cpu, timeridx); 2711 } else if ((oldval ^ value) & 2) { 2712 /* 2713 * IMASK toggled: don't need to recalculate, 2714 * just set the interrupt line based on ISTATUS 2715 */ 2716 trace_arm_gt_imask_toggle(timeridx); 2717 gt_update_irq(cpu, timeridx); 2718 } 2719 } 2720 2721 static void gt_phys_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2722 { 2723 gt_timer_reset(env, ri, GTIMER_PHYS); 2724 } 2725 2726 static void gt_phys_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2727 uint64_t value) 2728 { 2729 gt_cval_write(env, ri, GTIMER_PHYS, value); 2730 } 2731 2732 static uint64_t gt_phys_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2733 { 2734 return gt_tval_read(env, ri, GTIMER_PHYS); 2735 } 2736 2737 static void gt_phys_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2738 uint64_t value) 2739 { 2740 gt_tval_write(env, ri, GTIMER_PHYS, value); 2741 } 2742 2743 static void gt_phys_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2744 uint64_t value) 2745 { 2746 gt_ctl_write(env, ri, GTIMER_PHYS, value); 2747 } 2748 2749 static int gt_phys_redir_timeridx(CPUARMState *env) 2750 { 2751 switch (arm_mmu_idx(env)) { 2752 case ARMMMUIdx_E20_0: 2753 case ARMMMUIdx_E20_2: 2754 case ARMMMUIdx_E20_2_PAN: 2755 return GTIMER_HYP; 2756 default: 2757 return GTIMER_PHYS; 2758 } 2759 } 2760 2761 static int gt_virt_redir_timeridx(CPUARMState *env) 2762 { 2763 switch (arm_mmu_idx(env)) { 2764 case ARMMMUIdx_E20_0: 2765 case ARMMMUIdx_E20_2: 2766 case ARMMMUIdx_E20_2_PAN: 2767 return GTIMER_HYPVIRT; 2768 default: 2769 return GTIMER_VIRT; 2770 } 2771 } 2772 2773 static uint64_t gt_phys_redir_cval_read(CPUARMState *env, 2774 const ARMCPRegInfo *ri) 2775 { 2776 int timeridx = gt_phys_redir_timeridx(env); 2777 return env->cp15.c14_timer[timeridx].cval; 2778 } 2779 2780 static void gt_phys_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2781 uint64_t value) 2782 { 2783 int timeridx = gt_phys_redir_timeridx(env); 2784 gt_cval_write(env, ri, timeridx, value); 2785 } 2786 2787 static uint64_t gt_phys_redir_tval_read(CPUARMState *env, 2788 const ARMCPRegInfo *ri) 2789 { 2790 int timeridx = gt_phys_redir_timeridx(env); 2791 return gt_tval_read(env, ri, timeridx); 2792 } 2793 2794 static void gt_phys_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2795 uint64_t value) 2796 { 2797 int timeridx = gt_phys_redir_timeridx(env); 2798 gt_tval_write(env, ri, timeridx, value); 2799 } 2800 2801 static uint64_t gt_phys_redir_ctl_read(CPUARMState *env, 2802 const ARMCPRegInfo *ri) 2803 { 2804 int timeridx = gt_phys_redir_timeridx(env); 2805 return env->cp15.c14_timer[timeridx].ctl; 2806 } 2807 2808 static void gt_phys_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2809 uint64_t value) 2810 { 2811 int timeridx = gt_phys_redir_timeridx(env); 2812 gt_ctl_write(env, ri, timeridx, value); 2813 } 2814 2815 static void gt_virt_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2816 { 2817 gt_timer_reset(env, ri, GTIMER_VIRT); 2818 } 2819 2820 static void gt_virt_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2821 uint64_t value) 2822 { 2823 gt_cval_write(env, ri, GTIMER_VIRT, value); 2824 } 2825 2826 static uint64_t gt_virt_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2827 { 2828 /* 2829 * This is CNTV_TVAL_EL02; unlike the underlying CNTV_TVAL_EL0 2830 * we always apply CNTVOFF_EL2. Special case that here rather 2831 * than going into the generic gt_tval_read() and then having 2832 * to re-detect that it's this register. 2833 * Note that the accessfn/perms mean we know we're at EL2 or EL3 here. 2834 */ 2835 return do_tval_read(env, GTIMER_VIRT, env->cp15.cntvoff_el2); 2836 } 2837 2838 static void gt_virt_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2839 uint64_t value) 2840 { 2841 /* Similarly for writes to CNTV_TVAL_EL02 */ 2842 do_tval_write(env, GTIMER_VIRT, value, env->cp15.cntvoff_el2); 2843 } 2844 2845 static void gt_virt_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2846 uint64_t value) 2847 { 2848 gt_ctl_write(env, ri, GTIMER_VIRT, value); 2849 } 2850 2851 static void gt_cnthctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2852 uint64_t value) 2853 { 2854 ARMCPU *cpu = env_archcpu(env); 2855 uint32_t oldval = env->cp15.cnthctl_el2; 2856 uint32_t valid_mask = 2857 R_CNTHCTL_EL0PCTEN_E2H1_MASK | 2858 R_CNTHCTL_EL0VCTEN_E2H1_MASK | 2859 R_CNTHCTL_EVNTEN_MASK | 2860 R_CNTHCTL_EVNTDIR_MASK | 2861 R_CNTHCTL_EVNTI_MASK | 2862 R_CNTHCTL_EL0VTEN_MASK | 2863 R_CNTHCTL_EL0PTEN_MASK | 2864 R_CNTHCTL_EL1PCTEN_E2H1_MASK | 2865 R_CNTHCTL_EL1PTEN_MASK; 2866 2867 if (cpu_isar_feature(aa64_rme, cpu)) { 2868 valid_mask |= R_CNTHCTL_CNTVMASK_MASK | R_CNTHCTL_CNTPMASK_MASK; 2869 } 2870 if (cpu_isar_feature(aa64_ecv_traps, cpu)) { 2871 valid_mask |= 2872 R_CNTHCTL_EL1TVT_MASK | 2873 R_CNTHCTL_EL1TVCT_MASK | 2874 R_CNTHCTL_EL1NVPCT_MASK | 2875 R_CNTHCTL_EL1NVVCT_MASK | 2876 R_CNTHCTL_EVNTIS_MASK; 2877 } 2878 if (cpu_isar_feature(aa64_ecv, cpu)) { 2879 valid_mask |= R_CNTHCTL_ECV_MASK; 2880 } 2881 2882 /* Clear RES0 bits */ 2883 value &= valid_mask; 2884 2885 raw_write(env, ri, value); 2886 2887 if ((oldval ^ value) & R_CNTHCTL_CNTVMASK_MASK) { 2888 gt_update_irq(cpu, GTIMER_VIRT); 2889 } else if ((oldval ^ value) & R_CNTHCTL_CNTPMASK_MASK) { 2890 gt_update_irq(cpu, GTIMER_PHYS); 2891 } 2892 } 2893 2894 static void gt_cntvoff_write(CPUARMState *env, const ARMCPRegInfo *ri, 2895 uint64_t value) 2896 { 2897 ARMCPU *cpu = env_archcpu(env); 2898 2899 trace_arm_gt_cntvoff_write(value); 2900 raw_write(env, ri, value); 2901 gt_recalc_timer(cpu, GTIMER_VIRT); 2902 } 2903 2904 static uint64_t gt_virt_redir_cval_read(CPUARMState *env, 2905 const ARMCPRegInfo *ri) 2906 { 2907 int timeridx = gt_virt_redir_timeridx(env); 2908 return env->cp15.c14_timer[timeridx].cval; 2909 } 2910 2911 static void gt_virt_redir_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2912 uint64_t value) 2913 { 2914 int timeridx = gt_virt_redir_timeridx(env); 2915 gt_cval_write(env, ri, timeridx, value); 2916 } 2917 2918 static uint64_t gt_virt_redir_tval_read(CPUARMState *env, 2919 const ARMCPRegInfo *ri) 2920 { 2921 int timeridx = gt_virt_redir_timeridx(env); 2922 return gt_tval_read(env, ri, timeridx); 2923 } 2924 2925 static void gt_virt_redir_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2926 uint64_t value) 2927 { 2928 int timeridx = gt_virt_redir_timeridx(env); 2929 gt_tval_write(env, ri, timeridx, value); 2930 } 2931 2932 static uint64_t gt_virt_redir_ctl_read(CPUARMState *env, 2933 const ARMCPRegInfo *ri) 2934 { 2935 int timeridx = gt_virt_redir_timeridx(env); 2936 return env->cp15.c14_timer[timeridx].ctl; 2937 } 2938 2939 static void gt_virt_redir_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2940 uint64_t value) 2941 { 2942 int timeridx = gt_virt_redir_timeridx(env); 2943 gt_ctl_write(env, ri, timeridx, value); 2944 } 2945 2946 static void gt_hyp_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2947 { 2948 gt_timer_reset(env, ri, GTIMER_HYP); 2949 } 2950 2951 static void gt_hyp_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2952 uint64_t value) 2953 { 2954 gt_cval_write(env, ri, GTIMER_HYP, value); 2955 } 2956 2957 static uint64_t gt_hyp_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2958 { 2959 return gt_tval_read(env, ri, GTIMER_HYP); 2960 } 2961 2962 static void gt_hyp_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2963 uint64_t value) 2964 { 2965 gt_tval_write(env, ri, GTIMER_HYP, value); 2966 } 2967 2968 static void gt_hyp_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2969 uint64_t value) 2970 { 2971 gt_ctl_write(env, ri, GTIMER_HYP, value); 2972 } 2973 2974 static void gt_sec_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 2975 { 2976 gt_timer_reset(env, ri, GTIMER_SEC); 2977 } 2978 2979 static void gt_sec_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2980 uint64_t value) 2981 { 2982 gt_cval_write(env, ri, GTIMER_SEC, value); 2983 } 2984 2985 static uint64_t gt_sec_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 2986 { 2987 return gt_tval_read(env, ri, GTIMER_SEC); 2988 } 2989 2990 static void gt_sec_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 2991 uint64_t value) 2992 { 2993 gt_tval_write(env, ri, GTIMER_SEC, value); 2994 } 2995 2996 static void gt_sec_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 2997 uint64_t value) 2998 { 2999 gt_ctl_write(env, ri, GTIMER_SEC, value); 3000 } 3001 3002 static void gt_sec_pel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3003 { 3004 gt_timer_reset(env, ri, GTIMER_S_EL2_PHYS); 3005 } 3006 3007 static void gt_sec_pel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3008 uint64_t value) 3009 { 3010 gt_cval_write(env, ri, GTIMER_S_EL2_PHYS, value); 3011 } 3012 3013 static uint64_t gt_sec_pel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3014 { 3015 return gt_tval_read(env, ri, GTIMER_S_EL2_PHYS); 3016 } 3017 3018 static void gt_sec_pel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3019 uint64_t value) 3020 { 3021 gt_tval_write(env, ri, GTIMER_S_EL2_PHYS, value); 3022 } 3023 3024 static void gt_sec_pel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3025 uint64_t value) 3026 { 3027 gt_ctl_write(env, ri, GTIMER_S_EL2_PHYS, value); 3028 } 3029 3030 static void gt_sec_vel2_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3031 { 3032 gt_timer_reset(env, ri, GTIMER_S_EL2_VIRT); 3033 } 3034 3035 static void gt_sec_vel2_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3036 uint64_t value) 3037 { 3038 gt_cval_write(env, ri, GTIMER_S_EL2_VIRT, value); 3039 } 3040 3041 static uint64_t gt_sec_vel2_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3042 { 3043 return gt_tval_read(env, ri, GTIMER_S_EL2_VIRT); 3044 } 3045 3046 static void gt_sec_vel2_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3047 uint64_t value) 3048 { 3049 gt_tval_write(env, ri, GTIMER_S_EL2_VIRT, value); 3050 } 3051 3052 static void gt_sec_vel2_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3053 uint64_t value) 3054 { 3055 gt_ctl_write(env, ri, GTIMER_S_EL2_VIRT, value); 3056 } 3057 3058 static void gt_hv_timer_reset(CPUARMState *env, const ARMCPRegInfo *ri) 3059 { 3060 gt_timer_reset(env, ri, GTIMER_HYPVIRT); 3061 } 3062 3063 static void gt_hv_cval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3064 uint64_t value) 3065 { 3066 gt_cval_write(env, ri, GTIMER_HYPVIRT, value); 3067 } 3068 3069 static uint64_t gt_hv_tval_read(CPUARMState *env, const ARMCPRegInfo *ri) 3070 { 3071 return gt_tval_read(env, ri, GTIMER_HYPVIRT); 3072 } 3073 3074 static void gt_hv_tval_write(CPUARMState *env, const ARMCPRegInfo *ri, 3075 uint64_t value) 3076 { 3077 gt_tval_write(env, ri, GTIMER_HYPVIRT, value); 3078 } 3079 3080 static void gt_hv_ctl_write(CPUARMState *env, const ARMCPRegInfo *ri, 3081 uint64_t value) 3082 { 3083 gt_ctl_write(env, ri, GTIMER_HYPVIRT, value); 3084 } 3085 3086 void arm_gt_ptimer_cb(void *opaque) 3087 { 3088 ARMCPU *cpu = opaque; 3089 3090 gt_recalc_timer(cpu, GTIMER_PHYS); 3091 } 3092 3093 void arm_gt_vtimer_cb(void *opaque) 3094 { 3095 ARMCPU *cpu = opaque; 3096 3097 gt_recalc_timer(cpu, GTIMER_VIRT); 3098 } 3099 3100 void arm_gt_htimer_cb(void *opaque) 3101 { 3102 ARMCPU *cpu = opaque; 3103 3104 gt_recalc_timer(cpu, GTIMER_HYP); 3105 } 3106 3107 void arm_gt_stimer_cb(void *opaque) 3108 { 3109 ARMCPU *cpu = opaque; 3110 3111 gt_recalc_timer(cpu, GTIMER_SEC); 3112 } 3113 3114 void arm_gt_sel2timer_cb(void *opaque) 3115 { 3116 ARMCPU *cpu = opaque; 3117 3118 gt_recalc_timer(cpu, GTIMER_S_EL2_PHYS); 3119 } 3120 3121 void arm_gt_sel2vtimer_cb(void *opaque) 3122 { 3123 ARMCPU *cpu = opaque; 3124 3125 gt_recalc_timer(cpu, GTIMER_S_EL2_VIRT); 3126 } 3127 3128 void arm_gt_hvtimer_cb(void *opaque) 3129 { 3130 ARMCPU *cpu = opaque; 3131 3132 gt_recalc_timer(cpu, GTIMER_HYPVIRT); 3133 } 3134 3135 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 3136 /* 3137 * Note that CNTFRQ is purely reads-as-written for the benefit 3138 * of software; writing it doesn't actually change the timer frequency. 3139 * Our reset value matches the fixed frequency we implement the timer at. 3140 */ 3141 { .name = "CNTFRQ", .cp = 15, .crn = 14, .crm = 0, .opc1 = 0, .opc2 = 0, 3142 .type = ARM_CP_ALIAS, 3143 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 3144 .fieldoffset = offsetoflow32(CPUARMState, cp15.c14_cntfrq), 3145 }, 3146 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 3147 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 3148 .access = PL1_RW | PL0_R, .accessfn = gt_cntfrq_access, 3149 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 3150 .resetfn = arm_gt_cntfrq_reset, 3151 }, 3152 /* overall control: mostly access permissions */ 3153 { .name = "CNTKCTL", .state = ARM_CP_STATE_BOTH, 3154 .opc0 = 3, .opc1 = 0, .crn = 14, .crm = 1, .opc2 = 0, 3155 .access = PL1_RW, 3156 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntkctl), 3157 .resetvalue = 0, 3158 }, 3159 /* per-timer control */ 3160 { .name = "CNTP_CTL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 3161 .secure = ARM_CP_SECSTATE_NS, 3162 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3163 .accessfn = gt_ptimer_access, 3164 .fieldoffset = offsetoflow32(CPUARMState, 3165 cp15.c14_timer[GTIMER_PHYS].ctl), 3166 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read, 3167 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write, 3168 }, 3169 { .name = "CNTP_CTL_S", 3170 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 1, 3171 .secure = ARM_CP_SECSTATE_S, 3172 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3173 .accessfn = gt_ptimer_access, 3174 .fieldoffset = offsetoflow32(CPUARMState, 3175 cp15.c14_timer[GTIMER_SEC].ctl), 3176 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 3177 }, 3178 { .name = "CNTP_CTL_EL0", .state = ARM_CP_STATE_AA64, 3179 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 1, 3180 .type = ARM_CP_IO, .access = PL0_RW, 3181 .accessfn = gt_ptimer_access, 3182 .nv2_redirect_offset = 0x180 | NV2_REDIR_NV1, 3183 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 3184 .resetvalue = 0, 3185 .readfn = gt_phys_redir_ctl_read, .raw_readfn = raw_read, 3186 .writefn = gt_phys_redir_ctl_write, .raw_writefn = raw_write, 3187 }, 3188 { .name = "CNTV_CTL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 1, 3189 .type = ARM_CP_IO | ARM_CP_ALIAS, .access = PL0_RW, 3190 .accessfn = gt_vtimer_access, 3191 .fieldoffset = offsetoflow32(CPUARMState, 3192 cp15.c14_timer[GTIMER_VIRT].ctl), 3193 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read, 3194 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write, 3195 }, 3196 { .name = "CNTV_CTL_EL0", .state = ARM_CP_STATE_AA64, 3197 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 1, 3198 .type = ARM_CP_IO, .access = PL0_RW, 3199 .accessfn = gt_vtimer_access, 3200 .nv2_redirect_offset = 0x170 | NV2_REDIR_NV1, 3201 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 3202 .resetvalue = 0, 3203 .readfn = gt_virt_redir_ctl_read, .raw_readfn = raw_read, 3204 .writefn = gt_virt_redir_ctl_write, .raw_writefn = raw_write, 3205 }, 3206 /* TimerValue views: a 32 bit downcounting view of the underlying state */ 3207 { .name = "CNTP_TVAL", .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 3208 .secure = ARM_CP_SECSTATE_NS, 3209 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3210 .accessfn = gt_ptimer_access, 3211 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write, 3212 }, 3213 { .name = "CNTP_TVAL_S", 3214 .cp = 15, .crn = 14, .crm = 2, .opc1 = 0, .opc2 = 0, 3215 .secure = ARM_CP_SECSTATE_S, 3216 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3217 .accessfn = gt_ptimer_access, 3218 .readfn = gt_sec_tval_read, .writefn = gt_sec_tval_write, 3219 }, 3220 { .name = "CNTP_TVAL_EL0", .state = ARM_CP_STATE_AA64, 3221 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 0, 3222 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3223 .accessfn = gt_ptimer_access, .resetfn = gt_phys_timer_reset, 3224 .readfn = gt_phys_redir_tval_read, .writefn = gt_phys_redir_tval_write, 3225 }, 3226 { .name = "CNTV_TVAL", .cp = 15, .crn = 14, .crm = 3, .opc1 = 0, .opc2 = 0, 3227 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3228 .accessfn = gt_vtimer_access, 3229 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write, 3230 }, 3231 { .name = "CNTV_TVAL_EL0", .state = ARM_CP_STATE_AA64, 3232 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 0, 3233 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL0_RW, 3234 .accessfn = gt_vtimer_access, .resetfn = gt_virt_timer_reset, 3235 .readfn = gt_virt_redir_tval_read, .writefn = gt_virt_redir_tval_write, 3236 }, 3237 /* The counter itself */ 3238 { .name = "CNTPCT", .cp = 15, .crm = 14, .opc1 = 0, 3239 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 3240 .accessfn = gt_pct_access, 3241 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, 3242 }, 3243 { .name = "CNTPCT_EL0", .state = ARM_CP_STATE_AA64, 3244 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 1, 3245 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3246 .accessfn = gt_pct_access, .readfn = gt_cnt_read, 3247 }, 3248 { .name = "CNTVCT", .cp = 15, .crm = 14, .opc1 = 1, 3249 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 3250 .accessfn = gt_vct_access, 3251 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, 3252 }, 3253 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 3254 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 3255 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3256 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, 3257 }, 3258 /* Comparison value, indicating when the timer goes off */ 3259 { .name = "CNTP_CVAL", .cp = 15, .crm = 14, .opc1 = 2, 3260 .secure = ARM_CP_SECSTATE_NS, 3261 .access = PL0_RW, 3262 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3263 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 3264 .accessfn = gt_ptimer_access, 3265 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read, 3266 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write, 3267 }, 3268 { .name = "CNTP_CVAL_S", .cp = 15, .crm = 14, .opc1 = 2, 3269 .secure = ARM_CP_SECSTATE_S, 3270 .access = PL0_RW, 3271 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3272 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 3273 .accessfn = gt_ptimer_access, 3274 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 3275 }, 3276 { .name = "CNTP_CVAL_EL0", .state = ARM_CP_STATE_AA64, 3277 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 2, .opc2 = 2, 3278 .access = PL0_RW, 3279 .type = ARM_CP_IO, 3280 .nv2_redirect_offset = 0x178 | NV2_REDIR_NV1, 3281 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 3282 .resetvalue = 0, .accessfn = gt_ptimer_access, 3283 .readfn = gt_phys_redir_cval_read, .raw_readfn = raw_read, 3284 .writefn = gt_phys_redir_cval_write, .raw_writefn = raw_write, 3285 }, 3286 { .name = "CNTV_CVAL", .cp = 15, .crm = 14, .opc1 = 3, 3287 .access = PL0_RW, 3288 .type = ARM_CP_64BIT | ARM_CP_IO | ARM_CP_ALIAS, 3289 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 3290 .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 { .name = "CNTV_CVAL_EL0", .state = ARM_CP_STATE_AA64, 3295 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 3, .opc2 = 2, 3296 .access = PL0_RW, 3297 .type = ARM_CP_IO, 3298 .nv2_redirect_offset = 0x168 | NV2_REDIR_NV1, 3299 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 3300 .resetvalue = 0, .accessfn = gt_vtimer_access, 3301 .readfn = gt_virt_redir_cval_read, .raw_readfn = raw_read, 3302 .writefn = gt_virt_redir_cval_write, .raw_writefn = raw_write, 3303 }, 3304 /* 3305 * Secure timer -- this is actually restricted to only EL3 3306 * and configurably Secure-EL1 via the accessfn. 3307 */ 3308 { .name = "CNTPS_TVAL_EL1", .state = ARM_CP_STATE_AA64, 3309 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 0, 3310 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL1_RW, 3311 .accessfn = gt_stimer_access, 3312 .readfn = gt_sec_tval_read, 3313 .writefn = gt_sec_tval_write, 3314 .resetfn = gt_sec_timer_reset, 3315 }, 3316 { .name = "CNTPS_CTL_EL1", .state = ARM_CP_STATE_AA64, 3317 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 1, 3318 .type = ARM_CP_IO, .access = PL1_RW, 3319 .accessfn = gt_stimer_access, 3320 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].ctl), 3321 .resetvalue = 0, 3322 .writefn = gt_sec_ctl_write, .raw_writefn = raw_write, 3323 }, 3324 { .name = "CNTPS_CVAL_EL1", .state = ARM_CP_STATE_AA64, 3325 .opc0 = 3, .opc1 = 7, .crn = 14, .crm = 2, .opc2 = 2, 3326 .type = ARM_CP_IO, .access = PL1_RW, 3327 .accessfn = gt_stimer_access, 3328 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_SEC].cval), 3329 .writefn = gt_sec_cval_write, .raw_writefn = raw_write, 3330 }, 3331 }; 3332 3333 /* 3334 * FEAT_ECV adds extra views of CNTVCT_EL0 and CNTPCT_EL0 which 3335 * are "self-synchronizing". For QEMU all sysregs are self-synchronizing, 3336 * so our implementations here are identical to the normal registers. 3337 */ 3338 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = { 3339 { .name = "CNTVCTSS", .cp = 15, .crm = 14, .opc1 = 9, 3340 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 3341 .accessfn = gt_vct_access, 3342 .readfn = gt_virt_cnt_read, .resetfn = arm_cp_reset_ignore, 3343 }, 3344 { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64, 3345 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6, 3346 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3347 .accessfn = gt_vct_access, .readfn = gt_virt_cnt_read, 3348 }, 3349 { .name = "CNTPCTSS", .cp = 15, .crm = 14, .opc1 = 8, 3350 .access = PL0_R, .type = ARM_CP_64BIT | ARM_CP_NO_RAW | ARM_CP_IO, 3351 .accessfn = gt_pct_access, 3352 .readfn = gt_cnt_read, .resetfn = arm_cp_reset_ignore, 3353 }, 3354 { .name = "CNTPCTSS_EL0", .state = ARM_CP_STATE_AA64, 3355 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 5, 3356 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3357 .accessfn = gt_pct_access, .readfn = gt_cnt_read, 3358 }, 3359 }; 3360 3361 static CPAccessResult gt_cntpoff_access(CPUARMState *env, 3362 const ARMCPRegInfo *ri, 3363 bool isread) 3364 { 3365 if (arm_current_el(env) == 2 && arm_feature(env, ARM_FEATURE_EL3) && 3366 !(env->cp15.scr_el3 & SCR_ECVEN)) { 3367 return CP_ACCESS_TRAP_EL3; 3368 } 3369 return CP_ACCESS_OK; 3370 } 3371 3372 static void gt_cntpoff_write(CPUARMState *env, const ARMCPRegInfo *ri, 3373 uint64_t value) 3374 { 3375 ARMCPU *cpu = env_archcpu(env); 3376 3377 trace_arm_gt_cntpoff_write(value); 3378 raw_write(env, ri, value); 3379 gt_recalc_timer(cpu, GTIMER_PHYS); 3380 } 3381 3382 static const ARMCPRegInfo gen_timer_cntpoff_reginfo = { 3383 .name = "CNTPOFF_EL2", .state = ARM_CP_STATE_AA64, 3384 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 6, 3385 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, 3386 .accessfn = gt_cntpoff_access, .writefn = gt_cntpoff_write, 3387 .nv2_redirect_offset = 0x1a8, 3388 .fieldoffset = offsetof(CPUARMState, cp15.cntpoff_el2), 3389 }; 3390 #else 3391 3392 /* 3393 * In user-mode most of the generic timer registers are inaccessible 3394 * however modern kernels (4.12+) allow access to cntvct_el0 3395 */ 3396 3397 static uint64_t gt_virt_cnt_read(CPUARMState *env, const ARMCPRegInfo *ri) 3398 { 3399 ARMCPU *cpu = env_archcpu(env); 3400 3401 /* 3402 * Currently we have no support for QEMUTimer in linux-user so we 3403 * can't call gt_get_countervalue(env), instead we directly 3404 * call the lower level functions. 3405 */ 3406 return cpu_get_clock() / gt_cntfrq_period_ns(cpu); 3407 } 3408 3409 static const ARMCPRegInfo generic_timer_cp_reginfo[] = { 3410 { .name = "CNTFRQ_EL0", .state = ARM_CP_STATE_AA64, 3411 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 0, 3412 .type = ARM_CP_CONST, .access = PL0_R /* no PL1_RW in linux-user */, 3413 .fieldoffset = offsetof(CPUARMState, cp15.c14_cntfrq), 3414 .resetfn = arm_gt_cntfrq_reset, 3415 }, 3416 { .name = "CNTVCT_EL0", .state = ARM_CP_STATE_AA64, 3417 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 2, 3418 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3419 .readfn = gt_virt_cnt_read, 3420 }, 3421 }; 3422 3423 /* 3424 * CNTVCTSS_EL0 has the same trap conditions as CNTVCT_EL0, so it also 3425 * is exposed to userspace by Linux. 3426 */ 3427 static const ARMCPRegInfo gen_timer_ecv_cp_reginfo[] = { 3428 { .name = "CNTVCTSS_EL0", .state = ARM_CP_STATE_AA64, 3429 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 0, .opc2 = 6, 3430 .access = PL0_R, .type = ARM_CP_NO_RAW | ARM_CP_IO, 3431 .readfn = gt_virt_cnt_read, 3432 }, 3433 }; 3434 3435 #endif 3436 3437 static void par_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3438 { 3439 if (arm_feature(env, ARM_FEATURE_LPAE)) { 3440 raw_write(env, ri, value); 3441 } else if (arm_feature(env, ARM_FEATURE_V7)) { 3442 raw_write(env, ri, value & 0xfffff6ff); 3443 } else { 3444 raw_write(env, ri, value & 0xfffff1ff); 3445 } 3446 } 3447 3448 #ifndef CONFIG_USER_ONLY 3449 /* get_phys_addr() isn't present for user-mode-only targets */ 3450 3451 static CPAccessResult ats_access(CPUARMState *env, const ARMCPRegInfo *ri, 3452 bool isread) 3453 { 3454 if (ri->opc2 & 4) { 3455 /* 3456 * The ATS12NSO* operations must trap to EL3 or EL2 if executed in 3457 * Secure EL1 (which can only happen if EL3 is AArch64). 3458 * They are simply UNDEF if executed from NS EL1. 3459 * They function normally from EL2 or EL3. 3460 */ 3461 if (arm_current_el(env) == 1) { 3462 if (arm_is_secure_below_el3(env)) { 3463 if (env->cp15.scr_el3 & SCR_EEL2) { 3464 return CP_ACCESS_TRAP_EL2; 3465 } 3466 return CP_ACCESS_TRAP_EL3; 3467 } 3468 return CP_ACCESS_UNDEFINED; 3469 } 3470 } 3471 return CP_ACCESS_OK; 3472 } 3473 3474 #ifdef CONFIG_TCG 3475 static int par_el1_shareability(GetPhysAddrResult *res) 3476 { 3477 /* 3478 * The PAR_EL1.SH field must be 0b10 for Device or Normal-NC 3479 * memory -- see pseudocode PAREncodeShareability(). 3480 */ 3481 if (((res->cacheattrs.attrs & 0xf0) == 0) || 3482 res->cacheattrs.attrs == 0x44 || res->cacheattrs.attrs == 0x40) { 3483 return 2; 3484 } 3485 return res->cacheattrs.shareability; 3486 } 3487 3488 static uint64_t do_ats_write(CPUARMState *env, uint64_t value, 3489 MMUAccessType access_type, ARMMMUIdx mmu_idx, 3490 ARMSecuritySpace ss) 3491 { 3492 bool ret; 3493 uint64_t par64; 3494 bool format64 = false; 3495 ARMMMUFaultInfo fi = {}; 3496 GetPhysAddrResult res = {}; 3497 3498 /* 3499 * I_MXTJT: Granule protection checks are not performed on the final 3500 * address of a successful translation. This is a translation not a 3501 * memory reference, so "memop = none = 0". 3502 */ 3503 ret = get_phys_addr_with_space_nogpc(env, value, access_type, 0, 3504 mmu_idx, ss, &res, &fi); 3505 3506 /* 3507 * ATS operations only do S1 or S1+S2 translations, so we never 3508 * have to deal with the ARMCacheAttrs format for S2 only. 3509 */ 3510 assert(!res.cacheattrs.is_s2_format); 3511 3512 if (ret) { 3513 /* 3514 * Some kinds of translation fault must cause exceptions rather 3515 * than being reported in the PAR. 3516 */ 3517 int current_el = arm_current_el(env); 3518 int target_el; 3519 uint32_t syn, fsr, fsc; 3520 bool take_exc = false; 3521 3522 if (fi.s1ptw && current_el == 1 3523 && arm_mmu_idx_is_stage1_of_2(mmu_idx)) { 3524 /* 3525 * Synchronous stage 2 fault on an access made as part of the 3526 * translation table walk for AT S1E0* or AT S1E1* insn 3527 * executed from NS EL1. If this is a synchronous external abort 3528 * and SCR_EL3.EA == 1, then we take a synchronous external abort 3529 * to EL3. Otherwise the fault is taken as an exception to EL2, 3530 * and HPFAR_EL2 holds the faulting IPA. 3531 */ 3532 if (fi.type == ARMFault_SyncExternalOnWalk && 3533 (env->cp15.scr_el3 & SCR_EA)) { 3534 target_el = 3; 3535 } else { 3536 env->cp15.hpfar_el2 = extract64(fi.s2addr, 12, 47) << 4; 3537 if (arm_is_secure_below_el3(env) && fi.s1ns) { 3538 env->cp15.hpfar_el2 |= HPFAR_NS; 3539 } 3540 target_el = 2; 3541 } 3542 take_exc = true; 3543 } else if (fi.type == ARMFault_SyncExternalOnWalk) { 3544 /* 3545 * Synchronous external aborts during a translation table walk 3546 * are taken as Data Abort exceptions. 3547 */ 3548 if (fi.stage2) { 3549 if (current_el == 3) { 3550 target_el = 3; 3551 } else { 3552 target_el = 2; 3553 } 3554 } else { 3555 target_el = exception_target_el(env); 3556 } 3557 take_exc = true; 3558 } 3559 3560 if (take_exc) { 3561 /* Construct FSR and FSC using same logic as arm_deliver_fault() */ 3562 if (target_el == 2 || arm_el_is_aa64(env, target_el) || 3563 arm_s1_regime_using_lpae_format(env, mmu_idx)) { 3564 fsr = arm_fi_to_lfsc(&fi); 3565 fsc = extract32(fsr, 0, 6); 3566 } else { 3567 fsr = arm_fi_to_sfsc(&fi); 3568 fsc = 0x3f; 3569 } 3570 /* 3571 * Report exception with ESR indicating a fault due to a 3572 * translation table walk for a cache maintenance instruction. 3573 */ 3574 syn = syn_data_abort_no_iss(current_el == target_el, 0, 3575 fi.ea, 1, fi.s1ptw, 1, fsc); 3576 env->exception.vaddress = value; 3577 env->exception.fsr = fsr; 3578 raise_exception(env, EXCP_DATA_ABORT, syn, target_el); 3579 } 3580 } 3581 3582 if (is_a64(env)) { 3583 format64 = true; 3584 } else if (arm_feature(env, ARM_FEATURE_LPAE)) { 3585 /* 3586 * ATS1Cxx: 3587 * * TTBCR.EAE determines whether the result is returned using the 3588 * 32-bit or the 64-bit PAR format 3589 * * Instructions executed in Hyp mode always use the 64bit format 3590 * 3591 * ATS1S2NSOxx uses the 64bit format if any of the following is true: 3592 * * The Non-secure TTBCR.EAE bit is set to 1 3593 * * The implementation includes EL2, and the value of HCR.VM is 1 3594 * 3595 * (Note that HCR.DC makes HCR.VM behave as if it is 1.) 3596 * 3597 * ATS1Hx always uses the 64bit format. 3598 */ 3599 format64 = arm_s1_regime_using_lpae_format(env, mmu_idx); 3600 3601 if (arm_feature(env, ARM_FEATURE_EL2)) { 3602 if (mmu_idx == ARMMMUIdx_E10_0 || 3603 mmu_idx == ARMMMUIdx_E10_1 || 3604 mmu_idx == ARMMMUIdx_E10_1_PAN) { 3605 format64 |= env->cp15.hcr_el2 & (HCR_VM | HCR_DC); 3606 } else { 3607 format64 |= arm_current_el(env) == 2; 3608 } 3609 } 3610 } 3611 3612 if (format64) { 3613 /* Create a 64-bit PAR */ 3614 par64 = (1 << 11); /* LPAE bit always set */ 3615 if (!ret) { 3616 par64 |= res.f.phys_addr & ~0xfffULL; 3617 if (!res.f.attrs.secure) { 3618 par64 |= (1 << 9); /* NS */ 3619 } 3620 par64 |= (uint64_t)res.cacheattrs.attrs << 56; /* ATTR */ 3621 par64 |= par_el1_shareability(&res) << 7; /* SH */ 3622 } else { 3623 uint32_t fsr = arm_fi_to_lfsc(&fi); 3624 3625 par64 |= 1; /* F */ 3626 par64 |= (fsr & 0x3f) << 1; /* FS */ 3627 if (fi.stage2) { 3628 par64 |= (1 << 9); /* S */ 3629 } 3630 if (fi.s1ptw) { 3631 par64 |= (1 << 8); /* PTW */ 3632 } 3633 } 3634 } else { 3635 /* 3636 * fsr is a DFSR/IFSR value for the short descriptor 3637 * translation table format (with WnR always clear). 3638 * Convert it to a 32-bit PAR. 3639 */ 3640 if (!ret) { 3641 /* We do not set any attribute bits in the PAR */ 3642 if (res.f.lg_page_size == 24 3643 && arm_feature(env, ARM_FEATURE_V7)) { 3644 par64 = (res.f.phys_addr & 0xff000000) | (1 << 1); 3645 } else { 3646 par64 = res.f.phys_addr & 0xfffff000; 3647 } 3648 if (!res.f.attrs.secure) { 3649 par64 |= (1 << 9); /* NS */ 3650 } 3651 } else { 3652 uint32_t fsr = arm_fi_to_sfsc(&fi); 3653 3654 par64 = ((fsr & (1 << 10)) >> 5) | ((fsr & (1 << 12)) >> 6) | 3655 ((fsr & 0xf) << 1) | 1; 3656 } 3657 } 3658 return par64; 3659 } 3660 #endif /* CONFIG_TCG */ 3661 3662 static void ats_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 3663 { 3664 #ifdef CONFIG_TCG 3665 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3666 uint64_t par64; 3667 ARMMMUIdx mmu_idx; 3668 int el = arm_current_el(env); 3669 ARMSecuritySpace ss = arm_security_space(env); 3670 3671 switch (ri->opc2 & 6) { 3672 case 0: 3673 /* stage 1 current state PL1: ATS1CPR, ATS1CPW, ATS1CPRP, ATS1CPWP */ 3674 switch (el) { 3675 case 3: 3676 if (ri->crm == 9 && arm_pan_enabled(env)) { 3677 mmu_idx = ARMMMUIdx_E30_3_PAN; 3678 } else { 3679 mmu_idx = ARMMMUIdx_E3; 3680 } 3681 break; 3682 case 2: 3683 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */ 3684 /* fall through */ 3685 case 1: 3686 if (ri->crm == 9 && arm_pan_enabled(env)) { 3687 mmu_idx = ARMMMUIdx_Stage1_E1_PAN; 3688 } else { 3689 mmu_idx = ARMMMUIdx_Stage1_E1; 3690 } 3691 break; 3692 default: 3693 g_assert_not_reached(); 3694 } 3695 break; 3696 case 2: 3697 /* stage 1 current state PL0: ATS1CUR, ATS1CUW */ 3698 switch (el) { 3699 case 3: 3700 mmu_idx = ARMMMUIdx_E30_0; 3701 break; 3702 case 2: 3703 g_assert(ss != ARMSS_Secure); /* ARMv8.4-SecEL2 is 64-bit only */ 3704 mmu_idx = ARMMMUIdx_Stage1_E0; 3705 break; 3706 case 1: 3707 mmu_idx = ARMMMUIdx_Stage1_E0; 3708 break; 3709 default: 3710 g_assert_not_reached(); 3711 } 3712 break; 3713 case 4: 3714 /* stage 1+2 NonSecure PL1: ATS12NSOPR, ATS12NSOPW */ 3715 mmu_idx = ARMMMUIdx_E10_1; 3716 ss = ARMSS_NonSecure; 3717 break; 3718 case 6: 3719 /* stage 1+2 NonSecure PL0: ATS12NSOUR, ATS12NSOUW */ 3720 mmu_idx = ARMMMUIdx_E10_0; 3721 ss = ARMSS_NonSecure; 3722 break; 3723 default: 3724 g_assert_not_reached(); 3725 } 3726 3727 par64 = do_ats_write(env, value, access_type, mmu_idx, ss); 3728 3729 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3730 #else 3731 /* Handled by hardware accelerator. */ 3732 g_assert_not_reached(); 3733 #endif /* CONFIG_TCG */ 3734 } 3735 3736 static void ats1h_write(CPUARMState *env, const ARMCPRegInfo *ri, 3737 uint64_t value) 3738 { 3739 #ifdef CONFIG_TCG 3740 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3741 uint64_t par64; 3742 3743 /* There is no SecureEL2 for AArch32. */ 3744 par64 = do_ats_write(env, value, access_type, ARMMMUIdx_E2, 3745 ARMSS_NonSecure); 3746 3747 A32_BANKED_CURRENT_REG_SET(env, par, par64); 3748 #else 3749 /* Handled by hardware accelerator. */ 3750 g_assert_not_reached(); 3751 #endif /* CONFIG_TCG */ 3752 } 3753 3754 static CPAccessResult at_e012_access(CPUARMState *env, const ARMCPRegInfo *ri, 3755 bool isread) 3756 { 3757 /* 3758 * R_NYXTL: instruction is UNDEFINED if it applies to an Exception level 3759 * lower than EL3 and the combination SCR_EL3.{NSE,NS} is reserved. This can 3760 * only happen when executing at EL3 because that combination also causes an 3761 * illegal exception return. We don't need to check FEAT_RME either, because 3762 * scr_write() ensures that the NSE bit is not set otherwise. 3763 */ 3764 if ((env->cp15.scr_el3 & (SCR_NSE | SCR_NS)) == SCR_NSE) { 3765 return CP_ACCESS_UNDEFINED; 3766 } 3767 return CP_ACCESS_OK; 3768 } 3769 3770 static CPAccessResult at_s1e2_access(CPUARMState *env, const ARMCPRegInfo *ri, 3771 bool isread) 3772 { 3773 if (arm_current_el(env) == 3 && 3774 !(env->cp15.scr_el3 & (SCR_NS | SCR_EEL2))) { 3775 return CP_ACCESS_UNDEFINED; 3776 } 3777 return at_e012_access(env, ri, isread); 3778 } 3779 3780 static CPAccessResult at_s1e01_access(CPUARMState *env, const ARMCPRegInfo *ri, 3781 bool isread) 3782 { 3783 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_AT)) { 3784 return CP_ACCESS_TRAP_EL2; 3785 } 3786 return at_e012_access(env, ri, isread); 3787 } 3788 3789 static void ats_write64(CPUARMState *env, const ARMCPRegInfo *ri, 3790 uint64_t value) 3791 { 3792 #ifdef CONFIG_TCG 3793 MMUAccessType access_type = ri->opc2 & 1 ? MMU_DATA_STORE : MMU_DATA_LOAD; 3794 ARMMMUIdx mmu_idx; 3795 uint64_t hcr_el2 = arm_hcr_el2_eff(env); 3796 bool regime_e20 = (hcr_el2 & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE); 3797 bool for_el3 = false; 3798 ARMSecuritySpace ss; 3799 3800 switch (ri->opc2 & 6) { 3801 case 0: 3802 switch (ri->opc1) { 3803 case 0: /* AT S1E1R, AT S1E1W, AT S1E1RP, AT S1E1WP */ 3804 if (ri->crm == 9 && arm_pan_enabled(env)) { 3805 mmu_idx = regime_e20 ? 3806 ARMMMUIdx_E20_2_PAN : ARMMMUIdx_Stage1_E1_PAN; 3807 } else { 3808 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_Stage1_E1; 3809 } 3810 break; 3811 case 4: /* AT S1E2R, AT S1E2W */ 3812 mmu_idx = hcr_el2 & HCR_E2H ? ARMMMUIdx_E20_2 : ARMMMUIdx_E2; 3813 break; 3814 case 6: /* AT S1E3R, AT S1E3W */ 3815 mmu_idx = ARMMMUIdx_E3; 3816 for_el3 = true; 3817 break; 3818 default: 3819 g_assert_not_reached(); 3820 } 3821 break; 3822 case 2: /* AT S1E0R, AT S1E0W */ 3823 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_Stage1_E0; 3824 break; 3825 case 4: /* AT S12E1R, AT S12E1W */ 3826 mmu_idx = regime_e20 ? ARMMMUIdx_E20_2 : ARMMMUIdx_E10_1; 3827 break; 3828 case 6: /* AT S12E0R, AT S12E0W */ 3829 mmu_idx = regime_e20 ? ARMMMUIdx_E20_0 : ARMMMUIdx_E10_0; 3830 break; 3831 default: 3832 g_assert_not_reached(); 3833 } 3834 3835 ss = for_el3 ? arm_security_space(env) : arm_security_space_below_el3(env); 3836 env->cp15.par_el[1] = do_ats_write(env, value, access_type, mmu_idx, ss); 3837 #else 3838 /* Handled by hardware accelerator. */ 3839 g_assert_not_reached(); 3840 #endif /* CONFIG_TCG */ 3841 } 3842 #endif 3843 3844 /* Return basic MPU access permission bits. */ 3845 static uint32_t simple_mpu_ap_bits(uint32_t val) 3846 { 3847 uint32_t ret; 3848 uint32_t mask; 3849 int i; 3850 ret = 0; 3851 mask = 3; 3852 for (i = 0; i < 16; i += 2) { 3853 ret |= (val >> i) & mask; 3854 mask <<= 2; 3855 } 3856 return ret; 3857 } 3858 3859 /* Pad basic MPU access permission bits to extended format. */ 3860 static uint32_t extended_mpu_ap_bits(uint32_t val) 3861 { 3862 uint32_t ret; 3863 uint32_t mask; 3864 int i; 3865 ret = 0; 3866 mask = 3; 3867 for (i = 0; i < 16; i += 2) { 3868 ret |= (val & mask) << i; 3869 mask <<= 2; 3870 } 3871 return ret; 3872 } 3873 3874 static void pmsav5_data_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3875 uint64_t value) 3876 { 3877 env->cp15.pmsav5_data_ap = extended_mpu_ap_bits(value); 3878 } 3879 3880 static uint64_t pmsav5_data_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3881 { 3882 return simple_mpu_ap_bits(env->cp15.pmsav5_data_ap); 3883 } 3884 3885 static void pmsav5_insn_ap_write(CPUARMState *env, const ARMCPRegInfo *ri, 3886 uint64_t value) 3887 { 3888 env->cp15.pmsav5_insn_ap = extended_mpu_ap_bits(value); 3889 } 3890 3891 static uint64_t pmsav5_insn_ap_read(CPUARMState *env, const ARMCPRegInfo *ri) 3892 { 3893 return simple_mpu_ap_bits(env->cp15.pmsav5_insn_ap); 3894 } 3895 3896 static uint64_t pmsav7_read(CPUARMState *env, const ARMCPRegInfo *ri) 3897 { 3898 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3899 3900 if (!u32p) { 3901 return 0; 3902 } 3903 3904 u32p += env->pmsav7.rnr[M_REG_NS]; 3905 return *u32p; 3906 } 3907 3908 static void pmsav7_write(CPUARMState *env, const ARMCPRegInfo *ri, 3909 uint64_t value) 3910 { 3911 ARMCPU *cpu = env_archcpu(env); 3912 uint32_t *u32p = *(uint32_t **)raw_ptr(env, ri); 3913 3914 if (!u32p) { 3915 return; 3916 } 3917 3918 u32p += env->pmsav7.rnr[M_REG_NS]; 3919 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3920 *u32p = value; 3921 } 3922 3923 static void pmsav7_rgnr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3924 uint64_t value) 3925 { 3926 ARMCPU *cpu = env_archcpu(env); 3927 uint32_t nrgs = cpu->pmsav7_dregion; 3928 3929 if (value >= nrgs) { 3930 qemu_log_mask(LOG_GUEST_ERROR, 3931 "PMSAv7 RGNR write >= # supported regions, %" PRIu32 3932 " > %" PRIu32 "\n", (uint32_t)value, nrgs); 3933 return; 3934 } 3935 3936 raw_write(env, ri, value); 3937 } 3938 3939 static void prbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3940 uint64_t value) 3941 { 3942 ARMCPU *cpu = env_archcpu(env); 3943 3944 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3945 env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value; 3946 } 3947 3948 static uint64_t prbar_read(CPUARMState *env, const ARMCPRegInfo *ri) 3949 { 3950 return env->pmsav8.rbar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]]; 3951 } 3952 3953 static void prlar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3954 uint64_t value) 3955 { 3956 ARMCPU *cpu = env_archcpu(env); 3957 3958 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3959 env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]] = value; 3960 } 3961 3962 static uint64_t prlar_read(CPUARMState *env, const ARMCPRegInfo *ri) 3963 { 3964 return env->pmsav8.rlar[M_REG_NS][env->pmsav7.rnr[M_REG_NS]]; 3965 } 3966 3967 static void prselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 3968 uint64_t value) 3969 { 3970 ARMCPU *cpu = env_archcpu(env); 3971 3972 /* 3973 * Ignore writes that would select not implemented region. 3974 * This is architecturally UNPREDICTABLE. 3975 */ 3976 if (value >= cpu->pmsav7_dregion) { 3977 return; 3978 } 3979 3980 env->pmsav7.rnr[M_REG_NS] = value; 3981 } 3982 3983 static void hprbar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3984 uint64_t value) 3985 { 3986 ARMCPU *cpu = env_archcpu(env); 3987 3988 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 3989 env->pmsav8.hprbar[env->pmsav8.hprselr] = value; 3990 } 3991 3992 static uint64_t hprbar_read(CPUARMState *env, const ARMCPRegInfo *ri) 3993 { 3994 return env->pmsav8.hprbar[env->pmsav8.hprselr]; 3995 } 3996 3997 static void hprlar_write(CPUARMState *env, const ARMCPRegInfo *ri, 3998 uint64_t value) 3999 { 4000 ARMCPU *cpu = env_archcpu(env); 4001 4002 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 4003 env->pmsav8.hprlar[env->pmsav8.hprselr] = value; 4004 } 4005 4006 static uint64_t hprlar_read(CPUARMState *env, const ARMCPRegInfo *ri) 4007 { 4008 return env->pmsav8.hprlar[env->pmsav8.hprselr]; 4009 } 4010 4011 static void hprenr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4012 uint64_t value) 4013 { 4014 uint32_t n; 4015 uint32_t bit; 4016 ARMCPU *cpu = env_archcpu(env); 4017 4018 /* Ignore writes to unimplemented regions */ 4019 int rmax = MIN(cpu->pmsav8r_hdregion, 32); 4020 value &= MAKE_64BIT_MASK(0, rmax); 4021 4022 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 4023 4024 /* Register alias is only valid for first 32 indexes */ 4025 for (n = 0; n < rmax; ++n) { 4026 bit = extract32(value, n, 1); 4027 env->pmsav8.hprlar[n] = deposit32( 4028 env->pmsav8.hprlar[n], 0, 1, bit); 4029 } 4030 } 4031 4032 static uint64_t hprenr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4033 { 4034 uint32_t n; 4035 uint32_t result = 0x0; 4036 ARMCPU *cpu = env_archcpu(env); 4037 4038 /* Register alias is only valid for first 32 indexes */ 4039 for (n = 0; n < MIN(cpu->pmsav8r_hdregion, 32); ++n) { 4040 if (env->pmsav8.hprlar[n] & 0x1) { 4041 result |= (0x1 << n); 4042 } 4043 } 4044 return result; 4045 } 4046 4047 static void hprselr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4048 uint64_t value) 4049 { 4050 ARMCPU *cpu = env_archcpu(env); 4051 4052 /* 4053 * Ignore writes that would select not implemented region. 4054 * This is architecturally UNPREDICTABLE. 4055 */ 4056 if (value >= cpu->pmsav8r_hdregion) { 4057 return; 4058 } 4059 4060 env->pmsav8.hprselr = value; 4061 } 4062 4063 static void pmsav8r_regn_write(CPUARMState *env, const ARMCPRegInfo *ri, 4064 uint64_t value) 4065 { 4066 ARMCPU *cpu = env_archcpu(env); 4067 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) | 4068 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1); 4069 4070 tlb_flush(CPU(cpu)); /* Mappings may have changed - purge! */ 4071 4072 if (ri->opc1 & 4) { 4073 if (index >= cpu->pmsav8r_hdregion) { 4074 return; 4075 } 4076 if (ri->opc2 & 0x1) { 4077 env->pmsav8.hprlar[index] = value; 4078 } else { 4079 env->pmsav8.hprbar[index] = value; 4080 } 4081 } else { 4082 if (index >= cpu->pmsav7_dregion) { 4083 return; 4084 } 4085 if (ri->opc2 & 0x1) { 4086 env->pmsav8.rlar[M_REG_NS][index] = value; 4087 } else { 4088 env->pmsav8.rbar[M_REG_NS][index] = value; 4089 } 4090 } 4091 } 4092 4093 static uint64_t pmsav8r_regn_read(CPUARMState *env, const ARMCPRegInfo *ri) 4094 { 4095 ARMCPU *cpu = env_archcpu(env); 4096 uint8_t index = (extract32(ri->opc0, 0, 1) << 4) | 4097 (extract32(ri->crm, 0, 3) << 1) | extract32(ri->opc2, 2, 1); 4098 4099 if (ri->opc1 & 4) { 4100 if (index >= cpu->pmsav8r_hdregion) { 4101 return 0x0; 4102 } 4103 if (ri->opc2 & 0x1) { 4104 return env->pmsav8.hprlar[index]; 4105 } else { 4106 return env->pmsav8.hprbar[index]; 4107 } 4108 } else { 4109 if (index >= cpu->pmsav7_dregion) { 4110 return 0x0; 4111 } 4112 if (ri->opc2 & 0x1) { 4113 return env->pmsav8.rlar[M_REG_NS][index]; 4114 } else { 4115 return env->pmsav8.rbar[M_REG_NS][index]; 4116 } 4117 } 4118 } 4119 4120 static const ARMCPRegInfo pmsav8r_cp_reginfo[] = { 4121 { .name = "PRBAR", 4122 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 0, 4123 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4124 .accessfn = access_tvm_trvm, 4125 .readfn = prbar_read, .writefn = prbar_write }, 4126 { .name = "PRLAR", 4127 .cp = 15, .opc1 = 0, .crn = 6, .crm = 3, .opc2 = 1, 4128 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4129 .accessfn = access_tvm_trvm, 4130 .readfn = prlar_read, .writefn = prlar_write }, 4131 { .name = "PRSELR", .resetvalue = 0, 4132 .cp = 15, .opc1 = 0, .crn = 6, .crm = 2, .opc2 = 1, 4133 .access = PL1_RW, .accessfn = access_tvm_trvm, 4134 .writefn = prselr_write, 4135 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]) }, 4136 { .name = "HPRBAR", .resetvalue = 0, 4137 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 0, 4138 .access = PL2_RW, .type = ARM_CP_NO_RAW, 4139 .readfn = hprbar_read, .writefn = hprbar_write }, 4140 { .name = "HPRLAR", 4141 .cp = 15, .opc1 = 4, .crn = 6, .crm = 3, .opc2 = 1, 4142 .access = PL2_RW, .type = ARM_CP_NO_RAW, 4143 .readfn = hprlar_read, .writefn = hprlar_write }, 4144 { .name = "HPRSELR", .resetvalue = 0, 4145 .cp = 15, .opc1 = 4, .crn = 6, .crm = 2, .opc2 = 1, 4146 .access = PL2_RW, 4147 .writefn = hprselr_write, 4148 .fieldoffset = offsetof(CPUARMState, pmsav8.hprselr) }, 4149 { .name = "HPRENR", 4150 .cp = 15, .opc1 = 4, .crn = 6, .crm = 1, .opc2 = 1, 4151 .access = PL2_RW, .type = ARM_CP_NO_RAW, 4152 .readfn = hprenr_read, .writefn = hprenr_write }, 4153 }; 4154 4155 static const ARMCPRegInfo pmsav7_cp_reginfo[] = { 4156 /* 4157 * Reset for all these registers is handled in arm_cpu_reset(), 4158 * because the PMSAv7 is also used by M-profile CPUs, which do 4159 * not register cpregs but still need the state to be reset. 4160 */ 4161 { .name = "DRBAR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 0, 4162 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4163 .fieldoffset = offsetof(CPUARMState, pmsav7.drbar), 4164 .readfn = pmsav7_read, .writefn = pmsav7_write, 4165 .resetfn = arm_cp_reset_ignore }, 4166 { .name = "DRSR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 2, 4167 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4168 .fieldoffset = offsetof(CPUARMState, pmsav7.drsr), 4169 .readfn = pmsav7_read, .writefn = pmsav7_write, 4170 .resetfn = arm_cp_reset_ignore }, 4171 { .name = "DRACR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 1, .opc2 = 4, 4172 .access = PL1_RW, .type = ARM_CP_NO_RAW, 4173 .fieldoffset = offsetof(CPUARMState, pmsav7.dracr), 4174 .readfn = pmsav7_read, .writefn = pmsav7_write, 4175 .resetfn = arm_cp_reset_ignore }, 4176 { .name = "RGNR", .cp = 15, .crn = 6, .opc1 = 0, .crm = 2, .opc2 = 0, 4177 .access = PL1_RW, 4178 .fieldoffset = offsetof(CPUARMState, pmsav7.rnr[M_REG_NS]), 4179 .writefn = pmsav7_rgnr_write, 4180 .resetfn = arm_cp_reset_ignore }, 4181 }; 4182 4183 static const ARMCPRegInfo pmsav5_cp_reginfo[] = { 4184 { .name = "DATA_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 4185 .access = PL1_RW, .type = ARM_CP_ALIAS, 4186 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 4187 .readfn = pmsav5_data_ap_read, .writefn = pmsav5_data_ap_write, }, 4188 { .name = "INSN_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 4189 .access = PL1_RW, .type = ARM_CP_ALIAS, 4190 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 4191 .readfn = pmsav5_insn_ap_read, .writefn = pmsav5_insn_ap_write, }, 4192 { .name = "DATA_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 2, 4193 .access = PL1_RW, 4194 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_data_ap), 4195 .resetvalue = 0, }, 4196 { .name = "INSN_EXT_AP", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 3, 4197 .access = PL1_RW, 4198 .fieldoffset = offsetof(CPUARMState, cp15.pmsav5_insn_ap), 4199 .resetvalue = 0, }, 4200 { .name = "DCACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 0, 4201 .access = PL1_RW, 4202 .fieldoffset = offsetof(CPUARMState, cp15.c2_data), .resetvalue = 0, }, 4203 { .name = "ICACHE_CFG", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 1, 4204 .access = PL1_RW, 4205 .fieldoffset = offsetof(CPUARMState, cp15.c2_insn), .resetvalue = 0, }, 4206 /* Protection region base and size registers */ 4207 { .name = "946_PRBS0", .cp = 15, .crn = 6, .crm = 0, .opc1 = 0, 4208 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4209 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[0]) }, 4210 { .name = "946_PRBS1", .cp = 15, .crn = 6, .crm = 1, .opc1 = 0, 4211 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4212 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[1]) }, 4213 { .name = "946_PRBS2", .cp = 15, .crn = 6, .crm = 2, .opc1 = 0, 4214 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4215 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[2]) }, 4216 { .name = "946_PRBS3", .cp = 15, .crn = 6, .crm = 3, .opc1 = 0, 4217 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4218 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[3]) }, 4219 { .name = "946_PRBS4", .cp = 15, .crn = 6, .crm = 4, .opc1 = 0, 4220 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4221 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[4]) }, 4222 { .name = "946_PRBS5", .cp = 15, .crn = 6, .crm = 5, .opc1 = 0, 4223 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4224 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[5]) }, 4225 { .name = "946_PRBS6", .cp = 15, .crn = 6, .crm = 6, .opc1 = 0, 4226 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4227 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[6]) }, 4228 { .name = "946_PRBS7", .cp = 15, .crn = 6, .crm = 7, .opc1 = 0, 4229 .opc2 = CP_ANY, .access = PL1_RW, .resetvalue = 0, 4230 .fieldoffset = offsetof(CPUARMState, cp15.c6_region[7]) }, 4231 }; 4232 4233 static void vmsa_ttbcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4234 uint64_t value) 4235 { 4236 ARMCPU *cpu = env_archcpu(env); 4237 4238 if (!arm_feature(env, ARM_FEATURE_V8)) { 4239 if (arm_feature(env, ARM_FEATURE_LPAE) && (value & TTBCR_EAE)) { 4240 /* 4241 * Pre ARMv8 bits [21:19], [15:14] and [6:3] are UNK/SBZP when 4242 * using Long-descriptor translation table format 4243 */ 4244 value &= ~((7 << 19) | (3 << 14) | (0xf << 3)); 4245 } else if (arm_feature(env, ARM_FEATURE_EL3)) { 4246 /* 4247 * In an implementation that includes the Security Extensions 4248 * TTBCR has additional fields PD0 [4] and PD1 [5] for 4249 * Short-descriptor translation table format. 4250 */ 4251 value &= TTBCR_PD1 | TTBCR_PD0 | TTBCR_N; 4252 } else { 4253 value &= TTBCR_N; 4254 } 4255 } 4256 4257 if (arm_feature(env, ARM_FEATURE_LPAE)) { 4258 /* 4259 * With LPAE the TTBCR could result in a change of ASID 4260 * via the TTBCR.A1 bit, so do a TLB flush. 4261 */ 4262 tlb_flush(CPU(cpu)); 4263 } 4264 raw_write(env, ri, value); 4265 } 4266 4267 static void vmsa_tcr_el12_write(CPUARMState *env, const ARMCPRegInfo *ri, 4268 uint64_t value) 4269 { 4270 ARMCPU *cpu = env_archcpu(env); 4271 4272 /* For AArch64 the A1 bit could result in a change of ASID, so TLB flush. */ 4273 tlb_flush(CPU(cpu)); 4274 raw_write(env, ri, value); 4275 } 4276 4277 static void vmsa_ttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4278 uint64_t value) 4279 { 4280 /* If the ASID changes (with a 64-bit write), we must flush the TLB. */ 4281 if (cpreg_field_is_64bit(ri) && 4282 extract64(raw_read(env, ri) ^ value, 48, 16) != 0) { 4283 ARMCPU *cpu = env_archcpu(env); 4284 tlb_flush(CPU(cpu)); 4285 } 4286 raw_write(env, ri, value); 4287 } 4288 4289 static void vmsa_tcr_ttbr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 4290 uint64_t value) 4291 { 4292 /* 4293 * If we are running with E2&0 regime, then an ASID is active. 4294 * Flush if that might be changing. Note we're not checking 4295 * TCR_EL2.A1 to know if this is really the TTBRx_EL2 that 4296 * holds the active ASID, only checking the field that might. 4297 */ 4298 if (extract64(raw_read(env, ri) ^ value, 48, 16) && 4299 (arm_hcr_el2_eff(env) & HCR_E2H)) { 4300 uint16_t mask = ARMMMUIdxBit_E20_2 | 4301 ARMMMUIdxBit_E20_2_PAN | 4302 ARMMMUIdxBit_E20_0; 4303 tlb_flush_by_mmuidx(env_cpu(env), mask); 4304 } 4305 raw_write(env, ri, value); 4306 } 4307 4308 static void vttbr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4309 uint64_t value) 4310 { 4311 ARMCPU *cpu = env_archcpu(env); 4312 CPUState *cs = CPU(cpu); 4313 4314 /* 4315 * A change in VMID to the stage2 page table (Stage2) invalidates 4316 * the stage2 and combined stage 1&2 tlbs (EL10_1 and EL10_0). 4317 */ 4318 if (extract64(raw_read(env, ri) ^ value, 48, 16) != 0) { 4319 tlb_flush_by_mmuidx(cs, alle1_tlbmask(env)); 4320 } 4321 raw_write(env, ri, value); 4322 } 4323 4324 static const ARMCPRegInfo vmsa_pmsa_cp_reginfo[] = { 4325 { .name = "DFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 0, 4326 .access = PL1_RW, .accessfn = access_tvm_trvm, .type = ARM_CP_ALIAS, 4327 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dfsr_s), 4328 offsetoflow32(CPUARMState, cp15.dfsr_ns) }, }, 4329 { .name = "IFSR", .cp = 15, .crn = 5, .crm = 0, .opc1 = 0, .opc2 = 1, 4330 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 4331 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.ifsr_s), 4332 offsetoflow32(CPUARMState, cp15.ifsr_ns) } }, 4333 { .name = "DFAR", .cp = 15, .opc1 = 0, .crn = 6, .crm = 0, .opc2 = 0, 4334 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 4335 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.dfar_s), 4336 offsetof(CPUARMState, cp15.dfar_ns) } }, 4337 { .name = "FAR_EL1", .state = ARM_CP_STATE_AA64, 4338 .opc0 = 3, .crn = 6, .crm = 0, .opc1 = 0, .opc2 = 0, 4339 .access = PL1_RW, .accessfn = access_tvm_trvm, 4340 .fgt = FGT_FAR_EL1, 4341 .nv2_redirect_offset = 0x220 | NV2_REDIR_NV1, 4342 .fieldoffset = offsetof(CPUARMState, cp15.far_el[1]), 4343 .resetvalue = 0, }, 4344 }; 4345 4346 static const ARMCPRegInfo vmsa_cp_reginfo[] = { 4347 { .name = "ESR_EL1", .state = ARM_CP_STATE_AA64, 4348 .opc0 = 3, .crn = 5, .crm = 2, .opc1 = 0, .opc2 = 0, 4349 .access = PL1_RW, .accessfn = access_tvm_trvm, 4350 .fgt = FGT_ESR_EL1, 4351 .nv2_redirect_offset = 0x138 | NV2_REDIR_NV1, 4352 .fieldoffset = offsetof(CPUARMState, cp15.esr_el[1]), .resetvalue = 0, }, 4353 { .name = "TTBR0_EL1", .state = ARM_CP_STATE_BOTH, 4354 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 0, 4355 .access = PL1_RW, .accessfn = access_tvm_trvm, 4356 .fgt = FGT_TTBR0_EL1, 4357 .nv2_redirect_offset = 0x200 | NV2_REDIR_NV1, 4358 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write, 4359 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 4360 offsetof(CPUARMState, cp15.ttbr0_ns) } }, 4361 { .name = "TTBR1_EL1", .state = ARM_CP_STATE_BOTH, 4362 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 1, 4363 .access = PL1_RW, .accessfn = access_tvm_trvm, 4364 .fgt = FGT_TTBR1_EL1, 4365 .nv2_redirect_offset = 0x210 | NV2_REDIR_NV1, 4366 .writefn = vmsa_ttbr_write, .resetvalue = 0, .raw_writefn = raw_write, 4367 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 4368 offsetof(CPUARMState, cp15.ttbr1_ns) } }, 4369 { .name = "TCR_EL1", .state = ARM_CP_STATE_AA64, 4370 .opc0 = 3, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 4371 .access = PL1_RW, .accessfn = access_tvm_trvm, 4372 .fgt = FGT_TCR_EL1, 4373 .nv2_redirect_offset = 0x120 | NV2_REDIR_NV1, 4374 .writefn = vmsa_tcr_el12_write, 4375 .raw_writefn = raw_write, 4376 .resetvalue = 0, 4377 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[1]) }, 4378 { .name = "TTBCR", .cp = 15, .crn = 2, .crm = 0, .opc1 = 0, .opc2 = 2, 4379 .access = PL1_RW, .accessfn = access_tvm_trvm, 4380 .type = ARM_CP_ALIAS, .writefn = vmsa_ttbcr_write, 4381 .raw_writefn = raw_write, 4382 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.tcr_el[3]), 4383 offsetoflow32(CPUARMState, cp15.tcr_el[1])} }, 4384 }; 4385 4386 /* 4387 * Note that unlike TTBCR, writing to TTBCR2 does not require flushing 4388 * qemu tlbs nor adjusting cached masks. 4389 */ 4390 static const ARMCPRegInfo ttbcr2_reginfo = { 4391 .name = "TTBCR2", .cp = 15, .opc1 = 0, .crn = 2, .crm = 0, .opc2 = 3, 4392 .access = PL1_RW, .accessfn = access_tvm_trvm, 4393 .type = ARM_CP_ALIAS, 4394 .bank_fieldoffsets = { 4395 offsetofhigh32(CPUARMState, cp15.tcr_el[3]), 4396 offsetofhigh32(CPUARMState, cp15.tcr_el[1]), 4397 }, 4398 }; 4399 4400 static void omap_ticonfig_write(CPUARMState *env, const ARMCPRegInfo *ri, 4401 uint64_t value) 4402 { 4403 env->cp15.c15_ticonfig = value & 0xe7; 4404 /* The OS_TYPE bit in this register changes the reported CPUID! */ 4405 env->cp15.c0_cpuid = (value & (1 << 5)) ? 4406 ARM_CPUID_TI915T : ARM_CPUID_TI925T; 4407 } 4408 4409 static void omap_threadid_write(CPUARMState *env, const ARMCPRegInfo *ri, 4410 uint64_t value) 4411 { 4412 env->cp15.c15_threadid = value & 0xffff; 4413 } 4414 4415 static void omap_wfi_write(CPUARMState *env, const ARMCPRegInfo *ri, 4416 uint64_t value) 4417 { 4418 /* Wait-for-interrupt (deprecated) */ 4419 cpu_interrupt(env_cpu(env), CPU_INTERRUPT_HALT); 4420 } 4421 4422 static void omap_cachemaint_write(CPUARMState *env, const ARMCPRegInfo *ri, 4423 uint64_t value) 4424 { 4425 /* 4426 * On OMAP there are registers indicating the max/min index of dcache lines 4427 * containing a dirty line; cache flush operations have to reset these. 4428 */ 4429 env->cp15.c15_i_max = 0x000; 4430 env->cp15.c15_i_min = 0xff0; 4431 } 4432 4433 static const ARMCPRegInfo omap_cp_reginfo[] = { 4434 { .name = "DFSR", .cp = 15, .crn = 5, .crm = CP_ANY, 4435 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, .type = ARM_CP_OVERRIDE, 4436 .fieldoffset = offsetoflow32(CPUARMState, cp15.esr_el[1]), 4437 .resetvalue = 0, }, 4438 { .name = "", .cp = 15, .crn = 15, .crm = 0, .opc1 = 0, .opc2 = 0, 4439 .access = PL1_RW, .type = ARM_CP_NOP }, 4440 { .name = "TICONFIG", .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, 4441 .access = PL1_RW, 4442 .fieldoffset = offsetof(CPUARMState, cp15.c15_ticonfig), .resetvalue = 0, 4443 .writefn = omap_ticonfig_write }, 4444 { .name = "IMAX", .cp = 15, .crn = 15, .crm = 2, .opc1 = 0, .opc2 = 0, 4445 .access = PL1_RW, 4446 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_max), .resetvalue = 0, }, 4447 { .name = "IMIN", .cp = 15, .crn = 15, .crm = 3, .opc1 = 0, .opc2 = 0, 4448 .access = PL1_RW, .resetvalue = 0xff0, 4449 .fieldoffset = offsetof(CPUARMState, cp15.c15_i_min) }, 4450 { .name = "THREADID", .cp = 15, .crn = 15, .crm = 4, .opc1 = 0, .opc2 = 0, 4451 .access = PL1_RW, 4452 .fieldoffset = offsetof(CPUARMState, cp15.c15_threadid), .resetvalue = 0, 4453 .writefn = omap_threadid_write }, 4454 { .name = "TI925T_STATUS", .cp = 15, .crn = 15, 4455 .crm = 8, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 4456 .type = ARM_CP_NO_RAW, 4457 .readfn = arm_cp_read_zero, .writefn = omap_wfi_write, }, 4458 /* 4459 * TODO: Peripheral port remap register: 4460 * On OMAP2 mcr p15, 0, rn, c15, c2, 4 sets up the interrupt controller 4461 * base address at $rn & ~0xfff and map size of 0x200 << ($rn & 0xfff), 4462 * when MMU is off. 4463 */ 4464 { .name = "OMAP_CACHEMAINT", .cp = 15, .crn = 7, .crm = CP_ANY, 4465 .opc1 = 0, .opc2 = CP_ANY, .access = PL1_W, 4466 .type = ARM_CP_OVERRIDE | ARM_CP_NO_RAW, 4467 .writefn = omap_cachemaint_write }, 4468 { .name = "C9", .cp = 15, .crn = 9, 4469 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_RW, 4470 .type = ARM_CP_CONST | ARM_CP_OVERRIDE, .resetvalue = 0 }, 4471 }; 4472 4473 static void xscale_cpar_write(CPUARMState *env, const ARMCPRegInfo *ri, 4474 uint64_t value) 4475 { 4476 env->cp15.c15_cpar = value & 0x3fff; 4477 } 4478 4479 static const ARMCPRegInfo xscale_cp_reginfo[] = { 4480 { .name = "XSCALE_CPAR", 4481 .cp = 15, .crn = 15, .crm = 1, .opc1 = 0, .opc2 = 0, .access = PL1_RW, 4482 .fieldoffset = offsetof(CPUARMState, cp15.c15_cpar), .resetvalue = 0, 4483 .writefn = xscale_cpar_write, }, 4484 { .name = "XSCALE_AUXCR", 4485 .cp = 15, .crn = 1, .crm = 0, .opc1 = 0, .opc2 = 1, .access = PL1_RW, 4486 .fieldoffset = offsetof(CPUARMState, cp15.c1_xscaleauxcr), 4487 .resetvalue = 0, }, 4488 /* 4489 * XScale specific cache-lockdown: since we have no cache we NOP these 4490 * and hope the guest does not really rely on cache behaviour. 4491 */ 4492 { .name = "XSCALE_LOCK_ICACHE_LINE", 4493 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 0, 4494 .access = PL1_W, .type = ARM_CP_NOP }, 4495 { .name = "XSCALE_UNLOCK_ICACHE", 4496 .cp = 15, .opc1 = 0, .crn = 9, .crm = 1, .opc2 = 1, 4497 .access = PL1_W, .type = ARM_CP_NOP }, 4498 { .name = "XSCALE_DCACHE_LOCK", 4499 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 0, 4500 .access = PL1_RW, .type = ARM_CP_NOP }, 4501 { .name = "XSCALE_UNLOCK_DCACHE", 4502 .cp = 15, .opc1 = 0, .crn = 9, .crm = 2, .opc2 = 1, 4503 .access = PL1_W, .type = ARM_CP_NOP }, 4504 }; 4505 4506 static const ARMCPRegInfo dummy_c15_cp_reginfo[] = { 4507 /* 4508 * RAZ/WI the whole crn=15 space, when we don't have a more specific 4509 * implementation of this implementation-defined space. 4510 * Ideally this should eventually disappear in favour of actually 4511 * implementing the correct behaviour for all cores. 4512 */ 4513 { .name = "C15_IMPDEF", .cp = 15, .crn = 15, 4514 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 4515 .access = PL1_RW, 4516 .type = ARM_CP_CONST | ARM_CP_NO_RAW | ARM_CP_OVERRIDE, 4517 .resetvalue = 0 }, 4518 }; 4519 4520 static const ARMCPRegInfo cache_dirty_status_cp_reginfo[] = { 4521 /* Cache status: RAZ because we have no cache so it's always clean */ 4522 { .name = "CDSR", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 6, 4523 .access = PL1_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4524 .resetvalue = 0 }, 4525 }; 4526 4527 static const ARMCPRegInfo cache_block_ops_cp_reginfo[] = { 4528 /* We never have a block transfer operation in progress */ 4529 { .name = "BXSR", .cp = 15, .crn = 7, .crm = 12, .opc1 = 0, .opc2 = 4, 4530 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4531 .resetvalue = 0 }, 4532 /* The cache ops themselves: these all NOP for QEMU */ 4533 { .name = "IICR", .cp = 15, .crm = 5, .opc1 = 0, 4534 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4535 { .name = "IDCR", .cp = 15, .crm = 6, .opc1 = 0, 4536 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4537 { .name = "CDCR", .cp = 15, .crm = 12, .opc1 = 0, 4538 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4539 { .name = "PIR", .cp = 15, .crm = 12, .opc1 = 1, 4540 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4541 { .name = "PDR", .cp = 15, .crm = 12, .opc1 = 2, 4542 .access = PL0_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4543 { .name = "CIDCR", .cp = 15, .crm = 14, .opc1 = 0, 4544 .access = PL1_W, .type = ARM_CP_NOP | ARM_CP_64BIT }, 4545 }; 4546 4547 static const ARMCPRegInfo cache_test_clean_cp_reginfo[] = { 4548 /* 4549 * The cache test-and-clean instructions always return (1 << 30) 4550 * to indicate that there are no dirty cache lines. 4551 */ 4552 { .name = "TC_DCACHE", .cp = 15, .crn = 7, .crm = 10, .opc1 = 0, .opc2 = 3, 4553 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4554 .resetvalue = (1 << 30) }, 4555 { .name = "TCI_DCACHE", .cp = 15, .crn = 7, .crm = 14, .opc1 = 0, .opc2 = 3, 4556 .access = PL0_R, .type = ARM_CP_CONST | ARM_CP_NO_RAW, 4557 .resetvalue = (1 << 30) }, 4558 }; 4559 4560 static const ARMCPRegInfo strongarm_cp_reginfo[] = { 4561 /* Ignore ReadBuffer accesses */ 4562 { .name = "C9_READBUFFER", .cp = 15, .crn = 9, 4563 .crm = CP_ANY, .opc1 = CP_ANY, .opc2 = CP_ANY, 4564 .access = PL1_RW, .resetvalue = 0, 4565 .type = ARM_CP_CONST | ARM_CP_OVERRIDE | ARM_CP_NO_RAW }, 4566 }; 4567 4568 static uint64_t midr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4569 { 4570 unsigned int cur_el = arm_current_el(env); 4571 4572 if (arm_is_el2_enabled(env) && cur_el == 1) { 4573 return env->cp15.vpidr_el2; 4574 } 4575 return raw_read(env, ri); 4576 } 4577 4578 static uint64_t mpidr_read_val(CPUARMState *env) 4579 { 4580 ARMCPU *cpu = env_archcpu(env); 4581 uint64_t mpidr = cpu->mp_affinity; 4582 4583 if (arm_feature(env, ARM_FEATURE_V7MP)) { 4584 mpidr |= (1U << 31); 4585 /* 4586 * Cores which are uniprocessor (non-coherent) 4587 * but still implement the MP extensions set 4588 * bit 30. (For instance, Cortex-R5). 4589 */ 4590 if (cpu->mp_is_up) { 4591 mpidr |= (1u << 30); 4592 } 4593 } 4594 return mpidr; 4595 } 4596 4597 static uint64_t mpidr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4598 { 4599 unsigned int cur_el = arm_current_el(env); 4600 4601 if (arm_is_el2_enabled(env) && cur_el == 1) { 4602 return env->cp15.vmpidr_el2; 4603 } 4604 return mpidr_read_val(env); 4605 } 4606 4607 static const ARMCPRegInfo lpae_cp_reginfo[] = { 4608 /* NOP AMAIR0/1 */ 4609 { .name = "AMAIR0", .state = ARM_CP_STATE_BOTH, 4610 .opc0 = 3, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 0, 4611 .access = PL1_RW, .accessfn = access_tvm_trvm, 4612 .fgt = FGT_AMAIR_EL1, 4613 .nv2_redirect_offset = 0x148 | NV2_REDIR_NV1, 4614 .type = ARM_CP_CONST, .resetvalue = 0 }, 4615 /* AMAIR1 is mapped to AMAIR_EL1[63:32] */ 4616 { .name = "AMAIR1", .cp = 15, .crn = 10, .crm = 3, .opc1 = 0, .opc2 = 1, 4617 .access = PL1_RW, .accessfn = access_tvm_trvm, 4618 .type = ARM_CP_CONST, .resetvalue = 0 }, 4619 { .name = "PAR", .cp = 15, .crm = 7, .opc1 = 0, 4620 .access = PL1_RW, .type = ARM_CP_64BIT, .resetvalue = 0, 4621 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.par_s), 4622 offsetof(CPUARMState, cp15.par_ns)} }, 4623 { .name = "TTBR0", .cp = 15, .crm = 2, .opc1 = 0, 4624 .access = PL1_RW, .accessfn = access_tvm_trvm, 4625 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4626 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr0_s), 4627 offsetof(CPUARMState, cp15.ttbr0_ns) }, 4628 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write }, 4629 { .name = "TTBR1", .cp = 15, .crm = 2, .opc1 = 1, 4630 .access = PL1_RW, .accessfn = access_tvm_trvm, 4631 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 4632 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.ttbr1_s), 4633 offsetof(CPUARMState, cp15.ttbr1_ns) }, 4634 .writefn = vmsa_ttbr_write, .raw_writefn = raw_write }, 4635 }; 4636 4637 static uint64_t aa64_fpcr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4638 { 4639 return vfp_get_fpcr(env); 4640 } 4641 4642 static void aa64_fpcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4643 uint64_t value) 4644 { 4645 vfp_set_fpcr(env, value); 4646 } 4647 4648 static uint64_t aa64_fpsr_read(CPUARMState *env, const ARMCPRegInfo *ri) 4649 { 4650 return vfp_get_fpsr(env); 4651 } 4652 4653 static void aa64_fpsr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4654 uint64_t value) 4655 { 4656 vfp_set_fpsr(env, value); 4657 } 4658 4659 static CPAccessResult aa64_daif_access(CPUARMState *env, const ARMCPRegInfo *ri, 4660 bool isread) 4661 { 4662 if (arm_current_el(env) == 0 && !(arm_sctlr(env, 0) & SCTLR_UMA)) { 4663 return CP_ACCESS_TRAP_EL1; 4664 } 4665 return CP_ACCESS_OK; 4666 } 4667 4668 static void aa64_daif_write(CPUARMState *env, const ARMCPRegInfo *ri, 4669 uint64_t value) 4670 { 4671 env->daif = value & PSTATE_DAIF; 4672 } 4673 4674 static uint64_t aa64_pan_read(CPUARMState *env, const ARMCPRegInfo *ri) 4675 { 4676 return env->pstate & PSTATE_PAN; 4677 } 4678 4679 static void aa64_pan_write(CPUARMState *env, const ARMCPRegInfo *ri, 4680 uint64_t value) 4681 { 4682 env->pstate = (env->pstate & ~PSTATE_PAN) | (value & PSTATE_PAN); 4683 } 4684 4685 static const ARMCPRegInfo pan_reginfo = { 4686 .name = "PAN", .state = ARM_CP_STATE_AA64, 4687 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 3, 4688 .type = ARM_CP_NO_RAW, .access = PL1_RW, 4689 .readfn = aa64_pan_read, .writefn = aa64_pan_write 4690 }; 4691 4692 static uint64_t aa64_uao_read(CPUARMState *env, const ARMCPRegInfo *ri) 4693 { 4694 return env->pstate & PSTATE_UAO; 4695 } 4696 4697 static void aa64_uao_write(CPUARMState *env, const ARMCPRegInfo *ri, 4698 uint64_t value) 4699 { 4700 env->pstate = (env->pstate & ~PSTATE_UAO) | (value & PSTATE_UAO); 4701 } 4702 4703 static const ARMCPRegInfo uao_reginfo = { 4704 .name = "UAO", .state = ARM_CP_STATE_AA64, 4705 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 4, 4706 .type = ARM_CP_NO_RAW, .access = PL1_RW, 4707 .readfn = aa64_uao_read, .writefn = aa64_uao_write 4708 }; 4709 4710 static uint64_t aa64_dit_read(CPUARMState *env, const ARMCPRegInfo *ri) 4711 { 4712 return env->pstate & PSTATE_DIT; 4713 } 4714 4715 static void aa64_dit_write(CPUARMState *env, const ARMCPRegInfo *ri, 4716 uint64_t value) 4717 { 4718 env->pstate = (env->pstate & ~PSTATE_DIT) | (value & PSTATE_DIT); 4719 } 4720 4721 static const ARMCPRegInfo dit_reginfo = { 4722 .name = "DIT", .state = ARM_CP_STATE_AA64, 4723 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 5, 4724 .type = ARM_CP_NO_RAW, .access = PL0_RW, 4725 .readfn = aa64_dit_read, .writefn = aa64_dit_write 4726 }; 4727 4728 static uint64_t aa64_ssbs_read(CPUARMState *env, const ARMCPRegInfo *ri) 4729 { 4730 return env->pstate & PSTATE_SSBS; 4731 } 4732 4733 static void aa64_ssbs_write(CPUARMState *env, const ARMCPRegInfo *ri, 4734 uint64_t value) 4735 { 4736 env->pstate = (env->pstate & ~PSTATE_SSBS) | (value & PSTATE_SSBS); 4737 } 4738 4739 static const ARMCPRegInfo ssbs_reginfo = { 4740 .name = "SSBS", .state = ARM_CP_STATE_AA64, 4741 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 6, 4742 .type = ARM_CP_NO_RAW, .access = PL0_RW, 4743 .readfn = aa64_ssbs_read, .writefn = aa64_ssbs_write 4744 }; 4745 4746 static CPAccessResult aa64_cacheop_poc_access(CPUARMState *env, 4747 const ARMCPRegInfo *ri, 4748 bool isread) 4749 { 4750 /* Cache invalidate/clean to Point of Coherency or Persistence... */ 4751 switch (arm_current_el(env)) { 4752 case 0: 4753 /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set. */ 4754 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) { 4755 return CP_ACCESS_TRAP_EL1; 4756 } 4757 /* fall through */ 4758 case 1: 4759 /* ... EL1 must trap to EL2 if HCR_EL2.TPCP is set. */ 4760 if (arm_hcr_el2_eff(env) & HCR_TPCP) { 4761 return CP_ACCESS_TRAP_EL2; 4762 } 4763 break; 4764 } 4765 return CP_ACCESS_OK; 4766 } 4767 4768 static CPAccessResult do_cacheop_pou_access(CPUARMState *env, uint64_t hcrflags) 4769 { 4770 /* Cache invalidate/clean to Point of Unification... */ 4771 switch (arm_current_el(env)) { 4772 case 0: 4773 /* ... EL0 must trap to EL1 unless SCTLR_EL1.UCI is set. */ 4774 if (!(arm_sctlr(env, 0) & SCTLR_UCI)) { 4775 return CP_ACCESS_TRAP_EL1; 4776 } 4777 /* fall through */ 4778 case 1: 4779 /* ... EL1 must trap to EL2 if relevant HCR_EL2 flags are set. */ 4780 if (arm_hcr_el2_eff(env) & hcrflags) { 4781 return CP_ACCESS_TRAP_EL2; 4782 } 4783 break; 4784 } 4785 return CP_ACCESS_OK; 4786 } 4787 4788 static CPAccessResult access_ticab(CPUARMState *env, const ARMCPRegInfo *ri, 4789 bool isread) 4790 { 4791 return do_cacheop_pou_access(env, HCR_TICAB | HCR_TPU); 4792 } 4793 4794 static CPAccessResult access_tocu(CPUARMState *env, const ARMCPRegInfo *ri, 4795 bool isread) 4796 { 4797 return do_cacheop_pou_access(env, HCR_TOCU | HCR_TPU); 4798 } 4799 4800 static CPAccessResult aa64_zva_access(CPUARMState *env, const ARMCPRegInfo *ri, 4801 bool isread) 4802 { 4803 int cur_el = arm_current_el(env); 4804 4805 if (cur_el < 2) { 4806 uint64_t hcr = arm_hcr_el2_eff(env); 4807 4808 if (cur_el == 0) { 4809 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 4810 if (!(env->cp15.sctlr_el[2] & SCTLR_DZE)) { 4811 return CP_ACCESS_TRAP_EL2; 4812 } 4813 } else { 4814 if (!(env->cp15.sctlr_el[1] & SCTLR_DZE)) { 4815 return CP_ACCESS_TRAP_EL1; 4816 } 4817 if (hcr & HCR_TDZ) { 4818 return CP_ACCESS_TRAP_EL2; 4819 } 4820 } 4821 } else if (hcr & HCR_TDZ) { 4822 return CP_ACCESS_TRAP_EL2; 4823 } 4824 } 4825 return CP_ACCESS_OK; 4826 } 4827 4828 static uint64_t aa64_dczid_read(CPUARMState *env, const ARMCPRegInfo *ri) 4829 { 4830 ARMCPU *cpu = env_archcpu(env); 4831 int dzp_bit = 1 << 4; 4832 4833 /* DZP indicates whether DC ZVA access is allowed */ 4834 if (aa64_zva_access(env, NULL, false) == CP_ACCESS_OK) { 4835 dzp_bit = 0; 4836 } 4837 return cpu->dcz_blocksize | dzp_bit; 4838 } 4839 4840 static CPAccessResult sp_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 4841 bool isread) 4842 { 4843 if (!(env->pstate & PSTATE_SP)) { 4844 /* 4845 * Access to SP_EL0 is undefined if it's being used as 4846 * the stack pointer. 4847 */ 4848 return CP_ACCESS_UNDEFINED; 4849 } 4850 return CP_ACCESS_OK; 4851 } 4852 4853 static uint64_t spsel_read(CPUARMState *env, const ARMCPRegInfo *ri) 4854 { 4855 return env->pstate & PSTATE_SP; 4856 } 4857 4858 static void spsel_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 4859 { 4860 update_spsel(env, val); 4861 } 4862 4863 static void sctlr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4864 uint64_t value) 4865 { 4866 ARMCPU *cpu = env_archcpu(env); 4867 4868 if (arm_feature(env, ARM_FEATURE_PMSA) && !cpu->has_mpu) { 4869 /* M bit is RAZ/WI for PMSA with no MPU implemented */ 4870 value &= ~SCTLR_M; 4871 } 4872 4873 /* ??? Lots of these bits are not implemented. */ 4874 4875 if (ri->state == ARM_CP_STATE_AA64 && !cpu_isar_feature(aa64_mte, cpu)) { 4876 if (ri->opc1 == 6) { /* SCTLR_EL3 */ 4877 value &= ~(SCTLR_ITFSB | SCTLR_TCF | SCTLR_ATA); 4878 } else { 4879 value &= ~(SCTLR_ITFSB | SCTLR_TCF0 | SCTLR_TCF | 4880 SCTLR_ATA0 | SCTLR_ATA); 4881 } 4882 } 4883 4884 if (raw_read(env, ri) == value) { 4885 /* 4886 * Skip the TLB flush if nothing actually changed; Linux likes 4887 * to do a lot of pointless SCTLR writes. 4888 */ 4889 return; 4890 } 4891 4892 raw_write(env, ri, value); 4893 4894 /* This may enable/disable the MMU, so do a TLB flush. */ 4895 tlb_flush(CPU(cpu)); 4896 4897 if (tcg_enabled() && ri->type & ARM_CP_SUPPRESS_TB_END) { 4898 /* 4899 * Normally we would always end the TB on an SCTLR write; see the 4900 * comment in ARMCPRegInfo sctlr initialization below for why Xscale 4901 * is special. Setting ARM_CP_SUPPRESS_TB_END also stops the rebuild 4902 * of hflags from the translator, so do it here. 4903 */ 4904 arm_rebuild_hflags(env); 4905 } 4906 } 4907 4908 static void mdcr_el3_write(CPUARMState *env, const ARMCPRegInfo *ri, 4909 uint64_t value) 4910 { 4911 /* 4912 * Some MDCR_EL3 bits affect whether PMU counters are running: 4913 * if we are trying to change any of those then we must 4914 * bracket this update with PMU start/finish calls. 4915 */ 4916 bool pmu_op = (env->cp15.mdcr_el3 ^ value) & MDCR_EL3_PMU_ENABLE_BITS; 4917 4918 if (pmu_op) { 4919 pmu_op_start(env); 4920 } 4921 env->cp15.mdcr_el3 = value; 4922 if (pmu_op) { 4923 pmu_op_finish(env); 4924 } 4925 } 4926 4927 static void sdcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 4928 uint64_t value) 4929 { 4930 /* Not all bits defined for MDCR_EL3 exist in the AArch32 SDCR */ 4931 mdcr_el3_write(env, ri, value & SDCR_VALID_MASK); 4932 } 4933 4934 static void mdcr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 4935 uint64_t value) 4936 { 4937 /* 4938 * Some MDCR_EL2 bits affect whether PMU counters are running: 4939 * if we are trying to change any of those then we must 4940 * bracket this update with PMU start/finish calls. 4941 */ 4942 bool pmu_op = (env->cp15.mdcr_el2 ^ value) & MDCR_EL2_PMU_ENABLE_BITS; 4943 4944 if (pmu_op) { 4945 pmu_op_start(env); 4946 } 4947 env->cp15.mdcr_el2 = value; 4948 if (pmu_op) { 4949 pmu_op_finish(env); 4950 } 4951 } 4952 4953 static CPAccessResult access_nv1(CPUARMState *env, const ARMCPRegInfo *ri, 4954 bool isread) 4955 { 4956 if (arm_current_el(env) == 1) { 4957 uint64_t hcr_nv = arm_hcr_el2_eff(env) & (HCR_NV | HCR_NV1 | HCR_NV2); 4958 4959 if (hcr_nv == (HCR_NV | HCR_NV1)) { 4960 return CP_ACCESS_TRAP_EL2; 4961 } 4962 } 4963 return CP_ACCESS_OK; 4964 } 4965 4966 #ifdef CONFIG_USER_ONLY 4967 /* 4968 * `IC IVAU` is handled to improve compatibility with JITs that dual-map their 4969 * code to get around W^X restrictions, where one region is writable and the 4970 * other is executable. 4971 * 4972 * Since the executable region is never written to we cannot detect code 4973 * changes when running in user mode, and rely on the emulated JIT telling us 4974 * that the code has changed by executing this instruction. 4975 */ 4976 static void ic_ivau_write(CPUARMState *env, const ARMCPRegInfo *ri, 4977 uint64_t value) 4978 { 4979 uint64_t icache_line_mask, start_address, end_address; 4980 const ARMCPU *cpu; 4981 4982 cpu = env_archcpu(env); 4983 4984 icache_line_mask = (4 << extract32(cpu->ctr, 0, 4)) - 1; 4985 start_address = value & ~icache_line_mask; 4986 end_address = value | icache_line_mask; 4987 4988 mmap_lock(); 4989 4990 tb_invalidate_phys_range(start_address, end_address); 4991 4992 mmap_unlock(); 4993 } 4994 #endif 4995 4996 static const ARMCPRegInfo v8_cp_reginfo[] = { 4997 /* 4998 * Minimal set of EL0-visible registers. This will need to be expanded 4999 * significantly for system emulation of AArch64 CPUs. 5000 */ 5001 { .name = "NZCV", .state = ARM_CP_STATE_AA64, 5002 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 2, 5003 .access = PL0_RW, .type = ARM_CP_NZCV }, 5004 { .name = "DAIF", .state = ARM_CP_STATE_AA64, 5005 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 2, 5006 .type = ARM_CP_NO_RAW, 5007 .access = PL0_RW, .accessfn = aa64_daif_access, 5008 .fieldoffset = offsetof(CPUARMState, daif), 5009 .writefn = aa64_daif_write, .resetfn = arm_cp_reset_ignore }, 5010 { .name = "FPCR", .state = ARM_CP_STATE_AA64, 5011 .opc0 = 3, .opc1 = 3, .opc2 = 0, .crn = 4, .crm = 4, 5012 .access = PL0_RW, .type = ARM_CP_FPU, 5013 .readfn = aa64_fpcr_read, .writefn = aa64_fpcr_write }, 5014 { .name = "FPSR", .state = ARM_CP_STATE_AA64, 5015 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 4, .crm = 4, 5016 .access = PL0_RW, .type = ARM_CP_FPU | ARM_CP_SUPPRESS_TB_END, 5017 .readfn = aa64_fpsr_read, .writefn = aa64_fpsr_write }, 5018 { .name = "DCZID_EL0", .state = ARM_CP_STATE_AA64, 5019 .opc0 = 3, .opc1 = 3, .opc2 = 7, .crn = 0, .crm = 0, 5020 .access = PL0_R, .type = ARM_CP_NO_RAW, 5021 .fgt = FGT_DCZID_EL0, 5022 .readfn = aa64_dczid_read }, 5023 { .name = "DC_ZVA", .state = ARM_CP_STATE_AA64, 5024 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 1, 5025 .access = PL0_W, .type = ARM_CP_DC_ZVA, 5026 #ifndef CONFIG_USER_ONLY 5027 /* Avoid overhead of an access check that always passes in user-mode */ 5028 .accessfn = aa64_zva_access, 5029 .fgt = FGT_DCZVA, 5030 #endif 5031 }, 5032 { .name = "CURRENTEL", .state = ARM_CP_STATE_AA64, 5033 .opc0 = 3, .opc1 = 0, .opc2 = 2, .crn = 4, .crm = 2, 5034 .access = PL1_R, .type = ARM_CP_CURRENTEL }, 5035 /* 5036 * Instruction cache ops. All of these except `IC IVAU` NOP because we 5037 * don't emulate caches. 5038 */ 5039 { .name = "IC_IALLUIS", .state = ARM_CP_STATE_AA64, 5040 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 5041 .access = PL1_W, .type = ARM_CP_NOP, 5042 .fgt = FGT_ICIALLUIS, 5043 .accessfn = access_ticab }, 5044 { .name = "IC_IALLU", .state = ARM_CP_STATE_AA64, 5045 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 5046 .access = PL1_W, .type = ARM_CP_NOP, 5047 .fgt = FGT_ICIALLU, 5048 .accessfn = access_tocu }, 5049 { .name = "IC_IVAU", .state = ARM_CP_STATE_AA64, 5050 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 5, .opc2 = 1, 5051 .access = PL0_W, 5052 .fgt = FGT_ICIVAU, 5053 .accessfn = access_tocu, 5054 #ifdef CONFIG_USER_ONLY 5055 .type = ARM_CP_NO_RAW, 5056 .writefn = ic_ivau_write 5057 #else 5058 .type = ARM_CP_NOP 5059 #endif 5060 }, 5061 /* Cache ops: all NOPs since we don't emulate caches */ 5062 { .name = "DC_IVAC", .state = ARM_CP_STATE_AA64, 5063 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 5064 .access = PL1_W, .accessfn = aa64_cacheop_poc_access, 5065 .fgt = FGT_DCIVAC, 5066 .type = ARM_CP_NOP }, 5067 { .name = "DC_ISW", .state = ARM_CP_STATE_AA64, 5068 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 5069 .fgt = FGT_DCISW, 5070 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 5071 { .name = "DC_CVAC", .state = ARM_CP_STATE_AA64, 5072 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 1, 5073 .access = PL0_W, .type = ARM_CP_NOP, 5074 .fgt = FGT_DCCVAC, 5075 .accessfn = aa64_cacheop_poc_access }, 5076 { .name = "DC_CSW", .state = ARM_CP_STATE_AA64, 5077 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 5078 .fgt = FGT_DCCSW, 5079 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 5080 { .name = "DC_CVAU", .state = ARM_CP_STATE_AA64, 5081 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 11, .opc2 = 1, 5082 .access = PL0_W, .type = ARM_CP_NOP, 5083 .fgt = FGT_DCCVAU, 5084 .accessfn = access_tocu }, 5085 { .name = "DC_CIVAC", .state = ARM_CP_STATE_AA64, 5086 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 1, 5087 .access = PL0_W, .type = ARM_CP_NOP, 5088 .fgt = FGT_DCCIVAC, 5089 .accessfn = aa64_cacheop_poc_access }, 5090 { .name = "DC_CISW", .state = ARM_CP_STATE_AA64, 5091 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 5092 .fgt = FGT_DCCISW, 5093 .access = PL1_W, .accessfn = access_tsw, .type = ARM_CP_NOP }, 5094 #ifndef CONFIG_USER_ONLY 5095 /* 64 bit address translation operations */ 5096 { .name = "AT_S1E1R", .state = ARM_CP_STATE_AA64, 5097 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 0, 5098 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5099 .fgt = FGT_ATS1E1R, 5100 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 5101 { .name = "AT_S1E1W", .state = ARM_CP_STATE_AA64, 5102 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 1, 5103 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5104 .fgt = FGT_ATS1E1W, 5105 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 5106 { .name = "AT_S1E0R", .state = ARM_CP_STATE_AA64, 5107 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 2, 5108 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5109 .fgt = FGT_ATS1E0R, 5110 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 5111 { .name = "AT_S1E0W", .state = ARM_CP_STATE_AA64, 5112 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 8, .opc2 = 3, 5113 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5114 .fgt = FGT_ATS1E0W, 5115 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 5116 { .name = "AT_S12E1R", .state = ARM_CP_STATE_AA64, 5117 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 4, 5118 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5119 .accessfn = at_e012_access, .writefn = ats_write64 }, 5120 { .name = "AT_S12E1W", .state = ARM_CP_STATE_AA64, 5121 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 5, 5122 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5123 .accessfn = at_e012_access, .writefn = ats_write64 }, 5124 { .name = "AT_S12E0R", .state = ARM_CP_STATE_AA64, 5125 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 6, 5126 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5127 .accessfn = at_e012_access, .writefn = ats_write64 }, 5128 { .name = "AT_S12E0W", .state = ARM_CP_STATE_AA64, 5129 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 7, 5130 .access = PL2_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5131 .accessfn = at_e012_access, .writefn = ats_write64 }, 5132 /* AT S1E2* are elsewhere as they UNDEF from EL3 if EL2 is not present */ 5133 { .name = "AT_S1E3R", .state = ARM_CP_STATE_AA64, 5134 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 0, 5135 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5136 .writefn = ats_write64 }, 5137 { .name = "AT_S1E3W", .state = ARM_CP_STATE_AA64, 5138 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 8, .opc2 = 1, 5139 .access = PL3_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 5140 .writefn = ats_write64 }, 5141 { .name = "PAR_EL1", .state = ARM_CP_STATE_AA64, 5142 .type = ARM_CP_ALIAS, 5143 .opc0 = 3, .opc1 = 0, .crn = 7, .crm = 4, .opc2 = 0, 5144 .access = PL1_RW, .resetvalue = 0, 5145 .fgt = FGT_PAR_EL1, 5146 .fieldoffset = offsetof(CPUARMState, cp15.par_el[1]), 5147 .writefn = par_write }, 5148 #endif 5149 /* 32 bit cache operations */ 5150 { .name = "ICIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 0, 5151 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_ticab }, 5152 { .name = "BPIALLUIS", .cp = 15, .opc1 = 0, .crn = 7, .crm = 1, .opc2 = 6, 5153 .type = ARM_CP_NOP, .access = PL1_W }, 5154 { .name = "ICIALLU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 0, 5155 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu }, 5156 { .name = "ICIMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 1, 5157 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu }, 5158 { .name = "BPIALL", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 6, 5159 .type = ARM_CP_NOP, .access = PL1_W }, 5160 { .name = "BPIMVA", .cp = 15, .opc1 = 0, .crn = 7, .crm = 5, .opc2 = 7, 5161 .type = ARM_CP_NOP, .access = PL1_W }, 5162 { .name = "DCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 1, 5163 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5164 { .name = "DCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 2, 5165 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5166 { .name = "DCCMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 1, 5167 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5168 { .name = "DCCSW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 2, 5169 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5170 { .name = "DCCMVAU", .cp = 15, .opc1 = 0, .crn = 7, .crm = 11, .opc2 = 1, 5171 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tocu }, 5172 { .name = "DCCIMVAC", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 1, 5173 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = aa64_cacheop_poc_access }, 5174 { .name = "DCCISW", .cp = 15, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 2, 5175 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 5176 /* MMU Domain access control / MPU write buffer control */ 5177 { .name = "DACR", .cp = 15, .opc1 = 0, .crn = 3, .crm = 0, .opc2 = 0, 5178 .access = PL1_RW, .accessfn = access_tvm_trvm, .resetvalue = 0, 5179 .writefn = dacr_write, .raw_writefn = raw_write, 5180 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.dacr_s), 5181 offsetoflow32(CPUARMState, cp15.dacr_ns) } }, 5182 { .name = "ELR_EL1", .state = ARM_CP_STATE_AA64, 5183 .type = ARM_CP_ALIAS, 5184 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 1, 5185 .access = PL1_RW, .accessfn = access_nv1, 5186 .nv2_redirect_offset = 0x230 | NV2_REDIR_NV1, 5187 .fieldoffset = offsetof(CPUARMState, elr_el[1]) }, 5188 { .name = "SPSR_EL1", .state = ARM_CP_STATE_AA64, 5189 .type = ARM_CP_ALIAS, 5190 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 0, .opc2 = 0, 5191 .access = PL1_RW, .accessfn = access_nv1, 5192 .nv2_redirect_offset = 0x160 | NV2_REDIR_NV1, 5193 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_SVC]) }, 5194 /* 5195 * We rely on the access checks not allowing the guest to write to the 5196 * state field when SPSel indicates that it's being used as the stack 5197 * pointer. 5198 */ 5199 { .name = "SP_EL0", .state = ARM_CP_STATE_AA64, 5200 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 1, .opc2 = 0, 5201 .access = PL1_RW, .accessfn = sp_el0_access, 5202 .type = ARM_CP_ALIAS, 5203 .fieldoffset = offsetof(CPUARMState, sp_el[0]) }, 5204 { .name = "SP_EL1", .state = ARM_CP_STATE_AA64, 5205 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 1, .opc2 = 0, 5206 .nv2_redirect_offset = 0x240, 5207 .access = PL2_RW, .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_KEEP, 5208 .fieldoffset = offsetof(CPUARMState, sp_el[1]) }, 5209 { .name = "SPSel", .state = ARM_CP_STATE_AA64, 5210 .opc0 = 3, .opc1 = 0, .crn = 4, .crm = 2, .opc2 = 0, 5211 .type = ARM_CP_NO_RAW, 5212 .access = PL1_RW, .readfn = spsel_read, .writefn = spsel_write }, 5213 { .name = "SPSR_IRQ", .state = ARM_CP_STATE_AA64, 5214 .type = ARM_CP_ALIAS, 5215 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 0, 5216 .access = PL2_RW, 5217 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_IRQ]) }, 5218 { .name = "SPSR_ABT", .state = ARM_CP_STATE_AA64, 5219 .type = ARM_CP_ALIAS, 5220 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 1, 5221 .access = PL2_RW, 5222 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_ABT]) }, 5223 { .name = "SPSR_UND", .state = ARM_CP_STATE_AA64, 5224 .type = ARM_CP_ALIAS, 5225 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 2, 5226 .access = PL2_RW, 5227 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_UND]) }, 5228 { .name = "SPSR_FIQ", .state = ARM_CP_STATE_AA64, 5229 .type = ARM_CP_ALIAS, 5230 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 3, .opc2 = 3, 5231 .access = PL2_RW, 5232 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_FIQ]) }, 5233 { .name = "MDCR_EL3", .state = ARM_CP_STATE_AA64, 5234 .type = ARM_CP_IO, 5235 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 3, .opc2 = 1, 5236 .resetvalue = 0, 5237 .access = PL3_RW, 5238 .writefn = mdcr_el3_write, 5239 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el3) }, 5240 { .name = "SDCR", .type = ARM_CP_ALIAS | ARM_CP_IO, 5241 .cp = 15, .opc1 = 0, .crn = 1, .crm = 3, .opc2 = 1, 5242 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5243 .writefn = sdcr_write, 5244 .fieldoffset = offsetoflow32(CPUARMState, cp15.mdcr_el3) }, 5245 }; 5246 5247 /* These are present only when EL1 supports AArch32 */ 5248 static const ARMCPRegInfo v8_aa32_el1_reginfo[] = { 5249 { .name = "FPEXC32_EL2", .state = ARM_CP_STATE_AA64, 5250 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 3, .opc2 = 0, 5251 .access = PL2_RW, 5252 .type = ARM_CP_ALIAS | ARM_CP_FPU | ARM_CP_EL3_NO_EL2_KEEP, 5253 .fieldoffset = offsetof(CPUARMState, vfp.xregs[ARM_VFP_FPEXC]) }, 5254 { .name = "DACR32_EL2", .state = ARM_CP_STATE_AA64, 5255 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 0, .opc2 = 0, 5256 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP, 5257 .writefn = dacr_write, .raw_writefn = raw_write, 5258 .fieldoffset = offsetof(CPUARMState, cp15.dacr32_el2) }, 5259 { .name = "IFSR32_EL2", .state = ARM_CP_STATE_AA64, 5260 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 0, .opc2 = 1, 5261 .access = PL2_RW, .resetvalue = 0, .type = ARM_CP_EL3_NO_EL2_KEEP, 5262 .fieldoffset = offsetof(CPUARMState, cp15.ifsr32_el2) }, 5263 }; 5264 5265 static void do_hcr_write(CPUARMState *env, uint64_t value, uint64_t valid_mask) 5266 { 5267 ARMCPU *cpu = env_archcpu(env); 5268 5269 if (arm_feature(env, ARM_FEATURE_V8)) { 5270 valid_mask |= MAKE_64BIT_MASK(0, 34); /* ARMv8.0 */ 5271 } else { 5272 valid_mask |= MAKE_64BIT_MASK(0, 28); /* ARMv7VE */ 5273 } 5274 5275 if (arm_feature(env, ARM_FEATURE_EL3)) { 5276 valid_mask &= ~HCR_HCD; 5277 } else if (cpu->psci_conduit != QEMU_PSCI_CONDUIT_SMC) { 5278 /* 5279 * Architecturally HCR.TSC is RES0 if EL3 is not implemented. 5280 * However, if we're using the SMC PSCI conduit then QEMU is 5281 * effectively acting like EL3 firmware and so the guest at 5282 * EL2 should retain the ability to prevent EL1 from being 5283 * able to make SMC calls into the ersatz firmware, so in 5284 * that case HCR.TSC should be read/write. 5285 */ 5286 valid_mask &= ~HCR_TSC; 5287 } 5288 5289 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 5290 if (cpu_isar_feature(aa64_vh, cpu)) { 5291 valid_mask |= HCR_E2H; 5292 } 5293 if (cpu_isar_feature(aa64_ras, cpu)) { 5294 valid_mask |= HCR_TERR | HCR_TEA; 5295 } 5296 if (cpu_isar_feature(aa64_lor, cpu)) { 5297 valid_mask |= HCR_TLOR; 5298 } 5299 if (cpu_isar_feature(aa64_pauth, cpu)) { 5300 valid_mask |= HCR_API | HCR_APK; 5301 } 5302 if (cpu_isar_feature(aa64_mte, cpu)) { 5303 valid_mask |= HCR_ATA | HCR_DCT | HCR_TID5; 5304 } 5305 if (cpu_isar_feature(aa64_scxtnum, cpu)) { 5306 valid_mask |= HCR_ENSCXT; 5307 } 5308 if (cpu_isar_feature(aa64_fwb, cpu)) { 5309 valid_mask |= HCR_FWB; 5310 } 5311 if (cpu_isar_feature(aa64_rme, cpu)) { 5312 valid_mask |= HCR_GPF; 5313 } 5314 if (cpu_isar_feature(aa64_nv, cpu)) { 5315 valid_mask |= HCR_NV | HCR_NV1 | HCR_AT; 5316 } 5317 if (cpu_isar_feature(aa64_nv2, cpu)) { 5318 valid_mask |= HCR_NV2; 5319 } 5320 } 5321 5322 if (cpu_isar_feature(any_evt, cpu)) { 5323 valid_mask |= HCR_TTLBIS | HCR_TTLBOS | HCR_TICAB | HCR_TOCU | HCR_TID4; 5324 } else if (cpu_isar_feature(any_half_evt, cpu)) { 5325 valid_mask |= HCR_TICAB | HCR_TOCU | HCR_TID4; 5326 } 5327 5328 /* Clear RES0 bits. */ 5329 value &= valid_mask; 5330 5331 /* RW is RAO/WI if EL1 is AArch64 only */ 5332 if (!cpu_isar_feature(aa64_aa32_el1, cpu)) { 5333 value |= HCR_RW; 5334 } 5335 5336 /* 5337 * These bits change the MMU setup: 5338 * HCR_VM enables stage 2 translation 5339 * HCR_PTW forbids certain page-table setups 5340 * HCR_DC disables stage1 and enables stage2 translation 5341 * HCR_DCT enables tagging on (disabled) stage1 translation 5342 * HCR_FWB changes the interpretation of stage2 descriptor bits 5343 * HCR_NV and HCR_NV1 affect interpretation of descriptor bits 5344 */ 5345 if ((env->cp15.hcr_el2 ^ value) & 5346 (HCR_VM | HCR_PTW | HCR_DC | HCR_DCT | HCR_FWB | HCR_NV | HCR_NV1)) { 5347 tlb_flush(CPU(cpu)); 5348 } 5349 env->cp15.hcr_el2 = value; 5350 5351 /* 5352 * Updates to VI and VF require us to update the status of 5353 * virtual interrupts, which are the logical OR of these bits 5354 * and the state of the input lines from the GIC. (This requires 5355 * that we have the BQL, which is done by marking the 5356 * reginfo structs as ARM_CP_IO.) 5357 * Note that if a write to HCR pends a VIRQ or VFIQ or VINMI or 5358 * VFNMI, it is never possible for it to be taken immediately 5359 * because VIRQ, VFIQ, VINMI and VFNMI are masked unless running 5360 * at EL0 or EL1, and HCR can only be written at EL2. 5361 */ 5362 g_assert(bql_locked()); 5363 arm_cpu_update_virq(cpu); 5364 arm_cpu_update_vfiq(cpu); 5365 arm_cpu_update_vserr(cpu); 5366 if (cpu_isar_feature(aa64_nmi, cpu)) { 5367 arm_cpu_update_vinmi(cpu); 5368 arm_cpu_update_vfnmi(cpu); 5369 } 5370 } 5371 5372 static void hcr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t value) 5373 { 5374 do_hcr_write(env, value, 0); 5375 } 5376 5377 static void hcr_writehigh(CPUARMState *env, const ARMCPRegInfo *ri, 5378 uint64_t value) 5379 { 5380 /* Handle HCR2 write, i.e. write to high half of HCR_EL2 */ 5381 value = deposit64(env->cp15.hcr_el2, 32, 32, value); 5382 do_hcr_write(env, value, MAKE_64BIT_MASK(0, 32)); 5383 } 5384 5385 static void hcr_writelow(CPUARMState *env, const ARMCPRegInfo *ri, 5386 uint64_t value) 5387 { 5388 /* Handle HCR write, i.e. write to low half of HCR_EL2 */ 5389 value = deposit64(env->cp15.hcr_el2, 0, 32, value); 5390 do_hcr_write(env, value, MAKE_64BIT_MASK(32, 32)); 5391 } 5392 5393 static void hcr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 5394 { 5395 /* hcr_write will set the RES1 bits on an AArch64-only CPU */ 5396 hcr_write(env, ri, 0); 5397 } 5398 5399 /* 5400 * Return the effective value of HCR_EL2, at the given security state. 5401 * Bits that are not included here: 5402 * RW (read from SCR_EL3.RW as needed) 5403 */ 5404 uint64_t arm_hcr_el2_eff_secstate(CPUARMState *env, ARMSecuritySpace space) 5405 { 5406 uint64_t ret = env->cp15.hcr_el2; 5407 5408 assert(space != ARMSS_Root); 5409 5410 if (!arm_is_el2_enabled_secstate(env, space)) { 5411 /* 5412 * "This register has no effect if EL2 is not enabled in the 5413 * current Security state". This is ARMv8.4-SecEL2 speak for 5414 * !(SCR_EL3.NS==1 || SCR_EL3.EEL2==1). 5415 * 5416 * Prior to that, the language was "In an implementation that 5417 * includes EL3, when the value of SCR_EL3.NS is 0 the PE behaves 5418 * as if this field is 0 for all purposes other than a direct 5419 * read or write access of HCR_EL2". With lots of enumeration 5420 * on a per-field basis. In current QEMU, this is condition 5421 * is arm_is_secure_below_el3. 5422 * 5423 * Since the v8.4 language applies to the entire register, and 5424 * appears to be backward compatible, use that. 5425 */ 5426 return 0; 5427 } 5428 5429 /* 5430 * For a cpu that supports both aarch64 and aarch32, we can set bits 5431 * in HCR_EL2 (e.g. via EL3) that are RES0 when we enter EL2 as aa32. 5432 * Ignore all of the bits in HCR+HCR2 that are not valid for aarch32. 5433 */ 5434 if (!arm_el_is_aa64(env, 2)) { 5435 uint64_t aa32_valid; 5436 5437 /* 5438 * These bits are up-to-date as of ARMv8.6. 5439 * For HCR, it's easiest to list just the 2 bits that are invalid. 5440 * For HCR2, list those that are valid. 5441 */ 5442 aa32_valid = MAKE_64BIT_MASK(0, 32) & ~(HCR_RW | HCR_TDZ); 5443 aa32_valid |= (HCR_CD | HCR_ID | HCR_TERR | HCR_TEA | HCR_MIOCNCE | 5444 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_TTLBIS); 5445 ret &= aa32_valid; 5446 } 5447 5448 if (ret & HCR_TGE) { 5449 /* These bits are up-to-date as of ARMv8.6. */ 5450 if (ret & HCR_E2H) { 5451 ret &= ~(HCR_VM | HCR_FMO | HCR_IMO | HCR_AMO | 5452 HCR_BSU_MASK | HCR_DC | HCR_TWI | HCR_TWE | 5453 HCR_TID0 | HCR_TID2 | HCR_TPCP | HCR_TPU | 5454 HCR_TDZ | HCR_CD | HCR_ID | HCR_MIOCNCE | 5455 HCR_TID4 | HCR_TICAB | HCR_TOCU | HCR_ENSCXT | 5456 HCR_TTLBIS | HCR_TTLBOS | HCR_TID5); 5457 } else { 5458 ret |= HCR_FMO | HCR_IMO | HCR_AMO; 5459 } 5460 ret &= ~(HCR_SWIO | HCR_PTW | HCR_VF | HCR_VI | HCR_VSE | 5461 HCR_FB | HCR_TID1 | HCR_TID3 | HCR_TSC | HCR_TACR | 5462 HCR_TSW | HCR_TTLB | HCR_TVM | HCR_HCD | HCR_TRVM | 5463 HCR_TLOR); 5464 } 5465 5466 return ret; 5467 } 5468 5469 uint64_t arm_hcr_el2_eff(CPUARMState *env) 5470 { 5471 if (arm_feature(env, ARM_FEATURE_M)) { 5472 return 0; 5473 } 5474 return arm_hcr_el2_eff_secstate(env, arm_security_space_below_el3(env)); 5475 } 5476 5477 /* 5478 * Corresponds to ARM pseudocode function ELIsInHost(). 5479 */ 5480 bool el_is_in_host(CPUARMState *env, int el) 5481 { 5482 uint64_t mask; 5483 5484 /* 5485 * Since we only care about E2H and TGE, we can skip arm_hcr_el2_eff(). 5486 * Perform the simplest bit tests first, and validate EL2 afterward. 5487 */ 5488 if (el & 1) { 5489 return false; /* EL1 or EL3 */ 5490 } 5491 5492 /* 5493 * Note that hcr_write() checks isar_feature_aa64_vh(), 5494 * aka HaveVirtHostExt(), in allowing HCR_E2H to be set. 5495 */ 5496 mask = el ? HCR_E2H : HCR_E2H | HCR_TGE; 5497 if ((env->cp15.hcr_el2 & mask) != mask) { 5498 return false; 5499 } 5500 5501 /* TGE and/or E2H set: double check those bits are currently legal. */ 5502 return arm_is_el2_enabled(env) && arm_el_is_aa64(env, 2); 5503 } 5504 5505 static void hcrx_write(CPUARMState *env, const ARMCPRegInfo *ri, 5506 uint64_t value) 5507 { 5508 ARMCPU *cpu = env_archcpu(env); 5509 uint64_t valid_mask = 0; 5510 5511 /* FEAT_MOPS adds MSCEn and MCE2 */ 5512 if (cpu_isar_feature(aa64_mops, cpu)) { 5513 valid_mask |= HCRX_MSCEN | HCRX_MCE2; 5514 } 5515 5516 /* FEAT_NMI adds TALLINT, VINMI and VFNMI */ 5517 if (cpu_isar_feature(aa64_nmi, cpu)) { 5518 valid_mask |= HCRX_TALLINT | HCRX_VINMI | HCRX_VFNMI; 5519 } 5520 /* FEAT_CMOW adds CMOW */ 5521 if (cpu_isar_feature(aa64_cmow, cpu)) { 5522 valid_mask |= HCRX_CMOW; 5523 } 5524 /* FEAT_XS adds FGTnXS, FnXS */ 5525 if (cpu_isar_feature(aa64_xs, cpu)) { 5526 valid_mask |= HCRX_FGTNXS | HCRX_FNXS; 5527 } 5528 5529 /* Clear RES0 bits. */ 5530 env->cp15.hcrx_el2 = value & valid_mask; 5531 5532 /* 5533 * Updates to VINMI and VFNMI require us to update the status of 5534 * virtual NMI, which are the logical OR of these bits 5535 * and the state of the input lines from the GIC. (This requires 5536 * that we have the BQL, which is done by marking the 5537 * reginfo structs as ARM_CP_IO.) 5538 * Note that if a write to HCRX pends a VINMI or VFNMI it is never 5539 * possible for it to be taken immediately, because VINMI and 5540 * VFNMI are masked unless running at EL0 or EL1, and HCRX 5541 * can only be written at EL2. 5542 */ 5543 if (cpu_isar_feature(aa64_nmi, cpu)) { 5544 g_assert(bql_locked()); 5545 arm_cpu_update_vinmi(cpu); 5546 arm_cpu_update_vfnmi(cpu); 5547 } 5548 } 5549 5550 static CPAccessResult access_hxen(CPUARMState *env, const ARMCPRegInfo *ri, 5551 bool isread) 5552 { 5553 if (arm_current_el(env) == 2 5554 && arm_feature(env, ARM_FEATURE_EL3) 5555 && !(env->cp15.scr_el3 & SCR_HXEN)) { 5556 return CP_ACCESS_TRAP_EL3; 5557 } 5558 return CP_ACCESS_OK; 5559 } 5560 5561 static const ARMCPRegInfo hcrx_el2_reginfo = { 5562 .name = "HCRX_EL2", .state = ARM_CP_STATE_AA64, 5563 .type = ARM_CP_IO, 5564 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 2, 5565 .access = PL2_RW, .writefn = hcrx_write, .accessfn = access_hxen, 5566 .nv2_redirect_offset = 0xa0, 5567 .fieldoffset = offsetof(CPUARMState, cp15.hcrx_el2), 5568 }; 5569 5570 /* Return the effective value of HCRX_EL2. */ 5571 uint64_t arm_hcrx_el2_eff(CPUARMState *env) 5572 { 5573 /* 5574 * The bits in this register behave as 0 for all purposes other than 5575 * direct reads of the register if SCR_EL3.HXEn is 0. 5576 * If EL2 is not enabled in the current security state, then the 5577 * bit may behave as if 0, or as if 1, depending on the bit. 5578 * For the moment, we treat the EL2-disabled case as taking 5579 * priority over the HXEn-disabled case. This is true for the only 5580 * bit for a feature which we implement where the answer is different 5581 * for the two cases (MSCEn for FEAT_MOPS). 5582 * This may need to be revisited for future bits. 5583 */ 5584 if (!arm_is_el2_enabled(env)) { 5585 uint64_t hcrx = 0; 5586 if (cpu_isar_feature(aa64_mops, env_archcpu(env))) { 5587 /* MSCEn behaves as 1 if EL2 is not enabled */ 5588 hcrx |= HCRX_MSCEN; 5589 } 5590 return hcrx; 5591 } 5592 if (arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_HXEN)) { 5593 return 0; 5594 } 5595 return env->cp15.hcrx_el2; 5596 } 5597 5598 static void cptr_el2_write(CPUARMState *env, const ARMCPRegInfo *ri, 5599 uint64_t value) 5600 { 5601 /* 5602 * For A-profile AArch32 EL3, if NSACR.CP10 5603 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1. 5604 */ 5605 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 5606 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 5607 uint64_t mask = R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK; 5608 value = (value & ~mask) | (env->cp15.cptr_el[2] & mask); 5609 } 5610 env->cp15.cptr_el[2] = value; 5611 } 5612 5613 static uint64_t cptr_el2_read(CPUARMState *env, const ARMCPRegInfo *ri) 5614 { 5615 /* 5616 * For A-profile AArch32 EL3, if NSACR.CP10 5617 * is 0 then HCPTR.{TCP11,TCP10} ignore writes and read as 1. 5618 */ 5619 uint64_t value = env->cp15.cptr_el[2]; 5620 5621 if (arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 5622 !arm_is_secure(env) && !extract32(env->cp15.nsacr, 10, 1)) { 5623 value |= R_HCPTR_TCP11_MASK | R_HCPTR_TCP10_MASK; 5624 } 5625 return value; 5626 } 5627 5628 static const ARMCPRegInfo el2_cp_reginfo[] = { 5629 { .name = "HCR_EL2", .state = ARM_CP_STATE_AA64, 5630 .type = ARM_CP_IO, 5631 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 5632 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 5633 .nv2_redirect_offset = 0x78, 5634 .resetfn = hcr_reset, 5635 .writefn = hcr_write, .raw_writefn = raw_write }, 5636 { .name = "HCR", .state = ARM_CP_STATE_AA32, 5637 .type = ARM_CP_ALIAS | ARM_CP_IO, 5638 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 0, 5639 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.hcr_el2), 5640 .writefn = hcr_writelow }, 5641 { .name = "HACR_EL2", .state = ARM_CP_STATE_BOTH, 5642 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 7, 5643 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 5644 { .name = "ELR_EL2", .state = ARM_CP_STATE_AA64, 5645 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT, 5646 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 1, 5647 .access = PL2_RW, 5648 .fieldoffset = offsetof(CPUARMState, elr_el[2]) }, 5649 { .name = "ESR_EL2", .state = ARM_CP_STATE_BOTH, 5650 .type = ARM_CP_NV2_REDIRECT, 5651 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 0, 5652 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[2]) }, 5653 { .name = "FAR_EL2", .state = ARM_CP_STATE_BOTH, 5654 .type = ARM_CP_NV2_REDIRECT, 5655 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 0, 5656 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[2]) }, 5657 { .name = "HIFAR", .state = ARM_CP_STATE_AA32, 5658 .type = ARM_CP_ALIAS, 5659 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 2, 5660 .access = PL2_RW, 5661 .fieldoffset = offsetofhigh32(CPUARMState, cp15.far_el[2]) }, 5662 { .name = "SPSR_EL2", .state = ARM_CP_STATE_AA64, 5663 .type = ARM_CP_ALIAS | ARM_CP_NV2_REDIRECT, 5664 .opc0 = 3, .opc1 = 4, .crn = 4, .crm = 0, .opc2 = 0, 5665 .access = PL2_RW, 5666 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_HYP]) }, 5667 { .name = "VBAR_EL2", .state = ARM_CP_STATE_BOTH, 5668 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 0, 5669 .access = PL2_RW, .writefn = vbar_write, 5670 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[2]), 5671 .resetvalue = 0 }, 5672 { .name = "SP_EL2", .state = ARM_CP_STATE_AA64, 5673 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 1, .opc2 = 0, 5674 .access = PL3_RW, .type = ARM_CP_ALIAS, 5675 .fieldoffset = offsetof(CPUARMState, sp_el[2]) }, 5676 { .name = "CPTR_EL2", .state = ARM_CP_STATE_BOTH, 5677 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 2, 5678 .access = PL2_RW, .accessfn = cptr_access, .resetvalue = 0, 5679 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[2]), 5680 .readfn = cptr_el2_read, .writefn = cptr_el2_write }, 5681 { .name = "MAIR_EL2", .state = ARM_CP_STATE_BOTH, 5682 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 0, 5683 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.mair_el[2]), 5684 .resetvalue = 0 }, 5685 { .name = "HMAIR1", .state = ARM_CP_STATE_AA32, 5686 .cp = 15, .opc1 = 4, .crn = 10, .crm = 2, .opc2 = 1, 5687 .access = PL2_RW, .type = ARM_CP_ALIAS, 5688 .fieldoffset = offsetofhigh32(CPUARMState, cp15.mair_el[2]) }, 5689 { .name = "AMAIR_EL2", .state = ARM_CP_STATE_BOTH, 5690 .opc0 = 3, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 0, 5691 .access = PL2_RW, .type = ARM_CP_CONST, 5692 .resetvalue = 0 }, 5693 /* HAMAIR1 is mapped to AMAIR_EL2[63:32] */ 5694 { .name = "HAMAIR1", .state = ARM_CP_STATE_AA32, 5695 .cp = 15, .opc1 = 4, .crn = 10, .crm = 3, .opc2 = 1, 5696 .access = PL2_RW, .type = ARM_CP_CONST, 5697 .resetvalue = 0 }, 5698 { .name = "AFSR0_EL2", .state = ARM_CP_STATE_BOTH, 5699 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 0, 5700 .access = PL2_RW, .type = ARM_CP_CONST, 5701 .resetvalue = 0 }, 5702 { .name = "AFSR1_EL2", .state = ARM_CP_STATE_BOTH, 5703 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 1, .opc2 = 1, 5704 .access = PL2_RW, .type = ARM_CP_CONST, 5705 .resetvalue = 0 }, 5706 { .name = "TCR_EL2", .state = ARM_CP_STATE_BOTH, 5707 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 2, 5708 .access = PL2_RW, .writefn = vmsa_tcr_el12_write, 5709 .raw_writefn = raw_write, 5710 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[2]) }, 5711 { .name = "VTCR", .state = ARM_CP_STATE_AA32, 5712 .cp = 15, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 5713 .type = ARM_CP_ALIAS, 5714 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5715 .fieldoffset = offsetoflow32(CPUARMState, cp15.vtcr_el2) }, 5716 { .name = "VTCR_EL2", .state = ARM_CP_STATE_AA64, 5717 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 2, 5718 .access = PL2_RW, 5719 .nv2_redirect_offset = 0x40, 5720 /* no .writefn needed as this can't cause an ASID change */ 5721 .fieldoffset = offsetof(CPUARMState, cp15.vtcr_el2) }, 5722 { .name = "VTTBR", .state = ARM_CP_STATE_AA32, 5723 .cp = 15, .opc1 = 6, .crm = 2, 5724 .type = ARM_CP_64BIT | ARM_CP_ALIAS, 5725 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5726 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2), 5727 .writefn = vttbr_write, .raw_writefn = raw_write }, 5728 { .name = "VTTBR_EL2", .state = ARM_CP_STATE_AA64, 5729 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 1, .opc2 = 0, 5730 .access = PL2_RW, .writefn = vttbr_write, .raw_writefn = raw_write, 5731 .nv2_redirect_offset = 0x20, 5732 .fieldoffset = offsetof(CPUARMState, cp15.vttbr_el2) }, 5733 { .name = "SCTLR_EL2", .state = ARM_CP_STATE_BOTH, 5734 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 0, 5735 .access = PL2_RW, .raw_writefn = raw_write, .writefn = sctlr_write, 5736 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[2]) }, 5737 { .name = "TPIDR_EL2", .state = ARM_CP_STATE_BOTH, 5738 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 2, 5739 .access = PL2_RW, .resetvalue = 0, 5740 .nv2_redirect_offset = 0x90, 5741 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[2]) }, 5742 { .name = "TTBR0_EL2", .state = ARM_CP_STATE_AA64, 5743 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 5744 .access = PL2_RW, .resetvalue = 0, 5745 .writefn = vmsa_tcr_ttbr_el2_write, .raw_writefn = raw_write, 5746 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 5747 { .name = "HTTBR", .cp = 15, .opc1 = 4, .crm = 2, 5748 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS, 5749 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[2]) }, 5750 #ifndef CONFIG_USER_ONLY 5751 /* 5752 * Unlike the other EL2-related AT operations, these must 5753 * UNDEF from EL3 if EL2 is not implemented, which is why we 5754 * define them here rather than with the rest of the AT ops. 5755 */ 5756 { .name = "AT_S1E2R", .state = ARM_CP_STATE_AA64, 5757 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 5758 .access = PL2_W, .accessfn = at_s1e2_access, 5759 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF, 5760 .writefn = ats_write64 }, 5761 { .name = "AT_S1E2W", .state = ARM_CP_STATE_AA64, 5762 .opc0 = 1, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 5763 .access = PL2_W, .accessfn = at_s1e2_access, 5764 .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC | ARM_CP_EL3_NO_EL2_UNDEF, 5765 .writefn = ats_write64 }, 5766 /* 5767 * The AArch32 ATS1H* operations are CONSTRAINED UNPREDICTABLE 5768 * if EL2 is not implemented; we choose to UNDEF. Behaviour at EL3 5769 * with SCR.NS == 0 outside Monitor mode is UNPREDICTABLE; we choose 5770 * to behave as if SCR.NS was 1. 5771 */ 5772 { .name = "ATS1HR", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 0, 5773 .access = PL2_W, 5774 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 5775 { .name = "ATS1HW", .cp = 15, .opc1 = 4, .crn = 7, .crm = 8, .opc2 = 1, 5776 .access = PL2_W, 5777 .writefn = ats1h_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 5778 { .name = "CNTHCTL_EL2", .state = ARM_CP_STATE_BOTH, 5779 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 1, .opc2 = 0, 5780 /* 5781 * ARMv7 requires bit 0 and 1 to reset to 1. ARMv8 defines the 5782 * reset values as IMPDEF. We choose to reset to 3 to comply with 5783 * both ARMv7 and ARMv8. 5784 */ 5785 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 3, 5786 .writefn = gt_cnthctl_write, .raw_writefn = raw_write, 5787 .fieldoffset = offsetof(CPUARMState, cp15.cnthctl_el2) }, 5788 { .name = "CNTVOFF_EL2", .state = ARM_CP_STATE_AA64, 5789 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 0, .opc2 = 3, 5790 .access = PL2_RW, .type = ARM_CP_IO, .resetvalue = 0, 5791 .writefn = gt_cntvoff_write, 5792 .nv2_redirect_offset = 0x60, 5793 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 5794 { .name = "CNTVOFF", .cp = 15, .opc1 = 4, .crm = 14, 5795 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_ALIAS | ARM_CP_IO, 5796 .writefn = gt_cntvoff_write, 5797 .fieldoffset = offsetof(CPUARMState, cp15.cntvoff_el2) }, 5798 { .name = "CNTHP_CVAL_EL2", .state = ARM_CP_STATE_AA64, 5799 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 2, 5800 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 5801 .type = ARM_CP_IO, .access = PL2_RW, 5802 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 5803 { .name = "CNTHP_CVAL", .cp = 15, .opc1 = 6, .crm = 14, 5804 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].cval), 5805 .access = PL2_RW, .type = ARM_CP_64BIT | ARM_CP_IO, 5806 .writefn = gt_hyp_cval_write, .raw_writefn = raw_write }, 5807 { .name = "CNTHP_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 5808 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 0, 5809 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 5810 .resetfn = gt_hyp_timer_reset, 5811 .readfn = gt_hyp_tval_read, .writefn = gt_hyp_tval_write }, 5812 { .name = "CNTHP_CTL_EL2", .state = ARM_CP_STATE_BOTH, 5813 .type = ARM_CP_IO, 5814 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 2, .opc2 = 1, 5815 .access = PL2_RW, 5816 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYP].ctl), 5817 .resetvalue = 0, 5818 .writefn = gt_hyp_ctl_write, .raw_writefn = raw_write }, 5819 #endif 5820 { .name = "HPFAR", .state = ARM_CP_STATE_AA32, 5821 .cp = 15, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 5822 .access = PL2_RW, .accessfn = access_el3_aa32ns, 5823 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 5824 { .name = "HPFAR_EL2", .state = ARM_CP_STATE_AA64, 5825 .opc0 = 3, .opc1 = 4, .crn = 6, .crm = 0, .opc2 = 4, 5826 .access = PL2_RW, 5827 .fieldoffset = offsetof(CPUARMState, cp15.hpfar_el2) }, 5828 { .name = "HSTR_EL2", .state = ARM_CP_STATE_BOTH, 5829 .cp = 15, .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 3, 5830 .access = PL2_RW, 5831 .nv2_redirect_offset = 0x80, 5832 .fieldoffset = offsetof(CPUARMState, cp15.hstr_el2) }, 5833 }; 5834 5835 static const ARMCPRegInfo el2_v8_cp_reginfo[] = { 5836 { .name = "HCR2", .state = ARM_CP_STATE_AA32, 5837 .type = ARM_CP_ALIAS | ARM_CP_IO, 5838 .cp = 15, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 5839 .access = PL2_RW, 5840 .fieldoffset = offsetofhigh32(CPUARMState, cp15.hcr_el2), 5841 .writefn = hcr_writehigh }, 5842 }; 5843 5844 static CPAccessResult sel2_access(CPUARMState *env, const ARMCPRegInfo *ri, 5845 bool isread) 5846 { 5847 if (arm_current_el(env) == 3 || arm_is_secure_below_el3(env)) { 5848 return CP_ACCESS_OK; 5849 } 5850 return CP_ACCESS_UNDEFINED; 5851 } 5852 5853 static const ARMCPRegInfo el2_sec_cp_reginfo[] = { 5854 { .name = "VSTTBR_EL2", .state = ARM_CP_STATE_AA64, 5855 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 0, 5856 .access = PL2_RW, .accessfn = sel2_access, 5857 .nv2_redirect_offset = 0x30, 5858 .fieldoffset = offsetof(CPUARMState, cp15.vsttbr_el2) }, 5859 { .name = "VSTCR_EL2", .state = ARM_CP_STATE_AA64, 5860 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 6, .opc2 = 2, 5861 .access = PL2_RW, .accessfn = sel2_access, 5862 .nv2_redirect_offset = 0x48, 5863 .fieldoffset = offsetof(CPUARMState, cp15.vstcr_el2) }, 5864 #ifndef CONFIG_USER_ONLY 5865 /* Secure EL2 Physical Timer */ 5866 { .name = "CNTHPS_TVAL_EL2", .state = ARM_CP_STATE_AA64, 5867 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 0, 5868 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 5869 .accessfn = gt_sel2timer_access, 5870 .readfn = gt_sec_pel2_tval_read, 5871 .writefn = gt_sec_pel2_tval_write, 5872 .resetfn = gt_sec_pel2_timer_reset, 5873 }, 5874 { .name = "CNTHPS_CTL_EL2", .state = ARM_CP_STATE_AA64, 5875 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 1, 5876 .type = ARM_CP_IO, .access = PL2_RW, 5877 .accessfn = gt_sel2timer_access, 5878 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].ctl), 5879 .resetvalue = 0, 5880 .writefn = gt_sec_pel2_ctl_write, .raw_writefn = raw_write, 5881 }, 5882 { .name = "CNTHPS_CVAL_EL2", .state = ARM_CP_STATE_AA64, 5883 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 5, .opc2 = 2, 5884 .type = ARM_CP_IO, .access = PL2_RW, 5885 .accessfn = gt_sel2timer_access, 5886 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_PHYS].cval), 5887 .writefn = gt_sec_pel2_cval_write, .raw_writefn = raw_write, 5888 }, 5889 /* Secure EL2 Virtual Timer */ 5890 { .name = "CNTHVS_TVAL_EL2", .state = ARM_CP_STATE_AA64, 5891 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 0, 5892 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 5893 .accessfn = gt_sel2timer_access, 5894 .readfn = gt_sec_vel2_tval_read, 5895 .writefn = gt_sec_vel2_tval_write, 5896 .resetfn = gt_sec_vel2_timer_reset, 5897 }, 5898 { .name = "CNTHVS_CTL_EL2", .state = ARM_CP_STATE_AA64, 5899 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 1, 5900 .type = ARM_CP_IO, .access = PL2_RW, 5901 .accessfn = gt_sel2timer_access, 5902 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].ctl), 5903 .resetvalue = 0, 5904 .writefn = gt_sec_vel2_ctl_write, .raw_writefn = raw_write, 5905 }, 5906 { .name = "CNTHVS_CVAL_EL2", .state = ARM_CP_STATE_AA64, 5907 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 4, .opc2 = 2, 5908 .type = ARM_CP_IO, .access = PL2_RW, 5909 .accessfn = gt_sel2timer_access, 5910 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_S_EL2_VIRT].cval), 5911 .writefn = gt_sec_vel2_cval_write, .raw_writefn = raw_write, 5912 }, 5913 #endif 5914 }; 5915 5916 static CPAccessResult nsacr_access(CPUARMState *env, const ARMCPRegInfo *ri, 5917 bool isread) 5918 { 5919 /* 5920 * The NSACR is RW at EL3, and RO for NS EL1 and NS EL2. 5921 * At Secure EL1 it traps to EL3 or EL2. 5922 */ 5923 if (arm_current_el(env) == 3) { 5924 return CP_ACCESS_OK; 5925 } 5926 if (arm_is_secure_below_el3(env)) { 5927 if (env->cp15.scr_el3 & SCR_EEL2) { 5928 return CP_ACCESS_TRAP_EL2; 5929 } 5930 return CP_ACCESS_TRAP_EL3; 5931 } 5932 /* Accesses from EL1 NS and EL2 NS are UNDEF for write but allow reads. */ 5933 if (isread) { 5934 return CP_ACCESS_OK; 5935 } 5936 return CP_ACCESS_UNDEFINED; 5937 } 5938 5939 static const ARMCPRegInfo el3_cp_reginfo[] = { 5940 { .name = "SCR_EL3", .state = ARM_CP_STATE_AA64, 5941 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 0, 5942 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.scr_el3), 5943 .resetfn = scr_reset, .writefn = scr_write, .raw_writefn = raw_write }, 5944 { .name = "SCR", .type = ARM_CP_ALIAS | ARM_CP_NEWEL, 5945 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 0, 5946 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5947 .fieldoffset = offsetoflow32(CPUARMState, cp15.scr_el3), 5948 .writefn = scr_write, .raw_writefn = raw_write }, 5949 { .name = "SDER32_EL3", .state = ARM_CP_STATE_AA64, 5950 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 1, 5951 .access = PL3_RW, .resetvalue = 0, 5952 .fieldoffset = offsetof(CPUARMState, cp15.sder) }, 5953 { .name = "SDER", 5954 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 1, 5955 .access = PL3_RW, .resetvalue = 0, 5956 .fieldoffset = offsetoflow32(CPUARMState, cp15.sder) }, 5957 { .name = "MVBAR", .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 5958 .access = PL1_RW, .accessfn = access_trap_aa32s_el1, 5959 .writefn = vbar_write, .resetvalue = 0, 5960 .fieldoffset = offsetof(CPUARMState, cp15.mvbar) }, 5961 { .name = "TTBR0_EL3", .state = ARM_CP_STATE_AA64, 5962 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 0, 5963 .access = PL3_RW, .resetvalue = 0, 5964 .fieldoffset = offsetof(CPUARMState, cp15.ttbr0_el[3]) }, 5965 { .name = "TCR_EL3", .state = ARM_CP_STATE_AA64, 5966 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 0, .opc2 = 2, 5967 .access = PL3_RW, 5968 /* no .writefn needed as this can't cause an ASID change */ 5969 .resetvalue = 0, 5970 .fieldoffset = offsetof(CPUARMState, cp15.tcr_el[3]) }, 5971 { .name = "ELR_EL3", .state = ARM_CP_STATE_AA64, 5972 .type = ARM_CP_ALIAS, 5973 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 1, 5974 .access = PL3_RW, 5975 .fieldoffset = offsetof(CPUARMState, elr_el[3]) }, 5976 { .name = "ESR_EL3", .state = ARM_CP_STATE_AA64, 5977 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 2, .opc2 = 0, 5978 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.esr_el[3]) }, 5979 { .name = "FAR_EL3", .state = ARM_CP_STATE_AA64, 5980 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 0, 5981 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.far_el[3]) }, 5982 { .name = "SPSR_EL3", .state = ARM_CP_STATE_AA64, 5983 .type = ARM_CP_ALIAS, 5984 .opc0 = 3, .opc1 = 6, .crn = 4, .crm = 0, .opc2 = 0, 5985 .access = PL3_RW, 5986 .fieldoffset = offsetof(CPUARMState, banked_spsr[BANK_MON]) }, 5987 { .name = "VBAR_EL3", .state = ARM_CP_STATE_AA64, 5988 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 0, 5989 .access = PL3_RW, .writefn = vbar_write, 5990 .fieldoffset = offsetof(CPUARMState, cp15.vbar_el[3]), 5991 .resetvalue = 0 }, 5992 { .name = "CPTR_EL3", .state = ARM_CP_STATE_AA64, 5993 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 1, .opc2 = 2, 5994 .access = PL3_RW, .accessfn = cptr_access, .resetvalue = 0, 5995 .fieldoffset = offsetof(CPUARMState, cp15.cptr_el[3]) }, 5996 { .name = "TPIDR_EL3", .state = ARM_CP_STATE_AA64, 5997 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 2, 5998 .access = PL3_RW, .resetvalue = 0, 5999 .fieldoffset = offsetof(CPUARMState, cp15.tpidr_el[3]) }, 6000 { .name = "AMAIR_EL3", .state = ARM_CP_STATE_AA64, 6001 .opc0 = 3, .opc1 = 6, .crn = 10, .crm = 3, .opc2 = 0, 6002 .access = PL3_RW, .type = ARM_CP_CONST, 6003 .resetvalue = 0 }, 6004 { .name = "AFSR0_EL3", .state = ARM_CP_STATE_BOTH, 6005 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 0, 6006 .access = PL3_RW, .type = ARM_CP_CONST, 6007 .resetvalue = 0 }, 6008 { .name = "AFSR1_EL3", .state = ARM_CP_STATE_BOTH, 6009 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 1, .opc2 = 1, 6010 .access = PL3_RW, .type = ARM_CP_CONST, 6011 .resetvalue = 0 }, 6012 }; 6013 6014 #ifndef CONFIG_USER_ONLY 6015 6016 static CPAccessResult e2h_access(CPUARMState *env, const ARMCPRegInfo *ri, 6017 bool isread) 6018 { 6019 if (arm_current_el(env) == 1) { 6020 /* This must be a FEAT_NV access */ 6021 return CP_ACCESS_OK; 6022 } 6023 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) { 6024 return CP_ACCESS_UNDEFINED; 6025 } 6026 return CP_ACCESS_OK; 6027 } 6028 6029 static CPAccessResult access_el1nvpct(CPUARMState *env, const ARMCPRegInfo *ri, 6030 bool isread) 6031 { 6032 if (arm_current_el(env) == 1) { 6033 /* This must be a FEAT_NV access with NVx == 101 */ 6034 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVPCT)) { 6035 return CP_ACCESS_TRAP_EL2; 6036 } 6037 } 6038 return e2h_access(env, ri, isread); 6039 } 6040 6041 static CPAccessResult access_el1nvvct(CPUARMState *env, const ARMCPRegInfo *ri, 6042 bool isread) 6043 { 6044 if (arm_current_el(env) == 1) { 6045 /* This must be a FEAT_NV access with NVx == 101 */ 6046 if (FIELD_EX64(env->cp15.cnthctl_el2, CNTHCTL, EL1NVVCT)) { 6047 return CP_ACCESS_TRAP_EL2; 6048 } 6049 } 6050 return e2h_access(env, ri, isread); 6051 } 6052 6053 /* Test if system register redirection is to occur in the current state. */ 6054 static bool redirect_for_e2h(CPUARMState *env) 6055 { 6056 return arm_current_el(env) == 2 && (arm_hcr_el2_eff(env) & HCR_E2H); 6057 } 6058 6059 static uint64_t el2_e2h_read(CPUARMState *env, const ARMCPRegInfo *ri) 6060 { 6061 CPReadFn *readfn; 6062 6063 if (redirect_for_e2h(env)) { 6064 /* Switch to the saved EL2 version of the register. */ 6065 ri = ri->opaque; 6066 readfn = ri->readfn; 6067 } else { 6068 readfn = ri->orig_readfn; 6069 } 6070 if (readfn == NULL) { 6071 readfn = raw_read; 6072 } 6073 return readfn(env, ri); 6074 } 6075 6076 static void el2_e2h_write(CPUARMState *env, const ARMCPRegInfo *ri, 6077 uint64_t value) 6078 { 6079 CPWriteFn *writefn; 6080 6081 if (redirect_for_e2h(env)) { 6082 /* Switch to the saved EL2 version of the register. */ 6083 ri = ri->opaque; 6084 writefn = ri->writefn; 6085 } else { 6086 writefn = ri->orig_writefn; 6087 } 6088 if (writefn == NULL) { 6089 writefn = raw_write; 6090 } 6091 writefn(env, ri, value); 6092 } 6093 6094 static uint64_t el2_e2h_e12_read(CPUARMState *env, const ARMCPRegInfo *ri) 6095 { 6096 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */ 6097 return ri->orig_readfn(env, ri->opaque); 6098 } 6099 6100 static void el2_e2h_e12_write(CPUARMState *env, const ARMCPRegInfo *ri, 6101 uint64_t value) 6102 { 6103 /* Pass the EL1 register accessor its ri, not the EL12 alias ri */ 6104 return ri->orig_writefn(env, ri->opaque, value); 6105 } 6106 6107 static CPAccessResult el2_e2h_e12_access(CPUARMState *env, 6108 const ARMCPRegInfo *ri, 6109 bool isread) 6110 { 6111 if (arm_current_el(env) == 1) { 6112 /* 6113 * This must be a FEAT_NV access (will either trap or redirect 6114 * to memory). None of the registers with _EL12 aliases want to 6115 * apply their trap controls for this kind of access, so don't 6116 * call the orig_accessfn or do the "UNDEF when E2H is 0" check. 6117 */ 6118 return CP_ACCESS_OK; 6119 } 6120 /* FOO_EL12 aliases only exist when E2H is 1; otherwise they UNDEF */ 6121 if (!(arm_hcr_el2_eff(env) & HCR_E2H)) { 6122 return CP_ACCESS_UNDEFINED; 6123 } 6124 if (ri->orig_accessfn) { 6125 return ri->orig_accessfn(env, ri->opaque, isread); 6126 } 6127 return CP_ACCESS_OK; 6128 } 6129 6130 static void define_arm_vh_e2h_redirects_aliases(ARMCPU *cpu) 6131 { 6132 struct E2HAlias { 6133 uint32_t src_key, dst_key, new_key; 6134 const char *src_name, *dst_name, *new_name; 6135 bool (*feature)(const ARMISARegisters *id); 6136 }; 6137 6138 #define K(op0, op1, crn, crm, op2) \ 6139 ENCODE_AA64_CP_REG(CP_REG_ARM64_SYSREG_CP, crn, crm, op0, op1, op2) 6140 6141 static const struct E2HAlias aliases[] = { 6142 { K(3, 0, 1, 0, 0), K(3, 4, 1, 0, 0), K(3, 5, 1, 0, 0), 6143 "SCTLR", "SCTLR_EL2", "SCTLR_EL12" }, 6144 { K(3, 0, 1, 0, 2), K(3, 4, 1, 1, 2), K(3, 5, 1, 0, 2), 6145 "CPACR", "CPTR_EL2", "CPACR_EL12" }, 6146 { K(3, 0, 2, 0, 0), K(3, 4, 2, 0, 0), K(3, 5, 2, 0, 0), 6147 "TTBR0_EL1", "TTBR0_EL2", "TTBR0_EL12" }, 6148 { K(3, 0, 2, 0, 1), K(3, 4, 2, 0, 1), K(3, 5, 2, 0, 1), 6149 "TTBR1_EL1", "TTBR1_EL2", "TTBR1_EL12" }, 6150 { K(3, 0, 2, 0, 2), K(3, 4, 2, 0, 2), K(3, 5, 2, 0, 2), 6151 "TCR_EL1", "TCR_EL2", "TCR_EL12" }, 6152 { K(3, 0, 4, 0, 0), K(3, 4, 4, 0, 0), K(3, 5, 4, 0, 0), 6153 "SPSR_EL1", "SPSR_EL2", "SPSR_EL12" }, 6154 { K(3, 0, 4, 0, 1), K(3, 4, 4, 0, 1), K(3, 5, 4, 0, 1), 6155 "ELR_EL1", "ELR_EL2", "ELR_EL12" }, 6156 { K(3, 0, 5, 1, 0), K(3, 4, 5, 1, 0), K(3, 5, 5, 1, 0), 6157 "AFSR0_EL1", "AFSR0_EL2", "AFSR0_EL12" }, 6158 { K(3, 0, 5, 1, 1), K(3, 4, 5, 1, 1), K(3, 5, 5, 1, 1), 6159 "AFSR1_EL1", "AFSR1_EL2", "AFSR1_EL12" }, 6160 { K(3, 0, 5, 2, 0), K(3, 4, 5, 2, 0), K(3, 5, 5, 2, 0), 6161 "ESR_EL1", "ESR_EL2", "ESR_EL12" }, 6162 { K(3, 0, 6, 0, 0), K(3, 4, 6, 0, 0), K(3, 5, 6, 0, 0), 6163 "FAR_EL1", "FAR_EL2", "FAR_EL12" }, 6164 { K(3, 0, 10, 2, 0), K(3, 4, 10, 2, 0), K(3, 5, 10, 2, 0), 6165 "MAIR_EL1", "MAIR_EL2", "MAIR_EL12" }, 6166 { K(3, 0, 10, 3, 0), K(3, 4, 10, 3, 0), K(3, 5, 10, 3, 0), 6167 "AMAIR0", "AMAIR_EL2", "AMAIR_EL12" }, 6168 { K(3, 0, 12, 0, 0), K(3, 4, 12, 0, 0), K(3, 5, 12, 0, 0), 6169 "VBAR", "VBAR_EL2", "VBAR_EL12" }, 6170 { K(3, 0, 13, 0, 1), K(3, 4, 13, 0, 1), K(3, 5, 13, 0, 1), 6171 "CONTEXTIDR_EL1", "CONTEXTIDR_EL2", "CONTEXTIDR_EL12" }, 6172 { K(3, 0, 14, 1, 0), K(3, 4, 14, 1, 0), K(3, 5, 14, 1, 0), 6173 "CNTKCTL", "CNTHCTL_EL2", "CNTKCTL_EL12" }, 6174 6175 /* 6176 * Note that redirection of ZCR is mentioned in the description 6177 * of ZCR_EL2, and aliasing in the description of ZCR_EL1, but 6178 * not in the summary table. 6179 */ 6180 { K(3, 0, 1, 2, 0), K(3, 4, 1, 2, 0), K(3, 5, 1, 2, 0), 6181 "ZCR_EL1", "ZCR_EL2", "ZCR_EL12", isar_feature_aa64_sve }, 6182 { K(3, 0, 1, 2, 6), K(3, 4, 1, 2, 6), K(3, 5, 1, 2, 6), 6183 "SMCR_EL1", "SMCR_EL2", "SMCR_EL12", isar_feature_aa64_sme }, 6184 6185 { K(3, 0, 5, 6, 0), K(3, 4, 5, 6, 0), K(3, 5, 5, 6, 0), 6186 "TFSR_EL1", "TFSR_EL2", "TFSR_EL12", isar_feature_aa64_mte }, 6187 6188 { K(3, 0, 13, 0, 7), K(3, 4, 13, 0, 7), K(3, 5, 13, 0, 7), 6189 "SCXTNUM_EL1", "SCXTNUM_EL2", "SCXTNUM_EL12", 6190 isar_feature_aa64_scxtnum }, 6191 6192 /* TODO: ARMv8.2-SPE -- PMSCR_EL2 */ 6193 /* TODO: ARMv8.4-Trace -- TRFCR_EL2 */ 6194 }; 6195 #undef K 6196 6197 size_t i; 6198 6199 for (i = 0; i < ARRAY_SIZE(aliases); i++) { 6200 const struct E2HAlias *a = &aliases[i]; 6201 ARMCPRegInfo *src_reg, *dst_reg, *new_reg; 6202 bool ok; 6203 6204 if (a->feature && !a->feature(&cpu->isar)) { 6205 continue; 6206 } 6207 6208 src_reg = g_hash_table_lookup(cpu->cp_regs, 6209 (gpointer)(uintptr_t)a->src_key); 6210 dst_reg = g_hash_table_lookup(cpu->cp_regs, 6211 (gpointer)(uintptr_t)a->dst_key); 6212 g_assert(src_reg != NULL); 6213 g_assert(dst_reg != NULL); 6214 6215 /* Cross-compare names to detect typos in the keys. */ 6216 g_assert(strcmp(src_reg->name, a->src_name) == 0); 6217 g_assert(strcmp(dst_reg->name, a->dst_name) == 0); 6218 6219 /* None of the core system registers use opaque; we will. */ 6220 g_assert(src_reg->opaque == NULL); 6221 6222 /* Create alias before redirection so we dup the right data. */ 6223 new_reg = g_memdup(src_reg, sizeof(ARMCPRegInfo)); 6224 6225 new_reg->name = a->new_name; 6226 new_reg->type |= ARM_CP_ALIAS; 6227 /* Remove PL1/PL0 access, leaving PL2/PL3 R/W in place. */ 6228 new_reg->access &= PL2_RW | PL3_RW; 6229 /* The new_reg op fields are as per new_key, not the target reg */ 6230 new_reg->crn = (a->new_key & CP_REG_ARM64_SYSREG_CRN_MASK) 6231 >> CP_REG_ARM64_SYSREG_CRN_SHIFT; 6232 new_reg->crm = (a->new_key & CP_REG_ARM64_SYSREG_CRM_MASK) 6233 >> CP_REG_ARM64_SYSREG_CRM_SHIFT; 6234 new_reg->opc0 = (a->new_key & CP_REG_ARM64_SYSREG_OP0_MASK) 6235 >> CP_REG_ARM64_SYSREG_OP0_SHIFT; 6236 new_reg->opc1 = (a->new_key & CP_REG_ARM64_SYSREG_OP1_MASK) 6237 >> CP_REG_ARM64_SYSREG_OP1_SHIFT; 6238 new_reg->opc2 = (a->new_key & CP_REG_ARM64_SYSREG_OP2_MASK) 6239 >> CP_REG_ARM64_SYSREG_OP2_SHIFT; 6240 new_reg->opaque = src_reg; 6241 new_reg->orig_readfn = src_reg->readfn ?: raw_read; 6242 new_reg->orig_writefn = src_reg->writefn ?: raw_write; 6243 new_reg->orig_accessfn = src_reg->accessfn; 6244 if (!new_reg->raw_readfn) { 6245 new_reg->raw_readfn = raw_read; 6246 } 6247 if (!new_reg->raw_writefn) { 6248 new_reg->raw_writefn = raw_write; 6249 } 6250 new_reg->readfn = el2_e2h_e12_read; 6251 new_reg->writefn = el2_e2h_e12_write; 6252 new_reg->accessfn = el2_e2h_e12_access; 6253 6254 /* 6255 * If the _EL1 register is redirected to memory by FEAT_NV2, 6256 * then it shares the offset with the _EL12 register, 6257 * and which one is redirected depends on HCR_EL2.NV1. 6258 */ 6259 if (new_reg->nv2_redirect_offset) { 6260 assert(new_reg->nv2_redirect_offset & NV2_REDIR_NV1); 6261 new_reg->nv2_redirect_offset &= ~NV2_REDIR_NV1; 6262 new_reg->nv2_redirect_offset |= NV2_REDIR_NO_NV1; 6263 } 6264 6265 ok = g_hash_table_insert(cpu->cp_regs, 6266 (gpointer)(uintptr_t)a->new_key, new_reg); 6267 g_assert(ok); 6268 6269 src_reg->opaque = dst_reg; 6270 src_reg->orig_readfn = src_reg->readfn ?: raw_read; 6271 src_reg->orig_writefn = src_reg->writefn ?: raw_write; 6272 if (!src_reg->raw_readfn) { 6273 src_reg->raw_readfn = raw_read; 6274 } 6275 if (!src_reg->raw_writefn) { 6276 src_reg->raw_writefn = raw_write; 6277 } 6278 src_reg->readfn = el2_e2h_read; 6279 src_reg->writefn = el2_e2h_write; 6280 } 6281 } 6282 #endif 6283 6284 static CPAccessResult ctr_el0_access(CPUARMState *env, const ARMCPRegInfo *ri, 6285 bool isread) 6286 { 6287 int cur_el = arm_current_el(env); 6288 6289 if (cur_el < 2) { 6290 uint64_t hcr = arm_hcr_el2_eff(env); 6291 6292 if (cur_el == 0) { 6293 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 6294 if (!(env->cp15.sctlr_el[2] & SCTLR_UCT)) { 6295 return CP_ACCESS_TRAP_EL2; 6296 } 6297 } else { 6298 if (!(env->cp15.sctlr_el[1] & SCTLR_UCT)) { 6299 return CP_ACCESS_TRAP_EL1; 6300 } 6301 if (hcr & HCR_TID2) { 6302 return CP_ACCESS_TRAP_EL2; 6303 } 6304 } 6305 } else if (hcr & HCR_TID2) { 6306 return CP_ACCESS_TRAP_EL2; 6307 } 6308 } 6309 6310 if (arm_current_el(env) < 2 && arm_hcr_el2_eff(env) & HCR_TID2) { 6311 return CP_ACCESS_TRAP_EL2; 6312 } 6313 6314 return CP_ACCESS_OK; 6315 } 6316 6317 /* 6318 * Check for traps to RAS registers, which are controlled 6319 * by HCR_EL2.TERR and SCR_EL3.TERR. 6320 */ 6321 static CPAccessResult access_terr(CPUARMState *env, const ARMCPRegInfo *ri, 6322 bool isread) 6323 { 6324 int el = arm_current_el(env); 6325 6326 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TERR)) { 6327 return CP_ACCESS_TRAP_EL2; 6328 } 6329 if (!arm_is_el3_or_mon(env) && (env->cp15.scr_el3 & SCR_TERR)) { 6330 return CP_ACCESS_TRAP_EL3; 6331 } 6332 return CP_ACCESS_OK; 6333 } 6334 6335 static uint64_t disr_read(CPUARMState *env, const ARMCPRegInfo *ri) 6336 { 6337 int el = arm_current_el(env); 6338 6339 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) { 6340 return env->cp15.vdisr_el2; 6341 } 6342 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) { 6343 return 0; /* RAZ/WI */ 6344 } 6345 return env->cp15.disr_el1; 6346 } 6347 6348 static void disr_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 6349 { 6350 int el = arm_current_el(env); 6351 6352 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_AMO)) { 6353 env->cp15.vdisr_el2 = val; 6354 return; 6355 } 6356 if (el < 3 && (env->cp15.scr_el3 & SCR_EA)) { 6357 return; /* RAZ/WI */ 6358 } 6359 env->cp15.disr_el1 = val; 6360 } 6361 6362 /* 6363 * Minimal RAS implementation with no Error Records. 6364 * Which means that all of the Error Record registers: 6365 * ERXADDR_EL1 6366 * ERXCTLR_EL1 6367 * ERXFR_EL1 6368 * ERXMISC0_EL1 6369 * ERXMISC1_EL1 6370 * ERXMISC2_EL1 6371 * ERXMISC3_EL1 6372 * ERXPFGCDN_EL1 (RASv1p1) 6373 * ERXPFGCTL_EL1 (RASv1p1) 6374 * ERXPFGF_EL1 (RASv1p1) 6375 * ERXSTATUS_EL1 6376 * and 6377 * ERRSELR_EL1 6378 * may generate UNDEFINED, which is the effect we get by not 6379 * listing them at all. 6380 * 6381 * These registers have fine-grained trap bits, but UNDEF-to-EL1 6382 * is higher priority than FGT-to-EL2 so we do not need to list them 6383 * in order to check for an FGT. 6384 */ 6385 static const ARMCPRegInfo minimal_ras_reginfo[] = { 6386 { .name = "DISR_EL1", .state = ARM_CP_STATE_BOTH, 6387 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 1, .opc2 = 1, 6388 .access = PL1_RW, .fieldoffset = offsetof(CPUARMState, cp15.disr_el1), 6389 .readfn = disr_read, .writefn = disr_write, .raw_writefn = raw_write }, 6390 { .name = "ERRIDR_EL1", .state = ARM_CP_STATE_BOTH, 6391 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 3, .opc2 = 0, 6392 .access = PL1_R, .accessfn = access_terr, 6393 .fgt = FGT_ERRIDR_EL1, 6394 .type = ARM_CP_CONST, .resetvalue = 0 }, 6395 { .name = "VDISR_EL2", .state = ARM_CP_STATE_BOTH, 6396 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 1, .opc2 = 1, 6397 .nv2_redirect_offset = 0x500, 6398 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vdisr_el2) }, 6399 { .name = "VSESR_EL2", .state = ARM_CP_STATE_BOTH, 6400 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 2, .opc2 = 3, 6401 .nv2_redirect_offset = 0x508, 6402 .access = PL2_RW, .fieldoffset = offsetof(CPUARMState, cp15.vsesr_el2) }, 6403 }; 6404 6405 /* 6406 * Return the exception level to which exceptions should be taken 6407 * via SVEAccessTrap. This excludes the check for whether the exception 6408 * should be routed through AArch64.AdvSIMDFPAccessTrap. That can easily 6409 * be found by testing 0 < fp_exception_el < sve_exception_el. 6410 * 6411 * C.f. the ARM pseudocode function CheckSVEEnabled. Note that the 6412 * pseudocode does *not* separate out the FP trap checks, but has them 6413 * all in one function. 6414 */ 6415 int sve_exception_el(CPUARMState *env, int el) 6416 { 6417 #ifndef CONFIG_USER_ONLY 6418 if (el <= 1 && !el_is_in_host(env, el)) { 6419 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, ZEN)) { 6420 case 1: 6421 if (el != 0) { 6422 break; 6423 } 6424 /* fall through */ 6425 case 0: 6426 case 2: 6427 return 1; 6428 } 6429 } 6430 6431 if (el <= 2 && arm_is_el2_enabled(env)) { 6432 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */ 6433 if (env->cp15.hcr_el2 & HCR_E2H) { 6434 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, ZEN)) { 6435 case 1: 6436 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) { 6437 break; 6438 } 6439 /* fall through */ 6440 case 0: 6441 case 2: 6442 return 2; 6443 } 6444 } else { 6445 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TZ)) { 6446 return 2; 6447 } 6448 } 6449 } 6450 6451 /* CPTR_EL3. Since EZ is negative we must check for EL3. */ 6452 if (arm_feature(env, ARM_FEATURE_EL3) 6453 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, EZ)) { 6454 return 3; 6455 } 6456 #endif 6457 return 0; 6458 } 6459 6460 /* 6461 * Return the exception level to which exceptions should be taken for SME. 6462 * C.f. the ARM pseudocode function CheckSMEAccess. 6463 */ 6464 int sme_exception_el(CPUARMState *env, int el) 6465 { 6466 #ifndef CONFIG_USER_ONLY 6467 if (el <= 1 && !el_is_in_host(env, el)) { 6468 switch (FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, SMEN)) { 6469 case 1: 6470 if (el != 0) { 6471 break; 6472 } 6473 /* fall through */ 6474 case 0: 6475 case 2: 6476 return 1; 6477 } 6478 } 6479 6480 if (el <= 2 && arm_is_el2_enabled(env)) { 6481 /* CPTR_EL2 changes format with HCR_EL2.E2H (regardless of TGE). */ 6482 if (env->cp15.hcr_el2 & HCR_E2H) { 6483 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, SMEN)) { 6484 case 1: 6485 if (el != 0 || !(env->cp15.hcr_el2 & HCR_TGE)) { 6486 break; 6487 } 6488 /* fall through */ 6489 case 0: 6490 case 2: 6491 return 2; 6492 } 6493 } else { 6494 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TSM)) { 6495 return 2; 6496 } 6497 } 6498 } 6499 6500 /* CPTR_EL3. Since ESM is negative we must check for EL3. */ 6501 if (arm_feature(env, ARM_FEATURE_EL3) 6502 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) { 6503 return 3; 6504 } 6505 #endif 6506 return 0; 6507 } 6508 6509 /* 6510 * Given that SVE is enabled, return the vector length for EL. 6511 */ 6512 uint32_t sve_vqm1_for_el_sm(CPUARMState *env, int el, bool sm) 6513 { 6514 ARMCPU *cpu = env_archcpu(env); 6515 uint64_t *cr = env->vfp.zcr_el; 6516 uint32_t map = cpu->sve_vq.map; 6517 uint32_t len = ARM_MAX_VQ - 1; 6518 6519 if (sm) { 6520 cr = env->vfp.smcr_el; 6521 map = cpu->sme_vq.map; 6522 } 6523 6524 if (el <= 1 && !el_is_in_host(env, el)) { 6525 len = MIN(len, 0xf & (uint32_t)cr[1]); 6526 } 6527 if (el <= 2 && arm_is_el2_enabled(env)) { 6528 len = MIN(len, 0xf & (uint32_t)cr[2]); 6529 } 6530 if (arm_feature(env, ARM_FEATURE_EL3)) { 6531 len = MIN(len, 0xf & (uint32_t)cr[3]); 6532 } 6533 6534 map &= MAKE_64BIT_MASK(0, len + 1); 6535 if (map != 0) { 6536 return 31 - clz32(map); 6537 } 6538 6539 /* Bit 0 is always set for Normal SVE -- not so for Streaming SVE. */ 6540 assert(sm); 6541 return ctz32(cpu->sme_vq.map); 6542 } 6543 6544 uint32_t sve_vqm1_for_el(CPUARMState *env, int el) 6545 { 6546 return sve_vqm1_for_el_sm(env, el, FIELD_EX64(env->svcr, SVCR, SM)); 6547 } 6548 6549 static void zcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6550 uint64_t value) 6551 { 6552 int cur_el = arm_current_el(env); 6553 int old_len = sve_vqm1_for_el(env, cur_el); 6554 int new_len; 6555 6556 /* Bits other than [3:0] are RAZ/WI. */ 6557 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > 16); 6558 raw_write(env, ri, value & 0xf); 6559 6560 /* 6561 * Because we arrived here, we know both FP and SVE are enabled; 6562 * otherwise we would have trapped access to the ZCR_ELn register. 6563 */ 6564 new_len = sve_vqm1_for_el(env, cur_el); 6565 if (new_len < old_len) { 6566 aarch64_sve_narrow_vq(env, new_len + 1); 6567 } 6568 } 6569 6570 static const ARMCPRegInfo zcr_reginfo[] = { 6571 { .name = "ZCR_EL1", .state = ARM_CP_STATE_AA64, 6572 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 0, 6573 .nv2_redirect_offset = 0x1e0 | NV2_REDIR_NV1, 6574 .access = PL1_RW, .type = ARM_CP_SVE, 6575 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[1]), 6576 .writefn = zcr_write, .raw_writefn = raw_write }, 6577 { .name = "ZCR_EL2", .state = ARM_CP_STATE_AA64, 6578 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 0, 6579 .access = PL2_RW, .type = ARM_CP_SVE, 6580 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[2]), 6581 .writefn = zcr_write, .raw_writefn = raw_write }, 6582 { .name = "ZCR_EL3", .state = ARM_CP_STATE_AA64, 6583 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 0, 6584 .access = PL3_RW, .type = ARM_CP_SVE, 6585 .fieldoffset = offsetof(CPUARMState, vfp.zcr_el[3]), 6586 .writefn = zcr_write, .raw_writefn = raw_write }, 6587 }; 6588 6589 #ifdef TARGET_AARCH64 6590 static CPAccessResult access_tpidr2(CPUARMState *env, const ARMCPRegInfo *ri, 6591 bool isread) 6592 { 6593 int el = arm_current_el(env); 6594 6595 if (el == 0) { 6596 uint64_t sctlr = arm_sctlr(env, el); 6597 if (!(sctlr & SCTLR_EnTP2)) { 6598 return CP_ACCESS_TRAP_EL1; 6599 } 6600 } 6601 /* TODO: FEAT_FGT */ 6602 if (el < 3 6603 && arm_feature(env, ARM_FEATURE_EL3) 6604 && !(env->cp15.scr_el3 & SCR_ENTP2)) { 6605 return CP_ACCESS_TRAP_EL3; 6606 } 6607 return CP_ACCESS_OK; 6608 } 6609 6610 static CPAccessResult access_smprimap(CPUARMState *env, const ARMCPRegInfo *ri, 6611 bool isread) 6612 { 6613 /* If EL1 this is a FEAT_NV access and CPTR_EL3.ESM doesn't apply */ 6614 if (arm_current_el(env) == 2 6615 && arm_feature(env, ARM_FEATURE_EL3) 6616 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) { 6617 return CP_ACCESS_TRAP_EL3; 6618 } 6619 return CP_ACCESS_OK; 6620 } 6621 6622 static CPAccessResult access_smpri(CPUARMState *env, const ARMCPRegInfo *ri, 6623 bool isread) 6624 { 6625 if (arm_current_el(env) < 3 6626 && arm_feature(env, ARM_FEATURE_EL3) 6627 && !FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, ESM)) { 6628 return CP_ACCESS_TRAP_EL3; 6629 } 6630 return CP_ACCESS_OK; 6631 } 6632 6633 /* ResetSVEState */ 6634 static void arm_reset_sve_state(CPUARMState *env) 6635 { 6636 memset(env->vfp.zregs, 0, sizeof(env->vfp.zregs)); 6637 /* Recall that FFR is stored as pregs[16]. */ 6638 memset(env->vfp.pregs, 0, sizeof(env->vfp.pregs)); 6639 vfp_set_fpsr(env, 0x0800009f); 6640 } 6641 6642 void aarch64_set_svcr(CPUARMState *env, uint64_t new, uint64_t mask) 6643 { 6644 uint64_t change = (env->svcr ^ new) & mask; 6645 6646 if (change == 0) { 6647 return; 6648 } 6649 env->svcr ^= change; 6650 6651 if (change & R_SVCR_SM_MASK) { 6652 arm_reset_sve_state(env); 6653 } 6654 6655 /* 6656 * ResetSMEState. 6657 * 6658 * SetPSTATE_ZA zeros on enable and disable. We can zero this only 6659 * on enable: while disabled, the storage is inaccessible and the 6660 * value does not matter. We're not saving the storage in vmstate 6661 * when disabled either. 6662 */ 6663 if (change & new & R_SVCR_ZA_MASK) { 6664 memset(env->zarray, 0, sizeof(env->zarray)); 6665 } 6666 6667 if (tcg_enabled()) { 6668 arm_rebuild_hflags(env); 6669 } 6670 } 6671 6672 static void svcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6673 uint64_t value) 6674 { 6675 aarch64_set_svcr(env, value, -1); 6676 } 6677 6678 static void smcr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6679 uint64_t value) 6680 { 6681 int cur_el = arm_current_el(env); 6682 int old_len = sve_vqm1_for_el(env, cur_el); 6683 int new_len; 6684 6685 QEMU_BUILD_BUG_ON(ARM_MAX_VQ > R_SMCR_LEN_MASK + 1); 6686 value &= R_SMCR_LEN_MASK | R_SMCR_FA64_MASK; 6687 raw_write(env, ri, value); 6688 6689 /* 6690 * Note that it is CONSTRAINED UNPREDICTABLE what happens to ZA storage 6691 * when SVL is widened (old values kept, or zeros). Choose to keep the 6692 * current values for simplicity. But for QEMU internals, we must still 6693 * apply the narrower SVL to the Zregs and Pregs -- see the comment 6694 * above aarch64_sve_narrow_vq. 6695 */ 6696 new_len = sve_vqm1_for_el(env, cur_el); 6697 if (new_len < old_len) { 6698 aarch64_sve_narrow_vq(env, new_len + 1); 6699 } 6700 } 6701 6702 static const ARMCPRegInfo sme_reginfo[] = { 6703 { .name = "TPIDR2_EL0", .state = ARM_CP_STATE_AA64, 6704 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 5, 6705 .access = PL0_RW, .accessfn = access_tpidr2, 6706 .fgt = FGT_NTPIDR2_EL0, 6707 .fieldoffset = offsetof(CPUARMState, cp15.tpidr2_el0) }, 6708 { .name = "SVCR", .state = ARM_CP_STATE_AA64, 6709 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 2, 6710 .access = PL0_RW, .type = ARM_CP_SME, 6711 .fieldoffset = offsetof(CPUARMState, svcr), 6712 .writefn = svcr_write, .raw_writefn = raw_write }, 6713 { .name = "SMCR_EL1", .state = ARM_CP_STATE_AA64, 6714 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 6, 6715 .nv2_redirect_offset = 0x1f0 | NV2_REDIR_NV1, 6716 .access = PL1_RW, .type = ARM_CP_SME, 6717 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[1]), 6718 .writefn = smcr_write, .raw_writefn = raw_write }, 6719 { .name = "SMCR_EL2", .state = ARM_CP_STATE_AA64, 6720 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 6, 6721 .access = PL2_RW, .type = ARM_CP_SME, 6722 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[2]), 6723 .writefn = smcr_write, .raw_writefn = raw_write }, 6724 { .name = "SMCR_EL3", .state = ARM_CP_STATE_AA64, 6725 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 2, .opc2 = 6, 6726 .access = PL3_RW, .type = ARM_CP_SME, 6727 .fieldoffset = offsetof(CPUARMState, vfp.smcr_el[3]), 6728 .writefn = smcr_write, .raw_writefn = raw_write }, 6729 { .name = "SMIDR_EL1", .state = ARM_CP_STATE_AA64, 6730 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 6, 6731 .access = PL1_R, .accessfn = access_aa64_tid1, 6732 /* 6733 * IMPLEMENTOR = 0 (software) 6734 * REVISION = 0 (implementation defined) 6735 * SMPS = 0 (no streaming execution priority in QEMU) 6736 * AFFINITY = 0 (streaming sve mode not shared with other PEs) 6737 */ 6738 .type = ARM_CP_CONST, .resetvalue = 0, }, 6739 /* 6740 * Because SMIDR_EL1.SMPS is 0, SMPRI_EL1 and SMPRIMAP_EL2 are RES 0. 6741 */ 6742 { .name = "SMPRI_EL1", .state = ARM_CP_STATE_AA64, 6743 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 2, .opc2 = 4, 6744 .access = PL1_RW, .accessfn = access_smpri, 6745 .fgt = FGT_NSMPRI_EL1, 6746 .type = ARM_CP_CONST, .resetvalue = 0 }, 6747 { .name = "SMPRIMAP_EL2", .state = ARM_CP_STATE_AA64, 6748 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 2, .opc2 = 5, 6749 .nv2_redirect_offset = 0x1f8, 6750 .access = PL2_RW, .accessfn = access_smprimap, 6751 .type = ARM_CP_CONST, .resetvalue = 0 }, 6752 }; 6753 6754 static void gpccr_write(CPUARMState *env, const ARMCPRegInfo *ri, 6755 uint64_t value) 6756 { 6757 /* L0GPTSZ is RO; other bits not mentioned are RES0. */ 6758 uint64_t rw_mask = R_GPCCR_PPS_MASK | R_GPCCR_IRGN_MASK | 6759 R_GPCCR_ORGN_MASK | R_GPCCR_SH_MASK | R_GPCCR_PGS_MASK | 6760 R_GPCCR_GPC_MASK | R_GPCCR_GPCP_MASK; 6761 6762 env->cp15.gpccr_el3 = (value & rw_mask) | (env->cp15.gpccr_el3 & ~rw_mask); 6763 } 6764 6765 static void gpccr_reset(CPUARMState *env, const ARMCPRegInfo *ri) 6766 { 6767 env->cp15.gpccr_el3 = FIELD_DP64(0, GPCCR, L0GPTSZ, 6768 env_archcpu(env)->reset_l0gptsz); 6769 } 6770 6771 static const ARMCPRegInfo rme_reginfo[] = { 6772 { .name = "GPCCR_EL3", .state = ARM_CP_STATE_AA64, 6773 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 6, 6774 .access = PL3_RW, .writefn = gpccr_write, .resetfn = gpccr_reset, 6775 .fieldoffset = offsetof(CPUARMState, cp15.gpccr_el3) }, 6776 { .name = "GPTBR_EL3", .state = ARM_CP_STATE_AA64, 6777 .opc0 = 3, .opc1 = 6, .crn = 2, .crm = 1, .opc2 = 4, 6778 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.gptbr_el3) }, 6779 { .name = "MFAR_EL3", .state = ARM_CP_STATE_AA64, 6780 .opc0 = 3, .opc1 = 6, .crn = 6, .crm = 0, .opc2 = 5, 6781 .access = PL3_RW, .fieldoffset = offsetof(CPUARMState, cp15.mfar_el3) }, 6782 { .name = "DC_CIPAPA", .state = ARM_CP_STATE_AA64, 6783 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 1, 6784 .access = PL3_W, .type = ARM_CP_NOP }, 6785 }; 6786 6787 static const ARMCPRegInfo rme_mte_reginfo[] = { 6788 { .name = "DC_CIGDPAPA", .state = ARM_CP_STATE_AA64, 6789 .opc0 = 1, .opc1 = 6, .crn = 7, .crm = 14, .opc2 = 5, 6790 .access = PL3_W, .type = ARM_CP_NOP }, 6791 }; 6792 6793 static void aa64_allint_write(CPUARMState *env, const ARMCPRegInfo *ri, 6794 uint64_t value) 6795 { 6796 env->pstate = (env->pstate & ~PSTATE_ALLINT) | (value & PSTATE_ALLINT); 6797 } 6798 6799 static uint64_t aa64_allint_read(CPUARMState *env, const ARMCPRegInfo *ri) 6800 { 6801 return env->pstate & PSTATE_ALLINT; 6802 } 6803 6804 static CPAccessResult aa64_allint_access(CPUARMState *env, 6805 const ARMCPRegInfo *ri, bool isread) 6806 { 6807 if (!isread && arm_current_el(env) == 1 && 6808 (arm_hcrx_el2_eff(env) & HCRX_TALLINT)) { 6809 return CP_ACCESS_TRAP_EL2; 6810 } 6811 return CP_ACCESS_OK; 6812 } 6813 6814 static const ARMCPRegInfo nmi_reginfo[] = { 6815 { .name = "ALLINT", .state = ARM_CP_STATE_AA64, 6816 .opc0 = 3, .opc1 = 0, .opc2 = 0, .crn = 4, .crm = 3, 6817 .type = ARM_CP_NO_RAW, 6818 .access = PL1_RW, .accessfn = aa64_allint_access, 6819 .fieldoffset = offsetof(CPUARMState, pstate), 6820 .writefn = aa64_allint_write, .readfn = aa64_allint_read, 6821 .resetfn = arm_cp_reset_ignore }, 6822 }; 6823 #endif /* TARGET_AARCH64 */ 6824 6825 static void define_pmu_regs(ARMCPU *cpu) 6826 { 6827 /* 6828 * v7 performance monitor control register: same implementor 6829 * field as main ID register, and we implement four counters in 6830 * addition to the cycle count register. 6831 */ 6832 unsigned int i, pmcrn = pmu_num_counters(&cpu->env); 6833 ARMCPRegInfo pmcr = { 6834 .name = "PMCR", .cp = 15, .crn = 9, .crm = 12, .opc1 = 0, .opc2 = 0, 6835 .access = PL0_RW, 6836 .fgt = FGT_PMCR_EL0, 6837 .type = ARM_CP_IO | ARM_CP_ALIAS, 6838 .fieldoffset = offsetoflow32(CPUARMState, cp15.c9_pmcr), 6839 .accessfn = pmreg_access, 6840 .readfn = pmcr_read, .raw_readfn = raw_read, 6841 .writefn = pmcr_write, .raw_writefn = raw_write, 6842 }; 6843 ARMCPRegInfo pmcr64 = { 6844 .name = "PMCR_EL0", .state = ARM_CP_STATE_AA64, 6845 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 0, 6846 .access = PL0_RW, .accessfn = pmreg_access, 6847 .fgt = FGT_PMCR_EL0, 6848 .type = ARM_CP_IO, 6849 .fieldoffset = offsetof(CPUARMState, cp15.c9_pmcr), 6850 .resetvalue = cpu->isar.reset_pmcr_el0, 6851 .readfn = pmcr_read, .raw_readfn = raw_read, 6852 .writefn = pmcr_write, .raw_writefn = raw_write, 6853 }; 6854 6855 define_one_arm_cp_reg(cpu, &pmcr); 6856 define_one_arm_cp_reg(cpu, &pmcr64); 6857 for (i = 0; i < pmcrn; i++) { 6858 char *pmevcntr_name = g_strdup_printf("PMEVCNTR%d", i); 6859 char *pmevcntr_el0_name = g_strdup_printf("PMEVCNTR%d_EL0", i); 6860 char *pmevtyper_name = g_strdup_printf("PMEVTYPER%d", i); 6861 char *pmevtyper_el0_name = g_strdup_printf("PMEVTYPER%d_EL0", i); 6862 ARMCPRegInfo pmev_regs[] = { 6863 { .name = pmevcntr_name, .cp = 15, .crn = 14, 6864 .crm = 8 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 6865 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 6866 .fgt = FGT_PMEVCNTRN_EL0, 6867 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 6868 .accessfn = pmreg_access_xevcntr }, 6869 { .name = pmevcntr_el0_name, .state = ARM_CP_STATE_AA64, 6870 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 8 | (3 & (i >> 3)), 6871 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access_xevcntr, 6872 .type = ARM_CP_IO, 6873 .fgt = FGT_PMEVCNTRN_EL0, 6874 .readfn = pmevcntr_readfn, .writefn = pmevcntr_writefn, 6875 .raw_readfn = pmevcntr_rawread, 6876 .raw_writefn = pmevcntr_rawwrite }, 6877 { .name = pmevtyper_name, .cp = 15, .crn = 14, 6878 .crm = 12 | (3 & (i >> 3)), .opc1 = 0, .opc2 = i & 7, 6879 .access = PL0_RW, .type = ARM_CP_IO | ARM_CP_ALIAS, 6880 .fgt = FGT_PMEVTYPERN_EL0, 6881 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 6882 .accessfn = pmreg_access }, 6883 { .name = pmevtyper_el0_name, .state = ARM_CP_STATE_AA64, 6884 .opc0 = 3, .opc1 = 3, .crn = 14, .crm = 12 | (3 & (i >> 3)), 6885 .opc2 = i & 7, .access = PL0_RW, .accessfn = pmreg_access, 6886 .fgt = FGT_PMEVTYPERN_EL0, 6887 .type = ARM_CP_IO, 6888 .readfn = pmevtyper_readfn, .writefn = pmevtyper_writefn, 6889 .raw_writefn = pmevtyper_rawwrite }, 6890 }; 6891 define_arm_cp_regs(cpu, pmev_regs); 6892 g_free(pmevcntr_name); 6893 g_free(pmevcntr_el0_name); 6894 g_free(pmevtyper_name); 6895 g_free(pmevtyper_el0_name); 6896 } 6897 if (cpu_isar_feature(aa32_pmuv3p1, cpu)) { 6898 ARMCPRegInfo v81_pmu_regs[] = { 6899 { .name = "PMCEID2", .state = ARM_CP_STATE_AA32, 6900 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 4, 6901 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6902 .fgt = FGT_PMCEIDN_EL0, 6903 .resetvalue = extract64(cpu->pmceid0, 32, 32) }, 6904 { .name = "PMCEID3", .state = ARM_CP_STATE_AA32, 6905 .cp = 15, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 5, 6906 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6907 .fgt = FGT_PMCEIDN_EL0, 6908 .resetvalue = extract64(cpu->pmceid1, 32, 32) }, 6909 }; 6910 define_arm_cp_regs(cpu, v81_pmu_regs); 6911 } 6912 if (cpu_isar_feature(any_pmuv3p4, cpu)) { 6913 static const ARMCPRegInfo v84_pmmir = { 6914 .name = "PMMIR_EL1", .state = ARM_CP_STATE_BOTH, 6915 .opc0 = 3, .opc1 = 0, .crn = 9, .crm = 14, .opc2 = 6, 6916 .access = PL1_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 6917 .fgt = FGT_PMMIR_EL1, 6918 .resetvalue = 0 6919 }; 6920 define_one_arm_cp_reg(cpu, &v84_pmmir); 6921 } 6922 } 6923 6924 #ifndef CONFIG_USER_ONLY 6925 /* 6926 * We don't know until after realize whether there's a GICv3 6927 * attached, and that is what registers the gicv3 sysregs. 6928 * So we have to fill in the GIC fields in ID_PFR/ID_PFR1_EL1/ID_AA64PFR0_EL1 6929 * at runtime. 6930 */ 6931 static uint64_t id_pfr1_read(CPUARMState *env, const ARMCPRegInfo *ri) 6932 { 6933 ARMCPU *cpu = env_archcpu(env); 6934 uint64_t pfr1 = cpu->isar.id_pfr1; 6935 6936 if (env->gicv3state) { 6937 pfr1 |= 1 << 28; 6938 } 6939 return pfr1; 6940 } 6941 6942 static uint64_t id_aa64pfr0_read(CPUARMState *env, const ARMCPRegInfo *ri) 6943 { 6944 ARMCPU *cpu = env_archcpu(env); 6945 uint64_t pfr0 = cpu->isar.id_aa64pfr0; 6946 6947 if (env->gicv3state) { 6948 pfr0 |= 1 << 24; 6949 } 6950 return pfr0; 6951 } 6952 #endif 6953 6954 /* 6955 * Shared logic between LORID and the rest of the LOR* registers. 6956 * Secure state exclusion has already been dealt with. 6957 */ 6958 static CPAccessResult access_lor_ns(CPUARMState *env, 6959 const ARMCPRegInfo *ri, bool isread) 6960 { 6961 int el = arm_current_el(env); 6962 6963 if (el < 2 && (arm_hcr_el2_eff(env) & HCR_TLOR)) { 6964 return CP_ACCESS_TRAP_EL2; 6965 } 6966 if (el < 3 && (env->cp15.scr_el3 & SCR_TLOR)) { 6967 return CP_ACCESS_TRAP_EL3; 6968 } 6969 return CP_ACCESS_OK; 6970 } 6971 6972 static CPAccessResult access_lor_other(CPUARMState *env, 6973 const ARMCPRegInfo *ri, bool isread) 6974 { 6975 if (arm_is_secure_below_el3(env)) { 6976 /* UNDEF if SCR_EL3.NS == 0 */ 6977 return CP_ACCESS_UNDEFINED; 6978 } 6979 return access_lor_ns(env, ri, isread); 6980 } 6981 6982 /* 6983 * A trivial implementation of ARMv8.1-LOR leaves all of these 6984 * registers fixed at 0, which indicates that there are zero 6985 * supported Limited Ordering regions. 6986 */ 6987 static const ARMCPRegInfo lor_reginfo[] = { 6988 { .name = "LORSA_EL1", .state = ARM_CP_STATE_AA64, 6989 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 0, 6990 .access = PL1_RW, .accessfn = access_lor_other, 6991 .fgt = FGT_LORSA_EL1, 6992 .type = ARM_CP_CONST, .resetvalue = 0 }, 6993 { .name = "LOREA_EL1", .state = ARM_CP_STATE_AA64, 6994 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 1, 6995 .access = PL1_RW, .accessfn = access_lor_other, 6996 .fgt = FGT_LOREA_EL1, 6997 .type = ARM_CP_CONST, .resetvalue = 0 }, 6998 { .name = "LORN_EL1", .state = ARM_CP_STATE_AA64, 6999 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 2, 7000 .access = PL1_RW, .accessfn = access_lor_other, 7001 .fgt = FGT_LORN_EL1, 7002 .type = ARM_CP_CONST, .resetvalue = 0 }, 7003 { .name = "LORC_EL1", .state = ARM_CP_STATE_AA64, 7004 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 3, 7005 .access = PL1_RW, .accessfn = access_lor_other, 7006 .fgt = FGT_LORC_EL1, 7007 .type = ARM_CP_CONST, .resetvalue = 0 }, 7008 { .name = "LORID_EL1", .state = ARM_CP_STATE_AA64, 7009 .opc0 = 3, .opc1 = 0, .crn = 10, .crm = 4, .opc2 = 7, 7010 .access = PL1_R, .accessfn = access_lor_ns, 7011 .fgt = FGT_LORID_EL1, 7012 .type = ARM_CP_CONST, .resetvalue = 0 }, 7013 }; 7014 7015 #ifdef TARGET_AARCH64 7016 static CPAccessResult access_pauth(CPUARMState *env, const ARMCPRegInfo *ri, 7017 bool isread) 7018 { 7019 int el = arm_current_el(env); 7020 7021 if (el < 2 && 7022 arm_is_el2_enabled(env) && 7023 !(arm_hcr_el2_eff(env) & HCR_APK)) { 7024 return CP_ACCESS_TRAP_EL2; 7025 } 7026 if (el < 3 && 7027 arm_feature(env, ARM_FEATURE_EL3) && 7028 !(env->cp15.scr_el3 & SCR_APK)) { 7029 return CP_ACCESS_TRAP_EL3; 7030 } 7031 return CP_ACCESS_OK; 7032 } 7033 7034 static const ARMCPRegInfo pauth_reginfo[] = { 7035 { .name = "APDAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7036 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 0, 7037 .access = PL1_RW, .accessfn = access_pauth, 7038 .fgt = FGT_APDAKEY, 7039 .fieldoffset = offsetof(CPUARMState, keys.apda.lo) }, 7040 { .name = "APDAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7041 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 1, 7042 .access = PL1_RW, .accessfn = access_pauth, 7043 .fgt = FGT_APDAKEY, 7044 .fieldoffset = offsetof(CPUARMState, keys.apda.hi) }, 7045 { .name = "APDBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7046 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 2, 7047 .access = PL1_RW, .accessfn = access_pauth, 7048 .fgt = FGT_APDBKEY, 7049 .fieldoffset = offsetof(CPUARMState, keys.apdb.lo) }, 7050 { .name = "APDBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7051 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 2, .opc2 = 3, 7052 .access = PL1_RW, .accessfn = access_pauth, 7053 .fgt = FGT_APDBKEY, 7054 .fieldoffset = offsetof(CPUARMState, keys.apdb.hi) }, 7055 { .name = "APGAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7056 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 0, 7057 .access = PL1_RW, .accessfn = access_pauth, 7058 .fgt = FGT_APGAKEY, 7059 .fieldoffset = offsetof(CPUARMState, keys.apga.lo) }, 7060 { .name = "APGAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7061 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 3, .opc2 = 1, 7062 .access = PL1_RW, .accessfn = access_pauth, 7063 .fgt = FGT_APGAKEY, 7064 .fieldoffset = offsetof(CPUARMState, keys.apga.hi) }, 7065 { .name = "APIAKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7066 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 0, 7067 .access = PL1_RW, .accessfn = access_pauth, 7068 .fgt = FGT_APIAKEY, 7069 .fieldoffset = offsetof(CPUARMState, keys.apia.lo) }, 7070 { .name = "APIAKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7071 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 1, 7072 .access = PL1_RW, .accessfn = access_pauth, 7073 .fgt = FGT_APIAKEY, 7074 .fieldoffset = offsetof(CPUARMState, keys.apia.hi) }, 7075 { .name = "APIBKEYLO_EL1", .state = ARM_CP_STATE_AA64, 7076 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 2, 7077 .access = PL1_RW, .accessfn = access_pauth, 7078 .fgt = FGT_APIBKEY, 7079 .fieldoffset = offsetof(CPUARMState, keys.apib.lo) }, 7080 { .name = "APIBKEYHI_EL1", .state = ARM_CP_STATE_AA64, 7081 .opc0 = 3, .opc1 = 0, .crn = 2, .crm = 1, .opc2 = 3, 7082 .access = PL1_RW, .accessfn = access_pauth, 7083 .fgt = FGT_APIBKEY, 7084 .fieldoffset = offsetof(CPUARMState, keys.apib.hi) }, 7085 }; 7086 7087 static uint64_t rndr_readfn(CPUARMState *env, const ARMCPRegInfo *ri) 7088 { 7089 Error *err = NULL; 7090 uint64_t ret; 7091 7092 /* Success sets NZCV = 0000. */ 7093 env->NF = env->CF = env->VF = 0, env->ZF = 1; 7094 7095 if (qemu_guest_getrandom(&ret, sizeof(ret), &err) < 0) { 7096 /* 7097 * ??? Failed, for unknown reasons in the crypto subsystem. 7098 * The best we can do is log the reason and return the 7099 * timed-out indication to the guest. There is no reason 7100 * we know to expect this failure to be transitory, so the 7101 * guest may well hang retrying the operation. 7102 */ 7103 qemu_log_mask(LOG_UNIMP, "%s: Crypto failure: %s", 7104 ri->name, error_get_pretty(err)); 7105 error_free(err); 7106 7107 env->ZF = 0; /* NZCF = 0100 */ 7108 return 0; 7109 } 7110 return ret; 7111 } 7112 7113 /* We do not support re-seeding, so the two registers operate the same. */ 7114 static const ARMCPRegInfo rndr_reginfo[] = { 7115 { .name = "RNDR", .state = ARM_CP_STATE_AA64, 7116 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO, 7117 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 0, 7118 .access = PL0_R, .readfn = rndr_readfn }, 7119 { .name = "RNDRRS", .state = ARM_CP_STATE_AA64, 7120 .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END | ARM_CP_IO, 7121 .opc0 = 3, .opc1 = 3, .crn = 2, .crm = 4, .opc2 = 1, 7122 .access = PL0_R, .readfn = rndr_readfn }, 7123 }; 7124 7125 static void dccvap_writefn(CPUARMState *env, const ARMCPRegInfo *opaque, 7126 uint64_t value) 7127 { 7128 #ifdef CONFIG_TCG 7129 ARMCPU *cpu = env_archcpu(env); 7130 /* CTR_EL0 System register -> DminLine, bits [19:16] */ 7131 uint64_t dline_size = 4 << ((cpu->ctr >> 16) & 0xF); 7132 uint64_t vaddr_in = (uint64_t) value; 7133 uint64_t vaddr = vaddr_in & ~(dline_size - 1); 7134 void *haddr; 7135 int mem_idx = arm_env_mmu_index(env); 7136 7137 /* This won't be crossing page boundaries */ 7138 haddr = probe_read(env, vaddr, dline_size, mem_idx, GETPC()); 7139 if (haddr) { 7140 #ifndef CONFIG_USER_ONLY 7141 7142 ram_addr_t offset; 7143 MemoryRegion *mr; 7144 7145 /* RCU lock is already being held */ 7146 mr = memory_region_from_host(haddr, &offset); 7147 7148 if (mr) { 7149 memory_region_writeback(mr, offset, dline_size); 7150 } 7151 #endif /*CONFIG_USER_ONLY*/ 7152 } 7153 #else 7154 /* Handled by hardware accelerator. */ 7155 g_assert_not_reached(); 7156 #endif /* CONFIG_TCG */ 7157 } 7158 7159 static const ARMCPRegInfo dcpop_reg[] = { 7160 { .name = "DC_CVAP", .state = ARM_CP_STATE_AA64, 7161 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 1, 7162 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END, 7163 .fgt = FGT_DCCVAP, 7164 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn }, 7165 }; 7166 7167 static const ARMCPRegInfo dcpodp_reg[] = { 7168 { .name = "DC_CVADP", .state = ARM_CP_STATE_AA64, 7169 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 1, 7170 .access = PL0_W, .type = ARM_CP_NO_RAW | ARM_CP_SUPPRESS_TB_END, 7171 .fgt = FGT_DCCVADP, 7172 .accessfn = aa64_cacheop_poc_access, .writefn = dccvap_writefn }, 7173 }; 7174 7175 static CPAccessResult access_aa64_tid5(CPUARMState *env, const ARMCPRegInfo *ri, 7176 bool isread) 7177 { 7178 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID5)) { 7179 return CP_ACCESS_TRAP_EL2; 7180 } 7181 7182 return CP_ACCESS_OK; 7183 } 7184 7185 static CPAccessResult access_mte(CPUARMState *env, const ARMCPRegInfo *ri, 7186 bool isread) 7187 { 7188 int el = arm_current_el(env); 7189 if (el < 2 && arm_is_el2_enabled(env)) { 7190 uint64_t hcr = arm_hcr_el2_eff(env); 7191 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) { 7192 return CP_ACCESS_TRAP_EL2; 7193 } 7194 } 7195 if (el < 3 && 7196 arm_feature(env, ARM_FEATURE_EL3) && 7197 !(env->cp15.scr_el3 & SCR_ATA)) { 7198 return CP_ACCESS_TRAP_EL3; 7199 } 7200 return CP_ACCESS_OK; 7201 } 7202 7203 static CPAccessResult access_tfsr_el1(CPUARMState *env, const ARMCPRegInfo *ri, 7204 bool isread) 7205 { 7206 CPAccessResult nv1 = access_nv1(env, ri, isread); 7207 7208 if (nv1 != CP_ACCESS_OK) { 7209 return nv1; 7210 } 7211 return access_mte(env, ri, isread); 7212 } 7213 7214 static CPAccessResult access_tfsr_el2(CPUARMState *env, const ARMCPRegInfo *ri, 7215 bool isread) 7216 { 7217 /* 7218 * TFSR_EL2: similar to generic access_mte(), but we need to 7219 * account for FEAT_NV. At EL1 this must be a FEAT_NV access; 7220 * if NV2 is enabled then we will redirect this to TFSR_EL1 7221 * after doing the HCR and SCR ATA traps; otherwise this will 7222 * be a trap to EL2 and the HCR/SCR traps do not apply. 7223 */ 7224 int el = arm_current_el(env); 7225 7226 if (el == 1 && (arm_hcr_el2_eff(env) & HCR_NV2)) { 7227 return CP_ACCESS_OK; 7228 } 7229 if (el < 2 && arm_is_el2_enabled(env)) { 7230 uint64_t hcr = arm_hcr_el2_eff(env); 7231 if (!(hcr & HCR_ATA) && (!(hcr & HCR_E2H) || !(hcr & HCR_TGE))) { 7232 return CP_ACCESS_TRAP_EL2; 7233 } 7234 } 7235 if (el < 3 && 7236 arm_feature(env, ARM_FEATURE_EL3) && 7237 !(env->cp15.scr_el3 & SCR_ATA)) { 7238 return CP_ACCESS_TRAP_EL3; 7239 } 7240 return CP_ACCESS_OK; 7241 } 7242 7243 static uint64_t tco_read(CPUARMState *env, const ARMCPRegInfo *ri) 7244 { 7245 return env->pstate & PSTATE_TCO; 7246 } 7247 7248 static void tco_write(CPUARMState *env, const ARMCPRegInfo *ri, uint64_t val) 7249 { 7250 env->pstate = (env->pstate & ~PSTATE_TCO) | (val & PSTATE_TCO); 7251 } 7252 7253 static const ARMCPRegInfo mte_reginfo[] = { 7254 { .name = "TFSRE0_EL1", .state = ARM_CP_STATE_AA64, 7255 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 1, 7256 .access = PL1_RW, .accessfn = access_mte, 7257 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[0]) }, 7258 { .name = "TFSR_EL1", .state = ARM_CP_STATE_AA64, 7259 .opc0 = 3, .opc1 = 0, .crn = 5, .crm = 6, .opc2 = 0, 7260 .access = PL1_RW, .accessfn = access_tfsr_el1, 7261 .nv2_redirect_offset = 0x190 | NV2_REDIR_NV1, 7262 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[1]) }, 7263 { .name = "TFSR_EL2", .state = ARM_CP_STATE_AA64, 7264 .type = ARM_CP_NV2_REDIRECT, 7265 .opc0 = 3, .opc1 = 4, .crn = 5, .crm = 6, .opc2 = 0, 7266 .access = PL2_RW, .accessfn = access_tfsr_el2, 7267 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[2]) }, 7268 { .name = "TFSR_EL3", .state = ARM_CP_STATE_AA64, 7269 .opc0 = 3, .opc1 = 6, .crn = 5, .crm = 6, .opc2 = 0, 7270 .access = PL3_RW, 7271 .fieldoffset = offsetof(CPUARMState, cp15.tfsr_el[3]) }, 7272 { .name = "RGSR_EL1", .state = ARM_CP_STATE_AA64, 7273 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 5, 7274 .access = PL1_RW, .accessfn = access_mte, 7275 .fieldoffset = offsetof(CPUARMState, cp15.rgsr_el1) }, 7276 { .name = "GCR_EL1", .state = ARM_CP_STATE_AA64, 7277 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 6, 7278 .access = PL1_RW, .accessfn = access_mte, 7279 .fieldoffset = offsetof(CPUARMState, cp15.gcr_el1) }, 7280 { .name = "TCO", .state = ARM_CP_STATE_AA64, 7281 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7, 7282 .type = ARM_CP_NO_RAW, 7283 .access = PL0_RW, .readfn = tco_read, .writefn = tco_write }, 7284 { .name = "DC_IGVAC", .state = ARM_CP_STATE_AA64, 7285 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 3, 7286 .type = ARM_CP_NOP, .access = PL1_W, 7287 .fgt = FGT_DCIVAC, 7288 .accessfn = aa64_cacheop_poc_access }, 7289 { .name = "DC_IGSW", .state = ARM_CP_STATE_AA64, 7290 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 4, 7291 .fgt = FGT_DCISW, 7292 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7293 { .name = "DC_IGDVAC", .state = ARM_CP_STATE_AA64, 7294 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 5, 7295 .type = ARM_CP_NOP, .access = PL1_W, 7296 .fgt = FGT_DCIVAC, 7297 .accessfn = aa64_cacheop_poc_access }, 7298 { .name = "DC_IGDSW", .state = ARM_CP_STATE_AA64, 7299 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 6, .opc2 = 6, 7300 .fgt = FGT_DCISW, 7301 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7302 { .name = "DC_CGSW", .state = ARM_CP_STATE_AA64, 7303 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 4, 7304 .fgt = FGT_DCCSW, 7305 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7306 { .name = "DC_CGDSW", .state = ARM_CP_STATE_AA64, 7307 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 10, .opc2 = 6, 7308 .fgt = FGT_DCCSW, 7309 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7310 { .name = "DC_CIGSW", .state = ARM_CP_STATE_AA64, 7311 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 4, 7312 .fgt = FGT_DCCISW, 7313 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7314 { .name = "DC_CIGDSW", .state = ARM_CP_STATE_AA64, 7315 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 14, .opc2 = 6, 7316 .fgt = FGT_DCCISW, 7317 .type = ARM_CP_NOP, .access = PL1_W, .accessfn = access_tsw }, 7318 }; 7319 7320 static const ARMCPRegInfo mte_tco_ro_reginfo[] = { 7321 { .name = "TCO", .state = ARM_CP_STATE_AA64, 7322 .opc0 = 3, .opc1 = 3, .crn = 4, .crm = 2, .opc2 = 7, 7323 .type = ARM_CP_CONST, .access = PL0_RW, }, 7324 }; 7325 7326 static const ARMCPRegInfo mte_el0_cacheop_reginfo[] = { 7327 { .name = "DC_CGVAC", .state = ARM_CP_STATE_AA64, 7328 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 3, 7329 .type = ARM_CP_NOP, .access = PL0_W, 7330 .fgt = FGT_DCCVAC, 7331 .accessfn = aa64_cacheop_poc_access }, 7332 { .name = "DC_CGDVAC", .state = ARM_CP_STATE_AA64, 7333 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 10, .opc2 = 5, 7334 .type = ARM_CP_NOP, .access = PL0_W, 7335 .fgt = FGT_DCCVAC, 7336 .accessfn = aa64_cacheop_poc_access }, 7337 { .name = "DC_CGVAP", .state = ARM_CP_STATE_AA64, 7338 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 3, 7339 .type = ARM_CP_NOP, .access = PL0_W, 7340 .fgt = FGT_DCCVAP, 7341 .accessfn = aa64_cacheop_poc_access }, 7342 { .name = "DC_CGDVAP", .state = ARM_CP_STATE_AA64, 7343 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 12, .opc2 = 5, 7344 .type = ARM_CP_NOP, .access = PL0_W, 7345 .fgt = FGT_DCCVAP, 7346 .accessfn = aa64_cacheop_poc_access }, 7347 { .name = "DC_CGVADP", .state = ARM_CP_STATE_AA64, 7348 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 3, 7349 .type = ARM_CP_NOP, .access = PL0_W, 7350 .fgt = FGT_DCCVADP, 7351 .accessfn = aa64_cacheop_poc_access }, 7352 { .name = "DC_CGDVADP", .state = ARM_CP_STATE_AA64, 7353 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 13, .opc2 = 5, 7354 .type = ARM_CP_NOP, .access = PL0_W, 7355 .fgt = FGT_DCCVADP, 7356 .accessfn = aa64_cacheop_poc_access }, 7357 { .name = "DC_CIGVAC", .state = ARM_CP_STATE_AA64, 7358 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 3, 7359 .type = ARM_CP_NOP, .access = PL0_W, 7360 .fgt = FGT_DCCIVAC, 7361 .accessfn = aa64_cacheop_poc_access }, 7362 { .name = "DC_CIGDVAC", .state = ARM_CP_STATE_AA64, 7363 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 14, .opc2 = 5, 7364 .type = ARM_CP_NOP, .access = PL0_W, 7365 .fgt = FGT_DCCIVAC, 7366 .accessfn = aa64_cacheop_poc_access }, 7367 { .name = "DC_GVA", .state = ARM_CP_STATE_AA64, 7368 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 3, 7369 .access = PL0_W, .type = ARM_CP_DC_GVA, 7370 #ifndef CONFIG_USER_ONLY 7371 /* Avoid overhead of an access check that always passes in user-mode */ 7372 .accessfn = aa64_zva_access, 7373 .fgt = FGT_DCZVA, 7374 #endif 7375 }, 7376 { .name = "DC_GZVA", .state = ARM_CP_STATE_AA64, 7377 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 4, .opc2 = 4, 7378 .access = PL0_W, .type = ARM_CP_DC_GZVA, 7379 #ifndef CONFIG_USER_ONLY 7380 /* Avoid overhead of an access check that always passes in user-mode */ 7381 .accessfn = aa64_zva_access, 7382 .fgt = FGT_DCZVA, 7383 #endif 7384 }, 7385 }; 7386 7387 static CPAccessResult access_scxtnum(CPUARMState *env, const ARMCPRegInfo *ri, 7388 bool isread) 7389 { 7390 uint64_t hcr = arm_hcr_el2_eff(env); 7391 int el = arm_current_el(env); 7392 7393 if (el == 0 && !((hcr & HCR_E2H) && (hcr & HCR_TGE))) { 7394 if (env->cp15.sctlr_el[1] & SCTLR_TSCXT) { 7395 if (hcr & HCR_TGE) { 7396 return CP_ACCESS_TRAP_EL2; 7397 } 7398 return CP_ACCESS_TRAP_EL1; 7399 } 7400 } else if (el < 2 && (env->cp15.sctlr_el[2] & SCTLR_TSCXT)) { 7401 return CP_ACCESS_TRAP_EL2; 7402 } 7403 if (el < 2 && arm_is_el2_enabled(env) && !(hcr & HCR_ENSCXT)) { 7404 return CP_ACCESS_TRAP_EL2; 7405 } 7406 if (el < 3 7407 && arm_feature(env, ARM_FEATURE_EL3) 7408 && !(env->cp15.scr_el3 & SCR_ENSCXT)) { 7409 return CP_ACCESS_TRAP_EL3; 7410 } 7411 return CP_ACCESS_OK; 7412 } 7413 7414 static CPAccessResult access_scxtnum_el1(CPUARMState *env, 7415 const ARMCPRegInfo *ri, 7416 bool isread) 7417 { 7418 CPAccessResult nv1 = access_nv1(env, ri, isread); 7419 7420 if (nv1 != CP_ACCESS_OK) { 7421 return nv1; 7422 } 7423 return access_scxtnum(env, ri, isread); 7424 } 7425 7426 static const ARMCPRegInfo scxtnum_reginfo[] = { 7427 { .name = "SCXTNUM_EL0", .state = ARM_CP_STATE_AA64, 7428 .opc0 = 3, .opc1 = 3, .crn = 13, .crm = 0, .opc2 = 7, 7429 .access = PL0_RW, .accessfn = access_scxtnum, 7430 .fgt = FGT_SCXTNUM_EL0, 7431 .fieldoffset = offsetof(CPUARMState, scxtnum_el[0]) }, 7432 { .name = "SCXTNUM_EL1", .state = ARM_CP_STATE_AA64, 7433 .opc0 = 3, .opc1 = 0, .crn = 13, .crm = 0, .opc2 = 7, 7434 .access = PL1_RW, .accessfn = access_scxtnum_el1, 7435 .fgt = FGT_SCXTNUM_EL1, 7436 .nv2_redirect_offset = 0x188 | NV2_REDIR_NV1, 7437 .fieldoffset = offsetof(CPUARMState, scxtnum_el[1]) }, 7438 { .name = "SCXTNUM_EL2", .state = ARM_CP_STATE_AA64, 7439 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 7, 7440 .access = PL2_RW, .accessfn = access_scxtnum, 7441 .fieldoffset = offsetof(CPUARMState, scxtnum_el[2]) }, 7442 { .name = "SCXTNUM_EL3", .state = ARM_CP_STATE_AA64, 7443 .opc0 = 3, .opc1 = 6, .crn = 13, .crm = 0, .opc2 = 7, 7444 .access = PL3_RW, 7445 .fieldoffset = offsetof(CPUARMState, scxtnum_el[3]) }, 7446 }; 7447 7448 static CPAccessResult access_fgt(CPUARMState *env, const ARMCPRegInfo *ri, 7449 bool isread) 7450 { 7451 if (arm_current_el(env) == 2 && 7452 arm_feature(env, ARM_FEATURE_EL3) && !(env->cp15.scr_el3 & SCR_FGTEN)) { 7453 return CP_ACCESS_TRAP_EL3; 7454 } 7455 return CP_ACCESS_OK; 7456 } 7457 7458 static const ARMCPRegInfo fgt_reginfo[] = { 7459 { .name = "HFGRTR_EL2", .state = ARM_CP_STATE_AA64, 7460 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 4, 7461 .nv2_redirect_offset = 0x1b8, 7462 .access = PL2_RW, .accessfn = access_fgt, 7463 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HFGRTR]) }, 7464 { .name = "HFGWTR_EL2", .state = ARM_CP_STATE_AA64, 7465 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 5, 7466 .nv2_redirect_offset = 0x1c0, 7467 .access = PL2_RW, .accessfn = access_fgt, 7468 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HFGWTR]) }, 7469 { .name = "HDFGRTR_EL2", .state = ARM_CP_STATE_AA64, 7470 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 4, 7471 .nv2_redirect_offset = 0x1d0, 7472 .access = PL2_RW, .accessfn = access_fgt, 7473 .fieldoffset = offsetof(CPUARMState, cp15.fgt_read[FGTREG_HDFGRTR]) }, 7474 { .name = "HDFGWTR_EL2", .state = ARM_CP_STATE_AA64, 7475 .opc0 = 3, .opc1 = 4, .crn = 3, .crm = 1, .opc2 = 5, 7476 .nv2_redirect_offset = 0x1d8, 7477 .access = PL2_RW, .accessfn = access_fgt, 7478 .fieldoffset = offsetof(CPUARMState, cp15.fgt_write[FGTREG_HDFGWTR]) }, 7479 { .name = "HFGITR_EL2", .state = ARM_CP_STATE_AA64, 7480 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 6, 7481 .nv2_redirect_offset = 0x1c8, 7482 .access = PL2_RW, .accessfn = access_fgt, 7483 .fieldoffset = offsetof(CPUARMState, cp15.fgt_exec[FGTREG_HFGITR]) }, 7484 }; 7485 7486 static void vncr_write(CPUARMState *env, const ARMCPRegInfo *ri, 7487 uint64_t value) 7488 { 7489 /* 7490 * Clear the RES0 bottom 12 bits; this means at runtime we can guarantee 7491 * that VNCR_EL2 + offset is 64-bit aligned. We don't need to do anything 7492 * about the RESS bits at the top -- we choose the "generate an EL2 7493 * translation abort on use" CONSTRAINED UNPREDICTABLE option (i.e. let 7494 * the ptw.c code detect the resulting invalid address). 7495 */ 7496 env->cp15.vncr_el2 = value & ~0xfffULL; 7497 } 7498 7499 static const ARMCPRegInfo nv2_reginfo[] = { 7500 { .name = "VNCR_EL2", .state = ARM_CP_STATE_AA64, 7501 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 2, .opc2 = 0, 7502 .access = PL2_RW, 7503 .writefn = vncr_write, 7504 .nv2_redirect_offset = 0xb0, 7505 .fieldoffset = offsetof(CPUARMState, cp15.vncr_el2) }, 7506 }; 7507 7508 #endif /* TARGET_AARCH64 */ 7509 7510 static CPAccessResult access_predinv(CPUARMState *env, const ARMCPRegInfo *ri, 7511 bool isread) 7512 { 7513 int el = arm_current_el(env); 7514 7515 if (el == 0) { 7516 uint64_t sctlr = arm_sctlr(env, el); 7517 if (!(sctlr & SCTLR_EnRCTX)) { 7518 return CP_ACCESS_TRAP_EL1; 7519 } 7520 } else if (el == 1) { 7521 uint64_t hcr = arm_hcr_el2_eff(env); 7522 if (hcr & HCR_NV) { 7523 return CP_ACCESS_TRAP_EL2; 7524 } 7525 } 7526 return CP_ACCESS_OK; 7527 } 7528 7529 static const ARMCPRegInfo predinv_reginfo[] = { 7530 { .name = "CFP_RCTX", .state = ARM_CP_STATE_AA64, 7531 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 4, 7532 .fgt = FGT_CFPRCTX, 7533 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7534 { .name = "DVP_RCTX", .state = ARM_CP_STATE_AA64, 7535 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 5, 7536 .fgt = FGT_DVPRCTX, 7537 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7538 { .name = "CPP_RCTX", .state = ARM_CP_STATE_AA64, 7539 .opc0 = 1, .opc1 = 3, .crn = 7, .crm = 3, .opc2 = 7, 7540 .fgt = FGT_CPPRCTX, 7541 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7542 /* 7543 * Note the AArch32 opcodes have a different OPC1. 7544 */ 7545 { .name = "CFPRCTX", .state = ARM_CP_STATE_AA32, 7546 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 4, 7547 .fgt = FGT_CFPRCTX, 7548 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7549 { .name = "DVPRCTX", .state = ARM_CP_STATE_AA32, 7550 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 5, 7551 .fgt = FGT_DVPRCTX, 7552 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7553 { .name = "CPPRCTX", .state = ARM_CP_STATE_AA32, 7554 .cp = 15, .opc1 = 0, .crn = 7, .crm = 3, .opc2 = 7, 7555 .fgt = FGT_CPPRCTX, 7556 .type = ARM_CP_NOP, .access = PL0_W, .accessfn = access_predinv }, 7557 }; 7558 7559 static uint64_t ccsidr2_read(CPUARMState *env, const ARMCPRegInfo *ri) 7560 { 7561 /* Read the high 32 bits of the current CCSIDR */ 7562 return extract64(ccsidr_read(env, ri), 32, 32); 7563 } 7564 7565 static const ARMCPRegInfo ccsidr2_reginfo[] = { 7566 { .name = "CCSIDR2", .state = ARM_CP_STATE_BOTH, 7567 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 2, 7568 .access = PL1_R, 7569 .accessfn = access_tid4, 7570 .readfn = ccsidr2_read, .type = ARM_CP_NO_RAW }, 7571 }; 7572 7573 static CPAccessResult access_aa64_tid3(CPUARMState *env, const ARMCPRegInfo *ri, 7574 bool isread) 7575 { 7576 if ((arm_current_el(env) < 2) && (arm_hcr_el2_eff(env) & HCR_TID3)) { 7577 return CP_ACCESS_TRAP_EL2; 7578 } 7579 7580 return CP_ACCESS_OK; 7581 } 7582 7583 static CPAccessResult access_aa32_tid3(CPUARMState *env, const ARMCPRegInfo *ri, 7584 bool isread) 7585 { 7586 if (arm_feature(env, ARM_FEATURE_V8)) { 7587 return access_aa64_tid3(env, ri, isread); 7588 } 7589 7590 return CP_ACCESS_OK; 7591 } 7592 7593 static CPAccessResult access_jazelle(CPUARMState *env, const ARMCPRegInfo *ri, 7594 bool isread) 7595 { 7596 if (arm_current_el(env) == 1 && (arm_hcr_el2_eff(env) & HCR_TID0)) { 7597 return CP_ACCESS_TRAP_EL2; 7598 } 7599 7600 return CP_ACCESS_OK; 7601 } 7602 7603 static CPAccessResult access_joscr_jmcr(CPUARMState *env, 7604 const ARMCPRegInfo *ri, bool isread) 7605 { 7606 /* 7607 * HSTR.TJDBX traps JOSCR and JMCR accesses, but it exists only 7608 * in v7A, not in v8A. 7609 */ 7610 if (!arm_feature(env, ARM_FEATURE_V8) && 7611 arm_current_el(env) < 2 && !arm_is_secure_below_el3(env) && 7612 (env->cp15.hstr_el2 & HSTR_TJDBX)) { 7613 return CP_ACCESS_TRAP_EL2; 7614 } 7615 return CP_ACCESS_OK; 7616 } 7617 7618 static const ARMCPRegInfo jazelle_regs[] = { 7619 { .name = "JIDR", 7620 .cp = 14, .crn = 0, .crm = 0, .opc1 = 7, .opc2 = 0, 7621 .access = PL1_R, .accessfn = access_jazelle, 7622 .type = ARM_CP_CONST, .resetvalue = 0 }, 7623 { .name = "JOSCR", 7624 .cp = 14, .crn = 1, .crm = 0, .opc1 = 7, .opc2 = 0, 7625 .accessfn = access_joscr_jmcr, 7626 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 7627 { .name = "JMCR", 7628 .cp = 14, .crn = 2, .crm = 0, .opc1 = 7, .opc2 = 0, 7629 .accessfn = access_joscr_jmcr, 7630 .access = PL1_RW, .type = ARM_CP_CONST, .resetvalue = 0 }, 7631 }; 7632 7633 static const ARMCPRegInfo contextidr_el2 = { 7634 .name = "CONTEXTIDR_EL2", .state = ARM_CP_STATE_AA64, 7635 .opc0 = 3, .opc1 = 4, .crn = 13, .crm = 0, .opc2 = 1, 7636 .access = PL2_RW, 7637 .fieldoffset = offsetof(CPUARMState, cp15.contextidr_el[2]) 7638 }; 7639 7640 static const ARMCPRegInfo vhe_reginfo[] = { 7641 { .name = "TTBR1_EL2", .state = ARM_CP_STATE_AA64, 7642 .opc0 = 3, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 1, 7643 .access = PL2_RW, .writefn = vmsa_tcr_ttbr_el2_write, 7644 .raw_writefn = raw_write, 7645 .fieldoffset = offsetof(CPUARMState, cp15.ttbr1_el[2]) }, 7646 #ifndef CONFIG_USER_ONLY 7647 { .name = "CNTHV_CVAL_EL2", .state = ARM_CP_STATE_AA64, 7648 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 2, 7649 .fieldoffset = 7650 offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].cval), 7651 .type = ARM_CP_IO, .access = PL2_RW, 7652 .writefn = gt_hv_cval_write, .raw_writefn = raw_write }, 7653 { .name = "CNTHV_TVAL_EL2", .state = ARM_CP_STATE_BOTH, 7654 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 0, 7655 .type = ARM_CP_NO_RAW | ARM_CP_IO, .access = PL2_RW, 7656 .resetfn = gt_hv_timer_reset, 7657 .readfn = gt_hv_tval_read, .writefn = gt_hv_tval_write }, 7658 { .name = "CNTHV_CTL_EL2", .state = ARM_CP_STATE_BOTH, 7659 .type = ARM_CP_IO, 7660 .opc0 = 3, .opc1 = 4, .crn = 14, .crm = 3, .opc2 = 1, 7661 .access = PL2_RW, 7662 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_HYPVIRT].ctl), 7663 .writefn = gt_hv_ctl_write, .raw_writefn = raw_write }, 7664 { .name = "CNTP_CTL_EL02", .state = ARM_CP_STATE_AA64, 7665 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 1, 7666 .type = ARM_CP_IO | ARM_CP_ALIAS, 7667 .access = PL2_RW, .accessfn = access_el1nvpct, 7668 .nv2_redirect_offset = 0x180 | NV2_REDIR_NO_NV1, 7669 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].ctl), 7670 .writefn = gt_phys_ctl_write, .raw_writefn = raw_write }, 7671 { .name = "CNTV_CTL_EL02", .state = ARM_CP_STATE_AA64, 7672 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 1, 7673 .type = ARM_CP_IO | ARM_CP_ALIAS, 7674 .access = PL2_RW, .accessfn = access_el1nvvct, 7675 .nv2_redirect_offset = 0x170 | NV2_REDIR_NO_NV1, 7676 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].ctl), 7677 .writefn = gt_virt_ctl_write, .raw_writefn = raw_write }, 7678 { .name = "CNTP_TVAL_EL02", .state = ARM_CP_STATE_AA64, 7679 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 0, 7680 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS, 7681 .access = PL2_RW, .accessfn = e2h_access, 7682 .readfn = gt_phys_tval_read, .writefn = gt_phys_tval_write }, 7683 { .name = "CNTV_TVAL_EL02", .state = ARM_CP_STATE_AA64, 7684 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 0, 7685 .type = ARM_CP_NO_RAW | ARM_CP_IO | ARM_CP_ALIAS, 7686 .access = PL2_RW, .accessfn = e2h_access, 7687 .readfn = gt_virt_tval_read, .writefn = gt_virt_tval_write }, 7688 { .name = "CNTP_CVAL_EL02", .state = ARM_CP_STATE_AA64, 7689 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 2, .opc2 = 2, 7690 .type = ARM_CP_IO | ARM_CP_ALIAS, 7691 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_PHYS].cval), 7692 .nv2_redirect_offset = 0x178 | NV2_REDIR_NO_NV1, 7693 .access = PL2_RW, .accessfn = access_el1nvpct, 7694 .writefn = gt_phys_cval_write, .raw_writefn = raw_write }, 7695 { .name = "CNTV_CVAL_EL02", .state = ARM_CP_STATE_AA64, 7696 .opc0 = 3, .opc1 = 5, .crn = 14, .crm = 3, .opc2 = 2, 7697 .type = ARM_CP_IO | ARM_CP_ALIAS, 7698 .nv2_redirect_offset = 0x168 | NV2_REDIR_NO_NV1, 7699 .fieldoffset = offsetof(CPUARMState, cp15.c14_timer[GTIMER_VIRT].cval), 7700 .access = PL2_RW, .accessfn = access_el1nvvct, 7701 .writefn = gt_virt_cval_write, .raw_writefn = raw_write }, 7702 #endif 7703 }; 7704 7705 #ifndef CONFIG_USER_ONLY 7706 static const ARMCPRegInfo ats1e1_reginfo[] = { 7707 { .name = "AT_S1E1RP", .state = ARM_CP_STATE_AA64, 7708 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0, 7709 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 7710 .fgt = FGT_ATS1E1RP, 7711 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 7712 { .name = "AT_S1E1WP", .state = ARM_CP_STATE_AA64, 7713 .opc0 = 1, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1, 7714 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 7715 .fgt = FGT_ATS1E1WP, 7716 .accessfn = at_s1e01_access, .writefn = ats_write64 }, 7717 }; 7718 7719 static const ARMCPRegInfo ats1cp_reginfo[] = { 7720 { .name = "ATS1CPRP", 7721 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 0, 7722 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 7723 .writefn = ats_write }, 7724 { .name = "ATS1CPWP", 7725 .cp = 15, .opc1 = 0, .crn = 7, .crm = 9, .opc2 = 1, 7726 .access = PL1_W, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC, 7727 .writefn = ats_write }, 7728 }; 7729 #endif 7730 7731 /* 7732 * ACTLR2 and HACTLR2 map to ACTLR_EL1[63:32] and 7733 * ACTLR_EL2[63:32]. They exist only if the ID_MMFR4.AC2 field 7734 * is non-zero, which is never for ARMv7, optionally in ARMv8 7735 * and mandatorily for ARMv8.2 and up. 7736 * ACTLR2 is banked for S and NS if EL3 is AArch32. Since QEMU's 7737 * implementation is RAZ/WI we can ignore this detail, as we 7738 * do for ACTLR. 7739 */ 7740 static const ARMCPRegInfo actlr2_hactlr2_reginfo[] = { 7741 { .name = "ACTLR2", .state = ARM_CP_STATE_AA32, 7742 .cp = 15, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 3, 7743 .access = PL1_RW, .accessfn = access_tacr, 7744 .type = ARM_CP_CONST, .resetvalue = 0 }, 7745 { .name = "HACTLR2", .state = ARM_CP_STATE_AA32, 7746 .cp = 15, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 3, 7747 .access = PL2_RW, .type = ARM_CP_CONST, 7748 .resetvalue = 0 }, 7749 }; 7750 7751 void register_cp_regs_for_features(ARMCPU *cpu) 7752 { 7753 /* Register all the coprocessor registers based on feature bits */ 7754 CPUARMState *env = &cpu->env; 7755 if (arm_feature(env, ARM_FEATURE_M)) { 7756 /* M profile has no coprocessor registers */ 7757 return; 7758 } 7759 7760 define_arm_cp_regs(cpu, cp_reginfo); 7761 if (!arm_feature(env, ARM_FEATURE_V8)) { 7762 /* 7763 * Must go early as it is full of wildcards that may be 7764 * overridden by later definitions. 7765 */ 7766 define_arm_cp_regs(cpu, not_v8_cp_reginfo); 7767 } 7768 7769 define_tlb_insn_regs(cpu); 7770 7771 if (arm_feature(env, ARM_FEATURE_V6)) { 7772 /* The ID registers all have impdef reset values */ 7773 ARMCPRegInfo v6_idregs[] = { 7774 { .name = "ID_PFR0", .state = ARM_CP_STATE_BOTH, 7775 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 0, 7776 .access = PL1_R, .type = ARM_CP_CONST, 7777 .accessfn = access_aa32_tid3, 7778 .resetvalue = cpu->isar.id_pfr0 }, 7779 /* 7780 * ID_PFR1 is not a plain ARM_CP_CONST because we don't know 7781 * the value of the GIC field until after we define these regs. 7782 */ 7783 { .name = "ID_PFR1", .state = ARM_CP_STATE_BOTH, 7784 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 1, 7785 .access = PL1_R, .type = ARM_CP_NO_RAW, 7786 .accessfn = access_aa32_tid3, 7787 #ifdef CONFIG_USER_ONLY 7788 .type = ARM_CP_CONST, 7789 .resetvalue = cpu->isar.id_pfr1, 7790 #else 7791 .type = ARM_CP_NO_RAW, 7792 .accessfn = access_aa32_tid3, 7793 .readfn = id_pfr1_read, 7794 .writefn = arm_cp_write_ignore 7795 #endif 7796 }, 7797 { .name = "ID_DFR0", .state = ARM_CP_STATE_BOTH, 7798 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 2, 7799 .access = PL1_R, .type = ARM_CP_CONST, 7800 .accessfn = access_aa32_tid3, 7801 .resetvalue = cpu->isar.id_dfr0 }, 7802 { .name = "ID_AFR0", .state = ARM_CP_STATE_BOTH, 7803 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 3, 7804 .access = PL1_R, .type = ARM_CP_CONST, 7805 .accessfn = access_aa32_tid3, 7806 .resetvalue = cpu->id_afr0 }, 7807 { .name = "ID_MMFR0", .state = ARM_CP_STATE_BOTH, 7808 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 4, 7809 .access = PL1_R, .type = ARM_CP_CONST, 7810 .accessfn = access_aa32_tid3, 7811 .resetvalue = cpu->isar.id_mmfr0 }, 7812 { .name = "ID_MMFR1", .state = ARM_CP_STATE_BOTH, 7813 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 5, 7814 .access = PL1_R, .type = ARM_CP_CONST, 7815 .accessfn = access_aa32_tid3, 7816 .resetvalue = cpu->isar.id_mmfr1 }, 7817 { .name = "ID_MMFR2", .state = ARM_CP_STATE_BOTH, 7818 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 6, 7819 .access = PL1_R, .type = ARM_CP_CONST, 7820 .accessfn = access_aa32_tid3, 7821 .resetvalue = cpu->isar.id_mmfr2 }, 7822 { .name = "ID_MMFR3", .state = ARM_CP_STATE_BOTH, 7823 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 1, .opc2 = 7, 7824 .access = PL1_R, .type = ARM_CP_CONST, 7825 .accessfn = access_aa32_tid3, 7826 .resetvalue = cpu->isar.id_mmfr3 }, 7827 { .name = "ID_ISAR0", .state = ARM_CP_STATE_BOTH, 7828 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 0, 7829 .access = PL1_R, .type = ARM_CP_CONST, 7830 .accessfn = access_aa32_tid3, 7831 .resetvalue = cpu->isar.id_isar0 }, 7832 { .name = "ID_ISAR1", .state = ARM_CP_STATE_BOTH, 7833 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 1, 7834 .access = PL1_R, .type = ARM_CP_CONST, 7835 .accessfn = access_aa32_tid3, 7836 .resetvalue = cpu->isar.id_isar1 }, 7837 { .name = "ID_ISAR2", .state = ARM_CP_STATE_BOTH, 7838 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 2, 7839 .access = PL1_R, .type = ARM_CP_CONST, 7840 .accessfn = access_aa32_tid3, 7841 .resetvalue = cpu->isar.id_isar2 }, 7842 { .name = "ID_ISAR3", .state = ARM_CP_STATE_BOTH, 7843 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 3, 7844 .access = PL1_R, .type = ARM_CP_CONST, 7845 .accessfn = access_aa32_tid3, 7846 .resetvalue = cpu->isar.id_isar3 }, 7847 { .name = "ID_ISAR4", .state = ARM_CP_STATE_BOTH, 7848 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 4, 7849 .access = PL1_R, .type = ARM_CP_CONST, 7850 .accessfn = access_aa32_tid3, 7851 .resetvalue = cpu->isar.id_isar4 }, 7852 { .name = "ID_ISAR5", .state = ARM_CP_STATE_BOTH, 7853 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 5, 7854 .access = PL1_R, .type = ARM_CP_CONST, 7855 .accessfn = access_aa32_tid3, 7856 .resetvalue = cpu->isar.id_isar5 }, 7857 { .name = "ID_MMFR4", .state = ARM_CP_STATE_BOTH, 7858 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 6, 7859 .access = PL1_R, .type = ARM_CP_CONST, 7860 .accessfn = access_aa32_tid3, 7861 .resetvalue = cpu->isar.id_mmfr4 }, 7862 { .name = "ID_ISAR6", .state = ARM_CP_STATE_BOTH, 7863 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 2, .opc2 = 7, 7864 .access = PL1_R, .type = ARM_CP_CONST, 7865 .accessfn = access_aa32_tid3, 7866 .resetvalue = cpu->isar.id_isar6 }, 7867 }; 7868 define_arm_cp_regs(cpu, v6_idregs); 7869 define_arm_cp_regs(cpu, v6_cp_reginfo); 7870 } else { 7871 define_arm_cp_regs(cpu, not_v6_cp_reginfo); 7872 } 7873 if (arm_feature(env, ARM_FEATURE_V6K)) { 7874 define_arm_cp_regs(cpu, v6k_cp_reginfo); 7875 } 7876 if (arm_feature(env, ARM_FEATURE_V7VE)) { 7877 define_arm_cp_regs(cpu, pmovsset_cp_reginfo); 7878 } 7879 if (arm_feature(env, ARM_FEATURE_V7)) { 7880 ARMCPRegInfo clidr = { 7881 .name = "CLIDR", .state = ARM_CP_STATE_BOTH, 7882 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 1, .opc2 = 1, 7883 .access = PL1_R, .type = ARM_CP_CONST, 7884 .accessfn = access_tid4, 7885 .fgt = FGT_CLIDR_EL1, 7886 .resetvalue = cpu->clidr 7887 }; 7888 define_one_arm_cp_reg(cpu, &clidr); 7889 define_arm_cp_regs(cpu, v7_cp_reginfo); 7890 define_debug_regs(cpu); 7891 define_pmu_regs(cpu); 7892 } else { 7893 define_arm_cp_regs(cpu, not_v7_cp_reginfo); 7894 } 7895 if (arm_feature(env, ARM_FEATURE_V8)) { 7896 /* 7897 * v8 ID registers, which all have impdef reset values. 7898 * Note that within the ID register ranges the unused slots 7899 * must all RAZ, not UNDEF; future architecture versions may 7900 * define new registers here. 7901 * ID registers which are AArch64 views of the AArch32 ID registers 7902 * which already existed in v6 and v7 are handled elsewhere, 7903 * in v6_idregs[]. 7904 */ 7905 int i; 7906 ARMCPRegInfo v8_idregs[] = { 7907 /* 7908 * ID_AA64PFR0_EL1 is not a plain ARM_CP_CONST in system 7909 * emulation because we don't know the right value for the 7910 * GIC field until after we define these regs. 7911 */ 7912 { .name = "ID_AA64PFR0_EL1", .state = ARM_CP_STATE_AA64, 7913 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 0, 7914 .access = PL1_R, 7915 #ifdef CONFIG_USER_ONLY 7916 .type = ARM_CP_CONST, 7917 .resetvalue = cpu->isar.id_aa64pfr0 7918 #else 7919 .type = ARM_CP_NO_RAW, 7920 .accessfn = access_aa64_tid3, 7921 .readfn = id_aa64pfr0_read, 7922 .writefn = arm_cp_write_ignore 7923 #endif 7924 }, 7925 { .name = "ID_AA64PFR1_EL1", .state = ARM_CP_STATE_AA64, 7926 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 1, 7927 .access = PL1_R, .type = ARM_CP_CONST, 7928 .accessfn = access_aa64_tid3, 7929 .resetvalue = cpu->isar.id_aa64pfr1}, 7930 { .name = "ID_AA64PFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7931 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 2, 7932 .access = PL1_R, .type = ARM_CP_CONST, 7933 .accessfn = access_aa64_tid3, 7934 .resetvalue = 0 }, 7935 { .name = "ID_AA64PFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7936 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 3, 7937 .access = PL1_R, .type = ARM_CP_CONST, 7938 .accessfn = access_aa64_tid3, 7939 .resetvalue = 0 }, 7940 { .name = "ID_AA64ZFR0_EL1", .state = ARM_CP_STATE_AA64, 7941 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 4, 7942 .access = PL1_R, .type = ARM_CP_CONST, 7943 .accessfn = access_aa64_tid3, 7944 .resetvalue = cpu->isar.id_aa64zfr0 }, 7945 { .name = "ID_AA64SMFR0_EL1", .state = ARM_CP_STATE_AA64, 7946 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 5, 7947 .access = PL1_R, .type = ARM_CP_CONST, 7948 .accessfn = access_aa64_tid3, 7949 .resetvalue = cpu->isar.id_aa64smfr0 }, 7950 { .name = "ID_AA64PFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7951 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 6, 7952 .access = PL1_R, .type = ARM_CP_CONST, 7953 .accessfn = access_aa64_tid3, 7954 .resetvalue = 0 }, 7955 { .name = "ID_AA64PFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7956 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 4, .opc2 = 7, 7957 .access = PL1_R, .type = ARM_CP_CONST, 7958 .accessfn = access_aa64_tid3, 7959 .resetvalue = 0 }, 7960 { .name = "ID_AA64DFR0_EL1", .state = ARM_CP_STATE_AA64, 7961 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 0, 7962 .access = PL1_R, .type = ARM_CP_CONST, 7963 .accessfn = access_aa64_tid3, 7964 .resetvalue = cpu->isar.id_aa64dfr0 }, 7965 { .name = "ID_AA64DFR1_EL1", .state = ARM_CP_STATE_AA64, 7966 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 1, 7967 .access = PL1_R, .type = ARM_CP_CONST, 7968 .accessfn = access_aa64_tid3, 7969 .resetvalue = cpu->isar.id_aa64dfr1 }, 7970 { .name = "ID_AA64DFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7971 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 2, 7972 .access = PL1_R, .type = ARM_CP_CONST, 7973 .accessfn = access_aa64_tid3, 7974 .resetvalue = 0 }, 7975 { .name = "ID_AA64DFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7976 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 3, 7977 .access = PL1_R, .type = ARM_CP_CONST, 7978 .accessfn = access_aa64_tid3, 7979 .resetvalue = 0 }, 7980 { .name = "ID_AA64AFR0_EL1", .state = ARM_CP_STATE_AA64, 7981 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 4, 7982 .access = PL1_R, .type = ARM_CP_CONST, 7983 .accessfn = access_aa64_tid3, 7984 .resetvalue = cpu->id_aa64afr0 }, 7985 { .name = "ID_AA64AFR1_EL1", .state = ARM_CP_STATE_AA64, 7986 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 5, 7987 .access = PL1_R, .type = ARM_CP_CONST, 7988 .accessfn = access_aa64_tid3, 7989 .resetvalue = cpu->id_aa64afr1 }, 7990 { .name = "ID_AA64AFR2_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7991 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 6, 7992 .access = PL1_R, .type = ARM_CP_CONST, 7993 .accessfn = access_aa64_tid3, 7994 .resetvalue = 0 }, 7995 { .name = "ID_AA64AFR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 7996 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 5, .opc2 = 7, 7997 .access = PL1_R, .type = ARM_CP_CONST, 7998 .accessfn = access_aa64_tid3, 7999 .resetvalue = 0 }, 8000 { .name = "ID_AA64ISAR0_EL1", .state = ARM_CP_STATE_AA64, 8001 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 0, 8002 .access = PL1_R, .type = ARM_CP_CONST, 8003 .accessfn = access_aa64_tid3, 8004 .resetvalue = cpu->isar.id_aa64isar0 }, 8005 { .name = "ID_AA64ISAR1_EL1", .state = ARM_CP_STATE_AA64, 8006 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 1, 8007 .access = PL1_R, .type = ARM_CP_CONST, 8008 .accessfn = access_aa64_tid3, 8009 .resetvalue = cpu->isar.id_aa64isar1 }, 8010 { .name = "ID_AA64ISAR2_EL1", .state = ARM_CP_STATE_AA64, 8011 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 2, 8012 .access = PL1_R, .type = ARM_CP_CONST, 8013 .accessfn = access_aa64_tid3, 8014 .resetvalue = cpu->isar.id_aa64isar2 }, 8015 { .name = "ID_AA64ISAR3_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8016 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 3, 8017 .access = PL1_R, .type = ARM_CP_CONST, 8018 .accessfn = access_aa64_tid3, 8019 .resetvalue = 0 }, 8020 { .name = "ID_AA64ISAR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8021 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 4, 8022 .access = PL1_R, .type = ARM_CP_CONST, 8023 .accessfn = access_aa64_tid3, 8024 .resetvalue = 0 }, 8025 { .name = "ID_AA64ISAR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8026 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 5, 8027 .access = PL1_R, .type = ARM_CP_CONST, 8028 .accessfn = access_aa64_tid3, 8029 .resetvalue = 0 }, 8030 { .name = "ID_AA64ISAR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8031 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 6, 8032 .access = PL1_R, .type = ARM_CP_CONST, 8033 .accessfn = access_aa64_tid3, 8034 .resetvalue = 0 }, 8035 { .name = "ID_AA64ISAR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8036 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 6, .opc2 = 7, 8037 .access = PL1_R, .type = ARM_CP_CONST, 8038 .accessfn = access_aa64_tid3, 8039 .resetvalue = 0 }, 8040 { .name = "ID_AA64MMFR0_EL1", .state = ARM_CP_STATE_AA64, 8041 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 0, 8042 .access = PL1_R, .type = ARM_CP_CONST, 8043 .accessfn = access_aa64_tid3, 8044 .resetvalue = cpu->isar.id_aa64mmfr0 }, 8045 { .name = "ID_AA64MMFR1_EL1", .state = ARM_CP_STATE_AA64, 8046 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 1, 8047 .access = PL1_R, .type = ARM_CP_CONST, 8048 .accessfn = access_aa64_tid3, 8049 .resetvalue = cpu->isar.id_aa64mmfr1 }, 8050 { .name = "ID_AA64MMFR2_EL1", .state = ARM_CP_STATE_AA64, 8051 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 2, 8052 .access = PL1_R, .type = ARM_CP_CONST, 8053 .accessfn = access_aa64_tid3, 8054 .resetvalue = cpu->isar.id_aa64mmfr2 }, 8055 { .name = "ID_AA64MMFR3_EL1", .state = ARM_CP_STATE_AA64, 8056 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 3, 8057 .access = PL1_R, .type = ARM_CP_CONST, 8058 .accessfn = access_aa64_tid3, 8059 .resetvalue = cpu->isar.id_aa64mmfr3 }, 8060 { .name = "ID_AA64MMFR4_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8061 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 4, 8062 .access = PL1_R, .type = ARM_CP_CONST, 8063 .accessfn = access_aa64_tid3, 8064 .resetvalue = 0 }, 8065 { .name = "ID_AA64MMFR5_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8066 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 5, 8067 .access = PL1_R, .type = ARM_CP_CONST, 8068 .accessfn = access_aa64_tid3, 8069 .resetvalue = 0 }, 8070 { .name = "ID_AA64MMFR6_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8071 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 6, 8072 .access = PL1_R, .type = ARM_CP_CONST, 8073 .accessfn = access_aa64_tid3, 8074 .resetvalue = 0 }, 8075 { .name = "ID_AA64MMFR7_EL1_RESERVED", .state = ARM_CP_STATE_AA64, 8076 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 7, .opc2 = 7, 8077 .access = PL1_R, .type = ARM_CP_CONST, 8078 .accessfn = access_aa64_tid3, 8079 .resetvalue = 0 }, 8080 { .name = "MVFR0_EL1", .state = ARM_CP_STATE_AA64, 8081 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 8082 .access = PL1_R, .type = ARM_CP_CONST, 8083 .accessfn = access_aa64_tid3, 8084 .resetvalue = cpu->isar.mvfr0 }, 8085 { .name = "MVFR1_EL1", .state = ARM_CP_STATE_AA64, 8086 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 8087 .access = PL1_R, .type = ARM_CP_CONST, 8088 .accessfn = access_aa64_tid3, 8089 .resetvalue = cpu->isar.mvfr1 }, 8090 { .name = "MVFR2_EL1", .state = ARM_CP_STATE_AA64, 8091 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 8092 .access = PL1_R, .type = ARM_CP_CONST, 8093 .accessfn = access_aa64_tid3, 8094 .resetvalue = cpu->isar.mvfr2 }, 8095 /* 8096 * "0, c0, c3, {0,1,2}" are the encodings corresponding to 8097 * AArch64 MVFR[012]_EL1. Define the STATE_AA32 encoding 8098 * as RAZ, since it is in the "reserved for future ID 8099 * registers, RAZ" part of the AArch32 encoding space. 8100 */ 8101 { .name = "RES_0_C0_C3_0", .state = ARM_CP_STATE_AA32, 8102 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 0, 8103 .access = PL1_R, .type = ARM_CP_CONST, 8104 .accessfn = access_aa64_tid3, 8105 .resetvalue = 0 }, 8106 { .name = "RES_0_C0_C3_1", .state = ARM_CP_STATE_AA32, 8107 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 1, 8108 .access = PL1_R, .type = ARM_CP_CONST, 8109 .accessfn = access_aa64_tid3, 8110 .resetvalue = 0 }, 8111 { .name = "RES_0_C0_C3_2", .state = ARM_CP_STATE_AA32, 8112 .cp = 15, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 2, 8113 .access = PL1_R, .type = ARM_CP_CONST, 8114 .accessfn = access_aa64_tid3, 8115 .resetvalue = 0 }, 8116 /* 8117 * Other encodings in "0, c0, c3, ..." are STATE_BOTH because 8118 * they're also RAZ for AArch64, and in v8 are gradually 8119 * being filled with AArch64-view-of-AArch32-ID-register 8120 * for new ID registers. 8121 */ 8122 { .name = "RES_0_C0_C3_3", .state = ARM_CP_STATE_BOTH, 8123 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 3, 8124 .access = PL1_R, .type = ARM_CP_CONST, 8125 .accessfn = access_aa64_tid3, 8126 .resetvalue = 0 }, 8127 { .name = "ID_PFR2", .state = ARM_CP_STATE_BOTH, 8128 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 4, 8129 .access = PL1_R, .type = ARM_CP_CONST, 8130 .accessfn = access_aa64_tid3, 8131 .resetvalue = cpu->isar.id_pfr2 }, 8132 { .name = "ID_DFR1", .state = ARM_CP_STATE_BOTH, 8133 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 5, 8134 .access = PL1_R, .type = ARM_CP_CONST, 8135 .accessfn = access_aa64_tid3, 8136 .resetvalue = cpu->isar.id_dfr1 }, 8137 { .name = "ID_MMFR5", .state = ARM_CP_STATE_BOTH, 8138 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 6, 8139 .access = PL1_R, .type = ARM_CP_CONST, 8140 .accessfn = access_aa64_tid3, 8141 .resetvalue = cpu->isar.id_mmfr5 }, 8142 { .name = "RES_0_C0_C3_7", .state = ARM_CP_STATE_BOTH, 8143 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 3, .opc2 = 7, 8144 .access = PL1_R, .type = ARM_CP_CONST, 8145 .accessfn = access_aa64_tid3, 8146 .resetvalue = 0 }, 8147 { .name = "PMCEID0", .state = ARM_CP_STATE_AA32, 8148 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 6, 8149 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 8150 .fgt = FGT_PMCEIDN_EL0, 8151 .resetvalue = extract64(cpu->pmceid0, 0, 32) }, 8152 { .name = "PMCEID0_EL0", .state = ARM_CP_STATE_AA64, 8153 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 6, 8154 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 8155 .fgt = FGT_PMCEIDN_EL0, 8156 .resetvalue = cpu->pmceid0 }, 8157 { .name = "PMCEID1", .state = ARM_CP_STATE_AA32, 8158 .cp = 15, .opc1 = 0, .crn = 9, .crm = 12, .opc2 = 7, 8159 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 8160 .fgt = FGT_PMCEIDN_EL0, 8161 .resetvalue = extract64(cpu->pmceid1, 0, 32) }, 8162 { .name = "PMCEID1_EL0", .state = ARM_CP_STATE_AA64, 8163 .opc0 = 3, .opc1 = 3, .crn = 9, .crm = 12, .opc2 = 7, 8164 .access = PL0_R, .accessfn = pmreg_access, .type = ARM_CP_CONST, 8165 .fgt = FGT_PMCEIDN_EL0, 8166 .resetvalue = cpu->pmceid1 }, 8167 }; 8168 #ifdef CONFIG_USER_ONLY 8169 static const ARMCPRegUserSpaceInfo v8_user_idregs[] = { 8170 { .name = "ID_AA64PFR0_EL1", 8171 .exported_bits = R_ID_AA64PFR0_FP_MASK | 8172 R_ID_AA64PFR0_ADVSIMD_MASK | 8173 R_ID_AA64PFR0_SVE_MASK | 8174 R_ID_AA64PFR0_DIT_MASK, 8175 .fixed_bits = (0x1u << R_ID_AA64PFR0_EL0_SHIFT) | 8176 (0x1u << R_ID_AA64PFR0_EL1_SHIFT) }, 8177 { .name = "ID_AA64PFR1_EL1", 8178 .exported_bits = R_ID_AA64PFR1_BT_MASK | 8179 R_ID_AA64PFR1_SSBS_MASK | 8180 R_ID_AA64PFR1_MTE_MASK | 8181 R_ID_AA64PFR1_SME_MASK }, 8182 { .name = "ID_AA64PFR*_EL1_RESERVED", 8183 .is_glob = true }, 8184 { .name = "ID_AA64ZFR0_EL1", 8185 .exported_bits = R_ID_AA64ZFR0_SVEVER_MASK | 8186 R_ID_AA64ZFR0_AES_MASK | 8187 R_ID_AA64ZFR0_BITPERM_MASK | 8188 R_ID_AA64ZFR0_BFLOAT16_MASK | 8189 R_ID_AA64ZFR0_B16B16_MASK | 8190 R_ID_AA64ZFR0_SHA3_MASK | 8191 R_ID_AA64ZFR0_SM4_MASK | 8192 R_ID_AA64ZFR0_I8MM_MASK | 8193 R_ID_AA64ZFR0_F32MM_MASK | 8194 R_ID_AA64ZFR0_F64MM_MASK }, 8195 { .name = "ID_AA64SMFR0_EL1", 8196 .exported_bits = R_ID_AA64SMFR0_F32F32_MASK | 8197 R_ID_AA64SMFR0_BI32I32_MASK | 8198 R_ID_AA64SMFR0_B16F32_MASK | 8199 R_ID_AA64SMFR0_F16F32_MASK | 8200 R_ID_AA64SMFR0_I8I32_MASK | 8201 R_ID_AA64SMFR0_F16F16_MASK | 8202 R_ID_AA64SMFR0_B16B16_MASK | 8203 R_ID_AA64SMFR0_I16I32_MASK | 8204 R_ID_AA64SMFR0_F64F64_MASK | 8205 R_ID_AA64SMFR0_I16I64_MASK | 8206 R_ID_AA64SMFR0_SMEVER_MASK | 8207 R_ID_AA64SMFR0_FA64_MASK }, 8208 { .name = "ID_AA64MMFR0_EL1", 8209 .exported_bits = R_ID_AA64MMFR0_ECV_MASK, 8210 .fixed_bits = (0xfu << R_ID_AA64MMFR0_TGRAN64_SHIFT) | 8211 (0xfu << R_ID_AA64MMFR0_TGRAN4_SHIFT) }, 8212 { .name = "ID_AA64MMFR1_EL1", 8213 .exported_bits = R_ID_AA64MMFR1_AFP_MASK }, 8214 { .name = "ID_AA64MMFR2_EL1", 8215 .exported_bits = R_ID_AA64MMFR2_AT_MASK }, 8216 { .name = "ID_AA64MMFR3_EL1", 8217 .exported_bits = 0 }, 8218 { .name = "ID_AA64MMFR*_EL1_RESERVED", 8219 .is_glob = true }, 8220 { .name = "ID_AA64DFR0_EL1", 8221 .fixed_bits = (0x6u << R_ID_AA64DFR0_DEBUGVER_SHIFT) }, 8222 { .name = "ID_AA64DFR1_EL1" }, 8223 { .name = "ID_AA64DFR*_EL1_RESERVED", 8224 .is_glob = true }, 8225 { .name = "ID_AA64AFR*", 8226 .is_glob = true }, 8227 { .name = "ID_AA64ISAR0_EL1", 8228 .exported_bits = R_ID_AA64ISAR0_AES_MASK | 8229 R_ID_AA64ISAR0_SHA1_MASK | 8230 R_ID_AA64ISAR0_SHA2_MASK | 8231 R_ID_AA64ISAR0_CRC32_MASK | 8232 R_ID_AA64ISAR0_ATOMIC_MASK | 8233 R_ID_AA64ISAR0_RDM_MASK | 8234 R_ID_AA64ISAR0_SHA3_MASK | 8235 R_ID_AA64ISAR0_SM3_MASK | 8236 R_ID_AA64ISAR0_SM4_MASK | 8237 R_ID_AA64ISAR0_DP_MASK | 8238 R_ID_AA64ISAR0_FHM_MASK | 8239 R_ID_AA64ISAR0_TS_MASK | 8240 R_ID_AA64ISAR0_RNDR_MASK }, 8241 { .name = "ID_AA64ISAR1_EL1", 8242 .exported_bits = R_ID_AA64ISAR1_DPB_MASK | 8243 R_ID_AA64ISAR1_APA_MASK | 8244 R_ID_AA64ISAR1_API_MASK | 8245 R_ID_AA64ISAR1_JSCVT_MASK | 8246 R_ID_AA64ISAR1_FCMA_MASK | 8247 R_ID_AA64ISAR1_LRCPC_MASK | 8248 R_ID_AA64ISAR1_GPA_MASK | 8249 R_ID_AA64ISAR1_GPI_MASK | 8250 R_ID_AA64ISAR1_FRINTTS_MASK | 8251 R_ID_AA64ISAR1_SB_MASK | 8252 R_ID_AA64ISAR1_BF16_MASK | 8253 R_ID_AA64ISAR1_DGH_MASK | 8254 R_ID_AA64ISAR1_I8MM_MASK }, 8255 { .name = "ID_AA64ISAR2_EL1", 8256 .exported_bits = R_ID_AA64ISAR2_WFXT_MASK | 8257 R_ID_AA64ISAR2_RPRES_MASK | 8258 R_ID_AA64ISAR2_GPA3_MASK | 8259 R_ID_AA64ISAR2_APA3_MASK | 8260 R_ID_AA64ISAR2_MOPS_MASK | 8261 R_ID_AA64ISAR2_BC_MASK | 8262 R_ID_AA64ISAR2_RPRFM_MASK | 8263 R_ID_AA64ISAR2_CSSC_MASK }, 8264 { .name = "ID_AA64ISAR*_EL1_RESERVED", 8265 .is_glob = true }, 8266 }; 8267 modify_arm_cp_regs(v8_idregs, v8_user_idregs); 8268 #endif 8269 /* 8270 * RVBAR_EL1 and RMR_EL1 only implemented if EL1 is the highest EL. 8271 * TODO: For RMR, a write with bit 1 set should do something with 8272 * cpu_reset(). In the meantime, "the bit is strictly a request", 8273 * so we are in spec just ignoring writes. 8274 */ 8275 if (!arm_feature(env, ARM_FEATURE_EL3) && 8276 !arm_feature(env, ARM_FEATURE_EL2)) { 8277 ARMCPRegInfo el1_reset_regs[] = { 8278 { .name = "RVBAR_EL1", .state = ARM_CP_STATE_BOTH, 8279 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 8280 .access = PL1_R, 8281 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) }, 8282 { .name = "RMR_EL1", .state = ARM_CP_STATE_BOTH, 8283 .opc0 = 3, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2, 8284 .access = PL1_RW, .type = ARM_CP_CONST, 8285 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) } 8286 }; 8287 define_arm_cp_regs(cpu, el1_reset_regs); 8288 } 8289 define_arm_cp_regs(cpu, v8_idregs); 8290 define_arm_cp_regs(cpu, v8_cp_reginfo); 8291 if (cpu_isar_feature(aa64_aa32_el1, cpu)) { 8292 define_arm_cp_regs(cpu, v8_aa32_el1_reginfo); 8293 } 8294 8295 for (i = 4; i < 16; i++) { 8296 /* 8297 * Encodings in "0, c0, {c4-c7}, {0-7}" are RAZ for AArch32. 8298 * For pre-v8 cores there are RAZ patterns for these in 8299 * id_pre_v8_midr_cp_reginfo[]; for v8 we do that here. 8300 * v8 extends the "must RAZ" part of the ID register space 8301 * to also cover c0, 0, c{8-15}, {0-7}. 8302 * These are STATE_AA32 because in the AArch64 sysreg space 8303 * c4-c7 is where the AArch64 ID registers live (and we've 8304 * already defined those in v8_idregs[]), and c8-c15 are not 8305 * "must RAZ" for AArch64. 8306 */ 8307 g_autofree char *name = g_strdup_printf("RES_0_C0_C%d_X", i); 8308 ARMCPRegInfo v8_aa32_raz_idregs = { 8309 .name = name, 8310 .state = ARM_CP_STATE_AA32, 8311 .cp = 15, .opc1 = 0, .crn = 0, .crm = i, .opc2 = CP_ANY, 8312 .access = PL1_R, .type = ARM_CP_CONST, 8313 .accessfn = access_aa64_tid3, 8314 .resetvalue = 0 }; 8315 define_one_arm_cp_reg(cpu, &v8_aa32_raz_idregs); 8316 } 8317 } 8318 8319 /* 8320 * Register the base EL2 cpregs. 8321 * Pre v8, these registers are implemented only as part of the 8322 * Virtualization Extensions (EL2 present). Beginning with v8, 8323 * if EL2 is missing but EL3 is enabled, mostly these become 8324 * RES0 from EL3, with some specific exceptions. 8325 */ 8326 if (arm_feature(env, ARM_FEATURE_EL2) 8327 || (arm_feature(env, ARM_FEATURE_EL3) 8328 && arm_feature(env, ARM_FEATURE_V8))) { 8329 uint64_t vmpidr_def = mpidr_read_val(env); 8330 ARMCPRegInfo vpidr_regs[] = { 8331 { .name = "VPIDR", .state = ARM_CP_STATE_AA32, 8332 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 8333 .access = PL2_RW, .accessfn = access_el3_aa32ns, 8334 .resetvalue = cpu->midr, 8335 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ, 8336 .fieldoffset = offsetoflow32(CPUARMState, cp15.vpidr_el2) }, 8337 { .name = "VPIDR_EL2", .state = ARM_CP_STATE_AA64, 8338 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 0, 8339 .access = PL2_RW, .resetvalue = cpu->midr, 8340 .type = ARM_CP_EL3_NO_EL2_C_NZ, 8341 .nv2_redirect_offset = 0x88, 8342 .fieldoffset = offsetof(CPUARMState, cp15.vpidr_el2) }, 8343 { .name = "VMPIDR", .state = ARM_CP_STATE_AA32, 8344 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 8345 .access = PL2_RW, .accessfn = access_el3_aa32ns, 8346 .resetvalue = vmpidr_def, 8347 .type = ARM_CP_ALIAS | ARM_CP_EL3_NO_EL2_C_NZ, 8348 .fieldoffset = offsetoflow32(CPUARMState, cp15.vmpidr_el2) }, 8349 { .name = "VMPIDR_EL2", .state = ARM_CP_STATE_AA64, 8350 .opc0 = 3, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 5, 8351 .access = PL2_RW, .resetvalue = vmpidr_def, 8352 .type = ARM_CP_EL3_NO_EL2_C_NZ, 8353 .nv2_redirect_offset = 0x50, 8354 .fieldoffset = offsetof(CPUARMState, cp15.vmpidr_el2) }, 8355 }; 8356 /* 8357 * The only field of MDCR_EL2 that has a defined architectural reset 8358 * value is MDCR_EL2.HPMN which should reset to the value of PMCR_EL0.N. 8359 */ 8360 ARMCPRegInfo mdcr_el2 = { 8361 .name = "MDCR_EL2", .state = ARM_CP_STATE_BOTH, .type = ARM_CP_IO, 8362 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 1, .opc2 = 1, 8363 .writefn = mdcr_el2_write, 8364 .access = PL2_RW, .resetvalue = pmu_num_counters(env), 8365 .fieldoffset = offsetof(CPUARMState, cp15.mdcr_el2), 8366 }; 8367 define_one_arm_cp_reg(cpu, &mdcr_el2); 8368 define_arm_cp_regs(cpu, vpidr_regs); 8369 define_arm_cp_regs(cpu, el2_cp_reginfo); 8370 if (arm_feature(env, ARM_FEATURE_V8)) { 8371 define_arm_cp_regs(cpu, el2_v8_cp_reginfo); 8372 } 8373 if (cpu_isar_feature(aa64_sel2, cpu)) { 8374 define_arm_cp_regs(cpu, el2_sec_cp_reginfo); 8375 } 8376 /* 8377 * RVBAR_EL2 and RMR_EL2 only implemented if EL2 is the highest EL. 8378 * See commentary near RMR_EL1. 8379 */ 8380 if (!arm_feature(env, ARM_FEATURE_EL3)) { 8381 static const ARMCPRegInfo el2_reset_regs[] = { 8382 { .name = "RVBAR_EL2", .state = ARM_CP_STATE_AA64, 8383 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 1, 8384 .access = PL2_R, 8385 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) }, 8386 { .name = "RVBAR", .type = ARM_CP_ALIAS, 8387 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 1, 8388 .access = PL2_R, 8389 .fieldoffset = offsetof(CPUARMState, cp15.rvbar) }, 8390 { .name = "RMR_EL2", .state = ARM_CP_STATE_AA64, 8391 .opc0 = 3, .opc1 = 4, .crn = 12, .crm = 0, .opc2 = 2, 8392 .access = PL2_RW, .type = ARM_CP_CONST, .resetvalue = 1 }, 8393 }; 8394 define_arm_cp_regs(cpu, el2_reset_regs); 8395 } 8396 } 8397 8398 /* Register the base EL3 cpregs. */ 8399 if (arm_feature(env, ARM_FEATURE_EL3)) { 8400 define_arm_cp_regs(cpu, el3_cp_reginfo); 8401 ARMCPRegInfo el3_regs[] = { 8402 { .name = "RVBAR_EL3", .state = ARM_CP_STATE_AA64, 8403 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 1, 8404 .access = PL3_R, 8405 .fieldoffset = offsetof(CPUARMState, cp15.rvbar), }, 8406 { .name = "RMR_EL3", .state = ARM_CP_STATE_AA64, 8407 .opc0 = 3, .opc1 = 6, .crn = 12, .crm = 0, .opc2 = 2, 8408 .access = PL3_RW, .type = ARM_CP_CONST, .resetvalue = 1 }, 8409 { .name = "RMR", .state = ARM_CP_STATE_AA32, 8410 .cp = 15, .opc1 = 0, .crn = 12, .crm = 0, .opc2 = 2, 8411 .access = PL3_RW, .type = ARM_CP_CONST, 8412 .resetvalue = arm_feature(env, ARM_FEATURE_AARCH64) }, 8413 { .name = "SCTLR_EL3", .state = ARM_CP_STATE_AA64, 8414 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 0, 8415 .access = PL3_RW, 8416 .raw_writefn = raw_write, .writefn = sctlr_write, 8417 .fieldoffset = offsetof(CPUARMState, cp15.sctlr_el[3]), 8418 .resetvalue = cpu->reset_sctlr }, 8419 }; 8420 8421 define_arm_cp_regs(cpu, el3_regs); 8422 } 8423 /* 8424 * The behaviour of NSACR is sufficiently various that we don't 8425 * try to describe it in a single reginfo: 8426 * if EL3 is 64 bit, then trap to EL3 from S EL1, 8427 * reads as constant 0xc00 from NS EL1 and NS EL2 8428 * if EL3 is 32 bit, then RW at EL3, RO at NS EL1 and NS EL2 8429 * if v7 without EL3, register doesn't exist 8430 * if v8 without EL3, reads as constant 0xc00 from NS EL1 and NS EL2 8431 */ 8432 if (arm_feature(env, ARM_FEATURE_EL3)) { 8433 if (arm_feature(env, ARM_FEATURE_AARCH64)) { 8434 static const ARMCPRegInfo nsacr = { 8435 .name = "NSACR", .type = ARM_CP_CONST, 8436 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 8437 .access = PL1_RW, .accessfn = nsacr_access, 8438 .resetvalue = 0xc00 8439 }; 8440 define_one_arm_cp_reg(cpu, &nsacr); 8441 } else { 8442 static const ARMCPRegInfo nsacr = { 8443 .name = "NSACR", 8444 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 8445 .access = PL3_RW | PL1_R, 8446 .resetvalue = 0, 8447 .fieldoffset = offsetof(CPUARMState, cp15.nsacr) 8448 }; 8449 define_one_arm_cp_reg(cpu, &nsacr); 8450 } 8451 } else { 8452 if (arm_feature(env, ARM_FEATURE_V8)) { 8453 static const ARMCPRegInfo nsacr = { 8454 .name = "NSACR", .type = ARM_CP_CONST, 8455 .cp = 15, .opc1 = 0, .crn = 1, .crm = 1, .opc2 = 2, 8456 .access = PL1_R, 8457 .resetvalue = 0xc00 8458 }; 8459 define_one_arm_cp_reg(cpu, &nsacr); 8460 } 8461 } 8462 8463 if (arm_feature(env, ARM_FEATURE_PMSA)) { 8464 if (arm_feature(env, ARM_FEATURE_V6)) { 8465 /* PMSAv6 not implemented */ 8466 assert(arm_feature(env, ARM_FEATURE_V7)); 8467 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 8468 define_arm_cp_regs(cpu, pmsav7_cp_reginfo); 8469 } else { 8470 define_arm_cp_regs(cpu, pmsav5_cp_reginfo); 8471 } 8472 } else { 8473 define_arm_cp_regs(cpu, vmsa_pmsa_cp_reginfo); 8474 define_arm_cp_regs(cpu, vmsa_cp_reginfo); 8475 /* TTCBR2 is introduced with ARMv8.2-AA32HPD. */ 8476 if (cpu_isar_feature(aa32_hpd, cpu)) { 8477 define_one_arm_cp_reg(cpu, &ttbcr2_reginfo); 8478 } 8479 } 8480 if (arm_feature(env, ARM_FEATURE_THUMB2EE)) { 8481 define_arm_cp_regs(cpu, t2ee_cp_reginfo); 8482 } 8483 if (arm_feature(env, ARM_FEATURE_GENERIC_TIMER)) { 8484 define_arm_cp_regs(cpu, generic_timer_cp_reginfo); 8485 } 8486 if (cpu_isar_feature(aa64_ecv_traps, cpu)) { 8487 define_arm_cp_regs(cpu, gen_timer_ecv_cp_reginfo); 8488 } 8489 #ifndef CONFIG_USER_ONLY 8490 if (cpu_isar_feature(aa64_ecv, cpu)) { 8491 define_one_arm_cp_reg(cpu, &gen_timer_cntpoff_reginfo); 8492 } 8493 #endif 8494 if (arm_feature(env, ARM_FEATURE_VAPA)) { 8495 ARMCPRegInfo vapa_cp_reginfo[] = { 8496 { .name = "PAR", .cp = 15, .crn = 7, .crm = 4, .opc1 = 0, .opc2 = 0, 8497 .access = PL1_RW, .resetvalue = 0, 8498 .bank_fieldoffsets = { offsetoflow32(CPUARMState, cp15.par_s), 8499 offsetoflow32(CPUARMState, cp15.par_ns) }, 8500 .writefn = par_write}, 8501 #ifndef CONFIG_USER_ONLY 8502 /* This underdecoding is safe because the reginfo is NO_RAW. */ 8503 { .name = "ATS", .cp = 15, .crn = 7, .crm = 8, .opc1 = 0, .opc2 = CP_ANY, 8504 .access = PL1_W, .accessfn = ats_access, 8505 .writefn = ats_write, .type = ARM_CP_NO_RAW | ARM_CP_RAISES_EXC }, 8506 #endif 8507 }; 8508 8509 /* 8510 * When LPAE exists this 32-bit PAR register is an alias of the 8511 * 64-bit AArch32 PAR register defined in lpae_cp_reginfo[] 8512 */ 8513 if (arm_feature(env, ARM_FEATURE_LPAE)) { 8514 vapa_cp_reginfo[0].type = ARM_CP_ALIAS | ARM_CP_NO_GDB; 8515 } 8516 define_arm_cp_regs(cpu, vapa_cp_reginfo); 8517 } 8518 if (arm_feature(env, ARM_FEATURE_CACHE_TEST_CLEAN)) { 8519 define_arm_cp_regs(cpu, cache_test_clean_cp_reginfo); 8520 } 8521 if (arm_feature(env, ARM_FEATURE_CACHE_DIRTY_REG)) { 8522 define_arm_cp_regs(cpu, cache_dirty_status_cp_reginfo); 8523 } 8524 if (arm_feature(env, ARM_FEATURE_CACHE_BLOCK_OPS)) { 8525 define_arm_cp_regs(cpu, cache_block_ops_cp_reginfo); 8526 } 8527 if (arm_feature(env, ARM_FEATURE_OMAPCP)) { 8528 define_arm_cp_regs(cpu, omap_cp_reginfo); 8529 } 8530 if (arm_feature(env, ARM_FEATURE_STRONGARM)) { 8531 define_arm_cp_regs(cpu, strongarm_cp_reginfo); 8532 } 8533 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 8534 define_arm_cp_regs(cpu, xscale_cp_reginfo); 8535 } 8536 if (arm_feature(env, ARM_FEATURE_DUMMY_C15_REGS)) { 8537 define_arm_cp_regs(cpu, dummy_c15_cp_reginfo); 8538 } 8539 if (arm_feature(env, ARM_FEATURE_LPAE)) { 8540 define_arm_cp_regs(cpu, lpae_cp_reginfo); 8541 } 8542 if (cpu_isar_feature(aa32_jazelle, cpu)) { 8543 define_arm_cp_regs(cpu, jazelle_regs); 8544 } 8545 /* 8546 * Slightly awkwardly, the OMAP and StrongARM cores need all of 8547 * cp15 crn=0 to be writes-ignored, whereas for other cores they should 8548 * be read-only (ie write causes UNDEF exception). 8549 */ 8550 { 8551 ARMCPRegInfo id_pre_v8_midr_cp_reginfo[] = { 8552 /* 8553 * Pre-v8 MIDR space. 8554 * Note that the MIDR isn't a simple constant register because 8555 * of the TI925 behaviour where writes to another register can 8556 * cause the MIDR value to change. 8557 * 8558 * Unimplemented registers in the c15 0 0 0 space default to 8559 * MIDR. Define MIDR first as this entire space, then CTR, TCMTR 8560 * and friends override accordingly. 8561 */ 8562 { .name = "MIDR", 8563 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = CP_ANY, 8564 .access = PL1_R, .resetvalue = cpu->midr, 8565 .writefn = arm_cp_write_ignore, .raw_writefn = raw_write, 8566 .readfn = midr_read, 8567 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 8568 .type = ARM_CP_OVERRIDE }, 8569 /* crn = 0 op1 = 0 crm = 3..7 : currently unassigned; we RAZ. */ 8570 { .name = "DUMMY", 8571 .cp = 15, .crn = 0, .crm = 3, .opc1 = 0, .opc2 = CP_ANY, 8572 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 8573 { .name = "DUMMY", 8574 .cp = 15, .crn = 0, .crm = 4, .opc1 = 0, .opc2 = CP_ANY, 8575 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 8576 { .name = "DUMMY", 8577 .cp = 15, .crn = 0, .crm = 5, .opc1 = 0, .opc2 = CP_ANY, 8578 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 8579 { .name = "DUMMY", 8580 .cp = 15, .crn = 0, .crm = 6, .opc1 = 0, .opc2 = CP_ANY, 8581 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 8582 { .name = "DUMMY", 8583 .cp = 15, .crn = 0, .crm = 7, .opc1 = 0, .opc2 = CP_ANY, 8584 .access = PL1_R, .type = ARM_CP_CONST, .resetvalue = 0 }, 8585 }; 8586 ARMCPRegInfo id_v8_midr_cp_reginfo[] = { 8587 { .name = "MIDR_EL1", .state = ARM_CP_STATE_BOTH, 8588 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 0, 8589 .access = PL1_R, .type = ARM_CP_NO_RAW, .resetvalue = cpu->midr, 8590 .fgt = FGT_MIDR_EL1, 8591 .fieldoffset = offsetof(CPUARMState, cp15.c0_cpuid), 8592 .readfn = midr_read }, 8593 /* crn = 0 op1 = 0 crm = 0 op2 = 7 : AArch32 aliases of MIDR */ 8594 { .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST, 8595 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 7, 8596 .access = PL1_R, .resetvalue = cpu->midr }, 8597 { .name = "REVIDR_EL1", .state = ARM_CP_STATE_BOTH, 8598 .opc0 = 3, .opc1 = 0, .crn = 0, .crm = 0, .opc2 = 6, 8599 .access = PL1_R, 8600 .accessfn = access_aa64_tid1, 8601 .fgt = FGT_REVIDR_EL1, 8602 .type = ARM_CP_CONST, .resetvalue = cpu->revidr }, 8603 }; 8604 ARMCPRegInfo id_v8_midr_alias_cp_reginfo = { 8605 .name = "MIDR", .type = ARM_CP_ALIAS | ARM_CP_CONST | ARM_CP_NO_GDB, 8606 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 8607 .access = PL1_R, .resetvalue = cpu->midr 8608 }; 8609 ARMCPRegInfo id_cp_reginfo[] = { 8610 /* These are common to v8 and pre-v8 */ 8611 { .name = "CTR", 8612 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 1, 8613 .access = PL1_R, .accessfn = ctr_el0_access, 8614 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 8615 { .name = "CTR_EL0", .state = ARM_CP_STATE_AA64, 8616 .opc0 = 3, .opc1 = 3, .opc2 = 1, .crn = 0, .crm = 0, 8617 .access = PL0_R, .accessfn = ctr_el0_access, 8618 .fgt = FGT_CTR_EL0, 8619 .type = ARM_CP_CONST, .resetvalue = cpu->ctr }, 8620 /* TCMTR and TLBTR exist in v8 but have no 64-bit versions */ 8621 { .name = "TCMTR", 8622 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 2, 8623 .access = PL1_R, 8624 .accessfn = access_aa32_tid1, 8625 .type = ARM_CP_CONST, .resetvalue = 0 }, 8626 }; 8627 /* TLBTR is specific to VMSA */ 8628 ARMCPRegInfo id_tlbtr_reginfo = { 8629 .name = "TLBTR", 8630 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 3, 8631 .access = PL1_R, 8632 .accessfn = access_aa32_tid1, 8633 .type = ARM_CP_CONST, .resetvalue = 0, 8634 }; 8635 /* MPUIR is specific to PMSA V6+ */ 8636 ARMCPRegInfo id_mpuir_reginfo = { 8637 .name = "MPUIR", 8638 .cp = 15, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 4, 8639 .access = PL1_R, .type = ARM_CP_CONST, 8640 .resetvalue = cpu->pmsav7_dregion << 8 8641 }; 8642 /* HMPUIR is specific to PMSA V8 */ 8643 ARMCPRegInfo id_hmpuir_reginfo = { 8644 .name = "HMPUIR", 8645 .cp = 15, .opc1 = 4, .crn = 0, .crm = 0, .opc2 = 4, 8646 .access = PL2_R, .type = ARM_CP_CONST, 8647 .resetvalue = cpu->pmsav8r_hdregion 8648 }; 8649 static const ARMCPRegInfo crn0_wi_reginfo = { 8650 .name = "CRN0_WI", .cp = 15, .crn = 0, .crm = CP_ANY, 8651 .opc1 = CP_ANY, .opc2 = CP_ANY, .access = PL1_W, 8652 .type = ARM_CP_NOP | ARM_CP_OVERRIDE 8653 }; 8654 #ifdef CONFIG_USER_ONLY 8655 static const ARMCPRegUserSpaceInfo id_v8_user_midr_cp_reginfo[] = { 8656 { .name = "MIDR_EL1", 8657 .exported_bits = R_MIDR_EL1_REVISION_MASK | 8658 R_MIDR_EL1_PARTNUM_MASK | 8659 R_MIDR_EL1_ARCHITECTURE_MASK | 8660 R_MIDR_EL1_VARIANT_MASK | 8661 R_MIDR_EL1_IMPLEMENTER_MASK }, 8662 { .name = "REVIDR_EL1" }, 8663 }; 8664 modify_arm_cp_regs(id_v8_midr_cp_reginfo, id_v8_user_midr_cp_reginfo); 8665 #endif 8666 if (arm_feature(env, ARM_FEATURE_OMAPCP) || 8667 arm_feature(env, ARM_FEATURE_STRONGARM)) { 8668 size_t i; 8669 /* 8670 * Register the blanket "writes ignored" value first to cover the 8671 * whole space. Then update the specific ID registers to allow write 8672 * access, so that they ignore writes rather than causing them to 8673 * UNDEF. 8674 */ 8675 define_one_arm_cp_reg(cpu, &crn0_wi_reginfo); 8676 for (i = 0; i < ARRAY_SIZE(id_pre_v8_midr_cp_reginfo); ++i) { 8677 id_pre_v8_midr_cp_reginfo[i].access = PL1_RW; 8678 } 8679 for (i = 0; i < ARRAY_SIZE(id_cp_reginfo); ++i) { 8680 id_cp_reginfo[i].access = PL1_RW; 8681 } 8682 id_mpuir_reginfo.access = PL1_RW; 8683 id_tlbtr_reginfo.access = PL1_RW; 8684 } 8685 if (arm_feature(env, ARM_FEATURE_V8)) { 8686 define_arm_cp_regs(cpu, id_v8_midr_cp_reginfo); 8687 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 8688 define_one_arm_cp_reg(cpu, &id_v8_midr_alias_cp_reginfo); 8689 } 8690 } else { 8691 define_arm_cp_regs(cpu, id_pre_v8_midr_cp_reginfo); 8692 } 8693 define_arm_cp_regs(cpu, id_cp_reginfo); 8694 if (!arm_feature(env, ARM_FEATURE_PMSA)) { 8695 define_one_arm_cp_reg(cpu, &id_tlbtr_reginfo); 8696 } else if (arm_feature(env, ARM_FEATURE_PMSA) && 8697 arm_feature(env, ARM_FEATURE_V8)) { 8698 uint32_t i = 0; 8699 char *tmp_string; 8700 8701 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 8702 define_one_arm_cp_reg(cpu, &id_hmpuir_reginfo); 8703 define_arm_cp_regs(cpu, pmsav8r_cp_reginfo); 8704 8705 /* Register alias is only valid for first 32 indexes */ 8706 for (i = 0; i < MIN(cpu->pmsav7_dregion, 32); ++i) { 8707 uint8_t crm = 0b1000 | extract32(i, 1, 3); 8708 uint8_t opc1 = extract32(i, 4, 1); 8709 uint8_t opc2 = extract32(i, 0, 1) << 2; 8710 8711 tmp_string = g_strdup_printf("PRBAR%u", i); 8712 ARMCPRegInfo tmp_prbarn_reginfo = { 8713 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW, 8714 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2, 8715 .access = PL1_RW, .resetvalue = 0, 8716 .accessfn = access_tvm_trvm, 8717 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read 8718 }; 8719 define_one_arm_cp_reg(cpu, &tmp_prbarn_reginfo); 8720 g_free(tmp_string); 8721 8722 opc2 = extract32(i, 0, 1) << 2 | 0x1; 8723 tmp_string = g_strdup_printf("PRLAR%u", i); 8724 ARMCPRegInfo tmp_prlarn_reginfo = { 8725 .name = tmp_string, .type = ARM_CP_ALIAS | ARM_CP_NO_RAW, 8726 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2, 8727 .access = PL1_RW, .resetvalue = 0, 8728 .accessfn = access_tvm_trvm, 8729 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read 8730 }; 8731 define_one_arm_cp_reg(cpu, &tmp_prlarn_reginfo); 8732 g_free(tmp_string); 8733 } 8734 8735 /* Register alias is only valid for first 32 indexes */ 8736 for (i = 0; i < MIN(cpu->pmsav8r_hdregion, 32); ++i) { 8737 uint8_t crm = 0b1000 | extract32(i, 1, 3); 8738 uint8_t opc1 = 0b100 | extract32(i, 4, 1); 8739 uint8_t opc2 = extract32(i, 0, 1) << 2; 8740 8741 tmp_string = g_strdup_printf("HPRBAR%u", i); 8742 ARMCPRegInfo tmp_hprbarn_reginfo = { 8743 .name = tmp_string, 8744 .type = ARM_CP_NO_RAW, 8745 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2, 8746 .access = PL2_RW, .resetvalue = 0, 8747 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read 8748 }; 8749 define_one_arm_cp_reg(cpu, &tmp_hprbarn_reginfo); 8750 g_free(tmp_string); 8751 8752 opc2 = extract32(i, 0, 1) << 2 | 0x1; 8753 tmp_string = g_strdup_printf("HPRLAR%u", i); 8754 ARMCPRegInfo tmp_hprlarn_reginfo = { 8755 .name = tmp_string, 8756 .type = ARM_CP_NO_RAW, 8757 .cp = 15, .opc1 = opc1, .crn = 6, .crm = crm, .opc2 = opc2, 8758 .access = PL2_RW, .resetvalue = 0, 8759 .writefn = pmsav8r_regn_write, .readfn = pmsav8r_regn_read 8760 }; 8761 define_one_arm_cp_reg(cpu, &tmp_hprlarn_reginfo); 8762 g_free(tmp_string); 8763 } 8764 } else if (arm_feature(env, ARM_FEATURE_V7)) { 8765 define_one_arm_cp_reg(cpu, &id_mpuir_reginfo); 8766 } 8767 } 8768 8769 if (arm_feature(env, ARM_FEATURE_MPIDR)) { 8770 ARMCPRegInfo mpidr_cp_reginfo[] = { 8771 { .name = "MPIDR_EL1", .state = ARM_CP_STATE_BOTH, 8772 .opc0 = 3, .crn = 0, .crm = 0, .opc1 = 0, .opc2 = 5, 8773 .fgt = FGT_MPIDR_EL1, 8774 .access = PL1_R, .readfn = mpidr_read, .type = ARM_CP_NO_RAW }, 8775 }; 8776 #ifdef CONFIG_USER_ONLY 8777 static const ARMCPRegUserSpaceInfo mpidr_user_cp_reginfo[] = { 8778 { .name = "MPIDR_EL1", 8779 .fixed_bits = 0x0000000080000000 }, 8780 }; 8781 modify_arm_cp_regs(mpidr_cp_reginfo, mpidr_user_cp_reginfo); 8782 #endif 8783 define_arm_cp_regs(cpu, mpidr_cp_reginfo); 8784 } 8785 8786 if (arm_feature(env, ARM_FEATURE_AUXCR)) { 8787 ARMCPRegInfo auxcr_reginfo[] = { 8788 { .name = "ACTLR_EL1", .state = ARM_CP_STATE_BOTH, 8789 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 1, 8790 .access = PL1_RW, .accessfn = access_tacr, 8791 .nv2_redirect_offset = 0x118, 8792 .type = ARM_CP_CONST, .resetvalue = cpu->reset_auxcr }, 8793 { .name = "ACTLR_EL2", .state = ARM_CP_STATE_BOTH, 8794 .opc0 = 3, .opc1 = 4, .crn = 1, .crm = 0, .opc2 = 1, 8795 .access = PL2_RW, .type = ARM_CP_CONST, 8796 .resetvalue = 0 }, 8797 { .name = "ACTLR_EL3", .state = ARM_CP_STATE_AA64, 8798 .opc0 = 3, .opc1 = 6, .crn = 1, .crm = 0, .opc2 = 1, 8799 .access = PL3_RW, .type = ARM_CP_CONST, 8800 .resetvalue = 0 }, 8801 }; 8802 define_arm_cp_regs(cpu, auxcr_reginfo); 8803 if (cpu_isar_feature(aa32_ac2, cpu)) { 8804 define_arm_cp_regs(cpu, actlr2_hactlr2_reginfo); 8805 } 8806 } 8807 8808 if (arm_feature(env, ARM_FEATURE_CBAR)) { 8809 /* 8810 * CBAR is IMPDEF, but common on Arm Cortex-A implementations. 8811 * There are two flavours: 8812 * (1) older 32-bit only cores have a simple 32-bit CBAR 8813 * (2) 64-bit cores have a 64-bit CBAR visible to AArch64, plus a 8814 * 32-bit register visible to AArch32 at a different encoding 8815 * to the "flavour 1" register and with the bits rearranged to 8816 * be able to squash a 64-bit address into the 32-bit view. 8817 * We distinguish the two via the ARM_FEATURE_AARCH64 flag, but 8818 * in future if we support AArch32-only configs of some of the 8819 * AArch64 cores we might need to add a specific feature flag 8820 * to indicate cores with "flavour 2" CBAR. 8821 */ 8822 if (arm_feature(env, ARM_FEATURE_V8)) { 8823 /* 32 bit view is [31:18] 0...0 [43:32]. */ 8824 uint32_t cbar32 = (extract64(cpu->reset_cbar, 18, 14) << 18) 8825 | extract64(cpu->reset_cbar, 32, 12); 8826 ARMCPRegInfo cbar_reginfo[] = { 8827 { .name = "CBAR", 8828 .type = ARM_CP_CONST, 8829 .cp = 15, .crn = 15, .crm = 3, .opc1 = 1, .opc2 = 0, 8830 .access = PL1_R, .resetvalue = cbar32 }, 8831 { .name = "CBAR_EL1", .state = ARM_CP_STATE_AA64, 8832 .type = ARM_CP_CONST, 8833 .opc0 = 3, .opc1 = 1, .crn = 15, .crm = 3, .opc2 = 0, 8834 .access = PL1_R, .resetvalue = cpu->reset_cbar }, 8835 }; 8836 /* We don't implement a r/w 64 bit CBAR currently */ 8837 assert(arm_feature(env, ARM_FEATURE_CBAR_RO)); 8838 define_arm_cp_regs(cpu, cbar_reginfo); 8839 } else { 8840 ARMCPRegInfo cbar = { 8841 .name = "CBAR", 8842 .cp = 15, .crn = 15, .crm = 0, .opc1 = 4, .opc2 = 0, 8843 .access = PL1_R | PL3_W, .resetvalue = cpu->reset_cbar, 8844 .fieldoffset = offsetof(CPUARMState, 8845 cp15.c15_config_base_address) 8846 }; 8847 if (arm_feature(env, ARM_FEATURE_CBAR_RO)) { 8848 cbar.access = PL1_R; 8849 cbar.fieldoffset = 0; 8850 cbar.type = ARM_CP_CONST; 8851 } 8852 define_one_arm_cp_reg(cpu, &cbar); 8853 } 8854 } 8855 8856 if (arm_feature(env, ARM_FEATURE_VBAR)) { 8857 static const ARMCPRegInfo vbar_cp_reginfo[] = { 8858 { .name = "VBAR", .state = ARM_CP_STATE_BOTH, 8859 .opc0 = 3, .crn = 12, .crm = 0, .opc1 = 0, .opc2 = 0, 8860 .access = PL1_RW, .writefn = vbar_write, 8861 .accessfn = access_nv1, 8862 .fgt = FGT_VBAR_EL1, 8863 .nv2_redirect_offset = 0x250 | NV2_REDIR_NV1, 8864 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.vbar_s), 8865 offsetof(CPUARMState, cp15.vbar_ns) }, 8866 .resetvalue = 0 }, 8867 }; 8868 define_arm_cp_regs(cpu, vbar_cp_reginfo); 8869 } 8870 8871 /* Generic registers whose values depend on the implementation */ 8872 { 8873 ARMCPRegInfo sctlr = { 8874 .name = "SCTLR", .state = ARM_CP_STATE_BOTH, 8875 .opc0 = 3, .opc1 = 0, .crn = 1, .crm = 0, .opc2 = 0, 8876 .access = PL1_RW, .accessfn = access_tvm_trvm, 8877 .fgt = FGT_SCTLR_EL1, 8878 .nv2_redirect_offset = 0x110 | NV2_REDIR_NV1, 8879 .bank_fieldoffsets = { offsetof(CPUARMState, cp15.sctlr_s), 8880 offsetof(CPUARMState, cp15.sctlr_ns) }, 8881 .writefn = sctlr_write, .resetvalue = cpu->reset_sctlr, 8882 .raw_writefn = raw_write, 8883 }; 8884 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 8885 /* 8886 * Normally we would always end the TB on an SCTLR write, but Linux 8887 * arch/arm/mach-pxa/sleep.S expects two instructions following 8888 * an MMU enable to execute from cache. Imitate this behaviour. 8889 */ 8890 sctlr.type |= ARM_CP_SUPPRESS_TB_END; 8891 } 8892 define_one_arm_cp_reg(cpu, &sctlr); 8893 8894 if (arm_feature(env, ARM_FEATURE_PMSA) && 8895 arm_feature(env, ARM_FEATURE_V8)) { 8896 ARMCPRegInfo vsctlr = { 8897 .name = "VSCTLR", .state = ARM_CP_STATE_AA32, 8898 .cp = 15, .opc1 = 4, .crn = 2, .crm = 0, .opc2 = 0, 8899 .access = PL2_RW, .resetvalue = 0x0, 8900 .fieldoffset = offsetoflow32(CPUARMState, cp15.vsctlr), 8901 }; 8902 define_one_arm_cp_reg(cpu, &vsctlr); 8903 } 8904 } 8905 8906 if (cpu_isar_feature(aa64_lor, cpu)) { 8907 define_arm_cp_regs(cpu, lor_reginfo); 8908 } 8909 if (cpu_isar_feature(aa64_pan, cpu)) { 8910 define_one_arm_cp_reg(cpu, &pan_reginfo); 8911 } 8912 #ifndef CONFIG_USER_ONLY 8913 if (cpu_isar_feature(aa64_ats1e1, cpu)) { 8914 define_arm_cp_regs(cpu, ats1e1_reginfo); 8915 } 8916 if (cpu_isar_feature(aa32_ats1e1, cpu)) { 8917 define_arm_cp_regs(cpu, ats1cp_reginfo); 8918 } 8919 #endif 8920 if (cpu_isar_feature(aa64_uao, cpu)) { 8921 define_one_arm_cp_reg(cpu, &uao_reginfo); 8922 } 8923 8924 if (cpu_isar_feature(aa64_dit, cpu)) { 8925 define_one_arm_cp_reg(cpu, &dit_reginfo); 8926 } 8927 if (cpu_isar_feature(aa64_ssbs, cpu)) { 8928 define_one_arm_cp_reg(cpu, &ssbs_reginfo); 8929 } 8930 if (cpu_isar_feature(any_ras, cpu)) { 8931 define_arm_cp_regs(cpu, minimal_ras_reginfo); 8932 } 8933 8934 if (cpu_isar_feature(aa64_vh, cpu) || 8935 cpu_isar_feature(aa64_debugv8p2, cpu)) { 8936 define_one_arm_cp_reg(cpu, &contextidr_el2); 8937 } 8938 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) { 8939 define_arm_cp_regs(cpu, vhe_reginfo); 8940 } 8941 8942 if (cpu_isar_feature(aa64_sve, cpu)) { 8943 define_arm_cp_regs(cpu, zcr_reginfo); 8944 } 8945 8946 if (cpu_isar_feature(aa64_hcx, cpu)) { 8947 define_one_arm_cp_reg(cpu, &hcrx_el2_reginfo); 8948 } 8949 8950 #ifdef TARGET_AARCH64 8951 if (cpu_isar_feature(aa64_sme, cpu)) { 8952 define_arm_cp_regs(cpu, sme_reginfo); 8953 } 8954 if (cpu_isar_feature(aa64_pauth, cpu)) { 8955 define_arm_cp_regs(cpu, pauth_reginfo); 8956 } 8957 if (cpu_isar_feature(aa64_rndr, cpu)) { 8958 define_arm_cp_regs(cpu, rndr_reginfo); 8959 } 8960 /* Data Cache clean instructions up to PoP */ 8961 if (cpu_isar_feature(aa64_dcpop, cpu)) { 8962 define_one_arm_cp_reg(cpu, dcpop_reg); 8963 8964 if (cpu_isar_feature(aa64_dcpodp, cpu)) { 8965 define_one_arm_cp_reg(cpu, dcpodp_reg); 8966 } 8967 } 8968 8969 /* 8970 * If full MTE is enabled, add all of the system registers. 8971 * If only "instructions available at EL0" are enabled, 8972 * then define only a RAZ/WI version of PSTATE.TCO. 8973 */ 8974 if (cpu_isar_feature(aa64_mte, cpu)) { 8975 ARMCPRegInfo gmid_reginfo = { 8976 .name = "GMID_EL1", .state = ARM_CP_STATE_AA64, 8977 .opc0 = 3, .opc1 = 1, .crn = 0, .crm = 0, .opc2 = 4, 8978 .access = PL1_R, .accessfn = access_aa64_tid5, 8979 .type = ARM_CP_CONST, .resetvalue = cpu->gm_blocksize, 8980 }; 8981 define_one_arm_cp_reg(cpu, &gmid_reginfo); 8982 define_arm_cp_regs(cpu, mte_reginfo); 8983 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo); 8984 } else if (cpu_isar_feature(aa64_mte_insn_reg, cpu)) { 8985 define_arm_cp_regs(cpu, mte_tco_ro_reginfo); 8986 define_arm_cp_regs(cpu, mte_el0_cacheop_reginfo); 8987 } 8988 8989 if (cpu_isar_feature(aa64_scxtnum, cpu)) { 8990 define_arm_cp_regs(cpu, scxtnum_reginfo); 8991 } 8992 8993 if (cpu_isar_feature(aa64_fgt, cpu)) { 8994 define_arm_cp_regs(cpu, fgt_reginfo); 8995 } 8996 8997 if (cpu_isar_feature(aa64_rme, cpu)) { 8998 define_arm_cp_regs(cpu, rme_reginfo); 8999 if (cpu_isar_feature(aa64_mte, cpu)) { 9000 define_arm_cp_regs(cpu, rme_mte_reginfo); 9001 } 9002 } 9003 9004 if (cpu_isar_feature(aa64_nv2, cpu)) { 9005 define_arm_cp_regs(cpu, nv2_reginfo); 9006 } 9007 9008 if (cpu_isar_feature(aa64_nmi, cpu)) { 9009 define_arm_cp_regs(cpu, nmi_reginfo); 9010 } 9011 #endif 9012 9013 if (cpu_isar_feature(any_predinv, cpu)) { 9014 define_arm_cp_regs(cpu, predinv_reginfo); 9015 } 9016 9017 if (cpu_isar_feature(any_ccidx, cpu)) { 9018 define_arm_cp_regs(cpu, ccsidr2_reginfo); 9019 } 9020 9021 #ifndef CONFIG_USER_ONLY 9022 /* 9023 * Register redirections and aliases must be done last, 9024 * after the registers from the other extensions have been defined. 9025 */ 9026 if (arm_feature(env, ARM_FEATURE_EL2) && cpu_isar_feature(aa64_vh, cpu)) { 9027 define_arm_vh_e2h_redirects_aliases(cpu); 9028 } 9029 #endif 9030 } 9031 9032 /* 9033 * Private utility function for define_one_arm_cp_reg_with_opaque(): 9034 * add a single reginfo struct to the hash table. 9035 */ 9036 static void add_cpreg_to_hashtable(ARMCPU *cpu, const ARMCPRegInfo *r, 9037 void *opaque, CPState state, 9038 CPSecureState secstate, 9039 int crm, int opc1, int opc2, 9040 const char *name) 9041 { 9042 CPUARMState *env = &cpu->env; 9043 uint32_t key; 9044 ARMCPRegInfo *r2; 9045 bool is64 = r->type & ARM_CP_64BIT; 9046 bool ns = secstate & ARM_CP_SECSTATE_NS; 9047 int cp = r->cp; 9048 size_t name_len; 9049 bool make_const; 9050 9051 switch (state) { 9052 case ARM_CP_STATE_AA32: 9053 /* We assume it is a cp15 register if the .cp field is left unset. */ 9054 if (cp == 0 && r->state == ARM_CP_STATE_BOTH) { 9055 cp = 15; 9056 } 9057 key = ENCODE_CP_REG(cp, is64, ns, r->crn, crm, opc1, opc2); 9058 break; 9059 case ARM_CP_STATE_AA64: 9060 /* 9061 * To allow abbreviation of ARMCPRegInfo definitions, we treat 9062 * cp == 0 as equivalent to the value for "standard guest-visible 9063 * sysreg". STATE_BOTH definitions are also always "standard sysreg" 9064 * in their AArch64 view (the .cp value may be non-zero for the 9065 * benefit of the AArch32 view). 9066 */ 9067 if (cp == 0 || r->state == ARM_CP_STATE_BOTH) { 9068 cp = CP_REG_ARM64_SYSREG_CP; 9069 } 9070 key = ENCODE_AA64_CP_REG(cp, r->crn, crm, r->opc0, opc1, opc2); 9071 break; 9072 default: 9073 g_assert_not_reached(); 9074 } 9075 9076 /* Overriding of an existing definition must be explicitly requested. */ 9077 if (!(r->type & ARM_CP_OVERRIDE)) { 9078 const ARMCPRegInfo *oldreg = get_arm_cp_reginfo(cpu->cp_regs, key); 9079 if (oldreg) { 9080 assert(oldreg->type & ARM_CP_OVERRIDE); 9081 } 9082 } 9083 9084 /* 9085 * Eliminate registers that are not present because the EL is missing. 9086 * Doing this here makes it easier to put all registers for a given 9087 * feature into the same ARMCPRegInfo array and define them all at once. 9088 */ 9089 make_const = false; 9090 if (arm_feature(env, ARM_FEATURE_EL3)) { 9091 /* 9092 * An EL2 register without EL2 but with EL3 is (usually) RES0. 9093 * See rule RJFFP in section D1.1.3 of DDI0487H.a. 9094 */ 9095 int min_el = ctz32(r->access) / 2; 9096 if (min_el == 2 && !arm_feature(env, ARM_FEATURE_EL2)) { 9097 if (r->type & ARM_CP_EL3_NO_EL2_UNDEF) { 9098 return; 9099 } 9100 make_const = !(r->type & ARM_CP_EL3_NO_EL2_KEEP); 9101 } 9102 } else { 9103 CPAccessRights max_el = (arm_feature(env, ARM_FEATURE_EL2) 9104 ? PL2_RW : PL1_RW); 9105 if ((r->access & max_el) == 0) { 9106 return; 9107 } 9108 } 9109 9110 /* Combine cpreg and name into one allocation. */ 9111 name_len = strlen(name) + 1; 9112 r2 = g_malloc(sizeof(*r2) + name_len); 9113 *r2 = *r; 9114 r2->name = memcpy(r2 + 1, name, name_len); 9115 9116 /* 9117 * Update fields to match the instantiation, overwiting wildcards 9118 * such as CP_ANY, ARM_CP_STATE_BOTH, or ARM_CP_SECSTATE_BOTH. 9119 */ 9120 r2->cp = cp; 9121 r2->crm = crm; 9122 r2->opc1 = opc1; 9123 r2->opc2 = opc2; 9124 r2->state = state; 9125 r2->secure = secstate; 9126 if (opaque) { 9127 r2->opaque = opaque; 9128 } 9129 9130 if (make_const) { 9131 /* This should not have been a very special register to begin. */ 9132 int old_special = r2->type & ARM_CP_SPECIAL_MASK; 9133 assert(old_special == 0 || old_special == ARM_CP_NOP); 9134 /* 9135 * Set the special function to CONST, retaining the other flags. 9136 * This is important for e.g. ARM_CP_SVE so that we still 9137 * take the SVE trap if CPTR_EL3.EZ == 0. 9138 */ 9139 r2->type = (r2->type & ~ARM_CP_SPECIAL_MASK) | ARM_CP_CONST; 9140 /* 9141 * Usually, these registers become RES0, but there are a few 9142 * special cases like VPIDR_EL2 which have a constant non-zero 9143 * value with writes ignored. 9144 */ 9145 if (!(r->type & ARM_CP_EL3_NO_EL2_C_NZ)) { 9146 r2->resetvalue = 0; 9147 } 9148 /* 9149 * ARM_CP_CONST has precedence, so removing the callbacks and 9150 * offsets are not strictly necessary, but it is potentially 9151 * less confusing to debug later. 9152 */ 9153 r2->readfn = NULL; 9154 r2->writefn = NULL; 9155 r2->raw_readfn = NULL; 9156 r2->raw_writefn = NULL; 9157 r2->resetfn = NULL; 9158 r2->fieldoffset = 0; 9159 r2->bank_fieldoffsets[0] = 0; 9160 r2->bank_fieldoffsets[1] = 0; 9161 } else { 9162 bool isbanked = r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1]; 9163 9164 if (isbanked) { 9165 /* 9166 * Register is banked (using both entries in array). 9167 * Overwriting fieldoffset as the array is only used to define 9168 * banked registers but later only fieldoffset is used. 9169 */ 9170 r2->fieldoffset = r->bank_fieldoffsets[ns]; 9171 } 9172 if (state == ARM_CP_STATE_AA32) { 9173 if (isbanked) { 9174 /* 9175 * If the register is banked then we don't need to migrate or 9176 * reset the 32-bit instance in certain cases: 9177 * 9178 * 1) If the register has both 32-bit and 64-bit instances 9179 * then we can count on the 64-bit instance taking care 9180 * of the non-secure bank. 9181 * 2) If ARMv8 is enabled then we can count on a 64-bit 9182 * version taking care of the secure bank. This requires 9183 * that separate 32 and 64-bit definitions are provided. 9184 */ 9185 if ((r->state == ARM_CP_STATE_BOTH && ns) || 9186 (arm_feature(env, ARM_FEATURE_V8) && !ns)) { 9187 r2->type |= ARM_CP_ALIAS; 9188 } 9189 } else if ((secstate != r->secure) && !ns) { 9190 /* 9191 * The register is not banked so we only want to allow 9192 * migration of the non-secure instance. 9193 */ 9194 r2->type |= ARM_CP_ALIAS; 9195 } 9196 9197 if (HOST_BIG_ENDIAN && 9198 r->state == ARM_CP_STATE_BOTH && r2->fieldoffset) { 9199 r2->fieldoffset += sizeof(uint32_t); 9200 } 9201 } 9202 } 9203 9204 /* 9205 * By convention, for wildcarded registers only the first 9206 * entry is used for migration; the others are marked as 9207 * ALIAS so we don't try to transfer the register 9208 * multiple times. Special registers (ie NOP/WFI) are 9209 * never migratable and not even raw-accessible. 9210 */ 9211 if (r2->type & ARM_CP_SPECIAL_MASK) { 9212 r2->type |= ARM_CP_NO_RAW; 9213 } 9214 if (((r->crm == CP_ANY) && crm != 0) || 9215 ((r->opc1 == CP_ANY) && opc1 != 0) || 9216 ((r->opc2 == CP_ANY) && opc2 != 0)) { 9217 r2->type |= ARM_CP_ALIAS | ARM_CP_NO_GDB; 9218 } 9219 9220 /* 9221 * Check that raw accesses are either forbidden or handled. Note that 9222 * we can't assert this earlier because the setup of fieldoffset for 9223 * banked registers has to be done first. 9224 */ 9225 if (!(r2->type & ARM_CP_NO_RAW)) { 9226 assert(!raw_accessors_invalid(r2)); 9227 } 9228 9229 g_hash_table_insert(cpu->cp_regs, (gpointer)(uintptr_t)key, r2); 9230 } 9231 9232 9233 void define_one_arm_cp_reg_with_opaque(ARMCPU *cpu, 9234 const ARMCPRegInfo *r, void *opaque) 9235 { 9236 /* 9237 * Define implementations of coprocessor registers. 9238 * We store these in a hashtable because typically 9239 * there are less than 150 registers in a space which 9240 * is 16*16*16*8*8 = 262144 in size. 9241 * Wildcarding is supported for the crm, opc1 and opc2 fields. 9242 * If a register is defined twice then the second definition is 9243 * used, so this can be used to define some generic registers and 9244 * then override them with implementation specific variations. 9245 * At least one of the original and the second definition should 9246 * include ARM_CP_OVERRIDE in its type bits -- this is just a guard 9247 * against accidental use. 9248 * 9249 * The state field defines whether the register is to be 9250 * visible in the AArch32 or AArch64 execution state. If the 9251 * state is set to ARM_CP_STATE_BOTH then we synthesise a 9252 * reginfo structure for the AArch32 view, which sees the lower 9253 * 32 bits of the 64 bit register. 9254 * 9255 * Only registers visible in AArch64 may set r->opc0; opc0 cannot 9256 * be wildcarded. AArch64 registers are always considered to be 64 9257 * bits; the ARM_CP_64BIT* flag applies only to the AArch32 view of 9258 * the register, if any. 9259 */ 9260 int crm, opc1, opc2; 9261 int crmmin = (r->crm == CP_ANY) ? 0 : r->crm; 9262 int crmmax = (r->crm == CP_ANY) ? 15 : r->crm; 9263 int opc1min = (r->opc1 == CP_ANY) ? 0 : r->opc1; 9264 int opc1max = (r->opc1 == CP_ANY) ? 7 : r->opc1; 9265 int opc2min = (r->opc2 == CP_ANY) ? 0 : r->opc2; 9266 int opc2max = (r->opc2 == CP_ANY) ? 7 : r->opc2; 9267 CPState state; 9268 9269 /* 64 bit registers have only CRm and Opc1 fields */ 9270 assert(!((r->type & ARM_CP_64BIT) && (r->opc2 || r->crn))); 9271 /* op0 only exists in the AArch64 encodings */ 9272 assert((r->state != ARM_CP_STATE_AA32) || (r->opc0 == 0)); 9273 /* AArch64 regs are all 64 bit so ARM_CP_64BIT is meaningless */ 9274 assert((r->state != ARM_CP_STATE_AA64) || !(r->type & ARM_CP_64BIT)); 9275 /* 9276 * This API is only for Arm's system coprocessors (14 and 15) or 9277 * (M-profile or v7A-and-earlier only) for implementation defined 9278 * coprocessors in the range 0..7. Our decode assumes this, since 9279 * 8..13 can be used for other insns including VFP and Neon. See 9280 * valid_cp() in translate.c. Assert here that we haven't tried 9281 * to use an invalid coprocessor number. 9282 */ 9283 switch (r->state) { 9284 case ARM_CP_STATE_BOTH: 9285 /* 0 has a special meaning, but otherwise the same rules as AA32. */ 9286 if (r->cp == 0) { 9287 break; 9288 } 9289 /* fall through */ 9290 case ARM_CP_STATE_AA32: 9291 if (arm_feature(&cpu->env, ARM_FEATURE_V8) && 9292 !arm_feature(&cpu->env, ARM_FEATURE_M)) { 9293 assert(r->cp >= 14 && r->cp <= 15); 9294 } else { 9295 assert(r->cp < 8 || (r->cp >= 14 && r->cp <= 15)); 9296 } 9297 break; 9298 case ARM_CP_STATE_AA64: 9299 assert(r->cp == 0 || r->cp == CP_REG_ARM64_SYSREG_CP); 9300 break; 9301 default: 9302 g_assert_not_reached(); 9303 } 9304 /* 9305 * The AArch64 pseudocode CheckSystemAccess() specifies that op1 9306 * encodes a minimum access level for the register. We roll this 9307 * runtime check into our general permission check code, so check 9308 * here that the reginfo's specified permissions are strict enough 9309 * to encompass the generic architectural permission check. 9310 */ 9311 if (r->state != ARM_CP_STATE_AA32) { 9312 CPAccessRights mask; 9313 switch (r->opc1) { 9314 case 0: 9315 /* min_EL EL1, but some accessible to EL0 via kernel ABI */ 9316 mask = PL0U_R | PL1_RW; 9317 break; 9318 case 1: case 2: 9319 /* min_EL EL1 */ 9320 mask = PL1_RW; 9321 break; 9322 case 3: 9323 /* min_EL EL0 */ 9324 mask = PL0_RW; 9325 break; 9326 case 4: 9327 case 5: 9328 /* min_EL EL2 */ 9329 mask = PL2_RW; 9330 break; 9331 case 6: 9332 /* min_EL EL3 */ 9333 mask = PL3_RW; 9334 break; 9335 case 7: 9336 /* min_EL EL1, secure mode only (we don't check the latter) */ 9337 mask = PL1_RW; 9338 break; 9339 default: 9340 /* broken reginfo with out-of-range opc1 */ 9341 g_assert_not_reached(); 9342 } 9343 /* assert our permissions are not too lax (stricter is fine) */ 9344 assert((r->access & ~mask) == 0); 9345 } 9346 9347 /* 9348 * Check that the register definition has enough info to handle 9349 * reads and writes if they are permitted. 9350 */ 9351 if (!(r->type & (ARM_CP_SPECIAL_MASK | ARM_CP_CONST))) { 9352 if (r->access & PL3_R) { 9353 assert((r->fieldoffset || 9354 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 9355 r->readfn); 9356 } 9357 if (r->access & PL3_W) { 9358 assert((r->fieldoffset || 9359 (r->bank_fieldoffsets[0] && r->bank_fieldoffsets[1])) || 9360 r->writefn); 9361 } 9362 } 9363 9364 for (crm = crmmin; crm <= crmmax; crm++) { 9365 for (opc1 = opc1min; opc1 <= opc1max; opc1++) { 9366 for (opc2 = opc2min; opc2 <= opc2max; opc2++) { 9367 for (state = ARM_CP_STATE_AA32; 9368 state <= ARM_CP_STATE_AA64; state++) { 9369 if (r->state != state && r->state != ARM_CP_STATE_BOTH) { 9370 continue; 9371 } 9372 if ((r->type & ARM_CP_ADD_TLBI_NXS) && 9373 cpu_isar_feature(aa64_xs, cpu)) { 9374 /* 9375 * This is a TLBI insn which has an NXS variant. The 9376 * NXS variant is at the same encoding except that 9377 * crn is +1, and has the same behaviour except for 9378 * fine-grained trapping. Add the NXS insn here and 9379 * then fall through to add the normal register. 9380 * add_cpreg_to_hashtable() copies the cpreg struct 9381 * and name that it is passed, so it's OK to use 9382 * a local struct here. 9383 */ 9384 ARMCPRegInfo nxs_ri = *r; 9385 g_autofree char *name = g_strdup_printf("%sNXS", r->name); 9386 9387 assert(state == ARM_CP_STATE_AA64); 9388 assert(nxs_ri.crn < 0xf); 9389 nxs_ri.crn++; 9390 if (nxs_ri.fgt) { 9391 nxs_ri.fgt |= R_FGT_NXS_MASK; 9392 } 9393 add_cpreg_to_hashtable(cpu, &nxs_ri, opaque, state, 9394 ARM_CP_SECSTATE_NS, 9395 crm, opc1, opc2, name); 9396 } 9397 if (state == ARM_CP_STATE_AA32) { 9398 /* 9399 * Under AArch32 CP registers can be common 9400 * (same for secure and non-secure world) or banked. 9401 */ 9402 char *name; 9403 9404 switch (r->secure) { 9405 case ARM_CP_SECSTATE_S: 9406 case ARM_CP_SECSTATE_NS: 9407 add_cpreg_to_hashtable(cpu, r, opaque, state, 9408 r->secure, crm, opc1, opc2, 9409 r->name); 9410 break; 9411 case ARM_CP_SECSTATE_BOTH: 9412 name = g_strdup_printf("%s_S", r->name); 9413 add_cpreg_to_hashtable(cpu, r, opaque, state, 9414 ARM_CP_SECSTATE_S, 9415 crm, opc1, opc2, name); 9416 g_free(name); 9417 add_cpreg_to_hashtable(cpu, r, opaque, state, 9418 ARM_CP_SECSTATE_NS, 9419 crm, opc1, opc2, r->name); 9420 break; 9421 default: 9422 g_assert_not_reached(); 9423 } 9424 } else { 9425 /* 9426 * AArch64 registers get mapped to non-secure instance 9427 * of AArch32 9428 */ 9429 add_cpreg_to_hashtable(cpu, r, opaque, state, 9430 ARM_CP_SECSTATE_NS, 9431 crm, opc1, opc2, r->name); 9432 } 9433 } 9434 } 9435 } 9436 } 9437 } 9438 9439 /* Define a whole list of registers */ 9440 void define_arm_cp_regs_with_opaque_len(ARMCPU *cpu, const ARMCPRegInfo *regs, 9441 void *opaque, size_t len) 9442 { 9443 size_t i; 9444 for (i = 0; i < len; ++i) { 9445 define_one_arm_cp_reg_with_opaque(cpu, regs + i, opaque); 9446 } 9447 } 9448 9449 /* 9450 * Modify ARMCPRegInfo for access from userspace. 9451 * 9452 * This is a data driven modification directed by 9453 * ARMCPRegUserSpaceInfo. All registers become ARM_CP_CONST as 9454 * user-space cannot alter any values and dynamic values pertaining to 9455 * execution state are hidden from user space view anyway. 9456 */ 9457 void modify_arm_cp_regs_with_len(ARMCPRegInfo *regs, size_t regs_len, 9458 const ARMCPRegUserSpaceInfo *mods, 9459 size_t mods_len) 9460 { 9461 for (size_t mi = 0; mi < mods_len; ++mi) { 9462 const ARMCPRegUserSpaceInfo *m = mods + mi; 9463 GPatternSpec *pat = NULL; 9464 9465 if (m->is_glob) { 9466 pat = g_pattern_spec_new(m->name); 9467 } 9468 for (size_t ri = 0; ri < regs_len; ++ri) { 9469 ARMCPRegInfo *r = regs + ri; 9470 9471 if (pat && g_pattern_match_string(pat, r->name)) { 9472 r->type = ARM_CP_CONST; 9473 r->access = PL0U_R; 9474 r->resetvalue = 0; 9475 /* continue */ 9476 } else if (strcmp(r->name, m->name) == 0) { 9477 r->type = ARM_CP_CONST; 9478 r->access = PL0U_R; 9479 r->resetvalue &= m->exported_bits; 9480 r->resetvalue |= m->fixed_bits; 9481 break; 9482 } 9483 } 9484 if (pat) { 9485 g_pattern_spec_free(pat); 9486 } 9487 } 9488 } 9489 9490 const ARMCPRegInfo *get_arm_cp_reginfo(GHashTable *cpregs, uint32_t encoded_cp) 9491 { 9492 return g_hash_table_lookup(cpregs, (gpointer)(uintptr_t)encoded_cp); 9493 } 9494 9495 void arm_cp_write_ignore(CPUARMState *env, const ARMCPRegInfo *ri, 9496 uint64_t value) 9497 { 9498 /* Helper coprocessor write function for write-ignore registers */ 9499 } 9500 9501 uint64_t arm_cp_read_zero(CPUARMState *env, const ARMCPRegInfo *ri) 9502 { 9503 /* Helper coprocessor write function for read-as-zero registers */ 9504 return 0; 9505 } 9506 9507 void arm_cp_reset_ignore(CPUARMState *env, const ARMCPRegInfo *opaque) 9508 { 9509 /* Helper coprocessor reset function for do-nothing-on-reset registers */ 9510 } 9511 9512 static int bad_mode_switch(CPUARMState *env, int mode, CPSRWriteType write_type) 9513 { 9514 /* 9515 * Return true if it is not valid for us to switch to 9516 * this CPU mode (ie all the UNPREDICTABLE cases in 9517 * the ARM ARM CPSRWriteByInstr pseudocode). 9518 */ 9519 9520 /* Changes to or from Hyp via MSR and CPS are illegal. */ 9521 if (write_type == CPSRWriteByInstr && 9522 ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_HYP || 9523 mode == ARM_CPU_MODE_HYP)) { 9524 return 1; 9525 } 9526 9527 switch (mode) { 9528 case ARM_CPU_MODE_USR: 9529 return 0; 9530 case ARM_CPU_MODE_SYS: 9531 case ARM_CPU_MODE_SVC: 9532 case ARM_CPU_MODE_ABT: 9533 case ARM_CPU_MODE_UND: 9534 case ARM_CPU_MODE_IRQ: 9535 case ARM_CPU_MODE_FIQ: 9536 /* 9537 * Note that we don't implement the IMPDEF NSACR.RFR which in v7 9538 * allows FIQ mode to be Secure-only. (In v8 this doesn't exist.) 9539 */ 9540 /* 9541 * If HCR.TGE is set then changes from Monitor to NS PL1 via MSR 9542 * and CPS are treated as illegal mode changes. 9543 */ 9544 if (write_type == CPSRWriteByInstr && 9545 (env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON && 9546 (arm_hcr_el2_eff(env) & HCR_TGE)) { 9547 return 1; 9548 } 9549 return 0; 9550 case ARM_CPU_MODE_HYP: 9551 return !arm_is_el2_enabled(env) || arm_current_el(env) < 2; 9552 case ARM_CPU_MODE_MON: 9553 return arm_current_el(env) < 3; 9554 default: 9555 return 1; 9556 } 9557 } 9558 9559 uint32_t cpsr_read(CPUARMState *env) 9560 { 9561 int ZF; 9562 ZF = (env->ZF == 0); 9563 return env->uncached_cpsr | (env->NF & 0x80000000) | (ZF << 30) | 9564 (env->CF << 29) | ((env->VF & 0x80000000) >> 3) | (env->QF << 27) 9565 | (env->thumb << 5) | ((env->condexec_bits & 3) << 25) 9566 | ((env->condexec_bits & 0xfc) << 8) 9567 | (env->GE << 16) | (env->daif & CPSR_AIF); 9568 } 9569 9570 void cpsr_write(CPUARMState *env, uint32_t val, uint32_t mask, 9571 CPSRWriteType write_type) 9572 { 9573 uint32_t changed_daif; 9574 bool rebuild_hflags = (write_type != CPSRWriteRaw) && 9575 (mask & (CPSR_M | CPSR_E | CPSR_IL)); 9576 9577 if (mask & CPSR_NZCV) { 9578 env->ZF = (~val) & CPSR_Z; 9579 env->NF = val; 9580 env->CF = (val >> 29) & 1; 9581 env->VF = (val << 3) & 0x80000000; 9582 } 9583 if (mask & CPSR_Q) { 9584 env->QF = ((val & CPSR_Q) != 0); 9585 } 9586 if (mask & CPSR_T) { 9587 env->thumb = ((val & CPSR_T) != 0); 9588 } 9589 if (mask & CPSR_IT_0_1) { 9590 env->condexec_bits &= ~3; 9591 env->condexec_bits |= (val >> 25) & 3; 9592 } 9593 if (mask & CPSR_IT_2_7) { 9594 env->condexec_bits &= 3; 9595 env->condexec_bits |= (val >> 8) & 0xfc; 9596 } 9597 if (mask & CPSR_GE) { 9598 env->GE = (val >> 16) & 0xf; 9599 } 9600 9601 /* 9602 * In a V7 implementation that includes the security extensions but does 9603 * not include Virtualization Extensions the SCR.FW and SCR.AW bits control 9604 * whether non-secure software is allowed to change the CPSR_F and CPSR_A 9605 * bits respectively. 9606 * 9607 * In a V8 implementation, it is permitted for privileged software to 9608 * change the CPSR A/F bits regardless of the SCR.AW/FW bits. 9609 */ 9610 if (write_type != CPSRWriteRaw && !arm_feature(env, ARM_FEATURE_V8) && 9611 arm_feature(env, ARM_FEATURE_EL3) && 9612 !arm_feature(env, ARM_FEATURE_EL2) && 9613 !arm_is_secure(env)) { 9614 9615 changed_daif = (env->daif ^ val) & mask; 9616 9617 if (changed_daif & CPSR_A) { 9618 /* 9619 * Check to see if we are allowed to change the masking of async 9620 * abort exceptions from a non-secure state. 9621 */ 9622 if (!(env->cp15.scr_el3 & SCR_AW)) { 9623 qemu_log_mask(LOG_GUEST_ERROR, 9624 "Ignoring attempt to switch CPSR_A flag from " 9625 "non-secure world with SCR.AW bit clear\n"); 9626 mask &= ~CPSR_A; 9627 } 9628 } 9629 9630 if (changed_daif & CPSR_F) { 9631 /* 9632 * Check to see if we are allowed to change the masking of FIQ 9633 * exceptions from a non-secure state. 9634 */ 9635 if (!(env->cp15.scr_el3 & SCR_FW)) { 9636 qemu_log_mask(LOG_GUEST_ERROR, 9637 "Ignoring attempt to switch CPSR_F flag from " 9638 "non-secure world with SCR.FW bit clear\n"); 9639 mask &= ~CPSR_F; 9640 } 9641 9642 /* 9643 * Check whether non-maskable FIQ (NMFI) support is enabled. 9644 * If this bit is set software is not allowed to mask 9645 * FIQs, but is allowed to set CPSR_F to 0. 9646 */ 9647 if ((A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_NMFI) && 9648 (val & CPSR_F)) { 9649 qemu_log_mask(LOG_GUEST_ERROR, 9650 "Ignoring attempt to enable CPSR_F flag " 9651 "(non-maskable FIQ [NMFI] support enabled)\n"); 9652 mask &= ~CPSR_F; 9653 } 9654 } 9655 } 9656 9657 env->daif &= ~(CPSR_AIF & mask); 9658 env->daif |= val & CPSR_AIF & mask; 9659 9660 if (write_type != CPSRWriteRaw && 9661 ((env->uncached_cpsr ^ val) & mask & CPSR_M)) { 9662 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_USR) { 9663 /* 9664 * Note that we can only get here in USR mode if this is a 9665 * gdb stub write; for this case we follow the architectural 9666 * behaviour for guest writes in USR mode of ignoring an attempt 9667 * to switch mode. (Those are caught by translate.c for writes 9668 * triggered by guest instructions.) 9669 */ 9670 mask &= ~CPSR_M; 9671 } else if (bad_mode_switch(env, val & CPSR_M, write_type)) { 9672 /* 9673 * Attempt to switch to an invalid mode: this is UNPREDICTABLE in 9674 * v7, and has defined behaviour in v8: 9675 * + leave CPSR.M untouched 9676 * + allow changes to the other CPSR fields 9677 * + set PSTATE.IL 9678 * For user changes via the GDB stub, we don't set PSTATE.IL, 9679 * as this would be unnecessarily harsh for a user error. 9680 */ 9681 mask &= ~CPSR_M; 9682 if (write_type != CPSRWriteByGDBStub && 9683 arm_feature(env, ARM_FEATURE_V8)) { 9684 mask |= CPSR_IL; 9685 val |= CPSR_IL; 9686 } 9687 qemu_log_mask(LOG_GUEST_ERROR, 9688 "Illegal AArch32 mode switch attempt from %s to %s\n", 9689 aarch32_mode_name(env->uncached_cpsr), 9690 aarch32_mode_name(val)); 9691 } else { 9692 qemu_log_mask(CPU_LOG_INT, "%s %s to %s PC 0x%" PRIx32 "\n", 9693 write_type == CPSRWriteExceptionReturn ? 9694 "Exception return from AArch32" : 9695 "AArch32 mode switch from", 9696 aarch32_mode_name(env->uncached_cpsr), 9697 aarch32_mode_name(val), env->regs[15]); 9698 switch_mode(env, val & CPSR_M); 9699 } 9700 } 9701 mask &= ~CACHED_CPSR_BITS; 9702 env->uncached_cpsr = (env->uncached_cpsr & ~mask) | (val & mask); 9703 if (tcg_enabled() && rebuild_hflags) { 9704 arm_rebuild_hflags(env); 9705 } 9706 } 9707 9708 #ifdef CONFIG_USER_ONLY 9709 9710 static void switch_mode(CPUARMState *env, int mode) 9711 { 9712 ARMCPU *cpu = env_archcpu(env); 9713 9714 if (mode != ARM_CPU_MODE_USR) { 9715 cpu_abort(CPU(cpu), "Tried to switch out of user mode\n"); 9716 } 9717 } 9718 9719 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 9720 uint32_t cur_el, bool secure) 9721 { 9722 return 1; 9723 } 9724 9725 void aarch64_sync_64_to_32(CPUARMState *env) 9726 { 9727 g_assert_not_reached(); 9728 } 9729 9730 #else 9731 9732 static void switch_mode(CPUARMState *env, int mode) 9733 { 9734 int old_mode; 9735 int i; 9736 9737 old_mode = env->uncached_cpsr & CPSR_M; 9738 if (mode == old_mode) { 9739 return; 9740 } 9741 9742 if (old_mode == ARM_CPU_MODE_FIQ) { 9743 memcpy(env->fiq_regs, env->regs + 8, 5 * sizeof(uint32_t)); 9744 memcpy(env->regs + 8, env->usr_regs, 5 * sizeof(uint32_t)); 9745 } else if (mode == ARM_CPU_MODE_FIQ) { 9746 memcpy(env->usr_regs, env->regs + 8, 5 * sizeof(uint32_t)); 9747 memcpy(env->regs + 8, env->fiq_regs, 5 * sizeof(uint32_t)); 9748 } 9749 9750 i = bank_number(old_mode); 9751 env->banked_r13[i] = env->regs[13]; 9752 env->banked_spsr[i] = env->spsr; 9753 9754 i = bank_number(mode); 9755 env->regs[13] = env->banked_r13[i]; 9756 env->spsr = env->banked_spsr[i]; 9757 9758 env->banked_r14[r14_bank_number(old_mode)] = env->regs[14]; 9759 env->regs[14] = env->banked_r14[r14_bank_number(mode)]; 9760 } 9761 9762 /* 9763 * Physical Interrupt Target EL Lookup Table 9764 * 9765 * [ From ARM ARM section G1.13.4 (Table G1-15) ] 9766 * 9767 * The below multi-dimensional table is used for looking up the target 9768 * exception level given numerous condition criteria. Specifically, the 9769 * target EL is based on SCR and HCR routing controls as well as the 9770 * currently executing EL and secure state. 9771 * 9772 * Dimensions: 9773 * target_el_table[2][2][2][2][2][4] 9774 * | | | | | +--- Current EL 9775 * | | | | +------ Non-secure(0)/Secure(1) 9776 * | | | +--------- HCR mask override 9777 * | | +------------ SCR exec state control 9778 * | +--------------- SCR mask override 9779 * +------------------ 32-bit(0)/64-bit(1) EL3 9780 * 9781 * The table values are as such: 9782 * 0-3 = EL0-EL3 9783 * -1 = Cannot occur 9784 * 9785 * The ARM ARM target EL table includes entries indicating that an "exception 9786 * is not taken". The two cases where this is applicable are: 9787 * 1) An exception is taken from EL3 but the SCR does not have the exception 9788 * routed to EL3. 9789 * 2) An exception is taken from EL2 but the HCR does not have the exception 9790 * routed to EL2. 9791 * In these two cases, the below table contain a target of EL1. This value is 9792 * returned as it is expected that the consumer of the table data will check 9793 * for "target EL >= current EL" to ensure the exception is not taken. 9794 * 9795 * SCR HCR 9796 * 64 EA AMO From 9797 * BIT IRQ IMO Non-secure Secure 9798 * EL3 FIQ RW FMO EL0 EL1 EL2 EL3 EL0 EL1 EL2 EL3 9799 */ 9800 static const int8_t target_el_table[2][2][2][2][2][4] = { 9801 {{{{/* 0 0 0 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 9802 {/* 0 0 0 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},}, 9803 {{/* 0 0 1 0 */{ 1, 1, 2, -1 },{ 3, -1, -1, 3 },}, 9804 {/* 0 0 1 1 */{ 2, 2, 2, -1 },{ 3, -1, -1, 3 },},},}, 9805 {{{/* 0 1 0 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 9806 {/* 0 1 0 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},}, 9807 {{/* 0 1 1 0 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },}, 9808 {/* 0 1 1 1 */{ 3, 3, 3, -1 },{ 3, -1, -1, 3 },},},},}, 9809 {{{{/* 1 0 0 0 */{ 1, 1, 2, -1 },{ 1, 1, -1, 1 },}, 9810 {/* 1 0 0 1 */{ 2, 2, 2, -1 },{ 2, 2, -1, 1 },},}, 9811 {{/* 1 0 1 0 */{ 1, 1, 1, -1 },{ 1, 1, 1, 1 },}, 9812 {/* 1 0 1 1 */{ 2, 2, 2, -1 },{ 2, 2, 2, 1 },},},}, 9813 {{{/* 1 1 0 0 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },}, 9814 {/* 1 1 0 1 */{ 3, 3, 3, -1 },{ 3, 3, -1, 3 },},}, 9815 {{/* 1 1 1 0 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },}, 9816 {/* 1 1 1 1 */{ 3, 3, 3, -1 },{ 3, 3, 3, 3 },},},},}, 9817 }; 9818 9819 /* 9820 * Determine the target EL for physical exceptions 9821 */ 9822 uint32_t arm_phys_excp_target_el(CPUState *cs, uint32_t excp_idx, 9823 uint32_t cur_el, bool secure) 9824 { 9825 CPUARMState *env = cpu_env(cs); 9826 bool rw; 9827 bool scr; 9828 bool hcr; 9829 int target_el; 9830 /* Is the highest EL AArch64? */ 9831 bool is64 = arm_feature(env, ARM_FEATURE_AARCH64); 9832 uint64_t hcr_el2; 9833 9834 if (arm_feature(env, ARM_FEATURE_EL3)) { 9835 rw = arm_scr_rw_eff(env); 9836 } else { 9837 /* 9838 * Either EL2 is the highest EL (and so the EL2 register width 9839 * is given by is64); or there is no EL2 or EL3, in which case 9840 * the value of 'rw' does not affect the table lookup anyway. 9841 */ 9842 rw = is64; 9843 } 9844 9845 hcr_el2 = arm_hcr_el2_eff(env); 9846 switch (excp_idx) { 9847 case EXCP_IRQ: 9848 case EXCP_NMI: 9849 scr = ((env->cp15.scr_el3 & SCR_IRQ) == SCR_IRQ); 9850 hcr = hcr_el2 & HCR_IMO; 9851 break; 9852 case EXCP_FIQ: 9853 scr = ((env->cp15.scr_el3 & SCR_FIQ) == SCR_FIQ); 9854 hcr = hcr_el2 & HCR_FMO; 9855 break; 9856 default: 9857 scr = ((env->cp15.scr_el3 & SCR_EA) == SCR_EA); 9858 hcr = hcr_el2 & HCR_AMO; 9859 break; 9860 }; 9861 9862 /* 9863 * For these purposes, TGE and AMO/IMO/FMO both force the 9864 * interrupt to EL2. Fold TGE into the bit extracted above. 9865 */ 9866 hcr |= (hcr_el2 & HCR_TGE) != 0; 9867 9868 /* Perform a table-lookup for the target EL given the current state */ 9869 target_el = target_el_table[is64][scr][rw][hcr][secure][cur_el]; 9870 9871 assert(target_el > 0); 9872 9873 return target_el; 9874 } 9875 9876 void arm_log_exception(CPUState *cs) 9877 { 9878 int idx = cs->exception_index; 9879 9880 if (qemu_loglevel_mask(CPU_LOG_INT)) { 9881 const char *exc = NULL; 9882 static const char * const excnames[] = { 9883 [EXCP_UDEF] = "Undefined Instruction", 9884 [EXCP_SWI] = "SVC", 9885 [EXCP_PREFETCH_ABORT] = "Prefetch Abort", 9886 [EXCP_DATA_ABORT] = "Data Abort", 9887 [EXCP_IRQ] = "IRQ", 9888 [EXCP_FIQ] = "FIQ", 9889 [EXCP_BKPT] = "Breakpoint", 9890 [EXCP_EXCEPTION_EXIT] = "QEMU v7M exception exit", 9891 [EXCP_KERNEL_TRAP] = "QEMU intercept of kernel commpage", 9892 [EXCP_HVC] = "Hypervisor Call", 9893 [EXCP_HYP_TRAP] = "Hypervisor Trap", 9894 [EXCP_SMC] = "Secure Monitor Call", 9895 [EXCP_VIRQ] = "Virtual IRQ", 9896 [EXCP_VFIQ] = "Virtual FIQ", 9897 [EXCP_SEMIHOST] = "Semihosting call", 9898 [EXCP_NOCP] = "v7M NOCP UsageFault", 9899 [EXCP_INVSTATE] = "v7M INVSTATE UsageFault", 9900 [EXCP_STKOF] = "v8M STKOF UsageFault", 9901 [EXCP_LAZYFP] = "v7M exception during lazy FP stacking", 9902 [EXCP_LSERR] = "v8M LSERR UsageFault", 9903 [EXCP_UNALIGNED] = "v7M UNALIGNED UsageFault", 9904 [EXCP_DIVBYZERO] = "v7M DIVBYZERO UsageFault", 9905 [EXCP_VSERR] = "Virtual SERR", 9906 [EXCP_GPC] = "Granule Protection Check", 9907 [EXCP_NMI] = "NMI", 9908 [EXCP_VINMI] = "Virtual IRQ NMI", 9909 [EXCP_VFNMI] = "Virtual FIQ NMI", 9910 [EXCP_MON_TRAP] = "Monitor Trap", 9911 }; 9912 9913 if (idx >= 0 && idx < ARRAY_SIZE(excnames)) { 9914 exc = excnames[idx]; 9915 } 9916 if (!exc) { 9917 exc = "unknown"; 9918 } 9919 qemu_log_mask(CPU_LOG_INT, "Taking exception %d [%s] on CPU %d\n", 9920 idx, exc, cs->cpu_index); 9921 } 9922 } 9923 9924 /* 9925 * Function used to synchronize QEMU's AArch64 register set with AArch32 9926 * register set. This is necessary when switching between AArch32 and AArch64 9927 * execution state. 9928 */ 9929 void aarch64_sync_32_to_64(CPUARMState *env) 9930 { 9931 int i; 9932 uint32_t mode = env->uncached_cpsr & CPSR_M; 9933 9934 /* We can blanket copy R[0:7] to X[0:7] */ 9935 for (i = 0; i < 8; i++) { 9936 env->xregs[i] = env->regs[i]; 9937 } 9938 9939 /* 9940 * Unless we are in FIQ mode, x8-x12 come from the user registers r8-r12. 9941 * Otherwise, they come from the banked user regs. 9942 */ 9943 if (mode == ARM_CPU_MODE_FIQ) { 9944 for (i = 8; i < 13; i++) { 9945 env->xregs[i] = env->usr_regs[i - 8]; 9946 } 9947 } else { 9948 for (i = 8; i < 13; i++) { 9949 env->xregs[i] = env->regs[i]; 9950 } 9951 } 9952 9953 /* 9954 * Registers x13-x23 are the various mode SP and FP registers. Registers 9955 * r13 and r14 are only copied if we are in that mode, otherwise we copy 9956 * from the mode banked register. 9957 */ 9958 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 9959 env->xregs[13] = env->regs[13]; 9960 env->xregs[14] = env->regs[14]; 9961 } else { 9962 env->xregs[13] = env->banked_r13[bank_number(ARM_CPU_MODE_USR)]; 9963 /* HYP is an exception in that it is copied from r14 */ 9964 if (mode == ARM_CPU_MODE_HYP) { 9965 env->xregs[14] = env->regs[14]; 9966 } else { 9967 env->xregs[14] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)]; 9968 } 9969 } 9970 9971 if (mode == ARM_CPU_MODE_HYP) { 9972 env->xregs[15] = env->regs[13]; 9973 } else { 9974 env->xregs[15] = env->banked_r13[bank_number(ARM_CPU_MODE_HYP)]; 9975 } 9976 9977 if (mode == ARM_CPU_MODE_IRQ) { 9978 env->xregs[16] = env->regs[14]; 9979 env->xregs[17] = env->regs[13]; 9980 } else { 9981 env->xregs[16] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)]; 9982 env->xregs[17] = env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)]; 9983 } 9984 9985 if (mode == ARM_CPU_MODE_SVC) { 9986 env->xregs[18] = env->regs[14]; 9987 env->xregs[19] = env->regs[13]; 9988 } else { 9989 env->xregs[18] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)]; 9990 env->xregs[19] = env->banked_r13[bank_number(ARM_CPU_MODE_SVC)]; 9991 } 9992 9993 if (mode == ARM_CPU_MODE_ABT) { 9994 env->xregs[20] = env->regs[14]; 9995 env->xregs[21] = env->regs[13]; 9996 } else { 9997 env->xregs[20] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)]; 9998 env->xregs[21] = env->banked_r13[bank_number(ARM_CPU_MODE_ABT)]; 9999 } 10000 10001 if (mode == ARM_CPU_MODE_UND) { 10002 env->xregs[22] = env->regs[14]; 10003 env->xregs[23] = env->regs[13]; 10004 } else { 10005 env->xregs[22] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)]; 10006 env->xregs[23] = env->banked_r13[bank_number(ARM_CPU_MODE_UND)]; 10007 } 10008 10009 /* 10010 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 10011 * mode, then we can copy from r8-r14. Otherwise, we copy from the 10012 * FIQ bank for r8-r14. 10013 */ 10014 if (mode == ARM_CPU_MODE_FIQ) { 10015 for (i = 24; i < 31; i++) { 10016 env->xregs[i] = env->regs[i - 16]; /* X[24:30] <- R[8:14] */ 10017 } 10018 } else { 10019 for (i = 24; i < 29; i++) { 10020 env->xregs[i] = env->fiq_regs[i - 24]; 10021 } 10022 env->xregs[29] = env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)]; 10023 env->xregs[30] = env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)]; 10024 } 10025 10026 env->pc = env->regs[15]; 10027 } 10028 10029 /* 10030 * Function used to synchronize QEMU's AArch32 register set with AArch64 10031 * register set. This is necessary when switching between AArch32 and AArch64 10032 * execution state. 10033 */ 10034 void aarch64_sync_64_to_32(CPUARMState *env) 10035 { 10036 int i; 10037 uint32_t mode = env->uncached_cpsr & CPSR_M; 10038 10039 /* We can blanket copy X[0:7] to R[0:7] */ 10040 for (i = 0; i < 8; i++) { 10041 env->regs[i] = env->xregs[i]; 10042 } 10043 10044 /* 10045 * Unless we are in FIQ mode, r8-r12 come from the user registers x8-x12. 10046 * Otherwise, we copy x8-x12 into the banked user regs. 10047 */ 10048 if (mode == ARM_CPU_MODE_FIQ) { 10049 for (i = 8; i < 13; i++) { 10050 env->usr_regs[i - 8] = env->xregs[i]; 10051 } 10052 } else { 10053 for (i = 8; i < 13; i++) { 10054 env->regs[i] = env->xregs[i]; 10055 } 10056 } 10057 10058 /* 10059 * Registers r13 & r14 depend on the current mode. 10060 * If we are in a given mode, we copy the corresponding x registers to r13 10061 * and r14. Otherwise, we copy the x register to the banked r13 and r14 10062 * for the mode. 10063 */ 10064 if (mode == ARM_CPU_MODE_USR || mode == ARM_CPU_MODE_SYS) { 10065 env->regs[13] = env->xregs[13]; 10066 env->regs[14] = env->xregs[14]; 10067 } else { 10068 env->banked_r13[bank_number(ARM_CPU_MODE_USR)] = env->xregs[13]; 10069 10070 /* 10071 * HYP is an exception in that it does not have its own banked r14 but 10072 * shares the USR r14 10073 */ 10074 if (mode == ARM_CPU_MODE_HYP) { 10075 env->regs[14] = env->xregs[14]; 10076 } else { 10077 env->banked_r14[r14_bank_number(ARM_CPU_MODE_USR)] = env->xregs[14]; 10078 } 10079 } 10080 10081 if (mode == ARM_CPU_MODE_HYP) { 10082 env->regs[13] = env->xregs[15]; 10083 } else { 10084 env->banked_r13[bank_number(ARM_CPU_MODE_HYP)] = env->xregs[15]; 10085 } 10086 10087 if (mode == ARM_CPU_MODE_IRQ) { 10088 env->regs[14] = env->xregs[16]; 10089 env->regs[13] = env->xregs[17]; 10090 } else { 10091 env->banked_r14[r14_bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[16]; 10092 env->banked_r13[bank_number(ARM_CPU_MODE_IRQ)] = env->xregs[17]; 10093 } 10094 10095 if (mode == ARM_CPU_MODE_SVC) { 10096 env->regs[14] = env->xregs[18]; 10097 env->regs[13] = env->xregs[19]; 10098 } else { 10099 env->banked_r14[r14_bank_number(ARM_CPU_MODE_SVC)] = env->xregs[18]; 10100 env->banked_r13[bank_number(ARM_CPU_MODE_SVC)] = env->xregs[19]; 10101 } 10102 10103 if (mode == ARM_CPU_MODE_ABT) { 10104 env->regs[14] = env->xregs[20]; 10105 env->regs[13] = env->xregs[21]; 10106 } else { 10107 env->banked_r14[r14_bank_number(ARM_CPU_MODE_ABT)] = env->xregs[20]; 10108 env->banked_r13[bank_number(ARM_CPU_MODE_ABT)] = env->xregs[21]; 10109 } 10110 10111 if (mode == ARM_CPU_MODE_UND) { 10112 env->regs[14] = env->xregs[22]; 10113 env->regs[13] = env->xregs[23]; 10114 } else { 10115 env->banked_r14[r14_bank_number(ARM_CPU_MODE_UND)] = env->xregs[22]; 10116 env->banked_r13[bank_number(ARM_CPU_MODE_UND)] = env->xregs[23]; 10117 } 10118 10119 /* 10120 * Registers x24-x30 are mapped to r8-r14 in FIQ mode. If we are in FIQ 10121 * mode, then we can copy to r8-r14. Otherwise, we copy to the 10122 * FIQ bank for r8-r14. 10123 */ 10124 if (mode == ARM_CPU_MODE_FIQ) { 10125 for (i = 24; i < 31; i++) { 10126 env->regs[i - 16] = env->xregs[i]; /* X[24:30] -> R[8:14] */ 10127 } 10128 } else { 10129 for (i = 24; i < 29; i++) { 10130 env->fiq_regs[i - 24] = env->xregs[i]; 10131 } 10132 env->banked_r13[bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[29]; 10133 env->banked_r14[r14_bank_number(ARM_CPU_MODE_FIQ)] = env->xregs[30]; 10134 } 10135 10136 env->regs[15] = env->pc; 10137 } 10138 10139 static void take_aarch32_exception(CPUARMState *env, int new_mode, 10140 uint32_t mask, uint32_t offset, 10141 uint32_t newpc) 10142 { 10143 int new_el; 10144 10145 /* Change the CPU state so as to actually take the exception. */ 10146 switch_mode(env, new_mode); 10147 10148 /* 10149 * For exceptions taken to AArch32 we must clear the SS bit in both 10150 * PSTATE and in the old-state value we save to SPSR_<mode>, so zero it now. 10151 */ 10152 env->pstate &= ~PSTATE_SS; 10153 env->spsr = cpsr_read(env); 10154 /* Clear IT bits. */ 10155 env->condexec_bits = 0; 10156 /* Switch to the new mode, and to the correct instruction set. */ 10157 env->uncached_cpsr = (env->uncached_cpsr & ~CPSR_M) | new_mode; 10158 10159 /* This must be after mode switching. */ 10160 new_el = arm_current_el(env); 10161 10162 /* Set new mode endianness */ 10163 env->uncached_cpsr &= ~CPSR_E; 10164 if (env->cp15.sctlr_el[new_el] & SCTLR_EE) { 10165 env->uncached_cpsr |= CPSR_E; 10166 } 10167 /* J and IL must always be cleared for exception entry */ 10168 env->uncached_cpsr &= ~(CPSR_IL | CPSR_J); 10169 env->daif |= mask; 10170 10171 if (cpu_isar_feature(aa32_ssbs, env_archcpu(env))) { 10172 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_32) { 10173 env->uncached_cpsr |= CPSR_SSBS; 10174 } else { 10175 env->uncached_cpsr &= ~CPSR_SSBS; 10176 } 10177 } 10178 10179 if (new_mode == ARM_CPU_MODE_HYP) { 10180 env->thumb = (env->cp15.sctlr_el[2] & SCTLR_TE) != 0; 10181 env->elr_el[2] = env->regs[15]; 10182 } else { 10183 /* CPSR.PAN is normally preserved preserved unless... */ 10184 if (cpu_isar_feature(aa32_pan, env_archcpu(env))) { 10185 switch (new_el) { 10186 case 3: 10187 if (!arm_is_secure_below_el3(env)) { 10188 /* ... the target is EL3, from non-secure state. */ 10189 env->uncached_cpsr &= ~CPSR_PAN; 10190 break; 10191 } 10192 /* ... the target is EL3, from secure state ... */ 10193 /* fall through */ 10194 case 1: 10195 /* ... the target is EL1 and SCTLR.SPAN is 0. */ 10196 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPAN)) { 10197 env->uncached_cpsr |= CPSR_PAN; 10198 } 10199 break; 10200 } 10201 } 10202 /* 10203 * this is a lie, as there was no c1_sys on V4T/V5, but who cares 10204 * and we should just guard the thumb mode on V4 10205 */ 10206 if (arm_feature(env, ARM_FEATURE_V4T)) { 10207 env->thumb = 10208 (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_TE) != 0; 10209 } 10210 env->regs[14] = env->regs[15] + offset; 10211 } 10212 env->regs[15] = newpc; 10213 10214 if (tcg_enabled()) { 10215 arm_rebuild_hflags(env); 10216 } 10217 } 10218 10219 static void arm_cpu_do_interrupt_aarch32_hyp(CPUState *cs) 10220 { 10221 /* 10222 * Handle exception entry to Hyp mode; this is sufficiently 10223 * different to entry to other AArch32 modes that we handle it 10224 * separately here. 10225 * 10226 * The vector table entry used is always the 0x14 Hyp mode entry point, 10227 * unless this is an UNDEF/SVC/HVC/abort taken from Hyp to Hyp. 10228 * The offset applied to the preferred return address is always zero 10229 * (see DDI0487C.a section G1.12.3). 10230 * PSTATE A/I/F masks are set based only on the SCR.EA/IRQ/FIQ values. 10231 */ 10232 uint32_t addr, mask; 10233 ARMCPU *cpu = ARM_CPU(cs); 10234 CPUARMState *env = &cpu->env; 10235 10236 switch (cs->exception_index) { 10237 case EXCP_UDEF: 10238 addr = 0x04; 10239 break; 10240 case EXCP_SWI: 10241 addr = 0x08; 10242 break; 10243 case EXCP_BKPT: 10244 /* Fall through to prefetch abort. */ 10245 case EXCP_PREFETCH_ABORT: 10246 env->cp15.ifar_s = env->exception.vaddress; 10247 qemu_log_mask(CPU_LOG_INT, "...with HIFAR 0x%x\n", 10248 (uint32_t)env->exception.vaddress); 10249 addr = 0x0c; 10250 break; 10251 case EXCP_DATA_ABORT: 10252 env->cp15.dfar_s = env->exception.vaddress; 10253 qemu_log_mask(CPU_LOG_INT, "...with HDFAR 0x%x\n", 10254 (uint32_t)env->exception.vaddress); 10255 addr = 0x10; 10256 break; 10257 case EXCP_IRQ: 10258 addr = 0x18; 10259 break; 10260 case EXCP_FIQ: 10261 addr = 0x1c; 10262 break; 10263 case EXCP_HVC: 10264 addr = 0x08; 10265 break; 10266 case EXCP_HYP_TRAP: 10267 addr = 0x14; 10268 break; 10269 default: 10270 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10271 } 10272 10273 if (cs->exception_index != EXCP_IRQ && cs->exception_index != EXCP_FIQ) { 10274 if (!arm_feature(env, ARM_FEATURE_V8)) { 10275 /* 10276 * QEMU syndrome values are v8-style. v7 has the IL bit 10277 * UNK/SBZP for "field not valid" cases, where v8 uses RES1. 10278 * If this is a v7 CPU, squash the IL bit in those cases. 10279 */ 10280 if (cs->exception_index == EXCP_PREFETCH_ABORT || 10281 (cs->exception_index == EXCP_DATA_ABORT && 10282 !(env->exception.syndrome & ARM_EL_ISV)) || 10283 syn_get_ec(env->exception.syndrome) == EC_UNCATEGORIZED) { 10284 env->exception.syndrome &= ~ARM_EL_IL; 10285 } 10286 } 10287 env->cp15.esr_el[2] = env->exception.syndrome; 10288 } 10289 10290 if (arm_current_el(env) != 2 && addr < 0x14) { 10291 addr = 0x14; 10292 } 10293 10294 mask = 0; 10295 if (!(env->cp15.scr_el3 & SCR_EA)) { 10296 mask |= CPSR_A; 10297 } 10298 if (!(env->cp15.scr_el3 & SCR_IRQ)) { 10299 mask |= CPSR_I; 10300 } 10301 if (!(env->cp15.scr_el3 & SCR_FIQ)) { 10302 mask |= CPSR_F; 10303 } 10304 10305 addr += env->cp15.hvbar; 10306 10307 take_aarch32_exception(env, ARM_CPU_MODE_HYP, mask, 0, addr); 10308 } 10309 10310 static void arm_cpu_do_interrupt_aarch32(CPUState *cs) 10311 { 10312 ARMCPU *cpu = ARM_CPU(cs); 10313 CPUARMState *env = &cpu->env; 10314 uint32_t addr; 10315 uint32_t mask; 10316 int new_mode; 10317 uint32_t offset; 10318 uint32_t moe; 10319 10320 /* If this is a debug exception we must update the DBGDSCR.MOE bits */ 10321 switch (syn_get_ec(env->exception.syndrome)) { 10322 case EC_BREAKPOINT: 10323 case EC_BREAKPOINT_SAME_EL: 10324 moe = 1; 10325 break; 10326 case EC_WATCHPOINT: 10327 case EC_WATCHPOINT_SAME_EL: 10328 moe = 10; 10329 break; 10330 case EC_AA32_BKPT: 10331 moe = 3; 10332 break; 10333 case EC_VECTORCATCH: 10334 moe = 5; 10335 break; 10336 default: 10337 moe = 0; 10338 break; 10339 } 10340 10341 if (moe) { 10342 env->cp15.mdscr_el1 = deposit64(env->cp15.mdscr_el1, 2, 4, moe); 10343 } 10344 10345 if (env->exception.target_el == 2) { 10346 /* Debug exceptions are reported differently on AArch32 */ 10347 switch (syn_get_ec(env->exception.syndrome)) { 10348 case EC_BREAKPOINT: 10349 case EC_BREAKPOINT_SAME_EL: 10350 case EC_AA32_BKPT: 10351 case EC_VECTORCATCH: 10352 env->exception.syndrome = syn_insn_abort(arm_current_el(env) == 2, 10353 0, 0, 0x22); 10354 break; 10355 case EC_WATCHPOINT: 10356 env->exception.syndrome = syn_set_ec(env->exception.syndrome, 10357 EC_DATAABORT); 10358 break; 10359 case EC_WATCHPOINT_SAME_EL: 10360 env->exception.syndrome = syn_set_ec(env->exception.syndrome, 10361 EC_DATAABORT_SAME_EL); 10362 break; 10363 } 10364 arm_cpu_do_interrupt_aarch32_hyp(cs); 10365 return; 10366 } 10367 10368 switch (cs->exception_index) { 10369 case EXCP_UDEF: 10370 new_mode = ARM_CPU_MODE_UND; 10371 addr = 0x04; 10372 mask = CPSR_I; 10373 if (env->thumb) { 10374 offset = 2; 10375 } else { 10376 offset = 4; 10377 } 10378 break; 10379 case EXCP_SWI: 10380 new_mode = ARM_CPU_MODE_SVC; 10381 addr = 0x08; 10382 mask = CPSR_I; 10383 /* The PC already points to the next instruction. */ 10384 offset = 0; 10385 break; 10386 case EXCP_BKPT: 10387 /* Fall through to prefetch abort. */ 10388 case EXCP_PREFETCH_ABORT: 10389 A32_BANKED_CURRENT_REG_SET(env, ifsr, env->exception.fsr); 10390 A32_BANKED_CURRENT_REG_SET(env, ifar, env->exception.vaddress); 10391 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x IFAR 0x%x\n", 10392 env->exception.fsr, (uint32_t)env->exception.vaddress); 10393 new_mode = ARM_CPU_MODE_ABT; 10394 addr = 0x0c; 10395 mask = CPSR_A | CPSR_I; 10396 offset = 4; 10397 break; 10398 case EXCP_DATA_ABORT: 10399 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 10400 A32_BANKED_CURRENT_REG_SET(env, dfar, env->exception.vaddress); 10401 qemu_log_mask(CPU_LOG_INT, "...with DFSR 0x%x DFAR 0x%x\n", 10402 env->exception.fsr, 10403 (uint32_t)env->exception.vaddress); 10404 new_mode = ARM_CPU_MODE_ABT; 10405 addr = 0x10; 10406 mask = CPSR_A | CPSR_I; 10407 offset = 8; 10408 break; 10409 case EXCP_IRQ: 10410 new_mode = ARM_CPU_MODE_IRQ; 10411 addr = 0x18; 10412 /* Disable IRQ and imprecise data aborts. */ 10413 mask = CPSR_A | CPSR_I; 10414 offset = 4; 10415 if (env->cp15.scr_el3 & SCR_IRQ) { 10416 /* IRQ routed to monitor mode */ 10417 new_mode = ARM_CPU_MODE_MON; 10418 mask |= CPSR_F; 10419 } 10420 break; 10421 case EXCP_FIQ: 10422 new_mode = ARM_CPU_MODE_FIQ; 10423 addr = 0x1c; 10424 /* Disable FIQ, IRQ and imprecise data aborts. */ 10425 mask = CPSR_A | CPSR_I | CPSR_F; 10426 if (env->cp15.scr_el3 & SCR_FIQ) { 10427 /* FIQ routed to monitor mode */ 10428 new_mode = ARM_CPU_MODE_MON; 10429 } 10430 offset = 4; 10431 break; 10432 case EXCP_VIRQ: 10433 new_mode = ARM_CPU_MODE_IRQ; 10434 addr = 0x18; 10435 /* Disable IRQ and imprecise data aborts. */ 10436 mask = CPSR_A | CPSR_I; 10437 offset = 4; 10438 break; 10439 case EXCP_VFIQ: 10440 new_mode = ARM_CPU_MODE_FIQ; 10441 addr = 0x1c; 10442 /* Disable FIQ, IRQ and imprecise data aborts. */ 10443 mask = CPSR_A | CPSR_I | CPSR_F; 10444 offset = 4; 10445 break; 10446 case EXCP_VSERR: 10447 { 10448 /* 10449 * Note that this is reported as a data abort, but the DFAR 10450 * has an UNKNOWN value. Construct the SError syndrome from 10451 * AET and ExT fields. 10452 */ 10453 ARMMMUFaultInfo fi = { .type = ARMFault_AsyncExternal, }; 10454 10455 if (extended_addresses_enabled(env)) { 10456 env->exception.fsr = arm_fi_to_lfsc(&fi); 10457 } else { 10458 env->exception.fsr = arm_fi_to_sfsc(&fi); 10459 } 10460 env->exception.fsr |= env->cp15.vsesr_el2 & 0xd000; 10461 A32_BANKED_CURRENT_REG_SET(env, dfsr, env->exception.fsr); 10462 qemu_log_mask(CPU_LOG_INT, "...with IFSR 0x%x\n", 10463 env->exception.fsr); 10464 10465 new_mode = ARM_CPU_MODE_ABT; 10466 addr = 0x10; 10467 mask = CPSR_A | CPSR_I; 10468 offset = 8; 10469 } 10470 break; 10471 case EXCP_SMC: 10472 new_mode = ARM_CPU_MODE_MON; 10473 addr = 0x08; 10474 mask = CPSR_A | CPSR_I | CPSR_F; 10475 offset = 0; 10476 break; 10477 case EXCP_MON_TRAP: 10478 new_mode = ARM_CPU_MODE_MON; 10479 addr = 0x04; 10480 mask = CPSR_A | CPSR_I | CPSR_F; 10481 if (env->thumb) { 10482 offset = 2; 10483 } else { 10484 offset = 4; 10485 } 10486 break; 10487 default: 10488 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10489 return; /* Never happens. Keep compiler happy. */ 10490 } 10491 10492 if (new_mode == ARM_CPU_MODE_MON) { 10493 addr += env->cp15.mvbar; 10494 } else if (A32_BANKED_CURRENT_REG_GET(env, sctlr) & SCTLR_V) { 10495 /* High vectors. When enabled, base address cannot be remapped. */ 10496 addr += 0xffff0000; 10497 } else { 10498 /* 10499 * ARM v7 architectures provide a vector base address register to remap 10500 * the interrupt vector table. 10501 * This register is only followed in non-monitor mode, and is banked. 10502 * Note: only bits 31:5 are valid. 10503 */ 10504 addr += A32_BANKED_CURRENT_REG_GET(env, vbar); 10505 } 10506 10507 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 10508 env->cp15.scr_el3 &= ~SCR_NS; 10509 } 10510 10511 take_aarch32_exception(env, new_mode, mask, offset, addr); 10512 } 10513 10514 static int aarch64_regnum(CPUARMState *env, int aarch32_reg) 10515 { 10516 /* 10517 * Return the register number of the AArch64 view of the AArch32 10518 * register @aarch32_reg. The CPUARMState CPSR is assumed to still 10519 * be that of the AArch32 mode the exception came from. 10520 */ 10521 int mode = env->uncached_cpsr & CPSR_M; 10522 10523 switch (aarch32_reg) { 10524 case 0 ... 7: 10525 return aarch32_reg; 10526 case 8 ... 12: 10527 return mode == ARM_CPU_MODE_FIQ ? aarch32_reg + 16 : aarch32_reg; 10528 case 13: 10529 switch (mode) { 10530 case ARM_CPU_MODE_USR: 10531 case ARM_CPU_MODE_SYS: 10532 return 13; 10533 case ARM_CPU_MODE_HYP: 10534 return 15; 10535 case ARM_CPU_MODE_IRQ: 10536 return 17; 10537 case ARM_CPU_MODE_SVC: 10538 return 19; 10539 case ARM_CPU_MODE_ABT: 10540 return 21; 10541 case ARM_CPU_MODE_UND: 10542 return 23; 10543 case ARM_CPU_MODE_FIQ: 10544 return 29; 10545 default: 10546 g_assert_not_reached(); 10547 } 10548 case 14: 10549 switch (mode) { 10550 case ARM_CPU_MODE_USR: 10551 case ARM_CPU_MODE_SYS: 10552 case ARM_CPU_MODE_HYP: 10553 return 14; 10554 case ARM_CPU_MODE_IRQ: 10555 return 16; 10556 case ARM_CPU_MODE_SVC: 10557 return 18; 10558 case ARM_CPU_MODE_ABT: 10559 return 20; 10560 case ARM_CPU_MODE_UND: 10561 return 22; 10562 case ARM_CPU_MODE_FIQ: 10563 return 30; 10564 default: 10565 g_assert_not_reached(); 10566 } 10567 case 15: 10568 return 31; 10569 default: 10570 g_assert_not_reached(); 10571 } 10572 } 10573 10574 static uint32_t cpsr_read_for_spsr_elx(CPUARMState *env) 10575 { 10576 uint32_t ret = cpsr_read(env); 10577 10578 /* Move DIT to the correct location for SPSR_ELx */ 10579 if (ret & CPSR_DIT) { 10580 ret &= ~CPSR_DIT; 10581 ret |= PSTATE_DIT; 10582 } 10583 /* Merge PSTATE.SS into SPSR_ELx */ 10584 ret |= env->pstate & PSTATE_SS; 10585 10586 return ret; 10587 } 10588 10589 static bool syndrome_is_sync_extabt(uint32_t syndrome) 10590 { 10591 /* Return true if this syndrome value is a synchronous external abort */ 10592 switch (syn_get_ec(syndrome)) { 10593 case EC_INSNABORT: 10594 case EC_INSNABORT_SAME_EL: 10595 case EC_DATAABORT: 10596 case EC_DATAABORT_SAME_EL: 10597 /* Look at fault status code for all the synchronous ext abort cases */ 10598 switch (syndrome & 0x3f) { 10599 case 0x10: 10600 case 0x13: 10601 case 0x14: 10602 case 0x15: 10603 case 0x16: 10604 case 0x17: 10605 return true; 10606 default: 10607 return false; 10608 } 10609 default: 10610 return false; 10611 } 10612 } 10613 10614 /* Handle exception entry to a target EL which is using AArch64 */ 10615 static void arm_cpu_do_interrupt_aarch64(CPUState *cs) 10616 { 10617 ARMCPU *cpu = ARM_CPU(cs); 10618 CPUARMState *env = &cpu->env; 10619 unsigned int new_el = env->exception.target_el; 10620 target_ulong addr = env->cp15.vbar_el[new_el]; 10621 unsigned int new_mode = aarch64_pstate_mode(new_el, true); 10622 unsigned int old_mode; 10623 unsigned int cur_el = arm_current_el(env); 10624 int rt; 10625 10626 if (tcg_enabled()) { 10627 /* 10628 * Note that new_el can never be 0. If cur_el is 0, then 10629 * el0_a64 is is_a64(), else el0_a64 is ignored. 10630 */ 10631 aarch64_sve_change_el(env, cur_el, new_el, is_a64(env)); 10632 } 10633 10634 if (cur_el < new_el) { 10635 /* 10636 * Entry vector offset depends on whether the implemented EL 10637 * immediately lower than the target level is using AArch32 or AArch64 10638 */ 10639 bool is_aa64; 10640 uint64_t hcr; 10641 10642 switch (new_el) { 10643 case 3: 10644 is_aa64 = arm_scr_rw_eff(env); 10645 break; 10646 case 2: 10647 hcr = arm_hcr_el2_eff(env); 10648 if ((hcr & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 10649 is_aa64 = (hcr & HCR_RW) != 0; 10650 break; 10651 } 10652 /* fall through */ 10653 case 1: 10654 is_aa64 = is_a64(env); 10655 break; 10656 default: 10657 g_assert_not_reached(); 10658 } 10659 10660 if (is_aa64) { 10661 addr += 0x400; 10662 } else { 10663 addr += 0x600; 10664 } 10665 } else if (pstate_read(env) & PSTATE_SP) { 10666 addr += 0x200; 10667 } 10668 10669 switch (cs->exception_index) { 10670 case EXCP_GPC: 10671 qemu_log_mask(CPU_LOG_INT, "...with MFAR 0x%" PRIx64 "\n", 10672 env->cp15.mfar_el3); 10673 /* fall through */ 10674 case EXCP_PREFETCH_ABORT: 10675 case EXCP_DATA_ABORT: 10676 /* 10677 * FEAT_DoubleFault allows synchronous external aborts taken to EL3 10678 * to be taken to the SError vector entrypoint. 10679 */ 10680 if (new_el == 3 && (env->cp15.scr_el3 & SCR_EASE) && 10681 syndrome_is_sync_extabt(env->exception.syndrome)) { 10682 addr += 0x180; 10683 } 10684 env->cp15.far_el[new_el] = env->exception.vaddress; 10685 qemu_log_mask(CPU_LOG_INT, "...with FAR 0x%" PRIx64 "\n", 10686 env->cp15.far_el[new_el]); 10687 /* fall through */ 10688 case EXCP_BKPT: 10689 case EXCP_UDEF: 10690 case EXCP_SWI: 10691 case EXCP_HVC: 10692 case EXCP_HYP_TRAP: 10693 case EXCP_SMC: 10694 switch (syn_get_ec(env->exception.syndrome)) { 10695 case EC_ADVSIMDFPACCESSTRAP: 10696 /* 10697 * QEMU internal FP/SIMD syndromes from AArch32 include the 10698 * TA and coproc fields which are only exposed if the exception 10699 * is taken to AArch32 Hyp mode. Mask them out to get a valid 10700 * AArch64 format syndrome. 10701 */ 10702 env->exception.syndrome &= ~MAKE_64BIT_MASK(0, 20); 10703 break; 10704 case EC_CP14RTTRAP: 10705 case EC_CP15RTTRAP: 10706 case EC_CP14DTTRAP: 10707 /* 10708 * For a trap on AArch32 MRC/MCR/LDC/STC the Rt field is currently 10709 * the raw register field from the insn; when taking this to 10710 * AArch64 we must convert it to the AArch64 view of the register 10711 * number. Notice that we read a 4-bit AArch32 register number and 10712 * write back a 5-bit AArch64 one. 10713 */ 10714 rt = extract32(env->exception.syndrome, 5, 4); 10715 rt = aarch64_regnum(env, rt); 10716 env->exception.syndrome = deposit32(env->exception.syndrome, 10717 5, 5, rt); 10718 break; 10719 case EC_CP15RRTTRAP: 10720 case EC_CP14RRTTRAP: 10721 /* Similarly for MRRC/MCRR traps for Rt and Rt2 fields */ 10722 rt = extract32(env->exception.syndrome, 5, 4); 10723 rt = aarch64_regnum(env, rt); 10724 env->exception.syndrome = deposit32(env->exception.syndrome, 10725 5, 5, rt); 10726 rt = extract32(env->exception.syndrome, 10, 4); 10727 rt = aarch64_regnum(env, rt); 10728 env->exception.syndrome = deposit32(env->exception.syndrome, 10729 10, 5, rt); 10730 break; 10731 } 10732 env->cp15.esr_el[new_el] = env->exception.syndrome; 10733 break; 10734 case EXCP_IRQ: 10735 case EXCP_VIRQ: 10736 case EXCP_NMI: 10737 case EXCP_VINMI: 10738 addr += 0x80; 10739 break; 10740 case EXCP_FIQ: 10741 case EXCP_VFIQ: 10742 case EXCP_VFNMI: 10743 addr += 0x100; 10744 break; 10745 case EXCP_VSERR: 10746 addr += 0x180; 10747 /* Construct the SError syndrome from IDS and ISS fields. */ 10748 env->exception.syndrome = syn_serror(env->cp15.vsesr_el2 & 0x1ffffff); 10749 env->cp15.esr_el[new_el] = env->exception.syndrome; 10750 break; 10751 default: 10752 cpu_abort(cs, "Unhandled exception 0x%x\n", cs->exception_index); 10753 } 10754 10755 if (is_a64(env)) { 10756 old_mode = pstate_read(env); 10757 aarch64_save_sp(env, arm_current_el(env)); 10758 env->elr_el[new_el] = env->pc; 10759 10760 if (cur_el == 1 && new_el == 1) { 10761 uint64_t hcr = arm_hcr_el2_eff(env); 10762 if ((hcr & (HCR_NV | HCR_NV1 | HCR_NV2)) == HCR_NV || 10763 (hcr & (HCR_NV | HCR_NV2)) == (HCR_NV | HCR_NV2)) { 10764 /* 10765 * FEAT_NV, FEAT_NV2 may need to report EL2 in the SPSR 10766 * by setting M[3:2] to 0b10. 10767 * If NV2 is disabled, change SPSR when NV,NV1 == 1,0 (I_ZJRNN) 10768 * If NV2 is enabled, change SPSR when NV is 1 (I_DBTLM) 10769 */ 10770 old_mode = deposit32(old_mode, 2, 2, 2); 10771 } 10772 } 10773 } else { 10774 old_mode = cpsr_read_for_spsr_elx(env); 10775 env->elr_el[new_el] = env->regs[15]; 10776 10777 aarch64_sync_32_to_64(env); 10778 10779 env->condexec_bits = 0; 10780 } 10781 env->banked_spsr[aarch64_banked_spsr_index(new_el)] = old_mode; 10782 10783 qemu_log_mask(CPU_LOG_INT, "...with SPSR 0x%x\n", old_mode); 10784 qemu_log_mask(CPU_LOG_INT, "...with ELR 0x%" PRIx64 "\n", 10785 env->elr_el[new_el]); 10786 10787 if (cpu_isar_feature(aa64_pan, cpu)) { 10788 /* The value of PSTATE.PAN is normally preserved, except when ... */ 10789 new_mode |= old_mode & PSTATE_PAN; 10790 switch (new_el) { 10791 case 2: 10792 /* ... the target is EL2 with HCR_EL2.{E2H,TGE} == '11' ... */ 10793 if ((arm_hcr_el2_eff(env) & (HCR_E2H | HCR_TGE)) 10794 != (HCR_E2H | HCR_TGE)) { 10795 break; 10796 } 10797 /* fall through */ 10798 case 1: 10799 /* ... the target is EL1 ... */ 10800 /* ... and SCTLR_ELx.SPAN == 0, then set to 1. */ 10801 if ((env->cp15.sctlr_el[new_el] & SCTLR_SPAN) == 0) { 10802 new_mode |= PSTATE_PAN; 10803 } 10804 break; 10805 } 10806 } 10807 if (cpu_isar_feature(aa64_mte, cpu)) { 10808 new_mode |= PSTATE_TCO; 10809 } 10810 10811 if (cpu_isar_feature(aa64_ssbs, cpu)) { 10812 if (env->cp15.sctlr_el[new_el] & SCTLR_DSSBS_64) { 10813 new_mode |= PSTATE_SSBS; 10814 } else { 10815 new_mode &= ~PSTATE_SSBS; 10816 } 10817 } 10818 10819 if (cpu_isar_feature(aa64_nmi, cpu)) { 10820 if (!(env->cp15.sctlr_el[new_el] & SCTLR_SPINTMASK)) { 10821 new_mode |= PSTATE_ALLINT; 10822 } else { 10823 new_mode &= ~PSTATE_ALLINT; 10824 } 10825 } 10826 10827 pstate_write(env, PSTATE_DAIF | new_mode); 10828 env->aarch64 = true; 10829 aarch64_restore_sp(env, new_el); 10830 10831 if (tcg_enabled()) { 10832 helper_rebuild_hflags_a64(env, new_el); 10833 } 10834 10835 env->pc = addr; 10836 10837 qemu_log_mask(CPU_LOG_INT, "...to EL%d PC 0x%" PRIx64 " PSTATE 0x%x\n", 10838 new_el, env->pc, pstate_read(env)); 10839 } 10840 10841 /* 10842 * Do semihosting call and set the appropriate return value. All the 10843 * permission and validity checks have been done at translate time. 10844 * 10845 * We only see semihosting exceptions in TCG only as they are not 10846 * trapped to the hypervisor in KVM. 10847 */ 10848 #ifdef CONFIG_TCG 10849 static void tcg_handle_semihosting(CPUState *cs) 10850 { 10851 ARMCPU *cpu = ARM_CPU(cs); 10852 CPUARMState *env = &cpu->env; 10853 10854 if (is_a64(env)) { 10855 qemu_log_mask(CPU_LOG_INT, 10856 "...handling as semihosting call 0x%" PRIx64 "\n", 10857 env->xregs[0]); 10858 do_common_semihosting(cs); 10859 env->pc += 4; 10860 } else { 10861 qemu_log_mask(CPU_LOG_INT, 10862 "...handling as semihosting call 0x%x\n", 10863 env->regs[0]); 10864 do_common_semihosting(cs); 10865 env->regs[15] += env->thumb ? 2 : 4; 10866 } 10867 } 10868 #endif 10869 10870 /* 10871 * Handle a CPU exception for A and R profile CPUs. 10872 * Do any appropriate logging, handle PSCI calls, and then hand off 10873 * to the AArch64-entry or AArch32-entry function depending on the 10874 * target exception level's register width. 10875 * 10876 * Note: this is used for both TCG (as the do_interrupt tcg op), 10877 * and KVM to re-inject guest debug exceptions, and to 10878 * inject a Synchronous-External-Abort. 10879 */ 10880 void arm_cpu_do_interrupt(CPUState *cs) 10881 { 10882 ARMCPU *cpu = ARM_CPU(cs); 10883 CPUARMState *env = &cpu->env; 10884 unsigned int new_el = env->exception.target_el; 10885 10886 assert(!arm_feature(env, ARM_FEATURE_M)); 10887 10888 arm_log_exception(cs); 10889 qemu_log_mask(CPU_LOG_INT, "...from EL%d to EL%d\n", arm_current_el(env), 10890 new_el); 10891 if (qemu_loglevel_mask(CPU_LOG_INT) 10892 && !excp_is_internal(cs->exception_index)) { 10893 qemu_log_mask(CPU_LOG_INT, "...with ESR 0x%x/0x%" PRIx32 "\n", 10894 syn_get_ec(env->exception.syndrome), 10895 env->exception.syndrome); 10896 } 10897 10898 if (tcg_enabled() && arm_is_psci_call(cpu, cs->exception_index)) { 10899 arm_handle_psci_call(cpu); 10900 qemu_log_mask(CPU_LOG_INT, "...handled as PSCI call\n"); 10901 return; 10902 } 10903 10904 /* 10905 * Semihosting semantics depend on the register width of the code 10906 * that caused the exception, not the target exception level, so 10907 * must be handled here. 10908 */ 10909 #ifdef CONFIG_TCG 10910 if (cs->exception_index == EXCP_SEMIHOST) { 10911 tcg_handle_semihosting(cs); 10912 return; 10913 } 10914 #endif 10915 10916 /* 10917 * Hooks may change global state so BQL should be held, also the 10918 * BQL needs to be held for any modification of 10919 * cs->interrupt_request. 10920 */ 10921 g_assert(bql_locked()); 10922 10923 arm_call_pre_el_change_hook(cpu); 10924 10925 assert(!excp_is_internal(cs->exception_index)); 10926 if (arm_el_is_aa64(env, new_el)) { 10927 arm_cpu_do_interrupt_aarch64(cs); 10928 } else { 10929 arm_cpu_do_interrupt_aarch32(cs); 10930 } 10931 10932 arm_call_el_change_hook(cpu); 10933 10934 if (!kvm_enabled()) { 10935 cs->interrupt_request |= CPU_INTERRUPT_EXITTB; 10936 } 10937 } 10938 #endif /* !CONFIG_USER_ONLY */ 10939 10940 uint64_t arm_sctlr(CPUARMState *env, int el) 10941 { 10942 /* Only EL0 needs to be adjusted for EL1&0 or EL2&0 or EL3&0 */ 10943 if (el == 0) { 10944 ARMMMUIdx mmu_idx = arm_mmu_idx_el(env, 0); 10945 switch (mmu_idx) { 10946 case ARMMMUIdx_E20_0: 10947 el = 2; 10948 break; 10949 case ARMMMUIdx_E30_0: 10950 el = 3; 10951 break; 10952 default: 10953 el = 1; 10954 break; 10955 } 10956 } 10957 return env->cp15.sctlr_el[el]; 10958 } 10959 10960 int aa64_va_parameter_tbi(uint64_t tcr, ARMMMUIdx mmu_idx) 10961 { 10962 if (regime_has_2_ranges(mmu_idx)) { 10963 return extract64(tcr, 37, 2); 10964 } else if (regime_is_stage2(mmu_idx)) { 10965 return 0; /* VTCR_EL2 */ 10966 } else { 10967 /* Replicate the single TBI bit so we always have 2 bits. */ 10968 return extract32(tcr, 20, 1) * 3; 10969 } 10970 } 10971 10972 int aa64_va_parameter_tbid(uint64_t tcr, ARMMMUIdx mmu_idx) 10973 { 10974 if (regime_has_2_ranges(mmu_idx)) { 10975 return extract64(tcr, 51, 2); 10976 } else if (regime_is_stage2(mmu_idx)) { 10977 return 0; /* VTCR_EL2 */ 10978 } else { 10979 /* Replicate the single TBID bit so we always have 2 bits. */ 10980 return extract32(tcr, 29, 1) * 3; 10981 } 10982 } 10983 10984 int aa64_va_parameter_tcma(uint64_t tcr, ARMMMUIdx mmu_idx) 10985 { 10986 if (regime_has_2_ranges(mmu_idx)) { 10987 return extract64(tcr, 57, 2); 10988 } else { 10989 /* Replicate the single TCMA bit so we always have 2 bits. */ 10990 return extract32(tcr, 30, 1) * 3; 10991 } 10992 } 10993 10994 static ARMGranuleSize tg0_to_gran_size(int tg) 10995 { 10996 switch (tg) { 10997 case 0: 10998 return Gran4K; 10999 case 1: 11000 return Gran64K; 11001 case 2: 11002 return Gran16K; 11003 default: 11004 return GranInvalid; 11005 } 11006 } 11007 11008 static ARMGranuleSize tg1_to_gran_size(int tg) 11009 { 11010 switch (tg) { 11011 case 1: 11012 return Gran16K; 11013 case 2: 11014 return Gran4K; 11015 case 3: 11016 return Gran64K; 11017 default: 11018 return GranInvalid; 11019 } 11020 } 11021 11022 static inline bool have4k(ARMCPU *cpu, bool stage2) 11023 { 11024 return stage2 ? cpu_isar_feature(aa64_tgran4_2, cpu) 11025 : cpu_isar_feature(aa64_tgran4, cpu); 11026 } 11027 11028 static inline bool have16k(ARMCPU *cpu, bool stage2) 11029 { 11030 return stage2 ? cpu_isar_feature(aa64_tgran16_2, cpu) 11031 : cpu_isar_feature(aa64_tgran16, cpu); 11032 } 11033 11034 static inline bool have64k(ARMCPU *cpu, bool stage2) 11035 { 11036 return stage2 ? cpu_isar_feature(aa64_tgran64_2, cpu) 11037 : cpu_isar_feature(aa64_tgran64, cpu); 11038 } 11039 11040 static ARMGranuleSize sanitize_gran_size(ARMCPU *cpu, ARMGranuleSize gran, 11041 bool stage2) 11042 { 11043 switch (gran) { 11044 case Gran4K: 11045 if (have4k(cpu, stage2)) { 11046 return gran; 11047 } 11048 break; 11049 case Gran16K: 11050 if (have16k(cpu, stage2)) { 11051 return gran; 11052 } 11053 break; 11054 case Gran64K: 11055 if (have64k(cpu, stage2)) { 11056 return gran; 11057 } 11058 break; 11059 case GranInvalid: 11060 break; 11061 } 11062 /* 11063 * If the guest selects a granule size that isn't implemented, 11064 * the architecture requires that we behave as if it selected one 11065 * that is (with an IMPDEF choice of which one to pick). We choose 11066 * to implement the smallest supported granule size. 11067 */ 11068 if (have4k(cpu, stage2)) { 11069 return Gran4K; 11070 } 11071 if (have16k(cpu, stage2)) { 11072 return Gran16K; 11073 } 11074 assert(have64k(cpu, stage2)); 11075 return Gran64K; 11076 } 11077 11078 ARMVAParameters aa64_va_parameters(CPUARMState *env, uint64_t va, 11079 ARMMMUIdx mmu_idx, bool data, 11080 bool el1_is_aa32) 11081 { 11082 uint64_t tcr = regime_tcr(env, mmu_idx); 11083 bool epd, hpd, tsz_oob, ds, ha, hd; 11084 int select, tsz, tbi, max_tsz, min_tsz, ps, sh; 11085 ARMGranuleSize gran; 11086 ARMCPU *cpu = env_archcpu(env); 11087 bool stage2 = regime_is_stage2(mmu_idx); 11088 11089 if (!regime_has_2_ranges(mmu_idx)) { 11090 select = 0; 11091 tsz = extract32(tcr, 0, 6); 11092 gran = tg0_to_gran_size(extract32(tcr, 14, 2)); 11093 if (stage2) { 11094 /* VTCR_EL2 */ 11095 hpd = false; 11096 } else { 11097 hpd = extract32(tcr, 24, 1); 11098 } 11099 epd = false; 11100 sh = extract32(tcr, 12, 2); 11101 ps = extract32(tcr, 16, 3); 11102 ha = extract32(tcr, 21, 1) && cpu_isar_feature(aa64_hafs, cpu); 11103 hd = extract32(tcr, 22, 1) && cpu_isar_feature(aa64_hdbs, cpu); 11104 ds = extract64(tcr, 32, 1); 11105 } else { 11106 bool e0pd; 11107 11108 /* 11109 * Bit 55 is always between the two regions, and is canonical for 11110 * determining if address tagging is enabled. 11111 */ 11112 select = extract64(va, 55, 1); 11113 if (!select) { 11114 tsz = extract32(tcr, 0, 6); 11115 gran = tg0_to_gran_size(extract32(tcr, 14, 2)); 11116 epd = extract32(tcr, 7, 1); 11117 sh = extract32(tcr, 12, 2); 11118 hpd = extract64(tcr, 41, 1); 11119 e0pd = extract64(tcr, 55, 1); 11120 } else { 11121 tsz = extract32(tcr, 16, 6); 11122 gran = tg1_to_gran_size(extract32(tcr, 30, 2)); 11123 epd = extract32(tcr, 23, 1); 11124 sh = extract32(tcr, 28, 2); 11125 hpd = extract64(tcr, 42, 1); 11126 e0pd = extract64(tcr, 56, 1); 11127 } 11128 ps = extract64(tcr, 32, 3); 11129 ha = extract64(tcr, 39, 1) && cpu_isar_feature(aa64_hafs, cpu); 11130 hd = extract64(tcr, 40, 1) && cpu_isar_feature(aa64_hdbs, cpu); 11131 ds = extract64(tcr, 59, 1); 11132 11133 if (e0pd && cpu_isar_feature(aa64_e0pd, cpu) && 11134 regime_is_user(env, mmu_idx)) { 11135 epd = true; 11136 } 11137 } 11138 11139 gran = sanitize_gran_size(cpu, gran, stage2); 11140 11141 if (cpu_isar_feature(aa64_st, cpu)) { 11142 max_tsz = 48 - (gran == Gran64K); 11143 } else { 11144 max_tsz = 39; 11145 } 11146 11147 /* 11148 * DS is RES0 unless FEAT_LPA2 is supported for the given page size; 11149 * adjust the effective value of DS, as documented. 11150 */ 11151 min_tsz = 16; 11152 if (gran == Gran64K) { 11153 if (cpu_isar_feature(aa64_lva, cpu)) { 11154 min_tsz = 12; 11155 } 11156 ds = false; 11157 } else if (ds) { 11158 if (regime_is_stage2(mmu_idx)) { 11159 if (gran == Gran16K) { 11160 ds = cpu_isar_feature(aa64_tgran16_2_lpa2, cpu); 11161 } else { 11162 ds = cpu_isar_feature(aa64_tgran4_2_lpa2, cpu); 11163 } 11164 } else { 11165 if (gran == Gran16K) { 11166 ds = cpu_isar_feature(aa64_tgran16_lpa2, cpu); 11167 } else { 11168 ds = cpu_isar_feature(aa64_tgran4_lpa2, cpu); 11169 } 11170 } 11171 if (ds) { 11172 min_tsz = 12; 11173 } 11174 } 11175 11176 if (stage2 && el1_is_aa32) { 11177 /* 11178 * For AArch32 EL1 the min txsz (and thus max IPA size) requirements 11179 * are loosened: a configured IPA of 40 bits is permitted even if 11180 * the implemented PA is less than that (and so a 40 bit IPA would 11181 * fault for an AArch64 EL1). See R_DTLMN. 11182 */ 11183 min_tsz = MIN(min_tsz, 24); 11184 } 11185 11186 if (tsz > max_tsz) { 11187 tsz = max_tsz; 11188 tsz_oob = true; 11189 } else if (tsz < min_tsz) { 11190 tsz = min_tsz; 11191 tsz_oob = true; 11192 } else { 11193 tsz_oob = false; 11194 } 11195 11196 /* Present TBI as a composite with TBID. */ 11197 tbi = aa64_va_parameter_tbi(tcr, mmu_idx); 11198 if (!data) { 11199 tbi &= ~aa64_va_parameter_tbid(tcr, mmu_idx); 11200 } 11201 tbi = (tbi >> select) & 1; 11202 11203 return (ARMVAParameters) { 11204 .tsz = tsz, 11205 .ps = ps, 11206 .sh = sh, 11207 .select = select, 11208 .tbi = tbi, 11209 .epd = epd, 11210 .hpd = hpd, 11211 .tsz_oob = tsz_oob, 11212 .ds = ds, 11213 .ha = ha, 11214 .hd = ha && hd, 11215 .gran = gran, 11216 }; 11217 } 11218 11219 11220 /* 11221 * Return the exception level to which FP-disabled exceptions should 11222 * be taken, or 0 if FP is enabled. 11223 */ 11224 int fp_exception_el(CPUARMState *env, int cur_el) 11225 { 11226 #ifndef CONFIG_USER_ONLY 11227 uint64_t hcr_el2; 11228 11229 /* 11230 * CPACR and the CPTR registers don't exist before v6, so FP is 11231 * always accessible 11232 */ 11233 if (!arm_feature(env, ARM_FEATURE_V6)) { 11234 return 0; 11235 } 11236 11237 if (arm_feature(env, ARM_FEATURE_M)) { 11238 /* CPACR can cause a NOCP UsageFault taken to current security state */ 11239 if (!v7m_cpacr_pass(env, env->v7m.secure, cur_el != 0)) { 11240 return 1; 11241 } 11242 11243 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && !env->v7m.secure) { 11244 if (!extract32(env->v7m.nsacr, 10, 1)) { 11245 /* FP insns cause a NOCP UsageFault taken to Secure */ 11246 return 3; 11247 } 11248 } 11249 11250 return 0; 11251 } 11252 11253 hcr_el2 = arm_hcr_el2_eff(env); 11254 11255 /* 11256 * The CPACR controls traps to EL1, or PL1 if we're 32 bit: 11257 * 0, 2 : trap EL0 and EL1/PL1 accesses 11258 * 1 : trap only EL0 accesses 11259 * 3 : trap no accesses 11260 * This register is ignored if E2H+TGE are both set. 11261 */ 11262 if ((hcr_el2 & (HCR_E2H | HCR_TGE)) != (HCR_E2H | HCR_TGE)) { 11263 int fpen = FIELD_EX64(env->cp15.cpacr_el1, CPACR_EL1, FPEN); 11264 11265 switch (fpen) { 11266 case 1: 11267 if (cur_el != 0) { 11268 break; 11269 } 11270 /* fall through */ 11271 case 0: 11272 case 2: 11273 /* Trap from Secure PL0 or PL1 to Secure PL1. */ 11274 if (!arm_el_is_aa64(env, 3) 11275 && (cur_el == 3 || arm_is_secure_below_el3(env))) { 11276 return 3; 11277 } 11278 if (cur_el <= 1) { 11279 return 1; 11280 } 11281 break; 11282 } 11283 } 11284 11285 /* 11286 * The NSACR allows A-profile AArch32 EL3 and M-profile secure mode 11287 * to control non-secure access to the FPU. It doesn't have any 11288 * effect if EL3 is AArch64 or if EL3 doesn't exist at all. 11289 */ 11290 if ((arm_feature(env, ARM_FEATURE_EL3) && !arm_el_is_aa64(env, 3) && 11291 cur_el <= 2 && !arm_is_secure_below_el3(env))) { 11292 if (!extract32(env->cp15.nsacr, 10, 1)) { 11293 /* FP insns act as UNDEF */ 11294 return cur_el == 2 ? 2 : 1; 11295 } 11296 } 11297 11298 /* 11299 * CPTR_EL2 is present in v7VE or v8, and changes format 11300 * with HCR_EL2.E2H (regardless of TGE). 11301 */ 11302 if (cur_el <= 2) { 11303 if (hcr_el2 & HCR_E2H) { 11304 switch (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, FPEN)) { 11305 case 1: 11306 if (cur_el != 0 || !(hcr_el2 & HCR_TGE)) { 11307 break; 11308 } 11309 /* fall through */ 11310 case 0: 11311 case 2: 11312 return 2; 11313 } 11314 } else if (arm_is_el2_enabled(env)) { 11315 if (FIELD_EX64(env->cp15.cptr_el[2], CPTR_EL2, TFP)) { 11316 return 2; 11317 } 11318 } 11319 } 11320 11321 /* CPTR_EL3 : present in v8 */ 11322 if (FIELD_EX64(env->cp15.cptr_el[3], CPTR_EL3, TFP)) { 11323 /* Trap all FP ops to EL3 */ 11324 return 3; 11325 } 11326 #endif 11327 return 0; 11328 } 11329 11330 /* Return the exception level we're running at if this is our mmu_idx */ 11331 int arm_mmu_idx_to_el(ARMMMUIdx mmu_idx) 11332 { 11333 if (mmu_idx & ARM_MMU_IDX_M) { 11334 return mmu_idx & ARM_MMU_IDX_M_PRIV; 11335 } 11336 11337 switch (mmu_idx) { 11338 case ARMMMUIdx_E10_0: 11339 case ARMMMUIdx_E20_0: 11340 case ARMMMUIdx_E30_0: 11341 return 0; 11342 case ARMMMUIdx_E10_1: 11343 case ARMMMUIdx_E10_1_PAN: 11344 return 1; 11345 case ARMMMUIdx_E2: 11346 case ARMMMUIdx_E20_2: 11347 case ARMMMUIdx_E20_2_PAN: 11348 return 2; 11349 case ARMMMUIdx_E3: 11350 case ARMMMUIdx_E30_3_PAN: 11351 return 3; 11352 default: 11353 g_assert_not_reached(); 11354 } 11355 } 11356 11357 #ifndef CONFIG_TCG 11358 ARMMMUIdx arm_v7m_mmu_idx_for_secstate(CPUARMState *env, bool secstate) 11359 { 11360 g_assert_not_reached(); 11361 } 11362 #endif 11363 11364 ARMMMUIdx arm_mmu_idx_el(CPUARMState *env, int el) 11365 { 11366 ARMMMUIdx idx; 11367 uint64_t hcr; 11368 11369 if (arm_feature(env, ARM_FEATURE_M)) { 11370 return arm_v7m_mmu_idx_for_secstate(env, env->v7m.secure); 11371 } 11372 11373 /* See ARM pseudo-function ELIsInHost. */ 11374 switch (el) { 11375 case 0: 11376 hcr = arm_hcr_el2_eff(env); 11377 if ((hcr & (HCR_E2H | HCR_TGE)) == (HCR_E2H | HCR_TGE)) { 11378 idx = ARMMMUIdx_E20_0; 11379 } else if (arm_is_secure_below_el3(env) && 11380 !arm_el_is_aa64(env, 3)) { 11381 idx = ARMMMUIdx_E30_0; 11382 } else { 11383 idx = ARMMMUIdx_E10_0; 11384 } 11385 break; 11386 case 1: 11387 if (arm_pan_enabled(env)) { 11388 idx = ARMMMUIdx_E10_1_PAN; 11389 } else { 11390 idx = ARMMMUIdx_E10_1; 11391 } 11392 break; 11393 case 2: 11394 /* Note that TGE does not apply at EL2. */ 11395 if (arm_hcr_el2_eff(env) & HCR_E2H) { 11396 if (arm_pan_enabled(env)) { 11397 idx = ARMMMUIdx_E20_2_PAN; 11398 } else { 11399 idx = ARMMMUIdx_E20_2; 11400 } 11401 } else { 11402 idx = ARMMMUIdx_E2; 11403 } 11404 break; 11405 case 3: 11406 if (!arm_el_is_aa64(env, 3) && arm_pan_enabled(env)) { 11407 return ARMMMUIdx_E30_3_PAN; 11408 } 11409 return ARMMMUIdx_E3; 11410 default: 11411 g_assert_not_reached(); 11412 } 11413 11414 return idx; 11415 } 11416 11417 ARMMMUIdx arm_mmu_idx(CPUARMState *env) 11418 { 11419 return arm_mmu_idx_el(env, arm_current_el(env)); 11420 } 11421 11422 static bool mve_no_pred(CPUARMState *env) 11423 { 11424 /* 11425 * Return true if there is definitely no predication of MVE 11426 * instructions by VPR or LTPSIZE. (Returning false even if there 11427 * isn't any predication is OK; generated code will just be 11428 * a little worse.) 11429 * If the CPU does not implement MVE then this TB flag is always 0. 11430 * 11431 * NOTE: if you change this logic, the "recalculate s->mve_no_pred" 11432 * logic in gen_update_fp_context() needs to be updated to match. 11433 * 11434 * We do not include the effect of the ECI bits here -- they are 11435 * tracked in other TB flags. This simplifies the logic for 11436 * "when did we emit code that changes the MVE_NO_PRED TB flag 11437 * and thus need to end the TB?". 11438 */ 11439 if (cpu_isar_feature(aa32_mve, env_archcpu(env))) { 11440 return false; 11441 } 11442 if (env->v7m.vpr) { 11443 return false; 11444 } 11445 if (env->v7m.ltpsize < 4) { 11446 return false; 11447 } 11448 return true; 11449 } 11450 11451 void cpu_get_tb_cpu_state(CPUARMState *env, vaddr *pc, 11452 uint64_t *cs_base, uint32_t *pflags) 11453 { 11454 CPUARMTBFlags flags; 11455 11456 assert_hflags_rebuild_correctly(env); 11457 flags = env->hflags; 11458 11459 if (EX_TBFLAG_ANY(flags, AARCH64_STATE)) { 11460 *pc = env->pc; 11461 if (cpu_isar_feature(aa64_bti, env_archcpu(env))) { 11462 DP_TBFLAG_A64(flags, BTYPE, env->btype); 11463 } 11464 } else { 11465 *pc = env->regs[15]; 11466 11467 if (arm_feature(env, ARM_FEATURE_M)) { 11468 if (arm_feature(env, ARM_FEATURE_M_SECURITY) && 11469 FIELD_EX32(env->v7m.fpccr[M_REG_S], V7M_FPCCR, S) 11470 != env->v7m.secure) { 11471 DP_TBFLAG_M32(flags, FPCCR_S_WRONG, 1); 11472 } 11473 11474 if ((env->v7m.fpccr[env->v7m.secure] & R_V7M_FPCCR_ASPEN_MASK) && 11475 (!(env->v7m.control[M_REG_S] & R_V7M_CONTROL_FPCA_MASK) || 11476 (env->v7m.secure && 11477 !(env->v7m.control[M_REG_S] & R_V7M_CONTROL_SFPA_MASK)))) { 11478 /* 11479 * ASPEN is set, but FPCA/SFPA indicate that there is no 11480 * active FP context; we must create a new FP context before 11481 * executing any FP insn. 11482 */ 11483 DP_TBFLAG_M32(flags, NEW_FP_CTXT_NEEDED, 1); 11484 } 11485 11486 bool is_secure = env->v7m.fpccr[M_REG_S] & R_V7M_FPCCR_S_MASK; 11487 if (env->v7m.fpccr[is_secure] & R_V7M_FPCCR_LSPACT_MASK) { 11488 DP_TBFLAG_M32(flags, LSPACT, 1); 11489 } 11490 11491 if (mve_no_pred(env)) { 11492 DP_TBFLAG_M32(flags, MVE_NO_PRED, 1); 11493 } 11494 } else { 11495 /* 11496 * Note that XSCALE_CPAR shares bits with VECSTRIDE. 11497 * Note that VECLEN+VECSTRIDE are RES0 for M-profile. 11498 */ 11499 if (arm_feature(env, ARM_FEATURE_XSCALE)) { 11500 DP_TBFLAG_A32(flags, XSCALE_CPAR, env->cp15.c15_cpar); 11501 } else { 11502 DP_TBFLAG_A32(flags, VECLEN, env->vfp.vec_len); 11503 DP_TBFLAG_A32(flags, VECSTRIDE, env->vfp.vec_stride); 11504 } 11505 if (env->vfp.xregs[ARM_VFP_FPEXC] & (1 << 30)) { 11506 DP_TBFLAG_A32(flags, VFPEN, 1); 11507 } 11508 } 11509 11510 DP_TBFLAG_AM32(flags, THUMB, env->thumb); 11511 DP_TBFLAG_AM32(flags, CONDEXEC, env->condexec_bits); 11512 } 11513 11514 /* 11515 * The SS_ACTIVE and PSTATE_SS bits correspond to the state machine 11516 * states defined in the ARM ARM for software singlestep: 11517 * SS_ACTIVE PSTATE.SS State 11518 * 0 x Inactive (the TB flag for SS is always 0) 11519 * 1 0 Active-pending 11520 * 1 1 Active-not-pending 11521 * SS_ACTIVE is set in hflags; PSTATE__SS is computed every TB. 11522 */ 11523 if (EX_TBFLAG_ANY(flags, SS_ACTIVE) && (env->pstate & PSTATE_SS)) { 11524 DP_TBFLAG_ANY(flags, PSTATE__SS, 1); 11525 } 11526 11527 *pflags = flags.flags; 11528 *cs_base = flags.flags2; 11529 } 11530 11531 #ifdef TARGET_AARCH64 11532 /* 11533 * The manual says that when SVE is enabled and VQ is widened the 11534 * implementation is allowed to zero the previously inaccessible 11535 * portion of the registers. The corollary to that is that when 11536 * SVE is enabled and VQ is narrowed we are also allowed to zero 11537 * the now inaccessible portion of the registers. 11538 * 11539 * The intent of this is that no predicate bit beyond VQ is ever set. 11540 * Which means that some operations on predicate registers themselves 11541 * may operate on full uint64_t or even unrolled across the maximum 11542 * uint64_t[4]. Performing 4 bits of host arithmetic unconditionally 11543 * may well be cheaper than conditionals to restrict the operation 11544 * to the relevant portion of a uint16_t[16]. 11545 */ 11546 void aarch64_sve_narrow_vq(CPUARMState *env, unsigned vq) 11547 { 11548 int i, j; 11549 uint64_t pmask; 11550 11551 assert(vq >= 1 && vq <= ARM_MAX_VQ); 11552 assert(vq <= env_archcpu(env)->sve_max_vq); 11553 11554 /* Zap the high bits of the zregs. */ 11555 for (i = 0; i < 32; i++) { 11556 memset(&env->vfp.zregs[i].d[2 * vq], 0, 16 * (ARM_MAX_VQ - vq)); 11557 } 11558 11559 /* Zap the high bits of the pregs and ffr. */ 11560 pmask = 0; 11561 if (vq & 3) { 11562 pmask = ~(-1ULL << (16 * (vq & 3))); 11563 } 11564 for (j = vq / 4; j < ARM_MAX_VQ / 4; j++) { 11565 for (i = 0; i < 17; ++i) { 11566 env->vfp.pregs[i].p[j] &= pmask; 11567 } 11568 pmask = 0; 11569 } 11570 } 11571 11572 static uint32_t sve_vqm1_for_el_sm_ena(CPUARMState *env, int el, bool sm) 11573 { 11574 int exc_el; 11575 11576 if (sm) { 11577 exc_el = sme_exception_el(env, el); 11578 } else { 11579 exc_el = sve_exception_el(env, el); 11580 } 11581 if (exc_el) { 11582 return 0; /* disabled */ 11583 } 11584 return sve_vqm1_for_el_sm(env, el, sm); 11585 } 11586 11587 /* 11588 * Notice a change in SVE vector size when changing EL. 11589 */ 11590 void aarch64_sve_change_el(CPUARMState *env, int old_el, 11591 int new_el, bool el0_a64) 11592 { 11593 ARMCPU *cpu = env_archcpu(env); 11594 int old_len, new_len; 11595 bool old_a64, new_a64, sm; 11596 11597 /* Nothing to do if no SVE. */ 11598 if (!cpu_isar_feature(aa64_sve, cpu)) { 11599 return; 11600 } 11601 11602 /* Nothing to do if FP is disabled in either EL. */ 11603 if (fp_exception_el(env, old_el) || fp_exception_el(env, new_el)) { 11604 return; 11605 } 11606 11607 old_a64 = old_el ? arm_el_is_aa64(env, old_el) : el0_a64; 11608 new_a64 = new_el ? arm_el_is_aa64(env, new_el) : el0_a64; 11609 11610 /* 11611 * Both AArch64.TakeException and AArch64.ExceptionReturn 11612 * invoke ResetSVEState when taking an exception from, or 11613 * returning to, AArch32 state when PSTATE.SM is enabled. 11614 */ 11615 sm = FIELD_EX64(env->svcr, SVCR, SM); 11616 if (old_a64 != new_a64 && sm) { 11617 arm_reset_sve_state(env); 11618 return; 11619 } 11620 11621 /* 11622 * DDI0584A.d sec 3.2: "If SVE instructions are disabled or trapped 11623 * at ELx, or not available because the EL is in AArch32 state, then 11624 * for all purposes other than a direct read, the ZCR_ELx.LEN field 11625 * has an effective value of 0". 11626 * 11627 * Consider EL2 (aa64, vq=4) -> EL0 (aa32) -> EL1 (aa64, vq=0). 11628 * If we ignore aa32 state, we would fail to see the vq4->vq0 transition 11629 * from EL2->EL1. Thus we go ahead and narrow when entering aa32 so that 11630 * we already have the correct register contents when encountering the 11631 * vq0->vq0 transition between EL0->EL1. 11632 */ 11633 old_len = new_len = 0; 11634 if (old_a64) { 11635 old_len = sve_vqm1_for_el_sm_ena(env, old_el, sm); 11636 } 11637 if (new_a64) { 11638 new_len = sve_vqm1_for_el_sm_ena(env, new_el, sm); 11639 } 11640 11641 /* When changing vector length, clear inaccessible state. */ 11642 if (new_len < old_len) { 11643 aarch64_sve_narrow_vq(env, new_len + 1); 11644 } 11645 } 11646 #endif 11647 11648 #ifndef CONFIG_USER_ONLY 11649 ARMSecuritySpace arm_security_space(CPUARMState *env) 11650 { 11651 if (arm_feature(env, ARM_FEATURE_M)) { 11652 return arm_secure_to_space(env->v7m.secure); 11653 } 11654 11655 /* 11656 * If EL3 is not supported then the secure state is implementation 11657 * defined, in which case QEMU defaults to non-secure. 11658 */ 11659 if (!arm_feature(env, ARM_FEATURE_EL3)) { 11660 return ARMSS_NonSecure; 11661 } 11662 11663 /* Check for AArch64 EL3 or AArch32 Mon. */ 11664 if (is_a64(env)) { 11665 if (extract32(env->pstate, 2, 2) == 3) { 11666 if (cpu_isar_feature(aa64_rme, env_archcpu(env))) { 11667 return ARMSS_Root; 11668 } else { 11669 return ARMSS_Secure; 11670 } 11671 } 11672 } else { 11673 if ((env->uncached_cpsr & CPSR_M) == ARM_CPU_MODE_MON) { 11674 return ARMSS_Secure; 11675 } 11676 } 11677 11678 return arm_security_space_below_el3(env); 11679 } 11680 11681 ARMSecuritySpace arm_security_space_below_el3(CPUARMState *env) 11682 { 11683 assert(!arm_feature(env, ARM_FEATURE_M)); 11684 11685 /* 11686 * If EL3 is not supported then the secure state is implementation 11687 * defined, in which case QEMU defaults to non-secure. 11688 */ 11689 if (!arm_feature(env, ARM_FEATURE_EL3)) { 11690 return ARMSS_NonSecure; 11691 } 11692 11693 /* 11694 * Note NSE cannot be set without RME, and NSE & !NS is Reserved. 11695 * Ignoring NSE when !NS retains consistency without having to 11696 * modify other predicates. 11697 */ 11698 if (!(env->cp15.scr_el3 & SCR_NS)) { 11699 return ARMSS_Secure; 11700 } else if (env->cp15.scr_el3 & SCR_NSE) { 11701 return ARMSS_Realm; 11702 } else { 11703 return ARMSS_NonSecure; 11704 } 11705 } 11706 #endif /* !CONFIG_USER_ONLY */ 11707